Started a test case for the tor service

This commit is contained in:
Lee Miller 2024-11-09 01:11:14 +02:00
parent 28e021bec3
commit 05ce1aace5
Signed by: lee.miller
GPG Key ID: 4F97A5EA88F4AB63
4 changed files with 75 additions and 7 deletions

7
minode/tests/common.py Normal file
View File

@ -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

View File

@ -13,16 +13,13 @@ import psutil
from minode.i2p import util from minode.i2p import util
from minode.structure import NetAddrNoPrefix from minode.structure import NetAddrNoPrefix
from .common import tor_port_free
try: try:
socket.socket().bind(('127.0.0.1', 7656)) socket.socket().bind(('127.0.0.1', 7656))
i2p_port_free = True i2p_port_free = True
except (OSError, socket.error): except (OSError, socket.error):
i2p_port_free = False 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): class TestProcessProto(unittest.TestCase):

64
minode/tests/test_tor.py Normal file
View File

@ -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()

View File

@ -120,7 +120,7 @@ def start_tor_service():
logging.info('Started hidden service %s', shared.onion_hostname) logging.info('Started hidden service %s', shared.onion_hostname)
if onionkey: if onionkey:
return True return controller
try: try:
with open( with open(
@ -144,4 +144,4 @@ def start_tor_service():
logging.warning( logging.warning(
'Error while saving onion service public key.', exc_info=True) 'Error while saving onion service public key.', exc_info=True)
return True return controller