-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathtest_streams.py
89 lines (63 loc) · 2.79 KB
/
test_streams.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
"""Unit test suite for aws_encryption_sdk.internal.utils.streams"""
import io
import pytest
from aws_encryption_sdk.exceptions import ActionNotAllowedError
from aws_encryption_sdk.internal.str_ops import to_bytes, to_str
from aws_encryption_sdk.internal.utils.streams import InsistentReaderBytesIO, ROStream, TeeStream
from ...unit_test_utils import ExactlyTwoReads, NothingButRead, SometimesIncompleteReaderIO
pytestmark = [pytest.mark.unit, pytest.mark.local]
def data(length=None, stream_type=io.BytesIO, converter=to_bytes):
source = b"asdijfhoaisjdfoiasjdfoijawef"
chunk_length = 100
if length is None:
length = len(source)
while len(source) < length:
source += source[:chunk_length]
return stream_type(converter(source[:length]))
def test_rostream():
test = ROStream(data())
with pytest.raises(ActionNotAllowedError) as excinfo:
test.write(b"")
excinfo.match(r"Write not allowed on ROStream objects")
def test_teestream_full():
new_tee = io.BytesIO()
test_tee = TeeStream(data(), new_tee)
raw_read = test_tee.read()
assert data().getvalue() == raw_read == new_tee.getvalue()
@pytest.mark.parametrize(
"stream_type, converter",
(
(io.BytesIO, to_bytes),
(SometimesIncompleteReaderIO, to_bytes),
(io.StringIO, to_str),
(NothingButRead, to_bytes),
),
)
@pytest.mark.parametrize("bytes_to_read", range(1, 102))
@pytest.mark.parametrize("source_length", (1, 11, 100))
def test_insistent_stream(source_length, bytes_to_read, stream_type, converter):
source = InsistentReaderBytesIO(data(length=source_length, stream_type=stream_type, converter=converter))
test = source.read(bytes_to_read)
assert (source_length >= bytes_to_read and len(test) == bytes_to_read) or (
source_length < bytes_to_read and len(test) == source_length
)
def test_insistent_stream_close_partway_through():
raw = data(length=100)
source = ExactlyTwoReads(raw.getvalue())
wrapped = InsistentReaderBytesIO(source)
test = b""
test += wrapped.read(10) # actually reads 10 bytes
test += wrapped.read(10) # reads 5 bytes, stream is closed before third read can complete, truncating the result
assert test == raw.getvalue()[:15]