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