Пример #1
0
    def test_comparison(self):
        """
        Test that the equality/inequality operators return True/False when
        comparing two SetAttribute request payloads with the same data.
        """
        a = payloads.SetAttributeRequestPayload()
        b = payloads.SetAttributeRequestPayload()

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

        a = payloads.SetAttributeRequestPayload(
            unique_identifier="b4faee10-aa2a-4446-8ad4-0881f3422959",
            new_attribute=objects.NewAttribute(
                attribute=primitives.Enumeration(
                    enums.CryptographicAlgorithm, enums.CryptographicAlgorithm.
                    AES, enums.Tags.CRYPTOGRAPHIC_ALGORITHM)))
        b = payloads.SetAttributeRequestPayload(
            unique_identifier="b4faee10-aa2a-4446-8ad4-0881f3422959",
            new_attribute=objects.NewAttribute(
                attribute=primitives.Enumeration(
                    enums.CryptographicAlgorithm, enums.CryptographicAlgorithm.
                    AES, enums.Tags.CRYPTOGRAPHIC_ALGORITHM)))

        self.assertTrue(a == b)
        self.assertTrue(b == a)
        self.assertFalse(a != b)
        self.assertFalse(b != a)
Пример #2
0
    def test_comparison(self):
        """
        Test that the equality/inequality operators return True/False when
        comparing two NewAttribute objects with the same data.
        """
        a = objects.NewAttribute()
        b = objects.NewAttribute()

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

        a = objects.NewAttribute(
            attribute=primitives.Enumeration(
                enums.CryptographicAlgorithm,
                enums.CryptographicAlgorithm.AES,
                enums.Tags.CRYPTOGRAPHIC_ALGORITHM
            )
        )
        b = objects.NewAttribute(
            attribute=primitives.Enumeration(
                enums.CryptographicAlgorithm,
                enums.CryptographicAlgorithm.AES,
                enums.Tags.CRYPTOGRAPHIC_ALGORITHM
            )
        )

        self.assertTrue(a == b)
        self.assertTrue(b == a)
        self.assertFalse(a != b)
        self.assertFalse(b != a)
Пример #3
0
    def test_read_kmip_2_0(self):
        """
        Test that a ModifyAttribute request payload can be read from a buffer
        with KMIP 2.0 fields.
        """
        payload = payloads.ModifyAttributeRequestPayload()

        self.assertIsNone(payload.unique_identifier)
        self.assertIsNone(payload.attribute)
        self.assertIsNone(payload.current_attribute)
        self.assertIsNone(payload.new_attribute)

        payload.read(self.full_encoding_kmip_2_0,
                     kmip_version=enums.KMIPVersion.KMIP_2_0)

        self.assertEqual("b4faee10-aa2a-4446-8ad4-0881f3422959",
                         payload.unique_identifier)
        self.assertIsNone(payload.attribute)
        self.assertEqual(
            objects.CurrentAttribute(attribute=primitives.Enumeration(
                enums.CryptographicAlgorithm, enums.CryptographicAlgorithm.AES,
                enums.Tags.CRYPTOGRAPHIC_ALGORITHM)),
            payload.current_attribute)
        self.assertEqual(
            objects.NewAttribute(attribute=primitives.Enumeration(
                enums.CryptographicAlgorithm, enums.CryptographicAlgorithm.RSA,
                enums.Tags.CRYPTOGRAPHIC_ALGORITHM)), payload.new_attribute)
Пример #4
0
    def test_invalid_attribute(self):
        """
        Test that a TypeError is raised when an invalid value is used to
        create a NewAttribute object. Note that the value is not a valid
        PyKMIP object derived from Base and therefore cannot be an attribute.
        """
        kwargs = {"attribute": "invalid"}
        self.assertRaisesRegex(
            TypeError,
            "The attribute must be a Base object, not a {}.".format(
                type("invalid")
            ),
            objects.NewAttribute,
            **kwargs
        )

        args = (
            objects.NewAttribute(),
            "attribute",
            "invalid"
        )
        self.assertRaisesRegex(
            TypeError,
            "The attribute must be a Base object, not a {}.".format(
                type("invalid")
            ),
            setattr,
            *args
        )
