[NTDLL_APITEST]
[reactos.git] / rostests / apitests / ntdll / NtMapViewOfSection.c
index e9fc4ee..f191327 100644 (file)
@@ -1,24 +1,15 @@
+/*
+ * PROJECT:         ReactOS API Tests
+ * LICENSE:         LGPLv2.1+ - See COPYING.LIB in the top level directory
+ * PURPOSE:         Test for NtMapViewOfSection
+ * PROGRAMMERS:     Timo Kreuzer
+ */
 
 #include <apitest.h>
-
+#include <strsafe.h>
 #define WIN32_NO_STATUS
 #include <ndk/ntndk.h>
 
-NTSYSAPI
-NTSTATUS
-NTAPI
-NtMapViewOfSection(
-    HANDLE SectionHandle,
-       HANDLE ProcessHandle,
-       PVOID *BaseAddress,
-       ULONG_PTR ZeroBits,
-       SIZE_T CommitSize,
-       PLARGE_INTEGER SectionOffset,
-       PSIZE_T ViewSize,
-       SECTION_INHERIT InheritDisposition,
-       ULONG AllocationType,
-       ULONG Protect);
-
 void
 Test_PageFileSection(void)
 {
@@ -236,7 +227,7 @@ Test_PageFileSection(void)
     if (!NT_SUCCESS(Status))
         return;
 
-    ok(BaseAddress == UlongToPtr(0x40000000), "Invalid BaseAddress: %p", BaseAddress);
+    ok(BaseAddress == UlongToPtr(0x40000000), "Invalid BaseAddress: %p\n", BaseAddress);
 
     BaseAddress = (PVOID)0x40080000;
     SectionOffset.QuadPart = 0x10000;
@@ -253,7 +244,7 @@ Test_PageFileSection(void)
                                 PAGE_READWRITE);
     ok_ntstatus(Status, STATUS_SUCCESS);
 
-    ok(BaseAddress == (PVOID)0x40080000, "Invalid BaseAddress: %p", BaseAddress);
+    ok(BaseAddress == (PVOID)0x40080000, "Invalid BaseAddress: %p\n", BaseAddress);
 
     /* Commit a page in the section */
     BaseAddress = (PVOID)0x40000000;
@@ -266,8 +257,13 @@ Test_PageFileSection(void)
     ok_ntstatus(Status, STATUS_SUCCESS);
 
     /* Close the mapping */
-    NtUnmapViewOfSection(NtCurrentProcess(), BaseAddress);
-    NtClose(SectionHandle);
+    Status = NtUnmapViewOfSection(NtCurrentProcess(), BaseAddress);
+    ok_ntstatus(Status, STATUS_SUCCESS);
+    BaseAddress = (PVOID)0x30000000;
+    Status = NtUnmapViewOfSection(NtCurrentProcess(), BaseAddress);
+    ok_ntstatus(Status, STATUS_SUCCESS);
+    Status = NtClose(SectionHandle);
+    ok_ntstatus(Status, STATUS_SUCCESS);
 
     /* Create a page file backed section, but only reserved */
     MaximumSize.QuadPart = 0x20000;
@@ -401,12 +397,15 @@ Test_PageFileSection(void)
     _SEH2_END;
     ok_ntstatus(Status, STATUS_SUCCESS);
 
-    ok(*(PULONG)BaseAddress2 == 2, "Value in memory was wrong");
+    ok(*(PULONG)BaseAddress2 == 2, "Value in memory was wrong\n");
 
     /* Close the mapping */
-    NtUnmapViewOfSection(NtCurrentProcess(), BaseAddress);
-    NtUnmapViewOfSection(NtCurrentProcess(), (PUCHAR)BaseAddress2 - PAGE_SIZE);
-    NtClose(SectionHandle);
+    Status = NtUnmapViewOfSection(NtCurrentProcess(), BaseAddress);
+    ok_ntstatus(Status, STATUS_SUCCESS);
+    Status = NtUnmapViewOfSection(NtCurrentProcess(), (PUCHAR)BaseAddress2 - PAGE_SIZE);
+    ok_ntstatus(Status, STATUS_SUCCESS);
+    Status = NtClose(SectionHandle);
+    ok_ntstatus(Status, STATUS_SUCCESS);
 
     /* Try to create a 512 GB page file backed section with committed pages */
     MaximumSize.QuadPart = 0x8000000000;
