From 023ef0e1d769c6c3d09f57269d47df8f8c8065d1 Mon Sep 17 00:00:00 2001 From: Dennis Schwerdel Date: Mon, 8 Aug 2016 16:56:58 +0200 Subject: [PATCH] Test cases for config file --- src/config.rs | 12 +++---- src/crypto.rs | 2 +- src/device.rs | 2 +- src/main.rs | 2 +- src/tests.rs | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++- src/types.rs | 2 +- 6 files changed, 103 insertions(+), 11 deletions(-) diff --git a/src/config.rs b/src/config.rs index 92df308..69f5541 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,14 +1,14 @@ use super::{MAGIC, Args}; -use device::Type; -use types::{Mode, HeaderMagic}; -use crypto::CryptoMethod; -use util::{Encoder, Duration}; +use super::device::Type; +use super::types::{Mode, HeaderMagic}; +use super::crypto::CryptoMethod; +use super::util::{Encoder, Duration}; use std::hash::{Hash, SipHasher, Hasher}; -#[derive(RustcDecodable, Debug)] +#[derive(RustcDecodable, Debug, PartialEq)] pub struct Config { pub device_type: Type, pub device_name: String, @@ -145,7 +145,7 @@ impl Config { } -#[derive(RustcDecodable, Debug)] +#[derive(RustcDecodable, Debug, PartialEq, Default)] pub struct ConfigFile { pub device_type: Option, pub device_name: Option, diff --git a/src/crypto.rs b/src/crypto.rs index 571c234..86eb30e 100644 --- a/src/crypto.rs +++ b/src/crypto.rs @@ -119,7 +119,7 @@ extern { } -#[derive(RustcDecodable, Debug)] +#[derive(RustcDecodable, Debug, PartialEq)] pub enum CryptoMethod { ChaCha20, AES256 } diff --git a/src/device.rs b/src/device.rs index e2f61a3..8153d32 100644 --- a/src/device.rs +++ b/src/device.rs @@ -16,7 +16,7 @@ extern { /// The type of a tun/tap device -#[derive(RustcDecodable, Debug, Clone, Copy)] +#[derive(RustcDecodable, Debug, Clone, Copy, PartialEq)] pub enum Type { /// Tun interface: This interface transports IP packets. Tun, diff --git a/src/main.rs b/src/main.rs index 34545f4..2ba2cef 100644 --- a/src/main.rs +++ b/src/main.rs @@ -53,7 +53,7 @@ const MAGIC: HeaderMagic = *b"vpn\x01"; static USAGE: &'static str = include_str!("usage.txt"); -#[derive(RustcDecodable, Debug)] +#[derive(RustcDecodable, Debug, Default)] pub struct Args { flag_config: Option, flag_type: Option, diff --git a/src/tests.rs b/src/tests.rs index 11ca0ee..860a992 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -8,9 +8,13 @@ use std::str::FromStr; use super::MAGIC; use super::ethernet::{Frame, SwitchTable}; use super::ip::{RoutingTable, Packet}; -use super::types::{Protocol, Address, Range, Table}; +use super::device::Type; +use super::types::{Protocol, Address, Range, Table, Mode}; use super::udpmessage::{Message, decode, encode}; use super::crypto::{Crypto, CryptoMethod}; +use super::config::{Config, ConfigFile}; +use super::configfile; +use super::Args; impl<'a> PartialEq for Message<'a> { @@ -390,3 +394,91 @@ fn encrypt_decrypt_aes256() { receiver.decrypt(&mut buffer[..size], &nonce2, &header).unwrap(); assert_eq!(msg_bytes, &buffer[..msg_bytes.len()] as &[u8]); } + +#[test] +fn config_file() { + let config_file = " +device_type: tun +device_name: vpncloud%d +ifup: ifconfig $IFNAME 10.0.1.1/16 mtu 1400 up +crypto: aes256 +shared_key: mysecret +port: 3210 +peers: + - remote.machine.foo:3210 + - remote.machine.bar:3210 +peer_timeout: 1800 +mode: normal +subnets: + - 10.0.1.0/24 + "; + assert_eq!(configfile::parse_str::(config_file).unwrap(), ConfigFile{ + device_type: Some(Type::Tun), + device_name: Some("vpncloud%d".to_string()), + ifup: Some("ifconfig $IFNAME 10.0.1.1/16 mtu 1400 up".to_string()), + ifdown: None, + crypto: Some(CryptoMethod::AES256), + shared_key: Some("mysecret".to_string()), + magic: None, + port: Some(3210), + peers: Some(vec!["remote.machine.foo:3210".to_string(), "remote.machine.bar:3210".to_string()]), + peer_timeout: Some(1800), + mode: Some(Mode::Normal), + dst_timeout: None, + subnets: Some(vec!["10.0.1.0/24".to_string()]) + }) +} + +#[test] +fn config_merge() { + let mut config = Config::default(); + config.merge_file(ConfigFile{ + device_type: Some(Type::Tun), + device_name: Some("vpncloud%d".to_string()), + ifup: Some("ifconfig $IFNAME 10.0.1.1/16 mtu 1400 up".to_string()), + ifdown: None, + crypto: Some(CryptoMethod::AES256), + shared_key: Some("mysecret".to_string()), + magic: None, + port: Some(3210), + peers: Some(vec!["remote.machine.foo:3210".to_string(), "remote.machine.bar:3210".to_string()]), + peer_timeout: Some(1800), + mode: Some(Mode::Normal), + dst_timeout: None, + subnets: Some(vec!["10.0.1.0/24".to_string()]) + }); + assert_eq!(config, Config{ + device_type: Type::Tun, + device_name: "vpncloud%d".to_string(), + ifup: Some("ifconfig $IFNAME 10.0.1.1/16 mtu 1400 up".to_string()), + crypto: CryptoMethod::AES256, + shared_key: Some("mysecret".to_string()), + port: 3210, + peers: vec!["remote.machine.foo:3210".to_string(), "remote.machine.bar:3210".to_string()], + peer_timeout: 1800, + mode: Mode::Normal, + subnets: vec!["10.0.1.0/24".to_string()], + ..Default::default() + }); + config.merge_args(Args{ + flag_type: Some(Type::Tap), + flag_device: Some("vpncloud0".to_string()), + flag_shared_key: None, + flag_subnet: vec![], + flag_connect: vec!["another:3210".to_string()], + ..Default::default() + }); + assert_eq!(config, Config{ + device_type: Type::Tap, + device_name: "vpncloud0".to_string(), + ifup: Some("ifconfig $IFNAME 10.0.1.1/16 mtu 1400 up".to_string()), + crypto: CryptoMethod::AES256, + shared_key: Some("mysecret".to_string()), + port: 3210, + peers: vec!["remote.machine.foo:3210".to_string(), "remote.machine.bar:3210".to_string(), "another:3210".to_string()], + peer_timeout: 1800, + mode: Mode::Normal, + subnets: vec!["10.0.1.0/24".to_string()], + ..Default::default() + }); +} diff --git a/src/types.rs b/src/types.rs index 1275aa3..40fd4a3 100644 --- a/src/types.rs +++ b/src/types.rs @@ -185,7 +185,7 @@ impl fmt::Debug for Range { } -#[derive(RustcDecodable, Debug, Clone, Copy)] +#[derive(RustcDecodable, Debug, Clone, Copy, PartialEq)] pub enum Mode { Normal, Hub, Switch, Router }