mod argv;
use super::*;
use crate::test_support::world_under;
use tempfile::TempDir;
#[test]
fn an_unstated_plan_is_the_worlds_own_loopback() {
let tmp = TempDir::new().expect("tmp");
let world = world_under(tmp.path());
let plan = plan(&world, None, None, None, None, None, None);
assert_eq!(plan.dir, super::super::super::material::dir(&world));
assert_eq!(
plan.act,
Act::Mint {
address: format!("{LOOPBACK}:{PORT}"),
force: false,
},
"nothing stated is the mint it has always been"
);
}
#[test]
fn a_stated_plan_is_taken_and_an_empty_statement_is_not() {
let tmp = TempDir::new().expect("tmp");
let world = world_under(tmp.path());
let stated = plan(
&world,
Some("/elsewhere/wire".to_owned()),
Some("engine.example.com".to_owned()),
Some("7000".to_owned()),
Some("1".to_owned()),
None,
None,
);
assert_eq!(stated, stated.clone(), "a plan is a value, and says so");
assert_eq!(stated.dir, PathBuf::from("/elsewhere/wire"));
assert_eq!(
stated.act,
Act::Mint {
address: "engine.example.com:7000".to_owned(),
force: true,
}
);
let blank = plan(
&world,
Some(String::new()),
Some(String::new()),
Some(String::new()),
Some(String::new()),
Some(String::new()),
Some(String::new()),
);
assert_eq!(blank.dir, super::super::super::material::dir(&world));
assert_eq!(
blank.act,
Act::Mint {
address: format!("{LOOPBACK}:{PORT}"),
force: false,
},
"an empty FORCE is not a rotation, and an empty WIRE_LEAF is not a leaf"
);
}
#[test]
fn a_stated_leaf_is_the_other_act_and_takes_no_mint_facts() {
let tmp = TempDir::new().expect("tmp");
let world = world_under(tmp.path());
let stated = plan(
&world,
None,
Some("engine.example.com".to_owned()),
Some("7000".to_owned()),
Some("1".to_owned()),
Some("phone".to_owned()),
None,
);
assert_eq!(stated.dir, super::super::super::material::dir(&world));
assert_eq!(
stated.act,
Act::Leaf("phone".to_owned(), Grade::Operator),
"an unstated grade is operator, which is what keeps every existing recipe working"
);
}
#[test]
fn a_stated_foot_grades_the_leaf_and_an_empty_one_does_not() {
let tmp = TempDir::new().expect("tmp");
let world = world_under(tmp.path());
let footed = plan(
&world,
None,
None,
None,
None,
Some("box".to_owned()),
Some("1".to_owned()),
);
assert_eq!(footed.act, Act::Leaf("box".to_owned(), Grade::Foot));
let blank = plan(
&world,
None,
None,
None,
None,
Some("box".to_owned()),
Some(String::new()),
);
assert_eq!(blank.act, Act::Leaf("box".to_owned(), Grade::Operator));
}
#[test]
fn it_mints_then_refuses_then_rotates() {
let tmp = TempDir::new().expect("tmp");
let dir = tmp.path().join("wire");
let mut plan = Plan {
dir: dir.clone(),
act: Act::Mint {
address: "127.0.0.1:0".to_owned(),
force: false,
},
};
assert_eq!(perform(&plan), 0, "minted");
let first = std::fs::read(dir.join(ANCHORS)).expect("ca");
assert_eq!(perform(&plan), 1, "refused: material is already here");
assert_eq!(
std::fs::read(dir.join(ANCHORS)).expect("ca"),
first,
"and refused without touching it"
);
plan.act = Act::Mint {
address: "127.0.0.1:0".to_owned(),
force: true,
};
assert_eq!(perform(&plan), 0, "rotated");
assert_ne!(std::fs::read(dir.join(ANCHORS)).expect("ca"), first);
}
#[test]
fn a_mint_that_cannot_run_exits_one() {
let tmp = TempDir::new().expect("tmp");
let blocked = tmp.path().join("file");
std::fs::write(&blocked, b"not a directory").expect("file");
assert_eq!(
perform(&Plan {
dir: blocked,
act: Act::Mint {
address: "127.0.0.1:0".to_owned(),
force: false,
},
}),
1
);
}
#[test]
fn the_leaf_act_issues_once_and_writes_nothing_else() {
let tmp = TempDir::new().expect("tmp");
let dir = tmp.path().join("wire");
let leaf = Plan {
dir: dir.clone(),
act: Act::Leaf("phone".to_owned(), Grade::Operator),
};
assert_eq!(perform(&leaf), 1, "nothing to issue under yet");
assert_eq!(
perform(&Plan {
dir: dir.clone(),
act: Act::Mint {
address: "127.0.0.1:0".to_owned(),
force: false,
},
}),
0,
"minted"
);
let ca = std::fs::read(dir.join(ANCHORS)).expect("ca");
let address = std::fs::read(dir.join(super::super::ADDRESS)).expect("address");
assert_eq!(perform(&leaf), 0, "issued");
assert_eq!(perform(&leaf), 1, "and refuses a second under one name");
let mut left: Vec<String> = std::fs::read_dir(&dir)
.expect("dir")
.filter_map(|e| Some(e.ok()?.file_name().to_string_lossy().into_owned()))
.collect();
left.sort();
let mut named = super::super::artifacts();
named.push("phone.key".to_owned());
named.push("phone.pem".to_owned());
named.sort();
assert_eq!(
left, named,
"the mint's artifacts and exactly one more pair"
);
assert_eq!(std::fs::read(dir.join(ANCHORS)).expect("ca"), ca);
assert_eq!(
std::fs::read(dir.join(super::super::ADDRESS)).expect("address"),
address
);
}
#[test]
fn an_unusable_common_name_refuses() {
let tmp = TempDir::new().expect("tmp");
assert_eq!(
perform(&Plan {
dir: tmp.path().to_owned(),
act: Act::Leaf("a/b".to_owned(), Grade::Operator),
}),
1
);
assert_eq!(std::fs::read_dir(tmp.path()).expect("dir").count(), 0);
}
#[test]
fn the_reads_are_named_once() {
assert_eq!(
READS,
[
"WIRE_DIR",
"WIRE_HOST",
"WIRE_PORT",
"FORCE",
"WIRE_LEAF",
"WIRE_FOOT"
]
);
}
#[test]
fn the_verb_issues_a_foot_leaf_beside_an_ordinary_one() {
let tmp = TempDir::new().expect("tmp");
let dir = tmp.path().join("wire");
assert_eq!(
perform(&Plan {
dir: dir.clone(),
act: Act::Mint {
address: "127.0.0.1:0".to_owned(),
force: false,
},
}),
0,
"minted"
);
assert_eq!(
perform(&Plan {
dir: dir.clone(),
act: Act::Leaf("host".to_owned(), Grade::Foot),
}),
0,
"issued a foot"
);
assert!(dir.join("host.pem").is_file() && dir.join("host.key").is_file());
assert_eq!(word(Grade::Foot), crate::registry::peer::FOOT);
assert_eq!(word(Grade::Operator), "client");
}