[KMTESTS:CC] Properly check for memory allocations
[reactos.git] / modules / rostests / kmtests / ntos_cc / CcMapData_drv.c
index 6eded83..5f7dc93 100644 (file)
@@ -27,7 +27,6 @@ typedef struct _TEST_CONTEXT
     ULONG Length;
 } TEST_CONTEXT, *PTEST_CONTEXT;
 
-static BOOLEAN TestMap = FALSE;
 static ULONG TestTestId = -1;
 static PFILE_OBJECT TestFileObject;
 static PDEVICE_OBJECT TestDeviceObject;
@@ -168,9 +167,7 @@ MapInAnotherThread(IN PVOID Context)
     Ret = FALSE;
     Offset.QuadPart = 0x1000;
     KmtStartSeh();
-    TestMap = TRUE;
     Ret = CcMapData(TestFileObject, &Offset, TestContext->Length, MAP_WAIT, &Bcb, (PVOID *)&Buffer);
-    TestMap = FALSE;
     KmtEndSeh(STATUS_SUCCESS);
 
     if (!skip(Ret == TRUE, "CcMapData failed\n"))
@@ -181,6 +178,55 @@ MapInAnotherThread(IN PVOID Context)
         CcUnpinData(Bcb);
     }
 
+    KmtStartSeh();
+    Ret = CcPinRead(TestFileObject, &Offset, TestContext->Length, 0, &Bcb, (PVOID *)&Buffer);
+    KmtEndSeh(STATUS_SUCCESS);
+
+    if (!skip(Ret == TRUE, "CcPinRead failed\n"))
+    {
+        ok(Bcb != TestContext->Bcb, "Returned same BCB!\n");
+        ok_eq_pointer(Buffer, TestContext->Buffer);
+
+        CcUnpinData(Bcb);
+    }
+
+    KmtStartSeh();
+    Ret = CcPinRead(TestFileObject, &Offset, TestContext->Length, PIN_IF_BCB, &Bcb, (PVOID *)&Buffer);
+    KmtEndSeh(STATUS_SUCCESS);
+    ok(Ret == FALSE, "CcPinRead succeed\n");
+
+    if (Ret)
+    {
+        CcUnpinData(Bcb);
+    }
+
+    KmtStartSeh();
+    Ret = CcPinRead(TestFileObject, &Offset, TestContext->Length, PIN_EXCLUSIVE, &Bcb, (PVOID *)&Buffer);
+    KmtEndSeh(STATUS_SUCCESS);
+
+    if (!skip(Ret == TRUE, "CcPinRead failed\n"))
+    {
+        ok(Bcb != TestContext->Bcb, "Returned same BCB!\n");
+        ok_eq_pointer(Buffer, TestContext->Buffer);
+
+        CcUnpinData(Bcb);
+    }
+
+    Offset.QuadPart = 0x1500;
+    TestContext->Length -= 0x500;
+
+    KmtStartSeh();
+    Ret = CcMapData(TestFileObject, &Offset, TestContext->Length, MAP_WAIT, &Bcb, (PVOID *)&Buffer);
+    KmtEndSeh(STATUS_SUCCESS);
+
+    if (!skip(Ret == TRUE, "CcMapData failed\n"))
+    {
+        ok_eq_pointer(Bcb, TestContext->Bcb);
+        ok_eq_pointer(Buffer, (PVOID)((ULONG_PTR)TestContext->Buffer + 0x500));
+
+        CcUnpinData(Bcb);
+    }
+
     return;
 }
 
