diff --git a/Dockerfile b/Dockerfile index b7b15ee8..bfce7943 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM ubuntu:bionic +FROM ubuntu:disco #Base settings ENV HOME /root diff --git a/onionr/coredb/daemonqueue/__init__.py b/onionr/coredb/daemonqueue/__init__.py index 1ad2e6a2..34c2324c 100644 --- a/onionr/coredb/daemonqueue/__init__.py +++ b/onionr/coredb/daemonqueue/__init__.py @@ -24,7 +24,7 @@ from onionrutils import localcommand, epoch from .. import dbfiles import dbcreator -def daemon_queue(): +def daemon_queue()->str: ''' Gives commands to the communication proccess/daemon by reading an sqlite3 database @@ -51,7 +51,7 @@ def daemon_queue(): return retData -def daemon_queue_add(command, data='', responseID=''): +def daemon_queue_add(command: str, data='', responseID: str =''): ''' Add a command to the daemon queue, used by the communication daemon (communicator.py) ''' diff --git a/onionr/httpapi/miscclientapi/endpoints.py b/onionr/httpapi/miscclientapi/endpoints.py index b5a0e7eb..12082a1d 100644 --- a/onionr/httpapi/miscclientapi/endpoints.py +++ b/onionr/httpapi/miscclientapi/endpoints.py @@ -94,7 +94,7 @@ class PrivateEndpoints: # returns node stats while True: try: - return Response(client_api._too_many.get(SerializedData).getStats()) + return Response(client_api._too_many.get(SerializedData).get_stats()) except AttributeError as e: pass diff --git a/onionr/onionrcommands/parser/arguments.py b/onionr/onionrcommands/parser/arguments.py index 9ad000f4..1fd2484d 100644 --- a/onionr/onionrcommands/parser/arguments.py +++ b/onionr/onionrcommands/parser/arguments.py @@ -17,6 +17,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . ''' +from typing import Callable from .. import onionrstatistics, version, daemonlaunch, keyadders, openwebinterface from .. import banblocks # Command to blacklist a block by its hash from .. import filecommands # commands to share files with onionr @@ -28,7 +29,7 @@ from .. import softreset # command to delete onionr blocks import onionrexceptions from onionrutils import importnewblocks # func to import new blocks import onionrevents as events -def get_arguments(): +def get_arguments()->dict: """This is a function because we need to be able to dynamically modify them with plugins""" args = { ('blacklist', 'blacklist-block', 'remove-block', 'removeblock', 'banblock', 'ban-block'): banblocks.ban_block, @@ -64,7 +65,7 @@ def get_help(arg: str) -> str: if arg in argument: return arguments[argument].onionr_help raise KeyError -def get_func(argument): +def get_func(argument: str) -> Callable: """Returns the function for a given command argument""" argument = argument.lower() args = get_arguments() diff --git a/onionr/onionrpeers/peerprofiles.py b/onionr/onionrpeers/peerprofiles.py index 1f1aa536..b9c2b613 100644 --- a/onionr/onionrpeers/peerprofiles.py +++ b/onionr/onionrpeers/peerprofiles.py @@ -19,6 +19,9 @@ ''' from coredb import keydb from onionrutils import epoch +from onionrutils import stringvalidators +import onionrblacklist +import onionrexceptions UPDATE_DELAY = 300 @@ -27,6 +30,7 @@ class PeerProfiles: PeerProfiles ''' def __init__(self, address): + if not stringvalidators.validate_transport(address): raise onionrexceptions.InvalidAddress self.address = address # node address self.score = None self.friendSigCount = 0 @@ -38,7 +42,9 @@ class PeerProfiles: self.getConnectTime() self.last_updated = {'connect_time': UPDATE_DELAY, 'score': UPDATE_DELAY} # Last time a given value was updated - return + + if not address in keydb.listkeys.list_adders() and not onionrblacklist.OnionrBlackList().inBlacklist(address): + keydb.addkeys.add_address(address) def loadScore(self): '''Load the node's score from the database''' @@ -49,10 +55,13 @@ class PeerProfiles: self.score = self.success def getConnectTime(self): + """set the connectTime variable for when we last connected to them, using the db value""" try: self.connectTime = int(keydb.transportinfo.get_address_info(self.address, 'lastConnect')) except (KeyError, ValueError, TypeError) as e: pass + else: + return self.connectTime def update_connect_time(self): if epoch.get_epoch() - self.last_updated['connect_time'] >= UPDATE_DELAY: @@ -69,4 +78,4 @@ class PeerProfiles: def addScore(self, toAdd): '''Add to the peer's score (can add negative)''' self.score += toAdd - self.saveScore() \ No newline at end of file + self.saveScore() diff --git a/onionr/serializeddata.py b/onionr/serializeddata.py index b3966bde..6daac1e4 100755 --- a/onionr/serializeddata.py +++ b/onionr/serializeddata.py @@ -32,7 +32,7 @@ class SerializedData: } ''' - def getStats(self): + def get_stats(self): '''Return statistics about our node''' stats = {} try: diff --git a/onionr/utils/createdirs.py b/onionr/utils/createdirs.py index b0b00d63..054f04b5 100644 --- a/onionr/utils/createdirs.py +++ b/onionr/utils/createdirs.py @@ -23,7 +23,8 @@ import dbcreator, filepaths home = identifyhome.identify_home() def create_dirs(): - """Creates onionr data-related directories in order of the hardcoded list below""" + """Creates onionr data-related directories in order of the hardcoded list below, + then trigger creation of DBs""" gen_dirs = [home, filepaths.block_data_location, filepaths.contacts_location, filepaths.export_location] for path in gen_dirs: if not os.path.exists(path): diff --git a/tests/test_filepaths.py b/tests/test_filepaths.py new file mode 100644 index 00000000..45cbbd51 --- /dev/null +++ b/tests/test_filepaths.py @@ -0,0 +1,16 @@ +import sys, os +sys.path.append(".") +sys.path.append("onionr/") +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 +import filepaths +from utils import identifyhome + +class TestFilePaths(unittest.TestCase): + def test_filepaths_main(self): + home = identifyhome.identify_home() + self.assertTrue(filepaths.home.startswith(home)) + +unittest.main() diff --git a/tests/test_peerprofiles.py b/tests/test_peerprofiles.py new file mode 100644 index 00000000..f8c0d57c --- /dev/null +++ b/tests/test_peerprofiles.py @@ -0,0 +1,66 @@ +import sys, os +sys.path.append(".") +sys.path.append("onionr/") +import unittest, uuid +import base64 + +TEST_DIR = 'testdata/%s-%s' % (uuid.uuid4(), os.path.basename(__file__)) + '/' +print("Test directory:", TEST_DIR) +os.environ["ONIONR_HOME"] = TEST_DIR +from onionrpeers import peerprofiles +import onionrexceptions +from coredb import keydb +from utils import createdirs +from onionrutils import stringvalidators, epoch +TEST_PEER = '3n5wclq4w4pfkcfmjcpqrjluctpm2tzt7etfblavf42cntv6hrerkzyb.onion' +createdirs.create_dirs() + +def rand_fake_adder_generator(): + rand_bytes = os.urandom(35) + return base64.b32encode(rand_bytes).decode().lower() + '.onion' + +test_peers = [] +for x in range(100): + p = rand_fake_adder_generator() + assert stringvalidators.validate_transport(p) + test_peers.append(p) + +class TestPeerProfiles(unittest.TestCase): + def test_invalid_init(self): + self.assertRaises(onionrexceptions.InvalidAddress, peerprofiles.PeerProfiles, "invalid") + def test_valid_init(self): + peerprofiles.PeerProfiles(test_peers.pop()) + + def test_load_score(self): + p = peerprofiles.PeerProfiles(test_peers.pop()) + self.assertEqual(p.score, 0) + + def test_inc_score(self): + p = peerprofiles.PeerProfiles(test_peers.pop()) + s = 0 + for x in range(2): + s += 1 + p.addScore(1) + self.assertEqual(p.score, s) + + def test_inc_score_with_db(self): + p = peerprofiles.PeerProfiles(test_peers.pop()) + s = 0 + for x in range(2): + p.last_updated['score'] = epoch.get_epoch() - peerprofiles.UPDATE_DELAY + s += 1 + p.addScore(1) + self.assertEqual(p.score, keydb.transportinfo.get_address_info(p.address, 'success')) + + def test_inc_score_with_sync_Delay(self): + p = peerprofiles.PeerProfiles(test_peers.pop()) + s = 0 + for x in range(2): + s += 1 + p.addScore(1) + if x == 0: + self.assertEqual(p.score, keydb.transportinfo.get_address_info(p.address, 'success')) + else: + self.assertNotEqual(p.score, keydb.transportinfo.get_address_info(p.address, 'success')) + +unittest.main()