diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/receptor.rs | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/tests/receptor.rs b/tests/receptor.rs index fb99497..170fbae 100644 --- a/tests/receptor.rs +++ b/tests/receptor.rs @@ -644,3 +644,101 @@ if env | grep -v '^PWD=\|^SHLVL=\|^_=\|^OLDPWD=' | grep -q .; then object "has a assert!(plan.accepted, "{plan:?}"); assert!(!escaped.exists()); } + +/// Locations: a source declared once, in a file the source list includes, used by +/// items with variables of their own; and the mistakes. +#[test] +fn pack_from_locations() { + let host = Host::new("locations"); + let schema = host.schema(secrets_schema(&host)); + let secrets = host.program("secrets", r#"printf '%s of %s\n' "$3" "$1""#); + std::fs::create_dir_all(host.root.join("shared")).unwrap(); + std::fs::write( + host.root.join("shared/locations.json"), + json!({ "locations": { "secrets": { + "source": { "command": [secrets, "$(env:LOCATION_HOST)", "show", "$(var:key)"] } + } } }) + .to_string(), + ) + .unwrap(); + let list = |items: Value| { + let path = host.root.join("sources.json"); + std::fs::write( + &path, + json!({ + "channel": "secrets", + "include": ["shared/locations.json"], + "items": items + }) + .to_string(), + ) + .unwrap(); + path + }; + // SAFETY: the tests here don't read the environment concurrently. + unsafe { std::env::set_var("LOCATION_HOST", "ru") }; + let out = host.root.join("packed"); + enstate::pack::pack( + &list(json!({ + "a.key": { "location": "secrets", "key": "retiolum.ed25519_key.priv; rm -rf /" }, + "a.pub": { "location": "secrets", "key": "retiolum.ed25519_key.pub" } + })), + &out, + Some(&schema.0), + ) + .unwrap(); + // The variable went in as one argument, as data. + assert_eq!( + std::fs::read(out.join("channels/secrets/a.key")).unwrap(), + b"retiolum.ed25519_key.priv; rm -rf / of ru\n" + ); + assert_eq!( + std::fs::read(out.join("channels/secrets/a.pub")).unwrap(), + b"retiolum.ed25519_key.pub of ru\n" + ); + + let mistake = |items: Value| { + let out = host.root.join("mistake"); + let _ = std::fs::remove_dir_all(&out); + format!( + "{:#}", + enstate::pack::pack(&list(items), &out, None).unwrap_err() + ) + }; + let said = mistake(json!({ "a.key": { "location": "nope", "key": "x" } })); + assert!(said.contains("no location nope"), "{said}"); + let said = mistake(json!({ "a.key": { "location": "secrets" } })); + assert!(said.contains("key"), "{said}"); + let said = mistake(json!({ "a.key": { "source": "env:X", "key": "x" } })); + assert!(said.contains("unknown fields key"), "{said}"); + let said = mistake(json!({ "a.key": { "location": "secrets", "key": 5 } })); + assert!(said.contains("variable key is 5, not text"), "{said}"); + std::fs::write( + host.root.join("shared/more.json"), + json!({ "locations": { "secrets": { "source": "env:X" } } }).to_string(), + ) + .unwrap(); + std::fs::write( + host.root.join("twice.json"), + json!({ + "channel": "secrets", + "include": ["shared/locations.json", "shared/more.json"], + "items": {} + }) + .to_string(), + ) + .unwrap(); + let said = format!( + "{:#}", + enstate::pack::pack( + &host.root.join("twice.json"), + &host.root.join("twice"), + None + ) + .unwrap_err() + ); + assert!( + said.contains("location secrets is declared twice"), + "{said}" + ); +} |
