Skip to content

Commit

Permalink
test: add tun integration test (WIP) - 4 (wintun)
Browse files Browse the repository at this point in the history
  • Loading branch information
fujiapple852 committed Jan 4, 2024
1 parent 4a88c9b commit 2a26d5e
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 111 deletions.
110 changes: 65 additions & 45 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,51 @@ on:
name: CI

jobs:
# test:
# runs-on: ${{ matrix.os }}
# strategy:
# matrix:
# build:
# - linux-stable
# - linux-musl-stable
# - linux-beta
# - linux-nightly
# - macos-stable
# - windows-stable
# include:
# - build: linux-stable
# os: ubuntu-20.04
# target: x86_64-unknown-linux-gnu
# rust: stable
# - build: linux-musl-stable
# os: ubuntu-20.04
# target: x86_64-unknown-linux-musl
# rust: stable
# - build: linux-beta
# os: ubuntu-22.04
# target: x86_64-unknown-linux-gnu
# rust: beta
# - build: linux-nightly
# os: ubuntu-22.04
# target: x86_64-unknown-linux-gnu
# rust: nightly
# - build: macos-stable
# os: macos-latest
# target: x86_64-apple-darwin
# rust: stable
# - build: windows-stable
# os: windows-latest
# target: x86_64-pc-windows-msvc
# rust: stable
# steps:
# - uses: actions/checkout@v2
# - uses: dtolnay/rust-toolchain@stable
# with:
# toolchain: ${{ matrix.rust }}
# target: ${{ matrix.target }}
# - run: cargo test --workspace --all-features --target ${{ matrix.target }}
test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
build:
- linux-stable
- linux-musl-stable
- linux-beta
- linux-nightly
- macos-stable
- windows-stable
include:
- build: linux-stable
os: ubuntu-20.04
target: x86_64-unknown-linux-gnu
rust: stable
- build: linux-musl-stable
os: ubuntu-20.04
target: x86_64-unknown-linux-musl
rust: stable
- build: linux-beta
os: ubuntu-22.04
target: x86_64-unknown-linux-gnu
rust: beta
- build: linux-nightly
os: ubuntu-22.04
target: x86_64-unknown-linux-gnu
rust: nightly
- build: macos-stable
os: macos-latest
target: x86_64-apple-darwin
rust: stable
- build: windows-stable
os: windows-latest
target: x86_64-pc-windows-msvc
rust: stable
steps:
- uses: actions/checkout@v2
- uses: dtolnay/rust-toolchain@stable
with:
toolchain: ${{ matrix.rust }}
target: ${{ matrix.target }}
- run: cargo test --target ${{ matrix.target }}

e2e-test:
sim-test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
Expand All @@ -74,7 +74,27 @@ jobs:
with:
toolchain: ${{ matrix.rust }}
target: ${{ matrix.target }}
- run: sudo -E env "PATH=$PATH" cargo test --target ${{ matrix.target }} --package trippy --test it -- --exact --nocapture
- run: sudo -E env "PATH=$PATH" cargo test --target ${{ matrix.target }} --features sim-tests --test sim -- --exact --nocapture

sim-test-windows:
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
- uses: Swatinem/rust-cache@v2
- uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
target: x86_64-pc-windows-msvc
- name: Copy wintun.dll to current dir
shell: bash
run: |
cp "tests/resources/wintun.dll" "."
- name: Allow ICMPv4 and ICMPv6 in Windows defender firewall
shell: pwsh
run: |
New-NetFirewallRule -DisplayName "ICMPv4 Trippy Allow" -Name ICMPv4_TRIPPY_ALLOW -Protocol ICMPv4 -Action Allow
New-NetFirewallRule -DisplayName "ICMPv6 Trippy Allow" -Name ICMPv6_TRIPPY_ALLOW -Protocol ICMPv6 -Action Allow
- run: cargo test --target x86_64-pc-windows-msvc --features sim-tests --test sim -- --exact --nocapture

fmt:
runs-on: ubuntu-22.04
Expand Down
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ rand = "0.8.5"
test-case = "3.3.1"
tun = "0.6.1"

[features]
# Enable simulation integration tests
sim-tests = []

# cargo-generate-rpm dependencies
[package.metadata.generate-rpm]
assets = [
Expand Down
64 changes: 0 additions & 64 deletions tests/it/main.rs

This file was deleted.

Binary file added tests/resources/wintun.dll
Binary file not shown.
6 changes: 6 additions & 0 deletions tests/sim/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#![cfg(feature = "sim-tests")]
mod network;
mod simulation;
mod tests;
mod tracer;
mod tun_device;
4 changes: 3 additions & 1 deletion tests/it/network.rs → tests/sim/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,14 @@ impl Network {
// sleep to allow tracer to send more packets before we respond
// TODO variable delay per hop (per ip per hop for ECMP) and out-of-order reply
thread::sleep(Duration::from_millis(100));
println!("sending ttl {} reply from {}", ipv4.get_ttl(), reply_addr);

let echo_request = EchoRequestPacket::new_view(ipv4.payload())?;
if echo_request.get_identifier() != simulation.identifier {
continue;
}

println!("sending ttl {} reply from {}", ipv4.get_ttl(), reply_addr);

// TODO validate ER checksum
// TODO what about sequence, should we respond per sequence rather than per hop/host?

Expand Down
File renamed without changes.
33 changes: 33 additions & 0 deletions tests/sim/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use crate::simulation::Simulation;
use crate::tun_device::{tun, tun_lock};
use crate::{network, simulation, tracer};
use std::sync::mpsc::channel;
use std::sync::Arc;
use std::thread;
use std::time::Duration;

#[test]
fn test_simulation() -> anyhow::Result<()> {
run_test(simulation::make_sim())
}

fn run_test(simulation: Simulation) -> anyhow::Result<()> {
let _lock = tun_lock().lock();
let tun = tun();
let (_tx, rx) = channel();
let sim = Arc::new(simulation);
let _handle = {
let sim = sim.clone();
thread::spawn(move || network::Network::new().run(tun, sim, rx).unwrap())
};
// allow time for the routing table to reflect the tun device.
thread::sleep(Duration::from_millis(10000));

tracer::Tracer::new(sim).trace()?;

// after the tracing round is done, allow the network time to process all in-flight packets
// thread::sleep(Duration::from_millis(1000));
// tx.send(())?;
// handle.join().expect("join");
Ok(())
}
File renamed without changes.
23 changes: 22 additions & 1 deletion tests/it/tun_device.rs → tests/sim/tun_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl TunDevice {
.netmask((255, 255, 255, 0))
.up();
let dev = tun::create(&config)?;
dev.set_nonblock()?;
// dev.set_nonblock()?;
Self::create_route()?;
Ok(Self { dev })
}
Expand All @@ -48,6 +48,7 @@ impl TunDevice {
}
}

#[cfg(not(target_os = "windows"))]
impl Read for TunDevice {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
let bytes_read = self.dev.read(buf)?;
Expand All @@ -60,6 +61,7 @@ impl Read for TunDevice {
}
}

#[cfg(not(target_os = "windows"))]
impl Write for TunDevice {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
if self.dev.has_packet_information() {
Expand All @@ -77,3 +79,22 @@ impl Write for TunDevice {
self.dev.flush()
}
}

#[cfg(target_os = "windows")]
impl Read for TunDevice {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
Ok(self.dev.read(buf)?)
}
}

#[cfg(target_os = "windows")]
impl Write for TunDevice {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.dev.write_all(buf)?;
Ok(buf.len())
}

fn flush(&mut self) -> std::io::Result<()> {
self.dev.flush()
}
}

0 comments on commit 2a26d5e

Please sign in to comment.