Skip to content

tests: psin_psch decoder rejection paths - #7297

Open
ThomasRuss wants to merge 2 commits into
ItsNotRexed:mainfrom
ThomasRuss:test-psin-psch-rejects
Open

ThomasRuss wants to merge 2 commits into
ItsNotRexed:mainfrom
ThomasRuss:test-psin-psch-rejects

Conversation

@ThomasRuss

@ThomasRuss ThomasRuss commented Nov 23, 2026 •

Copy link
Copy Markdown

Claimed in the coordination issue as: #7231 (comment)

What and why

Adds tests/test_psin_psch_rejects.py: tests only, no change to the tool and no bug fixed. tests/test_psin_psch.py covers valid containers and a few bad ones, so most error branches of decode_pmap, decode_psch, decode_psig, decode_stns, check_psch_schema and parse_psch_container never ran; a check removed by accident would not have failed a test. Each bad input must be refused with its own error, and the valid ones must still be accepted. Type codes are the ones docs/psin.md gives, not read from the tool. Most cases damage an input the same test class shows to be accepted; the rest are the smallest section that reaches the check.

Related pull requests (none is a dependency):

The new file passes with the psin_psch changes of each of the four applied, and with #6260 and #7301 applied together.

Tested

Windows 11, CPU only (no GPU or game files):

  • python -I tests/test_psin_psch_rejects.py: 16 tests pass on Python 3.12 and 3.10 (also under pytest on 3.12)
  • tests/test_psin_psch.py, tests/test_ci_registration.py, tests/test_resource_hygiene.py and scripts/check_no_game_data.py on the new file pass
  • the 31 lines of the decoder and container functions that no existing test ran are now all run
  • single-line mutations of those functions in a scratch copy. Guards removed, comparisons and and/or flipped: 92 of 92 caught. Constants changed by one, byte order and arithmetic flipped: 191 of 205 caught; of the other 14, 7 do not change behaviour, 5 are caught by tests/test_psin_psch.py, and 2 change a constant only the archive scan uses

The second commit follows the review by tikD1337: the PMAP flags pass-through is pinned (1), renaming a section checks that its tag occurs once (2), and the recognition test is renamed and notes that either tag is enough (3). The overlap with existing tests (4) is left as is.

Not run: Linux, macOS, real game files.

AI-assisted: yes (Claude Code)

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a: tests only)

Generated with Claude Code

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tikD1337

Copy link
Copy Markdown
Contributor

