bashkit 0.20.1

Awesomely fast virtual sandbox with bash and file system
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
//! Live command I/O must make progress before producers finish.

use async_trait::async_trait;
use bashkit::{Bash, Command, CommandContext, CommandStatus};
use std::sync::Arc;
use tokio::sync::Notify;

struct Copy;

#[tokio::test]
async fn input_process_substitution_captures_streaming_producers() {
    for script in [
        "paste <(seq 3) <(seq 4 6)",
        "paste <(seq 3 | copy) <(seq 4 6 | copy)",
        "paste <(seq 3) <(seq 4 6) | copy",
    ] {
        let mut bash = Bash::builder().command("copy", Copy).build();
        let result = bash.execute(script).capture(1024).await.unwrap();
        assert_eq!(result.exit_code, 0, "{script}");
        assert_eq!(result.stdout.as_bytes(), b"1\t4\n2\t5\n3\t6\n", "{script}");
        assert!(result.stderr.is_empty(), "{script}");
    }
}

struct MixedOutput;
#[async_trait]
impl Command for MixedOutput {
    async fn execute(&self, ctx: CommandContext<'_>) -> bashkit::Result<CommandStatus> {
        ctx.stdout.write_all(b"a").await?;
        ctx.stderr.write_all(b"b").await?;
        ctx.stdout.write_all(b"c").await?;
        Ok(CommandStatus::success())
    }
}

#[tokio::test]
async fn command_redirects_preserve_write_and_duplication_order() {
    let mut bash = Bash::builder().command("mixed", MixedOutput).build();
    let result = bash.execute("mixed 2>&1").capture(64).await.unwrap();
    assert_eq!(result.stdout.as_bytes(), b"abc");
    assert!(result.stderr.is_empty());
    let result = bash
        .execute("mixed >/out 2>&1; cat /out")
        .capture(64)
        .await
        .unwrap();
    assert_eq!(result.stdout.as_bytes(), b"abc");
    let result = bash
        .execute("mixed 2>&1 >/out; cat /out")
        .capture(64)
        .await
        .unwrap();
    assert_eq!(result.stdout.as_bytes(), b"bac");
}

#[tokio::test]
async fn redirected_command_writes_are_visible_before_it_completes() {
    let release = Arc::new(Notify::new());
    let mut bash = Bash::builder()
        .command(
            "produce",
            GatedProducer {
                release: release.clone(),
            },
        )
        .build();
    let fs = bash.fs();
    let execution = async { bash.execute("produce >/out").capture(64).await };
    let observer = async {
        loop {
            if fs
                .read_file(std::path::Path::new("/out"))
                .await
                .is_ok_and(|b| b == b"error: first\n")
            {
                release.notify_one();
                break;
            }
            tokio::task::yield_now().await;
        }
    };
    let (result, ()) = tokio::time::timeout(std::time::Duration::from_secs(2), async {
        tokio::join!(execution, observer)
    })
    .await
    .unwrap();
    assert!(result.unwrap().stdout.is_empty());
    assert_eq!(
        fs.read_file(std::path::Path::new("/out")).await.unwrap(),
        b"error: first\nerror: last\n"
    );
}

struct BinaryProducer {
    handle_broken_pipe: bool,
}

#[async_trait]
impl Command for BinaryProducer {
    async fn execute(&self, ctx: CommandContext<'_>) -> bashkit::Result<CommandStatus> {
        let bytes: Vec<u8> = (0..1_100_000).map(|n| (n % 256) as u8).collect();
        match ctx.stdout.write_all(&bytes).await {
            Err(bashkit::Error::Io(e))
                if self.handle_broken_pipe && e.kind() == std::io::ErrorKind::BrokenPipe =>
            {
                Ok(CommandStatus::with_code(23))
            }
            result => {
                result?;
                Ok(CommandStatus::success())
            }
        }
    }
}

