use super::*; fn executable(domain: &Path) { let bin = domain.join("bin"); fs::create_dir_all(&bin).unwrap(); let source = bin.join("tool.rs"); fs::write( &source, r#"use std::io::{Read,Write}; fn main() { let mut bytes = Vec::new(); std::io::stdin().read_to_end(&mut bytes).unwrap(); let argument = std::env::args().nth(1).unwrap(); std::io::stdout().write_all(argument.as_bytes()).unwrap(); std::io::stdout().write_all(&bytes).unwrap(); std::io::stderr().write_all(b"diagnostic").unwrap(); std::process::exit(7); }"#, ) .unwrap(); success( Command::new("rustc") .args(["+1.95.0", "-C", "target-feature=+crt-static"]) .arg(&source) .arg("-o") .arg(bin.join("tool")) .output() .unwrap(), ); } #[test] fn external_command_runs_without_shell_and_is_embedded_in_published_binary() { let root = project( "external-command", "execute(args: List, stdin: Bytes?) (output: dever.process.Output) fails dever.process.Error", "external command \"bin/tool\" {}", r#"flow start() (code: Int, stdout: Text, stderr: Text) | step | kind | do | | output | port | port.execute(["$(printf wrong) 中文"], dever.bytes.fromText("input")) | | code | calc | output.code | | stdout | calc | dever.bytes.toText(output.stdout) | | stderr | calc | dever.bytes.toText(output.stderr) | "#, ); let domain = root.join("module/demo/service"); executable(&domain); let (loaded, program) = analyze(&root); let target = program .functions .iter() .find(|function| function.signature.name.ends_with("adapter.port.execute")) .unwrap(); assert!(target.effects.process && target.effects.suspends && target.effects.scope()); let artifact = build::build( &loaded, &program, "demo.service.app.start", Profile::Dev, None, ) .unwrap(); let output = root.join("published"); artifact.publish(&output).unwrap(); fs::remove_file(domain.join("bin/tool")).unwrap(); let text = run(&output); assert!(text.contains("7"), "{text}"); assert!(text.contains("diagnostic"), "{text}"); assert!(text.contains("$(printf wrong) 中文input"), "{text}"); success(cli(&["fmt", root.to_str().unwrap()])); success(cli(&["fmt", root.to_str().unwrap(), "--check"])); } #[test] fn external_command_requires_adapter_matching_port_and_explicit_failures() { for (name, port, adapter) in [ ( "missing-fails", "execute(args: List, stdin: Bytes?) (output: dever.process.Output)", "external command \"bin/tool\" {}", ), ( "signature", "execute(args: List) (output: dever.process.Output) fails dever.process.Error", "external command \"bin/tool\" {}", ), ( "escape", "execute(args: List, stdin: Bytes?) (output: dever.process.Output) fails dever.process.Error", "external command \"../tool\" {}", ), ( "limit", "execute(args: List, stdin: Bytes?) (output: dever.process.Output) fails dever.process.Error", "external command \"bin/tool\" { timeout_ms = 0 }", ), ] { let root = project( &format!("external-{name}"), port, adapter, "flow start() (out: dever.process.Output)\n| step | kind | do |\n| out | port | port.execute([], null) |", ); let project = Project::load(&root).unwrap(); assert!( !project.diagnostics.is_empty() || program::check(&project.declarations, &project.modules).is_err(), "{name}" ); } } #[test] fn external_command_failure_can_be_captured_but_not_discarded() { let root = project( "external-capture", "execute(args: List, stdin: Bytes?) (output: dever.process.Output) fails dever.process.Error", "external command \"bin/tool\" { output_limit = 1 }", r#"type Attempt { Done(output: dever.process.Output), error Failed(error: dever.process.Error) } flow start() (out: Attempt) | step | kind | do | | out | port | result(port.execute(["overflow"], null)) | "#, ); executable(&root.join("module/demo/service")); let (loaded, program) = analyze(&root); let artifact = build::build( &loaded, &program, "demo.service.app.start", Profile::Dev, None, ) .unwrap(); let text = run(&artifact.binary); assert!(text.contains("output exceeds combined limit"), "{text}"); write_source( root.join("module/demo/service/app/main.dever"), r#"type Attempt { Done(output: dever.process.Output), error Failed(error: dever.process.Error) } flow capture() (out: Attempt) | step | kind | do | | out | port | result(port.execute(["overflow"], null)) | flow start() (out: Int) | step | kind | do | | ignored | flow | capture() | | out | calc | 1 | "#, ) .unwrap(); let project = Project::load(&root).unwrap(); assert!(program::check(&project.declarations, &project.modules).is_err()); } #[test] fn command_build_rejects_non_elf_and_assets_outside_domain() { let root = project( "external-assets", "execute(args: List, stdin: Bytes?) (output: dever.process.Output) fails dever.process.Error", "external command \"bin/tool\" {}", "flow start() (out: dever.process.Output)\n| step | kind | do |\n| out | port | port.execute([], null) |", ); let domain = root.join("module/demo/service"); executable(&domain); let (loaded, program) = analyze(&root); let binary = domain.join("bin/tool"); let other = match build::Target::host().unwrap() { build::Target::LinuxX86_64 => build::Target::LinuxAarch64, build::Target::LinuxAarch64 => build::Target::LinuxX86_64, }; let error = build::build( &loaded, &program, "demo.service.app.start", Profile::Dev, Some(other), ) .err() .unwrap() .to_string(); assert!( error.contains(&format!("not an executable for {}", other.arch())), "{error}" ); fs::write(&binary, b"#!/bin/sh\nexit 0\n").unwrap(); assert!( build::build( &loaded, &program, "demo.service.app.start", Profile::Dev, None ) .is_err() ); fs::remove_file(&binary).unwrap(); std::os::unix::fs::symlink("/bin/true", &binary).unwrap(); assert!( build::build( &loaded, &program, "demo.service.app.start", Profile::Dev, None ) .is_err() ); } #[test] fn dynamic_command_carries_transitive_libraries_and_cleans_its_private_directory() { let root = project( "dynamic-command", "execute(args: List, stdin: Bytes?) (output: dever.process.Output) fails dever.process.Error", "external command \"bin/tool\" {}", r#"flow start() (first: Text, second: Text) | step | kind | do | | one | port | port.execute([], null) | | two | port | port.execute([], null) | | first | calc | dever.bytes.toText(one.stdout) | | second | calc | dever.bytes.toText(two.stdout) | "#, ); let bin = root.join("module/demo/service/bin"); fs::create_dir_all(&bin).unwrap(); fs::write(bin.join("leaf.c"), "int leaf(void) { return 37; }").unwrap(); fs::write( bin.join("answer.c"), "extern int leaf(void); int answer(void) { return leaf()+5; }", ) .unwrap(); fs::write( bin.join("tool.c"), r#"#include #include extern int answer(void); int main(void) { char path[4096]; ssize_t size=readlink("/proc/self/exe",path,sizeof(path)-1); if(size<0) return 1; path[size]=0; printf("%d:%s",answer(),path); return 0; }"#, ) .unwrap(); for args in [ vec![ "-shared", "-fPIC", "leaf.c", "-Wl,-soname,libdever_leaf.so", "-o", "libdever_leaf.so", ], vec![ "-shared", "-fPIC", "answer.c", "-L.", "-ldever_leaf", "-Wl,-rpath,$ORIGIN", "-Wl,-soname,libdever_answer.so", "-o", "libdever_answer.so", ], vec![ "tool.c", "-L.", "-ldever_answer", "-Wl,-rpath,$ORIGIN", "-o", "tool", ], ] { success( Command::new("cc") .current_dir(&bin) .args(args) .output() .unwrap(), ); } fs::write( root.join("module/demo/service/api.dever"), "cmd start = app.start", ) .unwrap(); let (loaded, program) = analyze(&root); let artifact = build::build(&loaded, &program, "main", Profile::Dev, None).unwrap(); let published = root.join("relocated"); artifact.publish(&published).unwrap(); fs::remove_dir_all(&bin).unwrap(); let output = success( Command::new(&published) .args(["demo.service.start", "{}"]) .env("LD_LIBRARY_PATH", "/does-not-exist") .output() .unwrap(), ); let values: serde_json::Value = serde_json::from_str(&output).unwrap(); let first = values["data"]["first"].as_str().unwrap(); assert!(first.starts_with("42:"), "{output}"); assert_eq!(first, values["data"]["second"].as_str().unwrap()); assert_eq!( Path::new(first.strip_prefix("42:").unwrap()) .file_name() .unwrap(), "program" ); let directory = Path::new(first.strip_prefix("42:").unwrap()) .parent() .unwrap(); assert!( !directory.exists(), "resource directory leaked: {}", directory.display() ); }