def test_init(self): g = exercise.Generator() self.assertEqual(g._counter, 0) self.assertEqual( g._key, binascii.unhexlify( '0000000000000000000000000000000000000000000000000000000000000000' ))
def test_generate_blocks(self): g = exercise.Generator() g.reseed(b'hello') # Because the generator is deterministic, the first two blocks are the same as in `test_generate_one_block` self.assertEqual( g._generate_blocks(3), binascii.unhexlify( '178b0a056e064d1eb07238ea7402fcf583d8d7e8a9a073b5f1d93f601fdaca40cce9cac929606fd6506ff1f9b082702d' ))
def test_generate_one_block(self): g = exercise.Generator() g.reseed(b'hello') self.assertEqual( g._generate_blocks(1), binascii.unhexlify('178b0a056e064d1eb07238ea7402fcf5')) self.assertEqual( g._generate_blocks(1), binascii.unhexlify('83d8d7e8a9a073b5f1d93f601fdaca40'))
def test_generate_data(self): g = exercise.Generator() g.reseed(b'hello') # The (beginning of) the first block, as seen in `test_generate_one_block` self.assertEqual(g.generate_data(13), binascii.unhexlify('178b0a056e064d1eb07238ea74')) # After each call to `_generate_data`, a new key is generated, making this data different # from the one from the second block in `test_generate_one_block` self.assertEqual(g.generate_data(8), binascii.unhexlify('8d6aa6fe3aeda893'))
def test_reseed(self): # `reseed` increments the counter to mark the generator as seeded, and computes the new key by # hashing the old key together with the seed. g = exercise.Generator() g.reseed(binascii.unhexlify('1234')) self.assertEqual(g._counter, 1) self.assertEqual( g._key, exercise.double_sha( binascii.unhexlify( '00000000000000000000000000000000000000000000000000000000000000001234' )))
def test_generate_data_with_invalid_size(self): g = exercise.Generator() g.reseed(b'hello') self.assertRaises(Exception, g.generate_data, -1) self.assertRaises(Exception, g.generate_data, 16_777_216)
def test_generate_blocks_from_unseeded(self): g = exercise.Generator() self.assertRaises(Exception, g._generate_blocks, 1)