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