def test_flush(self): p = exercise.EntropyPool() p.add_event(17, 9, b'hello') p.add_event(2, 9, b' world') seed = p._pools[9].flush() self.assertEquals(seed, exercise.double_sha(b'\x11\x05hello\x02\x06 world')) self.assertEquals(p._pools[9].length, 0)
def test_add_event(self): p = exercise.EntropyPool() p.add_event(17, 9, b'hello') # 1 byte for source id + 1 byte for length + 5 bytes of random data self.assertEqual(p._pools[9].length, 7) p.add_event(2, 9, b' world') # previous size + 1 byte for source id + 1 byte for length + 6 bytes of random data self.assertEquals(p._pools[9].length, 15)
def rate_limit_seeds(self): p = exercise.EntropyPool() p.add_event(1, 0, b'abcdefghijklmnopqrstuvwxyz') p.add_event(2, 0, b'ABCDEFGHIJKLMNOPQRSTUVWXYZ') p.add_event(3, 0, b'0123456789') p.maybe_seed() p.add_event(4, 0, b'abcdefghijklmnopqrstuvwxyz') p.add_event(5, 0, b'ABCDEFGHIJKLMNOPQRSTUVWXYZ') p.add_event(6, 0, b'0123456789') # Should not reseed because less than 100 ms elapsed since last reseeding self.assertEquals(p.maybe_seed(), None)
def test_return_just_the_first_pool_on_first_reseed(self): p = exercise.EntropyPool() p.add_event(1, 0, b'abcdefghijklmnopqrstuvwxyz') p.add_event(2, 0, b'ABCDEFGHIJKLMNOPQRSTUVWXYZ') p.add_event(3, 0, b'0123456789') p.add_event(1, 1, b'will not get used') p.add_event(1, 2, b'will not get used 2') p.add_event(1, 3, b'will not get used 3') self.assertEquals( p.maybe_seed(), exercise.double_sha( b'\x01\x1aabcdefghijklmnopqrstuvwxyz\x02\x1aABCDEFGHIJKLMNOPQRSTUVWXYZ\x03\x0a0123456789' ))
def test_return_from_two_pools_second_reseed(self): p = exercise.EntropyPool() p.add_event(1, 0, b'abcdefghijklmnopqrstuvwxyz') p.add_event(2, 0, b'ABCDEFGHIJKLMNOPQRSTUVWXYZ') p.add_event(3, 0, b'0123456789') p.add_event(1, 1, b'will get used') p.add_event(1, 2, b'will not get used') p.add_event(1, 3, b'will not get used 2') p._reseed_count = 1 self.assertEquals( p.maybe_seed(), exercise.double_sha( b'\x01\x1aabcdefghijklmnopqrstuvwxyz\x02\x1aABCDEFGHIJKLMNOPQRSTUVWXYZ\x03\x0a0123456789' ) + exercise.double_sha(b'\x01\x0dwill get used'))
def test_init(self): p = exercise.EntropyPool() self.assertEqual(len(p._pools), 32) self.assertLess(p._last_reseed, time.time() - 0.1) self.assertEqual(p.get_reseed_count(), 0)
def test_no_seed_if_pool_too_small(self): p = exercise.EntropyPool() p.add_event(1, 0, b'just a bit of data') self.assertEquals(p.maybe_seed(), None)