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 | ** Test that we can set a file READONLY, and then that we're unable to |
12 | ** open that file with WRITE access. Then change it to NORMAL attributes, and |
13 | ** try to open it again -- it should work now. |
14 | ** |
15 | ** Depends: |
16 | ** CreateFile |
17 | ** CloseHandle |
18 | ** |
19 | ** |
20 | **===================================================================*/ |
21 | |
22 | /* According to the spec, only READONLY attribute can be set |
23 | in FreeBSD. |
24 | */ |
25 | |
26 | #include <palsuite.h> |
27 | |
28 | |
29 | |
30 | int __cdecl main(int argc, char **argv) |
31 | { |
32 | DWORD TheResult; |
33 | HANDLE TheFile; |
34 | char* FileName = {"test_file" }; |
35 | |
36 | if (0 != PAL_Initialize(argc,argv)) |
37 | { |
38 | return FAIL; |
39 | } |
40 | |
41 | // Create the test file |
42 | FILE *testFile = fopen(FileName, "w" ); |
43 | if (testFile == NULL) |
44 | { |
45 | Fail("Unexpected error: Unable to open file %S with fopen. \n" , FileName); |
46 | } |
47 | if (fputs("testing" , testFile) == EOF) |
48 | { |
49 | Fail("Unexpected error: Unable to write to file %S with fputs. \n" , FileName); |
50 | } |
51 | if (fclose(testFile) != 0) |
52 | { |
53 | Fail("Unexpected error: Unable to close file %S with fclose. \n" , FileName); |
54 | } |
55 | testFile = NULL; |
56 | |
57 | /* Try to set the file to Read-only */ |
58 | |
59 | TheResult = SetFileAttributesA(FileName, FILE_ATTRIBUTE_READONLY); |
60 | |
61 | if(TheResult == 0) |
62 | { |
63 | Fail("ERROR: SetFileAttributesA returned 0, failure, when trying " |
64 | "to set the FILE_ATTRIBUTE_READONLY 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 | Fail("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 CreateFile 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 CreateFile 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 | /* Set the file to NORMAL */ |
127 | |
128 | TheResult = SetFileAttributesA(FileName, FILE_ATTRIBUTE_NORMAL); |
129 | |
130 | if(TheResult == 0) |
131 | { |
132 | Fail("ERROR: SetFileAttributesA returned 0, failure, when trying " |
133 | "to set the FILE_ATTRIBUTE_NORMAL attribute." ); |
134 | } |
135 | |
136 | /* To ensure that the set worked correctly, try to open the file |
137 | with WRITE access again -- this time it should succeed. |
138 | */ |
139 | |
140 | TheFile = CreateFileA( |
141 | FileName, // file name |
142 | GENERIC_READ|GENERIC_WRITE, // access mode |
143 | 0, // share mode |
144 | NULL, // SD |
145 | OPEN_ALWAYS, // how to create |
146 | FILE_ATTRIBUTE_NORMAL, // file attributes |
147 | NULL // handle to template file |
148 | ); |
149 | |
150 | if(TheFile == INVALID_HANDLE_VALUE) |
151 | { |
152 | Fail("ERROR: Tried to open a file that was created as " |
153 | "NORMAL with the GENERIC_WRITE access mode. This should" |
154 | " cause CreateFile to return an valid handle, but " |
155 | "INVALID_HANDLE_VALUE was returned!." ); |
156 | } |
157 | |
158 | TheResult = CloseHandle(TheFile); |
159 | |
160 | if(TheResult == 0) |
161 | { |
162 | Fail("ERROR: CloseHandle failed. This tests relies upon it " |
163 | "working properly." ); |
164 | } |
165 | |
166 | PAL_Terminate(); |
167 | return PASS; |
168 | } |
169 | |