1 # -*- coding: utf-8 -*-
3 # SelfTest/Random/Fortuna/test_FortunaGenerator.py: Self-test for the FortunaGenerator module
5 # =======================================================================
6 # Copyright (C) 2008 Dwayne C. Litzenberger <dlitz@dlitz.net>
8 # Permission is hereby granted, free of charge, to any person obtaining
9 # a copy of this software and associated documentation files (the
10 # "Software"), to deal in the Software without restriction, including
11 # without limitation the rights to use, copy, modify, merge, publish,
12 # distribute, sublicense, and/or sell copies of the Software, and to
13 # permit persons to whom the Software is furnished to do so.
15 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 # =======================================================================
29 """Self-tests for CryptoPlus.Random.Fortuna.FortunaGenerator"""
33 from CryptoPlus
.Util
.python_compat
import *
36 from binascii
import b2a_hex
38 class FortunaGeneratorTests(unittest
.TestCase
):
40 global FortunaGenerator
41 from CryptoPlus
.Random
.Fortuna
import FortunaGenerator
43 def test_generator(self
):
44 """FortunaGenerator.AESGenerator"""
45 fg
= FortunaGenerator
.AESGenerator()
47 # We shouldn't be able to read data until we've seeded the generator
48 self
.assertRaises(Exception, fg
.pseudo_random_data
, 1)
49 self
.assertEqual(0, fg
.counter
.get_value())
51 # Seed the generator, which should set the key and increment the counter.
53 self
.assertEqual("0ea6919d4361551364242a4ba890f8f073676e82cf1a52bb880f7e496648b565", b2a_hex(fg
.key
))
54 self
.assertEqual(1, fg
.counter
.get_value())
56 # Read 2 full blocks from the generator
57 self
.assertEqual("7cbe2c17684ac223d08969ee8b565616" + # counter=1
58 "717661c0d2f4758bd6ba140bf3791abd", # counter=2
59 b2a_hex(fg
.pseudo_random_data(32)))
61 # Meanwhile, the generator will have re-keyed itself and incremented its counter
62 self
.assertEqual("33a1bb21987859caf2bbfc5615bef56d" + # counter=3
63 "e6b71ff9f37112d0c193a135160862b7", # counter=4
65 self
.assertEqual(5, fg
.counter
.get_value())
67 # Read another 2 blocks from the generator
68 self
.assertEqual("fd6648ba3086e919cee34904ef09a7ff" + # counter=5
69 "021f77580558b8c3e9248275f23042bf", # counter=6
70 b2a_hex(fg
.pseudo_random_data(32)))
73 # Try to read more than 2**20 bytes using the internal function. This should fail.
74 self
.assertRaises(AssertionError, fg
._pseudo
_random
_data
, 2**20+1)
77 from CryptoPlus
.SelfTest
.st_common
import list_test_cases
78 return list_test_cases(FortunaGeneratorTests
)
80 if __name__
== '__main__':
81 suite
= lambda: unittest
.TestSuite(get_tests())
82 unittest
.main(defaultTest
='suite')
84 # vim:set ts=4 sw=4 sts=4 expandtab: