forked from winfsp/winfsp
-
Notifications
You must be signed in to change notification settings - Fork 5
/
winfsp-tests.c
363 lines (314 loc) · 10.7 KB
/
winfsp-tests.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/**
* @file winfsp-tests.c
*
* @copyright 2015-2020 Bill Zissimopoulos
*/
/*
* This file is part of WinFsp.
*
* You can redistribute it and/or modify it under the terms of the GNU
* General Public License version 3 as published by the Free Software
* Foundation.
*
* Licensees holding a valid commercial license may use this software
* in accordance with the commercial license agreement provided in
* conjunction with the software. The terms and conditions of any such
* commercial license agreement shall govern, supersede, and render
* ineffective any application of the GPLv3 license to this software,
* notwithstanding of any reference thereto in the software or
* associated repository.
*/
#include <windows.h>
#include <lm.h>
#include <signal.h>
#include <tlib/testsuite.h>
#include <time.h>
#define WINFSP_TESTS_NO_HOOKS
#include "winfsp-tests.h"
int NtfsTests = 0;
int WinFspDiskTests = 1;
int WinFspNetTests = 1;
BOOLEAN OptExternal = FALSE;
BOOLEAN OptResilient = FALSE;
BOOLEAN OptCaseInsensitiveCmp = FALSE;
BOOLEAN OptCaseInsensitive = FALSE;
BOOLEAN OptCaseRandomize = FALSE;
BOOLEAN OptFlushAndPurgeOnCleanup = FALSE;
WCHAR OptOplock = 0;
WCHAR OptMountPointBuf[MAX_PATH], *OptMountPoint;
WCHAR OptShareNameBuf[MAX_PATH], *OptShareName, *OptShareTarget;
WCHAR OptShareComputer[MAX_PATH] = L"\\\\localhost\\";
ULONG OptSharePrefixLength; /* only counts single leading slash: \localhost\target\path */
HANDLE OptNoTraverseToken = 0;
LUID OptNoTraverseLuid;
static void exiting(void);
int mywcscmp(PWSTR a, int alen, PWSTR b, int blen)
{
int len, res;
if (-1 == alen)
alen = (int)wcslen(a);
if (-1 == blen)
blen = (int)wcslen(b);
len = alen < blen ? alen : blen;
/* we should still be in the C locale */
if (OptCaseInsensitiveCmp)
res = _wcsnicmp(a, b, len);
else
res = wcsncmp(a, b, len);
if (0 == res)
res = alen - blen;
return res;
}
static unsigned myrandseed = 1;
int myrand(void)
{
/*
* This mimics MSVCRT rand(); we need our own version
* as to not interfere with the program's rand().
*/
myrandseed = myrandseed * 214013 + 2531011;
return (myrandseed >> 16) & RAND_MAX;
}
VOID GetTestDirectoryEx(PWSTR DirBuf, ULONG DirBufSize, PWSTR DriveBuf)
{
DirBufSize /= sizeof(WCHAR);
if (MAX_PATH > DirBufSize)
ABORT("test directory buffer must be at least MAX_PATH long");
DWORD Result = GetCurrentDirectoryW(DirBufSize - 4, DirBuf + 4);
if (0 == Result || Result >= DirBufSize - 4)
ABORT("GetCurrentDirectoryW failed");
if (!testalpha(DirBuf[4]) || L':' != DirBuf[5])
ABORT("--ntfs/--external tests must be run from a drive");
DirBuf[0] = L'\\';
DirBuf[1] = L'\\';
DirBuf[2] = L'?';
DirBuf[3] = L'\\';
if (L'\\' == DirBuf[6] && L'\0' == DirBuf[7])
DirBuf[6] = L'\0';
if (0 != DriveBuf)
{
DriveBuf[0] = DirBuf[4];
DriveBuf[1] = L':';
DriveBuf[2] = L'\0';
}
}
static VOID DisableBackupRestorePrivileges(VOID)
{
union
{
TOKEN_PRIVILEGES P;
UINT B[sizeof(TOKEN_PRIVILEGES) + sizeof(LUID_AND_ATTRIBUTES)];
} Privileges;
HANDLE Token;
Privileges.P.PrivilegeCount = 2;
Privileges.P.Privileges[0].Attributes = 0;
Privileges.P.Privileges[1].Attributes = 0;
if (!LookupPrivilegeValueW(0, SE_BACKUP_NAME, &Privileges.P.Privileges[0].Luid) ||
!LookupPrivilegeValueW(0, SE_RESTORE_NAME, &Privileges.P.Privileges[1].Luid))
ABORT("cannot lookup backup/restore privileges");
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &Token))
ABORT("cannot open process token");
if (!AdjustTokenPrivileges(Token, FALSE, &Privileges.P, 0, 0, 0))
ABORT("cannot disable backup/restore privileges");
CloseHandle(Token);
}
static VOID AddNetShareIfNeeded(VOID)
{
if (!OptShareTarget)
return;
SHARE_INFO_2 ShareInfo = { 0 };
NET_API_STATUS NetStatus;
ShareInfo.shi2_netname = OptShareName;
ShareInfo.shi2_type = STYPE_DISKTREE;
ShareInfo.shi2_permissions = ACCESS_ALL;
ShareInfo.shi2_max_uses = -1;
ShareInfo.shi2_path = OptShareTarget;
NetShareDel(0, OptShareName, 0);
NetStatus = NetShareAdd(0, 2, (PBYTE)&ShareInfo, 0);
if (NERR_Success != NetStatus)
ABORT("cannot add network share");
}
static VOID RemoveNetShareIfNeeded(VOID)
{
if (!OptShareTarget)
return;
NetShareDel(0, OptShareName, 0);
}
static void abort_handler(int sig)
{
DWORD Error = GetLastError();
exiting();
SetLastError(Error);
}
LONG WINAPI UnhandledExceptionHandler(struct _EXCEPTION_POINTERS *ExceptionInfo)
{
exiting();
return EXCEPTION_EXECUTE_HANDLER;
}
#define rmarg(argv, argc, argi) \
argc--,\
memmove(argv + argi, argv + argi + 1, (argc - argi) * sizeof(char *)),\
argi--,\
argv[argc] = 0
int main(int argc, char *argv[])
{
TESTSUITE(fuse_opt_tests);
TESTSUITE(fuse_tests);
TESTSUITE(posix_tests);
TESTSUITE(uuid5_tests);
TESTSUITE(eventlog_tests);
TESTSUITE(path_tests);
TESTSUITE(dirbuf_tests);
TESTSUITE(version_tests);
TESTSUITE(launch_tests);
TESTSUITE(launcher_ptrans_tests);
TESTSUITE(mount_tests);
TESTSUITE(timeout_tests);
TESTSUITE(memfs_tests);
TESTSUITE(create_tests);
TESTSUITE(info_tests);
TESTSUITE(security_tests);
TESTSUITE(rdwr_tests);
TESTSUITE(flush_tests);
TESTSUITE(lock_tests);
TESTSUITE(dirctl_tests);
TESTSUITE(exec_tests);
TESTSUITE(devctl_tests);
TESTSUITE(reparse_tests);
TESTSUITE(ea_tests);
TESTSUITE(stream_tests);
TESTSUITE(oplock_tests);
TESTSUITE(wsl_tests);
TESTSUITE(volpath_tests);
atexit(exiting);
signal(SIGABRT, abort_handler);
SetUnhandledExceptionFilter(UnhandledExceptionHandler);
for (int argi = 1; argc > argi; argi++)
{
const char *a = argv[argi];
if ('-' == a[0])
{
if (0 == strcmp("--ntfs", a) || 0 == strcmp("--external", a))
{
OptExternal = TRUE;
NtfsTests = 1;
WinFspDiskTests = 0;
WinFspNetTests = 0;
rmarg(argv, argc, argi);
}
else if (0 == strcmp("--resilient", a))
{
OptResilient = TRUE;
rmarg(argv, argc, argi);
}
else if (0 == strcmp("--case-insensitive-cmp", a))
{
OptCaseInsensitiveCmp = TRUE;
rmarg(argv, argc, argi);
}
else if (0 == strcmp("--case-insensitive", a))
{
OptCaseInsensitive = TRUE;
rmarg(argv, argc, argi);
}
else if (0 == strcmp("--case-randomize", a))
{
OptCaseRandomize = TRUE;
OptCaseInsensitive = TRUE;
OptCaseInsensitiveCmp = TRUE;
rmarg(argv, argc, argi);
}
else if (0 == strcmp("--flush-and-purge-on-cleanup", a))
{
OptFlushAndPurgeOnCleanup = TRUE;
rmarg(argv, argc, argi);
}
else if (0 == strcmp("--oplock=batch", a))
{
OptOplock = 'B';
rmarg(argv, argc, argi);
}
else if (0 == strcmp("--oplock=filter", a))
{
OptOplock = 'F';
rmarg(argv, argc, argi);
}
else if (0 == strncmp("--mountpoint=", a, sizeof "--mountpoint=" - 1))
{
if (0 != MultiByteToWideChar(CP_UTF8, 0,
a + sizeof "--mountpoint=" - 1, -1, OptMountPointBuf, MAX_PATH))
{
OptMountPoint = OptMountPointBuf;
rmarg(argv, argc, argi);
if (!(testalpha(OptMountPoint[0]) &&
L':' == OptMountPoint[1] &&
L'\0' == OptMountPoint[2]))
WinFspNetTests = 0;
}
}
else if (0 == strncmp("--share=", a, sizeof "--share=" - 1))
{
if (0 != MultiByteToWideChar(CP_UTF8, 0,
a + sizeof "--share=" - 1, -1, OptShareNameBuf, MAX_PATH))
{
OptShareTarget = wcschr(OptShareNameBuf, L'=');
if (OptShareTarget)
{
*OptShareTarget++ = L'\0';
OptShareName = OptShareNameBuf;
}
else if (L'\\' == OptShareNameBuf[0] && L'\\' == OptShareNameBuf[1])
{
OptShareName = wcschr(OptShareNameBuf + 2, L'\\');
if (OptShareName)
{
OptShareName++;
memcpy(OptShareComputer, OptShareNameBuf,
(OptShareName - OptShareNameBuf) * sizeof(WCHAR));
OptShareComputer[OptShareName - OptShareNameBuf] = L'\0';
}
}
}
if (OptShareName)
{
rmarg(argv, argc, argi);
OptSharePrefixLength = (ULONG)
((wcslen(OptShareComputer) + wcslen(OptShareName) - 1) * sizeof(WCHAR));
WinFspDiskTests = 0;
WinFspNetTests = 0;
}
}
else if (0 == strncmp("--share-prefix=", a, sizeof "--share-prefix=" - 1))
{
/* hack to allow name queries on network file systems with mapped drives */
WCHAR SharePrefixBuf[MAX_PATH];
if (0 != MultiByteToWideChar(CP_UTF8, 0,
a + sizeof "--share-prefix=" - 1, -1, SharePrefixBuf, MAX_PATH))
{
rmarg(argv, argc, argi);
OptSharePrefixLength = (ULONG)(wcslen(SharePrefixBuf) * sizeof(WCHAR));
}
}
else if (0 == strcmp("--no-traverse", a))
{
if (LookupPrivilegeValueW(0, SE_CHANGE_NOTIFY_NAME, &OptNoTraverseLuid) &&
OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &OptNoTraverseToken))
{
rmarg(argv, argc, argi);
}
}
}
}
if (!NtfsTests && OptShareName)
ABORT("option --share requires --ntfs/--external");
DisableBackupRestorePrivileges();
AddNetShareIfNeeded();
myrandseed = (unsigned)time(0);
tlib_run_tests(argc, argv);
return 0;
}
static void exiting(void)
{
OutputDebugStringA("winfsp-tests: exiting\n");
RemoveNetShareIfNeeded();
}