コード例 #1
0
    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)
コード例 #2
0
    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')
コード例 #3
0
    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'')
コード例 #4
0
    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'))
コード例 #5
0
    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')
コード例 #6
0
    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()
コード例 #7
0
    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())
コード例 #8
0
    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)
コード例 #9
0
    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)
コード例 #10
0
ファイル: _encrypting_stream.py プロジェクト: x0r10101/tink
  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)
コード例 #11
0
    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'')
コード例 #12
0
    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')
コード例 #13
0
    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'))
コード例 #14
0
    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'))
コード例 #15
0
 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)
コード例 #16
0
    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'')