2 * Unit test suite for PSAPI
4 * Copyright (C) 2005 Felix Nawothnig
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "wine/test.h"
28 #define PSAPI_GET_PROC(func) \
29 p ## func = (void*)GetProcAddress(hpsapi, #func); \
31 ok(0, "GetProcAddress(%s) failed\n", #func); \
32 FreeLibrary(hpsapi); \
36 static BOOL (WINAPI *pEmptyWorkingSet)(HANDLE);
37 static BOOL (WINAPI *pEnumProcesses)(DWORD*, DWORD, DWORD*);
38 static BOOL (WINAPI *pEnumProcessModules)(HANDLE, HMODULE*, DWORD, LPDWORD);
39 static DWORD (WINAPI *pGetModuleBaseNameA)(HANDLE, HMODULE, LPSTR, DWORD);
40 static DWORD (WINAPI *pGetModuleFileNameExA)(HANDLE, HMODULE, LPSTR, DWORD);
41 static BOOL (WINAPI *pGetModuleInformation)(HANDLE, HMODULE, LPMODULEINFO, DWORD);
42 static DWORD (WINAPI *pGetMappedFileNameA)(HANDLE, LPVOID, LPSTR, DWORD);
43 static DWORD (WINAPI *pGetProcessImageFileNameA)(HANDLE, LPSTR, DWORD);
44 static DWORD (WINAPI *pGetProcessImageFileNameW)(HANDLE, LPWSTR, DWORD);
45 static BOOL (WINAPI *pGetProcessMemoryInfo)(HANDLE, PPROCESS_MEMORY_COUNTERS, DWORD);
46 static BOOL (WINAPI *pGetWsChanges)(HANDLE, PPSAPI_WS_WATCH_INFORMATION, DWORD);
47 static BOOL (WINAPI *pInitializeProcessForWsWatch)(HANDLE);
48 static BOOL (WINAPI *pQueryWorkingSet)(HANDLE, PVOID, DWORD);
50 static BOOL InitFunctionPtrs(HMODULE hpsapi)
52 PSAPI_GET_PROC(EmptyWorkingSet);
53 PSAPI_GET_PROC(EnumProcessModules);
54 PSAPI_GET_PROC(EnumProcesses);
55 PSAPI_GET_PROC(GetModuleBaseNameA);
56 PSAPI_GET_PROC(GetModuleFileNameExA);
57 PSAPI_GET_PROC(GetModuleInformation);
58 PSAPI_GET_PROC(GetMappedFileNameA);
59 PSAPI_GET_PROC(GetProcessMemoryInfo);
60 PSAPI_GET_PROC(GetWsChanges);
61 PSAPI_GET_PROC(InitializeProcessForWsWatch);
62 PSAPI_GET_PROC(QueryWorkingSet);
63 /* GetProcessImageFileName is not exported on NT4 */
64 pGetProcessImageFileNameA =
65 (void *)GetProcAddress(hpsapi, "GetProcessImageFileNameA");
66 pGetProcessImageFileNameW =
67 (void *)GetProcAddress(hpsapi, "GetProcessImageFileNameW");
71 static HANDLE hpSR, hpQI, hpVR, hpQV, hpAA;
72 static const HANDLE hBad = (HANDLE)0xdeadbeef;
74 static void test_EnumProcesses(void)
76 DWORD pid, ret, cbUsed = 0xdeadbeef;
78 SetLastError(0xdeadbeef);
79 ret = pEnumProcesses(NULL, 0, &cbUsed);
80 ok(ret == 1, "failed with %d\n", GetLastError());
81 ok(cbUsed == 0, "cbUsed=%d\n", cbUsed);
83 SetLastError(0xdeadbeef);
84 ret = pEnumProcesses(&pid, 4, &cbUsed);
85 ok(ret == 1, "failed with %d\n", GetLastError());
86 ok(cbUsed == 4, "cbUsed=%d\n", cbUsed);
89 static void test_EnumProcessModules(void)
91 HMODULE hMod = GetModuleHandle(NULL);
92 DWORD ret, cbNeeded = 0xdeadbeef;
94 SetLastError(0xdeadbeef);
95 pEnumProcessModules(NULL, NULL, 0, &cbNeeded);
96 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
98 SetLastError(0xdeadbeef);
99 pEnumProcessModules(hpQI, NULL, 0, &cbNeeded);
100 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
102 SetLastError(0xdeadbeef);
103 ret = pEnumProcessModules(hpQV, NULL, 0, &cbNeeded);
104 ok(ret == 1, "failed with %d\n", GetLastError());
106 SetLastError(0xdeadbeef);
107 ret = pEnumProcessModules(hpQV, &hMod, sizeof(HMODULE), &cbNeeded);
110 ok(cbNeeded / sizeof(HMODULE) >= 3 && cbNeeded / sizeof(HMODULE) <= 5 * sizeof(HMODULE),
111 "cbNeeded=%d\n", cbNeeded);
112 ok(hMod == GetModuleHandle(NULL),
113 "hMod=%p GetModuleHandle(NULL)=%p\n", hMod, GetModuleHandle(NULL));
116 static void test_GetModuleInformation(void)
118 HMODULE hMod = GetModuleHandle(NULL);
122 SetLastError(0xdeadbeef);
123 pGetModuleInformation(NULL, hMod, &info, sizeof(info));
124 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
126 SetLastError(0xdeadbeef);
127 pGetModuleInformation(hpQI, hMod, &info, sizeof(info));
128 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
130 SetLastError(0xdeadbeef);
131 pGetModuleInformation(hpQV, hBad, &info, sizeof(info));
132 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
134 SetLastError(0xdeadbeef);
135 pGetModuleInformation(hpQV, hMod, &info, sizeof(info)-1);
136 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
138 SetLastError(0xdeadbeef);
139 ret = pGetModuleInformation(hpQV, hMod, &info, sizeof(info));
140 ok(ret == 1, "failed with %d\n", GetLastError());
141 ok(info.lpBaseOfDll == hMod, "lpBaseOfDll=%p hMod=%p\n", info.lpBaseOfDll, hMod);
144 static void test_GetProcessMemoryInfo(void)
146 PROCESS_MEMORY_COUNTERS pmc;
149 SetLastError(0xdeadbeef);
150 pGetProcessMemoryInfo(NULL, &pmc, sizeof(pmc));
151 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
153 SetLastError(0xdeadbeef);
154 pGetProcessMemoryInfo(hpSR, &pmc, sizeof(pmc));
155 todo_wine ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
157 SetLastError(0xdeadbeef);
158 pGetProcessMemoryInfo(hpQI, &pmc, sizeof(pmc)-1);
159 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
161 SetLastError(0xdeadbeef);
162 ret = pGetProcessMemoryInfo(hpQI, &pmc, sizeof(pmc));
163 ok(ret == 1, "failed with %d\n", GetLastError());
166 static void test_GetMappedFileName(void)
168 HMODULE hMod = GetModuleHandle(NULL);
169 char szMapPath[MAX_PATH], szModPath[MAX_PATH], *szMapBaseName;
172 SetLastError(0xdeadbeef);
173 pGetMappedFileNameA(NULL, hMod, szMapPath, sizeof(szMapPath));
174 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
176 SetLastError(0xdeadbeef);
177 pGetMappedFileNameA(hpSR, hMod, szMapPath, sizeof(szMapPath));
178 todo_wine ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
180 SetLastError( 0xdeadbeef );
181 ret = pGetMappedFileNameA(hpQI, hMod, szMapPath, sizeof(szMapPath));
182 ok( ret || broken(GetLastError() == ERROR_UNEXP_NET_ERR), /* win2k */
183 "GetMappedFileNameA failed with error %u\n", GetLastError() );
185 ok(ret == strlen(szMapPath), "szMapPath=\"%s\" ret=%d\n", szMapPath, ret);
186 ok(szMapPath[0] == '\\', "szMapPath=\"%s\"\n", szMapPath);
187 szMapBaseName = strrchr(szMapPath, '\\'); /* That's close enough for us */
188 if(!szMapBaseName || !*szMapBaseName)
190 ok(0, "szMapPath=\"%s\"\n", szMapPath);
193 GetModuleFileNameA(NULL, szModPath, sizeof(szModPath));
194 ok(!strcmp(strrchr(szModPath, '\\'), szMapBaseName),
195 "szModPath=\"%s\" szMapBaseName=\"%s\"\n", szModPath, szMapBaseName);
198 static void test_GetProcessImageFileName(void)
200 HMODULE hMod = GetModuleHandle(NULL);
201 char szImgPath[MAX_PATH], szMapPath[MAX_PATH];
202 WCHAR szImgPathW[MAX_PATH];
205 if(pGetProcessImageFileNameA == NULL)
208 /* This function is available on WinXP+ only */
209 SetLastError(0xdeadbeef);
210 if(!pGetProcessImageFileNameA(hpQI, szImgPath, sizeof(szImgPath)))
212 if(GetLastError() == ERROR_INVALID_FUNCTION) {
213 win_skip("GetProcessImageFileName not implemented\n");
217 if(GetLastError() == 0xdeadbeef)
218 todo_wine ok(0, "failed without error code\n");
220 todo_wine ok(0, "failed with %d\n", GetLastError());
223 SetLastError(0xdeadbeef);
224 pGetProcessImageFileNameA(NULL, szImgPath, sizeof(szImgPath));
225 todo_wine ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
227 SetLastError(0xdeadbeef);
228 pGetProcessImageFileNameA(hpSR, szImgPath, sizeof(szImgPath));
229 todo_wine ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
231 SetLastError(0xdeadbeef);
232 pGetProcessImageFileNameA(hpQI, szImgPath, 0);
233 todo_wine ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
235 ret = pGetProcessImageFileNameA(hpQI, szImgPath, sizeof(szImgPath));
236 ret1 = pGetMappedFileNameA(hpQV, hMod, szMapPath, sizeof(szMapPath));
239 /* Windows returns 2*strlen-1 */
240 todo_wine ok(ret >= strlen(szImgPath), "szImgPath=\"%s\" ret=%d\n", szImgPath, ret);
241 todo_wine ok(!strcmp(szImgPath, szMapPath), "szImgPath=\"%s\" szMapPath=\"%s\"\n", szImgPath, szMapPath);
244 SetLastError(0xdeadbeef);
245 pGetProcessImageFileNameW(NULL, szImgPathW, sizeof(szImgPathW));
246 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
248 /* no information about correct buffer size returned: */
249 SetLastError(0xdeadbeef);
250 pGetProcessImageFileNameW(hpQI, szImgPathW, 0);
251 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
253 SetLastError(0xdeadbeef);
254 pGetProcessImageFileNameW(hpQI, NULL, 0);
255 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
258 memset(szImgPathW, 0xff, sizeof(szImgPathW));
259 ret = pGetProcessImageFileNameW(hpQI, szImgPathW, sizeof(szImgPathW)/sizeof(WCHAR));
260 ok(ret > 0, "GetProcessImageFileNameW should have succeeded.\n");
261 ok(szImgPathW[0] == '\\', "GetProcessImageFileNameW should have returned an NT path.\n");
262 ok(lstrlenW(szImgPathW) == ret, "Expected length to be %d, got %d\n", ret, lstrlenW(szImgPathW));
264 /* boundary values of 'size' */
265 SetLastError(0xdeadbeef);
266 pGetProcessImageFileNameW(hpQI, szImgPathW, ret);
267 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
269 memset(szImgPathW, 0xff, sizeof(szImgPathW));
270 ret = pGetProcessImageFileNameW(hpQI, szImgPathW, ret + 1);
271 ok(ret > 0, "GetProcessImageFileNameW should have succeeded.\n");
272 ok(szImgPathW[0] == '\\', "GetProcessImageFileNameW should have returned an NT path.\n");
273 ok(lstrlenW(szImgPathW) == ret, "Expected length to be %d, got %d\n", ret, lstrlenW(szImgPathW));
276 static void test_GetModuleFileNameEx(void)
278 HMODULE hMod = GetModuleHandle(NULL);
279 char szModExPath[MAX_PATH+1], szModPath[MAX_PATH+1];
282 SetLastError(0xdeadbeef);
283 pGetModuleFileNameExA(NULL, hMod, szModExPath, sizeof(szModExPath));
284 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
286 SetLastError(0xdeadbeef);
287 pGetModuleFileNameExA(hpQI, hMod, szModExPath, sizeof(szModExPath));
288 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
290 SetLastError(0xdeadbeef);
291 pGetModuleFileNameExA(hpQV, hBad, szModExPath, sizeof(szModExPath));
292 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
294 ret = pGetModuleFileNameExA(hpQV, NULL, szModExPath, sizeof(szModExPath));
297 ok(ret == strlen(szModExPath), "szModExPath=\"%s\" ret=%d\n", szModExPath, ret);
298 GetModuleFileNameA(NULL, szModPath, sizeof(szModPath));
299 ok(!strncmp(szModExPath, szModPath, MAX_PATH),
300 "szModExPath=\"%s\" szModPath=\"%s\"\n", szModExPath, szModPath);
303 static void test_GetModuleBaseName(void)
305 HMODULE hMod = GetModuleHandle(NULL);
306 char szModPath[MAX_PATH], szModBaseName[MAX_PATH];
309 SetLastError(0xdeadbeef);
310 pGetModuleBaseNameA(NULL, hMod, szModBaseName, sizeof(szModBaseName));
311 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
313 SetLastError(0xdeadbeef);
314 pGetModuleBaseNameA(hpQI, hMod, szModBaseName, sizeof(szModBaseName));
315 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
317 SetLastError(0xdeadbeef);
318 pGetModuleBaseNameA(hpQV, hBad, szModBaseName, sizeof(szModBaseName));
319 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
321 ret = pGetModuleBaseNameA(hpQV, NULL, szModBaseName, sizeof(szModBaseName));
324 ok(ret == strlen(szModBaseName), "szModBaseName=\"%s\" ret=%d\n", szModBaseName, ret);
325 GetModuleFileNameA(NULL, szModPath, sizeof(szModPath));
326 ok(!strcmp(strrchr(szModPath, '\\') + 1, szModBaseName),
327 "szModPath=\"%s\" szModBaseName=\"%s\"\n", szModPath, szModBaseName);
330 static void test_ws_functions(void)
332 PSAPI_WS_WATCH_INFORMATION wswi[4096];
333 ULONG_PTR pages[4096];
338 SetLastError(0xdeadbeef);
339 pEmptyWorkingSet(NULL);
340 todo_wine ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
342 SetLastError(0xdeadbeef);
343 pEmptyWorkingSet(hpSR);
344 todo_wine ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
346 SetLastError(0xdeadbeef);
347 ret = pEmptyWorkingSet(hpAA);
348 ok(ret == 1, "failed with %d\n", GetLastError());
350 SetLastError( 0xdeadbeef );
351 ret = pInitializeProcessForWsWatch( NULL );
352 todo_wine ok( !ret, "InitializeProcessForWsWatch succeeded\n" );
355 if (GetLastError() == ERROR_INVALID_FUNCTION) /* not supported on xp in wow64 mode */
357 trace( "InitializeProcessForWsWatch not supported\n" );
360 ok( GetLastError() == ERROR_INVALID_HANDLE, "wrong error %u\n", GetLastError() );
362 SetLastError(0xdeadbeef);
363 ret = pInitializeProcessForWsWatch(hpAA);
364 ok(ret == 1, "failed with %d\n", GetLastError());
366 addr = VirtualAlloc(NULL, 1, MEM_COMMIT, PAGE_READWRITE);
370 *addr = 0; /* make sure it's paged in (needed on wow64) */
371 if(!VirtualLock(addr, 1))
373 trace("locking failed (error=%d) - skipping test\n", GetLastError());
377 SetLastError(0xdeadbeef);
378 ret = pQueryWorkingSet(hpQI, pages, 4096 * sizeof(ULONG_PTR));
379 todo_wine ok(ret == 1, "failed with %d\n", GetLastError());
382 for(i = 0; i < pages[0]; i++)
383 if((pages[i+1] & ~0xfffL) == (ULONG_PTR)addr)
385 todo_wine ok(ret == 1, "QueryWorkingSet found our page\n");
389 todo_wine ok(0, "QueryWorkingSet didn't find our page\n");
393 SetLastError(0xdeadbeef);
394 ret = pGetWsChanges(hpQI, wswi, sizeof(wswi));
395 todo_wine ok(ret == 1, "failed with %d\n", GetLastError());
398 for(i = 0; wswi[i].FaultingVa; i++)
399 if(((ULONG_PTR)wswi[i].FaultingVa & ~0xfffL) == (ULONG_PTR)addr)
401 todo_wine ok(ret == 1, "GetWsChanges found our page\n");
405 todo_wine ok(0, "GetWsChanges didn't find our page\n");
409 VirtualFree(addr, 0, MEM_RELEASE);
412 START_TEST(psapi_main)
414 HMODULE hpsapi = LoadLibraryA("psapi.dll");
418 trace("Could not load psapi.dll\n");
422 if(InitFunctionPtrs(hpsapi))
424 DWORD pid = GetCurrentProcessId();
426 hpSR = OpenProcess(STANDARD_RIGHTS_REQUIRED, FALSE, pid);
427 hpQI = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
428 hpVR = OpenProcess(PROCESS_VM_READ, FALSE, pid);
429 hpQV = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
430 hpAA = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
432 if(hpSR && hpQI && hpVR && hpQV && hpAA)
434 test_EnumProcesses();
435 test_EnumProcessModules();
436 test_GetModuleInformation();
437 test_GetProcessMemoryInfo();
438 todo_wine test_GetMappedFileName();
439 test_GetProcessImageFileName();
440 test_GetModuleFileNameEx();
441 test_GetModuleBaseName();