summaryrefslogtreecommitdiff
path: root/tests/glibext/storage.py
blob: b60377a05fb9a9609a85ae08f2ff010af37117af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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)