Commit ad8f116c authored by Christian Heimes's avatar Christian Heimes

Issue #19758: silence PendingDeprecationWarnings in test_hmac

I also removed some bare excepts from the tests.
parent e658570f
import functools
import hmac import hmac
import hashlib import hashlib
import unittest import unittest
import warnings import warnings
from test import support from test import support
def ignore_warning(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
with warnings.catch_warnings():
warnings.filterwarnings("ignore",
category=PendingDeprecationWarning)
return func(*args, **kwargs)
return wrapper
class TestVectorsTestCase(unittest.TestCase): class TestVectorsTestCase(unittest.TestCase):
def test_md5_vectors(self): def test_md5_vectors(self):
...@@ -264,56 +276,63 @@ class TestVectorsTestCase(unittest.TestCase): ...@@ -264,56 +276,63 @@ class TestVectorsTestCase(unittest.TestCase):
class ConstructorTestCase(unittest.TestCase): class ConstructorTestCase(unittest.TestCase):
@ignore_warning
def test_normal(self): def test_normal(self):
# Standard constructor call. # Standard constructor call.
failed = 0 failed = 0
try: try:
h = hmac.HMAC(b"key") h = hmac.HMAC(b"key")
except: except Exception:
self.fail("Standard constructor call raised exception.") self.fail("Standard constructor call raised exception.")
@ignore_warning
def test_with_str_key(self): def test_with_str_key(self):
# Pass a key of type str, which is an error, because it expects a key # Pass a key of type str, which is an error, because it expects a key
# of type bytes # of type bytes
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
h = hmac.HMAC("key") h = hmac.HMAC("key")
@ignore_warning
def test_dot_new_with_str_key(self): def test_dot_new_with_str_key(self):
# Pass a key of type str, which is an error, because it expects a key # Pass a key of type str, which is an error, because it expects a key
# of type bytes # of type bytes
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
h = hmac.new("key") h = hmac.new("key")
@ignore_warning
def test_withtext(self): def test_withtext(self):
# Constructor call with text. # Constructor call with text.
try: try:
h = hmac.HMAC(b"key", b"hash this!") h = hmac.HMAC(b"key", b"hash this!")
except: except Exception:
self.fail("Constructor call with text argument raised exception.") self.fail("Constructor call with text argument raised exception.")
self.assertEqual(h.hexdigest(), '34325b639da4cfd95735b381e28cb864')
def test_with_bytearray(self): def test_with_bytearray(self):
try: try:
h = hmac.HMAC(bytearray(b"key"), bytearray(b"hash this!")) h = hmac.HMAC(bytearray(b"key"), bytearray(b"hash this!"),
self.assertEqual(h.hexdigest(), '34325b639da4cfd95735b381e28cb864') digestmod="md5")
except: except Exception:
self.fail("Constructor call with bytearray arguments raised exception.") self.fail("Constructor call with bytearray arguments raised exception.")
self.assertEqual(h.hexdigest(), '34325b639da4cfd95735b381e28cb864')
def test_with_memoryview_msg(self): def test_with_memoryview_msg(self):
try: try:
h = hmac.HMAC(b"key", memoryview(b"hash this!")) h = hmac.HMAC(b"key", memoryview(b"hash this!"), digestmod="md5")
self.assertEqual(h.hexdigest(), '34325b639da4cfd95735b381e28cb864') except Exception:
except:
self.fail("Constructor call with memoryview msg raised exception.") self.fail("Constructor call with memoryview msg raised exception.")
self.assertEqual(h.hexdigest(), '34325b639da4cfd95735b381e28cb864')
def test_withmodule(self): def test_withmodule(self):
# Constructor call with text and digest module. # Constructor call with text and digest module.
try: try:
h = hmac.HMAC(b"key", b"", hashlib.sha1) h = hmac.HMAC(b"key", b"", hashlib.sha1)
except: except Exception:
self.fail("Constructor call with hashlib.sha1 raised exception.") self.fail("Constructor call with hashlib.sha1 raised exception.")
class SanityTestCase(unittest.TestCase): class SanityTestCase(unittest.TestCase):
@ignore_warning
def test_default_is_md5(self): def test_default_is_md5(self):
# Testing if HMAC defaults to MD5 algorithm. # Testing if HMAC defaults to MD5 algorithm.
# NOTE: this whitebox test depends on the hmac class internals # NOTE: this whitebox test depends on the hmac class internals
...@@ -324,19 +343,19 @@ class SanityTestCase(unittest.TestCase): ...@@ -324,19 +343,19 @@ class SanityTestCase(unittest.TestCase):
# Exercising all methods once. # Exercising all methods once.
# This must not raise any exceptions # This must not raise any exceptions
try: try:
h = hmac.HMAC(b"my secret key") h = hmac.HMAC(b"my secret key", digestmod="md5")
h.update(b"compute the hash of this text!") h.update(b"compute the hash of this text!")
dig = h.digest() dig = h.digest()
dig = h.hexdigest() dig = h.hexdigest()
h2 = h.copy() h2 = h.copy()
except: except Exception:
self.fail("Exception raised during normal usage of HMAC class.") self.fail("Exception raised during normal usage of HMAC class.")
class CopyTestCase(unittest.TestCase): class CopyTestCase(unittest.TestCase):
def test_attributes(self): def test_attributes(self):
# Testing if attributes are of same type. # Testing if attributes are of same type.
h1 = hmac.HMAC(b"key") h1 = hmac.HMAC(b"key", digestmod="md5")
h2 = h1.copy() h2 = h1.copy()
self.assertTrue(h1.digest_cons == h2.digest_cons, self.assertTrue(h1.digest_cons == h2.digest_cons,
"digest constructors don't match.") "digest constructors don't match.")
...@@ -347,7 +366,7 @@ class CopyTestCase(unittest.TestCase): ...@@ -347,7 +366,7 @@ class CopyTestCase(unittest.TestCase):
def test_realcopy(self): def test_realcopy(self):
# Testing if the copy method created a real copy. # Testing if the copy method created a real copy.
h1 = hmac.HMAC(b"key") h1 = hmac.HMAC(b"key", digestmod="md5")
h2 = h1.copy() h2 = h1.copy()
# Using id() in case somebody has overridden __eq__/__ne__. # Using id() in case somebody has overridden __eq__/__ne__.
self.assertTrue(id(h1) != id(h2), "No real copy of the HMAC instance.") self.assertTrue(id(h1) != id(h2), "No real copy of the HMAC instance.")
...@@ -358,7 +377,7 @@ class CopyTestCase(unittest.TestCase): ...@@ -358,7 +377,7 @@ class CopyTestCase(unittest.TestCase):
def test_equality(self): def test_equality(self):
# Testing if the copy has the same digests. # Testing if the copy has the same digests.
h1 = hmac.HMAC(b"key") h1 = hmac.HMAC(b"key", digestmod="md5")
h1.update(b"some random text") h1.update(b"some random text")
h2 = h1.copy() h2 = h1.copy()
self.assertEqual(h1.digest(), h2.digest(), self.assertEqual(h1.digest(), h2.digest(),
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment