summaryrefslogtreecommitdiff
path: root/tests/glibext
diff options
context:
space:
mode:
authorCyrille Bagard <nocbos@gmail.com>2025-03-07 09:29:32 (GMT)
committerCyrille Bagard <nocbos@gmail.com>2025-03-10 06:27:26 (GMT)
commita59fb1b3fb67a348c40bc3668445d64213e9e674 (patch)
treefcf493a6b1758c666b95a9c1c645beecc8e42a14 /tests/glibext
parente0614e482b3ca3faf43e296bb70348be0d651394 (diff)
Prepare a new way to store objects.
Diffstat (limited to 'tests/glibext')
-rw-r--r--tests/glibext/storage.py81
1 files changed, 81 insertions, 0 deletions
diff --git a/tests/glibext/storage.py b/tests/glibext/storage.py
new file mode 100644
index 0000000..612d500
--- /dev/null
+++ b/tests/glibext/storage.py
@@ -0,0 +1,81 @@
+
+from chrysacase import ChrysalideTestCase
+from pychrysalide import core
+from pychrysalide.analysis.contents import FileContent
+from pychrysalide.analysis.storage import ObjectStorage
+from pychrysalide.common import PackedBuffer
+import os
+import shutil
+import tempfile
+
+
+class TestObjectStorage(ChrysalideTestCase):
+ """TestCase for analysis.storage."""
+
+ @classmethod
+ def setUpClass(cls):
+
+ super(TestObjectStorage, cls).setUpClass()
+
+ cls._tmp_path = tempfile.mkdtemp()
+
+ config = core.get_main_configuration()
+ param = config.search(core.MainParameterKeys.TMPDIR)
+
+ cls._old_tmpdir = param.value
+ param.value = cls._tmp_path
+
+ cls.log('Using temporary directory "%s"' % cls._tmp_path)
+
+
+ @classmethod
+ def tearDownClass(cls):
+
+ super(TestObjectStorage, cls).tearDownClass()
+
+ config = core.get_main_configuration()
+ param = config.search(core.MainParameterKeys.TMPDIR)
+
+ param.value = cls._old_tmpdir
+
+ # import os
+ # os.system('ls -laihR %s' % cls._tmp_path)
+
+ cls.log('Delete directory "%s"' % cls._tmp_path)
+
+ shutil.rmtree(cls._tmp_path)
+
+
+ def testFileContentStorage(self):
+ """Store and load file binary content."""
+
+ storage = ObjectStorage('my-storage-hash')
+ self.assertIsNotNone(storage)
+
+ filename = os.path.join(self._tmp_path, 'test.bin')
+
+ with open(filename, 'wb') as fd:
+ fd.write(b'ABC')
+
+ cnt = FileContent(filename)
+ self.assertIsNotNone(cnt)
+
+ ret = storage.store_object('contents', cnt)
+ self.assertEqual(ret, 0)
+
+ pbuf = PackedBuffer()
+
+ ret = storage.store(pbuf)
+ self.assertTrue(ret)
+
+ self.assertTrue(pbuf.payload_length > 0)
+
+ pbuf.rewind()
+
+ storage2 = ObjectStorage.load(pbuf)
+ self.assertIsNotNone(storage2)
+
+ cnt2 = storage2.load_object('contents', 0)
+ self.assertIsNotNone(cnt2)
+
+ self.assertEqual(cnt.data, cnt2.data)