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
116
117
118
|
"""Test `openid.cryptutil` module."""
from __future__ import unicode_literals
import os.path
import random
import sys
import unittest
import warnings
import six
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.
class TestLongBinary(unittest.TestCase):
"""Test `longToBinary` and `binaryToLong` functions."""
def test_binaryLongConvert(self):
MAX = sys.maxsize
with warnings.catch_warnings():
warnings.simplefilter('ignore', category=DeprecationWarning)
for iteration in range(500):
n = 0
for i in range(10):
n += random.randrange(MAX)
s = cryptutil.longToBinary(n)
assert isinstance(s, six.binary_type)
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)
]
with warnings.catch_warnings():
warnings.simplefilter('ignore', category=DeprecationWarning)
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)
class TestFixBtwoc(unittest.TestCase):
"""Test `fix_btwoc` function."""
cases = (
(b'\x00', b'\x00'),
(b'\x01', b'\x01'),
(b'\x7F', b'\x7F'),
(b'\x80', b'\x00\x80'),
(b'\xFF', b'\x00\xFF'),
)
def test_bytes(self):
for value, output in self.cases:
self.assertEqual(cryptutil.fix_btwoc(value), output)
def test_bytearray(self):
for value, output in self.cases:
self.assertEqual(cryptutil.fix_btwoc(bytearray(value)), output)
class TestBytesIntConversion(unittest.TestCase):
"""Test bytes <-> int conversions."""
# Examples from http://openid.net/specs/openid-authentication-2_0.html#btwoc
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)
]
def test_conversions(self):
for string, number in self.cases:
self.assertEqual(cryptutil.bytes_to_int(string), number)
self.assertEqual(cryptutil.int_to_bytes(number), string)
class TestLongToBase64(unittest.TestCase):
"""Test `longToBase64` function."""
def test_longToBase64(self):
f = open(os.path.join(os.path.dirname(__file__), 'n2b64'))
try:
for line in f:
parts = line.strip().split(' ')
assert parts[0] == cryptutil.longToBase64(int(parts[1]))
finally:
f.close()
class TestBase64ToLong(unittest.TestCase):
"""Test `Base64ToLong` function."""
def test_base64ToLong(self):
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()
|