FreeRDP
TestSetFileAttributes.c
1 
2 #include <stdio.h>
3 #include <winpr/crt.h>
4 #include <winpr/file.h>
5 #include <winpr/path.h>
6 #include <winpr/handle.h>
7 #include <winpr/windows.h>
8 #include <winpr/sysinfo.h>
9 
10 static const DWORD allflags[] = {
11  0,
12  FILE_ATTRIBUTE_READONLY,
13  FILE_ATTRIBUTE_HIDDEN,
14  FILE_ATTRIBUTE_SYSTEM,
15  FILE_ATTRIBUTE_DIRECTORY,
16  FILE_ATTRIBUTE_ARCHIVE,
17  FILE_ATTRIBUTE_DEVICE,
18  FILE_ATTRIBUTE_NORMAL,
19  FILE_ATTRIBUTE_TEMPORARY,
20  FILE_ATTRIBUTE_SPARSE_FILE,
21  FILE_ATTRIBUTE_REPARSE_POINT,
22  FILE_ATTRIBUTE_COMPRESSED,
23  FILE_ATTRIBUTE_OFFLINE,
24  FILE_ATTRIBUTE_NOT_CONTENT_INDEXED,
25  FILE_ATTRIBUTE_ENCRYPTED,
26  FILE_ATTRIBUTE_VIRTUAL,
27  FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM,
28  FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_DEVICE |
29  FILE_ATTRIBUTE_NORMAL,
30  FILE_ATTRIBUTE_TEMPORARY | FILE_ATTRIBUTE_SPARSE_FILE | FILE_ATTRIBUTE_REPARSE_POINT |
31  FILE_ATTRIBUTE_COMPRESSED | FILE_ATTRIBUTE_OFFLINE,
32  FILE_ATTRIBUTE_NOT_CONTENT_INDEXED | FILE_ATTRIBUTE_ENCRYPTED | FILE_ATTRIBUTE_VIRTUAL
33 };
34 
35 static BOOL test_SetFileAttributesA(void)
36 {
37  BOOL rc = FALSE;
38  HANDLE handle = NULL;
39  const DWORD flags[] = { 0, FILE_ATTRIBUTE_READONLY };
40  char* name = GetKnownSubPath(KNOWN_PATH_TEMP, "afsklhjwe4oq5iu432oijrlkejadlkhjaklhfdkahfd");
41  if (!name)
42  goto fail;
43 
44  for (size_t x = 0; x < ARRAYSIZE(allflags); x++)
45  {
46  const DWORD flag = allflags[x];
47  const BOOL brc = SetFileAttributesA(NULL, flag);
48  if (brc)
49  goto fail;
50 
51  const BOOL crc = SetFileAttributesA(name, flag);
52  if (crc)
53  goto fail;
54  }
55 
56  handle = CreateFileA(name, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_NEW,
57  FILE_ATTRIBUTE_NORMAL, NULL);
58  if (handle == INVALID_HANDLE_VALUE)
59  goto fail;
60  (void)CloseHandle(handle);
61 
62  for (size_t x = 0; x < ARRAYSIZE(flags); x++)
63  {
64  DWORD attr = 0;
65  const DWORD flag = flags[x];
66  const BOOL brc = SetFileAttributesA(name, flag);
67  if (!brc)
68  goto fail;
69 
70  attr = GetFileAttributesA(name);
71  if (flag != 0)
72  {
73  if ((attr & flag) == 0)
74  goto fail;
75  }
76  }
77 
78  rc = TRUE;
79 
80 fail:
81  DeleteFileA(name);
82  free(name);
83  return rc;
84 }
85 
86 static BOOL test_SetFileAttributesW(void)
87 {
88  BOOL rc = FALSE;
89  WCHAR* name = NULL;
90  HANDLE handle = NULL;
91  const DWORD flags[] = { 0, FILE_ATTRIBUTE_READONLY };
92  char* base = GetKnownSubPath(KNOWN_PATH_TEMP, "afsklhjwe4oq5iu432oijrlkejadlkhjaklhfdkahfd");
93  if (!base)
94  goto fail;
95 
96  name = ConvertUtf8ToWCharAlloc(base, NULL);
97  if (!name)
98  goto fail;
99 
100  for (size_t x = 0; x < ARRAYSIZE(allflags); x++)
101  {
102  const DWORD flag = allflags[x];
103  const BOOL brc = SetFileAttributesW(NULL, flag);
104  if (brc)
105  goto fail;
106 
107  const BOOL crc = SetFileAttributesW(name, flag);
108  if (crc)
109  goto fail;
110  }
111 
112  handle = CreateFileW(name, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_NEW,
113  FILE_ATTRIBUTE_NORMAL, NULL);
114  if (handle == INVALID_HANDLE_VALUE)
115  goto fail;
116  (void)CloseHandle(handle);
117 
118  for (size_t x = 0; x < ARRAYSIZE(flags); x++)
119  {
120  DWORD attr = 0;
121  const DWORD flag = flags[x];
122  const BOOL brc = SetFileAttributesW(name, flag);
123  if (!brc)
124  goto fail;
125 
126  attr = GetFileAttributesW(name);
127  if (flag != 0)
128  {
129  if ((attr & flag) == 0)
130  goto fail;
131  }
132  }
133 
134  rc = TRUE;
135 fail:
136  DeleteFileW(name);
137  free(name);
138  free(base);
139  return rc;
140 }
141 
142 int TestSetFileAttributes(int argc, char* argv[])
143 {
144  WINPR_UNUSED(argc);
145  WINPR_UNUSED(argv);
146 
147  if (!test_SetFileAttributesA())
148  return -1;
149  if (!test_SetFileAttributesW())
150  return -1;
151  return 0;
152 }