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 the defined attributes aside from READONLY on a
11** file, and that it doesn't return failure. Note, these attributes won't
12** do anything to the file, however.
13**
14**
15**===================================================================*/
16
17#define UNICODE
18
19#include <palsuite.h>
20
21/* this cleanup method tries to revert the file back to its initial attributes */
22void do_cleanup(WCHAR* filename, DWORD attributes)
23{
24 DWORD result;
25 result = SetFileAttributes(filename, attributes);
26 if (result == 0)
27 {
28 Fail("ERROR:SetFileAttributesW 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
33int __cdecl main(int argc, char **argv)
34{
35 DWORD TheResult;
36 DWORD initialAttr;
37 CHAR *FileName_Multibyte = "test_file";
38 WCHAR FileName[MAX_PATH];
39
40 if (0 != PAL_Initialize(argc,argv))
41 {
42 return FAIL;
43 }
44
45 // Create the test file
46 FILE *testFile = fopen(FileName_Multibyte, "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 /* Make a wide character string for the file name */
62
63 MultiByteToWideChar(CP_ACP,
64 0,
65 FileName_Multibyte,
66 -1,
67 FileName,
68 MAX_PATH);
69
70 /* Get the initial attributes of the file */
71 initialAttr = GetFileAttributesW(FileName);
72
73 /* Try to set the file to HIDDEN */
74
75 TheResult = SetFileAttributes(FileName,FILE_ATTRIBUTE_HIDDEN);
76
77 if(TheResult == 0)
78 {
79 do_cleanup(FileName,initialAttr);
80 Fail("ERROR: SetFileAttributes returned 0, failure, when trying "
81 "to set the FILE_ATTRIBUTE_HIDDEN attribute. This should "
82 "not do anything in FreeBSD, but it shouldn't fail.");
83 }
84
85 /* Try to set the file to ARCHIVE */
86
87 TheResult = SetFileAttributes(FileName,FILE_ATTRIBUTE_ARCHIVE);
88
89 if(TheResult == 0)
90 {
91 do_cleanup(FileName,initialAttr);
92 Fail("ERROR: SetFileAttributes returned 0, failure, when trying "
93 "to set the FILE_ATTRIBUTE_ARCHIVE attribute.");
94 }
95
96 /* Try to set the file to SYSTEM */
97
98 TheResult = SetFileAttributes(FileName,FILE_ATTRIBUTE_SYSTEM);
99
100 if(TheResult == 0)
101 {
102 do_cleanup(FileName,initialAttr);
103 Fail("ERROR: SetFileAttributes returned 0, failure, when trying "
104 "to set the FILE_ATTRIBUTE_SYSTEM attribute.");
105 }
106
107 /* Try to set the file to DIRECTORY */
108
109 TheResult = SetFileAttributes(FileName,FILE_ATTRIBUTE_DIRECTORY);
110
111 if(TheResult == 0)
112 {
113 do_cleanup(FileName,initialAttr);
114 Fail("ERROR: SetFileAttributes returned 0, failure, when trying "
115 "to set the FILE_ATTRIBUTE_DIRECTORY attribute.");
116 }
117
118 do_cleanup(FileName,initialAttr);
119 PAL_Terminate();
120 return PASS;
121}
122