From 8a3f84097a90c4e768a09d35d4f5c23dc38a0a88 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Wed, 29 Jan 2020 22:56:47 -0600 Subject: [PATCH] + now require runtime tests to generate unittest result that is somewhat fresh + add runtime test for webpass needing to be set --- src/communicator/daemoneventhooks/__init__.py | 7 +++- src/onionrcommands/daemonlaunch.py | 1 - src/onionrcommands/runtimetestcmd.py | 7 ++-- src/onionrutils/localcommand.py | 3 +- src/runtests/__init__.py | 24 +++++++++-- src/runtests/ownnode.py | 6 +-- src/runtests/uicheck.py | 2 +- src/runtests/webpasstest.py | 11 +++++ static-data/index.html | 2 +- tests/runtime-result.txt | 1 + tests/test_runtime_result.py | 26 ++++++++++++ tests/test_webpass_required.py | 42 +++++++++++++++++++ 12 files changed, 115 insertions(+), 17 deletions(-) create mode 100644 src/runtests/webpasstest.py create mode 100644 tests/runtime-result.txt create mode 100644 tests/test_runtime_result.py create mode 100644 tests/test_webpass_required.py diff --git a/src/communicator/daemoneventhooks/__init__.py b/src/communicator/daemoneventhooks/__init__.py index fe46f15b..f7bec957 100644 --- a/src/communicator/daemoneventhooks/__init__.py +++ b/src/communicator/daemoneventhooks/__init__.py @@ -2,6 +2,8 @@ Hooks to handle daemon events """ +from threading import Thread + from .removefrominsertqueue import remove_from_insert_queue from typing import TYPE_CHECKING @@ -67,10 +69,11 @@ def daemon_event_handlers(shared_state: 'TooMany'): comm_inst.offlinePeers = [] def test_runtime(): - comm_inst.shared_state.get_by_string( - "OnionrRunTestManager").run_tests() + Thread(target=comm_inst.shared_state.get_by_string( + "OnionrRunTestManager").run_tests).start() events_api.register_listener(remove_from_insert_queue_wrapper) events_api.register_listener(print_test) events_api.register_listener(upload_event) + events_api.register_listener(test_runtime) diff --git a/src/onionrcommands/daemonlaunch.py b/src/onionrcommands/daemonlaunch.py index ef13ea66..27b75eae 100755 --- a/src/onionrcommands/daemonlaunch.py +++ b/src/onionrcommands/daemonlaunch.py @@ -134,7 +134,6 @@ def daemon(): events.event('daemon_start') communicator.startCommunicator(shared_state) - localcommand.local_command('shutdown') if not offline_mode: net.killTor() diff --git a/src/onionrcommands/runtimetestcmd.py b/src/onionrcommands/runtimetestcmd.py index 5004e37d..d523ba04 100644 --- a/src/onionrcommands/runtimetestcmd.py +++ b/src/onionrcommands/runtimetestcmd.py @@ -25,11 +25,12 @@ def do_runtime_test(): """Send runtime test daemon queue command.""" spawn( localcommand.local_command, - f'/daemon-event/test_runtime', + f'daemon-event/test_runtime', post=True, is_json=True, - postData={} - ).get(10) + postData={}, + maxWait=300 + ).get(300) do_runtime_test.onionr_help = "If Onionr is running, " # type: ignore diff --git a/src/onionrutils/localcommand.py b/src/onionrutils/localcommand.py index 9e1ab9fd..7a3bde84 100644 --- a/src/onionrutils/localcommand.py +++ b/src/onionrutils/localcommand.py @@ -66,6 +66,8 @@ def local_command(command, data='', silent = True, post=False, if data != '': data = '&data=' + urllib.parse.quote_plus(data) payload = 'http://%s/%s%s' % (hostname, command, data) + if not config.get('client.webpassword'): + config.reload() try: if post: @@ -89,5 +91,4 @@ def local_command(command, data='', silent = True, post=False, if not silent: logger.error('Failed to make local request (command: %s):%s' % (command, error), terminal=True) ret_data = False - return ret_data diff --git a/src/runtests/__init__.py b/src/runtests/__init__.py index 51b29eb6..2dbfa931 100644 --- a/src/runtests/__init__.py +++ b/src/runtests/__init__.py @@ -1,13 +1,15 @@ -""" - Onionr - Private P2P Communication +"""Onionr - Private P2P Communication. - Test Onionr as it is running +Test Onionr as it is running """ +import os + import logger from onionrutils import epoch from . import uicheck, inserttest, stresstest from . import ownnode +from .webpasstest import webpass_test """ This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -27,9 +29,12 @@ RUN_TESTS = [uicheck.check_ui, inserttest.insert_bin_test, ownnode.test_tor_adder, ownnode.test_own_node, - stresstest.stress_test_block_insert + stresstest.stress_test_block_insert, + webpass_test ] +SUCCESS_FILE = os.path.dirname(os.path.realpath(__file__)) + '/../../tests/runtime-result.txt' + class OnionrRunTestManager: def __init__(self): @@ -40,6 +45,11 @@ class OnionrRunTestManager: cur_time = epoch.get_epoch() logger.info(f"Doing runtime tests at {cur_time}") + try: + os.remove(SUCCESS_FILE) + except FileNotFoundError: + pass + try: for i in RUN_TESTS: last = i @@ -47,3 +57,9 @@ class OnionrRunTestManager: logger.info("[RUNTIME TEST] " + last.__name__ + " passed") except (ValueError, AttributeError): logger.error(last.__name__ + ' failed') + else: + ep = str(epoch.get_epoch()) + logger.info(f'All runtime tests passed at {ep}') + with open(SUCCESS_FILE, 'w') as f: + f.write(ep) + diff --git a/src/runtests/ownnode.py b/src/runtests/ownnode.py index ec78b51e..1c4a3e5f 100644 --- a/src/runtests/ownnode.py +++ b/src/runtests/ownnode.py @@ -29,10 +29,9 @@ def test_own_node(test_manager): if config.get('general.security_level', 0) > 0: return own_tor_address = gettransports.get()[0] - print(socks_port) if 'this is an onionr node' \ - not in basicrequests.do_get_request(own_tor_address, - port=socks_port).lower(): + not in basicrequests.do_get_request('http://' + own_tor_address, + port=socks_port, ignoreAPI=True).lower(): logger.warn('Own node not reachable in test') raise ValueError @@ -47,7 +46,6 @@ def test_tor_adder(test_manager): raise ValueError('No Tor node address created yet') if hs not in gettransports.get(): - print(hs in gettransports.get(), 'meme') logger.error('gettransports Tor not same as file: %s %s' % (hs, gettransports.get())) raise ValueError('gettransports Tor not same as file') diff --git a/src/runtests/uicheck.py b/src/runtests/uicheck.py index 03f4560f..128d5250 100644 --- a/src/runtests/uicheck.py +++ b/src/runtests/uicheck.py @@ -5,5 +5,5 @@ def check_ui(test_manager): result = localcommand.local_command(point) if not result: raise ValueError result = result.lower() - if not 'script' in result: + if 'script' not in result: raise ValueError(f'uicheck failed on {point}') diff --git a/src/runtests/webpasstest.py b/src/runtests/webpasstest.py new file mode 100644 index 00000000..ec1136cd --- /dev/null +++ b/src/runtests/webpasstest.py @@ -0,0 +1,11 @@ +import requests + +from onionrutils import localcommand + + +def webpass_test(test_manager): + if requests.get('http://' + localcommand.get_hostname() + '/ping') == \ + 'pong!': + raise ValueError + if localcommand.local_command('ping') != 'pong!': + raise ValueError('Could not ping with normal localcommand in webpasstest') diff --git a/static-data/index.html b/static-data/index.html index f60d62a9..428cdbe3 100755 --- a/static-data/index.html +++ b/static-data/index.html @@ -5,4 +5,4 @@

Onionr is a decentralized, distributed peer-to-peer data communication network.

-

To learn more about Onionr, see the website at http://onionr.onionkvc5ibm37bmxwr56bdxcdnb6w3wm4bdghh5qo6f6za7gn7styid.onion or Onionr.net

+

To learn more about Onionr, see the website at http://onionrbak72t5zhbzuey2fdkpczlvhowgcpqc6uoyrd3uxztzxwz5cyd.onion/ or Onionr.net

diff --git a/tests/runtime-result.txt b/tests/runtime-result.txt new file mode 100644 index 00000000..c44c576a --- /dev/null +++ b/tests/runtime-result.txt @@ -0,0 +1 @@ +1580360010 \ No newline at end of file diff --git a/tests/test_runtime_result.py b/tests/test_runtime_result.py new file mode 100644 index 00000000..374a6788 --- /dev/null +++ b/tests/test_runtime_result.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 +import sys, os, time, math +sys.path.append(".") +sys.path.append("src/") +import unittest, uuid +import json +TEST_DIR = 'testdata/%s-%s' % (uuid.uuid4(), os.path.basename(__file__)) + '/' +print("Test directory:", TEST_DIR) +os.environ["ONIONR_HOME"] = TEST_DIR + +SUCCESS_FILE = os.path.dirname(__file__) + '/runtime-result.txt' + +from utils import identifyhome, createdirs +from onionrsetup import setup_config +createdirs.create_dirs() +setup_config() + +class TestRuntimeFile(unittest.TestCase): + def test_runtime_result(self): + self.assertTrue(os.path.exists(SUCCESS_FILE)) + with open(SUCCESS_FILE, 'r') as result_file: + self.assertLess(math.floor(time.time()) - int(result_file.read()), 600) + + + +unittest.main() diff --git a/tests/test_webpass_required.py b/tests/test_webpass_required.py new file mode 100644 index 00000000..945ef910 --- /dev/null +++ b/tests/test_webpass_required.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 +import sys, os +import threading + +from gevent import sleep +import requests + +sys.path.append(".") +sys.path.append("src/") +import unittest, uuid +import json +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 identifyhome, createdirs +from onionrsetup import setup_config +createdirs.create_dirs() +setup_config() +from onionrcommands import daemonlaunch +from onionrutils import localcommand, getclientapiserver +import config + +class TestWebpass(unittest.TestCase): + def test_needs_webpass(self): + config.set('general.use_bootstrap', False) + threading.Thread(target=daemonlaunch.start).start() + while localcommand.local_command('/ping') != 'pong!': + sleep(1) + self.assertNotEqual( + requests.get('http://' + getclientapiserver.get_client_API_server() + '/ping'), + 'pong!' + ) + while True: + try: + daemonlaunch.kill_daemon() + except KeyError: + sleep(1) + + + +unittest.main()