use super::super::super::ANCHORS;
use super::super::{Act, Plan, hosts, perform};
use crate::wire::material::{ADDRESS, Role};
use tempfile::TempDir;
#[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 {
hosts: Vec::new(),
port: "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 {
hosts: Vec::new(),
port: "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 {
hosts: Vec::new(),
port: "0".to_owned(),
force: false,
},
}),
1
);
}
#[test]
fn a_stated_host_is_a_list_of_them() {
assert_eq!(hosts(None), Vec::<String>::new());
assert_eq!(hosts(Some("")), Vec::<String>::new());
assert_eq!(hosts(Some(", ,")), Vec::<String>::new());
assert_eq!(hosts(Some("engine.example.com")), ["engine.example.com"]);
assert_eq!(
hosts(Some(" engine.example.com , 192.0.2.7 ,")),
["engine.example.com", "192.0.2.7"]
);
}
#[test]
fn a_stated_host_over_standing_material_re_issues_the_server_leaf() {
let tmp = TempDir::new().expect("tmp");
let dir = tmp.path().join("wire");
let stated = |hosts: &[&str]| Plan {
dir: dir.clone(),
act: Act::Mint {
hosts: hosts.iter().map(|h| (*h).to_owned()).collect(),
port: "7737".to_owned(),
force: false,
},
};
assert_eq!(perform(&stated(&[])), 0, "minted");
let ca = std::fs::read(dir.join(ANCHORS)).expect("ca");
let address = std::fs::read(dir.join(ADDRESS)).expect("address");
let client = std::fs::read(dir.join("client.pem")).expect("client leaf");
let server = std::fs::read(dir.join("server.pem")).expect("server leaf");
assert_eq!(
perform(&stated(&["engine.example.com", "192.0.2.7"])),
0,
"re-issued rather than refused"
);
assert_ne!(
std::fs::read(dir.join("server.pem")).expect("server leaf"),
server,
"the one artifact the act replaces"
);
assert_eq!(std::fs::read(dir.join(ANCHORS)).expect("ca"), ca);
assert_eq!(
std::fs::read(dir.join("client.pem")).expect("client leaf"),
client
);
assert_eq!(std::fs::read(dir.join(ADDRESS)).expect("address"), address);
assert!(Role::Server.leaf() == "server");
}
#[test]
fn a_re_issue_a_client_box_cannot_perform_exits_one() {
let tmp = TempDir::new().expect("tmp");
std::fs::write(tmp.path().join(ANCHORS), b"an operator's anchors").expect("anchors");
assert_eq!(
perform(&Plan {
dir: tmp.path().to_owned(),
act: Act::Mint {
hosts: vec!["engine.example.com".to_owned()],
port: "7737".to_owned(),
force: false,
},
}),
1
);
}