Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions crates/data/src/source/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,35 @@ fn geopackage_in_memory() {
}
}

#[test]
fn geopackage_in_memory_handles_mutated_headers() {
let folder = tempfile::tempdir().unwrap();
let path = folder.path().join("mutated.gpkg");
write_gpkg(&path, false);
let original = std::fs::read(path).unwrap();
let mut offsets: Vec<usize> = (0..100).collect();
// SQLite pages have a short header; exercise the beginning of each one.
for page in (4096..original.len()).step_by(4096) {
offsets.extend(page..(page + 12).min(original.len()));
}
Comment on lines +384 to +386

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.

Page 1's B-tree header is skipped. The loop starts at 4096, so page 1 is never visited. Its B-tree header sits at byte 100, just past the 100-byte database header that line 383 covers, so bytes 100–111 are never mutated. The 4096 page size is also hard-coded; it is stored in the database header at bytes 16–17 (confidence: medium-high).

Suggested change
for page in (4096..original.len()).step_by(4096) {
offsets.extend(page..(page + 12).min(original.len()));
}
for page in (0..original.len()).step_by(4096) {
let start = if page == 0 { 100 } else { page };
offsets.extend(start..(start + 12).min(original.len()));
}

// Exercise flags and envelope metadata in actual GeoPackage geometry blobs.
let geometries: Vec<_> =
original.windows(4).enumerate().filter_map(|(i, bytes)| (bytes == b"GP\x00\x03").then_some(i)).take(3).collect();

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.

Nits, grouped:

  • 100 and 4096 are magic numbers. A named constant or a reference to the SQLite header layout would make the intent clearer.
  • filter_map(|(i, bytes)| (bytes == b"GP\x00\x03").then_some(i)) reads more simply as .filter(...).map(|(i, _)| i).
  • A mutation that makes the reader loop or over-allocate would hang the test rather than fail it. There is no timeout, so this is worth knowing about even though the test is correct as written.

assert_eq!(geometries.len(), 3);
for i in geometries {
offsets.extend(i..i + 8);

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.

Envelope is never mutated, despite the comment on line 387. The fixture writes each blob as an 8-byte header (GP, version, flags, srs_id) followed by a 32-byte XY envelope. i..i + 8 covers only the header, so the envelope that gpkg_mem reads via split_blob for pruning is untouched. Covering the envelope too is a one-line change (confidence: high).

Suggested change
offsets.extend(i..i + 8);
offsets.extend(i..i + 40);

Also update the comment on line 387 to say it covers the header and the XY envelope.

}
offsets.sort_unstable();
offsets.dedup();
for offset in offsets {
let mut bytes = original.clone();
bytes[offset] ^= 0xff;
if let Ok(source) = gpkg_mem::GpkgMemSource::open_shared(Shared(Arc::new(bytes)), None) {
let _ = source.read(&Request::all());
}
}
}

#[test]
fn an_unknown_coordinate_system_is_reported() {
let p = dir().join("unknown_crs.gpkg");
Expand Down
Loading