use clm_rust_core::hash::{raw_hash_bytes_for, HashConfig};
use clm_rust_core::layer0::hash::verify_hash as verify_legacy_hash;
use clm_rust_core::layer0::mcard_record::{
create_record, verify_record, write_record, MCardRecord, RecordVerification,
};
use clm_rust_core::tape;
use rusqlite::Connection;
use serde_json::{json, Value};
use std::fs;
use std::path::PathBuf;
use tempfile::tempdir;
const CONTENT: &[u8] = b"CAS-10 Rust writer truth payload";
fn record_from_json(value: &Value) -> MCardRecord {
serde_json::from_value(value.clone()).expect("valid shared MCardRecord JSON")
}
fn wire_record(record: &MCardRecord) -> Value {
json!({
"hash": record.hash,
"content": record.content,
"g_time": record.g_time,
})
}
#[test]
fn writer_truth_persists_two_algorithms_and_checks_cross_runtime_records() {
unsafe {
std::env::remove_var("CLM_HASH_CONFIG");
std::env::set_var("CLM_HASH_ALGO", "sha256");
}
let dir = tempdir().expect("temporary CAS-10 tape directory");
let db_path = dir.path().join("writer-truth.sqlite");
let conn = Connection::open(&db_path).expect("open temporary tape");
conn.execute(
"CREATE TABLE card (hash TEXT PRIMARY KEY, content BLOB NOT NULL, g_time TEXT NOT NULL)",
[],
)
.expect("create temporary card table");
let configured_content = b"CAS-10 configured Rust write";
let configured = write_record(&conn, configured_content, None).expect("configured SHA-256 write");
assert_eq!(configured.content, configured_content);
assert_eq!(
verify_record(&configured),
RecordVerification::DeclaredOk {
algorithm: "sha256".to_string()
}
);
let mut own_records = vec![configured.clone()];
for (content, g_time, expected_algorithm) in [
(
b"CAS-10 tape BLAKE3".as_slice(),
"blake3|2026-10-12T12:00:00.000Z|UTC",
"blake3",
),
(
b"CAS-10 tape SHA-256".as_slice(),
"sha256|2026-10-12T12:00:00.000Z|UTC",
"sha256",
),
(
b"CAS-10 tape configured".as_slice(),
"sha256|2026-10-12T12:00:00.000Z|UTC",
"sha256",
),
] {
let hash = tape::put(&conn, content, g_time).expect("production tape writer");
let (stored_content, stored_g_time) = tape::get(&conn, &hash)
.expect("read production tape row")
.expect("production tape row exists");
let stored = MCardRecord {
hash,
content: stored_content,
g_time: stored_g_time,
};
assert_eq!(
verify_record(&stored),
RecordVerification::DeclaredOk {
algorithm: expected_algorithm.to_string(),
}
);
own_records.push(stored);
}
for algorithm in ["blake3", "sha256"] {
let written = write_record(&conn, CONTENT, Some(algorithm))
.unwrap_or_else(|error| panic!("{algorithm} write failed: {error}"));
let stored = conn
.query_row(
"SELECT hash, content, g_time FROM card WHERE hash = ?1",
[&written.hash],
|row| {
Ok(MCardRecord {
hash: row.get(0)?,
content: row.get(1)?,
g_time: row.get(2)?,
})
},
)
.expect("read record back from tape");
assert!(matches!(verify_record(&stored), RecordVerification::DeclaredOk { .. }));
assert_eq!(stored.content, CONTENT);
own_records.push(stored);
}
let negative = create_record(CONTENT, Some("sha256")).expect("SHA-256 negative-control source");
let negative = MCardRecord {
hash: negative.hash,
content: negative.content,
g_time: negative.g_time.replacen("sha256|", "blake3|", 1),
};
assert!(matches!(verify_record(&negative), RecordVerification::DeclaredMismatch { .. }));
let legacy_source = create_record(CONTENT, Some("blake3")).expect("legacy BLAKE3 source");
let iso_legacy = MCardRecord {
hash: legacy_source.hash.clone(),
content: CONTENT.to_vec(),
g_time: "2026-10-12T12:00:00+0000".to_string(),
};
assert_eq!(
verify_record(&iso_legacy),
RecordVerification::LegacyClassified {
algorithm: "blake3".to_string()
}
);
let ts_legacy = MCardRecord {
hash: legacy_source.hash,
content: CONTENT.to_vec(),
g_time: "a:blake3;t:2026-10-12T12:00:00.000Z;c:UTC".to_string(),
};
assert_eq!(
verify_record(&ts_legacy),
RecordVerification::DeclaredOk {
algorithm: "blake3".to_string()
}
);
if let Ok(input_path) = std::env::var("CAS10_POLYGLOT_RECORDS_IN") {
let input: Vec<Value> = serde_json::from_slice(
&fs::read(&input_path).expect("read cross-runtime records"),
)
.expect("cross-runtime record list JSON");
for value in input {
let record = record_from_json(&value);
assert!(
matches!(verify_record(&record), RecordVerification::DeclaredOk { .. }),
"Rust must verify another runtime's declared record: {:?}",
record
);
}
}
if let Ok(output_path) = std::env::var("CAS10_POLYGLOT_RECORDS_OUT") {
let mut all_records: Vec<Value> = own_records.iter().map(wire_record).collect();
all_records.push(wire_record(&negative));
fs::write(
PathBuf::from(output_path),
serde_json::to_vec(&all_records).expect("serialize cross-runtime records"),
)
.expect("write cross-runtime record fixture");
}
unsafe {
std::env::set_var("CLM_HASH_ALGO", "cas10-unsupported");
}
let invalid_config = HashConfig::from_env();
assert_eq!(invalid_config.active_algorithm, "cas10-unsupported:");
assert!(raw_hash_bytes_for(invalid_config.active_algorithm, CONTENT).is_err());
let blake3_digest = raw_hash_bytes_for("blake3", CONTENT).expect("BLAKE3 provider");
let sha256_digest = raw_hash_bytes_for("sha256", CONTENT).expect("SHA-256 provider");
assert!(verify_legacy_hash(CONTENT, &blake3_digest));
assert!(!verify_legacy_hash(CONTENT, &sha256_digest));
assert!(verify_legacy_hash(CONTENT, &format!("sha256:{sha256_digest}")));
assert_eq!(
conn.query_row("SELECT COUNT(*) FROM card", [], |row| row.get::<_, usize>(0))
.expect("count persisted records"),
6
);
}