Add cmd/pdfdump smoke tests against hostile input#15
Merged
Conversation
pdfdump is the entry point run on untrusted files, but main() was untestable (global flag state, log.Fatal). Extract run(args, stdout) error so the command can be exercised; main() stays a thin wrapper. Tests assert hostile inputs (empty, garbage, broken xref, NUL bytes, unclosed dict) yield a graceful error or valid JSON — never a panic — and that every testdata fixture dumps successfully.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
pdfdumpis the real-world entry point a user runs on an untrusted file. If it panics on a hostile PDF, the "never crash on untrusted input" mandate is violated at its most visible point — yetcmd/pdfdumpwas 0% covered becausemain()is untestable (globalflagstate,log.Fatal).Change
Extract
run(args []string, stdout io.Writer) error;main()becomes a thin wrapper that maps the error tolog.Fatal(and treats-has exit 0). No behavior change for real use.Tests
TestRunHostileInputsNoPanic— empty, garbage, broken-xref-recovery, NUL bytes, and an unclosed dict each yield a graceful error or valid JSON, never a panic.TestRunValidFixtures— everytestdata/fixtures/*/input.pdfdumps to JSON (with-stream-content, exercising the inline-content path).TestRunArgErrors— no-arg and missing-file both error.Coverage:
cmd/pdfdump0% → 74%;run87%.