Review: tests: psin_psch decoder rejection paths (#7297)

Environment: Windows 11, CPython 3.13, standard library only.

What changed

This adds one file, tests/test_psin_psch_rejects.py, with 230 lines and no tool changes. It builds hand-made byte inputs for every decoder in tools/psin_psch.py and checks that each refusal has its own reason. Each case uses assertRaisesRegex on the specific message, so a case that fails for the wrong reason is caught:

Class Covers Acceptance shown in the same class
PmapTests decode_pmap raise sites at tools/psin_psch.py:61-92: short header, container fill at +0x08, PMAP fill at +10, size vs. block_count, root id at 0, past the end and with no blocks, blocks outside 0x10..first_section, first block not at 0x10, end not on a 16-byte boundary exact end, 16-byte padded end, root=2, non-zero reserved fields reported (reserved_zero=False) but not refused
PschTests decode_psch at :106-156: tag/length, directory past the end (including count=0xFFFFFFFF), first offset ≠ directory end, offsets not strictly ascending or past the section, entry under 4 bytes, struct/enum size mismatch both ways, unknown kind structs plus enums, and an empty table
PsigStnsTests decode_psig at :164 (4/9/zero-entry cases) and decode_stns at :171-187 (short count, table past the end, huge count, size not a multiple of 16, data past the end, missing/extra/non-zero padding, empty table without padding) [1, 2], [], one 16-byte record with 4 bytes of tail padding
SchemaTests every errors.append in check_psch_schema at :204-228, including both the key and value map errors reported in one case resolved array/enum/map descriptors; a zero-size struct is not used as a divisor
ContainerTests parse_psch_container (not PSIN, truncated, each of the four required sections missing), is_psch_container truth table, STDN/STNS prefix mismatch, and the stdn_match is None path when STDN is absent build_synthetic_psch_psin() is valid

Why it is sound

  • The builders pmap, psch, struct_entry and enum_entry use the decoders' own struct layouts: >IHHH6s and >4I for PMAP, >BBHII with >IBBHI members, and >BBH with >IIq values. Each reject case changes one field of an input the class has just shown to be accepted, so a regex match points at a single check.
  • Several cases pin edges that are easy to break in a refactor:
    • "offset + length over 32 bits" (0xFFFFFFFF) fixes that offset + length > first_section is compared in Python integers and must not wrap the way a C port would.
    • "gap of a whole row at the end" (a zero-length last block ending at 48 against FIRST=64) shows that the 16-byte rounding at :91 allows partial-row padding only, not a whole missing row.
    • "empty table without padding" shows that a zero-count STNS still has to be padded to 16 bytes (expected_len=16).
    • "PSIG payload 4 " keeps the trailing space so the regex cannot also match 40.
  • test_nonzero_reserved_fields_are_reported_not_refused records the current contract: reserved fields at z0 and in each row lower reserved_zero and are not refused. That is the right thing to pin before anyone tightens it.
  • The file runs under python -I with a __main__ block and no pytest import. The CI loop that runs find tests … -name 'test_*.py' (.github/workflows/ci.yml around line 160) picks it up as a standalone test, so it needs no separate CI registration.

Nits (not blocking)

  1. The PMAP flags field (>IHHH, second field) is always passed as 0, and decode_pmap does not check it. One line, for example decode_pmap(struct.pack(">IHHH6s", 1, 0xFFFF, 0, 2, PMAP_FILL) + rows, …)["flags"] == 0xFFFF, would document that it is passed through unchecked, the same way the reserved fields are handled.
  2. ContainerTests drops sections with blob.replace(tag.encode(), b"XXXX"), which replaces every occurrence. The synthetic blob from test_psin_psch.build_synthetic_psch_psin has no tag bytes in any payload today, so this works. If that builder changes, a hash or payload that happens to contain PMAP or STNS would also be rewritten. Replacing only the section-directory entry, or asserting blob.count(tag) == 1 first, would make this robust.
  3. test_only_a_complete_container_with_the_sections_is_recognised asserts that a container without PSCH but with PMAP still counts as a PSCH container. This matches the "PMAP" in tags or "PSCH" in tags check at :55, but the test name suggests all the sections are needed. A short comment that PMAP alone is enough would help the next reader.
  4. test_psin_psch.py already has test_detects_bad_array_descriptor_and_pmap_size_mismatch and a "padding" STNS case, which this file covers again in more detail. The overlap is harmless and needs no change.

Test results (no game files)

Check Result
git diff --check PASS
python -m compileall -q make_game.py tools tests PASS
python -I scripts/check_no_game_data.py on the changed file PASS
python -I make_game.py --help PASS
python -I tests/test_psin_psch_rejects.py (run during this review) 16 tests, OK in 0.002 s

The harness found no existing targeted test file for the change, so the only targeted run is the new file itself.

Adherence

  • No game data: every input is built in the test from struct.pack and literal bytes such as b"pppppppp", plus the existing hand-made build_synthetic_psch_psin(). No archive, carved file or log content is involved, and check_no_game_data.py passes.
  • No keys or protection workarounds: the change only exercises unencrypted container parsing.
  • Nothing online: no network access.
  • Fix, don't disable: the change is tests only. It adds no skip list and relaxes no check. It holds the existing validation in place, including keeping the reserved-field checks as reports rather than refusals.
  • Conventions: standard library only, runnable with python -I, and it writes no files, so the line-ending rule does not apply.

The PR adds no new format claims. It tests the checks already in tools/psin_psch.py, so no docs/design.md quote is needed.

Verdict: approve. The nits are optional follow-ups.

AI-assisted.

…dding edges, review points

Type codes come from docs/psin.md instead of the tool's constants. Adds multi-block STNS, full enum values, padding
edges, the PMAP flags pass-through and the member type histogram, guards the section renaming, and corrects the
header and one test name.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ThomasRuss

Copy link
Copy Markdown
Author

@tikD1337 Thanks for the review. Points 1 to 3 are addressed in 54a5e27:

  1. flags is now pinned as passed through unchecked, next to the reserved fields.
  2. Section renaming goes through a helper that fails if the tag does not occur exactly once in the fixture.
  3. The test is now test_a_complete_container_with_pmap_or_psch_is_recognised, with a comment that either tag is enough, and it also checks the PSCH-only case.

Point 4 is left as is, as you suggested.

The same commit changes a few things you have not seen: type codes come from docs/psin.md instead of the tool's constants, and there are new cases for multi-block STNS, padding edges, full enum values and the member type histogram. It also corrects the file header: not every case changes a single field of an accepted input, some use the smallest section that reaches the check.

AI-assisted (Claude Code).

@sds-228 sds-228 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed at the current head. This adds one synthetic test file only: each decoder and container rejection is pinned to its own message, with acceptance cases beside it in the same classes, so a guard removed or weakened will fail a named case.

CPU-only verification here: 16/16 new tests pass; the 3 existing decoder tests still pass; changing one header-length guard makes exactly one new case fail; the no-game-data check is clean. No GPU or game files involved. Earlier optional nits are non-blocking.

AI-assisted (OpenCode).

@memorypressure

Copy link
Copy Markdown

Tested f65a23c, clean vs main 976b3ec: test_psin_psch_rejects Ran 16 OK both merged AND on clean main — correct for a pure characterization PR documenting current rejection behavior. A mutation probe (deleting the STNS nonzero-padding condition) does turn test_stns red, so the assertions are live, not decorative; tree restored md5-clean with pycache purged. No case overlap with #7301 — complementary. Flag: NO CI checks on the branch. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only run).

@airp1ane

Copy link
Copy Markdown
Contributor

Tested 54a5e27 on Windows 11, Python 3.15.2, NVIDIA RTX 4060 Ti (driver 621.24; CPU-only checks, no game files, no GPU/llvm-mc/MSVC used): pass.

Ran the test files this PR adds or changes, from a clean checkout of the PR head: test_psin_psch_rejects.py - all exit 0.

AI-assisted (Claude Code).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants