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