onionr/tests/test_stringvalidations.py

73 lines
3.1 KiB
Python
Raw Permalink Normal View History

2019-02-14 23:48:41 +00:00
#!/usr/bin/env python3
import sys, os
sys.path.append(".")
2019-11-21 09:26:23 +00:00
sys.path.append("src/")
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
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)
self.assertTrue(stringvalidators.validate_transport(x))
2019-02-14 23:48:41 +00:00
for x in invalid:
print('testing', x)
self.assertFalse(stringvalidators.validate_transport(x))
2019-02-14 23:48:41 +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
valids = ['JZ5VE72GUS3C7BOHDRIYZX4B5U5EJMCMLKHLYCVBQQF3UKHYIRRQ====', 'JZ5VE72GUS3C7BOHDRIYZX4B5U5EJMCMLKHLYCVBQQF3UKHYIRRQ']
2019-02-14 23:48:41 +00:00
invalid = [None, '', ' ', 'dfsg', '\n', 'JZ5VE72GUS3C7BOHDRIYZX4B5U5EJMCMLKHLYCVBQQF3UKHYIR$Q====']
for valid in valids:
print('testing', valid)
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)
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)
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)
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()