use super::*;
fn rendezvous(root: &Path) -> Rendezvous {
Rendezvous {
version: "1.2.3".to_string(),
pid: 4242,
http_address: std::net::IpAddr::V4(std::net::Ipv4Addr::LOCALHOST),
http_port: 3000,
workspace_root: root.to_path_buf(),
started_at: 1_700_000_000,
}
}
#[test]
fn test9850_a_rendezvous_survives_the_round_trip() {
let dir = tempfile::tempdir().expect("a scratch directory");
let written = rendezvous(dir.path());
written.write(dir.path()).expect("the file is written");
let read = Rendezvous::read(dir.path())
.expect("the file is readable")
.expect("the file is there");
assert_eq!(read, written);
}
#[test]
fn test9851_no_file_is_no_daemon_rather_than_a_failure() {
let dir = tempfile::tempdir().expect("a scratch directory");
assert_eq!(
Rendezvous::read(dir.path()).expect("an absent file is not an error"),
None
);
}
#[test]
fn test9852_a_file_that_is_not_a_rendezvous_is_reported() {
let dir = tempfile::tempdir().expect("a scratch directory");
std::fs::write(Rendezvous::path_in(dir.path()), b"{not json at all")
.expect("the fixture is written");
let error = Rendezvous::read(dir.path())
.expect_err("a file that is not a rendezvous must be reported");
assert!(
error.contains("not a daemon rendezvous file"),
"the error must say what the file is not: {error}"
);
}
#[test]
fn test9853_removing_an_absent_rendezvous_succeeds() {
let dir = tempfile::tempdir().expect("a scratch directory");
rendezvous(dir.path())
.write(dir.path())
.expect("the file is written");
Rendezvous::remove(dir.path()).expect("the first removal succeeds");
Rendezvous::remove(dir.path()).expect("removing an absent file succeeds");
assert_eq!(Rendezvous::read(dir.path()).expect("readable"), None);
}
#[test]
fn test9854_the_rendezvous_carries_no_credential() {
let dir = tempfile::tempdir().expect("a scratch directory");
rendezvous(dir.path())
.write(dir.path())
.expect("the file is written");
let text = std::fs::read_to_string(Rendezvous::path_in(dir.path()))
.expect("the file is readable")
.to_lowercase();
for forbidden in ["key", "token", "secret", "passphrase", "session"] {
assert!(
!text.contains(forbidden),
"the rendezvous file mentions '{forbidden}':\n{text}\n\
Nothing in it may authorise anything — a session is earned with \
the store's passphrase, and a file that grants one makes the \
passphrase optional."
);
}
}
#[cfg(unix)]
#[test]
fn test9855_the_rendezvous_is_private() {
use std::os::unix::fs::PermissionsExt;
let dir = tempfile::tempdir().expect("a scratch directory");
rendezvous(dir.path())
.write(dir.path())
.expect("the file is written");
let mode = std::fs::metadata(Rendezvous::path_in(dir.path()))
.expect("the file is there")
.permissions()
.mode()
& 0o777;
assert_eq!(mode, 0o600, "the rendezvous file must be readable only by its owner");
}
#[test]
fn test9856_rewriting_a_rendezvous_replaces_it_whole() {
let dir = tempfile::tempdir().expect("a scratch directory");
rendezvous(dir.path())
.write(dir.path())
.expect("the first write succeeds");
let mut second = rendezvous(dir.path());
second.pid = 5150;
second.http_port = 3100;
second.write(dir.path()).expect("the second write succeeds");
let read = Rendezvous::read(dir.path())
.expect("readable")
.expect("present");
assert_eq!(read, second, "the second write must be what is read back");
let strays: Vec<String> = std::fs::read_dir(dir.path())
.expect("the directory is readable")
.flatten()
.map(|entry| entry.file_name().to_string_lossy().into_owned())
.filter(|name| name != "daemon.json")
.collect();
assert!(strays.is_empty(), "the write left files behind: {strays:?}");
}