def test_handle_is_valid(self): _, library = dist.load() handle = OpenProcess( library.PROCESS_QUERY_INFORMATION, False, os.getpid()) # If the handle were invalid, this would fail. CloseHandle(handle)
def test_returns_handle(self): _, library = dist.load() handle = OpenProcess(library.PROCESS_QUERY_INFORMATION, False, os.getpid()) self.assertIsInstance(handle, HANDLE) CloseHandle(handle)
def test_process_still_active(self): process = self.create_python_process("import time; time.sleep(5)") pid = process.pid _, library = dist.load() hProcess = OpenProcess(library.PROCESS_QUERY_INFORMATION, False, pid) self.addCleanup(CloseHandle, hProcess) self.assertEqual(GetExitCodeProcess(hProcess), library.STILL_ACTIVE)
def test_get_pid_of_external_process(self): process = self.create_python_process("import time; time.sleep(3)") expected_pid = process.pid _, library = dist.load() handle = OpenProcess( library.PROCESS_QUERY_INFORMATION, False, expected_pid) self.assertEqual(GetProcessId(handle), expected_pid) CloseHandle(handle)
def test_terminates_process(self): process = self.create_python_process("import time; time.sleep(5)") _, library = dist.load() handle = OpenProcess(library.PROCESS_TERMINATE, False, process.pid) self.addCleanup(CloseHandle, handle) TerminateProcess(handle, 42) process.communicate() self.assertEqual(process.returncode, 42)
def test_get_process_id_current_process(self): # We should be able to access the pid of the process # we created a handle to. _, library = dist.load() handle = OpenProcess(library.PROCESS_QUERY_INFORMATION, False, os.getpid()) self.assertEqual(GetProcessId(handle), os.getpid()) CloseHandle(handle)
def test_get_exit_code_non_zero(self): process = self.create_python_process("import sys; sys.exit(1)") pid = process.pid process.communicate() _, library = dist.load() hProcess = OpenProcess(library.PROCESS_QUERY_INFORMATION, False, pid) self.addCleanup(CloseHandle, hProcess) self.assertEqual(GetExitCodeProcess(hProcess), 1)
def test_process_dies_before_timeout(self): process = self.create_python_process("import time; time.sleep(1)") _, library = dist.load() hProcess = OpenProcess( library.PROCESS_QUERY_INFORMATION | library.SYNCHRONIZE, False, process.pid) self.addCleanup(CloseHandle, hProcess) self.assertEqual(WaitForSingleObject(hProcess, library.INFINITE), library.WAIT_OBJECT_0)
def test_wait_on_running_process(self): process = self.create_python_process("import time; time.sleep(1)") _, library = dist.load() hProcess = OpenProcess( library.PROCESS_QUERY_INFORMATION | library.SYNCHRONIZE, False, process.pid) self.addCleanup(CloseHandle, hProcess) self.assertEqual(WaitForSingleObject(hProcess, 0), library.WAIT_TIMEOUT)
def test_wait_failed(self): # This should cause WAIT_FAILED to be returned by the underlying # WaitForSingleObject because we didn't request the SYNCHRONIZE # permission. process = self.create_python_process("import time; time.sleep(3)") _, library = dist.load() hProcess = OpenProcess(library.PROCESS_QUERY_INFORMATION, False, process.pid) self.addCleanup(CloseHandle, hProcess) with self.assertRaises(WindowsAPIError) as exec_: WaitForSingleObject(hProcess, 3) self.assertEqual(exec_.code, library.WAIT_FAILED) self.assertEqual(self.GetLastError()[0], library.ERROR_ACCESS_DENIED) self.SetLastError(0)
def test_access_denied_for_null_desired_access(self): with self.assertRaises(WindowsAPIError) as error: OpenProcess(0, False, os.getpid()) self.assertEqual(error.exception.errno, 5) self.SetLastError(0)
def open_process(_, bInheritHandle, dwProcessId): return OpenProcess(0, bInheritHandle, dwProcessId)