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(hMod
== GetModuleHandle(NULL
),
111 "hMod=%p GetModuleHandle(NULL)=%p\n", hMod
, GetModuleHandle(NULL
));
112 ok(cbNeeded
% sizeof(hMod
) == 0, "not a multiple of sizeof(HMODULE) cbNeeded=%d\n", cbNeeded
);
113 /* Windows sometimes has a bunch of extra dlls, presumably brought in by
116 if (cbNeeded
< 4 * sizeof(HMODULE
) || cbNeeded
> 30 * sizeof(HMODULE
))
120 ok(0, "cbNeeded=%d\n", cbNeeded
);
122 pEnumProcessModules(hpQV
, hmods
, sizeof(hmods
), &cbNeeded
);
123 for (i
= 0 ; i
< cbNeeded
/sizeof(*hmods
); i
++)
126 GetModuleFileNameA(hmods
[i
], path
, sizeof(path
));
127 trace("i=%d hmod=%p path=[%s]\n", i
, hmods
[i
], path
);
132 static void test_GetModuleInformation(void)
134 HMODULE hMod
= GetModuleHandle(NULL
);
138 SetLastError(0xdeadbeef);
139 pGetModuleInformation(NULL
, hMod
, &info
, sizeof(info
));
140 ok(GetLastError() == ERROR_INVALID_HANDLE
, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
142 SetLastError(0xdeadbeef);
143 pGetModuleInformation(hpQI
, hMod
, &info
, sizeof(info
));
144 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
146 SetLastError(0xdeadbeef);
147 pGetModuleInformation(hpQV
, hBad
, &info
, sizeof(info
));
148 ok(GetLastError() == ERROR_INVALID_HANDLE
, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
150 SetLastError(0xdeadbeef);
151 pGetModuleInformation(hpQV
, hMod
, &info
, sizeof(info
)-1);
152 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
154 SetLastError(0xdeadbeef);
155 ret
= pGetModuleInformation(hpQV
, hMod
, &info
, sizeof(info
));
156 ok(ret
== 1, "failed with %d\n", GetLastError());
157 ok(info
.lpBaseOfDll
== hMod
, "lpBaseOfDll=%p hMod=%p\n", info
.lpBaseOfDll
, hMod
);
160 static void test_GetProcessMemoryInfo(void)
162 PROCESS_MEMORY_COUNTERS pmc
;
165 SetLastError(0xdeadbeef);
166 pGetProcessMemoryInfo(NULL
, &pmc
, sizeof(pmc
));
167 ok(GetLastError() == ERROR_INVALID_HANDLE
, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
169 SetLastError(0xdeadbeef);
170 pGetProcessMemoryInfo(hpSR
, &pmc
, sizeof(pmc
));
171 todo_wine
ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
173 SetLastError(0xdeadbeef);
174 pGetProcessMemoryInfo(hpQI
, &pmc
, sizeof(pmc
)-1);
175 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
177 SetLastError(0xdeadbeef);
178 ret
= pGetProcessMemoryInfo(hpQI
, &pmc
, sizeof(pmc
));
179 ok(ret
== 1, "failed with %d\n", GetLastError());
182 static void test_GetMappedFileName(void)
184 HMODULE hMod
= GetModuleHandle(NULL
);
185 char szMapPath
[MAX_PATH
], szModPath
[MAX_PATH
], *szMapBaseName
;
188 SetLastError(0xdeadbeef);
189 pGetMappedFileNameA(NULL
, hMod
, szMapPath
, sizeof(szMapPath
));
190 ok(GetLastError() == ERROR_INVALID_HANDLE
, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
192 SetLastError(0xdeadbeef);
193 pGetMappedFileNameA(hpSR
, hMod
, szMapPath
, sizeof(szMapPath
));
194 todo_wine
ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
196 SetLastError( 0xdeadbeef );
197 ret
= pGetMappedFileNameA(hpQI
, hMod
, szMapPath
, sizeof(szMapPath
));
198 ok( ret
|| broken(GetLastError() == ERROR_UNEXP_NET_ERR
), /* win2k */
199 "GetMappedFileNameA failed with error %u\n", GetLastError() );
201 ok(ret
== strlen(szMapPath
), "szMapPath=\"%s\" ret=%d\n", szMapPath
, ret
);
202 ok(szMapPath
[0] == '\\', "szMapPath=\"%s\"\n", szMapPath
);
203 szMapBaseName
= strrchr(szMapPath
, '\\'); /* That's close enough for us */
204 if(!szMapBaseName
|| !*szMapBaseName
)
206 ok(0, "szMapPath=\"%s\"\n", szMapPath
);
209 GetModuleFileNameA(NULL
, szModPath
, sizeof(szModPath
));
210 ok(!strcmp(strrchr(szModPath
, '\\'), szMapBaseName
),
211 "szModPath=\"%s\" szMapBaseName=\"%s\"\n", szModPath
, szMapBaseName
);
214 static void test_GetProcessImageFileName(void)
216 HMODULE hMod
= GetModuleHandle(NULL
);
217 char szImgPath
[MAX_PATH
], szMapPath
[MAX_PATH
];
218 WCHAR szImgPathW
[MAX_PATH
];
221 if(pGetProcessImageFileNameA
== NULL
)
224 /* This function is available on WinXP+ only */
225 SetLastError(0xdeadbeef);
226 if(!pGetProcessImageFileNameA(hpQI
, szImgPath
, sizeof(szImgPath
)))
228 if(GetLastError() == ERROR_INVALID_FUNCTION
) {
229 win_skip("GetProcessImageFileName not implemented\n");
233 if(GetLastError() == 0xdeadbeef)
234 todo_wine
ok(0, "failed without error code\n");
236 todo_wine
ok(0, "failed with %d\n", GetLastError());
239 SetLastError(0xdeadbeef);
240 pGetProcessImageFileNameA(NULL
, szImgPath
, sizeof(szImgPath
));
241 ok(GetLastError() == ERROR_INVALID_HANDLE
, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
243 SetLastError(0xdeadbeef);
244 pGetProcessImageFileNameA(hpSR
, szImgPath
, sizeof(szImgPath
));
245 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
247 SetLastError(0xdeadbeef);
248 pGetProcessImageFileNameA(hpQI
, szImgPath
, 0);
249 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
251 ret
= pGetProcessImageFileNameA(hpQI
, szImgPath
, sizeof(szImgPath
));
252 ret1
= pGetMappedFileNameA(hpQV
, hMod
, szMapPath
, sizeof(szMapPath
));
255 /* Windows returns 2*strlen-1 */
256 todo_wine
ok(ret
>= strlen(szImgPath
), "szImgPath=\"%s\" ret=%d\n", szImgPath
, ret
);
257 todo_wine
ok(!strcmp(szImgPath
, szMapPath
), "szImgPath=\"%s\" szMapPath=\"%s\"\n", szImgPath
, szMapPath
);
260 SetLastError(0xdeadbeef);
261 pGetProcessImageFileNameW(NULL
, szImgPathW
, sizeof(szImgPathW
));
262 ok(GetLastError() == ERROR_INVALID_HANDLE
, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
264 /* no information about correct buffer size returned: */
265 SetLastError(0xdeadbeef);
266 pGetProcessImageFileNameW(hpQI
, szImgPathW
, 0);
267 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
269 SetLastError(0xdeadbeef);
270 pGetProcessImageFileNameW(hpQI
, NULL
, 0);
271 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
274 memset(szImgPathW
, 0xff, sizeof(szImgPathW
));
275 ret
= pGetProcessImageFileNameW(hpQI
, szImgPathW
, sizeof(szImgPathW
)/sizeof(WCHAR
));
276 ok(ret
> 0, "GetProcessImageFileNameW should have succeeded.\n");
277 ok(szImgPathW
[0] == '\\', "GetProcessImageFileNameW should have returned an NT path.\n");
278 ok(lstrlenW(szImgPathW
) == ret
, "Expected length to be %d, got %d\n", ret
, lstrlenW(szImgPathW
));
280 /* boundary values of 'size' */
281 SetLastError(0xdeadbeef);
282 pGetProcessImageFileNameW(hpQI
, szImgPathW
, ret
);
283 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
285 memset(szImgPathW
, 0xff, sizeof(szImgPathW
));
286 ret
= pGetProcessImageFileNameW(hpQI
, szImgPathW
, ret
+ 1);
287 ok(ret
> 0, "GetProcessImageFileNameW should have succeeded.\n");
288 ok(szImgPathW
[0] == '\\', "GetProcessImageFileNameW should have returned an NT path.\n");
289 ok(lstrlenW(szImgPathW
) == ret
, "Expected length to be %d, got %d\n", ret
, lstrlenW(szImgPathW
));
292 static void test_GetModuleFileNameEx(void)
294 HMODULE hMod
= GetModuleHandle(NULL
);
295 char szModExPath
[MAX_PATH
+1], szModPath
[MAX_PATH
+1];
298 SetLastError(0xdeadbeef);
299 pGetModuleFileNameExA(NULL
, hMod
, szModExPath
, sizeof(szModExPath
));
300 ok(GetLastError() == ERROR_INVALID_HANDLE
, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
302 SetLastError(0xdeadbeef);
303 pGetModuleFileNameExA(hpQI
, hMod
, szModExPath
, sizeof(szModExPath
));
304 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
306 SetLastError(0xdeadbeef);
307 pGetModuleFileNameExA(hpQV
, hBad
, szModExPath
, sizeof(szModExPath
));
308 ok(GetLastError() == ERROR_INVALID_HANDLE
, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
310 ret
= pGetModuleFileNameExA(hpQV
, NULL
, szModExPath
, sizeof(szModExPath
));
313 ok(ret
== strlen(szModExPath
), "szModExPath=\"%s\" ret=%d\n", szModExPath
, ret
);
314 GetModuleFileNameA(NULL
, szModPath
, sizeof(szModPath
));
315 ok(!strncmp(szModExPath
, szModPath
, MAX_PATH
),
316 "szModExPath=\"%s\" szModPath=\"%s\"\n", szModExPath
, szModPath
);
319 static void test_GetModuleBaseName(void)
321 HMODULE hMod
= GetModuleHandle(NULL
);
322 char szModPath
[MAX_PATH
], szModBaseName
[MAX_PATH
];
325 SetLastError(0xdeadbeef);
326 pGetModuleBaseNameA(NULL
, hMod
, szModBaseName
, sizeof(szModBaseName
));
327 ok(GetLastError() == ERROR_INVALID_HANDLE
, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
329 SetLastError(0xdeadbeef);
330 pGetModuleBaseNameA(hpQI
, hMod
, szModBaseName
, sizeof(szModBaseName
));
331 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
333 SetLastError(0xdeadbeef);
334 pGetModuleBaseNameA(hpQV
, hBad
, szModBaseName
, sizeof(szModBaseName
));
335 ok(GetLastError() == ERROR_INVALID_HANDLE
, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
337 ret
= pGetModuleBaseNameA(hpQV
, NULL
, szModBaseName
, sizeof(szModBaseName
));
340 ok(ret
== strlen(szModBaseName
), "szModBaseName=\"%s\" ret=%d\n", szModBaseName
, ret
);
341 GetModuleFileNameA(NULL
, szModPath
, sizeof(szModPath
));
342 ok(!strcmp(strrchr(szModPath
, '\\') + 1, szModBaseName
),
343 "szModPath=\"%s\" szModBaseName=\"%s\"\n", szModPath
, szModBaseName
);
346 static void test_ws_functions(void)
348 PSAPI_WS_WATCH_INFORMATION wswi
[4096];
349 ULONG_PTR pages
[4096];
354 SetLastError(0xdeadbeef);
355 pEmptyWorkingSet(NULL
);
356 todo_wine
ok(GetLastError() == ERROR_INVALID_HANDLE
, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
358 SetLastError(0xdeadbeef);
359 pEmptyWorkingSet(hpSR
);
360 todo_wine
ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
362 SetLastError(0xdeadbeef);
363 ret
= pEmptyWorkingSet(hpAA
);
364 ok(ret
== 1, "failed with %d\n", GetLastError());
366 SetLastError( 0xdeadbeef );
367 ret
= pInitializeProcessForWsWatch( NULL
);
368 todo_wine
ok( !ret
, "InitializeProcessForWsWatch succeeded\n" );
371 if (GetLastError() == ERROR_INVALID_FUNCTION
) /* not supported on xp in wow64 mode */
373 trace( "InitializeProcessForWsWatch not supported\n" );
376 ok( GetLastError() == ERROR_INVALID_HANDLE
, "wrong error %u\n", GetLastError() );
378 SetLastError(0xdeadbeef);
379 ret
= pInitializeProcessForWsWatch(hpAA
);
380 ok(ret
== 1, "failed with %d\n", GetLastError());
382 addr
= VirtualAlloc(NULL
, 1, MEM_COMMIT
, PAGE_READWRITE
);
386 *addr
= 0; /* make sure it's paged in (needed on wow64) */
387 if(!VirtualLock(addr
, 1))
389 trace("locking failed (error=%d) - skipping test\n", GetLastError());
393 SetLastError(0xdeadbeef);
394 ret
= pQueryWorkingSet(hpQI
, pages
, 4096 * sizeof(ULONG_PTR
));
395 todo_wine
ok(ret
== 1, "failed with %d\n", GetLastError());
398 for(i
= 0; i
< pages
[0]; i
++)
399 if((pages
[i
+1] & ~0xfffL
) == (ULONG_PTR
)addr
)
401 todo_wine
ok(ret
== 1, "QueryWorkingSet found our page\n");
405 todo_wine
ok(0, "QueryWorkingSet didn't find our page\n");
409 SetLastError(0xdeadbeef);
410 ret
= pGetWsChanges(hpQI
, wswi
, sizeof(wswi
));
411 todo_wine
ok(ret
== 1, "failed with %d\n", GetLastError());
414 for(i
= 0; wswi
[i
].FaultingVa
; i
++)
415 if(((ULONG_PTR
)wswi
[i
].FaultingVa
& ~0xfffL
) == (ULONG_PTR
)addr
)
417 todo_wine
ok(ret
== 1, "GetWsChanges found our page\n");
421 todo_wine
ok(0, "GetWsChanges didn't find our page\n");
425 VirtualFree(addr
, 0, MEM_RELEASE
);
428 START_TEST(psapi_main
)
430 HMODULE hpsapi
= LoadLibraryA("psapi.dll");
434 trace("Could not load psapi.dll\n");
438 if(InitFunctionPtrs(hpsapi
))
440 DWORD pid
= GetCurrentProcessId();
442 hpSR
= OpenProcess(STANDARD_RIGHTS_REQUIRED
, FALSE
, pid
);
443 hpQI
= OpenProcess(PROCESS_QUERY_INFORMATION
, FALSE
, pid
);
444 hpVR
= OpenProcess(PROCESS_VM_READ
, FALSE
, pid
);
445 hpQV
= OpenProcess(PROCESS_QUERY_INFORMATION
| PROCESS_VM_READ
, FALSE
, pid
);
446 hpAA
= OpenProcess(PROCESS_ALL_ACCESS
, FALSE
, pid
);
448 if(hpSR
&& hpQI
&& hpVR
&& hpQV
&& hpAA
)
450 test_EnumProcesses();
451 test_EnumProcessModules();
452 test_GetModuleInformation();
453 test_GetProcessMemoryInfo();
454 todo_wine
test_GetMappedFileName();
455 test_GetProcessImageFileName();
456 test_GetModuleFileNameEx();
457 test_GetModuleBaseName();