comctl32: We can now store binary files in the repository.
[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     /* our implementation is a stub for now */
386     ok( ModuleCount > 0, "Expected some modules to be loaded\n");
387
388     /* Loop through all the modules/drivers, Wine doesn't get here (yet) */
389     for (i = 0; i < ModuleCount ; i++)
390     {
391         ok( i == sm->Id, "Id (%d) should have matched %u\n", sm->Id, i);
392         sm++;
393     }
394
395     HeapFree( GetProcessHeap(), 0, smi);
396 }
397
398 static void test_query_handle(void)
399 {
400     DWORD status;
401     ULONG ReturnLength;
402     ULONG SystemInformationLength = sizeof(SYSTEM_HANDLE_INFORMATION);
403     SYSTEM_HANDLE_INFORMATION* shi = HeapAlloc(GetProcessHeap(), 0, SystemInformationLength);
404
405     /* Request the needed length : a SystemInformationLength greater than one struct sets ReturnLength */
406     status = pNtQuerySystemInformation(SystemHandleInformation, shi, SystemInformationLength, &ReturnLength);
407
408     /* The following check assumes more than one handle on any given system */
409     todo_wine
410     {
411         ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
412     }
413     ok( ReturnLength > 0, "Expected ReturnLength to be > 0, it was %d\n", ReturnLength);
414
415     SystemInformationLength = ReturnLength;
416     shi = HeapReAlloc(GetProcessHeap(), 0, shi , SystemInformationLength);
417     status = pNtQuerySystemInformation(SystemHandleInformation, shi, SystemInformationLength, &ReturnLength);
418     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
419
420     /* Check if we have some return values */
421     trace("Number of Handles : %d\n", shi->Count);
422     todo_wine
423     {
424         /* our implementation is a stub for now */
425         ok( shi->Count > 1, "Expected more than 1 handles, got (%d)\n", shi->Count);
426     }
427
428     HeapFree( GetProcessHeap(), 0, shi);
429 }
430
431 static void test_query_cache(void)
432 {
433     DWORD status;
434     ULONG ReturnLength;
435     SYSTEM_CACHE_INFORMATION sci;
436
437     status = pNtQuerySystemInformation(SystemCacheInformation, &sci, 0, &ReturnLength);
438     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
439
440     status = pNtQuerySystemInformation(SystemCacheInformation, &sci, sizeof(sci), &ReturnLength);
441     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
442     ok( sizeof(sci) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
443
444     status = pNtQuerySystemInformation(SystemCacheInformation, &sci, sizeof(sci) + 2, &ReturnLength);
445     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
446     ok( sizeof(sci) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
447 }
448
449 static void test_query_interrupt(void)
450 {
451     DWORD status;
452     ULONG ReturnLength;
453     ULONG NeededLength;
454     SYSTEM_BASIC_INFORMATION sbi;
455     SYSTEM_INTERRUPT_INFORMATION* sii;
456
457     /* Find out the number of processors */
458     status = pNtQuerySystemInformation(SystemBasicInformation, &sbi, sizeof(sbi), &ReturnLength);
459     NeededLength = sbi.NumberOfProcessors * sizeof(SYSTEM_INTERRUPT_INFORMATION);
460
461     sii = HeapAlloc(GetProcessHeap(), 0, NeededLength);
462
463     status = pNtQuerySystemInformation(SystemInterruptInformation, sii, 0, &ReturnLength);
464     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
465
466     /* Try it for all processors */
467     status = pNtQuerySystemInformation(SystemInterruptInformation, sii, NeededLength, &ReturnLength);
468     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
469
470     /* Windows XP and W2K3 (and others?) always return 0 for the ReturnLength
471      * No test added for this as it's highly unlikely that an app depends on this
472     */
473
474     HeapFree( GetProcessHeap(), 0, sii);
475 }
476
477 static void test_query_kerndebug(void)
478 {
479     DWORD status;
480     ULONG ReturnLength;
481     SYSTEM_KERNEL_DEBUGGER_INFORMATION skdi;
482
483     status = pNtQuerySystemInformation(SystemKernelDebuggerInformation, &skdi, 0, &ReturnLength);
484     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
485
486     status = pNtQuerySystemInformation(SystemKernelDebuggerInformation, &skdi, sizeof(skdi), &ReturnLength);
487     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
488     ok( sizeof(skdi) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
489
490     status = pNtQuerySystemInformation(SystemKernelDebuggerInformation, &skdi, sizeof(skdi) + 2, &ReturnLength);
491     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
492     ok( sizeof(skdi) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
493 }
494
495 static void test_query_regquota(void)
496 {
497     DWORD status;
498     ULONG ReturnLength;
499     SYSTEM_REGISTRY_QUOTA_INFORMATION srqi;
500
501     status = pNtQuerySystemInformation(SystemRegistryQuotaInformation, &srqi, 0, &ReturnLength);
502     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
503
504     status = pNtQuerySystemInformation(SystemRegistryQuotaInformation, &srqi, sizeof(srqi), &ReturnLength);
505     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
506     ok( sizeof(srqi) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
507
508     status = pNtQuerySystemInformation(SystemRegistryQuotaInformation, &srqi, sizeof(srqi) + 2, &ReturnLength);
509     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
510     ok( sizeof(srqi) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
511 }
512
513 static void test_query_process_basic(void)
514 {
515     DWORD status;
516     ULONG ReturnLength;
517
518     typedef struct _PROCESS_BASIC_INFORMATION_PRIVATE {
519         DWORD ExitStatus;
520         DWORD PebBaseAddress;
521         DWORD AffinityMask;
522         DWORD BasePriority;
523         ULONG UniqueProcessId;
524         ULONG InheritedFromUniqueProcessId;
525     } PROCESS_BASIC_INFORMATION_PRIVATE, *PPROCESS_BASIC_INFORMATION_PRIVATE;
526
527     PROCESS_BASIC_INFORMATION_PRIVATE pbi;
528
529     /* This test also covers some basic parameter testing that should be the same for
530      * every information class
531     */
532
533     /* Use a nonexistent info class */
534     trace("Check nonexistent info class\n");
535     status = pNtQueryInformationProcess(NULL, -1, NULL, 0, NULL);
536     ok( status == STATUS_INVALID_INFO_CLASS, "Expected STATUS_INVALID_INFO_CLASS, got %08x\n", status);
537
538     /* Do not give a handle and buffer */
539     trace("Check NULL handle and buffer and zero-length buffersize\n");
540     status = pNtQueryInformationProcess(NULL, ProcessBasicInformation, NULL, 0, NULL);
541     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
542
543     /* Use a correct info class and buffer size, but still no handle and buffer */
544     trace("Check NULL handle and buffer\n");
545     status = pNtQueryInformationProcess(NULL, ProcessBasicInformation, NULL, sizeof(pbi), NULL);
546     ok( status == STATUS_ACCESS_VIOLATION || status == STATUS_INVALID_HANDLE,
547         "Expected STATUS_ACCESS_VIOLATION or STATUS_INVALID_HANDLE(W2K3), got %08x\n", status);
548
549     /* Use a correct info class and buffer size, but still no handle */
550     trace("Check NULL handle\n");
551     status = pNtQueryInformationProcess(NULL, ProcessBasicInformation, &pbi, sizeof(pbi), NULL);
552     ok( status == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got %08x\n", status);
553
554     /* Use a greater buffer size */
555     trace("Check NULL handle and too large buffersize\n");
556     status = pNtQueryInformationProcess(NULL, ProcessBasicInformation, &pbi, sizeof(pbi) * 2, NULL);
557     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
558
559     /* Use no ReturnLength */
560     trace("Check NULL ReturnLength\n");
561     status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessBasicInformation, &pbi, sizeof(pbi), NULL);
562     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
563
564     /* Finally some correct calls */
565     trace("Check with correct parameters\n");
566     status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessBasicInformation, &pbi, sizeof(pbi), &ReturnLength);
567     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
568     ok( sizeof(pbi) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
569
570     /* Everything is correct except a too large buffersize */
571     trace("Too large buffersize\n");
572     status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessBasicInformation, &pbi, sizeof(pbi) * 2, &ReturnLength);
573     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
574     ok( sizeof(pbi) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
575                                                                                                                                                
576     /* Check if we have some return values */
577     trace("ProcessID : %d\n", pbi.UniqueProcessId);
578     ok( pbi.UniqueProcessId > 0, "Expected a ProcessID > 0, got 0\n");
579 }
580
581 static void test_query_process_vm(void)
582 {
583     DWORD status;
584     ULONG ReturnLength;
585     VM_COUNTERS pvi;
586
587     status = pNtQueryInformationProcess(NULL, ProcessVmCounters, NULL, sizeof(pvi), NULL);
588     ok( status == STATUS_ACCESS_VIOLATION || status == STATUS_INVALID_HANDLE,
589         "Expected STATUS_ACCESS_VIOLATION or STATUS_INVALID_HANDLE(W2K3), got %08x\n", status);
590
591     status = pNtQueryInformationProcess(NULL, ProcessVmCounters, &pvi, sizeof(pvi), NULL);
592     ok( status == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got %08x\n", status);
593
594     /* Windows XP and W2K3 will report success for a size of 44 AND 48 !
595        Windows W2K will only report success for 44.
596        For now we only care for 44, which is sizeof(VM_COUNTERS)
597        If an app depends on it, we have to implement this in ntdll/process.c
598     */
599
600     status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessVmCounters, &pvi, 24, &ReturnLength);
601     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
602
603     status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessVmCounters, &pvi, sizeof(pvi), &ReturnLength);
604     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
605     ok( sizeof(pvi) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
606
607     status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessVmCounters, &pvi, 46, &ReturnLength);
608     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
609     ok( sizeof(pvi) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
610
611     /* Check if we have some return values */
612     trace("WorkingSetSize : %ld\n", pvi.WorkingSetSize);
613     todo_wine
614     {
615         ok( pvi.WorkingSetSize > 0, "Expected a WorkingSetSize > 0\n");
616     }
617 }
618
619 static void test_query_process_io(void)
620 {
621     DWORD status;
622     ULONG ReturnLength;
623     IO_COUNTERS pii;
624
625     /* NT4 doesn't support this information class, so check for it */
626     status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessIoCounters, &pii, sizeof(pii), &ReturnLength);
627     if (status == STATUS_NOT_SUPPORTED)
628     {
629         skip("ProcessIoCounters information class is not supported\n");
630         return;
631     }
632  
633     status = pNtQueryInformationProcess(NULL, ProcessIoCounters, NULL, sizeof(pii), NULL);
634     ok( status == STATUS_ACCESS_VIOLATION || status == STATUS_INVALID_HANDLE,
635         "Expected STATUS_ACCESS_VIOLATION or STATUS_INVALID_HANDLE(W2K3), got %08x\n", status);
636
637     status = pNtQueryInformationProcess(NULL, ProcessIoCounters, &pii, sizeof(pii), NULL);
638     ok( status == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got %08x\n", status);
639
640     status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessIoCounters, &pii, 24, &ReturnLength);
641     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
642
643     status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessIoCounters, &pii, sizeof(pii), &ReturnLength);
644     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
645     ok( sizeof(pii) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
646
647     status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessIoCounters, &pii, sizeof(pii) * 2, &ReturnLength);
648     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
649     ok( sizeof(pii) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
650
651     /* Check if we have some return values */
652     trace("OtherOperationCount : 0x%x%08x\n", (DWORD)(pii.OtherOperationCount >> 32), (DWORD)pii.OtherOperationCount);
653     todo_wine
654     {
655         ok( pii.OtherOperationCount > 0, "Expected an OtherOperationCount > 0\n");
656     }
657 }
658
659 static void test_query_process_times(void)
660 {
661     DWORD status;
662     ULONG ReturnLength;
663     HANDLE process;
664     SYSTEMTIME UTC, Local;
665     KERNEL_USER_TIMES spti;
666
667     status = pNtQueryInformationProcess(NULL, ProcessTimes, NULL, sizeof(spti), NULL);
668     ok( status == STATUS_ACCESS_VIOLATION || status == STATUS_INVALID_HANDLE,
669         "Expected STATUS_ACCESS_VIOLATION or STATUS_INVALID_HANDLE(W2K3), got %08x\n", status);
670
671     status = pNtQueryInformationProcess(NULL, ProcessTimes, &spti, sizeof(spti), NULL);
672     ok( status == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got %08x\n", status);
673
674     status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessTimes, &spti, 24, &ReturnLength);
675     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
676
677     process = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, one_before_last_pid);
678     if (!process)
679     {
680         trace("Could not open process with ID : %d, error : %u. Going to use current one.\n", one_before_last_pid, GetLastError());
681         process = GetCurrentProcess();
682         trace("ProcessTimes for current process\n");
683     }
684     else
685         trace("ProcessTimes for process with ID : %d\n", one_before_last_pid);
686
687     status = pNtQueryInformationProcess( process, ProcessTimes, &spti, sizeof(spti), &ReturnLength);
688     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
689     ok( sizeof(spti) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
690     CloseHandle(process);
691
692     FileTimeToSystemTime((const FILETIME *)&spti.CreateTime, &UTC);
693     SystemTimeToTzSpecificLocalTime(NULL, &UTC, &Local);
694     trace("CreateTime : %02d/%02d/%04d %02d:%02d:%02d\n", Local.wMonth, Local.wDay, Local.wYear,
695            Local.wHour, Local.wMinute, Local.wSecond);
696
697     FileTimeToSystemTime((const FILETIME *)&spti.ExitTime, &UTC);
698     SystemTimeToTzSpecificLocalTime(NULL, &UTC, &Local);
699     trace("ExitTime   : %02d/%02d/%04d %02d:%02d:%02d\n", Local.wMonth, Local.wDay, Local.wYear,
700            Local.wHour, Local.wMinute, Local.wSecond);
701
702     FileTimeToSystemTime((const FILETIME *)&spti.KernelTime, &Local);
703     trace("KernelTime : %02d:%02d:%02d.%03d\n", Local.wHour, Local.wMinute, Local.wSecond, Local.wMilliseconds);
704
705     FileTimeToSystemTime((const FILETIME *)&spti.UserTime, &Local);
706     trace("UserTime   : %02d:%02d:%02d.%03d\n", Local.wHour, Local.wMinute, Local.wSecond, Local.wMilliseconds);
707
708     status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessTimes, &spti, sizeof(spti) * 2, &ReturnLength);
709     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
710     ok( sizeof(spti) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
711 }
712
713 static void test_query_process_handlecount(void)
714 {
715     DWORD status;
716     ULONG ReturnLength;
717     DWORD handlecount;
718     HANDLE process;
719
720     status = pNtQueryInformationProcess(NULL, ProcessHandleCount, NULL, sizeof(handlecount), NULL);
721     ok( status == STATUS_ACCESS_VIOLATION || status == STATUS_INVALID_HANDLE,
722         "Expected STATUS_ACCESS_VIOLATION or STATUS_INVALID_HANDLE(W2K3), got %08x\n", status);
723
724     status = pNtQueryInformationProcess(NULL, ProcessHandleCount, &handlecount, sizeof(handlecount), NULL);
725     ok( status == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got %08x\n", status);
726
727     status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessHandleCount, &handlecount, 2, &ReturnLength);
728     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
729
730     process = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, one_before_last_pid);
731     if (!process)
732     {
733         trace("Could not open process with ID : %d, error : %u. Going to use current one.\n", one_before_last_pid, GetLastError());
734         process = GetCurrentProcess();
735         trace("ProcessHandleCount for current process\n");
736     }
737     else
738         trace("ProcessHandleCount for process with ID : %d\n", one_before_last_pid);
739
740     status = pNtQueryInformationProcess( process, ProcessHandleCount, &handlecount, sizeof(handlecount), &ReturnLength);
741     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
742     ok( sizeof(handlecount) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
743     CloseHandle(process);
744
745     status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessHandleCount, &handlecount, sizeof(handlecount) * 2, &ReturnLength);
746     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
747     ok( sizeof(handlecount) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
748
749     /* Check if we have some return values */
750     trace("HandleCount : %d\n", handlecount);
751     todo_wine
752     {
753         ok( handlecount > 0, "Expected some handles, got 0\n");
754     }
755 }
756
757
758 static void test_readvirtualmemory(void)
759 {
760     HANDLE process;
761     DWORD status;
762     SIZE_T readcount;
763     static const char teststring[] = "test string";
764     char buffer[12];
765
766     process = OpenProcess(PROCESS_VM_READ, FALSE, GetCurrentProcessId());
767     ok(process != 0, "Expected to be able to open own process for reading memory\n");
768
769     /* normal operation */
770     status = pNtReadVirtualMemory(process, teststring, buffer, 12, &readcount);
771     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
772     ok( readcount == 12, "Expected to read 12 bytes, got %ld\n",readcount);
773     ok( strcmp(teststring, buffer) == 0, "Expected read memory to be the same as original memory\n");
774
775     /* no number of bytes */
776     memset(buffer, 0, 12);
777     status = pNtReadVirtualMemory(process, teststring, buffer, 12, NULL);
778     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
779     ok( strcmp(teststring, buffer) == 0, "Expected read memory to be the same as original memory\n");
780
781     /* illegal remote address */
782     todo_wine{
783     status = pNtReadVirtualMemory(process, (void *) 0x1234, buffer, 12, &readcount);
784     ok( status == STATUS_PARTIAL_COPY, "Expected STATUS_PARTIAL_COPY, got %08x\n", status);
785     ok( readcount == 0, "Expected to read 0 bytes, got %ld\n",readcount);
786     }
787
788     /* 0 handle */
789     status = pNtReadVirtualMemory(0, teststring, buffer, 12, &readcount);
790     ok( status == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got %08x\n", status);
791     ok( readcount == 0, "Expected to read 0 bytes, got %ld\n",readcount);
792
793     /* pseudo handle for current process*/
794     memset(buffer, 0, 12);
795     status = pNtReadVirtualMemory((HANDLE)-1, teststring, buffer, 12, &readcount);
796     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
797     ok( readcount == 12, "Expected to read 12 bytes, got %ld\n",readcount);
798     ok( strcmp(teststring, buffer) == 0, "Expected read memory to be the same as original memory\n");
799
800     /* this test currently crashes wine with "wine client error:<process id>: read: Bad address"
801      * because the reply from wine server is directly read into the buffer and that fails with EFAULT
802      */
803     /* illegal local address */
804     /*status = pNtReadVirtualMemory(process, teststring, (void *)0x1234, 12, &readcount);
805     ok( status == STATUS_ACCESS_VIOLATION, "Expected STATUS_ACCESS_VIOLATION, got %08lx\n", status);
806     ok( readcount == 0, "Expected to read 0 bytes, got %ld\n",readcount);
807     */
808
809     CloseHandle(process);
810 }
811
812 START_TEST(info)
813 {
814     if(!InitFunctionPtrs())
815         return;
816
817     /* NtQuerySystemInformation */
818
819     /* 0x0 SystemBasicInformation */
820     trace("Starting test_query_basic()\n");
821     test_query_basic();
822
823     /* 0x1 SystemCpuInformation */
824     trace("Starting test_query_cpu()\n");
825     test_query_cpu();
826
827     /* 0x2 SystemPerformanceInformation */
828     trace("Starting test_query_performance()\n");
829     test_query_performance();
830
831     /* 0x3 SystemTimeOfDayInformation */
832     trace("Starting test_query_timeofday()\n");
833     test_query_timeofday();
834
835     /* 0x5 SystemProcessInformation */
836     trace("Starting test_query_process()\n");
837     test_query_process();
838
839     /* 0x8 SystemProcessorPerformanceInformation */
840     trace("Starting test_query_procperf()\n");
841     test_query_procperf();
842
843     /* 0xb SystemModuleInformation */
844     trace("Starting test_query_module()\n");
845     test_query_module();
846
847     /* 0x10 SystemHandleInformation */
848     trace("Starting test_query_handle()\n");
849     test_query_handle();
850
851     /* 0x15 SystemCacheInformation */
852     trace("Starting test_query_cache()\n");
853     test_query_cache();
854
855     /* 0x17 SystemInterruptInformation */
856     trace("Starting test_query_interrupt()\n");
857     test_query_interrupt();
858
859     /* 0x23 SystemKernelDebuggerInformation */
860     trace("Starting test_query_kerndebug()\n");
861     test_query_kerndebug();
862
863     /* 0x25 SystemRegistryQuotaInformation */
864     trace("Starting test_query_regquota()\n");
865     test_query_regquota();
866
867     /* NtQueryInformationProcess */
868
869     /* 0x0 ProcessBasicInformation */
870     trace("Starting test_query_process_basic()\n");
871     test_query_process_basic();
872
873     /* 0x2 ProcessIoCounters */
874     trace("Starting test_query_process_io()\n");
875     test_query_process_io();
876
877     /* 0x3 ProcessVmCounters */
878     trace("Starting test_query_process_vm()\n");
879     test_query_process_vm();
880
881     /* 0x4 ProcessTimes */
882     trace("Starting test_query_process_times()\n");
883     test_query_process_times();
884
885     /* 0x14 ProcessHandleCount */
886     trace("Starting test_query_process_handlecount()\n");
887     test_query_process_handlecount();
888
889     /* belongs into it's own file */
890     trace("Starting test_readvirtualmemory()\n");
891     test_readvirtualmemory();
892 }