@@ -444,7 +443,7 @@ Test_PageFileSection(void)
 #else
     /* WoW64 returns STATUS_INSUFFICIENT_RESOURCES */
     ok((Status == STATUS_INSUFFICIENT_RESOURCES) || (Status == STATUS_SECTION_TOO_BIG),
-       "got wrong Status: 0x%lx", Status);
+       "got wrong Status: 0x%lx\n", Status);
 #endif
 
     /* Try to create a even huger page file backed section, but only reserved */
@@ -488,7 +487,7 @@ Test_PageFileSection(void)
 #else
     /* WoW64 returns STATUS_INVALID_PARAMETER_4 */
     ok((Status == STATUS_INVALID_PARAMETER_4) || (Status == STATUS_INVALID_PARAMETER_3),
-       "got wrong Status: 0x%lx", Status);
+       "got wrong Status: 0x%lx\n", Status);
 #endif
 
     /* Pass 0 region size */
@@ -511,7 +510,7 @@ Test_PageFileSection(void)
 #else
     /* WoW64 returns STATUS_NO_MEMORY */
     ok((Status == STATUS_NO_MEMORY) || (Status == STATUS_INVALID_VIEW_SIZE),
-       "got wrong Status: 0x%lx", Status);
+       "got wrong Status: 0x%lx\n", Status);
     ok(ViewSize == 0, "wrong ViewSize: 0x%Ix\n", ViewSize);
 #endif
 
@@ -642,6 +641,10 @@ Test_PageFileSection(void)
     Status = NtAllocateVirtualMemory(NtCurrentProcess(), &BaseAddress2, 0, &ViewSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
     ok_ntstatus(Status, STATUS_SUCCESS);
 
+    Status = NtUnmapViewOfSection(NtCurrentProcess(), BaseAddress);
+    ok_ntstatus(Status, STATUS_SUCCESS);
+    Status = NtClose(SectionHandle);
+    ok_ntstatus(Status, STATUS_SUCCESS);
 }
 
 void
@@ -651,11 +654,15 @@ Test_ImageSection(void)
     NTSTATUS Status;
     OBJECT_ATTRIBUTES FileObjectAttributes;
     IO_STATUS_BLOCK IoStatusBlock;
+    WCHAR TestDllPath[MAX_PATH];
     HANDLE FileHandle, DataSectionHandle, ImageSectionHandle;
     PVOID DataBase, ImageBase;
     SIZE_T ViewSize;
 
-    if (!RtlDosPathNameToNtPathName_U(L"testdata\\test.dll",
+    GetModuleFileNameW(NULL, TestDllPath, RTL_NUMBER_OF(TestDllPath));
+    wcsrchr(TestDllPath, L'\\')[1] = UNICODE_NULL;
+    StringCbCatW(TestDllPath, sizeof(TestDllPath), L"testdata\\test.dll");
+    if (!RtlDosPathNameToNtPathName_U(TestDllPath,
                                       &FileName,
                                       NULL,
                                       NULL))
@@ -677,6 +684,11 @@ Test_ImageSection(void)
                         FILE_SHARE_READ,
                         FILE_SYNCHRONOUS_IO_NONALERT);
     ok_ntstatus(Status, STATUS_SUCCESS);
+    if (!NT_SUCCESS(Status))
+    {
+        skip("Failed to open file\n");
+        return;
+    }
 
     /* Create a data section with write access */
     Status = NtCreateSection(&DataSectionHandle,
@@ -687,6 +699,12 @@ Test_ImageSection(void)
                              SEC_COMMIT, // AllocationAttributes
                              FileHandle);
     ok_ntstatus(Status, STATUS_SUCCESS);
+    if (!NT_SUCCESS(Status))
+    {
+        skip("Failed to create data section\n");
+        NtClose(FileHandle);
+        return;
+    }
 
     /* Map the data section as flat mapping */
     DataBase = NULL;
@@ -703,6 +721,13 @@ Test_ImageSection(void)
                                 PAGE_READWRITE);
     ok_ntstatus(Status, STATUS_SUCCESS);
     //ok(ViewSize == 0x3f95cc48, "ViewSize wrong: 0x%lx\n");
