Premiere version : mise en route du suivi.
[auf_roundup.git] / test / test_anypy_hashlib.py
1 #! /usr/bin/env python
2 import unittest
3 import warnings
4
5 import roundup.anypy.hashlib_
6
7 class UntestableWarning(Warning):
8 pass
9
10 # suppress deprecation warnings; -> warnings.filters[0]:
11 warnings.simplefilter(action='ignore',
12 category=DeprecationWarning,
13 append=0)
14
15 try:
16 import sha
17 except:
18 warnings.warn('sha module functions', UntestableWarning)
19 sha = None
20
21 try:
22 import md5
23 except:
24 warnings.warn('md5 module functions', UntestableWarning)
25 md5 = None
26
27 try:
28 import hashlib
29 except:
30 warnings.warn('hashlib module functions', UntestableWarning)
31 hashlib = None
32
33 # preserve other warning filters set elsewhere:
34 del warnings.filters[0]
35
36 if not ((sha or md5) and hashlib):
37 warnings.warn('anypy.hashlib_ continuity', UntestableWarning)
38
39 class TestCase_anypy_hashlib(unittest.TestCase):
40 """test the hashlib compatibility layer"""
41
42 data_for_test = (
43 ('',
44 'da39a3ee5e6b4b0d3255bfef95601890afd80709',
45 'd41d8cd98f00b204e9800998ecf8427e'),
46 ('Strange women lying in ponds distributing swords'
47 ' is no basis for a system of government.',
48 'da9b2b00466b00411038c057681fe67349f92d7d',
49 'b71c5178d316ec446c25386f4857d4f9'),
50 ('Ottos Mops hopst fort',
51 'fdf7e6c54cf07108c86edd8d47c90450671c2c81',
52 'a3dce74bee59ee92f1038263e5252500'),
53 ('Dieser Satz kein Verb',
54 '3030aded8a079b92043a39dc044a35443959dcdd',
55 '2f20c69d514228011fb0d32e14dd5d80'),
56 )
57
58 # the following two are always excecuted:
59 def test_sha1_expected_anypy(self):
60 """...anypy.hashlib_.sha1().hexdigest() yields expected results"""
61 for src, SHA, MD5 in self.data_for_test:
62 self.assertEqual(roundup.anypy.hashlib_.sha1(src).hexdigest(), SHA)
63
64 def test_md5_expected_anypy(self):
65 """...anypy.hashlib_.md5().hexdigest() yields expected results"""
66 for src, SHA, MD5 in self.data_for_test:
67 self.assertEqual(roundup.anypy.hashlib_.md5(src).hexdigest(), MD5)
68
69 # execution depending on availability of modules:
70 if md5 and hashlib:
71 def test_md5_continuity(self):
72 """md5.md5().digest() == hashlib.md5().digest()"""
73 if md5.md5 is hashlib.md5:
74 return
75 else:
76 for s, i1, i2 in self.data_for_test:
77 self.assertEqual(md5.md5(s).digest(),
78 hashlib.md5().digest())
79
80 if md5:
81 def test_md5_expected(self):
82 """md5.md5().hexdigest() yields expected results"""
83 for src, SHA, MD5 in self.data_for_test:
84 self.assertEqual(md5.md5(src).hexdigest(), MD5)
85
86 def test_md5_new_expected(self):
87 """md5.new is md5.md5, or at least yields expected results"""
88 if md5.new is md5.md5:
89 return
90 else:
91 for src, SHA, MD5 in self.data_for_test:
92 self.assertEqual(md5.new(src).hexdigest(), MD5)
93
94 if sha and hashlib:
95 def test_sha1_continuity(self):
96 """sha.sha().digest() == hashlib.sha1().digest()"""
97 if sha.sha is hashlib.sha1:
98 return
99 else:
100 for s in self.data_for_test:
101 self.assertEqual(sha.sha(s).digest(),
102 hashlib.sha1().digest())
103
104 if sha:
105 def test_sha_expected(self):
106 """sha.sha().hexdigest() yields expected results"""
107 for src, SHA, MD5 in self.data_for_test:
108 self.assertEqual(sha.sha(src).hexdigest(), SHA)
109
110 # fails for me with Python 2.3; unittest module bug?
111 def test_sha_new_expected(self):
112 """sha.new is sha.sha, or at least yields expected results"""
113 if sha.new is sha.sha:
114 return
115 else:
116 for src, SHA, MD5 in self.data_for_test:
117 self.assertEqual(sha.new(src).hexdigest(), SHA)
118
119 if hashlib:
120 def test_sha1_expected_hashlib(self):
121 """hashlib.sha1().hexdigest() yields expected results"""
122 for src, SHA, MD5 in self.data_for_test:
123 self.assertEqual(hashlib.sha1(src).hexdigest(), SHA)
124
125 def test_md5_expected_hashlib(self):
126 """hashlib.md5().hexdigest() yields expected results"""
127 for src, SHA, MD5 in self.data_for_test:
128 self.assertEqual(hashlib.md5(src).hexdigest(), MD5)
129
130 def test_suite():
131 suite = unittest.TestSuite()
132 suite.addTest(unittest.makeSuite(TestCase_anypy_hashlib))
133 return suite
134
135 if __name__ == '__main__':
136 runner = unittest.TextTestRunner()
137 unittest.main(testRunner=runner)
138
139 # vim: ts=8 et sts=4 sw=4 si