From 0d3a05141b81762c90d691fbc78505f50ab73074 Mon Sep 17 00:00:00 2001 From: "Antoine R. Dumont (@ardumont)" <antoine.romain.dumont@gmail.com> Date: Wed, 20 Jun 2018 11:05:22 +0200 Subject: [PATCH] identifiers: Raise when invalid contextual data in persistent id Related T1104 --- swh/model/identifiers.py | 3 ++- swh/model/tests/test_identifiers.py | 11 ++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/swh/model/identifiers.py b/swh/model/identifiers.py index 138ca33a..934ace85 100644 --- a/swh/model/identifiers.py +++ b/swh/model/identifiers.py @@ -702,6 +702,7 @@ def parse_persistent_identifier(persistent_id): key, val = part.split('=') persistent_id_metadata[key] = val except Exception: - pass + msg = 'Contextual data is badly formatted, form key=val expected' + raise SWHMalformedIdentifierException(msg) pid_data.append(persistent_id_metadata) return dict(zip(PERSISTENT_IDENTIFIER_KEYS, pid_data)) diff --git a/swh/model/tests/test_identifiers.py b/swh/model/tests/test_identifiers.py index 8282aebb..0cab5003 100644 --- a/swh/model/tests/test_identifiers.py +++ b/swh/model/tests/test_identifiers.py @@ -834,9 +834,7 @@ class SnapshotIdentifier(unittest.TestCase): 'dir', '1', '0b6959356d30f1a4e9b7f6bca59b9a336464c03d', { 'origin': 'deb://Debian/packages/linuxdoc-tools' - }), - ('swh:1:dir:0b6959356d30f1a4e9b7f6bca59b9a336464c03d;invalid;malformed', # noqa - 'dir', '1', '0b6959356d30f1a4e9b7f6bca59b9a336464c03d', {}) + }) ]: expected_result = { 'namespace': 'swh', @@ -858,6 +856,8 @@ class SnapshotIdentifier(unittest.TestCase): 'Wrong format: There should be 4 mandatory parameters'), ('swh:', 'Wrong format: There should be 4 mandatory parameters'), + ('swh:1:cnt:', + 'Wrong format: Identifier should be present'), ('foo:1:cnt:abc8bc9d7a6bcf6db04f476d29314f157507d505', 'Wrong format: Supported namespace is \'swh\''), ('swh:2:dir:def8bc9d7a6bcf6db04f476d29314f157507d505', @@ -865,8 +865,9 @@ class SnapshotIdentifier(unittest.TestCase): ('swh:1:foo:fed8bc9d7a6bcf6db04f476d29314f157507d505', 'Wrong format: Supported types are %s' % ( ', '.join(PERSISTENT_IDENTIFIER_TYPES))), - ('swh:1:cnt:', - 'Wrong format: Identifier should be present'), + ('swh:1:dir:0b6959356d30f1a4e9b7f6bca59b9a336464c03d;invalid;' + 'malformed', + 'Contextual data is badly formatted, form key=val expected') ]: with self.assertRaisesRegex( SWHMalformedIdentifierException, _error): -- GitLab