|
| 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