Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions tests/actions.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use nix::unistd::Uid;
use std::{fs::read_to_string, thread::sleep};
use tempfile::Builder;

Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
20 changes: 20 additions & 0 deletions tests/auth.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use nix::unistd::Uid;
use std::{error::Error, path::PathBuf, thread::sleep};
use tempfile::{Builder, TempDir};

Expand Down Expand Up @@ -64,6 +65,10 @@ X-GitHub-Hook-Installation-Target-Type: repository

#[test]
fn ping() -> Result<(), Box<dyn Error>> {
if Uid::current().is_root() {
println!("test skipped: cannot run as root");
return Ok(());
}
let (cfg, _td, _tp) = cfg(true)?;
run_success(
&cfg,
Expand All @@ -85,6 +90,11 @@ fn successful_auth() -> Result<(), Box<dyn Error>> {
// 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 <tempfile>`.

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
Expand Down Expand Up @@ -112,6 +122,11 @@ fn bad_sha256() -> Result<(), Box<dyn Error>> {
// 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
Expand All @@ -138,6 +153,11 @@ fn wrong_secret() -> Result<(), Box<dyn Error>> {
// 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
Expand Down
11 changes: 11 additions & 0 deletions tests/basic.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
use nix::unistd::Uid;
use std::error::Error;

mod common;
use common::run_success;

#[test]
fn content_type_json() -> Result<(), Box<dyn Error>> {
if Uid::current().is_root() {
println!("test skipped: cannot run as root");
return Ok(());
}

run_success(
r#"
listen = "127.0.0.1:0";
Expand Down Expand Up @@ -53,6 +59,11 @@ X-GitHub-Hook-Installation-Target-Type: repository

#[test]
fn content_type_url_encoded() -> Result<(), Box<dyn Error>> {
if Uid::current().is_root() {
println!("test skipped: cannot run as root");
return Ok(());
}

run_success(
r#"
listen = "127.0.0.1:0";
Expand Down
11 changes: 11 additions & 0 deletions tests/config.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
use nix::unistd::Uid;
use std::error::Error;

mod common;
use common::{run_preserver_error, run_preserver_success};

#[test]
fn empty_config() -> Result<(), Box<dyn Error>> {
if Uid::current().is_root() {
println!("test skipped: cannot run as root");
return Ok(());
}

run_preserver_error(r#""#)
}

#[test]
fn minimal_config() -> Result<(), Box<dyn Error>> {
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 {
Expand Down
16 changes: 16 additions & 0 deletions tests/queue.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use nix::unistd::Uid;
use std::{error::Error, fs::read_dir, thread::sleep};
use tempfile::Builder;

Expand Down Expand Up @@ -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
Expand All @@ -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);
}