use super::{ADDRESS, ANCHORS, CHAIN, KEY, Whose, read_dir};
use crate::test_support::Scratch;
use std::path::Path;
fn provision(dir: &Path, address: &str) {
for file in [ANCHORS, CHAIN, KEY] {
std::fs::write(dir.join(file), b"pem bytes stand in here").expect("write");
}
std::fs::write(dir.join(ADDRESS), address).expect("write");
}
#[test]
fn nothing_provisioned_is_an_answer_and_not_an_error() {
let tmp = Scratch::new();
assert_eq!(read_dir(tmp.path(), Whose::Elsewhere), Ok(None));
}
#[test]
fn a_directory_that_is_not_there_reads_as_nothing_provisioned() {
let tmp = Scratch::new();
assert_eq!(
read_dir(&tmp.path().join("never-made"), Whose::Elsewhere),
Ok(None)
);
}
#[test]
fn a_provisioned_directory_answers_its_material() {
let tmp = Scratch::new();
provision(tmp.path(), " server.example:9000\n");
let held = read_dir(tmp.path(), Whose::Elsewhere)
.expect("readable")
.expect("provisioned");
assert_eq!(held.addresses, ["server.example:9000"]);
assert_eq!(held.anchors, tmp.path().join(ANCHORS));
assert_eq!(held.chain, tmp.path().join(CHAIN));
assert_eq!(held.key, tmp.path().join(KEY));
}
#[test]
fn a_half_provisioned_directory_names_every_gap_at_once() {
let tmp = Scratch::new();
provision(tmp.path(), "server.example:9000");
std::fs::remove_file(tmp.path().join(KEY)).expect("rm");
std::fs::remove_file(tmp.path().join(ADDRESS)).expect("rm");
let refusal = read_dir(tmp.path(), Whose::Elsewhere).expect_err("refused");
assert!(refusal.contains(KEY), "{refusal}");
assert!(refusal.contains(ADDRESS), "{refusal}");
assert!(refusal.contains("half-provisioned"), "{refusal}");
}
#[test]
fn every_refusal_names_registering_and_the_road_by_hand() {
let tmp = Scratch::new();
provision(tmp.path(), "server.example:9000");
std::fs::remove_file(tmp.path().join(ANCHORS)).expect("rm");
let refusal = read_dir(tmp.path(), Whose::Elsewhere).expect_err("refused");
assert!(refusal.contains("`lernie register <url>`"), "{refusal}");
assert!(refusal.contains("By hand instead"), "{refusal}");
assert!(
refusal.contains("`WIRE_LEAF=<name> yog wire-certs`"),
"{refusal}"
);
for named in [ANCHORS, CHAIN, KEY, ADDRESS] {
assert!(refusal.contains(named), "{named}: {refusal}");
}
}
#[test]
fn an_address_that_says_nothing_refuses_however_it_says_it() {
for stated in ["", " \n"] {
let tmp = Scratch::new();
provision(tmp.path(), stated);
let refusal = read_dir(tmp.path(), Whose::Elsewhere).expect_err("refused");
assert!(refusal.contains("names no address"), "{refusal}");
}
let tmp = Scratch::new();
provision(tmp.path(), "server.example:9000");
std::fs::remove_file(tmp.path().join(ADDRESS)).expect("rm");
std::fs::create_dir(tmp.path().join(ADDRESS)).expect("mkdir");
let refusal = read_dir(tmp.path(), Whose::Elsewhere).expect_err("refused");
assert!(refusal.contains("half-provisioned"), "{refusal}");
}
#[test]
fn an_address_file_of_several_lines_is_dialled_in_the_order_it_states() {
let tmp = Scratch::new();
provision(
tmp.path(),
"engine.example:7737\n\n 192.0.2.7:7737\n[2001:db8::7]:7737\n",
);
let held = read_dir(tmp.path(), Whose::Elsewhere)
.expect("readable")
.expect("provisioned");
assert_eq!(
held.addresses,
[
"engine.example:7737",
"192.0.2.7:7737",
"[2001:db8::7]:7737"
]
);
assert_eq!(
held.address(),
"engine.example:7737, 192.0.2.7:7737, [2001:db8::7]:7737"
);
}
#[test]
fn the_retired_rendezvous_files_are_ignored_where_an_entry_still_has_them() {
let tmp = Scratch::new();
provision(tmp.path(), "server.example:9000");
let bare = read_dir(tmp.path(), Whose::Elsewhere);
std::fs::write(tmp.path().join("rendezvous.pub"), "not a key").expect("write");
std::fs::write(tmp.path().join("pairing.salt"), "").expect("write");
assert_eq!(read_dir(tmp.path(), Whose::Elsewhere), bare);
assert!(bare.expect("readable").is_some());
}