Onionr/onionr/tests.py

276 lines
10 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
'''
Onionr - P2P Microblogging Platform & Social network
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
'''
import unittest, sys, os, base64, tarfile, shutil, simplecrypt, logger
class OnionrTests(unittest.TestCase):
def testPython3(self):
if sys.version_info.major != 3:
2018-01-26 07:22:48 +00:00
logger.debug('Python version: ' + sys.version_info.major)
self.assertTrue(False)
else:
self.assertTrue(True)
def testNone(self):
2018-03-03 22:18:57 +00:00
logger.debug('-'*26 + '\n')
2018-01-26 07:22:48 +00:00
logger.info('Running simple program run test...')
2018-03-03 22:18:57 +00:00
2018-02-22 07:24:25 +00:00
blank = os.system('./onionr.py --version')
if blank != 0:
self.assertTrue(False)
else:
self.assertTrue(True)
def testPeer_a_DBCreation(self):
2018-03-03 22:18:57 +00:00
logger.debug('-'*26 + '\n')
2018-01-26 07:22:48 +00:00
logger.info('Running peer db creation test...')
2018-03-03 22:18:57 +00:00
2018-01-10 03:50:38 +00:00
if os.path.exists('data/peers.db'):
os.remove('data/peers.db')
import core
myCore = core.Core()
myCore.createPeerDB()
if os.path.exists('data/peers.db'):
self.assertTrue(True)
else:
self.assertTrue(False)
def testPeer_b_addPeerToDB(self):
2018-03-03 22:18:57 +00:00
logger.debug('-'*26 + '\n')
2018-01-26 07:22:48 +00:00
logger.info('Running peer db insertion test...')
2018-03-03 22:18:57 +00:00
import core
myCore = core.Core()
if not os.path.exists('data/peers.db'):
myCore.createPeerDB()
2018-05-07 07:40:08 +00:00
if myCore.addPeer('6M5MXL237OK57ITHVYN5WGHANPGOMKS5C3PJLHBBNKFFJQOIDOJA====', '1cSix9Ao/yQSdo0sNif8cm2uTcYnSphb4JdZL/3WkN4=') and not myCore.addPeer('NFXHMYLMNFSAU===', '1cSix9Ao/yQSdo0sNif8cm2uTcYnSphb4JdZL/3WkN4='):
self.assertTrue(True)
else:
self.assertTrue(False)
2018-01-10 03:50:38 +00:00
def testData_b_Encrypt(self):
self.assertTrue(True)
return
2018-03-03 22:18:57 +00:00
logger.debug('-'*26 + '\n')
2018-01-26 07:22:48 +00:00
logger.info('Running data dir encrypt test...')
2018-03-03 22:18:57 +00:00
2018-01-07 08:55:44 +00:00
import core
2018-01-08 09:25:32 +00:00
myCore = core.Core()
myCore.dataDirEncrypt('password')
if os.path.exists('data-encrypted.dat'):
self.assertTrue(True)
else:
self.assertTrue(False)
2018-01-10 03:50:38 +00:00
def testData_a_Decrypt(self):
self.assertTrue(True)
return
2018-03-03 22:18:57 +00:00
logger.debug('-'*26 + '\n')
2018-01-26 07:22:48 +00:00
logger.info('Running data dir decrypt test...')
2018-03-03 22:18:57 +00:00
2018-01-08 09:25:32 +00:00
import core
myCore = core.Core()
myCore.dataDirDecrypt('password')
2018-01-10 03:50:38 +00:00
if os.path.exists('data/'):
2018-01-08 09:25:32 +00:00
self.assertTrue(True)
else:
self.assertTrue(False)
2018-03-03 22:18:57 +00:00
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)
2018-05-19 22:21:35 +00:00
def testBlockAPI(self):
self.assertTrue(True); return
2018-05-19 22:21:35 +00:00
logger.debug('-'*26 + '\n')
logger.info('Running BlockAPI test #1...')
2018-05-19 22:21:35 +00:00
content = 'Onionr test block'
from onionrblockapi import Block
2018-06-20 04:36:07 +00:00
hash = Block(type = 'test', content = content).save()
2018-05-19 22:21:35 +00:00
block = Block(hash) # test init
if len(Block.getBlocks(type = 'test')) == 0:
logger.warn('Failed to find test block.')
self.assertTrue(False)
if not block.getContent() == content:
logger.warn('Test block content is invalid! (%s != %s)' % (block.getContent(), content))
self.assertTrue(False)
2018-06-05 02:26:04 +00:00
logger.debug('-'*26 + '\n')
logger.info('Running BlockAPI test #2...')
2018-06-05 02:26:04 +00:00
original_content = 'onionr'
2018-06-05 02:26:04 +00:00
logger.debug('original: %s' % original_content)
blocks = Block.createChain(data = original_content, chunksize = 2, verbose = True)
logger.debug(blocks[1])
child = blocks[0]
merged = Block.mergeChain(child)
logger.debug('merged blocks (child: %s): %s' % (child, merged))
if merged != original_content:
self.assertTrue(False)
self.assertTrue(True)
2018-05-19 22:21:35 +00:00
2018-03-03 22:18:57 +00:00
def testPluginReload(self):
logger.debug('-'*26 + '\n')
logger.info('Running simple plugin reload test...')
2018-04-21 01:26:46 +00:00
import onionrplugins, os
if not onionrplugins.exists('test'):
os.makedirs(onionrplugins.get_plugins_folder('test'))
with open(onionrplugins.get_plugins_folder('test') + '/main.py', 'a') as main:
2018-04-21 03:10:50 +00:00
main.write("print('Running')\n\ndef on_test(pluginapi, data = None):\n print('received test event!')\n return True\n\ndef on_start(pluginapi, data = None):\n print('start event called')\n\ndef on_stop(pluginapi, data = None):\n print('stop event called')\n\ndef on_enable(pluginapi, data = None):\n print('enable event called')\n\ndef on_disable(pluginapi, data = None):\n print('disable event called')\n")
2018-04-21 01:26:46 +00:00
onionrplugins.enable('test')
2018-03-03 07:10:27 +00:00
try:
onionrplugins.reload('test')
self.assertTrue(True)
except:
self.assertTrue(False)
2018-03-03 22:18:57 +00:00
def testPluginStopStart(self):
logger.debug('-'*26 + '\n')
logger.info('Running simple plugin restart test...')
2018-04-21 01:26:46 +00:00
import onionrplugins, os
if not onionrplugins.exists('test'):
os.makedirs(onionrplugins.get_plugins_folder('test'))
with open(onionrplugins.get_plugins_folder('test') + '/main.py', 'a') as main:
2018-04-21 03:10:50 +00:00
main.write("print('Running')\n\ndef on_test(pluginapi, data = None):\n print('received test event!')\n return True\n\ndef on_start(pluginapi, data = None):\n print('start event called')\n\ndef on_stop(pluginapi, data = None):\n print('stop event called')\n\ndef on_enable(pluginapi, data = None):\n print('enable event called')\n\ndef on_disable(pluginapi, data = None):\n print('disable event called')\n")
2018-04-21 01:26:46 +00:00
onionrplugins.enable('test')
2018-03-03 22:18:57 +00:00
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...')
2018-04-21 01:26:46 +00:00
import onionrplugins as plugins, onionrevents as events, os
if not plugins.exists('test'):
os.makedirs(plugins.get_plugins_folder('test'))
with open(plugins.get_plugins_folder('test') + '/main.py', 'a') as main:
main.write("print('Running')\n\ndef on_test(pluginapi, data = None):\n print('received test event!')\n print('thread test started...')\n import time\n time.sleep(1)\n \n return True\n\ndef on_start(pluginapi, data = None):\n print('start event called')\n\ndef on_stop(pluginapi, data = None):\n print('stop event called')\n\ndef on_enable(pluginapi, data = None):\n print('enable event called')\n\ndef on_disable(pluginapi, data = None):\n print('disable event called')\n")
2018-04-21 01:26:46 +00:00
plugins.enable('test')
2018-03-03 22:18:57 +00:00
plugins.start('test')
if not events.call(plugins.get_plugin('test'), 'enable'):
2018-03-03 22:18:57 +00:00
self.assertTrue(False)
logger.debug('preparing to start thread', timestamp = False)
thread = events.event('test', data = {'tests': self})
logger.debug('thread running...', timestamp = False)
thread.join()
logger.debug('thread finished.', timestamp = False)
2018-03-03 22:18:57 +00:00
self.assertTrue(True)
def testQueue(self):
2018-03-03 22:18:57 +00:00
logger.debug('-'*26 + '\n')
2018-01-26 07:22:48 +00:00
logger.info('Running daemon queue test...')
2018-03-03 22:18:57 +00:00
# test if the daemon queue can read/write data
import core
myCore = core.Core()
2018-01-10 03:50:38 +00:00
if not os.path.exists('data/queue.db'):
myCore.daemonQueue()
while True:
command = myCore.daemonQueue()
if command == False:
2018-01-27 00:52:20 +00:00
logger.debug('The queue is empty (false)')
break
else:
2018-01-27 00:52:20 +00:00
logger.debug(command[0])
myCore.daemonQueueAdd('testCommand', 'testData')
command = myCore.daemonQueue()
if command[0] == 'testCommand':
if myCore.daemonQueue() == False:
2018-01-27 00:52:20 +00:00
logger.info('Succesfully added and read command')
def testHashValidation(self):
2018-03-03 22:18:57 +00:00
logger.debug('-'*26 + '\n')
logger.info('Running hash validation test...')
2018-03-03 22:18:57 +00:00
import core
myCore = core.Core()
if not myCore._utils.validateHash("$324dfgfdg") and myCore._utils.validateHash("f2ca1bb6c7e907d06dafe4687e579fce76b37e4e93b7605022da52e6ccc26fd2") and not myCore._utils.validateHash("f2ca1bb6c7e907d06dafe4687e579fce76b37e4e93b7605022da52e6ccc26fd$"):
self.assertTrue(True)
else:
self.assertTrue(False)
2018-02-27 21:23:49 +00:00
def testAddAdder(self):
2018-03-03 22:18:57 +00:00
logger.debug('-'*26 + '\n')
2018-02-27 21:23:49 +00:00
logger.info('Running address add+remove test')
2018-03-03 22:18:57 +00:00
2018-02-27 21:23:49 +00:00
import core
myCore = core.Core()
2018-02-28 00:18:48 +00:00
if not os.path.exists('data/address.db'):
myCore.createAddressDB()
2018-02-27 21:23:49 +00:00
if myCore.addAddress('facebookcorewwwi.onion') and not myCore.removeAddress('invalid'):
if myCore.removeAddress('facebookcorewwwi.onion'):
self.assertTrue(True)
else:
self.assertTrue(False)
else:
self.assertTrue(False) # <- annoying :(
def testCrypto(self):
logger.info('running cryptotests')
if os.system('python3 cryptotests.py') == 0:
self.assertTrue(True)
else:
self.assertTrue(False)
2018-03-03 22:18:57 +00:00
unittest.main()