a095d1ad2aed016e15de67a4d5cdf7cc68eafd3d
[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 LONG State;
60
61 RtlFillMemory(&Mutant, sizeof(Mutant), 0x55);
62 KeInitializeMutant(&Mutant, FALSE);
63 ok_irql(PASSIVE_LEVEL);
64 CheckMutex(&Mutant, FALSE, TRUE, 0);
65
66 RtlFillMemory(&Mutant, sizeof(Mutant), 0x55);
67 KeInitializeMutant(&Mutant, TRUE);
68 ok_irql(PASSIVE_LEVEL);
69 CheckMutex(&Mutant, TRUE, TRUE, 0);
70 State = KeReleaseMutant(&Mutant, 1, FALSE, FALSE);
71 ok_eq_long(State, 0);
72 ok_irql(PASSIVE_LEVEL);
73 CheckMutex(&Mutant, FALSE, FALSE, 0);
74 }
75
76 static
77 VOID
78 TestMutex(VOID)
79 {
80 KMUTEX Mutex;
81
82 RtlFillMemory(&Mutex, sizeof(Mutex), 0x55);
83 KeInitializeMutex(&Mutex, 0);
84 ok_irql(PASSIVE_LEVEL);
85 CheckMutex(&Mutex, FALSE, TRUE, 1);
86
87 RtlFillMemory(&Mutex, sizeof(Mutex), 0x55);
88 KeInitializeMutex(&Mutex, 123);
89 ok_irql(PASSIVE_LEVEL);
90 CheckMutex(&Mutex, FALSE, TRUE, 1);
91 }
92
93 START_TEST(KeMutex)
94 {
95 TestMutant();
96 TestMutex();
97 }