~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Wine Cross Reference
wine/dlls/kernel32/tests/toolhelp.c

Version: ~ [ wine-1.1.2 ] ~ [ wine-1.1.1 ] ~ [ wine-1.1.0 ] ~ [ wine-1.0 ] ~ [ wine-1.0-rc5 ] ~ [ wine-1.0-rc4 ] ~ [ wine-1.0-rc3 ] ~ [ wine-1.0-rc2 ] ~ [ wine-1.0-rc1 ] ~ [ wine-0.9.61 ] ~ [ wine-0.9.60 ] ~ [ wine-0.9.59 ] ~ [ wine-0.9.58 ] ~ [ wine-0.9.57 ] ~ [ wine-0.9.56 ] ~ [ wine-0.9.55 ] ~ [ wine-0.9.54 ] ~ [ wine-0.9.53 ] ~ [ wine-0.9.52 ] ~ [ wine-0.9.51 ] ~ [ wine-0.9.50 ] ~ [ wine-0.9.49 ] ~ [ wine-0.9.48 ] ~ [ wine-0.9.47 ] ~ [ wine-0.9.46 ] ~ [ wine-0.9.45 ] ~ [ wine-0.9.44 ] ~ [ wine-0.9.43 ] ~ [ wine-0.9.42 ] ~ [ wine-0.9.41 ] ~ [ wine-0.9.40 ] ~ [ wine-0.9.39 ] ~ [ wine-0.9.38 ] ~ [ wine-0.9.37 ] ~ [ wine-0.9.36 ] ~ [ wine-0.9.35 ] ~ [ wine-0.9.34 ] ~ [ wine-0.9.33 ] ~ [ wine-0.9.32 ] ~ [ wine-0.9.31 ] ~ [ wine-0.9.30 ] ~ [ wine-0.9.29 ] ~ [ wine-0.9.28 ] ~ [ wine-0.9.27 ] ~ [ wine-0.9.26 ] ~ [ wine-0.9.25 ] ~ [ wine-0.9.24 ] ~ [ wine-0.9.23 ] ~ [ wine-0.9.22 ] ~ [ wine-0.9.21 ] ~ [ wine-0.9.20 ] ~ [ wine-0.9.19 ] ~ [ wine-0.9.18 ] ~ [ wine-0.9.17 ] ~ [ wine-0.9.16 ] ~ [ wine-0.9.15 ] ~ [ wine-0.9.14 ] ~ [ wine-0.9.13 ] ~ [ wine-0.9.12 ] ~ [ wine-0.9.11 ] ~ [ wine-0.9.10 ] ~ [ wine-0.9.9 ] ~ [ wine-0.9.8 ] ~ [ wine-0.9.7 ] ~ [ wine-0.9.6 ] ~ [ wine-0.9.5 ] ~ [ wine-0.9.4 ] ~ [ wine-0.9.3 ] ~ [ wine-0.9.2 ] ~ [ wine-0.9.1 ] ~ [ wine-0.9 ] ~ [ wine20050930 ] ~ [ wine20050830 ] ~ [ wine20050725 ] ~ [ wine20050628 ] ~ [ wine20050524 ] ~ [ wine20050419 ] ~ [ wine20050310 ] ~ [ wine20050211 ] ~ [ wine20050111 ] ~ [ wine20041201 ] ~ [ wine20041019 ] ~ [ wine20040914 ] ~ [ wine20040813 ] ~ [ wine20040716 ] ~ [ wine20040615 ] ~ [ wine20040505 ] ~ [ wine20040408 ] ~ [ wine20040309 ] ~ [ wine20040213 ] ~ [ wine20040121 ] ~ [ wine20031212 ] ~ [ wine20031118 ] ~ [ wine20031016 ] ~ [ wine20030911 ] ~ [ wine20030813 ] ~ [ wine20030709 ] ~ [ wine20030618 ] ~ [ wine20030508 ] ~ [ wine20030408 ] ~ [ wine20030318 ] ~ [ wine20030219 ] ~ [ wine20030115 ] ~ [ wine20021219 ] ~ [ wine20021125 ] ~ [ wine20021031 ] ~ [ wine20021007 ] ~ [ wine20020904 ] ~ [ wine20020804 ] ~ [ wine20020710 ] ~ [ wine20020605 ] ~ [ wine20020509 ] ~ [ wine20020411 ] ~ [ wine20020310 ] ~ [ wine20020228 ] ~ [ wine20011226 ] ~ [ wine20011108 ] ~ [ wine20011004 ] ~ [ wine20010824 ] ~ [ wine20010731 ] ~ [ wine20010629 ] ~ [ wine20010510 ] ~ [ wine20010418 ] ~ [ wine20010326 ] ~ [ wine20010305 ] ~ [ wine20010216 ] ~ [ wine20010112 ] ~ [ wine20001222 ] ~ [ wine20001202 ] ~ [ wine20001026 ] ~ [ wine20001002 ] ~ [ wine20000909 ] ~ [ wine20000821 ] ~ [ wine20000801 ] ~ [ wine20000716 ] ~ [ wine20000326 ] ~ [ wine20000227 ] ~ [ wine20000130 ] ~ [ wine20000109 ] ~

  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     MODULEENTRY32       me;
106     unsigned            found = 0;
107     int                 num = 0;
108     int                 childpos = -1;
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) { childpos = num; 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     /* one broken program does Process32First() and does not expect anything
143      * interesting to be there, especially not the just forked off child */
144     ok (childpos !=0, "child is not expected to be at position 0.\n");
145 
146     me.dwSize = sizeof(me);
147     ok(!pModule32First( hSnapshot, &me ), "shouldn't return a module\n");
148 
149     CloseHandle(hSnapshot);
150     ok(!pProcess32First( hSnapshot, &pe ), "shouldn't return a process\n");
151 }
152 
153 static void test_thread(DWORD curr_pid, DWORD sub_pcs_pid)
154 {
155     HANDLE              hSnapshot;
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             if (winetest_debug > 1)
174                 trace("PID=%x TID=%x %d\n", te.th32OwnerProcessID, te.th32ThreadID, te.tpBasePri);
175             num++;
176         } while (pThread32Next( hSnapshot, &te ));
177     }
178     ok(curr_found == 1, "couldn't find self in thread list\n");
179     ok(sub_found == 2, "couldn't find sub-process thread's in thread list\n");
180 
181     /* check that first really resets enumeration */
182     curr_found = 0;
183     sub_found = 0;
184     if (pThread32First( hSnapshot, &te ))
185     {
186         do
187         {
188             if (te.th32OwnerProcessID == curr_pid) curr_found++;
189             if (te.th32OwnerProcessID == sub_pcs_pid) sub_found++;
190             if (winetest_debug > 1)
191                 trace("PID=%x TID=%x %d\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     me.dwSize = sizeof(me);
199     ok(!pModule32First( hSnapshot, &me ), "shouldn't return a module\n");
200 
201     CloseHandle(hSnapshot);
202     ok(!pThread32First( hSnapshot, &te ), "shouldn't return a thread\n");
203 }
204 
205 static const char* curr_expected_modules[] =
206 {
207     "kernel32.dll",
208     "kernel32_test.exe"
209     /* FIXME: could test for ntdll on NT and Wine */
210 };
211 static const char* sub_expected_modules[] =
212 {
213     "kernel32.dll",
214     "kernel32_test.exe",
215     "shell32.dll"
216     /* FIXME: could test for ntdll on NT and Wine */
217 };
218 #define NUM_OF(x) (sizeof(x) / sizeof(x[0]))
219 
220 static void test_module(DWORD pid, const char* expected[], unsigned num_expected)
221 {
222     HANDLE              hSnapshot;
223     PROCESSENTRY32      pe;
224     THREADENTRY32       te;
225     MODULEENTRY32       me;
226     unsigned            found[32];
227     unsigned            i;
228     int                 num = 0;
229 
230     ok(NUM_OF(found) >= num_expected, "Internal: bump found[] size\n");
231 
232     hSnapshot = pCreateToolhelp32Snapshot( TH32CS_SNAPMODULE, pid );
233     ok(hSnapshot != NULL, "Cannot create snapshot\n");
234 
235     for (i = 0; i < num_expected; i++) found[i] = 0;
236     me.dwSize = sizeof(me);
237     if (pModule32First( hSnapshot, &me ))
238     {
239         do
240         {
241             trace("PID=%x base=%p size=%x %s %s\n",
242                   me.th32ProcessID, me.modBaseAddr, me.modBaseSize, me.szExePath, me.szModule);
243             ok(me.th32ProcessID == pid, "wrong returned process id\n");
244             for (i = 0; i < num_expected; i++)
245                 if (!lstrcmpi(expected[i], me.szModule)) found[i]++;
246             num++;
247         } while (pModule32Next( hSnapshot, &me ));
248     }
249     for (i = 0; i < num_expected; i++)
250         ok(found[i] == 1, "Module %s is %s\n",
251            expected[i], found[i] ? "listed more than once" : "not listed");
252 
253     /* check that first really resets the enumeration */
254     for (i = 0; i < num_expected; i++) found[i] = 0;
255     me.dwSize = sizeof(me);
256     if (pModule32First( hSnapshot, &me ))
257     {
258         do
259         {
260             trace("PID=%x base=%p size=%x %s %s\n",
261                   me.th32ProcessID, me.modBaseAddr, me.modBaseSize, me.szExePath, me.szModule);
262             for (i = 0; i < num_expected; i++)
263                 if (!lstrcmpi(expected[i], me.szModule)) found[i]++;
264             num--;
265         } while (pModule32Next( hSnapshot, &me ));
266     }
267     for (i = 0; i < num_expected; i++)
268         ok(found[i] == 1, "Module %s is %s\n",
269            expected[i], found[i] ? "listed more than once" : "not listed");
270     ok(!num, "mismatch in counting\n");
271 
272     pe.dwSize = sizeof(pe);
273     ok(!pProcess32First( hSnapshot, &pe ), "shouldn't return a process\n");
274 
275     te.dwSize = sizeof(te);
276     ok(!pThread32First( hSnapshot, &te ), "shouldn't return a thread\n");
277 
278     CloseHandle(hSnapshot);
279     ok(!pModule32First( hSnapshot, &me ), "shouldn't return a module\n");
280 }
281 
282 START_TEST(toolhelp)
283 {
284     DWORD               pid = GetCurrentProcessId();
285     int                 r;
286     char                buffer[MAX_PATH];
287     SECURITY_ATTRIBUTES sa;
288     PROCESS_INFORMATION info;
289     STARTUPINFOA        startup;
290     HANDLE              ev1, ev2;
291     DWORD               w;
292     HANDLE              hkernel32 = GetModuleHandleA("kernel32");
293 
294     pCreateToolhelp32Snapshot = (VOID *) GetProcAddress(hkernel32, "CreateToolhelp32Snapshot");
295     pModule32First = (VOID *) GetProcAddress(hkernel32, "Module32First");
296     pModule32Next = (VOID *) GetProcAddress(hkernel32, "Module32Next");
297     pProcess32First = (VOID *) GetProcAddress(hkernel32, "Process32First");
298     pProcess32Next = (VOID *) GetProcAddress(hkernel32, "Process32Next");
299     pThread32First = (VOID *) GetProcAddress(hkernel32, "Thread32First");
300     pThread32Next = (VOID *) GetProcAddress(hkernel32, "Thread32Next");
301 
302     if (!pCreateToolhelp32Snapshot || 
303         !pModule32First || !pModule32Next ||
304         !pProcess32First || !pProcess32Next ||
305         !pThread32First || !pThread32Next)
306     {
307         skip("Needed functions are not available, most likely running on Windows NT\n");
308         return;
309     }
310 
311     r = init();
312     ok(r == 0, "Basic init of sub-process test\n");
313     if (r != 0) return;
314 
315     sa.nLength = sizeof(sa);
316     sa.lpSecurityDescriptor = NULL;
317     sa.bInheritHandle = TRUE;
318 
319     ev1 = CreateEvent(&sa, FALSE, FALSE, NULL);
320     ev2 = CreateEvent(&sa, FALSE, FALSE, NULL);
321     ok (ev1 != NULL && ev2 != NULL, "Couldn't create events\n");
322     memset(&startup, 0, sizeof(startup));
323     startup.cb = sizeof(startup);
324     startup.dwFlags = STARTF_USESHOWWINDOW;
325     startup.wShowWindow = SW_SHOWNORMAL;
326 
327     sprintf(buffer, "%s tests/toolhelp.c %u %u", selfname, (DWORD)ev1, (DWORD)ev2);
328     ok(CreateProcessA(NULL, buffer, NULL, NULL, TRUE, 0, NULL, NULL, &startup, &info), "CreateProcess\n");
329     /* wait for child to be initialized */
330     w = WaitForSingleObject(ev1, WAIT_TIME);
331     ok(w == WAIT_OBJECT_0, "Failed to wait on sub-process startup\n");
332 
333     test_process(pid, info.dwProcessId);
334     test_thread(pid, info.dwProcessId);
335     test_module(pid, curr_expected_modules, NUM_OF(curr_expected_modules));
336     test_module(info.dwProcessId, sub_expected_modules, NUM_OF(sub_expected_modules));
337 
338     SetEvent(ev2);
339     winetest_wait_child_process( info.hProcess );
340 }
341 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.