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