2019-02-14 23:48:41 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import sys, os
|
|
|
|
sys.path.append(".")
|
2019-09-13 02:22:25 +00:00
|
|
|
sys.path.append("onionr/")
|
2019-02-15 03:28:41 +00:00
|
|
|
import unittest, uuid
|
2019-02-14 23:48:41 +00:00
|
|
|
TEST_DIR = 'testdata/%s-%s' % (uuid.uuid4(), os.path.basename(__file__)) + '/'
|
|
|
|
print("Test directory:", TEST_DIR)
|
|
|
|
os.environ["ONIONR_HOME"] = TEST_DIR
|
2019-06-23 17:41:07 +00:00
|
|
|
from onionrutils import stringvalidators
|
2019-02-14 23:48:41 +00:00
|
|
|
|
|
|
|
class OnionrValidations(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_peer_validator(self):
|
|
|
|
# Test hidden service domain validities
|
|
|
|
valid = ['facebookcorewwwi.onion', 'vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd.onion',
|
|
|
|
'5bvb5ncnfr4dlsfriwczpzcvo65kn7fnnlnt2ln7qvhzna2xaldq.b32.i2p']
|
|
|
|
|
2019-07-26 05:55:20 +00:00
|
|
|
invalid = [None, 'dsfewjirji0ejipdfs', '', ' ', '\n', '\r\n', 'f$ce%^okc+rewwwi.onion', 'facebookc0rewwi.onion']
|
2019-02-14 23:48:41 +00:00
|
|
|
|
|
|
|
for x in valid:
|
|
|
|
print('testing', x)
|
2019-06-23 17:41:07 +00:00
|
|
|
self.assertTrue(stringvalidators.validate_transport(x))
|
2019-02-14 23:48:41 +00:00
|
|
|
|
|
|
|
for x in invalid:
|
|
|
|
print('testing', x)
|
2019-06-23 17:41:07 +00:00
|
|
|
self.assertFalse(stringvalidators.validate_transport(x))
|
2019-02-14 23:48:41 +00:00
|
|
|
|
2019-06-28 05:00:38 +00:00
|
|
|
def test_hash_validator(self):
|
|
|
|
valid = ['00003b3813a166e706e490238e9515633cc3d083efe982a67753d50d87a00c96\n', '00003b3813a166e706e490238e9515633cc3d083efe982a67753d50d87a00c96', b'00003b3813a166e706e490238e9515633cc3d083efe982a67753d50d87a00c96',
|
|
|
|
'00003b3813a166e706e490238e9515633cc36', b'00003b3813a166e706e490238e9515633cc3d083']
|
|
|
|
invalid = [None, 0, 1, True, False, '%#W483242#', '00003b3813a166e706e490238e9515633cc3d083efe982a67753d50d87a00c9666', '', b'',
|
|
|
|
b'00003b3813a166e706e490238e9515633cc3d083efe982a67753d50d87a00c9666666', b' ', '\n', '00003b3813a166e706e490238e9515633cc3d083efe982a67753d50d87a00ccccc\n']
|
|
|
|
|
|
|
|
for x in valid:
|
|
|
|
self.assertTrue(stringvalidators.validate_hash(x))
|
|
|
|
for x in invalid:
|
|
|
|
try:
|
|
|
|
result = stringvalidators.validate_hash(x)
|
|
|
|
print('testing', x, result)
|
|
|
|
except AttributeError:
|
|
|
|
result = False
|
|
|
|
try:
|
|
|
|
self.assertFalse(result)
|
|
|
|
except AssertionError:
|
|
|
|
raise AssertionError("%s returned true" % (x,))
|
|
|
|
|
2019-02-14 23:48:41 +00:00
|
|
|
def test_pubkey_validator(self):
|
|
|
|
# Test ed25519 public key validity
|
2019-06-19 06:57:13 +00:00
|
|
|
valids = ['JZ5VE72GUS3C7BOHDRIYZX4B5U5EJMCMLKHLYCVBQQF3UKHYIRRQ====', 'JZ5VE72GUS3C7BOHDRIYZX4B5U5EJMCMLKHLYCVBQQF3UKHYIRRQ']
|
2019-02-14 23:48:41 +00:00
|
|
|
invalid = [None, '', ' ', 'dfsg', '\n', 'JZ5VE72GUS3C7BOHDRIYZX4B5U5EJMCMLKHLYCVBQQF3UKHYIR$Q====']
|
2019-06-19 06:57:13 +00:00
|
|
|
|
|
|
|
for valid in valids:
|
|
|
|
print('testing', valid)
|
2019-06-26 00:32:44 +00:00
|
|
|
self.assertTrue(stringvalidators.validate_pub_key(valid))
|
2019-02-14 23:48:41 +00:00
|
|
|
|
|
|
|
for x in invalid:
|
2019-02-15 03:28:41 +00:00
|
|
|
#print('testing', x)
|
2019-06-26 00:32:44 +00:00
|
|
|
self.assertFalse(stringvalidators.validate_pub_key(x))
|
2019-02-14 23:48:41 +00:00
|
|
|
|
|
|
|
def test_integer_string(self):
|
|
|
|
valid = ["1", "100", 100, "-5", -5]
|
|
|
|
invalid = ['test', "1d3434", "1e100", None]
|
|
|
|
|
|
|
|
for x in valid:
|
2019-02-15 03:28:41 +00:00
|
|
|
#print('testing', x)
|
2019-06-23 17:41:07 +00:00
|
|
|
self.assertTrue(stringvalidators.is_integer_string(x))
|
2019-02-14 23:48:41 +00:00
|
|
|
|
|
|
|
for x in invalid:
|
2019-02-15 03:28:41 +00:00
|
|
|
#print('testing', x)
|
2019-06-23 17:41:07 +00:00
|
|
|
self.assertFalse(stringvalidators.is_integer_string(x))
|
2019-02-14 23:48:41 +00:00
|
|
|
|
2019-09-13 02:22:25 +00:00
|
|
|
unittest.main()
|