Skip to main content

bash_interop/scratch/
accounts.rs

1//! The account a shell gives of itself, built by hand.
2//!
3//! One builder, so a test that needs the words and a test that needs the shell
4//! read the same one. The numbers are a real 5.3.9's: what is under test
5//! wherever this is used is the arrangement or the walk, never the reading of
6//! this payload — that is tested against a live shell.
7
8use std::sync::Arc;
9
10use bash_strings::emit_array;
11
12use crate::rig::wire::Account;
13use crate::rig::{Micros, Shell, Stamp};
14
15/// The pairs an announcement carries. `zero` is the shell's `$0` and `flags`
16/// its `$-`, which between them decide how a walk taken in it reads.
17pub fn account(pid: u32, zero: &str, flags: &str) -> Vec<String> {
18    let versinfo = emit_array(&["5", "3", "9", "1", "release", "x86_64-pc-linux-gnu"].map(String::from));
19    let command = if flags.contains('c') { "true" } else { "" };
20    let pid = pid.to_string();
21
22    [
23        "pid",
24        pid.as_str(),
25        "shlvl",
26        "5",
27        "subshell",
28        "0",
29        "versinfo",
30        versinfo.as_str(),
31        "bash",
32        "/usr/bin/bash",
33        "zero",
34        zero,
35        "flags",
36        flags,
37        "shellopts",
38        "braceexpand:hashall",
39        "bashopts",
40        "checkwinsize",
41        "command",
42        command,
43        "brought",
44        "()",
45    ]
46    .iter()
47    .map(ToString::to_string)
48    .collect()
49}
50
51/// A shell, as one would arrive.
52pub fn shell(nth: usize, pid: u32, zero: &str, flags: &str) -> Arc<Shell> {
53    let stamp = Stamp {
54        sent_at: Micros(100),
55        heard_at: Micros(101),
56    };
57
58    Arc::new(
59        Shell::of(
60            nth,
61            Account {
62                stamp,
63                words: account(pid, zero, flags),
64            },
65        )
66        .expect("an account"),
67    )
68}
69
70/// A shell bash was handed a file to read.
71pub fn reading(zero: &str) -> Arc<Shell> {
72    shell(0, 7, zero, "hB")
73}
74
75/// A shell bash was given its code directly, where `$0` is a word and not a
76/// path.
77pub fn given(zero: &str) -> Arc<Shell> {
78    shell(0, 7, zero, "hBc")
79}