1
//! Helpers shared by the integration tests that run the built `nms` binary.
2

            
3
use std::path::PathBuf;
4
use std::process::{Command, Output};
5

            
6
29
pub fn nms_bin() -> PathBuf {
7
    // CARGO_BIN_EXE_<name> is set by cargo when building integration
8
    // tests for a crate with a `[[bin]]` of that name.
9
29
    PathBuf::from(env!("CARGO_BIN_EXE_nms"))
10
29
}
11

            
12
23
pub fn tempfile_dir(prefix: &str) -> PathBuf {
13
23
    let dir = std::env::temp_dir().join(format!(
14
23
        "{prefix}{}",
15
23
        std::time::SystemTime::now()
16
23
            .duration_since(std::time::UNIX_EPOCH)
17
23
            .unwrap()
18
23
            .as_nanos()
19
23
    ));
20
23
    std::fs::create_dir_all(&dir).unwrap();
21
23
    dir
22
23
}
23

            
24
/// Runs `source` through `nms` in a subprocess limited to a `stack_kib`
25
/// native stack, so a compiler stack overflow kills only the child.
26
#[cfg(unix)]
27
20
pub fn run_nms_with_stack_limit(prefix: &str, source: &str, stack_kib: u32) -> Output {
28
20
    nms_with_stack_limit(prefix, source, stack_kib, &[])
29
20
}
30

            
31
/// [`run_nms_with_stack_limit`] with extra `nms` flags before the source path.
32
#[cfg(unix)]
33
21
pub fn nms_with_stack_limit(prefix: &str, source: &str, stack_kib: u32, flags: &[&str]) -> Output {
34
21
    let dir = tempfile_dir(prefix);
35
21
    let path = dir.join("chain.nms");
36
21
    std::fs::write(&path, source).unwrap();
37
21
    let out = Command::new("sh")
38
21
        .arg("-c")
39
21
        .arg(format!("ulimit -s {stack_kib} && exec \"$@\""))
40
21
        .arg("sh")
41
21
        .arg(nms_bin())
42
21
        .args(flags)
43
21
        .arg(&path)
44
21
        .current_dir(&dir)
45
21
        .output()
46
21
        .expect("invoke nms under a constrained stack");
47
21
    std::fs::remove_dir_all(&dir).ok();
48
21
    out
49
21
}
50

            
51
/// Asserts the child was not killed by a signal (a stack overflow aborts)
52
/// and failed with `needle` in its stderr.
53
#[cfg(unix)]
54
11
pub fn assert_fails_cleanly_with(out: &Output, needle: &str) {
55
    use std::os::unix::process::ExitStatusExt;
56
11
    assert_eq!(
57
11
        out.status.signal(),
58
11
        None,
59
        "must fail cleanly, not crash: {:?}",
60
        out.status
61
    );
62
11
    let stderr = String::from_utf8_lossy(&out.stderr);
63
11
    assert!(!out.status.success(), "expected failure, stderr: {stderr}");
64
11
    assert!(stderr.contains(needle), "stderr: {stderr}");
65
11
}
66

            
67
/// Runs `source` under a `stack_kib` stack and asserts it prints
68
/// `expected_value`. A constrained stack (2 MiB) is deliberate: an 8 MiB
69
/// stack survives overflows an ordinary thread stack would not.
70
#[cfg(unix)]
71
9
pub fn assert_nms_source_prints_with_stack_limit(
72
9
    name_prefix: &str,
73
9
    source: &str,
74
9
    expected_value: &str,
75
9
    stack_kib: u32,
76
9
) {
77
9
    let out = run_nms_with_stack_limit(name_prefix, source, stack_kib);
78
    use std::os::unix::process::ExitStatusExt;
79
9
    assert_eq!(
80
9
        out.status.signal(),
81
9
        None,
82
        "nms subprocess was killed by a signal (status: {:?}) under a {stack_kib} KiB stack — \
83
         likely a native stack overflow (SIGABRT)",
84
        out.status
85
    );
86
9
    let stdout = String::from_utf8_lossy(&out.stdout);
87
9
    let stderr = String::from_utf8_lossy(&out.stderr);
88
9
    assert!(
89
9
        out.status.success(),
90
        "expected success, got {:?}\nstdout: {stdout}\nstderr: {stderr}",
91
        out.status
92
    );
93
9
    assert!(
94
9
        stdout.trim() == expected_value,
95
        "expected {expected_value:?}, got stdout: {stdout}\nstderr: {stderr}"
96
    );
97
9
}