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.internal.formatting.encryption_context"""
14
- import unittest
15
-
16
14
import pytest
17
- import six
18
15
19
16
import aws_encryption_sdk .internal .defaults
20
17
import aws_encryption_sdk .internal .formatting .encryption_context
26
23
pytestmark = [pytest .mark .unit , pytest .mark .local ]
27
24
28
25
29
- class TestEncryptionContext (unittest . TestCase ):
26
+ class TestEncryptionContext (object ):
30
27
def test_assemble_content_aad (self ):
31
28
"""Validate that the assemble_content_aad function
32
29
behaves as expected.
@@ -40,10 +37,11 @@ def test_assemble_content_aad(self):
40
37
assert test == VALUES ["non_framed_aac" ]
41
38
42
39
def test_assemble_content_aad_unknown_type (self ):
43
- with six . assertRaisesRegex ( self , SerializationError , "Unknown aad_content_string" ) :
40
+ with pytest . raises ( SerializationError ) as excinfo :
44
41
aws_encryption_sdk .internal .formatting .encryption_context .assemble_content_aad (
45
42
message_id = VALUES ["message_id" ], aad_content_string = None , seq_num = 1 , length = VALUES ["content_len" ]
46
43
)
44
+ excinfo .match ("Unknown aad_content_string" )
47
45
48
46
def test_serialize_encryption_context_no_encryption_context (self ):
49
47
"""Validate that the serialize_encryption_context
@@ -59,20 +57,22 @@ def test_serialize_encryption_context_too_many_elements(self):
59
57
with an encryption context with too many
60
58
elements.
61
59
"""
62
- with six . assertRaisesRegex ( self , SerializationError , "The encryption context contains too many elements." ) :
60
+ with pytest . raises ( SerializationError ) as excinfo :
63
61
aws_encryption_sdk .internal .formatting .encryption_context .serialize_encryption_context (
64
62
VALUES ["encryption_context_too_many_elements" ]
65
63
)
64
+ excinfo .match ("The encryption context contains too many elements." )
66
65
67
66
def test_serialize_encryption_context_too_large (self ):
68
67
"""Validate that the serialize_encryption_context
69
68
function behaves as expected when presented
70
69
with an encryption context which is too large.
71
70
"""
72
- with six . assertRaisesRegex ( self , SerializationError , "The serialized context is too large" ) :
71
+ with pytest . raises ( SerializationError ) as excinfo :
73
72
aws_encryption_sdk .internal .formatting .encryption_context .serialize_encryption_context (
74
73
VALUES ["encryption_context_too_large" ]
75
74
)
75
+ excinfo .match ("The serialized context is too large" )
76
76
77
77
def test_serialize_encryption_context_unencodable (self ):
78
78
"""Validate that the serialize_encryption_context
@@ -81,10 +81,11 @@ def test_serialize_encryption_context_unencodable(self):
81
81
unencodable elements.
82
82
"""
83
83
for encryption_context in [{"a" : b"\xc4 " }, {b"\xc4 " : "a" }, {b"\xc4 " : b"\xc4 " }]:
84
- with six . assertRaisesRegex ( self , SerializationError , "Cannot encode dictionary key or value using *" ) :
84
+ with pytest . raises ( SerializationError ) as excinfo :
85
85
aws_encryption_sdk .internal .formatting .encryption_context .serialize_encryption_context (
86
86
encryption_context
87
87
)
88
+ excinfo .match ("Cannot encode dictionary key or value using *" )
88
89
89
90
def test_serialize_encryption_context_valid (self ):
90
91
"""Validate that the serialize_encryption_context
@@ -100,8 +101,9 @@ def test_read_short_too_short(self):
100
101
"""Validate that the read_short function behaves
101
102
as expected when it encounters a struct error.
102
103
"""
103
- with six . assertRaisesRegex ( self , SerializationError , "Bad format of serialized context." ) :
104
+ with pytest . raises ( SerializationError ) as excinfo :
104
105
aws_encryption_sdk .internal .formatting .encryption_context .read_short (b"d" , 0 )
106
+ excinfo .match ("Bad format of serialized context." )
105
107
106
108
def test_read_short_valid (self ):
107
109
"""Validate that the read_short function behaves
@@ -116,8 +118,9 @@ def test_read_string_encoding_error(self):
116
118
as expected when it encounters an encoding
117
119
error.
118
120
"""
119
- with six . assertRaisesRegex ( self , SerializationError , "Bad format of serialized context." ) :
121
+ with pytest . raises ( SerializationError ) as excinfo :
120
122
aws_encryption_sdk .internal .formatting .encryption_context .read_string (b"\xc4 " , 0 , 1 )
123
+ excinfo .match ("Bad format of serialized context." )
121
124
122
125
def test_read_string_valid (self ):
123
126
"""Validate that the read_string function behaves
@@ -136,21 +139,23 @@ def test_deserialize_encryption_context_too_large(self):
136
139
data = ""
137
140
for i in range (aws_encryption_sdk .internal .defaults .MAX_BYTE_ARRAY_SIZE + 1 ):
138
141
data += str (i )
139
- with six . assertRaisesRegex ( self , SerializationError , "Serialized context is too long." ) :
142
+ with pytest . raises ( SerializationError ) as excinfo :
140
143
aws_encryption_sdk .internal .formatting .encryption_context .deserialize_encryption_context (
141
144
serialized_encryption_context = data
142
145
)
146
+ excinfo .match ("Serialized context is too long." )
143
147
144
148
def test_deserialize_encryption_context_duplicate_key (self ):
145
149
"""Validate that the deserialize_encryption_context
146
150
function behaves as expected when it encounters
147
151
a serialized encryption context which contains
148
152
duplicate keys.
149
153
"""
150
- with six . assertRaisesRegex ( self , SerializationError , "Duplicate key in serialized context." ) :
154
+ with pytest . raises ( SerializationError ) as excinfo :
151
155
aws_encryption_sdk .internal .formatting .encryption_context .deserialize_encryption_context (
152
156
serialized_encryption_context = VALUES ["serialized_encryption_context_duplicate_key" ]
153
157
)
158
+ excinfo .match ("Duplicate key in serialized context." )
154
159
155
160
def test_deserialize_encryption_context_extra_data (self ):
156
161
"""Validate that the deserialize_encryption_context
@@ -160,10 +165,11 @@ def test_deserialize_encryption_context_extra_data(self):
160
165
of pairs (formatting error).
161
166
"""
162
167
data = VALUES ["serialized_encryption_context" ] + b"jhofguijhsuskldfh"
163
- with six . assertRaisesRegex ( self , SerializationError , "Formatting error: Extra data in serialized context." ) :
168
+ with pytest . raises ( SerializationError ) as excinfo :
164
169
aws_encryption_sdk .internal .formatting .encryption_context .deserialize_encryption_context (
165
170
serialized_encryption_context = data
166
171
)
172
+ excinfo .match ("Formatting error: Extra data in serialized context." )
167
173
168
174
def test_deserialize_encryption_context_valid (self ):
169
175
"""Validate that the deserialize_encryption_context
0 commit comments