From ce0de97df1a4a7ff6777b19063554e2e10ea271b Mon Sep 17 00:00:00 2001 From: clenk Date: Thu, 6 Apr 2017 13:08:48 -0400 Subject: [PATCH] Validate UUID portion of IDProperty --- stix2/properties.py | 5 ++++- stix2/test/test_properties.py | 7 ++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/stix2/properties.py b/stix2/properties.py index 3bb2633..471ffa9 100644 --- a/stix2/properties.py +++ b/stix2/properties.py @@ -102,9 +102,12 @@ class IDProperty(Property): super(IDProperty, self).__init__() def validate(self, value): - # TODO: validate GUID as well if not value.startswith(self.required_prefix): raise ValueError("must start with '{0}'.".format(self.required_prefix)) + try: + uuid.UUID(value.split('--', 1)[1], version=4) + except Exception: + raise ValueError("must have a valid version 4 UUID after the prefix.") return value def default(self): diff --git a/stix2/test/test_properties.py b/stix2/test/test_properties.py index f3c4ff9..6e1d44d 100644 --- a/stix2/test/test_properties.py +++ b/stix2/test/test_properties.py @@ -63,8 +63,13 @@ def test_id_property(): idprop = IDProperty('my-type') assert idprop.validate('my-type--90aaca8a-1110-5d32-956d-ac2f34a1bd8c') - with pytest.raises(ValueError): + with pytest.raises(ValueError) as excinfo: idprop.validate('not-my-type--90aaca8a-1110-5d32-956d-ac2f34a1bd8c') + assert str(excinfo.value) == "must start with 'my-type--'." + with pytest.raises(ValueError) as excinfo: + idprop.validate('my-type--foo') + assert str(excinfo.value) == "must have a valid version 4 UUID after the prefix." + assert idprop.validate(idprop.default())