+    if (!NT_SUCCESS(Status))
+    {
+        skip("Failed to map view of data section\n");
+        NtClose(DataSectionHandle);
+        NtClose(FileHandle);
+        return;
+    }
 
     /* Check the original data */
     ok(*(ULONG*)DataBase == 0x00905a4d, "Header not ok\n");
@@ -725,6 +750,7 @@ Test_ImageSection(void)
                              SEC_IMAGE, // AllocationAttributes
                              FileHandle);
     ok_ntstatus(Status, STATUS_INVALID_IMAGE_NOT_MZ);
+    if (NT_SUCCESS(Status)) NtClose(ImageSectionHandle);
 
     /* Restore the original header */
     *(ULONG*)DataBase = 0x00905a4d;
@@ -742,6 +768,13 @@ Test_ImageSection(void)
                              SEC_IMAGE, // AllocationAttributes
                              FileHandle);
     ok_ntstatus(Status, STATUS_SUCCESS);
+    if (!NT_SUCCESS(Status))
+    {
+        skip("Failed to create image section\n");
+        NtClose(DataSectionHandle);
+        NtClose(FileHandle);
+        return;
+    }
 
     /* Map the image section */
     ImageBase = NULL;
@@ -757,10 +790,18 @@ Test_ImageSection(void)
                                 0, // AllocationType
                                 PAGE_READONLY);
 #ifdef _M_IX86
-    ok_ntstatus(Status, STATUS_IMAGE_NOT_AT_BASE);
+    ok_ntstatus(Status, STATUS_SUCCESS);
 #else
     ok_ntstatus(Status, STATUS_IMAGE_MACHINE_TYPE_MISMATCH);
 #endif
+    if (!NT_SUCCESS(Status))
+    {
+        skip("Failed to map view of image section\n");
+        NtClose(ImageSectionHandle);
+        NtClose(DataSectionHandle);
+        NtClose(FileHandle);
+        return;
+    }
 
     /* Check the header */
     ok(*(ULONG*)DataBase == 0x00905a4d, "Header not ok\n");
@@ -813,6 +854,13 @@ Test_ImageSection(void)
                              SEC_IMAGE, // AllocationAttributes
                              FileHandle);
     ok_ntstatus(Status, STATUS_SUCCESS);
+    if (!NT_SUCCESS(Status))
+    {
+        skip("Failed to create image section\n");
+        NtClose(DataSectionHandle);
+        NtClose(FileHandle);
+        return;
+    }
 
     /* Map the image section again */
     ImageBase = NULL;
@@ -828,10 +876,18 @@ Test_ImageSection(void)
                                 0,
                                 PAGE_READONLY);
 #ifdef _M_IX86
-    ok_ntstatus(Status, STATUS_IMAGE_NOT_AT_BASE);
+    ok_ntstatus(Status, STATUS_SUCCESS);
 #else
     ok_ntstatus(Status, STATUS_IMAGE_MACHINE_TYPE_MISMATCH);
 #endif
+    if (!NT_SUCCESS(Status))
+    {
+        skip("Failed to map view of image section\n");
+        NtClose(ImageSectionHandle);
+        NtClose(DataSectionHandle);
+        NtClose(FileHandle);
+        return;
+    }
 
     // This one doesn't always work, needs investigation
     /* Check the .data section again */
@@ -1012,13 +1068,11 @@ Test_BasedSection(void)
     ok_ntstatus(Status, STATUS_CONFLICTING_ADDRESSES);
 #else
     ok_ntstatus(Status, STATUS_SUCCESS);
-#endif
-
-    ok_ntstatus(Status, STATUS_SUCCESS);
     ok((ULONG_PTR)BaseAddress2 < (ULONG_PTR)BaseAddress1,
        "Invalid addresses: BaseAddress1=%p, BaseAddress2=%p\n", BaseAddress1, BaseAddress2);
     ok(((ULONG_PTR)BaseAddress1 - (ULONG_PTR)BaseAddress2) == 0x10000,
        "Invalid addresses: BaseAddress1=%p, BaseAddress2=%p\n", BaseAddress1, BaseAddress2);
+#endif
 
 }
 
@@ -1028,4 +1082,3 @@ START_TEST(NtMapViewOfSection)
     Test_ImageSection();
     Test_BasedSection();
 }
-