use crate::core::types::{MachineTarget, Resource, ResourceType};
use crate::resources::file::apply_script;
struct Sandbox {
dir: tempfile::TempDir,
}
impl Sandbox {
fn new() -> Self {
Self {
dir: tempfile::tempdir().expect("tempdir"),
}
}
fn target(&self) -> String {
self.dir.path().join("managed.conf").display().to_string()
}
fn canary(&self) -> String {
self.dir.path().join("PWNED").display().to_string()
}
fn canary_exists(&self) -> bool {
self.dir.path().join("PWNED").exists()
}
}
fn file_resource(path: &str, content: &str) -> Resource {
Resource {
resource_type: ResourceType::File,
machine: MachineTarget::Single("m1".to_string()),
path: Some(path.to_string()),
state: Some("file".to_string()),
content: Some(content.to_string()),
..Default::default()
}
}
fn apply(sb: &Sandbox, content: &str) -> i32 {
let r = file_resource(&sb.target(), content);
let script = apply_script(&r);
let out = crate::transport::local::exec_local(&script, None).expect("bash is available");
out.exit_code
}
fn assert_written_verbatim(sb: &Sandbox, content: &str) {
let code = apply(sb, content);
assert!(
!sb.canary_exists(),
"content ESCAPED and executed as shell on the target: {} was created by content {:?}",
sb.canary(),
content
);
assert_eq!(code, 0, "apply script did not converge for {content:?}");
let on_disk = std::fs::read(sb.target()).unwrap_or_default();
assert_eq!(
String::from_utf8_lossy(&on_disk),
content,
"file on disk is not byte-exact ({} bytes written, {} declared)",
on_disk.len(),
content.len()
);
}
#[test]
fn content_equal_to_the_delimiter_is_written_not_interpreted() {
let sb = Sandbox::new();
assert_written_verbatim(&sb, "FORJAR_EOF");
}
#[test]
fn delimiter_mid_file_does_not_truncate_the_file() {
let sb = Sandbox::new();
assert_written_verbatim(&sb, "alpha\nFORJAR_EOF\nbeta\n");
}
#[test]
fn delimiter_plus_trailing_shell_does_not_execute_and_does_not_report_converged() {
let sb = Sandbox::new();
let content = format!(
"harmless=1\nFORJAR_EOF\ntouch {}\ncat > /dev/null <<'FORJAR_EOF'\nswallowed",
sb.canary()
);
assert_written_verbatim(&sb, &content);
}
#[test]
fn delimiter_plus_trailing_shell_with_command_substitution() {
let sb = Sandbox::new();
let content = format!(
"x\nFORJAR_EOF\necho $(touch {}) >/dev/null\ncat > /dev/null <<'FORJAR_EOF'\nswallowed",
sb.canary()
);
assert_written_verbatim(&sb, &content);
}
#[test]
fn delimiter_with_trailing_space_is_preserved_verbatim() {
let sb = Sandbox::new();
assert_written_verbatim(&sb, "alpha\nFORJAR_EOF \nbeta\n");
}
#[test]
fn delimiter_with_trailing_tab_is_preserved_verbatim() {
let sb = Sandbox::new();
assert_written_verbatim(&sb, "alpha\nFORJAR_EOF\t\nbeta\n");
}
#[test]
fn delimiter_with_leading_whitespace_is_preserved_verbatim() {
let sb = Sandbox::new();
assert_written_verbatim(&sb, "alpha\n FORJAR_EOF\n\tFORJAR_EOF\nbeta\n");
}
#[test]
fn crlf_delimiter_line_is_preserved_verbatim() {
let sb = Sandbox::new();
assert_written_verbatim(&sb, "alpha\r\nFORJAR_EOF\r\nbeta\r\n");
}
#[test]
fn crlf_payload_with_trailing_shell_does_not_execute() {
let sb = Sandbox::new();
let content = format!(
"x\r\nFORJAR_EOF\r\ntouch {}\r\ncat > /dev/null <<'FORJAR_EOF'\r\nswallowed",
sb.canary()
);
assert_written_verbatim(&sb, &content);
}
#[test]
fn content_containing_every_delimiter_forjar_might_pick() {
let sb = Sandbox::new();
let mut content = String::new();
for marker in ["FORJAR_EOF", "FORJAR_SUDO", "FORJAR_B64", "EOF", "EOT"] {
content.push_str(&format!("{marker}\n"));
for n in 0..8 {
content.push_str(&format!("{marker}_{n}\n"));
}
}
content.push_str(&format!("touch {}\n", sb.canary()));
assert_written_verbatim(&sb, &content);
}
#[test]
fn content_without_a_trailing_newline_is_not_padded() {
let sb = Sandbox::new();
assert_written_verbatim(&sb, "no-trailing-newline");
}
#[test]
fn content_with_a_trailing_newline_is_not_doubled() {
let sb = Sandbox::new();
assert_written_verbatim(&sb, "key=value\n");
}
#[test]
fn empty_content_writes_an_empty_file() {
let sb = Sandbox::new();
assert_written_verbatim(&sb, "");
}
#[test]
fn shell_metacharacters_are_written_literally_not_expanded() {
let sb = Sandbox::new();
assert_written_verbatim(&sb, "$HOME ${PATH} $(whoami) `id` \"q\" 'q' \\ ; | &\n");
}
#[test]
fn unicode_and_tabs_survive_byte_for_byte() {
let sb = Sandbox::new();
assert_written_verbatim(&sb, "café ☕\tπ = 3.14159\n日本語\n");
}
proptest::proptest! {
#![proptest_config(proptest::prelude::ProptestConfig::with_cases(48))]
#[test]
fn arbitrary_content_round_trips_byte_exact(
parts in proptest::collection::vec(
proptest::sample::select(vec![
"FORJAR_EOF", "FORJAR_SUDO", "FORJAR_B64", "EOF", "\n", "\r\n", "\r",
" ", "\t", "a", "$(id)", "'", "\"", "\\", ";", "#", "café",
]),
0..24,
)
) {
let content: String = parts.concat();
let sb = Sandbox::new();
let r = file_resource(&sb.target(), &content);
let script = apply_script(&r);
let out = crate::transport::local::exec_local(&script, None).expect("bash");
proptest::prop_assert!(!sb.canary_exists());
proptest::prop_assert_eq!(out.exit_code, 0, "apply failed for {:?}", content);
let on_disk = std::fs::read(sb.target()).unwrap_or_default();
proptest::prop_assert_eq!(
String::from_utf8_lossy(&on_disk).to_string(),
content
);
}
}