My SQLite tests only ever read files they had just written
Linux Hardener keeps the copies rollback restores in one SQLite file. Before a SQLite upgrade I committed two files the old one wrote, and made the test check that they really came from it.
Linux Hardener can undo what it changes, and the copies it restores sit in one SQLite file. It's a Rust tool of mine that finds weak security settings on Linux machines and fixes them. Before each change it saves a copy of the files it's about to touch, called a checkpoint, and hardener rollback puts them back. A comment in my tests calls checkpoints.db "the only copy of the files rollback restores". If a SQLite upgrade ever misread it, rollback would go with it, perhaps on a machine where a bad change to SSH or the login rules had already locked someone out.
Every test I had for that file started by making a fresh database with whatever SQLite the build linked. So a newer SQLite that misread an older file would have passed all of them. One migration test even builds an old table by hand, to stand in for a database from an earlier release. The table's shape is old, but the SQLite writing it is today's.
An upgrade was coming, because of a security advisory. cargo audit flagged RUSTSEC-2026-0459 against libsqlite3-sys, the crate that compiles SQLite into the binary. The version I had bundled SQLite 3.46.0. The bug is in how SQLite reads a database together with its write-ahead log when the two disagree on page size. That log is a -wal file where SQLite puts changes before folding them into the main file.
The fix needed a newer libsqlite3-sys, and no released version of sqlx, the database library Linux Hardener uses, would take one yet. Meanwhile, machines on Linux Hardener 1.9.1 hold checkpoint files that SQLite 3.46.0 wrote, and the release with the new SQLite will open them. That was the case to test.
So I put two database files into the repository. They're fixtures, files a test starts from, written once on 15 November by the database code and the SQLite that 1.9.1 shipped. The rows are made up. They hold one checkpoint with a fake sshd_config and a 12,000 byte file, big enough to spill onto overflow pages, the extra pages SQLite chains together when a value won't fit on one.
The first, engine-1.9.1.db, was copied after its connections had closed, so SQLite had already folded the log into it. The second, engine-1.9.1-wal.db, was copied with its -wal file while the connections were still open, so its rows exist only in the log, as a killed process would leave them. That pair matters most, because it is the case the advisory is about.
The test builds the files into itself at compile time with include_bytes!, then writes them out to a temporary folder. It opens them the way the CLI does and runs SQLite's own PRAGMA integrity_check. Every row has to match what went in, byte for byte. Then it adds a checkpoint and checks again, because an upgraded binary goes on writing to the old file.
There was one small trap. The repository's .gitignore ignores database files, so the fixtures needed exceptions before Git would track them. Here they are, comments left out:
*.db
*.db-shm
*.db-wal
!/crates/hardener-state/tests/fixtures/engine-1.9.1*.db
!/crates/hardener-state/tests/fixtures/engine-1.9.1*.db-wal
"Never regenerate them with a newer SQLite," said the comment in the test file. They're only worth something because an older SQLite wrote them. If anyone rebuilt them after the upgrade, me included, the test would still pass and would no longer be testing an older SQLite. The first version's checks promised more than they did, and this rule lived only in a comment.
SQLite already writes down the answer. The four bytes at offset 96 of the file header hold the version number of the SQLite that last changed the file. Version X.Y.Z is stored as X times a million, plus Y times a thousand, plus Z, so 3.46.0 is 3046000. You can read it on any SQLite database you have with od:
$ od -A d -t x1 -j 96 -N 4 engine-1.9.1.db
0000096 00 2e 7a 70
0000100
Read as one hex number, 00 2e 7a 70 is 3046000. The last line is only od saying where it stopped.
The test now reads that number from both main files, and from every copy of the header inside the -wal file, and fails unless each one says 3046000. Here's the core of it, with the doc comments left out:
const FIXTURE_WRITER_VERSION: u32 = 3_046_000;
fn be_u32(bytes: &[u8], at: usize) -> u32 {
u32::from_be_bytes(bytes[at..at + 4].try_into().expect("four bytes"))
}
fn header_writer_version(page_one: &[u8]) -> u32 {
be_u32(page_one, 96)
}
The check has a limit. The header shows which SQLite last wrote a file, not which version of Linux Hardener did.
The other weak check was in the WAL test. Before the real work it opens the main file without its log, to show the rows aren't in there. The first version took any error as proof of that, so a file that failed to open for some unrelated reason would have passed too. Now only an empty table or no such table: file_states counts, since in that fixture even the table definitions sit in the log.
On 18 November the upgrade went in, with sqlx 0.9.1 bringing in SQLite 3.54.1. Both fixtures read back byte for byte, the WAL one included.
For the three days before that, the test couldn't tell me anything about a newer SQLite. The build still had 3.46.0, so it read the files with the same SQLite that wrote them. It only started to mean something when the upgrade landed. Because it was already in place, the first build with the new SQLite had old files waiting for it. No release has the new SQLite yet, so the machines with old files haven't met it.
If you keep data in SQLite and a library bump is coming, commit a file the released version wrote, with made-up rows, and a second one copied while a connection is open so its rows sit only in the -wal file. Then make the test fail unless offset 96 still holds the old version number, so nobody can quietly regenerate them, you included, six months from now.
SQLite promises that newer versions can always read and write files older ones created, and I'm not doubting that. I think the value is in my own code on top of it, the schema step that runs on open and the way sqlx hands the bytes back.
The whole test is in tests.rs. The od line you can run today, on any SQLite file you already have.
Sources and further reading:
- Linux Hardener and its issue #188
- The commits that added the fixtures and tightened their checks
- RUSTSEC-2026-0459 in the RustSec database
- SQLite on write-ahead logging and file compatibility
- The SQLite file format on the writer's version and overflow pages
- SQLite's version numbers and integrity check
- sqlx and Rust's
include_bytes! - Git's gitignore manual and Wikipedia on test fixtures