GNU File Oracle Testing#
Oracle tests in libmagic-rs compare rmagic output against the host file binary to verify behavioral parity. Two project-specific traps affect any test that invokes the real file binary: a Windows PATH hazard that makes the standard availability check unreliable, and upstream version-dependent size limits that determine what classifications file produces.
The shared oracle infrastructure lives in tests/common/magic_oracle.rs. It is reused or mirrored by tests/description_bound_oracle_tests.rs and tests/text_class_tail_tests.rs (introduced in PR #527).
Trap 1: has_file_binary() Is Not Sufficient on Windows#
has_file_binary() checks whether file --version exits 0. This passes on GitHub's windows-latest runners because Git for Windows places an MSYS file.exe on PATH. That binary misreads a Windows drive-letter path in MAGIC=C:\...\magic as a colon-separated list and dies with "could not find any valid magic files". An oracle test that stages its own inline magic in a temp directory therefore fails on Windows ā even when no system magic database is involved.
Do not gate oracle tests on has_file_binary() alone. The correct guard probes whether the file binary actually honors MAGIC=<dir> by calling it with your temp directory and checking that it exits 0. The current shared helper file_says() sets MAGIC via the environment variable (not --magic-file) and panics with the binary's stderr on non-zero exit ā it does not skip silently. Callers must perform the MAGIC probe before calling file_says() and call skip() with the stderr reason when the probe fails .
Why MAGIC= instead of --magic-file#
The module doc for tests/common/magic_oracle.rs records the measurement: --magic-file <dir> prefers a sibling compiled <dir>.mgc over the directory itself, so pointing at a staged source directory can cause file to silently answer from the pre-compiled system database. Setting MAGIC=<dir> forces it to read only the specified sources .
Canary verification in stage_system_magic()#
When staging the system magic directory, stage_system_magic() runs a canary check: it writes a sentinel rule that can never match, points file at that rule alone, and asserts the target file does not classify as real_class. If file still returns the real class, it is answering from some other database and the comparison would be meaningless .
RMAGIC_REQUIRE_ORACLE#
skip() prints a skip message and passes. When RMAGIC_REQUIRE_ORACLE=1 is set (used on macOS CI, where file and source magic are expected), a skipped oracle fails loudly instead of silently. Use this environment variable in CI jobs where the oracle must be present .
Trap 2: Version-Dependent Upstream Size Limits#
Several constants that govern file's behavior changed between releases. Tests that pin exact classification boundaries must cite the version where the constant was introduced and verify against src/file.h at that tag. Comparing against a host binary on the wrong version silently validates the wrong value.
MAXstring (string field render bound)#
MAXstring in GNU file's src/file.h was 96 through file 5.38, and became 128 at 5.39. rmagic's MAX_DESCRIPTION_FIELD_LEN = 127 is MAXstring - 1 and is therefore only correct for file 5.39 and later.
tests/description_bound_oracle_tests.rs enforces this: require_oracle_ready() parses file --version, fails loudly (not skips) when the version is below 5.39, and passes clearly why:
"
MAXstringwas 96 through file 5.38 and became 128 at 5.39 ... Comparing against an older binary would silently assert the wrong bound"
Oracle tests then verify exact agreement: any_value_string_304_bytes_matches_gnu_file_127_byte_bound asserts rmagic and file-5.41 both render exactly 127 Q bytes for a 304-byte input.
FILE_ENCODING_MAX (text classification window)#
The text pass ā classification, qualifier scan, and text-pass evaluation ā operates on the buffer with trailing NULs trimmed and capped at 64 KiB (FILE_ENCODING_MAX). The /b and /t skip logic uses the untrimmed view. PR #527 introduced and measured this two-window behavior; tests/text_class_tail_tests.rs pins the 64 KiB boundary .
FILE_BYTES_MAX (read cap, file 5.44+)#
FILE_BYTES_MAX ā the maximum bytes file reads from a file ā is version-dependent:
file version | FILE_BYTES_MAX |
|---|---|
| through 5.43 | 1 MiB |
| 5.44 and later | 7 MiB |
A file containing a NUL run that crosses 1 MiB followed by non-NUL bytes within 7 MiB classifies as data under rmagic and file 5.44+, but as text under macOS file 5.41 (which uses the 1 MiB cap). The test the_read_cap_is_file_544s_7_mib in tests/text_class_tail_tests.rs pins both edges. That file is part of PR #527 and is not yet in the main branch snapshot .
Important: An early estimate placed this change at file 5.45 ā that was wrong. The correct version is 5.44. Always verify version-dependent constants against src/file.h at the specific upstream tag, not from memory or secondary sources.
Oracle Infrastructure Quick Reference#
| Symbol | File | Purpose |
|---|---|---|
has_file_binary() | tests/common/magic_oracle.rs:40 | Checks file --version exits 0. Not sufficient alone on Windows. |
file_says() | tests/common/magic_oracle.rs:65 | Runs file -b with MAGIC=<dir>. Panics on non-zero exit. |
stage_system_magic() | tests/common/magic_oracle.rs:87 | Copies system magic sources to a temp dir with canary verification. |
skip() | tests/common/magic_oracle.rs:24 | Reports a skipped oracle; fails if RMAGIC_REQUIRE_ORACLE=1. |
OracleReadiness | tests/common/magic_oracle.rs:33 | Ready(TempDir) or Skip(String) gate result. |
require_oracle_ready() | tests/description_bound_oracle_tests.rs:471 | Adds version gate (ā„5.39); fails loudly on old file. |
MAX_DESCRIPTION_FIELD_LEN | src/output/format/mod.rs:226 | 127 = MAXstring - 1, valid for file 5.39+. |
Key version thresholds#
file5.39 āMAXstringincreased from 96 to 128;MAX_DESCRIPTION_FIELD_LEN = 127requires this version or later.file5.44 āFILE_BYTES_MAXincreased from 1 MiB to 7 MiB; rmagic's text classification window uses the 5.44 value.file5.45 ā description rewrites introduced (e.g.c program textāc program, ASCII text); macOSfile5.41 does not apply these rewrites .
Debian/Ubuntu compiled-only installs#
On Debian and Ubuntu, /usr/share/file/magic exists but contains only the compiled magic.mgc with no source files. magic_source_file_count() detects this (count == 0) and returns OracleReadiness::Skip. These platforms cannot run system-magic oracle tests .