psapi/tests: Call GetPerformanceInfo() after NtQuerySystemInformation().
[wine.git] / dlls / psapi / tests / psapi_main.c
blobe61cf02948be20a3868f2e9f98c1b93bb1cdff8b
1 /*
2 * Unit test suite for PSAPI
4 * Copyright (C) 2005 Felix Nawothnig
5 * Copyright (C) 2012 Dmitry Timoshkov
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 /* 0x0600 makes PROCESS_ALL_ACCESS not compatible with pre-Vista versions */
23 #define _WIN32_WINNT 0x0500
25 #include <stdarg.h>
27 #include "ntstatus.h"
28 #define WIN32_NO_STATUS
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winreg.h"
33 #include "winnt.h"
34 #include "winternl.h"
35 #include "winnls.h"
36 #include "winuser.h"
37 #define PSAPI_VERSION 1
38 #include "psapi.h"
39 #include "wine/test.h"
41 static NTSTATUS (WINAPI *pNtQuerySystemInformation)(SYSTEM_INFORMATION_CLASS, PVOID, ULONG, PULONG);
42 static NTSTATUS (WINAPI *pNtQueryVirtualMemory)(HANDLE, LPCVOID, ULONG, PVOID, SIZE_T, SIZE_T *);
43 static BOOL (WINAPI *pIsWow64Process)(HANDLE, BOOL *);
44 static BOOL (WINAPI *pWow64DisableWow64FsRedirection)(void **);
45 static BOOL (WINAPI *pWow64RevertWow64FsRedirection)(void *);
47 static BOOL wow64;
49 static BOOL init_func_ptrs(void)
51 pNtQuerySystemInformation = (void *)GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtQuerySystemInformation");
52 pNtQueryVirtualMemory = (void *)GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtQueryVirtualMemory");
53 pIsWow64Process = (void *)GetProcAddress(GetModuleHandleA("kernel32.dll"), "IsWow64Process");
54 pWow64DisableWow64FsRedirection = (void *)GetProcAddress(GetModuleHandleA("kernel32.dll"), "Wow64DisableWow64FsRedirection");
55 pWow64RevertWow64FsRedirection = (void *)GetProcAddress(GetModuleHandleA("kernel32.dll"), "Wow64RevertWow64FsRedirection");
56 return TRUE;
59 static HANDLE hpSR, hpQI, hpVR, hpQV;
60 static const HANDLE hBad = (HANDLE)0xdeadbeef;
62 static void test_EnumProcesses(void)
64 DWORD pid, ret, cbUsed = 0xdeadbeef;
66 SetLastError(0xdeadbeef);
67 ret = EnumProcesses(NULL, 0, &cbUsed);
68 ok(ret == 1, "failed with %d\n", GetLastError());
69 ok(cbUsed == 0, "cbUsed=%d\n", cbUsed);
71 SetLastError(0xdeadbeef);
72 ret = EnumProcesses(&pid, 4, &cbUsed);
73 ok(ret == 1, "failed with %d\n", GetLastError());
74 ok(cbUsed == 4, "cbUsed=%d\n", cbUsed);
77 static void test_EnumProcessModules(void)
79 char buffer[200] = "C:\\windows\\system32\\notepad.exe";
80 PROCESS_INFORMATION pi = {0};
81 STARTUPINFOA si = {0};
82 void *cookie;
83 HMODULE hMod;
84 DWORD ret, cbNeeded = 0xdeadbeef;
86 SetLastError(0xdeadbeef);
87 EnumProcessModules(NULL, NULL, 0, &cbNeeded);
88 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
90 SetLastError(0xdeadbeef);
91 EnumProcessModules(hpQI, NULL, 0, &cbNeeded);
92 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
94 SetLastError(0xdeadbeef);
95 hMod = (void *)0xdeadbeef;
96 ret = EnumProcessModules(hpQI, &hMod, sizeof(HMODULE), NULL);
97 ok(!ret, "succeeded\n");
98 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
100 SetLastError(0xdeadbeef);
101 hMod = (void *)0xdeadbeef;
102 ret = EnumProcessModules(hpQV, &hMod, sizeof(HMODULE), NULL);
103 ok(!ret, "succeeded\n");
104 ok(GetLastError() == ERROR_NOACCESS, "expected error=ERROR_NOACCESS but got %d\n", GetLastError());
105 ok(hMod == GetModuleHandleA(NULL),
106 "hMod=%p GetModuleHandleA(NULL)=%p\n", hMod, GetModuleHandleA(NULL));
108 SetLastError(0xdeadbeef);
109 ret = EnumProcessModules(hpQV, NULL, 0, &cbNeeded);
110 ok(ret == 1, "failed with %d\n", GetLastError());
112 SetLastError(0xdeadbeef);
113 ret = EnumProcessModules(hpQV, NULL, sizeof(HMODULE), &cbNeeded);
114 ok(!ret, "succeeded\n");
115 ok(GetLastError() == ERROR_NOACCESS, "expected error=ERROR_NOACCESS but got %d\n", GetLastError());
117 SetLastError(0xdeadbeef);
118 hMod = (void *)0xdeadbeef;
119 ret = EnumProcessModules(hpQV, &hMod, sizeof(HMODULE), &cbNeeded);
120 ok(ret == 1, "got %d, failed with %d\n", ret, GetLastError());
121 ok(hMod == GetModuleHandleA(NULL),
122 "hMod=%p GetModuleHandleA(NULL)=%p\n", hMod, GetModuleHandleA(NULL));
123 ok(cbNeeded % sizeof(hMod) == 0, "not a multiple of sizeof(HMODULE) cbNeeded=%d\n", cbNeeded);
125 ret = CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
126 ok(ret, "CreateProcess failed: %u\n", GetLastError());
128 ret = WaitForInputIdle(pi.hProcess, 1000);
129 ok(!ret, "wait timed out\n");
131 SetLastError(0xdeadbeef);
132 hMod = NULL;
133 ret = EnumProcessModules(pi.hProcess, &hMod, sizeof(HMODULE), &cbNeeded);
134 ok(ret == 1, "got %d, error %u\n", ret, GetLastError());
135 ok(!!hMod, "expected non-NULL module\n");
136 ok(cbNeeded % sizeof(hMod) == 0, "got %u\n", cbNeeded);
138 TerminateProcess(pi.hProcess, 0);
140 if (sizeof(void *) == 8)
142 MODULEINFO info;
143 char name[40];
145 strcpy(buffer, "C:\\windows\\syswow64\\notepad.exe");
146 ret = CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
147 ok(ret, "CreateProcess failed: %u\n", GetLastError());
149 ret = WaitForInputIdle(pi.hProcess, 1000);
150 ok(!ret, "wait timed out\n");
152 SetLastError(0xdeadbeef);
153 hMod = NULL;
154 ret = EnumProcessModules(pi.hProcess, &hMod, sizeof(HMODULE), &cbNeeded);
155 ok(ret == 1, "got %d, error %u\n", ret, GetLastError());
156 ok(!!hMod, "expected non-NULL module\n");
157 ok(cbNeeded % sizeof(hMod) == 0, "got %u\n", cbNeeded);
159 ret = GetModuleBaseNameA(pi.hProcess, hMod, name, sizeof(name));
160 ok(ret, "got error %u\n", GetLastError());
161 ok(!strcmp(name, "notepad.exe"), "got %s\n", name);
163 ret = GetModuleFileNameExA(pi.hProcess, hMod, name, sizeof(name));
164 ok(ret, "got error %u\n", GetLastError());
165 todo_wine
166 ok(!strcmp(name, buffer), "got %s\n", name);
168 ret = GetModuleInformation(pi.hProcess, hMod, &info, sizeof(info));
169 ok(ret, "got error %u\n", GetLastError());
170 ok(info.lpBaseOfDll == hMod, "expected %p, got %p\n", hMod, info.lpBaseOfDll);
171 ok(info.SizeOfImage, "image size was 0\n");
172 ok(info.EntryPoint >= info.lpBaseOfDll, "got entry point %p\n", info.EntryPoint);
174 TerminateProcess(pi.hProcess, 0);
176 else if (wow64)
178 pWow64DisableWow64FsRedirection(&cookie);
179 ret = CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
180 pWow64RevertWow64FsRedirection(cookie);
181 ok(ret, "CreateProcess failed: %u\n", GetLastError());
183 ret = WaitForInputIdle(pi.hProcess, 1000);
184 ok(!ret, "wait timed out\n");
186 SetLastError(0xdeadbeef);
187 ret = EnumProcessModules(pi.hProcess, &hMod, sizeof(HMODULE), &cbNeeded);
188 ok(!ret, "got %d\n", ret);
189 todo_wine
190 ok(GetLastError() == ERROR_PARTIAL_COPY, "got error %u\n", GetLastError());
192 TerminateProcess(pi.hProcess, 0);
196 static void test_GetModuleInformation(void)
198 HMODULE hMod = GetModuleHandleA(NULL);
199 MODULEINFO info;
200 DWORD ret;
202 SetLastError(0xdeadbeef);
203 GetModuleInformation(NULL, hMod, &info, sizeof(info));
204 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
206 SetLastError(0xdeadbeef);
207 GetModuleInformation(hpQI, hMod, &info, sizeof(info));
208 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
210 SetLastError(0xdeadbeef);
211 GetModuleInformation(hpQV, hBad, &info, sizeof(info));
212 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
214 SetLastError(0xdeadbeef);
215 GetModuleInformation(hpQV, hMod, &info, sizeof(info)-1);
216 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
218 SetLastError(0xdeadbeef);
219 ret = GetModuleInformation(hpQV, hMod, &info, sizeof(info));
220 ok(ret == 1, "failed with %d\n", GetLastError());
221 ok(info.lpBaseOfDll == hMod, "lpBaseOfDll=%p hMod=%p\n", info.lpBaseOfDll, hMod);
224 static BOOL check_with_margin(SIZE_T perf, SIZE_T sysperf, int margin)
226 return (perf >= max(sysperf, margin) - margin && perf <= sysperf + margin);
229 static void test_GetPerformanceInfo(void)
231 PERFORMANCE_INFORMATION info;
232 NTSTATUS status;
233 DWORD size;
234 BOOL ret;
236 SetLastError(0xdeadbeef);
237 ret = GetPerformanceInfo(&info, sizeof(info)-1);
238 ok(!ret, "GetPerformanceInfo unexpectedly succeeded\n");
239 ok(GetLastError() == ERROR_BAD_LENGTH, "expected error=ERROR_BAD_LENGTH but got %d\n", GetLastError());
241 if (!pNtQuerySystemInformation)
242 win_skip("NtQuerySystemInformation not found, skipping tests\n");
243 else
245 char performance_buffer[sizeof(SYSTEM_PERFORMANCE_INFORMATION) + 16]; /* larger on w2k8/win7 */
246 SYSTEM_PERFORMANCE_INFORMATION *sys_performance_info = (SYSTEM_PERFORMANCE_INFORMATION *)performance_buffer;
247 SYSTEM_PROCESS_INFORMATION *sys_process_info = NULL, *spi;
248 SYSTEM_BASIC_INFORMATION sys_basic_info;
249 DWORD process_count, handle_count, thread_count;
251 /* compare with values from SYSTEM_PERFORMANCE_INFORMATION */
252 size = 0;
253 status = pNtQuerySystemInformation(SystemPerformanceInformation, sys_performance_info, sizeof(performance_buffer), &size);
254 ok(status == STATUS_SUCCESS, "expected STATUS_SUCCESS, got %08x\n", status);
255 ok(size >= sizeof(SYSTEM_PERFORMANCE_INFORMATION), "incorrect length %d\n", size);
257 SetLastError(0xdeadbeef);
258 ret = GetPerformanceInfo(&info, sizeof(info));
259 ok(ret, "GetPerformanceInfo failed with %d\n", GetLastError());
260 ok(info.cb == sizeof(PERFORMANCE_INFORMATION), "got %d\n", info.cb);
262 ok(check_with_margin(info.CommitTotal, sys_performance_info->TotalCommittedPages, 288),
263 "expected approximately %ld but got %d\n", info.CommitTotal, sys_performance_info->TotalCommittedPages);
265 ok(check_with_margin(info.CommitLimit, sys_performance_info->TotalCommitLimit, 32),
266 "expected approximately %ld but got %d\n", info.CommitLimit, sys_performance_info->TotalCommitLimit);
268 ok(check_with_margin(info.CommitPeak, sys_performance_info->PeakCommitment, 32),
269 "expected approximately %ld but got %d\n", info.CommitPeak, sys_performance_info->PeakCommitment);
271 ok(check_with_margin(info.PhysicalAvailable, sys_performance_info->AvailablePages, 512),
272 "expected approximately %ld but got %d\n", info.PhysicalAvailable, sys_performance_info->AvailablePages);
274 /* TODO: info.SystemCache not checked yet - to which field(s) does this value correspond to? */
276 ok(check_with_margin(info.KernelTotal, sys_performance_info->PagedPoolUsage + sys_performance_info->NonPagedPoolUsage, 256),
277 "expected approximately %ld but got %d\n", info.KernelTotal,
278 sys_performance_info->PagedPoolUsage + sys_performance_info->NonPagedPoolUsage);
280 ok(check_with_margin(info.KernelPaged, sys_performance_info->PagedPoolUsage, 256),
281 "expected approximately %ld but got %d\n", info.KernelPaged, sys_performance_info->PagedPoolUsage);
283 ok(check_with_margin(info.KernelNonpaged, sys_performance_info->NonPagedPoolUsage, 16),
284 "expected approximately %ld but got %d\n", info.KernelNonpaged, sys_performance_info->NonPagedPoolUsage);
286 /* compare with values from SYSTEM_BASIC_INFORMATION */
287 size = 0;
288 status = pNtQuerySystemInformation(SystemBasicInformation, &sys_basic_info, sizeof(sys_basic_info), &size);
289 ok(status == STATUS_SUCCESS, "expected STATUS_SUCCESS, got %08x\n", status);
290 ok(size >= sizeof(SYSTEM_BASIC_INFORMATION), "incorrect length %d\n", size);
292 ok(info.PhysicalTotal == sys_basic_info.MmNumberOfPhysicalPages,
293 "expected info.PhysicalTotal=%u but got %u\n",
294 sys_basic_info.MmNumberOfPhysicalPages, (ULONG)info.PhysicalTotal);
296 ok(info.PageSize == sys_basic_info.PageSize,
297 "expected info.PageSize=%u but got %u\n",
298 sys_basic_info.PageSize, (ULONG)info.PageSize);
300 /* compare with values from SYSTEM_PROCESS_INFORMATION */
301 size = 0;
302 status = pNtQuerySystemInformation(SystemProcessInformation, NULL, 0, &size);
303 ok(status == STATUS_INFO_LENGTH_MISMATCH, "expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
304 ok(size > 0, "incorrect length %d\n", size);
305 while (status == STATUS_INFO_LENGTH_MISMATCH)
307 sys_process_info = HeapAlloc(GetProcessHeap(), 0, size);
308 ok(sys_process_info != NULL, "failed to allocate memory\n");
309 status = pNtQuerySystemInformation(SystemProcessInformation, sys_process_info, size, &size);
310 if (status == STATUS_SUCCESS) break;
311 HeapFree(GetProcessHeap(), 0, sys_process_info);
313 ok(status == STATUS_SUCCESS, "expected STATUS_SUCCESS, got %08x\n", status);
315 process_count = handle_count = thread_count = 0;
316 for (spi = sys_process_info;; spi = (SYSTEM_PROCESS_INFORMATION *)(((char *)spi) + spi->NextEntryOffset))
318 process_count++;
319 handle_count += spi->HandleCount;
320 thread_count += spi->dwThreadCount;
321 if (spi->NextEntryOffset == 0) break;
323 HeapFree(GetProcessHeap(), 0, sys_process_info);
325 ok(check_with_margin(info.HandleCount, handle_count, 24),
326 "expected approximately %d but got %d\n", info.HandleCount, handle_count);
328 ok(check_with_margin(info.ProcessCount, process_count, 4),
329 "expected approximately %d but got %d\n", info.ProcessCount, process_count);
331 ok(check_with_margin(info.ThreadCount, thread_count, 4),
332 "expected approximately %d but got %d\n", info.ThreadCount, thread_count);
337 static void test_GetProcessMemoryInfo(void)
339 PROCESS_MEMORY_COUNTERS pmc;
340 DWORD ret;
342 SetLastError(0xdeadbeef);
343 ret = GetProcessMemoryInfo(NULL, &pmc, sizeof(pmc));
344 ok(!ret, "GetProcessMemoryInfo should fail\n");
345 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
347 SetLastError(0xdeadbeef);
348 ret = GetProcessMemoryInfo(hpSR, &pmc, sizeof(pmc));
349 ok(!ret, "GetProcessMemoryInfo should fail\n");
350 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
352 SetLastError(0xdeadbeef);
353 ret = GetProcessMemoryInfo(hpQI, &pmc, sizeof(pmc)-1);
354 ok(!ret, "GetProcessMemoryInfo should fail\n");
355 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
357 SetLastError(0xdeadbeef);
358 ret = GetProcessMemoryInfo(hpQI, &pmc, sizeof(pmc));
359 ok(ret == 1, "failed with %d\n", GetLastError());
362 static BOOL nt_get_mapped_file_name(HANDLE process, LPVOID addr, LPWSTR name, DWORD len)
364 MEMORY_SECTION_NAME *section_name;
365 WCHAR *buf;
366 SIZE_T buf_len, ret_len;
367 NTSTATUS status;
369 if (!pNtQueryVirtualMemory) return FALSE;
371 buf_len = len * sizeof(WCHAR) + sizeof(MEMORY_SECTION_NAME);
372 buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buf_len);
374 ret_len = 0xdeadbeef;
375 status = pNtQueryVirtualMemory(process, addr, MemorySectionName, buf, buf_len, &ret_len);
376 todo_wine
377 ok(!status, "NtQueryVirtualMemory error %x\n", status);
378 /* FIXME: remove once Wine is fixed */
379 if (status)
381 HeapFree(GetProcessHeap(), 0, buf);
382 return FALSE;
385 section_name = (MEMORY_SECTION_NAME *)buf;
386 ok(ret_len == section_name->SectionFileName.MaximumLength + sizeof(*section_name), "got %lu, %u\n",
387 ret_len, section_name->SectionFileName.MaximumLength);
388 ok((char *)section_name->SectionFileName.Buffer == (char *)section_name + sizeof(*section_name), "got %p, %p\n",
389 section_name, section_name->SectionFileName.Buffer);
390 ok(section_name->SectionFileName.MaximumLength == section_name->SectionFileName.Length + sizeof(WCHAR), "got %u, %u\n",
391 section_name->SectionFileName.MaximumLength, section_name->SectionFileName.Length);
392 ok(section_name->SectionFileName.Length == lstrlenW(section_name->SectionFileName.Buffer) * sizeof(WCHAR), "got %u, %u\n",
393 section_name->SectionFileName.Length, lstrlenW(section_name->SectionFileName.Buffer));
395 memcpy(name, section_name->SectionFileName.Buffer, section_name->SectionFileName.MaximumLength);
396 HeapFree(GetProcessHeap(), 0, buf);
397 return TRUE;
400 static void test_GetMappedFileName(void)
402 HMODULE hMod = GetModuleHandleA(NULL);
403 char szMapPath[MAX_PATH], szModPath[MAX_PATH], *szMapBaseName;
404 DWORD ret;
405 char *base;
406 char temp_path[MAX_PATH], file_name[MAX_PATH], map_name[MAX_PATH], device_name[MAX_PATH], drive[3];
407 WCHAR map_nameW[MAX_PATH], nt_map_name[MAX_PATH];
408 HANDLE hfile, hmap;
410 SetLastError(0xdeadbeef);
411 ret = GetMappedFileNameA(NULL, hMod, szMapPath, sizeof(szMapPath));
412 ok(!ret, "GetMappedFileName should fail\n");
413 todo_wine
414 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
416 SetLastError(0xdeadbeef);
417 ret = GetMappedFileNameA(hpSR, hMod, szMapPath, sizeof(szMapPath));
418 ok(!ret, "GetMappedFileName should fail\n");
419 todo_wine
420 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
422 SetLastError( 0xdeadbeef );
423 ret = GetMappedFileNameA(hpQI, hMod, szMapPath, sizeof(szMapPath));
424 todo_wine
425 ok( ret || broken(GetLastError() == ERROR_UNEXP_NET_ERR), /* win2k */
426 "GetMappedFileNameA failed with error %u\n", GetLastError() );
427 if (ret)
429 ok(ret == strlen(szMapPath), "szMapPath=\"%s\" ret=%d\n", szMapPath, ret);
430 todo_wine
431 ok(szMapPath[0] == '\\', "szMapPath=\"%s\"\n", szMapPath);
432 szMapBaseName = strrchr(szMapPath, '\\'); /* That's close enough for us */
433 todo_wine
434 ok(szMapBaseName && *szMapBaseName, "szMapPath=\"%s\"\n", szMapPath);
435 if (szMapBaseName)
437 GetModuleFileNameA(NULL, szModPath, sizeof(szModPath));
438 ok(!strcmp(strrchr(szModPath, '\\'), szMapBaseName),
439 "szModPath=\"%s\" szMapBaseName=\"%s\"\n", szModPath, szMapBaseName);
443 GetTempPathA(MAX_PATH, temp_path);
444 GetTempFileNameA(temp_path, "map", 0, file_name);
446 drive[0] = file_name[0];
447 drive[1] = ':';
448 drive[2] = 0;
449 SetLastError(0xdeadbeef);
450 ret = QueryDosDeviceA(drive, device_name, sizeof(device_name));
451 ok(ret, "QueryDosDeviceA error %d\n", GetLastError());
452 trace("%s -> %s\n", drive, device_name);
454 SetLastError(0xdeadbeef);
455 hfile = CreateFileA(file_name, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
456 ok(hfile != INVALID_HANDLE_VALUE, "CreateFileA(%s) error %d\n", file_name, GetLastError());
457 SetFilePointer(hfile, 0x4000, NULL, FILE_BEGIN);
458 SetEndOfFile(hfile);
460 SetLastError(0xdeadbeef);
461 hmap = CreateFileMappingA(hfile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
462 ok(hmap != 0, "CreateFileMappingA error %d\n", GetLastError());
464 SetLastError(0xdeadbeef);
465 base = MapViewOfFile(hmap, FILE_MAP_READ, 0, 0, 0);
466 ok(base != NULL, "MapViewOfFile error %d\n", GetLastError());
468 SetLastError(0xdeadbeef);
469 ret = GetMappedFileNameA(GetCurrentProcess(), base, map_name, 0);
470 ok(!ret, "GetMappedFileName should fail\n");
471 todo_wine
472 ok(GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == ERROR_INSUFFICIENT_BUFFER,
473 "wrong error %d\n", GetLastError());
475 SetLastError(0xdeadbeef);
476 ret = GetMappedFileNameA(GetCurrentProcess(), base, 0, sizeof(map_name));
477 ok(!ret, "GetMappedFileName should fail\n");
478 todo_wine
479 ok(GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
481 SetLastError(0xdeadbeef);
482 ret = GetMappedFileNameA(GetCurrentProcess(), base, map_name, 1);
483 todo_wine
484 ok(ret == 1, "GetMappedFileName error %d\n", GetLastError());
485 ok(!map_name[0] || broken(map_name[0] == device_name[0]) /* before win2k */, "expected 0, got %c\n", map_name[0]);
487 SetLastError(0xdeadbeef);
488 ret = GetMappedFileNameA(GetCurrentProcess(), base, map_name, sizeof(map_name));
489 todo_wine {
490 ok(ret, "GetMappedFileName error %d\n", GetLastError());
491 ok(ret > strlen(device_name), "map_name should be longer than device_name\n");
492 ok(memcmp(map_name, device_name, strlen(device_name)) == 0, "map name does not start with a device name: %s\n", map_name);
495 SetLastError(0xdeadbeef);
496 ret = GetMappedFileNameW(GetCurrentProcess(), base, map_nameW, ARRAY_SIZE(map_nameW));
497 todo_wine {
498 ok(ret, "GetMappedFileNameW error %d\n", GetLastError());
499 ok(ret > strlen(device_name), "map_name should be longer than device_name\n");
501 if (nt_get_mapped_file_name(GetCurrentProcess(), base, nt_map_name, ARRAY_SIZE(nt_map_name)))
503 ok(memcmp(map_nameW, nt_map_name, lstrlenW(map_nameW)) == 0, "map name does not start with a device name: %s\n", map_name);
504 WideCharToMultiByte(CP_ACP, 0, map_nameW, -1, map_name, MAX_PATH, NULL, NULL);
505 ok(memcmp(map_name, device_name, strlen(device_name)) == 0, "map name does not start with a device name: %s\n", map_name);
508 SetLastError(0xdeadbeef);
509 ret = GetMappedFileNameA(GetCurrentProcess(), base + 0x2000, map_name, sizeof(map_name));
510 todo_wine {
511 ok(ret, "GetMappedFileName error %d\n", GetLastError());
512 ok(ret > strlen(device_name), "map_name should be longer than device_name\n");
513 ok(memcmp(map_name, device_name, strlen(device_name)) == 0, "map name does not start with a device name: %s\n", map_name);
516 SetLastError(0xdeadbeef);
517 ret = GetMappedFileNameA(GetCurrentProcess(), base + 0x4000, map_name, sizeof(map_name));
518 ok(!ret, "GetMappedFileName should fail\n");
519 todo_wine
520 ok(GetLastError() == ERROR_UNEXP_NET_ERR, "expected ERROR_UNEXP_NET_ERR, got %d\n", GetLastError());
522 SetLastError(0xdeadbeef);
523 ret = GetMappedFileNameA(GetCurrentProcess(), NULL, map_name, sizeof(map_name));
524 ok(!ret, "GetMappedFileName should fail\n");
525 todo_wine
526 ok(GetLastError() == ERROR_UNEXP_NET_ERR, "expected ERROR_UNEXP_NET_ERR, got %d\n", GetLastError());
528 SetLastError(0xdeadbeef);
529 ret = GetMappedFileNameA(0, base, map_name, sizeof(map_name));
530 ok(!ret, "GetMappedFileName should fail\n");
531 todo_wine
532 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
534 UnmapViewOfFile(base);
535 CloseHandle(hmap);
536 CloseHandle(hfile);
537 DeleteFileA(file_name);
539 SetLastError(0xdeadbeef);
540 hmap = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READONLY | SEC_COMMIT, 0, 4096, NULL);
541 ok(hmap != 0, "CreateFileMappingA error %d\n", GetLastError());
543 SetLastError(0xdeadbeef);
544 base = MapViewOfFile(hmap, FILE_MAP_READ, 0, 0, 0);
545 ok(base != NULL, "MapViewOfFile error %d\n", GetLastError());
547 SetLastError(0xdeadbeef);
548 ret = GetMappedFileNameA(GetCurrentProcess(), base, map_name, sizeof(map_name));
549 ok(!ret, "GetMappedFileName should fail\n");
550 todo_wine
551 ok(GetLastError() == ERROR_FILE_INVALID, "expected ERROR_FILE_INVALID, got %d\n", GetLastError());
553 UnmapViewOfFile(base);
554 CloseHandle(hmap);
557 static void test_GetProcessImageFileName(void)
559 HMODULE hMod = GetModuleHandleA(NULL);
560 char szImgPath[MAX_PATH], szMapPath[MAX_PATH];
561 WCHAR szImgPathW[MAX_PATH];
562 DWORD ret, ret1;
564 /* This function is available on WinXP+ only */
565 SetLastError(0xdeadbeef);
566 if(!GetProcessImageFileNameA(hpQI, szImgPath, sizeof(szImgPath)))
568 if(GetLastError() == ERROR_INVALID_FUNCTION) {
569 win_skip("GetProcessImageFileName not implemented\n");
570 return;
573 if(GetLastError() == 0xdeadbeef)
574 todo_wine ok(0, "failed without error code\n");
575 else
576 todo_wine ok(0, "failed with %d\n", GetLastError());
579 SetLastError(0xdeadbeef);
580 GetProcessImageFileNameA(NULL, szImgPath, sizeof(szImgPath));
581 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
583 SetLastError(0xdeadbeef);
584 GetProcessImageFileNameA(hpSR, szImgPath, sizeof(szImgPath));
585 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
587 SetLastError(0xdeadbeef);
588 GetProcessImageFileNameA(hpQI, szImgPath, 0);
589 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
591 ret = GetProcessImageFileNameA(hpQI, szImgPath, sizeof(szImgPath));
592 ret1 = GetMappedFileNameA(hpQV, hMod, szMapPath, sizeof(szMapPath));
593 if(ret && ret1)
595 /* Windows returns 2*strlen-1 */
596 todo_wine ok(ret >= strlen(szImgPath), "szImgPath=\"%s\" ret=%d\n", szImgPath, ret);
597 todo_wine ok(!strcmp(szImgPath, szMapPath), "szImgPath=\"%s\" szMapPath=\"%s\"\n", szImgPath, szMapPath);
600 SetLastError(0xdeadbeef);
601 GetProcessImageFileNameW(NULL, szImgPathW, ARRAY_SIZE(szImgPathW));
602 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
604 /* no information about correct buffer size returned: */
605 SetLastError(0xdeadbeef);
606 GetProcessImageFileNameW(hpQI, szImgPathW, 0);
607 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
609 SetLastError(0xdeadbeef);
610 GetProcessImageFileNameW(hpQI, NULL, 0);
611 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
613 /* correct call */
614 memset(szImgPathW, 0xff, sizeof(szImgPathW));
615 ret = GetProcessImageFileNameW(hpQI, szImgPathW, ARRAY_SIZE(szImgPathW));
616 ok(ret > 0, "GetProcessImageFileNameW should have succeeded.\n");
617 ok(szImgPathW[0] == '\\', "GetProcessImageFileNameW should have returned an NT path.\n");
618 ok(lstrlenW(szImgPathW) == ret, "Expected length to be %d, got %d\n", ret, lstrlenW(szImgPathW));
620 /* boundary values of 'size' */
621 SetLastError(0xdeadbeef);
622 GetProcessImageFileNameW(hpQI, szImgPathW, ret);
623 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
625 memset(szImgPathW, 0xff, sizeof(szImgPathW));
626 ret = GetProcessImageFileNameW(hpQI, szImgPathW, ret + 1);
627 ok(ret > 0, "GetProcessImageFileNameW should have succeeded.\n");
628 ok(szImgPathW[0] == '\\', "GetProcessImageFileNameW should have returned an NT path.\n");
629 ok(lstrlenW(szImgPathW) == ret, "Expected length to be %d, got %d\n", ret, lstrlenW(szImgPathW));
632 static void test_GetModuleFileNameEx(void)
634 HMODULE hMod = GetModuleHandleA(NULL);
635 char szModExPath[MAX_PATH+1], szModPath[MAX_PATH+1];
636 WCHAR buffer[MAX_PATH];
637 DWORD ret;
639 SetLastError(0xdeadbeef);
640 ret = GetModuleFileNameExA(NULL, hMod, szModExPath, sizeof(szModExPath));
641 ok( !ret, "GetModuleFileNameExA succeeded\n" );
642 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
644 SetLastError(0xdeadbeef);
645 ret = GetModuleFileNameExA(hpQI, hMod, szModExPath, sizeof(szModExPath));
646 ok( !ret, "GetModuleFileNameExA succeeded\n" );
647 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
649 SetLastError(0xdeadbeef);
650 ret = GetModuleFileNameExA(hpQV, hBad, szModExPath, sizeof(szModExPath));
651 ok( !ret, "GetModuleFileNameExA succeeded\n" );
652 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
654 ret = GetModuleFileNameExA(hpQV, NULL, szModExPath, sizeof(szModExPath));
655 if(!ret)
656 return;
657 ok(ret == strlen(szModExPath), "szModExPath=\"%s\" ret=%d\n", szModExPath, ret);
658 GetModuleFileNameA(NULL, szModPath, sizeof(szModPath));
659 ok(!strncmp(szModExPath, szModPath, MAX_PATH),
660 "szModExPath=\"%s\" szModPath=\"%s\"\n", szModExPath, szModPath);
662 SetLastError(0xdeadbeef);
663 memset( szModExPath, 0xcc, sizeof(szModExPath) );
664 ret = GetModuleFileNameExA(hpQV, NULL, szModExPath, 4 );
665 ok( ret == 4 || ret == strlen(szModExPath), "wrong length %u\n", ret );
666 ok( broken(szModExPath[3]) /*w2kpro*/ || strlen(szModExPath) == 3,
667 "szModExPath=\"%s\" ret=%d\n", szModExPath, ret );
668 ok(GetLastError() == 0xdeadbeef, "got error %d\n", GetLastError());
670 if (0) /* crashes on Windows 10 */
672 SetLastError(0xdeadbeef);
673 ret = GetModuleFileNameExA(hpQV, NULL, szModExPath, 0 );
674 ok( ret == 0, "wrong length %u\n", ret );
675 ok(GetLastError() == ERROR_INVALID_PARAMETER, "got error %d\n", GetLastError());
678 SetLastError(0xdeadbeef);
679 memset( buffer, 0xcc, sizeof(buffer) );
680 ret = GetModuleFileNameExW(hpQV, NULL, buffer, 4 );
681 ok( ret == 4 || ret == lstrlenW(buffer), "wrong length %u\n", ret );
682 ok( broken(buffer[3]) /*w2kpro*/ || lstrlenW(buffer) == 3,
683 "buffer=%s ret=%d\n", wine_dbgstr_w(buffer), ret );
684 ok(GetLastError() == 0xdeadbeef, "got error %d\n", GetLastError());
686 if (0) /* crashes on Windows 10 */
688 SetLastError(0xdeadbeef);
689 buffer[0] = 0xcc;
690 ret = GetModuleFileNameExW(hpQV, NULL, buffer, 0 );
691 ok( ret == 0, "wrong length %u\n", ret );
692 ok(GetLastError() == 0xdeadbeef, "got error %d\n", GetLastError());
693 ok( buffer[0] == 0xcc, "buffer modified %s\n", wine_dbgstr_w(buffer) );
697 static void test_GetModuleBaseName(void)
699 HMODULE hMod = GetModuleHandleA(NULL);
700 char szModPath[MAX_PATH], szModBaseName[MAX_PATH];
701 DWORD ret;
703 SetLastError(0xdeadbeef);
704 GetModuleBaseNameA(NULL, hMod, szModBaseName, sizeof(szModBaseName));
705 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
707 SetLastError(0xdeadbeef);
708 GetModuleBaseNameA(hpQI, hMod, szModBaseName, sizeof(szModBaseName));
709 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
711 SetLastError(0xdeadbeef);
712 GetModuleBaseNameA(hpQV, hBad, szModBaseName, sizeof(szModBaseName));
713 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
715 ret = GetModuleBaseNameA(hpQV, NULL, szModBaseName, sizeof(szModBaseName));
716 if(!ret)
717 return;
718 ok(ret == strlen(szModBaseName), "szModBaseName=\"%s\" ret=%d\n", szModBaseName, ret);
719 GetModuleFileNameA(NULL, szModPath, sizeof(szModPath));
720 ok(!strcmp(strrchr(szModPath, '\\') + 1, szModBaseName),
721 "szModPath=\"%s\" szModBaseName=\"%s\"\n", szModPath, szModBaseName);
724 static void test_ws_functions(void)
726 PSAPI_WS_WATCH_INFORMATION wswi[4096];
727 ULONG_PTR pages[4096];
728 HANDLE ws_handle;
729 char *addr;
730 unsigned int i;
731 BOOL ret;
733 ws_handle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_SET_QUOTA |
734 PROCESS_SET_INFORMATION, FALSE, GetCurrentProcessId());
735 ok(!!ws_handle, "got error %u\n", GetLastError());
737 SetLastError(0xdeadbeef);
738 EmptyWorkingSet(NULL);
739 todo_wine ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
741 SetLastError(0xdeadbeef);
742 EmptyWorkingSet(hpSR);
743 todo_wine ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
745 SetLastError(0xdeadbeef);
746 ret = EmptyWorkingSet(ws_handle);
747 ok(ret == 1, "failed with %d\n", GetLastError());
749 SetLastError( 0xdeadbeef );
750 ret = InitializeProcessForWsWatch( NULL );
751 todo_wine ok( !ret, "InitializeProcessForWsWatch succeeded\n" );
752 if (!ret)
754 if (GetLastError() == ERROR_INVALID_FUNCTION) /* not supported on xp in wow64 mode */
756 trace( "InitializeProcessForWsWatch not supported\n" );
757 return;
759 ok( GetLastError() == ERROR_INVALID_HANDLE, "wrong error %u\n", GetLastError() );
761 SetLastError(0xdeadbeef);
762 ret = InitializeProcessForWsWatch(ws_handle);
763 ok(ret == 1, "failed with %d\n", GetLastError());
765 addr = VirtualAlloc(NULL, 1, MEM_COMMIT, PAGE_READWRITE);
766 if(!addr)
767 return;
769 *addr = 0; /* make sure it's paged in (needed on wow64) */
770 if(!VirtualLock(addr, 1))
772 trace("locking failed (error=%d) - skipping test\n", GetLastError());
773 goto free_page;
776 SetLastError(0xdeadbeef);
777 ret = QueryWorkingSet(hpQI, pages, 4096 * sizeof(ULONG_PTR));
778 todo_wine ok(ret == 1, "failed with %d\n", GetLastError());
779 if(ret == 1)
781 for(i = 0; i < pages[0]; i++)
782 if((pages[i+1] & ~0xfffL) == (ULONG_PTR)addr)
784 todo_wine ok(ret == 1, "QueryWorkingSet found our page\n");
785 goto test_gwsc;
788 todo_wine ok(0, "QueryWorkingSet didn't find our page\n");
791 test_gwsc:
792 SetLastError(0xdeadbeef);
793 ret = GetWsChanges(hpQI, wswi, sizeof(wswi));
794 todo_wine ok(ret == 1, "failed with %d\n", GetLastError());
795 if(ret == 1)
797 for(i = 0; wswi[i].FaultingVa; i++)
798 if(((ULONG_PTR)wswi[i].FaultingVa & ~0xfffL) == (ULONG_PTR)addr)
800 todo_wine ok(ret == 1, "GetWsChanges found our page\n");
801 goto free_page;
804 todo_wine ok(0, "GetWsChanges didn't find our page\n");
807 free_page:
808 VirtualFree(addr, 0, MEM_RELEASE);
811 START_TEST(psapi_main)
813 DWORD pid = GetCurrentProcessId();
815 init_func_ptrs();
817 if (pIsWow64Process)
818 IsWow64Process(GetCurrentProcess(), &wow64);
820 hpSR = OpenProcess(STANDARD_RIGHTS_REQUIRED, FALSE, pid);
821 ok(!!hpSR, "got error %u\n", GetLastError());
822 hpQI = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
823 ok(!!hpQI, "got error %u\n", GetLastError());
824 hpVR = OpenProcess(PROCESS_VM_READ, FALSE, pid);
825 ok(!!hpVR, "got error %u\n", GetLastError());
826 hpQV = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
827 ok(!!hpQV, "got error %u\n", GetLastError());
829 test_EnumProcesses();
830 test_EnumProcessModules();
831 test_GetModuleInformation();
832 test_GetPerformanceInfo();
833 test_GetProcessMemoryInfo();
834 test_GetMappedFileName();
835 test_GetProcessImageFileName();
836 test_GetModuleFileNameEx();
837 test_GetModuleBaseName();
838 test_ws_functions();
840 CloseHandle(hpSR);
841 CloseHandle(hpQI);
842 CloseHandle(hpVR);
843 CloseHandle(hpQV);