-
-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
23a9476
commit 9ecba02
Showing
11 changed files
with
225 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
...esources/simulation/ipv4_icmp_simple.yaml → tests/resources/simulation/ipv4_icmp.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
name: Simple IPv4/TCP example with a fixed dest port | ||
target: 10.0.0.103 | ||
protocol: Tcp | ||
port_direction: !FixedDest 80 | ||
icmp_identifier: 0 | ||
hops: | ||
- ttl: 1 | ||
resp: !SingleHost | ||
addr: 10.0.0.101 | ||
rtt_ms: 10 | ||
- ttl: 2 | ||
resp: !SingleHost | ||
addr: 10.0.0.102 | ||
rtt_ms: 20 | ||
- ttl: 3 | ||
resp: !SingleHost | ||
addr: 10.0.0.103 | ||
rtt_ms: 20 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
name: Simple IPv4/UDP example with a fixed src port | ||
target: 10.0.0.103 | ||
protocol: Udp | ||
port_direction: !FixedSrc 5000 | ||
icmp_identifier: 0 | ||
hops: | ||
- ttl: 1 | ||
resp: !SingleHost | ||
addr: 10.0.0.101 | ||
rtt_ms: 10 | ||
- ttl: 2 | ||
resp: !SingleHost | ||
addr: 10.0.0.102 | ||
rtt_ms: 20 | ||
- ttl: 3 | ||
resp: !SingleHost | ||
addr: 10.0.0.103 | ||
rtt_ms: 20 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,60 @@ | ||
use crate::simulation::Simulation; | ||
use crate::tun_device::{tun, tun_lock}; | ||
use crate::tun_device::tun; | ||
use crate::{network, tracer}; | ||
use std::sync::Arc; | ||
use std::thread; | ||
use std::sync::{Arc, Mutex, OnceLock}; | ||
use tokio::runtime::Runtime; | ||
use tokio_util::sync::CancellationToken; | ||
|
||
static RUNTIME: OnceLock<Arc<Mutex<Runtime>>> = OnceLock::new(); | ||
|
||
pub fn tokio() -> &'static Arc<Mutex<Runtime>> { | ||
RUNTIME.get_or_init(|| { | ||
let runtime = tokio::runtime::Builder::new_multi_thread() | ||
.enable_all() | ||
.build() | ||
.unwrap(); | ||
Arc::new(Mutex::new(runtime)) | ||
}) | ||
} | ||
|
||
macro_rules! sim { | ||
($path:expr) => {{ | ||
println!("simulating {}", $path); | ||
let yaml = include_str!(concat!("../resources/simulation/", $path)); | ||
serde_yaml::from_str(yaml)? | ||
}}; | ||
} | ||
|
||
#[test] | ||
fn test_simulations_icmp() -> anyhow::Result<()> { | ||
let runtime = tokio().lock().unwrap(); | ||
let sim = sim!("ipv4_icmp.yaml"); | ||
runtime.block_on(run_simulation(sim))?; | ||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn test_simulation() -> anyhow::Result<()> { | ||
let sim = serde_yaml::from_str(include_str!( | ||
"../resources/simulation/ipv4_icmp_simple.yaml" | ||
))?; | ||
run_test(sim) | ||
fn test_simulations_udp() -> anyhow::Result<()> { | ||
let runtime = tokio().lock().unwrap(); | ||
let sim = sim!("ipv4_udp_fixed_src.yaml"); | ||
runtime.block_on(run_simulation(sim))?; | ||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn test_simulations_tcp() -> anyhow::Result<()> { | ||
let runtime = tokio().lock().unwrap(); | ||
let sim = sim!("ipv4_tcp_fixed_dest.yaml"); | ||
runtime.block_on(run_simulation(sim))?; | ||
Ok(()) | ||
} | ||
|
||
fn run_test(simulation: Simulation) -> anyhow::Result<()> { | ||
let _lock = tun_lock().lock(); | ||
async fn run_simulation(simulation: Simulation) -> anyhow::Result<()> { | ||
let tun = tun(); | ||
let sim = Arc::new(simulation); | ||
let _handle = { | ||
let sim = sim.clone(); | ||
thread::spawn(move || network::run(tun, sim).unwrap()) | ||
}; | ||
tracer::Tracer::new(sim).trace()?; | ||
let token = CancellationToken::new(); | ||
let handle = tokio::spawn(network::run(tun.clone(), sim.clone(), token.clone())); | ||
tokio::task::spawn_blocking(move || tracer::Tracer::new(sim, token).trace()).await??; | ||
handle.await??; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.