mfplat: Read queue subscriber within the critical section.
[wine/zf.git] / dlls / psapi / tests / psapi_main.c
blob8f94e0b5798dc521d8f0ac32fb3afa810797d6ae
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 #include <stdarg.h>
24 #include "ntstatus.h"
25 #define WIN32_NO_STATUS
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winreg.h"
30 #include "winnt.h"
31 #include "winternl.h"
32 #include "winnls.h"
33 #include "winuser.h"
34 #define PSAPI_VERSION 1
35 #include "psapi.h"
36 #include "wine/test.h"
38 static NTSTATUS (WINAPI *pNtQuerySystemInformation)(SYSTEM_INFORMATION_CLASS, PVOID, ULONG, PULONG);
39 static NTSTATUS (WINAPI *pNtQueryVirtualMemory)(HANDLE, LPCVOID, MEMORY_INFORMATION_CLASS, PVOID, SIZE_T, SIZE_T *);
40 static BOOL (WINAPI *pIsWow64Process)(HANDLE, BOOL *);
41 static BOOL (WINAPI *pWow64DisableWow64FsRedirection)(void **);
42 static BOOL (WINAPI *pWow64RevertWow64FsRedirection)(void *);
43 static BOOL (WINAPI *pQueryWorkingSetEx)(HANDLE, PVOID, DWORD);
45 static BOOL wow64;
47 static BOOL init_func_ptrs(void)
49 pNtQuerySystemInformation = (void *)GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtQuerySystemInformation");
50 pNtQueryVirtualMemory = (void *)GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtQueryVirtualMemory");
51 pIsWow64Process = (void *)GetProcAddress(GetModuleHandleA("kernel32.dll"), "IsWow64Process");
52 pWow64DisableWow64FsRedirection = (void *)GetProcAddress(GetModuleHandleA("kernel32.dll"), "Wow64DisableWow64FsRedirection");
53 pWow64RevertWow64FsRedirection = (void *)GetProcAddress(GetModuleHandleA("kernel32.dll"), "Wow64RevertWow64FsRedirection");
54 pQueryWorkingSetEx = (void *)GetProcAddress(GetModuleHandleA("psapi.dll"), "QueryWorkingSetEx");
55 return TRUE;
58 static HANDLE hpSR, hpQI, hpVR, hpQV;
59 static const HANDLE hBad = (HANDLE)0xdeadbeef;
61 static void test_EnumProcesses(void)
63 DWORD pid, ret, cbUsed = 0xdeadbeef;
65 SetLastError(0xdeadbeef);
66 ret = EnumProcesses(NULL, 0, &cbUsed);
67 ok(ret == 1, "failed with %d\n", GetLastError());
68 ok(cbUsed == 0, "cbUsed=%d\n", cbUsed);
70 SetLastError(0xdeadbeef);
71 ret = EnumProcesses(&pid, 4, &cbUsed);
72 ok(ret == 1, "failed with %d\n", GetLastError());
73 ok(cbUsed == 4, "cbUsed=%d\n", cbUsed);
76 static void test_EnumProcessModules(void)
78 char buffer[200] = "C:\\windows\\system32\\notepad.exe";
79 PROCESS_INFORMATION pi = {0};
80 STARTUPINFOA si = {0};
81 void *cookie;
82 HMODULE hMod;
83 DWORD ret, cbNeeded = 0xdeadbeef;
85 SetLastError(0xdeadbeef);
86 EnumProcessModules(NULL, NULL, 0, &cbNeeded);
87 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
89 SetLastError(0xdeadbeef);
90 EnumProcessModules(hpQI, NULL, 0, &cbNeeded);
91 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
93 SetLastError(0xdeadbeef);
94 hMod = (void *)0xdeadbeef;
95 ret = EnumProcessModules(hpQI, &hMod, sizeof(HMODULE), NULL);
96 ok(!ret, "succeeded\n");
97 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
99 SetLastError(0xdeadbeef);
100 hMod = (void *)0xdeadbeef;
101 ret = EnumProcessModules(hpQV, &hMod, sizeof(HMODULE), NULL);
102 ok(!ret, "succeeded\n");
103 ok(GetLastError() == ERROR_NOACCESS, "expected error=ERROR_NOACCESS but got %d\n", GetLastError());
104 ok(hMod == GetModuleHandleA(NULL),
105 "hMod=%p GetModuleHandleA(NULL)=%p\n", hMod, GetModuleHandleA(NULL));
107 SetLastError(0xdeadbeef);
108 ret = EnumProcessModules(hpQV, NULL, 0, &cbNeeded);
109 ok(ret == 1, "failed with %d\n", GetLastError());
111 SetLastError(0xdeadbeef);
112 ret = EnumProcessModules(hpQV, NULL, sizeof(HMODULE), &cbNeeded);
113 ok(!ret, "succeeded\n");
114 ok(GetLastError() == ERROR_NOACCESS, "expected error=ERROR_NOACCESS but got %d\n", GetLastError());
116 SetLastError(0xdeadbeef);
117 hMod = (void *)0xdeadbeef;
118 ret = EnumProcessModules(hpQV, &hMod, sizeof(HMODULE), &cbNeeded);
119 ok(ret == 1, "got %d, failed with %d\n", ret, GetLastError());
120 ok(hMod == GetModuleHandleA(NULL),
121 "hMod=%p GetModuleHandleA(NULL)=%p\n", hMod, GetModuleHandleA(NULL));
122 ok(cbNeeded % sizeof(hMod) == 0, "not a multiple of sizeof(HMODULE) cbNeeded=%d\n", cbNeeded);
124 ret = CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
125 ok(ret, "CreateProcess failed: %u\n", GetLastError());
127 ret = WaitForInputIdle(pi.hProcess, 5000);
128 ok(!ret, "wait timed out\n");
130 SetLastError(0xdeadbeef);
131 hMod = NULL;
132 ret = EnumProcessModules(pi.hProcess, &hMod, sizeof(HMODULE), &cbNeeded);
133 ok(ret == 1, "got %d, error %u\n", ret, GetLastError());
134 ok(!!hMod, "expected non-NULL module\n");
135 ok(cbNeeded % sizeof(hMod) == 0, "got %u\n", cbNeeded);
137 TerminateProcess(pi.hProcess, 0);
139 if (sizeof(void *) == 8)
141 MODULEINFO info;
142 char name[40];
144 strcpy(buffer, "C:\\windows\\syswow64\\notepad.exe");
145 ret = CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
146 ok(ret, "CreateProcess failed: %u\n", GetLastError());
148 ret = WaitForInputIdle(pi.hProcess, 5000);
149 ok(!ret, "wait timed out\n");
151 SetLastError(0xdeadbeef);
152 hMod = NULL;
153 ret = EnumProcessModules(pi.hProcess, &hMod, sizeof(HMODULE), &cbNeeded);
154 ok(ret == 1, "got %d, error %u\n", ret, GetLastError());
155 ok(!!hMod, "expected non-NULL module\n");
156 ok(cbNeeded % sizeof(hMod) == 0, "got %u\n", cbNeeded);
158 ret = GetModuleBaseNameA(pi.hProcess, hMod, name, sizeof(name));
159 ok(ret, "got error %u\n", GetLastError());
160 ok(!strcmp(name, "notepad.exe"), "got %s\n", name);
162 ret = GetModuleFileNameExA(pi.hProcess, hMod, name, sizeof(name));
163 ok(ret, "got error %u\n", GetLastError());
164 todo_wine
165 ok(!strcmp(name, buffer), "got %s\n", name);
167 ret = GetModuleInformation(pi.hProcess, hMod, &info, sizeof(info));
168 ok(ret, "got error %u\n", GetLastError());
169 ok(info.lpBaseOfDll == hMod, "expected %p, got %p\n", hMod, info.lpBaseOfDll);
170 ok(info.SizeOfImage, "image size was 0\n");
171 ok(info.EntryPoint >= info.lpBaseOfDll, "got entry point %p\n", info.EntryPoint);
173 TerminateProcess(pi.hProcess, 0);
175 else if (wow64)
177 pWow64DisableWow64FsRedirection(&cookie);
178 ret = CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
179 pWow64RevertWow64FsRedirection(cookie);
180 ok(ret, "CreateProcess failed: %u\n", GetLastError());
182 ret = WaitForInputIdle(pi.hProcess, 5000);
183 ok(!ret, "wait timed out\n");
185 SetLastError(0xdeadbeef);
186 ret = EnumProcessModules(pi.hProcess, &hMod, sizeof(HMODULE), &cbNeeded);
187 ok(!ret, "got %d\n", ret);
188 todo_wine
189 ok(GetLastError() == ERROR_PARTIAL_COPY, "got error %u\n", GetLastError());
191 TerminateProcess(pi.hProcess, 0);
195 static void test_GetModuleInformation(void)
197 HMODULE hMod = GetModuleHandleA(NULL);
198 MODULEINFO info;
199 DWORD ret;
201 SetLastError(0xdeadbeef);
202 GetModuleInformation(NULL, hMod, &info, sizeof(info));
203 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
205 SetLastError(0xdeadbeef);
206 GetModuleInformation(hpQI, hMod, &info, sizeof(info));
207 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
209 SetLastError(0xdeadbeef);
210 GetModuleInformation(hpQV, hBad, &info, sizeof(info));
211 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
213 SetLastError(0xdeadbeef);
214 GetModuleInformation(hpQV, hMod, &info, sizeof(info)-1);
215 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
217 SetLastError(0xdeadbeef);
218 ret = GetModuleInformation(hpQV, hMod, &info, sizeof(info));
219 ok(ret == 1, "failed with %d\n", GetLastError());
220 ok(info.lpBaseOfDll == hMod, "lpBaseOfDll=%p hMod=%p\n", info.lpBaseOfDll, hMod);
223 static BOOL check_with_margin(SIZE_T perf, SIZE_T sysperf, int margin)
225 return (perf >= max(sysperf, margin) - margin && perf <= sysperf + margin);
228 static void test_GetPerformanceInfo(void)
230 PERFORMANCE_INFORMATION info;
231 NTSTATUS status;
232 DWORD size;
233 BOOL ret;
235 SetLastError(0xdeadbeef);
236 ret = GetPerformanceInfo(&info, sizeof(info)-1);
237 ok(!ret, "GetPerformanceInfo unexpectedly succeeded\n");
238 ok(GetLastError() == ERROR_BAD_LENGTH, "expected error=ERROR_BAD_LENGTH but got %d\n", GetLastError());
240 if (!pNtQuerySystemInformation)
241 win_skip("NtQuerySystemInformation not found, skipping tests\n");
242 else
244 char performance_buffer[sizeof(SYSTEM_PERFORMANCE_INFORMATION) + 16]; /* larger on w2k8/win7 */
245 SYSTEM_PERFORMANCE_INFORMATION *sys_performance_info = (SYSTEM_PERFORMANCE_INFORMATION *)performance_buffer;
246 SYSTEM_PROCESS_INFORMATION *sys_process_info = NULL, *spi;
247 SYSTEM_BASIC_INFORMATION sys_basic_info;
248 DWORD process_count, handle_count, thread_count;
250 /* compare with values from SYSTEM_PERFORMANCE_INFORMATION */
251 size = 0;
252 status = pNtQuerySystemInformation(SystemPerformanceInformation, sys_performance_info, sizeof(performance_buffer), &size);
253 ok(status == STATUS_SUCCESS, "expected STATUS_SUCCESS, got %08x\n", status);
254 ok(size >= sizeof(SYSTEM_PERFORMANCE_INFORMATION), "incorrect length %d\n", size);
256 SetLastError(0xdeadbeef);
257 ret = GetPerformanceInfo(&info, sizeof(info));
258 ok(ret, "GetPerformanceInfo failed with %d\n", GetLastError());
259 ok(info.cb == sizeof(PERFORMANCE_INFORMATION), "got %d\n", info.cb);
261 ok(check_with_margin(info.CommitTotal, sys_performance_info->TotalCommittedPages, 2048),
262 "expected approximately %ld but got %d\n", info.CommitTotal, sys_performance_info->TotalCommittedPages);
264 ok(check_with_margin(info.CommitLimit, sys_performance_info->TotalCommitLimit, 32),
265 "expected approximately %ld but got %d\n", info.CommitLimit, sys_performance_info->TotalCommitLimit);
267 ok(check_with_margin(info.CommitPeak, sys_performance_info->PeakCommitment, 32),
268 "expected approximately %ld but got %d\n", info.CommitPeak, sys_performance_info->PeakCommitment);
270 ok(check_with_margin(info.PhysicalAvailable, sys_performance_info->AvailablePages, 2048),
271 "expected approximately %ld but got %d\n", info.PhysicalAvailable, sys_performance_info->AvailablePages);
273 /* TODO: info.SystemCache not checked yet - to which field(s) does this value correspond to? */
275 ok(check_with_margin(info.KernelTotal, sys_performance_info->PagedPoolUsage + sys_performance_info->NonPagedPoolUsage, 256),
276 "expected approximately %ld but got %d\n", info.KernelTotal,
277 sys_performance_info->PagedPoolUsage + sys_performance_info->NonPagedPoolUsage);
279 ok(check_with_margin(info.KernelPaged, sys_performance_info->PagedPoolUsage, 256),
280 "expected approximately %ld but got %d\n", info.KernelPaged, sys_performance_info->PagedPoolUsage);
282 ok(check_with_margin(info.KernelNonpaged, sys_performance_info->NonPagedPoolUsage, 16),
283 "expected approximately %ld but got %d\n", info.KernelNonpaged, sys_performance_info->NonPagedPoolUsage);
285 /* compare with values from SYSTEM_BASIC_INFORMATION */
286 size = 0;
287 status = pNtQuerySystemInformation(SystemBasicInformation, &sys_basic_info, sizeof(sys_basic_info), &size);
288 ok(status == STATUS_SUCCESS, "expected STATUS_SUCCESS, got %08x\n", status);
289 ok(size >= sizeof(SYSTEM_BASIC_INFORMATION), "incorrect length %d\n", size);
291 ok(info.PhysicalTotal == sys_basic_info.MmNumberOfPhysicalPages,
292 "expected info.PhysicalTotal=%u but got %u\n",
293 sys_basic_info.MmNumberOfPhysicalPages, (ULONG)info.PhysicalTotal);
295 ok(info.PageSize == sys_basic_info.PageSize,
296 "expected info.PageSize=%u but got %u\n",
297 sys_basic_info.PageSize, (ULONG)info.PageSize);
299 /* compare with values from SYSTEM_PROCESS_INFORMATION */
300 size = 0;
301 status = pNtQuerySystemInformation(SystemProcessInformation, NULL, 0, &size);
302 ok(status == STATUS_INFO_LENGTH_MISMATCH, "expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
303 ok(size > 0, "incorrect length %d\n", size);
304 while (status == STATUS_INFO_LENGTH_MISMATCH)
306 sys_process_info = HeapAlloc(GetProcessHeap(), 0, size);
307 ok(sys_process_info != NULL, "failed to allocate memory\n");
308 status = pNtQuerySystemInformation(SystemProcessInformation, sys_process_info, size, &size);
309 if (status == STATUS_SUCCESS) break;
310 HeapFree(GetProcessHeap(), 0, sys_process_info);
312 ok(status == STATUS_SUCCESS, "expected STATUS_SUCCESS, got %08x\n", status);
314 process_count = handle_count = thread_count = 0;
315 for (spi = sys_process_info;; spi = (SYSTEM_PROCESS_INFORMATION *)(((char *)spi) + spi->NextEntryOffset))
317 process_count++;
318 handle_count += spi->HandleCount;
319 thread_count += spi->dwThreadCount;
320 if (spi->NextEntryOffset == 0) break;
322 HeapFree(GetProcessHeap(), 0, sys_process_info);
324 ok(check_with_margin(info.HandleCount, handle_count, 512),
325 "expected approximately %d but got %d\n", info.HandleCount, handle_count);
327 ok(check_with_margin(info.ProcessCount, process_count, 4),
328 "expected approximately %d but got %d\n", info.ProcessCount, process_count);
330 ok(check_with_margin(info.ThreadCount, thread_count, 4),
331 "expected approximately %d but got %d\n", info.ThreadCount, thread_count);
336 static void test_GetProcessMemoryInfo(void)
338 PROCESS_MEMORY_COUNTERS pmc;
339 DWORD ret;
341 SetLastError(0xdeadbeef);
342 ret = GetProcessMemoryInfo(NULL, &pmc, sizeof(pmc));
343 ok(!ret, "GetProcessMemoryInfo should fail\n");
344 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
346 SetLastError(0xdeadbeef);
347 ret = GetProcessMemoryInfo(hpSR, &pmc, sizeof(pmc));
348 ok(!ret, "GetProcessMemoryInfo should fail\n");
349 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
351 SetLastError(0xdeadbeef);
352 ret = GetProcessMemoryInfo(hpQI, &pmc, sizeof(pmc)-1);
353 ok(!ret, "GetProcessMemoryInfo should fail\n");
354 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
356 SetLastError(0xdeadbeef);
357 ret = GetProcessMemoryInfo(hpQI, &pmc, sizeof(pmc));
358 ok(ret == 1, "failed with %d\n", GetLastError());
361 static BOOL nt_get_mapped_file_name(HANDLE process, LPVOID addr, LPWSTR name, DWORD len)
363 MEMORY_SECTION_NAME *section_name;
364 WCHAR *buf;
365 SIZE_T buf_len, ret_len;
366 NTSTATUS status;
368 if (!pNtQueryVirtualMemory) return FALSE;
370 buf_len = len * sizeof(WCHAR) + sizeof(MEMORY_SECTION_NAME);
371 buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buf_len);
373 ret_len = 0xdeadbeef;
374 status = pNtQueryVirtualMemory(process, addr, MemorySectionName, buf, buf_len, &ret_len);
375 ok(!status, "NtQueryVirtualMemory error %x\n", status);
377 section_name = (MEMORY_SECTION_NAME *)buf;
378 ok(ret_len == section_name->SectionFileName.MaximumLength + sizeof(*section_name), "got %lu, %u\n",
379 ret_len, section_name->SectionFileName.MaximumLength);
380 ok((char *)section_name->SectionFileName.Buffer == (char *)section_name + sizeof(*section_name), "got %p, %p\n",
381 section_name, section_name->SectionFileName.Buffer);
382 ok(section_name->SectionFileName.MaximumLength == section_name->SectionFileName.Length + sizeof(WCHAR), "got %u, %u\n",
383 section_name->SectionFileName.MaximumLength, section_name->SectionFileName.Length);
384 ok(section_name->SectionFileName.Length == lstrlenW(section_name->SectionFileName.Buffer) * sizeof(WCHAR), "got %u, %u\n",
385 section_name->SectionFileName.Length, lstrlenW(section_name->SectionFileName.Buffer));
387 memcpy(name, section_name->SectionFileName.Buffer, section_name->SectionFileName.MaximumLength);
388 HeapFree(GetProcessHeap(), 0, buf);
389 return TRUE;
392 static void test_GetMappedFileName(void)
394 HMODULE hMod = GetModuleHandleA(NULL);
395 char szMapPath[MAX_PATH], szModPath[MAX_PATH], *szMapBaseName;
396 DWORD ret;
397 char *base;
398 char temp_path[MAX_PATH], file_name[MAX_PATH], map_name[MAX_PATH], device_name[MAX_PATH], drive[3];
399 WCHAR map_nameW[MAX_PATH], nt_map_name[MAX_PATH];
400 HANDLE hfile, hmap;
402 SetLastError(0xdeadbeef);
403 ret = GetMappedFileNameA(NULL, hMod, szMapPath, sizeof(szMapPath));
404 ok(!ret, "GetMappedFileName should fail\n");
405 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
407 SetLastError(0xdeadbeef);
408 ret = GetMappedFileNameA(hpSR, hMod, szMapPath, sizeof(szMapPath));
409 ok(!ret, "GetMappedFileName should fail\n");
410 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
412 SetLastError( 0xdeadbeef );
413 ret = GetMappedFileNameA(hpQI, hMod, szMapPath, sizeof(szMapPath));
414 ok( ret || broken(GetLastError() == ERROR_UNEXP_NET_ERR), /* win2k */
415 "GetMappedFileNameA failed with error %u\n", GetLastError() );
416 if (ret)
418 ok(ret == strlen(szMapPath), "szMapPath=\"%s\" ret=%d\n", szMapPath, ret);
419 ok(szMapPath[0] == '\\', "szMapPath=\"%s\"\n", szMapPath);
420 szMapBaseName = strrchr(szMapPath, '\\'); /* That's close enough for us */
421 ok(szMapBaseName && *szMapBaseName, "szMapPath=\"%s\"\n", szMapPath);
422 if (szMapBaseName)
424 GetModuleFileNameA(NULL, szModPath, sizeof(szModPath));
425 ok(!strcmp(strrchr(szModPath, '\\'), szMapBaseName),
426 "szModPath=\"%s\" szMapBaseName=\"%s\"\n", szModPath, szMapBaseName);
430 GetTempPathA(MAX_PATH, temp_path);
431 GetTempFileNameA(temp_path, "map", 0, file_name);
433 drive[0] = file_name[0];
434 drive[1] = ':';
435 drive[2] = 0;
436 SetLastError(0xdeadbeef);
437 ret = QueryDosDeviceA(drive, device_name, sizeof(device_name));
438 ok(ret, "QueryDosDeviceA error %d\n", GetLastError());
439 trace("%s -> %s\n", drive, device_name);
441 SetLastError(0xdeadbeef);
442 hfile = CreateFileA(file_name, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
443 ok(hfile != INVALID_HANDLE_VALUE, "CreateFileA(%s) error %d\n", file_name, GetLastError());
444 SetFilePointer(hfile, 0x4000, NULL, FILE_BEGIN);
445 SetEndOfFile(hfile);
447 SetLastError(0xdeadbeef);
448 hmap = CreateFileMappingA(hfile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
449 ok(hmap != 0, "CreateFileMappingA error %d\n", GetLastError());
451 SetLastError(0xdeadbeef);
452 base = MapViewOfFile(hmap, FILE_MAP_READ, 0, 0, 0);
453 ok(base != NULL, "MapViewOfFile error %d\n", GetLastError());
455 SetLastError(0xdeadbeef);
456 ret = GetMappedFileNameA(GetCurrentProcess(), base, map_name, 0);
457 ok(!ret, "GetMappedFileName should fail\n");
458 ok(GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == ERROR_INSUFFICIENT_BUFFER,
459 "wrong error %d\n", GetLastError());
461 SetLastError(0xdeadbeef);
462 ret = GetMappedFileNameA(GetCurrentProcess(), base, 0, sizeof(map_name));
463 ok(!ret, "GetMappedFileName should fail\n");
464 ok(GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
466 SetLastError(0xdeadbeef);
467 ret = GetMappedFileNameA(GetCurrentProcess(), base, map_name, 1);
468 ok(ret == 1, "GetMappedFileName error %d\n", GetLastError());
469 ok(!map_name[0] || broken(map_name[0] == device_name[0]) /* before win2k */, "expected 0, got %c\n", map_name[0]);
471 SetLastError(0xdeadbeef);
472 ret = GetMappedFileNameA(GetCurrentProcess(), base, map_name, sizeof(map_name));
473 ok(ret, "GetMappedFileName error %d\n", GetLastError());
474 ok(ret > strlen(device_name), "map_name should be longer than device_name\n");
475 todo_wine
476 ok(memcmp(map_name, device_name, strlen(device_name)) == 0, "map name does not start with a device name: %s\n", map_name);
478 SetLastError(0xdeadbeef);
479 ret = GetMappedFileNameW(GetCurrentProcess(), base, map_nameW, ARRAY_SIZE(map_nameW));
480 ok(ret, "GetMappedFileNameW error %d\n", GetLastError());
481 ok(ret > strlen(device_name), "map_name should be longer than device_name\n");
482 if (nt_get_mapped_file_name(GetCurrentProcess(), base, nt_map_name, ARRAY_SIZE(nt_map_name)))
484 ok(memcmp(map_nameW, nt_map_name, lstrlenW(map_nameW)) == 0, "map name does not start with a device name: %s\n", map_name);
485 WideCharToMultiByte(CP_ACP, 0, map_nameW, -1, map_name, MAX_PATH, NULL, NULL);
486 todo_wine
487 ok(memcmp(map_name, device_name, strlen(device_name)) == 0, "map name does not start with a device name: %s\n", map_name);
490 SetLastError(0xdeadbeef);
491 ret = GetMappedFileNameA(GetCurrentProcess(), base + 0x2000, map_name, sizeof(map_name));
492 ok(ret, "GetMappedFileName error %d\n", GetLastError());
493 ok(ret > strlen(device_name), "map_name should be longer than device_name\n");
494 todo_wine
495 ok(memcmp(map_name, device_name, strlen(device_name)) == 0, "map name does not start with a device name: %s\n", map_name);
497 SetLastError(0xdeadbeef);
498 ret = GetMappedFileNameA(GetCurrentProcess(), base + 0x4000, map_name, sizeof(map_name));
499 ok(!ret, "GetMappedFileName should fail\n");
500 ok(GetLastError() == ERROR_UNEXP_NET_ERR, "expected ERROR_UNEXP_NET_ERR, got %d\n", GetLastError());
502 SetLastError(0xdeadbeef);
503 ret = GetMappedFileNameA(GetCurrentProcess(), NULL, map_name, sizeof(map_name));
504 ok(!ret, "GetMappedFileName should fail\n");
505 ok(GetLastError() == ERROR_UNEXP_NET_ERR, "expected ERROR_UNEXP_NET_ERR, got %d\n", GetLastError());
507 SetLastError(0xdeadbeef);
508 ret = GetMappedFileNameA(0, base, map_name, sizeof(map_name));
509 ok(!ret, "GetMappedFileName should fail\n");
510 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
512 UnmapViewOfFile(base);
513 CloseHandle(hmap);
514 CloseHandle(hfile);
515 DeleteFileA(file_name);
517 SetLastError(0xdeadbeef);
518 hmap = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READONLY | SEC_COMMIT, 0, 4096, NULL);
519 ok(hmap != 0, "CreateFileMappingA error %d\n", GetLastError());
521 SetLastError(0xdeadbeef);
522 base = MapViewOfFile(hmap, FILE_MAP_READ, 0, 0, 0);
523 ok(base != NULL, "MapViewOfFile error %d\n", GetLastError());
525 SetLastError(0xdeadbeef);
526 ret = GetMappedFileNameA(GetCurrentProcess(), base, map_name, sizeof(map_name));
527 ok(!ret, "GetMappedFileName should fail\n");
528 ok(GetLastError() == ERROR_FILE_INVALID, "expected ERROR_FILE_INVALID, got %d\n", GetLastError());
530 UnmapViewOfFile(base);
531 CloseHandle(hmap);
534 static void test_GetProcessImageFileName(void)
536 HMODULE hMod = GetModuleHandleA(NULL);
537 char szImgPath[MAX_PATH], szMapPath[MAX_PATH];
538 WCHAR szImgPathW[MAX_PATH];
539 DWORD ret, ret1;
541 /* This function is available on WinXP+ only */
542 SetLastError(0xdeadbeef);
543 if(!GetProcessImageFileNameA(hpQI, szImgPath, sizeof(szImgPath)))
545 if(GetLastError() == ERROR_INVALID_FUNCTION) {
546 win_skip("GetProcessImageFileName not implemented\n");
547 return;
549 ok(0, "failed with %d\n", GetLastError());
552 SetLastError(0xdeadbeef);
553 GetProcessImageFileNameA(NULL, szImgPath, sizeof(szImgPath));
554 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
556 SetLastError(0xdeadbeef);
557 GetProcessImageFileNameA(hpSR, szImgPath, sizeof(szImgPath));
558 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
560 SetLastError(0xdeadbeef);
561 GetProcessImageFileNameA(hpQI, szImgPath, 0);
562 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
564 ret = GetProcessImageFileNameA(hpQI, szImgPath, sizeof(szImgPath));
565 ret1 = GetMappedFileNameA(hpQV, hMod, szMapPath, sizeof(szMapPath));
566 if(ret && ret1)
568 /* Windows returns 2*strlen-1 */
569 ok(ret >= strlen(szImgPath), "szImgPath=\"%s\" ret=%d\n", szImgPath, ret);
570 todo_wine ok(!strcmp(szImgPath, szMapPath), "szImgPath=\"%s\" szMapPath=\"%s\"\n", szImgPath, szMapPath);
573 SetLastError(0xdeadbeef);
574 GetProcessImageFileNameW(NULL, szImgPathW, ARRAY_SIZE(szImgPathW));
575 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
577 /* no information about correct buffer size returned: */
578 SetLastError(0xdeadbeef);
579 GetProcessImageFileNameW(hpQI, szImgPathW, 0);
580 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
582 SetLastError(0xdeadbeef);
583 GetProcessImageFileNameW(hpQI, NULL, 0);
584 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
586 /* correct call */
587 memset(szImgPathW, 0xff, sizeof(szImgPathW));
588 ret = GetProcessImageFileNameW(hpQI, szImgPathW, ARRAY_SIZE(szImgPathW));
589 ok(ret > 0, "GetProcessImageFileNameW should have succeeded.\n");
590 ok(szImgPathW[0] == '\\', "GetProcessImageFileNameW should have returned an NT path.\n");
591 ok(lstrlenW(szImgPathW) == ret, "Expected length to be %d, got %d\n", ret, lstrlenW(szImgPathW));
593 /* boundary values of 'size' */
594 SetLastError(0xdeadbeef);
595 GetProcessImageFileNameW(hpQI, szImgPathW, ret);
596 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
598 memset(szImgPathW, 0xff, sizeof(szImgPathW));
599 ret = GetProcessImageFileNameW(hpQI, szImgPathW, ret + 1);
600 ok(ret > 0, "GetProcessImageFileNameW should have succeeded.\n");
601 ok(szImgPathW[0] == '\\', "GetProcessImageFileNameW should have returned an NT path.\n");
602 ok(lstrlenW(szImgPathW) == ret, "Expected length to be %d, got %d\n", ret, lstrlenW(szImgPathW));
605 static void test_GetModuleFileNameEx(void)
607 HMODULE hMod = GetModuleHandleA(NULL);
608 char szModExPath[MAX_PATH+1], szModPath[MAX_PATH+1];
609 WCHAR buffer[MAX_PATH];
610 DWORD ret;
612 SetLastError(0xdeadbeef);
613 ret = GetModuleFileNameExA(NULL, hMod, szModExPath, sizeof(szModExPath));
614 ok( !ret, "GetModuleFileNameExA succeeded\n" );
615 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
617 SetLastError(0xdeadbeef);
618 ret = GetModuleFileNameExA(hpQI, hMod, szModExPath, sizeof(szModExPath));
619 ok( !ret, "GetModuleFileNameExA succeeded\n" );
620 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
622 SetLastError(0xdeadbeef);
623 ret = GetModuleFileNameExA(hpQV, hBad, szModExPath, sizeof(szModExPath));
624 ok( !ret, "GetModuleFileNameExA succeeded\n" );
625 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
627 ret = GetModuleFileNameExA(hpQV, NULL, szModExPath, sizeof(szModExPath));
628 if(!ret)
629 return;
630 ok(ret == strlen(szModExPath), "szModExPath=\"%s\" ret=%d\n", szModExPath, ret);
631 GetModuleFileNameA(NULL, szModPath, sizeof(szModPath));
632 ok(!strncmp(szModExPath, szModPath, MAX_PATH),
633 "szModExPath=\"%s\" szModPath=\"%s\"\n", szModExPath, szModPath);
635 SetLastError(0xdeadbeef);
636 memset( szModExPath, 0xcc, sizeof(szModExPath) );
637 ret = GetModuleFileNameExA(hpQV, NULL, szModExPath, 4 );
638 ok( ret == 4 || ret == strlen(szModExPath), "wrong length %u\n", ret );
639 ok( broken(szModExPath[3]) /*w2kpro*/ || strlen(szModExPath) == 3,
640 "szModExPath=\"%s\" ret=%d\n", szModExPath, ret );
641 ok(GetLastError() == 0xdeadbeef, "got error %d\n", GetLastError());
643 if (0) /* crashes on Windows 10 */
645 SetLastError(0xdeadbeef);
646 ret = GetModuleFileNameExA(hpQV, NULL, szModExPath, 0 );
647 ok( ret == 0, "wrong length %u\n", ret );
648 ok(GetLastError() == ERROR_INVALID_PARAMETER, "got error %d\n", GetLastError());
651 SetLastError(0xdeadbeef);
652 memset( buffer, 0xcc, sizeof(buffer) );
653 ret = GetModuleFileNameExW(hpQV, NULL, buffer, 4 );
654 ok( ret == 4 || ret == lstrlenW(buffer), "wrong length %u\n", ret );
655 ok( broken(buffer[3]) /*w2kpro*/ || lstrlenW(buffer) == 3,
656 "buffer=%s ret=%d\n", wine_dbgstr_w(buffer), ret );
657 ok(GetLastError() == 0xdeadbeef, "got error %d\n", GetLastError());
659 if (0) /* crashes on Windows 10 */
661 SetLastError(0xdeadbeef);
662 buffer[0] = 0xcc;
663 ret = GetModuleFileNameExW(hpQV, NULL, buffer, 0 );
664 ok( ret == 0, "wrong length %u\n", ret );
665 ok(GetLastError() == 0xdeadbeef, "got error %d\n", GetLastError());
666 ok( buffer[0] == 0xcc, "buffer modified %s\n", wine_dbgstr_w(buffer) );
670 static void test_GetModuleBaseName(void)
672 HMODULE hMod = GetModuleHandleA(NULL);
673 char szModPath[MAX_PATH], szModBaseName[MAX_PATH];
674 DWORD ret;
676 SetLastError(0xdeadbeef);
677 GetModuleBaseNameA(NULL, hMod, szModBaseName, sizeof(szModBaseName));
678 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
680 SetLastError(0xdeadbeef);
681 GetModuleBaseNameA(hpQI, hMod, szModBaseName, sizeof(szModBaseName));
682 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
684 SetLastError(0xdeadbeef);
685 GetModuleBaseNameA(hpQV, hBad, szModBaseName, sizeof(szModBaseName));
686 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
688 ret = GetModuleBaseNameA(hpQV, NULL, szModBaseName, sizeof(szModBaseName));
689 if(!ret)
690 return;
691 ok(ret == strlen(szModBaseName), "szModBaseName=\"%s\" ret=%d\n", szModBaseName, ret);
692 GetModuleFileNameA(NULL, szModPath, sizeof(szModPath));
693 ok(!strcmp(strrchr(szModPath, '\\') + 1, szModBaseName),
694 "szModPath=\"%s\" szModBaseName=\"%s\"\n", szModPath, szModBaseName);
697 static void test_ws_functions(void)
699 PSAPI_WS_WATCH_INFORMATION wswi[4096];
700 HANDLE ws_handle;
701 char *addr;
702 unsigned int i;
703 BOOL ret;
705 ws_handle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_SET_QUOTA |
706 PROCESS_SET_INFORMATION, FALSE, GetCurrentProcessId());
707 ok(!!ws_handle, "got error %u\n", GetLastError());
709 SetLastError(0xdeadbeef);
710 EmptyWorkingSet(NULL);
711 todo_wine ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
713 SetLastError(0xdeadbeef);
714 EmptyWorkingSet(hpSR);
715 todo_wine ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
717 SetLastError(0xdeadbeef);
718 ret = EmptyWorkingSet(ws_handle);
719 ok(ret == 1, "failed with %d\n", GetLastError());
721 SetLastError( 0xdeadbeef );
722 ret = InitializeProcessForWsWatch( NULL );
723 todo_wine ok( !ret, "InitializeProcessForWsWatch succeeded\n" );
724 if (!ret)
726 if (GetLastError() == ERROR_INVALID_FUNCTION) /* not supported on xp in wow64 mode */
728 trace( "InitializeProcessForWsWatch not supported\n" );
729 return;
731 ok( GetLastError() == ERROR_INVALID_HANDLE, "wrong error %u\n", GetLastError() );
733 SetLastError(0xdeadbeef);
734 ret = InitializeProcessForWsWatch(ws_handle);
735 ok(ret == 1, "failed with %d\n", GetLastError());
737 addr = VirtualAlloc(NULL, 1, MEM_COMMIT, PAGE_READWRITE);
738 if(!addr)
739 return;
741 *addr = 0; /* make sure it's paged in (needed on wow64) */
742 if(!VirtualLock(addr, 1))
744 trace("locking failed (error=%d) - skipping test\n", GetLastError());
745 goto free_page;
748 SetLastError(0xdeadbeef);
749 ret = GetWsChanges(hpQI, wswi, sizeof(wswi));
750 todo_wine ok(ret == 1, "failed with %d\n", GetLastError());
751 if(ret == 1)
753 for(i = 0; wswi[i].FaultingVa; i++)
754 if(((ULONG_PTR)wswi[i].FaultingVa & ~0xfffL) == (ULONG_PTR)addr)
756 todo_wine ok(ret == 1, "GetWsChanges found our page\n");
757 goto free_page;
760 todo_wine ok(0, "GetWsChanges didn't find our page\n");
763 free_page:
764 VirtualFree(addr, 0, MEM_RELEASE);
767 static void check_QueryWorkingSetEx(PVOID addr, const char *desc, DWORD expected_valid,
768 DWORD expected_protection, DWORD expected_shared, BOOL todo)
770 PSAPI_WORKING_SET_EX_INFORMATION info;
771 BOOL ret;
773 memset(&info, 0x41, sizeof(info));
774 info.VirtualAddress = addr;
775 ret = pQueryWorkingSetEx(GetCurrentProcess(), &info, sizeof(info));
776 ok(ret, "QueryWorkingSetEx failed with %d\n", GetLastError());
778 todo_wine_if(todo)
779 ok(info.VirtualAttributes.Valid == expected_valid, "%s expected Valid=%u but got %u\n",
780 desc, expected_valid, info.VirtualAttributes.Valid);
782 todo_wine_if(todo)
783 ok(info.VirtualAttributes.Win32Protection == expected_protection, "%s expected Win32Protection=%u but got %u\n",
784 desc, expected_protection, info.VirtualAttributes.Win32Protection);
786 ok(info.VirtualAttributes.Node == 0, "%s expected Node=0 but got %u\n",
787 desc, info.VirtualAttributes.Node);
788 ok(info.VirtualAttributes.LargePage == 0, "%s expected LargePage=0 but got %u\n",
789 desc, info.VirtualAttributes.LargePage);
791 ok(info.VirtualAttributes.Shared == expected_shared || broken(!info.VirtualAttributes.Valid) /* w2003 */,
792 "%s expected Shared=%u but got %u\n", desc, expected_shared, info.VirtualAttributes.Shared);
793 if (info.VirtualAttributes.Valid && info.VirtualAttributes.Shared)
794 ok(info.VirtualAttributes.ShareCount > 0, "%s expected ShareCount > 0 but got %u\n",
795 desc, info.VirtualAttributes.ShareCount);
796 else
797 ok(info.VirtualAttributes.ShareCount == 0, "%s expected ShareCount == 0 but got %u\n",
798 desc, info.VirtualAttributes.ShareCount);
801 static void test_QueryWorkingSetEx(void)
803 PVOID addr;
804 DWORD prot;
805 BOOL ret;
807 if (pQueryWorkingSetEx == NULL)
809 win_skip("QueryWorkingSetEx not found, skipping tests\n");
810 return;
813 addr = GetModuleHandleA(NULL);
814 check_QueryWorkingSetEx(addr, "exe", 1, PAGE_READONLY, 1, FALSE);
816 ret = VirtualProtect(addr, 0x1000, PAGE_NOACCESS, &prot);
817 ok(ret, "VirtualProtect failed with %d\n", GetLastError());
818 check_QueryWorkingSetEx(addr, "exe,noaccess", 0, 0, 1, FALSE);
820 ret = VirtualProtect(addr, 0x1000, prot, &prot);
821 ok(ret, "VirtualProtect failed with %d\n", GetLastError());
822 check_QueryWorkingSetEx(addr, "exe,readonly1", 0, 0, 1, TRUE);
824 *(volatile char *)addr;
825 ok(ret, "VirtualProtect failed with %d\n", GetLastError());
826 check_QueryWorkingSetEx(addr, "exe,readonly2", 1, PAGE_READONLY, 1, FALSE);
828 addr = VirtualAlloc(NULL, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
829 ok(addr != NULL, "VirtualAlloc failed with %d\n", GetLastError());
830 check_QueryWorkingSetEx(addr, "valloc", 0, 0, 0, FALSE);
832 *(volatile char *)addr;
833 check_QueryWorkingSetEx(addr, "valloc,read", 1, PAGE_READWRITE, 0, FALSE);
835 *(volatile char *)addr = 0x42;
836 check_QueryWorkingSetEx(addr, "valloc,write", 1, PAGE_READWRITE, 0, FALSE);
838 ret = VirtualProtect(addr, 0x1000, PAGE_NOACCESS, &prot);
839 ok(ret, "VirtualProtect failed with %d\n", GetLastError());
840 check_QueryWorkingSetEx(addr, "valloc,noaccess", 0, 0, 0, FALSE);
842 ret = VirtualProtect(addr, 0x1000, prot, &prot);
843 ok(ret, "VirtualProtect failed with %d\n", GetLastError());
844 check_QueryWorkingSetEx(addr, "valloc,readwrite1", 0, 0, 0, TRUE);
846 *(volatile char *)addr;
847 check_QueryWorkingSetEx(addr, "valloc,readwrite2", 1, PAGE_READWRITE, 0, FALSE);
849 ret = VirtualFree(addr, 0, MEM_RELEASE);
850 ok(ret, "VirtualFree failed with %d\n", GetLastError());
851 check_QueryWorkingSetEx(addr, "valloc,free", FALSE, 0, 0, FALSE);
854 START_TEST(psapi_main)
856 DWORD pid = GetCurrentProcessId();
858 init_func_ptrs();
860 if (pIsWow64Process)
861 IsWow64Process(GetCurrentProcess(), &wow64);
863 hpSR = OpenProcess(STANDARD_RIGHTS_REQUIRED, FALSE, pid);
864 ok(!!hpSR, "got error %u\n", GetLastError());
865 hpQI = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
866 ok(!!hpQI, "got error %u\n", GetLastError());
867 hpVR = OpenProcess(PROCESS_VM_READ, FALSE, pid);
868 ok(!!hpVR, "got error %u\n", GetLastError());
869 hpQV = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
870 ok(!!hpQV, "got error %u\n", GetLastError());
872 test_EnumProcesses();
873 test_EnumProcessModules();
874 test_GetModuleInformation();
875 test_GetPerformanceInfo();
876 test_GetProcessMemoryInfo();
877 test_GetMappedFileName();
878 test_GetProcessImageFileName();
879 test_GetModuleFileNameEx();
880 test_GetModuleBaseName();
881 test_QueryWorkingSetEx();
882 test_ws_functions();
884 CloseHandle(hpSR);
885 CloseHandle(hpQI);
886 CloseHandle(hpVR);
887 CloseHandle(hpQV);