| 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: test1.c |
| 8 | ** |
| 9 | ** Purpose: Tests the PAL implementation of the GetFileAttributesExW function. |
| 10 | ** Call the function on a normal directory and file and a read-only directory |
| 11 | ** and file and a hidden file and directory. |
| 12 | ** Ensure that the returned attributes and file sizes are correct. |
| 13 | ** |
| 14 | ** |
| 15 | **===================================================================*/ |
| 16 | |
| 17 | #define UNICODE |
| 18 | #include <palsuite.h> |
| 19 | |
| 20 | typedef enum Item |
| 21 | { |
| 22 | IS_DIR, |
| 23 | IS_FILE |
| 24 | }ItemType; |
| 25 | |
| 26 | /* This function takes a structure and checks that the information |
| 27 | within the structure is correct. The 'Attribs' are the expected |
| 28 | file attributes, 'TheType' is IS_DIR or IS_FILE and the 'Name' is the |
| 29 | name of the file/directory in question. |
| 30 | */ |
| 31 | void VerifyInfo(WIN32_FILE_ATTRIBUTE_DATA InfoStruct, |
| 32 | DWORD Attribs, ItemType TheType, WCHAR* Name) |
| 33 | { |
| 34 | HANDLE hFile; |
| 35 | FILETIME CorrectCreation, CorrectAccess, CorrectModify; |
| 36 | WCHAR CopyName[64]; |
| 37 | |
| 38 | wcscpy(CopyName,Name); |
| 39 | free(Name); |
| 40 | |
| 41 | /* Check to see that the file attributes were recorded */ |
| 42 | if(InfoStruct.dwFileAttributes != Attribs) |
| 43 | { |
| 44 | Fail("ERROR: The file attributes on the file/directory were " |
| 45 | "recorded as being %d instead of %d.\n" , |
| 46 | InfoStruct.dwFileAttributes, |
| 47 | Attribs); |
| 48 | } |
| 49 | |
| 50 | /* Note: We can't open a handle to a directory in windows. This |
| 51 | block of tests will only be run on files. |
| 52 | */ |
| 53 | if(TheType == IS_FILE) |
| 54 | { |
| 55 | |
| 56 | /* Get a handle to the file */ |
| 57 | hFile = CreateFile(CopyName, |
| 58 | 0, |
| 59 | 0, |
| 60 | NULL, |
| 61 | OPEN_EXISTING, |
| 62 | FILE_ATTRIBUTE_NORMAL, |
| 63 | NULL); |
| 64 | |
| 65 | if (hFile == INVALID_HANDLE_VALUE) |
| 66 | { |
| 67 | Fail("ERROR: Could not open a handle to the file " |
| 68 | "'%S'. GetLastError() returned %d." ,CopyName, |
| 69 | GetLastError()); |
| 70 | } |
| 71 | |
| 72 | |
| 73 | if(InfoStruct.nFileSizeLow != GetFileSize(hFile,NULL)) |
| 74 | { |
| 75 | Fail("ERROR: The file size reported by GetFileAttributesEx " |
| 76 | "did not match the file size given by GetFileSize.\n" ); |
| 77 | } |
| 78 | |
| 79 | if(CloseHandle(hFile) == 0) |
| 80 | { |
| 81 | Fail("ERROR: Failed to properly close the handle to the " |
| 82 | "file we're testing. GetLastError() returned %d.\n" , |
| 83 | GetLastError()); |
| 84 | |
| 85 | } |
| 86 | |
| 87 | } |
| 88 | |
| 89 | |
| 90 | } |
| 91 | |
| 92 | /* Given a file/directory name, the expected attribs and whether or not it |
| 93 | is a file or directory, call GetFileAttributesEx and verify the |
| 94 | results are correct. |
| 95 | */ |
| 96 | |
| 97 | void RunTest(char* Name, DWORD Attribs, ItemType TheType ) |
| 98 | { |
| 99 | WCHAR* TheName; |
| 100 | WIN32_FILE_ATTRIBUTE_DATA InfoStruct; |
| 101 | DWORD TheResult; |
| 102 | |
| 103 | TheName = convert(Name); |
| 104 | |
| 105 | TheResult = GetFileAttributesEx(TheName, |
| 106 | GetFileExInfoStandard, |
| 107 | &InfoStruct); |
| 108 | if(TheResult == 0) |
| 109 | { |
| 110 | free(TheName); |
| 111 | Fail("ERROR: GetFileAttributesEx returned 0, indicating failure. " |
| 112 | "GetLastError returned %d.\n" ,GetLastError()); |
| 113 | } |
| 114 | |
| 115 | VerifyInfo(InfoStruct, Attribs, TheType, TheName); |
| 116 | |
| 117 | } |
| 118 | |
| 119 | int __cdecl main(int argc, char **argv) |
| 120 | { |
| 121 | DWORD TheResult; |
| 122 | WCHAR* FileName; |
| 123 | WIN32_FILE_ATTRIBUTE_DATA InfoStruct; |
| 124 | |
| 125 | if (0 != PAL_Initialize(argc,argv)) |
| 126 | { |
| 127 | return FAIL; |
| 128 | } |
| 129 | |
| 130 | /* Test a Directroy */ |
| 131 | RunTest("normal_test_directory" , FILE_ATTRIBUTE_DIRECTORY, IS_DIR); |
| 132 | |
| 133 | |
| 134 | /* Test a Normal File */ |
| 135 | |
| 136 | RunTest("normal_test_file" , FILE_ATTRIBUTE_NORMAL, IS_FILE); |
| 137 | |
| 138 | /* Test a Read-Only Directroy */ |
| 139 | |
| 140 | RunTest("ro_test_directory" , |
| 141 | FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_DIRECTORY, IS_DIR); |
| 142 | |
| 143 | /* Test a Read-Only File */ |
| 144 | |
| 145 | RunTest("ro_test_file" , FILE_ATTRIBUTE_READONLY, IS_FILE); |
| 146 | |
| 147 | /* Test a Hidden File */ |
| 148 | |
| 149 | RunTest(".hidden_file" , FILE_ATTRIBUTE_HIDDEN, IS_FILE); |
| 150 | |
| 151 | /* Test a Hidden Directroy */ |
| 152 | |
| 153 | RunTest(".hidden_directory" , |
| 154 | FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_DIRECTORY, IS_DIR); |
| 155 | |
| 156 | /* Test a Non-Existant File */ |
| 157 | |
| 158 | FileName = convert("nonexistent_test_file" ); |
| 159 | |
| 160 | TheResult = GetFileAttributesEx(FileName, |
| 161 | GetFileExInfoStandard, |
| 162 | &InfoStruct); |
| 163 | |
| 164 | if(TheResult != 0) |
| 165 | { |
| 166 | free(FileName); |
| 167 | Fail("ERROR: GetFileAttributesEx returned non-zero, indicating " |
| 168 | "success when it should have failed. It was called on a " |
| 169 | "non-existent file." ); |
| 170 | } |
| 171 | |
| 172 | free(FileName); |
| 173 | |
| 174 | PAL_Terminate(); |
| 175 | return PASS; |
| 176 | } |
| 177 | |