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,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!')

View File

@ -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

View File

@ -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()

View File

@ -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)

View File

@ -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"
echo "ran $ran test files successfully in $SECONDS seconds"