Initial import to Tizen
[profile/ivi/python-twisted.git] / twisted / test / test_randbytes.py
1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
3
4 """
5 Test cases for L{twisted.python.randbytes}.
6 """
7
8 import os
9
10 from twisted.trial import unittest
11 from twisted.python import randbytes
12
13
14
15 class SecureRandomTestCaseBase(object):
16     """
17     Base class for secureRandom test cases.
18     """
19
20     def _check(self, source):
21         """
22         The given random bytes source should return the number of bytes
23         requested each time it is called and should probably not return the
24         same bytes on two consecutive calls (although this is a perfectly
25         legitimate occurrence and rejecting it may generate a spurious failure
26         -- maybe we'll get lucky and the heat death with come first).
27         """
28         for nbytes in range(17, 25):
29             s = source(nbytes)
30             self.assertEqual(len(s), nbytes)
31             s2 = source(nbytes)
32             self.assertEqual(len(s2), nbytes)
33             # This is crude but hey
34             self.assertNotEquals(s2, s)
35
36
37
38 class SecureRandomTestCase(SecureRandomTestCaseBase, unittest.TestCase):
39     """
40     Test secureRandom under normal conditions.
41     """
42
43     def test_normal(self):
44         """
45         L{randbytes.secureRandom} should return a string of the requested
46         length and make some effort to make its result otherwise unpredictable.
47         """
48         self._check(randbytes.secureRandom)
49
50
51
52 class ConditionalSecureRandomTestCase(SecureRandomTestCaseBase,
53                                       unittest.TestCase):
54     """
55     Test random sources one by one, then remove it to.
56     """
57
58     def setUp(self):
59         """
60         Create a L{randbytes.RandomFactory} to use in the tests.
61         """
62         self.factory = randbytes.RandomFactory()
63
64
65     def errorFactory(self, nbytes):
66         """
67         A factory raising an error when a source is not available.
68         """
69         raise randbytes.SourceNotAvailable()
70
71
72     def test_osUrandom(self):
73         """
74         L{RandomFactory._osUrandom} should work as a random source whenever
75         L{os.urandom} is available.
76         """
77         self._check(self.factory._osUrandom)
78
79
80     def test_withoutAnything(self):
81         """
82         Remove all secure sources and assert it raises a failure. Then try the
83         fallback parameter.
84         """
85         self.factory._osUrandom = self.errorFactory
86         self.assertRaises(randbytes.SecureRandomNotAvailable,
87                           self.factory.secureRandom, 18)
88         def wrapper():
89             return self.factory.secureRandom(18, fallback=True)
90         s = self.assertWarns(
91             RuntimeWarning,
92             "urandom unavailable - "
93             "proceeding with non-cryptographically secure random source",
94             __file__,
95             wrapper)
96         self.assertEqual(len(s), 18)
97
98
99
100 class RandomTestCaseBase(SecureRandomTestCaseBase, unittest.TestCase):
101     """
102     'Normal' random test cases.
103     """
104
105     def test_normal(self):
106         """
107         Test basic case.
108         """
109         self._check(randbytes.insecureRandom)
110
111
112     def test_withoutGetrandbits(self):
113         """
114         Test C{insecureRandom} without C{random.getrandbits}.
115         """
116         factory = randbytes.RandomFactory()
117         factory.getrandbits = None
118         self._check(factory.insecureRandom)
119