psapi/tests: Add test for EnumProcessModules with NULL pointer.
[wine.git] / dlls / psapi / tests / psapi_main.c
blobcebfc5fc05d00d433b9d721dc6bbd2f96e671385
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 "psapi.h"
37 #include "wine/test.h"
39 #define PSAPI_GET_PROC(func) \
40 p ## func = (void*)GetProcAddress(hpsapi, #func); \
41 if(!p ## func) { \
42 ok(0, "GetProcAddress(%s) failed\n", #func); \
43 FreeLibrary(hpsapi); \
44 return FALSE; \
47 static BOOL (WINAPI *pEmptyWorkingSet)(HANDLE);
48 static BOOL (WINAPI *pEnumProcesses)(DWORD*, DWORD, DWORD*);
49 static BOOL (WINAPI *pEnumProcessModules)(HANDLE, HMODULE*, DWORD, LPDWORD);
50 static DWORD (WINAPI *pGetModuleBaseNameA)(HANDLE, HMODULE, LPSTR, DWORD);
51 static DWORD (WINAPI *pGetModuleFileNameExA)(HANDLE, HMODULE, LPSTR, DWORD);
52 static DWORD (WINAPI *pGetModuleFileNameExW)(HANDLE, HMODULE, LPWSTR, DWORD);
53 static BOOL (WINAPI *pGetModuleInformation)(HANDLE, HMODULE, LPMODULEINFO, DWORD);
54 static DWORD (WINAPI *pGetMappedFileNameA)(HANDLE, LPVOID, LPSTR, DWORD);
55 static DWORD (WINAPI *pGetMappedFileNameW)(HANDLE, LPVOID, LPWSTR, DWORD);
56 static BOOL (WINAPI *pGetPerformanceInfo)(PPERFORMANCE_INFORMATION, DWORD);
57 static DWORD (WINAPI *pGetProcessImageFileNameA)(HANDLE, LPSTR, DWORD);
58 static DWORD (WINAPI *pGetProcessImageFileNameW)(HANDLE, LPWSTR, DWORD);
59 static BOOL (WINAPI *pGetProcessMemoryInfo)(HANDLE, PPROCESS_MEMORY_COUNTERS, DWORD);
60 static BOOL (WINAPI *pGetWsChanges)(HANDLE, PPSAPI_WS_WATCH_INFORMATION, DWORD);
61 static BOOL (WINAPI *pInitializeProcessForWsWatch)(HANDLE);
62 static BOOL (WINAPI *pQueryWorkingSet)(HANDLE, PVOID, DWORD);
63 static NTSTATUS (WINAPI *pNtQuerySystemInformation)(SYSTEM_INFORMATION_CLASS, PVOID, ULONG, PULONG);
64 static NTSTATUS (WINAPI *pNtQueryVirtualMemory)(HANDLE, LPCVOID, ULONG, PVOID, SIZE_T, SIZE_T *);
66 static BOOL InitFunctionPtrs(HMODULE hpsapi)
68 PSAPI_GET_PROC(EmptyWorkingSet);
69 PSAPI_GET_PROC(EnumProcessModules);
70 PSAPI_GET_PROC(EnumProcesses);
71 PSAPI_GET_PROC(GetModuleBaseNameA);
72 PSAPI_GET_PROC(GetModuleFileNameExA);
73 PSAPI_GET_PROC(GetModuleFileNameExW);
74 PSAPI_GET_PROC(GetModuleInformation);
75 PSAPI_GET_PROC(GetMappedFileNameA);
76 PSAPI_GET_PROC(GetMappedFileNameW);
77 PSAPI_GET_PROC(GetProcessMemoryInfo);
78 PSAPI_GET_PROC(GetWsChanges);
79 PSAPI_GET_PROC(InitializeProcessForWsWatch);
80 PSAPI_GET_PROC(QueryWorkingSet);
81 /* GetProcessImageFileName is not exported on NT4 */
82 pGetPerformanceInfo =
83 (void *)GetProcAddress(hpsapi, "GetPerformanceInfo");
84 pGetProcessImageFileNameA =
85 (void *)GetProcAddress(hpsapi, "GetProcessImageFileNameA");
86 pGetProcessImageFileNameW =
87 (void *)GetProcAddress(hpsapi, "GetProcessImageFileNameW");
88 pNtQuerySystemInformation = (void *)GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtQuerySystemInformation");
89 pNtQueryVirtualMemory = (void *)GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtQueryVirtualMemory");
90 return TRUE;
93 static HANDLE hpSR, hpQI, hpVR, hpQV, hpAA;
94 static const HANDLE hBad = (HANDLE)0xdeadbeef;
96 static void test_EnumProcesses(void)
98 DWORD pid, ret, cbUsed = 0xdeadbeef;
100 SetLastError(0xdeadbeef);
101 ret = pEnumProcesses(NULL, 0, &cbUsed);
102 ok(ret == 1, "failed with %d\n", GetLastError());
103 ok(cbUsed == 0, "cbUsed=%d\n", cbUsed);
105 SetLastError(0xdeadbeef);
106 ret = pEnumProcesses(&pid, 4, &cbUsed);
107 ok(ret == 1, "failed with %d\n", GetLastError());
108 ok(cbUsed == 4, "cbUsed=%d\n", cbUsed);
111 static void test_EnumProcessModules(void)
113 HMODULE hMod = GetModuleHandleA(NULL);
114 DWORD ret, cbNeeded = 0xdeadbeef;
116 SetLastError(0xdeadbeef);
117 pEnumProcessModules(NULL, NULL, 0, &cbNeeded);
118 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
120 SetLastError(0xdeadbeef);
121 pEnumProcessModules(hpQI, NULL, 0, &cbNeeded);
122 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
124 SetLastError(0xdeadbeef);
125 ret = pEnumProcessModules(hpQI, &hMod, sizeof(HMODULE), NULL);
126 ok(!ret, "succeeded\n");
127 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
129 SetLastError(0xdeadbeef);
130 ret = pEnumProcessModules(hpQV, &hMod, sizeof(HMODULE), NULL);
131 ok(!ret, "succeeded\n");
132 ok(GetLastError() == ERROR_NOACCESS, "expected error=ERROR_NOACCESS but got %d\n", GetLastError());
134 SetLastError(0xdeadbeef);
135 ret = pEnumProcessModules(hpQV, NULL, 0, &cbNeeded);
136 ok(ret == 1, "failed with %d\n", GetLastError());
138 SetLastError(0xdeadbeef);
139 ret = pEnumProcessModules(hpQV, NULL, sizeof(HMODULE), &cbNeeded);
140 ok(!ret, "succeeded\n");
141 ok(GetLastError() == ERROR_NOACCESS, "expected error=ERROR_NOACCESS but got %d\n", GetLastError());
143 SetLastError(0xdeadbeef);
144 ret = pEnumProcessModules(hpQV, &hMod, sizeof(HMODULE), &cbNeeded);
145 if(ret != 1)
146 return;
147 ok(hMod == GetModuleHandleA(NULL),
148 "hMod=%p GetModuleHandleA(NULL)=%p\n", hMod, GetModuleHandleA(NULL));
149 ok(cbNeeded % sizeof(hMod) == 0, "not a multiple of sizeof(HMODULE) cbNeeded=%d\n", cbNeeded);
150 /* Windows sometimes has a bunch of extra dlls, presumably brought in by
151 * aclayers.dll.
153 if (cbNeeded < 4 * sizeof(HMODULE) || cbNeeded > 30 * sizeof(HMODULE))
155 HMODULE hmods[100];
156 int i;
157 ok(0, "cbNeeded=%d\n", cbNeeded);
159 pEnumProcessModules(hpQV, hmods, sizeof(hmods), &cbNeeded);
160 for (i = 0 ; i < cbNeeded/sizeof(*hmods); i++)
162 char path[1024];
163 GetModuleFileNameA(hmods[i], path, sizeof(path));
164 trace("i=%d hmod=%p path=[%s]\n", i, hmods[i], path);
169 static void test_GetModuleInformation(void)
171 HMODULE hMod = GetModuleHandleA(NULL);
172 MODULEINFO info;
173 DWORD ret;
175 SetLastError(0xdeadbeef);
176 pGetModuleInformation(NULL, hMod, &info, sizeof(info));
177 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
179 SetLastError(0xdeadbeef);
180 pGetModuleInformation(hpQI, hMod, &info, sizeof(info));
181 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
183 SetLastError(0xdeadbeef);
184 pGetModuleInformation(hpQV, hBad, &info, sizeof(info));
185 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
187 SetLastError(0xdeadbeef);
188 pGetModuleInformation(hpQV, hMod, &info, sizeof(info)-1);
189 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
191 SetLastError(0xdeadbeef);
192 ret = pGetModuleInformation(hpQV, hMod, &info, sizeof(info));
193 ok(ret == 1, "failed with %d\n", GetLastError());
194 ok(info.lpBaseOfDll == hMod, "lpBaseOfDll=%p hMod=%p\n", info.lpBaseOfDll, hMod);
197 static void test_GetPerformanceInfo(void)
199 PERFORMANCE_INFORMATION info;
200 NTSTATUS status;
201 DWORD size;
202 BOOL ret;
204 SetLastError(0xdeadbeef);
205 ret = pGetPerformanceInfo(&info, sizeof(info)-1);
206 ok(!ret, "GetPerformanceInfo unexpectedly succeeded\n");
207 ok(GetLastError() == ERROR_BAD_LENGTH, "expected error=ERROR_BAD_LENGTH but got %d\n", GetLastError());
209 SetLastError(0xdeadbeef);
210 ret = pGetPerformanceInfo(&info, sizeof(info));
211 ok(ret, "GetPerformanceInfo failed with %d\n", GetLastError());
212 ok(info.cb == sizeof(PERFORMANCE_INFORMATION), "got %d\n", info.cb);
214 if (!pNtQuerySystemInformation)
215 win_skip("NtQuerySystemInformation not found, skipping tests\n");
216 else
218 char performance_buffer[sizeof(SYSTEM_PERFORMANCE_INFORMATION) + 16]; /* larger on w2k8/win7 */
219 SYSTEM_PERFORMANCE_INFORMATION *sys_performance_info = (SYSTEM_PERFORMANCE_INFORMATION *)performance_buffer;
220 SYSTEM_PROCESS_INFORMATION *sys_process_info = NULL, *spi;
221 SYSTEM_BASIC_INFORMATION sys_basic_info;
222 DWORD process_count, handle_count, thread_count;
224 /* compare with values from SYSTEM_PERFORMANCE_INFORMATION */
225 size = 0;
226 status = pNtQuerySystemInformation(SystemPerformanceInformation, sys_performance_info, sizeof(performance_buffer), &size);
227 ok(status == STATUS_SUCCESS, "expected STATUS_SUCCESS, got %08x\n", status);
228 ok(size >= sizeof(SYSTEM_PERFORMANCE_INFORMATION), "incorrect length %d\n", size);
230 ok(info.CommitTotal == sys_performance_info->TotalCommittedPages,
231 "expected info.CommitTotal=%u but got %u\n",
232 sys_performance_info->TotalCommittedPages, (ULONG)info.CommitTotal);
234 ok(info.CommitLimit == sys_performance_info->TotalCommitLimit,
235 "expected info.CommitLimit=%u but got %u\n",
236 sys_performance_info->TotalCommitLimit, (ULONG)info.CommitLimit);
238 ok(info.CommitPeak == sys_performance_info->PeakCommitment,
239 "expected info.CommitPeak=%u but got %u\n",
240 sys_performance_info->PeakCommitment, (ULONG)info.CommitPeak);
242 ok(info.PhysicalAvailable >= max(sys_performance_info->AvailablePages, 25) - 25 &&
243 info.PhysicalAvailable <= sys_performance_info->AvailablePages + 25,
244 "expected approximately info.PhysicalAvailable=%u but got %u\n",
245 sys_performance_info->AvailablePages, (ULONG)info.PhysicalAvailable);
247 /* TODO: info.SystemCache not checked yet - to which field(s) does this value correspond to? */
249 ok(info.KernelTotal == sys_performance_info->PagedPoolUsage + sys_performance_info->NonPagedPoolUsage,
250 "expected info.KernelTotal=%u but got %u\n",
251 sys_performance_info->PagedPoolUsage + sys_performance_info->NonPagedPoolUsage, (ULONG)info.KernelTotal);
253 ok(info.KernelPaged == sys_performance_info->PagedPoolUsage,
254 "expected info.KernelPaged=%u but got %u\n",
255 sys_performance_info->PagedPoolUsage, (ULONG)info.KernelPaged);
257 ok(info.KernelNonpaged == sys_performance_info->NonPagedPoolUsage,
258 "expected info.KernelNonpaged=%u but got %u\n",
259 sys_performance_info->NonPagedPoolUsage, (ULONG)info.KernelNonpaged);
261 /* compare with values from SYSTEM_BASIC_INFORMATION */
262 size = 0;
263 status = pNtQuerySystemInformation(SystemBasicInformation, &sys_basic_info, sizeof(sys_basic_info), &size);
264 ok(status == STATUS_SUCCESS, "expected STATUS_SUCCESS, got %08x\n", status);
265 ok(size >= sizeof(SYSTEM_BASIC_INFORMATION), "incorrect length %d\n", size);
267 ok(info.PhysicalTotal == sys_basic_info.MmNumberOfPhysicalPages,
268 "expected info.PhysicalTotal=%u but got %u\n",
269 sys_basic_info.MmNumberOfPhysicalPages, (ULONG)info.PhysicalTotal);
271 ok(info.PageSize == sys_basic_info.PageSize,
272 "expected info.PageSize=%u but got %u\n",
273 sys_basic_info.PageSize, (ULONG)info.PageSize);
275 /* compare with values from SYSTEM_PROCESS_INFORMATION */
276 size = 0;
277 status = pNtQuerySystemInformation(SystemProcessInformation, NULL, 0, &size);
278 ok(status == STATUS_INFO_LENGTH_MISMATCH, "expected STATUS_LENGTH_MISMATCH, got %08x\n", status);
279 ok(size > 0, "incorrect length %d\n", size);
280 while (status == STATUS_INFO_LENGTH_MISMATCH)
282 sys_process_info = HeapAlloc(GetProcessHeap(), 0, size);
283 ok(sys_process_info != NULL, "failed to allocate memory\n");
284 status = pNtQuerySystemInformation(SystemProcessInformation, sys_process_info, size, &size);
285 if (status == STATUS_SUCCESS) break;
286 HeapFree(GetProcessHeap(), 0, sys_process_info);
288 ok(status == STATUS_SUCCESS, "expected STATUS_SUCCESS, got %08x\n", status);
290 process_count = handle_count = thread_count = 0;
291 for (spi = sys_process_info;; spi = (SYSTEM_PROCESS_INFORMATION *)(((char *)spi) + spi->NextEntryOffset))
293 process_count++;
294 handle_count += spi->HandleCount;
295 thread_count += spi->dwThreadCount;
296 if (spi->NextEntryOffset == 0) break;
298 HeapFree(GetProcessHeap(), 0, sys_process_info);
300 ok(info.HandleCount == handle_count,
301 "expected info.HandleCount=%u but got %u\n", handle_count, info.HandleCount);
303 ok(info.ProcessCount == process_count,
304 "expected info.ProcessCount=%u but got %u\n", process_count, info.ProcessCount);
306 ok(info.ThreadCount == thread_count,
307 "expected info.ThreadCount=%u but got %u\n", thread_count, info.ThreadCount);
312 static void test_GetProcessMemoryInfo(void)
314 PROCESS_MEMORY_COUNTERS pmc;
315 DWORD ret;
317 SetLastError(0xdeadbeef);
318 ret = pGetProcessMemoryInfo(NULL, &pmc, sizeof(pmc));
319 ok(!ret, "GetProcessMemoryInfo should fail\n");
320 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
322 SetLastError(0xdeadbeef);
323 ret = pGetProcessMemoryInfo(hpSR, &pmc, sizeof(pmc));
324 todo_wine
325 ok(!ret, "GetProcessMemoryInfo should fail\n");
326 todo_wine
327 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
329 SetLastError(0xdeadbeef);
330 ret = pGetProcessMemoryInfo(hpQI, &pmc, sizeof(pmc)-1);
331 ok(!ret, "GetProcessMemoryInfo should fail\n");
332 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
334 SetLastError(0xdeadbeef);
335 ret = pGetProcessMemoryInfo(hpQI, &pmc, sizeof(pmc));
336 ok(ret == 1, "failed with %d\n", GetLastError());
339 static BOOL nt_get_mapped_file_name(HANDLE process, LPVOID addr, LPWSTR name, DWORD len)
341 MEMORY_SECTION_NAME *section_name;
342 WCHAR *buf;
343 SIZE_T buf_len, ret_len;
344 NTSTATUS status;
346 if (!pNtQueryVirtualMemory) return FALSE;
348 buf_len = len * sizeof(WCHAR) + sizeof(MEMORY_SECTION_NAME);
349 buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buf_len);
351 ret_len = 0xdeadbeef;
352 status = pNtQueryVirtualMemory(process, addr, MemorySectionName, buf, buf_len, &ret_len);
353 todo_wine
354 ok(!status, "NtQueryVirtualMemory error %x\n", status);
355 /* FIXME: remove once Wine is fixed */
356 if (status)
358 HeapFree(GetProcessHeap(), 0, buf);
359 return FALSE;
362 section_name = (MEMORY_SECTION_NAME *)buf;
363 ok(ret_len == section_name->SectionFileName.MaximumLength + sizeof(*section_name), "got %lu, %u\n",
364 ret_len, section_name->SectionFileName.MaximumLength);
365 ok((char *)section_name->SectionFileName.Buffer == (char *)section_name + sizeof(*section_name), "got %p, %p\n",
366 section_name, section_name->SectionFileName.Buffer);
367 ok(section_name->SectionFileName.MaximumLength == section_name->SectionFileName.Length + sizeof(WCHAR), "got %u, %u\n",
368 section_name->SectionFileName.MaximumLength, section_name->SectionFileName.Length);
369 ok(section_name->SectionFileName.Length == lstrlenW(section_name->SectionFileName.Buffer) * sizeof(WCHAR), "got %u, %u\n",
370 section_name->SectionFileName.Length, lstrlenW(section_name->SectionFileName.Buffer));
372 memcpy(name, section_name->SectionFileName.Buffer, section_name->SectionFileName.MaximumLength);
373 HeapFree(GetProcessHeap(), 0, buf);
374 return TRUE;
377 static void test_GetMappedFileName(void)
379 HMODULE hMod = GetModuleHandleA(NULL);
380 char szMapPath[MAX_PATH], szModPath[MAX_PATH], *szMapBaseName;
381 DWORD ret;
382 char *base;
383 char temp_path[MAX_PATH], file_name[MAX_PATH], map_name[MAX_PATH], device_name[MAX_PATH], drive[3];
384 WCHAR map_nameW[MAX_PATH], nt_map_name[MAX_PATH];
385 HANDLE hfile, hmap;
387 SetLastError(0xdeadbeef);
388 ret = pGetMappedFileNameA(NULL, hMod, szMapPath, sizeof(szMapPath));
389 ok(!ret, "GetMappedFileName should fail\n");
390 todo_wine
391 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
393 SetLastError(0xdeadbeef);
394 ret = pGetMappedFileNameA(hpSR, hMod, szMapPath, sizeof(szMapPath));
395 ok(!ret, "GetMappedFileName should fail\n");
396 todo_wine
397 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
399 SetLastError( 0xdeadbeef );
400 ret = pGetMappedFileNameA(hpQI, hMod, szMapPath, sizeof(szMapPath));
401 todo_wine
402 ok( ret || broken(GetLastError() == ERROR_UNEXP_NET_ERR), /* win2k */
403 "GetMappedFileNameA failed with error %u\n", GetLastError() );
404 if (ret)
406 ok(ret == strlen(szMapPath), "szMapPath=\"%s\" ret=%d\n", szMapPath, ret);
407 todo_wine
408 ok(szMapPath[0] == '\\', "szMapPath=\"%s\"\n", szMapPath);
409 szMapBaseName = strrchr(szMapPath, '\\'); /* That's close enough for us */
410 todo_wine
411 ok(szMapBaseName && *szMapBaseName, "szMapPath=\"%s\"\n", szMapPath);
412 if (szMapBaseName)
414 GetModuleFileNameA(NULL, szModPath, sizeof(szModPath));
415 ok(!strcmp(strrchr(szModPath, '\\'), szMapBaseName),
416 "szModPath=\"%s\" szMapBaseName=\"%s\"\n", szModPath, szMapBaseName);
420 GetTempPathA(MAX_PATH, temp_path);
421 GetTempFileNameA(temp_path, "map", 0, file_name);
423 drive[0] = file_name[0];
424 drive[1] = ':';
425 drive[2] = 0;
426 SetLastError(0xdeadbeef);
427 ret = QueryDosDeviceA(drive, device_name, sizeof(device_name));
428 ok(ret, "QueryDosDeviceA error %d\n", GetLastError());
429 trace("%s -> %s\n", drive, device_name);
431 SetLastError(0xdeadbeef);
432 hfile = CreateFileA(file_name, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
433 ok(hfile != INVALID_HANDLE_VALUE, "CreateFileA(%s) error %d\n", file_name, GetLastError());
434 SetFilePointer(hfile, 0x4000, NULL, FILE_BEGIN);
435 SetEndOfFile(hfile);
437 SetLastError(0xdeadbeef);
438 hmap = CreateFileMappingA(hfile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
439 ok(hmap != 0, "CreateFileMappingA error %d\n", GetLastError());
441 SetLastError(0xdeadbeef);
442 base = MapViewOfFile(hmap, FILE_MAP_READ, 0, 0, 0);
443 ok(base != NULL, "MapViewOfFile error %d\n", GetLastError());
445 SetLastError(0xdeadbeef);
446 ret = pGetMappedFileNameA(GetCurrentProcess(), base, map_name, 0);
447 ok(!ret, "GetMappedFileName should fail\n");
448 todo_wine
449 ok(GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == ERROR_INSUFFICIENT_BUFFER,
450 "wrong error %d\n", GetLastError());
452 SetLastError(0xdeadbeef);
453 ret = pGetMappedFileNameA(GetCurrentProcess(), base, 0, sizeof(map_name));
454 ok(!ret, "GetMappedFileName should fail\n");
455 todo_wine
456 ok(GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
458 SetLastError(0xdeadbeef);
459 ret = pGetMappedFileNameA(GetCurrentProcess(), base, map_name, 1);
460 todo_wine
461 ok(ret == 1, "GetMappedFileName error %d\n", GetLastError());
462 ok(!map_name[0] || broken(map_name[0] == device_name[0]) /* before win2k */, "expected 0, got %c\n", map_name[0]);
464 SetLastError(0xdeadbeef);
465 ret = pGetMappedFileNameA(GetCurrentProcess(), base, map_name, sizeof(map_name));
466 todo_wine {
467 ok(ret, "GetMappedFileName error %d\n", GetLastError());
468 ok(ret > strlen(device_name), "map_name should be longer than device_name\n");
469 ok(memcmp(map_name, device_name, strlen(device_name)) == 0, "map name does not start with a device name: %s\n", map_name);
472 SetLastError(0xdeadbeef);
473 ret = pGetMappedFileNameW(GetCurrentProcess(), base, map_nameW, sizeof(map_nameW)/sizeof(map_nameW[0]));
474 todo_wine {
475 ok(ret, "GetMappedFileNameW error %d\n", GetLastError());
476 ok(ret > strlen(device_name), "map_name should be longer than device_name\n");
478 if (nt_get_mapped_file_name(GetCurrentProcess(), base, nt_map_name, sizeof(nt_map_name)/sizeof(nt_map_name[0])))
480 ok(memcmp(map_nameW, nt_map_name, lstrlenW(map_nameW)) == 0, "map name does not start with a device name: %s\n", map_name);
481 WideCharToMultiByte(CP_ACP, 0, map_nameW, -1, map_name, MAX_PATH, NULL, NULL);
482 ok(memcmp(map_name, device_name, strlen(device_name)) == 0, "map name does not start with a device name: %s\n", map_name);
485 SetLastError(0xdeadbeef);
486 ret = pGetMappedFileNameA(GetCurrentProcess(), base + 0x2000, map_name, sizeof(map_name));
487 todo_wine {
488 ok(ret, "GetMappedFileName error %d\n", GetLastError());
489 ok(ret > strlen(device_name), "map_name should be longer than device_name\n");
490 ok(memcmp(map_name, device_name, strlen(device_name)) == 0, "map name does not start with a device name: %s\n", map_name);
493 SetLastError(0xdeadbeef);
494 ret = pGetMappedFileNameA(GetCurrentProcess(), base + 0x4000, map_name, sizeof(map_name));
495 ok(!ret, "GetMappedFileName should fail\n");
496 todo_wine
497 ok(GetLastError() == ERROR_UNEXP_NET_ERR, "expected ERROR_UNEXP_NET_ERR, got %d\n", GetLastError());
499 SetLastError(0xdeadbeef);
500 ret = pGetMappedFileNameA(GetCurrentProcess(), NULL, map_name, sizeof(map_name));
501 ok(!ret, "GetMappedFileName should fail\n");
502 todo_wine
503 ok(GetLastError() == ERROR_UNEXP_NET_ERR, "expected ERROR_UNEXP_NET_ERR, got %d\n", GetLastError());
505 SetLastError(0xdeadbeef);
506 ret = pGetMappedFileNameA(0, base, map_name, sizeof(map_name));
507 ok(!ret, "GetMappedFileName should fail\n");
508 todo_wine
509 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
511 UnmapViewOfFile(base);
512 CloseHandle(hmap);
513 CloseHandle(hfile);
514 DeleteFileA(file_name);
516 SetLastError(0xdeadbeef);
517 hmap = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READONLY | SEC_COMMIT, 0, 4096, NULL);
518 ok(hmap != 0, "CreateFileMappingA error %d\n", GetLastError());
520 SetLastError(0xdeadbeef);
521 base = MapViewOfFile(hmap, FILE_MAP_READ, 0, 0, 0);
522 ok(base != NULL, "MapViewOfFile error %d\n", GetLastError());
524 SetLastError(0xdeadbeef);
525 ret = pGetMappedFileNameA(GetCurrentProcess(), base, map_name, sizeof(map_name));
526 ok(!ret, "GetMappedFileName should fail\n");
527 todo_wine
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 if(pGetProcessImageFileNameA == NULL)
542 return;
544 /* This function is available on WinXP+ only */
545 SetLastError(0xdeadbeef);
546 if(!pGetProcessImageFileNameA(hpQI, szImgPath, sizeof(szImgPath)))
548 if(GetLastError() == ERROR_INVALID_FUNCTION) {
549 win_skip("GetProcessImageFileName not implemented\n");
550 return;
553 if(GetLastError() == 0xdeadbeef)
554 todo_wine ok(0, "failed without error code\n");
555 else
556 todo_wine ok(0, "failed with %d\n", GetLastError());
559 SetLastError(0xdeadbeef);
560 pGetProcessImageFileNameA(NULL, szImgPath, sizeof(szImgPath));
561 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
563 SetLastError(0xdeadbeef);
564 pGetProcessImageFileNameA(hpSR, szImgPath, sizeof(szImgPath));
565 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
567 SetLastError(0xdeadbeef);
568 pGetProcessImageFileNameA(hpQI, szImgPath, 0);
569 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
571 ret = pGetProcessImageFileNameA(hpQI, szImgPath, sizeof(szImgPath));
572 ret1 = pGetMappedFileNameA(hpQV, hMod, szMapPath, sizeof(szMapPath));
573 if(ret && ret1)
575 /* Windows returns 2*strlen-1 */
576 todo_wine ok(ret >= strlen(szImgPath), "szImgPath=\"%s\" ret=%d\n", szImgPath, ret);
577 todo_wine ok(!strcmp(szImgPath, szMapPath), "szImgPath=\"%s\" szMapPath=\"%s\"\n", szImgPath, szMapPath);
580 SetLastError(0xdeadbeef);
581 pGetProcessImageFileNameW(NULL, szImgPathW, sizeof(szImgPathW));
582 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
584 /* no information about correct buffer size returned: */
585 SetLastError(0xdeadbeef);
586 pGetProcessImageFileNameW(hpQI, szImgPathW, 0);
587 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
589 SetLastError(0xdeadbeef);
590 pGetProcessImageFileNameW(hpQI, NULL, 0);
591 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
593 /* correct call */
594 memset(szImgPathW, 0xff, sizeof(szImgPathW));
595 ret = pGetProcessImageFileNameW(hpQI, szImgPathW, sizeof(szImgPathW)/sizeof(WCHAR));
596 ok(ret > 0, "GetProcessImageFileNameW should have succeeded.\n");
597 ok(szImgPathW[0] == '\\', "GetProcessImageFileNameW should have returned an NT path.\n");
598 ok(lstrlenW(szImgPathW) == ret, "Expected length to be %d, got %d\n", ret, lstrlenW(szImgPathW));
600 /* boundary values of 'size' */
601 SetLastError(0xdeadbeef);
602 pGetProcessImageFileNameW(hpQI, szImgPathW, ret);
603 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
605 memset(szImgPathW, 0xff, sizeof(szImgPathW));
606 ret = pGetProcessImageFileNameW(hpQI, szImgPathW, ret + 1);
607 ok(ret > 0, "GetProcessImageFileNameW should have succeeded.\n");
608 ok(szImgPathW[0] == '\\', "GetProcessImageFileNameW should have returned an NT path.\n");
609 ok(lstrlenW(szImgPathW) == ret, "Expected length to be %d, got %d\n", ret, lstrlenW(szImgPathW));
612 static void test_GetModuleFileNameEx(void)
614 HMODULE hMod = GetModuleHandleA(NULL);
615 char szModExPath[MAX_PATH+1], szModPath[MAX_PATH+1];
616 WCHAR buffer[MAX_PATH];
617 DWORD ret;
619 SetLastError(0xdeadbeef);
620 ret = pGetModuleFileNameExA(NULL, hMod, szModExPath, sizeof(szModExPath));
621 ok( !ret, "GetModuleFileNameExA succeeded\n" );
622 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
624 SetLastError(0xdeadbeef);
625 ret = pGetModuleFileNameExA(hpQI, hMod, szModExPath, sizeof(szModExPath));
626 ok( !ret, "GetModuleFileNameExA succeeded\n" );
627 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
629 SetLastError(0xdeadbeef);
630 ret = pGetModuleFileNameExA(hpQV, hBad, szModExPath, sizeof(szModExPath));
631 ok( !ret, "GetModuleFileNameExA succeeded\n" );
632 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
634 ret = pGetModuleFileNameExA(hpQV, NULL, szModExPath, sizeof(szModExPath));
635 if(!ret)
636 return;
637 ok(ret == strlen(szModExPath), "szModExPath=\"%s\" ret=%d\n", szModExPath, ret);
638 GetModuleFileNameA(NULL, szModPath, sizeof(szModPath));
639 ok(!strncmp(szModExPath, szModPath, MAX_PATH),
640 "szModExPath=\"%s\" szModPath=\"%s\"\n", szModExPath, szModPath);
642 SetLastError(0xdeadbeef);
643 memset( szModExPath, 0xcc, sizeof(szModExPath) );
644 ret = pGetModuleFileNameExA(hpQV, NULL, szModExPath, 4 );
645 ok( ret == 4, "wrong length %u\n", ret );
646 ok( broken(szModExPath[3]) /*w2kpro*/ || strlen(szModExPath) == 3,
647 "szModExPath=\"%s\" ret=%d\n", szModExPath, ret );
648 ok(GetLastError() == 0xdeadbeef, "got error %d\n", GetLastError());
650 SetLastError(0xdeadbeef);
651 ret = pGetModuleFileNameExA(hpQV, NULL, szModExPath, 0 );
652 ok( ret == 0, "wrong length %u\n", ret );
653 ok(GetLastError() == ERROR_INVALID_PARAMETER, "got error %d\n", GetLastError());
655 SetLastError(0xdeadbeef);
656 memset( buffer, 0xcc, sizeof(buffer) );
657 ret = pGetModuleFileNameExW(hpQV, NULL, buffer, 4 );
658 ok( ret == 4, "wrong length %u\n", ret );
659 ok( broken(buffer[3]) /*w2kpro*/ || lstrlenW(buffer) == 3,
660 "buffer=%s ret=%d\n", wine_dbgstr_w(buffer), ret );
661 ok(GetLastError() == 0xdeadbeef, "got error %d\n", GetLastError());
663 SetLastError(0xdeadbeef);
664 buffer[0] = 0xcc;
665 ret = pGetModuleFileNameExW(hpQV, NULL, buffer, 0 );
666 ok( ret == 0, "wrong length %u\n", ret );
667 ok(GetLastError() == 0xdeadbeef, "got error %d\n", GetLastError());
668 ok( buffer[0] == 0xcc, "buffer modified %s\n", wine_dbgstr_w(buffer) );
671 static void test_GetModuleBaseName(void)
673 HMODULE hMod = GetModuleHandleA(NULL);
674 char szModPath[MAX_PATH], szModBaseName[MAX_PATH];
675 DWORD ret;
677 SetLastError(0xdeadbeef);
678 pGetModuleBaseNameA(NULL, hMod, szModBaseName, sizeof(szModBaseName));
679 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
681 SetLastError(0xdeadbeef);
682 pGetModuleBaseNameA(hpQI, hMod, szModBaseName, sizeof(szModBaseName));
683 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
685 SetLastError(0xdeadbeef);
686 pGetModuleBaseNameA(hpQV, hBad, szModBaseName, sizeof(szModBaseName));
687 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
689 ret = pGetModuleBaseNameA(hpQV, NULL, szModBaseName, sizeof(szModBaseName));
690 if(!ret)
691 return;
692 ok(ret == strlen(szModBaseName), "szModBaseName=\"%s\" ret=%d\n", szModBaseName, ret);
693 GetModuleFileNameA(NULL, szModPath, sizeof(szModPath));
694 ok(!strcmp(strrchr(szModPath, '\\') + 1, szModBaseName),
695 "szModPath=\"%s\" szModBaseName=\"%s\"\n", szModPath, szModBaseName);
698 static void test_ws_functions(void)
700 PSAPI_WS_WATCH_INFORMATION wswi[4096];
701 ULONG_PTR pages[4096];
702 char *addr;
703 unsigned int i;
704 BOOL ret;
706 SetLastError(0xdeadbeef);
707 pEmptyWorkingSet(NULL);
708 todo_wine ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
710 SetLastError(0xdeadbeef);
711 pEmptyWorkingSet(hpSR);
712 todo_wine ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
714 SetLastError(0xdeadbeef);
715 ret = pEmptyWorkingSet(hpAA);
716 ok(ret == 1, "failed with %d\n", GetLastError());
718 SetLastError( 0xdeadbeef );
719 ret = pInitializeProcessForWsWatch( NULL );
720 todo_wine ok( !ret, "InitializeProcessForWsWatch succeeded\n" );
721 if (!ret)
723 if (GetLastError() == ERROR_INVALID_FUNCTION) /* not supported on xp in wow64 mode */
725 trace( "InitializeProcessForWsWatch not supported\n" );
726 return;
728 ok( GetLastError() == ERROR_INVALID_HANDLE, "wrong error %u\n", GetLastError() );
730 SetLastError(0xdeadbeef);
731 ret = pInitializeProcessForWsWatch(hpAA);
732 ok(ret == 1, "failed with %d\n", GetLastError());
734 addr = VirtualAlloc(NULL, 1, MEM_COMMIT, PAGE_READWRITE);
735 if(!addr)
736 return;
738 *addr = 0; /* make sure it's paged in (needed on wow64) */
739 if(!VirtualLock(addr, 1))
741 trace("locking failed (error=%d) - skipping test\n", GetLastError());
742 goto free_page;
745 SetLastError(0xdeadbeef);
746 ret = pQueryWorkingSet(hpQI, pages, 4096 * sizeof(ULONG_PTR));
747 todo_wine ok(ret == 1, "failed with %d\n", GetLastError());
748 if(ret == 1)
750 for(i = 0; i < pages[0]; i++)
751 if((pages[i+1] & ~0xfffL) == (ULONG_PTR)addr)
753 todo_wine ok(ret == 1, "QueryWorkingSet found our page\n");
754 goto test_gwsc;
757 todo_wine ok(0, "QueryWorkingSet didn't find our page\n");
760 test_gwsc:
761 SetLastError(0xdeadbeef);
762 ret = pGetWsChanges(hpQI, wswi, sizeof(wswi));
763 todo_wine ok(ret == 1, "failed with %d\n", GetLastError());
764 if(ret == 1)
766 for(i = 0; wswi[i].FaultingVa; i++)
767 if(((ULONG_PTR)wswi[i].FaultingVa & ~0xfffL) == (ULONG_PTR)addr)
769 todo_wine ok(ret == 1, "GetWsChanges found our page\n");
770 goto free_page;
773 todo_wine ok(0, "GetWsChanges didn't find our page\n");
776 free_page:
777 VirtualFree(addr, 0, MEM_RELEASE);
780 START_TEST(psapi_main)
782 HMODULE hpsapi = LoadLibraryA("psapi.dll");
784 if(!hpsapi)
786 win_skip("Could not load psapi.dll\n");
787 return;
790 if(InitFunctionPtrs(hpsapi))
792 DWORD pid = GetCurrentProcessId();
794 hpSR = OpenProcess(STANDARD_RIGHTS_REQUIRED, FALSE, pid);
795 hpQI = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
796 hpVR = OpenProcess(PROCESS_VM_READ, FALSE, pid);
797 hpQV = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
798 hpAA = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
800 if(hpSR && hpQI && hpVR && hpQV && hpAA)
802 test_EnumProcesses();
803 test_EnumProcessModules();
804 test_GetModuleInformation();
805 test_GetPerformanceInfo();
806 test_GetProcessMemoryInfo();
807 test_GetMappedFileName();
808 test_GetProcessImageFileName();
809 test_GetModuleFileNameEx();
810 test_GetModuleBaseName();
811 test_ws_functions();
813 CloseHandle(hpSR);
814 CloseHandle(hpQI);
815 CloseHandle(hpVR);
816 CloseHandle(hpQV);
817 CloseHandle(hpAA);
820 FreeLibrary(hpsapi);