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: FindClose.c
8**
9** Purpose: Tests the PAL implementation of the FindClose function.
10**
11**
12**===================================================================*/
13
14
15#include <palsuite.h>
16
17
18
19const WCHAR szFindName[] = {'t','e', 's', 't', '0', '1', '.', 't', 'x', 't', '\0'};
20const WCHAR szFindName_02[] = {'t','e', 's', 't', '0', '2', '.', 't', 'x', 't', '\0'};
21const WCHAR szFindName_03[] = {'t','e', 's', 't', '0', '3', '.', 't', 'x', 't', '\0'};
22const WCHAR szFindNameWldCard_01[] = {'t','e', 's', 't', '0', '?', '.', 't', 'x', 't', '\0'};
23const WCHAR szFindNameWldCard_02[] = {'*', '.', 't', 'x', 't', '\0'};
24const WCHAR szDirName[] = {'t','e', 's', 't', '_', 'd', 'i', 'r', '\0'};
25const WCHAR szDirName_02[] = {'t','e', 's', 't', '_', 'd', 'i', 'r', '0', '2', '\0'};
26const WCHAR szDirNameWldCard[] = {'t','e', 's', 't', '_', '*', '\0'};
27
28
29
30BOOL createTestFile(const WCHAR* szName)
31{
32 FILE *pFile = NULL;
33 char* pTemp = NULL;
34
35 pTemp = convertC((WCHAR*)szName);
36 pFile = fopen(pTemp, "w");
37 if (pFile == NULL)
38 {
39 Trace("FindClose: ERROR -> Unable to create file \"%s\".\n", pTemp);
40 free(pTemp);
41 return FALSE;
42 }
43 else
44 {
45 fprintf(pFile, "FindClose test file, \"%s\".\n", pTemp);
46 free(pTemp);
47 fclose(pFile);
48 }
49 return TRUE;
50}
51
52
53void removeAll()
54{
55 RemoveDirectoryW(szDirName);
56 RemoveDirectoryW(szDirName_02);
57
58 DeleteFileW(szFindName);
59 DeleteFileW(szFindName_02);
60 DeleteFileW(szFindName_03);
61}
62
63
64int __cdecl main(int argc, char *argv[])
65{
66 WIN32_FIND_DATAW findFileData;
67 WIN32_FIND_DATAW findFileData_02;
68 HANDLE hFind = NULL;
69 BOOL bRc = FALSE;
70 char* pTemp = NULL;
71
72
73 if (0 != PAL_Initialize(argc,argv))
74 {
75 return FAIL;
76 }
77
78 /* do some clean up just to be sure */
79 removeAll();
80
81 /* FindClose a null handle */
82 if(FindClose(NULL)!=0)
83 {
84 Fail("FindClose: ERROR -> Closing a NULL handle succeeded.\n");
85 }
86
87 /* find a file that exists */
88 if(createTestFile(szFindName) == FALSE)
89 {
90 removeAll();
91 PAL_TerminateEx(FAIL);
92 return FAIL;
93 }
94 if(createTestFile(szFindName_02) == FALSE)
95 {
96 removeAll();
97 PAL_TerminateEx(FAIL);
98 return FAIL;
99 }
100 if(createTestFile(szFindName_03) == FALSE)
101 {
102 removeAll();
103 PAL_TerminateEx(FAIL);
104 return FAIL;
105 }
106
107 // close a FindFirstFileW handle
108 hFind = FindFirstFileW(szFindName, &findFileData);
109 if (hFind == INVALID_HANDLE_VALUE)
110 {
111 pTemp = convertC((WCHAR*)szFindName);
112 Trace("FindClose: ERROR -> Unable to find \"%s\"\n", pTemp);
113 free(pTemp);
114 removeAll();
115 PAL_TerminateEx(FAIL);
116 return FAIL;
117 }
118 else
119 {
120 bRc = FindClose(hFind);
121 if (bRc == FALSE)
122 {
123 removeAll();
124 Fail("FindClose: ERROR -> Unable to close a valid"
125 " FindFirstFileW handle.\n");
126 }
127 }
128 hFind = FindFirstFileW(szFindName, &findFileData);
129 if (hFind == INVALID_HANDLE_VALUE)
130 {
131 pTemp = convertC((WCHAR*)szFindName);
132 Trace("FindClose: ERROR -> Unable to find \"%s\"\n", pTemp);
133 free(pTemp);
134 removeAll();
135 PAL_TerminateEx(FAIL);
136 return FAIL;
137 }
138 else
139 {
140 bRc = FindNextFileW(hFind, &findFileData);
141 if (bRc != FALSE)
142 {
143 removeAll();
144 Fail("FindClose: ERROR -> Found a file that doesn't exist.\n");
145 }
146 else
147 {
148 bRc = FindClose(hFind);
149 if (bRc == FALSE)
150 {
151 removeAll();
152 Fail("FindClose: ERROR -> Unable to close a valid "
153 "FindNextFileW handle.\n");
154 }
155 }
156 }
157
158 /* find a directory that exists */
159 bRc = CreateDirectoryW(szDirName, NULL);
160 if (bRc == FALSE)
161 {
162 pTemp = convertC((WCHAR*)szDirName);
163 Trace("FindClose: ERROR -> Failed to create the directory \"%s\"\n",
164 pTemp);
165 free(pTemp);
166 removeAll();
167 PAL_TerminateEx(FAIL);
168 return FAIL;
169 }
170
171 bRc = CreateDirectoryW(szDirName_02, NULL);
172 if (bRc == FALSE)
173 {
174 pTemp = convertC((WCHAR*)szDirName_02);
175 Trace("FindClose: ERROR -> Failed to create the directory \"%s\"\n",
176 pTemp);
177 free(pTemp);
178 removeAll();
179 PAL_TerminateEx(FAIL);
180 return FAIL;
181 }
182
183 hFind = FindFirstFileW(szDirName, &findFileData);
184 if (hFind == INVALID_HANDLE_VALUE)
185 {
186 pTemp = convertC((WCHAR*)szDirName);
187 Trace("FindClose: ERROR. FindFirstFileW was unable to find \"%s\"\n",
188 pTemp);
189 free(pTemp);
190 removeAll();
191 PAL_TerminateEx(FAIL);
192 return FAIL;
193 }
194 else
195 {
196 bRc = FindClose(hFind);
197 if (bRc == FALSE)
198 {
199 removeAll();
200 Fail("FindClose: ERROR -> Unable to close a valid"
201 " FindFirstFileW handle of a directory.\n");
202 }
203 }
204
205 /* find a file using wild cards */
206 hFind = FindFirstFileW(szFindNameWldCard_01, &findFileData);
207 if (hFind == INVALID_HANDLE_VALUE)
208 {
209 pTemp = convertC((WCHAR*)szFindNameWldCard_01);
210 Trace("FindClose: ERROR -> FindFirstFileW was unable to find \"%s\"\n",
211 pTemp);
212 free(pTemp);
213 removeAll();
214 PAL_TerminateEx(FAIL);
215 return FAIL;
216 }
217 else
218 {
219 bRc = FindNextFileW(hFind, &findFileData_02);
220 if (bRc == FALSE)
221 {
222 removeAll();
223 Fail("FindClose: ERROR -> Unable to find another file.\n");
224 }
225 else
226 {
227 bRc = FindClose(hFind);
228 if (bRc == FALSE)
229 {
230 removeAll();
231 Fail("FindClose: ERROR -> Unable to close a valid"
232 " FindNextFileW handle.\n");
233 }
234 }
235 }
236
237 /* find a directory using wild cards */
238 hFind = FindFirstFileW(szDirNameWldCard, &findFileData);
239 if (hFind == INVALID_HANDLE_VALUE)
240 {
241 pTemp = convertC((WCHAR*)szDirNameWldCard);
242 Trace("FindClose: ERROR -> Unable to find \"%s\"\n",
243 pTemp);
244 free(pTemp);
245 removeAll();
246 PAL_TerminateEx(FAIL);
247 return FAIL;
248 }
249 else
250 {
251 bRc = FindNextFileW(hFind, &findFileData_02);
252 if (bRc == FALSE)
253 {
254 removeAll();
255 Fail("FindClose: ERROR -> Unable to find another directory.\n");
256 }
257 else
258 {
259 bRc = FindClose(hFind);
260 if (bRc == FALSE)
261 {
262 removeAll();
263 Fail("FindClose: ERROR -> Unable to close a valid"
264 " FindNextFileW handle of a directory.\n");
265 }
266 }
267 }
268
269
270 removeAll();
271 PAL_Terminate();
272
273 return PASS;
274}
275
276