Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move tests into the tests directory & run tests on GH Actions #123

Merged
merged 3 commits into from
Aug 8, 2023
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
14 changes: 10 additions & 4 deletions .github/workflows/build-rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,24 @@ jobs:
platform: Linux
packages:
- gcc-aarch64-linux-gnu
targets:
test-targets:
- x86_64-unknown-linux-gnu
targets:
- aarch64-unknown-linux-gnu
- os: macos-12
platform: macOS
targets:
test-targets:
- x86_64-apple-darwin
targets:
- aarch64-apple-darwin
- aarch64-apple-ios
- aarch64-apple-ios-sim
- x86_64-apple-ios
- os: windows-2022
platform: Windows
targets:
test-targets:
- x86_64-pc-windows-msvc
targets:
- aarch64-pc-windows-msvc
runs-on: ${{ matrix.os }}
env:
Expand Down Expand Up @@ -59,4 +62,7 @@ jobs:
targets: ${{ join(matrix.targets, ', ') }}
- name: Build
shell: bash
run: cargo build --verbose --workspace --all-features --target ${{ join(matrix.targets, ' --target ') }}
run: cargo build --verbose --workspace --all-features --target ${{ join(matrix.targets, ' --target ') }} --target ${{ join(matrix.test-targets, ' --target ') }}
- name: Test
shell: bash
run: cargo test --verbose --workspace --all-features --target ${{ join(matrix.test-targets, ' --target ') }}
9 changes: 0 additions & 9 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,6 @@
"editor.acceptSuggestionOnEnter": "on",
"rust-analyzer.restartServerOnConfigChange": true,
"rust-analyzer.cargo.features": "all",
"rust-analyzer.check.overrideCommand": [
"cargo",
"clippy",
"--fix",
"--workspace",
"--message-format=json",
"--all-targets",
"--allow-dirty"
],
"[rust]": {
"editor.defaultFormatter": "rust-lang.rust-analyzer",
}
Expand Down
24 changes: 0 additions & 24 deletions tun/src/tokio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,3 @@ impl TunInterface {
}
}
}

#[cfg(test)]
mod tests {
use std::net::Ipv4Addr;

use super::*;
#[tokio::test]
async fn test_create() {
let tun = crate::TunInterface::new().unwrap();
let _async_tun = TunInterface::new(tun).unwrap();
}

#[tokio::test]
async fn test_write() {
let tun = crate::TunInterface::new().unwrap();
tun.set_ipv4_addr(Ipv4Addr::from([192, 168, 1, 10]))
.unwrap();
let async_tun = TunInterface::new(tun).unwrap();
let mut buf = [0u8; 1500];
buf[0] = 6 << 4;
let bytes_written = async_tun.write(&buf).await.unwrap();
assert!(bytes_written > 0);
}
}
29 changes: 0 additions & 29 deletions tun/src/unix/apple/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,32 +156,3 @@ impl TunInterface {
.map_err(|_| Error::new(ErrorKind::Other, "Conversion error"))?
}
}

#[cfg(test)]
mod test {
use super::*;
use std::net::Ipv4Addr;

#[test]
fn mtu() {
let interf = TunInterface::new().unwrap();

interf.set_mtu(500).unwrap();

assert_eq!(interf.mtu().unwrap(), 500);
}

#[test]
#[throws]
fn netmask() {
let interf = TunInterface::new()?;

let netmask = Ipv4Addr::new(255, 0, 0, 0);
let addr = Ipv4Addr::new(192, 168, 1, 1);

interf.set_ipv4_addr(addr)?;
interf.set_netmask(netmask)?;

assert_eq!(interf.netmask()?, netmask);
}
}
29 changes: 0 additions & 29 deletions tun/src/unix/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,32 +172,3 @@ impl TunInterface {
self.socket.send(buf)?
}
}

#[cfg(test)]
mod test {
use super::TunInterface;
use std::net::Ipv4Addr;

#[test]
fn mtu() {
let interf = TunInterface::new().unwrap();

interf.set_mtu(500).unwrap();

assert_eq!(interf.mtu().unwrap(), 500);
}

#[test]
#[throws]
fn netmask() {
let interf = TunInterface::new()?;

let netmask = Ipv4Addr::new(255, 0, 0, 0);
let addr = Ipv4Addr::new(192, 168, 1, 1);

interf.set_ipv4_addr(addr)?;
interf.set_netmask(netmask)?;

assert_eq!(interf.netmask()?, netmask);
}
}
37 changes: 1 addition & 36 deletions tun/src/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,39 +61,4 @@ pub fn string_to_ifname(name: &str) -> [libc::c_char; libc::IFNAMSIZ] {
let len = name.len().min(buf.len());
buf[..len].copy_from_slice(unsafe { &*(name.as_bytes() as *const _ as *const [libc::c_char]) });
buf
}

