def test_read_eof(self): file_object = mock.Mock() file_object.read = mock.Mock(return_value=b'') adapter = file_object_adapter.FileObjectAdapter(file_object) with self.assertRaises(EOFError): adapter.read(10)
def test_multiple_read(self): file_object = io.BytesIO(b'something') adapter = file_object_adapter.FileObjectAdapter(file_object) self.assertEqual(adapter.read(3), b'som') self.assertEqual(adapter.read(3), b'eth') self.assertEqual(adapter.read(3), b'ing')
def test_read_raises_blocking_error(self): file_object = mock.Mock() file_object.read = mock.Mock( side_effect=io.BlockingIOError(None, None)) adapter = file_object_adapter.FileObjectAdapter(file_object) self.assertEqual(adapter.read(10), b'')
def test_write_raises_blocking_error(self): file_object = mock.Mock() file_object.write = mock.Mock( side_effect=io.BlockingIOError(None, None, 5)) adapter = file_object_adapter.FileObjectAdapter(file_object) self.assertEqual(5, adapter.write(b'something'))
def test_write_after_close(self): file_object = io.BytesIO() adapter = file_object_adapter.FileObjectAdapter(file_object) adapter.close() with self.assertRaises(ValueError): adapter.write(b'something')
def test_basic_write(self): file_object = io.BytesIO() adapter = file_object_adapter.FileObjectAdapter(file_object) self.assertEqual(9, adapter.write(b'something')) self.assertEqual(b'something', file_object.getvalue()) adapter.close()
def test_multiple_write(self): file_object = io.BytesIO() adapter = file_object_adapter.FileObjectAdapter(file_object) self.assertEqual(9, adapter.write(b'something')) self.assertEqual(3, adapter.write(b'123')) self.assertEqual(3, adapter.write(b'456')) self.assertEqual(b'something123456', file_object.getvalue())
def __init__(self, stream_aead: tink_bindings.StreamingAead, ciphertext_destination: BinaryIO, associated_data: bytes): """Create a new RawEncryptingStream. Args: stream_aead: C++ StreamingAead primitive from which a C++ EncryptingStream will be obtained. ciphertext_destination: A writable file-like object to which ciphertext bytes will be written. associated_data: The associated data to use for encryption. This must match the associated_data used for decryption. """ super(RawEncryptingStream, self).__init__() if not ciphertext_destination.writable(): raise ValueError('ciphertext_destination must be writable') cc_ciphertext_destination = file_object_adapter.FileObjectAdapter( ciphertext_destination) self._cc_encrypting_stream = _new_cc_encrypting_stream( stream_aead, associated_data, cc_ciphertext_destination)
def __init__(self, stream_aead: tink_bindings.StreamingAead, ciphertext_source: BinaryIO, associated_data: bytes): """Create a new RawDecryptingStream. Args: stream_aead: C++ StreamingAead primitive from which a C++ DecryptingStream will be obtained. ciphertext_source: A readable file-like object from which ciphertext bytes will be read. associated_data: The associated data to use for decryption. """ super(RawDecryptingStream, self).__init__() self._ciphertext_source = ciphertext_source if not ciphertext_source.readable(): raise ValueError('ciphertext_source must be readable') cc_ciphertext_source = file_object_adapter.FileObjectAdapter( ciphertext_source) self._input_stream_adapter = self._get_input_stream_adapter( stream_aead, associated_data, cc_ciphertext_source)
def __init__(self, stream_aead, ciphertext_destination: BinaryIO, associated_data: bytes): """Create a new EncryptingStream. Args: stream_aead: C++ StreamingAead primitive from which a C++ EncryptingStream will be obtained. ciphertext_destination: A writable file-like object to which ciphertext bytes will be written. associated_data: The associated data to use for encryption. This must match the associated_data used for decryption. """ super(EncryptingStream, self).__init__() self._closed = False self._bytes_written = 0 # Create FileObjectAdapter if not ciphertext_destination.writable(): raise ValueError('ciphertext_destination must be writable') cc_ciphertext_destination = file_object_adapter.FileObjectAdapter( ciphertext_destination) # Get OutputStreamAdapter of C++ EncryptingStream self._output_stream_adapter = self._get_output_stream_adapter( stream_aead, associated_data, cc_ciphertext_destination)
def test_read_returns_none(self): file_object = mock.Mock() file_object.read = mock.Mock(return_value=None) adapter = file_object_adapter.FileObjectAdapter(file_object) self.assertEqual(adapter.read(10), b'')
def test_basic_read(self): file_object = io.BytesIO(b'something') adapter = file_object_adapter.FileObjectAdapter(file_object) self.assertEqual(adapter.read(9), b'something')
def test_partial_write(self): file_object = mock.Mock() file_object.write = mock.Mock(wraps=lambda data: len(data) - 1) adapter = file_object_adapter.FileObjectAdapter(file_object) self.assertEqual(8, adapter.write(b'something'))
def test_write_returns_none(self): file_object = mock.Mock() file_object.write = mock.Mock(return_value=None) adapter = file_object_adapter.FileObjectAdapter(file_object) self.assertEqual(0, adapter.write(b'something'))
def test_read_negative_size_fails(self): file_object = io.BytesIO(b'something') adapter = file_object_adapter.FileObjectAdapter(file_object) with self.assertRaises(ValueError): adapter.read(-1)
def test_read_size_0(self): file_object = io.BytesIO(b'something') adapter = file_object_adapter.FileObjectAdapter(file_object) self.assertEqual(adapter.read(0), b'')