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 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
|
# Copyright (c) 2014 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.attributes import ApplicationData
from kmip.core.attributes import ApplicationNamespace
from kmip.core.attributes import CertificateType
from kmip.core.attributes import CryptographicParameters
from kmip.core.attributes import DigestValue
from kmip.core.attributes import HashingAlgorithm
from kmip.core.attributes import Name
from kmip.core.attributes import OperationPolicyName
from kmip.core.attributes import Tags
from kmip.core.factories.attribute_values import AttributeValueFactory
from kmip.core.enums import AttributeType
from kmip.core.enums import BlockCipherMode
from kmip.core.enums import CertificateTypeEnum
from kmip.core.enums import HashingAlgorithm as HashingAlgorithmEnum
from kmip.core.enums import KeyRoleType
from kmip.core.enums import PaddingMethod
from kmip.core.enums import NameType
from kmip.core.utils import BytearrayStream
class TestNameValue(TestCase):
def setUp(self):
super(TestNameValue, self).setUp()
self.stream = BytearrayStream()
self.stringName1 = 'Jenny'
self.stringName2 = 'Johnny'
def tearDown(self):
super(TestNameValue, self).tearDown()
def test_write_no_padding(self):
self.skip('Not implemented')
def test_write_with_padding(self):
self.skip('Not implemented')
def test_read_no_padding(self):
self.skip('Not implemented')
def test_read_with_padding(self):
self.skip('Not implemented')
def test__eq(self):
name_val = Name.NameValue(self.stringName1)
same_name_val = Name.NameValue(self.stringName1)
other_name_val = Name.NameValue(self.stringName2)
self.assertTrue(name_val == same_name_val)
self.assertFalse(name_val == other_name_val)
self.assertFalse(name_val == 'invalid')
def test__str(self):
name_val = Name.NameValue(self.stringName1)
repr_name = "NameValue(value='{0}')".format(self.stringName1)
self.assertEqual(self.stringName1, str(name_val))
self.assertEqual(repr_name, repr(name_val))
class TestNameType(TestCase):
def setUp(self):
super(TestNameType, self).setUp()
self.stream = BytearrayStream()
self.enum_uri = NameType.URI
self.enum_txt = NameType.UNINTERPRETED_TEXT_STRING
def tearDown(self):
super(TestNameType, self).tearDown()
def test_write_no_padding(self):
self.skip('Not implemented')
def test_write_with_padding(self):
self.skip('Not implemented')
def test_read_no_padding(self):
self.skip('Not implemented')
def test_read_with_padding(self):
self.skip('Not implemented')
def test__eq(self):
type_uri = Name.NameType(self.enum_uri)
same_type = Name.NameType(self.enum_uri)
type_txt = Name.NameType(self.enum_txt)
self.assertTrue(type_uri == same_type)
self.assertFalse(type_uri == type_txt)
self.assertFalse(type_uri == 'invalid')
def test__str(self):
type_uri = Name.NameType(self.enum_uri)
str_uri = "{0}".format(self.enum_uri)
repr_uri = "NameType(value=<{0}: {1}>)".format(
self.enum_uri,
self.enum_uri.value)
self.assertEqual(str_uri, str(type_uri))
self.assertEqual(repr_uri, repr(type_uri))
class TestName(TestCase):
def setUp(self):
super(TestName, self).setUp()
self.stream = BytearrayStream()
self.badFormatName = 8675309
self.stringName1 = 'Jenny'
self.stringName2 = 'Johnny'
self.enumNameType = NameType.UNINTERPRETED_TEXT_STRING
self.enumNameTypeUri = NameType.URI
def tearDown(self):
super(TestName, self).tearDown()
def test_bad_name_value_format(self):
"""
Test that an error is raised in for an incorrectly formatted name
value
"""
name_obj = Name()
name_obj.name_value = self.badFormatName
name_obj.name_type = self.enumNameType
self.assertRaises(TypeError, name_obj.validate)
def test_bad_name_type_format(self):
"""
Test that an error is raised for an incorrectly formatted name type
"""
name_obj = Name()
name_obj.name_value = self.stringName1
name_obj.name_type = self.badFormatName
self.assertRaises(TypeError, name_obj.validate)
def test_name_create_string_input(self):
"""
Test the creation of object names with an enum value for the name type
"""
name_obj = Name.create(self.stringName1, self.enumNameType)
self.assertIsInstance(name_obj.name_value, Name.NameValue)
self.assertEqual(self.stringName1, name_obj.name_value.value)
def test_name_create_bad_input(self):
"""
Test the creation of object names with a bad value input
"""
name_value = self.badFormatName
name_type = self.enumNameType
self.assertRaises(TypeError, Name.create, *(name_value, name_type))
def test_name_create_bad_type_input(self):
"""
Test the creation of object names with a bad value input
"""
self.assertRaises(TypeError, Name.create, *(self.stringName1,
self.badFormatName))
def test__eq(self):
name_obj = Name.create(self.stringName1, self.enumNameType)
same_name = Name.create(self.stringName1, self.enumNameType)
other_name = Name.create(self.stringName2, self.enumNameType)
other_type = Name.create(self.stringName1, self.enumNameTypeUri)
self.assertTrue(name_obj == same_name)
self.assertFalse(name_obj == other_name)
self.assertFalse(name_obj == other_type)
self.assertFalse(name_obj == 'invalid')
def test__str(self):
name_obj = Name.create(self.stringName1, self.enumNameType)
repr_name = (
"Name(type=NameType(value="
"<NameType.UNINTERPRETED_TEXT_STRING: {0}>),"
"value=NameValue(value='{1}'))"
).format(self.enumNameType.value, self.stringName1)
self.assertEqual(self.stringName1, str(name_obj))
self.assertEqual(repr_name, repr(name_obj))
class TestOperationPolicyName(TestCase):
def setUp(self):
super(TestOperationPolicyName, self).setUp()
def tearDown(self):
super(TestOperationPolicyName, self).tearDown()
def _test_operation_policy_name(self, value):
opn = OperationPolicyName(value)
if value is None:
value = ''
msg = "expected {0}, received {1}".format(value, opn.value)
self.assertEqual(value, opn.value, msg)
def test_operation_policy_name(self):
self._test_operation_policy_name('test')
def test_operation_policy_name_on_none(self):
self._test_operation_policy_name(None)
class TestHashingAlgorithm(TestCase):
"""
A test suite for the HashingAlgorithm class.
Since HashingAlgorithm is a simple wrapper for the Enumeration primitive,
only a few tests pertaining to construction are needed.
"""
def setUp(self):
super(TestHashingAlgorithm, self).setUp()
def tearDown(self):
super(TestHashingAlgorithm, self).tearDown()
def _test_init(self, value):
if (isinstance(value, HashingAlgorithmEnum)) or (value is None):
hashing_algorithm = HashingAlgorithm(value)
msg = "expected {0}, observed {1}".format(
value, hashing_algorithm.value)
self.assertEqual(value, hashing_algorithm.value, msg)
else:
self.assertRaises(TypeError, HashingAlgorithm, value)
def test_init_with_none(self):
"""
Test that a HashingAlgorithm object can be constructed with no
specified value.
"""
self._test_init(None)
def test_init_with_valid(self):
"""
Test that a HashingAlgorithm object can be constructed with a valid
HashingAlgorithm enumeration value.
"""
self._test_init(HashingAlgorithmEnum.MD5)
def test_init_with_invalid(self):
"""
Test that a TypeError exception is raised when a non HashingAlgorithm
enumeration value is used to construct a HashingAlgorithm object.
"""
self._test_init("invalid")
# TODO (peter-hamilton) Replace with generic Enumeration subclass test suite.
class TestCertificateType(TestCase):
"""
A test suite for the CertificateType class.
Since CertificateType is a simple wrapper for the Enumeration primitive,
only a few tests pertaining to construction are needed.
"""
def setUp(self):
super(TestCertificateType, self).setUp()
def tearDown(self):
super(TestCertificateType, self).tearDown()
def _test_init(self, value):
if (isinstance(value, CertificateTypeEnum)) or (value is None):
if value is None:
certificate_type = CertificateType()
value = CertificateTypeEnum.X_509
else:
certificate_type = CertificateType(value)
msg = "expected {0}, observed {1}".format(
value, certificate_type.value)
self.assertEqual(value, certificate_type.value, msg)
else:
self.assertRaises(TypeError, CertificateType, value)
def test_init_with_none(self):
"""
Test that a CertificateType object can be constructed with no specified
value.
"""
self._test_init(None)
def test_init_with_valid(self):
"""
Test that a CertificateType object can be constructed with valid byte
data.
"""
self._test_init(CertificateTypeEnum.PGP)
class TestDigestValue(TestCase):
"""
A test suite for the DigestValue class.
Since DigestValue is a simple wrapper for the ByteString primitive, only
a few tests pertaining to construction are needed.
"""
def setUp(self):
super(TestDigestValue, self).setUp()
def tearDown(self):
super(TestDigestValue, self).tearDown()
def _test_init(self, value):
if (isinstance(value, bytes)) or (value is None):
digest_value = DigestValue(value)
if value is None:
value = bytes()
msg = "expected {0}, observed {1}".format(
value, digest_value.value)
self.assertEqual(value, digest_value.value, msg)
else:
self.assertRaises(TypeError, DigestValue, value)
def test_init_with_none(self):
"""
Test that a DigestValue object can be constructed with no specified
value.
"""
self._test_init(None)
def test_init_with_valid(self):
"""
Test that a DigestValue object can be constructed with valid byte data.
"""
self._test_init(b'\x00\x01\x02\x03')
class TestApplicationNamespace(TestCase):
"""
A test suite for the ApplicationNamespace class.
Since ApplicationNamespace is a simple wrapper for the TextString
primitive, only a few tests pertaining to construction are needed.
"""
def setUp(self):
super(TestApplicationNamespace, self).setUp()
def tearDown(self):
super(TestApplicationNamespace, self).tearDown()
def _test_init(self, value):
if (isinstance(value, str)) or (value is None):
application_namespace = ApplicationNamespace(value)
if value is None:
value = ''
msg = "expected {0}, observed {1}".format(
value, application_namespace.value)
self.assertEqual(value, application_namespace.value, msg)
else:
self.assertRaises(TypeError, ApplicationNamespace, value)
def test_init_with_none(self):
"""
Test that an ApplicationNamespace object can be constructed with no
specified value.
"""
self._test_init(None)
def test_init_with_valid(self):
"""
Test that an ApplicationNamespace 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 an ApplicationNamespace object.
"""
self._test_init(0)
class TestApplicationData(TestCase):
"""
A test suite for the ApplicationData class.
Since ApplicationData is a simple wrapper for the TextString primitive,
only a few tests pertaining to construction are needed.
"""
def setUp(self):
super(TestApplicationData, self).setUp()
def tearDown(self):
super(TestApplicationData, self).tearDown()
def _test_init(self, value):
if (isinstance(value, str)) or (value is None):
application_data = ApplicationData(value)
if value is None:
value = ''
msg = "expected {0}, observed {1}".format(
value, application_data.value)
self.assertEqual(value, application_data.value, msg)
else:
self.assertRaises(TypeError, ApplicationData, value)
def test_init_with_none(self):
"""
Test that an ApplicationData object can be constructed with no
specified value.
"""
self._test_init(None)
def test_init_with_valid(self):
"""
Test that an ApplicationData 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 an ApplicationData object.
"""
self._test_init(0)
class TestCryptographicParameters(TestCase):
"""
A test suite for the CryptographicParameters class
"""
def setUp(self):
super(TestCryptographicParameters, self).setUp()
self.bad_enum_code = 8535937
self.factory = AttributeValueFactory()
self.cp = self.factory.create_attribute_value(
AttributeType.CRYPTOGRAPHIC_PARAMETERS,
{'block_cipher_mode': BlockCipherMode.CBC,
'padding_method': PaddingMethod.PKCS5,
'hashing_algorithm': HashingAlgorithmEnum.SHA_1,
'key_role_type': KeyRoleType.BDK})
self.cp_none = self.factory.create_attribute_value(
AttributeType.CRYPTOGRAPHIC_PARAMETERS, {})
# Symmetric key object with Cryptographic Parameters
# Byte stream edited to add Key Role Type parameter
# Based on the KMIP Spec 1.1 Test Cases document
# 11.1 page 255 on the pdf version
self.key_req_with_crypt_params = BytearrayStream((
b'\x42\x00\x2B\x01\x00\x00\x00\x40'
b'\x42\x00\x11\x05\x00\x00\x00\x04\x00\x00\x00\x01\x00\x00\x00\x00'
b'\x42\x00\x5F\x05\x00\x00\x00\x04\x00\x00\x00\x03\x00\x00\x00\x00'
b'\x42\x00\x38\x05\x00\x00\x00\x04\x00\x00\x00\x04\x00\x00\x00\x00'
b'\x42\x00\x83\x05\x00\x00\x00\x04\x00\x00\x00\x01\x00\x00\x00\x00'
))
def teardown(self):
super(TestDigestValue, self).tearDown()
def test_write_crypto_params(self):
ostream = BytearrayStream()
self.cp.write(ostream)
self.assertEqual(self.key_req_with_crypt_params, ostream)
def test_read_crypto_params(self):
CryptographicParameters.read(self.cp, self.key_req_with_crypt_params)
self.assertEqual(Tags.BLOCK_CIPHER_MODE.value,
self.cp.block_cipher_mode.tag.value)
self.assertEqual(BlockCipherMode.CBC.value,
self.cp.block_cipher_mode.value.value)
self.assertEqual(Tags.PADDING_METHOD.value,
self.cp.padding_method.tag.value)
self.assertEqual(PaddingMethod.PKCS5.value,
self.cp.padding_method.value.value)
self.assertEqual(Tags.KEY_ROLE_TYPE.value,
self.cp.key_role_type.tag.value)
self.assertEqual(KeyRoleType.BDK.value,
self.cp.key_role_type.value.value)
self.assertEqual(Tags.HASHING_ALGORITHM.value,
self.cp.hashing_algorithm.tag.value)
self.assertEqual(HashingAlgorithmEnum.SHA_1.value,
self.cp.hashing_algorithm.value.value)
def test_bad_cipher_mode(self):
self.cp.block_cipher_mode = self.bad_enum_code
cp_valid = self.factory.create_attribute_value(
AttributeType.CRYPTOGRAPHIC_PARAMETERS,
{'block_cipher_mode': BlockCipherMode.CBC,
'padding_method': PaddingMethod.PKCS5,
'hashing_algorithm': HashingAlgorithmEnum.SHA_1,
'key_role_type': KeyRoleType.BDK})
self.assertFalse(self.cp == cp_valid)
self.assertRaises(TypeError, self.cp.validate)
def test_bad_padding_method(self):
self.cp.padding_method = self.bad_enum_code
cp_valid = self.factory.create_attribute_value(
AttributeType.CRYPTOGRAPHIC_PARAMETERS,
{'block_cipher_mode': BlockCipherMode.CBC,
'padding_method': PaddingMethod.PKCS5,
'hashing_algorithm': HashingAlgorithmEnum.SHA_1,
'key_role_type': KeyRoleType.BDK})
self.assertFalse(self.cp == cp_valid)
self.assertRaises(TypeError, self.cp.validate)
def test_bad_hash_algorithm(self):
self.cp.hashing_algorithm = self.bad_enum_code
cp_valid = self.factory.create_attribute_value(
AttributeType.CRYPTOGRAPHIC_PARAMETERS,
{'block_cipher_mode': BlockCipherMode.CBC,
'padding_method': PaddingMethod.PKCS5,
'hashing_algorithm': HashingAlgorithmEnum.SHA_1,
'key_role_type': KeyRoleType.BDK})
self.assertFalse(self.cp == cp_valid)
self.assertRaises(TypeError, self.cp.validate)
def test_bad_key_role_type(self):
self.cp.key_role_type = self.bad_enum_code
cp_valid = self.factory.create_attribute_value(
AttributeType.CRYPTOGRAPHIC_PARAMETERS,
{'block_cipher_mode': BlockCipherMode.CBC,
'padding_method': PaddingMethod.PKCS5,
'hashing_algorithm': HashingAlgorithmEnum.SHA_1,
'key_role_type': KeyRoleType.BDK})
self.assertFalse(self.cp == cp_valid)
self.assertRaises(TypeError, self.cp.validate)
def test_bad_object(self):
name_value = 'puppies'
name_type = NameType.UNINTERPRETED_TEXT_STRING
bad_obj = Name.create(name_value, name_type)
self.assertNotEqual(NotImplemented, bad_obj)
|