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