mshtml: Added IHTMLStyleSheet::get_rules implementation.
[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;
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, NULL);
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
110     hSnapshot = pCreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
111     ok(hSnapshot != NULL, "Cannot create snapshot\n");
112
113     /* check that this current process is enumerated */
114     pe.dwSize = sizeof(pe);
115     if (pProcess32First( hSnapshot, &pe ))
116     {
117         do
118         {
119             if (pe.th32ProcessID == curr_pid) found++;
120             if (pe.th32ProcessID == sub_pcs_pid) found++;
121             trace("PID=%x %s\n", pe.th32ProcessID, pe.szExeFile);
122             num++;
123         } while (pProcess32Next( hSnapshot, &pe ));
124     }
125     ok(found == 2, "couldn't find self and/or sub-process in process list\n");
126
127     /* check that first really resets the enumeration */
128     found = 0;
129     if (pProcess32First( hSnapshot, &pe ))
130     {
131         do
132         {
133             if (pe.th32ProcessID == curr_pid) found++;
134             if (pe.th32ProcessID == sub_pcs_pid) found++;
135             trace("PID=%x %s\n", pe.th32ProcessID, pe.szExeFile);
136             num--;
137         } while (pProcess32Next( hSnapshot, &pe ));
138     }
139     ok(found == 2, "couldn't find self and/or sub-process in process list\n");
140     ok(!num, "mismatch in counting\n");
141
142     te.dwSize = sizeof(te);
143     ok(!pThread32First( hSnapshot, &te ), "shouldn't return a thread\n");
144
145     me.dwSize = sizeof(me);
146     ok(!pModule32First( hSnapshot, &me ), "shouldn't return a module\n");
147
148     CloseHandle(hSnapshot);
149     ok(!pProcess32First( hSnapshot, &pe ), "shouldn't return a process\n");
150 }
151
152 static void test_thread(DWORD curr_pid, DWORD sub_pcs_pid)
153 {
154     HANDLE              hSnapshot;
155     PROCESSENTRY32      pe;
156     THREADENTRY32       te;
157     MODULEENTRY32       me;
158     int                 num = 0;
159     unsigned            curr_found = 0;
160     unsigned            sub_found = 0;
161     
162     hSnapshot = pCreateToolhelp32Snapshot( TH32CS_SNAPTHREAD, 0 );
163     ok(hSnapshot != NULL, "Cannot create snapshot\n");
164
165     /* check that this current process is enumerated */
166     te.dwSize = sizeof(te);
167     if (pThread32First( hSnapshot, &te ))
168     {
169         do
170         {
171             if (te.th32OwnerProcessID == curr_pid) curr_found++;
172             if (te.th32OwnerProcessID == sub_pcs_pid) sub_found++;
173             trace("PID=%x TID=%x %d\n", te.th32OwnerProcessID, te.th32ThreadID, te.tpBasePri);
174             num++;
175         } while (pThread32Next( hSnapshot, &te ));
176     }
177     ok(curr_found == 1, "couldn't find self in thread list\n");
178     ok(sub_found == 2, "couldn't find sub-process thread's in thread list\n");
179
180     /* check that first really resets enumeration */
181     curr_found = 0;
182     sub_found = 0;
183     if (pThread32First( hSnapshot, &te ))
184     {
185         do
186         {
187             if (te.th32OwnerProcessID == curr_pid) curr_found++;
188             if (te.th32OwnerProcessID == sub_pcs_pid) sub_found++;
189             trace("PID=%x TID=%x %d\n", te.th32OwnerProcessID, te.th32ThreadID, te.tpBasePri);
190             num--;
191         } while (pThread32Next( hSnapshot, &te ));
192     }
193     ok(curr_found == 1, "couldn't find self in thread list\n");
194     ok(sub_found == 2, "couldn't find sub-process thread's in thread list\n");
195
196     pe.dwSize = sizeof(pe);
197     ok(!pProcess32First( hSnapshot, &pe ), "shouldn't return a process\n");
198
199     me.dwSize = sizeof(me);
200     ok(!pModule32First( hSnapshot, &me ), "shouldn't return a module\n");
201
202     CloseHandle(hSnapshot);
203     ok(!pThread32First( hSnapshot, &te ), "shouldn't return a thread\n");
204 }
205
206 static const char* curr_expected_modules[] =
207 {
208     "kernel32.dll",
209     "kernel32_test.exe"
210     /* FIXME: could test for ntdll on NT and Wine */
211 };
212 static const char* sub_expected_modules[] =
213 {
214     "kernel32.dll",
215     "kernel32_test.exe",
216     "shell32.dll"
217     /* FIXME: could test for ntdll on NT and Wine */
218 };
219 #define NUM_OF(x) (sizeof(x) / sizeof(x[0]))
220
221 static void test_module(DWORD pid, const char* expected[], unsigned num_expected)
222 {
223     HANDLE              hSnapshot;
224     PROCESSENTRY32      pe;
225     THREADENTRY32       te;
226     MODULEENTRY32       me;
227     unsigned            found[32];
228     unsigned            i;
229     int                 num = 0;
230
231     ok(NUM_OF(found) >= num_expected, "Internal: bump found[] size\n");
232
233     hSnapshot = pCreateToolhelp32Snapshot( TH32CS_SNAPMODULE, pid );
234     ok(hSnapshot != NULL, "Cannot create snapshot\n");
235
236     for (i = 0; i < num_expected; i++) found[i] = 0;
237     me.dwSize = sizeof(me);
238     if (pModule32First( hSnapshot, &me ))
239     {
240         do
241         {
242             trace("PID=%x base=%p size=%x %s %s\n",
243                   me.th32ProcessID, me.modBaseAddr, me.modBaseSize, me.szExePath, me.szModule);
244             ok(me.th32ProcessID == pid, "wrong returned process id\n");
245             for (i = 0; i < num_expected; i++)
246                 if (!lstrcmpi(expected[i], me.szModule)) found[i]++;
247             num++;
248         } while (pModule32Next( hSnapshot, &me ));
249     }
250     for (i = 0; i < num_expected; i++)
251         ok(found[i] == 1, "Module %s is %s\n",
252            expected[i], found[i] ? "listed more than once" : "not listed");
253
254     /* check that first really resets the enumeration */
255     for (i = 0; i < num_expected; i++) found[i] = 0;
256     me.dwSize = sizeof(me);
257     if (pModule32First( hSnapshot, &me ))
258     {
259         do
260         {
261             trace("PID=%x base=%p size=%x %s %s\n",
262                   me.th32ProcessID, me.modBaseAddr, me.modBaseSize, me.szExePath, me.szModule);
263             for (i = 0; i < num_expected; i++)
264                 if (!lstrcmpi(expected[i], me.szModule)) found[i]++;
265             num--;
266         } while (pModule32Next( hSnapshot, &me ));
267     }
268     for (i = 0; i < num_expected; i++)
269         ok(found[i] == 1, "Module %s is %s\n",
270            expected[i], found[i] ? "listed more than once" : "not listed");
271     ok(!num, "mismatch in counting\n");
272
273     pe.dwSize = sizeof(pe);
274     ok(!pProcess32First( hSnapshot, &pe ), "shouldn't return a process\n");
275
276     me.dwSize = sizeof(me);
277     ok(!pThread32First( hSnapshot, &te ), "shouldn't return a thread\n");
278
279     CloseHandle(hSnapshot);
280     ok(!pModule32First( hSnapshot, &me ), "shouldn't return a module\n");
281 }
282
283 START_TEST(toolhelp)
284 {
285     DWORD               pid = GetCurrentProcessId();
286     int                 r;
287     char                buffer[MAX_PATH];
288     SECURITY_ATTRIBUTES sa;
289     PROCESS_INFORMATION info;
290     STARTUPINFOA        startup;
291     HANDLE              ev1, ev2;
292     DWORD               w;
293     HANDLE              hkernel32 = GetModuleHandleA("kernel32");
294
295     pCreateToolhelp32Snapshot = (VOID *) GetProcAddress(hkernel32, "CreateToolhelp32Snapshot");
296     pModule32First = (VOID *) GetProcAddress(hkernel32, "Module32First");
297     pModule32Next = (VOID *) GetProcAddress(hkernel32, "Module32Next");
298     pProcess32First = (VOID *) GetProcAddress(hkernel32, "Process32First");
299     pProcess32Next = (VOID *) GetProcAddress(hkernel32, "Process32Next");
300     pThread32First = (VOID *) GetProcAddress(hkernel32, "Thread32First");
301     pThread32Next = (VOID *) GetProcAddress(hkernel32, "Thread32Next");
302
303     if (!pCreateToolhelp32Snapshot || 
304         !pModule32First || !pModule32Next ||
305         !pProcess32First || !pProcess32Next ||
306         !pThread32First || !pThread32Next)
307     {
308         skip("Needed functions are not available, most likely running on Windows NT\n");
309         return;
310     }
311
312     r = init();
313     ok(r == 0, "Basic init of sub-process test\n");
314     if (r != 0) return;
315
316     sa.nLength = sizeof(sa);
317     sa.lpSecurityDescriptor = NULL;
318     sa.bInheritHandle = TRUE;
319
320     ev1 = CreateEvent(&sa, FALSE, FALSE, NULL);
321     ev2 = CreateEvent(&sa, FALSE, FALSE, NULL);
322     ok (ev1 != NULL && ev2 != NULL, "Couldn't create events\n");
323     memset(&startup, 0, sizeof(startup));
324     startup.cb = sizeof(startup);
325     startup.dwFlags = STARTF_USESHOWWINDOW;
326     startup.wShowWindow = SW_SHOWNORMAL;
327
328     sprintf(buffer, "%s tests/toolhelp.c %u %u", selfname, (DWORD)ev1, (DWORD)ev2);
329     ok(CreateProcessA(NULL, buffer, NULL, NULL, TRUE, 0, NULL, NULL, &startup, &info), "CreateProcess\n");
330     /* wait for child to be initialized */
331     w = WaitForSingleObject(ev1, WAIT_TIME);
332     ok(w == WAIT_OBJECT_0, "Failed to wait on sub-process startup\n");
333
334     test_process(pid, info.dwProcessId);
335     test_thread(pid, info.dwProcessId);
336     test_module(pid, curr_expected_modules, NUM_OF(curr_expected_modules));
337     test_module(info.dwProcessId, sub_expected_modules, NUM_OF(sub_expected_modules));
338
339     SetEvent(ev2);
340     w = WaitForSingleObject(info.hProcess, WAIT_TIME);
341     ok(w == WAIT_OBJECT_0, "Failed to wait on sub-process termination\n");
342     ok(GetExitCodeProcess(info.hProcess, &w), "couldn't get process exit code\n");
343     ok(w == WAIT_OBJECT_0, "Sub-Process failed to terminate properly\n");
344 }