def test_bytes_read_greater_than_bytes_written(self): reader, writer = self.create_anonymous_pipes() data = b"hello world" bytes_written = WriteFile(writer, data) result = PeekNamedPipe(reader, bytes_written * 2) self.assertEqual(result.lpBytesRead, bytes_written)
def test_peek_does_not_remove_data(self): reader, writer = self.create_anonymous_pipes() data = b"hello world" data_written = WriteFile(writer, data) PeekNamedPipe(reader, 0) self.assertEqual(ReadFile(reader, data_written), data)
def test_python_bytes(self): reader, writer = self.create_anonymous_pipes() data = b"hello world" bytes_written = WriteFile(writer, data) self.assertEqual( ReadFile(reader, bytes_written), b"hello world")
def test_total_bytes_avail(self): reader, writer = self.create_anonymous_pipes() data = b"hello world" bytes_written = WriteFile(writer, data) result = PeekNamedPipe(reader, 0) self.assertEqual(result.lpTotalBytesAvail, bytes_written)
def test_bytes_read_greater_than_bytes_written(self): reader, writer = self.create_anonymous_pipes() data = b"hello world" bytes_written = WriteFile(writer, data) result = PeekNamedPipe(reader, bytes_written * 2) self.assertEqual(result.lpBytesRead, bytes_written) _, library = dist.load() self.maybe_assert_last_error(library.ERROR_INVALID_HANDLE)
def test_peek_does_not_remove_data(self): reader, writer = self.create_anonymous_pipes() data = b"hello world" data_written = WriteFile(writer, data) PeekNamedPipe(reader, 0) self.assertEqual(ReadFile(reader, data_written), data) _, library = dist.load() self.maybe_assert_last_error(library.ERROR_INVALID_HANDLE)
def test_total_bytes_avail(self): reader, writer = self.create_anonymous_pipes() data = b"hello world" bytes_written = WriteFile(writer, data) result = PeekNamedPipe(reader, 0) self.assertEqual(result.lpTotalBytesAvail, bytes_written) _, library = dist.load() self.maybe_assert_last_error(library.ERROR_INVALID_HANDLE)
def test_python_bytes(self): reader, writer = self.create_anonymous_pipes() data = b"hello world" bytes_written = WriteFile(writer, data) self.assertEqual( ReadFile(reader, bytes_written), b"hello world") _, library = dist.load() self.maybe_assert_last_error(library.ERROR_INVALID_HANDLE)
def setUp(self): super(LockFileCase, self).setUp() fd, path = tempfile.mkstemp() self.path = path os.close(fd) self.addCleanup(os.remove, path) _, library = dist.load() path = text_type(path) self.handle = CreateFile(path, library.GENERIC_WRITE) self.assert_last_error(library.ERROR_ALREADY_EXISTS) self.addCleanup(CloseHandle, self.handle) WriteFile(self.handle, b"hello")
def setUp(self): super(LockFileCase, self).setUp() fd, path = tempfile.mkstemp() self.path = path os.close(fd) self.addCleanup(os.remove, path) _, library = dist.load() path = text_type(path) # pylint: disable=redefined-variable-type self.handle = CreateFile(path, library.GENERIC_WRITE) self.SetLastError(0) self.addCleanup(CloseHandle, self.handle) WriteFile(self.handle, b"hello")
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_write_binary_num_bytes(self): handle, path = self.create_handle() WriteFile(handle, b"hello world", nNumberOfBytesToWrite=5) FlushFileBuffers(handle) with open(path, "rb") as file_: self.assertEqual(file_.read(), b"hello")
def test_write_binary(self): handle, path = self.create_handle() WriteFile(handle, b"hello world") FlushFileBuffers(handle) with open(path, "rb") as file_: self.assertEqual(file_.read(), b"hello world")