1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-01-13 17:00:59 +00:00

fix trezor.crypto.random test

This commit is contained in:
Pavol Rusnak 2016-06-02 14:51:00 +02:00
parent 9a30a13aff
commit 93f4a97470
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
2 changed files with 10 additions and 9 deletions

View File

@ -114,7 +114,6 @@ class TestCase:
def assertRaises(self, exc, func=None, *args, **kwargs): def assertRaises(self, exc, func=None, *args, **kwargs):
if func is None: if func is None:
return AssertRaisesContext(exc) return AssertRaisesContext(exc)
try: try:
func(*args, **kwargs) func(*args, **kwargs)
assert False, "%r not raised" % exc assert False, "%r not raised" % exc
@ -124,7 +123,6 @@ class TestCase:
raise raise
def skip(msg): def skip(msg):
def _decor(fun): def _decor(fun):
# We just replace original fun with _inner # We just replace original fun with _inner
@ -141,19 +139,25 @@ def skipUnless(cond, msg):
class TestSuite: class TestSuite:
def __init__(self): def __init__(self):
self.tests = [] self.tests = []
def addTest(self, cls): def addTest(self, cls):
self.tests.append(cls) self.tests.append(cls)
class TestRunner: class TestRunner:
def run(self, suite): def run(self, suite):
res = TestResult() res = TestResult()
for c in suite.tests: for c in suite.tests:
run_class(c, res) run_class(c, res)
return res return res
class TestResult: class TestResult:
def __init__(self): def __init__(self):
self.errorsNum = 0 self.errorsNum = 0
self.failuresNum = 0 self.failuresNum = 0
@ -163,7 +167,7 @@ class TestResult:
def wasSuccessful(self): def wasSuccessful(self):
return self.errorsNum == 0 and self.failuresNum == 0 return self.errorsNum == 0 and self.failuresNum == 0
# TODO: Uncompliant
def run_class(c, test_result): def run_class(c, test_result):
o = c() o = c()
set_up = getattr(o, "setUp", lambda: None) set_up = getattr(o, "setUp", lambda: None)
@ -171,7 +175,7 @@ def run_class(c, test_result):
print('class', c.__qualname__) print('class', c.__qualname__)
for name in dir(o): for name in dir(o):
if name.startswith("test"): if name.startswith("test"):
print(name, end=' ...') print(' ', name, end=' ...')
m = getattr(o, name) m = getattr(o, name)
try: try:
set_up() set_up()

View File

@ -16,8 +16,7 @@ class TestCryptoRandom(unittest.TestCase):
r = random.uniform(15) r = random.uniform(15)
c[r] += 1 c[r] += 1
for i in range(15): for i in range(15):
self.assertTrue(c[r] > 900) self.assertAlmostEqual(c[r], 1000, delta=150)
self.assertTrue(c[r] < 1100)
def test_bytes_length(self): def test_bytes_length(self):
for l in range(1024 + 1): for l in range(1024 + 1):
@ -33,9 +32,7 @@ class TestCryptoRandom(unittest.TestCase):
for h in hexlify(b): for h in hexlify(b):
c[chr(h)] += 1 c[chr(h)] += 1
for h in '0123456789abcdef': for h in '0123456789abcdef':
self.assertTrue(c[h] > 900) self.assertAlmostEqual(c[h], 1000, delta=150)
self.assertTrue(c[h] < 1100)
def test_shuffle(self): def test_shuffle(self):
for l in range(256 + 1): for l in range(256 + 1):