Пример #5
0
    def test_comparison(self):
        """
        Test that the equality/inequality operators return True/False when
        comparing two ModifyAttribute request payloads with the same data.
        """
        a = payloads.ModifyAttributeRequestPayload()
        b = payloads.ModifyAttributeRequestPayload()

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

        a = payloads.ModifyAttributeRequestPayload(
            unique_identifier="b4faee10-aa2a-4446-8ad4-0881f3422959",
            attribute=objects.Attribute(
                attribute_name=objects.Attribute.AttributeName("x-attribute1"),
                attribute_value=primitives.TextString(
                    value="ModifiedValue1", tag=enums.Tags.ATTRIBUTE_VALUE)),
            current_attribute=objects.CurrentAttribute(
                attribute=primitives.Enumeration(
                    enums.CryptographicAlgorithm, enums.CryptographicAlgorithm.
                    AES, enums.Tags.CRYPTOGRAPHIC_ALGORITHM)),
            new_attribute=objects.NewAttribute(
                attribute=primitives.Enumeration(
                    enums.CryptographicAlgorithm, enums.CryptographicAlgorithm.
                    RSA, enums.Tags.CRYPTOGRAPHIC_ALGORITHM)))
        b = payloads.ModifyAttributeRequestPayload(
            unique_identifier="b4faee10-aa2a-4446-8ad4-0881f3422959",
            attribute=objects.Attribute(
                attribute_name=objects.Attribute.AttributeName("x-attribute1"),
                attribute_value=primitives.TextString(
                    value="ModifiedValue1", tag=enums.Tags.ATTRIBUTE_VALUE)),
            current_attribute=objects.CurrentAttribute(
                attribute=primitives.Enumeration(
                    enums.CryptographicAlgorithm, enums.CryptographicAlgorithm.
                    AES, enums.Tags.CRYPTOGRAPHIC_ALGORITHM)),
            new_attribute=objects.NewAttribute(
                attribute=primitives.Enumeration(
                    enums.CryptographicAlgorithm, enums.CryptographicAlgorithm.
                    RSA, enums.Tags.CRYPTOGRAPHIC_ALGORITHM)))

        self.assertTrue(a == b)
        self.assertTrue(b == a)
        self.assertFalse(a != b)
        self.assertFalse(b != a)
Пример #6
0
    def test_comparison_on_different_new_attributes(self):
        """
        Test that the equality/inequality operators return False/True when
        comparing two ModifyAttribute request payloads with different new
        attributes.
        """
        a = payloads.ModifyAttributeRequestPayload(
            new_attribute=objects.NewAttribute(
                attribute=primitives.Enumeration(
                    enums.CryptographicAlgorithm, enums.CryptographicAlgorithm.
                    AES, enums.Tags.CRYPTOGRAPHIC_ALGORITHM)))
        b = payloads.ModifyAttributeRequestPayload(
            new_attribute=objects.NewAttribute(attribute=primitives.Integer(
                128, enums.Tags.CRYPTOGRAPHIC_LENGTH)))

        self.assertFalse(a == b)
        self.assertFalse(b == a)
        self.assertTrue(a != b)
        self.assertTrue(b != a)
Пример #7
0
    def read(self, input_buffer, kmip_version=enums.KMIPVersion.KMIP_1_0):
        """
        Read the data encoding the ModifyAttribute request payload and decode
        it into its constituent part.

        Args:
            input_buffer (stream): A data stream containing encoded object
                data, supporting a read method; usually a BytearrayStream
                object.
            kmip_version (KMIPVersion): An enumeration defining the KMIP
                version with which the object will be decoded. Optional,
                defaults to KMIP 1.0.

        Raises:
            InvalidKmipEncoding: Raised if fields are missing from the
                encoding.
        """
        super(ModifyAttributeRequestPayload,
              self).read(input_buffer, kmip_version=kmip_version)
        local_buffer = utils.BytearrayStream(input_buffer.read(self.length))

        if self.is_tag_next(enums.Tags.UNIQUE_IDENTIFIER, local_buffer):
            self._unique_identifier = primitives.TextString(
                tag=enums.Tags.UNIQUE_IDENTIFIER)
            self._unique_identifier.read(local_buffer,
                                         kmip_version=kmip_version)
        else:
            self._unique_identifier = None

        if kmip_version < enums.KMIPVersion.KMIP_2_0:
            if self.is_tag_next(enums.Tags.ATTRIBUTE, local_buffer):
                self._attribute = objects.Attribute()
                self._attribute.read(local_buffer, kmip_version=kmip_version)
            else:
                raise exceptions.InvalidKmipEncoding(
                    "The ModifyAttribute request payload encoding is missing "
                    "the attribute field.")
        else:
            if self.is_tag_next(enums.Tags.CURRENT_ATTRIBUTE, local_buffer):
                self._current_attribute = objects.CurrentAttribute()
                self._current_attribute.read(local_buffer,
                                             kmip_version=kmip_version)
            else:
                self._current_attribute = None

            if self.is_tag_next(enums.Tags.NEW_ATTRIBUTE, local_buffer):
                self._new_attribute = objects.NewAttribute()
                self._new_attribute.read(local_buffer,
                                         kmip_version=kmip_version)
            else:
                raise exceptions.InvalidKmipEncoding(
                    "The ModifyAttribute request payload encoding is missing "
                    "the new attribute field.")

        self.is_oversized(local_buffer)
