Skip to content

Commit

Permalink
Add test cases for gethash.utils.strxor
Browse files Browse the repository at this point in the history
  • Loading branch information
xymy committed Nov 28, 2023
1 parent 655424d commit 32925c2
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Testing

- Added test cases for `gethash.utils.strxor`.
- Improved tox config.
- Now store `hypothesis` and `pytest` cache in `.cache` directory.

Expand Down
Empty file added tests/unit/utils/__init__.py
Empty file.
51 changes: 51 additions & 0 deletions tests/unit/utils/test_strxor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from __future__ import annotations

import pytest

from gethash.utils.strxor import _py_strxor, strxor


@pytest.mark.parametrize(
("term1", "term2", "expected"),
[
(
bytes.fromhex("0000000000000000000000000000000000000000000000000000000000000000"),
bytes.fromhex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
bytes.fromhex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
),
(
bytes.fromhex("0000000000000000000000000000000000000000000000000000000000000000"),
bytes.fromhex("0000000000000000000000000000000000000000000000000000000000000000"),
bytes.fromhex("0000000000000000000000000000000000000000000000000000000000000000"),
),
(
bytes.fromhex("54974d6a31b907910fb6bf679825ed5d678b7d65d4ae15c8b2a024723299ac80"),
bytes.fromhex("8696ac14f1f1e6a7219326a6b3b2c10a47464185a6d30f224caa800d5e69dd8e"),
bytes.fromhex("d201e17ec048e1362e2599c12b972c5720cd3ce0727d1aeafe0aa47f6cf0710e"),
),
],
)
class TestStrxor:
def test__py_strxor(self, term1: bytes, term2: bytes, expected: bytes) -> None:
result = _py_strxor(term1, term2)
assert result == expected

output = bytearray(len(expected))
_py_strxor(term1, term2, output)
assert output == expected

def test_strxor(self, term1: bytes, term2: bytes, expected: bytes) -> None:
result = strxor(term1, term2)
assert result == expected

output = bytearray(len(expected))
strxor(term1, term2, output)
assert output == expected

def test_strxor__error(self, term1: bytes, term2: bytes, expected: bytes) -> None:
with pytest.raises(ValueError, match="term1 and term2 must have the same length"):
strxor(term1, term2[1:])

output = bytearray(len(expected) - 1)
with pytest.raises(ValueError, match="output must have the same length as the input"):
strxor(term1, term2, output)

0 comments on commit 32925c2

Please sign in to comment.