Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
ad8f116c
Commit
ad8f116c
authored
Nov 24, 2013
by
Christian Heimes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #19758: silence PendingDeprecationWarnings in test_hmac
I also removed some bare excepts from the tests.
parent
e658570f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
33 additions
and
14 deletions
+33
-14
Lib/test/test_hmac.py
Lib/test/test_hmac.py
+33
-14
No files found.
Lib/test/test_hmac.py
View file @
ad8f116c
import
functools
import
hmac
import
hashlib
import
unittest
import
warnings
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
):
def
test_md5_vectors
(
self
):
...
...
@@ -264,56 +276,63 @@ class TestVectorsTestCase(unittest.TestCase):
class
ConstructorTestCase
(
unittest
.
TestCase
):
@
ignore_warning
def
test_normal
(
self
):
# Standard constructor call.
failed
=
0
try
:
h
=
hmac
.
HMAC
(
b"key"
)
except
:
except
Exception
:
self
.
fail
(
"Standard constructor call raised exception."
)
@
ignore_warning
def
test_with_str_key
(
self
):
# Pass a key of type str, which is an error, because it expects a key
# of type bytes
with
self
.
assertRaises
(
TypeError
):
h
=
hmac
.
HMAC
(
"key"
)
@
ignore_warning
def
test_dot_new_with_str_key
(
self
):
# Pass a key of type str, which is an error, because it expects a key
# of type bytes
with
self
.
assertRaises
(
TypeError
):
h
=
hmac
.
new
(
"key"
)
@
ignore_warning
def
test_withtext
(
self
):
# Constructor call with text.
try
:
h
=
hmac
.
HMAC
(
b"key"
,
b"hash this!"
)
except
:
except
Exception
:
self
.
fail
(
"Constructor call with text argument raised exception."
)
self
.
assertEqual
(
h
.
hexdigest
(),
'34325b639da4cfd95735b381e28cb864'
)
def
test_with_bytearray
(
self
):
try
:
h
=
hmac
.
HMAC
(
bytearray
(
b"key"
),
bytearray
(
b"hash this!"
)
)
self
.
assertEqual
(
h
.
hexdigest
(),
'34325b639da4cfd95735b381e28cb864'
)
except
:
h
=
hmac
.
HMAC
(
bytearray
(
b"key"
),
bytearray
(
b"hash this!"
)
,
digestmod
=
"md5"
)
except
Exception
:
self
.
fail
(
"Constructor call with bytearray arguments raised exception."
)
self
.
assertEqual
(
h
.
hexdigest
(),
'34325b639da4cfd95735b381e28cb864'
)
def
test_with_memoryview_msg
(
self
):
try
:
h
=
hmac
.
HMAC
(
b"key"
,
memoryview
(
b"hash this!"
))
self
.
assertEqual
(
h
.
hexdigest
(),
'34325b639da4cfd95735b381e28cb864'
)
except
:
h
=
hmac
.
HMAC
(
b"key"
,
memoryview
(
b"hash this!"
),
digestmod
=
"md5"
)
except
Exception
:
self
.
fail
(
"Constructor call with memoryview msg raised exception."
)
self
.
assertEqual
(
h
.
hexdigest
(),
'34325b639da4cfd95735b381e28cb864'
)
def
test_withmodule
(
self
):
# Constructor call with text and digest module.
try
:
h
=
hmac
.
HMAC
(
b"key"
,
b""
,
hashlib
.
sha1
)
except
:
except
Exception
:
self
.
fail
(
"Constructor call with hashlib.sha1 raised exception."
)
class
SanityTestCase
(
unittest
.
TestCase
):
@
ignore_warning
def
test_default_is_md5
(
self
):
# Testing if HMAC defaults to MD5 algorithm.
# NOTE: this whitebox test depends on the hmac class internals
...
...
@@ -324,19 +343,19 @@ class SanityTestCase(unittest.TestCase):
# Exercising all methods once.
# This must not raise any exceptions
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!"
)
dig
=
h
.
digest
()
dig
=
h
.
hexdigest
()
h2
=
h
.
copy
()
except
:
except
Exception
:
self
.
fail
(
"Exception raised during normal usage of HMAC class."
)
class
CopyTestCase
(
unittest
.
TestCase
):
def
test_attributes
(
self
):
# Testing if attributes are of same type.
h1
=
hmac
.
HMAC
(
b"key"
)
h1
=
hmac
.
HMAC
(
b"key"
,
digestmod
=
"md5"
)
h2
=
h1
.
copy
()
self
.
assertTrue
(
h1
.
digest_cons
==
h2
.
digest_cons
,
"digest constructors don't match."
)
...
...
@@ -347,7 +366,7 @@ class CopyTestCase(unittest.TestCase):
def
test_realcopy
(
self
):
# Testing if the copy method created a real copy.
h1
=
hmac
.
HMAC
(
b"key"
)
h1
=
hmac
.
HMAC
(
b"key"
,
digestmod
=
"md5"
)
h2
=
h1
.
copy
()
# Using id() in case somebody has overridden __eq__/__ne__.
self
.
assertTrue
(
id
(
h1
)
!=
id
(
h2
),
"No real copy of the HMAC instance."
)
...
...
@@ -358,7 +377,7 @@ class CopyTestCase(unittest.TestCase):
def
test_equality
(
self
):
# 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"
)
h2
=
h1
.
copy
()
self
.
assertEqual
(
h1
.
digest
(),
h2
.
digest
(),
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment