- implement encoding and decoding of enumerated types, unsigned
[wine] / dlls / ntdll / tests / info.c
1 /* Unit test suite for *Information* Registry API functions
2  *
3  * Copyright 2005 Paul Vriens
4  *
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.
9  *
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.
14  *
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
18  *
19  */
20
21 #include "ntdll_test.h"
22
23 static NTSTATUS (WINAPI * pNtQuerySystemInformation)(SYSTEM_INFORMATION_CLASS, PVOID, ULONG, PULONG);
24 static NTSTATUS (WINAPI * pNtQueryInformationProcess)(HANDLE, PROCESSINFOCLASS, PVOID, ULONG, PULONG);
25
26 static HMODULE hntdll = 0;
27
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; \
34     }
35
36 static BOOL InitFunctionPtrs(void)
37 {
38     hntdll = LoadLibraryA("ntdll.dll");
39     if(!hntdll) {
40       trace("Could not load ntdll.dll\n");
41       return FALSE;
42     }
43     if (hntdll)
44     {
45       NTDLL_GET_PROC(NtQuerySystemInformation)
46       NTDLL_GET_PROC(NtQueryInformationProcess)
47     }
48     return TRUE;
49 }
50
51 static void test_query_basic()
52 {
53     DWORD status;
54     ULONG ReturnLength;
55     SYSTEM_BASIC_INFORMATION sbi;
56
57     /* This test also covers some basic parameter testing that should be the same for 
58      * every information class
59     */
60
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);
65
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);
70
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);
75
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);
80
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);
85
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);
91
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);
95 }
96
97 static void test_query_cpu()
98 {
99     DWORD status;
100     ULONG ReturnLength;
101     SYSTEM_CPU_INFORMATION sci;
102
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);
106                                                                                                                        
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);
110 }
111
112 static void test_query_timeofday()
113 {
114     DWORD status;
115     ULONG ReturnLength;
116     int isnt = 0;
117
118     /* Copy of our winternl.h structure turned into a private one */
119     typedef struct _SYSTEM_TIMEOFDAY_INFORMATION_PRIVATE {
120         LARGE_INTEGER liKeBootTime;
121         LARGE_INTEGER liKeSystemTime;
122         LARGE_INTEGER liExpTimeZoneBias;
123         ULONG uCurrentTimeZoneId;
124         DWORD dwUnknown1[5];
125     } SYSTEM_TIMEOFDAY_INFORMATION_PRIVATE, *PSYSTEM_TIMEOFDAY_INFORMATION_PRIVATE;
126
127     SYSTEM_TIMEOFDAY_INFORMATION_PRIVATE sti;
128   
129     /*  The structsize for NT (32 bytes) and Win2K/XP (48 bytes) differ.
130      *
131      *  Windows 2000 and XP return STATUS_INFO_LENGTH_MISMATCH if the given buffer size is greater
132      *  then 48 and 0 otherwise
133      *  Windows NT returns STATUS_INFO_LENGTH_MISMATCH when the given buffer size is not correct
134      *  and 0 otherwise
135      *
136      *  Windows 2000 and XP copy the given buffer size into the provided buffer, if the return code is STATUS_SUCCESS
137      *  NT only fills the buffer if the return code is STATUS_SUCCESS
138      *
139     */
140
141     status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, sizeof(sti), &ReturnLength);
142     isnt = ( status == STATUS_INFO_LENGTH_MISMATCH);
143
144     if (isnt)
145     {
146         trace("Windows version is NT, we have to cater for differences with W2K/WinXP\n");
147  
148         status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, 0, &ReturnLength);
149         ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08lx\n", status);
150         ok( 0 == ReturnLength, "ReturnLength should be 0, it is (%ld)\n", ReturnLength);
151
152         sti.uCurrentTimeZoneId = 0xdeadbeef;
153         status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, 28, &ReturnLength);
154         ok( 0xdeadbeef == sti.uCurrentTimeZoneId, "This part of the buffer should not have been filled\n");
155
156         status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, 32, &ReturnLength);
157         ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08lx\n", status);
158         ok( 32 == ReturnLength, "ReturnLength should be 0, it is (%ld)\n", ReturnLength);
159     }
160     else
161     {
162
163         status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, 0, &ReturnLength);
164         ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08lx\n", status);
165         ok( 0 == ReturnLength, "ReturnLength should be 0, it is (%ld)\n", ReturnLength);
166
167         sti.uCurrentTimeZoneId = 0xdeadbeef;
168         status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, 24, &ReturnLength);
169         ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08lx\n", status);
170         ok( 24 == ReturnLength, "ReturnLength should be 24, it is (%ld)\n", ReturnLength);
171         ok( 0xdeadbeef == sti.uCurrentTimeZoneId, "This part of the buffer should not have been filled\n");
172     
173         sti.uCurrentTimeZoneId = 0xdeadbeef;
174         status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, 32, &ReturnLength);
175         ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08lx\n", status);
176         ok( 32 == ReturnLength, "ReturnLength should be 32, it is (%ld)\n", ReturnLength);
177         ok( 0xdeadbeef != sti.uCurrentTimeZoneId, "Buffer should have been partially filled\n");
178     
179         status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, 49, &ReturnLength);
180         ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08lx\n", status);
181         ok( 0 == ReturnLength, "ReturnLength should be 0, it is (%ld)\n", ReturnLength);
182     
183         status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, sizeof(sti), &ReturnLength);
184         ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08lx\n", status);
185         ok( sizeof(sti) == ReturnLength, "Inconsistent length (%d) <-> (%ld)\n", sizeof(sti), ReturnLength);
186     }
187
188     /* Check if we have some return values */
189     trace("uCurrentTimeZoneId : (%ld)\n", sti.uCurrentTimeZoneId);
190 }
191
192 static void test_query_process()
193 {
194     DWORD status;
195     ULONG ReturnLength;
196     int i = 0, j = 0, k = 0;
197     int isnt = 0;
198     SYSTEM_BASIC_INFORMATION sbi;
199
200     /* Copy of our winternl.h structure turned into a private one */
201     typedef struct _SYSTEM_PROCESS_INFORMATION_PRIVATE {
202         DWORD dwOffset;
203         DWORD dwThreadCount;
204         DWORD dwUnknown1[6];
205         FILETIME ftCreationTime;
206         FILETIME ftUserTime;
207         FILETIME ftKernelTime;
208         UNICODE_STRING ProcessName;
209         DWORD dwBasePriority;
210         DWORD dwProcessID;
211         DWORD dwParentProcessID;
212         DWORD dwHandleCount;
213         DWORD dwUnknown3;
214         DWORD dwUnknown4;
215         VM_COUNTERS vmCounters;
216         IO_COUNTERS ioCounters;
217         SYSTEM_THREAD_INFORMATION ti[1];
218     } SYSTEM_PROCESS_INFORMATION_PRIVATE, *PSYSTEM_PROCESS_INFORMATION_PRIVATE;
219
220     ULONG SystemInformationLength = sizeof(SYSTEM_PROCESS_INFORMATION_PRIVATE);
221     SYSTEM_PROCESS_INFORMATION_PRIVATE* spi = HeapAlloc(GetProcessHeap(), 0, SystemInformationLength);
222
223     /* Only W2K3 returns the needed length, the rest returns 0, so we have to loop */
224
225     for (;;)
226     {
227         status = pNtQuerySystemInformation(SystemProcessInformation, spi, SystemInformationLength, &ReturnLength);
228
229         if (status != STATUS_INFO_LENGTH_MISMATCH) break;
230         
231         spi = HeapReAlloc(GetProcessHeap(), 0, spi , SystemInformationLength *= 2);
232     }
233
234     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08lx\n", status);
235
236     /* Get the first dwOffset, from this we can deduce the OS version we're running
237      *
238      * W2K/WinXP/W2K3:
239      *   dwOffset for a process is 184 + (no. of threads) * sizeof(SYSTEM_THREAD_INFORMATION)
240      * NT:
241      *   dwOffset for a process is 136 + (no. of threads) * sizeof(SYSTEM_THREAD_INFORMATION)
242      * Wine (with every windows version):
243      *   dwOffset for a process is 0 if just this test is running
244      *   dwOffset for a process is 184 + (no. of threads) * sizeof(SYSTEM_THREAD_INFORMATION) +
245      *                             ProcessName.MaximumLength
246      *     if more wine processes are running
247      *
248      * Note : On windows the first process is in fact the Idle 'process' with a thread for every processor
249     */
250
251     pNtQuerySystemInformation(SystemBasicInformation, &sbi, sizeof(sbi), &ReturnLength);
252
253     isnt = ( spi->dwOffset - (sbi.NumberOfProcessors * sizeof(SYSTEM_THREAD_INFORMATION)) == 136);
254
255     if (isnt) trace("Windows version is NT, we will skip thread tests\n");
256
257     /* Check if we have some return values
258      * 
259      * On windows there will be several processes running (Including the always present Idle and System)
260      * On wine we only have one (if this test is the only wine process running)
261     */
262     
263     /* Loop through the processes */
264
265     for (;;)
266     {
267         i++;
268
269         ok( spi->dwThreadCount > 0, "Expected some threads for this process, got 0\"");
270
271         /* Loop through the threads, skip NT4 for now */
272         
273         if (!isnt)
274         {
275             for ( j = 0; j < spi->dwThreadCount; j++) 
276             {
277                 k++;
278                 ok ( spi->ti[j].dwOwningPID == spi->dwProcessID, 
279                      "The owning pid of the thread (%ld) doesn't equal the pid (%ld) of the process\n",
280                      spi->ti[j].dwOwningPID, spi->dwProcessID);
281             }
282         }
283
284         if (!spi->dwOffset) break;
285                                                                                                                               
286         spi = (SYSTEM_PROCESS_INFORMATION_PRIVATE*)((char*)spi + spi->dwOffset);
287     }
288     trace("Total number of running processes : %d\n", i);
289     if (!isnt) trace("Total number of running threads   : %d\n", k);
290
291     HeapFree( GetProcessHeap(), 0, spi);
292 }
293
294 static void test_query_handle()
295 {
296     DWORD status;
297     ULONG ReturnLength;
298     ULONG SystemInformationLength = sizeof(SYSTEM_HANDLE_INFORMATION);
299     SYSTEM_HANDLE_INFORMATION* shi = HeapAlloc(GetProcessHeap(), 0, SystemInformationLength);
300
301     /* Request the needed length : a SystemInformationLength greater than one struct sets ReturnLength */
302     status = pNtQuerySystemInformation(SystemHandleInformation, shi, SystemInformationLength, &ReturnLength);
303
304     /* The following check assumes more than one handle on any given system */
305     todo_wine
306     {
307         ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08lx\n", status);
308     }
309     ok( ReturnLength > 0, "Expected ReturnLength to be > 0, it was %ld\n", ReturnLength);
310
311     SystemInformationLength = ReturnLength;
312     shi = HeapReAlloc(GetProcessHeap(), 0, shi , SystemInformationLength);
313     status = pNtQuerySystemInformation(SystemHandleInformation, shi, SystemInformationLength, &ReturnLength);
314     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08lx\n", status);
315
316     /* Check if we have some return values */
317     trace("Number of Handles : %ld\n", shi->Count);
318     todo_wine
319     {
320         /* our implementation is a stub for now */
321         ok( shi->Count > 1, "Expected more than 1 handles, got (%ld)\n", shi->Count);
322     }
323
324     HeapFree( GetProcessHeap(), 0, shi);
325 }
326
327 static void test_query_process_basic()
328 {
329     DWORD status;
330     ULONG ReturnLength;
331
332     typedef struct _PROCESS_BASIC_INFORMATION_PRIVATE {
333         DWORD ExitStatus;
334         DWORD PebBaseAddress;
335         DWORD AffinityMask;
336         DWORD BasePriority;
337         ULONG UniqueProcessId;
338         ULONG InheritedFromUniqueProcessId;
339     } PROCESS_BASIC_INFORMATION_PRIVATE, *PPROCESS_BASIC_INFORMATION_PRIVATE;
340
341     PROCESS_BASIC_INFORMATION_PRIVATE pbi;
342
343     /* This test also covers some basic parameter testing that should be the same for
344      * every information class
345     */
346
347     /* Use a nonexistent info class */
348     trace("Check nonexistent info class\n");
349     status = pNtQueryInformationProcess(NULL, -1, NULL, 0, NULL);
350     ok( status == STATUS_INVALID_INFO_CLASS, "Expected STATUS_INVALID_INFO_CLASS, got %08lx\n", status);
351
352     /* Do not give a handle and buffer */
353     trace("Check NULL handle and buffer and zero-length buffersize\n");
354     status = pNtQueryInformationProcess(NULL, ProcessBasicInformation, NULL, 0, NULL);
355     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08lx\n", status);
356
357     /* Use a correct info class and buffer size, but still no handle and buffer */
358     trace("Check NULL handle and buffer\n");
359     status = pNtQueryInformationProcess(NULL, ProcessBasicInformation, NULL, sizeof(pbi), NULL);
360     ok( status == STATUS_ACCESS_VIOLATION || status == STATUS_INVALID_HANDLE,
361         "Expected STATUS_ACCESS_VIOLATION or STATUS_INVALID_HANDLE(W2K3), got %08lx\n", status);
362
363     /* Use a correct info class and buffer size, but still no handle */
364     trace("Check NULL handle\n");
365     status = pNtQueryInformationProcess(NULL, ProcessBasicInformation, &pbi, sizeof(pbi), NULL);
366     ok( status == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got %08lx\n", status);
367
368     /* Use a greater buffer size */
369     trace("Check NULL handle and too large buffersize\n");
370     status = pNtQueryInformationProcess(NULL, ProcessBasicInformation, &pbi, sizeof(pbi) * 2, NULL);
371     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08lx\n", status);
372
373     /* Use no ReturnLength */
374     trace("Check NULL ReturnLength\n");
375     status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessBasicInformation, &pbi, sizeof(pbi), NULL);
376     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08lx\n", status);
377
378     /* Finally some correct calls */
379     trace("Check with correct parameters\n");
380     status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessBasicInformation, &pbi, sizeof(pbi), &ReturnLength);
381     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08lx\n", status);
382     ok( sizeof(pbi) == ReturnLength, "Inconsistent length (%d) <-> (%ld)\n", sizeof(pbi), ReturnLength);
383                                                                                                                                                
384     /* Check if we have some return values */
385     trace("ProcessID : %ld\n", pbi.UniqueProcessId);
386     ok( pbi.UniqueProcessId > 0, "Expected a ProcessID > 0, got 0, got %ld\n", pbi.UniqueProcessId);
387 }
388
389 START_TEST(info)
390 {
391     if(!InitFunctionPtrs())
392         return;
393
394     /* NtQuerySystemInformation */
395
396     /* 0x0 SystemBasicInformation */
397     trace("Starting test_query_basic()\n");
398     test_query_basic();
399
400     /* 0x1 SystemCpuInformation */
401     trace("Starting test_query_cpu()\n");
402     test_query_cpu();
403
404     /* 0x3 SystemCpuInformation */
405     trace("Starting test_query_timeofday()\n");
406     test_query_timeofday();
407
408     /* 0x5 SystemProcessInformation */
409     trace("Starting test_query_process()\n");
410     test_query_process();
411
412     /* 0x10 SystemHandleInformation */
413     trace("Starting test_query_handle()\n");
414     test_query_handle();
415
416     /* NtQueryInformationProcess */
417
418     trace("Starting test_query_process_basic()\n");
419     test_query_process_basic();
420
421     FreeLibrary(hntdll);
422 }