From 866346b05948fed9d32614f3814e6c62d15631f9 Mon Sep 17 00:00:00 2001 From: shortcutme Date: Wed, 17 Jul 2019 16:29:54 +0200 Subject: [PATCH] Fix and test bootstrapper hash cache reload from db --- .../disabled-Bootstrapper/BootstrapperDb.py | 2 +- .../Test/TestBootstrapper.py | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/plugins/disabled-Bootstrapper/BootstrapperDb.py b/plugins/disabled-Bootstrapper/BootstrapperDb.py index fcc428f7..3c47b76d 100644 --- a/plugins/disabled-Bootstrapper/BootstrapperDb.py +++ b/plugins/disabled-Bootstrapper/BootstrapperDb.py @@ -26,7 +26,7 @@ class BootstrapperDb(Db.Db): def updateHashCache(self): res = self.execute("SELECT * FROM hash") - self.hash_ids = {str(row["hash"]): row["hash_id"] for row in res} + self.hash_ids = {row["hash"]: row["hash_id"] for row in res} self.log.debug("Loaded %s hash_ids" % len(self.hash_ids)) def checkTables(self): diff --git a/plugins/disabled-Bootstrapper/Test/TestBootstrapper.py b/plugins/disabled-Bootstrapper/Test/TestBootstrapper.py index 983cb44c..69bdc54c 100644 --- a/plugins/disabled-Bootstrapper/Test/TestBootstrapper.py +++ b/plugins/disabled-Bootstrapper/Test/TestBootstrapper.py @@ -27,6 +27,28 @@ def bootstrapper_db(request): @pytest.mark.usefixtures("resetSettings") class TestBootstrapper: + def testHashCache(self, file_server, bootstrapper_db): + ip_type = helper.getIpType(file_server.ip) + peer = Peer(file_server.ip, 1544, connection_server=file_server) + hash1 = hashlib.sha256(b"site1").digest() + hash2 = hashlib.sha256(b"site2").digest() + hash3 = hashlib.sha256(b"site3").digest() + + # Verify empty result + res = peer.request("announce", { + "hashes": [hash1, hash2], + "port": 15441, "need_types": [ip_type], "need_num": 10, "add": [ip_type] + }) + + assert len(res["peers"][0][ip_type]) == 0 # Empty result + + hash_ids_before = bootstrapper_db.hash_ids.copy() + + bootstrapper_db.updateHashCache() + + assert hash_ids_before == bootstrapper_db.hash_ids + + def testBootstrapperDb(self, file_server, bootstrapper_db): ip_type = helper.getIpType(file_server.ip) peer = Peer(file_server.ip, 1544, connection_server=file_server)