File: test_misc.py

package info (click to toggle)
python-pykmip 0.5.0-4%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,388 kB
  • sloc: python: 29,126; makefile: 34; sh: 32
file content (211 lines) | stat: -rw-r--r-- 6,733 bytes parent folder | download | duplicates (5)
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
# Copyright (c) 2015 The Johns Hopkins University/Applied Physics Laboratory
# 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 six import binary_type
from six import string_types

from testtools import TestCase

from kmip.core.enums import KeyFormatType as KeyFormatTypeEnum
from kmip.core.enums import QueryFunction as QueryFunctionEnum

from kmip.core.misc import CertificateValue
from kmip.core.misc import KeyFormatType
from kmip.core.misc import QueryFunction
from kmip.core.misc import VendorIdentification


# TODO (peter-hamilton) Replace with generic ByteString subclass test suite.
class TestCertificateValue(TestCase):
    """
    A test suite for the CertificateValue class.

    Since CertificateValue is a simple wrapper for the ByteString primitive,
    only a few tests pertaining to construction are needed.
    """

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

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

    def _test_init(self, value):
        if (isinstance(value, binary_type)) or (value is None):
            certificate_value = CertificateValue(value)

            if value is None:
                value = b''

            msg = "expected {0}, observed {1}".format(
                value, certificate_value.value)
            self.assertEqual(value, certificate_value.value, msg)
        else:
            self.assertRaises(TypeError, CertificateValue, value)

    def test_init_with_none(self):
        """
        Test that a CertificateValue object can be constructed with no
        specified value.
        """
        self._test_init(None)

    def test_init_with_valid(self):
        """
        Test that a CertificateValue object can be constructed with a valid,
        byte-string value.
        """
        self._test_init(b'\x00\x01\x02')


class TestQueryFunction(TestCase):
    """
    A test suite for the QueryFunction class.

    Since QueryFunction is a simple wrapper for the Enumeration primitive,
    only a few tests pertaining to construction are needed.
    """

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

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

    def _test_init(self, value):
        if (isinstance(value, QueryFunctionEnum)) or (value is None):
            query_function = QueryFunction(value)

            msg = "expected {0}, observed {1}".format(
                value, query_function.value)
            self.assertEqual(value, query_function.value, msg)
        else:
            self.assertRaises(TypeError, QueryFunction, value)

    def test_init_with_none(self):
        """
        Test that a QueryFunction object can be constructed with no specified
        value.
        """
        self._test_init(None)

    def test_init_with_valid(self):
        """
        Test that a QueryFunction object can be constructed with a valid
        QueryFunction enumeration value.
        """
        self._test_init(QueryFunctionEnum.QUERY_OBJECTS)

    def test_init_with_invalid(self):
        """
        Test that a TypeError exception is raised when a non QueryFunction
        enumeration value is used to construct a QueryFunction object.
        """
        self._test_init("invalid")


class TestVendorIdentification(TestCase):
    """
    A test suite for the VendorIdentification class.

    Since VendorIdentification is a simple wrapper for the TextString
    primitive, only a few tests pertaining to construction are needed.
    """

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

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

    def _test_init(self, value):
        if (isinstance(value, string_types)) or (value is None):
            vendor_identification = VendorIdentification(value)

            if value is None:
                value = ''

            msg = "expected {0}, observed {1}".format(
                value, vendor_identification.value)
            self.assertEqual(value, vendor_identification.value, msg)
        else:
            self.assertRaises(TypeError, VendorIdentification, value)

    def test_init_with_none(self):
        """
        Test that a VendorIdentification object can be constructed with no
        specified value.
        """
        self._test_init(None)

    def test_init_with_valid(self):
        """
        Test that a VendorIdentification object can be constructed with a
        valid, string-type value.
        """
        self._test_init("valid")

    def test_init_with_invalid(self):
        """
        Test that a TypeError exception is raised when a non-string value is
        used to construct a VendorIdentification object.
        """
        self._test_init(0)


class TestKeyFormatType(TestCase):
    """
    A test suite for the KeyFormatType class.

    Since KeyFormatType is a simple wrapper for the Enumeration primitive,
    only a few tests pertaining to construction are needed.
    """

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

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

    def _test_init(self, value):
        if (isinstance(value, KeyFormatTypeEnum)) or (value is None):
            key_format_type = KeyFormatType(value)

            msg = "expected {0}, observed {1}".format(
                value, key_format_type.value)
            self.assertEqual(value, key_format_type.value, msg)
        else:
            self.assertRaises(TypeError, KeyFormatType, value)

    def test_init_with_none(self):
        """
        Test that a KeyFormatType object can be constructed with no specified
        value.
        """
        self._test_init(None)

    def test_init_with_valid(self):
        """
        Test that a KeyFormatType object can be constructed with a valid
        KeyFormatType enumeration value.
        """
        self._test_init(KeyFormatTypeEnum.RAW)

    def test_init_with_invalid(self):
        """
        Test that a TypeError exception is raised when a non KeyFormatType
        enumeration value is used to construct a KeyFormatType object.
        """
        self._test_init("invalid")