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