Пример #8
0
    def test_comparison_on_type_mismatch(self):
        """
        Test that the equality/inequality operators return False/True when
        comparing two NewAttribute objects with different types.
        """
        a = objects.NewAttribute()
        b = "invalid"

        self.assertFalse(a == b)
        self.assertFalse(b == a)
        self.assertTrue(a != b)
        self.assertTrue(b != a)
Пример #9
0
 def test_write_no_attribute(self):
     """
     Test that an InvalidField error is raised when an empty
     NewAttribute object is written to a data stream.
     """
     new_attribute = objects.NewAttribute()
     args = (utils.BytearrayStream(), )
     self.assertRaisesRegex(
         exceptions.InvalidField,
         "The NewAttribute object is missing the attribute field.",
         new_attribute.write,
         *args
     )
Пример #10
0
 def test_read_invalid_attribute(self):
     """
     Test that an InvalidKmipEncoding error is raised when an invalid
     encoding containing an invalid attribute is used to decode a
     NewAttribute object.
     """
     new_attribute = objects.NewAttribute()
     args = (self.invalid_encoding, )
     self.assertRaisesRegex(
         exceptions.InvalidKmipEncoding,
         "The NewAttribute encoding is missing the attribute field.",
         new_attribute.read,
         *args
     )
Пример #11
0
    def test_write(self):
        """
        Test that a SetAttribute request payload can be written to a buffer.
        """
        payload = payloads.SetAttributeRequestPayload(
            unique_identifier="b4faee10-aa2a-4446-8ad4-0881f3422959",
            new_attribute=objects.NewAttribute(
                attribute=primitives.Enumeration(
                    enums.CryptographicAlgorithm, enums.CryptographicAlgorithm.
                    AES, enums.Tags.CRYPTOGRAPHIC_ALGORITHM)))

        buffer = utils.BytearrayStream()
        payload.write(buffer)

        self.assertEqual(len(self.full_encoding), len(buffer))
        self.assertEqual(str(self.full_encoding), str(buffer))
Пример #12
0
    def test_write_no_unique_identifier(self):
        """
        Test that a SetAttribute request payload can be written to a buffer
        without the unique identifier field.
        """
        payload = payloads.SetAttributeRequestPayload(
            new_attribute=objects.NewAttribute(
                attribute=primitives.Enumeration(
                    enums.CryptographicAlgorithm, enums.CryptographicAlgorithm.
                    AES, enums.Tags.CRYPTOGRAPHIC_ALGORITHM)))

        buffer = utils.BytearrayStream()
        payload.write(buffer)

        self.assertEqual(len(self.no_unique_identifier_encoding), len(buffer))
        self.assertEqual(str(self.no_unique_identifier_encoding), str(buffer))
Пример #13
0
 def test_read_unsupported_attribute(self):
     """
     Test that an AttributeNotSupported error is raised when an unsupported
     attribute is parsed while reading in a NewAttribute object from a
     data stream. This can occur when an older attribute is no longer
     supported by a newer version of KMIP, or vice versa.
     """
     new_attribute = objects.NewAttribute()
     args = (self.unsupported_encoding, )
     kwargs = {"kmip_version": enums.KMIPVersion.KMIP_2_0}
     self.assertRaisesRegex(
         exceptions.AttributeNotSupported,
         "Attribute OPERATION_POLICY_NAME is not supported by KMIP 2.0.",
         new_attribute.read,
         *args,
         **kwargs
     )
