[KMTESTS:CC] Add tests for CcSetFileSizes
[reactos.git] / modules / rostests / kmtests / ntos_cc / CcSetFileSizes_drv.c
1 /*
2 * PROJECT: ReactOS kernel-mode tests
3 * LICENSE: LGPLv2.1+ - See COPYING.LIB in the top level directory
4 * PURPOSE: Test driver for CcSetFileSizes function
5 * PROGRAMMER: Pierre Schweitzer <pierre@reactos.org>
6 */
7
8 #include <kmt_test.h>
9
10 #define NDEBUG
11 #include <debug.h>
12
13 #define IOCTL_START_TEST 1
14 #define IOCTL_FINISH_TEST 2
15
16 typedef struct _TEST_FCB
17 {
18 FSRTL_ADVANCED_FCB_HEADER Header;
19 SECTION_OBJECT_POINTERS SectionObjectPointers;
20 FAST_MUTEX HeaderMutex;
21 } TEST_FCB, *PTEST_FCB;
22
23 static ULONG TestTestId = -1;
24 static PFILE_OBJECT TestFileObject;
25 static PDEVICE_OBJECT TestDeviceObject;
26 static KMT_IRP_HANDLER TestIrpHandler;
27 static KMT_MESSAGE_HANDLER TestMessageHandler;
28
29 NTSTATUS
30 TestEntry(
31 _In_ PDRIVER_OBJECT DriverObject,
32 _In_ PCUNICODE_STRING RegistryPath,
33 _Out_ PCWSTR *DeviceName,
34 _Inout_ INT *Flags)
35 {
36 NTSTATUS Status = STATUS_SUCCESS;
37
38 PAGED_CODE();
39
40 UNREFERENCED_PARAMETER(RegistryPath);
41
42 *DeviceName = L"CcSetFileSizes";
43 *Flags = TESTENTRY_NO_EXCLUSIVE_DEVICE |
44 TESTENTRY_BUFFERED_IO_DEVICE |
45 TESTENTRY_NO_READONLY_DEVICE;
46
47 KmtRegisterIrpHandler(IRP_MJ_READ, NULL, TestIrpHandler);
48 KmtRegisterMessageHandler(0, NULL, TestMessageHandler);
49
50 return Status;
51 }
52
53 VOID
54 TestUnload(
55 _In_ PDRIVER_OBJECT DriverObject)
56 {
57 PAGED_CODE();
58 }
59
60 BOOLEAN
61 NTAPI
62 AcquireForLazyWrite(
63 _In_ PVOID Context,
64 _In_ BOOLEAN Wait)
65 {
66 return TRUE;
67 }
68
69 VOID
70 NTAPI
71 ReleaseFromLazyWrite(
72 _In_ PVOID Context)
73 {
74 return;
75 }
76
77 BOOLEAN
78 NTAPI
79 AcquireForReadAhead(
80 _In_ PVOID Context,
81 _In_ BOOLEAN Wait)
82 {
83 return TRUE;
84 }
85
86 VOID
87 NTAPI
88 ReleaseFromReadAhead(
89 _In_ PVOID Context)
90 {
91 return;
92 }
93
94 static CACHE_MANAGER_CALLBACKS Callbacks = {
95 AcquireForLazyWrite,
96 ReleaseFromLazyWrite,
97 AcquireForReadAhead,
98 ReleaseFromReadAhead,
99 };
100
101 static CC_FILE_SIZES NewFileSizes = {
102 RTL_CONSTANT_LARGE_INTEGER((LONGLONG)VACB_MAPPING_GRANULARITY), // .AllocationSize
103 RTL_CONSTANT_LARGE_INTEGER((LONGLONG)VACB_MAPPING_GRANULARITY), // .FileSize
104 RTL_CONSTANT_LARGE_INTEGER((LONGLONG)VACB_MAPPING_GRANULARITY) // .ValidDataLength
105 };
106
107 static
108 PVOID
109 MapAndLockUserBuffer(
110 _In_ _Out_ PIRP Irp,
111 _In_ ULONG BufferLength)
112 {
113 PMDL Mdl;
114
115 if (Irp->MdlAddress == NULL)
116 {
117 Mdl = IoAllocateMdl(Irp->UserBuffer, BufferLength, FALSE, FALSE, Irp);
118 if (Mdl == NULL)
119 {
120 return NULL;
121 }
122
123 _SEH2_TRY
124 {
125 MmProbeAndLockPages(Mdl, Irp->RequestorMode, IoWriteAccess);
126 }
127 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
128 {
129 IoFreeMdl(Mdl);
130 Irp->MdlAddress = NULL;
131 _SEH2_YIELD(return NULL);
132 }
133 _SEH2_END;
134 }
135
136 return MmGetSystemAddressForMdlSafe(Irp->MdlAddress, NormalPagePriority);
137 }
138
139 static
140 VOID
141 PerformTest(
142 ULONG TestId,
143 PDEVICE_OBJECT DeviceObject)
144 {
145 PVOID Bcb;
146 BOOLEAN Ret;
147 PULONG Buffer;
148 PTEST_FCB Fcb;
149 LARGE_INTEGER Offset;
150 IO_STATUS_BLOCK IoStatus;
151
152 ok_eq_pointer(TestFileObject, NULL);
153 ok_eq_pointer(TestDeviceObject, NULL);
154 ok_eq_ulong(TestTestId, -1);
155
156 TestDeviceObject = DeviceObject;
157 TestTestId = TestId;
158 TestFileObject = IoCreateStreamFileObject(NULL, DeviceObject);
159 if (!skip(TestFileObject != NULL, "Failed to allocate FO\n"))
160 {
161 Fcb = ExAllocatePool(NonPagedPool, sizeof(TEST_FCB));
162 if (!skip(Fcb != NULL, "ExAllocatePool failed\n"))
163 {
164 RtlZeroMemory(Fcb, sizeof(TEST_FCB));
165 ExInitializeFastMutex(&Fcb->HeaderMutex);
166 FsRtlSetupAdvancedHeader(&Fcb->Header, &Fcb->HeaderMutex);
167
168 TestFileObject->FsContext = Fcb;
169 TestFileObject->SectionObjectPointer = &Fcb->SectionObjectPointers;
170 Fcb->Header.AllocationSize.QuadPart = VACB_MAPPING_GRANULARITY;
171 Fcb->Header.FileSize.QuadPart = VACB_MAPPING_GRANULARITY - PAGE_SIZE;
172 Fcb->Header.ValidDataLength.QuadPart = VACB_MAPPING_GRANULARITY - PAGE_SIZE;
173
174 KmtStartSeh();
175 CcInitializeCacheMap(TestFileObject, (PCC_FILE_SIZES)&Fcb->Header.AllocationSize, TRUE, &Callbacks, NULL);
176 KmtEndSeh(STATUS_SUCCESS);
177
178 if (!skip(CcIsFileCached(TestFileObject) == TRUE, "CcInitializeCacheMap failed\n"))
179 {
180 if (TestId == 0)
181 {
182 Offset.QuadPart = 0;
183 KmtStartSeh();
184 Ret = CcMapData(TestFileObject, &Offset, VACB_MAPPING_GRANULARITY - PAGE_SIZE, MAP_WAIT, &Bcb, (PVOID *)&Buffer);
185 KmtEndSeh(STATUS_SUCCESS);
186
187 if (!skip(Ret == TRUE, "CcMapData failed\n"))
188 {
189 ok_eq_ulong(Buffer[(VACB_MAPPING_GRANULARITY - PAGE_SIZE - sizeof(ULONG)) / sizeof(ULONG)], 0xBABABABA);
190 CcUnpinData(Bcb);
191 }
192
193 KmtStartSeh();
194 CcSetFileSizes(TestFileObject, &NewFileSizes);
195 KmtEndSeh(STATUS_SUCCESS);
196
197 Fcb->Header.FileSize.QuadPart = VACB_MAPPING_GRANULARITY;
198
199 Offset.QuadPart = 0;
200 KmtStartSeh();
201 Ret = CcMapData(TestFileObject, &Offset, VACB_MAPPING_GRANULARITY, MAP_WAIT, &Bcb, (PVOID *)&Buffer);
202 KmtEndSeh(STATUS_SUCCESS);
203
204 if (!skip(Ret == TRUE, "CcMapData failed\n"))
205 {
206 ok_eq_ulong(Buffer[(VACB_MAPPING_GRANULARITY - sizeof(ULONG)) / sizeof(ULONG)], 0xBABABABA);
207
208 CcUnpinData(Bcb);
209 }
210 }
211 else if (TestId == 1)
212 {
213 Buffer = ExAllocatePool(NonPagedPool, PAGE_SIZE);
214 if (!skip(Buffer != NULL, "ExAllocatePool failed\n"))
215 {
216 Ret = FALSE;
217 Offset.QuadPart = VACB_MAPPING_GRANULARITY - 2 * PAGE_SIZE;
218
219 KmtStartSeh();
220 Ret = CcCopyRead(TestFileObject, &Offset, PAGE_SIZE, TRUE, Buffer, &IoStatus);
221 KmtEndSeh(STATUS_SUCCESS);
222
223 ok_eq_ulong(Buffer[(PAGE_SIZE - sizeof(ULONG)) / sizeof(ULONG)], 0xBABABABA);
224
225 KmtStartSeh();
226 CcSetFileSizes(TestFileObject, &NewFileSizes);
227 KmtEndSeh(STATUS_SUCCESS);
228
229 Fcb->Header.FileSize.QuadPart = VACB_MAPPING_GRANULARITY;
230 RtlZeroMemory(Buffer, PAGE_SIZE);
231
232 Offset.QuadPart = VACB_MAPPING_GRANULARITY - PAGE_SIZE;
233
234 KmtStartSeh();
235 Ret = CcCopyRead(TestFileObject, &Offset, PAGE_SIZE, TRUE, Buffer, &IoStatus);
236 KmtEndSeh(STATUS_SUCCESS);
237
238 ok_eq_ulong(Buffer[(PAGE_SIZE - sizeof(ULONG)) / sizeof(ULONG)], 0xBABABABA);
239
240 ExFreePool(Buffer);
241 }
242 }
243 }
244 }
245 }
246 }
247
248
249 static
250 VOID
251 CleanupTest(
252 ULONG TestId,
253 PDEVICE_OBJECT DeviceObject)
254 {
255 LARGE_INTEGER Zero = RTL_CONSTANT_LARGE_INTEGER(0LL);
256 CACHE_UNINITIALIZE_EVENT CacheUninitEvent;
257
258 ok_eq_pointer(TestDeviceObject, DeviceObject);
259 ok_eq_ulong(TestTestId, TestId);
260
261 if (!skip(TestFileObject != NULL, "No test FO\n"))
262 {
263 if (CcIsFileCached(TestFileObject))
264 {
265 KeInitializeEvent(&CacheUninitEvent.Event, NotificationEvent, FALSE);
266 CcUninitializeCacheMap(TestFileObject, &Zero, &CacheUninitEvent);
267 KeWaitForSingleObject(&CacheUninitEvent.Event, Executive, KernelMode, FALSE, NULL);
268 }
269
270 if (TestFileObject->FsContext != NULL)
271 {
272 ExFreePool(TestFileObject->FsContext);
273 TestFileObject->FsContext = NULL;
274 TestFileObject->SectionObjectPointer = NULL;
275 }
276
277 ObDereferenceObject(TestFileObject);
278 }
279
280 TestFileObject = NULL;
281 TestDeviceObject = NULL;
282 TestTestId = -1;
283 }
284
285
286 static
287 NTSTATUS
288 TestMessageHandler(
289 _In_ PDEVICE_OBJECT DeviceObject,
290 _In_ ULONG ControlCode,
291 _In_opt_ PVOID Buffer,
292 _In_ SIZE_T InLength,
293 _Inout_ PSIZE_T OutLength)
294 {
295 NTSTATUS Status = STATUS_SUCCESS;
296
297 FsRtlEnterFileSystem();
298
299 switch (ControlCode)
300 {
301 case IOCTL_START_TEST:
302 ok_eq_ulong((ULONG)InLength, sizeof(ULONG));
303 PerformTest(*(PULONG)Buffer, DeviceObject);
304 break;
305
306 case IOCTL_FINISH_TEST:
307 ok_eq_ulong((ULONG)InLength, sizeof(ULONG));
308 CleanupTest(*(PULONG)Buffer, DeviceObject);
309 break;
310
311 default:
312 Status = STATUS_NOT_IMPLEMENTED;
313 break;
314 }
315
316 FsRtlExitFileSystem();
317
318 return Status;
319 }
320
321 static
322 NTSTATUS
323 TestIrpHandler(
324 _In_ PDEVICE_OBJECT DeviceObject,
325 _In_ PIRP Irp,
326 _In_ PIO_STACK_LOCATION IoStack)
327 {
328 NTSTATUS Status;
329
330 PAGED_CODE();
331
332 DPRINT("IRP %x/%x\n", IoStack->MajorFunction, IoStack->MinorFunction);
333 ASSERT(IoStack->MajorFunction == IRP_MJ_READ);
334
335 FsRtlEnterFileSystem();
336
337 Status = STATUS_NOT_SUPPORTED;
338 Irp->IoStatus.Information = 0;
339
340 if (IoStack->MajorFunction == IRP_MJ_READ)
341 {
342 PMDL Mdl;
343 ULONG Length;
344 PVOID Buffer;
345 PTEST_FCB Fcb;
346 LARGE_INTEGER Offset;
347
348 Offset = IoStack->Parameters.Read.ByteOffset;
349 Length = IoStack->Parameters.Read.Length;
350 Fcb = IoStack->FileObject->FsContext;
351
352 ok_eq_pointer(DeviceObject, TestDeviceObject);
353 ok_eq_pointer(IoStack->FileObject, TestFileObject);
354 ok(Fcb != NULL, "Null FCB\n");
355
356 ok(FlagOn(Irp->Flags, IRP_NOCACHE), "Not coming from Cc\n");
357
358 ok_irql(APC_LEVEL);
359 ok((Offset.QuadPart % PAGE_SIZE == 0 || Offset.QuadPart == 0), "Offset is not aligned: %I64i\n", Offset.QuadPart);
360 ok(Length % PAGE_SIZE == 0, "Length is not aligned: %I64i\n", Length);
361
362 ok(Irp->AssociatedIrp.SystemBuffer == NULL, "A SystemBuffer was allocated!\n");
363 Buffer = MapAndLockUserBuffer(Irp, Length);
364 ok(Buffer != NULL, "Null pointer!\n");
365
366 if (Offset.QuadPart < Fcb->Header.FileSize.QuadPart)
367 {
368 RtlFillMemory(Buffer, min(Length, Fcb->Header.FileSize.QuadPart - Offset.QuadPart), 0xBA);
369 Buffer = (PVOID)((ULONG_PTR)Buffer + (ULONG_PTR)min(Length, Fcb->Header.FileSize.QuadPart - Offset.QuadPart));
370
371 if (Length > (Fcb->Header.FileSize.QuadPart - Offset.QuadPart))
372 {
373 RtlFillMemory(Buffer, Length - Fcb->Header.FileSize.QuadPart, 0xBD);
374 }
375 }
376 else
377 {
378 RtlFillMemory(Buffer, Length, 0xBD);
379 }
380
381 Status = STATUS_SUCCESS;
382
383 Mdl = Irp->MdlAddress;
384 ok(Mdl != NULL, "Null pointer for MDL!\n");
385 ok((Mdl->MdlFlags & MDL_PAGES_LOCKED) != 0, "MDL not locked\n");
386 ok((Mdl->MdlFlags & MDL_SOURCE_IS_NONPAGED_POOL) == 0, "MDL from non paged\n");
387 ok((Mdl->MdlFlags & MDL_IO_PAGE_READ) != 0, "Non paging IO\n");
388 ok((Irp->Flags & IRP_PAGING_IO) != 0, "Non paging IO\n");
389
390 Irp->IoStatus.Information = Length;
391 }
392
393 if (Status == STATUS_PENDING)
394 {
395 IoMarkIrpPending(Irp);
396 IoCompleteRequest(Irp, IO_NO_INCREMENT);
397 Status = STATUS_PENDING;
398 }
399 else
400 {
401 Irp->IoStatus.Status = Status;
402 IoCompleteRequest(Irp, IO_NO_INCREMENT);
403 }
404
405 FsRtlExitFileSystem();
406
407 return Status;
408 }