forked from stacks-archive/python-utilitybelt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unit_tests.py
216 lines (168 loc) · 5.74 KB
/
unit_tests.py
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# -*- coding: utf-8 -*-
"""
Utilitybelt
~~~~~
:copyright: (c) 2015 by Halfmoon Labs
:license: MIT, see LICENSE for more details.
"""
import unittest
from test import test_support
import os
import sys
import string
import binascii
from base64 import b64encode, b64decode
from utilitybelt import recursive_dict, scrub_dict, to_dict, \
recursive_dict_to_dict
from utilitybelt import int_to_charset, charset_to_int, change_charset, \
base16_chars, base58_chars, base32_chars, zbase32_chars, base64_chars
from utilitybelt import hex_to_int, int_to_hex, hex_to_charset, \
charset_to_hex, hexpad, is_hex, is_int, is_valid_int
from utilitybelt import dev_urandom_entropy, dev_random_entropy, secure_randint
class IntToCharsetTests(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def test_int_to_deadbeef(self):
i = 3735928559
reference_value = ("%x" % i).replace('0x', '')
value = int_to_charset(i, base16_chars)
self.assertEqual(value, reference_value)
def test_long_to_hex(self):
i = int("985936198705846800453632448571546073741637651923621735932"
"00564555477131708560")
reference_value = hex(i).rstrip('L').lstrip('0x')
value = int_to_charset(i, base16_chars)
self.assertEqual(value, reference_value)
class CharsetToIntTests(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def test_deadbeef_to_int(self):
s = "deadbeef"
value = charset_to_int(s, base16_chars)
reference_value = int(s, 16)
self.assertEqual(value, reference_value)
class Base16Tests(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def test_int_to_hex_with_long(self):
i = int("985936198705846800453632448571546073741637651923621735932"
"00564555477131708560")
reference_value = hex(i)
reference_value = reference_value.rstrip('L').lstrip('0x')
value = int_to_hex(i)
self.assertEqual(value, reference_value)
def test_hex_to_int_with_64bit_string(self):
s = "d9fa02e46cd3867f51279dfae592d3706022ee93c175b49c30c8c962722fc890"
reference_value = int(s, 16)
value = hex_to_int(s)
self.assertEqual(value, reference_value)
def test_is_int_with_long(self):
i = int("985936198705846800453632448571546073741637651923621735932"
"00564555477131708560")
self.assertTrue(is_int(i))
def test_is_valid_int_with_string(self):
i = ("9859361987058468004536324485715460737416376519236217359320056"
"4555477131708560")
self.assertTrue(is_valid_int(i))
def test_is_hex_with_64bit_string(self):
s = "d9fa02e46cd3867f51279dfae592d3706022ee93c175b49c30c8c962722fc890"
self.assertTrue(is_hex(s))
def test_hex_to_charset(self):
s = "d9fa02e46cd3867f51279dfae592d3706022ee93c175b49c30c8c962722fc890"
s2 = hex_to_charset(s, string.digits)
self.assertTrue(is_valid_int(s2))
def test_charset_to_hex(self):
s = ("9859361987058468004536324485715460737416376519236217359320056"
"4555477131708560")
s2 = charset_to_hex(s, string.digits)
self.assertTrue(is_hex(s2))
class ChangeCharsetTests(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
class ScrubDictTests(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def test_nested_dict(self):
d = {"a": {"b": {"c": "", "d": [{"e": ""}]}}}
d = scrub_dict(d)
self.assertEqual(len(d), 0)
class ToDictTests(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def test_class_instance_to_dict(self):
class Thing():
def __init__(self):
self.name = "My Class"
thing = Thing()
d = to_dict(thing)
self.assertTrue(isinstance(d, dict))
self.assertEqual('name' in d and d['name'], "My Class")
def test_str_to_dict(self):
s = "a"
try:
d = to_dict(a)
except:
self.assertTrue(True)
else:
self.assertTrue(False)
def test_int_to_dict(self):
i = 1
try:
d = to_dict(i)
except:
self.assertTrue(True)
else:
self.assertTrue(False)
def test_dict_to_dict(self):
d = {}
d = to_dict(d)
self.assertTrue(isinstance(d, dict))
class EntropyTests(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def test_dev_urandom_entropy(self):
bytes16 = dev_urandom_entropy(16)
self.assertEqual(len(bytes16), 16)
def test_dev_random_entropy(self):
bytes16 = dev_random_entropy(16)
self.assertEqual(len(bytes16), 16)
def test_dev_random_entropy_fallback_on_nt_operating_system(self):
os.name = 'nt'
bytes16 = dev_random_entropy(16)
self.assertEqual(len(bytes16), 16)
def test_random_integer(self):
min_value, max_value = 0, sys.maxint
r = secure_randint(min_value, max_value)
self.assertTrue(r > min_value)
self.assertTrue(r < max_value)
def test_random_integer_larger_than_maxint(self):
min_value, max_value = 0, sys.maxint*2
r = secure_randint(min_value, max_value)
self.assertTrue(r > min_value)
self.assertTrue(r < max_value)
def test_main():
test_support.run_unittest(
ToDictTests,
ScrubDictTests,
IntToCharsetTests,
CharsetToIntTests,
ChangeCharsetTests,
Base16Tests,
EntropyTests
)
if __name__ == '__main__':
test_main()