Example 2: Verify

  • file format 6
  • Rust SDK 1.0 preview

Needs the Rust SDK

Read the verify report of a good pack, then change one letter: the default open refuses the pack, and the report shows why.

02_verify.rsrust
// Verify a pack, and see what a damaged pack reports.
use plxi_sdk::{write, ErrorKind, OpenOptions, Pack, Record};

fn main() -> Result<(), plxi_sdk::Error> {
    let records = vec![
        Record::from_json_line(r#"{"k":"ent","id":"e1","t":"Person"}"#)?,
        Record::from_json_line(r#"{"k":"ent","id":"e2","t":"Person"}"#)?,
    ];
    let good = write(&records, None)?;

    // A good pack: every member of the report is fine.
    let pack = Pack::from_bytes(good.clone(), OpenOptions::default())?;
    let report = pack.verify()?;
    assert!(report.is_ok());
    let count = &report.record_count;
    assert_eq!((count.declared, count.actual), (2, 2));
    println!("{}", report.to_json());

    // Change one letter in the record text after the pack was sealed:
    // "Person" becomes "person". The JSON is still valid; the digest
    // no longer matches.
    let mut damaged = good;
    let at = damaged
        .windows(6)
        .position(|w| w == b"Person")
        .expect("the record text contains \"Person\"");
    damaged[at] = b'p';

    // The default open refuses it and names the reason.
    let err = Pack::from_bytes(damaged.clone(), OpenOptions::default())
        .unwrap_err();
    assert_eq!(err.kind(), ErrorKind::ChecksumMismatch);
    assert_eq!(err.kind().as_str(), "checksum_mismatch");
    println!("refused: {err}");

    // With verification off the pack opens, and the report says what
    // is wrong with it.
    let pack = Pack::from_bytes(damaged, OpenOptions::no_verify())?;
    let report = pack.verify()?;
    assert!(!report.sha256_ok);
    assert!(!report.is_ok());
    assert_eq!(report.record_count.actual, 2); // the records still parse
    println!("{}", report.to_json());
    Ok(())
}
Output
{"sha256_ok":true,"header_footer_agree":true,"record_count":{"declared":2,"actual":2},"appendix":{"present":false,"file_checksum_ok":true,"sections":[]},"embrefs":{"total":0,"resolved":0,"dangling":0,"type_mismatch":0}}
refused: checksum_mismatch: the file's SHA-256 digest does not match the one recorded in its footer
{"sha256_ok":false,"header_footer_agree":true,"record_count":{"declared":2,"actual":2},"appendix":{"present":false,"file_checksum_ok":true,"sections":[]},"embrefs":{"total":0,"resolved":0,"dangling":0,"type_mismatch":0}}

Ran with cargo run --example 02_verify · exit status 0

Explained in: Open and verify a pack

Sections