def test_not_equal_on_not_equal_template_attribute(self): """ Test that the inequality operator returns True when comparing two Rekey response payloads with different template attributes. """ a = payloads.RekeyResponsePayload( template_attribute=objects.TemplateAttribute(attributes=[ objects.Attribute(attribute_name=objects.Attribute. AttributeName('Cryptographic Algorithm'), attribute_value=primitives.Enumeration( enums.CryptographicAlgorithm, value=enums.CryptographicAlgorithm.AES, tag=enums.Tags.CRYPTOGRAPHIC_ALGORITHM)) ])) b = payloads.RekeyResponsePayload( template_attribute=objects.TemplateAttribute(attributes=[ objects.Attribute( attribute_name=objects.Attribute.AttributeName( 'Cryptographic Length'), attribute_value=primitives.Integer( value=128, tag=enums.Tags.CRYPTOGRAPHIC_LENGTH)) ])) self.assertTrue(a != b) self.assertTrue(b != a)
def test_not_equal_on_not_equal_unique_identifier(self): """ Test that the inequality operator returns True when comparing two Rekey response payloads with different unique identifiers. """ a = payloads.RekeyResponsePayload(unique_identifier='a') b = payloads.RekeyResponsePayload(unique_identifier='b') self.assertTrue(a != b) self.assertTrue(b != a)
def test_write(self): """ Test that a Rekey response payload can be written to a data stream. """ payload = payloads.RekeyResponsePayload( unique_identifier='8efbbd67-2847-46b5-b7e7-4ab3b5e175de', template_attribute=objects.TemplateAttribute( attributes=[ objects.Attribute( attribute_name=objects.Attribute.AttributeName( 'Cryptographic Algorithm' ), attribute_value=primitives.Enumeration( enums.CryptographicAlgorithm, value=enums.CryptographicAlgorithm.AES, tag=enums.Tags.CRYPTOGRAPHIC_ALGORITHM ) ), objects.Attribute( attribute_name=objects.Attribute.AttributeName( 'Cryptographic Length' ), attribute_value=primitives.Integer( value=128, tag=enums.Tags.CRYPTOGRAPHIC_LENGTH ) ) ] ) ) stream = utils.BytearrayStream() payload.write(stream) self.assertEqual(len(self.full_encoding), len(stream)) self.assertEqual(str(self.full_encoding), str(stream))
def test_str(self): """ Test that str can be applied to a Rekey response payload """ payload = payloads.RekeyResponsePayload( unique_identifier='49a1ca88-6bea-4fb2-b450-7e58802c3038', template_attribute=objects.TemplateAttribute( attributes=[ objects.Attribute( attribute_name=objects.Attribute.AttributeName( 'Cryptographic Length' ), attribute_value=primitives.Integer( value=128, tag=enums.Tags.CRYPTOGRAPHIC_LENGTH ) ) ] ) ) # TODO (peter-hamilton) Update this when TemplateAttributes have str expected = str({ 'unique_identifier': '49a1ca88-6bea-4fb2-b450-7e58802c3038', 'template_attribute': 'Struct()' }) observed = str(payload) self.assertEqual(expected, observed)
def test_read(self): """ Test that a Rekey response payload can be read from a data stream. """ payload = payloads.RekeyResponsePayload() self.assertEqual(None, payload.unique_identifier) self.assertEqual(None, payload.template_attribute) payload.read(self.full_encoding) self.assertEqual('8efbbd67-2847-46b5-b7e7-4ab3b5e175de', payload.unique_identifier) self.assertEqual( objects.TemplateAttribute(attributes=[ objects.Attribute(attribute_name=objects.Attribute. AttributeName('Cryptographic Algorithm'), attribute_value=primitives.Enumeration( enums.CryptographicAlgorithm, value=enums.CryptographicAlgorithm.AES, tag=enums.Tags.CRYPTOGRAPHIC_ALGORITHM)), objects.Attribute( attribute_name=objects.Attribute.AttributeName( 'Cryptographic Length'), attribute_value=primitives.Integer( value=128, tag=enums.Tags.CRYPTOGRAPHIC_LENGTH)) ]), payload.template_attribute)
def test_init(self): """ Test that a Rekey response payload can be constructed with no arguments. """ payload = payloads.RekeyResponsePayload() self.assertEqual(None, payload.unique_identifier) self.assertEqual(None, payload.template_attribute)
def test_not_equal_on_equal(self): """ Test that the inequality operator returns False when comparing two Rekey response payloads with the same data. """ a = payloads.RekeyResponsePayload() b = payloads.RekeyResponsePayload() self.assertFalse(a != b) self.assertFalse(b != a) a = payloads.RekeyResponsePayload( unique_identifier='1346d253-69d6-474c-8cd5-ad475a3e0a81', template_attribute=objects.TemplateAttribute(attributes=[ objects.Attribute(attribute_name=objects.Attribute. AttributeName('Cryptographic Algorithm'), attribute_value=primitives.Enumeration( enums.CryptographicAlgorithm, value=enums.CryptographicAlgorithm.AES, tag=enums.Tags.CRYPTOGRAPHIC_ALGORITHM)), objects.Attribute( attribute_name=objects.Attribute.AttributeName( 'Cryptographic Length'), attribute_value=primitives.Integer( value=128, tag=enums.Tags.CRYPTOGRAPHIC_LENGTH)) ])) b = payloads.RekeyResponsePayload( unique_identifier='1346d253-69d6-474c-8cd5-ad475a3e0a81', template_attribute=objects.TemplateAttribute(attributes=[ objects.Attribute(attribute_name=objects.Attribute. AttributeName('Cryptographic Algorithm'), attribute_value=primitives.Enumeration( enums.CryptographicAlgorithm, value=enums.CryptographicAlgorithm.AES, tag=enums.Tags.CRYPTOGRAPHIC_ALGORITHM)), objects.Attribute( attribute_name=objects.Attribute.AttributeName( 'Cryptographic Length'), attribute_value=primitives.Integer( value=128, tag=enums.Tags.CRYPTOGRAPHIC_LENGTH)) ])) self.assertFalse(a != b) self.assertFalse(b != a)
def test_not_equal_on_type_mismatch(self): """ Test that the inequality operator returns True when comparing two Rekey response payloads with different types. """ a = payloads.RekeyResponsePayload() b = 'invalid' self.assertTrue(a != b) self.assertTrue(b != a)
def test_read_invalid(self): """ Test that a ValueError gets raised when a required Rekey response payload attribute is missing from the payload encoding. """ payload = payloads.RekeyResponsePayload() args = (self.empty_encoding, ) self.assertRaisesRegex( ValueError, "The Rekey response payload encoding is missing the unique " "identifier.", payload.read, *args)
def test_write_invalid(self): """ Test that a ValueError gets raised when a required Rekey response payload attribute is missing when encoding the payload. """ payload = payloads.RekeyResponsePayload() stream = utils.BytearrayStream() args = (stream, ) self.assertRaisesRegex( ValueError, "The Rekey response payload is missing the unique identifier.", payload.write, *args)
def test_write_partial(self): """ Test that a partial Rekey response payload can be written to a data stream. """ payload = payloads.RekeyResponsePayload( unique_identifier='8efbbd67-2847-46b5-b7e7-4ab3b5e175de') stream = utils.BytearrayStream() payload.write(stream) self.assertEqual(len(self.partial_encoding), len(stream)) self.assertEqual(str(self.partial_encoding), str(stream))
def test_init_with_args(self): """ Test that a Rekey response payload can be constructed with valid values. """ payload = payloads.RekeyResponsePayload( unique_identifier='00000000-2222-4444-6666-888888888888', template_attribute=objects.TemplateAttribute()) self.assertEqual('00000000-2222-4444-6666-888888888888', payload.unique_identifier) self.assertEqual(objects.TemplateAttribute(), payload.template_attribute)
def test_invalid_unique_identifier(self): """ Test that a TypeError is raised when an invalid value is used to set the unique identifier of a Rekey response payload. """ kwargs = {'unique_identifier': 0} self.assertRaisesRegex(TypeError, "Unique identifier must be a string.", payloads.RekeyResponsePayload, **kwargs) args = (payloads.RekeyResponsePayload(), 'unique_identifier', 0) self.assertRaisesRegex(TypeError, "Unique identifier must be a string.", setattr, *args)
def test_read_partial(self): """ Test that a Rekey response payload can be read from a partial data stream. """ payload = payloads.RekeyResponsePayload() self.assertEqual(None, payload.unique_identifier) self.assertEqual(None, payload.template_attribute) payload.read(self.partial_encoding) self.assertEqual('8efbbd67-2847-46b5-b7e7-4ab3b5e175de', payload.unique_identifier) self.assertEqual(None, payload.template_attribute)
def test_invalid_template_attribute(self): """ Test that a TypeError is raised when an invalid value is used to set the template attribute of a Rekey response payload. """ kwargs = {'template_attribute': 'invalid'} self.assertRaisesRegex( TypeError, "Template attribute must be a TemplateAttribute struct.", payloads.RekeyResponsePayload, **kwargs) args = (payloads.RekeyResponsePayload(), 'template_attribute', 'invalid') self.assertRaisesRegex( TypeError, "Template attribute must be a TemplateAttribute struct.", setattr, *args)
def _create_rekey_payload(self): return payloads.RekeyResponsePayload()