From ee5c620cc60fdcd71e521221bebc62860e9c3a30 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Tue, 19 Feb 2019 16:14:06 -0600 Subject: [PATCH] small fixes and work on tests --- onionr/onionr.py | 43 +++++++++++++++------------- onionr/onionrusers/onionrusers.py | 14 --------- onionr/tests/test_highlevelcrypto.py | 30 +++++++++++++++++-- onionr/tests/test_onionrusers.py | 2 +- run_tests.sh | 6 ++-- 5 files changed, 55 insertions(+), 40 deletions(-) diff --git a/onionr/onionr.py b/onionr/onionr.py index c79b6488..13e6e47b 100755 --- a/onionr/onionr.py +++ b/onionr/onionr.py @@ -54,10 +54,12 @@ class Onionr: ''' self.userRunDir = os.getcwd() # Directory user runs the program from self.killed = False - try: - os.chdir(sys.path[0]) - except FileNotFoundError: - pass + + if sys.argv[0] == os.path.basename(__file__): + try: + os.chdir(sys.path[0]) + except FileNotFoundError: + pass try: self.dataDir = os.environ['ONIONR_HOME'] @@ -267,6 +269,17 @@ class Onionr: THIS SECTION HANDLES THE COMMANDS ''' + def doExport(self, bHash): + exportDir = self.dataDir + 'block-export/' + if not os.path.exists(exportDir): + if os.path.exists(self.dataDir): + os.mkdir(exportDir) + else: + logger.error('Onionr Not initialized') + data = onionrstorage.getData(self.onionrCore, bHash) + with open('%s/%s.dat' % (exportDir, bHash), 'wb') as exportFile: + exportFile.write(data) + def exportBlock(self): exportDir = self.dataDir + 'block-export/' try: @@ -276,19 +289,7 @@ class Onionr: sys.exit(1) else: bHash = sys.argv[2] - try: - path = sys.argv[3] - except (IndexError): - if not os.path.exists(exportDir): - if os.path.exists(self.dataDir): - os.mkdir(exportDir) - else: - logger.error('Onionr not initialized') - sys.exit(1) - path = exportDir - data = onionrstorage.getData(self.onionrCore, bHash) - with open('%s/%s.dat' % (exportDir, bHash), 'wb') as exportFile: - exportFile.write(data) + self.doExport(bHash) def showDetails(self): details = { @@ -691,8 +692,8 @@ class Onionr: ''' Displays a message suggesting help ''' - - logger.info('Do ' + logger.colors.bold + sys.argv[0] + ' --help' + logger.colors.reset + logger.colors.fg.green + ' for Onionr help.') + if __name__ == '__main__': + logger.info('Do ' + logger.colors.bold + sys.argv[0] + ' --help' + logger.colors.reset + logger.colors.fg.green + ' for Onionr help.') def start(self, input = False, override = False): ''' @@ -993,7 +994,9 @@ class Onionr: os.mkdir(dataDir) if os.path.exists('static-data/default_config.json'): - config.set_config(json.loads(open('static-data/default_config.json').read())) # this is the default config, it will be overwritten if a config file already exists. Else, it saves it + # this is the default config, it will be overwritten if a config file already exists. Else, it saves it + with open('static-data/default_config.json', 'r') as configReadIn: + config.set_config(json.loads(configReadIn.read())) else: # the default config file doesn't exist, try hardcoded config logger.warn('Default configuration file does not exist, switching to hardcoded fallback configuration!') diff --git a/onionr/onionrusers/onionrusers.py b/onionr/onionrusers/onionrusers.py index ab14a5a0..e57c45c0 100755 --- a/onionr/onionrusers/onionrusers.py +++ b/onionr/onionrusers/onionrusers.py @@ -190,17 +190,3 @@ class OnionrUser: conn.commit() conn.close() return - - def findAndSetID(self): - '''Find any info about the user from existing blocks and cache it to their DB entry''' - infoBlocks = [] - for bHash in self._core.getBlocksByType('userInfo'): - block = onionrblockapi.Block(bHash, core=self._core) - if block.signer == self.publicKey: - if block.verifySig(): - newName = block.getMetadata('name') - if newName.isalnum(): - logger.info('%s is now using the name %s.' % (self.publicKey, self._core._utils.escapeAnsi(newName))) - self._core.setPeerInfo(self.publicKey, 'name', newName) - else: - raise onionrexceptions.InvalidPubkey diff --git a/onionr/tests/test_highlevelcrypto.py b/onionr/tests/test_highlevelcrypto.py index 67236b45..fa4334f6 100644 --- a/onionr/tests/test_highlevelcrypto.py +++ b/onionr/tests/test_highlevelcrypto.py @@ -1,13 +1,13 @@ #!/usr/bin/env python3 import sys, os sys.path.append(".") -import unittest, uuid, hashlib +import unittest, uuid, hashlib, base64 import nacl.exceptions import nacl.signing, nacl.hash, nacl.encoding TEST_DIR = 'testdata/%s-%s' % (uuid.uuid4(), os.path.basename(__file__)) + '/' print("Test directory:", TEST_DIR) os.environ["ONIONR_HOME"] = TEST_DIR -import core, onionr +import core, onionr, onionrexceptions c = core.Core() crypto = c._crypto @@ -126,5 +126,31 @@ class OnionrCryptoTests(unittest.TestCase): pass else: self.assertFalse(True) + + def test_deterministic(self): + password = os.urandom(32) + gen = crypto.generateDeterministic(password) + self.assertTrue(c._utils.validatePubKey(gen[0])) + try: + crypto.generateDeterministic('weakpassword') + except onionrexceptions.PasswordStrengthError: + pass + else: + self.assertFalse(True) + try: + crypto.generateDeterministic(None) + except TypeError: + pass + else: + self.assertFalse(True) + + gen = crypto.generateDeterministic('weakpassword', bypassCheck=True) + + password = base64.b64encode(os.urandom(32)) + gen1 = crypto.generateDeterministic(password) + gen2 = crypto.generateDeterministic(password) + self.assertFalse(gen == gen1) + self.assertTrue(gen1 == gen2) + self.assertTrue(c._utils.validatePubKey(gen1[0])) unittest.main() \ No newline at end of file diff --git a/onionr/tests/test_onionrusers.py b/onionr/tests/test_onionrusers.py index 152c5882..0c9bbaac 100644 --- a/onionr/tests/test_onionrusers.py +++ b/onionr/tests/test_onionrusers.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 import sys, os sys.path.append(".") -import unittest, uuid, hashlib +import unittest, uuid import json TEST_DIR = 'testdata/%s-%s' % (uuid.uuid4(), os.path.basename(__file__)) + '/' print("Test directory:", TEST_DIR) diff --git a/run_tests.sh b/run_tests.sh index d0a60b5b..c59e6b3a 100755 --- a/run_tests.sh +++ b/run_tests.sh @@ -1,9 +1,10 @@ #!/bin/bash cd onionr; +rm -rf testdata; mkdir testdata; ran=0 - +SECONDS=0 ; close () { rm -rf testdata; exit 10; @@ -13,5 +14,4 @@ for f in tests/*.py; do python3 "$f" || close # if needed let "ran++" done -rm -rf testdata; -echo "ran $ran test files successfully" \ No newline at end of file +echo "ran $ran test files successfully in $SECONDS seconds" \ No newline at end of file