1 /* 2 * Unit test suite for process functions 3 * 4 * Copyright 2017 Michael Müller 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 <stdio.h> 22 23 #include "ntdll_test.h" 24 25 #include "windef.h" 26 #include "winbase.h" 27 28 static NTSTATUS (WINAPI *pNtResumeProcess)(HANDLE); 29 static NTSTATUS (WINAPI *pNtSuspendProcess)(HANDLE); 30 static NTSTATUS (WINAPI *pNtSuspendThread)(HANDLE,PULONG); 31 static NTSTATUS (WINAPI *pNtResumeThread)(HANDLE); 32 33 static void test_NtSuspendProcess(char *process_name) 34 { 35 PROCESS_INFORMATION info; 36 DEBUG_EVENT ev; 37 STARTUPINFOA startup; 38 NTSTATUS status; 39 HANDLE event; 40 char buffer[MAX_PATH]; 41 ULONG count; 42 DWORD ret; 43 44 status = pNtResumeProcess(GetCurrentProcess()); 45 ok(status == STATUS_SUCCESS, "NtResumeProcess failed: %x\n", status); 46 47 event = CreateEventA(NULL, TRUE, FALSE, "wine_suspend_event"); 48 ok(!!event, "Failed to create event: %u\n", GetLastError()); 49 50 memset(&startup, 0, sizeof(startup)); 51 startup.cb = sizeof(startup); 52 53 sprintf(buffer, "%s tests/process.c dummy_process wine_suspend_event", process_name); 54 ret = CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &startup, &info); 55 ok(ret, "CreateProcess failed with error %u\n", GetLastError()); 56 57 ret = WaitForSingleObject(event, 500); 58 ok(ret == WAIT_OBJECT_0, "Event was not signaled: %d\n", ret); 59 60 status = pNtSuspendProcess(info.hProcess); 61 ok(status == STATUS_SUCCESS, "NtResumeProcess failed: %x\n", status); 62 63 ResetEvent(event); 64 65 ret = WaitForSingleObject(event, 200); 66 ok(ret == WAIT_TIMEOUT, "Expected timeout, got: %d\n", ret); 67 68 status = NtResumeThread(info.hThread, &count); 69 ok(status == STATUS_SUCCESS, "NtResumeProcess failed: %x\n", status); 70 ok(count == 1, "Expected count 1, got %d\n", count); 71 72 ret = WaitForSingleObject(event, 200); 73 ok(ret == WAIT_OBJECT_0, "Event was not signaled: %d\n", ret); 74 75 status = pNtResumeProcess(info.hProcess); 76 ok(status == STATUS_SUCCESS, "NtResumeProcess failed: %x\n", status); 77 78 status = pNtSuspendThread(info.hThread, &count); 79 ok(status == STATUS_SUCCESS, "NtSuspendThread failed: %x\n", status); 80 ok(count == 0, "Expected count 0, got %d\n", count); 81 82 ResetEvent(event); 83 84 ret = WaitForSingleObject(event, 200); 85 ok(ret == WAIT_TIMEOUT, "Expected timeout, got: %d\n", ret); 86 87 status = pNtResumeProcess(info.hProcess); 88 ok(status == STATUS_SUCCESS, "NtResumeProcess failed: %x\n", status); 89 90 ret = WaitForSingleObject(event, 200); 91 ok(ret == WAIT_OBJECT_0, "Event was not signaled: %d\n", ret); 92 93 status = pNtSuspendThread(info.hThread, &count); 94 ok(status == STATUS_SUCCESS, "NtSuspendThread failed: %x\n", status); 95 ok(count == 0, "Expected count 0, got %d\n", count); 96 97 status = pNtSuspendThread(info.hThread, &count); 98 ok(status == STATUS_SUCCESS, "NtSuspendThread failed: %x\n", status); 99 ok(count == 1, "Expected count 1, got %d\n", count); 100 101 ResetEvent(event); 102 103 ret = WaitForSingleObject(event, 200); 104 ok(ret == WAIT_TIMEOUT, "Expected timeout, got: %d\n", ret); 105 106 status = pNtResumeProcess(info.hProcess); 107 ok(status == STATUS_SUCCESS, "NtResumeProcess failed: %x\n", status); 108 109 ret = WaitForSingleObject(event, 200); 110 ok(ret == WAIT_TIMEOUT, "Expected timeout, got: %d\n", ret); 111 112 status = pNtResumeProcess(info.hProcess); 113 ok(status == STATUS_SUCCESS, "NtResumeProcess failed: %x\n", status); 114 115 ret = WaitForSingleObject(event, 200); 116 ok(ret == WAIT_OBJECT_0, "Event was not signaled: %d\n", ret); 117 118 ret = DebugActiveProcess(info.dwProcessId); 119 ok(ret, "Failed to debug process: %d\n", GetLastError()); 120 121 ResetEvent(event); 122 123 ret = WaitForSingleObject(event, 200); 124 ok(ret == WAIT_TIMEOUT, "Expected timeout, got: %d\n", ret); 125 126 for (;;) 127 { 128 ret = WaitForDebugEvent(&ev, INFINITE); 129 ok(ret, "WaitForDebugEvent failed, last error %#x.\n", GetLastError()); 130 if (!ret) break; 131 132 if (ev.dwDebugEventCode == OUTPUT_DEBUG_STRING_EVENT) break; 133 134 ret = ContinueDebugEvent(ev.dwProcessId, ev.dwThreadId, DBG_CONTINUE); 135 ok(ret, "ContinueDebugEvent failed, last error %#x.\n", GetLastError()); 136 if (!ret) break; 137 } 138 139 ResetEvent(event); 140 141 ret = WaitForSingleObject(event, 200); 142 ok(ret == WAIT_TIMEOUT, "Expected timeout, got: %d\n", ret); 143 144 status = pNtResumeProcess(info.hProcess); 145 ok(status == STATUS_SUCCESS, "NtResumeProcess failed: %x\n", status); 146 147 ret = WaitForSingleObject(event, 200); 148 ok(ret == WAIT_TIMEOUT, "Expected timeout, got: %d\n", ret); 149 150 status = NtResumeThread(info.hThread, &count); 151 ok(status == STATUS_SUCCESS, "NtResumeProcess failed: %x\n", status); 152 ok(count == 0, "Expected count 0, got %d\n", count); 153 154 ret = WaitForSingleObject(event, 200); 155 ok(ret == WAIT_TIMEOUT, "Expected timeout, got: %d\n", ret); 156 157 ret = ContinueDebugEvent(ev.dwProcessId, ev.dwThreadId, DBG_CONTINUE); 158 ok(ret, "ContinueDebugEvent failed, last error %#x.\n", GetLastError()); 159 160 ret = WaitForSingleObject(event, 200); 161 ok(ret == WAIT_OBJECT_0, "Event was not signaled: %d\n", ret); 162 163 TerminateProcess(info.hProcess, 0); 164 165 CloseHandle(info.hProcess); 166 CloseHandle(info.hThread); 167 } 168 169 static void dummy_process(char *event_name) 170 { 171 HANDLE event = OpenEventA(EVENT_ALL_ACCESS, FALSE, event_name); 172 173 while (TRUE) 174 { 175 SetEvent(event); 176 OutputDebugStringA("test"); 177 Sleep(5); 178 } 179 } 180 181 START_TEST(process) 182 { 183 HMODULE mod; 184 char **argv; 185 int argc; 186 187 argc = winetest_get_mainargs(&argv); 188 if (argc >= 4 && strcmp(argv[2], "dummy_process") == 0) 189 { 190 dummy_process(argv[3]); 191 return; 192 } 193 194 mod = GetModuleHandleA("ntdll.dll"); 195 if (!mod) 196 { 197 win_skip("Not running on NT, skipping tests\n"); 198 return; 199 } 200 201 pNtResumeProcess = (void*)GetProcAddress(mod, "NtResumeProcess"); 202 pNtSuspendProcess = (void*)GetProcAddress(mod, "NtSuspendProcess"); 203 pNtResumeThread = (void*)GetProcAddress(mod, "NtResumeThread"); 204 pNtSuspendThread = (void*)GetProcAddress(mod, "NtSuspendThread"); 205 206 test_NtSuspendProcess(argv[0]); 207 } 208