~ubuntu-branches/ubuntu/quantal/m2crypto/quantal

« back to all changes in this revision

Viewing changes to tests/test_rand.py

  • Committer: Bazaar Package Importer
  • Author(s): Dima Barsky
  • Date: 2007-05-24 21:14:36 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20070524211436-73w3oonappxy8k4a
Tags: 0.17-1
* New upstream release
* Acknowledge NMU (Closes: #380861)
* Changed section to python (Closes: #425875)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
"""Unit tests for M2Crypto.Rand.
 
4
 
 
5
Copyright (C) 2006 Open Source Applications Foundation (OSAF). All Rights Reserved.
 
6
"""
 
7
 
 
8
import unittest
 
9
import os, sys
 
10
from M2Crypto import Rand
 
11
 
 
12
class RandTestCase(unittest.TestCase):
 
13
    def test_bytes(self):
 
14
        self.assertRaises(MemoryError, Rand.rand_bytes, -1)
 
15
        assert Rand.rand_bytes(0) == ''
 
16
        assert len(Rand.rand_bytes(1)) == 1
 
17
        
 
18
    def test_pseudo_bytes(self):
 
19
        self.assertRaises(MemoryError, Rand.rand_pseudo_bytes, -1)
 
20
        assert Rand.rand_pseudo_bytes(0) == ('', 1)
 
21
        a, b = Rand.rand_pseudo_bytes(1)
 
22
        assert len(a) == 1
 
23
        assert b == 1
 
24
        
 
25
    def test_load_save(self):
 
26
        try:
 
27
            os.remove('tests/randpool.dat')
 
28
        except OSError:
 
29
            pass
 
30
        assert Rand.load_file('tests/randpool.dat', -1) == 0
 
31
        assert Rand.save_file('tests/randpool.dat') == 1024
 
32
        assert Rand.load_file('tests/randpool.dat', -1) == 1024
 
33
        
 
34
    def test_seed_add(self):
 
35
        if sys.version_info >= (2, 4):
 
36
            assert Rand.rand_seed(os.urandom(1024)) is None
 
37
            
 
38
            # XXX Should there be limits on the entropy parameter?
 
39
            assert Rand.rand_add(os.urandom(2), 0.5) is None
 
40
            Rand.rand_add(os.urandom(2), -0.5)
 
41
            Rand.rand_add(os.urandom(2), 5000.0)
 
42
 
 
43
        
 
44
def suite():
 
45
    suite = unittest.TestSuite()
 
46
    suite.addTest(unittest.makeSuite(RandTestCase))
 
47
    return suite
 
48
 
 
49
 
 
50
if __name__ == '__main__':
 
51
    unittest.TextTestRunner().run(suite())