4 * Copyright 2005 Eric Pouech
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.
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.
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
28 #include "wine/test.h"
33 static char selfname[MAX_PATH];
35 /* 1 minute should be more than enough */
36 #define WAIT_TIME (60 * 1000)
38 static DWORD WINAPI sub_thread(void* pmt)
40 DWORD w = WaitForSingleObject((HANDLE)pmt, WAIT_TIME);
44 /******************************************************************
47 * generates basic information like:
48 * selfname: the way to reinvoke ourselves
52 * doesn't return if child
58 HANDLE ev1, ev2, ev3, hThread;
61 argc = winetest_get_mainargs( &argv );
62 strcpy(selfname, argv[0]);
66 case 2: /* the test program */
68 case 4: /* the sub-process */
69 ev1 = (HANDLE)atoi(argv[2]);
70 ev2 = (HANDLE)atoi(argv[3]);
71 ev3 = CreateEvent(NULL, FALSE, FALSE, NULL);
73 if (ev3 == NULL) ExitProcess(WAIT_ABANDONED);
74 hThread = CreateThread(NULL, 0, sub_thread, ev3, 0, NULL);
75 if (hThread == NULL) ExitProcess(WAIT_ABANDONED);
76 if (!LoadLibraryA("shell32.dll")) ExitProcess(WAIT_ABANDONED);
78 /* signal init of sub-process is done */
80 /* wait for parent to have done all its queries */
81 w = WaitForSingleObject(ev2, WAIT_TIME);
82 if (w != WAIT_OBJECT_0) ExitProcess(w);
83 /* signal sub-thread to terminate */
85 w = WaitForSingleObject(hThread, WAIT_TIME);
86 if (w != WAIT_OBJECT_0) ExitProcess(w);
87 GetExitCodeThread(hThread, &w);
94 static void test_process(DWORD curr_pid, DWORD sub_pcs_pid)
103 hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
104 ok(hSnapshot != NULL, "Cannot create snapshot\n");
106 /* check that this current process is enumerated */
107 pe.dwSize = sizeof(pe);
108 if (Process32First( hSnapshot, &pe ))
112 if (pe.th32ProcessID == curr_pid) found++;
113 if (pe.th32ProcessID == sub_pcs_pid) found++;
114 trace("PID=%lx %s\n", pe.th32ProcessID, pe.szExeFile);
116 } while (Process32Next( hSnapshot, &pe ));
118 ok(found == 2, "couldn't find self and/or sub-process in process list\n");
120 /* check that first really resets the enumeration */
122 if (Process32First( hSnapshot, &pe ))
126 if (pe.th32ProcessID == curr_pid) found++;
127 if (pe.th32ProcessID == sub_pcs_pid) found++;
128 trace("PID=%lx %s\n", pe.th32ProcessID, pe.szExeFile);
130 } while (Process32Next( hSnapshot, &pe ));
132 ok(found == 2, "couldn't find self and/or sub-process in process list\n");
133 ok(!num, "mismatch in counting\n");
135 te.dwSize = sizeof(te);
136 ok(!Thread32First( hSnapshot, &te ), "shouldn't return a thread\n");
138 me.dwSize = sizeof(me);
139 ok(!Module32First( hSnapshot, &me ), "shouldn't return a module\n");
141 CloseHandle(hSnapshot);
142 ok(!Process32First( hSnapshot, &pe ), "shouldn't return a process\n");
145 static void test_thread(DWORD curr_pid, DWORD sub_pcs_pid)
152 unsigned curr_found = 0;
153 unsigned sub_found = 0;
155 hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPTHREAD, 0 );
156 ok(hSnapshot != NULL, "Cannot create snapshot\n");
158 /* check that this current process is enumerated */
159 te.dwSize = sizeof(te);
160 if (Thread32First( hSnapshot, &te ))
164 if (te.th32OwnerProcessID == curr_pid) curr_found++;
165 if (te.th32OwnerProcessID == sub_pcs_pid) sub_found++;
166 trace("PID=%lx TID=%lx %ld\n", te.th32OwnerProcessID, te.th32ThreadID, te.tpBasePri);
168 } while (Thread32Next( hSnapshot, &te ));
170 ok(curr_found == 1, "couldn't find self in thread list\n");
171 ok(sub_found == 2, "couldn't find sub-process thread's in thread list\n");
173 /* check that first really resets enumeration */
176 if (Thread32First( hSnapshot, &te ))
180 if (te.th32OwnerProcessID == curr_pid) curr_found++;
181 if (te.th32OwnerProcessID == sub_pcs_pid) sub_found++;
182 trace("PID=%lx TID=%lx %ld\n", te.th32OwnerProcessID, te.th32ThreadID, te.tpBasePri);
184 } while (Thread32Next( hSnapshot, &te ));
186 ok(curr_found == 1, "couldn't find self in thread list\n");
187 ok(sub_found == 2, "couldn't find sub-process thread's in thread list\n");
189 pe.dwSize = sizeof(pe);
190 ok(!Process32First( hSnapshot, &pe ), "shouldn't return a process\n");
192 me.dwSize = sizeof(me);
193 ok(!Module32First( hSnapshot, &me ), "shouldn't return a module\n");
195 CloseHandle(hSnapshot);
196 ok(!Thread32First( hSnapshot, &te ), "shouldn't return a thread\n");
199 static const char* curr_expected_modules[] =
203 /* FIXME: could test for ntdll on NT and Wine */
205 static const char* sub_expected_modules[] =
210 /* FIXME: could test for ntdll on NT and Wine */
212 #define NUM_OF(x) (sizeof(x) / sizeof(x[0]))
214 static void test_module(DWORD pid, const char* expected[], unsigned num_expected)
223 ok(NUM_OF(found) >= num_expected, "Internal: bump found[] size\n");
225 hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, pid );
226 ok(hSnapshot != NULL, "Cannot create snapshot\n");
228 for (i = 0; i < num_expected; i++) found[i] = 0;
229 me.dwSize = sizeof(me);
230 if (Module32First( hSnapshot, &me ))
234 trace("PID=%lx base=%p size=%lx %s %s\n",
235 me.th32ProcessID, me.modBaseAddr, me.modBaseSize, me.szExePath, me.szModule);
236 ok(me.th32ProcessID == pid, "wrong returned process id");
237 for (i = 0; i < num_expected; i++)
238 if (!strcmp(expected[i], me.szModule)) found[i]++;
240 } while (Module32Next( hSnapshot, &me ));
242 for (i = 0; i < num_expected; i++)
243 ok(found[i] == 1, "Module %s is %s\n",
244 expected[i], found[i] ? "listed more than once" : "not listed");
246 /* check that first really resets the enumeration */
247 for (i = 0; i < num_expected; i++) found[i] = 0;
248 me.dwSize = sizeof(me);
249 if (Module32First( hSnapshot, &me ))
253 trace("PID=%lx base=%p size=%lx %s %s\n",
254 me.th32ProcessID, me.modBaseAddr, me.modBaseSize, me.szExePath, me.szModule);
255 for (i = 0; i < num_expected; i++)
256 if (!strcmp(expected[i], me.szModule)) found[i]++;
258 } while (Module32Next( hSnapshot, &me ));
260 for (i = 0; i < num_expected; i++)
261 ok(found[i] == 1, "Module %s is %s\n",
262 expected[i], found[i] ? "listed more than once" : "not listed");
263 ok(!num, "mismatch in counting\n");
265 pe.dwSize = sizeof(pe);
266 ok(!Process32First( hSnapshot, &pe ), "shouldn't return a process\n");
268 me.dwSize = sizeof(me);
269 ok(!Thread32First( hSnapshot, &te ), "shouldn't return a thread\n");
271 CloseHandle(hSnapshot);
272 ok(!Module32First( hSnapshot, &me ), "shouldn't return a module\n");
277 DWORD pid = GetCurrentProcessId();
279 char buffer[MAX_PATH];
280 SECURITY_ATTRIBUTES sa;
281 PROCESS_INFORMATION info;
282 STARTUPINFOA startup;
287 ok(r == 0, "Basic init of sub-process test\n");
290 sa.nLength = sizeof(sa);
291 sa.lpSecurityDescriptor = NULL;
292 sa.bInheritHandle = TRUE;
294 ev1 = CreateEvent(&sa, FALSE, FALSE, NULL);
295 ev2 = CreateEvent(&sa, FALSE, FALSE, NULL);
296 ok (ev1 != NULL && ev2 != NULL, "Couldn't create events\n");
297 memset(&startup, 0, sizeof(startup));
298 startup.cb = sizeof(startup);
299 startup.dwFlags = STARTF_USESHOWWINDOW;
300 startup.wShowWindow = SW_SHOWNORMAL;
302 sprintf(buffer, "%s tests/toolhelp.c %lu %lu", selfname, (DWORD)ev1, (DWORD)ev2);
303 ok(CreateProcessA(NULL, buffer, NULL, NULL, TRUE, 0, NULL, NULL, &startup, &info), "CreateProcess\n");
304 /* wait for child to be initialized */
305 w = WaitForSingleObject(ev1, WAIT_TIME);
306 ok(w == WAIT_OBJECT_0, "Failed to wait on sub-process startup\n");
308 test_process(pid, info.dwProcessId);
309 test_thread(pid, info.dwProcessId);
310 test_module(pid, curr_expected_modules, NUM_OF(curr_expected_modules));
311 test_module(info.dwProcessId, sub_expected_modules, NUM_OF(sub_expected_modules));
314 w = WaitForSingleObject(info.hProcess, WAIT_TIME);
315 ok(w == WAIT_OBJECT_0, "Failed to wait on sub-process termination\n");
316 ok(GetExitCodeProcess(info.hProcess, &w), "couldn't get process exit code\n");
317 ok(w == WAIT_OBJECT_0, "Sub-Process failed to terminate properly\n");