forked from winfsp/winfsp
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmount-test.c
391 lines (328 loc) · 12.9 KB
/
mount-test.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/**
* @file mount-test.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 <winfsp/winfsp.h>
#include <tlib/testsuite.h>
#include <process.h>
#include <strsafe.h>
#include "winfsp-tests.h"
void mount_invalid_test(void)
{
NTSTATUS Result;
FSP_FSCTL_VOLUME_PARAMS VolumeParams = { 0 };
WCHAR VolumeName[MAX_PATH] = L"foo";
HANDLE VolumeHandle;
VolumeParams.SectorSize = 16384;
VolumeParams.VolumeSerialNumber = 0x12345678;
Result = FspFsctlCreateVolume(L"WinFsp.DoesNotExist", &VolumeParams,
VolumeName, sizeof VolumeName, &VolumeHandle);
ASSERT(STATUS_NO_SUCH_DEVICE == Result);
ASSERT(L'\0' == VolumeName[0]);
ASSERT(INVALID_HANDLE_VALUE == VolumeHandle);
}
void mount_open_device_dotest(PWSTR DeviceName)
{
BOOL Success;
HANDLE DeviceHandle;
WCHAR DevicePath[MAX_PATH];
StringCbPrintfW(DevicePath, sizeof DevicePath, L"\\\\?\\GLOBALROOT\\Device\\%s", DeviceName);
DeviceHandle = CreateFileW(DevicePath,
0, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);
ASSERT(INVALID_HANDLE_VALUE != DeviceHandle);
Success = CloseHandle(DeviceHandle);
ASSERT(Success);
}
void mount_open_device_test(void)
{
if (WinFspDiskTests)
mount_open_device_dotest(L"WinFsp.Disk");
if (WinFspNetTests)
mount_open_device_dotest(L"WinFsp.Net");
}
void mount_create_volume_v0_dotest(PWSTR DeviceName)
{
NTSTATUS Result;
BOOL Success;
FSP_FSCTL_VOLUME_PARAMS_V0 VolumeParams = { 0 };
WCHAR VolumeName[MAX_PATH];
HANDLE VolumeHandle;
VolumeParams.SectorSize = 16384;
VolumeParams.VolumeSerialNumber = 0x12345678;
wcscpy_s(VolumeParams.Prefix, sizeof VolumeParams.Prefix / sizeof(WCHAR), L"\\winfsp-tests\\share");
Result = FspFsctlCreateVolume(DeviceName, (FSP_FSCTL_VOLUME_PARAMS *)&VolumeParams,
VolumeName, sizeof VolumeName, &VolumeHandle);
ASSERT(STATUS_SUCCESS == Result);
ASSERT(0 == wcsncmp(L"\\Device\\Volume{", VolumeName, 15));
ASSERT(INVALID_HANDLE_VALUE != VolumeHandle);
Success = CloseHandle(VolumeHandle);
ASSERT(Success);
}
void mount_create_volume_v0_test(void)
{
if (WinFspDiskTests)
mount_create_volume_v0_dotest(L"WinFsp.Disk");
if (WinFspNetTests)
mount_create_volume_v0_dotest(L"WinFsp.Net");
}
void mount_create_volume_dotest(PWSTR DeviceName)
{
NTSTATUS Result;
BOOL Success;
FSP_FSCTL_VOLUME_PARAMS VolumeParams = { .Version = sizeof VolumeParams };
WCHAR VolumeName[MAX_PATH];
HANDLE VolumeHandle;
VolumeParams.SectorSize = 16384;
VolumeParams.VolumeSerialNumber = 0x12345678;
wcscpy_s(VolumeParams.Prefix, sizeof VolumeParams.Prefix / sizeof(WCHAR), L"\\winfsp-tests\\share");
Result = FspFsctlCreateVolume(DeviceName, &VolumeParams,
VolumeName, sizeof VolumeName, &VolumeHandle);
ASSERT(STATUS_SUCCESS == Result);
ASSERT(0 == wcsncmp(L"\\Device\\Volume{", VolumeName, 15));
ASSERT(INVALID_HANDLE_VALUE != VolumeHandle);
Success = CloseHandle(VolumeHandle);
ASSERT(Success);
}
void mount_create_volume_test(void)
{
if (WinFspDiskTests)
mount_create_volume_dotest(L"WinFsp.Disk");
if (WinFspNetTests)
mount_create_volume_dotest(L"WinFsp.Net");
}
static unsigned __stdcall mount_volume_cancel_dotest_thread(void *FilePath)
{
FspDebugLog(__FUNCTION__ ": \"%S\"\n", FilePath);
HANDLE Handle;
Handle = CreateFileW(FilePath,
GENERIC_READ | GENERIC_WRITE, 0, 0, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0);
if (INVALID_HANDLE_VALUE == Handle)
return GetLastError();
CloseHandle(Handle);
return 0;
}
void mount_volume_cancel_dotest(PWSTR DeviceName, PWSTR Prefix)
{
NTSTATUS Result;
BOOL Success;
FSP_FSCTL_VOLUME_PARAMS VolumeParams = { 0 };
WCHAR VolumeName[MAX_PATH];
WCHAR FilePath[MAX_PATH];
HANDLE VolumeHandle;
HANDLE Thread;
DWORD ExitCode;
VolumeParams.SectorSize = 16384;
VolumeParams.VolumeSerialNumber = 0x12345678;
wcscpy_s(VolumeParams.Prefix, sizeof VolumeParams.Prefix / sizeof(WCHAR), L"\\winfsp-tests\\share");
Result = FspFsctlCreateVolume(DeviceName, &VolumeParams,
VolumeName, sizeof VolumeName, &VolumeHandle);
ASSERT(STATUS_SUCCESS == Result);
ASSERT(0 == wcsncmp(L"\\Device\\Volume{", VolumeName, 15));
ASSERT(INVALID_HANDLE_VALUE != VolumeHandle);
StringCbPrintfW(FilePath, sizeof FilePath, L"%s%s\\file0",
Prefix ? L"" : L"\\\\?\\GLOBALROOT", Prefix ? Prefix : VolumeName);
Thread = (HANDLE)_beginthreadex(0, 0, mount_volume_cancel_dotest_thread, FilePath, 0, 0);
ASSERT(0 != Thread);
Sleep(0 == Prefix ? 1000 : 5000); /* give some time to the thread to execute */
Success = CloseHandle(VolumeHandle);
ASSERT(Success);
WaitForSingleObject(Thread, INFINITE);
GetExitCodeThread(Thread, &ExitCode);
CloseHandle(Thread);
ASSERT(ERROR_OPERATION_ABORTED == ExitCode);
}
void mount_volume_cancel_test(void)
{
if (WinFspDiskTests)
mount_volume_cancel_dotest(L"WinFsp.Disk", 0);
if (WinFspNetTests)
mount_volume_cancel_dotest(L"WinFsp.Net", L"\\\\winfsp-tests\\share");
}
static unsigned __stdcall mount_volume_transact_dotest_thread(void *FilePath)
{
FspDebugLog(__FUNCTION__ ": \"%S\"\n", FilePath);
HANDLE Handle;
Handle = CreateFileW(FilePath,
FILE_GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0);
if (INVALID_HANDLE_VALUE == Handle)
return GetLastError();
CloseHandle(Handle);
return 0;
}
void mount_volume_transact_dotest(PWSTR DeviceName, PWSTR Prefix)
{
NTSTATUS Result;
BOOL Success;
FSP_FSCTL_VOLUME_PARAMS VolumeParams = { 0 };
WCHAR VolumeName[MAX_PATH];
WCHAR FilePath[MAX_PATH];
HANDLE VolumeHandle;
HANDLE Thread;
DWORD ExitCode;
VolumeParams.TransactTimeout = 10000; /* allow for longer transact timeout to handle MUP redir */
VolumeParams.SectorSize = 16384;
VolumeParams.VolumeSerialNumber = 0x12345678;
wcscpy_s(VolumeParams.Prefix, sizeof VolumeParams.Prefix / sizeof(WCHAR), L"\\winfsp-tests\\share");
Result = FspFsctlCreateVolume(DeviceName, &VolumeParams,
VolumeName, sizeof VolumeName, &VolumeHandle);
ASSERT(STATUS_SUCCESS == Result);
ASSERT(0 == wcsncmp(L"\\Device\\Volume{", VolumeName, 15));
ASSERT(INVALID_HANDLE_VALUE != VolumeHandle);
StringCbPrintfW(FilePath, sizeof FilePath, L"%s%s\\file0",
Prefix ? L"" : L"\\\\?\\GLOBALROOT", Prefix ? Prefix : VolumeName);
Thread = (HANDLE)_beginthreadex(0, 0, mount_volume_transact_dotest_thread, FilePath, 0, 0);
ASSERT(0 != Thread);
Sleep(1000); /* give some time to the thread to execute */
FSP_FSCTL_DECLSPEC_ALIGN UINT8 RequestBuf[FSP_FSCTL_TRANSACT_BATCH_BUFFER_SIZEMIN];
FSP_FSCTL_DECLSPEC_ALIGN UINT8 ResponseBuf[FSP_FSCTL_TRANSACT_RSP_SIZEMAX];
UINT8 *RequestBufEnd;
UINT8 *ResponseBufEnd = ResponseBuf + sizeof ResponseBuf;
SIZE_T RequestBufSize;
SIZE_T ResponseBufSize;
FSP_FSCTL_TRANSACT_REQ *Request = (PVOID)RequestBuf, *NextRequest;
FSP_FSCTL_TRANSACT_RSP *Response = (PVOID)ResponseBuf;
ResponseBufSize = 0;
RequestBufSize = sizeof RequestBuf;
Result = FspFsctlTransact(VolumeHandle, 0, 0, RequestBuf, &RequestBufSize, TRUE);
ASSERT(STATUS_SUCCESS == Result);
RequestBufEnd = RequestBuf + RequestBufSize;
NextRequest = FspFsctlTransactConsumeRequest(Request, RequestBufEnd);
ASSERT(0 != NextRequest);
ASSERT(0 == Request->Version);
ASSERT(FSP_FSCTL_TRANSACT_REQ_SIZEMAX >= Request->Size);
ASSERT(0 != Request->Hint);
ASSERT(FspFsctlTransactCreateKind == Request->Kind ||
FspFsctlTransactQueryVolumeInformationKind == Request->Kind);
if (FspFsctlTransactCreateKind == Request->Kind)
{
ASSERT(FILE_CREATE == ((Request->Req.Create.CreateOptions >> 24) & 0xff));
ASSERT(0 == Request->Req.Create.FileAttributes);
ASSERT(0 == Request->Req.Create.SecurityDescriptor.Offset);
ASSERT(0 == Request->Req.Create.SecurityDescriptor.Size);
ASSERT(0 == Request->Req.Create.AllocationSize);
ASSERT(FILE_GENERIC_READ == Request->Req.Create.DesiredAccess);
ASSERT((FILE_SHARE_READ | FILE_SHARE_WRITE) == Request->Req.Create.ShareAccess);
ASSERT(0 == Request->Req.Create.Ea.Offset);
ASSERT(0 == Request->Req.Create.Ea.Size);
ASSERT(Request->Req.Create.UserMode);
ASSERT(!OptNoTraverseToken == Request->Req.Create.HasTraversePrivilege);
ASSERT(!Request->Req.Create.OpenTargetDirectory);
ASSERT(!Request->Req.Create.CaseSensitive);
ASSERT(0 == Request->FileName.Offset);
ASSERT((wcslen((PVOID)Request->Buffer) + 1) * sizeof(WCHAR) == Request->FileName.Size);
ASSERT(0 == mywcscmp((PVOID)Request->Buffer, -1, L"\\file0", -1));
}
ASSERT(FspFsctlTransactCanProduceResponse(Response, ResponseBufEnd));
RtlZeroMemory(Response, sizeof *Response);
Response->Size = sizeof *Response;
Response->Hint = Request->Hint;
Response->Kind = Request->Kind;
Response->IoStatus.Status = STATUS_ACCESS_DENIED;
Response->IoStatus.Information = 0;
Response = FspFsctlTransactProduceResponse(Response, Response->Size);
ASSERT(0 != Response);
Request = NextRequest;
NextRequest = FspFsctlTransactConsumeRequest(Request, RequestBufEnd);
ASSERT(0 == NextRequest);
ResponseBufSize = (PUINT8)Response - ResponseBuf;
RequestBufSize = 0;
Result = FspFsctlTransact(VolumeHandle, ResponseBuf, ResponseBufSize, 0, &RequestBufSize, TRUE);
ASSERT(STATUS_SUCCESS == Result);
Success = CloseHandle(VolumeHandle);
ASSERT(Success);
WaitForSingleObject(Thread, INFINITE);
GetExitCodeThread(Thread, &ExitCode);
CloseHandle(Thread);
ASSERT(ERROR_ACCESS_DENIED == ExitCode || ERROR_OPERATION_ABORTED == ExitCode);
}
void mount_volume_transact_test(void)
{
if (WinFspDiskTests)
mount_volume_transact_dotest(L"WinFsp.Disk", 0);
if (WinFspNetTests)
mount_volume_transact_dotest(L"WinFsp.Net", L"\\\\winfsp-tests\\share");
}
void mount_preflight_dotest(PWSTR DeviceName)
{
NTSTATUS Result;
WCHAR MountPoint[MAX_PATH];
WCHAR DirBuf[MAX_PATH];
DWORD Drives;
WCHAR Drive;
BOOL Success;
MountPoint[0] = L'C';
MountPoint[1] = L':';
MountPoint[2] = L'\0';
GetTestDirectory(DirBuf);
/*
* Mount points starting with \\?\X: or \\.\X: are now considered MountManager mountpoints.
* So skip the \\?\ prefix to avoid this problem.
*/
if (L'\\' == DirBuf[0] &&
L'\\' == DirBuf[1] &&
(L'?' == DirBuf[2] || L'.' == DirBuf[2]) &&
L'\\' == DirBuf[3])
memmove(DirBuf, DirBuf + 4, (wcslen(DirBuf + 4) + 1) * sizeof(WCHAR));
Drives = GetLogicalDrives();
ASSERT(0 != Drives);
Result = FspFileSystemPreflight(DeviceName, 0);
ASSERT(STATUS_SUCCESS == Result);
for (Drive = 'Z'; 'A' <= Drive; Drive--)
if (0 == (Drives & (1 << (Drive - 'A'))))
break;
ASSERT('A' <= Drive);
MountPoint[0] = Drive;
Result = FspFileSystemPreflight(DeviceName, MountPoint);
ASSERT(STATUS_SUCCESS == Result);
for (Drive = 'Z'; 'A' <= Drive; Drive--)
if (0 != (Drives & (1 << (Drive - 'A'))))
break;
ASSERT('A' <= Drive);
MountPoint[0] = Drive;
Result = FspFileSystemPreflight(DeviceName, MountPoint);
ASSERT(STATUS_OBJECT_NAME_COLLISION == Result);
StringCbPrintfW(MountPoint, sizeof MountPoint, L"%s\\dir1", DirBuf);
Result = FspFileSystemPreflight(DeviceName, MountPoint);
ASSERT(STATUS_SUCCESS == Result);
Success = CreateDirectoryW(MountPoint, 0);
ASSERT(Success);
Result = FspFileSystemPreflight(DeviceName, MountPoint);
ASSERT(STATUS_OBJECT_NAME_COLLISION == Result);
Success = RemoveDirectoryW(MountPoint);
ASSERT(Success);
}
void mount_preflight_test(void)
{
if (WinFspDiskTests)
mount_preflight_dotest(L"WinFsp.Disk");
if (WinFspNetTests)
mount_preflight_dotest(L"WinFsp.Net");
}
void mount_tests(void)
{
if (OptExternal || OptOplock)
return;
TEST_OPT(mount_invalid_test);
TEST_OPT(mount_open_device_test);
TEST_OPT(mount_create_volume_v0_test);
TEST_OPT(mount_create_volume_test);
TEST_OPT(mount_volume_cancel_test);
TEST_OPT(mount_volume_transact_test);
TEST_OPT(mount_preflight_test);
}