[issue14322] More test coverage for hmac

Arjun report at bugs.python.org
Wed Jun 9 17:43:28 EDT 2021


Arjun <ccldarjun at icloud.com> added the comment:

The only things I think we should add to the current hmac tests are test_update_error_handling and test_with_invalid_msg. 

For test_withnoncallable_digestmod(), hmac itself seems it can no longer be used:

>>> hmac.HMAC(b"gggg", None, "hmac")
<module '_hashlib' from '/Users/arjun/Python/Sources/cpython/build/lib.macosx-11.4-x86_64-3.11-pydebug/_hashlib.cpython-311d-darwin.so'>
using new
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/arjun/Python/Sources/cpython/Lib/hmac.py", line 61, in __init__
    self._init_hmac(key, msg, digestmod)
  File "/Users/arjun/Python/Sources/cpython/Lib/hmac.py", line 69, in _init_hmac
    self._hmac = _hashopenssl.hmac_new(key, msg, digestmod=digestmod)
ValueError: unsupported hash type hmac

For tests test_small_block_size and test_no_block_size, a custom .blocksize cannot be set (changing .block_size has no difference on digest()):

>>> hmac.HMAC(b"gggg", None, "md5").blocksize = 15
<module '_hashlib' from '/Users/arjun/Python/Sources/cpython/build/lib.macosx-11.4-x86_64-3.11-pydebug/_hashlib.cpython-311d-darwin.so'>
using new
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'HMAC' object attribute 'blocksize' is read-only

```
class myHMAC(hmac.HMAC):
    blocksize = 1
    def __init__(self, key, msg, digestmod_):
        super().__init__(key, msg, digestmod=digestmod_)

h = myHMAC(b"key", b"", digestmod_="md5")
h.digest() # <- works perfectly fine.
```
Does this sound okay? I can go ahead an implement it if so.

----------
nosy: +CCLDArjun -Vijay.Majagaonkar, christian.heimes, eric.araujo, gregory.p.smith, packetslave, pitrou

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue14322>
_______________________________________


More information about the Python-bugs-list mailing list