Skip to content

Commit cc19cac

Browse files
committed
Add two tests for --background
1 parent b1a4e58 commit cc19cac

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

src/exec/use_pty/parent.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub(in crate::exec) fn exec_pty(
6060
// `policy_init_session`.
6161
// FIXME (ogsudo): initializes ttyblock sigset here by calling `init_ttyblock`
6262

63-
// Fetch the parent process group so we can signals to it.
63+
// Fetch the parent process group so we can send signals to it.
6464
let parent_pgrp = getpgrp();
6565

6666
// Set all the IO streams for the command to the follower side of the pty.

test-framework/sudo-compliance-tests/src/sudo.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ mod apparmor;
33
mod child_process;
44
mod cli;
55
mod env_reset;
6+
mod flag_background;
67
mod flag_chdir;
78
mod flag_group;
89
mod flag_help;
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
use sudo_test::{Command, Env};
2+
3+
use crate::SUDOERS_ALL_ALL_NOPASSWD;
4+
5+
#[test]
6+
fn runs_in_background() {
7+
let env = Env(SUDOERS_ALL_ALL_NOPASSWD).build();
8+
9+
Command::new("sudo")
10+
.args([
11+
"-b",
12+
"sh",
13+
"-c",
14+
"touch /tmp/barrier1; until [ -f /tmp/barrier2 ]; do sleep 0.1; done; touch /tmp/barrier3",
15+
])
16+
.output(&env)
17+
.assert_success();
18+
19+
Command::new("sh")
20+
.args([
21+
"-c",
22+
"until [ -f /tmp/barrier1 ]; do sleep 0.1; done
23+
touch /tmp/barrier2
24+
until [ -f /tmp/barrier3 ]; do sleep 0.1; done",
25+
])
26+
.output(&env)
27+
.assert_success();
28+
}
29+
30+
#[test]
31+
fn stdin_pipe() {
32+
let env = Env([SUDOERS_ALL_ALL_NOPASSWD, "Defaults use_pty"]).build();
33+
34+
// Everything is put in a single command with separators to keep the pts numbers predictable
35+
Command::new("sh")
36+
.args([
37+
"-c",
38+
"ls -l /proc/self/fd > /tmp/output; echo @@@@ >> /tmp/output; sudo -b sh -c 'ls -l /proc/self/fd; touch /tmp/barrier' >> /tmp/output",
39+
])
40+
.tty(true)
41+
.output(&env)
42+
.assert_success();
43+
44+
let stdout = Command::new("sh")
45+
.args([
46+
"-c",
47+
"until [ -f /tmp/barrier ]; do sleep 0.1; done; cat /tmp/output",
48+
])
49+
.output(&env)
50+
.stdout();
51+
52+
dbg!(&stdout);
53+
54+
let (term_in, term_background) = stdout.split_once("@@@@").unwrap();
55+
assert_contains!(term_in, " 0 -> /dev/pts/0");
56+
assert_contains!(term_in, " 1 -> /tmp/output");
57+
assert_contains!(term_in, " 2 -> /dev/pts/0");
58+
// Background mode makes stdin a half-open pipe and handles stdout and stderr like normal.
59+
assert_contains!(term_background, " 0 -> pipe:");
60+
assert_contains!(term_background, " 1 -> /tmp/output");
61+
assert_contains!(term_background, " 2 -> /dev/pts/0");
62+
}

0 commit comments

Comments
 (0)