1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
import sys
import random
import os.path
from openid import cryptutil
# Most of the purpose of this test is to make sure that cryptutil can
# find a good source of randomness on this machine.
def test_cryptrand():
# It's possible, but HIGHLY unlikely that a correct implementation
# will fail by returning the same number twice
s = cryptutil.getBytes(32)
t = cryptutil.getBytes(32)
assert len(s) == 32
assert len(t) == 32
assert s != t
a = cryptutil.randrange(2 ** 128)
b = cryptutil.randrange(2 ** 128)
assert type(a) is int
assert type(b) is int
assert b != a
# Make sure that we can generate random numbers that are larger
# than platform int size
cryptutil.randrange(int(sys.maxsize) + 1)
def test_reversed():
if hasattr(cryptutil, 'reversed'):
cases = [
('', ''),
('a', 'a'),
('ab', 'ba'),
('abc', 'cba'),
('abcdefg', 'gfedcba'),
([], []),
([1], [1]),
([1,2], [2,1]),
([1,2,3], [3,2,1]),
(list(range(1000)), list(range(999, -1, -1))),
]
for case, expected in cases:
expected = list(expected)
actual = list(cryptutil.reversed(case))
assert actual == expected, (case, expected, actual)
twice = list(cryptutil.reversed(actual))
assert twice == list(case), (actual, case, twice)
def test_binaryLongConvert():
MAX = sys.maxsize
for iteration in range(500):
n = 0
for i in range(10):
n += int(random.randrange(MAX))
s = cryptutil.longToBinary(n)
assert isinstance(s, bytes)
n_prime = cryptutil.binaryToLong(s)
assert n == n_prime, (n, n_prime)
cases = [
(b'\x00', 0),
(b'\x01', 1),
(b'\x7F', 127),
(b'\x00\xFF', 255),
(b'\x00\x80', 128),
(b'\x00\x81', 129),
(b'\x00\x80\x00', 32768),
(b'OpenID is cool', 1611215304203901150134421257416556)
]
for s, n in cases:
n_prime = cryptutil.binaryToLong(s)
s_prime = cryptutil.longToBinary(n)
assert n == n_prime, (s, n, n_prime)
assert s == s_prime, (n, s, s_prime)
def test_longToBase64():
f = open(os.path.join(os.path.dirname(__file__), 'n2b64'))
try:
for line in f:
parts = line.strip().split(' ')
p0 = bytes(parts[0], encoding="utf-8")
p1 = cryptutil.longToBase64(int(parts[1]))
assert p0 == p1, (p0, p1, parts)
finally:
f.close()
def test_base64ToLong():
f = open(os.path.join(os.path.dirname(__file__), 'n2b64'))
try:
for line in f:
parts = line.strip().split(' ')
assert int(parts[1]) == cryptutil.base64ToLong(parts[0])
finally:
f.close()
def test():
test_reversed()
test_binaryLongConvert()
test_cryptrand()
test_longToBase64()
test_base64ToLong()
if __name__ == '__main__':
test()
|