[PATCH] [Kernel32]: added some tests for toolhelp functions

Eric Pouech eric.pouech at wanadoo.fr
Mon Apr 3 13:32:12 CDT 2006




A+
---

 dlls/kernel/tests/Makefile.in |    1 
 dlls/kernel/tests/toolhelp.c  |  203 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 204 insertions(+), 0 deletions(-)

diff --git a/dlls/kernel/tests/Makefile.in b/dlls/kernel/tests/Makefile.in
index 9543047..80eb401 100644
--- a/dlls/kernel/tests/Makefile.in
+++ b/dlls/kernel/tests/Makefile.in
@@ -30,6 +30,7 @@ CTESTS = \
 	thread.c \
 	time.c \
 	timer.c \
+	toolhelp.c \
 	virtual.c \
 	volume.c
 
diff --git a/dlls/kernel/tests/toolhelp.c b/dlls/kernel/tests/toolhelp.c
new file mode 100644
index 0000000..16122d9
--- /dev/null
+++ b/dlls/kernel/tests/toolhelp.c
@@ -0,0 +1,203 @@
+/*
+ * Toolhelp
+ *
+ * Copyright 2005 Eric Pouech
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#include <stdarg.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <windef.h>
+#include <winbase.h>
+
+#include "tlhelp32.h"
+#include "wine/test.h"
+
+static void test_process(DWORD curr_pid)
+{
+    HANDLE hSnapshot;
+    PROCESSENTRY32 pe;
+    THREADENTRY32 te;
+    MODULEENTRY32 me;
+    BOOL found = FALSE;
+    int num = 0;
+
+    hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
+    ok(hSnapshot != NULL, "Cannot create snapshot\n");
+
+    /* check that this current process is enumerated */
+    pe.dwSize = sizeof(pe);
+    if (Process32First( hSnapshot, &pe ))
+    {
+        do
+        {
+            if (pe.th32ProcessID == curr_pid) found = TRUE;
+            trace("PID=%lx %s\n", pe.th32ProcessID, pe.szExeFile);
+            num++;
+        } while (Process32Next( hSnapshot, &pe ));
+    }
+    ok(found, "couldn't find self in process list\n");
+
+    /* check that first really resets the enumeration */
+    found = FALSE;
+    if (Process32First( hSnapshot, &pe ))
+    {
+        do
+        {
+            if (pe.th32ProcessID == curr_pid) found = TRUE;
+            trace("PID=%lx %s\n", pe.th32ProcessID, pe.szExeFile);
+            num--;
+        } while (Process32Next( hSnapshot, &pe ));
+    }
+    ok(found, "couldn't find self in process list\n");
+    ok(!num, "mismatch in counting\n");
+
+    te.dwSize = sizeof(te);
+    ok(!Thread32First( hSnapshot, &te ), "shouldn't return a thread\n");
+
+    me.dwSize = sizeof(me);
+    ok(!Module32First( hSnapshot, &me ), "shouldn't return a module\n");
+
+    CloseHandle(hSnapshot);
+    ok(!Process32First( hSnapshot, &pe ), "shouldn't return a process\n");
+}
+
+static void test_thread(DWORD curr_pid)
+{
+    HANDLE hSnapshot;
+    PROCESSENTRY32 pe;
+    THREADENTRY32 te;
+    MODULEENTRY32 me;
+    int num = 0;
+
+    BOOL found = FALSE;
+    
+    hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPTHREAD, 0 );
+    ok(hSnapshot != NULL, "Cannot create snapshot\n");
+
+    /* check that this current process is enumerated */
+    te.dwSize = sizeof(te);
+    if (Thread32First( hSnapshot, &te ))
+    {
+        do
+        {
+            if (te.th32OwnerProcessID == curr_pid) found = TRUE;
+            trace("PID=%lx TID=%lx %ld\n", te.th32OwnerProcessID, te.th32ThreadID, te.tpBasePri);
+            num++;
+        } while (Thread32Next( hSnapshot, &te ));
+    }
+    ok(found, "couldn't find self in process list\n");
+
+    /* check that first really resets enumeration */
+    found = FALSE;
+    if (Thread32First( hSnapshot, &te ))
+    {
+        do
+        {
+            if (te.th32OwnerProcessID == curr_pid) found = TRUE;
+            trace("PID=%lx TID=%lx %ld\n", te.th32OwnerProcessID, te.th32ThreadID, te.tpBasePri);
+            num--;
+        } while (Thread32Next( hSnapshot, &te ));
+    }
+    ok(found, "couldn't find self in process list\n");
+    ok(!num, "mismatch in counting\n");
+
+    pe.dwSize = sizeof(pe);
+    ok(!Process32First( hSnapshot, &pe ), "shouldn't return a process\n");
+
+    me.dwSize = sizeof(me);
+    ok(!Module32First( hSnapshot, &me ), "shouldn't return a module\n");
+
+    CloseHandle(hSnapshot);
+    ok(!Thread32First( hSnapshot, &te ), "shouldn't return a thread\n");
+}
+
+static const char* expected_modules[] = {
+    "kernel32.dll",
+    "kernel32_test.exe"
+    /* FIXME: could test for ntdll on NT and Wine */
+};
+#define NUM_EXP_MOD (sizeof(expected_modules) / sizeof(expected_modules[0]))
+
+static void test_module(DWORD pid)
+{
+    HANDLE hSnapshot;
+    PROCESSENTRY32 pe;
+    THREADENTRY32 te;
+    MODULEENTRY32 me;
+    unsigned found[NUM_EXP_MOD];
+    int i, num = 0;
+
+    hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, 0 );
+    ok(hSnapshot != NULL, "Cannot create snapshot\n");
+
+    for (i = 0; i < NUM_EXP_MOD; i++) found[i] = 0;
+    me.dwSize = sizeof(me);
+    if (Module32First( hSnapshot, &me ))
+    {
+        do
+        {
+            trace("PID=%lx base=%p size=%lx %s %s\n",
+                  me.th32ProcessID, me.modBaseAddr, me.modBaseSize, me.szExePath, me.szModule);
+            ok(me.th32ProcessID == pid, "wrong returned process id");
+            for (i = 0; i < NUM_EXP_MOD; i++)
+                if (!strcmp(expected_modules[i], me.szModule)) found[i]++;
+            ok(GetModuleHandleA(me.szExePath) == me.hModule, "Wrong module handle\n");
+            num++;
+        } while (Module32Next( hSnapshot, &me ));
+    }
+    for (i = 0; i < NUM_EXP_MOD; i++)
+        ok(found[i] == 1, "Module %s is %s\n",
+           expected_modules[i], found[i] ? "listed more than once" : "not listed");
+
+    /* check that first really resets the enumeration */
+    for (i = 0; i < NUM_EXP_MOD; i++) found[i] = 0;
+    me.dwSize = sizeof(me);
+    if (Module32First( hSnapshot, &me ))
+    {
+        do
+        {
+            trace("PID=%lx base=%p size=%lx %s %s\n",
+                  me.th32ProcessID, me.modBaseAddr, me.modBaseSize, me.szExePath, me.szModule);
+            for (i = 0; i < NUM_EXP_MOD; i++)
+                if (!strcmp(expected_modules[i], me.szModule)) found[i]++;
+            num--;
+        } while (Module32Next( hSnapshot, &me ));
+    }
+    for (i = 0; i < NUM_EXP_MOD; i++)
+        ok(found[i] == 1, "Module %s is %s\n",
+           expected_modules[i], found[i] ? "listed more than once" : "not listed");
+    ok(!num, "mismatch in counting\n");
+
+    pe.dwSize = sizeof(pe);
+    ok(!Process32First( hSnapshot, &pe ), "shouldn't return a process\n");
+
+    me.dwSize = sizeof(me);
+    ok(!Thread32First( hSnapshot, &te ), "shouldn't return a thread\n");
+
+    CloseHandle(hSnapshot);
+    ok(!Module32First( hSnapshot, &me ), "shouldn't return a module\n");
+}
+
+START_TEST(toolhelp)
+{
+    DWORD pid = GetCurrentProcessId();
+
+    test_process(pid);
+    test_thread(pid);
+    test_module(pid);
+}





More information about the wine-patches mailing list