Skip to content

Instantly share code, notes, and snippets.

@oconnor663
Last active March 4, 2022 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oconnor663/533048580b1c0f4a01d1d55f57f92792 to your computer and use it in GitHub Desktop.
Save oconnor663/533048580b1c0f4a01d1d55f57f92792 to your computer and use it in GitHub Desktop.
running blake3-py tests against the proposed hashlib implementation

running blake3-py tests against the proposed hashlib implementation

Scroll down to see the test output on my machine.

My procedure for running these tests (on Arch Linux):

mkdir /tmp/whatever
cd /tmp/whatever

git clone https://github.com/larryhastings/cpython --branch add_blake3_to_hashlib
cd cpython
mkdir /tmp/whatever/install
./configure --prefix=/tmp/whatever/install
make -j
make install

/tmp/whatever/install/bin/python3 -m venv /tmp/whatever/venv
source /tmp/whatever/venv/bin/activate
hash -r

pip install pytest
# we'd also like to install numpy, but that doesn't build on Python 3.11 today

cd /tmp/whatever
git clone https://github.com/oconnor663/blake3-py --branch hashlib_tests
cd blake3-py
python3 -m pytest -v
============================= test session starts ==============================
platform linux -- Python 3.11.0a5+, pytest-7.0.1, pluggy-1.0.0 -- /tmp/whatever/venv/bin/python3
cachedir: .pytest_cache
rootdir: /tmp/whatever/blake3-py
collecting ... collected 23 items
tests/test_blake3.py::test_vectors FAILED [ 4%]
tests/test_blake3.py::test_buffer_types PASSED [ 8%]
tests/test_blake3.py::test_key_types PASSED [ 13%]
tests/test_blake3.py::test_invalid_key_lengths FAILED [ 17%]
tests/test_blake3.py::test_int_array_fails FAILED [ 21%]
tests/test_blake3.py::test_string_fails PASSED [ 26%]
tests/test_blake3.py::test_constants FAILED [ 30%]
tests/test_blake3.py::test_example_dot_py PASSED [ 34%]
tests/test_blake3.py::test_xof FAILED [ 39%]
tests/test_blake3.py::test_max_threads_value PASSED [ 43%]
tests/test_blake3.py::test_max_threads_auto FAILED [ 47%]
tests/test_blake3.py::test_key_context_incompatible FAILED [ 52%]
tests/test_blake3.py::test_name PASSED [ 56%]
tests/test_blake3.py::test_copy_basic PASSED [ 60%]
tests/test_blake3.py::test_copy_with_threads PASSED [ 65%]
tests/test_blake3.py::test_invalid_max_threads FAILED [ 69%]
tests/test_blake3.py::test_positional_only_arguments FAILED [ 73%]
tests/test_blake3.py::test_keyword_only_arguments FAILED [ 78%]
tests/test_blake3.py::test_usedforsecurity_ignored PASSED [ 82%]
tests/test_blake3.py::test_context_must_be_str FAILED [ 86%]
tests/test_blake3.py::test_buffers_released PASSED [ 91%]
tests/test_blake3.py::test_reset FAILED [ 95%]
tests/test_blake3.py::test_output_overflows_isize PASSED [100%]
=================================== FAILURES ===================================
_________________________________ test_vectors _________________________________
def test_vectors():
cases = VECTORS["cases"]
for case in cases:
input_len = int(case["input_len"])
input_bytes = make_input(input_len)
extended_hash_hex = case["hash"]
extended_keyed_hash_hex = case["keyed_hash"]
extended_derive_key_hex = case["derive_key"]
extended_hash_bytes = unhexlify(extended_hash_hex)
extended_keyed_hash_bytes = unhexlify(extended_keyed_hash_hex)
extended_derive_key_bytes = unhexlify(extended_derive_key_hex)
hash_bytes = extended_hash_bytes[:32]
keyed_hash_bytes = extended_keyed_hash_bytes[:32]
derive_key_bytes = extended_derive_key_bytes[:32]
extended_len = len(extended_hash_bytes)
assert extended_len == len(extended_keyed_hash_bytes)
assert extended_len == len(extended_derive_key_bytes)
# default hash
assert hash_bytes == blake3(input_bytes).digest()
assert extended_hash_bytes == blake3(input_bytes).digest(length=extended_len)
assert extended_hash_hex == blake3(input_bytes).hexdigest(length=extended_len)
incremental_hash = blake3()
incremental_hash.update(input_bytes[: input_len // 2])
incremental_hash.update(input_bytes[input_len // 2 :])
assert hash_bytes == incremental_hash.digest()
# keyed hash
key = VECTORS["key"].encode()
assert keyed_hash_bytes == blake3(input_bytes, key=key).digest()
assert extended_keyed_hash_bytes == blake3(input_bytes, key=key).digest(
length=extended_len
)
assert extended_keyed_hash_hex == blake3(input_bytes, key=key).hexdigest(
length=extended_len
)
incremental_keyed_hash = blake3(key=key)
incremental_keyed_hash.update(input_bytes[: input_len // 2])
incremental_keyed_hash.update(input_bytes[input_len // 2 :])
assert keyed_hash_bytes == incremental_keyed_hash.digest()
# derive key
context = "BLAKE3 2019-12-27 16:29:52 test vectors context"
> assert (
derive_key_bytes == blake3(input_bytes, derive_key_context=context).digest()
)
E TypeError: a bytes-like object is required, not 'str'
tests/test_blake3.py:66: TypeError
___________________________ test_invalid_key_lengths ___________________________
def test_invalid_key_lengths():
for key_length in range(0, 100):
key = b"\xff" * key_length
> if key_length == blake3.key_size:
E AttributeError: type object '_blake3.blake3' has no attribute 'key_size'
tests/test_blake3.py:116: AttributeError
_____________________________ test_int_array_fails _____________________________
def test_int_array_fails():
try:
# "i" represents the int type, which is larger than a char.
blake3(array.array("i"))
# We get BufferError in Rust and ValueError in C.
except (BufferError, ValueError):
pass
else:
> assert False, "expected a buffer error"
E AssertionError: expected a buffer error
E assert False
tests/test_blake3.py:136: AssertionError
________________________________ test_constants ________________________________
def test_constants():
# These are class attributes, so they should work on the class itself and
# also on instances of the class.
> assert blake3.name == "blake3"
E AssertionError: assert <attribute 'n...ake3' objects> == 'blake3'
E +<attribute 'name' of '_blake3.blake3' objects>
E -'blake3'
tests/test_blake3.py:162: AssertionError
___________________________________ test_xof ___________________________________
def test_xof():
extended = blake3(b"foo").digest(length=100)
for i in range(100):
> assert extended[:i] == blake3(b"foo").digest(length=i)
E ValueError: digest length cannot be 0
tests/test_blake3.py:193: ValueError
____________________________ test_max_threads_auto _____________________________
def test_max_threads_auto():
b = make_input(10 ** 6)
expected = blake3(b).digest()
> assert expected == blake3(b, max_threads=blake3.AUTO).digest()
E AttributeError: type object '_blake3.blake3' has no attribute 'AUTO'
tests/test_blake3.py:209: AttributeError
________________________ test_key_context_incompatible _________________________
def test_key_context_incompatible():
zero_key = bytearray(32)
try:
> blake3(b"foo", key=zero_key, derive_key_context="")
E TypeError: a bytes-like object is required, not 'str'
tests/test_blake3.py:218: TypeError
___________________________ test_invalid_max_threads ___________________________
def test_invalid_max_threads():
# Check 0.
try:
blake3(max_threads=0)
except ValueError:
pass
else:
> assert False, "expected a ValueError"
E AssertionError: expected a ValueError
E assert False
tests/test_blake3.py:281: AssertionError
________________________ test_positional_only_arguments ________________________
def test_positional_only_arguments():
try:
# Passing the data as a keyword argument should fail.
blake3(data=b"")
assert False, "expected TypeError"
except TypeError:
pass
try:
# Passing the data as a keyword argument should fail.
blake3().update(data=b"")
> assert False, "expected TypeError"
E AssertionError: expected TypeError
E assert False
tests/test_blake3.py:302: AssertionError
_________________________ test_keyword_only_arguments __________________________
def test_keyword_only_arguments():
try:
# Passing the key as a positional argument should fail.
blake3(b"", b"\0" * 32)
assert False, "expected TypeError"
except TypeError:
pass
# The digest length is allowed to be positional or keyword.
blake3(b"").digest(32)
blake3(b"").digest(length=32)
blake3(b"").hexdigest(32)
blake3(b"").hexdigest(length=32)
# But the seek parameter is keyword-only.
blake3(b"").digest(32, seek=0)
blake3(b"").digest(length=32, seek=0)
blake3(b"").hexdigest(32, seek=0)
blake3(b"").hexdigest(length=32, seek=0)
try:
blake3(b"").digest(32, 0)
> assert False, "expected TypeError"
E AssertionError: expected TypeError
E assert False
tests/test_blake3.py:327: AssertionError
___________________________ test_context_must_be_str ___________________________
def test_context_must_be_str():
# string works
> blake3(derive_key_context="foo")
E TypeError: a bytes-like object is required, not 'str'
tests/test_blake3.py:344: TypeError
__________________________________ test_reset __________________________________
def test_reset():
hasher = blake3()
hash1 = hasher.digest()
hasher.update(b"foo")
hash2 = hasher.digest()
> hasher.reset()
E AttributeError: '_blake3.blake3' object has no attribute 'reset'
tests/test_blake3.py:372: AttributeError
=========================== short test summary info ============================
FAILED tests/test_blake3.py::test_vectors - TypeError: a bytes-like object is...
FAILED tests/test_blake3.py::test_invalid_key_lengths - AttributeError: type ...
FAILED tests/test_blake3.py::test_int_array_fails - AssertionError: expected ...
FAILED tests/test_blake3.py::test_constants - AssertionError: assert <attribu...
FAILED tests/test_blake3.py::test_xof - ValueError: digest length cannot be 0
FAILED tests/test_blake3.py::test_max_threads_auto - AttributeError: type obj...
FAILED tests/test_blake3.py::test_key_context_incompatible - TypeError: a byt...
FAILED tests/test_blake3.py::test_invalid_max_threads - AssertionError: expec...
FAILED tests/test_blake3.py::test_positional_only_arguments - AssertionError:...
FAILED tests/test_blake3.py::test_keyword_only_arguments - AssertionError: ex...
FAILED tests/test_blake3.py::test_context_must_be_str - TypeError: a bytes-li...
FAILED tests/test_blake3.py::test_reset - AttributeError: '_blake3.blake3' ob...
======================== 12 failed, 11 passed in 0.32s =========================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment