From 3b0611bcc9ef9d4422778b4ab7f7daf494f9807b Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Gardou?= Date: Sat, 4 May 2013 15:26:48 +0000 Subject: [PATCH] [NTDLL_APITEST] * Add tests for NtProtectVirtualMemory svn path=/trunk/; revision=58924 --- rostests/apitests/ntdll/CMakeLists.txt | 1 + .../apitests/ntdll/NtProtectVirtualMemory.c | 102 ++++++++++++++++++ rostests/apitests/ntdll/testlist.c | 2 + 3 files changed, 105 insertions(+) create mode 100644 rostests/apitests/ntdll/NtProtectVirtualMemory.c diff --git a/rostests/apitests/ntdll/CMakeLists.txt b/rostests/apitests/ntdll/CMakeLists.txt index dd78b59121a..7e51573c5c6 100644 --- a/rostests/apitests/ntdll/CMakeLists.txt +++ b/rostests/apitests/ntdll/CMakeLists.txt @@ -4,6 +4,7 @@ list(APPEND SOURCE NtAllocateVirtualMemory.c NtFreeVirtualMemory.c NtMapViewOfSection.c + NtProtectVirtualMemory.c NtQuerySystemEnvironmentValue.c RtlBitmap.c RtlDetermineDosPathNameType.c diff --git a/rostests/apitests/ntdll/NtProtectVirtualMemory.c b/rostests/apitests/ntdll/NtProtectVirtualMemory.c new file mode 100644 index 00000000000..b32c66c101d --- /dev/null +++ b/rostests/apitests/ntdll/NtProtectVirtualMemory.c @@ -0,0 +1,102 @@ +/* + * PROJECT: ReactOS API Tests + * LICENSE: GPLv2+ - See COPYING in the top level directory + * PURPOSE: Test for the NtProtectVirtualMemory API + */ + +#define WIN32_NO_STATUS +#include +#include +#include +#include + +#define StartSeh status = STATUS_SUCCESS; _SEH2_TRY +#define EndSeh(ExpectedStatus) _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) { status = _SEH2_GetExceptionCode(); } _SEH2_END; ok(status == ExpectedStatus, "Exception %lx, expected %lx\n", status, ExpectedStatus) + +START_TEST(NtProtectVirtualMemory) +{ + ULONG* allocationStart = NULL; + NTSTATUS status; + SIZE_T allocationSize; + ULONG oldProtection; + + /* Reserve a page */ + allocationSize = PAGE_SIZE; + status = NtAllocateVirtualMemory(NtCurrentProcess(), + (void**)&allocationStart, + 0, + &allocationSize, + MEM_RESERVE, + PAGE_NOACCESS); + ok(NT_SUCCESS(status), "Reserving memory failed\n"); + + /* Commit the page (RW) */ + status = NtAllocateVirtualMemory(NtCurrentProcess(), + (void**)&allocationStart, + 0, + &allocationSize, + MEM_COMMIT, + PAGE_READWRITE); + ok(NT_SUCCESS(status), "Commiting memory failed\n"); + + /* Try writing it */ + StartSeh + { + *allocationStart = 0xFF; + } EndSeh(STATUS_SUCCESS); + + /* Try reading it */ + StartSeh + { + ok(*allocationStart == 0xFF, "Memory was not written\n"); + } EndSeh(STATUS_SUCCESS); + + /* Set it as read only */ + status = NtProtectVirtualMemory(NtCurrentProcess(), + (void**)&allocationStart, + &allocationSize, + PAGE_READONLY, + &oldProtection); + ok(NT_SUCCESS(status), "NtProtectVirtualMemory failed.\n"); + ok(oldProtection == PAGE_READWRITE, "Expected PAGE_READWRITE, got %08x.\n", oldProtection); + + /* Try writing it */ + StartSeh + { + *allocationStart = 0xAA; + } EndSeh(STATUS_ACCESS_VIOLATION); + + /* Try reading it */ + StartSeh + { + ok(*allocationStart == 0xFF, "read-only memory were changed.\n"); + } EndSeh(STATUS_SUCCESS); + + /* Set it as no access */ + status = NtProtectVirtualMemory(NtCurrentProcess(), + (void**)&allocationStart, + &allocationSize, + PAGE_NOACCESS, + &oldProtection); + ok(NT_SUCCESS(status), "NtProtectVirtualMemory failed.\n"); + ok(oldProtection == PAGE_READONLY, "Expected PAGE_READONLY, got %08x.\n", oldProtection); + + /* Try writing it */ + StartSeh + { + *allocationStart = 0xAA; + } EndSeh(STATUS_ACCESS_VIOLATION); + + /* Try reading it */ + StartSeh + { + ok(*allocationStart == 0, "Test should not go as far as this.\n"); + } EndSeh(STATUS_ACCESS_VIOLATION); + + /* Free memory */ + status = NtFreeVirtualMemory(NtCurrentProcess(), + (void**)&allocationStart, + &allocationSize, + MEM_RELEASE); + ok(NT_SUCCESS(status), "Failed freeing memory.\n"); +} \ No newline at end of file diff --git a/rostests/apitests/ntdll/testlist.c b/rostests/apitests/ntdll/testlist.c index b8a6c9e8e45..56e15ab1b23 100644 --- a/rostests/apitests/ntdll/testlist.c +++ b/rostests/apitests/ntdll/testlist.c @@ -7,6 +7,7 @@ extern void func_LdrEnumResources(void); extern void func_NtAllocateVirtualMemory(void); extern void func_NtFreeVirtualMemory(void); extern void func_NtMapViewOfSection(void); +extern void func_NtProtectVirtualMemory(void); extern void func_NtQuerySystemEnvironmentValue(void); extern void func_NtSystemInformation(void); extern void func_RtlBitmap(void); @@ -28,6 +29,7 @@ const struct test winetest_testlist[] = { "NtAllocateVirtualMemory", func_NtAllocateVirtualMemory }, { "NtFreeVirtualMemory", func_NtFreeVirtualMemory }, { "NtMapViewOfSection", func_NtMapViewOfSection }, + { "NtProtectVirtualMemory", func_NtProtectVirtualMemory }, { "NtQuerySystemEnvironmentValue", func_NtQuerySystemEnvironmentValue }, { "NtSystemInformation", func_NtSystemInformation }, { "RtlBitmapApi", func_RtlBitmap }, -- 2.17.1