| 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: GetFileAttributesW.c |
| 8 | ** |
| 9 | ** Purpose: Tests the PAL implementation of the GetFileAttributesW function by |
| 10 | ** checking the attributes of: |
| 11 | ** - a normal directory and file |
| 12 | ** - a read only directory and file |
| 13 | ** - a read write directory and file |
| 14 | ** - a hidden directory and file |
| 15 | ** - a read only hidden directory and file |
| 16 | ** - a directory and a file with no attributes |
| 17 | ** - an invalid file name |
| 18 | ** |
| 19 | ** |
| 20 | **===========================================================================*/ |
| 21 | #include <palsuite.h> |
| 22 | |
| 23 | const int TYPE_DIR = 0; |
| 24 | const int TYPE_FILE = 1; |
| 25 | /* Structure defining a test case */ |
| 26 | typedef struct |
| 27 | { |
| 28 | char *name; /* name of the file/directory */ |
| 29 | DWORD expectedAttribs; /* expected attributes */ |
| 30 | HANDLE hFile; /* Handle to the file */ |
| 31 | int isFile; /* is file (1) or dir (0) */ |
| 32 | }TestCaseFile; |
| 33 | |
| 34 | typedef struct |
| 35 | { |
| 36 | char *name; /* name of the file/directory */ |
| 37 | DWORD expectedAttribs; /* expected attributes */ |
| 38 | HANDLE hFile; /* Handle to the file */ |
| 39 | int isFile; /* is file (1) or dir (0) */ |
| 40 | }TestCaseDir; |
| 41 | |
| 42 | DWORD desiredAccessFile = GENERIC_READ | GENERIC_WRITE; |
| 43 | DWORD shareModeFile = FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE; |
| 44 | LPSECURITY_ATTRIBUTES lpAttrFile = NULL; |
| 45 | DWORD dwCreationDispFile = CREATE_NEW; |
| 46 | DWORD dwFlagsAttribFile = FILE_ATTRIBUTE_NORMAL; |
| 47 | HANDLE hTemplateFile = NULL; |
| 48 | |
| 49 | int numFileTests = 6; |
| 50 | TestCaseFile gfaTestsFile[6]; /* GetFileAttributes tests list */ |
| 51 | |
| 52 | int numDirTests = 6; |
| 53 | TestCaseDir gfaTestsDir[6]; /* GetFileAttributes tests list */ |
| 54 | |
| 55 | BOOL CleanUpFiles() |
| 56 | { |
| 57 | DWORD dwAtt; |
| 58 | int i; |
| 59 | BOOL result = TRUE; |
| 60 | for (i = 0; i < numFileTests - 1 ; i++ ) |
| 61 | { |
| 62 | dwAtt = GetFileAttributesA(gfaTestsFile[i].name); |
| 63 | |
| 64 | if( dwAtt != INVALID_FILE_ATTRIBUTES ) |
| 65 | { |
| 66 | //Trace("Files iteration %d\n", i); |
| 67 | if(!SetFileAttributesA (gfaTestsFile[i].name, FILE_ATTRIBUTE_NORMAL)) |
| 68 | { |
| 69 | result = FALSE; |
| 70 | Trace("ERROR:%d: Error setting attributes [%s][%d]\n" , GetLastError(), gfaTestsFile[i].name, FILE_ATTRIBUTE_NORMAL); |
| 71 | } |
| 72 | |
| 73 | if(!DeleteFileA (gfaTestsFile[i].name)) |
| 74 | { |
| 75 | result = FALSE; |
| 76 | Trace("ERROR:%d: Error deleting file [%s][%d]\n" , GetLastError(), gfaTestsFile[i].name, dwAtt); |
| 77 | } |
| 78 | |
| 79 | } |
| 80 | } |
| 81 | // Trace("Value of result is %d\n", result); |
| 82 | return result; |
| 83 | } |
| 84 | BOOL SetUpFiles() |
| 85 | { |
| 86 | int i = 0; |
| 87 | BOOL result = TRUE; |
| 88 | for (i = 0; i < numFileTests - 1 ; i++ ) |
| 89 | { |
| 90 | gfaTestsFile[i].hFile = CreateFile(gfaTestsFile[i].name, |
| 91 | desiredAccessFile, |
| 92 | shareModeFile, |
| 93 | lpAttrFile, |
| 94 | dwCreationDispFile, |
| 95 | dwFlagsAttribFile, |
| 96 | hTemplateFile); |
| 97 | |
| 98 | if( gfaTestsFile[i].hFile == NULL ) |
| 99 | { |
| 100 | Fail("Error while creating files for iteration %d\n" , i); |
| 101 | } |
| 102 | |
| 103 | if(!SetFileAttributesA (gfaTestsFile[i].name, gfaTestsFile[i].expectedAttribs)) |
| 104 | { |
| 105 | result = FALSE; |
| 106 | Trace("ERROR:%d: Error setting attributes [%s][%d]\n" , GetLastError(), gfaTestsFile[i].name, gfaTestsFile[i].expectedAttribs); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | return result; |
| 111 | } |
| 112 | |
| 113 | BOOL CleanUpDirs() |
| 114 | { |
| 115 | DWORD dwAtt; |
| 116 | int i; |
| 117 | BOOL result = TRUE; |
| 118 | for (i = 0; i < numDirTests - 1; i++ ) |
| 119 | { |
| 120 | dwAtt = GetFileAttributesA(gfaTestsDir[i].name); |
| 121 | |
| 122 | if( dwAtt != INVALID_FILE_ATTRIBUTES ) |
| 123 | { |
| 124 | |
| 125 | if(!SetFileAttributesA (gfaTestsDir[i].name, FILE_ATTRIBUTE_DIRECTORY)) |
| 126 | { |
| 127 | result = FALSE; |
| 128 | Trace("ERROR:%d: Error setting attributes [%s][%d]\n" , GetLastError(), gfaTestsDir[i].name, (FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_DIRECTORY)); |
| 129 | } |
| 130 | |
| 131 | if(!RemoveDirectoryA (gfaTestsDir[i].name)) |
| 132 | { |
| 133 | result = FALSE; |
| 134 | Trace("ERROR:%d: Error deleting file [%s][%d]\n" , GetLastError(), gfaTestsDir[i].name, dwAtt); |
| 135 | } |
| 136 | |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | return result; |
| 141 | } |
| 142 | |
| 143 | BOOL SetUpDirs() |
| 144 | { |
| 145 | int i = 0; |
| 146 | BOOL result = TRUE; |
| 147 | DWORD ret = 0; |
| 148 | for (i = 0; i < numDirTests - 1; i++ ) |
| 149 | { |
| 150 | result = CreateDirectory(gfaTestsDir[i].name, |
| 151 | NULL); |
| 152 | |
| 153 | if(!result ) |
| 154 | { |
| 155 | result = FALSE; |
| 156 | Fail("Error while creating directory for iteration %d\n" , i); |
| 157 | } |
| 158 | |
| 159 | if(!SetFileAttributesA (gfaTestsDir[i].name, gfaTestsDir[i].expectedAttribs)) |
| 160 | { |
| 161 | result = FALSE; |
| 162 | Trace("ERROR:%d: Error setting attributes [%s][%d]\n" , GetLastError(), gfaTestsDir[i].name, gfaTestsDir[i].expectedAttribs); |
| 163 | } |
| 164 | |
| 165 | ret = GetFileAttributesA (gfaTestsDir[i].name); |
| 166 | if(ret != gfaTestsDir[i].expectedAttribs) |
| 167 | { |
| 168 | result = FALSE; |
| 169 | Trace("ERROR: Error setting attributes [%s][%d]\n" , gfaTestsDir[i].name, gfaTestsDir[i].expectedAttribs); |
| 170 | } |
| 171 | // Trace("Setup Dir setting attr [%d], returned [%d]\n", gfaTestsDir[i].expectedAttribs, ret); |
| 172 | |
| 173 | } |
| 174 | // Trace("Setup dirs returning %d\n", result); |
| 175 | return result; |
| 176 | } |
| 177 | int __cdecl main(int argc, char **argv) |
| 178 | { |
| 179 | int i; |
| 180 | BOOL bFailed = FALSE; |
| 181 | DWORD result; |
| 182 | |
| 183 | char * NormalDirectoryName = "normal_test_directory" ; |
| 184 | char * ReadOnlyDirectoryName = "ro_test_directory" ; |
| 185 | char * ReadWriteDirectoryName = "rw_directory" ; |
| 186 | char * HiddenDirectoryName = ".hidden_directory" ; |
| 187 | char * HiddenReadOnlyDirectoryName = ".hidden_ro_directory" ; |
| 188 | char * NoDirectoryName = "no_directory" ; |
| 189 | |
| 190 | char * NormalFileName = "normal_test_file" ; |
| 191 | char * ReadOnlyFileName = "ro_test_file" ; |
| 192 | char * ReadWriteFileName = "rw_file" ; |
| 193 | char * HiddenFileName = ".hidden_file" ; |
| 194 | char * HiddenReadOnlyFileName = ".hidden_ro_file" ; |
| 195 | char * NotReallyAFileName = "not_really_a_file" ; |
| 196 | |
| 197 | WCHAR *WStr; |
| 198 | /* Tests on directory */ |
| 199 | gfaTestsDir[0].name = NormalDirectoryName; |
| 200 | gfaTestsDir[0].expectedAttribs = FILE_ATTRIBUTE_DIRECTORY; |
| 201 | gfaTestsDir[0].isFile = TYPE_DIR; |
| 202 | |
| 203 | gfaTestsDir[1].name = ReadOnlyDirectoryName; |
| 204 | gfaTestsDir[1].expectedAttribs = FILE_ATTRIBUTE_DIRECTORY | |
| 205 | FILE_ATTRIBUTE_READONLY; |
| 206 | gfaTestsDir[1].isFile = TYPE_DIR; |
| 207 | |
| 208 | gfaTestsDir[2].name = ReadWriteDirectoryName; |
| 209 | gfaTestsDir[2].expectedAttribs = FILE_ATTRIBUTE_DIRECTORY; |
| 210 | gfaTestsDir[2].isFile = TYPE_DIR; |
| 211 | |
| 212 | gfaTestsDir[3].name = HiddenDirectoryName; |
| 213 | gfaTestsDir[3].expectedAttribs = FILE_ATTRIBUTE_DIRECTORY; //| |
| 214 | //FILE_ATTRIBUTE_HIDDEN; |
| 215 | gfaTestsDir[3].isFile = TYPE_DIR; |
| 216 | |
| 217 | gfaTestsDir[4].name = HiddenReadOnlyDirectoryName; |
| 218 | gfaTestsDir[4].expectedAttribs = FILE_ATTRIBUTE_DIRECTORY | |
| 219 | FILE_ATTRIBUTE_READONLY; //| |
| 220 | //FILE_ATTRIBUTE_HIDDEN; |
| 221 | gfaTestsDir[4].isFile = TYPE_DIR; |
| 222 | |
| 223 | gfaTestsDir[5].name = NoDirectoryName; |
| 224 | gfaTestsDir[5].expectedAttribs = INVALID_FILE_ATTRIBUTES; |
| 225 | gfaTestsDir[5].isFile = TYPE_DIR; |
| 226 | |
| 227 | /* Tests on file */ |
| 228 | gfaTestsFile[0].name = NormalFileName; |
| 229 | gfaTestsFile[0].expectedAttribs = FILE_ATTRIBUTE_NORMAL; |
| 230 | gfaTestsFile[0].isFile = TYPE_FILE; |
| 231 | |
| 232 | |
| 233 | gfaTestsFile[1].name = ReadOnlyFileName; |
| 234 | gfaTestsFile[1].expectedAttribs = FILE_ATTRIBUTE_READONLY; |
| 235 | gfaTestsFile[1].isFile = TYPE_FILE; |
| 236 | |
| 237 | gfaTestsFile[2].name = ReadWriteFileName; |
| 238 | gfaTestsFile[2].expectedAttribs = FILE_ATTRIBUTE_NORMAL; |
| 239 | gfaTestsFile[2].isFile = TYPE_FILE; |
| 240 | |
| 241 | gfaTestsFile[3].name = HiddenFileName; |
| 242 | gfaTestsFile[3].expectedAttribs = FILE_ATTRIBUTE_NORMAL; //FILE_ATTRIBUTE_HIDDEN; |
| 243 | gfaTestsFile[3].isFile = TYPE_FILE; |
| 244 | |
| 245 | gfaTestsFile[4].name = HiddenReadOnlyFileName; |
| 246 | gfaTestsFile[4].expectedAttribs = FILE_ATTRIBUTE_READONLY; //| |
| 247 | //FILE_ATTRIBUTE_HIDDEN; |
| 248 | gfaTestsFile[4].isFile = TYPE_FILE; |
| 249 | |
| 250 | |
| 251 | gfaTestsFile[5].name = NotReallyAFileName; |
| 252 | gfaTestsFile[5].expectedAttribs = INVALID_FILE_ATTRIBUTES; |
| 253 | gfaTestsFile[5].isFile = TYPE_FILE; |
| 254 | |
| 255 | /* Initialize PAL environment */ |
| 256 | if (0 != PAL_Initialize(argc,argv)) |
| 257 | { |
| 258 | return FAIL; |
| 259 | } |
| 260 | |
| 261 | if(!CleanUpFiles()) |
| 262 | { |
| 263 | Fail("GetFileAttributesW: Pre-Clean Up Files Failed\n" ); |
| 264 | } |
| 265 | |
| 266 | if(0 == SetUpFiles()) |
| 267 | { |
| 268 | Fail("GetFileAttributesW: SetUp Files Failed\n" ); |
| 269 | } |
| 270 | |
| 271 | if(!CleanUpDirs()) |
| 272 | { |
| 273 | Fail("GetFileAttributesW: Pre-Clean Up Directories Failed\n" ); |
| 274 | } |
| 275 | |
| 276 | if(!SetUpDirs()) |
| 277 | { |
| 278 | Fail("GetFileAttributesW: SetUp Directories Failed\n" ); |
| 279 | } |
| 280 | |
| 281 | /* |
| 282 | * Go through all the test cases above, |
| 283 | * call GetFileAttributesW on the name and |
| 284 | * make sure the return value is the one expected |
| 285 | */ |
| 286 | for( i = 0; i < numFileTests; i++ ) |
| 287 | { |
| 288 | WStr = convert(gfaTestsFile[i].name); |
| 289 | result = GetFileAttributesW(WStr); |
| 290 | |
| 291 | if( result != gfaTestsFile[i].expectedAttribs ) |
| 292 | { |
| 293 | bFailed = TRUE; |
| 294 | |
| 295 | Trace("ERROR: GetFileAttributesW Test#%u on %s " |
| 296 | "returned %u instead of %u. \n" , |
| 297 | i, |
| 298 | gfaTestsFile[i].name, |
| 299 | result, |
| 300 | gfaTestsFile[i].expectedAttribs); |
| 301 | |
| 302 | } |
| 303 | free(WStr); |
| 304 | } |
| 305 | |
| 306 | |
| 307 | for( i = 0; i < numDirTests; i++ ) |
| 308 | { |
| 309 | WStr = convert(gfaTestsDir[i].name); |
| 310 | result = GetFileAttributesW(WStr); |
| 311 | |
| 312 | if( result != gfaTestsDir[i].expectedAttribs ) |
| 313 | { |
| 314 | bFailed = TRUE; |
| 315 | |
| 316 | Trace("ERROR: GetFileAttributesW on Directories Test#%u on %s " |
| 317 | "returned %u instead of %u. \n" , |
| 318 | i, |
| 319 | gfaTestsDir[i].name, |
| 320 | result, |
| 321 | gfaTestsDir[i].expectedAttribs); |
| 322 | |
| 323 | } |
| 324 | free(WStr); |
| 325 | } |
| 326 | |
| 327 | if(!CleanUpFiles()) |
| 328 | { |
| 329 | Fail("GetFileAttributesW: Post-Clean Up Files Failed\n" ); |
| 330 | } |
| 331 | |
| 332 | if(!CleanUpDirs()) |
| 333 | { |
| 334 | Fail("GetFileAttributesW: Post-Clean Up Directories Failed\n" ); |
| 335 | } |
| 336 | |
| 337 | /* If any errors, just call Fail() */ |
| 338 | if( bFailed ) |
| 339 | { |
| 340 | Fail("" ); |
| 341 | } |
| 342 | |
| 343 | PAL_Terminate(); |
| 344 | return PASS; |
| 345 | } |
| 346 | |