Skip to content

Commit

Permalink
test(rsjudge-runner): 🚚 normalize test names
Browse files Browse the repository at this point in the history
  • Loading branch information
Jisu-Woniu committed Jun 7, 2024
1 parent 2869e4d commit fbbdce6
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use std::path::PathBuf;

use capctl::{Cap, FullCapState};
use capctl::{Cap, CapState};
use rsjudge_runner::{use_caps, user::builder, RunAs};
use rsjudge_utils::command::check_output;
use tokio::process::Command;
Expand All @@ -19,11 +19,11 @@ use tokio::process::Command;
/// ```
#[tokio::main]
async fn main() -> anyhow::Result<()> {
dbg!(FullCapState::get_current().unwrap());
dbg!(CapState::get_current().unwrap());

use_caps!(Cap::SETUID, Cap::SETGID, Cap::DAC_READ_SEARCH);

dbg!(FullCapState::get_current().unwrap());
dbg!(CapState::get_current().unwrap());

// Get the path to the examples.
// This crate is located at crates/rsjudge-runner,
Expand Down
4 changes: 2 additions & 2 deletions crates/rsjudge-runner/examples/exploit_inner.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: Apache-2.0

use capctl::FullCapState;
use capctl::CapState;
use nix::unistd::{setuid, Uid};

/// This example should be called by the `exploit` example.
Expand All @@ -11,7 +11,7 @@ use nix::unistd::{setuid, Uid};
/// have no permitted capabilities.
fn main() {
eprintln!("Start exploit_inner binary");
dbg!(FullCapState::get_current().unwrap());
dbg!(CapState::get_current().unwrap());

eprintln!("Starting setuid syscall.");
let result = setuid(Uid::from_raw(0)).expect_err("Should fail to set UID");
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions crates/rsjudge-runner/examples/normal.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// SPDX-License-Identifier: Apache-2.0

use capctl::FullCapState;
use capctl::CapState;

fn main() {
eprintln!("Start normal binary");
dbg!(FullCapState::get_current().unwrap());
dbg!(CapState::get_current().unwrap());
eprintln!("End normal binary");
}
43 changes: 43 additions & 0 deletions crates/rsjudge-runner/examples/rusage_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use std::{path::PathBuf, time::Duration};

use rsjudge_runner::utils::resources::{rusage::WaitForResourceUsage, RunWithResourceLimit};
use rsjudge_traits::resource::ResourceLimit;
use tokio::{process::Command, time::Instant};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
let examples = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.parent()
.and_then(|p| p.parent())
.ok_or_else(|| anyhow::anyhow!("cannot find crate root"))?
.join("target/debug/examples");
let spin_lock = examples.join("spin_lock");
eprintln!("Starting spin_lock with CPU time limit of 1s, wall time limit 2s:");
let start_time = Instant::now();
let _ = Command::new(spin_lock)
.spawn_with_resource_limit(ResourceLimit::new(
Some(Duration::from_secs(1)),
Some(Duration::from_secs(2)),
None,
None,
))?
.wait_for_resource_usage()
.await;

dbg!(start_time.elapsed());
let sleep = examples.join("sleep");
eprintln!("Starting sleep with CPU time limit of 1s, wall time limit 2s:");
let start_time = Instant::now();
let _ = Command::new(sleep)
.spawn_with_resource_limit(ResourceLimit::new(
Some(Duration::from_secs(1)),
Some(Duration::from_secs(2)),
None,
None,
))?
.wait_for_resource_usage()
.await;

dbg!(start_time.elapsed());
Ok(())
}
36 changes: 4 additions & 32 deletions crates/rsjudge-runner/examples/sleep.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,6 @@
use std::{path::PathBuf, time::Duration};
use std::{thread::sleep, time::Duration};

use capctl::{Cap, CapState};
use rsjudge_runner::{
use_caps,
utils::resources::{rusage::WaitForResourceUsage, RunWithResourceLimit},
};
use rsjudge_traits::resource::ResourceLimit;
use tokio::process::Command;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
use_caps!(Cap::DAC_READ_SEARCH);
dbg!(CapState::get_current().unwrap());

let examples = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.parent()
.and_then(|p| p.parent())
.ok_or_else(|| anyhow::anyhow!("cannot find crate root"))?
.join("target/debug/examples");
let sleep_inner = examples.join("sleep_inner");
dbg!(&sleep_inner);
let status = Command::new(sleep_inner)
.spawn_with_resource_limit(ResourceLimit::new(
Some(Duration::from_secs(1)),
Some(Duration::from_secs(2)),
None,
None,
))?
.wait_for_resource_usage()
.await?;
dbg!(status);
Ok(())
fn main() {
println!("Trying to sleep for 10s.");
sleep(Duration::from_secs(10));
}
5 changes: 0 additions & 5 deletions crates/rsjudge-runner/examples/sleep_inner.rs

This file was deleted.

7 changes: 7 additions & 0 deletions crates/rsjudge-runner/examples/spin_lock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use std::time::{Duration, Instant};

fn main() {
println!("Trying to spin for 10s.");
let start_time = Instant::now();
while start_time.elapsed() < Duration::from_secs(10) {}
}
20 changes: 8 additions & 12 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ enum Command {
/// Requires `sudo` to set capabilities.
CapTest,
/// Run integrated sleep test.
SleepTest,
RusageTest,
/// Debug a command.
#[cfg(feature = "dbg")]
Debug,
Expand All @@ -55,22 +55,18 @@ fn main() -> anyhow::Result<()> {
Command::CapTest => cmd! {
cargo build "--examples" "--workspace";

echo "Setting capabilities for demo and exploit binaries with sudo.";
echo "Setting capabilities for get_user_info and cap_test binaries with sudo.";

sudo setcap "cap_setuid,cap_setgid=p" "target/debug/examples/demo";
sudo setcap "cap_setuid,cap_setgid,cap_dac_read_search=p" "target/debug/examples/exploit";
sudo setcap "cap_setuid,cap_setgid=p" "target/debug/examples/get_user_info";
sudo setcap "cap_setuid,cap_setgid,cap_dac_read_search=p" "target/debug/examples/cap_test";

"target/debug/examples/demo";
"target/debug/examples/exploit";
"target/debug/examples/get_user_info";
"target/debug/examples/cap_test";
},
Command::SleepTest => cmd! {
Command::RusageTest => cmd! {
cargo build "--examples" "--workspace";

echo "Setting capabilities for sleep binary with sudo.";

sudo setcap "cap_dac_read_search=p" "target/debug/examples/sleep";

"target/debug/examples/sleep";
"target/debug/examples/rusage_test";
},

#[cfg(feature = "dbg")]
Expand Down

0 comments on commit fbbdce6

Please sign in to comment.