Skip to content

Commit 29dbced

Browse files
authored
Merge pull request #111 from mattsb42-aws/dev-103c
Remove need for source_stream.closed
2 parents 1942f59 + f417067 commit 29dbced

File tree

4 files changed

+26
-14
lines changed

4 files changed

+26
-14
lines changed

src/aws_encryption_sdk/streaming_client.py

+16-9
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,11 @@ def read(self, b=-1):
243243
output.write(self.output_buffer[:b])
244244
self.output_buffer = self.output_buffer[b:]
245245
else:
246-
while not self.source_stream.closed:
247-
self._read_bytes(LINE_LENGTH)
248-
output.write(self.output_buffer)
249-
self.output_buffer = b""
246+
while True:
247+
line = self.readline()
248+
if not line:
249+
break
250+
output.write(line)
250251

251252
self.bytes_read += output.tell()
252253
_LOGGER.debug("Returning %d bytes of %d bytes requested", output.tell(), b)
@@ -294,10 +295,13 @@ def next(self):
294295
if self.closed:
295296
_LOGGER.debug("stream is closed")
296297
raise StopIteration()
297-
if self.source_stream.closed and not self.output_buffer:
298+
299+
line = self.readline()
300+
if not line:
298301
_LOGGER.debug("nothing more to read")
299302
raise StopIteration()
300-
return self.readline()
303+
304+
return line
301305

302306
#: Provides hook for Python3 iterator functionality.
303307
__next__ = next
@@ -400,6 +404,7 @@ def __init__(self, **kwargs): # pylint: disable=unused-argument,super-init-not-
400404
raise SerializationError("Source too large for non-framed message")
401405

402406
self.__unframed_plaintext_cache = io.BytesIO()
407+
self.__message_complete = False
403408

404409
def ciphertext_length(self):
405410
"""Returns the length of the resulting ciphertext message in bytes.
@@ -552,6 +557,7 @@ def _read_bytes_to_non_framed_body(self, b):
552557

553558
if self.signer is not None:
554559
closing += serialize_footer(self.signer)
560+
self.__message_complete = True
555561
return ciphertext + closing
556562

557563
return ciphertext
@@ -618,6 +624,7 @@ def _read_bytes_to_framed_body(self, b):
618624
_LOGGER.debug("Writing footer")
619625
if self.signer is not None:
620626
output += serialize_footer(self.signer)
627+
self.__message_complete = True
621628
self.source_stream.close()
622629
return output
623630

@@ -628,7 +635,7 @@ def _read_bytes(self, b):
628635
:raises NotSupportedError: if content type is not supported
629636
"""
630637
_LOGGER.debug("%d bytes requested from stream with content type: %s", b, self.content_type)
631-
if 0 <= b <= len(self.output_buffer) or self.source_stream.closed:
638+
if 0 <= b <= len(self.output_buffer) or self.__message_complete:
632639
_LOGGER.debug("No need to read from source stream or source stream closed")
633640
return
634641

@@ -900,8 +907,8 @@ def _read_bytes(self, b):
900907
:param int b: Number of bytes to read
901908
:raises NotSupportedError: if content type is not supported
902909
"""
903-
if self.source_stream.closed:
904-
_LOGGER.debug("Source stream closed")
910+
if hasattr(self, "footer"):
911+
_LOGGER.debug("Source stream processing complete")
905912
return
906913

907914
buffer_length = len(self.output_buffer)

test/functional/test_f_aws_encryption_sdk_client.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -761,9 +761,11 @@ def tell(self):
761761
raise NotImplementedError("NoTell does not tell().")
762762

763763

764-
class NoClose(ObjectProxy):
764+
class NoClosed(ObjectProxy):
765765
closed = NotImplemented
766766

767+
768+
class NoClose(ObjectProxy):
767769
def close(self):
768770
raise NotImplementedError("NoClose does not close().")
769771

@@ -772,6 +774,7 @@ def close(self):
772774
"wrapping_class",
773775
(
774776
NoTell,
777+
NoClosed,
775778
pytest.param(NoClose, marks=pytest.mark.xfail(strict=True)),
776779
pytest.param(NothingButRead, marks=pytest.mark.xfail(strict=True)),
777780
),
@@ -793,6 +796,7 @@ def test_cycle_minimal_source_stream_api(frame_length, wrapping_class):
793796
"wrapping_class",
794797
(
795798
NoTell,
799+
NoClosed,
796800
pytest.param(NoClose, marks=pytest.mark.xfail(strict=True)),
797801
pytest.param(NothingButRead, marks=pytest.mark.xfail(strict=True)),
798802
),
@@ -813,6 +817,7 @@ def test_encrypt_minimal_source_stream_api(frame_length, wrapping_class):
813817
"wrapping_class",
814818
(
815819
NoTell,
820+
NoClosed,
816821
pytest.param(NoClose, marks=pytest.mark.xfail(strict=True)),
817822
pytest.param(NothingButRead, marks=pytest.mark.xfail(strict=True)),
818823
),

test/unit/test_streaming_client_stream_decryptor.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -502,10 +502,10 @@ def test_read_bytes_from_framed_body_bad_sequence_number(self):
502502

503503
@patch("aws_encryption_sdk.streaming_client.StreamDecryptor._read_bytes_from_non_framed_body")
504504
@patch("aws_encryption_sdk.streaming_client.StreamDecryptor._read_bytes_from_framed_body")
505-
def test_read_bytes_closed(self, mock_read_frame, mock_read_block):
505+
def test_read_bytes_completed(self, mock_read_frame, mock_read_block):
506506
ct_stream = io.BytesIO(VALUES["data_128"])
507507
test_decryptor = StreamDecryptor(key_provider=self.mock_key_provider, source=ct_stream)
508-
test_decryptor.source_stream.close()
508+
test_decryptor.footer = None
509509
test_decryptor._read_bytes(5)
510510
assert not mock_read_frame.called
511511
assert not mock_read_block.called

test/unit/test_streaming_client_stream_encryptor.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -451,10 +451,10 @@ def test_read_bytes_less_than_buffer(self, mock_read_non_framed, mock_read_frame
451451

452452
@patch("aws_encryption_sdk.streaming_client.StreamEncryptor._read_bytes_to_framed_body")
453453
@patch("aws_encryption_sdk.streaming_client.StreamEncryptor._read_bytes_to_non_framed_body")
454-
def test_read_bytes_closed(self, mock_read_non_framed, mock_read_framed):
454+
def test_read_bytes_completed(self, mock_read_non_framed, mock_read_framed):
455455
pt_stream = io.BytesIO(self.plaintext)
456456
test_encryptor = StreamEncryptor(source=pt_stream, key_provider=self.mock_key_provider)
457-
test_encryptor.source_stream.close()
457+
test_encryptor._StreamEncryptor__message_complete = True
458458
test_encryptor._read_bytes(5)
459459
assert not mock_read_non_framed.called
460460
assert not mock_read_framed.called

0 commit comments

Comments
 (0)