hlink/tests: Add a trailing '\n' to an ok() call.
[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 #include <winnls.h>
23 #include <stdio.h>
24
25 static NTSTATUS (WINAPI * pNtQuerySystemInformation)(SYSTEM_INFORMATION_CLASS, PVOID, ULONG, PULONG);
26 static NTSTATUS (WINAPI * pNtQueryInformationProcess)(HANDLE, PROCESSINFOCLASS, PVOID, ULONG, PULONG);
27 static NTSTATUS (WINAPI * pNtQueryInformationThread)(HANDLE, THREADINFOCLASS, PVOID, ULONG, PULONG);
28 static NTSTATUS (WINAPI * pNtSetInformationProcess)(HANDLE, PROCESSINFOCLASS, PVOID, ULONG);
29 static NTSTATUS (WINAPI * pNtSetInformationThread)(HANDLE, THREADINFOCLASS, PVOID, ULONG);
30 static NTSTATUS (WINAPI * pNtReadVirtualMemory)(HANDLE, const void*, void*, SIZE_T, SIZE_T*);
31
32 /* one_before_last_pid is used to be able to compare values of a still running process
33    with the output of the test_query_process_times and test_query_process_handlecount tests.
34 */
35 static DWORD one_before_last_pid = 0;
36
37 #define NTDLL_GET_PROC(func) do {                     \
38     p ## func = (void*)GetProcAddress(hntdll, #func); \
39     if(!p ## func) { \
40       trace("GetProcAddress(%s) failed\n", #func); \
41       return FALSE; \
42     } \
43   } while(0)
44
45 static BOOL InitFunctionPtrs(void)
46 {
47     /* All needed functions are NT based, so using GetModuleHandle is a good check */
48     HMODULE hntdll = GetModuleHandle("ntdll");
49     if (!hntdll)
50     {
51         win_skip("Not running on NT\n");
52         return FALSE;
53     }
54
55     NTDLL_GET_PROC(NtQuerySystemInformation);
56     NTDLL_GET_PROC(NtQueryInformationProcess);
57     NTDLL_GET_PROC(NtQueryInformationThread);
58     NTDLL_GET_PROC(NtSetInformationProcess);
59     NTDLL_GET_PROC(NtSetInformationThread);
60     NTDLL_GET_PROC(NtReadVirtualMemory);
61
62     return TRUE;
63 }
64
65 static void test_query_basic(void)
66 {
67     NTSTATUS status;
68     ULONG ReturnLength;
69     SYSTEM_BASIC_INFORMATION sbi;
70
71     /* This test also covers some basic parameter testing that should be the same for 
72      * every information class
73     */
74
75     /* Use a nonexistent info class */
76     trace("Check nonexistent info class\n");
77     status = pNtQuerySystemInformation(-1, NULL, 0, NULL);
78     ok( status == STATUS_INVALID_INFO_CLASS || status == STATUS_NOT_IMPLEMENTED /* vista */,
79         "Expected STATUS_INVALID_INFO_CLASS or STATUS_NOT_IMPLEMENTED, got %08x\n", status);
80
81     /* Use an existing class but with a zero-length buffer */
82     trace("Check zero-length buffer\n");
83     status = pNtQuerySystemInformation(SystemBasicInformation, NULL, 0, NULL);
84     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
85
86     /* Use an existing class, correct length but no SystemInformation buffer */
87     trace("Check no SystemInformation buffer\n");
88     status = pNtQuerySystemInformation(SystemBasicInformation, NULL, sizeof(sbi), NULL);
89     ok( status == STATUS_ACCESS_VIOLATION || status == STATUS_INVALID_PARAMETER /* vista */,
90         "Expected STATUS_ACCESS_VIOLATION or STATUS_INVALID_PARAMETER, got %08x\n", status);
91
92     /* Use a existing class, correct length, a pointer to a buffer but no ReturnLength pointer */
93     trace("Check no ReturnLength pointer\n");
94     status = pNtQuerySystemInformation(SystemBasicInformation, &sbi, sizeof(sbi), NULL);
95     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
96
97     /* Check a too large buffer size */
98     trace("Check a too large buffer size\n");
99     status = pNtQuerySystemInformation(SystemBasicInformation, &sbi, sizeof(sbi) * 2, &ReturnLength);
100     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
101
102     /* Finally some correct calls */
103     trace("Check with correct parameters\n");
104     status = pNtQuerySystemInformation(SystemBasicInformation, &sbi, sizeof(sbi), &ReturnLength);
105     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
106     ok( sizeof(sbi) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
107
108     /* Check if we have some return values */
109     trace("Number of Processors : %d\n", sbi.NumberOfProcessors);
110     ok( sbi.NumberOfProcessors > 0, "Expected more than 0 processors, got %d\n", sbi.NumberOfProcessors);
111 }
112
113 static void test_query_cpu(void)
114 {
115     DWORD status;
116     ULONG ReturnLength;
117     SYSTEM_CPU_INFORMATION sci;
118
119     status = pNtQuerySystemInformation(SystemCpuInformation, &sci, sizeof(sci), &ReturnLength);
120     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
121     ok( sizeof(sci) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
122
123     /* Check if we have some return values */
124     trace("Processor FeatureSet : %08x\n", sci.FeatureSet);
125     ok( sci.FeatureSet != 0, "Expected some features for this processor, got %08x\n", sci.FeatureSet);
126 }
127
128 static void test_query_performance(void)
129 {
130     NTSTATUS status;
131     ULONG ReturnLength;
132     ULONGLONG buffer[sizeof(SYSTEM_PERFORMANCE_INFORMATION)/sizeof(ULONGLONG) + 1];
133
134     status = pNtQuerySystemInformation(SystemPerformanceInformation, buffer, 0, &ReturnLength);
135     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
136
137     status = pNtQuerySystemInformation(SystemPerformanceInformation, buffer,
138                                        sizeof(SYSTEM_PERFORMANCE_INFORMATION), &ReturnLength);
139     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
140     ok( ReturnLength == sizeof(SYSTEM_PERFORMANCE_INFORMATION), "Inconsistent length %d\n", ReturnLength);
141
142     status = pNtQuerySystemInformation(SystemPerformanceInformation, buffer,
143                                        sizeof(SYSTEM_PERFORMANCE_INFORMATION) + 2, &ReturnLength);
144     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
145     ok( ReturnLength == sizeof(SYSTEM_PERFORMANCE_INFORMATION) ||
146         ReturnLength == sizeof(SYSTEM_PERFORMANCE_INFORMATION) + 2,
147         "Inconsistent length %d\n", ReturnLength);
148
149     /* Not return values yet, as struct members are unknown */
150 }
151
152 static void test_query_timeofday(void)
153 {
154     NTSTATUS status;
155     ULONG ReturnLength;
156
157     /* Copy of our winternl.h structure turned into a private one */
158     typedef struct _SYSTEM_TIMEOFDAY_INFORMATION_PRIVATE {
159         LARGE_INTEGER liKeBootTime;
160         LARGE_INTEGER liKeSystemTime;
161         LARGE_INTEGER liExpTimeZoneBias;
162         ULONG uCurrentTimeZoneId;
163         DWORD dwUnknown1[5];
164     } SYSTEM_TIMEOFDAY_INFORMATION_PRIVATE, *PSYSTEM_TIMEOFDAY_INFORMATION_PRIVATE;
165
166     SYSTEM_TIMEOFDAY_INFORMATION_PRIVATE sti;
167   
168     /*  The struct size for NT (32 bytes) and Win2K/XP (48 bytes) differ.
169      *
170      *  Windows 2000 and XP return STATUS_INFO_LENGTH_MISMATCH if the given buffer size is greater
171      *  then 48 and 0 otherwise
172      *  Windows NT returns STATUS_INFO_LENGTH_MISMATCH when the given buffer size is not correct
173      *  and 0 otherwise
174      *
175      *  Windows 2000 and XP copy the given buffer size into the provided buffer, if the return code is STATUS_SUCCESS
176      *  NT only fills the buffer if the return code is STATUS_SUCCESS
177      *
178     */
179
180     status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, sizeof(sti), &ReturnLength);
181
182     if (status == STATUS_INFO_LENGTH_MISMATCH)
183     {
184         trace("Windows version is NT, we have to cater for differences with W2K/WinXP\n");
185  
186         status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, 0, &ReturnLength);
187         ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, 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, 28, &ReturnLength);
192         ok( 0xdeadbeef == sti.uCurrentTimeZoneId, "This part of the buffer should not have been filled\n");
193
194         status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, 32, &ReturnLength);
195         ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
196         ok( 32 == ReturnLength, "ReturnLength should be 0, it is (%d)\n", ReturnLength);
197     }
198     else
199     {
200         status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, 0, &ReturnLength);
201         ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
202         ok( 0 == ReturnLength, "ReturnLength should be 0, it is (%d)\n", ReturnLength);
203
204         sti.uCurrentTimeZoneId = 0xdeadbeef;
205         status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, 24, &ReturnLength);
206         ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
207         ok( 24 == ReturnLength, "ReturnLength should be 24, it is (%d)\n", ReturnLength);
208         ok( 0xdeadbeef == sti.uCurrentTimeZoneId, "This part of the buffer should not have been filled\n");
209     
210         sti.uCurrentTimeZoneId = 0xdeadbeef;
211         status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, 32, &ReturnLength);
212         ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
213         ok( 32 == ReturnLength, "ReturnLength should be 32, it is (%d)\n", ReturnLength);
214         ok( 0xdeadbeef != sti.uCurrentTimeZoneId, "Buffer should have been partially filled\n");
215     
216         status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, 49, &ReturnLength);
217         ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
218         ok( ReturnLength == 0 || ReturnLength == sizeof(sti) /* vista */,
219             "ReturnLength should be 0, it is (%d)\n", ReturnLength);
220     
221         status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, sizeof(sti), &ReturnLength);
222         ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
223         ok( sizeof(sti) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
224     }
225
226     /* Check if we have some return values */
227     trace("uCurrentTimeZoneId : (%d)\n", sti.uCurrentTimeZoneId);
228 }
229
230 static void test_query_process(void)
231 {
232     NTSTATUS status;
233     DWORD last_pid;
234     ULONG ReturnLength;
235     int i = 0, k = 0;
236     int is_nt = 0;
237     SYSTEM_BASIC_INFORMATION sbi;
238
239     /* Copy of our winternl.h structure turned into a private one */
240     typedef struct _SYSTEM_PROCESS_INFORMATION_PRIVATE {
241         ULONG NextEntryOffset;
242         DWORD dwThreadCount;
243         DWORD dwUnknown1[6];
244         FILETIME ftCreationTime;
245         FILETIME ftUserTime;
246         FILETIME ftKernelTime;
247         UNICODE_STRING ProcessName;
248         DWORD dwBasePriority;
249         HANDLE UniqueProcessId;
250         HANDLE ParentProcessId;
251         ULONG HandleCount;
252         DWORD dwUnknown3;
253         DWORD dwUnknown4;
254         VM_COUNTERS vmCounters;
255         IO_COUNTERS ioCounters;
256         SYSTEM_THREAD_INFORMATION ti[1];
257     } SYSTEM_PROCESS_INFORMATION_PRIVATE, *PSYSTEM_PROCESS_INFORMATION_PRIVATE;
258
259     ULONG SystemInformationLength = sizeof(SYSTEM_PROCESS_INFORMATION_PRIVATE);
260     SYSTEM_PROCESS_INFORMATION_PRIVATE *spi, *spi_buf = HeapAlloc(GetProcessHeap(), 0, SystemInformationLength);
261
262     /* Only W2K3 returns the needed length, the rest returns 0, so we have to loop */
263
264     for (;;)
265     {
266         status = pNtQuerySystemInformation(SystemProcessInformation, spi_buf, SystemInformationLength, &ReturnLength);
267
268         if (status != STATUS_INFO_LENGTH_MISMATCH) break;
269         
270         spi_buf = HeapReAlloc(GetProcessHeap(), 0, spi_buf , SystemInformationLength *= 2);
271     }
272     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
273     spi = spi_buf;
274
275     /* Get the first NextEntryOffset, from this we can deduce the OS version we're running
276      *
277      * W2K/WinXP/W2K3:
278      *   NextEntryOffset for a process is 184 + (no. of threads) * sizeof(SYSTEM_THREAD_INFORMATION)
279      * NT:
280      *   NextEntryOffset for a process is 136 + (no. of threads) * sizeof(SYSTEM_THREAD_INFORMATION)
281      * Wine (with every windows version):
282      *   NextEntryOffset for a process is 0 if just this test is running
283      *   NextEntryOffset for a process is 184 + (no. of threads) * sizeof(SYSTEM_THREAD_INFORMATION) +
284      *                             ProcessName.MaximumLength
285      *     if more wine processes are running
286      *
287      * Note : On windows the first process is in fact the Idle 'process' with a thread for every processor
288     */
289
290     pNtQuerySystemInformation(SystemBasicInformation, &sbi, sizeof(sbi), &ReturnLength);
291
292     is_nt = ( spi->NextEntryOffset - (sbi.NumberOfProcessors * sizeof(SYSTEM_THREAD_INFORMATION)) == 136);
293
294     if (is_nt) win_skip("Windows version is NT, we will skip thread tests\n");
295
296     /* Check if we have some return values
297      * 
298      * On windows there will be several processes running (Including the always present Idle and System)
299      * On wine we only have one (if this test is the only wine process running)
300     */
301     
302     /* Loop through the processes */
303
304     for (;;)
305     {
306         i++;
307
308         last_pid = (DWORD_PTR)spi->UniqueProcessId;
309
310         ok( spi->dwThreadCount > 0, "Expected some threads for this process, got 0\n");
311
312         /* Loop through the threads, skip NT4 for now */
313         
314         if (!is_nt)
315         {
316             DWORD j;
317             for ( j = 0; j < spi->dwThreadCount; j++) 
318             {
319                 k++;
320                 ok ( spi->ti[j].ClientId.UniqueProcess == spi->UniqueProcessId,
321                      "The owning pid of the thread (%p) doesn't equal the pid (%p) of the process\n",
322                      spi->ti[j].ClientId.UniqueProcess, spi->UniqueProcessId);
323             }
324         }
325
326         if (!spi->NextEntryOffset) break;
327
328         one_before_last_pid = last_pid;
329
330         spi = (SYSTEM_PROCESS_INFORMATION_PRIVATE*)((char*)spi + spi->NextEntryOffset);
331     }
332     trace("Total number of running processes : %d\n", i);
333     if (!is_nt) trace("Total number of running threads   : %d\n", k);
334
335     if (one_before_last_pid == 0) one_before_last_pid = last_pid;
336
337     HeapFree( GetProcessHeap(), 0, spi_buf);
338 }
339
340 static void test_query_procperf(void)
341 {
342     NTSTATUS status;
343     ULONG ReturnLength;
344     ULONG NeededLength;
345     SYSTEM_BASIC_INFORMATION sbi;
346     SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION* sppi;
347
348     /* Find out the number of processors */
349     status = pNtQuerySystemInformation(SystemBasicInformation, &sbi, sizeof(sbi), &ReturnLength);
350     NeededLength = sbi.NumberOfProcessors * sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION);
351
352     sppi = HeapAlloc(GetProcessHeap(), 0, NeededLength);
353
354     status = pNtQuerySystemInformation(SystemProcessorPerformanceInformation, sppi, 0, &ReturnLength);
355     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
356
357     /* Try it for 1 processor */
358     sppi->KernelTime.QuadPart = 0xdeaddead;
359     sppi->UserTime.QuadPart = 0xdeaddead;
360     sppi->IdleTime.QuadPart = 0xdeaddead;
361     status = pNtQuerySystemInformation(SystemProcessorPerformanceInformation, sppi,
362                                        sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION), &ReturnLength);
363     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
364     ok( sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) == ReturnLength,
365         "Inconsistent length %d\n", ReturnLength);
366     ok (sppi->KernelTime.QuadPart != 0xdeaddead, "KernelTime unchanged\n");
367     ok (sppi->UserTime.QuadPart != 0xdeaddead, "UserTime unchanged\n");
368     ok (sppi->IdleTime.QuadPart != 0xdeaddead, "IdleTime unchanged\n");
369
370     /* Try it for all processors */
371     sppi->KernelTime.QuadPart = 0xdeaddead;
372     sppi->UserTime.QuadPart = 0xdeaddead;
373     sppi->IdleTime.QuadPart = 0xdeaddead;
374     status = pNtQuerySystemInformation(SystemProcessorPerformanceInformation, sppi, NeededLength, &ReturnLength);
375     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
376     ok( NeededLength == ReturnLength, "Inconsistent length (%d) <-> (%d)\n", NeededLength, ReturnLength);
377     ok (sppi->KernelTime.QuadPart != 0xdeaddead, "KernelTime unchanged\n");
378     ok (sppi->UserTime.QuadPart != 0xdeaddead, "UserTime unchanged\n");
379     ok (sppi->IdleTime.QuadPart != 0xdeaddead, "IdleTime unchanged\n");
380
381     /* A too large given buffer size */
382     sppi = HeapReAlloc(GetProcessHeap(), 0, sppi , NeededLength + 2);
383     sppi->KernelTime.QuadPart = 0xdeaddead;
384     sppi->UserTime.QuadPart = 0xdeaddead;
385     sppi->IdleTime.QuadPart = 0xdeaddead;
386     status = pNtQuerySystemInformation(SystemProcessorPerformanceInformation, sppi, NeededLength + 2, &ReturnLength);
387     ok( status == STATUS_SUCCESS || status == STATUS_INFO_LENGTH_MISMATCH /* vista */,
388         "Expected STATUS_SUCCESS or STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
389     ok( NeededLength == ReturnLength, "Inconsistent length (%d) <-> (%d)\n", NeededLength, ReturnLength);
390     if (status == STATUS_SUCCESS)
391     {
392         ok (sppi->KernelTime.QuadPart != 0xdeaddead, "KernelTime unchanged\n");
393         ok (sppi->UserTime.QuadPart != 0xdeaddead, "UserTime unchanged\n");
394         ok (sppi->IdleTime.QuadPart != 0xdeaddead, "IdleTime unchanged\n");
395     }
396     else /* vista and 2008 */
397     {
398         ok (sppi->KernelTime.QuadPart == 0xdeaddead, "KernelTime changed\n");
399         ok (sppi->UserTime.QuadPart == 0xdeaddead, "UserTime changed\n");
400         ok (sppi->IdleTime.QuadPart == 0xdeaddead, "IdleTime changed\n");
401     }
402
403     HeapFree( GetProcessHeap(), 0, sppi);
404 }
405
406 static void test_query_module(void)
407 {
408     NTSTATUS status;
409     ULONG ReturnLength;
410     ULONG ModuleCount, i;
411
412     ULONG SystemInformationLength = sizeof(SYSTEM_MODULE_INFORMATION);
413     SYSTEM_MODULE_INFORMATION* smi = HeapAlloc(GetProcessHeap(), 0, SystemInformationLength); 
414     SYSTEM_MODULE* sm;
415
416     /* Request the needed length */
417     status = pNtQuerySystemInformation(SystemModuleInformation, smi, 0, &ReturnLength);
418     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
419     ok( ReturnLength > 0, "Expected a ReturnLength to show the needed length\n");
420
421     SystemInformationLength = ReturnLength;
422     smi = HeapReAlloc(GetProcessHeap(), 0, smi , SystemInformationLength);
423     status = pNtQuerySystemInformation(SystemModuleInformation, smi, SystemInformationLength, &ReturnLength);
424     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
425
426     ModuleCount = smi->ModulesCount;
427     sm = &smi->Modules[0];
428     /* our implementation is a stub for now */
429     ok( ModuleCount > 0, "Expected some modules to be loaded\n");
430
431     /* Loop through all the modules/drivers, Wine doesn't get here (yet) */
432     for (i = 0; i < ModuleCount ; i++)
433     {
434         ok( i == sm->Id, "Id (%d) should have matched %u\n", sm->Id, i);
435         sm++;
436     }
437
438     HeapFree( GetProcessHeap(), 0, smi);
439 }
440
441 static void test_query_handle(void)
442 {
443     NTSTATUS status;
444     ULONG ReturnLength;
445     ULONG SystemInformationLength = sizeof(SYSTEM_HANDLE_INFORMATION);
446     SYSTEM_HANDLE_INFORMATION* shi = HeapAlloc(GetProcessHeap(), 0, SystemInformationLength);
447
448     /* Request the needed length : a SystemInformationLength greater than one struct sets ReturnLength */
449     status = pNtQuerySystemInformation(SystemHandleInformation, shi, SystemInformationLength, &ReturnLength);
450     todo_wine ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
451
452     SystemInformationLength = ReturnLength;
453     shi = HeapReAlloc(GetProcessHeap(), 0, shi , SystemInformationLength);
454     status = pNtQuerySystemInformation(SystemHandleInformation, shi, SystemInformationLength, &ReturnLength);
455     if (status != STATUS_INFO_LENGTH_MISMATCH) /* vista */
456     {
457         ok( status == STATUS_SUCCESS,
458             "Expected STATUS_SUCCESS, got %08x\n", status);
459
460         /* Check if we have some return values */
461         trace("Number of Handles : %d\n", shi->Count);
462         todo_wine
463         {
464             /* our implementation is a stub for now */
465             ok( shi->Count > 1, "Expected more than 1 handles, got (%d)\n", shi->Count);
466         }
467     }
468     HeapFree( GetProcessHeap(), 0, shi);
469 }
470
471 static void test_query_cache(void)
472 {
473     NTSTATUS status;
474     ULONG ReturnLength;
475     SYSTEM_CACHE_INFORMATION sci;
476
477     status = pNtQuerySystemInformation(SystemCacheInformation, &sci, 0, &ReturnLength);
478     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
479
480     status = pNtQuerySystemInformation(SystemCacheInformation, &sci, sizeof(sci), &ReturnLength);
481     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
482     ok( sizeof(sci) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
483
484     status = pNtQuerySystemInformation(SystemCacheInformation, &sci, sizeof(sci) + 2, &ReturnLength);
485     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
486     ok( sizeof(sci) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
487 }
488
489 static void test_query_interrupt(void)
490 {
491     NTSTATUS status;
492     ULONG ReturnLength;
493     ULONG NeededLength;
494     SYSTEM_BASIC_INFORMATION sbi;
495     SYSTEM_INTERRUPT_INFORMATION* sii;
496
497     /* Find out the number of processors */
498     status = pNtQuerySystemInformation(SystemBasicInformation, &sbi, sizeof(sbi), &ReturnLength);
499     NeededLength = sbi.NumberOfProcessors * sizeof(SYSTEM_INTERRUPT_INFORMATION);
500
501     sii = HeapAlloc(GetProcessHeap(), 0, NeededLength);
502
503     status = pNtQuerySystemInformation(SystemInterruptInformation, sii, 0, &ReturnLength);
504     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
505
506     /* Try it for all processors */
507     status = pNtQuerySystemInformation(SystemInterruptInformation, sii, NeededLength, &ReturnLength);
508     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
509
510     /* Windows XP and W2K3 (and others?) always return 0 for the ReturnLength
511      * No test added for this as it's highly unlikely that an app depends on this
512     */
513
514     HeapFree( GetProcessHeap(), 0, sii);
515 }
516
517 static void test_query_kerndebug(void)
518 {
519     NTSTATUS status;
520     ULONG ReturnLength;
521     SYSTEM_KERNEL_DEBUGGER_INFORMATION skdi;
522
523     status = pNtQuerySystemInformation(SystemKernelDebuggerInformation, &skdi, 0, &ReturnLength);
524     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
525
526     status = pNtQuerySystemInformation(SystemKernelDebuggerInformation, &skdi, sizeof(skdi), &ReturnLength);
527     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
528     ok( sizeof(skdi) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
529
530     status = pNtQuerySystemInformation(SystemKernelDebuggerInformation, &skdi, sizeof(skdi) + 2, &ReturnLength);
531     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
532     ok( sizeof(skdi) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
533 }
534
535 static void test_query_regquota(void)
536 {
537     NTSTATUS status;
538     ULONG ReturnLength;
539     SYSTEM_REGISTRY_QUOTA_INFORMATION srqi;
540
541     status = pNtQuerySystemInformation(SystemRegistryQuotaInformation, &srqi, 0, &ReturnLength);
542     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
543
544     status = pNtQuerySystemInformation(SystemRegistryQuotaInformation, &srqi, sizeof(srqi), &ReturnLength);
545     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
546     ok( sizeof(srqi) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
547
548     status = pNtQuerySystemInformation(SystemRegistryQuotaInformation, &srqi, sizeof(srqi) + 2, &ReturnLength);
549     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
550     ok( sizeof(srqi) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
551 }
552
553 static void test_query_process_basic(void)
554 {
555     NTSTATUS status;
556     ULONG ReturnLength;
557
558     typedef struct _PROCESS_BASIC_INFORMATION_PRIVATE {
559         DWORD_PTR ExitStatus;
560         PPEB      PebBaseAddress;
561         DWORD_PTR AffinityMask;
562         DWORD_PTR BasePriority;
563         ULONG_PTR UniqueProcessId;
564         ULONG_PTR InheritedFromUniqueProcessId;
565     } PROCESS_BASIC_INFORMATION_PRIVATE, *PPROCESS_BASIC_INFORMATION_PRIVATE;
566
567     PROCESS_BASIC_INFORMATION_PRIVATE pbi;
568
569     /* This test also covers some basic parameter testing that should be the same for
570      * every information class
571     */
572
573     /* Use a nonexistent info class */
574     trace("Check nonexistent info class\n");
575     status = pNtQueryInformationProcess(NULL, -1, NULL, 0, NULL);
576     ok( status == STATUS_INVALID_INFO_CLASS || status == STATUS_NOT_IMPLEMENTED /* vista */,
577         "Expected STATUS_INVALID_INFO_CLASS or STATUS_NOT_IMPLEMENTED, got %08x\n", status);
578
579     /* Do not give a handle and buffer */
580     trace("Check NULL handle and buffer and zero-length buffersize\n");
581     status = pNtQueryInformationProcess(NULL, ProcessBasicInformation, NULL, 0, NULL);
582     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
583
584     /* Use a correct info class and buffer size, but still no handle and buffer */
585     trace("Check NULL handle and buffer\n");
586     status = pNtQueryInformationProcess(NULL, ProcessBasicInformation, NULL, sizeof(pbi), NULL);
587     ok( status == STATUS_ACCESS_VIOLATION || status == STATUS_INVALID_HANDLE,
588         "Expected STATUS_ACCESS_VIOLATION or STATUS_INVALID_HANDLE(W2K3), got %08x\n", status);
589
590     /* Use a correct info class and buffer size, but still no handle */
591     trace("Check NULL handle\n");
592     status = pNtQueryInformationProcess(NULL, ProcessBasicInformation, &pbi, sizeof(pbi), NULL);
593     ok( status == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got %08x\n", status);
594
595     /* Use a greater buffer size */
596     trace("Check NULL handle and too large buffersize\n");
597     status = pNtQueryInformationProcess(NULL, ProcessBasicInformation, &pbi, sizeof(pbi) * 2, NULL);
598     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
599
600     /* Use no ReturnLength */
601     trace("Check NULL ReturnLength\n");
602     status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessBasicInformation, &pbi, sizeof(pbi), NULL);
603     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
604
605     /* Finally some correct calls */
606     trace("Check with correct parameters\n");
607     status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessBasicInformation, &pbi, sizeof(pbi), &ReturnLength);
608     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
609     ok( sizeof(pbi) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
610
611     /* Everything is correct except a too large buffersize */
612     trace("Too large buffersize\n");
613     status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessBasicInformation, &pbi, sizeof(pbi) * 2, &ReturnLength);
614     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
615     ok( sizeof(pbi) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
616                                                                                                                                                
617     /* Check if we have some return values */
618     trace("ProcessID : %lx\n", pbi.UniqueProcessId);
619     ok( pbi.UniqueProcessId > 0, "Expected a ProcessID > 0, got 0\n");
620 }
621
622 static void test_query_process_vm(void)
623 {
624     NTSTATUS status;
625     ULONG ReturnLength;
626     VM_COUNTERS pvi;
627     ULONG old_size = FIELD_OFFSET(VM_COUNTERS,PrivatePageCount);
628
629     status = pNtQueryInformationProcess(NULL, ProcessVmCounters, NULL, sizeof(pvi), NULL);
630     ok( status == STATUS_ACCESS_VIOLATION || status == STATUS_INVALID_HANDLE,
631         "Expected STATUS_ACCESS_VIOLATION or STATUS_INVALID_HANDLE(W2K3), got %08x\n", status);
632
633     status = pNtQueryInformationProcess(NULL, ProcessVmCounters, &pvi, old_size, NULL);
634     ok( status == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got %08x\n", status);
635
636     /* Windows XP and W2K3 will report success for a size of 44 AND 48 !
637        Windows W2K will only report success for 44.
638        For now we only care for 44, which is FIELD_OFFSET(VM_COUNTERS,PrivatePageCount))
639     */
640
641     status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessVmCounters, &pvi, 24, &ReturnLength);
642     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
643
644     status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessVmCounters, &pvi, old_size, &ReturnLength);
645     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
646     ok( old_size == ReturnLength, "Inconsistent length %d\n", ReturnLength);
647
648     status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessVmCounters, &pvi, 46, &ReturnLength);
649     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
650     ok( ReturnLength == old_size || ReturnLength == sizeof(pvi), "Inconsistent length %d\n", ReturnLength);
651
652     /* Check if we have some return values */
653     trace("WorkingSetSize : %ld\n", pvi.WorkingSetSize);
654     todo_wine
655     {
656         ok( pvi.WorkingSetSize > 0, "Expected a WorkingSetSize > 0\n");
657     }
658 }
659
660 static void test_query_process_io(void)
661 {
662     NTSTATUS status;
663     ULONG ReturnLength;
664     IO_COUNTERS pii;
665
666     /* NT4 doesn't support this information class, so check for it */
667     status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessIoCounters, &pii, sizeof(pii), &ReturnLength);
668     if (status == STATUS_NOT_SUPPORTED)
669     {
670         win_skip("ProcessIoCounters information class is not supported\n");
671         return;
672     }
673  
674     status = pNtQueryInformationProcess(NULL, ProcessIoCounters, NULL, sizeof(pii), NULL);
675     ok( status == STATUS_ACCESS_VIOLATION || status == STATUS_INVALID_HANDLE,
676         "Expected STATUS_ACCESS_VIOLATION or STATUS_INVALID_HANDLE(W2K3), got %08x\n", status);
677
678     status = pNtQueryInformationProcess(NULL, ProcessIoCounters, &pii, sizeof(pii), NULL);
679     ok( status == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got %08x\n", status);
680
681     status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessIoCounters, &pii, 24, &ReturnLength);
682     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
683
684     status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessIoCounters, &pii, sizeof(pii), &ReturnLength);
685     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
686     ok( sizeof(pii) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
687
688     status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessIoCounters, &pii, sizeof(pii) * 2, &ReturnLength);
689     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
690     ok( sizeof(pii) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
691
692     /* Check if we have some return values */
693     trace("OtherOperationCount : 0x%x%08x\n", (DWORD)(pii.OtherOperationCount >> 32), (DWORD)pii.OtherOperationCount);
694     todo_wine
695     {
696         ok( pii.OtherOperationCount > 0, "Expected an OtherOperationCount > 0\n");
697     }
698 }
699
700 static void test_query_process_times(void)
701 {
702     NTSTATUS status;
703     ULONG ReturnLength;
704     HANDLE process;
705     SYSTEMTIME UTC, Local;
706     KERNEL_USER_TIMES spti;
707
708     status = pNtQueryInformationProcess(NULL, ProcessTimes, NULL, sizeof(spti), NULL);
709     ok( status == STATUS_ACCESS_VIOLATION || status == STATUS_INVALID_HANDLE,
710         "Expected STATUS_ACCESS_VIOLATION or STATUS_INVALID_HANDLE(W2K3), got %08x\n", status);
711
712     status = pNtQueryInformationProcess(NULL, ProcessTimes, &spti, sizeof(spti), NULL);
713     ok( status == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got %08x\n", status);
714
715     status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessTimes, &spti, 24, &ReturnLength);
716     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
717
718     process = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, one_before_last_pid);
719     if (!process)
720     {
721         trace("Could not open process with ID : %d, error : %u. Going to use current one.\n", one_before_last_pid, GetLastError());
722         process = GetCurrentProcess();
723         trace("ProcessTimes for current process\n");
724     }
725     else
726         trace("ProcessTimes for process with ID : %d\n", one_before_last_pid);
727
728     status = pNtQueryInformationProcess( process, ProcessTimes, &spti, sizeof(spti), &ReturnLength);
729     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
730     ok( sizeof(spti) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
731     CloseHandle(process);
732
733     FileTimeToSystemTime((const FILETIME *)&spti.CreateTime, &UTC);
734     SystemTimeToTzSpecificLocalTime(NULL, &UTC, &Local);
735     trace("CreateTime : %02d/%02d/%04d %02d:%02d:%02d\n", Local.wMonth, Local.wDay, Local.wYear,
736            Local.wHour, Local.wMinute, Local.wSecond);
737
738     FileTimeToSystemTime((const FILETIME *)&spti.ExitTime, &UTC);
739     SystemTimeToTzSpecificLocalTime(NULL, &UTC, &Local);
740     trace("ExitTime   : %02d/%02d/%04d %02d:%02d:%02d\n", Local.wMonth, Local.wDay, Local.wYear,
741            Local.wHour, Local.wMinute, Local.wSecond);
742
743     FileTimeToSystemTime((const FILETIME *)&spti.KernelTime, &Local);
744     trace("KernelTime : %02d:%02d:%02d.%03d\n", Local.wHour, Local.wMinute, Local.wSecond, Local.wMilliseconds);
745
746     FileTimeToSystemTime((const FILETIME *)&spti.UserTime, &Local);
747     trace("UserTime   : %02d:%02d:%02d.%03d\n", Local.wHour, Local.wMinute, Local.wSecond, Local.wMilliseconds);
748
749     status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessTimes, &spti, sizeof(spti) * 2, &ReturnLength);
750     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
751     ok( sizeof(spti) == ReturnLength || ReturnLength == 0 /* vista */,
752         "Inconsistent length %d\n", ReturnLength);
753 }
754
755 static void test_query_process_debug_port(int argc, char **argv)
756 {
757     DWORD_PTR debug_port = 0xdeadbeef;
758     char cmdline[MAX_PATH];
759     PROCESS_INFORMATION pi;
760     STARTUPINFO si = { 0 };
761     NTSTATUS status;
762     BOOL ret;
763
764     sprintf(cmdline, "%s %s %s", argv[0], argv[1], "debuggee");
765
766     si.cb = sizeof(si);
767     ret = CreateProcess(NULL, cmdline, NULL, NULL, FALSE, DEBUG_PROCESS, NULL, NULL, &si, &pi);
768     ok(ret, "CreateProcess failed, last error %#x.\n", GetLastError());
769     if (!ret) return;
770
771     status = pNtQueryInformationProcess(NULL, ProcessDebugPort,
772             NULL, 0, NULL);
773     ok(status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %#x.\n", status);
774
775     status = pNtQueryInformationProcess(NULL, ProcessDebugPort,
776             NULL, sizeof(debug_port), NULL);
777     ok(status == STATUS_INVALID_HANDLE || status == STATUS_ACCESS_VIOLATION,
778             "Expected STATUS_INVALID_HANDLE, got %#x.\n", status);
779
780     status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessDebugPort,
781             NULL, sizeof(debug_port), NULL);
782     ok(status == STATUS_ACCESS_VIOLATION, "Expected STATUS_ACCESS_VIOLATION, got %#x.\n", status);
783
784     status = pNtQueryInformationProcess(NULL, ProcessDebugPort,
785             &debug_port, sizeof(debug_port), NULL);
786     ok(status == STATUS_INVALID_HANDLE, "Expected STATUS_ACCESS_VIOLATION, got %#x.\n", status);
787
788     status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessDebugPort,
789             &debug_port, sizeof(debug_port) - 1, NULL);
790     ok(status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %#x.\n", status);
791
792     status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessDebugPort,
793             &debug_port, sizeof(debug_port) + 1, NULL);
794     ok(status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %#x.\n", status);
795
796     status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessDebugPort,
797             &debug_port, sizeof(debug_port), NULL);
798     ok(!status, "NtQueryInformationProcess failed, status %#x.\n", status);
799     ok(debug_port == 0, "Expected port 0, got %#lx.\n", debug_port);
800
801     status = pNtQueryInformationProcess(pi.hProcess, ProcessDebugPort,
802             &debug_port, sizeof(debug_port), NULL);
803     ok(!status, "NtQueryInformationProcess failed, status %#x.\n", status);
804     ok(debug_port == ~(DWORD_PTR)0, "Expected port %#lx, got %#lx.\n", ~(DWORD_PTR)0, debug_port);
805
806     for (;;)
807     {
808         DEBUG_EVENT ev;
809
810         ret = WaitForDebugEvent(&ev, INFINITE);
811         ok(ret, "WaitForDebugEvent failed, last error %#x.\n", GetLastError());
812         if (!ret) break;
813
814         if (ev.dwDebugEventCode == EXIT_PROCESS_DEBUG_EVENT) break;
815
816         ret = ContinueDebugEvent(ev.dwProcessId, ev.dwThreadId, DBG_CONTINUE);
817         ok(ret, "ContinueDebugEvent failed, last error %#x.\n", GetLastError());
818         if (!ret) break;
819     }
820
821     ret = CloseHandle(pi.hThread);
822     ok(ret, "CloseHandle failed, last error %#x.\n", GetLastError());
823     ret = CloseHandle(pi.hProcess);
824     ok(ret, "CloseHandle failed, last error %#x.\n", GetLastError());
825 }
826
827 static void test_query_process_handlecount(void)
828 {
829     NTSTATUS status;
830     ULONG ReturnLength;
831     DWORD handlecount;
832     BYTE buffer[2 * sizeof(DWORD)];
833     HANDLE process;
834
835     status = pNtQueryInformationProcess(NULL, ProcessHandleCount, NULL, sizeof(handlecount), NULL);
836     ok( status == STATUS_ACCESS_VIOLATION || status == STATUS_INVALID_HANDLE,
837         "Expected STATUS_ACCESS_VIOLATION or STATUS_INVALID_HANDLE(W2K3), got %08x\n", status);
838
839     status = pNtQueryInformationProcess(NULL, ProcessHandleCount, &handlecount, sizeof(handlecount), NULL);
840     ok( status == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got %08x\n", status);
841
842     status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessHandleCount, &handlecount, 2, &ReturnLength);
843     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
844
845     process = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, one_before_last_pid);
846     if (!process)
847     {
848         trace("Could not open process with ID : %d, error : %u. Going to use current one.\n", one_before_last_pid, GetLastError());
849         process = GetCurrentProcess();
850         trace("ProcessHandleCount for current process\n");
851     }
852     else
853         trace("ProcessHandleCount for process with ID : %d\n", one_before_last_pid);
854
855     status = pNtQueryInformationProcess( process, ProcessHandleCount, &handlecount, sizeof(handlecount), &ReturnLength);
856     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
857     ok( sizeof(handlecount) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
858     CloseHandle(process);
859
860     status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessHandleCount, buffer, sizeof(buffer), &ReturnLength);
861     ok( status == STATUS_INFO_LENGTH_MISMATCH || status == STATUS_SUCCESS,
862         "Expected STATUS_INFO_LENGTH_MISMATCH or STATUS_SUCCESS, got %08x\n", status);
863     ok( sizeof(handlecount) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
864
865     /* Check if we have some return values */
866     trace("HandleCount : %d\n", handlecount);
867     todo_wine
868     {
869         ok( handlecount > 0, "Expected some handles, got 0\n");
870     }
871 }
872
873 static void test_query_process_image_file_name(void)
874 {
875     NTSTATUS status;
876     ULONG ReturnLength;
877     UNICODE_STRING image_file_name;
878     void *buffer;
879     char *file_nameA;
880     INT len;
881
882     status = pNtQueryInformationProcess(NULL, ProcessImageFileName, &image_file_name, sizeof(image_file_name), NULL);
883     if (status == STATUS_INVALID_INFO_CLASS)
884     {
885         win_skip("ProcessImageFileName is not supported\n");
886         return;
887     }
888     ok( status == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got %08x\n", status);
889
890     status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessImageFileName, &image_file_name, 2, &ReturnLength);
891     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
892
893     status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessImageFileName, &image_file_name, sizeof(image_file_name), &ReturnLength);
894     ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
895
896     buffer = HeapAlloc(GetProcessHeap(), 0, ReturnLength);
897     status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessImageFileName, buffer, ReturnLength, &ReturnLength);
898     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
899     memcpy(&image_file_name, buffer, sizeof(image_file_name));
900     len = WideCharToMultiByte(CP_ACP, 0, image_file_name.Buffer, image_file_name.Length/sizeof(WCHAR), NULL, 0, NULL, NULL);
901     file_nameA = HeapAlloc(GetProcessHeap(), 0, len + 1);
902     WideCharToMultiByte(CP_ACP, 0, image_file_name.Buffer, image_file_name.Length/sizeof(WCHAR), file_nameA, len, NULL, NULL);
903     file_nameA[len] = '\0';
904     HeapFree(GetProcessHeap(), 0, buffer);
905     trace("process image file name: %s\n", file_nameA);
906     todo_wine ok(strncmp(file_nameA, "\\Device\\", 8) == 0, "Process image name should be an NT path beginning with \\Device\\ (is %s)\n", file_nameA);
907     HeapFree(GetProcessHeap(), 0, file_nameA);
908 }
909
910
911 static void test_readvirtualmemory(void)
912 {
913     HANDLE process;
914     NTSTATUS status;
915     SIZE_T readcount;
916     static const char teststring[] = "test string";
917     char buffer[12];
918
919     process = OpenProcess(PROCESS_VM_READ, FALSE, GetCurrentProcessId());
920     ok(process != 0, "Expected to be able to open own process for reading memory\n");
921
922     /* normal operation */
923     status = pNtReadVirtualMemory(process, teststring, buffer, 12, &readcount);
924     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
925     ok( readcount == 12, "Expected to read 12 bytes, got %ld\n",readcount);
926     ok( strcmp(teststring, buffer) == 0, "Expected read memory to be the same as original memory\n");
927
928     /* no number of bytes */
929     memset(buffer, 0, 12);
930     status = pNtReadVirtualMemory(process, teststring, buffer, 12, NULL);
931     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
932     ok( strcmp(teststring, buffer) == 0, "Expected read memory to be the same as original memory\n");
933
934     /* illegal remote address */
935     todo_wine{
936     status = pNtReadVirtualMemory(process, (void *) 0x1234, buffer, 12, &readcount);
937     ok( status == STATUS_PARTIAL_COPY || broken(status == STATUS_ACCESS_VIOLATION), "Expected STATUS_PARTIAL_COPY, got %08x\n", status);
938     if (status == STATUS_PARTIAL_COPY)
939         ok( readcount == 0, "Expected to read 0 bytes, got %ld\n",readcount);
940     }
941
942     /* 0 handle */
943     status = pNtReadVirtualMemory(0, teststring, buffer, 12, &readcount);
944     ok( status == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got %08x\n", status);
945     ok( readcount == 0, "Expected to read 0 bytes, got %ld\n",readcount);
946
947     /* pseudo handle for current process*/
948     memset(buffer, 0, 12);
949     status = pNtReadVirtualMemory((HANDLE)-1, teststring, buffer, 12, &readcount);
950     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
951     ok( readcount == 12, "Expected to read 12 bytes, got %ld\n",readcount);
952     ok( strcmp(teststring, buffer) == 0, "Expected read memory to be the same as original memory\n");
953
954     /* illegal local address */
955     status = pNtReadVirtualMemory(process, teststring, (void *)0x1234, 12, &readcount);
956     ok( status == STATUS_ACCESS_VIOLATION, "Expected STATUS_ACCESS_VIOLATION, got %08x\n", status);
957     ok( readcount == 0, "Expected to read 0 bytes, got %ld\n",readcount);
958
959     CloseHandle(process);
960 }
961
962 static void test_affinity(void)
963 {
964     NTSTATUS status;
965     PROCESS_BASIC_INFORMATION pbi;
966     DWORD_PTR proc_affinity, thread_affinity;
967     THREAD_BASIC_INFORMATION tbi;
968     SYSTEM_INFO si;
969
970     GetSystemInfo(&si);
971     status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessBasicInformation, &pbi, sizeof(pbi), NULL );
972     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
973     proc_affinity = (DWORD_PTR)pbi.Reserved2[0];
974     ok( proc_affinity == (1 << si.dwNumberOfProcessors) - 1, "Unexpected process affinity\n" );
975     proc_affinity = 1 << si.dwNumberOfProcessors;
976     status = pNtSetInformationProcess( GetCurrentProcess(), ProcessAffinityMask, &proc_affinity, sizeof(proc_affinity) );
977     ok( status == STATUS_INVALID_PARAMETER,
978         "Expected STATUS_INVALID_PARAMETER, got %08x\n", status);
979
980     proc_affinity = 0;
981     status = pNtSetInformationProcess( GetCurrentProcess(), ProcessAffinityMask, &proc_affinity, sizeof(proc_affinity) );
982     ok( status == STATUS_INVALID_PARAMETER,
983         "Expected STATUS_INVALID_PARAMETER, got %08x\n", status);
984
985     status = pNtQueryInformationThread( GetCurrentThread(), ThreadBasicInformation, &tbi, sizeof(tbi), NULL );
986     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
987     ok( tbi.AffinityMask == (1 << si.dwNumberOfProcessors) - 1, "Unexpected thread affinity\n" );
988     thread_affinity = 1 << si.dwNumberOfProcessors;
989     status = pNtSetInformationThread( GetCurrentThread(), ThreadAffinityMask, &thread_affinity, sizeof(thread_affinity) );
990     ok( status == STATUS_INVALID_PARAMETER,
991         "Expected STATUS_INVALID_PARAMETER, got %08x\n", status);
992     thread_affinity = 0;
993     status = pNtSetInformationThread( GetCurrentThread(), ThreadAffinityMask, &thread_affinity, sizeof(thread_affinity) );
994     ok( status == STATUS_INVALID_PARAMETER,
995         "Expected STATUS_INVALID_PARAMETER, got %08x\n", status);
996
997     thread_affinity = 1;
998     status = pNtSetInformationThread( GetCurrentThread(), ThreadAffinityMask, &thread_affinity, sizeof(thread_affinity) );
999     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1000     status = pNtQueryInformationThread( GetCurrentThread(), ThreadBasicInformation, &tbi, sizeof(tbi), NULL );
1001     ok( tbi.AffinityMask == 1, "Unexpected thread affinity\n" );
1002
1003     /* NOTE: Pre-Vista does not recognize the "all processors" flag (all bits set) */
1004     thread_affinity = ~0UL;
1005     status = pNtSetInformationThread( GetCurrentThread(), ThreadAffinityMask, &thread_affinity, sizeof(thread_affinity) );
1006     ok( broken(status == STATUS_INVALID_PARAMETER) || status == STATUS_SUCCESS,
1007         "Expected STATUS_SUCCESS, got %08x\n", status);
1008
1009     if (si.dwNumberOfProcessors <= 1)
1010     {
1011         skip("only one processor, skipping affinity testing\n");
1012         return;
1013     }
1014
1015     /* Test thread affinity mask resulting from "all processors" flag */
1016     if (status == STATUS_SUCCESS)
1017     {
1018         status = pNtQueryInformationThread( GetCurrentThread(), ThreadBasicInformation, &tbi, sizeof(tbi), NULL );
1019         ok( broken(tbi.AffinityMask == 1) || tbi.AffinityMask == (1 << si.dwNumberOfProcessors) - 1,
1020             "Unexpected thread affinity\n" );
1021     }
1022     else
1023         skip("Cannot test thread affinity mask for 'all processors' flag\n");
1024
1025     proc_affinity = 2;
1026     status = pNtSetInformationProcess( GetCurrentProcess(), ProcessAffinityMask, &proc_affinity, sizeof(proc_affinity) );
1027     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1028     status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessBasicInformation, &pbi, sizeof(pbi), NULL );
1029     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1030     proc_affinity = (DWORD_PTR)pbi.Reserved2[0];
1031     ok( proc_affinity == 2, "Unexpected process affinity\n" );
1032     /* Setting the process affinity changes the thread affinity to match */
1033     status = pNtQueryInformationThread( GetCurrentThread(), ThreadBasicInformation, &tbi, sizeof(tbi), NULL );
1034     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1035     ok( tbi.AffinityMask == 2, "Unexpected thread affinity\n" );
1036     /* The thread affinity is restricted to the process affinity */
1037     thread_affinity = 1;
1038     status = pNtSetInformationThread( GetCurrentThread(), ThreadAffinityMask, &thread_affinity, sizeof(thread_affinity) );
1039     ok( status == STATUS_INVALID_PARAMETER,
1040         "Expected STATUS_INVALID_PARAMETER, got %08x\n", status);
1041
1042     proc_affinity = (1 << si.dwNumberOfProcessors) - 1;
1043     status = pNtSetInformationProcess( GetCurrentProcess(), ProcessAffinityMask, &proc_affinity, sizeof(proc_affinity) );
1044     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1045     /* Resetting the process affinity also resets the thread affinity */
1046     status = pNtQueryInformationThread( GetCurrentThread(), ThreadBasicInformation, &tbi, sizeof(tbi), NULL );
1047     ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1048     ok( tbi.AffinityMask == (1 << si.dwNumberOfProcessors) - 1,
1049         "Unexpected thread affinity\n" );
1050 }
1051
1052 START_TEST(info)
1053 {
1054     char **argv;
1055     int argc;
1056
1057     if(!InitFunctionPtrs())
1058         return;
1059
1060     argc = winetest_get_mainargs(&argv);
1061     if (argc >= 3) return; /* Child */
1062
1063     /* NtQuerySystemInformation */
1064
1065     /* 0x0 SystemBasicInformation */
1066     trace("Starting test_query_basic()\n");
1067     test_query_basic();
1068
1069     /* 0x1 SystemCpuInformation */
1070     trace("Starting test_query_cpu()\n");
1071     test_query_cpu();
1072
1073     /* 0x2 SystemPerformanceInformation */
1074     trace("Starting test_query_performance()\n");
1075     test_query_performance();
1076
1077     /* 0x3 SystemTimeOfDayInformation */
1078     trace("Starting test_query_timeofday()\n");
1079     test_query_timeofday();
1080
1081     /* 0x5 SystemProcessInformation */
1082     trace("Starting test_query_process()\n");
1083     test_query_process();
1084
1085     /* 0x8 SystemProcessorPerformanceInformation */
1086     trace("Starting test_query_procperf()\n");
1087     test_query_procperf();
1088
1089     /* 0xb SystemModuleInformation */
1090     trace("Starting test_query_module()\n");
1091     test_query_module();
1092
1093     /* 0x10 SystemHandleInformation */
1094     trace("Starting test_query_handle()\n");
1095     test_query_handle();
1096
1097     /* 0x15 SystemCacheInformation */
1098     trace("Starting test_query_cache()\n");
1099     test_query_cache();
1100
1101     /* 0x17 SystemInterruptInformation */
1102     trace("Starting test_query_interrupt()\n");
1103     test_query_interrupt();
1104
1105     /* 0x23 SystemKernelDebuggerInformation */
1106     trace("Starting test_query_kerndebug()\n");
1107     test_query_kerndebug();
1108
1109     /* 0x25 SystemRegistryQuotaInformation */
1110     trace("Starting test_query_regquota()\n");
1111     test_query_regquota();
1112
1113     /* NtQueryInformationProcess */
1114
1115     /* 0x0 ProcessBasicInformation */
1116     trace("Starting test_query_process_basic()\n");
1117     test_query_process_basic();
1118
1119     /* 0x2 ProcessIoCounters */
1120     trace("Starting test_query_process_io()\n");
1121     test_query_process_io();
1122
1123     /* 0x3 ProcessVmCounters */
1124     trace("Starting test_query_process_vm()\n");
1125     test_query_process_vm();
1126
1127     /* 0x4 ProcessTimes */
1128     trace("Starting test_query_process_times()\n");
1129     test_query_process_times();
1130
1131     /* 0x7 ProcessDebugPort */
1132     trace("Starting test_process_debug_port()\n");
1133     test_query_process_debug_port(argc, argv);
1134
1135     /* 0x14 ProcessHandleCount */
1136     trace("Starting test_query_process_handlecount()\n");
1137     test_query_process_handlecount();
1138
1139     /* 27 ProcessImageFileName */
1140     trace("Starting test_query_process_image_file_name()\n");
1141     test_query_process_image_file_name();
1142
1143     /* belongs into it's own file */
1144     trace("Starting test_readvirtualmemory()\n");
1145     test_readvirtualmemory();
1146
1147     trace("Starting test_affinity()\n");
1148     test_affinity();
1149 }