@@ -241,20 +287,31 @@ PerformTest(
                     PTEST_CONTEXT TestContext;
 
                     TestContext = ExAllocatePool(NonPagedPool, sizeof(TEST_CONTEXT));
-                    if (!skip(Fcb != NULL, "ExAllocatePool failed\n"))
+                    if (!skip(TestContext != NULL, "ExAllocatePool failed\n"))
                     {
                         Ret = FALSE;
                         Offset.QuadPart = 0x1000;
                         KmtStartSeh();
-                        TestMap = TRUE;
                         Ret = CcMapData(TestFileObject, &Offset, FileSizes.FileSize.QuadPart - Offset.QuadPart, MAP_WAIT, &TestContext->Bcb, &TestContext->Buffer);
-                        TestMap = FALSE;
                         KmtEndSeh(STATUS_SUCCESS);
 
                         if (!skip(Ret == TRUE, "CcMapData failed\n"))
                         {
                             PKTHREAD ThreadHandle;
 
+#ifdef _X86_
+                            /* FIXME: Should be fixed, will fail under certains conditions */
+                            ok(TestContext->Buffer > (PVOID)0xC1000000 && TestContext->Buffer < (PVOID)0xDCFFFFFF,
+                               "Buffer %p not mapped in system space\n", TestContext->Buffer);
+#else
+#ifdef _M_AMD64
+                            ok(TestContext->Buffer > (PVOID)0xFFFFF98000000000 && TestContext->Buffer < (PVOID)0xFFFFFA8000000000,
+                               "Buffer %p not mapped in system space\n", TestContext->Buffer);
+#else
+                            skip(FALSE, "System space mapping not defined\n");
+#endif
+#endif
+
                             TestContext->Length = FileSizes.FileSize.QuadPart - Offset.QuadPart;
                             ThreadHandle = KmtStartThread(MapInAnotherThread, TestContext);
                             KmtFinishThread(ThreadHandle, NULL);
@@ -269,6 +326,50 @@ PerformTest(
                         ExFreePool(TestContext);
                     }
                 }
+                else if (TestId == 4)
+                {
+                    /* Map after EOF */
+                    Ret = FALSE;
+                    Offset.QuadPart = FileSizes.FileSize.QuadPart + 0x1000;
+
+                    KmtStartSeh();
+                    Ret = CcMapData(TestFileObject, &Offset, 0x1000, 0, &Bcb, (PVOID *)&Buffer);
+                    KmtEndSeh(STATUS_SUCCESS);
+                    ok(Ret == FALSE, "CcMapData succeed\n");
+
+                    if (Ret)
+                    {
+                        CcUnpinData(Bcb);
+                    }
+
+                    /* Map a VACB after EOF */
+                    Ret = FALSE;
+                    Offset.QuadPart = FileSizes.FileSize.QuadPart + 0x1000 + VACB_MAPPING_GRANULARITY;
+
+                    KmtStartSeh();
+                    Ret = CcMapData(TestFileObject, &Offset, 0x1000, 0, &Bcb, (PVOID *)&Buffer);
+                    KmtEndSeh(STATUS_ACCESS_VIOLATION);
+                    ok(Ret == FALSE, "CcMapData succeed\n");
+
+                    if (Ret)
+                    {
+                        CcUnpinData(Bcb);
+                    }
+
+                    /* Map more than a VACB */
+                    Ret = FALSE;
+                    Offset.QuadPart = 0x0;
+
+                    KmtStartSeh();
+                    Ret = CcMapData(TestFileObject, &Offset, 0x1000 + VACB_MAPPING_GRANULARITY, 0, &Bcb, (PVOID *)&Buffer);
+                    KmtEndSeh(STATUS_SUCCESS);
+                    ok(Ret == FALSE, "CcMapData succeed\n");
+
+                    if (Ret)
+                    {
+                        CcUnpinData(Bcb);
+                    }
+                }
             }
         }
     }
@@ -323,6 +424,8 @@ TestMessageHandler(
 {
     NTSTATUS Status = STATUS_SUCCESS;
 
+    FsRtlEnterFileSystem();
+
     switch (ControlCode)
     {
         case IOCTL_START_TEST:
@@ -340,6 +443,8 @@ TestMessageHandler(
             break;
     }
 
+    FsRtlExitFileSystem();
+
     return Status;
 }
 
@@ -357,6 +462,8 @@ TestIrpHandler(
     DPRINT("IRP %x/%x\n", IoStack->MajorFunction, IoStack->MinorFunction);
     ASSERT(IoStack->MajorFunction == IRP_MJ_READ);
 
+    FsRtlEnterFileSystem();
+
     Status = STATUS_NOT_SUPPORTED;
     Irp->IoStatus.Information = 0;
 
@@ -412,5 +519,7 @@ TestIrpHandler(
         IoCompleteRequest(Irp, IO_NO_INCREMENT);
     }
 
+    FsRtlExitFileSystem();
+
     return Status;
 }