File: test_mac.py

package info (click to toggle)
python-pykmip 0.10.0-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,796 kB
  • sloc: python: 102,456; makefile: 33; sh: 12
file content (298 lines) | stat: -rw-r--r-- 11,164 bytes parent folder | download | duplicates (4)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# Copyright (c) 2017 Pure Storage, Inc. 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. You may obtain
# a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License 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.

from testtools import TestCase

from kmip.core import attributes
from kmip.core import objects
from kmip.core import utils
from kmip.core import enums
from kmip.core import exceptions

from kmip.core.messages import payloads


class TestMACRequestPayload(TestCase):

    def setUp(self):
        super(TestMACRequestPayload, self).setUp()

        self.unique_identifier = attributes.UniqueIdentifier(value='1')
        self.cryptographic_parameters = \
            attributes.CryptographicParameters(
                cryptographic_algorithm=enums.CryptographicAlgorithm.
                HMAC_SHA512
            )
        self.data = objects.Data(
            value=(b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B'
                   b'\x0C\x0D\x0E\x0F')
        )

        self.encoding_full = utils.BytearrayStream((
            b'\x42\x00\x79\x01\x00\x00\x00\x40\x42\x00\x94\x07\x00\x00\x00\x01'
            b'\x31\x00\x00\x00\x00\x00\x00\x00\x42\x00\x2b\x01\x00\x00\x00\x10'
            b'\x42\x00\x28\x05\x00\x00\x00\x04\x00\x00\x00\x0b\x00\x00\x00\x00'
            b'\x42\x00\xc2\x08\x00\x00\x00\x10\x00\x01\x02\x03\x04\x05\x06\x07'
            b'\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f'))
        self.encoding_no_data = utils.BytearrayStream((
            b'\x42\x00\x79\x01\x00\x00\x00\x28\x42\x00\x94\x07\x00\x00\x00\x01'
            b'\x31\x00\x00\x00\x00\x00\x00\x00\x42\x00\x2b\x01\x00\x00\x00\x10'
            b'\x42\x00\x28\x05\x00\x00\x00\x04\x00\x00\x00\x0b\x00\x00\x00\x00'
        ))

    def tearDown(self):
        super(TestMACRequestPayload, self).tearDown()

    def test_init_with_none(self):
        payloads.MACRequestPayload()

    def test_init_valid(self):
        """
        Test that the payload can be properly constructed and the attributes
        cab be properly set and retrieved.
        """
        payload = payloads.MACRequestPayload(
            self.unique_identifier,
            self.cryptographic_parameters,
            self.data)
        self.assertEqual(payload.unique_identifier, self.unique_identifier)
        self.assertEqual(payload.cryptographic_parameters,
                         self.cryptographic_parameters)
        self.assertEqual(payload.data, self.data)

    def test_init_with_invalid_unique_identifier(self):
        kwargs = {'unique_identifier': 'invalid',
                  'cryptographic_parameters': None,
                  'data': None}
        self.assertRaisesRegex(
            TypeError, "unique identifier must be UniqueIdentifier type",
            payloads.MACRequestPayload, **kwargs)

    def test_init_with_invalid_cryptographic_parameters(self):
        kwargs = {'unique_identifier': None,
                  'cryptographic_parameters': 'invalid',
                  'data': None}
        self.assertRaisesRegex(
            TypeError,
            "cryptographic parameters must be CryptographicParameters type",
            payloads.MACRequestPayload, **kwargs)

    def test_init_with_invalid_data(self):
        kwargs = {'unique_identifier': None,
                  'cryptographic_parameters': None,
                  'data': 'invalid'}
        self.assertRaises(
            TypeError, "data must be Data type",
            payloads.MACRequestPayload, **kwargs)

    def test_read_valid(self):
        stream = self.encoding_full
        payload = payloads.MACRequestPayload()
        payload.read(stream)

        self.assertEqual(self.unique_identifier, payload.unique_identifier)
        self.assertEqual(self.cryptographic_parameters,
                         payload.cryptographic_parameters)
        self.assertEqual(self.data, payload.data)

    def test_read_no_data(self):
        """
        Test that an InvalidKmipEncoding error gets raised when attempting to
        read a mac request encoding with no data.
        """
        payload = payloads.MACRequestPayload()
        args = (self.encoding_no_data,)
        self.assertRaisesRegex(
            exceptions.InvalidKmipEncoding,
            "expected mac request data not found",
            payload.read,
            *args
        )

    def test_write_valid(self):
        expected = self.encoding_full

        stream = utils.BytearrayStream()
        payload = payloads.MACRequestPayload(
            self.unique_identifier,
            self.cryptographic_parameters,
            self.data)
        payload.write(stream)

        self.assertEqual(expected, stream)

    def test_write_with_no_data(self):
        """
        Test that an InvalidField error gets raised when attempting to
        write a mac request with no data.
        """
        stream = utils.BytearrayStream()
        payload = payloads.MACRequestPayload(
            self.unique_identifier,
            self.cryptographic_parameters,
            None)
        args = (stream,)
        self.assertRaisesRegex(
            exceptions.InvalidField,
            "The mac request data is required",
            payload.write,
            *args
        )