#[tokio::test]
async fn more_than_one_megabyte_crosses_command_and_builtin_stages_unchanged() {
    let mut bash = Bash::builder()
        .limits(bashkit::ExecutionLimits::default().max_stdout_bytes(1_200_000))
        .command(
            "binary",
            BinaryProducer {
                handle_broken_pipe: false,
            },
        )
        .command("copy", Copy)
        .build();
    let result = bash
        .execute("binary | copy | cat")
        .capture(1_200_000)
        .await
        .unwrap();
    assert_eq!(result.stdout.len(), 1_100_000);
    assert!(
        result
            .stdout
            .as_bytes()
            .iter()
            .enumerate()
            .all(|(n, &b)| b == (n % 256) as u8)
    );
    assert!(!result.stdout_truncated);
}

#[tokio::test]
async fn handled_broken_pipe_keeps_handler_status() {
    let mut bash = Bash::builder()
        .command(
            "binary",
            BinaryProducer {
                handle_broken_pipe: true,
            },
        )
        .build();
    let result = bash
        .execute("binary | head -c1; printf ':%s' \"${PIPESTATUS[*]}\"")
        .capture(64)
        .await
        .unwrap();
    assert_eq!(result.stdout.as_bytes(), b"\0:23 0");
}

#[async_trait]
impl Command for Copy {
    async fn execute(&self, ctx: CommandContext<'_>) -> bashkit::Result<CommandStatus> {
        while let Some(bytes) = ctx.stdin.read_chunk().await? {
            ctx.stdout.write_all(&bytes).await?;
        }
        Ok(CommandStatus::success())
    }
}

struct GatedProducer {
    release: Arc<Notify>,
}

#[async_trait]
impl Command for GatedProducer {
    async fn execute(&self, ctx: CommandContext<'_>) -> bashkit::Result<CommandStatus> {
        ctx.stdout.write_all(b"error: first\n").await?;
        self.release.notified().await;
        ctx.stdout.write_all(b"error: last\n").await?;
        Ok(CommandStatus::success())
    }
}

#[tokio::test]
async fn final_filter_delivers_before_producer_finishes() {
    let release = Arc::new(Notify::new());
    let mut bash = Bash::builder()
        .command(
            "produce",
            GatedProducer {
                release: release.clone(),
            },
        )
        .command("copy", Copy)
        .build();
    let (mut writer, mut reader) = tokio::io::duplex(64);
    let execution = async {
        bash.execute("produce | copy | grep error")
            .stdout(&mut writer)
            .capture(1024)
            .await
    };
    let observer = async {
        use tokio::io::AsyncReadExt;
        let mut first = [0; 13];
        reader.read_exact(&mut first).await.unwrap();
        assert_eq!(&first, b"error: first\n");
        release.notify_one();
    };
    let (result, ()) = tokio::time::timeout(std::time::Duration::from_secs(2), async {
        tokio::join!(execution, observer)
    })
    .await
    .expect("final filter buffered until producer completion");
    let result = result.unwrap();
    assert_eq!(result.stdout.as_bytes(), b"error: first\nerror: last\n");
    assert_eq!(result.exit_code, 0);
}

#[tokio::test]
async fn streaming_capture_and_command_substitution_are_distinct() {
    let mut bash = Bash::builder().command("copy", Copy).build();
    let result = bash
        .execute("v=$(printf hello | copy); printf '%s' \"$v\" | copy")
        .capture(32)
        .await
        .unwrap();
    assert_eq!(result.stdout.as_bytes(), b"hello");
    assert!(!result.stdout_truncated);
    let result = bash
        .execute("printf 'abcdefgh' | copy")
        .capture(3)
        .await
        .unwrap();
    assert_eq!(result.stdout.as_bytes(), b"abc");
    assert!(result.stdout_truncated);
    let result = bash.execute("printf hello | copy").await.unwrap();
    assert!(result.stdout.is_empty());
}

#[tokio::test]
async fn command_redirection_keeps_output_out_of_caller_sink() {
    let mut bash = Bash::builder().command("copy", Copy).build();
    let result = bash
        .execute("printf hello | copy > /out; cat /out")
        .capture(32)
        .await
        .unwrap();
    assert_eq!(result.stdout.as_bytes(), b"hello");
}

struct StalledWriter;

