more work on tests

master
Kevin Froman 2019-02-14 21:28:41 -06:00
parent 398d8da347
commit 4afff79d2f
4 changed files with 15 additions and 49 deletions

View File

@ -11,6 +11,7 @@
(***pre-alpha & experimental, not well tested or easy to use yet***) (***pre-alpha & experimental, not well tested or easy to use yet***)
[![Open Source Love](https://badges.frapsoft.com/os/v3/open-source.png?v=103)](https://github.com/ellerbrock/open-source-badges/) [![Open Source Love](https://badges.frapsoft.com/os/v3/open-source.png?v=103)](https://github.com/ellerbrock/open-source-badges/)
<img src='https://gitlab.com/beardog/Onionr/badges/master/build.svg'>
<hr> <hr>

View File

@ -1,35 +0,0 @@
#!/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, core, onionrcrypto, logger
class OnionrCryptoTests(unittest.TestCase):
def testSymmetric(self):
dataString = "this is a secret message"
dataBytes = dataString.encode()
myCore = core.Core()
crypto = onionrcrypto.OnionrCrypto(myCore)
key = key = b"tttttttttttttttttttttttttttttttt"
logger.info("Encrypting: " + dataString, timestamp=True)
encrypted = crypto.symmetricEncrypt(dataString, key, returnEncoded=True)
logger.info(encrypted, timestamp=True)
logger.info('Decrypting encrypted string:', timestamp=True)
decrypted = crypto.symmetricDecrypt(encrypted, key, encodedMessage=True)
logger.info(decrypted, timestamp=True)
self.assertTrue(True)
if __name__ == "__main__":
unittest.main()

View File

@ -18,7 +18,7 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
''' '''
import nacl.signing, nacl.encoding, nacl.public, nacl.hash, nacl.pwhash, nacl.utils, nacl.secret, os, binascii, base64, hashlib, logger, onionrproofs, time, math, sys, hmac import nacl.signing, nacl.encoding, nacl.public, nacl.hash, nacl.pwhash, nacl.utils, nacl.secret, os, binascii, base64, hashlib, logger, onionrproofs, time, math, sys, hmac
import onionrexceptions, keymanager import onionrexceptions, keymanager, core
# secrets module was added into standard lib in 3.6+ # secrets module was added into standard lib in 3.6+
if sys.version_info[0] == 3 and sys.version_info[1] < 6: if sys.version_info[0] == 3 and sys.version_info[1] < 6:
from dependencies import secrets from dependencies import secrets
@ -180,12 +180,6 @@ class OnionrCrypto:
decrypted = base64.b64encode(decrypted) decrypted = base64.b64encode(decrypted)
return decrypted return decrypted
def generateSymmetricPeer(self, peer):
'''Generate symmetric key for a peer and save it to the peer database'''
key = self.generateSymmetric()
self._core.setPeerInfo(peer, 'forwardKey', key)
return
def generateSymmetric(self): def generateSymmetric(self):
'''Generate a symmetric key (bytes) and return it''' '''Generate a symmetric key (bytes) and return it'''
return binascii.hexlify(nacl.utils.random(nacl.secret.SecretBox.KEY_SIZE)) return binascii.hexlify(nacl.utils.random(nacl.secret.SecretBox.KEY_SIZE))
@ -283,6 +277,15 @@ class OnionrCrypto:
@staticmethod @staticmethod
def safeCompare(one, two): def safeCompare(one, two):
# Do encode here to avoid spawning core
try:
one = one.encode()
except AttributeError:
pass
try:
two = two.encode()
except AttributeError:
pass
return hmac.compare_digest(one, two) return hmac.compare_digest(one, two)
@staticmethod @staticmethod

View File

@ -1,11 +1,10 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import sys, os import sys, os
sys.path.append(".") sys.path.append(".")
import unittest, uuid, sqlite3 import unittest, uuid
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
from urllib.request import pathname2url
import core, onionr import core, onionr
core.Core() core.Core()
@ -37,7 +36,7 @@ class OnionrValidations(unittest.TestCase):
self.assertTrue(c._utils.validatePubKey(valid)) self.assertTrue(c._utils.validatePubKey(valid))
for x in invalid: for x in invalid:
print('testing', x) #print('testing', x)
self.assertFalse(c._utils.validatePubKey(x)) self.assertFalse(c._utils.validatePubKey(x))
def test_integer_string(self): def test_integer_string(self):
@ -46,13 +45,11 @@ class OnionrValidations(unittest.TestCase):
c = core.Core() c = core.Core()
for x in valid: for x in valid:
print('testing', x) #print('testing', x)
self.assertTrue(c._utils.isIntegerString(x)) self.assertTrue(c._utils.isIntegerString(x))
for x in invalid: for x in invalid:
print('testing', x) #print('testing', x)
self.assertFalse(c._utils.isIntegerString(x)) self.assertFalse(c._utils.isIntegerString(x))
unittest.main() unittest.main()