11
11
# ANY KIND, either express or implied. See the License for the specific
12
12
# language governing permissions and limitations under the License.
13
13
"""Unit test suite for aws_encryption_sdk.streaming_client._EncryptionStream"""
14
+ import copy
14
15
import io
15
16
import unittest
16
17
@@ -45,20 +46,23 @@ def _read_bytes(self, b):
45
46
return self .config .mock_read_bytes
46
47
47
48
48
- class TestEncryptionStream (unittest .TestCase ):
49
- def setUp (self ):
50
- self .mock_source_stream = MagicMock ()
51
- self .mock_source_stream .__class__ = io .IOBase
52
- self .mock_source_stream .tell .side_effect = (10 , 500 )
49
+ class TestEncryptionStream (object ):
53
50
54
- self .mock_key_provider = MagicMock ()
55
- self .mock_key_provider .__class__ = MasterKeyProvider
51
+ def _mock_key_provider (self ):
52
+ mock_key_provider = MagicMock ()
53
+ mock_key_provider .__class__ = MasterKeyProvider
54
+ return mock_key_provider
56
55
57
- self .mock_line_length = MagicMock ()
58
- self .mock_line_length .__class__ = int
56
+ def _mock_source_stream (self ):
57
+ mock_source_stream = MagicMock ()
58
+ mock_source_stream .__class__ = io .IOBase
59
+ mock_source_stream .tell .side_effect = (10 , 500 )
60
+ return mock_source_stream
59
61
60
- self .mock_source_length = MagicMock ()
61
- self .mock_source_length .__class__ = int
62
+ @pytest .fixture (autouse = True )
63
+ def apply_fixtures (self ):
64
+ self .mock_key_provider = self ._mock_key_provider ()
65
+ self .mock_source_stream = self ._mock_source_stream ()
62
66
63
67
def test_read_bytes_enforcement (self ):
64
68
class TestStream (_EncryptionStream ):
@@ -67,19 +71,23 @@ class TestStream(_EncryptionStream):
67
71
def _prep_message (self ):
68
72
pass
69
73
70
- with six . assertRaisesRegex ( self , TypeError , "Can't instantiate abstract class TestStream" ) :
74
+ with pytest . raises ( TypeError ) as excinfo :
71
75
TestStream ()
72
76
77
+ excinfo .match ("Can't instantiate abstract class TestStream" )
78
+
73
79
def test_prep_message_enforcement (self ):
74
80
class TestStream (_EncryptionStream ):
75
81
_config_class = MockClientConfig
76
82
77
83
def _read_bytes (self ):
78
84
pass
79
85
80
- with six . assertRaisesRegex ( self , TypeError , "Can't instantiate abstract class TestStream" ) :
86
+ with pytest . raises ( TypeError ) as excinfo :
81
87
TestStream ()
82
88
89
+ excinfo .match ("Can't instantiate abstract class TestStream" )
90
+
83
91
def test_config_class_enforcement (self ):
84
92
class TestStream (_EncryptionStream ):
85
93
def _read_bytes (self ):
@@ -88,29 +96,32 @@ def _read_bytes(self):
88
96
def _prep_message (self ):
89
97
pass
90
98
91
- with six . assertRaisesRegex ( self , TypeError , "Can't instantiate abstract class TestStream" ) :
99
+ with pytest . raises ( TypeError ) as excinfo :
92
100
TestStream ()
93
101
102
+ excinfo .match ("Can't instantiate abstract class TestStream" )
103
+
94
104
def test_new_with_params (self ):
105
+ mock_int_sentinel = MagicMock (__class__ = int )
95
106
mock_stream = MockEncryptionStream (
96
107
source = self .mock_source_stream ,
97
108
key_provider = self .mock_key_provider ,
98
109
mock_read_bytes = sentinel .read_bytes ,
99
- line_length = self . mock_line_length ,
100
- source_length = self . mock_source_length ,
110
+ line_length = io . DEFAULT_BUFFER_SIZE ,
111
+ source_length = mock_int_sentinel ,
101
112
)
102
113
assert mock_stream .config == MockClientConfig (
103
114
source = self .mock_source_stream ,
104
115
key_provider = self .mock_key_provider ,
105
116
mock_read_bytes = sentinel .read_bytes ,
106
- line_length = self . mock_line_length ,
107
- source_length = self . mock_source_length ,
117
+ line_length = io . DEFAULT_BUFFER_SIZE ,
118
+ source_length = mock_int_sentinel ,
108
119
)
109
120
assert mock_stream .bytes_read == 0
110
121
assert mock_stream .output_buffer == b""
111
122
assert not mock_stream ._message_prepped
112
123
assert mock_stream .source_stream is self .mock_source_stream
113
- assert mock_stream ._stream_length is self . mock_source_length
124
+ assert mock_stream ._stream_length is mock_int_sentinel
114
125
assert mock_stream .line_length == io .DEFAULT_BUFFER_SIZE
115
126
116
127
def test_new_with_config (self ):
@@ -154,7 +165,8 @@ class CustomUnknownError(Exception):
154
165
mock_stream = MockEncryptionStream (
155
166
source = self .mock_source_stream , key_provider = self .mock_key_provider , mock_read_bytes = sentinel .read_bytes
156
167
)
157
- with self .assertRaises (CustomUnknownError ):
168
+
169
+ with pytest .raises (CustomUnknownError ):
158
170
mock_stream .__exit__ (None , None , None )
159
171
160
172
def test_stream_length (self ):
@@ -173,9 +185,12 @@ def test_stream_length_unsupported(self):
173
185
mock_stream = MockEncryptionStream (
174
186
source = self .mock_source_stream , key_provider = self .mock_key_provider , mock_read_bytes = sentinel .read_bytes
175
187
)
176
- with six .assertRaisesRegex (self , aws_encryption_sdk .exceptions .NotSupportedError , "Unexpected exception!" ):
188
+
189
+ with pytest .raises (aws_encryption_sdk .exceptions .NotSupportedError ) as excinfo :
177
190
mock_stream .stream_length # pylint: disable=pointless-statement
178
191
192
+ excinfo .match ("Unexpected exception!" )
193
+
179
194
def test_header_property (self ):
180
195
mock_prep_message = MagicMock ()
181
196
mock_stream = MockEncryptionStream (
@@ -205,21 +220,26 @@ def test_read_closed(self):
205
220
source = self .mock_source_stream , key_provider = self .mock_key_provider , mock_read_bytes = sentinel .read_bytes
206
221
)
207
222
mock_stream .close ()
208
- with six .assertRaisesRegex (self , ValueError , "I/O operation on closed file" ):
223
+
224
+ with pytest .raises (ValueError ) as excinfo :
209
225
mock_stream .read ()
210
226
211
- def test_read_b (self ):
227
+ excinfo .match ("I/O operation on closed file" )
228
+
229
+ @pytest .mark .parametrize ('bytes_to_read' , range (1 , 11 ))
230
+ def test_read_b (self , bytes_to_read ):
212
231
mock_stream = MockEncryptionStream (
213
232
source = io .BytesIO (VALUES ["data_128" ]),
214
233
key_provider = self .mock_key_provider ,
215
234
mock_read_bytes = sentinel .read_bytes ,
216
235
)
236
+ data = b"1234567890"
217
237
mock_stream ._read_bytes = MagicMock ()
218
- mock_stream .output_buffer = b"1234567890"
219
- test = mock_stream .read (5 )
220
- mock_stream ._read_bytes .assert_called_once_with (5 )
221
- assert test == b"12345"
222
- assert mock_stream .output_buffer == b"67890"
238
+ mock_stream .output_buffer = copy . copy ( data )
239
+ test = mock_stream .read (bytes_to_read )
240
+ mock_stream ._read_bytes .assert_called_once_with (bytes_to_read )
241
+ assert test == data [: bytes_to_read ]
242
+ assert mock_stream .output_buffer == data [ bytes_to_read :]
223
243
224
244
def test_read_all (self ):
225
245
mock_stream = MockEncryptionStream (
@@ -262,23 +282,32 @@ def test_writelines(self):
262
282
mock_stream = MockEncryptionStream (
263
283
source = self .mock_source_stream , key_provider = self .mock_key_provider , mock_read_bytes = sentinel .read_bytes
264
284
)
265
- with six .assertRaisesRegex (self , NotImplementedError , "writelines is not available for this object" ):
285
+
286
+ with pytest .raises (NotImplementedError ) as excinfo :
266
287
mock_stream .writelines (None )
267
288
289
+ excinfo .match ("writelines is not available for this object" )
290
+
268
291
def test_write (self ):
269
292
mock_stream = MockEncryptionStream (
270
293
source = self .mock_source_stream , key_provider = self .mock_key_provider , mock_read_bytes = sentinel .read_bytes
271
294
)
272
- with six .assertRaisesRegex (self , NotImplementedError , "write is not available for this object" ):
295
+
296
+ with pytest .raises (NotImplementedError ) as excinfo :
273
297
mock_stream .write (None )
274
298
299
+ excinfo .match ("write is not available for this object" )
300
+
275
301
def test_seek (self ):
276
302
mock_stream = MockEncryptionStream (
277
303
source = self .mock_source_stream , key_provider = self .mock_key_provider , mock_read_bytes = sentinel .read_bytes
278
304
)
279
- with six .assertRaisesRegex (self , NotImplementedError , "seek is not available for this object" ):
305
+
306
+ with pytest .raises (NotImplementedError ) as excinfo :
280
307
mock_stream .seek (None )
281
308
309
+ excinfo .match ("seek is not available for this object" )
310
+
282
311
def test_readline (self ):
283
312
test_line = "TEST_LINE_AAAA"
284
313
test_line_length = len (test_line )
@@ -319,7 +348,8 @@ def test_next_stream_closed(self):
319
348
source = self .mock_source_stream , key_provider = self .mock_key_provider , mock_read_bytes = sentinel .read_bytes
320
349
)
321
350
mock_stream .close ()
322
- with self .assertRaises (StopIteration ):
351
+
352
+ with pytest .raises (StopIteration ):
323
353
mock_stream .next ()
324
354
325
355
def test_next_source_stream_closed_and_buffer_empty (self ):
@@ -328,7 +358,8 @@ def test_next_source_stream_closed_and_buffer_empty(self):
328
358
)
329
359
self .mock_source_stream .closed = True
330
360
mock_stream .output_buffer = b""
331
- with self .assertRaises (StopIteration ):
361
+
362
+ with pytest .raises (StopIteration ):
332
363
mock_stream .next ()
333
364
334
365
@patch ("aws_encryption_sdk.streaming_client._EncryptionStream.closed" , new_callable = PropertyMock )
0 commit comments