kernel: Fix CreateToolhelp32Snapshot tests on win2k.
[wine] / dlls / kernel / tests / toolhelp.c
1 /*
2  * Toolhelp
3  *
4  * Copyright 2005 Eric Pouech
5  *
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.
10  *
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.
15  *
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <stdarg.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <windef.h>
25 #include <winbase.h>
26
27 #include "tlhelp32.h"
28 #include "wine/test.h"
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winuser.h"
32
33 static char     selfname[MAX_PATH];
34
35 /* Some functions are only in later versions of kernel32.dll */
36 static HANDLE (WINAPI *pCreateToolhelp32Snapshot)(DWORD, DWORD);
37 static BOOL (WINAPI *pModule32First)(HANDLE, LPMODULEENTRY32);
38 static BOOL (WINAPI *pModule32Next)(HANDLE, LPMODULEENTRY32);
39 static BOOL (WINAPI *pProcess32First)(HANDLE, LPPROCESSENTRY32);
40 static BOOL (WINAPI *pProcess32Next)(HANDLE, LPPROCESSENTRY32);
41 static BOOL (WINAPI *pThread32First)(HANDLE, LPTHREADENTRY32);
42 static BOOL (WINAPI *pThread32Next)(HANDLE, LPTHREADENTRY32);
43
44 /* 1 minute should be more than enough */
45 #define WAIT_TIME       (60 * 1000)
46
47 static DWORD WINAPI sub_thread(void* pmt)
48 {
49     DWORD w = WaitForSingleObject((HANDLE)pmt, WAIT_TIME);
50     return w;
51 }
52
53 /******************************************************************
54  *              init
55  *
56  * generates basic information like:
57  *      selfname:       the way to reinvoke ourselves
58  * returns:
59  *      -1      on error
60  *      0       if parent
61  *      doesn't return if child
62  */
63 static int     init(void)
64 {
65     int                 argc;
66     char**              argv;
67     HANDLE              ev1, ev2, ev3, hThread;
68     DWORD               w;
69
70     argc = winetest_get_mainargs( &argv );
71     strcpy(selfname, argv[0]);
72
73     switch (argc)
74     {
75     case 2: /* the test program */
76         return 0;
77     case 4: /* the sub-process */
78         ev1 = (HANDLE)atoi(argv[2]);
79         ev2 = (HANDLE)atoi(argv[3]);
80         ev3 = CreateEvent(NULL, FALSE, FALSE, NULL);
81
82         if (ev3 == NULL) ExitProcess(WAIT_ABANDONED);
83         hThread = CreateThread(NULL, 0, sub_thread, ev3, 0, NULL);
84         if (hThread == NULL) ExitProcess(WAIT_ABANDONED);
85         if (!LoadLibraryA("shell32.dll")) ExitProcess(WAIT_ABANDONED);
86     
87         /* signal init of sub-process is done */
88         SetEvent(ev1);
89         /* wait for parent to have done all its queries */
90         w = WaitForSingleObject(ev2, WAIT_TIME);
91         if (w != WAIT_OBJECT_0) ExitProcess(w);
92         /* signal sub-thread to terminate */
93         SetEvent(ev3);
94         w = WaitForSingleObject(hThread, WAIT_TIME);
95         if (w != WAIT_OBJECT_0) ExitProcess(w);
96         GetExitCodeThread(hThread, &w);
97         ExitProcess(w);
98     default:
99         return -1;
100     }
101 }
102
103 static void test_process(DWORD curr_pid, DWORD sub_pcs_pid)
104 {
105     HANDLE              hSnapshot;
106     PROCESSENTRY32      pe;
107     THREADENTRY32       te;
108     MODULEENTRY32       me;
109     unsigned            found = 0;
110     int                 num = 0;
111
112     hSnapshot = pCreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
113     ok(hSnapshot != NULL, "Cannot create snapshot\n");
114
115     /* check that this current process is enumerated */
116     pe.dwSize = sizeof(pe);
117     if (pProcess32First( hSnapshot, &pe ))
118     {
119         do
120         {
121             if (pe.th32ProcessID == curr_pid) found++;
122             if (pe.th32ProcessID == sub_pcs_pid) found++;
123             trace("PID=%lx %s\n", pe.th32ProcessID, pe.szExeFile);
124             num++;
125         } while (pProcess32Next( hSnapshot, &pe ));
126     }
127     ok(found == 2, "couldn't find self and/or sub-process in process list\n");
128
129     /* check that first really resets the enumeration */
130     found = 0;
131     if (pProcess32First( hSnapshot, &pe ))
132     {
133         do
134         {
135             if (pe.th32ProcessID == curr_pid) found++;
136             if (pe.th32ProcessID == sub_pcs_pid) found++;
137             trace("PID=%lx %s\n", pe.th32ProcessID, pe.szExeFile);
138             num--;
139         } while (pProcess32Next( hSnapshot, &pe ));
140     }
141     ok(found == 2, "couldn't find self and/or sub-process in process list\n");
142     ok(!num, "mismatch in counting\n");
143
144     te.dwSize = sizeof(te);
145     ok(!pThread32First( hSnapshot, &te ), "shouldn't return a thread\n");
146
147     me.dwSize = sizeof(me);
148     ok(!pModule32First( hSnapshot, &me ), "shouldn't return a module\n");
149
150     CloseHandle(hSnapshot);
151     ok(!pProcess32First( hSnapshot, &pe ), "shouldn't return a process\n");
152 }
153
154 static void test_thread(DWORD curr_pid, DWORD sub_pcs_pid)
155 {
156     HANDLE              hSnapshot;
157     PROCESSENTRY32      pe;
158     THREADENTRY32       te;
159     MODULEENTRY32       me;
160     int                 num = 0;
161     unsigned            curr_found = 0;
162     unsigned            sub_found = 0;
163     
164     hSnapshot = pCreateToolhelp32Snapshot( TH32CS_SNAPTHREAD, 0 );
165     ok(hSnapshot != NULL, "Cannot create snapshot\n");
166
167     /* check that this current process is enumerated */
168     te.dwSize = sizeof(te);
169     if (pThread32First( hSnapshot, &te ))
170     {
171         do
172         {
173             if (te.th32OwnerProcessID == curr_pid) curr_found++;
174             if (te.th32OwnerProcessID == sub_pcs_pid) sub_found++;
175             trace("PID=%lx TID=%lx %ld\n", te.th32OwnerProcessID, te.th32ThreadID, te.tpBasePri);
176             num++;
177         } while (pThread32Next( hSnapshot, &te ));
178     }
179     ok(curr_found == 1, "couldn't find self in thread list\n");
180     ok(sub_found == 2, "couldn't find sub-process thread's in thread list\n");
181
182     /* check that first really resets enumeration */
183     curr_found = 0;
184     sub_found = 0;
185     if (pThread32First( hSnapshot, &te ))
186     {
187         do
188         {
189             if (te.th32OwnerProcessID == curr_pid) curr_found++;
190             if (te.th32OwnerProcessID == sub_pcs_pid) sub_found++;
191             trace("PID=%lx TID=%lx %ld\n", te.th32OwnerProcessID, te.th32ThreadID, te.tpBasePri);
192             num--;
193         } while (pThread32Next( hSnapshot, &te ));
194     }
195     ok(curr_found == 1, "couldn't find self in thread list\n");
196     ok(sub_found == 2, "couldn't find sub-process thread's in thread list\n");
197
198     pe.dwSize = sizeof(pe);
199     ok(!pProcess32First( hSnapshot, &pe ), "shouldn't return a process\n");
200
201     me.dwSize = sizeof(me);
202     ok(!pModule32First( hSnapshot, &me ), "shouldn't return a module\n");
203
204     CloseHandle(hSnapshot);
205     ok(!pThread32First( hSnapshot, &te ), "shouldn't return a thread\n");
206 }
207
208 static const char* curr_expected_modules[] =
209 {
210     "kernel32.dll",
211     "kernel32_test.exe"
212     /* FIXME: could test for ntdll on NT and Wine */
213 };
214 static const char* sub_expected_modules[] =
215 {
216     "kernel32.dll",
217     "kernel32_test.exe",
218     "shell32.dll"
219     /* FIXME: could test for ntdll on NT and Wine */
220 };
221 #define NUM_OF(x) (sizeof(x) / sizeof(x[0]))
222
223 static void test_module(DWORD pid, const char* expected[], unsigned num_expected)
224 {
225     HANDLE              hSnapshot;
226     PROCESSENTRY32      pe;
227     THREADENTRY32       te;
228     MODULEENTRY32       me;
229     unsigned            found[32];
230     int                 i, num = 0;
231
232     ok(NUM_OF(found) >= num_expected, "Internal: bump found[] size\n");
233
234     hSnapshot = pCreateToolhelp32Snapshot( TH32CS_SNAPMODULE, pid );
235     ok(hSnapshot != NULL, "Cannot create snapshot\n");
236
237     for (i = 0; i < num_expected; i++) found[i] = 0;
238     me.dwSize = sizeof(me);
239     if (pModule32First( hSnapshot, &me ))
240     {
241         do
242         {
243             trace("PID=%lx base=%p size=%lx %s %s\n",
244                   me.th32ProcessID, me.modBaseAddr, me.modBaseSize, me.szExePath, me.szModule);
245             ok(me.th32ProcessID == pid, "wrong returned process id");
246             for (i = 0; i < num_expected; i++)
247                 if (!lstrcmpi(expected[i], me.szModule)) found[i]++;
248             num++;
249         } while (pModule32Next( hSnapshot, &me ));
250     }
251     for (i = 0; i < num_expected; i++)
252         ok(found[i] == 1, "Module %s is %s\n",
253            expected[i], found[i] ? "listed more than once" : "not listed");
254
255     /* check that first really resets the enumeration */
256     for (i = 0; i < num_expected; i++) found[i] = 0;
257     me.dwSize = sizeof(me);
258     if (pModule32First( hSnapshot, &me ))
259     {
260         do
261         {
262             trace("PID=%lx base=%p size=%lx %s %s\n",
263                   me.th32ProcessID, me.modBaseAddr, me.modBaseSize, me.szExePath, me.szModule);
264             for (i = 0; i < num_expected; i++)
265                 if (!lstrcmpi(expected[i], me.szModule)) found[i]++;
266             num--;
267         } while (pModule32Next( hSnapshot, &me ));
268     }
269     for (i = 0; i < num_expected; i++)
270         ok(found[i] == 1, "Module %s is %s\n",
271            expected[i], found[i] ? "listed more than once" : "not listed");
272     ok(!num, "mismatch in counting\n");
273
274     pe.dwSize = sizeof(pe);
275     ok(!pProcess32First( hSnapshot, &pe ), "shouldn't return a process\n");
276
277     me.dwSize = sizeof(me);
278     ok(!pThread32First( hSnapshot, &te ), "shouldn't return a thread\n");
279
280     CloseHandle(hSnapshot);
281     ok(!pModule32First( hSnapshot, &me ), "shouldn't return a module\n");
282 }
283
284 START_TEST(toolhelp)
285 {
286     DWORD               pid = GetCurrentProcessId();
287     int                 r;
288     char                buffer[MAX_PATH];
289     SECURITY_ATTRIBUTES sa;
290     PROCESS_INFORMATION info;
291     STARTUPINFOA        startup;
292     HANDLE              ev1, ev2;
293     DWORD               w;
294     HANDLE              hkernel32 = GetModuleHandleA("kernel32");
295
296     pCreateToolhelp32Snapshot = (VOID *) GetProcAddress(hkernel32, "CreateToolhelp32Snapshot");
297     pModule32First = (VOID *) GetProcAddress(hkernel32, "Module32First");
298     pModule32Next = (VOID *) GetProcAddress(hkernel32, "Module32Next");
299     pProcess32First = (VOID *) GetProcAddress(hkernel32, "Process32First");
300     pProcess32Next = (VOID *) GetProcAddress(hkernel32, "Process32Next");
301     pThread32First = (VOID *) GetProcAddress(hkernel32, "Thread32First");
302     pThread32Next = (VOID *) GetProcAddress(hkernel32, "Thread32Next");
303
304     if (!pCreateToolhelp32Snapshot || 
305         !pModule32First || !pModule32Next ||
306         !pProcess32First || !pProcess32Next ||
307         !pThread32First || !pThread32Next) return;
308
309     r = init();
310     ok(r == 0, "Basic init of sub-process test\n");
311     if (r != 0) return;
312
313     sa.nLength = sizeof(sa);
314     sa.lpSecurityDescriptor = NULL;
315     sa.bInheritHandle = TRUE;
316
317     ev1 = CreateEvent(&sa, FALSE, FALSE, NULL);
318     ev2 = CreateEvent(&sa, FALSE, FALSE, NULL);
319     ok (ev1 != NULL && ev2 != NULL, "Couldn't create events\n");
320     memset(&startup, 0, sizeof(startup));
321     startup.cb = sizeof(startup);
322     startup.dwFlags = STARTF_USESHOWWINDOW;
323     startup.wShowWindow = SW_SHOWNORMAL;
324
325     sprintf(buffer, "%s tests/toolhelp.c %lu %lu", selfname, (DWORD)ev1, (DWORD)ev2);
326     ok(CreateProcessA(NULL, buffer, NULL, NULL, TRUE, 0, NULL, NULL, &startup, &info), "CreateProcess\n");
327     /* wait for child to be initialized */
328     w = WaitForSingleObject(ev1, WAIT_TIME);
329     ok(w == WAIT_OBJECT_0, "Failed to wait on sub-process startup\n");
330
331     test_process(pid, info.dwProcessId);
332     test_thread(pid, info.dwProcessId);
333     test_module(pid, curr_expected_modules, NUM_OF(curr_expected_modules));
334     test_module(info.dwProcessId, sub_expected_modules, NUM_OF(sub_expected_modules));
335
336     SetEvent(ev2);
337     w = WaitForSingleObject(info.hProcess, WAIT_TIME);
338     ok(w == WAIT_OBJECT_0, "Failed to wait on sub-process termination\n");
339     ok(GetExitCodeProcess(info.hProcess, &w), "couldn't get process exit code\n");
340     ok(w == WAIT_OBJECT_0, "Sub-Process failed to terminate properly\n");
341 }