def cleanup_process(self, create_process_result): """ Ensures that we properly cleanup a launched process after the test. """ try: TerminateProcess( create_process_result.lpProcessInformation.hProcess, 0) # Process may already be dead. except WindowsAPIError: self.SetLastError(0) CloseHandle(create_process_result.lpProcessInformation.hProcess) CloseHandle(create_process_result.lpProcessInformation.hThread)
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_create_and_close_pipes(self): reader, writer = CreatePipe() CloseHandle(writer) # Second attempt should fail with self.assertRaises(WindowsAPIError): CloseHandle(writer) _, library = dist.load() self.assert_last_error(library.ERROR_INVALID_HANDLE) # Second attempt should fail CloseHandle(reader) with self.assertRaises(WindowsAPIError): CloseHandle(reader) self.assert_last_error(library.ERROR_INVALID_HANDLE)
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_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_overlapped_write_file(self): # Test outline: # - Create a temp dir. # - CreateFile for writing with FILE_FLAG_OVERLAPPED. # - WriteFile in overlapped mode. # - Use GetOverlappedResult to wait for IO completion. temp_dir = tempfile.mkdtemp(prefix="pywincffi-test-ovr-") self.addCleanup(shutil.rmtree, temp_dir, ignore_errors=True) filename = text_type(os.path.join(temp_dir, "overlapped-write-file")) file_contents = b"hello overlapped world" _, lib = dist.load() handle = CreateFile( lpFileName=filename, dwDesiredAccess=lib.GENERIC_WRITE, dwCreationDisposition=lib.CREATE_NEW, dwFlagsAndAttributes=lib.FILE_FLAG_OVERLAPPED, ) # Prepare overlapped write ovr = OVERLAPPED() ovr.hEvent = CreateEvent(bManualReset=True, bInitialState=False) # Go for overlapped WriteFile. Should result in: # - num_bytes_written == 0 # - GetLastError() == ERROR_IO_PENDING # HOWEVER, https://msdn.microsoft.com/en-us/library/aa365683 states: # "Further, the WriteFile function will sometimes return TRUE with a # GetLastError value of ERROR_SUCCESS, even though it is using an # asynchronous handle (which can also return FALSE with # ERROR_IO_PENDING). # Test strategy: # - Disregard WriteFile return result. # - Assert GetLastError is either ERROR_IO_PENDING or ERROR_SUCCESS. # - Later validate that the correct number of bytes was written. _ = WriteFile(handle, file_contents, lpOverlapped=ovr) self.maybe_assert_last_error(lib.ERROR_IO_PENDING) # Block until async write is completed. num_bytes_written = GetOverlappedResult(handle, ovr, bWait=True) self.assertEqual(num_bytes_written, len(file_contents)) CloseHandle(handle)
def test_opens_correct_file_handle(self): fd, path = tempfile.mkstemp() self.addCleanup(os.unlink, path) os.close(fd) test_file = open(path, "w") handle = handle_from_file(test_file) CloseHandle(handle) # If CloseHandle() was passed the same handle # that test_file is trying to write to the file # and/or flushing it should fail. try: test_file.write("foo") test_file.flush() except (OSError, IOError, WindowsError) as error: # EBADF == Bad file descriptor (because CloseHandle closed it) self.assertEqual(error.errno, EBADF) else: self.fail("Expected os.close(%r) to fail" % fd)
def test_create_event_valid_handle(self): handle = CreateEvent(False, False) CloseHandle(handle) # will raise exception if the handle is invalid
def test_create_event_valid_handle(self): handle = CreateEvent(bManualReset=False, bInitialState=False) CloseHandle(handle) # will raise exception if the handle is invalid