small fixes and work on tests

This commit is contained in:
Kevin Froman 2019-02-19 16:14:06 -06:00
parent a05a391b7e
commit ee5c620cc6
5 changed files with 55 additions and 40 deletions

View File

@ -54,6 +54,8 @@ class Onionr:
''' '''
self.userRunDir = os.getcwd() # Directory user runs the program from self.userRunDir = os.getcwd() # Directory user runs the program from
self.killed = False self.killed = False
if sys.argv[0] == os.path.basename(__file__):
try: try:
os.chdir(sys.path[0]) os.chdir(sys.path[0])
except FileNotFoundError: except FileNotFoundError:
@ -267,6 +269,17 @@ class Onionr:
THIS SECTION HANDLES THE COMMANDS 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): def exportBlock(self):
exportDir = self.dataDir + 'block-export/' exportDir = self.dataDir + 'block-export/'
try: try:
@ -276,19 +289,7 @@ class Onionr:
sys.exit(1) sys.exit(1)
else: else:
bHash = sys.argv[2] bHash = sys.argv[2]
try: self.doExport(bHash)
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)
def showDetails(self): def showDetails(self):
details = { details = {
@ -691,7 +692,7 @@ class Onionr:
''' '''
Displays a message suggesting help Displays a message suggesting help
''' '''
if __name__ == '__main__':
logger.info('Do ' + logger.colors.bold + sys.argv[0] + ' --help' + logger.colors.reset + logger.colors.fg.green + ' for Onionr help.') 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): def start(self, input = False, override = False):
@ -993,7 +994,9 @@ class Onionr:
os.mkdir(dataDir) os.mkdir(dataDir)
if os.path.exists('static-data/default_config.json'): 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: else:
# the default config file doesn't exist, try hardcoded config # the default config file doesn't exist, try hardcoded config
logger.warn('Default configuration file does not exist, switching to hardcoded fallback configuration!') logger.warn('Default configuration file does not exist, switching to hardcoded fallback configuration!')

View File

@ -190,17 +190,3 @@ class OnionrUser:
conn.commit() conn.commit()
conn.close() conn.close()
return 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

View File

@ -1,13 +1,13 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import sys, os import sys, os
sys.path.append(".") sys.path.append(".")
import unittest, uuid, hashlib import unittest, uuid, hashlib, base64
import nacl.exceptions import nacl.exceptions
import nacl.signing, nacl.hash, nacl.encoding import nacl.signing, nacl.hash, nacl.encoding
TEST_DIR = 'testdata/%s-%s' % (uuid.uuid4(), os.path.basename(__file__)) + '/' TEST_DIR = 'testdata/%s-%s' % (uuid.uuid4(), os.path.basename(__file__)) + '/'
print("Test directory:", TEST_DIR) print("Test directory:", TEST_DIR)
os.environ["ONIONR_HOME"] = TEST_DIR os.environ["ONIONR_HOME"] = TEST_DIR
import core, onionr import core, onionr, onionrexceptions
c = core.Core() c = core.Core()
crypto = c._crypto crypto = c._crypto
@ -127,4 +127,30 @@ class OnionrCryptoTests(unittest.TestCase):
else: else:
self.assertFalse(True) 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() unittest.main()

View File

@ -1,7 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import sys, os import sys, os
sys.path.append(".") sys.path.append(".")
import unittest, uuid, hashlib import unittest, uuid
import json import json
TEST_DIR = 'testdata/%s-%s' % (uuid.uuid4(), os.path.basename(__file__)) + '/' TEST_DIR = 'testdata/%s-%s' % (uuid.uuid4(), os.path.basename(__file__)) + '/'
print("Test directory:", TEST_DIR) print("Test directory:", TEST_DIR)

View File

@ -1,9 +1,10 @@
#!/bin/bash #!/bin/bash
cd onionr; cd onionr;
rm -rf testdata;
mkdir testdata; mkdir testdata;
ran=0 ran=0
SECONDS=0 ;
close () { close () {
rm -rf testdata; rm -rf testdata;
exit 10; exit 10;
@ -13,5 +14,4 @@ for f in tests/*.py; do
python3 "$f" || close # if needed python3 "$f" || close # if needed
let "ran++" let "ran++"
done done
rm -rf testdata; echo "ran $ran test files successfully in $SECONDS seconds"
echo "ran $ran test files successfully"