Skip to content

Commit bf1114c

Browse files
committed
properly handle negative read values to address #26
1 parent e232c32 commit bf1114c

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

src/aws_encryption_sdk/streaming_client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,10 @@ def read(self, b=None):
209209
:returns: Processed (encrypted or decrypted) bytes from source stream
210210
:rtype: bytes
211211
"""
212+
# Any negative value for b is interpreted as a full read
213+
if b is not None and b < 0:
214+
b = None
215+
212216
_LOGGER.debug("Stream read called, requesting %s bytes", b)
213217
output = io.BytesIO()
214218
if not self._message_prepped:

test/unit/test_streaming_client_encryption_stream.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ def _read_bytes(self, b):
4747

4848

4949
class TestEncryptionStream(object):
50-
5150
def _mock_key_provider(self):
5251
mock_key_provider = MagicMock()
5352
mock_key_provider.__class__ = MasterKeyProvider
@@ -226,7 +225,7 @@ def test_read_closed(self):
226225

227226
excinfo.match("I/O operation on closed file")
228227

229-
@pytest.mark.parametrize('bytes_to_read', range(1, 11))
228+
@pytest.mark.parametrize("bytes_to_read", range(1, 11))
230229
def test_read_b(self, bytes_to_read):
231230
mock_stream = MockEncryptionStream(
232231
source=io.BytesIO(VALUES["data_128"]),
@@ -241,15 +240,16 @@ def test_read_b(self, bytes_to_read):
241240
assert test == data[:bytes_to_read]
242241
assert mock_stream.output_buffer == data[bytes_to_read:]
243242

244-
def test_read_all(self):
243+
@pytest.mark.parametrize("bytes_to_read", (None, -1, -99))
244+
def test_read_all(self, bytes_to_read):
245245
mock_stream = MockEncryptionStream(
246246
source=self.mock_source_stream, key_provider=self.mock_key_provider, mock_read_bytes=sentinel.read_bytes
247247
)
248248
mock_stream._stream_length = 5
249249
mock_stream.output_buffer = b"1234567890"
250250
mock_stream.source_stream = MagicMock()
251251
type(mock_stream.source_stream).closed = PropertyMock(side_effect=(False, False, True))
252-
test = mock_stream.read()
252+
test = mock_stream.read(bytes_to_read)
253253
assert test == b"1234567890"
254254

255255
def test_read_all_empty_source(self):

0 commit comments

Comments
 (0)