From ee59b122385847a63503966e20da876b4afa4dba Mon Sep 17 00:00:00 2001 From: Arinerron Date: Sat, 3 Mar 2018 14:18:57 -0800 Subject: [PATCH] Add more unit tests --- .gitignore | 1 + Makefile | 16 ++++--- onionr/config.py | 7 ++- onionr/onionrplugins.py | 9 ++-- onionr/tests.py | 95 +++++++++++++++++++++++++++++++++++------ 5 files changed, 103 insertions(+), 25 deletions(-) diff --git a/.gitignore b/.gitignore index 11ff0671..12f3a0fd 100644 --- a/.gitignore +++ b/.gitignore @@ -6,5 +6,6 @@ onionr/*.pyc onionr/*.log onionr/data/hs/hostname onionr/data/* +onionr/data-backup/* onionr/gnupg/* run.sh diff --git a/Makefile b/Makefile index 3e8c020e..10ab5390 100644 --- a/Makefile +++ b/Makefile @@ -2,10 +2,10 @@ setup: sudo pip3 install -r requirements.txt - sudo rm -rf /usr/share/onionr/ - sudo rm -f /usr/bin/onionr install: + sudo rm -rf /usr/share/onionr/ + sudo rm -f /usr/bin/onionr sudo cp -rp ./onionr /usr/share/onionr sudo sh -c "echo \"#!/bin/sh\ncd /usr/share/onionr/\n./onionr.py \\\"\\\$$@\\\"\" > /usr/bin/onionr" sudo chmod +x /usr/bin/onionr @@ -16,12 +16,14 @@ uninstall: sudo rm -f /usr/bin/onionr test: - @cd onionr; ./tests.py - + @rm -rf onionr/data-backup + @mv onionr/data onionr/data-backup | true > /dev/null 2>&1 + -@cd onionr; ./tests.py + @rm -rf onionr/data + @mv onionr/data-backup onionr/data | true > /dev/null 2>&1 + reset: - echo "RESETING ONIONR" rm -f onionr/data/blocks/*.dat | true > /dev/null 2>&1 rm -f onionr/data/peers.db | true > /dev/null 2>&1 rm -f onionr/data/blocks.db | true > /dev/null 2>&1 - rm -rf onionr/data/address.db | true > /dev/null 2>&1 - + rm -f onionr/data/address.db | true > /dev/null 2>&1 diff --git a/onionr/config.py b/onionr/config.py index 27002d34..fb8ac161 100644 --- a/onionr/config.py +++ b/onionr/config.py @@ -27,7 +27,7 @@ def get(key, default = None): ''' Gets the key from configuration, or returns `default` ''' - + if is_set(key): return get_config()[key] return default @@ -38,7 +38,10 @@ def set(key, value = None, savefile = False): ''' global _config - _config[key] = value + if value is None: + del _config[key] + else: + _config[key] = value if savefile: save() diff --git a/onionr/onionrplugins.py b/onionr/onionrplugins.py index d3c3aefb..ae9c8d8e 100644 --- a/onionr/onionrplugins.py +++ b/onionr/onionrplugins.py @@ -173,8 +173,6 @@ def exists(name): Return value indicates whether or not the plugin exists ''' - check() - return os.path.isdir(get_plugins_folder(str(name).lower())) def get_enabled_plugins(): @@ -226,6 +224,11 @@ def check(): if not os.path.exists(os.path.dirname(get_plugins_folder())): logger.debug('Generating plugin data folder...') - os.path.mkdirs(os.path.dirname(get_plugins_folder())) + os.makedirs(os.path.dirname(get_plugins_folder())) + if not exists('test'): + os.makedirs(get_plugins_folder('test')) + with open(get_plugins_folder('test') + '/main.py', 'a') as main: + main.write("print('Running')\n\ndef on_test(onionr = None, data = None):\n print('received test event!')\n return True\n\ndef on_start(onionr = None, data = None):\n print('start event called')\n\ndef on_stop(onionr = None, data = None):\n print('stop event called')\n\ndef on_enable(onionr = None, data = None):\n print('enable event called')\n\ndef on_disable(onionr = None, data = None):\n print('disable event called')\n") + enable('test') return diff --git a/onionr/tests.py b/onionr/tests.py index 684640b5..50a7f6b9 100755 --- a/onionr/tests.py +++ b/onionr/tests.py @@ -14,7 +14,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . ''' -import unittest, sys, os, base64, tarfile, shutil, simplecrypt, logger +import unittest, sys, os, base64, tarfile, shutil, simplecrypt, logger, btc class OnionrTests(unittest.TestCase): def testPython3(self): @@ -25,9 +25,9 @@ class OnionrTests(unittest.TestCase): self.assertTrue(True) def testNone(self): - logger.debug('--------------------------') + logger.debug('-'*26 + '\n') logger.info('Running simple program run test...') - # Test just running ./onionr with no arguments + blank = os.system('./onionr.py --version') if blank != 0: self.assertTrue(False) @@ -35,8 +35,9 @@ class OnionrTests(unittest.TestCase): self.assertTrue(True) def testPeer_a_DBCreation(self): - logger.debug('--------------------------') + logger.debug('-'*26 + '\n') logger.info('Running peer db creation test...') + if os.path.exists('data/peers.db'): os.remove('data/peers.db') import core @@ -48,8 +49,9 @@ class OnionrTests(unittest.TestCase): self.assertTrue(False) def testPeer_b_addPeerToDB(self): - logger.debug('--------------------------') + logger.debug('-'*26 + '\n') logger.info('Running peer db insertion test...') + import core myCore = core.Core() if not os.path.exists('data/peers.db'): @@ -62,8 +64,10 @@ class OnionrTests(unittest.TestCase): def testData_b_Encrypt(self): self.assertTrue(True) return - logger.debug('--------------------------') + + logger.debug('-'*26 + '\n') logger.info('Running data dir encrypt test...') + import core myCore = core.Core() myCore.dataDirEncrypt('password') @@ -75,8 +79,10 @@ class OnionrTests(unittest.TestCase): def testData_a_Decrypt(self): self.assertTrue(True) return - logger.debug('--------------------------') + + logger.debug('-'*26 + '\n') logger.info('Running data dir decrypt test...') + import core myCore = core.Core() myCore.dataDirDecrypt('password') @@ -85,9 +91,42 @@ class OnionrTests(unittest.TestCase): else: self.assertTrue(False) - def testPlugins(self): - logger.debug('--------------------------') - logger.info('Running simple plugin system test...') + def testConfig(self): + logger.debug('-'*26 + '\n') + logger.info('Running simple configuration test...') + + import config + + config.check() + config.reload() + configdata = str(config.get_config()) + + config.set('testval', 1337) + if not config.get('testval', None) is 1337: + self.assertTrue(False) + + config.set('testval') + if not config.get('testval', None) is None: + self.assertTrue(False) + + config.save() + config.reload() + + if not str(config.get_config()) == configdata: + self.assertTrue(False) + + self.assertTrue(True) + + def testBitcoinNode(self): + logger.debug('-'*26 + '\n') + logger.info('Running bitcoin node test...') + + sbitcoin = btc.OnionrBTC() + + def testPluginReload(self): + logger.debug('-'*26 + '\n') + logger.info('Running simple plugin reload test...') + import onionrplugins try: onionrplugins.reload('test') @@ -95,9 +134,36 @@ class OnionrTests(unittest.TestCase): except: self.assertTrue(False) + def testPluginStopStart(self): + logger.debug('-'*26 + '\n') + logger.info('Running simple plugin restart test...') + + import onionrplugins + try: + onionrplugins.start('test') + onionrplugins.stop('test') + self.assertTrue(True) + except: + self.assertTrue(False) + + def testPluginEvent(self): + logger.debug('-'*26 + '\n') + logger.info('Running plugin event test...') + + import onionrplugins as plugins, onionrevents as events + + plugins.start('test') + if not events.call(plugins.get_plugin('test'), 'test'): + self.assertTrue(False) + + events.event('test', data = {'tests': self}) + + self.assertTrue(True) + def testQueue(self): - logger.debug('--------------------------') + logger.debug('-'*26 + '\n') logger.info('Running daemon queue test...') + # test if the daemon queue can read/write data import core myCore = core.Core() @@ -117,8 +183,9 @@ class OnionrTests(unittest.TestCase): logger.info('Succesfully added and read command') def testHashValidation(self): - logger.debug('--------------------------') + logger.debug('-'*26 + '\n') logger.info('Running hash validation test...') + import core myCore = core.Core() if not myCore._utils.validateHash("$324dfgfdg") and myCore._utils.validateHash("f2ca1bb6c7e907d06dafe4687e579fce76b37e4e93b7605022da52e6ccc26fd2") and not myCore._utils.validateHash("f2ca1bb6c7e907d06dafe4687e579fce76b37e4e93b7605022da52e6ccc26fd$"): @@ -127,8 +194,9 @@ class OnionrTests(unittest.TestCase): self.assertTrue(False) def testAddAdder(self): - logger.debug('--------------------------') + logger.debug('-'*26 + '\n') logger.info('Running address add+remove test') + import core myCore = core.Core() if not os.path.exists('data/address.db'): @@ -140,4 +208,5 @@ class OnionrTests(unittest.TestCase): self.assertTrue(False) else: self.assertTrue(False) + unittest.main()