From 05ce1aace51a9cbb614e43eedeac15105d1a4173 Mon Sep 17 00:00:00 2001 From: Lee Miller Date: Sat, 9 Nov 2024 01:11:14 +0200 Subject: [PATCH] Started a test case for the tor service --- minode/tests/common.py | 7 ++++ minode/tests/test_process.py | 7 ++-- minode/tests/test_tor.py | 64 ++++++++++++++++++++++++++++++++++++ minode/tor.py | 4 +-- 4 files changed, 75 insertions(+), 7 deletions(-) create mode 100644 minode/tests/common.py create mode 100644 minode/tests/test_tor.py diff --git a/minode/tests/common.py b/minode/tests/common.py new file mode 100644 index 0000000..4c202b1 --- /dev/null +++ b/minode/tests/common.py @@ -0,0 +1,7 @@ +import socket + +try: + socket.socket().bind(('127.0.0.1', 9050)) + tor_port_free = True +except (OSError, socket.error): + tor_port_free = False diff --git a/minode/tests/test_process.py b/minode/tests/test_process.py index e76504f..0f22c36 100644 --- a/minode/tests/test_process.py +++ b/minode/tests/test_process.py @@ -13,16 +13,13 @@ import psutil from minode.i2p import util from minode.structure import NetAddrNoPrefix +from .common import tor_port_free + try: socket.socket().bind(('127.0.0.1', 7656)) i2p_port_free = True except (OSError, socket.error): i2p_port_free = False -try: - socket.socket().bind(('127.0.0.1', 9050)) - tor_port_free = True -except (OSError, socket.error): - tor_port_free = False class TestProcessProto(unittest.TestCase): diff --git a/minode/tests/test_tor.py b/minode/tests/test_tor.py new file mode 100644 index 0000000..21549cc --- /dev/null +++ b/minode/tests/test_tor.py @@ -0,0 +1,64 @@ +"""Tests for tor module""" +import collections +import os +import tempfile +import unittest + +from minode import shared + +from .common import tor_port_free + +try: + from minode import tor +except ImportError: + tor = None + + +Proxy = collections.namedtuple('Proxy', ['hostname', 'port']) + + +@unittest.skipIf( + tor_port_free or tor is None, 'Inapropriate environment for tor service') +class TestTor(unittest.TestCase): + """A test case running the tor service""" + tor = None + _files = ['onion_dest_priv.key', 'onion_dest.pub'] + + @classmethod + def cleanup(cls): + """Remove used files""" + for f in cls._files: + try: + os.remove(os.path.join(shared.data_directory, f)) + except FileNotFoundError: + pass + + @classmethod + def setUpClass(cls): + shared.data_directory = tempfile.gettempdir() + shared.socks_proxy = Proxy('127.0.0.1', 9050) + cls.cleanup() + + @classmethod + def tearDownClass(cls): + if cls.tor: + cls.tor.close() + cls.cleanup() + + def test_tor(self): + """Start the tor service as in main and check the environment""" + self.tor = tor.start_tor_service() + if not self.tor: + self.fail('The tor service has hot started.') + + with open( + os.path.join(shared.data_directory, 'onion_dest.pub'), + 'r', encoding='ascii' + ) as key_file: + onion_key = key_file.read() + self.assertEqual(onion_key + '.onion', shared.onion_hostname) + + # with open( + # os.path.join(shared.data_directory, 'onion_dest_priv.key'), 'rb' + # ) as key_file: + # private_key = key_file.read() diff --git a/minode/tor.py b/minode/tor.py index e42d33c..2fe14b9 100644 --- a/minode/tor.py +++ b/minode/tor.py @@ -120,7 +120,7 @@ def start_tor_service(): logging.info('Started hidden service %s', shared.onion_hostname) if onionkey: - return True + return controller try: with open( @@ -144,4 +144,4 @@ def start_tor_service(): logging.warning( 'Error while saving onion service public key.', exc_info=True) - return True + return controller