Пример #14
0
 def test_write_version_not_supported(self):
     """
     Test that a VersionNotSupported error is raised when an unsupported
     KMIP version is provided while writing a NewAttribute object to
     a data stream. The NewAttribute structure is only supported in
     KMIP 2.0+.
     """
     new_attribute = objects.NewAttribute()
     args = (utils.BytearrayStream(), )
     kwargs = {"kmip_version": enums.KMIPVersion.KMIP_1_4}
     self.assertRaisesRegex(
         exceptions.VersionNotSupported,
         "KMIP 1.4 does not support the NewAttribute object.",
         new_attribute.write,
         *args,
         **kwargs
     )
Пример #15
0
    def test_read_no_unique_identifier(self):
        """
        Test that a SetAttribute request payload can be read from a buffer
        even when the encoding is missing the unique identifier field.
        """
        payload = payloads.SetAttributeRequestPayload()

        self.assertIsNone(payload.unique_identifier)
        self.assertIsNone(payload.new_attribute)

        payload.read(self.no_unique_identifier_encoding)

        self.assertIsNone(payload.unique_identifier)
        self.assertEqual(
            objects.NewAttribute(attribute=primitives.Enumeration(
                enums.CryptographicAlgorithm, enums.CryptographicAlgorithm.AES,
                enums.Tags.CRYPTOGRAPHIC_ALGORITHM)), payload.new_attribute)
Пример #16
0
    def test_write(self):
        """
        Test that a NewAttribute object can be written to a data stream.
        """
        new_attribute = objects.NewAttribute(
            attribute=primitives.Enumeration(
                enums.CryptographicAlgorithm,
                enums.CryptographicAlgorithm.AES,
                enums.Tags.CRYPTOGRAPHIC_ALGORITHM
            )
        )

        stream = utils.BytearrayStream()
        new_attribute.write(stream)

        self.assertEqual(len(self.full_encoding), len(stream))
        self.assertEqual(str(self.full_encoding), str(stream))
Пример #17
0
 def test_read_version_not_supported(self):
     """
     Test that a VersionNotSupported error is raised when an unsupported
     KMIP version is provided while reading in a NewAttribute object
     from a data stream. The NewAttribute structure is only supported
     in KMIP 2.0+.
     """
     new_attribute = objects.NewAttribute()
     args = (self.full_encoding, )
     kwargs = {"kmip_version": enums.KMIPVersion.KMIP_1_2}
     self.assertRaisesRegex(
         exceptions.VersionNotSupported,
         "KMIP 1.2 does not support the NewAttribute object.",
         new_attribute.read,
         *args,
         **kwargs
     )
Пример #18
0
    def test_read(self):
        """
        Test that a SetAttribute request payload can be read from a buffer.
        """
        payload = payloads.SetAttributeRequestPayload()

        self.assertIsNone(payload.unique_identifier)
        self.assertIsNone(payload.new_attribute)

        payload.read(self.full_encoding)

        self.assertEqual("b4faee10-aa2a-4446-8ad4-0881f3422959",
                         payload.unique_identifier)
        self.assertEqual(
            objects.NewAttribute(attribute=primitives.Enumeration(
                enums.CryptographicAlgorithm, enums.CryptographicAlgorithm.AES,
                enums.Tags.CRYPTOGRAPHIC_ALGORITHM)), payload.new_attribute)
Пример #19
0
 def test_str(self):
     """
     Test that str can be applied to a NewAttribute object.
     """
     new_attribute = objects.NewAttribute(
         attribute=primitives.Enumeration(
             enums.CryptographicAlgorithm,
             enums.CryptographicAlgorithm.AES,
             enums.Tags.CRYPTOGRAPHIC_ALGORITHM
         )
     )
     self.assertEqual(
         '{"attribute": Enumeration('
         'enum=CryptographicAlgorithm, '
         'value=CryptographicAlgorithm.AES, '
         'tag=Tags.CRYPTOGRAPHIC_ALGORITHM)}',
         str(new_attribute)
     )
