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 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
|
# 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.
import testtools
from kmip.core import exceptions
from kmip.core import primitives
from kmip.core import utils
class TestLongInteger(testtools.TestCase):
"""
Test suite for the LongInteger primitive.
"""
def setUp(self):
super(TestLongInteger, self).setUp()
def tearDown(self):
super(TestLongInteger, self).tearDown()
def test_init(self):
"""
Test that a LongInteger can be instantiated.
"""
long_int = primitives.LongInteger(1)
self.assertEqual(1, long_int.value)
def test_init_unset(self):
"""
Test that a LongInteger can be instantiated with no input.
"""
long_int = primitives.LongInteger()
self.assertEqual(0, long_int.value)
def test_init_on_max(self):
"""
Test that a LongInteger can be instantiated with the maximum possible
signed 64-bit value.
"""
primitives.LongInteger(primitives.LongInteger.MAX)
def test_init_on_min(self):
"""
Test that a LongInteger can be instantiated with the minimum possible
signed 64-bit value.
"""
primitives.LongInteger(primitives.LongInteger.MIN)
def test_validate_on_valid(self):
"""
Test that a LongInteger can be validated on good input.
"""
long_int = primitives.LongInteger(1)
long_int.validate()
def test_validate_on_valid_unset(self):
"""
Test that a LongInteger with no preset value can be validated.
"""
long_int = primitives.LongInteger()
long_int.validate()
def test_validate_on_invalid_type(self):
"""
Test that a TypeError is thrown on input of invalid type (e.g., str).
"""
self.assertRaises(TypeError, primitives.LongInteger, 'invalid')
def test_validate_on_invalid_value_too_big(self):
"""
Test that a ValueError is thrown on input that is too large.
"""
self.assertRaises(
ValueError, primitives.LongInteger, primitives.LongInteger.MAX + 1)
def test_validate_on_invalid_value_too_small(self):
"""
Test that a ValueError is thrown on input that is too small.
"""
self.assertRaises(
ValueError, primitives.LongInteger, primitives.LongInteger.MIN - 1)
def test_read_zero(self):
"""
Test that a LongInteger representing the value 0 can be read from a
byte stream.
"""
encoding = (
b'\x42\x00\x00\x03\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00'
b'\x00')
stream = utils.BytearrayStream(encoding)
long_int = primitives.LongInteger()
long_int.read(stream)
self.assertEqual(0, long_int.value)
def test_read_max_max(self):
"""
Test that a LongInteger representing the maximum positive value can be
read from a byte stream.
"""
encoding = (
b'\x42\x00\x00\x03\x00\x00\x00\x08\x7f\xff\xff\xff\xff\xff\xff'
b'\xff')
stream = utils.BytearrayStream(encoding)
long_int = primitives.LongInteger()
long_int.read(stream)
self.assertEqual(primitives.LongInteger.MAX, long_int.value)
def test_read_min_max(self):
"""
Test that a LongInteger representing the minimum positive value can be
read from a byte stream.
"""
encoding = (
b'\x42\x00\x00\x03\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00'
b'\x01')
stream = utils.BytearrayStream(encoding)
long_int = primitives.LongInteger()
long_int.read(stream)
self.assertEqual(1, long_int.value)
def test_read_max_min(self):
"""
Test that a LongInteger representing the maximum negative value can be
read from a byte stream.
"""
encoding = (
b'\x42\x00\x00\x03\x00\x00\x00\x08\xff\xff\xff\xff\xff\xff\xff'
b'\xff')
stream = utils.BytearrayStream(encoding)
long_int = primitives.LongInteger()
long_int.read(stream)
self.assertEqual(-1, long_int.value)
def test_read_min_min(self):
"""
Test that a LongInteger representing the minimum negative value can be
read from a byte stream.
"""
encoding = (
b'\x42\x00\x00\x03\x00\x00\x00\x08\x80\x00\x00\x00\x00\x00\x00'
b'\x00')
stream = utils.BytearrayStream(encoding)
long_int = primitives.LongInteger(primitives.LongInteger.MIN)
long_int.read(stream)
self.assertEqual(primitives.LongInteger.MIN, long_int.value)
def test_read_on_invalid_length(self):
"""
Test that an InvalidPrimitiveLength exception is thrown when attempting
to decode a LongInteger with an invalid length.
"""
encoding = (
b'\x42\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00')
stream = utils.BytearrayStream(encoding)
long_int = primitives.LongInteger()
self.assertRaises(
exceptions.InvalidPrimitiveLength, long_int.read, stream)
def test_write_zero(self):
"""
Test that a LongInteger representing the value 0 can be written to a
byte stream.
"""
encoding = (
b'\x42\x00\x00\x03\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00'
b'\x00')
stream = utils.BytearrayStream()
long_int = primitives.LongInteger(0)
long_int.write(stream)
result = stream.read()
self.assertEqual(len(encoding), len(result))
self.assertEqual(encoding, result)
def test_write_max_max(self):
"""
Test that a LongInteger representing the maximum positive value can be
written to a byte stream.
"""
encoding = (
b'\x42\x00\x00\x03\x00\x00\x00\x08\x7f\xff\xff\xff\xff\xff\xff'
b'\xff')
stream = utils.BytearrayStream()
long_int = primitives.LongInteger(primitives.LongInteger.MAX)
long_int.write(stream)
result = stream.read()
self.assertEqual(len(encoding), len(result))
self.assertEqual(encoding, result)
def test_write_min_max(self):
"""
Test that a LongInteger representing the minimum positive value can be
written to a byte stream.
"""
encoding = (
b'\x42\x00\x00\x03\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00'
b'\x01')
stream = utils.BytearrayStream()
long_int = primitives.LongInteger(1)
long_int.write(stream)
result = stream.read()
self.assertEqual(len(encoding), len(result))
self.assertEqual(encoding, result)
def test_write_max_min(self):
"""
Test that a LongInteger representing the maximum negative value can be
written to a byte stream.
"""
encoding = (
b'\x42\x00\x00\x03\x00\x00\x00\x08\xff\xff\xff\xff\xff\xff\xff'
b'\xff')
stream = utils.BytearrayStream()
long_int = primitives.LongInteger(-1)
long_int.write(stream)
result = stream.read()
self.assertEqual(len(encoding), len(result))
self.assertEqual(encoding, result)
def test_write_min_min(self):
"""
Test that a LongInteger representing the minimum negative value can be
written to a byte stream.
"""
encoding = (
b'\x42\x00\x00\x03\x00\x00\x00\x08\x80\x00\x00\x00\x00\x00\x00'
b'\x00')
stream = utils.BytearrayStream()
long_int = primitives.LongInteger(primitives.LongInteger.MIN)
long_int.write(stream)
result = stream.read()
self.assertEqual(len(encoding), len(result))
self.assertEqual(encoding, result)
def test_repr(self):
"""
Test that the representation of a LongInteger is formatted properly.
"""
long_int = primitives.LongInteger()
value = "value={0}".format(long_int.value)
tag = "tag={0}".format(long_int.tag)
self.assertEqual(
"LongInteger({0}, {1})".format(value, tag), repr(long_int))
def test_str(self):
"""
Test that the string representation of a LongInteger is formatted
properly.
"""
self.assertEqual("0", str(primitives.LongInteger()))
def test_equal_on_equal(self):
"""
Test that the equality operator returns True when comparing two
LongIntegers.
"""
a = primitives.LongInteger(1)
b = primitives.LongInteger(1)
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
LongIntegers.
"""
a = primitives.LongInteger()
b = primitives.LongInteger()
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
LongIntegers with different values.
"""
a = primitives.LongInteger(1)
b = primitives.LongInteger(2)
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
LongInteger to a non-LongInteger object.
"""
a = primitives.LongInteger()
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 LongIntegers with the same values.
"""
a = primitives.LongInteger(1)
b = primitives.LongInteger(1)
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 LongIntegers.
"""
a = primitives.LongInteger()
b = primitives.LongInteger()
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
LongIntegers with different values.
"""
a = primitives.LongInteger(1)
b = primitives.LongInteger(2)
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
LongInteger to a non-LongInteger object.
"""
a = primitives.LongInteger()
b = 'invalid'
self.assertTrue(a != b)
self.assertTrue(b != a)
|