+ added more tests

* fixed chdir home bug
* fixed some tests creating normal data dir
master
Kevin Froman 2020-03-04 00:59:29 -06:00
parent 665cb0c732
commit 60d2ebfaed
20 changed files with 139 additions and 55 deletions

4
docs/dev/error-codes.md Normal file
View File

@ -0,0 +1,4 @@
# Exit codes
10: run with no command
3: command does not exist

View File

@ -24,9 +24,9 @@ echo "ran $ran integration tests."
echo "total test time $SECONDS" echo "total test time $SECONDS"
ran=0; ran=0;
for f in tests/browser-tests/*.py; do #for f in tests/browser-tests/*.py; do
python3 "$f" || close # if needed # python3 "$f" || close # if needed
let "ran++" # let "ran++"
done #done
echo "ran $ran browser tests." #echo "ran $ran browser tests."
echo "total test time $SECONDS" echo "total test time $SECONDS"

View File

@ -1,9 +1,17 @@
''' """Onionr - Private P2P Communication.
Onionr - Private P2P Communication
Cleanup old Onionr blocks and forward secrecy keys using the communicator. Ran from a timer usually Cleanup old Onionr blocks and forward secrecy keys using the communicator.
''' Ran from a communicator timer usually
''' """
import sqlite3
import logger
from onionrusers import onionrusers
from onionrutils import epoch
from coredb import blockmetadb, dbfiles
import onionrstorage
from onionrstorage import removeblock
from onionrblocks import onionrblacklist
"""
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
@ -16,15 +24,8 @@
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
''' """
import sqlite3
import logger
from onionrusers import onionrusers
from onionrutils import epoch
from coredb import blockmetadb, dbfiles
import onionrstorage
from onionrstorage import removeblock
from onionrblocks import onionrblacklist
def __remove_from_upload(comm_inst, block_hash: str): def __remove_from_upload(comm_inst, block_hash: str):
try: try:
@ -32,8 +33,9 @@ def __remove_from_upload(comm_inst, block_hash: str):
except ValueError: except ValueError:
pass pass
def clean_old_blocks(comm_inst): def clean_old_blocks(comm_inst):
'''Delete old blocks if our disk allocation is full/near full, and also expired blocks''' """Delete expired blocks + old blocks if disk allocation is near full"""
blacklist = onionrblacklist.OnionrBlackList() blacklist = onionrblacklist.OnionrBlackList()
# Delete expired blocks # Delete expired blocks
for bHash in blockmetadb.expiredblocks.get_expired_blocks(): for bHash in blockmetadb.expiredblocks.get_expired_blocks():
@ -56,8 +58,9 @@ def clean_old_blocks(comm_inst):
comm_inst.decrementThreadCount('clean_old_blocks') comm_inst.decrementThreadCount('clean_old_blocks')
def clean_keys(comm_inst): def clean_keys(comm_inst):
'''Delete expired forward secrecy keys''' """Delete expired forward secrecy keys"""
conn = sqlite3.connect(dbfiles.user_id_info_db, timeout=10) conn = sqlite3.connect(dbfiles.user_id_info_db, timeout=10)
c = conn.cursor() c = conn.cursor()
time = epoch.get_epoch() time = epoch.get_epoch()

View File

@ -33,7 +33,7 @@ def add_to_block_DB(newHash, selfInsert=False, dataSaved=False):
""" """
if blockmetadata.has_block(newHash): if blockmetadata.has_block(newHash):
raise raise BlockMetaEntryExists
conn = sqlite3.connect(dbfiles.block_meta_db, timeout=onionrvalues.DATABASE_LOCK_TIMEOUT) conn = sqlite3.connect(dbfiles.block_meta_db, timeout=onionrvalues.DATABASE_LOCK_TIMEOUT)
c = conn.cursor() c = conn.cursor()
currentTime = epoch.get_epoch() + secrets.randbelow(301) currentTime = epoch.get_epoch() + secrets.randbelow(301)

View File

@ -17,7 +17,7 @@ announce_cache = home + 'announcecache.dat'
export_location = home + 'block-export/' export_location = home + 'block-export/'
upload_list = home + 'upload-list.json' upload_list = home + 'upload-list.json'
config_file = home + 'config.json' config_file = home + 'config.json'
daemon_mark_file = app_root + '/daemon-true.txt' daemon_mark_file = home + '/daemon-true.txt'
lock_file = home + 'onionr.lock' lock_file = home + 'onionr.lock'
site_cache = home + 'onionr-sites.txt' site_cache = home + 'onionr-sites.txt'

View File

@ -5,6 +5,12 @@ This module loads in the Onionr arguments and their help messages
import sys import sys
import os import os
try:
if sys.argv[1] not in ('start', 'details', 'show-details'):
os.chdir(os.environ['ORIG_ONIONR_RUN_DIR'])
except (KeyError, IndexError) as _:
pass
import logger import logger
import onionrexceptions import onionrexceptions
import onionrplugins import onionrplugins
@ -74,19 +80,14 @@ def register():
try: try:
cmd = sys.argv[1] cmd = sys.argv[1]
except IndexError: except IndexError:
logger.debug("Detected Onionr run with no commands specified") logger.info('Run with --help to see available commands', terminal=True)
return sys.exit(10)
is_help_cmd = False is_help_cmd = False
if cmd.replace('--', '').lower() == 'help': if cmd.replace('--', '').lower() == 'help':
is_help_cmd = True is_help_cmd = True
try: try:
try:
if cmd not in ('start', 'details', 'show-details'):
os.chdir(os.environ['ORIG_ONIONR_RUN_DIR'])
except KeyError:
pass
try: try:
arguments.get_func(cmd)() arguments.get_func(cmd)()
except KeyboardInterrupt: except KeyboardInterrupt:

View File

@ -12,6 +12,7 @@ from . import ownnode
from .webpasstest import webpass_test from .webpasstest import webpass_test
from .osver import test_os_ver_endpoint from .osver import test_os_ver_endpoint
from .clearnettor import test_clearnet_tor_request from .clearnettor import test_clearnet_tor_request
from .housekeeping import test_inserted_housekeeping
""" """
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
@ -34,7 +35,8 @@ RUN_TESTS = [uicheck.check_ui,
stresstest.stress_test_block_insert, stresstest.stress_test_block_insert,
webpass_test, webpass_test,
test_os_ver_endpoint, test_os_ver_endpoint,
test_clearnet_tor_request test_clearnet_tor_request,
test_inserted_housekeeping
] ]
SUCCESS_FILE = os.path.dirname(os.path.realpath(__file__)) + '/../../tests/runtime-result.txt' SUCCESS_FILE = os.path.dirname(os.path.realpath(__file__)) + '/../../tests/runtime-result.txt'

View File

@ -0,0 +1,25 @@
import os
from gevent import sleep
from onionrblocks import insert
import logger
from coredb.blockmetadb import get_block_list
from onionrutils import epoch
def test_inserted_housekeeping(testmanager):
"""Tests that inserted blocks are proprely deleted"""
bl = insert('testdata', expire=12)
wait_seconds = 132 # Wait two minutes plus expire time
count = 0
if bl in get_block_list():
while count < wait_seconds:
if bl in get_block_list():
sleep(1)
count += 1
else:
return
raise ValueError('Inserted block with expiry not erased')
else:
raise ValueError('Inserted block in expiry test not present in list')

View File

@ -29,10 +29,6 @@ def identify_home() -> str:
path = os.environ.get('ONIONR_HOME', None) path = os.environ.get('ONIONR_HOME', None)
if path is not None and not os.getcwd().endswith('src') \
and 'test' not in path:
path = 'src/' + path
if path is None: if path is None:
system = platform.system() system = platform.system()
if system == 'Linux': if system == 'Linux':

View File

@ -25,7 +25,10 @@ class OnionrTests(unittest.TestCase):
def test_export(self): def test_export(self):
testargs = ["onionr.py", "flowsend", "tests", "hello"] testargs = ["onionr.py", "flowsend", "tests", "hello"]
with patch.object(sys, 'argv', testargs): with patch.object(sys, 'argv', testargs):
parser.register() try:
parser.register()
except SystemExit:
pass
bl = blockmetadb.get_block_list()[0] bl = blockmetadb.get_block_list()[0]
testargs = ["onionr.py", "export-block", bl] testargs = ["onionr.py", "export-block", bl]
with patch.object(sys, 'argv', testargs): with patch.object(sys, 'argv', testargs):

View File

@ -7,10 +7,13 @@ TEST_DIR = 'testdata/%s-%s' % (uuid.uuid4(), os.path.basename(__file__)) + '/'
os.environ["ONIONR_HOME"] = TEST_DIR os.environ["ONIONR_HOME"] = TEST_DIR
print(f'running integration test for {__file__}') print(f'running integration test for {__file__}')
with Popen(['./onionr.sh'], stdout=PIPE) as onionr_proc: try:
output = onionr_proc.stdout.read().decode() with Popen(['./onionr.sh'], stdout=PIPE) as onionr_proc:
if onionr_proc.returncode != 0: output = onionr_proc.stdout.read().decode()
raise ValueError('Raised non zero exit ' + str(onionr_proc.returncode)) except SystemExit:
pass
if onionr_proc.returncode != 10:
raise ValueError('Raised non 10 exit ' + str(onionr_proc.returncode))
if output != '': if 'Run with --help to see available commands' not in output:
raise ValueError('No command run returned non-blank output') raise ValueError('No command run returned non-blank output')

View File

@ -25,7 +25,10 @@ class OnionrTests(unittest.TestCase):
def test_vanity(self): def test_vanity(self):
testargs = ["onionr.py"] testargs = ["onionr.py"]
with patch.object(sys, 'argv', testargs): with patch.object(sys, 'argv', testargs):
parser.register() try:
parser.register()
except SystemExit:
pass
testargs = ["onionr.py", "add-vanity", "jolt"] testargs = ["onionr.py", "add-vanity", "jolt"]
with patch.object(sys, 'argv', testargs): with patch.object(sys, 'argv', testargs):
parser.register() parser.register()

View File

@ -1 +1 @@
1583020786 1583304780

View File

@ -4,6 +4,10 @@ sys.path.append(".")
sys.path.append("src/") sys.path.append("src/")
import unittest, uuid, hashlib import unittest, uuid, hashlib
TEST_DIR = 'testdata/%s-%s' % (uuid.uuid4(), os.path.basename(__file__)) + '/'
print("Test directory:", TEST_DIR)
os.environ["ONIONR_HOME"] = TEST_DIR
import onionrblocks import onionrblocks
import onionrstorage import onionrstorage
from utils import createdirs from utils import createdirs

View File

@ -11,10 +11,6 @@ from onionrcommands import parser
import onionrsetup as setup import onionrsetup as setup
from netcontroller.torcontrol import customtorrc from netcontroller.torcontrol import customtorrc
class OnionrTests(unittest.TestCase): class OnionrTests(unittest.TestCase):
def test_no_command(self):
testargs = ["onionr.py"]
with patch.object(sys, 'argv', testargs):
parser.register()
def test_version_command(self): def test_version_command(self):
testargs = ["onionr.py", "version"] testargs = ["onionr.py", "version"]
with patch.object(sys, 'argv', testargs): with patch.object(sys, 'argv', testargs):

View File

@ -0,0 +1,25 @@
#!/usr/bin/env python3
import sys, os
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
from coredb import blockmetadb
from onionrexceptions import BlockMetaEntryExists
createdirs.create_dirs()
setup_config()
class TestDuplicateMetaEntry(unittest.TestCase):
def test_no_duplicate(self):
bl_hash = '0c88c7d4515363310f0a2522706c49f3f21def5f6fd69af1f91a1849239e7ea6'
blockmetadb.add_to_block_DB(bl_hash)
self.assertRaises(
BlockMetaEntryExists, blockmetadb.add_to_block_DB, bl_hash)
unittest.main()

View File

@ -1,7 +1,10 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import sys, os import sys, os, uuid
sys.path.append(".") sys.path.append(".")
sys.path.append("src/") sys.path.append("src/")
TEST_DIR = 'testdata/%s-%s' % (uuid.uuid4(), os.path.basename(__file__)) + '/'
print("Test directory:", TEST_DIR)
os.environ["ONIONR_HOME"] = TEST_DIR
import unittest, uuid import unittest, uuid
from utils import identifyhome from utils import identifyhome

View File

@ -1,10 +1,19 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import unittest, sys import sys, os
sys.path.append(".") sys.path.append(".")
sys.path.append("src/") 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 onionrblocks import time_insert from onionrblocks import time_insert
from onionrblocks import onionrblockapi from onionrblocks import onionrblockapi
from onionrsetup import setup_config, setup_default_plugins
from utils import createdirs
createdirs.create_dirs()
setup_config()
setup_default_plugins()
class TestTimeInsert(unittest.TestCase): class TestTimeInsert(unittest.TestCase):
def test_time_insert_none(self): def test_time_insert_none(self):

View File

@ -1,6 +1,10 @@
import sys, os import sys, os
import uuid
sys.path.append(".") sys.path.append(".")
sys.path.append("src/") sys.path.append("src/")
TEST_DIR = 'testdata/%s-%s' % (uuid.uuid4(), os.path.basename(__file__)) + '/'
print("Test directory:", TEST_DIR)
os.environ["ONIONR_HOME"] = TEST_DIR
import unittest import unittest
import vanityonionr import vanityonionr

View File

@ -1,5 +1,8 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import unittest, sys import unittest, sys, uuid, os
TEST_DIR = 'testdata/%s-%s' % (uuid.uuid4(), os.path.basename(__file__)) + '/'
print("Test directory:", TEST_DIR)
os.environ["ONIONR_HOME"] = TEST_DIR
sys.path.append(".") sys.path.append(".")
sys.path.append("src/") sys.path.append("src/")