| 1 |
|
| 2 |
#include <stdio.h> |
| 3 |
#include <winpr/crt.h> |
| 4 |
#include <winpr/file.h> |
| 5 |
#include <winpr/path.h> |
| 6 |
#include <winpr/tchar.h> |
| 7 |
#include <winpr/windows.h> |
| 8 |
|
| 9 |
#define TEST_MAX_PATH 4096 |
| 10 |
|
| 11 |
static TCHAR testDirectory2File1[] = _T("TestDirectory2File1"); |
| 12 |
static TCHAR testDirectory2File2[] = _T("TestDirectory2File2"); |
| 13 |
|
| 14 |
int TestFileFindNextFile(int argc, char* argv[]) |
| 15 |
{ |
| 16 |
char* str = nullptr; |
| 17 |
size_t length = 0; |
| 18 |
BOOL status = 0; |
| 19 |
HANDLE hFind = nullptr; |
| 20 |
LPTSTR BasePath = nullptr; |
| 21 |
WIN32_FIND_DATA FindData; |
| 22 |
TCHAR FilePath[TEST_MAX_PATH] = WINPR_C_ARRAY_INIT; |
| 23 |
WINPR_UNUSED(argc); |
| 24 |
str = argv[1]; |
| 25 |
#ifdef UNICODE |
| 26 |
BasePath = ConvertUtf8ToWChar(str, &length); |
| 27 |
|
| 28 |
if (!BasePath) |
| 29 |
{ |
| 30 |
_tprintf(_T("Unable to allocate memory")); |
| 31 |
return -1; |
| 32 |
} |
| 33 |
#else |
| 34 |
BasePath = _strdup(str); |
| 35 |
|
| 36 |
if (!BasePath) |
| 37 |
{ |
| 38 |
printf("Unable to allocate memory"); |
| 39 |
return -1; |
| 40 |
} |
| 41 |
|
| 42 |
length = strlen(BasePath); |
| 43 |
#endif |
| 44 |
/* Simple filter matching all files inside current directory */ |
| 45 |
CopyMemory(FilePath, BasePath, length * sizeof(TCHAR)); |
| 46 |
FilePath[length] = 0; |
| 47 |
if (FAILED(PathCchConvertStyle(BasePath, length, PATH_STYLE_WINDOWS))) |
| 48 |
return -1; |
| 49 |
if (FAILED(NativePathCchAppend(FilePath, ARRAYSIZE(FilePath), _T("TestDirectory2")))) |
| 50 |
return -1; |
| 51 |
if (FAILED(NativePathCchAppend(FilePath, ARRAYSIZE(FilePath), _T("TestDirectory2File*")))) |
| 52 |
return -1; |
| 53 |
free(BasePath); |
| 54 |
_tprintf(_T("Finding file: %s\n"), FilePath); |
| 55 |
hFind = FindFirstFile(FilePath, &FindData); |
| 56 |
|
| 57 |
if (hFind == INVALID_HANDLE_VALUE) |
| 58 |
{ |
| 59 |
_tprintf(_T("FindFirstFile failure: %s\n"), FilePath); |
| 60 |
return -1; |
| 61 |
} |
| 62 |
|
| 63 |
_tprintf(_T("FindFirstFile: %s"), FindData.cFileName); |
| 64 |
|
| 65 |
/** |
| 66 |
* The current implementation does not enforce a particular order |
| 67 |
*/ |
| 68 |
|
| 69 |
if ((_tcsncmp(FindData.cFileName, testDirectory2File1, ARRAYSIZE(testDirectory2File1)) != 0) && |
| 70 |
(_tcsncmp(FindData.cFileName, testDirectory2File2, ARRAYSIZE(testDirectory2File2)) != 0)) |
| 71 |
{ |
| 72 |
_tprintf(_T("FindFirstFile failure: Expected: %s, Actual: %s\n"), testDirectory2File1, |
| 73 |
FindData.cFileName); |
| 74 |
return -1; |
| 75 |
} |
| 76 |
|
| 77 |
status = FindNextFile(hFind, &FindData); |
| 78 |
|
| 79 |
if (!status) |
| 80 |
{ |
| 81 |
_tprintf(_T("FindNextFile failure: Expected: TRUE, Actual: %") _T(PRId32) _T("\n"), status); |
| 82 |
return -1; |
| 83 |
} |
| 84 |
|
| 85 |
if ((_tcsncmp(FindData.cFileName, testDirectory2File1, ARRAYSIZE(testDirectory2File1)) != 0) && |
| 86 |
(_tcsncmp(FindData.cFileName, testDirectory2File2, ARRAYSIZE(testDirectory2File2)) != 0)) |
| 87 |
{ |
| 88 |
_tprintf(_T("FindNextFile failure: Expected: %s, Actual: %s\n"), testDirectory2File2, |
| 89 |
FindData.cFileName); |
| 90 |
return -1; |
| 91 |
} |
| 92 |
|
| 93 |
status = FindNextFile(hFind, &FindData); |
| 94 |
|
| 95 |
if (status) |
| 96 |
{ |
| 97 |
_tprintf(_T("FindNextFile failure: Expected: FALSE, Actual: %") _T(PRId32) _T("\n"), |
| 98 |
status); |
| 99 |
return -1; |
| 100 |
} |
| 101 |
|
| 102 |
FindClose(hFind); |
| 103 |
return 0; |
| 104 |
} |