2004-10-28 Casper S. Hornstrup <chorns@users.sourceforge.net>
[reactos.git] / reactos / ntoskrnl / tests / tests / VirtualMemory.c
1 #include <ntoskrnl.h>
2 #include "regtests.h"
3
4 #define TestProcessHandle (HANDLE) 1
5 #define TestProcessObject (PVOID) 0x2
6 #define TestBaseAddress (PVOID) 0x1000
7 #define TestNumberOfBytesToLock 0x2000
8 #define TestMdl (PMDL) 0xD0000000
9
10 static BOOLEAN MockExFreePoolCalled = FALSE;
11
12 static VOID STDCALL
13 MockExFreePool(PVOID Block)
14 {
15 _AssertFalse(MockExFreePoolCalled);
16 _AssertEqualValue(TestMdl, Block);
17 MockExFreePoolCalled = TRUE;
18 }
19
20 static BOOLEAN MockMmCreateMdlCalled = FALSE;
21
22 static PMDL STDCALL
23 MockMmCreateMdl(PMDL Mdl,
24 PVOID Base,
25 ULONG Length)
26 {
27 _AssertFalse(MockMmCreateMdlCalled);
28 _AssertEqualValue(TestBaseAddress, Base);
29 _AssertEqualValue(TestNumberOfBytesToLock, Length);
30 MockMmCreateMdlCalled = TRUE;
31 return TestMdl;
32 }
33
34 static BOOLEAN MockMmProbeAndLockPagesCalled = FALSE;
35
36 static VOID STDCALL
37 MockMmProbeAndLockPages(PMDL Mdl,
38 KPROCESSOR_MODE AccessMode,
39 LOCK_OPERATION Operation)
40 {
41 _AssertFalse(MockMmProbeAndLockPagesCalled);
42 _AssertEqualValue(TestMdl, Mdl);
43 _AssertEqualValue(UserMode, AccessMode);
44 _AssertEqualValue(IoWriteAccess, Operation);
45 MockMmProbeAndLockPagesCalled = TRUE;
46 }
47
48 static BOOLEAN MockObDereferenceObjectCalled = FALSE;
49
50 static VOID FASTCALL
51 MockObDereferenceObject(PVOID Object)
52 {
53 _AssertFalse(MockObDereferenceObjectCalled);
54 _AssertEqualValue(TestProcessObject, Object);
55 MockObDereferenceObjectCalled = TRUE;
56 }
57
58 static BOOLEAN MockObReferenceObjectByHandleCalled = FALSE;
59
60 static NTSTATUS STDCALL
61 MockObReferenceObjectByHandle(HANDLE Handle,
62 ACCESS_MASK DesiredAccess,
63 POBJECT_TYPE ObjectType,
64 KPROCESSOR_MODE AccessMode,
65 PVOID* Object,
66 POBJECT_HANDLE_INFORMATION HandleInformation)
67 {
68 _AssertFalse(MockObReferenceObjectByHandleCalled);
69 _AssertEqualValue(TestProcessHandle, Handle);
70 _AssertEqualValue(PROCESS_VM_WRITE, DesiredAccess);
71 _AssertEqualValue(NULL, ObjectType);
72 _AssertEqualValue(UserMode, AccessMode);
73 _AssertNotEqualValue(NULL, Object);
74 _AssertEqualValue(NULL, HandleInformation);
75 *Object = TestProcessObject;
76 MockObReferenceObjectByHandleCalled = TRUE;
77 return STATUS_SUCCESS;
78 }
79
80 static void RunTest()
81 {
82 ULONG NumberOfBytesLocked;
83 NTSTATUS status;
84
85 status = NtLockVirtualMemoryInternal(TestProcessHandle,
86 TestBaseAddress,
87 TestNumberOfBytesToLock,
88 &NumberOfBytesLocked,
89 MockObReferenceObjectByHandle,
90 MockMmCreateMdl,
91 MockObDereferenceObject,
92 MockMmProbeAndLockPages,
93 MockExFreePool);
94 _AssertEqualValue(STATUS_SUCCESS, status);
95 _AssertTrue(MockObReferenceObjectByHandleCalled);
96 _AssertTrue(MockMmCreateMdlCalled);
97 _AssertTrue(MockMmProbeAndLockPagesCalled);
98 _AssertTrue(MockExFreePoolCalled);
99 _AssertTrue(MockObDereferenceObjectCalled);
100 _AssertEqualValue(TestNumberOfBytesToLock, NumberOfBytesLocked);
101 }
102
103 _Dispatcher(VirtualmemoryTest, "Virtual memory")