//! The program under both its names: `wai-key-release`, as `wai serve key-release` starts it, and //! `wai-key-proxy`, its earlier name, the same program. `serve --check` reads the //! configuration as the proxy starts from it (its signing key and its lists among it), starts //! nothing, and refuses one that does not read, exit 1, with the reason. use std::path::Path; use std::process::{Command, Output, Stdio}; fn run(bin: &str, args: &[&str]) -> Output { Command::new(bin).args(args).stdin(Stdio::null()).output().expect("it runs") } #[test] fn both_names_check_a_configuration_and_start_nothing() { let d = std::env::temp_dir().join(format!("wai-key-release-check-{}", std::process::id())); let _ = std::fs::remove_dir_all(&d); std::fs::create_dir_all(&d).unwrap(); let key = d.join("proxy.key"); let release = env!("CARGO_BIN_EXE_wai-key-release"); let proxy = env!("CARGO_BIN_EXE_wai-key-proxy"); assert!(run(release, &["keygen", key.to_str().unwrap()]).status.success()); let config = |authorities: &str| { format!( r#"{{"listen": "127.0.0.1:0", "signer_key": {}, "signer_id": "did:example:proxy", "authorities": [{authorities}], "lists": [], "routes": [], "log": {}}}"#, serde_json::to_string(key.to_str().unwrap()).unwrap(), serde_json::to_string(d.join("log.jsonl").to_str().unwrap()).unwrap() ) }; let good = d.join("proxy.json"); std::fs::write(&good, config(&format!("\"{}\"", "ab".repeat(32)))).unwrap(); let bad = d.join("bad.json"); std::fs::write(&bad, config("\"not-a-key\"")).unwrap(); for (bin, name) in [(release, "wai-key-release"), (proxy, "wai-key-proxy")] { let o = run(bin, &["serve", good.to_str().unwrap(), "--check"]); assert!(o.status.success(), "{name}: {}", String::from_utf8_lossy(&o.stderr)); assert_eq!(String::from_utf8_lossy(&o.stdout), format!("{name}: {} reads as the proxy would start from it\n", good.display()), "{name}"); let o = run(bin, &["serve", bad.to_str().unwrap(), "--check"]); assert_eq!(o.status.code(), Some(1), "{name}"); assert_eq!(String::from_utf8_lossy(&o.stderr), format!("{name}: authority not-a-key: not 64 lowercase hexadecimal digits\n"), "{name}"); let o = run(bin, &["serve", d.join("gone.json").to_str().unwrap(), "--check"]); assert_eq!(o.status.code(), Some(1), "{name}"); } // Nothing was started, so nothing wrote the log. assert!(!Path::new(&d.join("log.jsonl")).exists(), "a check opens no log"); let _ = std::fs::remove_dir_all(&d); }