diff --git a/tests/actions.rs b/tests/actions.rs index b5e67c2..fca83cc 100644 --- a/tests/actions.rs +++ b/tests/actions.rs @@ -1,3 +1,4 @@ +use nix::unistd::Uid; use std::{fs::read_to_string, thread::sleep}; use tempfile::Builder; @@ -8,6 +9,11 @@ use common::{run_success, SNARE_PAUSE}; fn multiple() { // This tests that when there are multiple actions, the correct one takes effect. + if Uid::current().is_root() { + println!("test skipped: cannot run as root"); + return; + } + let td = Builder::new() .tempdir_in(env!("CARGO_TARGET_TMPDIR")) .unwrap(); @@ -89,6 +95,11 @@ X-GitHub-Hook-Installation-Target-Type: repository fn errorcmd() { // This tests both large stdout/stderr output from `cmd` as well as that `errorcmd` works. + if Uid::current().is_root() { + println!("test skipped: cannot run as root"); + return; + } + let td = Builder::new() .tempdir_in(env!("CARGO_TARGET_TMPDIR")) .unwrap(); diff --git a/tests/auth.rs b/tests/auth.rs index 1e9f138..74ae92d 100644 --- a/tests/auth.rs +++ b/tests/auth.rs @@ -1,3 +1,4 @@ +use nix::unistd::Uid; use std::{error::Error, path::PathBuf, thread::sleep}; use tempfile::{Builder, TempDir}; @@ -64,6 +65,10 @@ X-GitHub-Hook-Installation-Target-Type: repository #[test] fn ping() -> Result<(), Box> { + if Uid::current().is_root() { + println!("test skipped: cannot run as root"); + return Ok(()); + } let (cfg, _td, _tp) = cfg(true)?; run_success( &cfg, @@ -85,6 +90,11 @@ fn successful_auth() -> Result<(), Box> { // This test checks that snare both responds to, and executes the correct command for, a given // (user, repo) pair. It does that by checking that snare executes `touch `. + if Uid::current().is_root() { + println!("test skipped: cannot run as root"); + return Ok(()); + } + let (cfg, _td, tp) = cfg(true)?; assert!(!tp.is_file()); // Example secret and payload from @@ -112,6 +122,11 @@ fn bad_sha256() -> Result<(), Box> { // doesn't execute any commands (so, by proxy, we assume that snare doesn't authenticate the // request). + if Uid::current().is_root() { + println!("test skipped: cannot run as root"); + return Ok(()); + } + let (cfg, _td, tp) = cfg(true)?; assert!(!tp.is_file()); // Example secret and payload from @@ -138,6 +153,11 @@ fn wrong_secret() -> Result<(), Box> { // Takes the example from [full_request], alters the client-side secret, and checks that this // causes snare not execute any commands (so, by proxy, we assume that authentication failed). + if Uid::current().is_root() { + println!("test skipped: cannot run as root"); + return Ok(()); + } + let (cfg, _td, tp) = cfg(false)?; assert!(!tp.is_file()); // Example secret and payload from diff --git a/tests/basic.rs b/tests/basic.rs index 7706eaa..be5532c 100644 --- a/tests/basic.rs +++ b/tests/basic.rs @@ -1,3 +1,4 @@ +use nix::unistd::Uid; use std::error::Error; mod common; @@ -5,6 +6,11 @@ use common::run_success; #[test] fn content_type_json() -> Result<(), Box> { + if Uid::current().is_root() { + println!("test skipped: cannot run as root"); + return Ok(()); + } + run_success( r#" listen = "127.0.0.1:0"; @@ -53,6 +59,11 @@ X-GitHub-Hook-Installation-Target-Type: repository #[test] fn content_type_url_encoded() -> Result<(), Box> { + if Uid::current().is_root() { + println!("test skipped: cannot run as root"); + return Ok(()); + } + run_success( r#" listen = "127.0.0.1:0"; diff --git a/tests/config.rs b/tests/config.rs index 0bebbdd..db8ba35 100644 --- a/tests/config.rs +++ b/tests/config.rs @@ -1,3 +1,4 @@ +use nix::unistd::Uid; use std::error::Error; mod common; @@ -5,11 +6,21 @@ use common::{run_preserver_error, run_preserver_success}; #[test] fn empty_config() -> Result<(), Box> { + if Uid::current().is_root() { + println!("test skipped: cannot run as root"); + return Ok(()); + } + run_preserver_error(r#""#) } #[test] fn minimal_config() -> Result<(), Box> { + if Uid::current().is_root() { + println!("test skipped: cannot run as root"); + return Ok(()); + } + run_preserver_success( r#"listen = "127.0.0.1:0"; github { diff --git a/tests/queue.rs b/tests/queue.rs index 27c8059..df51853 100644 --- a/tests/queue.rs +++ b/tests/queue.rs @@ -1,3 +1,4 @@ +use nix::unistd::Uid; use std::{error::Error, fs::read_dir, thread::sleep}; use tempfile::Builder; @@ -80,11 +81,21 @@ github {{ #[test] fn sequential() { + if Uid::current().is_root() { + println!("test skipped: cannot run as root"); + return; + } + assert_eq!(run_queue("sequential", 20, "", 2).unwrap(), 20); } #[test] fn evict() { + if Uid::current().is_root() { + println!("test skipped: cannot run as root"); + return; + } + let i = run_queue("evict", 20, "&& sleep 0.4", 4).unwrap(); // We have a fairly healthy `sleep` above which means most of our requests are likely to be // received while the first `cmd` is still running. However, we can't guarantee that, so we are @@ -99,5 +110,10 @@ fn evict() { #[test] fn parallel() { + if Uid::current().is_root() { + println!("test skipped: cannot run as root"); + return; + } + assert_eq!(run_queue("parallel", 20, "", 1,).unwrap(), 20); }