Remove an unneeded variable in test_query_timeofday().
[wine.git] / dlls / ntdll / tests / info.c
blobb38fa381d07f344e6db0287967e171fcac59120b
1 /* Unit test suite for *Information* Registry API functions
3 * Copyright 2005 Paul Vriens
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "ntdll_test.h"
23 static NTSTATUS (WINAPI * pNtQuerySystemInformation)(SYSTEM_INFORMATION_CLASS, PVOID, ULONG, PULONG);
24 static NTSTATUS (WINAPI * pNtQueryInformationProcess)(HANDLE, PROCESSINFOCLASS, PVOID, ULONG, PULONG);
26 static HMODULE hntdll = 0;
28 #define NTDLL_GET_PROC(func) \
29 p ## func = (void*)GetProcAddress(hntdll, #func); \
30 if(!p ## func) { \
31 trace("GetProcAddress(%s) failed\n", #func); \
32 FreeLibrary(hntdll); \
33 return FALSE; \
36 static BOOL InitFunctionPtrs(void)
38 hntdll = LoadLibraryA("ntdll.dll");
39 if(!hntdll) {
40 trace("Could not load ntdll.dll\n");
41 return FALSE;
43 if (hntdll)
45 NTDLL_GET_PROC(NtQuerySystemInformation)
46 NTDLL_GET_PROC(NtQueryInformationProcess)
48 return TRUE;
51 static void test_query_basic()
53 DWORD status;
54 ULONG ReturnLength;
55 SYSTEM_BASIC_INFORMATION sbi;
57 /* This test also covers some basic parameter testing that should be the same for
58 * every information class
61 /* Use a nonexistent info class */
62 trace("Check nonexistent info class\n");
63 status = pNtQuerySystemInformation(-1, NULL, 0, NULL);
64 ok( status == STATUS_INVALID_INFO_CLASS, "Expected STATUS_INVALID_INFO_CLASS, got %08lx\n", status);
66 /* Use an existing class but with a zero-length buffer */
67 trace("Check zero-length buffer\n");
68 status = pNtQuerySystemInformation(SystemBasicInformation, NULL, 0, NULL);
69 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08lx\n", status);
71 /* Use an existing class, correct length but no SystemInformation buffer */
72 trace("Check no SystemInformation buffer\n");
73 status = pNtQuerySystemInformation(SystemBasicInformation, NULL, sizeof(sbi), NULL);
74 ok( status == STATUS_ACCESS_VIOLATION, "Expected STATUS_ACCESS_VIOLATION, got %08lx\n", status);
76 /* Use a existing class, correct length, a pointer to a buffer but no ReturnLength pointer */
77 trace("Check no ReturnLength pointer\n");
78 status = pNtQuerySystemInformation(SystemBasicInformation, &sbi, sizeof(sbi), NULL);
79 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08lx\n", status);
81 /* Check a too large buffer size */
82 trace("Check a too large buffer size\n");
83 status = pNtQuerySystemInformation(SystemBasicInformation, &sbi, sizeof(sbi) * 2, &ReturnLength);
84 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08lx\n", status);
86 /* Finally some correct calls */
87 trace("Check with correct parameters\n");
88 status = pNtQuerySystemInformation(SystemBasicInformation, &sbi, sizeof(sbi), &ReturnLength);
89 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08lx\n", status);
90 ok( sizeof(sbi) == ReturnLength, "Inconsistent length (%d) <-> (%ld)\n", sizeof(sbi), ReturnLength);
92 /* Check if we have some return values */
93 trace("Number of Processors : %d\n", sbi.NumberOfProcessors);
94 ok( sbi.NumberOfProcessors > 0, "Expected more than 0 processors, got %d\n", sbi.NumberOfProcessors);
97 static void test_query_cpu()
99 DWORD status;
100 ULONG ReturnLength;
101 SYSTEM_CPU_INFORMATION sci;
103 status = pNtQuerySystemInformation(SystemCpuInformation, &sci, sizeof(sci), &ReturnLength);
104 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08lx\n", status);
105 ok( sizeof(sci) == ReturnLength, "Inconsistent length (%d) <-> (%ld)\n", sizeof(sci), ReturnLength);
107 /* Check if we have some return values */
108 trace("Processor FeatureSet : %08lx\n", sci.FeatureSet);
109 ok( sci.FeatureSet != 0, "Expected some features for this processor, got %08lx\n", sci.FeatureSet);
112 static void test_query_timeofday()
114 DWORD status;
115 ULONG ReturnLength;
117 /* Copy of our winternl.h structure turned into a private one */
118 typedef struct _SYSTEM_TIMEOFDAY_INFORMATION_PRIVATE {
119 LARGE_INTEGER liKeBootTime;
120 LARGE_INTEGER liKeSystemTime;
121 LARGE_INTEGER liExpTimeZoneBias;
122 ULONG uCurrentTimeZoneId;
123 DWORD dwUnknown1[5];
124 } SYSTEM_TIMEOFDAY_INFORMATION_PRIVATE, *PSYSTEM_TIMEOFDAY_INFORMATION_PRIVATE;
126 SYSTEM_TIMEOFDAY_INFORMATION_PRIVATE sti;
128 /* The struct size for NT (32 bytes) and Win2K/XP (48 bytes) differ.
130 * Windows 2000 and XP return STATUS_INFO_LENGTH_MISMATCH if the given buffer size is greater
131 * then 48 and 0 otherwise
132 * Windows NT returns STATUS_INFO_LENGTH_MISMATCH when the given buffer size is not correct
133 * and 0 otherwise
135 * Windows 2000 and XP copy the given buffer size into the provided buffer, if the return code is STATUS_SUCCESS
136 * NT only fills the buffer if the return code is STATUS_SUCCESS
140 status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, sizeof(sti), &ReturnLength);
142 if (status == STATUS_INFO_LENGTH_MISMATCH)
144 trace("Windows version is NT, we have to cater for differences with W2K/WinXP\n");
146 status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, 0, &ReturnLength);
147 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08lx\n", status);
148 ok( 0 == ReturnLength, "ReturnLength should be 0, it is (%ld)\n", ReturnLength);
150 sti.uCurrentTimeZoneId = 0xdeadbeef;
151 status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, 28, &ReturnLength);
152 ok( 0xdeadbeef == sti.uCurrentTimeZoneId, "This part of the buffer should not have been filled\n");
154 status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, 32, &ReturnLength);
155 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08lx\n", status);
156 ok( 32 == ReturnLength, "ReturnLength should be 0, it is (%ld)\n", ReturnLength);
158 else
160 status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, 0, &ReturnLength);
161 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08lx\n", status);
162 ok( 0 == ReturnLength, "ReturnLength should be 0, it is (%ld)\n", ReturnLength);
164 sti.uCurrentTimeZoneId = 0xdeadbeef;
165 status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, 24, &ReturnLength);
166 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08lx\n", status);
167 ok( 24 == ReturnLength, "ReturnLength should be 24, it is (%ld)\n", ReturnLength);
168 ok( 0xdeadbeef == sti.uCurrentTimeZoneId, "This part of the buffer should not have been filled\n");
170 sti.uCurrentTimeZoneId = 0xdeadbeef;
171 status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, 32, &ReturnLength);
172 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08lx\n", status);
173 ok( 32 == ReturnLength, "ReturnLength should be 32, it is (%ld)\n", ReturnLength);
174 ok( 0xdeadbeef != sti.uCurrentTimeZoneId, "Buffer should have been partially filled\n");
176 status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, 49, &ReturnLength);
177 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08lx\n", status);
178 ok( 0 == ReturnLength, "ReturnLength should be 0, it is (%ld)\n", ReturnLength);
180 status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, sizeof(sti), &ReturnLength);
181 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08lx\n", status);
182 ok( sizeof(sti) == ReturnLength, "Inconsistent length (%d) <-> (%ld)\n", sizeof(sti), ReturnLength);
185 /* Check if we have some return values */
186 trace("uCurrentTimeZoneId : (%ld)\n", sti.uCurrentTimeZoneId);
189 static void test_query_process()
191 DWORD status;
192 ULONG ReturnLength;
193 int i = 0, j = 0, k = 0;
194 int is_nt = 0;
195 SYSTEM_BASIC_INFORMATION sbi;
197 /* Copy of our winternl.h structure turned into a private one */
198 typedef struct _SYSTEM_PROCESS_INFORMATION_PRIVATE {
199 DWORD dwOffset;
200 DWORD dwThreadCount;
201 DWORD dwUnknown1[6];
202 FILETIME ftCreationTime;
203 FILETIME ftUserTime;
204 FILETIME ftKernelTime;
205 UNICODE_STRING ProcessName;
206 DWORD dwBasePriority;
207 DWORD dwProcessID;
208 DWORD dwParentProcessID;
209 DWORD dwHandleCount;
210 DWORD dwUnknown3;
211 DWORD dwUnknown4;
212 VM_COUNTERS vmCounters;
213 IO_COUNTERS ioCounters;
214 SYSTEM_THREAD_INFORMATION ti[1];
215 } SYSTEM_PROCESS_INFORMATION_PRIVATE, *PSYSTEM_PROCESS_INFORMATION_PRIVATE;
217 ULONG SystemInformationLength = sizeof(SYSTEM_PROCESS_INFORMATION_PRIVATE);
218 SYSTEM_PROCESS_INFORMATION_PRIVATE* spi = HeapAlloc(GetProcessHeap(), 0, SystemInformationLength);
220 /* Only W2K3 returns the needed length, the rest returns 0, so we have to loop */
222 for (;;)
224 status = pNtQuerySystemInformation(SystemProcessInformation, spi, SystemInformationLength, &ReturnLength);
226 if (status != STATUS_INFO_LENGTH_MISMATCH) break;
228 spi = HeapReAlloc(GetProcessHeap(), 0, spi , SystemInformationLength *= 2);
231 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08lx\n", status);
233 /* Get the first dwOffset, from this we can deduce the OS version we're running
235 * W2K/WinXP/W2K3:
236 * dwOffset for a process is 184 + (no. of threads) * sizeof(SYSTEM_THREAD_INFORMATION)
237 * NT:
238 * dwOffset for a process is 136 + (no. of threads) * sizeof(SYSTEM_THREAD_INFORMATION)
239 * Wine (with every windows version):
240 * dwOffset for a process is 0 if just this test is running
241 * dwOffset for a process is 184 + (no. of threads) * sizeof(SYSTEM_THREAD_INFORMATION) +
242 * ProcessName.MaximumLength
243 * if more wine processes are running
245 * Note : On windows the first process is in fact the Idle 'process' with a thread for every processor
248 pNtQuerySystemInformation(SystemBasicInformation, &sbi, sizeof(sbi), &ReturnLength);
250 is_nt = ( spi->dwOffset - (sbi.NumberOfProcessors * sizeof(SYSTEM_THREAD_INFORMATION)) == 136);
252 if (is_nt) trace("Windows version is NT, we will skip thread tests\n");
254 /* Check if we have some return values
256 * On windows there will be several processes running (Including the always present Idle and System)
257 * On wine we only have one (if this test is the only wine process running)
260 /* Loop through the processes */
262 for (;;)
264 i++;
266 ok( spi->dwThreadCount > 0, "Expected some threads for this process, got 0\"");
268 /* Loop through the threads, skip NT4 for now */
270 if (!is_nt)
272 for ( j = 0; j < spi->dwThreadCount; j++)
274 k++;
275 ok ( spi->ti[j].dwOwningPID == spi->dwProcessID,
276 "The owning pid of the thread (%ld) doesn't equal the pid (%ld) of the process\n",
277 spi->ti[j].dwOwningPID, spi->dwProcessID);
281 if (!spi->dwOffset) break;
283 spi = (SYSTEM_PROCESS_INFORMATION_PRIVATE*)((char*)spi + spi->dwOffset);
285 trace("Total number of running processes : %d\n", i);
286 if (!is_nt) trace("Total number of running threads : %d\n", k);
288 HeapFree( GetProcessHeap(), 0, spi);
291 static void test_query_handle()
293 DWORD status;
294 ULONG ReturnLength;
295 ULONG SystemInformationLength = sizeof(SYSTEM_HANDLE_INFORMATION);
296 SYSTEM_HANDLE_INFORMATION* shi = HeapAlloc(GetProcessHeap(), 0, SystemInformationLength);
298 /* Request the needed length : a SystemInformationLength greater than one struct sets ReturnLength */
299 status = pNtQuerySystemInformation(SystemHandleInformation, shi, SystemInformationLength, &ReturnLength);
301 /* The following check assumes more than one handle on any given system */
302 todo_wine
304 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08lx\n", status);
306 ok( ReturnLength > 0, "Expected ReturnLength to be > 0, it was %ld\n", ReturnLength);
308 SystemInformationLength = ReturnLength;
309 shi = HeapReAlloc(GetProcessHeap(), 0, shi , SystemInformationLength);
310 status = pNtQuerySystemInformation(SystemHandleInformation, shi, SystemInformationLength, &ReturnLength);
311 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08lx\n", status);
313 /* Check if we have some return values */
314 trace("Number of Handles : %ld\n", shi->Count);
315 todo_wine
317 /* our implementation is a stub for now */
318 ok( shi->Count > 1, "Expected more than 1 handles, got (%ld)\n", shi->Count);
321 HeapFree( GetProcessHeap(), 0, shi);
324 static void test_query_process_basic()
326 DWORD status;
327 ULONG ReturnLength;
329 typedef struct _PROCESS_BASIC_INFORMATION_PRIVATE {
330 DWORD ExitStatus;
331 DWORD PebBaseAddress;
332 DWORD AffinityMask;
333 DWORD BasePriority;
334 ULONG UniqueProcessId;
335 ULONG InheritedFromUniqueProcessId;
336 } PROCESS_BASIC_INFORMATION_PRIVATE, *PPROCESS_BASIC_INFORMATION_PRIVATE;
338 PROCESS_BASIC_INFORMATION_PRIVATE pbi;
340 /* This test also covers some basic parameter testing that should be the same for
341 * every information class
344 /* Use a nonexistent info class */
345 trace("Check nonexistent info class\n");
346 status = pNtQueryInformationProcess(NULL, -1, NULL, 0, NULL);
347 ok( status == STATUS_INVALID_INFO_CLASS, "Expected STATUS_INVALID_INFO_CLASS, got %08lx\n", status);
349 /* Do not give a handle and buffer */
350 trace("Check NULL handle and buffer and zero-length buffersize\n");
351 status = pNtQueryInformationProcess(NULL, ProcessBasicInformation, NULL, 0, NULL);
352 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08lx\n", status);
354 /* Use a correct info class and buffer size, but still no handle and buffer */
355 trace("Check NULL handle and buffer\n");
356 status = pNtQueryInformationProcess(NULL, ProcessBasicInformation, NULL, sizeof(pbi), NULL);
357 ok( status == STATUS_ACCESS_VIOLATION || status == STATUS_INVALID_HANDLE,
358 "Expected STATUS_ACCESS_VIOLATION or STATUS_INVALID_HANDLE(W2K3), got %08lx\n", status);
360 /* Use a correct info class and buffer size, but still no handle */
361 trace("Check NULL handle\n");
362 status = pNtQueryInformationProcess(NULL, ProcessBasicInformation, &pbi, sizeof(pbi), NULL);
363 ok( status == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got %08lx\n", status);
365 /* Use a greater buffer size */
366 trace("Check NULL handle and too large buffersize\n");
367 status = pNtQueryInformationProcess(NULL, ProcessBasicInformation, &pbi, sizeof(pbi) * 2, NULL);
368 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08lx\n", status);
370 /* Use no ReturnLength */
371 trace("Check NULL ReturnLength\n");
372 status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessBasicInformation, &pbi, sizeof(pbi), NULL);
373 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08lx\n", status);
375 /* Finally some correct calls */
376 trace("Check with correct parameters\n");
377 status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessBasicInformation, &pbi, sizeof(pbi), &ReturnLength);
378 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08lx\n", status);
379 ok( sizeof(pbi) == ReturnLength, "Inconsistent length (%d) <-> (%ld)\n", sizeof(pbi), ReturnLength);
381 /* Check if we have some return values */
382 trace("ProcessID : %ld\n", pbi.UniqueProcessId);
383 ok( pbi.UniqueProcessId > 0, "Expected a ProcessID > 0, got 0, got %ld\n", pbi.UniqueProcessId);
386 START_TEST(info)
388 if(!InitFunctionPtrs())
389 return;
391 /* NtQuerySystemInformation */
393 /* 0x0 SystemBasicInformation */
394 trace("Starting test_query_basic()\n");
395 test_query_basic();
397 /* 0x1 SystemCpuInformation */
398 trace("Starting test_query_cpu()\n");
399 test_query_cpu();
401 /* 0x3 SystemCpuInformation */
402 trace("Starting test_query_timeofday()\n");
403 test_query_timeofday();
405 /* 0x5 SystemProcessInformation */
406 trace("Starting test_query_process()\n");
407 test_query_process();
409 /* 0x10 SystemHandleInformation */
410 trace("Starting test_query_handle()\n");
411 test_query_handle();
413 /* NtQueryInformationProcess */
415 trace("Starting test_query_process_basic()\n");
416 test_query_process_basic();
418 FreeLibrary(hntdll);