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