summaryrefslogtreecommitdiff
path: root/tests/glibext
diff options
context:
space:
mode:
Diffstat (limited to 'tests/glibext')
-rw-r--r--tests/glibext/re.chrysalide.tests.secstorage.gschema.xml15
-rw-r--r--tests/glibext/secstorage.py153
-rw-r--r--tests/glibext/storage.py74
3 files changed, 242 insertions, 0 deletions
diff --git a/tests/glibext/re.chrysalide.tests.secstorage.gschema.xml b/tests/glibext/re.chrysalide.tests.secstorage.gschema.xml
new file mode 100644
index 0000000..6afa96b
--- /dev/null
+++ b/tests/glibext/re.chrysalide.tests.secstorage.gschema.xml
@@ -0,0 +1,15 @@
+<schemalist>
+
+ <schema id="re.chrysalide.tests.secstorage" path="/re/chrysalide/tests/secstorage/">
+
+ <key name="salt" type="ay">
+ <default>[]</default>
+ </key>
+
+ <key name="master" type="ay">
+ <default>[]</default>
+ </key>
+
+ </schema>
+
+</schemalist>
diff --git a/tests/glibext/secstorage.py b/tests/glibext/secstorage.py
new file mode 100644
index 0000000..5b8f680
--- /dev/null
+++ b/tests/glibext/secstorage.py
@@ -0,0 +1,153 @@
+
+import gi
+import os
+import subprocess
+
+from chrysacase import ChrysalideTestCase
+from gi.repository import Gio, GLib
+from pychrysalide.glibext import SecretStorage
+
+
+class TestSecretStorage(ChrysalideTestCase):
+ """TestCase for secret storage features."""
+
+ @classmethod
+ def setUpClass(cls):
+
+ super(TestSecretStorage, cls).setUpClass()
+
+ cls.log('Creating GSettings schema...')
+
+ path = os.path.dirname(os.path.realpath(__file__))
+
+ subprocess.run([ 'glib-compile-schemas', path ])
+
+
+ @classmethod
+ def tearDownClass(cls):
+
+ super(TestSecretStorage, cls).tearDownClass()
+
+ cls.log('Removing compiled GSettings schema...')
+
+ path = os.path.dirname(os.path.realpath(__file__))
+
+ filename = os.path.join(path, 'gschemas.compiled')
+
+ if os.path.exists(filename):
+ os.remove(filename)
+
+
+ def _get_settings(self, sid):
+ """Provide local GSettings instance."""
+
+ path = os.path.dirname(os.path.realpath(__file__))
+
+ source = Gio.SettingsSchemaSource.new_from_directory(path, None, True)
+
+ schema = Gio.SettingsSchemaSource.lookup(source, sid, False)
+
+ settings = Gio.Settings.new_full(schema, None, None)
+
+ return settings
+
+
+ def testMasterKeyDefinition(self):
+ """Check for cryptographic parameters for secret storage."""
+
+ settings = self._get_settings('re.chrysalide.tests.secstorage')
+
+ storage = SecretStorage(settings)
+
+ settings.reset('master')
+
+ self.assertEqual(len(settings.get_value('master').unpack()), 0)
+
+ self.assertFalse(storage.has_key)
+
+ settings.set_value('master', GLib.Variant('ay', b'ABC'))
+
+ self.assertFalse(storage.has_key)
+
+ settings.set_value('master', GLib.Variant('ay', b'A' * 23))
+
+ self.assertTrue(storage.has_key)
+
+
+ def testMasterKeyCreation(self):
+ """Create and update cryptographic parameters for secret storage."""
+
+ settings = self._get_settings('re.chrysalide.tests.secstorage')
+
+ storage = SecretStorage(settings)
+
+ settings.reset('salt')
+ settings.reset('master')
+
+ self.assertFalse(storage.has_key)
+
+ status = storage.set_password('')
+
+ self.assertTrue(status);
+
+ self.assertTrue(storage.has_key)
+ self.assertTrue(storage.is_locked)
+
+ status = storage.unlock('')
+
+ self.assertTrue(status)
+
+ self.assertFalse(storage.is_locked)
+
+ storage.lock()
+
+ self.assertTrue(storage.is_locked)
+
+ status = storage.unlock('XXX')
+
+ self.assertFalse(status)
+
+ self.assertTrue(storage.is_locked)
+
+
+ def testDataEncryption(self):
+ """Encrypt and decrypt data with the secret storage."""
+
+ settings = self._get_settings('re.chrysalide.tests.secstorage')
+
+ storage = SecretStorage(settings)
+
+ settings.reset('salt')
+ settings.reset('master')
+
+ status = storage.set_password('<s3cUre>')
+
+ self.assertTrue(status);
+
+ status = storage.unlock('<s3cUre>')
+
+ self.assertTrue(status)
+
+
+ original = b'ABC'
+
+ encrypted = storage.encrypt_data(original)
+
+ self.assertIsNotNone(encrypted)
+
+ plain = storage.decrypt_data(encrypted)
+
+ self.assertIsNotNone(plain)
+ self.assertEqual(original, plain)
+
+
+ original = b'A' * 136
+
+ encrypted = storage.encrypt_data(original)
+
+ self.assertIsNotNone(encrypted)
+
+ plain = storage.decrypt_data(encrypted)
+
+ self.assertIsNotNone(plain)
+ self.assertEqual(original, plain)
diff --git a/tests/glibext/storage.py b/tests/glibext/storage.py
new file mode 100644
index 0000000..b60377a
--- /dev/null
+++ b/tests/glibext/storage.py
@@ -0,0 +1,74 @@
+
+from chrysacase import ChrysalideTestCase
+from pychrysalide.glibext import ObjectStorage, SerializableObject
+import gi
+import os
+import tempfile
+
+
+class TestObjectStorage(ChrysalideTestCase):
+ """TestCase for analysis.storage."""
+
+ @classmethod
+ def setUpClass(cls):
+
+ super(TestObjectStorage, cls).setUpClass()
+
+ _, cls._tmp_filename = tempfile.mkstemp()
+
+ cls.log('Using temporary filename "%s"' % cls._tmp_filename)
+
+
+ @classmethod
+ def tearDownClass(cls):
+
+ super(TestObjectStorage, cls).tearDownClass()
+
+ cls.log('Delete filename "%s"' % cls._tmp_filename)
+
+ os.unlink(cls._tmp_filename)
+
+
+ def testGenericStorage(self):
+ """Store and load basic objects."""
+
+
+ class SimpleObject(gi._gi.GObject, SerializableObject):
+
+ def __init__(self, b=None):
+ super().__init__()
+ self._b = b
+
+ def _load(self, storage, fd):
+ assert(self._b is None)
+ self._b = os.read(fd, 1)[0]
+ return True
+
+ def _store(self, storage, fd):
+ os.write(fd, bytes([ self._b ]))
+ return True
+
+ def __eq__(self, other):
+ return self._b == other._b
+
+
+ # Store
+
+ storage = ObjectStorage('TestStorage', 0, 'my-storage-hash')
+ self.assertIsNotNone(storage)
+
+ so = SimpleObject(0x23)
+
+ pos = storage.store_object('simple', so)
+ self.assertIsNotNone(pos)
+
+ status = storage.store(self._tmp_filename)
+ self.assertTrue(status)
+
+ # Reload
+
+ storage2 = ObjectStorage.load(self._tmp_filename)
+
+ so2 = storage2.load_object('simple', pos)
+
+ self.assertEqual(so, so2)