use std::collections::BTreeMap;
mod common;
use proptest::prelude::*;
use stratum_node_config::{preview, schema, Change, ConfigDocument, Value};
const SECRET_PATH: &str = "secrets.pod_admin_key_ref";
fn valid_change() -> impl Strategy<Value = Change> {
prop_oneof![
"[a-zA-Z0-9 ._-]{1,64}".prop_map(|s| Change::new("node.name", s)),
prop_oneof![
Just(String::new()),
"[a-z][a-z0-9]{0,9}(\\.[a-z][a-z0-9]{0,9}){0,2}".prop_map(|h| format!("https://{h}")),
]
.prop_map(|s| Change::new("node.canonical_origin", s)),
any::<u16>().prop_map(|p| Change::new("listen.address", format!("127.0.0.1:{p}"))),
"[a-z]{1,8}".prop_map(|d| Change::new("storage.state_dir", format!("/state/{d}"))),
"[a-z]{1,8}".prop_map(|d| Change::new("storage.backup_dir", format!("/backup/{d}"))),
proptest::collection::vec(proptest::sample::select(common::pq_recipients()), 0..4)
.prop_map(|l| Change::new("backup.age_recipients", Value::from(&l[..]))),
secret_ref().prop_map(|s| Change::new(SECRET_PATH, s)),
any::<bool>().prop_map(|b| Change::new("federation.egress_enabled", b)),
(1024i64..=1_048_576).prop_map(|n| Change::new("limits.max_body_bytes", n)),
(1i64..=64).prop_map(|n| Change::new("limits.max_outstanding_challenges", n)),
]
}
fn secret_ref() -> impl Strategy<Value = String> {
"[a-z0-9:/_-]{0,24}"
}
const COMMENTED_BASE: &str = "\
# header comment
schema_version = 1 # version
# node section
[node]
name = \"start\" # trailing
# between keys
canonical_origin = \"\"
[limits] # limits
max_body_bytes = 262144
";
fn comment_lines(text: &str) -> Vec<String> {
text.lines()
.filter_map(|l| l.find('#').map(|i| l[i..].to_owned()))
.collect()
}
fn apply_sequence(base: &str, changes: &[Change]) -> String {
let mut text = base.to_owned();
for change in changes {
let p = preview(&text, std::slice::from_ref(change)).expect("preview of a valid change");
assert!(p.is_valid(), "{change:?} gave errors {:?}", p.errors());
p.text().clone_into(&mut text);
}
text
}
proptest! {
#[test]
fn valid_edit_sequences_reparse_to_the_same_values(
changes in proptest::collection::vec(valid_change(), 0..16)
) {
let text = apply_sequence(COMMENTED_BASE, &changes);
let base = ConfigDocument::parse(COMMENTED_BASE).expect("base valid");
let mut model: BTreeMap<&str, Value> = schema()
.fields()
.map(|f| (f.path(), base.value(f.path()).expect("value").clone()))
.collect();
for change in &changes {
let path = schema().field(change.path()).expect("known path").path();
model.insert(path, change.value().clone());
}
let doc = ConfigDocument::parse(&text).expect("result re-parses");
for (path, want) in &model {
prop_assert_eq!(doc.value(path), Some(want), "value of {}", path);
}
prop_assert_eq!(
comment_lines(&text),
comment_lines(COMMENTED_BASE),
"comments preserved in order"
);
}
#[test]
fn batched_preview_matches_sequential_application(
changes in proptest::collection::vec(valid_change(), 0..16)
) {
let mut last: BTreeMap<String, Change> = BTreeMap::new();
for change in &changes {
last.insert(change.path().to_owned(), change.clone());
}
let batch: Vec<Change> = last.into_values().collect();
let batched = preview(COMMENTED_BASE, &batch).expect("batched preview");
let sequential = ConfigDocument::parse(&apply_sequence(COMMENTED_BASE, &changes))
.expect("sequential valid");
let batched = batched.document().expect("batched valid");
for field in schema().fields() {
prop_assert_eq!(batched.value(field.path()), sequential.value(field.path()));
}
}
#[test]
fn public_hash_ignores_secret_references(
changes in proptest::collection::vec(valid_change(), 0..12),
first in secret_ref(),
second in secret_ref(),
) {
let text = apply_sequence(&schema().seed_text(), &changes);
let hash_with = |secret: &str| {
let p = preview(&text, &[Change::new(SECRET_PATH, secret)]).expect("preview");
p.document().expect("valid").public_hash()
};
prop_assert_eq!(hash_with(&first), hash_with(&second));
}
#[test]
fn public_hash_tracks_every_public_change(
changes in proptest::collection::vec(valid_change(), 0..12),
change in valid_change().prop_filter("public fields only", |c| c.path() != SECRET_PATH),
) {
let text = apply_sequence(&schema().seed_text(), &changes);
let before = ConfigDocument::parse(&text).expect("valid");
let p = preview(&text, std::slice::from_ref(&change)).expect("preview");
let after = p.document().expect("valid");
if p.changes().is_empty() {
prop_assert_eq!(after.public_hash(), before.public_hash());
} else {
prop_assert_ne!(after.public_hash(), before.public_hash());
}
}
}