ntdll/tests: Use GetModuleHandle and skip.
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 static NTSTATUS (WINAPI * pNtReadVirtualMemory)(HANDLE, const void*, void*, SIZE_T, SIZE_T*);
26
27 /* one_before_last_pid is used to be able to compare values of a still running process
28    with the output of the test_query_process_times and test_query_process_handlecount tests.
29 */
30 static DWORD one_before_last_pid = 0;
31
32 #define NTDLL_GET_PROC(func) do {                     \
33     p ## func = (void*)GetProcAddress(hntdll, #func); \
34     if(!p ## func) { \
35       trace("GetProcAddress(%s) failed\n", #func); \
36       return FALSE; \
37     } \
38   } while(0)
39
40 static BOOL InitFunctionPtrs(void)
41 {
42     /* All needed functions are NT based, so using GetModuleHandle is a good check */
43     HMODULE hntdll = GetModuleHandle("ntdll");
44     if (!hntdll)
45     {
46         skip("Not running on NT\n");
47         return FALSE;
48     }
49
50     NTDLL_GET_PROC(NtQuerySystemInformation);
51     NTDLL_GET_PROC(NtQueryInformationProcess);
52     NTDLL_GET_PROC(NtReadVirtualMemory);
53
54     return TRUE;
55 }
56
57 static void test_query_basic(void)
58 {
59     DWORD status;
60     ULONG ReturnLength;
61     SYSTEM_BASIC_INFORMATION sbi;
62
63     /* This test also covers some basic parameter testing that should be the same for 
64      * every information class
65     */
66
67     /* Use a nonexistent info class */
68     trace("Check nonexistent info class\n");
69     status = pNtQuerySystemInformation(-1, NULL, 0, NULL);
70     ok( status == STATUS_INVALID_INFO_CLASS, "Expected STATUS_INVALID_INFO_CLASS, got %08x\n", status);
71
72     /* Use an existing class but with a zero-length buffer */
73     trace("Check zero-length buffer\n");
74     status = pNtQuerySystemInformation(SystemBasicInformation, NULL, 0, NULL);
75     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
76
77     /* Use an existing class, correct length but no SystemInformation buffer */
78     trace("Check no SystemInformation buffer\n");
79     status = pNtQuerySystemInformation(SystemBasicInformation, NULL, sizeof(sbi), NULL);
80     ok( status == STATUS_ACCESS_VIOLATION, "Expected STATUS_ACCESS_VIOLATION, got %08x\n", status);
81
82     /* Use a existing class, correct length, a pointer to a buffer but no ReturnLength pointer */
83     trace("Check no ReturnLength pointer\n");
84     status = pNtQuerySystemInformation(SystemBasicInformation, &sbi, sizeof(sbi), NULL);
85     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
86
87     /* Check a too large buffer size */
88     trace("Check a too large buffer size\n");
89     status = pNtQuerySystemInformation(SystemBasicInformation, &sbi, sizeof(sbi) * 2, &ReturnLength);
90     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
91
92     /* Finally some correct calls */
93     trace("Check with correct parameters\n");
94     status = pNtQuerySystemInformation(SystemBasicInformation, &sbi, sizeof(sbi), &ReturnLength);
95     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
96     ok( sizeof(sbi) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
97
98     /* Check if we have some return values */
99     trace("Number of Processors : %d\n", sbi.NumberOfProcessors);
100     ok( sbi.NumberOfProcessors > 0, "Expected more than 0 processors, got %d\n", sbi.NumberOfProcessors);
101 }
102
103 static void test_query_cpu(void)
104 {
105     DWORD status;
106     ULONG ReturnLength;
107     SYSTEM_CPU_INFORMATION sci;
108
109     status = pNtQuerySystemInformation(SystemCpuInformation, &sci, sizeof(sci), &ReturnLength);
110     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
111     ok( sizeof(sci) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
112
113     /* Check if we have some return values */
114     trace("Processor FeatureSet : %08x\n", sci.FeatureSet);
115     ok( sci.FeatureSet != 0, "Expected some features for this processor, got %08x\n", sci.FeatureSet);
116 }
117
118 static void test_query_performance(void)
119 {
120     DWORD status;
121     ULONG ReturnLength;
122     SYSTEM_PERFORMANCE_INFORMATION spi;
123
124     status = pNtQuerySystemInformation(SystemPerformanceInformation, &spi, 0, &ReturnLength);
125     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
126
127     status = pNtQuerySystemInformation(SystemPerformanceInformation, &spi, sizeof(spi), &ReturnLength);
128     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
129     ok( sizeof(spi) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
130
131     status = pNtQuerySystemInformation(SystemPerformanceInformation, &spi, sizeof(spi) + 2, &ReturnLength);
132     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
133     ok( sizeof(spi) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
134
135     /* Not return values yet, as struct members are unknown */
136 }
137
138 static void test_query_timeofday(void)
139 {
140     DWORD status;
141     ULONG ReturnLength;
142
143     /* Copy of our winternl.h structure turned into a private one */
144     typedef struct _SYSTEM_TIMEOFDAY_INFORMATION_PRIVATE {
145         LARGE_INTEGER liKeBootTime;
146         LARGE_INTEGER liKeSystemTime;
147         LARGE_INTEGER liExpTimeZoneBias;
148         ULONG uCurrentTimeZoneId;
149         DWORD dwUnknown1[5];
150     } SYSTEM_TIMEOFDAY_INFORMATION_PRIVATE, *PSYSTEM_TIMEOFDAY_INFORMATION_PRIVATE;
151
152     SYSTEM_TIMEOFDAY_INFORMATION_PRIVATE sti;
153   
154     /*  The struct size for NT (32 bytes) and Win2K/XP (48 bytes) differ.
155      *
156      *  Windows 2000 and XP return STATUS_INFO_LENGTH_MISMATCH if the given buffer size is greater
157      *  then 48 and 0 otherwise
158      *  Windows NT returns STATUS_INFO_LENGTH_MISMATCH when the given buffer size is not correct
159      *  and 0 otherwise
160      *
161      *  Windows 2000 and XP copy the given buffer size into the provided buffer, if the return code is STATUS_SUCCESS
162      *  NT only fills the buffer if the return code is STATUS_SUCCESS
163      *
164     */
165
166     status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, sizeof(sti), &ReturnLength);
167
168     if (status == STATUS_INFO_LENGTH_MISMATCH)
169     {
170         trace("Windows version is NT, we have to cater for differences with W2K/WinXP\n");
171  
172         status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, 0, &ReturnLength);
173         ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
174         ok( 0 == ReturnLength, "ReturnLength should be 0, it is (%d)\n", ReturnLength);
175
176         sti.uCurrentTimeZoneId = 0xdeadbeef;
177         status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, 28, &ReturnLength);
178         ok( 0xdeadbeef == sti.uCurrentTimeZoneId, "This part of the buffer should not have been filled\n");
179
180         status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, 32, &ReturnLength);
181         ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
182         ok( 32 == ReturnLength, "ReturnLength should be 0, it is (%d)\n", ReturnLength);
183     }
184     else
185     {
186         status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, 0, &ReturnLength);
187         ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
188         ok( 0 == ReturnLength, "ReturnLength should be 0, it is (%d)\n", ReturnLength);
189
190         sti.uCurrentTimeZoneId = 0xdeadbeef;
191         status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, 24, &ReturnLength);
192         ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
193         ok( 24 == ReturnLength, "ReturnLength should be 24, it is (%d)\n", ReturnLength);
194         ok( 0xdeadbeef == sti.uCurrentTimeZoneId, "This part of the buffer should not have been filled\n");
195     
196         sti.uCurrentTimeZoneId = 0xdeadbeef;
197         status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, 32, &ReturnLength);
198         ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
199         ok( 32 == ReturnLength, "ReturnLength should be 32, it is (%d)\n", ReturnLength);
200         ok( 0xdeadbeef != sti.uCurrentTimeZoneId, "Buffer should have been partially filled\n");
201     
202         status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, 49, &ReturnLength);
203         ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
204         ok( 0 == ReturnLength, "ReturnLength should be 0, it is (%d)\n", ReturnLength);
205     
206         status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, sizeof(sti), &ReturnLength);
207         ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
208         ok( sizeof(sti) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
209     }
210
211     /* Check if we have some return values */
212     trace("uCurrentTimeZoneId : (%d)\n", sti.uCurrentTimeZoneId);
213 }
214
215 static void test_query_process(void)
216 {
217     DWORD status;
218     DWORD last_pid;
219     ULONG ReturnLength;
220     int i = 0, k = 0;
221     int is_nt = 0;
222     SYSTEM_BASIC_INFORMATION sbi;
223
224     /* Copy of our winternl.h structure turned into a private one */
225     typedef struct _SYSTEM_PROCESS_INFORMATION_PRIVATE {
226         DWORD dwOffset;
227         DWORD dwThreadCount;
228         DWORD dwUnknown1[6];
229         FILETIME ftCreationTime;
230         FILETIME ftUserTime;
231         FILETIME ftKernelTime;
232         UNICODE_STRING ProcessName;
233         DWORD dwBasePriority;
234         DWORD dwProcessID;
235         DWORD dwParentProcessID;
236         DWORD dwHandleCount;
237         DWORD dwUnknown3;
238         DWORD dwUnknown4;
239         VM_COUNTERS vmCounters;
240         IO_COUNTERS ioCounters;
241         SYSTEM_THREAD_INFORMATION ti[1];
242     } SYSTEM_PROCESS_INFORMATION_PRIVATE, *PSYSTEM_PROCESS_INFORMATION_PRIVATE;
243
244     ULONG SystemInformationLength = sizeof(SYSTEM_PROCESS_INFORMATION_PRIVATE);
245     SYSTEM_PROCESS_INFORMATION_PRIVATE* spi = HeapAlloc(GetProcessHeap(), 0, SystemInformationLength);
246
247     /* Only W2K3 returns the needed length, the rest returns 0, so we have to loop */
248
249     for (;;)
250     {
251         status = pNtQuerySystemInformation(SystemProcessInformation, spi, SystemInformationLength, &ReturnLength);
252
253         if (status != STATUS_INFO_LENGTH_MISMATCH) break;
254         
255         spi = HeapReAlloc(GetProcessHeap(), 0, spi , SystemInformationLength *= 2);
256     }
257
258     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
259
260     /* Get the first dwOffset, from this we can deduce the OS version we're running
261      *
262      * W2K/WinXP/W2K3:
263      *   dwOffset for a process is 184 + (no. of threads) * sizeof(SYSTEM_THREAD_INFORMATION)
264      * NT:
265      *   dwOffset for a process is 136 + (no. of threads) * sizeof(SYSTEM_THREAD_INFORMATION)
266      * Wine (with every windows version):
267      *   dwOffset for a process is 0 if just this test is running
268      *   dwOffset for a process is 184 + (no. of threads) * sizeof(SYSTEM_THREAD_INFORMATION) +
269      *                             ProcessName.MaximumLength
270      *     if more wine processes are running
271      *
272      * Note : On windows the first process is in fact the Idle 'process' with a thread for every processor
273     */
274
275     pNtQuerySystemInformation(SystemBasicInformation, &sbi, sizeof(sbi), &ReturnLength);
276
277     is_nt = ( spi->dwOffset - (sbi.NumberOfProcessors * sizeof(SYSTEM_THREAD_INFORMATION)) == 136);
278
279     if (is_nt) skip("Windows version is NT, we will skip thread tests\n");
280
281     /* Check if we have some return values
282      * 
283      * On windows there will be several processes running (Including the always present Idle and System)
284      * On wine we only have one (if this test is the only wine process running)
285     */
286     
287     /* Loop through the processes */
288
289     for (;;)
290     {
291         i++;
292
293         last_pid = spi->dwProcessID;
294
295         ok( spi->dwThreadCount > 0, "Expected some threads for this process, got 0\n");
296
297         /* Loop through the threads, skip NT4 for now */
298         
299         if (!is_nt)
300         {
301             DWORD j;
302             for ( j = 0; j < spi->dwThreadCount; j++) 
303             {
304                 k++;
305                 ok ( spi->ti[j].dwOwningPID == spi->dwProcessID, 
306                      "The owning pid of the thread (%d) doesn't equal the pid (%d) of the process\n",
307                      spi->ti[j].dwOwningPID, spi->dwProcessID);
308             }
309         }
310
311         if (!spi->dwOffset) break;
312
313         one_before_last_pid = last_pid;
314
315         spi = (SYSTEM_PROCESS_INFORMATION_PRIVATE*)((char*)spi + spi->dwOffset);
316     }
317     trace("Total number of running processes : %d\n", i);
318     if (!is_nt) trace("Total number of running threads   : %d\n", k);
319
320     if (one_before_last_pid == 0) one_before_last_pid = last_pid;
321
322     HeapFree( GetProcessHeap(), 0, spi);
323 }
324
325 static void test_query_procperf(void)
326 {
327     DWORD status;
328     ULONG ReturnLength;
329     ULONG NeededLength;
330     SYSTEM_BASIC_INFORMATION sbi;
331     SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION* sppi;
332
333     /* Find out the number of processors */
334     status = pNtQuerySystemInformation(SystemBasicInformation, &sbi, sizeof(sbi), &ReturnLength);
335     NeededLength = sbi.NumberOfProcessors * sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION);
336
337     sppi = HeapAlloc(GetProcessHeap(), 0, NeededLength);
338
339     status = pNtQuerySystemInformation(SystemProcessorPerformanceInformation, sppi, 0, &ReturnLength);
340     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
341
342     /* Try it for 1 processor */
343     status = pNtQuerySystemInformation(SystemProcessorPerformanceInformation, sppi,
344                                        sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION), &ReturnLength);
345     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
346     ok( sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) == ReturnLength,
347         "Inconsistent length %d\n", ReturnLength);
348  
349     /* Try it for all processors */
350     status = pNtQuerySystemInformation(SystemProcessorPerformanceInformation, sppi, NeededLength, &ReturnLength);
351     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
352     ok( NeededLength == ReturnLength, "Inconsistent length (%d) <-> (%d)\n", NeededLength, ReturnLength);
353
354     /* A too large given buffer size */
355     sppi = HeapReAlloc(GetProcessHeap(), 0, sppi , NeededLength + 2);
356     status = pNtQuerySystemInformation(SystemProcessorPerformanceInformation, sppi, NeededLength + 2, &ReturnLength);
357     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
358     ok( NeededLength == ReturnLength, "Inconsistent length (%d) <-> (%d)\n", NeededLength, ReturnLength);
359
360     HeapFree( GetProcessHeap(), 0, sppi);
361 }
362
363 static void test_query_module(void)
364 {
365     DWORD status;
366     ULONG ReturnLength;
367     ULONG ModuleCount, i;
368
369     ULONG SystemInformationLength = sizeof(SYSTEM_MODULE_INFORMATION);
370     SYSTEM_MODULE_INFORMATION* smi = HeapAlloc(GetProcessHeap(), 0, SystemInformationLength); 
371     SYSTEM_MODULE* sm;
372
373     /* Request the needed length */
374     status = pNtQuerySystemInformation(SystemModuleInformation, smi, 0, &ReturnLength);
375     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
376     ok( ReturnLength > 0, "Expected a ReturnLength to show the needed length\n");
377
378     SystemInformationLength = ReturnLength;
379     smi = HeapReAlloc(GetProcessHeap(), 0, smi , SystemInformationLength);
380     status = pNtQuerySystemInformation(SystemModuleInformation, smi, SystemInformationLength, &ReturnLength);
381     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
382
383     ModuleCount = smi->ModulesCount;
384     sm = &smi->Modules[0];
385     todo_wine{
386         /* our implementation is a stub for now */
387         ok( ModuleCount > 0, "Expected some modules to be loaded\n");
388     }
389
390     /* Loop through all the modules/drivers, Wine doesn't get here (yet) */
391     for (i = 0; i < ModuleCount ; i++)
392     {
393         ok( i == sm->Id, "Id (%d) should have matched %u\n", sm->Id, i);
394         sm++;
395     }
396
397     HeapFree( GetProcessHeap(), 0, smi);
398 }
399
400 static void test_query_handle(void)
401 {
402     DWORD status;
403     ULONG ReturnLength;
404     ULONG SystemInformationLength = sizeof(SYSTEM_HANDLE_INFORMATION);
405     SYSTEM_HANDLE_INFORMATION* shi = HeapAlloc(GetProcessHeap(), 0, SystemInformationLength);
406
407     /* Request the needed length : a SystemInformationLength greater than one struct sets ReturnLength */
408     status = pNtQuerySystemInformation(SystemHandleInformation, shi, SystemInformationLength, &ReturnLength);
409
410     /* The following check assumes more than one handle on any given system */
411     todo_wine
412     {
413         ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
414     }
415     ok( ReturnLength > 0, "Expected ReturnLength to be > 0, it was %d\n", ReturnLength);
416
417     SystemInformationLength = ReturnLength;
418     shi = HeapReAlloc(GetProcessHeap(), 0, shi , SystemInformationLength);
419     status = pNtQuerySystemInformation(SystemHandleInformation, shi, SystemInformationLength, &ReturnLength);
420     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
421
422     /* Check if we have some return values */
423     trace("Number of Handles : %d\n", shi->Count);
424     todo_wine
425     {
426         /* our implementation is a stub for now */
427         ok( shi->Count > 1, "Expected more than 1 handles, got (%d)\n", shi->Count);
428     }
429
430     HeapFree( GetProcessHeap(), 0, shi);
431 }
432
433 static void test_query_cache(void)
434 {
435     DWORD status;
436     ULONG ReturnLength;
437     SYSTEM_CACHE_INFORMATION sci;
438
439     status = pNtQuerySystemInformation(SystemCacheInformation, &sci, 0, &ReturnLength);
440     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
441
442     status = pNtQuerySystemInformation(SystemCacheInformation, &sci, sizeof(sci), &ReturnLength);
443     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
444     ok( sizeof(sci) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
445
446     status = pNtQuerySystemInformation(SystemCacheInformation, &sci, sizeof(sci) + 2, &ReturnLength);
447     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
448     ok( sizeof(sci) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
449 }
450
451 static void test_query_interrupt(void)
452 {
453     DWORD status;
454     ULONG ReturnLength;
455     ULONG NeededLength;
456     SYSTEM_BASIC_INFORMATION sbi;
457     SYSTEM_INTERRUPT_INFORMATION* sii;
458
459     /* Find out the number of processors */
460     status = pNtQuerySystemInformation(SystemBasicInformation, &sbi, sizeof(sbi), &ReturnLength);
461     NeededLength = sbi.NumberOfProcessors * sizeof(SYSTEM_INTERRUPT_INFORMATION);
462
463     sii = HeapAlloc(GetProcessHeap(), 0, NeededLength);
464
465     status = pNtQuerySystemInformation(SystemInterruptInformation, sii, 0, &ReturnLength);
466     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
467
468     /* Try it for all processors */
469     status = pNtQuerySystemInformation(SystemInterruptInformation, sii, NeededLength, &ReturnLength);
470     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
471
472     /* Windows XP and W2K3 (and others?) always return 0 for the ReturnLength
473      * No test added for this as it's highly unlikely that an app depends on this
474     */
475
476     HeapFree( GetProcessHeap(), 0, sii);
477 }
478
479 static void test_query_kerndebug(void)
480 {
481     DWORD status;
482     ULONG ReturnLength;
483     SYSTEM_KERNEL_DEBUGGER_INFORMATION skdi;
484
485     status = pNtQuerySystemInformation(SystemKernelDebuggerInformation, &skdi, 0, &ReturnLength);
486     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
487
488     status = pNtQuerySystemInformation(SystemKernelDebuggerInformation, &skdi, sizeof(skdi), &ReturnLength);
489     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
490     ok( sizeof(skdi) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
491
492     status = pNtQuerySystemInformation(SystemKernelDebuggerInformation, &skdi, sizeof(skdi) + 2, &ReturnLength);
493     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
494     ok( sizeof(skdi) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
495 }
496
497 static void test_query_regquota(void)
498 {
499     DWORD status;
500     ULONG ReturnLength;
501     SYSTEM_REGISTRY_QUOTA_INFORMATION srqi;
502
503     status = pNtQuerySystemInformation(SystemRegistryQuotaInformation, &srqi, 0, &ReturnLength);
504     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
505
506     status = pNtQuerySystemInformation(SystemRegistryQuotaInformation, &srqi, sizeof(srqi), &ReturnLength);
507     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
508     ok( sizeof(srqi) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
509
510     status = pNtQuerySystemInformation(SystemRegistryQuotaInformation, &srqi, sizeof(srqi) + 2, &ReturnLength);
511     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
512     ok( sizeof(srqi) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
513 }
514
515 static void test_query_process_basic(void)
516 {
517     DWORD status;
518     ULONG ReturnLength;
519
520     typedef struct _PROCESS_BASIC_INFORMATION_PRIVATE {
521         DWORD ExitStatus;
522         DWORD PebBaseAddress;
523         DWORD AffinityMask;
524         DWORD BasePriority;
525         ULONG UniqueProcessId;
526         ULONG InheritedFromUniqueProcessId;
527     } PROCESS_BASIC_INFORMATION_PRIVATE, *PPROCESS_BASIC_INFORMATION_PRIVATE;
528
529     PROCESS_BASIC_INFORMATION_PRIVATE pbi;
530
531     /* This test also covers some basic parameter testing that should be the same for
532      * every information class
533     */
534
535     /* Use a nonexistent info class */
536     trace("Check nonexistent info class\n");
537     status = pNtQueryInformationProcess(NULL, -1, NULL, 0, NULL);
538     ok( status == STATUS_INVALID_INFO_CLASS, "Expected STATUS_INVALID_INFO_CLASS, got %08x\n", status);
539
540     /* Do not give a handle and buffer */
541     trace("Check NULL handle and buffer and zero-length buffersize\n");
542     status = pNtQueryInformationProcess(NULL, ProcessBasicInformation, NULL, 0, NULL);
543     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
544
545     /* Use a correct info class and buffer size, but still no handle and buffer */
546     trace("Check NULL handle and buffer\n");
547     status = pNtQueryInformationProcess(NULL, ProcessBasicInformation, NULL, sizeof(pbi), NULL);
548     ok( status == STATUS_ACCESS_VIOLATION || status == STATUS_INVALID_HANDLE,
549         "Expected STATUS_ACCESS_VIOLATION or STATUS_INVALID_HANDLE(W2K3), got %08x\n", status);
550
551     /* Use a correct info class and buffer size, but still no handle */
552     trace("Check NULL handle\n");
553     status = pNtQueryInformationProcess(NULL, ProcessBasicInformation, &pbi, sizeof(pbi), NULL);
554     ok( status == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got %08x\n", status);
555
556     /* Use a greater buffer size */
557     trace("Check NULL handle and too large buffersize\n");
558     status = pNtQueryInformationProcess(NULL, ProcessBasicInformation, &pbi, sizeof(pbi) * 2, NULL);
559     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
560
561     /* Use no ReturnLength */
562     trace("Check NULL ReturnLength\n");
563     status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessBasicInformation, &pbi, sizeof(pbi), NULL);
564     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
565
566     /* Finally some correct calls */
567     trace("Check with correct parameters\n");
568     status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessBasicInformation, &pbi, sizeof(pbi), &ReturnLength);
569     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
570     ok( sizeof(pbi) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
571
572     /* Everything is correct except a too large buffersize */
573     trace("Too large buffersize\n");
574     status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessBasicInformation, &pbi, sizeof(pbi) * 2, &ReturnLength);
575     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
576     ok( sizeof(pbi) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
577                                                                                                                                                
578     /* Check if we have some return values */
579     trace("ProcessID : %d\n", pbi.UniqueProcessId);
580     ok( pbi.UniqueProcessId > 0, "Expected a ProcessID > 0, got 0\n");
581 }
582
583 static void test_query_process_vm(void)
584 {
585     DWORD status;
586     ULONG ReturnLength;
587     VM_COUNTERS pvi;
588
589     status = pNtQueryInformationProcess(NULL, ProcessVmCounters, NULL, sizeof(pvi), NULL);
590     ok( status == STATUS_ACCESS_VIOLATION || status == STATUS_INVALID_HANDLE,
591         "Expected STATUS_ACCESS_VIOLATION or STATUS_INVALID_HANDLE(W2K3), got %08x\n", status);
592
593     status = pNtQueryInformationProcess(NULL, ProcessVmCounters, &pvi, sizeof(pvi), NULL);
594     ok( status == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got %08x\n", status);
595
596     /* Windows XP and W2K3 will report success for a size of 44 AND 48 !
597        Windows W2K will only report success for 44.
598        For now we only care for 44, which is sizeof(VM_COUNTERS)
599        If an app depends on it, we have to implement this in ntdll/process.c
600     */
601
602     status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessVmCounters, &pvi, 24, &ReturnLength);
603     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
604
605     status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessVmCounters, &pvi, sizeof(pvi), &ReturnLength);
606     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
607     ok( sizeof(pvi) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
608
609     status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessVmCounters, &pvi, 46, &ReturnLength);
610     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
611     ok( sizeof(pvi) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
612
613     /* Check if we have some return values */
614     trace("WorkingSetSize : %ld\n", pvi.WorkingSetSize);
615     todo_wine
616     {
617         ok( pvi.WorkingSetSize > 0, "Expected a WorkingSetSize > 0\n");
618     }
619 }
620
621 static void test_query_process_io(void)
622 {
623     DWORD status;
624     ULONG ReturnLength;
625     IO_COUNTERS pii;
626
627     /* NT4 doesn't support this information class, so check for it */
628     status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessIoCounters, &pii, sizeof(pii), &ReturnLength);
629     if (status == STATUS_NOT_SUPPORTED)
630     {
631         skip("ProcessIoCounters information class is not supported\n");
632         return;
633     }
634  
635     status = pNtQueryInformationProcess(NULL, ProcessIoCounters, NULL, sizeof(pii), NULL);
636     ok( status == STATUS_ACCESS_VIOLATION || status == STATUS_INVALID_HANDLE,
637         "Expected STATUS_ACCESS_VIOLATION or STATUS_INVALID_HANDLE(W2K3), got %08x\n", status);
638
639     status = pNtQueryInformationProcess(NULL, ProcessIoCounters, &pii, sizeof(pii), NULL);
640     ok( status == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got %08x\n", status);
641
642     status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessIoCounters, &pii, 24, &ReturnLength);
643     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
644
645     status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessIoCounters, &pii, sizeof(pii), &ReturnLength);
646     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
647     ok( sizeof(pii) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
648
649     status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessIoCounters, &pii, sizeof(pii) * 2, &ReturnLength);
650     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
651     ok( sizeof(pii) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
652
653     /* Check if we have some return values */
654     trace("OtherOperationCount : 0x%x%08x\n", (DWORD)(pii.OtherOperationCount >> 32), (DWORD)pii.OtherOperationCount);
655     todo_wine
656     {
657         ok( pii.OtherOperationCount > 0, "Expected an OtherOperationCount > 0\n");
658     }
659 }
660
661 static void test_query_process_times(void)
662 {
663     DWORD status;
664     ULONG ReturnLength;
665     HANDLE process;
666     SYSTEMTIME UTC, Local;
667     KERNEL_USER_TIMES spti;
668
669     status = pNtQueryInformationProcess(NULL, ProcessTimes, NULL, sizeof(spti), NULL);
670     ok( status == STATUS_ACCESS_VIOLATION || status == STATUS_INVALID_HANDLE,
671         "Expected STATUS_ACCESS_VIOLATION or STATUS_INVALID_HANDLE(W2K3), got %08x\n", status);
672
673     status = pNtQueryInformationProcess(NULL, ProcessTimes, &spti, sizeof(spti), NULL);
674     ok( status == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got %08x\n", status);
675
676     status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessTimes, &spti, 24, &ReturnLength);
677     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
678
679     process = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, one_before_last_pid);
680     if (!process)
681     {
682         trace("Could not open process with ID : %d, error : %u. Going to use current one.\n", one_before_last_pid, GetLastError());
683         process = GetCurrentProcess();
684         trace("ProcessTimes for current process\n");
685     }
686     else
687         trace("ProcessTimes for process with ID : %d\n", one_before_last_pid);
688
689     status = pNtQueryInformationProcess( process, ProcessTimes, &spti, sizeof(spti), &ReturnLength);
690     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
691     ok( sizeof(spti) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
692     CloseHandle(process);
693
694     FileTimeToSystemTime((const FILETIME *)&spti.CreateTime, &UTC);
695     SystemTimeToTzSpecificLocalTime(NULL, &UTC, &Local);
696     trace("CreateTime : %02d/%02d/%04d %02d:%02d:%02d\n", Local.wMonth, Local.wDay, Local.wYear,
697            Local.wHour, Local.wMinute, Local.wSecond);
698
699     FileTimeToSystemTime((const FILETIME *)&spti.ExitTime, &UTC);
700     SystemTimeToTzSpecificLocalTime(NULL, &UTC, &Local);
701     trace("ExitTime   : %02d/%02d/%04d %02d:%02d:%02d\n", Local.wMonth, Local.wDay, Local.wYear,
702            Local.wHour, Local.wMinute, Local.wSecond);
703
704     FileTimeToSystemTime((const FILETIME *)&spti.KernelTime, &Local);
705     trace("KernelTime : %02d:%02d:%02d.%03d\n", Local.wHour, Local.wMinute, Local.wSecond, Local.wMilliseconds);
706
707     FileTimeToSystemTime((const FILETIME *)&spti.UserTime, &Local);
708     trace("UserTime   : %02d:%02d:%02d.%03d\n", Local.wHour, Local.wMinute, Local.wSecond, Local.wMilliseconds);
709
710     status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessTimes, &spti, sizeof(spti) * 2, &ReturnLength);
711     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
712     ok( sizeof(spti) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
713 }
714
715 static void test_query_process_handlecount(void)
716 {
717     DWORD status;
718     ULONG ReturnLength;
719     DWORD handlecount;
720     HANDLE process;
721
722     status = pNtQueryInformationProcess(NULL, ProcessHandleCount, NULL, sizeof(handlecount), NULL);
723     ok( status == STATUS_ACCESS_VIOLATION || status == STATUS_INVALID_HANDLE,
724         "Expected STATUS_ACCESS_VIOLATION or STATUS_INVALID_HANDLE(W2K3), got %08x\n", status);
725
726     status = pNtQueryInformationProcess(NULL, ProcessHandleCount, &handlecount, sizeof(handlecount), NULL);
727     ok( status == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got %08x\n", status);
728
729     status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessHandleCount, &handlecount, 2, &ReturnLength);
730     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
731
732     process = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, one_before_last_pid);
733     if (!process)
734     {
735         trace("Could not open process with ID : %d, error : %u. Going to use current one.\n", one_before_last_pid, GetLastError());
736         process = GetCurrentProcess();
737         trace("ProcessHandleCount for current process\n");
738     }
739     else
740         trace("ProcessHandleCount for process with ID : %d\n", one_before_last_pid);
741
742     status = pNtQueryInformationProcess( process, ProcessHandleCount, &handlecount, sizeof(handlecount), &ReturnLength);
743     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
744     ok( sizeof(handlecount) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
745     CloseHandle(process);
746
747     status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessHandleCount, &handlecount, sizeof(handlecount) * 2, &ReturnLength);
748     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
749     ok( sizeof(handlecount) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
750
751     /* Check if we have some return values */
752     trace("HandleCount : %d\n", handlecount);
753     todo_wine
754     {
755         ok( handlecount > 0, "Expected some handles, got 0\n");
756     }
757 }
758
759
760 static void test_readvirtualmemory(void)
761 {
762     HANDLE process;
763     DWORD status;
764     SIZE_T readcount;
765     static const char teststring[] = "test string";
766     char buffer[12];
767
768     process = OpenProcess(PROCESS_VM_READ, FALSE, GetCurrentProcessId());
769     ok(process != 0, "Expected to be able to open own process for reading memory\n");
770
771     /* normal operation */
772     status = pNtReadVirtualMemory(process, teststring, buffer, 12, &readcount);
773     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
774     ok( readcount == 12, "Expected to read 12 bytes, got %ld\n",readcount);
775     ok( strcmp(teststring, buffer) == 0, "Expected read memory to be the same as original memory\n");
776
777     /* no number of bytes */
778     memset(buffer, 0, 12);
779     status = pNtReadVirtualMemory(process, teststring, buffer, 12, NULL);
780     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
781     ok( strcmp(teststring, buffer) == 0, "Expected read memory to be the same as original memory\n");
782
783     /* illegal remote address */
784     todo_wine{
785     status = pNtReadVirtualMemory(process, (void *) 0x1234, buffer, 12, &readcount);
786     ok( status == STATUS_PARTIAL_COPY, "Expected STATUS_PARTIAL_COPY, got %08x\n", status);
787     ok( readcount == 0, "Expected to read 0 bytes, got %ld\n",readcount);
788     }
789
790     /* 0 handle */
791     status = pNtReadVirtualMemory(0, teststring, buffer, 12, &readcount);
792     ok( status == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got %08x\n", status);
793     ok( readcount == 0, "Expected to read 0 bytes, got %ld\n",readcount);
794
795     /* pseudo handle for current process*/
796     memset(buffer, 0, 12);
797     status = pNtReadVirtualMemory((HANDLE)-1, teststring, buffer, 12, &readcount);
798     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
799     ok( readcount == 12, "Expected to read 12 bytes, got %ld\n",readcount);
800     ok( strcmp(teststring, buffer) == 0, "Expected read memory to be the same as original memory\n");
801
802     /* this test currently crashes wine with "wine client error:<process id>: read: Bad address"
803      * because the reply from wine server is directly read into the buffer and that fails with EFAULT
804      */
805     /* illegal local address */
806     /*status = pNtReadVirtualMemory(process, teststring, (void *)0x1234, 12, &readcount);
807     ok( status == STATUS_ACCESS_VIOLATION, "Expected STATUS_ACCESS_VIOLATION, got %08lx\n", status);
808     ok( readcount == 0, "Expected to read 0 bytes, got %ld\n",readcount);
809     */
810
811     CloseHandle(process);
812 }
813
814 START_TEST(info)
815 {
816     if(!InitFunctionPtrs())
817         return;
818
819     /* NtQuerySystemInformation */
820
821     /* 0x0 SystemBasicInformation */
822     trace("Starting test_query_basic()\n");
823     test_query_basic();
824
825     /* 0x1 SystemCpuInformation */
826     trace("Starting test_query_cpu()\n");
827     test_query_cpu();
828
829     /* 0x2 SystemPerformanceInformation */
830     trace("Starting test_query_performance()\n");
831     test_query_performance();
832
833     /* 0x3 SystemTimeOfDayInformation */
834     trace("Starting test_query_timeofday()\n");
835     test_query_timeofday();
836
837     /* 0x5 SystemProcessInformation */
838     trace("Starting test_query_process()\n");
839     test_query_process();
840
841     /* 0x8 SystemProcessorPerformanceInformation */
842     trace("Starting test_query_procperf()\n");
843     test_query_procperf();
844
845     /* 0xb SystemModuleInformation */
846     trace("Starting test_query_module()\n");
847     test_query_module();
848
849     /* 0x10 SystemHandleInformation */
850     trace("Starting test_query_handle()\n");
851     test_query_handle();
852
853     /* 0x15 SystemCacheInformation */
854     trace("Starting test_query_cache()\n");
855     test_query_cache();
856
857     /* 0x17 SystemInterruptInformation */
858     trace("Starting test_query_interrupt()\n");
859     test_query_interrupt();
860
861     /* 0x23 SystemKernelDebuggerInformation */
862     trace("Starting test_query_kerndebug()\n");
863     test_query_kerndebug();
864
865     /* 0x25 SystemRegistryQuotaInformation */
866     trace("Starting test_query_regquota()\n");
867     test_query_regquota();
868
869     /* NtQueryInformationProcess */
870
871     /* 0x0 ProcessBasicInformation */
872     trace("Starting test_query_process_basic()\n");
873     test_query_process_basic();
874
875     /* 0x2 ProcessIoCounters */
876     trace("Starting test_query_process_io()\n");
877     test_query_process_io();
878
879     /* 0x3 ProcessVmCounters */
880     trace("Starting test_query_process_vm()\n");
881     test_query_process_vm();
882
883     /* 0x4 ProcessTimes */
884     trace("Starting test_query_process_times()\n");
885     test_query_process_times();
886
887     /* 0x14 ProcessHandleCount */
888     trace("Starting test_query_process_handlecount()\n");
889     test_query_process_handlecount();
890
891     /* belongs into it's own file */
892     trace("Starting test_readvirtualmemory()\n");
893     test_readvirtualmemory();
894 }