summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_pep247.py
diff options
context:
space:
mode:
authorBrett Cannon <bcannon@gmail.com>2008-03-18 01:00:07 (GMT)
committerBrett Cannon <bcannon@gmail.com>2008-03-18 01:00:07 (GMT)
commit6eeaddc341ec3ac03070c47cf9095a6c82d3a395 (patch)
tree095a6e07a27e4a63011e18a94a927d476442eba9 /Lib/test/test_pep247.py
parent887290d275a68754aa15a9924640faab392754b6 (diff)
downloadcpython-6eeaddc341ec3ac03070c47cf9095a6c82d3a395.zip
cpython-6eeaddc341ec3ac03070c47cf9095a6c82d3a395.tar.gz
cpython-6eeaddc341ec3ac03070c47cf9095a6c82d3a395.tar.bz2
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Diffstat (limited to 'Lib/test/test_pep247.py')
-rw-r--r--Lib/test/test_pep247.py99
1 files changed, 55 insertions, 44 deletions
diff --git a/Lib/test/test_pep247.py b/Lib/test/test_pep247.py
index 0497ee9..691c85a 100644
--- a/Lib/test/test_pep247.py
+++ b/Lib/test/test_pep247.py
@@ -1,63 +1,74 @@
-#
-# Test suite to check compliance with PEP 247, the standard API for
-# hashing algorithms.
-#
+"""
+Test suite to check compilance with PEP 247, the standard API
+for hashing algorithms
+"""
import warnings
-warnings.filterwarnings("ignore", "the md5 module is deprecated.*",
+warnings.filterwarnings('ignore', 'the md5 module is deprecated.*',
DeprecationWarning)
-warnings.filterwarnings("ignore", "the sha module is deprecated.*",
+warnings.filterwarnings('ignore', 'the sha module is deprecated.*',
DeprecationWarning)
-import md5, sha, hmac
-from test.test_support import verbose
+import hmac
+import md5
+import sha
+import unittest
+from test import test_support
-def check_hash_module(module, key=None):
- assert hasattr(module, 'digest_size'), "Must have digest_size"
- assert (module.digest_size is None or
- module.digest_size > 0), "digest_size must be None or positive"
+class Pep247Test(unittest.TestCase):
- if key is not None:
- obj1 = module.new(key)
- obj2 = module.new(key, "string")
+ def check_module(self, module, key=None):
+ self.assert_(hasattr(module, 'digest_size'))
+ self.assert_(module.digest_size is None or module.digest_size > 0)
- h1 = module.new(key, "string").digest()
- obj3 = module.new(key) ; obj3.update("string") ; h2 = obj3.digest()
- assert h1 == h2, "Hashes must match"
+ if not key is None:
+ obj1 = module.new(key)
+ obj2 = module.new(key, 'string')
- else:
- obj1 = module.new()
- obj2 = module.new("string")
+ h1 = module.new(key, 'string').digest()
+ obj3 = module.new(key)
+ obj3.update('string')
+ h2 = obj3.digest()
+ else:
+ obj1 = module.new()
+ obj2 = module.new('string')
- h1 = module.new("string").digest()
- obj3 = module.new() ; obj3.update("string") ; h2 = obj3.digest()
- assert h1 == h2, "Hashes must match"
+ h1 = module.new('string').digest()
+ obj3 = module.new()
+ obj3.update('string')
+ h2 = obj3.digest()
- assert hasattr(obj1, 'digest_size'), "Objects must have digest_size attr"
- if module.digest_size is not None:
- assert obj1.digest_size == module.digest_size, "digest_size must match"
- assert obj1.digest_size == len(h1), "digest_size must match actual size"
- obj1.update("string")
- obj_copy = obj1.copy()
- assert obj1.digest() == obj_copy.digest(), "Copied objects must match"
- assert obj1.hexdigest() == obj_copy.hexdigest(), \
- "Copied objects must match"
- digest, hexdigest = obj1.digest(), obj1.hexdigest()
- hd2 = ""
- for byte in digest:
- hd2 += "%02x" % ord(byte)
- assert hd2 == hexdigest, "hexdigest doesn't appear correct"
+ self.assertEquals(h1, h2)
- if verbose:
- print 'Module', module.__name__, 'seems to comply with PEP 247'
+ self.assert_(hasattr(obj1, 'digest_size'))
+ if not module.digest_size is None:
+ self.assertEquals(obj1.digest_size, module.digest_size)
-def test_main():
- check_hash_module(md5)
- check_hash_module(sha)
- check_hash_module(hmac, key='abc')
+ self.assertEquals(obj1.digest_size, len(h1))
+ obj1.update('string')
+ obj_copy = obj1.copy()
+ self.assertEquals(obj1.digest(), obj_copy.digest())
+ self.assertEquals(obj1.hexdigest(), obj_copy.hexdigest())
+
+ digest, hexdigest = obj1.digest(), obj1.hexdigest()
+ hd2 = ""
+ for byte in digest:
+ hd2 += '%02x' % ord(byte)
+ self.assertEquals(hd2, hexdigest)
+
+ def test_md5(self):
+ self.check_module(md5)
+ def test_sha(self):
+ self.check_module(sha)
+
+ def test_hmac(self):
+ self.check_module(hmac, key='abc')
+
+def test_main():
+ test_support.run_unittest(Pep247Test)
if __name__ == '__main__':
test_main()