[KMTEST]
[reactos.git] / rostests / drivers / kmtest / ntos_ex.c
1 /*
2 * NTOSKRNL Executive Regressions KM-Test
3 * ReactOS Kernel Mode Regression Testing framework
4 *
5 * Copyright 2006 Aleksey Bragin <aleksey@reactos.org>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; see the file COPYING.LIB.
19 * If not, write to the Free Software Foundation,
20 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 */
22
23 /* INCLUDES *******************************************************************/
24
25 #include <ddk/ntddk.h>
26 #include <ntifs.h>
27 #include <ndk/ntndk.h>
28 #include "kmtest.h"
29
30 #define NDEBUG
31 #include "debug.h"
32
33 /* PRIVATE FUNCTIONS ***********************************************************/
34
35 VOID
36 NTAPI
37 TestTimerApcRoutine(IN PVOID TimerContext,
38 IN ULONG TimerLowValue,
39 IN LONG TimerHighValue)
40
41 {
42 ULONG *ApcCount = (ULONG *)TimerContext;
43 DPRINT("Timer Apc called!\n");
44 (*ApcCount)++;
45 }
46
47 /* PUBLIC FUNCTIONS *************************************************************/
48
49 VOID
50 ExTimerTest(HANDLE KeyHandle)
51 {
52 UNICODE_STRING TimerName;
53 OBJECT_ATTRIBUTES ObjectAttributes;
54 HANDLE TimerHandle;
55 HANDLE HandleOpened;
56 LARGE_INTEGER DueTime;
57 BOOLEAN PreviousState, CurrentState;
58 NTSTATUS Status;
59 ULONG ApcCount;
60
61 StartTest();
62
63 // Create the timer
64 RtlInitUnicodeString(&TimerName, L"\\TestTimer");
65 InitializeObjectAttributes(&ObjectAttributes, &TimerName, 0, NULL, NULL);
66 Status = ZwCreateTimer(&TimerHandle, TIMER_ALL_ACCESS,
67 &ObjectAttributes, NotificationTimer);
68 ok(Status == STATUS_SUCCESS, "ZwCreateTimer failed with Status=0x%08lX", Status);
69
70 // Open the timer
71 Status = ZwOpenTimer(&HandleOpened, TIMER_ALL_ACCESS, &ObjectAttributes);
72 ok(Status == STATUS_SUCCESS, "ZwOpenTimer failed with Status=0x%08lX", Status);
73
74 // Set the timer, to some rather high value so it doesn't expire
75 DPRINT("Set timer 1\n");
76 DueTime.LowPart = -10000;
77 DueTime.HighPart = -10;
78 PreviousState = TRUE;
79 Status = ZwSetTimer(HandleOpened, &DueTime, NULL, NULL, FALSE, 0L, &PreviousState);
80 ok(Status == STATUS_SUCCESS, "ZwSetTimer failed with Status=0x%08lX", Status);
81 ok(PreviousState == FALSE, "Incorrect PreviousState returned when setting the timer");
82
83 // Cancel the timer
84 CurrentState = TRUE;
85 Status = ZwCancelTimer(HandleOpened, &CurrentState);
86 ok(Status == STATUS_SUCCESS, "ZwCancelTimer failed with Status=0x%08lX", Status);
87 ok(CurrentState == FALSE, "Incorrect CurrentState returned when canceling the timer");
88
89 // Set the timer to some small value, because we'll wait for it to expire
90 DPRINT("Set timer 2\n");
91 DueTime.LowPart = -100;
92 DueTime.HighPart = -1;
93 PreviousState = TRUE;
94 Status = ZwSetTimer(HandleOpened, &DueTime, NULL, NULL, FALSE, 0L, &PreviousState);
95 ok(Status == STATUS_SUCCESS, "ZwSetTimer failed with Status=0x%08lX", Status);
96 ok(PreviousState == FALSE, "Incorrect PreviousState returned when setting the timer");
97
98 // Wait until it expires
99 DPRINT("Wait till timer expires\n");
100 Status = ZwWaitForSingleObject(HandleOpened, FALSE, NULL);
101 ok(Status == STATUS_SUCCESS, "ZwWaitForSingleObject failed with Status=0x%08lX", Status);
102
103 // And cancel it
104 DPRINT("Cancel it\n");
105 CurrentState = FALSE;
106 Status = ZwCancelTimer(HandleOpened, &CurrentState);
107 ok(Status == STATUS_SUCCESS, "ZwCancelTimer failed with Status=0x%08lX", Status);
108 ok(CurrentState == TRUE, "Incorrect CurrentState returned when setting the timer");
109
110 // Test it with APC: Set, Cancel, check if APC has been called
111 DPRINT("Set timer with Apc (3)\n");
112 ApcCount = 0;
113 DueTime.LowPart = -10000;
114 DueTime.HighPart = -10;
115 PreviousState = FALSE;
116 Status = ZwSetTimer(HandleOpened, &DueTime,
117 (PTIMER_APC_ROUTINE)TestTimerApcRoutine, &ApcCount, FALSE,
118 0L, &PreviousState);
119
120 ok(Status == STATUS_SUCCESS, "ZwSetTimer failed with Status=0x%08lX", Status);
121 ok(PreviousState == TRUE, "Incorrect PreviousState returned when setting the timer");
122
123 DPRINT("Cancel it\n");
124 CurrentState = TRUE;
125 Status = ZwCancelTimer(HandleOpened, &CurrentState);
126 ok(Status == STATUS_SUCCESS, "ZwCancelTimer failed with Status=0x%08lX", Status);
127 ok(CurrentState == FALSE, "Incorrect CurrentState returned when cancelling the timer");
128 ok(ApcCount == 0, "Incorrect count of TimerApcRoutine calls: %ld, should be 0\n", ApcCount);
129
130 // Test setting the timer two times in a row, APC routine must not be called
131 DPRINT("Set timer with Apc (4)\n");
132 ApcCount = 0;
133 DueTime.LowPart = -10000;
134 DueTime.HighPart = -10;
135 PreviousState = TRUE;
136 Status = ZwSetTimer(HandleOpened, &DueTime,
137 (PTIMER_APC_ROUTINE)TestTimerApcRoutine, &ApcCount, FALSE,
138 0L, &PreviousState);
139 ok(Status == STATUS_SUCCESS, "ZwSetTimer failed with Status=0x%08lX", Status);
140 ok(PreviousState == FALSE, "Incorrect PreviousState returned when setting the timer");
141
142 // Set small due time, since we have to wait for timer to finish
143 DPRINT("Set timer with Apc (5)\n");
144 DueTime.LowPart = -10;
145 DueTime.HighPart = -1;
146 PreviousState = TRUE;
147 Status = ZwSetTimer(HandleOpened, &DueTime,
148 (PTIMER_APC_ROUTINE)TestTimerApcRoutine, &ApcCount, FALSE,
149 0L, &PreviousState);
150 ok(Status == STATUS_SUCCESS, "ZwSetTimer failed with Status=0x%08lX", Status);
151 ok(PreviousState == FALSE, "Incorrect PreviousState returned when setting the timer");
152
153 // Now wait till it's finished, and then check APC call
154 DPRINT("Wait for it\n");
155 Status = ZwWaitForSingleObject(HandleOpened, FALSE, NULL);
156 ok(Status == STATUS_SUCCESS, "ZwWaitForSingleObject failed with Status=0x%08lX", Status);
157
158 CurrentState = FALSE;
159 Status = ZwCancelTimer(HandleOpened, &CurrentState);
160 ok(Status == STATUS_SUCCESS, "ZwCancelTimer failed with Status=0x%08lX", Status);
161 ok(CurrentState == TRUE, "Incorrect CurrentState returned when cancelling the timer");
162 ok(ApcCount == 1, "Incorrect count of TimerApcRoutine calls: %ld, should be 1\n", ApcCount);
163
164 // Cleanup...
165 Status = ZwClose(HandleOpened);
166 ok(Status == STATUS_SUCCESS, "ZwClose failed with Status=0x%08lX", Status);
167
168 Status = ZwClose(TimerHandle);
169 ok(Status == STATUS_SUCCESS, "ZwClose failed with Status=0x%08lX", Status);
170
171 FinishTest(KeyHandle, L"ExTimerTest");
172 }