class TestMACResponsePayload(TestCase):

    def setUp(self):
        super(TestMACResponsePayload, self).setUp()

        self.unique_identifier = attributes.UniqueIdentifier(value='1')
        self.mac_data = objects.MACData(value=(
            b'\x99\x8b\x55\x59\x90\x9b\x85\x87\x5b\x90\x63\x13\x12\xbb\x32\x9f'
            b'\x6a\xc4\xed\x97\x6e\xac\x99\xe5\x21\x53\xc4\x19\x28\xf2\x2a\x5b'
            b'\xef\x79\xa4\xbe\x05\x3b\x31\x49\x19\xe0\x75\x23\xb9\xbe\xc8\x23'
            b'\x35\x60\x7e\x49\xba\xa9\x7e\xe0\x9e\x6b\x3d\x55\xf4\x51\xff\x7c'
            )
        )

        self.encoding_full = utils.BytearrayStream((
            b'\x42\x00\x7c\x01\x00\x00\x00\x58\x42\x00\x94\x07\x00\x00\x00\x01'
            b'\x31\x00\x00\x00\x00\x00\x00\x00\x42\x00\xc6\x08\x00\x00\x00\x40'
            b'\x99\x8b\x55\x59\x90\x9b\x85\x87\x5b\x90\x63\x13\x12\xbb\x32\x9f'
            b'\x6a\xc4\xed\x97\x6e\xac\x99\xe5\x21\x53\xc4\x19\x28\xf2\x2a\x5b'
            b'\xef\x79\xa4\xbe\x05\x3b\x31\x49\x19\xe0\x75\x23\xb9\xbe\xc8\x23'
            b'\x35\x60\x7e\x49\xba\xa9\x7e\xe0\x9e\x6b\x3d\x55\xf4\x51\xff\x7c'
        ))
        self.encoding_no_unique_identifier = utils.BytearrayStream((
            b'\x42\x00\x7c\x01\x00\x00\x00\x48\x42\x00\xc6\x08\x00\x00\x00\x40'
            b'\x99\x8b\x55\x59\x90\x9b\x85\x87\x5b\x90\x63\x13\x12\xbb\x32\x9f'
            b'\x6a\xc4\xed\x97\x6e\xac\x99\xe5\x21\x53\xc4\x19\x28\xf2\x2a\x5b'
            b'\xef\x79\xa4\xbe\x05\x3b\x31\x49\x19\xe0\x75\x23\xb9\xbe\xc8\x23'
            b'\x35\x60\x7e\x49\xba\xa9\x7e\xe0\x9e\x6b\x3d\x55\xf4\x51\xff\x7c'
        ))
        self.encoding_no_mac_data = utils.BytearrayStream((
            b'\x42\x00\x7c\x01\x00\x00\x00\x10\x42\x00\x94\x07\x00\x00\x00\x01'
            b'\x31\x00\x00\x00\x00\x00\x00\x00'
        ))

    def tearDown(self):
        super(TestMACResponsePayload, self).tearDown()

    def test_init_with_none(self):
        payloads.MACResponsePayload()

    def test_init_valid(self):
        """
        Test that the payload can be properly constructed and the attributes
        can be properly set and retrieved.
        """
        payload = payloads.MACResponsePayload(
            self.unique_identifier,
            self.mac_data)
        self.assertEqual(payload.unique_identifier, self.unique_identifier)
        self.assertEqual(payload.mac_data, self.mac_data)

    def test_init_with_invalid_unique_identifier(self):
        kwargs = {'unique_identifier': 'invalid',
                  'mac_data': None}
        self.assertRaisesRegex(
            TypeError, "unique identifier must be UniqueIdentifier type",
            payloads.MACResponsePayload, **kwargs)

    def test_init_with_invalid_mac_data(self):
        kwargs = {'unique_identifier': None,
                  'mac_data': 'invalid'}
        self.assertRaises(
            TypeError, "data must be MACData type",
            payloads.MACResponsePayload, **kwargs)

    def test_read_valid(self):
        stream = self.encoding_full
        payload = payloads.MACResponsePayload()
        payload.read(stream)

        self.assertEqual(self.unique_identifier, payload.unique_identifier)
        self.assertEqual(self.mac_data, payload.mac_data)

    def test_read_no_unique_identifier(self):
        """
        Test that an InvalidKmipEncoding error gets raised when attempting to
        read a mac response encoding with no unique identifier.
        """
        payload = payloads.MACResponsePayload()
        args = (self.encoding_no_unique_identifier,)
        self.assertRaisesRegex(
            exceptions.InvalidKmipEncoding,
            "expected mac response unique identifier not found",
            payload.read,
            *args
        )

    def test_read_no_mac_data(self):
        """
        Test that an InvalidKmipEncoding error gets raised when attempting to
        read a mac response encoding with no mac data.
        """
        payload = payloads.MACResponsePayload()
        args = (self.encoding_no_mac_data,)
        self.assertRaisesRegex(
            exceptions.InvalidKmipEncoding,
            "expected mac response mac data not found",
            payload.read,
            *args
        )

    def test_write_valid(self):
        expected = self.encoding_full

        stream = utils.BytearrayStream()
        payload = payloads.MACResponsePayload(
            self.unique_identifier,
            self.mac_data)
        payload.write(stream)

        self.assertEqual(expected, stream)

    def test_write_with_no_unique_identifier(self):
        """
        Test that an InvalidField error gets raised when attempting to
        write a mac response with no unique identifier.
        """
        stream = utils.BytearrayStream()
        payload = payloads.MACResponsePayload(
            None,
            self.mac_data)
        args = (stream,)
        self.assertRaisesRegex(
            exceptions.InvalidField,
            "The mac response unique identifier is required",
            payload.write,
            *args
        )

    def test_write_with_no_data(self):
        """
        Test that an InvalidField error gets raised when attempting to
        write a mac response with no mac data.
        """
        stream = utils.BytearrayStream()
        payload = payloads.MACResponsePayload(
            self.unique_identifier,
            None)
        args = (stream,)
        self.assertRaisesRegex(
            exceptions.InvalidField,
            "The mac response mac data is required",
            payload.write,
            *args
        )