1 | // Licensed to the .NET Foundation under one or more agreements. |
2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | // See the LICENSE file in the project root for more information. |
4 | |
5 | /*===================================================================== |
6 | ** |
7 | ** Source: FindFirstFileW.c |
8 | ** |
9 | ** Purpose: Tests the PAL implementation of the FindFirstFileW function. |
10 | ** |
11 | ** |
12 | **===================================================================*/ |
13 | |
14 | |
15 | #include <palsuite.h> |
16 | |
17 | |
18 | const char* szNoFileName = "333asdf.x77t" ; |
19 | const char* szFindName = "test01.txt" ; |
20 | const char* szFindNameWldCard_01 = "test0?.txt" ; |
21 | const char* szFindNameWldCard_02 = "*.txt" ; |
22 | const char* szDirName = "test_dir" ; |
23 | const char* szDirNameSlash = "test_dir\\" ; |
24 | const char* szDirNameWldCard_01 = "?est_dir" ; |
25 | const char* szDirNameWldCard_02 = "test_*" ; |
26 | |
27 | |
28 | BOOL CleanUp() |
29 | { |
30 | DWORD dwAtt; |
31 | BOOL result = TRUE; |
32 | |
33 | dwAtt = GetFileAttributesA(szFindName); |
34 | if( dwAtt != INVALID_FILE_ATTRIBUTES ) |
35 | { |
36 | if(!SetFileAttributesA (szFindName, FILE_ATTRIBUTE_NORMAL)) |
37 | { |
38 | result = FALSE; |
39 | Trace("ERROR:%d: Error setting attributes [%s][%d]\n" , szFindName, FILE_ATTRIBUTE_NORMAL); |
40 | } |
41 | if(!DeleteFileA (szFindName)) |
42 | { |
43 | result = FALSE; |
44 | Trace("ERROR:%d: Error deleting file [%s][%d]\n" , GetLastError(), szFindName, dwAtt); |
45 | } |
46 | } |
47 | |
48 | dwAtt = GetFileAttributesA(szDirName); |
49 | if( dwAtt != INVALID_FILE_ATTRIBUTES ) |
50 | { |
51 | if(!RemoveDirectoryA (szDirName)) |
52 | { |
53 | result = FALSE; |
54 | Trace("ERROR:%d: Error deleting file [%s][%d]\n" , GetLastError(), szDirName, dwAtt); |
55 | } |
56 | } |
57 | |
58 | return result; |
59 | } |
60 | |
61 | int __cdecl main(int argc, char *argv[]) |
62 | { |
63 | WIN32_FIND_DATAW findFileData; |
64 | HANDLE hFind = NULL; |
65 | FILE *pFile = NULL; |
66 | BOOL bRc = FALSE; |
67 | WCHAR* pTemp = NULL; |
68 | |
69 | |
70 | if (0 != PAL_Initialize(argc,argv)) |
71 | { |
72 | return FAIL; |
73 | } |
74 | |
75 | if(!CleanUp()) |
76 | { |
77 | Fail("FindFirstFileW: ERROR : Initial Clean Up failed\n" ); |
78 | } |
79 | |
80 | // |
81 | // find a file that doesn't exist |
82 | // |
83 | pTemp = convert((LPSTR)szNoFileName); |
84 | hFind = FindFirstFileW(pTemp, &findFileData); |
85 | free(pTemp); |
86 | if (hFind != INVALID_HANDLE_VALUE) |
87 | { |
88 | Fail ("FindFirstFileW: ERROR -> Found invalid NULL file\n" ); |
89 | } |
90 | |
91 | |
92 | // |
93 | // find a file that exists |
94 | // |
95 | pFile = fopen(szFindName, "w" ); |
96 | if (pFile == NULL) |
97 | { |
98 | Fail("FindFirstFileW: ERROR -> Unable to create a test file\n" ); |
99 | } |
100 | else |
101 | { |
102 | fclose(pFile); |
103 | } |
104 | pTemp = convert((LPSTR)szFindName); |
105 | hFind = FindFirstFileW(pTemp, &findFileData); |
106 | if (hFind == INVALID_HANDLE_VALUE) |
107 | { |
108 | free(pTemp); |
109 | Fail ("FindFirstFileW: ERROR -> Unable to find \"%s\"\n" , szFindName); |
110 | } |
111 | else |
112 | { |
113 | // validate we found the correct file |
114 | if (wcscmp(pTemp, findFileData.cFileName) != 0) |
115 | { |
116 | free(pTemp); |
117 | Fail ("FindFirstFileW: ERROR -> Found the wrong file\n" ); |
118 | } |
119 | } |
120 | free(pTemp); |
121 | |
122 | // |
123 | // find a directory that exists |
124 | // |
125 | pTemp = convert((LPSTR)szDirName); |
126 | bRc = CreateDirectoryW(pTemp, NULL); |
127 | if (bRc == FALSE) |
128 | { |
129 | Fail("FindFirstFileW: ERROR[%u] -> Failed to create the directory \"%s\"\n" , |
130 | GetLastError(), szDirName); |
131 | } |
132 | |
133 | hFind = FindFirstFileW(pTemp, &findFileData); |
134 | if (hFind == INVALID_HANDLE_VALUE) |
135 | { |
136 | free(pTemp); |
137 | Fail("FindFirstFileW: ERROR. Unable to find \"%s\"\n" , szDirName); |
138 | } |
139 | else |
140 | { |
141 | // validate we found the correct directory |
142 | if (wcscmp(pTemp, findFileData.cFileName) != 0) |
143 | { |
144 | free(pTemp); |
145 | Fail("FindFirstFileW: ERROR -> Found the wrong directory\n" ); |
146 | } |
147 | } |
148 | free(pTemp); |
149 | |
150 | // |
151 | // find a directory using a trailing '\' on the directory name: should fail |
152 | // |
153 | pTemp = convert((LPSTR)szDirNameSlash); |
154 | hFind = FindFirstFileW(pTemp, &findFileData); |
155 | free(pTemp); |
156 | if (hFind != INVALID_HANDLE_VALUE) |
157 | { |
158 | Fail("FindFirstFileW: ERROR -> Able to find \"%s\": trailing " |
159 | "slash should have failed.\n" , |
160 | szDirNameSlash); |
161 | } |
162 | |
163 | // find a file using wild cards |
164 | pTemp = convert((LPSTR)szFindNameWldCard_01); |
165 | hFind = FindFirstFileW(pTemp, &findFileData); |
166 | free(pTemp); |
167 | if (hFind == INVALID_HANDLE_VALUE) |
168 | { |
169 | Fail("FindFirstFileW: ERROR -> Unable to find \"%s\"\n" , |
170 | szFindNameWldCard_01); |
171 | } |
172 | |
173 | pTemp = convert((LPSTR)szFindNameWldCard_02); |
174 | hFind = FindFirstFileW(pTemp, &findFileData); |
175 | free(pTemp); |
176 | if (hFind == INVALID_HANDLE_VALUE) |
177 | { |
178 | Fail("FindFirstFileW: ERROR -> Unable to find \"%s\"\n" , |
179 | szFindNameWldCard_02); |
180 | } |
181 | |
182 | |
183 | // |
184 | // find a directory using wild cards |
185 | // |
186 | |
187 | pTemp = convert((LPSTR)szDirNameWldCard_01); |
188 | hFind = FindFirstFileW(pTemp, &findFileData); |
189 | free(pTemp); |
190 | if (hFind == INVALID_HANDLE_VALUE) |
191 | { |
192 | Fail("FindFirstFileW: ERROR -> Unable to find \"%s\"\n" , |
193 | szDirNameWldCard_01); |
194 | } |
195 | |
196 | pTemp = convert((LPSTR)szDirNameWldCard_02); |
197 | hFind = FindFirstFileW(pTemp, &findFileData); |
198 | free(pTemp); |
199 | if (hFind == INVALID_HANDLE_VALUE) |
200 | { |
201 | Fail("FindFirstFileW: ERROR -> Unable to find \"%s\"\n" , |
202 | szDirNameWldCard_02); |
203 | } |
204 | |
205 | if(!CleanUp()) |
206 | { |
207 | Fail("FindFirstFileW: ERROR : Final Clean Up failed\n" ); |
208 | } |
209 | |
210 | PAL_Terminate(); |
211 | return PASS; |
212 | } |
213 | |