2022-05-17 18:11:31 +02:00
|
|
|
"""Tests for inventory"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import struct
|
|
|
|
import tempfile
|
|
|
|
import time
|
|
|
|
import unittest
|
|
|
|
|
2021-07-29 21:16:37 +02:00
|
|
|
from pybitmessage import highlevelcrypto
|
2022-09-21 00:02:36 +02:00
|
|
|
from pybitmessage.storage import storage
|
2022-05-17 18:11:31 +02:00
|
|
|
|
|
|
|
from .partial import TestPartialRun
|
|
|
|
|
|
|
|
|
|
|
|
class TestFilesystemInventory(TestPartialRun):
|
|
|
|
"""A test case for the inventory using filesystem backend"""
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
cls.home = os.environ['BITMESSAGE_HOME'] = tempfile.mkdtemp()
|
|
|
|
super(TestFilesystemInventory, cls).setUpClass()
|
|
|
|
|
|
|
|
from inventory import create_inventory_instance
|
|
|
|
cls.inventory = create_inventory_instance('filesystem')
|
|
|
|
|
|
|
|
def test_consistency(self):
|
|
|
|
"""Ensure the inventory is of proper class"""
|
|
|
|
if os.path.isfile(os.path.join(self.home, 'messages.dat')):
|
|
|
|
# this will likely never happen
|
|
|
|
self.fail("Failed to configure filesystem inventory!")
|
|
|
|
|
|
|
|
def test_appending(self):
|
|
|
|
"""Add a sample message to the inventory"""
|
|
|
|
TTL = 24 * 60 * 60
|
|
|
|
embedded_time = int(time.time() + TTL)
|
|
|
|
msg = struct.pack('>Q', embedded_time) + os.urandom(166)
|
2021-07-29 21:16:37 +02:00
|
|
|
invhash = highlevelcrypto.calculateInventoryHash(msg)
|
2022-05-17 18:11:31 +02:00
|
|
|
self.inventory[invhash] = (2, 1, msg, embedded_time, b'')
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
super(TestFilesystemInventory, cls).tearDownClass()
|
|
|
|
cls.inventory.flush()
|
|
|
|
shutil.rmtree(os.path.join(cls.home, cls.inventory.topDir))
|
2022-09-21 00:02:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestStorageAbstract(unittest.TestCase):
|
|
|
|
"""A test case for refactoring of the storage abstract classes"""
|
|
|
|
|
|
|
|
def test_inventory_storage(self):
|
|
|
|
"""Check inherited abstract methods"""
|
|
|
|
with self.assertRaisesRegexp(
|
|
|
|
TypeError, "^Can't instantiate abstract class.*"
|
|
|
|
"methods __contains__, __delitem__, __getitem__, __iter__,"
|
|
|
|
" __len__, __setitem__"
|
|
|
|
): # pylint: disable=abstract-class-instantiated
|
|
|
|
storage.InventoryStorage()
|