From ac6e7f1ffdf73c5f96f11d330fe33aeebd187073 Mon Sep 17 00:00:00 2001 From: Dennis Schwerdel Date: Thu, 21 Feb 2019 16:57:25 +0100 Subject: [PATCH] Beacon tests --- Cargo.lock | 24 ++++++++++++++++++++++++ Cargo.toml | 3 +++ src/beacon.rs | 44 +++++++++++++++++++++++++++++++++++++++++++- src/main.rs | 1 + 4 files changed, 71 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 2ef0b61..14c4c43 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -488,6 +488,14 @@ dependencies = [ "ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "remove_dir_all" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "ring" version = "0.14.6" @@ -611,6 +619,19 @@ dependencies = [ "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "tempfile" +version = "3.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_syscall 0.1.51 (registry+https://github.com/rust-lang/crates.io-index)", + "remove_dir_all 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "thread_local" version = "0.3.6" @@ -733,6 +754,7 @@ dependencies = [ "serde_yaml 0.8.8 (registry+https://github.com/rust-lang/crates.io-index)", "signal 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "siphasher 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "tempfile 3.0.7 (registry+https://github.com/rust-lang/crates.io-index)", "time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)", "yaml-rust 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -841,6 +863,7 @@ dependencies = [ "checksum regex 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "37e7cbbd370869ce2e8dff25c7018702d10b21a20ef7135316f8daecd6c25b7f" "checksum regex-syntax 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)" = "7d707a4fa2637f2dca2ef9fd02225ec7661fe01a53623c1e6515b6916511f7a7" "checksum regex-syntax 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "8c2f35eedad5295fdf00a63d7d4b238135723f92b434ec06774dad15c7ab0861" +"checksum remove_dir_all 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3488ba1b9a2084d38645c4c08276a1752dcbf2c7130d74f1569681ad5d2799c5" "checksum ring 0.14.6 (registry+https://github.com/rust-lang/crates.io-index)" = "426bc186e3e95cac1e4a4be125a4aca7e84c2d616ffc02244eef36e2a60a093c" "checksum rustc-demangle 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)" = "adacaae16d02b6ec37fdc7acfcddf365978de76d1983d3ee22afc260e1ca9619" "checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" @@ -857,6 +880,7 @@ dependencies = [ "checksum strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4f380125926a99e52bc279241539c018323fab05ad6368b56f93d9369ff550" "checksum syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)" = "f92e629aa1d9c827b2bb8297046c1ccffc57c99b947a680d3ccff1f136a3bee9" "checksum synstructure 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "73687139bf99285483c96ac0add482c3776528beac1d97d444f6e91f203a2015" +"checksum tempfile 3.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "b86c784c88d98c801132806dadd3819ed29d8600836c4088e855cdf3e178ed8a" "checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b" "checksum time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f" "checksum traitobject 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "efd1f82c56340fdf16f2a953d7bda4f8fdffba13d93b00844c25572110b26079" diff --git a/Cargo.toml b/Cargo.toml index 9f75cd4..b82ac7c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,6 +34,9 @@ base-62 = "0.1" cc = "^1" pkg-config = "0.3" +[dev-dependencies] +tempfile = "3" + [features] default = [] bench = [] diff --git a/src/beacon.rs b/src/beacon.rs index 83705aa..5dd0e13 100644 --- a/src/beacon.rs +++ b/src/beacon.rs @@ -266,7 +266,6 @@ impl BeaconSerializer { error!("Beacon command failed: {}", String::from_utf8_lossy(&output.stderr)); } }); - //TODO: implement Ok(()) } @@ -284,6 +283,8 @@ impl BeaconSerializer { #[cfg(test)] use std::str::FromStr; +#[cfg(test)] use std::time::Duration; +#[cfg(test)] use tempfile; #[test] fn encode() { @@ -362,4 +363,45 @@ fn decode_invalid() { assert_eq!(0, ser.decode_internal("JHEiLpSFTn8R4PIQ1nuDy5tY3lESGriv", None, 2000).len()); assert_eq!(2, ser.decode_internal("SGrivJHEiLpSFTn8R4PIQ1muDy5tY3lESGrivJHEiL", None, 2000).len()); assert_eq!(2, ser.decode_internal("JHEiLJHEiLpSFTn8R4PIQ1muDy5tY3lESGriv", None, 2000).len()); +} + +#[test] +fn encode_decode() { + let ser = BeaconSerializer::new(b"vpnc", b"mysecretkey"); + let mut peers = Vec::new(); + peers.push(SocketAddr::from_str("1.2.3.4:5678").unwrap()); + peers.push(SocketAddr::from_str("6.6.6.6:53").unwrap()); + let data = ser.encode(&peers); + let peers2 = ser.decode(&data, None); + assert_eq!(format!("{:?}", peers), format!("{:?}", peers2)); +} + +#[test] +fn encode_decode_file() { + let ser = BeaconSerializer::new(b"vpnc", b"mysecretkey"); + let mut peers = Vec::new(); + peers.push(SocketAddr::from_str("1.2.3.4:5678").unwrap()); + peers.push(SocketAddr::from_str("6.6.6.6:53").unwrap()); + let file = tempfile::NamedTempFile::new().expect("Failed to create temp file"); + assert!(ser.write_to_file(&peers, file.path()).is_ok()); + let peers2 = ser.read_from_file(file.path(), None); + assert!(peers2.is_ok()); + assert_eq!(format!("{:?}", peers), format!("{:?}", peers2.unwrap())); +} + +#[test] +fn encode_decode_cmd() { + let ser = BeaconSerializer::new(b"vpnc", b"mysecretkey"); + let mut peers = Vec::new(); + peers.push(SocketAddr::from_str("1.2.3.4:5678").unwrap()); + peers.push(SocketAddr::from_str("6.6.6.6:53").unwrap()); + let file = tempfile::NamedTempFile::new().expect("Failed to create temp file"); + assert!(ser.write_to_cmd(&peers, &format!("echo $beacon > {}", file.path().display())).is_ok()); + thread::sleep(Duration::from_millis(10)); + let res = ser.read_from_cmd(&format!("cat {}", file.path().display()), None); + assert!(res.is_ok()); + thread::sleep(Duration::from_millis(10)); + let peers2 = ser.get_cmd_results(); + assert!(peers2.is_some()); + assert_eq!(format!("{:?}", peers), format!("{:?}", peers2.unwrap())); } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 49eaad1..5cd31b2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,6 +22,7 @@ extern crate siphasher; extern crate daemonize; extern crate ring; extern crate base_62; +#[cfg(test)] extern crate tempfile; #[cfg(feature = "bench")] extern crate test; #[macro_use] pub mod util;