impl tokio::io::AsyncWrite for StalledWriter {
    fn poll_write(
        self: std::pin::Pin<&mut Self>,
        _: &mut std::task::Context<'_>,
        _: &[u8],
    ) -> std::task::Poll<std::io::Result<usize>> {
        std::task::Poll::Pending
    }
    fn poll_flush(
        self: std::pin::Pin<&mut Self>,
        _: &mut std::task::Context<'_>,
    ) -> std::task::Poll<std::io::Result<()>> {
        std::task::Poll::Ready(Ok(()))
    }
    fn poll_shutdown(
        self: std::pin::Pin<&mut Self>,
        _: &mut std::task::Context<'_>,
    ) -> std::task::Poll<std::io::Result<()>> {
        std::task::Poll::Ready(Ok(()))
    }
}

#[tokio::test]
async fn deadline_interrupts_a_stalled_sink_and_session_recovers() {
    let limits = bashkit::ExecutionLimits::default().timeout(std::time::Duration::from_millis(50));
    let mut bash = Bash::builder().limits(limits).build();
    let mut sink = StalledWriter;
    let result = tokio::time::timeout(std::time::Duration::from_secs(1), async {
        bash.execute("echo hello").stdout(&mut sink).await
    })
    .await
    .expect("sink was not subject to execution deadline");
    assert!(matches!(
        result,
        Err(bashkit::Error::ResourceLimit(
            bashkit::LimitExceeded::Timeout(_)
        ))
    ));
    assert_eq!(
        bash.execute("echo recovered")
            .capture(64)
            .await
            .unwrap()
            .stdout
            .as_bytes(),
        b"recovered\n"
    );
}

#[tokio::test]
async fn failed_sink_drops_pending_producer_and_revokes_handles() {
    struct KeepHandles(Arc<std::sync::Mutex<Option<bashkit::CommandOutput>>>);
    #[async_trait]
    impl Command for KeepHandles {
        async fn execute(&self, ctx: CommandContext<'_>) -> bashkit::Result<CommandStatus> {
            *self.0.lock().unwrap() = Some(ctx.stdout.clone());
            ctx.stdout.write_all(b"first").await?;
            std::future::pending().await
        }
    }
    let handle = Arc::new(std::sync::Mutex::new(None));
    let mut bash = Bash::builder()
        .command("produce", KeepHandles(handle.clone()))
        .build();
    let result = tokio::time::timeout(std::time::Duration::from_secs(1), async {
        bash.execute("produce")
            .stdout_fn(|_| async { Err(std::io::Error::other("sink closed").into()) })
            .await
    })
    .await
    .unwrap();
    assert!(matches!(result, Err(bashkit::Error::Io(_))));
    let output = handle.lock().unwrap().take().unwrap();
    assert!(output.write_all(b"late").await.is_err());
    assert_eq!(
        bash.execute("echo recovered")
            .capture(64)
            .await
            .unwrap()
            .stdout
            .as_bytes(),
        b"recovered\n"
    );
}

#[tokio::test]
async fn cancellation_interrupts_pending_command_and_sink() {
    for script in ["produce", "echo first"] {
        let mut bash = Bash::builder()
            .command(
                "produce",
                GatedProducer {
                    release: Arc::new(Notify::new()),
                },
            )
            .build();
        let cancel = bash.cancellation_token();
        let mut writer = StalledWriter;
        let execution = async { bash.execute(script).stdout(&mut writer).await };
        let cancellation = async {
            tokio::time::sleep(std::time::Duration::from_millis(20)).await;
            cancel.store(true, std::sync::atomic::Ordering::Relaxed);
        };
        let (result, ()) = tokio::time::timeout(std::time::Duration::from_secs(1), async {
            tokio::join!(execution, cancellation)
        })
        .await
        .unwrap();
        assert!(matches!(result, Err(bashkit::Error::Cancelled)));
        bash.cancellation_token()
            .store(false, std::sync::atomic::Ordering::Relaxed);
        assert_eq!(
            bash.execute("echo recovered")
                .capture(64)
                .await
                .unwrap()
                .stdout
                .as_bytes(),
            b"recovered\n"
        );
    }
}

