File: test_boolean.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 (280 lines) | stat: -rw-r--r-- 8,932 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
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
# 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 testtools import TestCase

from kmip.core import primitives
from kmip.core import utils


class TestBoolean(TestCase):

    def setUp(self):
        super(TestBoolean, self).setUp()
        self.stream = utils.BytearrayStream()

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

    def test_init(self):
        """
        Test that a Boolean object can be instantiated.
        """
        boolean = primitives.Boolean(False)
        self.assertEqual(False, boolean.value)

    def test_init_unset(self):
        """
        Test that a Boolean object can be instantiated with no input.
        """
        boolean = primitives.Boolean()
        self.assertEqual(True, boolean.value)

    def test_validate_on_valid(self):
        """
        Test that a Boolean object can be validated on good input.
        """
        boolean = primitives.Boolean(True)
        boolean.validate()

    def test_validate_on_valid_unset(self):
        """
        Test that a Boolean object with no preset value can be validated.
        """
        boolean = primitives.Boolean()
        boolean.validate()

    def test_validate_on_invalid_type(self):
        """
        Test that a TypeError is raised when a Boolean object is built with an
        invalid value.
        """
        self.assertRaises(TypeError, primitives.Boolean, 'invalid')

    def test_read_true(self):
        """
        Test that a Boolean object representing the value True can be read
        from a byte stream.
        """
        encoding = (
            b'\x42\x00\x00\x06\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00'
            b'\x01')
        stream = utils.BytearrayStream(encoding)
        boolean = primitives.Boolean()
        boolean.read(stream)
        self.assertTrue(boolean.value)

    def test_read_false(self):
        """
        Test that a Boolean object representing the value False can be read
        from a byte stream.
        """
        encoding = (
            b'\x42\x00\x00\x06\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00'
            b'\x00')
        stream = utils.BytearrayStream(encoding)
        boolean = primitives.Boolean()
        boolean.read(stream)
        self.assertFalse(boolean.value)

    def test_read_bad_encoding(self):
        """
        Test that an Exception is raised when the Boolean read operation fails
        on a bad encoding.
        """
        encoding = (b'\x42\x00\x00\x06\x00\x00\x00\x08')
        stream = utils.BytearrayStream(encoding)
        boolean = primitives.Boolean()
        self.assertRaises(Exception, boolean.read, stream)

    def test_read_bad_value(self):
        """
        Test that a ValueError is raised when the Boolean read operations
        reads a valid integer but invalid Boolean.
        """
        encoding = (
            b'\x42\x00\x00\x06\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00'
            b'\x02')
        stream = utils.BytearrayStream(encoding)
        boolean = primitives.Boolean()
        self.assertRaises(ValueError, boolean.read, stream)

    def test_write_true(self):
        """
        Test that a Boolean object representing the value True can be written
        to a byte stream.
        """
        encoding = (
            b'\x42\x00\x00\x06\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00'
            b'\x01')
        stream = utils.BytearrayStream()
        boolean = primitives.Boolean(True)
        boolean.write(stream)
        self.assertEqual(encoding, stream.read())

    def test_write_false(self):
        """
        Test that a Boolean object representing the value False can be written
        to a byte stream.
        """
        encoding = (
            b'\x42\x00\x00\x06\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00'
            b'\x00')
        stream = utils.BytearrayStream()
        boolean = primitives.Boolean(False)
        boolean.write(stream)
        self.assertEqual(encoding, stream.read())

    def test_write_bad_value(self):
        """
        Test that an Exception is raised when the Boolean write operation fails
        on a bad Boolean value.
        """
        stream = utils.BytearrayStream()
        boolean = primitives.Boolean()
        boolean.value = 'invalid'
        self.assertRaises(Exception, boolean.write, stream)

    def test_repr_default(self):
        """
        Test that the representation of a Boolean object is formatted properly
        """
        boolean = primitives.Boolean()
        self.assertEqual("Boolean(value=True)", repr(boolean))

    def test_repr_true(self):
        """
        Test that the representation of a Boolean object representing the
        value True is formatted properly.
        """
        boolean = primitives.Boolean(True)
        self.assertEqual("Boolean(value=True)", repr(boolean))

    def test_repr_false(self):
        """
        Test that the representation of a Boolean object representing the
        value False is formatted properly.
        """
        boolean = primitives.Boolean(False)
        self.assertEqual("Boolean(value=False)", repr(boolean))

    def test_str_default(self):
        """
        Test that the string representation of a Boolean object is formatted
        properly.
        """
        self.assertEqual("True", str(primitives.Boolean()))

    def test_str_true(self):
        """
        Test that the string representation of a Boolean object representing
        the value True is formatted properly.
        """
        self.assertEqual("True", str(primitives.Boolean(True)))

    def test_str_false(self):
        """
        Test that the string representation of a Boolean object representing
        the value False is formatted properly.
        """
        self.assertEqual("False", str(primitives.Boolean(False)))

    def test_equal_on_equal(self):
        """
        Test that the equality operator returns True when comparing two
        Boolean objects.
        """
        a = primitives.Boolean(False)
        b = primitives.Boolean(False)

        self.assertTrue(a == b)
        self.assertTrue(b == a)

    def test_equal_on_equal_and_empty(self):
        """
        Test that the equality operator returns True when comparing two
        Boolean objects.
        """
        a = primitives.Boolean()
        b = primitives.Boolean()

        self.assertTrue(a == b)
        self.assertTrue(b == a)

    def test_equal_on_not_equal(self):
        """
        Test that the equality operator returns False when comparing two
        Boolean objects with different values.
        """
        a = primitives.Boolean(True)
        b = primitives.Boolean(False)

        self.assertFalse(a == b)
        self.assertFalse(b == a)

    def test_equal_on_type_mismatch(self):
        """
        Test that the equality operator returns False when comparing a
        Boolean object to a non-Boolean object.
        """
        a = primitives.Boolean()
        b = 'invalid'

        self.assertFalse(a == b)
        self.assertFalse(b == a)

    def test_not_equal_on_equal(self):
        """
        Test that the inequality operator returns False when comparing
        two Boolean objects with the same values.
        """
        a = primitives.Boolean(False)
        b = primitives.Boolean(False)

        self.assertFalse(a != b)
        self.assertFalse(b != a)

    def test_not_equal_on_equal_and_empty(self):
        """
        Test that the inequality operator returns False when comparing
        two Boolean objects.
        """
        a = primitives.Boolean()
        b = primitives.Boolean()

        self.assertFalse(a != b)
        self.assertFalse(b != a)

    def test_not_equal_on_not_equal(self):
        """
        Test that the inequality operator returns True when comparing two
        Boolean objects with different values.
        """
        a = primitives.Boolean(True)
        b = primitives.Boolean(False)

        self.assertTrue(a != b)
        self.assertTrue(b != a)

    def test_not_equal_on_type_mismatch(self):
        """
        Test that the inequality operator returns True when comparing a
        Boolean object to a non-Boolean object.
        """
        a = primitives.Boolean()
        b = 'invalid'

        self.assertTrue(a != b)
        self.assertTrue(b != a)