a8fe243a7399f86b7c21dcccccbc5c044a1e1e1b
[reactos.git] / modules / rostests / winetests / psapi / psapi_main.c
1 /*
2 * Unit test suite for PSAPI
3 *
4 * Copyright (C) 2005 Felix Nawothnig
5 * Copyright (C) 2012 Dmitry Timoshkov
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include <stdarg.h>
23
24 #include "ntstatus.h"
25 #define WIN32_NO_STATUS
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winreg.h"
30 #include "winnt.h"
31 #include "wine/winternl.h"
32 #include "winnls.h"
33 #include "psapi.h"
34 #include "wine/test.h"
35
36 #define PSAPI_GET_PROC(func) \
37 p ## func = (void*)GetProcAddress(hpsapi, #func); \
38 if(!p ## func) { \
39 ok(0, "GetProcAddress(%s) failed\n", #func); \
40 FreeLibrary(hpsapi); \
41 return FALSE; \
42 }
43
44 static BOOL (WINAPI *pEmptyWorkingSet)(HANDLE);
45 static BOOL (WINAPI *pEnumProcesses)(DWORD*, DWORD, DWORD*);
46 static BOOL (WINAPI *pEnumProcessModules)(HANDLE, HMODULE*, DWORD, LPDWORD);
47 static DWORD (WINAPI *pGetModuleBaseNameA)(HANDLE, HMODULE, LPSTR, DWORD);
48 static DWORD (WINAPI *pGetModuleFileNameExA)(HANDLE, HMODULE, LPSTR, DWORD);
49 static DWORD (WINAPI *pGetModuleFileNameExW)(HANDLE, HMODULE, LPWSTR, DWORD);
50 static BOOL (WINAPI *pGetModuleInformation)(HANDLE, HMODULE, LPMODULEINFO, DWORD);
51 static DWORD (WINAPI *pGetMappedFileNameA)(HANDLE, LPVOID, LPSTR, DWORD);
52 static DWORD (WINAPI *pGetMappedFileNameW)(HANDLE, LPVOID, LPWSTR, DWORD);
53 static BOOL (WINAPI *pGetPerformanceInfo)(PPERFORMANCE_INFORMATION, DWORD);
54 static DWORD (WINAPI *pGetProcessImageFileNameA)(HANDLE, LPSTR, DWORD);
55 static DWORD (WINAPI *pGetProcessImageFileNameW)(HANDLE, LPWSTR, DWORD);
56 static BOOL (WINAPI *pGetProcessMemoryInfo)(HANDLE, PPROCESS_MEMORY_COUNTERS, DWORD);
57 static BOOL (WINAPI *pGetWsChanges)(HANDLE, PPSAPI_WS_WATCH_INFORMATION, DWORD);
58 static BOOL (WINAPI *pInitializeProcessForWsWatch)(HANDLE);
59 static BOOL (WINAPI *pQueryWorkingSet)(HANDLE, PVOID, DWORD);
60 static NTSTATUS (WINAPI *pNtQuerySystemInformation)(SYSTEM_INFORMATION_CLASS, PVOID, ULONG, PULONG);
61 static NTSTATUS (WINAPI *pNtQueryVirtualMemory)(HANDLE, LPCVOID, ULONG, PVOID, SIZE_T, SIZE_T *);
62
63 static BOOL InitFunctionPtrs(HMODULE hpsapi)
64 {
65 PSAPI_GET_PROC(EmptyWorkingSet);
66 PSAPI_GET_PROC(EnumProcessModules);
67 PSAPI_GET_PROC(EnumProcesses);
68 PSAPI_GET_PROC(GetModuleBaseNameA);
69 PSAPI_GET_PROC(GetModuleFileNameExA);
70 PSAPI_GET_PROC(GetModuleFileNameExW);
71 PSAPI_GET_PROC(GetModuleInformation);
72 PSAPI_GET_PROC(GetMappedFileNameA);
73 PSAPI_GET_PROC(GetMappedFileNameW);
74 PSAPI_GET_PROC(GetProcessMemoryInfo);
75 PSAPI_GET_PROC(GetWsChanges);
76 PSAPI_GET_PROC(InitializeProcessForWsWatch);
77 PSAPI_GET_PROC(QueryWorkingSet);
78 /* GetProcessImageFileName is not exported on NT4 */
79 pGetPerformanceInfo =
80 (void *)GetProcAddress(hpsapi, "GetPerformanceInfo");
81 pGetProcessImageFileNameA =
82 (void *)GetProcAddress(hpsapi, "GetProcessImageFileNameA");
83 pGetProcessImageFileNameW =
84 (void *)GetProcAddress(hpsapi, "GetProcessImageFileNameW");
85 pNtQuerySystemInformation = (void *)GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtQuerySystemInformation");
86 pNtQueryVirtualMemory = (void *)GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtQueryVirtualMemory");
87 return TRUE;
88 }
89
90 static HANDLE hpSR, hpQI, hpVR, hpQV, hpAA;
91 static const HANDLE hBad = (HANDLE)0xdeadbeef;
92
93 static void test_EnumProcesses(void)
94 {
95 DWORD pid, ret, cbUsed = 0xdeadbeef;
96
97 SetLastError(0xdeadbeef);
98 ret = pEnumProcesses(NULL, 0, &cbUsed);
99 ok(ret == 1, "failed with %d\n", GetLastError());
100 ok(cbUsed == 0, "cbUsed=%d\n", cbUsed);
101
102 SetLastError(0xdeadbeef);
103 ret = pEnumProcesses(&pid, 4, &cbUsed);
104 ok(ret == 1, "failed with %d\n", GetLastError());
105 ok(cbUsed == 4, "cbUsed=%d\n", cbUsed);
106 }
107
108 static void test_EnumProcessModules(void)
109 {
110 HMODULE hMod;
111 DWORD ret, cbNeeded = 0xdeadbeef;
112
113 SetLastError(0xdeadbeef);
114 pEnumProcessModules(NULL, NULL, 0, &cbNeeded);
115 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
116
117 SetLastError(0xdeadbeef);
118 pEnumProcessModules(hpQI, NULL, 0, &cbNeeded);
119 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
120
121 SetLastError(0xdeadbeef);
122 hMod = (void *)0xdeadbeef;
123 ret = pEnumProcessModules(hpQI, &hMod, sizeof(HMODULE), NULL);
124 ok(!ret, "succeeded\n");
125 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
126
127 SetLastError(0xdeadbeef);
128 hMod = (void *)0xdeadbeef;
129 ret = pEnumProcessModules(hpQV, &hMod, sizeof(HMODULE), NULL);
130 ok(!ret, "succeeded\n");
131 ok(GetLastError() == ERROR_NOACCESS, "expected error=ERROR_NOACCESS but got %d\n", GetLastError());
132 todo_wine ok(hMod == GetModuleHandleA(NULL),
133 "hMod=%p GetModuleHandleA(NULL)=%p\n", hMod, GetModuleHandleA(NULL));
134
135 SetLastError(0xdeadbeef);
136 ret = pEnumProcessModules(hpQV, NULL, 0, &cbNeeded);
137 ok(ret == 1, "failed with %d\n", GetLastError());
138
139 SetLastError(0xdeadbeef);
140 ret = pEnumProcessModules(hpQV, NULL, sizeof(HMODULE), &cbNeeded);
141 ok(!ret, "succeeded\n");
142 ok(GetLastError() == ERROR_NOACCESS, "expected error=ERROR_NOACCESS but got %d\n", GetLastError());
143
144 SetLastError(0xdeadbeef);
145 hMod = (void *)0xdeadbeef;
146 ret = pEnumProcessModules(hpQV, &hMod, sizeof(HMODULE), &cbNeeded);
147 ok(ret == 1, "got %d, failed with %d\n", ret, GetLastError());
148 ok(hMod == GetModuleHandleA(NULL),
149 "hMod=%p GetModuleHandleA(NULL)=%p\n", hMod, GetModuleHandleA(NULL));
150 ok(cbNeeded % sizeof(hMod) == 0, "not a multiple of sizeof(HMODULE) cbNeeded=%d\n", cbNeeded);
151 }
152
153 static void test_GetModuleInformation(void)
154 {
155 HMODULE hMod = GetModuleHandleA(NULL);
156 MODULEINFO info;
157 DWORD ret;
158
159 SetLastError(0xdeadbeef);
160 pGetModuleInformation(NULL, hMod, &info, sizeof(info));
161 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
162
163 SetLastError(0xdeadbeef);
164 pGetModuleInformation(hpQI, hMod, &info, sizeof(info));
165 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
166
167 SetLastError(0xdeadbeef);
168 pGetModuleInformation(hpQV, hBad, &info, sizeof(info));
169 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
170
171 SetLastError(0xdeadbeef);
172 pGetModuleInformation(hpQV, hMod, &info, sizeof(info)-1);
173 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
174
175 SetLastError(0xdeadbeef);
176 ret = pGetModuleInformation(hpQV, hMod, &info, sizeof(info));
177 ok(ret == 1, "failed with %d\n", GetLastError());
178 ok(info.lpBaseOfDll == hMod, "lpBaseOfDll=%p hMod=%p\n", info.lpBaseOfDll, hMod);
179 }
180
181 static BOOL check_with_margin(SIZE_T perf, SIZE_T sysperf, int margin)
182 {
183 return (perf >= max(sysperf, margin) - margin && perf <= sysperf + margin);
184 }
185
186 static void test_GetPerformanceInfo(void)
187 {
188 PERFORMANCE_INFORMATION info;
189 NTSTATUS status;
190 DWORD size;
191 BOOL ret;
192
193 SetLastError(0xdeadbeef);
194 ret = pGetPerformanceInfo(&info, sizeof(info)-1);
195 ok(!ret, "GetPerformanceInfo unexpectedly succeeded\n");
196 ok(GetLastError() == ERROR_BAD_LENGTH, "expected error=ERROR_BAD_LENGTH but got %d\n", GetLastError());
197
198 SetLastError(0xdeadbeef);
199 ret = pGetPerformanceInfo(&info, sizeof(info));
200 ok(ret, "GetPerformanceInfo failed with %d\n", GetLastError());
201 ok(info.cb == sizeof(PERFORMANCE_INFORMATION), "got %d\n", info.cb);
202
203 if (!pNtQuerySystemInformation)
204 win_skip("NtQuerySystemInformation not found, skipping tests\n");
205 else
206 {
207 char performance_buffer[sizeof(SYSTEM_PERFORMANCE_INFORMATION) + 16]; /* larger on w2k8/win7 */
208 SYSTEM_PERFORMANCE_INFORMATION *sys_performance_info = (SYSTEM_PERFORMANCE_INFORMATION *)performance_buffer;
209 SYSTEM_PROCESS_INFORMATION *sys_process_info = NULL, *spi;
210 SYSTEM_BASIC_INFORMATION sys_basic_info;
211 DWORD process_count, handle_count, thread_count;
212
213 /* compare with values from SYSTEM_PERFORMANCE_INFORMATION */
214 size = 0;
215 status = pNtQuerySystemInformation(SystemPerformanceInformation, sys_performance_info, sizeof(performance_buffer), &size);
216 ok(status == STATUS_SUCCESS, "expected STATUS_SUCCESS, got %08x\n", status);
217 ok(size >= sizeof(SYSTEM_PERFORMANCE_INFORMATION), "incorrect length %d\n", size);
218
219
220 ok(check_with_margin(info.CommitTotal, sys_performance_info->TotalCommittedPages, 288),
221 "expected approximately %ld but got %d\n", info.CommitTotal, sys_performance_info->TotalCommittedPages);
222
223 ok(check_with_margin(info.CommitLimit, sys_performance_info->TotalCommitLimit, 32),
224 "expected approximately %ld but got %d\n", info.CommitLimit, sys_performance_info->TotalCommitLimit);
225
226 ok(check_with_margin(info.CommitPeak, sys_performance_info->PeakCommitment, 32),
227 "expected approximately %ld but got %d\n", info.CommitPeak, sys_performance_info->PeakCommitment);
228
229 ok(check_with_margin(info.PhysicalAvailable, sys_performance_info->AvailablePages, 512),
230 "expected approximately %ld but got %d\n", info.PhysicalAvailable, sys_performance_info->AvailablePages);
231
232 /* TODO: info.SystemCache not checked yet - to which field(s) does this value correspond to? */
233
234 ok(check_with_margin(info.KernelTotal, sys_performance_info->PagedPoolUsage + sys_performance_info->NonPagedPoolUsage, 256),
235 "expected approximately %ld but got %d\n", info.KernelTotal,
236 sys_performance_info->PagedPoolUsage + sys_performance_info->NonPagedPoolUsage);
237
238 ok(check_with_margin(info.KernelPaged, sys_performance_info->PagedPoolUsage, 256),
239 "expected approximately %ld but got %d\n", info.KernelPaged, sys_performance_info->PagedPoolUsage);
240
241 ok(check_with_margin(info.KernelNonpaged, sys_performance_info->NonPagedPoolUsage, 16),
242 "expected approximately %ld but got %d\n", info.KernelNonpaged, sys_performance_info->NonPagedPoolUsage);
243
244 /* compare with values from SYSTEM_BASIC_INFORMATION */
245 size = 0;
246 status = pNtQuerySystemInformation(SystemBasicInformation, &sys_basic_info, sizeof(sys_basic_info), &size);
247 ok(status == STATUS_SUCCESS, "expected STATUS_SUCCESS, got %08x\n", status);
248 ok(size >= sizeof(SYSTEM_BASIC_INFORMATION), "incorrect length %d\n", size);
249
250 ok(info.PhysicalTotal == sys_basic_info.MmNumberOfPhysicalPages,
251 "expected info.PhysicalTotal=%u but got %u\n",
252 sys_basic_info.MmNumberOfPhysicalPages, (ULONG)info.PhysicalTotal);
253
254 ok(info.PageSize == sys_basic_info.PageSize,
255 "expected info.PageSize=%u but got %u\n",
256 sys_basic_info.PageSize, (ULONG)info.PageSize);
257
258 /* compare with values from SYSTEM_PROCESS_INFORMATION */
259 size = 0;
260 status = pNtQuerySystemInformation(SystemProcessInformation, NULL, 0, &size);
261 ok(status == STATUS_INFO_LENGTH_MISMATCH, "expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
262 ok(size > 0, "incorrect length %d\n", size);
263 while (status == STATUS_INFO_LENGTH_MISMATCH)
264 {
265 sys_process_info = HeapAlloc(GetProcessHeap(), 0, size);
266 ok(sys_process_info != NULL, "failed to allocate memory\n");
267 status = pNtQuerySystemInformation(SystemProcessInformation, sys_process_info, size, &size);
268 if (status == STATUS_SUCCESS) break;
269 HeapFree(GetProcessHeap(), 0, sys_process_info);
270 }
271 ok(status == STATUS_SUCCESS, "expected STATUS_SUCCESS, got %08x\n", status);
272
273 process_count = handle_count = thread_count = 0;
274 for (spi = sys_process_info;; spi = (SYSTEM_PROCESS_INFORMATION *)(((char *)spi) + spi->NextEntryOffset))
275 {
276 process_count++;
277 handle_count += spi->HandleCount;
278 thread_count += spi->dwThreadCount;
279 if (spi->NextEntryOffset == 0) break;
280 }
281 HeapFree(GetProcessHeap(), 0, sys_process_info);
282
283 ok(check_with_margin(info.HandleCount, handle_count, 24),
284 "expected approximately %d but got %d\n", info.HandleCount, handle_count);
285
286 ok(check_with_margin(info.ProcessCount, process_count, 4),
287 "expected approximately %d but got %d\n", info.ProcessCount, process_count);
288
289 ok(check_with_margin(info.ThreadCount, thread_count, 4),
290 "expected approximately %d but got %d\n", info.ThreadCount, thread_count);
291 }
292 }
293
294
295 static void test_GetProcessMemoryInfo(void)
296 {
297 PROCESS_MEMORY_COUNTERS pmc;
298 DWORD ret;
299
300 SetLastError(0xdeadbeef);
301 ret = pGetProcessMemoryInfo(NULL, &pmc, sizeof(pmc));
302 ok(!ret, "GetProcessMemoryInfo should fail\n");
303 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
304
305 SetLastError(0xdeadbeef);
306 ret = pGetProcessMemoryInfo(hpSR, &pmc, sizeof(pmc));
307 ok(!ret, "GetProcessMemoryInfo should fail\n");
308 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
309
310 SetLastError(0xdeadbeef);
311 ret = pGetProcessMemoryInfo(hpQI, &pmc, sizeof(pmc)-1);
312 ok(!ret, "GetProcessMemoryInfo should fail\n");
313 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
314
315 SetLastError(0xdeadbeef);
316 ret = pGetProcessMemoryInfo(hpQI, &pmc, sizeof(pmc));
317 ok(ret == 1, "failed with %d\n", GetLastError());
318 }
319
320 static BOOL nt_get_mapped_file_name(HANDLE process, LPVOID addr, LPWSTR name, DWORD len)
321 {
322 MEMORY_SECTION_NAME *section_name;
323 WCHAR *buf;
324 SIZE_T buf_len, ret_len;
325 NTSTATUS status;
326
327 if (!pNtQueryVirtualMemory) return FALSE;
328
329 buf_len = len * sizeof(WCHAR) + sizeof(MEMORY_SECTION_NAME);
330 buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buf_len);
331
332 ret_len = 0xdeadbeef;
333 status = pNtQueryVirtualMemory(process, addr, MemorySectionName, buf, buf_len, &ret_len);
334 todo_wine
335 ok(!status, "NtQueryVirtualMemory error %x\n", status);
336 /* FIXME: remove once Wine is fixed */
337 if (status)
338 {
339 HeapFree(GetProcessHeap(), 0, buf);
340 return FALSE;
341 }
342
343 section_name = (MEMORY_SECTION_NAME *)buf;
344 ok(ret_len == section_name->SectionFileName.MaximumLength + sizeof(*section_name), "got %lu, %u\n",
345 ret_len, section_name->SectionFileName.MaximumLength);
346 ok((char *)section_name->SectionFileName.Buffer == (char *)section_name + sizeof(*section_name), "got %p, %p\n",
347 section_name, section_name->SectionFileName.Buffer);
348 ok(section_name->SectionFileName.MaximumLength == section_name->SectionFileName.Length + sizeof(WCHAR), "got %u, %u\n",
349 section_name->SectionFileName.MaximumLength, section_name->SectionFileName.Length);
350 ok(section_name->SectionFileName.Length == lstrlenW(section_name->SectionFileName.Buffer) * sizeof(WCHAR), "got %u, %u\n",
351 section_name->SectionFileName.Length, lstrlenW(section_name->SectionFileName.Buffer));
352
353 memcpy(name, section_name->SectionFileName.Buffer, section_name->SectionFileName.MaximumLength);
354 HeapFree(GetProcessHeap(), 0, buf);
355 return TRUE;
356 }
357
358 static void test_GetMappedFileName(void)
359 {
360 HMODULE hMod = GetModuleHandleA(NULL);
361 char szMapPath[MAX_PATH], szModPath[MAX_PATH], *szMapBaseName;
362 DWORD ret;
363 char *base;
364 char temp_path[MAX_PATH], file_name[MAX_PATH], map_name[MAX_PATH], device_name[MAX_PATH], drive[3];
365 WCHAR map_nameW[MAX_PATH], nt_map_name[MAX_PATH];
366 HANDLE hfile, hmap;
367
368 SetLastError(0xdeadbeef);
369 ret = pGetMappedFileNameA(NULL, hMod, szMapPath, sizeof(szMapPath));
370 ok(!ret, "GetMappedFileName should fail\n");
371 todo_wine
372 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
373
374 SetLastError(0xdeadbeef);
375 ret = pGetMappedFileNameA(hpSR, hMod, szMapPath, sizeof(szMapPath));
376 ok(!ret, "GetMappedFileName should fail\n");
377 todo_wine
378 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
379
380 SetLastError( 0xdeadbeef );
381 ret = pGetMappedFileNameA(hpQI, hMod, szMapPath, sizeof(szMapPath));
382 todo_wine
383 ok( ret || broken(GetLastError() == ERROR_UNEXP_NET_ERR), /* win2k */
384 "GetMappedFileNameA failed with error %u\n", GetLastError() );
385 if (ret)
386 {
387 ok(ret == strlen(szMapPath), "szMapPath=\"%s\" ret=%d\n", szMapPath, ret);
388 todo_wine
389 ok(szMapPath[0] == '\\', "szMapPath=\"%s\"\n", szMapPath);
390 szMapBaseName = strrchr(szMapPath, '\\'); /* That's close enough for us */
391 todo_wine
392 ok(szMapBaseName && *szMapBaseName, "szMapPath=\"%s\"\n", szMapPath);
393 if (szMapBaseName)
394 {
395 GetModuleFileNameA(NULL, szModPath, sizeof(szModPath));
396 ok(!strcmp(strrchr(szModPath, '\\'), szMapBaseName),
397 "szModPath=\"%s\" szMapBaseName=\"%s\"\n", szModPath, szMapBaseName);
398 }
399 }
400
401 GetTempPathA(MAX_PATH, temp_path);
402 GetTempFileNameA(temp_path, "map", 0, file_name);
403
404 drive[0] = file_name[0];
405 drive[1] = ':';
406 drive[2] = 0;
407 SetLastError(0xdeadbeef);
408 ret = QueryDosDeviceA(drive, device_name, sizeof(device_name));
409 ok(ret, "QueryDosDeviceA error %d\n", GetLastError());
410 trace("%s -> %s\n", drive, device_name);
411
412 SetLastError(0xdeadbeef);
413 hfile = CreateFileA(file_name, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
414 ok(hfile != INVALID_HANDLE_VALUE, "CreateFileA(%s) error %d\n", file_name, GetLastError());
415 SetFilePointer(hfile, 0x4000, NULL, FILE_BEGIN);
416 SetEndOfFile(hfile);
417
418 SetLastError(0xdeadbeef);
419 hmap = CreateFileMappingA(hfile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
420 ok(hmap != 0, "CreateFileMappingA error %d\n", GetLastError());
421
422 SetLastError(0xdeadbeef);
423 base = MapViewOfFile(hmap, FILE_MAP_READ, 0, 0, 0);
424 ok(base != NULL, "MapViewOfFile error %d\n", GetLastError());
425
426 SetLastError(0xdeadbeef);
427 ret = pGetMappedFileNameA(GetCurrentProcess(), base, map_name, 0);
428 ok(!ret, "GetMappedFileName should fail\n");
429 todo_wine
430 ok(GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == ERROR_INSUFFICIENT_BUFFER,
431 "wrong error %d\n", GetLastError());
432
433 SetLastError(0xdeadbeef);
434 ret = pGetMappedFileNameA(GetCurrentProcess(), base, 0, sizeof(map_name));
435 ok(!ret, "GetMappedFileName should fail\n");
436 todo_wine
437 ok(GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
438
439 SetLastError(0xdeadbeef);
440 ret = pGetMappedFileNameA(GetCurrentProcess(), base, map_name, 1);
441 todo_wine
442 ok(ret == 1, "GetMappedFileName error %d\n", GetLastError());
443 ok(!map_name[0] || broken(map_name[0] == device_name[0]) /* before win2k */, "expected 0, got %c\n", map_name[0]);
444
445 SetLastError(0xdeadbeef);
446 ret = pGetMappedFileNameA(GetCurrentProcess(), base, map_name, sizeof(map_name));
447 todo_wine {
448 ok(ret, "GetMappedFileName error %d\n", GetLastError());
449 ok(ret > strlen(device_name), "map_name should be longer than device_name\n");
450 ok(memcmp(map_name, device_name, strlen(device_name)) == 0, "map name does not start with a device name: %s\n", map_name);
451 }
452
453 SetLastError(0xdeadbeef);
454 ret = pGetMappedFileNameW(GetCurrentProcess(), base, map_nameW, sizeof(map_nameW)/sizeof(map_nameW[0]));
455 todo_wine {
456 ok(ret, "GetMappedFileNameW error %d\n", GetLastError());
457 ok(ret > strlen(device_name), "map_name should be longer than device_name\n");
458 }
459 if (nt_get_mapped_file_name(GetCurrentProcess(), base, nt_map_name, sizeof(nt_map_name)/sizeof(nt_map_name[0])))
460 {
461 ok(memcmp(map_nameW, nt_map_name, lstrlenW(map_nameW)) == 0, "map name does not start with a device name: %s\n", map_name);
462 WideCharToMultiByte(CP_ACP, 0, map_nameW, -1, map_name, MAX_PATH, NULL, NULL);
463 ok(memcmp(map_name, device_name, strlen(device_name)) == 0, "map name does not start with a device name: %s\n", map_name);
464 }
465
466 SetLastError(0xdeadbeef);
467 ret = pGetMappedFileNameA(GetCurrentProcess(), base + 0x2000, map_name, sizeof(map_name));
468 todo_wine {
469 ok(ret, "GetMappedFileName error %d\n", GetLastError());
470 ok(ret > strlen(device_name), "map_name should be longer than device_name\n");
471 ok(memcmp(map_name, device_name, strlen(device_name)) == 0, "map name does not start with a device name: %s\n", map_name);
472 }
473
474 SetLastError(0xdeadbeef);
475 ret = pGetMappedFileNameA(GetCurrentProcess(), base + 0x4000, map_name, sizeof(map_name));
476 ok(!ret, "GetMappedFileName should fail\n");
477 todo_wine
478 ok(GetLastError() == ERROR_UNEXP_NET_ERR, "expected ERROR_UNEXP_NET_ERR, got %d\n", GetLastError());
479
480 SetLastError(0xdeadbeef);
481 ret = pGetMappedFileNameA(GetCurrentProcess(), NULL, map_name, sizeof(map_name));
482 ok(!ret, "GetMappedFileName should fail\n");
483 todo_wine
484 ok(GetLastError() == ERROR_UNEXP_NET_ERR, "expected ERROR_UNEXP_NET_ERR, got %d\n", GetLastError());
485
486 SetLastError(0xdeadbeef);
487 ret = pGetMappedFileNameA(0, base, map_name, sizeof(map_name));
488 ok(!ret, "GetMappedFileName should fail\n");
489 todo_wine
490 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
491
492 UnmapViewOfFile(base);
493 CloseHandle(hmap);
494 CloseHandle(hfile);
495 DeleteFileA(file_name);
496
497 SetLastError(0xdeadbeef);
498 hmap = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READONLY | SEC_COMMIT, 0, 4096, NULL);
499 ok(hmap != 0, "CreateFileMappingA error %d\n", GetLastError());
500
501 SetLastError(0xdeadbeef);
502 base = MapViewOfFile(hmap, FILE_MAP_READ, 0, 0, 0);
503 ok(base != NULL, "MapViewOfFile error %d\n", GetLastError());
504
505 SetLastError(0xdeadbeef);
506 ret = pGetMappedFileNameA(GetCurrentProcess(), base, map_name, sizeof(map_name));
507 ok(!ret, "GetMappedFileName should fail\n");
508 todo_wine
509 ok(GetLastError() == ERROR_FILE_INVALID, "expected ERROR_FILE_INVALID, got %d\n", GetLastError());
510
511 UnmapViewOfFile(base);
512 CloseHandle(hmap);
513 }
514
515 static void test_GetProcessImageFileName(void)
516 {
517 HMODULE hMod = GetModuleHandleA(NULL);
518 char szImgPath[MAX_PATH], szMapPath[MAX_PATH];
519 WCHAR szImgPathW[MAX_PATH];
520 DWORD ret, ret1;
521
522 if(pGetProcessImageFileNameA == NULL)
523 return;
524
525 /* This function is available on WinXP+ only */
526 SetLastError(0xdeadbeef);
527 if(!pGetProcessImageFileNameA(hpQI, szImgPath, sizeof(szImgPath)))
528 {
529 if(GetLastError() == ERROR_INVALID_FUNCTION) {
530 win_skip("GetProcessImageFileName not implemented\n");
531 return;
532 }
533
534 if(GetLastError() == 0xdeadbeef)
535 todo_wine ok(0, "failed without error code\n");
536 else
537 todo_wine ok(0, "failed with %d\n", GetLastError());
538 }
539
540 SetLastError(0xdeadbeef);
541 pGetProcessImageFileNameA(NULL, szImgPath, sizeof(szImgPath));
542 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
543
544 SetLastError(0xdeadbeef);
545 pGetProcessImageFileNameA(hpSR, szImgPath, sizeof(szImgPath));
546 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
547
548 SetLastError(0xdeadbeef);
549 pGetProcessImageFileNameA(hpQI, szImgPath, 0);
550 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
551
552 ret = pGetProcessImageFileNameA(hpQI, szImgPath, sizeof(szImgPath));
553 ret1 = pGetMappedFileNameA(hpQV, hMod, szMapPath, sizeof(szMapPath));
554 if(ret && ret1)
555 {
556 /* Windows returns 2*strlen-1 */
557 todo_wine ok(ret >= strlen(szImgPath), "szImgPath=\"%s\" ret=%d\n", szImgPath, ret);
558 todo_wine ok(!strcmp(szImgPath, szMapPath), "szImgPath=\"%s\" szMapPath=\"%s\"\n", szImgPath, szMapPath);
559 }
560
561 SetLastError(0xdeadbeef);
562 pGetProcessImageFileNameW(NULL, szImgPathW, sizeof(szImgPathW));
563 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
564
565 /* no information about correct buffer size returned: */
566 SetLastError(0xdeadbeef);
567 pGetProcessImageFileNameW(hpQI, szImgPathW, 0);
568 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
569
570 SetLastError(0xdeadbeef);
571 pGetProcessImageFileNameW(hpQI, NULL, 0);
572 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
573
574 /* correct call */
575 memset(szImgPathW, 0xff, sizeof(szImgPathW));
576 ret = pGetProcessImageFileNameW(hpQI, szImgPathW, sizeof(szImgPathW)/sizeof(WCHAR));
577 ok(ret > 0, "GetProcessImageFileNameW should have succeeded.\n");
578 ok(szImgPathW[0] == '\\', "GetProcessImageFileNameW should have returned an NT path.\n");
579 ok(lstrlenW(szImgPathW) == ret, "Expected length to be %d, got %d\n", ret, lstrlenW(szImgPathW));
580
581 /* boundary values of 'size' */
582 SetLastError(0xdeadbeef);
583 pGetProcessImageFileNameW(hpQI, szImgPathW, ret);
584 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
585
586 memset(szImgPathW, 0xff, sizeof(szImgPathW));
587 ret = pGetProcessImageFileNameW(hpQI, szImgPathW, ret + 1);
588 ok(ret > 0, "GetProcessImageFileNameW should have succeeded.\n");
589 ok(szImgPathW[0] == '\\', "GetProcessImageFileNameW should have returned an NT path.\n");
590 ok(lstrlenW(szImgPathW) == ret, "Expected length to be %d, got %d\n", ret, lstrlenW(szImgPathW));
591 }
592
593 static void test_GetModuleFileNameEx(void)
594 {
595 HMODULE hMod = GetModuleHandleA(NULL);
596 char szModExPath[MAX_PATH+1], szModPath[MAX_PATH+1];
597 WCHAR buffer[MAX_PATH];
598 DWORD ret;
599
600 SetLastError(0xdeadbeef);
601 ret = pGetModuleFileNameExA(NULL, hMod, szModExPath, sizeof(szModExPath));
602 ok( !ret, "GetModuleFileNameExA succeeded\n" );
603 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
604
605 SetLastError(0xdeadbeef);
606 ret = pGetModuleFileNameExA(hpQI, hMod, szModExPath, sizeof(szModExPath));
607 ok( !ret, "GetModuleFileNameExA succeeded\n" );
608 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
609
610 SetLastError(0xdeadbeef);
611 ret = pGetModuleFileNameExA(hpQV, hBad, szModExPath, sizeof(szModExPath));
612 ok( !ret, "GetModuleFileNameExA succeeded\n" );
613 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
614
615 ret = pGetModuleFileNameExA(hpQV, NULL, szModExPath, sizeof(szModExPath));
616 if(!ret)
617 return;
618 ok(ret == strlen(szModExPath), "szModExPath=\"%s\" ret=%d\n", szModExPath, ret);
619 GetModuleFileNameA(NULL, szModPath, sizeof(szModPath));
620 ok(!strncmp(szModExPath, szModPath, MAX_PATH),
621 "szModExPath=\"%s\" szModPath=\"%s\"\n", szModExPath, szModPath);
622
623 SetLastError(0xdeadbeef);
624 memset( szModExPath, 0xcc, sizeof(szModExPath) );
625 ret = pGetModuleFileNameExA(hpQV, NULL, szModExPath, 4 );
626 ok( ret == 4 || ret == strlen(szModExPath), "wrong length %u\n", ret );
627 ok( broken(szModExPath[3]) /*w2kpro*/ || strlen(szModExPath) == 3,
628 "szModExPath=\"%s\" ret=%d\n", szModExPath, ret );
629 ok(GetLastError() == 0xdeadbeef, "got error %d\n", GetLastError());
630
631 if (0) /* crashes on Windows 10 */
632 {
633 SetLastError(0xdeadbeef);
634 ret = pGetModuleFileNameExA(hpQV, NULL, szModExPath, 0 );
635 ok( ret == 0, "wrong length %u\n", ret );
636 ok(GetLastError() == ERROR_INVALID_PARAMETER, "got error %d\n", GetLastError());
637 }
638
639 SetLastError(0xdeadbeef);
640 memset( buffer, 0xcc, sizeof(buffer) );
641 ret = pGetModuleFileNameExW(hpQV, NULL, buffer, 4 );
642 ok( ret == 4 || ret == lstrlenW(buffer), "wrong length %u\n", ret );
643 ok( broken(buffer[3]) /*w2kpro*/ || lstrlenW(buffer) == 3,
644 "buffer=%s ret=%d\n", wine_dbgstr_w(buffer), ret );
645 ok(GetLastError() == 0xdeadbeef, "got error %d\n", GetLastError());
646
647 if (0) /* crashes on Windows 10 */
648 {
649 SetLastError(0xdeadbeef);
650 buffer[0] = 0xcc;
651 ret = pGetModuleFileNameExW(hpQV, NULL, buffer, 0 );
652 ok( ret == 0, "wrong length %u\n", ret );
653 ok(GetLastError() == 0xdeadbeef, "got error %d\n", GetLastError());
654 ok( buffer[0] == 0xcc, "buffer modified %s\n", wine_dbgstr_w(buffer) );
655 }
656 }
657
658 static void test_GetModuleBaseName(void)
659 {
660 HMODULE hMod = GetModuleHandleA(NULL);
661 char szModPath[MAX_PATH], szModBaseName[MAX_PATH];
662 DWORD ret;
663
664 SetLastError(0xdeadbeef);
665 pGetModuleBaseNameA(NULL, hMod, szModBaseName, sizeof(szModBaseName));
666 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
667
668 SetLastError(0xdeadbeef);
669 pGetModuleBaseNameA(hpQI, hMod, szModBaseName, sizeof(szModBaseName));
670 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
671
672 SetLastError(0xdeadbeef);
673 pGetModuleBaseNameA(hpQV, hBad, szModBaseName, sizeof(szModBaseName));
674 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
675
676 ret = pGetModuleBaseNameA(hpQV, NULL, szModBaseName, sizeof(szModBaseName));
677 if(!ret)
678 return;
679 ok(ret == strlen(szModBaseName), "szModBaseName=\"%s\" ret=%d\n", szModBaseName, ret);
680 GetModuleFileNameA(NULL, szModPath, sizeof(szModPath));
681 ok(!strcmp(strrchr(szModPath, '\\') + 1, szModBaseName),
682 "szModPath=\"%s\" szModBaseName=\"%s\"\n", szModPath, szModBaseName);
683 }
684
685 static void test_ws_functions(void)
686 {
687 PSAPI_WS_WATCH_INFORMATION wswi[4096];
688 ULONG_PTR pages[4096];
689 char *addr;
690 unsigned int i;
691 BOOL ret;
692
693 SetLastError(0xdeadbeef);
694 pEmptyWorkingSet(NULL);
695 todo_wine ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
696
697 SetLastError(0xdeadbeef);
698 pEmptyWorkingSet(hpSR);
699 todo_wine ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
700
701 SetLastError(0xdeadbeef);
702 ret = pEmptyWorkingSet(hpAA);
703 ok(ret == 1, "failed with %d\n", GetLastError());
704
705 SetLastError( 0xdeadbeef );
706 ret = pInitializeProcessForWsWatch( NULL );
707 todo_wine ok( !ret, "InitializeProcessForWsWatch succeeded\n" );
708 if (!ret)
709 {
710 if (GetLastError() == ERROR_INVALID_FUNCTION) /* not supported on xp in wow64 mode */
711 {
712 trace( "InitializeProcessForWsWatch not supported\n" );
713 return;
714 }
715 ok( GetLastError() == ERROR_INVALID_HANDLE, "wrong error %u\n", GetLastError() );
716 }
717 SetLastError(0xdeadbeef);
718 ret = pInitializeProcessForWsWatch(hpAA);
719 ok(ret == 1, "failed with %d\n", GetLastError());
720
721 addr = VirtualAlloc(NULL, 1, MEM_COMMIT, PAGE_READWRITE);
722 if(!addr)
723 return;
724
725 *addr = 0; /* make sure it's paged in (needed on wow64) */
726 if(!VirtualLock(addr, 1))
727 {
728 trace("locking failed (error=%d) - skipping test\n", GetLastError());
729 goto free_page;
730 }
731
732 SetLastError(0xdeadbeef);
733 ret = pQueryWorkingSet(hpQI, pages, 4096 * sizeof(ULONG_PTR));
734 todo_wine ok(ret == 1, "failed with %d\n", GetLastError());
735 if(ret == 1)
736 {
737 for(i = 0; i < pages[0]; i++)
738 if((pages[i+1] & ~0xfffL) == (ULONG_PTR)addr)
739 {
740 todo_wine ok(ret == 1, "QueryWorkingSet found our page\n");
741 goto test_gwsc;
742 }
743
744 todo_wine ok(0, "QueryWorkingSet didn't find our page\n");
745 }
746
747 test_gwsc:
748 SetLastError(0xdeadbeef);
749 ret = pGetWsChanges(hpQI, wswi, sizeof(wswi));
750 todo_wine ok(ret == 1, "failed with %d\n", GetLastError());
751 if(ret == 1)
752 {
753 for(i = 0; wswi[i].FaultingVa; i++)
754 if(((ULONG_PTR)wswi[i].FaultingVa & ~0xfffL) == (ULONG_PTR)addr)
755 {
756 todo_wine ok(ret == 1, "GetWsChanges found our page\n");
757 goto free_page;
758 }
759
760 todo_wine ok(0, "GetWsChanges didn't find our page\n");
761 }
762
763 free_page:
764 VirtualFree(addr, 0, MEM_RELEASE);
765 }
766
767 START_TEST(psapi_main)
768 {
769 HMODULE hpsapi = LoadLibraryA("psapi.dll");
770
771 if(!hpsapi)
772 {
773 win_skip("Could not load psapi.dll\n");
774 return;
775 }
776
777 if(InitFunctionPtrs(hpsapi))
778 {
779 DWORD pid = GetCurrentProcessId();
780
781 hpSR = OpenProcess(STANDARD_RIGHTS_REQUIRED, FALSE, pid);
782 hpQI = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
783 hpVR = OpenProcess(PROCESS_VM_READ, FALSE, pid);
784 hpQV = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
785 hpAA = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
786
787 if(hpSR && hpQI && hpVR && hpQV && hpAA)
788 {
789 test_EnumProcesses();
790 test_EnumProcessModules();
791 test_GetModuleInformation();
792 test_GetPerformanceInfo();
793 test_GetProcessMemoryInfo();
794 test_GetMappedFileName();
795 test_GetProcessImageFileName();
796 test_GetModuleFileNameEx();
797 test_GetModuleBaseName();
798 test_ws_functions();
799 }
800 CloseHandle(hpSR);
801 CloseHandle(hpQI);
802 CloseHandle(hpVR);
803 CloseHandle(hpQV);
804 CloseHandle(hpAA);
805 }
806
807 FreeLibrary(hpsapi);
808 }