utils/freedays/freedays/tests/test_hmac.py
Krzysztof kuhy Rudnicki 6154dc09bf Pin pre-commit's ruff to 0.16.7 and satisfy the CPY001 it brings
The three Python packages pinned ruff twice at different versions:
requirements.txt at 0.16.7, .pre-commit-config.yaml at v0.15.2. Only the
older one ever ran, so `select = ["ALL"]` was enforced two minor versions
behind. Measured before aligning: v0.15.2 reports 0 findings, every 0.16.x
reports CPY001 on each file -- 70 crdt-sync, 23 freedays, 71 gatelock, and
no other rule.

Resolved by adding the notice rather than ignoring the rule, so the gate
keeps its teeth.

Two gatelock modules sat at exactly the 250-line cap, so a one-line header
tipped them to 251. Both are split along a real seam rather than shaved:

  _widget_fills.py    colour arithmetic over LockConfig, no tkinter import
  _surfaces_focus.py  selects among existing surfaces, never mutates them

The focus split keeps _surfaces.py's documented containment (it stays the
only module that creates, moves or destroys windows), and the static guard
in test_recovery_invariants.py globs _surfaces*.py, so the new sibling is
covered with no list to extend. widgets.py re-exports the fills helpers,
which keeps gatelock.widgets the single public surface its tests import.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019kMvSMtsVMB3d2uoZ56pbp
2026-09-12 18:11:24 +02:00

98 lines
3.3 KiB
Python

# Copyright (c) 2026 Krzysztof Rudnicki
"""Signing, and the ways it is allowed to fail."""
from __future__ import annotations
from typing import TYPE_CHECKING
from freedays._hmac import generate_key, load_key, sign, verify
if TYPE_CHECKING:
from pathlib import Path
_ENTRY = {"action": "mark", "day": "2026-12-24", "actor": "device-1"}
def test_a_generated_key_is_owner_only(tmp_path: Path) -> None:
"""A signing key the rest of the machine can read is not much of one."""
key_file = tmp_path / "hmac.key"
generate_key(key_file)
assert key_file.stat().st_mode & 0o077 == 0
def test_a_generated_key_round_trips(tmp_path: Path) -> None:
key_file = tmp_path / "hmac.key"
created = generate_key(key_file)
assert created is not None
assert load_key(key_file) == created
def test_two_generated_keys_differ(tmp_path: Path) -> None:
first = generate_key(tmp_path / "a.key")
second = generate_key(tmp_path / "b.key")
assert first != second
def test_a_key_that_cannot_be_written_returns_none(tmp_path: Path) -> None:
blocked = tmp_path / "a-file" / "hmac.key"
blocked.parent.write_text("I am a file, not a directory", encoding="utf-8")
assert generate_key(blocked) is None
def test_a_missing_key_reads_as_none(tmp_path: Path) -> None:
assert load_key(tmp_path / "absent.key") is None
def test_signing_and_verifying_agree(tmp_path: Path) -> None:
key_file = tmp_path / "hmac.key"
generate_key(key_file)
signature = sign(_ENTRY, key_file=key_file)
assert signature is not None
assert verify({**_ENTRY, "hmac": signature}, key_file=key_file)
def test_signing_is_stable_across_key_order(tmp_path: Path) -> None:
"""Sorted-key JSON: the same fields sign the same however they arrive."""
key_file = tmp_path / "hmac.key"
generate_key(key_file)
reordered = dict(reversed(list(_ENTRY.items())))
assert sign(_ENTRY, key_file=key_file) == sign(reordered, key_file=key_file)
def test_a_tampered_field_fails_verification(tmp_path: Path) -> None:
key_file = tmp_path / "hmac.key"
generate_key(key_file)
signature = sign(_ENTRY, key_file=key_file)
tampered = {**_ENTRY, "day": "2026-12-25", "hmac": signature}
assert not verify(tampered, key_file=key_file)
def test_another_key_fails_verification(tmp_path: Path) -> None:
mine = tmp_path / "mine.key"
theirs = tmp_path / "theirs.key"
generate_key(mine)
generate_key(theirs)
signature = sign(_ENTRY, key_file=mine)
assert not verify({**_ENTRY, "hmac": signature}, key_file=theirs)
def test_an_entry_without_a_signature_does_not_verify(tmp_path: Path) -> None:
key_file = tmp_path / "hmac.key"
generate_key(key_file)
assert not verify(dict(_ENTRY), key_file=key_file)
def test_a_non_string_signature_does_not_verify(tmp_path: Path) -> None:
key_file = tmp_path / "hmac.key"
generate_key(key_file)
assert not verify({**_ENTRY, "hmac": 17}, key_file=key_file)
def test_verification_without_a_key_is_a_no(tmp_path: Path) -> None:
""" "Cannot check" and "does not match" are the same answer to the caller."""
assert not verify({**_ENTRY, "hmac": "deadbeef"}, key_file=tmp_path / "absent.key")
def test_signing_without_a_key_returns_none(tmp_path: Path) -> None:
assert sign(_ENTRY, key_file=tmp_path / "absent.key") is None