| 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: SetFileAttributesW.c |
| 8 | ** |
| 9 | ** Purpose: Tests the PAL implementation of the SetFileAttributesW function |
| 10 | ** Check that using two flags (READONLY and NORMAL) only sets the file |
| 11 | ** as READONLY, as MSDN notes that everything else overrides NORMAL. |
| 12 | ** |
| 13 | ** Depends: |
| 14 | ** CreateFile |
| 15 | ** CloseHandle |
| 16 | ** |
| 17 | ** |
| 18 | **===================================================================*/ |
| 19 | |
| 20 | #define UNICODE |
| 21 | |
| 22 | #include <palsuite.h> |
| 23 | |
| 24 | |
| 25 | |
| 26 | int __cdecl main(int argc, char **argv) |
| 27 | { |
| 28 | DWORD TheResult; |
| 29 | HANDLE TheFile; |
| 30 | CHAR *FileName_Multibyte = "test_file" ; |
| 31 | WCHAR FileName[MAX_PATH]; |
| 32 | |
| 33 | if (0 != PAL_Initialize(argc,argv)) |
| 34 | { |
| 35 | return FAIL; |
| 36 | } |
| 37 | |
| 38 | // Create the test file |
| 39 | FILE *testFile = fopen(FileName_Multibyte, "w" ); |
| 40 | if (testFile == NULL) |
| 41 | { |
| 42 | Fail("Unexpected error: Unable to open file %S with fopen. \n" , FileName); |
| 43 | } |
| 44 | if (fputs("testing" , testFile) == EOF) |
| 45 | { |
| 46 | Fail("Unexpected error: Unable to write to file %S with fputs. \n" , FileName); |
| 47 | } |
| 48 | if (fclose(testFile) != 0) |
| 49 | { |
| 50 | Fail("Unexpected error: Unable to close file %S with fclose. \n" , FileName); |
| 51 | } |
| 52 | testFile = NULL; |
| 53 | |
| 54 | /* Make a wide character string for the file name */ |
| 55 | |
| 56 | MultiByteToWideChar(CP_ACP, |
| 57 | 0, |
| 58 | FileName_Multibyte, |
| 59 | -1, |
| 60 | FileName, |
| 61 | MAX_PATH); |
| 62 | |
| 63 | |
| 64 | /* Try to set the file to Read-only|Normal ... It should |
| 65 | end up as Readonly, since this overrides Normal*/ |
| 66 | |
| 67 | TheResult = SetFileAttributes(FileName, |
| 68 | FILE_ATTRIBUTE_NORMAL| |
| 69 | FILE_ATTRIBUTE_READONLY); |
| 70 | |
| 71 | if(TheResult == 0) |
| 72 | { |
| 73 | Fail("ERROR: SetFileAttributes returned 0, failure, when trying " |
| 74 | "to set the FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_NORMAL " |
| 75 | "attribute." ); |
| 76 | } |
| 77 | |
| 78 | /* Attempt to open this READONLY file with WRITE access, |
| 79 | The open should fail and the HANDLE should be invalid. |
| 80 | */ |
| 81 | |
| 82 | TheFile = CreateFile( |
| 83 | FileName, // file name |
| 84 | GENERIC_READ|GENERIC_WRITE, // access mode |
| 85 | 0, // share mode |
| 86 | NULL, // SD |
| 87 | OPEN_ALWAYS, // how to create |
| 88 | FILE_ATTRIBUTE_NORMAL, // file attributes |
| 89 | NULL // handle to template file |
| 90 | ); |
| 91 | |
| 92 | if(TheFile != INVALID_HANDLE_VALUE) |
| 93 | { |
| 94 | Fail("ERROR: Tried to open a file that was created as " |
| 95 | "READONLY with the GENERIC_WRITE access mode. This should" |
| 96 | " cause CreateFile to return an INVALID_HANDLE_VALUE." ); |
| 97 | } |
| 98 | |
| 99 | /* Try to open the file with READ access, this should be ok. |
| 100 | The HANDLE will be valid. |
| 101 | */ |
| 102 | |
| 103 | TheFile = CreateFile( |
| 104 | FileName, // file name |
| 105 | GENERIC_READ, // access mode |
| 106 | 0, // share mode |
| 107 | NULL, // SD |
| 108 | OPEN_ALWAYS, // how to create |
| 109 | FILE_ATTRIBUTE_NORMAL, // file attributes |
| 110 | NULL // handle to template file |
| 111 | ); |
| 112 | |
| 113 | if(TheFile == INVALID_HANDLE_VALUE) |
| 114 | { |
| 115 | Fail("ERROR: Tried to open a file that was created as " |
| 116 | "READONLY with the GENERIC_READ access mode. This should" |
| 117 | " cause CreateFile to return an valid handle, but " |
| 118 | "INVALID_HANDLE_VALUE was returned!." ); |
| 119 | } |
| 120 | |
| 121 | /* Close that HANDLE */ |
| 122 | |
| 123 | TheResult = CloseHandle(TheFile); |
| 124 | |
| 125 | if(TheResult == 0) |
| 126 | { |
| 127 | Fail("ERROR: CloseHandle failed. This tests relies upon it " |
| 128 | "working properly." ); |
| 129 | } |
| 130 | |
| 131 | PAL_Terminate(); |
| 132 | return PASS; |
| 133 | } |
| 134 | |