#[cfg(test)]
mod test {

use super::*;

use std::net::Ipv4Addr;

#[throws]
#[test]
fn tst_read() {
// This test is interactive, you need to send a packet to any server through 192.168.1.10
// EG. `sudo route add 8.8.8.8 192.168.1.10`,
//`dig @8.8.8.8 hackclub.com`
let mut tun = TunInterface::new()?;
println!("tun name: {:?}", tun.name()?);
tun.set_ipv4_addr(Ipv4Addr::from([192, 168, 1, 10]))?;
println!("tun ip: {:?}", tun.ipv4_addr()?);
println!("Waiting for a packet...");
let buf = &mut [0u8; 1500];
let res = tun.recv(buf);
println!("Received!");
assert!(res.is_ok());
}

#[test]
#[throws]
fn write_packets() {
let tun = TunInterface::new()?;
let mut buf = [0u8; 1500];
buf[0] = 6 << 4;
let bytes_written = tun.send(&buf)?;
assert_eq!(bytes_written, 1504);
}
}
}
33 changes: 31 additions & 2 deletions tun/tests/configure.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use fehler::throws;
use std::io::Error;
use std::net::{Ipv4Addr};
use std::net::Ipv4Addr;
use tun::TunInterface;

#[test]
Expand All @@ -11,6 +11,7 @@ fn test_create() {

#[test]
#[throws]
#[cfg(not(target_os = "windows"))]
fn test_set_get_ipv4() {
let tun = TunInterface::new()?;

Expand All @@ -23,8 +24,10 @@ fn test_set_get_ipv4() {

#[test]
#[throws]
#[cfg(target_os = "linux")]
#[cfg(not(any(target_os = "windows", target_vendor = "apple")))]
fn test_set_get_ipv6() {
use std::net::Ipv6Addr;

let tun = TunInterface::new()?;

let addr = Ipv6Addr::new(1, 1, 1, 1, 1, 1, 1, 1);
Expand All @@ -33,3 +36,29 @@ fn test_set_get_ipv6() {
// let result = tun.ipv6_addr()?;
// assert_eq!(addr, result);
}

#[test]
#[throws]
#[cfg(not(target_os = "windows"))]
fn test_set_get_mtu() {
let interf = TunInterface::new()?;

interf.set_mtu(500)?;

assert_eq!(interf.mtu().unwrap(), 500);
}

#[test]
#[throws]
#[cfg(not(target_os = "windows"))]
fn test_set_get_netmask() {
let interf = TunInterface::new()?;

let netmask = Ipv4Addr::new(255, 0, 0, 0);
let addr = Ipv4Addr::new(192, 168, 1, 1);

interf.set_ipv4_addr(addr)?;
interf.set_netmask(netmask)?;

assert_eq!(interf.netmask()?, netmask);
}
36 changes: 36 additions & 0 deletions tun/tests/packets.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use fehler::throws;
use std::io::Error;

use std::net::Ipv4Addr;
use tun::TunInterface;

#[throws]
#[test]
#[ignore = "requires interactivity"]
#[cfg(not(target_os = "windows"))]
fn tst_read() {
// This test is interactive, you need to send a packet to any server through 192.168.1.10
// EG. `sudo route add 8.8.8.8 192.168.1.10`,
//`dig @8.8.8.8 hackclub.com`
let mut tun = TunInterface::new()?;
println!("tun name: {:?}", tun.name()?);
tun.set_ipv4_addr(Ipv4Addr::from([192, 168, 1, 10]))?;
println!("tun ip: {:?}", tun.ipv4_addr()?);
println!("Waiting for a packet...");
let buf = &mut [0u8; 1500];
let res = tun.recv(buf);
println!("Received!");
assert!(res.is_ok());
}

#[test]
#[throws]
#[ignore = "requires interactivity"]
#[cfg(not(target_os = "windows"))]
fn write_packets() {
let tun = TunInterface::new()?;
let mut buf = [0u8; 1500];
buf[0] = 6 << 4;
let bytes_written = tun.send(&buf)?;
assert_eq!(bytes_written, 1504);
}
22 changes: 22 additions & 0 deletions tun/tests/tokio.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use std::net::Ipv4Addr;

#[tokio::test]
#[cfg(all(feature = "tokio", not(target_os = "windows")))]
async fn test_create() {
let tun = tun::TunInterface::new().unwrap();
let async_tun = tun::tokio::TunInterface::new(tun).unwrap();
}

#[tokio::test]
#[ignore = "requires interactivity"]
#[cfg(all(feature = "tokio", not(target_os = "windows")))]
async fn test_write() {
let tun = tun::TunInterface::new().unwrap();
tun.set_ipv4_addr(Ipv4Addr::from([192, 168, 1, 10]))
.unwrap();
let async_tun = tun::tokio::TunInterface::new(tun).unwrap();
let mut buf = [0u8; 1500];
buf[0] = 6 << 4;
let bytes_written = async_tun.write(&buf).await.unwrap();
assert!(bytes_written > 0);
}
Loading