[KMTESTS:KE]
authorThomas Faber <thomas.faber@reactos.org>
Fri, 28 Sep 2012 07:09:08 +0000 (07:09 +0000)
committerThomas Faber <thomas.faber@reactos.org>
Fri, 28 Sep 2012 07:09:08 +0000 (07:09 +0000)
- Add test for KeInitializeMutant vs KeInitializeMutex. Dedicated to Mike.

svn path=/trunk/; revision=57408

rostests/kmtests/CMakeLists.txt
rostests/kmtests/kmtest_drv/testlist.c
rostests/kmtests/ntos_ke/KeMutex.c [new file with mode: 0644]

index 6126479..96f1396 100644 (file)
@@ -45,6 +45,7 @@ list(APPEND KMTEST_DRV_SOURCE
     ntos_ke/KeEvent.c
     ntos_ke/KeGuardedMutex.c
     ntos_ke/KeIrql.c
+    ntos_ke/KeMutex.c
     ntos_ke/KeProcessor.c
     ntos_ke/KeSpinLock.c
     ntos_ke/KeTimer.c
index 406f8ab..1175b29 100644 (file)
@@ -30,6 +30,7 @@ KMT_TESTFUNC Test_KeDpc;
 KMT_TESTFUNC Test_KeEvent;
 KMT_TESTFUNC Test_KeGuardedMutex;
 KMT_TESTFUNC Test_KeIrql;
+KMT_TESTFUNC Test_KeMutex;
 KMT_TESTFUNC Test_KeProcessor;
 KMT_TESTFUNC Test_KeTimer;
 KMT_TESTFUNC Test_KernelType;
@@ -70,6 +71,7 @@ const KMT_TEST TestList[] =
     { "KeEvent",                            Test_KeEvent },
     { "KeGuardedMutex",                     Test_KeGuardedMutex },
     { "KeIrql",                             Test_KeIrql },
+    { "KeMutex",                            Test_KeMutex },
     { "-KeProcessor",                       Test_KeProcessor },
     { "KeTimer",                            Test_KeTimer },
     { "-KernelType",                        Test_KernelType },
diff --git a/rostests/kmtests/ntos_ke/KeMutex.c b/rostests/kmtests/ntos_ke/KeMutex.c
new file mode 100644 (file)
index 0000000..4354370
--- /dev/null
@@ -0,0 +1,98 @@
+/*
+ * PROJECT:         ReactOS kernel-mode tests
+ * LICENSE:         GPLv2+ - See COPYING in the top level directory
+ * PURPOSE:         Kernel-Mode Test Suite Mutant/Mutex test
+ * PROGRAMMER:      Thomas Faber <thfabba@gmx.de>
+ */
+
+#include <kmt_test.h>
+
+#define ULONGS_PER_POINTER (sizeof(PVOID) / sizeof(ULONG))
+#define MUTANT_SIZE (2 + 6 * ULONGS_PER_POINTER)
+
+C_ASSERT(sizeof(DISPATCHER_HEADER) == 8 + 2 * sizeof(PVOID));
+C_ASSERT(sizeof(KMUTANT) == sizeof(DISPATCHER_HEADER) + 3 * sizeof(PVOID) + sizeof(PVOID));
+C_ASSERT(sizeof(KMUTANT) == MUTANT_SIZE * sizeof(ULONG));
+
+#define CheckMutex(Mutex, Held, New, ExpectedApcDisable) do {                   \
+    PKTHREAD Thread = KeGetCurrentThread();                                     \
+    ok_eq_uint((Mutex)->Header.Type, MutantObject);                             \
+    ok_eq_uint((Mutex)->Header.Abandoned, 0x55);                                \
+    ok_eq_uint((Mutex)->Header.Size, MUTANT_SIZE);                              \
+    ok_eq_uint((Mutex)->Header.DpcActive, 0x55);                                \
+    ok_eq_pointer((Mutex)->Header.WaitListHead.Flink,                           \
+                  &(Mutex)->Header.WaitListHead);                               \
+    ok_eq_pointer((Mutex)->Header.WaitListHead.Blink,                           \
+                  &(Mutex)->Header.WaitListHead);                               \
+    if (Held)                                                                   \
+    {                                                                           \
+        ok_eq_long((Mutex)->Header.SignalState, 0);                             \
+        ok_eq_pointer((Mutex)->MutantListEntry.Flink, &Thread->MutantListHead); \
+        ok_eq_pointer((Mutex)->MutantListEntry.Blink, &Thread->MutantListHead); \
+        ok_eq_pointer(Thread->MutantListHead.Flink, &(Mutex)->MutantListEntry); \
+        ok_eq_pointer(Thread->MutantListHead.Blink, &(Mutex)->MutantListEntry); \
+        ok_eq_pointer((Mutex)->OwnerThread, Thread);                            \
+    }                                                                           \
+    else                                                                        \
+    {                                                                           \
+        ok_eq_long((Mutex)->Header.SignalState, 1);                             \
+        if (New)                                                                \
+        {                                                                       \
+            ok_eq_pointer((Mutex)->MutantListEntry.Flink,                       \
+                          (PVOID)0x5555555555555555ULL);                        \
+            ok_eq_pointer((Mutex)->MutantListEntry.Blink,                       \
+                          (PVOID)0x5555555555555555ULL);                        \
+        }                                                                       \
+        ok_eq_pointer(Thread->MutantListHead.Flink, &Thread->MutantListHead);   \
+        ok_eq_pointer(Thread->MutantListHead.Blink, &Thread->MutantListHead);   \
+        ok_eq_pointer((Mutex)->OwnerThread, NULL);                              \
+    }                                                                           \
+    ok_eq_uint((Mutex)->Abandoned, 0);                                          \
+    ok_eq_uint((Mutex)->ApcDisable, ExpectedApcDisable);                        \
+} while (0)
+
+static
+VOID
+TestMutant(VOID)
+{
+    KMUTANT Mutant;
+    PKTHREAD Thread = KeGetCurrentThread();
+    LONG State;
+
+    RtlFillMemory(&Mutant, sizeof(Mutant), 0x55);
+    KeInitializeMutant(&Mutant, FALSE);
+    ok_irql(PASSIVE_LEVEL);
+    CheckMutex(&Mutant, FALSE, TRUE, 0);
+
+    RtlFillMemory(&Mutant, sizeof(Mutant), 0x55);
+    KeInitializeMutant(&Mutant, TRUE);
+    ok_irql(PASSIVE_LEVEL);
+    CheckMutex(&Mutant, TRUE, TRUE, 0);
+    State = KeReleaseMutant(&Mutant, 1, FALSE, FALSE);
+    ok_eq_long(State, 0);
+    ok_irql(PASSIVE_LEVEL);
+    CheckMutex(&Mutant, FALSE, FALSE, 0);
+}
+
+static
+VOID
+TestMutex(VOID)
+{
+    KMUTEX Mutex;
+
+    RtlFillMemory(&Mutex, sizeof(Mutex), 0x55);
+    KeInitializeMutex(&Mutex, 0);
+    ok_irql(PASSIVE_LEVEL);
+    CheckMutex(&Mutex, FALSE, TRUE, 1);
+
+    RtlFillMemory(&Mutex, sizeof(Mutex), 0x55);
+    KeInitializeMutex(&Mutex, 123);
+    ok_irql(PASSIVE_LEVEL);
+    CheckMutex(&Mutex, FALSE, TRUE, 1);
+}
+
+START_TEST(KeMutex)
+{
+    TestMutant();
+    TestMutex();
+}