| 1 |
|
| 2 |
#include <stdio.h> |
| 3 |
#include <winpr/crt.h> |
| 4 |
#include <winpr/handle.h> |
| 5 |
#include <winpr/file.h> |
| 6 |
#include <winpr/path.h> |
| 7 |
#include <winpr/tchar.h> |
| 8 |
#include <winpr/collections.h> |
| 9 |
#include <winpr/windows.h> |
| 10 |
|
| 11 |
#define TEST_MAX_PATH 4096 |
| 12 |
|
| 13 |
static const CHAR testFile1A[] = "TestFile1A"; |
| 14 |
|
| 15 |
static BOOL create_fileA(const char* FilePath) |
| 16 |
{ |
| 17 |
HANDLE hdl = winpr_CreateFile(FilePath, GENERIC_ALL, 0, nullptr, CREATE_ALWAYS, |
| 18 |
FILE_ATTRIBUTE_NORMAL, nullptr); |
| 19 |
if (hdl == INVALID_HANDLE_VALUE) |
| 20 |
return FALSE; |
| 21 |
(void)CloseHandle(hdl); |
| 22 |
return TRUE; |
| 23 |
} |
| 24 |
|
| 25 |
static BOOL create_fileW(const WCHAR* FilePath) |
| 26 |
{ |
| 27 |
HANDLE hdl = CreateFileW(FilePath, GENERIC_ALL, 0, nullptr, CREATE_ALWAYS, |
| 28 |
FILE_ATTRIBUTE_NORMAL, nullptr); |
| 29 |
if (hdl == INVALID_HANDLE_VALUE) |
| 30 |
return FALSE; |
| 31 |
(void)CloseHandle(hdl); |
| 32 |
return TRUE; |
| 33 |
} |
| 34 |
|
| 35 |
static BOOL create_layout_files(size_t level, const char* BasePath, wArrayList* files) |
| 36 |
{ |
| 37 |
BOOL rc = TRUE; |
| 38 |
for (size_t x = 0; x < 10; x++) |
| 39 |
{ |
| 40 |
CHAR FilePath[TEST_MAX_PATH] = WINPR_C_ARRAY_INIT; |
| 41 |
strncpy(FilePath, BasePath, ARRAYSIZE(FilePath)); |
| 42 |
|
| 43 |
CHAR name[64] = WINPR_C_ARRAY_INIT; |
| 44 |
(void)_snprintf(name, ARRAYSIZE(name), "%zd-TestFile%zd", level, x); |
| 45 |
if (FAILED(NativePathCchAppendA(FilePath, TEST_MAX_PATH, name))) |
| 46 |
rc = FALSE; |
| 47 |
|
| 48 |
if (create_fileA(FilePath)) |
| 49 |
{ |
| 50 |
if (!ArrayList_Append(files, FilePath)) |
| 51 |
rc = FALSE; |
| 52 |
} |
| 53 |
} |
| 54 |
return rc; |
| 55 |
} |
| 56 |
|
| 57 |
static BOOL create_layout_directories(size_t level, size_t max_level, const char* BasePath, |
| 58 |
wArrayList* files) |
| 59 |
{ |
| 60 |
if (level >= max_level) |
| 61 |
return TRUE; |
| 62 |
|
| 63 |
CHAR FilePath[TEST_MAX_PATH] = WINPR_C_ARRAY_INIT; |
| 64 |
strncpy(FilePath, BasePath, ARRAYSIZE(FilePath)); |
| 65 |
if (FAILED(PathCchConvertStyleA(FilePath, ARRAYSIZE(FilePath), PATH_STYLE_NATIVE))) |
| 66 |
return FALSE; |
| 67 |
if (!winpr_PathMakePath(FilePath, nullptr)) |
| 68 |
return FALSE; |
| 69 |
ArrayList_Append(files, FilePath); |
| 70 |
|
| 71 |
if (!create_layout_files(level + 1, BasePath, files)) |
| 72 |
return FALSE; |
| 73 |
|
| 74 |
for (size_t x = 0; x < 10; x++) |
| 75 |
{ |
| 76 |
CHAR CurFilePath[TEST_MAX_PATH] = WINPR_C_ARRAY_INIT; |
| 77 |
strncpy(CurFilePath, FilePath, ARRAYSIZE(CurFilePath)); |
| 78 |
|
| 79 |
if (FAILED(PathCchConvertStyleA(CurFilePath, ARRAYSIZE(CurFilePath), PATH_STYLE_NATIVE))) |
| 80 |
return FALSE; |
| 81 |
|
| 82 |
CHAR name[64] = WINPR_C_ARRAY_INIT; |
| 83 |
(void)_snprintf(name, ARRAYSIZE(name), "%zd-TestPath%zd", level, x); |
| 84 |
if (FAILED(NativePathCchAppendA(CurFilePath, TEST_MAX_PATH, name))) |
| 85 |
return FALSE; |
| 86 |
|
| 87 |
if (!create_layout_directories(level + 1, max_level, CurFilePath, files)) |
| 88 |
return FALSE; |
| 89 |
} |
| 90 |
return TRUE; |
| 91 |
} |
| 92 |
|
| 93 |
static BOOL create_layout(const char* BasePath, wArrayList* files) |
| 94 |
{ |
| 95 |
CHAR BasePathNative[TEST_MAX_PATH] = WINPR_C_ARRAY_INIT; |
| 96 |
memcpy(BasePathNative, BasePath, sizeof(BasePathNative)); |
| 97 |
if (FAILED(PathCchConvertStyleA(BasePathNative, ARRAYSIZE(BasePathNative), PATH_STYLE_NATIVE))) |
| 98 |
return FALSE; |
| 99 |
|
| 100 |
return create_layout_directories(0, 3, BasePathNative, files); |
| 101 |
} |
| 102 |
|
| 103 |
static void cleanup_layout(const char* BasePath) |
| 104 |
{ |
| 105 |
winpr_RemoveDirectory_RecursiveA(BasePath); |
| 106 |
} |
| 107 |
|
| 108 |
static BOOL find_first_file_success(const char* FilePath) |
| 109 |
{ |
| 110 |
BOOL rc = FALSE; |
| 111 |
WIN32_FIND_DATAA FindData = WINPR_C_ARRAY_INIT; |
| 112 |
HANDLE hFind = FindFirstFileA(FilePath, &FindData); |
| 113 |
if (hFind == INVALID_HANDLE_VALUE) |
| 114 |
{ |
| 115 |
printf("FindFirstFile failure: %s (INVALID_HANDLE_VALUE -1)\n", FilePath); |
| 116 |
goto fail; |
| 117 |
} |
| 118 |
|
| 119 |
printf("FindFirstFile: %s\n", FindData.cFileName); |
| 120 |
|
| 121 |
if (strcmp(FindData.cFileName, testFile1A) != 0) |
| 122 |
{ |
| 123 |
printf("FindFirstFile failure: Expected: %s, Actual: %s\n", testFile1A, FindData.cFileName); |
| 124 |
goto fail; |
| 125 |
} |
| 126 |
rc = TRUE; |
| 127 |
fail: |
| 128 |
if (hFind != INVALID_HANDLE_VALUE) |
| 129 |
FindClose(hFind); |
| 130 |
return rc; |
| 131 |
} |
| 132 |
|
| 133 |
static BOOL list_directory_dot(const char* BasePath, wArrayList* files) |
| 134 |
{ |
| 135 |
BOOL rc = FALSE; |
| 136 |
CHAR BasePathDot[TEST_MAX_PATH] = WINPR_C_ARRAY_INIT; |
| 137 |
memcpy(BasePathDot, BasePath, ARRAYSIZE(BasePathDot)); |
| 138 |
if (FAILED(PathCchConvertStyleA(BasePathDot, ARRAYSIZE(BasePathDot), PATH_STYLE_NATIVE))) |
| 139 |
return FALSE; |
| 140 |
if (FAILED(NativePathCchAppendA(BasePathDot, TEST_MAX_PATH, "."))) |
| 141 |
return FALSE; |
| 142 |
WIN32_FIND_DATAA FindData = WINPR_C_ARRAY_INIT; |
| 143 |
HANDLE hFind = FindFirstFileA(BasePathDot, &FindData); |
| 144 |
if (hFind == INVALID_HANDLE_VALUE) |
| 145 |
return FALSE; |
| 146 |
size_t count = 0; |
| 147 |
do |
| 148 |
{ |
| 149 |
count++; |
| 150 |
if (strcmp(FindData.cFileName, ".") != 0) |
| 151 |
goto fail; |
| 152 |
} while (FindNextFile(hFind, &FindData)); |
| 153 |
|
| 154 |
rc = TRUE; |
| 155 |
fail: |
| 156 |
FindClose(hFind); |
| 157 |
|
| 158 |
if (count != 1) |
| 159 |
return FALSE; |
| 160 |
return rc; |
| 161 |
} |
| 162 |
|
| 163 |
static BOOL list_directory_star(const char* BasePath, wArrayList* files) |
| 164 |
{ |
| 165 |
CHAR BasePathDot[TEST_MAX_PATH] = WINPR_C_ARRAY_INIT; |
| 166 |
memcpy(BasePathDot, BasePath, ARRAYSIZE(BasePathDot)); |
| 167 |
|
| 168 |
if (FAILED(PathCchConvertStyleA(BasePathDot, ARRAYSIZE(BasePathDot), PATH_STYLE_NATIVE))) |
| 169 |
return FALSE; |
| 170 |
|
| 171 |
if (FAILED(NativePathCchAppendA(BasePathDot, TEST_MAX_PATH, "*"))) |
| 172 |
return FALSE; |
| 173 |
|
| 174 |
WIN32_FIND_DATAA FindData = WINPR_C_ARRAY_INIT; |
| 175 |
HANDLE hFind = FindFirstFileA(BasePathDot, &FindData); |
| 176 |
if (hFind == INVALID_HANDLE_VALUE) |
| 177 |
return FALSE; |
| 178 |
size_t count = 0; |
| 179 |
size_t dotcount = 0; |
| 180 |
size_t dotdotcount = 0; |
| 181 |
do |
| 182 |
{ |
| 183 |
if (strcmp(FindData.cFileName, ".") == 0) |
| 184 |
dotcount++; |
| 185 |
else if (strcmp(FindData.cFileName, "..") == 0) |
| 186 |
dotdotcount++; |
| 187 |
else |
| 188 |
count++; |
| 189 |
} while (FindNextFile(hFind, &FindData)); |
| 190 |
FindClose(hFind); |
| 191 |
|
| 192 |
const char sep = PathGetSeparatorA(PATH_STYLE_NATIVE); |
| 193 |
size_t fcount = 0; |
| 194 |
const size_t baselen = strlen(BasePath); |
| 195 |
const size_t total = ArrayList_Count(files); |
| 196 |
for (size_t x = 0; x < total; x++) |
| 197 |
{ |
| 198 |
const char* path = ArrayList_GetItem(files, x); |
| 199 |
const size_t pathlen = strlen(path); |
| 200 |
if (pathlen < baselen) |
| 201 |
continue; |
| 202 |
const char* skip = &path[baselen]; |
| 203 |
if (*skip == sep) |
| 204 |
skip++; |
| 205 |
const char* end = strrchr(skip, sep); |
| 206 |
if (end) |
| 207 |
continue; |
| 208 |
fcount++; |
| 209 |
} |
| 210 |
|
| 211 |
return (fcount == count); |
| 212 |
} |
| 213 |
|
| 214 |
static BOOL find_first_file_fail(const char* FilePath) |
| 215 |
{ |
| 216 |
WIN32_FIND_DATAA FindData = WINPR_C_ARRAY_INIT; |
| 217 |
HANDLE hFind = FindFirstFileA(FilePath, &FindData); |
| 218 |
if (hFind == INVALID_HANDLE_VALUE) |
| 219 |
return TRUE; |
| 220 |
|
| 221 |
FindClose(hFind); |
| 222 |
return FALSE; |
| 223 |
} |
| 224 |
|
| 225 |
static int TestFileFindFirstFileA(const char* str) |
| 226 |
{ |
| 227 |
int rc = -1; |
| 228 |
|
| 229 |
printf("[%s] basepath: '%s'\n", __func__, str); |
| 230 |
if (!str) |
| 231 |
return -1; |
| 232 |
|
| 233 |
CHAR BasePath[TEST_MAX_PATH] = WINPR_C_ARRAY_INIT; |
| 234 |
|
| 235 |
strncpy(BasePath, str, ARRAYSIZE(BasePath)); |
| 236 |
|
| 237 |
const size_t length = strnlen(BasePath, TEST_MAX_PATH - 1); |
| 238 |
|
| 239 |
CHAR FilePath[TEST_MAX_PATH] = WINPR_C_ARRAY_INIT; |
| 240 |
CopyMemory(FilePath, BasePath, length * sizeof(CHAR)); |
| 241 |
|
| 242 |
if (FAILED(PathCchConvertStyleA(BasePath, length, PATH_STYLE_WINDOWS))) |
| 243 |
return -2; |
| 244 |
|
| 245 |
wArrayList* files = ArrayList_New(FALSE); |
| 246 |
if (!files) |
| 247 |
return -3; |
| 248 |
wObject* obj = ArrayList_Object(files); |
| 249 |
obj->fnObjectFree = winpr_ObjectStringFree; |
| 250 |
obj->fnObjectNew = winpr_ObjectStringClone; |
| 251 |
|
| 252 |
if (!create_layout(BasePath, files)) |
| 253 |
goto fail; |
| 254 |
|
| 255 |
if (FAILED(NativePathCchAppendA(FilePath, TEST_MAX_PATH, testFile1A))) |
| 256 |
goto fail; |
| 257 |
|
| 258 |
printf("Finding file: %s\n", FilePath); |
| 259 |
|
| 260 |
if (!find_first_file_fail(FilePath)) |
| 261 |
goto fail; |
| 262 |
|
| 263 |
if (!create_fileA(FilePath)) |
| 264 |
goto fail; |
| 265 |
|
| 266 |
if (!find_first_file_success(FilePath)) |
| 267 |
goto fail; |
| 268 |
|
| 269 |
CHAR BasePathInvalid[TEST_MAX_PATH] = WINPR_C_ARRAY_INIT; |
| 270 |
(void)_snprintf(BasePathInvalid, ARRAYSIZE(BasePathInvalid), "%s\\", BasePath); |
| 271 |
|
| 272 |
if (!find_first_file_fail(BasePathInvalid)) |
| 273 |
goto fail; |
| 274 |
|
| 275 |
if (!list_directory_dot(BasePath, files)) |
| 276 |
goto fail; |
| 277 |
|
| 278 |
if (!list_directory_star(BasePath, files)) |
| 279 |
goto fail; |
| 280 |
|
| 281 |
rc = 0; |
| 282 |
fail: |
| 283 |
winpr_DeleteFile(FilePath); |
| 284 |
cleanup_layout(BasePath); |
| 285 |
ArrayList_Free(files); |
| 286 |
return rc; |
| 287 |
} |
| 288 |
|
| 289 |
WINPR_ATTR_FORMAT_ARG(1, 0) |
| 290 |
static int printf1W(const char* WINPR_FORMAT_ARG fmt, const WCHAR* arg1) |
| 291 |
{ |
| 292 |
char* var1 = ConvertWCharToUtf8Alloc(arg1, nullptr); |
| 293 |
const int rc = printf(fmt, var1); |
| 294 |
free(var1); |
| 295 |
return rc; |
| 296 |
} |
| 297 |
|
| 298 |
WINPR_ATTR_FORMAT_ARG(1, 0) |
| 299 |
static int printf2W(const char* WINPR_FORMAT_ARG fmt, const WCHAR* arg1, const WCHAR* arg2) |
| 300 |
{ |
| 301 |
char* var1 = ConvertWCharToUtf8Alloc(arg1, nullptr); |
| 302 |
char* var2 = ConvertWCharToUtf8Alloc(arg2, nullptr); |
| 303 |
const int rc = printf(fmt, var1, var2); |
| 304 |
free(var1); |
| 305 |
free(var2); |
| 306 |
return rc; |
| 307 |
} |
| 308 |
|
| 309 |
static int TestFileFindFirstFileW(const char* str) |
| 310 |
{ |
| 311 |
WCHAR buffer[32] = WINPR_C_ARRAY_INIT; |
| 312 |
const WCHAR* testFile1W = InitializeConstWCharFromUtf8("TestFile1W", buffer, ARRAYSIZE(buffer)); |
| 313 |
int rc = -1; |
| 314 |
if (!str) |
| 315 |
return -1; |
| 316 |
|
| 317 |
WCHAR BasePath[TEST_MAX_PATH] = WINPR_C_ARRAY_INIT; |
| 318 |
|
| 319 |
printf("[%s] basepath: '%s'\n", __func__, str); |
| 320 |
(void)ConvertUtf8ToWChar(str, BasePath, ARRAYSIZE(BasePath)); |
| 321 |
|
| 322 |
const size_t length = _wcsnlen(BasePath, TEST_MAX_PATH - 1); |
| 323 |
|
| 324 |
WCHAR FilePath[TEST_MAX_PATH] = WINPR_C_ARRAY_INIT; |
| 325 |
CopyMemory(FilePath, BasePath, length * sizeof(WCHAR)); |
| 326 |
|
| 327 |
HANDLE hFind = INVALID_HANDLE_VALUE; |
| 328 |
if (FAILED(PathCchConvertStyleW(BasePath, length, PATH_STYLE_WINDOWS))) |
| 329 |
goto fail; |
| 330 |
if (FAILED(NativePathCchAppendW(FilePath, TEST_MAX_PATH, testFile1W))) |
| 331 |
goto fail; |
| 332 |
|
| 333 |
if (!create_fileW(FilePath)) |
| 334 |
goto fail; |
| 335 |
|
| 336 |
printf1W("Finding file: %s\n", FilePath); |
| 337 |
|
| 338 |
WIN32_FIND_DATAW FindData = WINPR_C_ARRAY_INIT; |
| 339 |
hFind = FindFirstFileW(FilePath, &FindData); |
| 340 |
|
| 341 |
if (hFind == INVALID_HANDLE_VALUE) |
| 342 |
{ |
| 343 |
printf1W("FindFirstFile failure: %s (INVALID_HANDLE_VALUE -1)\n", FilePath); |
| 344 |
goto fail; |
| 345 |
} |
| 346 |
|
| 347 |
printf1W("FindFirstFile: %s\n", FindData.cFileName); |
| 348 |
|
| 349 |
if (_wcscmp(FindData.cFileName, testFile1W) != 0) |
| 350 |
{ |
| 351 |
printf2W("FindFirstFile failure: Expected: %s, Actual: %s\n", testFile1W, |
| 352 |
FindData.cFileName); |
| 353 |
goto fail; |
| 354 |
} |
| 355 |
|
| 356 |
rc = 0; |
| 357 |
fail: |
| 358 |
DeleteFileW(FilePath); |
| 359 |
FindClose(hFind); |
| 360 |
return rc; |
| 361 |
} |
| 362 |
|
| 363 |
int TestFileFindFirstFile(int argc, char* argv[]) |
| 364 |
{ |
| 365 |
char* str = GetKnownSubPath(KNOWN_PATH_TEMP, "TestFileFindFirstFile"); |
| 366 |
if (!str) |
| 367 |
return -23; |
| 368 |
|
| 369 |
cleanup_layout(str); |
| 370 |
|
| 371 |
int rc1 = -1; |
| 372 |
int rc2 = -1; |
| 373 |
if (winpr_PathMakePath(str, nullptr)) |
| 374 |
{ |
| 375 |
rc1 = TestFileFindFirstFileA(str); |
| 376 |
rc2 = TestFileFindFirstFileW(str); |
| 377 |
winpr_RemoveDirectory(str); |
| 378 |
} |
| 379 |
free(str); |
| 380 |
return rc1 + rc2; |
| 381 |
} |