From 5e7ee4ad6970a7a714d0a76c8c28996903d03f82 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Sun, 12 Apr 2020 19:53:57 -0500 Subject: [PATCH] work on tests and pack improvements --- kasten/pack/__init__.py | 12 ++++++++---- setup.py | 3 ++- tests/test_pack.py | 9 +++++++++ 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/kasten/pack/__init__.py b/kasten/pack/__init__.py index 55dc688..534ee28 100644 --- a/kasten/pack/__init__.py +++ b/kasten/pack/__init__.py @@ -24,10 +24,14 @@ def pack(data: bytes, data_type: 'KastenDataType', if not data_type or len(data_type) > 4: raise exceptions.InvalidKastenTypeLength - # Ensure encryption mode is valid (asymmetric, symmetric, plaintext) - enc_mode = int(enc_mode) - if enc_mode not in range(3): - raise + # Ensure encryption mode is in [0, 100) + try: + enc_mode = int(enc_mode) + except (TypeError, ValueError): + raise exceptions.InvalidEncryptionMode + if not enc_mode >= 0 or enc_mode >= 100: + raise exceptions.InvalidEncryptionMode + try: data = data.encode('utf8') except AttributeError: diff --git a/setup.py b/setup.py index 295e328..594accd 100644 --- a/setup.py +++ b/setup.py @@ -12,7 +12,8 @@ setup(name='kasten', author_email='beardog@mailbox.org', url='https://chaoswebs.net', packages=find_packages(exclude=['contrib', 'docs', 'tests']), - install_requires=['mimcvdf', 'msgpack'], + python_requires='>3.6.0', + install_requires=['msgpack', 'mimcvdf'], long_description=long_description, long_description_content_type='text/markdown', classifiers=[ diff --git a/tests/test_pack.py b/tests/test_pack.py index 6db559c..c508458 100644 --- a/tests/test_pack.py +++ b/tests/test_pack.py @@ -27,6 +27,15 @@ class TestPack(unittest.TestCase): data = os.urandom(10) self.assertRaises(exceptions.InvalidKastenTypeLength, pack.pack, data, 'aaaaa', 1) + def test_invalid_enc_mode(self): + data = os.urandom(10) + self.assertRaises(exceptions.InvalidEncryptionMode, pack.pack, data, 'txt', None) + self.assertRaises(exceptions.InvalidEncryptionMode, pack.pack, data, 'txt', "100") + self.assertRaises(exceptions.InvalidEncryptionMode, pack.pack, data, 'txt', 100) + self.assertRaises(exceptions.InvalidEncryptionMode, pack.pack, data, 'txt', -1) + self.assertRaises(exceptions.InvalidEncryptionMode, pack.pack, data, 'txt', -5) + self.assertRaises(exceptions.InvalidEncryptionMode, pack.pack, data, 'txt', "test") + unittest.main() \ No newline at end of file