pe: extract main(argv) and add directory and struct.error guards in pe_authentihash with CLI suite #2585

Merged
Ammmae0x merged 2 commits from upstream-pr/2593 into main 2026-11-25 22:27:13 +00:00
Ammmae0x commented 2026-11-25 22:15:21 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-25T22:15:21Z.


Summary

Extracts a testable main(argv=None) -> int entrypoint in tools/pe_authentihash.py, validates upfront that the target image is a regular file (rejecting directories and non-regular paths cleanly), and catches struct.error alongside (OSError, ValueError) with zero raw tracebacks, backed by a standalone pure-Python CLI test suite registered in CTest.

Changes

  • tools/pe_authentihash.py:
    • Extract main(argv=None) -> int forwarding argv to parser.parse_args(argv) and returning 0, invoking raise SystemExit(main()) under __main__.
    • Add upfront args.image.is_dir() and not args.image.is_file() checks before opening or mapping the input image.
    • Expand main() exception handling to (OSError, ValueError, struct.error) via parser.exit(1, f"{args.image}: {error}\n") with zero raw tracebacks, preserving read-only behavior, the 0-byte mmap fallback, and exact hex digest stdout.
  • tests/pe_authentihash_cli_check.py:
    • Add standalone pure-Python CLI tests covering PE32, PE32+, signed certificate-table images, 0-byte mmap fallback, directory/missing/non-regular/truncated inputs, sys.argv/__main__ entrypoints, and OSError/ValueError/struct.error guards.
  • CMakeLists.txt:
    • Register pe_authentihash_cli_check in CTest right after pe_authentihash_oracle.

Verification

All commands executed after rebasing onto upstream/main (826b7347):

  • out/pe-env/Scripts/python.exe -B tests/pe_authentihash_cli_check.py -v
    • Observed: Ran 5 tests in 1.080s / OK (exit 0)
  • out/pe-env/Scripts/python.exe -B tests/pe_authentihash_check.py
    • Observed: Ran 12 tests in 0.361s / OK (exit 0)
  • out/pe-env/Scripts/python.exe -B tests/form_coverage_check.py
    • Observed: form coverage checks passed (exit 0)
  • out/pe-env/Scripts/python.exe -B tests/docs_links_check.py
    • Observed: Self-test passed. Checked 19 Markdown files and 495 links. (exit 0)
  • out/pe-env/Scripts/python.exe -B tests/bidi_controls_check.py
    • Observed: Self-test passed. Checked 1819 tracked source, script and build/CI files: no bidirectional controls. (exit 0)
  • Minimal temp-copy mutation testing (9 adversarial mutations across argv forwarding, is_dir(), is_file(), 0-byte mmap fallback, OSError/ValueError/struct.error catches, main() return code, and __main__ SystemExit): 9/9 killed.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6614855498 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2593). Originally posted by [Ammmae0x](https://github.com/Ammmae0x) on 2026-11-25T22:15:21Z. --- ## Summary Extracts a testable `main(argv=None) -> int` entrypoint in `tools/pe_authentihash.py`, validates upfront that the target image is a regular file (rejecting directories and non-regular paths cleanly), and catches `struct.error` alongside `(OSError, ValueError)` with zero raw tracebacks, backed by a standalone pure-Python CLI test suite registered in CTest. ### Changes - **`tools/pe_authentihash.py`**: - Extract `main(argv=None) -> int` forwarding `argv` to `parser.parse_args(argv)` and returning `0`, invoking `raise SystemExit(main())` under `__main__`. - Add upfront `args.image.is_dir()` and `not args.image.is_file()` checks before opening or mapping the input image. - Expand `main()` exception handling to `(OSError, ValueError, struct.error)` via `parser.exit(1, f"{args.image}: {error}\n")` with zero raw tracebacks, preserving read-only behavior, the 0-byte `mmap` fallback, and exact hex digest stdout. - **`tests/pe_authentihash_cli_check.py`**: - Add standalone pure-Python CLI tests covering PE32, PE32+, signed certificate-table images, 0-byte `mmap` fallback, directory/missing/non-regular/truncated inputs, `sys.argv`/`__main__` entrypoints, and `OSError`/`ValueError`/`struct.error` guards. - **`CMakeLists.txt`**: - Register `pe_authentihash_cli_check` in CTest right after `pe_authentihash_oracle`. ## Verification All commands executed after rebasing onto `upstream/main` (`826b7347`): - `out/pe-env/Scripts/python.exe -B tests/pe_authentihash_cli_check.py -v` - Observed: `Ran 5 tests in 1.080s` / `OK` (exit 0) - `out/pe-env/Scripts/python.exe -B tests/pe_authentihash_check.py` - Observed: `Ran 12 tests in 0.361s` / `OK` (exit 0) - `out/pe-env/Scripts/python.exe -B tests/form_coverage_check.py` - Observed: `form coverage checks passed` (exit 0) - `out/pe-env/Scripts/python.exe -B tests/docs_links_check.py` - Observed: `Self-test passed. Checked 19 Markdown files and 495 links.` (exit 0) - `out/pe-env/Scripts/python.exe -B tests/bidi_controls_check.py` - Observed: `Self-test passed. Checked 1819 tracked source, script and build/CI files: no bidirectional controls.` (exit 0) - Minimal temp-copy mutation testing (9 adversarial mutations across `argv` forwarding, `is_dir()`, `is_file()`, 0-byte `mmap` fallback, `OSError`/`ValueError`/`struct.error` catches, `main()` return code, and `__main__` `SystemExit`): 9/9 killed. AI-assisted.
upstream-sync closed this pull request 2026-11-25 23:01:58 +00:00
tedin7 commented 2026-11-25 23:01:58 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T22:17:05Z.

Review: COMMENTED


Review of head 5ee1a811 (comment only).

Verdict: sound, no blocking findings.

What I checked

  • tools/pe_authentihash.py: main(argv=None) -> int, parse_args(argv), raise SystemExit(main()). Behavior on the happy path is unchanged (same digest on stdout, read-only, 0-byte mmap fallback kept).
  • New upfront is_dir() / is_file() checks run inside the existing try, so they exit 1 with <path>: <message> and no traceback.
  • struct.error is now caught next to OSError / ValueError, so truncated headers give a clean exit 1.
  • CMakeLists.txt: pe_authentihash_cli_check registered inside the INTAKE_PYTHON block right after pe_authentihash_oracle. It needs no shim target, so no skip code is needed.
  • Sibling check: no other open or merged PR covers this slice (#1911 merged the tool, #2499 closed duplicate).

Commands executed (Linux, clean worktree at the head)

  • python3 tests/pe_authentihash_cli_check.py -> 5 tests OK
  • python3 tests/docs_links_check.py -> pass; python3 tests/bidi_controls_check.py -> pass
  • git diff --check origin/main...HEAD -> clean
  • Mutation 1: drop struct.error from the except tuple -> the new suite fails (errors=1)
  • Mutation 2: disable the is_dir() check -> the new suite fails (failures=1)

Not a startup/game change; no executable claims are made here. Win2025 receipt and CI are still needed before merge.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6065751738 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2593#pullrequestreview-6065751738). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T22:17:05Z. **Review:** COMMENTED --- Review of head 5ee1a811 (comment only). Verdict: sound, no blocking findings. What I checked - `tools/pe_authentihash.py`: `main(argv=None) -> int`, `parse_args(argv)`, `raise SystemExit(main())`. Behavior on the happy path is unchanged (same digest on stdout, read-only, 0-byte mmap fallback kept). - New upfront `is_dir()` / `is_file()` checks run inside the existing `try`, so they exit 1 with `<path>: <message>` and no traceback. - `struct.error` is now caught next to `OSError` / `ValueError`, so truncated headers give a clean exit 1. - `CMakeLists.txt`: `pe_authentihash_cli_check` registered inside the `INTAKE_PYTHON` block right after `pe_authentihash_oracle`. It needs no shim target, so no skip code is needed. - Sibling check: no other open or merged PR covers this slice (#1911 merged the tool, #2499 closed duplicate). Commands executed (Linux, clean worktree at the head) - `python3 tests/pe_authentihash_cli_check.py` -> 5 tests OK - `python3 tests/docs_links_check.py` -> pass; `python3 tests/bidi_controls_check.py` -> pass - `git diff --check origin/main...HEAD` -> clean - Mutation 1: drop `struct.error` from the except tuple -> the new suite fails (errors=1) - Mutation 2: disable the `is_dir()` check -> the new suite fails (failures=1) Not a startup/game change; no executable claims are made here. Win2025 receipt and CI are still needed before merge. AI-assisted; commands listed were executed locally.
tedin7 commented 2026-11-25 23:02:05 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T22:27:44Z.


Win2025 WARP receipt for head 3b12eb35 after updating with main.

Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP) at commit 3b12eb35ee8e96da96a9dbf34e93bfd4e70a520a:

  • Debug (--preset dev):
    • Build (cmake --build --preset dev -- -j 2): 183.17s (configure 2.43s)
    • Test (ctest --preset dev -R 'pe_authentihash' --parallel 2 --no-tests=error --output-on-failure): 3/3 passed (2.07s: pe_authentihash_oracle 0.34s, pe_authentihash 0.65s, pe_authentihash_cli_check 1.56s)
  • Release /O2 (--preset release):
    • Build (cmake --build --preset release -- -j 2): 241.84s (configure 2.81s)
    • Test (ctest --preset release -R 'pe_authentihash' --parallel 2 --no-tests=error --output-on-failure): 3/3 passed (1.55s: pe_authentihash_oracle 0.26s, pe_authentihash 0.47s, pe_authentihash_cli_check 1.11s)
  • Windows Python checks: tests/pe_authentihash_check.py ok (0.5s), tests/pe_authentihash_cli_check.py ok (1.1s)
  • sccache: 239/557 hits; wall 451.6s

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7176391253 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2593#issuecomment-7176391253). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T22:27:44Z. --- Win2025 WARP receipt for head 3b12eb35 after updating with main. Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP) at commit `3b12eb35ee8e96da96a9dbf34e93bfd4e70a520a`: - **Debug (`--preset dev`)**: - Build (`cmake --build --preset dev -- -j 2`): `183.17s` (configure `2.43s`) - Test (`ctest --preset dev -R 'pe_authentihash' --parallel 2 --no-tests=error --output-on-failure`): **3/3 passed** (`2.07s`: `pe_authentihash_oracle` `0.34s`, `pe_authentihash` `0.65s`, `pe_authentihash_cli_check` `1.56s`) - **Release `/O2` (`--preset release`)**: - Build (`cmake --build --preset release -- -j 2`): `241.84s` (configure `2.81s`) - Test (`ctest --preset release -R 'pe_authentihash' --parallel 2 --no-tests=error --output-on-failure`): **3/3 passed** (`1.55s`: `pe_authentihash_oracle` `0.26s`, `pe_authentihash` `0.47s`, `pe_authentihash_cli_check` `1.11s`) - **Windows Python checks**: `tests/pe_authentihash_check.py` ok (`0.5s`), `tests/pe_authentihash_cli_check.py` ok (`1.1s`) - `sccache`: 239/557 hits; wall `451.6s` AI-assisted; commands listed were executed locally.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows!2585
No description provided.