[KMTESTS:CC] Add a trace to identify failing tests
[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 if (TestId > 1 && TestId < 4)
175 {
176 Fcb->Header.AllocationSize.QuadPart = VACB_MAPPING_GRANULARITY - PAGE_SIZE;
177 }
178
179 KmtStartSeh();
180 CcInitializeCacheMap(TestFileObject, (PCC_FILE_SIZES)&Fcb->Header.AllocationSize, TRUE, &Callbacks, NULL);
181 KmtEndSeh(STATUS_SUCCESS);
182
183 if (!skip(CcIsFileCached(TestFileObject) == TRUE, "CcInitializeCacheMap failed\n"))
184 {
185 trace("Starting test: %d\n", TestId);
186
187 if (TestId == 0 || TestId == 2)
188 {
189 Offset.QuadPart = 0;
190 KmtStartSeh();
191 Ret = CcMapData(TestFileObject, &Offset, VACB_MAPPING_GRANULARITY - PAGE_SIZE, MAP_WAIT, &Bcb, (PVOID *)&Buffer);
192 KmtEndSeh(STATUS_SUCCESS);
193
194 if (!skip(Ret == TRUE, "CcMapData failed\n"))
195 {
196 ok_eq_ulong(Buffer[(VACB_MAPPING_GRANULARITY - PAGE_SIZE - sizeof(ULONG)) / sizeof(ULONG)], 0xBABABABA);
197 CcUnpinData(Bcb);
198 }
199
200 KmtStartSeh();
201 CcSetFileSizes(TestFileObject, &NewFileSizes);
202 KmtEndSeh(STATUS_SUCCESS);
203
204 Fcb->Header.AllocationSize.QuadPart = VACB_MAPPING_GRANULARITY;
205 Fcb->Header.FileSize.QuadPart = VACB_MAPPING_GRANULARITY;
206
207 Offset.QuadPart = 0;
208 KmtStartSeh();
209 Ret = CcMapData(TestFileObject, &Offset, VACB_MAPPING_GRANULARITY, MAP_WAIT, &Bcb, (PVOID *)&Buffer);
210 KmtEndSeh(STATUS_SUCCESS);
211
212 if (!skip(Ret == TRUE, "CcMapData failed\n"))
213 {
214 ok_eq_ulong(Buffer[(VACB_MAPPING_GRANULARITY - sizeof(ULONG)) / sizeof(ULONG)], 0xBABABABA);
215
216 CcUnpinData(Bcb);
217 }
218 }
219 else if (TestId == 1 || TestId == 3)
220 {
221 Buffer = ExAllocatePool(NonPagedPool, PAGE_SIZE);
222 if (!skip(Buffer != NULL, "ExAllocatePool failed\n"))
223 {
224 Ret = FALSE;
225 Offset.QuadPart = VACB_MAPPING_GRANULARITY - 2 * PAGE_SIZE;
226
227 KmtStartSeh();
228 Ret = CcCopyRead(TestFileObject, &Offset, PAGE_SIZE, TRUE, Buffer, &IoStatus);
229 KmtEndSeh(STATUS_SUCCESS);
230
231 ok_eq_ulong(Buffer[(PAGE_SIZE - sizeof(ULONG)) / sizeof(ULONG)], 0xBABABABA);
232
233 KmtStartSeh();
234 CcSetFileSizes(TestFileObject, &NewFileSizes);
235 KmtEndSeh(STATUS_SUCCESS);
236
237 Fcb->Header.AllocationSize.QuadPart = VACB_MAPPING_GRANULARITY;
238 Fcb->Header.FileSize.QuadPart = VACB_MAPPING_GRANULARITY;
239 RtlZeroMemory(Buffer, PAGE_SIZE);
240
241 Offset.QuadPart = VACB_MAPPING_GRANULARITY - PAGE_SIZE;
242
243 KmtStartSeh();
244 Ret = CcCopyRead(TestFileObject, &Offset, PAGE_SIZE, TRUE, Buffer, &IoStatus);
245 KmtEndSeh(STATUS_SUCCESS);
246
247 ok_eq_ulong(Buffer[(PAGE_SIZE - sizeof(ULONG)) / sizeof(ULONG)], 0xBABABABA);
248
249 ExFreePool(Buffer);
250 }
251 }
252 }
253 }
254 }
255 }
256
257
258 static
259 VOID
260 CleanupTest(
261 ULONG TestId,
262 PDEVICE_OBJECT DeviceObject)
263 {
264 LARGE_INTEGER Zero = RTL_CONSTANT_LARGE_INTEGER(0LL);
265 CACHE_UNINITIALIZE_EVENT CacheUninitEvent;
266
267 ok_eq_pointer(TestDeviceObject, DeviceObject);
268 ok_eq_ulong(TestTestId, TestId);
269
270 if (!skip(TestFileObject != NULL, "No test FO\n"))
271 {
272 if (CcIsFileCached(TestFileObject))
273 {
274 KeInitializeEvent(&CacheUninitEvent.Event, NotificationEvent, FALSE);
275 CcUninitializeCacheMap(TestFileObject, &Zero, &CacheUninitEvent);
276 KeWaitForSingleObject(&CacheUninitEvent.Event, Executive, KernelMode, FALSE, NULL);
277 }
278
279 if (TestFileObject->FsContext != NULL)
280 {
281 ExFreePool(TestFileObject->FsContext);
282 TestFileObject->FsContext = NULL;
283 TestFileObject->SectionObjectPointer = NULL;
284 }
285
286 ObDereferenceObject(TestFileObject);
287 }
288
289 TestFileObject = NULL;
290 TestDeviceObject = NULL;
291 TestTestId = -1;
292 }
293
294
295 static
296 NTSTATUS
297 TestMessageHandler(
298 _In_ PDEVICE_OBJECT DeviceObject,
299 _In_ ULONG ControlCode,
300 _In_opt_ PVOID Buffer,
301 _In_ SIZE_T InLength,
302 _Inout_ PSIZE_T OutLength)
303 {
304 NTSTATUS Status = STATUS_SUCCESS;
305
306 FsRtlEnterFileSystem();
307
308 switch (ControlCode)
309 {
310 case IOCTL_START_TEST:
311 ok_eq_ulong((ULONG)InLength, sizeof(ULONG));
312 PerformTest(*(PULONG)Buffer, DeviceObject);
313 break;
314
315 case IOCTL_FINISH_TEST:
316 ok_eq_ulong((ULONG)InLength, sizeof(ULONG));
317 CleanupTest(*(PULONG)Buffer, DeviceObject);
318 break;
319
320 default:
321 Status = STATUS_NOT_IMPLEMENTED;
322 break;
323 }
324
325 FsRtlExitFileSystem();
326
327 return Status;
328 }
329
330 static
331 NTSTATUS
332 TestIrpHandler(
333 _In_ PDEVICE_OBJECT DeviceObject,
334 _In_ PIRP Irp,
335 _In_ PIO_STACK_LOCATION IoStack)
336 {
337 NTSTATUS Status;
338
339 PAGED_CODE();
340
341 DPRINT("IRP %x/%x\n", IoStack->MajorFunction, IoStack->MinorFunction);
342 ASSERT(IoStack->MajorFunction == IRP_MJ_READ);
343
344 FsRtlEnterFileSystem();
345
346 Status = STATUS_NOT_SUPPORTED;
347 Irp->IoStatus.Information = 0;
348
349 if (IoStack->MajorFunction == IRP_MJ_READ)
350 {
351 PMDL Mdl;
352 ULONG Length;
353 PVOID Buffer;
354 PTEST_FCB Fcb;
355 LARGE_INTEGER Offset;
356
357 Offset = IoStack->Parameters.Read.ByteOffset;
358 Length = IoStack->Parameters.Read.Length;
359 Fcb = IoStack->FileObject->FsContext;
360
361 ok_eq_pointer(DeviceObject, TestDeviceObject);
362 ok_eq_pointer(IoStack->FileObject, TestFileObject);
363 ok(Fcb != NULL, "Null FCB\n");
364
365 ok(FlagOn(Irp->Flags, IRP_NOCACHE), "Not coming from Cc\n");
366
367 ok_irql(APC_LEVEL);
368 ok((Offset.QuadPart % PAGE_SIZE == 0 || Offset.QuadPart == 0), "Offset is not aligned: %I64i\n", Offset.QuadPart);
369 ok(Length % PAGE_SIZE == 0, "Length is not aligned: %I64i\n", Length);
370
371 ok(Irp->AssociatedIrp.SystemBuffer == NULL, "A SystemBuffer was allocated!\n");
372 Buffer = MapAndLockUserBuffer(Irp, Length);
373 ok(Buffer != NULL, "Null pointer!\n");
374
375 if (Offset.QuadPart < Fcb->Header.FileSize.QuadPart)
376 {
377 RtlFillMemory(Buffer, min(Length, Fcb->Header.FileSize.QuadPart - Offset.QuadPart), 0xBA);
378 Buffer = (PVOID)((ULONG_PTR)Buffer + (ULONG_PTR)min(Length, Fcb->Header.FileSize.QuadPart - Offset.QuadPart));
379
380 if (Length > (Fcb->Header.FileSize.QuadPart - Offset.QuadPart))
381 {
382 RtlFillMemory(Buffer, Length - Fcb->Header.FileSize.QuadPart, 0xBD);
383 }
384 }
385 else
386 {
387 RtlFillMemory(Buffer, Length, 0xBD);
388 }
389
390 Status = STATUS_SUCCESS;
391
392 Mdl = Irp->MdlAddress;
393 ok(Mdl != NULL, "Null pointer for MDL!\n");
394 ok((Mdl->MdlFlags & MDL_PAGES_LOCKED) != 0, "MDL not locked\n");
395 ok((Mdl->MdlFlags & MDL_SOURCE_IS_NONPAGED_POOL) == 0, "MDL from non paged\n");
396 ok((Mdl->MdlFlags & MDL_IO_PAGE_READ) != 0, "Non paging IO\n");
397 ok((Irp->Flags & IRP_PAGING_IO) != 0, "Non paging IO\n");
398
399 Irp->IoStatus.Information = Length;
400 }
401
402 if (Status == STATUS_PENDING)
403 {
404 IoMarkIrpPending(Irp);
405 IoCompleteRequest(Irp, IO_NO_INCREMENT);
406 Status = STATUS_PENDING;
407 }
408 else
409 {
410 Irp->IoStatus.Status = Status;
411 IoCompleteRequest(Irp, IO_NO_INCREMENT);
412 }
413
414 FsRtlExitFileSystem();
415
416 return Status;
417 }