#[tokio::test]
async fn redirecting_stdout_keeps_stderr_live() {
    struct ErrorProducer(Arc<Notify>);
    #[async_trait]
    impl Command for ErrorProducer {
        async fn execute(&self, ctx: CommandContext<'_>) -> bashkit::Result<CommandStatus> {
            ctx.stdout.write_all(b"file").await?;
            ctx.stderr.write_all(b"first").await?;
            self.0.notified().await;
            Ok(CommandStatus::success())
        }
    }
    let release = Arc::new(Notify::new());
    let mut bash = Bash::builder()
        .command("produce", ErrorProducer(release.clone()))
        .build();
    let result = tokio::time::timeout(std::time::Duration::from_secs(1), async {
        bash.execute("produce >/out")
            .capture(64)
            .stderr_fn(move |bytes| {
                assert_eq!(bytes, b"first");
                release.notify_one();
                async { Ok(()) }
            })
            .await
    })
    .await
    .expect("stderr was buffered behind an unrelated stdout redirect")
    .unwrap();
    assert!(result.stdout.is_empty());
    assert_eq!(result.stderr.as_bytes(), b"first");
}

#[tokio::test]
async fn sandbox_output_caps_apply_to_binary_sinks_independently_of_capture() {
    let mut bash = Bash::builder()
        .limits(
            bashkit::ExecutionLimits::default()
                .max_stdout_bytes(3)
                .max_stderr_bytes(2),
        )
        .command("copy", Copy)
        .build();
    let bytes = Arc::new(std::sync::Mutex::new(Vec::new()));
    let sink = bytes.clone();
    let result = bash
        .execute("printf '\\000\\377abcdef' | copy; printf 'error' >&2")
        .capture(1)
        .stdout_fn(move |chunk| {
            sink.lock().unwrap().extend(chunk);
            async { Ok(()) }
        })
        .await
        .unwrap();
    assert_eq!(*bytes.lock().unwrap(), b"\0\xffa");
    assert_eq!(result.stdout.as_bytes(), b"\0");
    assert_eq!(result.stderr.as_bytes(), b"e");
    assert!(result.stdout_truncated && result.stderr_truncated);
    let result = bash.execute("printf 'abcdef'").await.unwrap();
    assert!(result.stdout.is_empty());
    assert!(result.stdout_truncated);
}

#[tokio::test]
async fn substitution_in_a_streaming_producer_does_not_leak_to_its_pipe() {
    let script = "for i in $(seq 1 20); do echo item-$i; done | grep -E 'item-[0-9]$' | sort -t- -k2 -n | wc -l";
    let mut bash = Bash::new();
    assert_eq!(bash.exec(script).await.unwrap().stdout.as_bytes(), b"9\n");
    assert_eq!(
        bash.execute(script)
            .capture(64)
            .await
            .unwrap()
            .stdout
            .as_bytes(),
        b"9\n"
    );
    let mut bash = Bash::builder().command("copy", Copy).build();
    assert_eq!(
        bash.execute("{ v=$(printf hello | copy); printf '%s' \"$v\"; } | cat")
            .capture(64)
            .await
            .unwrap()
            .stdout
            .as_bytes(),
        b"hello"
    );
}

#[tokio::test]
async fn accepted_output_is_drained_before_a_command_limit_error() {
    let mut bash = Bash::builder()
        .limits(bashkit::ExecutionLimits::default().max_commands(2))
        .build();
    let bytes = Arc::new(std::sync::Mutex::new(Vec::new()));
    let sink = bytes.clone();
    let result = bash
        .execute("echo one; echo two; echo three")
        .stdout_fn(move |chunk| {
            sink.lock().unwrap().extend(chunk);
            async { Ok(()) }
        })
        .await;
    assert!(result.is_err());
    assert!(bytes.lock().unwrap().starts_with(b"one\n"));
}

#[tokio::test]
async fn closed_diagnostic_fd_keeps_a_command_io_failure_an_ordinary_status() {
    let mut bash = Bash::builder().command("mixed", MixedOutput).build();
    let result = bash
        .execute("mixed >&- 2>&-; printf ':done'")
        .capture(64)
        .await
        .unwrap();
    assert_eq!(result.stdout.as_bytes(), b":done");
    assert!(result.stderr.is_empty());
    assert_eq!(result.exit_code, 0);
}