-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathtest_aws_encryption_sdk.py
131 lines (117 loc) · 4.6 KB
/
test_aws_encryption_sdk.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# 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 high-level functions in aws_encryption_sdk module"""
import unittest
from mock import MagicMock, patch, sentinel
import pytest
import six
import aws_encryption_sdk
import aws_encryption_sdk.internal.defaults
pytestmark = [pytest.mark.unit, pytest.mark.local]
class TestAwsEncryptionSdk(unittest.TestCase):
def setUp(self):
# Set up StreamEncryptor patch
self.mock_stream_encryptor_patcher = patch('aws_encryption_sdk.StreamEncryptor')
self.mock_stream_encryptor = self.mock_stream_encryptor_patcher.start()
self.mock_stream_encryptor_instance = MagicMock()
self.mock_stream_encryptor_instance.read.return_value = sentinel.ciphertext
self.mock_stream_encryptor_instance.header = sentinel.header
self.mock_stream_encryptor.return_value = self.mock_stream_encryptor_instance
self.mock_stream_encryptor_instance.__enter__.return_value = self.mock_stream_encryptor_instance
# Set up StreamDecryptor patch
self.mock_stream_decryptor_patcher = patch('aws_encryption_sdk.StreamDecryptor')
self.mock_stream_decryptor = self.mock_stream_decryptor_patcher.start()
self.mock_stream_decryptor_instance = MagicMock()
self.mock_stream_decryptor_instance.read.return_value = sentinel.plaintext
self.mock_stream_decryptor_instance.header = sentinel.header
self.mock_stream_decryptor.return_value = self.mock_stream_decryptor_instance
self.mock_stream_decryptor_instance.__enter__.return_value = self.mock_stream_decryptor_instance
def tearDown(self):
self.mock_stream_encryptor_patcher.stop()
self.mock_stream_decryptor_patcher.stop()
def test_encrypt(self):
test_ciphertext, test_header = aws_encryption_sdk.encrypt(
a=sentinel.a,
b=sentinel.b,
c=sentinel.b
)
self.mock_stream_encryptor.called_once_with(
a=sentinel.a,
b=sentinel.b,
c=sentinel.b
)
assert test_ciphertext is sentinel.ciphertext
assert test_header is sentinel.header
def test_decrypt(self):
test_plaintext, test_header = aws_encryption_sdk.decrypt(
a=sentinel.a,
b=sentinel.b,
c=sentinel.b
)
self.mock_stream_encryptor.called_once_with(
a=sentinel.a,
b=sentinel.b,
c=sentinel.b
)
assert test_plaintext is sentinel.plaintext
assert test_header is sentinel.header
def test_stream_encryptor_e(self):
test = aws_encryption_sdk.stream(
mode='e',
a=sentinel.a,
b=sentinel.b,
c=sentinel.b
)
assert test is self.mock_stream_encryptor_instance
self.mock_stream_encryptor.assert_called_once_with(
a=sentinel.a,
b=sentinel.b,
c=sentinel.b
)
def test_stream_encryptor_encrypt(self):
test = aws_encryption_sdk.stream(
mode='ENCRYPT',
a=sentinel.a,
b=sentinel.b,
c=sentinel.b
)
assert test is self.mock_stream_encryptor_instance
def test_stream_decryptor_d(self):
test = aws_encryption_sdk.stream(
mode='d',
a=sentinel.a,
b=sentinel.b,
c=sentinel.b
)
assert test is self.mock_stream_decryptor_instance
self.mock_stream_decryptor.assert_called_once_with(
a=sentinel.a,
b=sentinel.b,
c=sentinel.b
)
def test_stream_decryptor_decrypt(self):
test = aws_encryption_sdk.stream(
mode='DECRYPT',
a=sentinel.a,
b=sentinel.b,
c=sentinel.b
)
assert test is self.mock_stream_decryptor_instance
def test_stream_unknown(self):
with six.assertRaisesRegex(self, ValueError, 'Unsupported mode: *'):
aws_encryption_sdk.stream(
mode='ERROR',
a=sentinel.a,
b=sentinel.b,
c=sentinel.b
)