| 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: GetFileAttributesA.c |
| 8 | ** |
| 9 | ** Purpose: Tests the PAL implementation of the GetFileAttributesA 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 | /* Tests on directory */ |
| 198 | gfaTestsDir[0].name = NormalDirectoryName; |
| 199 | gfaTestsDir[0].expectedAttribs = FILE_ATTRIBUTE_DIRECTORY; |
| 200 | gfaTestsDir[0].isFile = TYPE_DIR; |
| 201 | |
| 202 | gfaTestsDir[1].name = ReadOnlyDirectoryName; |
| 203 | gfaTestsDir[1].expectedAttribs = FILE_ATTRIBUTE_DIRECTORY | |
| 204 | FILE_ATTRIBUTE_READONLY; |
| 205 | gfaTestsDir[1].isFile = TYPE_DIR; |
| 206 | |
| 207 | gfaTestsDir[2].name = ReadWriteDirectoryName; |
| 208 | gfaTestsDir[2].expectedAttribs = FILE_ATTRIBUTE_DIRECTORY; |
| 209 | gfaTestsDir[2].isFile = TYPE_DIR; |
| 210 | |
| 211 | gfaTestsDir[3].name = HiddenDirectoryName; |
| 212 | gfaTestsDir[3].expectedAttribs = FILE_ATTRIBUTE_DIRECTORY; //| |
| 213 | //FILE_ATTRIBUTE_HIDDEN; |
| 214 | gfaTestsDir[3].isFile = TYPE_DIR; |
| 215 | |
| 216 | gfaTestsDir[4].name = HiddenReadOnlyDirectoryName; |
| 217 | gfaTestsDir[4].expectedAttribs = FILE_ATTRIBUTE_DIRECTORY | |
| 218 | FILE_ATTRIBUTE_READONLY; //| |
| 219 | //FILE_ATTRIBUTE_HIDDEN; |
| 220 | gfaTestsDir[4].isFile = TYPE_DIR; |
| 221 | |
| 222 | gfaTestsDir[5].name = NoDirectoryName; |
| 223 | gfaTestsDir[5].expectedAttribs = INVALID_FILE_ATTRIBUTES; |
| 224 | gfaTestsDir[5].isFile = TYPE_DIR; |
| 225 | |
| 226 | /* Tests on file */ |
| 227 | gfaTestsFile[0].name = NormalFileName; |
| 228 | gfaTestsFile[0].expectedAttribs = FILE_ATTRIBUTE_NORMAL; |
| 229 | gfaTestsFile[0].isFile = TYPE_FILE; |
| 230 | |
| 231 | |
| 232 | gfaTestsFile[1].name = ReadOnlyFileName; |
| 233 | gfaTestsFile[1].expectedAttribs = FILE_ATTRIBUTE_READONLY; |
| 234 | gfaTestsFile[1].isFile = TYPE_FILE; |
| 235 | |
| 236 | gfaTestsFile[2].name = ReadWriteFileName; |
| 237 | gfaTestsFile[2].expectedAttribs = FILE_ATTRIBUTE_NORMAL; |
| 238 | gfaTestsFile[2].isFile = TYPE_FILE; |
| 239 | |
| 240 | gfaTestsFile[3].name = HiddenFileName; |
| 241 | gfaTestsFile[3].expectedAttribs = FILE_ATTRIBUTE_NORMAL; //FILE_ATTRIBUTE_HIDDEN; |
| 242 | gfaTestsFile[3].isFile = TYPE_FILE; |
| 243 | |
| 244 | gfaTestsFile[4].name = HiddenReadOnlyFileName; |
| 245 | gfaTestsFile[4].expectedAttribs = FILE_ATTRIBUTE_READONLY; //| |
| 246 | //FILE_ATTRIBUTE_HIDDEN; |
| 247 | gfaTestsFile[4].isFile = TYPE_FILE; |
| 248 | |
| 249 | |
| 250 | gfaTestsFile[5].name = NotReallyAFileName; |
| 251 | gfaTestsFile[5].expectedAttribs = INVALID_FILE_ATTRIBUTES; |
| 252 | gfaTestsFile[5].isFile = TYPE_FILE; |
| 253 | |
| 254 | /* Initialize PAL environment */ |
| 255 | if (0 != PAL_Initialize(argc,argv)) |
| 256 | { |
| 257 | return FAIL; |
| 258 | } |
| 259 | |
| 260 | if(!CleanUpFiles()) |
| 261 | { |
| 262 | Fail("GetFileAttributesA: Pre-Clean Up Files Failed\n" ); |
| 263 | } |
| 264 | |
| 265 | if(0 == SetUpFiles()) |
| 266 | { |
| 267 | Fail("GetFileAttributesA: SetUp Files Failed\n" ); |
| 268 | } |
| 269 | |
| 270 | if(!CleanUpDirs()) |
| 271 | { |
| 272 | Fail("GetFileAttributesA: Pre-Clean Up Directories Failed\n" ); |
| 273 | } |
| 274 | |
| 275 | if(!SetUpDirs()) |
| 276 | { |
| 277 | Fail("GetFileAttributesA: SetUp Directories Failed\n" ); |
| 278 | } |
| 279 | |
| 280 | /* |
| 281 | * Go through all the test cases above, |
| 282 | * call GetFileAttributesA on the name and |
| 283 | * make sure the return value is the one expected |
| 284 | */ |
| 285 | for( i = 0; i < numFileTests; i++ ) |
| 286 | { |
| 287 | result = GetFileAttributesA(gfaTestsFile[i].name); |
| 288 | |
| 289 | if( result != gfaTestsFile[i].expectedAttribs ) |
| 290 | { |
| 291 | bFailed = TRUE; |
| 292 | |
| 293 | Trace("ERROR: GetFileAttributesA Test#%u on %s " |
| 294 | "returned %u instead of %u. \n" , |
| 295 | i, |
| 296 | gfaTestsFile[i].name, |
| 297 | result, |
| 298 | gfaTestsFile[i].expectedAttribs); |
| 299 | |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | |
| 304 | for( i = 0; i < numDirTests; i++ ) |
| 305 | { |
| 306 | result = GetFileAttributesA(gfaTestsDir[i].name); |
| 307 | |
| 308 | if( result != gfaTestsDir[i].expectedAttribs ) |
| 309 | { |
| 310 | bFailed = TRUE; |
| 311 | |
| 312 | Trace("ERROR: GetFileAttributesA on Directories Test#%u on %s " |
| 313 | "returned %u instead of %u. \n" , |
| 314 | i, |
| 315 | gfaTestsDir[i].name, |
| 316 | result, |
| 317 | gfaTestsDir[i].expectedAttribs); |
| 318 | |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | if(!CleanUpFiles()) |
| 323 | { |
| 324 | Fail("GetFileAttributesA: Post-Clean Up Files Failed\n" ); |
| 325 | } |
| 326 | |
| 327 | if(!CleanUpDirs()) |
| 328 | { |
| 329 | Fail("GetFileAttributesA: Post-Clean Up Directories Failed\n" ); |
| 330 | } |
| 331 | |
| 332 | /* If any errors, just call Fail() */ |
| 333 | if( bFailed ) |
| 334 | { |
| 335 | Fail("" ); |
| 336 | } |
| 337 | |
| 338 | PAL_Terminate(); |
| 339 | return PASS; |
| 340 | } |
| 341 | |