| 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: FindNextFileA.c |
| 8 | ** |
| 9 | ** Purpose: Tests the PAL implementation of the FindNextFileA function. |
| 10 | ** |
| 11 | ** |
| 12 | **===================================================================*/ |
| 13 | |
| 14 | #include <palsuite.h> |
| 15 | |
| 16 | |
| 17 | const char* szFindName = "test01.txt" ; |
| 18 | const char* szFindName_02 = "test02.txt" ; |
| 19 | const char* szFindNameWldCard_01 = "test0?.txt" ; |
| 20 | const char* szFindNameWldCard_02 = "*.txt" ; |
| 21 | const char* szDirName = "test_dir" ; |
| 22 | const char* szDirName_02 = "test_dir_02" ; |
| 23 | const char* szDirNameWldCard = "test_*" ; |
| 24 | |
| 25 | |
| 26 | |
| 27 | void removeAll() |
| 28 | { |
| 29 | WCHAR* wTempPtr = NULL; |
| 30 | |
| 31 | wTempPtr = convert((LPSTR)szDirName); |
| 32 | RemoveDirectoryW(wTempPtr); |
| 33 | free (wTempPtr); |
| 34 | wTempPtr = convert((LPSTR)szDirName_02); |
| 35 | RemoveDirectoryW(wTempPtr); |
| 36 | free (wTempPtr); |
| 37 | DeleteFile(szFindName); |
| 38 | DeleteFile(szFindName_02); |
| 39 | } |
| 40 | |
| 41 | |
| 42 | |
| 43 | BOOL createTestFile(const char* szName) |
| 44 | { |
| 45 | FILE *pFile = NULL; |
| 46 | |
| 47 | pFile = fopen(szName, "w" ); |
| 48 | if (pFile == NULL) |
| 49 | { |
| 50 | Trace("FindNextFile: ERROR -> Unable to create file \"%s\".\n" , |
| 51 | szName); |
| 52 | removeAll(); |
| 53 | return FALSE; |
| 54 | } |
| 55 | else |
| 56 | { |
| 57 | fprintf(pFile, "FindNextFile test file, \"%s\".\n" , szFindName); |
| 58 | fclose(pFile); |
| 59 | } |
| 60 | return TRUE; |
| 61 | } |
| 62 | |
| 63 | |
| 64 | |
| 65 | int __cdecl main(int argc, char *argv[]) |
| 66 | { |
| 67 | WIN32_FIND_DATA findFileData; |
| 68 | WIN32_FIND_DATA findFileData_02; |
| 69 | HANDLE hFind = NULL; |
| 70 | BOOL bRc = FALSE; |
| 71 | DWORD dwBytesWritten; |
| 72 | WCHAR* wTempPtr = NULL; |
| 73 | |
| 74 | |
| 75 | if (0 != PAL_Initialize(argc,argv)) |
| 76 | { |
| 77 | return FAIL; |
| 78 | } |
| 79 | removeAll(); |
| 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("FindNextFile: ERROR -> Found invalid NULL file" ); |
| 89 | } |
| 90 | |
| 91 | bRc = FindNextFile(hFind, &findFileData); |
| 92 | if (bRc == TRUE) |
| 93 | { |
| 94 | Fail("FindNextFile: ERROR -> Found a file based on an invalid handle" ); |
| 95 | } |
| 96 | |
| 97 | |
| 98 | // |
| 99 | // find a file that exists |
| 100 | // |
| 101 | if(createTestFile(szFindName) == FALSE) |
| 102 | { |
| 103 | PAL_TerminateEx(FAIL); |
| 104 | return FAIL; |
| 105 | } |
| 106 | if(createTestFile(szFindName_02) == FALSE) |
| 107 | { |
| 108 | PAL_TerminateEx(FAIL); |
| 109 | return FAIL; |
| 110 | } |
| 111 | |
| 112 | hFind = FindFirstFileA(szFindName, &findFileData); |
| 113 | if (hFind == INVALID_HANDLE_VALUE) |
| 114 | { |
| 115 | removeAll(); |
| 116 | Fail("FindNextFile: ERROR -> Unable to find \"%s\"\n" , szFindName); |
| 117 | } |
| 118 | else |
| 119 | { |
| 120 | bRc = FindNextFile(hFind, &findFileData); |
| 121 | if (bRc != FALSE) |
| 122 | { |
| 123 | removeAll(); |
| 124 | Fail("FindNextFile: ERROR -> Found a file that doesn't exist.\n" ); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | |
| 129 | // |
| 130 | // find a directory that exists |
| 131 | // |
| 132 | wTempPtr = convert((LPSTR)szDirName); |
| 133 | bRc = CreateDirectoryW(wTempPtr, NULL); |
| 134 | free (wTempPtr); |
| 135 | if (bRc == FALSE) |
| 136 | { |
| 137 | removeAll(); |
| 138 | Fail("FindNextFile: ERROR -> Failed to create the directory \"%s\"\n" , |
| 139 | szDirName); |
| 140 | } |
| 141 | wTempPtr = convert((LPSTR)szDirName_02); |
| 142 | bRc = CreateDirectoryW(wTempPtr, NULL); |
| 143 | free (wTempPtr); |
| 144 | if (bRc == FALSE) |
| 145 | { |
| 146 | removeAll(); |
| 147 | Fail("FindNextFile: ERROR -> Failed to create the directory \"%s\"\n" , |
| 148 | szDirName_02); |
| 149 | } |
| 150 | |
| 151 | hFind = FindFirstFileA(szDirName, &findFileData); |
| 152 | if (hFind == INVALID_HANDLE_VALUE) |
| 153 | { |
| 154 | removeAll(); |
| 155 | Fail("FindNextFile: ERROR. FindFirstFileA was unable to find \"%s\"\n" , |
| 156 | szDirName); |
| 157 | } |
| 158 | else |
| 159 | { |
| 160 | bRc = FindNextFile(hFind, &findFileData); |
| 161 | if (bRc != FALSE) |
| 162 | { |
| 163 | removeAll(); |
| 164 | Fail("FindNextFile: ERROR -> Found a directory that doesn't exist.\n" ); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | |
| 169 | // |
| 170 | // find a file using wild cards |
| 171 | // |
| 172 | hFind = FindFirstFileA(szFindNameWldCard_01, &findFileData); |
| 173 | if (hFind == INVALID_HANDLE_VALUE) |
| 174 | { |
| 175 | removeAll(); |
| 176 | Fail("FindNextFile: ERROR -> FindFirstFileA was unable to find \"%s\"\n" , |
| 177 | szFindNameWldCard_01); |
| 178 | } |
| 179 | else |
| 180 | { |
| 181 | bRc = FindNextFile(hFind, &findFileData_02); |
| 182 | if (bRc == FALSE) |
| 183 | { |
| 184 | removeAll(); |
| 185 | Fail("FindNextFile: ERROR -> Unable to find another file.\n" ); |
| 186 | } |
| 187 | else |
| 188 | { |
| 189 | // validate we found the correct file |
| 190 | if (strcmp(findFileData_02.cFileName, findFileData.cFileName) == 0) |
| 191 | { |
| 192 | removeAll(); |
| 193 | Fail("FindNextFile: ERROR -> Found the same file \"%s\".\n" , |
| 194 | findFileData.cFileName); |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | |
| 200 | // |
| 201 | // find a directory using wild cards |
| 202 | // |
| 203 | hFind = FindFirstFileA(szDirNameWldCard, &findFileData); |
| 204 | if (hFind == INVALID_HANDLE_VALUE) |
| 205 | { |
| 206 | removeAll(); |
| 207 | Fail("FindNextFile: ERROR -> Unable to find \"%s\"\n" , |
| 208 | szDirNameWldCard); |
| 209 | } |
| 210 | else |
| 211 | { |
| 212 | bRc = FindNextFile(hFind, &findFileData_02); |
| 213 | if (bRc == FALSE) |
| 214 | { |
| 215 | removeAll(); |
| 216 | Fail("FindNextFile: ERROR -> Unable to find another directory.\n" ); |
| 217 | } |
| 218 | else |
| 219 | { |
| 220 | // validate we found the correct directory |
| 221 | if (strcmp(findFileData_02.cFileName, findFileData.cFileName) == 0) |
| 222 | { |
| 223 | removeAll(); |
| 224 | Fail("FindNextFile: ERROR -> Found the same directory \"%s\".\n" , |
| 225 | findFileData.cFileName); |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | // |
| 231 | // attempt to write to the hFind handle (which should fail) |
| 232 | // |
| 233 | bRc = WriteFile(hFind, "this is a test" , 10, &dwBytesWritten, NULL); |
| 234 | removeAll(); |
| 235 | if (bRc == TRUE) |
| 236 | { |
| 237 | Fail("FindNextFile: ERROR -> Able to write to a FindNextFile handle.\n" ); |
| 238 | } |
| 239 | |
| 240 | PAL_Terminate(); |
| 241 | |
| 242 | return PASS; |
| 243 | } |
| 244 | |