Add a pytest suite covering the pure logic of the DeDRM and Obok plugins.
Because the plugins normally run inside calibre and use intra-package
imports, tests/dedrm_test_utils.py sets up an import shim (plugin dirs on
sys.path, a minimal calibre stub, and a synthetic `dedrm` package) so the
modules can be imported and exercised standalone.
Coverage:
- alfcrypto: PC1 and Topaz cipher round-trips, PBKDF2 vs hashlib.
- kgenpids / kindlepid: PID encoding, bit-field extraction, device-PID
and serial-PID known vectors, CRC32.
- mobidedrm: PC1 round-trip, trailing-data sizing, bad-key handling.
- ineptpdf / ineptepub: nunpack and PKCS7 unpad.
- topazextract: encoded number/string parsing, plus a regression test
that a malicious header tag ("../../evil") cannot escape the output
directory (covers the path-traversal fix).
- obok: unpad, hash-key table, SafeUnbuffered str/bytes handling.
- utilities / argv_utils: uStrCmp normalisation, unicode_argv.
- erdr2pml: deXOR/sanitiseFileName, skipped on Python 3.13+ where the
module's `cgi` import is unavailable.
Run with `poetry install` then `poetry run pytest`. 36 pass, 2 skip on
Python 3.13+.
Note: this surfaced two dead-code modules that are broken on Python 3
(aescbc's pure-Python AES and kgenpids.decode, neither on a live path,
since the real crypto goes through pycryptodome); left as-is here.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
35 lines
1023 B
Python
35 lines
1023 B
Python
"""Tests for mobidedrm helpers."""
|
|
|
|
import pytest
|
|
|
|
import dedrm_test_utils as U
|
|
|
|
md = U.load("mobidedrm", package="dedrm")
|
|
|
|
|
|
def test_pc1_roundtrip():
|
|
key = bytes(range(16))
|
|
plaintext = b"cookie-data-1234"
|
|
ciphertext = md.PC1(key, plaintext, decryption=False)
|
|
assert ciphertext != plaintext
|
|
assert md.PC1(key, ciphertext, decryption=True) == plaintext
|
|
|
|
|
|
def test_pc1_rejects_bad_key_length():
|
|
with pytest.raises(Exception):
|
|
md.PC1(b"\x00" * 8, b"data............", decryption=False)
|
|
|
|
|
|
def test_trailing_data_entries():
|
|
buf = b"XXXX" + bytes([0x82])
|
|
# bit 1 set -> one size entry encoded in the final byte (0x82 -> 2)
|
|
assert md.getSizeOfTrailingDataEntries(buf, len(buf), 0b10) == 2
|
|
# no flags -> no trailing data
|
|
assert md.getSizeOfTrailingDataEntries(buf, len(buf), 0) == 0
|
|
# bit 0 set -> multibyte entry: (last_byte & 3) + 1
|
|
assert md.getSizeOfTrailingDataEntries(b"ABCDE", 5, 1) == 2
|
|
|
|
|
|
def test_crc32_known_vector():
|
|
assert md.crc32(b"test") == 4181434640
|