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 | ** Test that we can set a file READONLY, and then that we're unable to |
11 | ** open that file with WRITE access. Then change it to NORMAL attributes, and |
12 | ** try to open it again -- it should work now. |
13 | ** |
14 | ** Depends: |
15 | ** CreateFile |
16 | ** CloseHandle |
17 | ** |
18 | ** |
19 | **===================================================================*/ |
20 | |
21 | /* According to the spec, only READONLY attribute can be set |
22 | in FreeBSD. |
23 | */ |
24 | |
25 | #define UNICODE |
26 | |
27 | #include <palsuite.h> |
28 | |
29 | |
30 | |
31 | int __cdecl main(int argc, char **argv) |
32 | { |
33 | DWORD TheResult; |
34 | HANDLE TheFile; |
35 | CHAR *FileName_Multibyte = "test_file" ; |
36 | WCHAR FileName[MAX_PATH]; |
37 | |
38 | if (0 != PAL_Initialize(argc,argv)) |
39 | { |
40 | return FAIL; |
41 | } |
42 | |
43 | // Create the test file |
44 | FILE *testFile = fopen(FileName_Multibyte, "w" ); |
45 | if (testFile == NULL) |
46 | { |
47 | Fail("Unexpected error: Unable to open file %S with fopen. \n" , FileName); |
48 | } |
49 | if (fputs("testing" , testFile) == EOF) |
50 | { |
51 | Fail("Unexpected error: Unable to write to file %S with fputs. \n" , FileName); |
52 | } |
53 | if (fclose(testFile) != 0) |
54 | { |
55 | Fail("Unexpected error: Unable to close file %S with fclose. \n" , FileName); |
56 | } |
57 | testFile = NULL; |
58 | |
59 | /* Make a wide character string for the file name */ |
60 | |
61 | MultiByteToWideChar(CP_ACP, |
62 | 0, |
63 | FileName_Multibyte, |
64 | -1, |
65 | FileName, |
66 | MAX_PATH); |
67 | |
68 | |
69 | /* Try to set the file to Read-only */ |
70 | |
71 | TheResult = SetFileAttributes(FileName,FILE_ATTRIBUTE_READONLY); |
72 | |
73 | if(TheResult == 0) |
74 | { |
75 | Fail("ERROR: SetFileAttributes returned 0, failure, when trying " |
76 | "to set the FILE_ATTRIBUTE_READONLY attribute." ); |
77 | } |
78 | |
79 | /* Attempt to open this READONLY file with WRITE access, |
80 | The open should fail and the HANDLE should be invalid. |
81 | */ |
82 | |
83 | TheFile = CreateFile( |
84 | FileName, // file name |
85 | GENERIC_READ|GENERIC_WRITE, // access mode |
86 | 0, // share mode |
87 | NULL, // SD |
88 | OPEN_ALWAYS, // how to create |
89 | FILE_ATTRIBUTE_NORMAL, // file attributes |
90 | NULL // handle to template file |
91 | ); |
92 | |
93 | if(TheFile != INVALID_HANDLE_VALUE) |
94 | { |
95 | Fail("ERROR: Tried to open a file that was created as " |
96 | "READONLY with the GENERIC_WRITE access mode. This should" |
97 | " cause CreateFile to return an INVALID_HANDLE_VALUE." ); |
98 | } |
99 | |
100 | /* Try to open the file with READ access, this should be ok. |
101 | The HANDLE will be valid. |
102 | */ |
103 | |
104 | TheFile = CreateFile( |
105 | FileName, // file name |
106 | GENERIC_READ, // access mode |
107 | 0, // share mode |
108 | NULL, // SD |
109 | OPEN_ALWAYS, // how to create |
110 | FILE_ATTRIBUTE_NORMAL, // file attributes |
111 | NULL // handle to template file |
112 | ); |
113 | |
114 | if(TheFile == INVALID_HANDLE_VALUE) |
115 | { |
116 | Fail("ERROR: Tried to open a file that was created as " |
117 | "READONLY with the GENERIC_READ access mode. This should" |
118 | " cause CreateFile to return an valid handle, but " |
119 | "INVALID_HANDLE_VALUE was returned!." ); |
120 | } |
121 | |
122 | /* Close that HANDLE */ |
123 | |
124 | TheResult = CloseHandle(TheFile); |
125 | |
126 | if(TheResult == 0) |
127 | { |
128 | Fail("ERROR: CloseHandle failed. This tests relies upon it " |
129 | "working properly." ); |
130 | } |
131 | |
132 | /* Set the file to NORMAL */ |
133 | |
134 | TheResult = SetFileAttributes(FileName,FILE_ATTRIBUTE_NORMAL); |
135 | |
136 | if(TheResult == 0) |
137 | { |
138 | Fail("ERROR: SetFileAttributes returned 0, failure, when trying " |
139 | "to set the FILE_ATTRIBUTE_NORMAL attribute." ); |
140 | } |
141 | |
142 | /* To ensure that the set worked correctly, try to open the file |
143 | with WRITE access again -- this time it should succeed. |
144 | */ |
145 | |
146 | TheFile = CreateFile( |
147 | FileName, // file name |
148 | GENERIC_READ|GENERIC_WRITE, // access mode |
149 | 0, // share mode |
150 | NULL, // SD |
151 | OPEN_ALWAYS, // how to create |
152 | FILE_ATTRIBUTE_NORMAL, // file attributes |
153 | NULL // handle to template file |
154 | ); |
155 | |
156 | if(TheFile == INVALID_HANDLE_VALUE) |
157 | { |
158 | Fail("ERROR: Tried to open a file that was created as " |
159 | "NORMAL with the GENERIC_WRITE access mode. This should" |
160 | " cause CreateFile to return an valid handle, but " |
161 | "INVALID_HANDLE_VALUE was returned!." ); |
162 | } |
163 | |
164 | |
165 | PAL_Terminate(); |
166 | return PASS; |
167 | } |
168 | |