Merge aicom-network-branch (without NDIS changes for now)
[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
48 VOID
49 ExTimerTest()
50 {
51 UNICODE_STRING TimerName;
52 OBJECT_ATTRIBUTES ObjectAttributes;
53 HANDLE TimerHandle;
54 HANDLE HandleOpened;
55 LARGE_INTEGER DueTime;
56 BOOLEAN PreviousState, CurrentState;
57 NTSTATUS Status;
58 ULONG ApcCount;
59
60 StartTest();
61
62 // Create the timer
63 RtlInitUnicodeString(&TimerName, L"\\TestTimer");
64 InitializeObjectAttributes(&ObjectAttributes, &TimerName, 0, NULL, NULL);
65 Status = ZwCreateTimer(&TimerHandle, TIMER_ALL_ACCESS,
66 &ObjectAttributes, NotificationTimer);
67 ok(Status == STATUS_SUCCESS, "ZwCreateTimer failed with Status=0x%08lX", Status);
68
69 // Open the timer
70 Status = ZwOpenTimer(&HandleOpened, TIMER_ALL_ACCESS, &ObjectAttributes);
71 ok(Status == STATUS_SUCCESS, "ZwOpenTimer failed with Status=0x%08lX", Status);
72
73 // Set the timer, to some rather high value so it doesn't expire
74 DPRINT("Set timer 1\n");
75 DueTime.LowPart = -10000;
76 DueTime.HighPart = -10;
77 PreviousState = TRUE;
78 Status = ZwSetTimer(HandleOpened, &DueTime, NULL, NULL, FALSE, 0L, &PreviousState);
79 ok(Status == STATUS_SUCCESS, "ZwSetTimer failed with Status=0x%08lX", Status);
80 ok(PreviousState == FALSE, "Incorrect PreviousState returned when setting the timer");
81
82 // Cancel the timer
83 CurrentState = TRUE;
84 Status = ZwCancelTimer(HandleOpened, &CurrentState);
85 ok(Status == STATUS_SUCCESS, "ZwCancelTimer failed with Status=0x%08lX", Status);
86 ok(CurrentState == FALSE, "Incorrect CurrentState returned when canceling the timer");
87
88 // Set the timer to some small value, because we'll wait for it to expire
89 DPRINT("Set timer 2\n");
90 DueTime.LowPart = -100;
91 DueTime.HighPart = -1;
92 PreviousState = TRUE;
93 Status = ZwSetTimer(HandleOpened, &DueTime, NULL, NULL, FALSE, 0L, &PreviousState);
94 ok(Status == STATUS_SUCCESS, "ZwSetTimer failed with Status=0x%08lX", Status);
95 ok(PreviousState == FALSE, "Incorrect PreviousState returned when setting the timer");
96
97 // Wait until it expires
98 DPRINT("Wait till timer expires\n");
99 Status = ZwWaitForSingleObject(HandleOpened, FALSE, NULL);
100 ok(Status == STATUS_SUCCESS, "ZwWaitForSingleObject failed with Status=0x%08lX", Status);
101
102 // And cancel it
103 DPRINT("Cancel it\n");
104 CurrentState = FALSE;
105 Status = ZwCancelTimer(HandleOpened, &CurrentState);
106 ok(Status == STATUS_SUCCESS, "ZwCancelTimer failed with Status=0x%08lX", Status);
107 ok(CurrentState == TRUE, "Incorrect CurrentState returned when setting the timer");
108
109 // Test it with APC: Set, Cancel, check if APC has been called
110 DPRINT("Set timer with Apc (3)\n");
111 ApcCount = 0;
112 DueTime.LowPart = -10000;
113 DueTime.HighPart = -10;
114 PreviousState = FALSE;
115 Status = ZwSetTimer(HandleOpened, &DueTime,
116 (PTIMER_APC_ROUTINE)TestTimerApcRoutine, &ApcCount, FALSE,
117 0L, &PreviousState);
118
119 ok(Status == STATUS_SUCCESS, "ZwSetTimer failed with Status=0x%08lX", Status);
120 ok(PreviousState == TRUE, "Incorrect PreviousState returned when setting the timer");
121
122 DPRINT("Cancel it\n");
123 CurrentState = TRUE;
124 Status = ZwCancelTimer(HandleOpened, &CurrentState);
125 ok(Status == STATUS_SUCCESS, "ZwCancelTimer failed with Status=0x%08lX", Status);
126 ok(CurrentState == FALSE, "Incorrect CurrentState returned when cancelling the timer");
127 ok(ApcCount == 0, "Incorrect count of TimerApcRoutine calls: %ld, should be 0\n", ApcCount);
128
129 // Test setting the timer two times in a row, APC routine must not be called
130 DPRINT("Set timer with Apc (4)\n");
131 ApcCount = 0;
132 DueTime.LowPart = -10000;
133 DueTime.HighPart = -10;
134 PreviousState = TRUE;
135 Status = ZwSetTimer(HandleOpened, &DueTime,
136 (PTIMER_APC_ROUTINE)TestTimerApcRoutine, &ApcCount, FALSE,
137 0L, &PreviousState);
138 ok(Status == STATUS_SUCCESS, "ZwSetTimer failed with Status=0x%08lX", Status);
139 ok(PreviousState == FALSE, "Incorrect PreviousState returned when setting the timer");
140
141 // Set small due time, since we have to wait for timer to finish
142 DPRINT("Set timer with Apc (5)\n");
143 DueTime.LowPart = -10;
144 DueTime.HighPart = -1;
145 PreviousState = TRUE;
146 Status = ZwSetTimer(HandleOpened, &DueTime,
147 (PTIMER_APC_ROUTINE)TestTimerApcRoutine, &ApcCount, FALSE,
148 0L, &PreviousState);
149 ok(Status == STATUS_SUCCESS, "ZwSetTimer failed with Status=0x%08lX", Status);
150 ok(PreviousState == FALSE, "Incorrect PreviousState returned when setting the timer");
151
152 // Now wait till it's finished, and then check APC call
153 DPRINT("Wait for it\n");
154 Status = ZwWaitForSingleObject(HandleOpened, FALSE, NULL);
155 ok(Status == STATUS_SUCCESS, "ZwWaitForSingleObject failed with Status=0x%08lX", Status);
156
157 CurrentState = FALSE;
158 Status = ZwCancelTimer(HandleOpened, &CurrentState);
159 ok(Status == STATUS_SUCCESS, "ZwCancelTimer failed with Status=0x%08lX", Status);
160 ok(CurrentState == TRUE, "Incorrect CurrentState returned when cancelling the timer");
161 ok(ApcCount == 1, "Incorrect count of TimerApcRoutine calls: %ld, should be 1\n", ApcCount);
162
163 // Cleanup...
164 Status = ZwClose(HandleOpened);
165 ok(Status == STATUS_SUCCESS, "ZwClose failed with Status=0x%08lX", Status);
166
167 Status = ZwClose(TimerHandle);
168 ok(Status == STATUS_SUCCESS, "ZwClose failed with Status=0x%08lX", Status);
169
170 FinishTest("NTOSKRNL Executive Timer");
171 }
172
173 /* PUBLIC FUNCTIONS ***********************************************************/
174
175 VOID
176 NtoskrnlExecutiveTests()
177 {
178 ExTimerTest();
179 }