added test for learning lan services

This commit is contained in:
Kevin Froman 2020-03-13 23:47:44 -05:00
parent 7bb95c3b87
commit d4652e1107
5 changed files with 93 additions and 40 deletions

View file

@ -1,35 +0,0 @@
from unittest.mock import patch
import sys, os
sys.path.append(".")
sys.path.append("src/")
import unittest, uuid
TEST_DIR = 'testdata/%s-%s' % (uuid.uuid4(), os.path.basename(__file__)) + '/'
print("Test directory:", TEST_DIR)
os.environ["ONIONR_HOME"] = TEST_DIR
from utils import createdirs
from onionrcommands import parser
import onionrsetup as setup
from netcontroller.torcontrol import customtorrc
from utils import createdirs
from onionrsetup import setup_config, setup_default_plugins
from coredb import blockmetadb
from etc.onionrvalues import BLOCK_EXPORT_FILE_EXT
from threading import Thread
createdirs.create_dirs()
setup_config()
setup_default_plugins()
import config
from filepaths import export_location
class OnionrTests(unittest.TestCase):
def test_lan_discover(self):
testargs = ['onionr.py', 'start']
with patch.object(sys, 'argv', testargs):
try:
Thread(target=parser.register, daemon=True).start()
except SystemExit:
pass
unittest.main()

48
tests/test_lan_learn.py Normal file
View file

@ -0,0 +1,48 @@
#!/usr/bin/env python3
import sys, os
sys.path.append(".")
sys.path.append("src/")
import uuid
from threading import Thread
TEST_DIR = 'testdata/%s-%s' % (uuid.uuid4(), os.path.basename(__file__)) + '/'
print("Test directory:", TEST_DIR)
os.environ["ONIONR_HOME"] = TEST_DIR
import unittest, json
from utils import identifyhome, createdirs
from onionrsetup import setup_config
createdirs.create_dirs()
setup_config()
from utils import bettersleep
from lan.discover import lan_ips, MCAST_GRP, MCAST_PORT
from lan.discover import learn_services, advertise_service
import socket
from socket import SHUT_RDWR
lan_ips = ['']
class TestLanLearn(unittest.TestCase):
def test_lan_learn(self):
test_ip = '192.168.1.30'
def multicast():
port = 1349
MULTICAST_TTL = 3
ips = '-'.join([test_ip]) + f'-{port}'
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, MULTICAST_TTL)
sock.sendto(f"onionr-{ips}".encode('utf-8'), (MCAST_GRP, MCAST_PORT))
bettersleep.better_sleep(1)
try:
sock.shutdown(SHUT_RDWR)
except OSError:
pass
sock.close()
test_list = [test_ip]
Thread(target=learn_services, args=[test_list], daemon=True).start()
bettersleep.better_sleep(3)
multicast()
self.assertIn(test_ip, test_list)
unittest.main()