435437018385a5c824637634e34c0af6a2b9cefb
[reactos.git] / rostests / kmtests / ntos_ke / KeMutex.c
1 /*
2 * PROJECT: ReactOS kernel-mode tests
3 * LICENSE: GPLv2+ - See COPYING in the top level directory
4 * PURPOSE: Kernel-Mode Test Suite Mutant/Mutex test
5 * PROGRAMMER: Thomas Faber <thfabba@gmx.de>
6 */
7
8 #include <kmt_test.h>
9
10 #define ULONGS_PER_POINTER (sizeof(PVOID) / sizeof(ULONG))
11 #define MUTANT_SIZE (2 + 6 * ULONGS_PER_POINTER)
12
13 C_ASSERT(sizeof(DISPATCHER_HEADER) == 8 + 2 * sizeof(PVOID));
14 C_ASSERT(sizeof(KMUTANT) == sizeof(DISPATCHER_HEADER) + 3 * sizeof(PVOID) + sizeof(PVOID));
15 C_ASSERT(sizeof(KMUTANT) == MUTANT_SIZE * sizeof(ULONG));
16
17 #define CheckMutex(Mutex, Held, New, ExpectedApcDisable) do { \
18 PKTHREAD Thread = KeGetCurrentThread(); \
19 ok_eq_uint((Mutex)->Header.Type, MutantObject); \
20 ok_eq_uint((Mutex)->Header.Abandoned, 0x55); \
21 ok_eq_uint((Mutex)->Header.Size, MUTANT_SIZE); \
22 ok_eq_uint((Mutex)->Header.DpcActive, 0x55); \
23 ok_eq_pointer((Mutex)->Header.WaitListHead.Flink, \
24 &(Mutex)->Header.WaitListHead); \
25 ok_eq_pointer((Mutex)->Header.WaitListHead.Blink, \
26 &(Mutex)->Header.WaitListHead); \
27 if (Held) \
28 { \
29 ok_eq_long((Mutex)->Header.SignalState, 0); \
30 ok_eq_pointer((Mutex)->MutantListEntry.Flink, &Thread->MutantListHead); \
31 ok_eq_pointer((Mutex)->MutantListEntry.Blink, &Thread->MutantListHead); \
32 ok_eq_pointer(Thread->MutantListHead.Flink, &(Mutex)->MutantListEntry); \
33 ok_eq_pointer(Thread->MutantListHead.Blink, &(Mutex)->MutantListEntry); \
34 ok_eq_pointer((Mutex)->OwnerThread, Thread); \
35 } \
36 else \
37 { \
38 ok_eq_long((Mutex)->Header.SignalState, 1); \
39 if (New) \
40 { \
41 ok_eq_pointer((Mutex)->MutantListEntry.Flink, \
42 (PVOID)0x5555555555555555ULL); \
43 ok_eq_pointer((Mutex)->MutantListEntry.Blink, \
44 (PVOID)0x5555555555555555ULL); \
45 } \
46 ok_eq_pointer(Thread->MutantListHead.Flink, &Thread->MutantListHead); \
47 ok_eq_pointer(Thread->MutantListHead.Blink, &Thread->MutantListHead); \
48 ok_eq_pointer((Mutex)->OwnerThread, NULL); \
49 } \
50 ok_eq_uint((Mutex)->Abandoned, 0); \
51 ok_eq_uint((Mutex)->ApcDisable, ExpectedApcDisable); \
52 } while (0)
53
54 static
55 VOID
56 TestMutant(VOID)
57 {
58 KMUTANT Mutant;
59 PKTHREAD Thread = KeGetCurrentThread();
60 LONG State;
61
62 RtlFillMemory(&Mutant, sizeof(Mutant), 0x55);
63 KeInitializeMutant(&Mutant, FALSE);
64 ok_irql(PASSIVE_LEVEL);
65 CheckMutex(&Mutant, FALSE, TRUE, 0);
66
67 RtlFillMemory(&Mutant, sizeof(Mutant), 0x55);
68 KeInitializeMutant(&Mutant, TRUE);
69 ok_irql(PASSIVE_LEVEL);
70 CheckMutex(&Mutant, TRUE, TRUE, 0);
71 State = KeReleaseMutant(&Mutant, 1, FALSE, FALSE);
72 ok_eq_long(State, 0);
73 ok_irql(PASSIVE_LEVEL);
74 CheckMutex(&Mutant, FALSE, FALSE, 0);
75 }
76
77 static
78 VOID
79 TestMutex(VOID)
80 {
81 KMUTEX Mutex;
82
83 RtlFillMemory(&Mutex, sizeof(Mutex), 0x55);
84 KeInitializeMutex(&Mutex, 0);
85 ok_irql(PASSIVE_LEVEL);
86 CheckMutex(&Mutex, FALSE, TRUE, 1);
87
88 RtlFillMemory(&Mutex, sizeof(Mutex), 0x55);
89 KeInitializeMutex(&Mutex, 123);
90 ok_irql(PASSIVE_LEVEL);
91 CheckMutex(&Mutex, FALSE, TRUE, 1);
92 }
93
94 START_TEST(KeMutex)
95 {
96 TestMutant();
97 TestMutex();
98 }