2023-08-01 23:15:00 +02:00
|
|
|
"""Blind tests, starting the minode process"""
|
2021-03-07 22:39:37 +01:00
|
|
|
import unittest
|
|
|
|
import signal
|
2021-03-11 18:37:23 +01:00
|
|
|
import socket
|
2021-03-07 22:39:37 +01:00
|
|
|
import subprocess
|
2021-08-02 15:45:25 +02:00
|
|
|
import sys
|
2021-03-07 22:39:37 +01:00
|
|
|
import tempfile
|
|
|
|
import time
|
|
|
|
|
|
|
|
import psutil
|
|
|
|
|
2021-03-11 18:37:23 +01:00
|
|
|
try:
|
|
|
|
socket.socket().bind(('127.0.0.1', 7656))
|
|
|
|
i2p_port_free = True
|
|
|
|
except (OSError, socket.error):
|
|
|
|
i2p_port_free = False
|
2023-04-13 20:00:31 +02:00
|
|
|
try:
|
|
|
|
socket.socket().bind(('127.0.0.1', 9050))
|
|
|
|
tor_port_free = True
|
|
|
|
except (OSError, socket.error):
|
|
|
|
tor_port_free = False
|
2021-03-11 18:37:23 +01:00
|
|
|
|
2021-03-07 22:39:37 +01:00
|
|
|
|
|
|
|
class TestProcessProto(unittest.TestCase):
|
|
|
|
"""Test process attributes, common flow"""
|
|
|
|
_process_cmd = ['minode']
|
2022-09-23 03:25:24 +02:00
|
|
|
_connection_limit = 4 if sys.platform.startswith('win') else 10
|
|
|
|
_listen = False
|
2021-03-07 22:39:37 +01:00
|
|
|
_listening_port = None
|
|
|
|
|
|
|
|
home = None
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
if not cls.home:
|
|
|
|
cls.home = tempfile.gettempdir()
|
|
|
|
cmd = cls._process_cmd + [
|
|
|
|
'--data-dir', cls.home,
|
|
|
|
'--connection-limit', str(cls._connection_limit)
|
|
|
|
]
|
2022-09-23 03:25:24 +02:00
|
|
|
if not cls._listen:
|
2021-03-07 22:39:37 +01:00
|
|
|
cmd += ['--no-incoming']
|
2022-09-23 03:25:24 +02:00
|
|
|
elif cls._listening_port:
|
|
|
|
cmd += ['-p', str(cls._listening_port)]
|
2021-03-07 22:39:37 +01:00
|
|
|
cls.process = psutil.Popen(cmd, stderr=subprocess.STDOUT) # nosec
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def _stop_process(cls, timeout=5):
|
|
|
|
cls.process.send_signal(signal.SIGTERM)
|
|
|
|
try:
|
|
|
|
cls.process.wait(timeout)
|
|
|
|
except psutil.TimeoutExpired:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
2023-08-01 23:15:00 +02:00
|
|
|
"""Ensures that process stopped and removes files"""
|
2021-03-07 22:39:37 +01:00
|
|
|
try:
|
|
|
|
if not cls._stop_process(10):
|
|
|
|
try:
|
|
|
|
cls.process.kill()
|
|
|
|
except psutil.NoSuchProcess:
|
|
|
|
pass
|
|
|
|
except psutil.NoSuchProcess:
|
|
|
|
pass
|
|
|
|
|
2023-08-02 19:53:36 +02:00
|
|
|
def connections(self):
|
|
|
|
"""All process' established connections"""
|
|
|
|
return [
|
|
|
|
c for c in self.process.connections()
|
|
|
|
if c.status == 'ESTABLISHED']
|
|
|
|
|
2021-03-07 22:39:37 +01:00
|
|
|
|
|
|
|
class TestProcessShutdown(TestProcessProto):
|
|
|
|
"""Separate test case for SIGTERM"""
|
2024-06-17 19:47:54 +02:00
|
|
|
_wait_time = 30
|
|
|
|
# longer wait time because it's not a benchmark
|
|
|
|
|
2021-03-07 22:39:37 +01:00
|
|
|
def test_shutdown(self):
|
|
|
|
"""Send to minode SIGTERM and ensure it stopped"""
|
|
|
|
self.assertTrue(
|
2024-06-17 19:47:54 +02:00
|
|
|
self._stop_process(self._wait_time),
|
|
|
|
'%s has not stopped in %i sec' % (
|
|
|
|
' '.join(self._process_cmd), self._wait_time))
|
2021-03-07 22:39:37 +01:00
|
|
|
|
|
|
|
|
|
|
|
class TestProcess(TestProcessProto):
|
|
|
|
"""The test case for minode process"""
|
2022-09-23 06:18:39 +02:00
|
|
|
_wait_time = 120
|
|
|
|
_check_limit = False
|
|
|
|
|
2021-03-07 22:39:37 +01:00
|
|
|
def test_connections(self):
|
|
|
|
"""Check minode process connections"""
|
|
|
|
_started = time.time()
|
2022-09-23 03:25:24 +02:00
|
|
|
|
2022-09-23 06:18:39 +02:00
|
|
|
def continue_check_limit(extra_time):
|
2023-08-02 04:17:58 +02:00
|
|
|
for _ in range(extra_time * 2):
|
2022-09-23 06:18:39 +02:00
|
|
|
self.assertLessEqual(
|
2023-08-02 19:53:36 +02:00
|
|
|
len(self.connections()),
|
2022-09-23 06:18:39 +02:00
|
|
|
# shared.outgoing_connections, one listening
|
|
|
|
# TODO: find the cause of one extra
|
|
|
|
(min(self._connection_limit, 8) if not self._listen
|
|
|
|
else self._connection_limit) + 1,
|
|
|
|
'Opened more connections than required'
|
|
|
|
' by --connection-limit')
|
|
|
|
time.sleep(1)
|
|
|
|
|
2023-08-02 04:17:58 +02:00
|
|
|
for _ in range(self._wait_time * 2):
|
2023-08-02 19:53:36 +02:00
|
|
|
if len(self.connections()) > self._connection_limit / 2:
|
2021-03-07 22:39:37 +01:00
|
|
|
_time_to_connect = round(time.time() - _started)
|
|
|
|
break
|
|
|
|
time.sleep(0.5)
|
|
|
|
else:
|
|
|
|
self.fail(
|
2024-06-17 19:47:54 +02:00
|
|
|
'Failed establish at least %i connections in %s sec'
|
|
|
|
% (int(self._connection_limit / 2), self._wait_time))
|
2022-09-23 06:18:39 +02:00
|
|
|
|
|
|
|
if self._check_limit:
|
|
|
|
continue_check_limit(_time_to_connect)
|
2022-09-23 03:25:24 +02:00
|
|
|
|
|
|
|
for c in self.process.connections():
|
2021-03-07 22:39:37 +01:00
|
|
|
if c.status == 'LISTEN':
|
|
|
|
if self._listen is False:
|
2023-08-14 05:05:29 +02:00
|
|
|
self.fail('Listening while started with --no-incoming')
|
|
|
|
return
|
2021-03-07 22:39:37 +01:00
|
|
|
self.assertEqual(c.laddr[1], self._listening_port or 8444)
|
|
|
|
break
|
2022-09-23 03:25:24 +02:00
|
|
|
else:
|
|
|
|
if self._listen:
|
|
|
|
self.fail('No listening connection found')
|
2021-03-11 18:37:23 +01:00
|
|
|
|
|
|
|
|
|
|
|
@unittest.skipIf(i2p_port_free, 'No running i2pd detected')
|
|
|
|
class TestProcessI2P(TestProcess):
|
|
|
|
"""Test minode process with --i2p and no IP"""
|
|
|
|
_process_cmd = ['minode', '--i2p', '--no-ip']
|
|
|
|
_connection_limit = 4
|
|
|
|
_listen = True
|
|
|
|
_listening_port = 8448
|
2023-08-02 19:53:36 +02:00
|
|
|
|
|
|
|
def test_connections(self):
|
|
|
|
"""Ensure all connections are I2P"""
|
|
|
|
super().test_connections()
|
|
|
|
for c in self.connections():
|
|
|
|
self.assertEqual(c.raddr[0], '127.0.0.1')
|
|
|
|
self.assertEqual(c.raddr[1], 7656)
|
2023-08-10 02:55:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
@unittest.skipUnless(i2p_port_free, 'Detected running i2pd')
|
|
|
|
class TestProcessNoI2P(TestProcessShutdown):
|
|
|
|
"""Test minode process shutdown with --i2p and no IP"""
|
|
|
|
_process_cmd = ['minode', '--i2p', '--no-ip']
|
2023-04-13 20:00:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
@unittest.skipIf(tor_port_free, 'No running tor detected')
|
|
|
|
class TestProcessTor(TestProcessProto):
|
|
|
|
"""A test case for minode process running with tor enabled"""
|
2023-09-08 03:03:23 +02:00
|
|
|
_process_cmd = ['minode', '--tor']
|
2023-04-13 20:00:31 +02:00
|
|
|
_wait_time = 60
|
|
|
|
|
|
|
|
def test_connections(self):
|
|
|
|
"""Check minode process connections"""
|
|
|
|
for _ in range(self._wait_time):
|
|
|
|
time.sleep(0.5)
|
|
|
|
connections = self.connections()
|
|
|
|
for c in connections:
|
|
|
|
self.assertEqual(c.raddr[0], '127.0.0.1')
|
|
|
|
self.assertEqual(c.raddr[1], 9050)
|
|
|
|
if len(connections) > self._connection_limit / 2:
|
|
|
|
break
|