Files
DeDRM_tools/tests/test_utilities.py
JMR-devandClaude Opus 4.8 d407298be4 Update tests for shared helpers and fix test nits
- Cover the new utilities helpers (unpad, crc32, checksumPid type
  preservation, pidFromSerial, safe_join) and drop the now-redundant
  per-module crc32 tests.
- Rename test_unpad_removes_pkcs7_padding to reflect that unpad only
  trusts the trailing pad-length byte rather than validating PKCS#7.
- Replace the KOBO_HASH_KEYS change-detector with a test that documents
  the intentional value pin and checks the ASCII-salt contract.
- Note kgenpids' hybrid import in the test-loader docstring.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-24 09:18:01 -05:00

71 lines
2.0 KiB
Python

"""Tests for DeDRM utilities (uStrCmp and the shared crypto/path helpers)."""
import os
import dedrm_test_utils as U
ut = U.load("utilities", package="dedrm")
def test_ustrcmp_equal():
assert ut.uStrCmp("Abc", "Abc") is True
def test_ustrcmp_caseless():
assert ut.uStrCmp("ABC", "abc", caseless=True) is True
assert ut.uStrCmp("ABC", "abc", caseless=False) is False
def test_ustrcmp_unicode_normalisation():
# Composed 'é' (U+00E9) vs decomposed 'e' + combining acute (U+0065 U+0301).
assert ut.uStrCmp("é", "é", caseless=False) is True
def test_unpad_strips_trailing_pad_length():
assert ut.unpad(b"data\x04\x04\x04\x04") == b"data"
assert ut.unpad(b"hello\x03\x03\x03") == b"hello"
def test_crc32_known_vector():
assert ut.crc32(b"test") == 4181434640
def test_pid_alphabet():
assert ut.PID_ALPHABET == b"ABCDEFGHIJKLMNPQRSTUVWXYZ123456789"
def test_checksum_pid_preserves_bytes():
out = ut.checksumPid(b"12345678")
assert out == b"12345678EL"
assert isinstance(out, bytes)
def test_checksum_pid_preserves_str():
out = ut.checksumPid("12345678")
assert out == "12345678EL"
assert isinstance(out, str)
def test_pid_from_serial_known_vector():
assert ut.pidFromSerial(b"B00212345678", 8) == b"VBKTRX7Q"
def test_safe_join_plain_name(tmp_path):
assert ut.safe_join(str(tmp_path), "book0000.dat") == os.path.join(
str(tmp_path), "book0000.dat"
)
def test_safe_join_strips_directory_components(tmp_path):
# Any directory part (including ``../``) is reduced to a bare basename.
result = ut.safe_join(str(tmp_path), "../../etc/evil")
assert os.path.basename(result) == "evil"
assert os.path.dirname(os.path.abspath(result)) == os.path.abspath(str(tmp_path))
def test_safe_join_result_always_contained(tmp_path):
root = os.path.abspath(str(tmp_path))
for name in ["../../evil", "a/b/c", "..\\..\\win", "/abs/path", "plain"]:
result = os.path.abspath(ut.safe_join(str(tmp_path), name))
assert result == root or result.startswith(root + os.sep)