コード例 #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_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')
コード例 #4
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()
コード例 #5
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'))
コード例 #6
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'')
コード例 #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
ファイル: _encrypting_stream.py プロジェクト: yoavamit/tink
    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().__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
ファイル: _decrypting_stream.py プロジェクト: wwjiang007/tink
    def __init__(self, stream_aead: tink_bindings.StreamingAead,
                 ciphertext_source: BinaryIO, associated_data: bytes, *,
                 close_ciphertext_source: bool):
        """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.
      close_ciphertext_source: Whether ciphertext_source should be closed when
        close() is called.
    """
        super().__init__()
        self._ciphertext_source = ciphertext_source
        self._close_ciphertext_source = close_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
  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'')
コード例 #11
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')
コード例 #12
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'))
コード例 #13
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'))
コード例 #14
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)
コード例 #15
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'')