OLD | NEW |
(Empty) | |
| 1 # -*- coding: utf-8 -*- |
| 2 # |
| 3 # SelfTest/Util/test_generic.py: Self-test for the Crypto.Random.new() function |
| 4 # |
| 5 # Written in 2008 by Dwayne C. Litzenberger <dlitz@dlitz.net> |
| 6 # |
| 7 # =================================================================== |
| 8 # The contents of this file are dedicated to the public domain. To |
| 9 # the extent that dedication to the public domain is not available, |
| 10 # everyone is granted a worldwide, perpetual, royalty-free, |
| 11 # non-exclusive license to exercise all rights associated with the |
| 12 # contents of this file for any purpose whatsoever. |
| 13 # No rights are reserved. |
| 14 # |
| 15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 18 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 19 # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 20 # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 21 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 # SOFTWARE. |
| 23 # =================================================================== |
| 24 |
| 25 """Self-test suite for Crypto.Random.new()""" |
| 26 |
| 27 __revision__ = "$Id$" |
| 28 |
| 29 import unittest |
| 30 import sys |
| 31 if sys.version_info[0] == 2 and sys.version_info[1] == 1: |
| 32 from Crypto.Util.py21compat import * |
| 33 from Crypto.Util.py3compat import * |
| 34 |
| 35 class SimpleTest(unittest.TestCase): |
| 36 def runTest(self): |
| 37 """Crypto.Random.new()""" |
| 38 # Import the Random module and try to use it |
| 39 from Crypto import Random |
| 40 randobj = Random.new() |
| 41 x = randobj.read(16) |
| 42 y = randobj.read(16) |
| 43 self.assertNotEqual(x, y) |
| 44 z = Random.get_random_bytes(16) |
| 45 self.assertNotEqual(x, z) |
| 46 self.assertNotEqual(y, z) |
| 47 # Test the Random.random module, which |
| 48 # implements a subset of Python's random API |
| 49 # Not implemented: |
| 50 # seed(), getstate(), setstate(), jumpahead() |
| 51 # random(), uniform(), triangular(), betavariate() |
| 52 # expovariate(), gammavariate(), gauss(), |
| 53 # longnormvariate(), normalvariate(), |
| 54 # vonmisesvariate(), paretovariate() |
| 55 # weibullvariate() |
| 56 # WichmannHill(), whseed(), SystemRandom() |
| 57 from Crypto.Random import random |
| 58 x = random.getrandbits(16*8) |
| 59 y = random.getrandbits(16*8) |
| 60 self.assertNotEqual(x, y) |
| 61 # Test randrange |
| 62 if x>y: |
| 63 start = y |
| 64 stop = x |
| 65 else: |
| 66 start = x |
| 67 stop = y |
| 68 for step in range(1,10): |
| 69 x = random.randrange(start,stop,step) |
| 70 y = random.randrange(start,stop,step) |
| 71 self.assertNotEqual(x, y) |
| 72 self.assertEqual(start <= x < stop, True) |
| 73 self.assertEqual(start <= y < stop, True) |
| 74 self.assertEqual((x - start) % step, 0) |
| 75 self.assertEqual((y - start) % step, 0) |
| 76 for i in range(10): |
| 77 self.assertEqual(random.randrange(1,2), 1) |
| 78 self.assertRaises(ValueError, random.randrange, start, start) |
| 79 self.assertRaises(ValueError, random.randrange, stop, start, step) |
| 80 self.assertRaises(TypeError, random.randrange, start, stop, step, step) |
| 81 self.assertRaises(TypeError, random.randrange, start, stop, "1") |
| 82 self.assertRaises(TypeError, random.randrange, "1", stop, step) |
| 83 self.assertRaises(TypeError, random.randrange, 1, "2", step) |
| 84 self.assertRaises(ValueError, random.randrange, start, stop, 0) |
| 85 # Test randint |
| 86 x = random.randint(start,stop) |
| 87 y = random.randint(start,stop) |
| 88 self.assertNotEqual(x, y) |
| 89 self.assertEqual(start <= x <= stop, True) |
| 90 self.assertEqual(start <= y <= stop, True) |
| 91 for i in range(10): |
| 92 self.assertEqual(random.randint(1,1), 1) |
| 93 self.assertRaises(ValueError, random.randint, stop, start) |
| 94 self.assertRaises(TypeError, random.randint, start, stop, step) |
| 95 self.assertRaises(TypeError, random.randint, "1", stop) |
| 96 self.assertRaises(TypeError, random.randint, 1, "2") |
| 97 # Test choice |
| 98 seq = range(10000) |
| 99 x = random.choice(seq) |
| 100 y = random.choice(seq) |
| 101 self.assertNotEqual(x, y) |
| 102 self.assertEqual(x in seq, True) |
| 103 self.assertEqual(y in seq, True) |
| 104 for i in range(10): |
| 105 self.assertEqual(random.choice((1,2,3)) in (1,2,3), True) |
| 106 self.assertEqual(random.choice([1,2,3]) in [1,2,3], True) |
| 107 if sys.version_info[0] is 3: |
| 108 self.assertEqual(random.choice(bytearray(b('123'))) in bytearray(b('
123')), True) |
| 109 self.assertEqual(1, random.choice([1])) |
| 110 self.assertRaises(IndexError, random.choice, []) |
| 111 self.assertRaises(TypeError, random.choice, 1) |
| 112 # Test shuffle. Lacks random parameter to specify function. |
| 113 # Make copies of seq |
| 114 seq = range(500) |
| 115 x = list(seq) |
| 116 y = list(seq) |
| 117 random.shuffle(x) |
| 118 random.shuffle(y) |
| 119 self.assertNotEqual(x, y) |
| 120 self.assertEqual(len(seq), len(x)) |
| 121 self.assertEqual(len(seq), len(y)) |
| 122 for i in range(len(seq)): |
| 123 self.assertEqual(x[i] in seq, True) |
| 124 self.assertEqual(y[i] in seq, True) |
| 125 self.assertEqual(seq[i] in x, True) |
| 126 self.assertEqual(seq[i] in y, True) |
| 127 z = [1] |
| 128 random.shuffle(z) |
| 129 self.assertEqual(z, [1]) |
| 130 if sys.version_info[0] == 3: |
| 131 z = bytearray(b('12')) |
| 132 random.shuffle(z) |
| 133 self.assertEqual(b('1') in z, True) |
| 134 self.assertRaises(TypeError, random.shuffle, b('12')) |
| 135 self.assertRaises(TypeError, random.shuffle, 1) |
| 136 self.assertRaises(TypeError, random.shuffle, "1") |
| 137 self.assertRaises(TypeError, random.shuffle, (1,2)) |
| 138 # 2to3 wraps a list() around it, alas - but I want to shoot |
| 139 # myself in the foot here! :D |
| 140 # if sys.version_info[0] == 3: |
| 141 # self.assertRaises(TypeError, random.shuffle, range(3)) |
| 142 # Test sample |
| 143 x = random.sample(seq, 20) |
| 144 y = random.sample(seq, 20) |
| 145 self.assertNotEqual(x, y) |
| 146 for i in range(20): |
| 147 self.assertEqual(x[i] in seq, True) |
| 148 self.assertEqual(y[i] in seq, True) |
| 149 z = random.sample([1], 1) |
| 150 self.assertEqual(z, [1]) |
| 151 z = random.sample((1,2,3), 1) |
| 152 self.assertEqual(z[0] in (1,2,3), True) |
| 153 z = random.sample("123", 1) |
| 154 self.assertEqual(z[0] in "123", True) |
| 155 z = random.sample(range(3), 1) |
| 156 self.assertEqual(z[0] in range(3), True) |
| 157 if sys.version_info[0] == 3: |
| 158 z = random.sample(b("123"), 1) |
| 159 self.assertEqual(z[0] in b("123"), True) |
| 160 z = random.sample(bytearray(b("123")), 1) |
| 161 self.assertEqual(z[0] in bytearray(b("123")), True) |
| 162 self.assertRaises(TypeError, random.sample, 1) |
| 163 |
| 164 def get_tests(config={}): |
| 165 return [SimpleTest()] |
| 166 |
| 167 if __name__ == '__main__': |
| 168 suite = lambda: unittest.TestSuite(get_tests()) |
| 169 unittest.main(defaultTest='suite') |
| 170 |
| 171 # vim:set ts=4 sw=4 sts=4 expandtab: |
OLD | NEW |