Пример #20
0
    def test_read(self):
        """
        Test that a NewAttribute structure can be correctly read in from
        a data stream.
        """
        new_attribute = objects.NewAttribute()

        self.assertIsNone(new_attribute.attribute)

        new_attribute.read(self.full_encoding)

        self.assertIsInstance(
            new_attribute.attribute,
            primitives.Enumeration
        )
        self.assertEqual(
            new_attribute.attribute.value,
            enums.CryptographicAlgorithm.AES
        )
Пример #21
0
 def test_repr(self):
     """
     Test that repr can be applied to a NewAttribute object.
     """
     new_attribute = objects.NewAttribute(
         attribute=primitives.Enumeration(
             enums.CryptographicAlgorithm,
             enums.CryptographicAlgorithm.AES,
             enums.Tags.CRYPTOGRAPHIC_ALGORITHM
         )
     )
     self.assertEqual(
         "NewAttribute("
         "attribute=Enumeration("
         "enum=CryptographicAlgorithm, "
         "value=CryptographicAlgorithm.AES, "
         "tag=Tags.CRYPTOGRAPHIC_ALGORITHM))",
         repr(new_attribute)
     )
Пример #22
0
    def test_write_kmip_2_0(self):
        """
        Test that a ModifyAttribute request payload can be written to a buffer
        with KMIP 2.0 fields.
        """
        payload = payloads.ModifyAttributeRequestPayload(
            unique_identifier="b4faee10-aa2a-4446-8ad4-0881f3422959",
            current_attribute=objects.CurrentAttribute(
                attribute=primitives.Enumeration(
                    enums.CryptographicAlgorithm, enums.CryptographicAlgorithm.
                    AES, enums.Tags.CRYPTOGRAPHIC_ALGORITHM)),
            new_attribute=objects.NewAttribute(
                attribute=primitives.Enumeration(
                    enums.CryptographicAlgorithm, enums.CryptographicAlgorithm.
                    RSA, enums.Tags.CRYPTOGRAPHIC_ALGORITHM)))

        buffer = utils.BytearrayStream()
        payload.write(buffer, kmip_version=enums.KMIPVersion.KMIP_2_0)

        self.assertEqual(len(self.full_encoding_kmip_2_0), len(buffer))
        self.assertEqual(str(self.full_encoding_kmip_2_0), str(buffer))
Пример #23
0
 def test_write_unsupported_attribute(self):
     """
     Test that an AttributeNotSupported error is raised when an unsupported
     attribute is found while writing a NewAttribute object to a data
     stream. This can occur when an older attribute is no longer supported
     by a newer version of KMIP, or vice versa.
     """
     new_attribute = objects.NewAttribute(
         attribute=primitives.TextString(
             "default",
             tag=enums.Tags.OPERATION_POLICY_NAME
         )
     )
     args = (utils.BytearrayStream(), )
     kwargs = {"kmip_version": enums.KMIPVersion.KMIP_2_0}
     self.assertRaisesRegex(
         exceptions.AttributeNotSupported,
         "Attribute OPERATION_POLICY_NAME is not supported by KMIP 2.0.",
         new_attribute.write,
         *args,
         **kwargs
     )
Пример #24
0
    def test_unrecognized_attribute(self):
        """
        Test that a TypeError is raised when an unrecognized attribute is used
        to create a NewAttribute object. Note that this unrecognized
        attribute is a valid PyKMIP object derived from Base, it just isn't an
        attribute.
        """
        kwargs = {
            "attribute": primitives.Enumeration(
                enums.WrappingMethod,
                enums.WrappingMethod.ENCRYPT,
                enums.Tags.WRAPPING_METHOD
            )
        }
        self.assertRaisesRegex(
            TypeError,
            "The attribute must be a supported attribute type.",
            objects.NewAttribute,
            **kwargs
        )

        args = (
            objects.NewAttribute(),
            "attribute",
            primitives.Enumeration(
                enums.WrappingMethod,
                enums.WrappingMethod.ENCRYPT,
                enums.Tags.WRAPPING_METHOD
            )
        )
        self.assertRaisesRegex(
            TypeError,
            "The attribute must be a supported attribute type.",
            setattr,
            *args
        )