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 the defined attributes aside from READONLY on a |
12 | ** file, and that it doesn't return failure. Note, these attributes won't |
13 | ** do anything to the file, however. |
14 | ** |
15 | ** |
16 | **===================================================================*/ |
17 | |
18 | |
19 | #include <palsuite.h> |
20 | |
21 | /* this cleanup method tries to revert the file back to its initial attributes */ |
22 | void do_cleanup(char* filename, DWORD attributes) |
23 | { |
24 | DWORD result; |
25 | result = SetFileAttributesA(filename, attributes); |
26 | if (result == 0) |
27 | { |
28 | Fail("ERROR:SetFileAttributesA returned 0,failure in the do_cleanup " |
29 | "method when trying to revert the file back to its initial attributes (%u)" , GetLastError()); |
30 | } |
31 | } |
32 | |
33 | int __cdecl main(int argc, char **argv) |
34 | { |
35 | DWORD TheResult; |
36 | DWORD initialAttr; |
37 | |
38 | char* FileName = {"test_file" }; |
39 | |
40 | if (0 != PAL_Initialize(argc,argv)) |
41 | { |
42 | return FAIL; |
43 | } |
44 | |
45 | // Create the test file |
46 | FILE *testFile = fopen(FileName, "w" ); |
47 | if (testFile == NULL) |
48 | { |
49 | Fail("Unexpected error: Unable to open file %S with fopen. \n" , FileName); |
50 | } |
51 | if (fputs("testing" , testFile) == EOF) |
52 | { |
53 | Fail("Unexpected error: Unable to write to file %S with fputs. \n" , FileName); |
54 | } |
55 | if (fclose(testFile) != 0) |
56 | { |
57 | Fail("Unexpected error: Unable to close file %S with fclose. \n" , FileName); |
58 | } |
59 | testFile = NULL; |
60 | |
61 | /* Get the initial attributes of the file */ |
62 | |
63 | initialAttr = GetFileAttributesA(FileName); |
64 | |
65 | /* Try to set the file to HIDDEN */ |
66 | |
67 | TheResult = SetFileAttributesA(FileName, FILE_ATTRIBUTE_HIDDEN); |
68 | |
69 | if(TheResult == 0) |
70 | { |
71 | do_cleanup(FileName,initialAttr); |
72 | Fail("ERROR: SetFileAttributesA returned 0, failure, when trying " |
73 | "to set the FILE_ATTRIBUTE_HIDDEN attribute. This should " |
74 | "not do anything in FreeBSD, but it shouldn't fail." ); |
75 | } |
76 | |
77 | /* Try to set the file to ARCHIVE */ |
78 | |
79 | TheResult = SetFileAttributesA(FileName, FILE_ATTRIBUTE_ARCHIVE); |
80 | |
81 | if(TheResult == 0) |
82 | { |
83 | do_cleanup(FileName,initialAttr); |
84 | Fail("ERROR: SetFileAttributesA returned 0, failure, when trying " |
85 | "to set the FILE_ATTRIBUTE_ARCHIVE attribute." ); |
86 | } |
87 | |
88 | /* Try to set the file to SYSTEM */ |
89 | |
90 | TheResult = SetFileAttributesA(FileName, FILE_ATTRIBUTE_SYSTEM); |
91 | |
92 | if(TheResult == 0) |
93 | { |
94 | do_cleanup(FileName,initialAttr); |
95 | Fail("ERROR: SetFileAttributesA returned 0, failure, when trying " |
96 | "to set the FILE_ATTRIBUTE_SYSTEM attribute." ); |
97 | } |
98 | |
99 | /* Try to set the file to DIRECTORY */ |
100 | |
101 | TheResult = SetFileAttributesA(FileName, FILE_ATTRIBUTE_DIRECTORY); |
102 | |
103 | if(TheResult == 0) |
104 | { |
105 | do_cleanup(FileName,initialAttr); |
106 | Fail("ERROR: SetFileAttributesA returned 0, failure, when trying " |
107 | "to set the FILE_ATTRIBUTE_DIRECTORY attribute." ); |
108 | } |
109 | |
110 | |
111 | do_cleanup(FileName,initialAttr); |
112 | PAL_Terminate(); |
113 | return PASS; |
114 | } |
115 | |