1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-22 15:38:11 +00:00

add unit tests for trezor.crypto.random

This commit is contained in:
Pavol Rusnak 2016-06-02 14:32:12 +02:00
parent 9232c77529
commit 154184e4e2
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
3 changed files with 51 additions and 1 deletions

View File

@ -56,6 +56,9 @@ void random_buffer(uint8_t *buf, size_t len)
void random_permute(void *buf, size_t size, size_t count)
{
if (count < 1 || size < 1) {
return;
}
uint8_t *d = (uint8_t *)buf;
uint8_t t[size];
for (size_t i = count - 1; i >= 1; i--) {

View File

@ -0,0 +1,48 @@
import sys
sys.path.append('..')
sys.path.append('../lib')
import unittest
from ubinascii import hexlify
from trezor.crypto import random
class TestCryptoRandom(unittest.TestCase):
def test_uniform(self):
c = {}
for i in range(15):
c[i] = 0
for _ in range(15000):
r = random.uniform(15)
c[r] += 1
for i in range(15):
self.assertTrue(c[r] > 900)
self.assertTrue(c[r] < 1100)
def test_bytes_length(self):
for l in range(1024 + 1):
lst = random.bytes(l)
self.assertEqual(len(lst), l)
def test_bytes_uniform(self):
for _ in range(100):
b = random.bytes(8000)
c = {}
for h in '0123456789abcdef':
c[h] = 0
for h in hexlify(b):
c[chr(h)] += 1
for h in '0123456789abcdef':
self.assertTrue(c[h] > 900)
self.assertTrue(c[h] < 1100)
def test_shuffle(self):
for l in range(256 + 1):
lst = list(range(l))
random.shuffle(lst)
self.assertEqual(len(lst), l)
self.assertEqual(sorted(lst), list(range(l)))
if __name__ == '__main__':
unittest.main()

View File

@ -2,7 +2,6 @@ import sys
sys.path.append('..')
sys.path.append('../lib')
import unittest
import trezor.utils
from ubinascii import unhexlify
from trezor.crypto import hashlib