From 76ef42bf1009e542b90814dc0b629e0ee5b0356c Mon Sep 17 00:00:00 2001 From: Arthur de Jong Date: Thu, 29 May 2014 14:49:05 +0200 Subject: Add test for missing key encryption algorithm This also introduces a toplevel PSKCError exception that all exceptions have as parent. --- pskc/encryption.py | 2 ++ pskc/exceptions.py | 19 +++++++++++-------- tests/invalid-encryption.pskcxml | 18 +++++++++++++++++- tests/test_invalid.doctest | 7 +++++++ 4 files changed, 37 insertions(+), 9 deletions(-) diff --git a/pskc/encryption.py b/pskc/encryption.py index 4e6a661..cd5720a 100644 --- a/pskc/encryption.py +++ b/pskc/encryption.py @@ -72,6 +72,8 @@ class EncryptedValue(object): key = self.encryption.key if key is None: raise DecryptionError('No key available') + if self.algorithm is None: + raise DecryptionError('No algorithm specified') if self.algorithm.endswith('#aes128-cbc') or \ self.algorithm.endswith('#aes192-cbc') or \ self.algorithm.endswith('#aes256-cbc'): diff --git a/pskc/exceptions.py b/pskc/exceptions.py index 9203dfe..801de20 100644 --- a/pskc/exceptions.py +++ b/pskc/exceptions.py @@ -21,21 +21,24 @@ """Collection of exceptions.""" -class ParseError(Exception): +class PSKCError(Exception): + """General top-level exception.""" + + def __str__(self): + return getattr(self, 'message', '') + + +class ParseError(PSKCError): """Something went wrong with parsing the PSKC file. Either the file is invalid XML or required elements or attributes are missing.""" - - def __str__(self): - return getattr(self, 'message', '') + pass -class DecryptionError(Exception): +class DecryptionError(PSKCError): """There was a problem decrypting the value. The encrypted value as available but something went wrong with decrypting it.""" - - def __str__(self): - return getattr(self, 'message', '') + pass diff --git a/tests/invalid-encryption.pskcxml b/tests/invalid-encryption.pskcxml index 18ee5f1..d900dc9 100644 --- a/tests/invalid-encryption.pskcxml +++ b/tests/invalid-encryption.pskcxml @@ -2,7 +2,7 @@ +AAECAwQFBgcICQoLDA0OD+cIHItlB3Wra1DUpxVvOx2lef1VmNPCMl8jwZqIUqGv + + + + + + + + + + + + + + + AAECAwQFBgcICQoLDA0OD+cIHItlB3Wra1DUpxVvOx2lef1VmNPCMl8jwZqIUqGv diff --git a/tests/test_invalid.doctest b/tests/test_invalid.doctest index 2665bae..7c291e1 100644 --- a/tests/test_invalid.doctest +++ b/tests/test_invalid.doctest @@ -55,3 +55,10 @@ DecryptionError: No key available Traceback (most recent call last): ... DecryptionError: Unsupported algorithm: ... +>>> key = pskc.keys[1] +>>> key.id +'45678901' +>>> key.secret +Traceback (most recent call last): + ... +DecryptionError: No algorithm specified -- cgit v1.2.3