diff options
Diffstat (limited to 'tests/glibext/storage.py')
-rw-r--r-- | tests/glibext/storage.py | 75 |
1 files changed, 34 insertions, 41 deletions
diff --git a/tests/glibext/storage.py b/tests/glibext/storage.py index 612d500..b60377a 100644 --- a/tests/glibext/storage.py +++ b/tests/glibext/storage.py @@ -1,11 +1,8 @@ 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 +from pychrysalide.glibext import ObjectStorage, SerializableObject +import gi import os -import shutil import tempfile @@ -17,15 +14,9 @@ class TestObjectStorage(ChrysalideTestCase): super(TestObjectStorage, cls).setUpClass() - cls._tmp_path = tempfile.mkdtemp() + _, cls._tmp_filename = tempfile.mkstemp() - 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) + cls.log('Using temporary filename "%s"' % cls._tmp_filename) @classmethod @@ -33,49 +24,51 @@ class TestObjectStorage(ChrysalideTestCase): super(TestObjectStorage, cls).tearDownClass() - config = core.get_main_configuration() - param = config.search(core.MainParameterKeys.TMPDIR) + cls.log('Delete filename "%s"' % cls._tmp_filename) - param.value = cls._old_tmpdir + os.unlink(cls._tmp_filename) - # import os - # os.system('ls -laihR %s' % cls._tmp_path) - cls.log('Delete directory "%s"' % cls._tmp_path) + def testGenericStorage(self): + """Store and load basic objects.""" - shutil.rmtree(cls._tmp_path) + class SimpleObject(gi._gi.GObject, SerializableObject): - def testFileContentStorage(self): - """Store and load file binary content.""" + def __init__(self, b=None): + super().__init__() + self._b = b - storage = ObjectStorage('my-storage-hash') - self.assertIsNotNone(storage) + def _load(self, storage, fd): + assert(self._b is None) + self._b = os.read(fd, 1)[0] + return True - filename = os.path.join(self._tmp_path, 'test.bin') + def _store(self, storage, fd): + os.write(fd, bytes([ self._b ])) + return True - with open(filename, 'wb') as fd: - fd.write(b'ABC') + def __eq__(self, other): + return self._b == other._b - cnt = FileContent(filename) - self.assertIsNotNone(cnt) - ret = storage.store_object('contents', cnt) - self.assertEqual(ret, 0) + # Store + + storage = ObjectStorage('TestStorage', 0, 'my-storage-hash') + self.assertIsNotNone(storage) - pbuf = PackedBuffer() + so = SimpleObject(0x23) - ret = storage.store(pbuf) - self.assertTrue(ret) + pos = storage.store_object('simple', so) + self.assertIsNotNone(pos) - self.assertTrue(pbuf.payload_length > 0) + status = storage.store(self._tmp_filename) + self.assertTrue(status) - pbuf.rewind() + # Reload - storage2 = ObjectStorage.load(pbuf) - self.assertIsNotNone(storage2) + storage2 = ObjectStorage.load(self._tmp_filename) - cnt2 = storage2.load_object('contents', 0) - self.assertIsNotNone(cnt2) + so2 = storage2.load_object('simple', pos) - self.assertEqual(cnt.data, cnt2.data) + self.assertEqual(so, so2) |