Test cases for config file

This commit is contained in:
Dennis Schwerdel 2016-08-08 16:56:58 +02:00
parent 06fc814b4b
commit 023ef0e1d7
6 changed files with 103 additions and 11 deletions

View File

@ -1,14 +1,14 @@
use super::{MAGIC, Args}; use super::{MAGIC, Args};
use device::Type; use super::device::Type;
use types::{Mode, HeaderMagic}; use super::types::{Mode, HeaderMagic};
use crypto::CryptoMethod; use super::crypto::CryptoMethod;
use util::{Encoder, Duration}; use super::util::{Encoder, Duration};
use std::hash::{Hash, SipHasher, Hasher}; use std::hash::{Hash, SipHasher, Hasher};
#[derive(RustcDecodable, Debug)] #[derive(RustcDecodable, Debug, PartialEq)]
pub struct Config { pub struct Config {
pub device_type: Type, pub device_type: Type,
pub device_name: String, pub device_name: String,
@ -145,7 +145,7 @@ impl Config {
} }
#[derive(RustcDecodable, Debug)] #[derive(RustcDecodable, Debug, PartialEq, Default)]
pub struct ConfigFile { pub struct ConfigFile {
pub device_type: Option<Type>, pub device_type: Option<Type>,
pub device_name: Option<String>, pub device_name: Option<String>,

View File

@ -119,7 +119,7 @@ extern {
} }
#[derive(RustcDecodable, Debug)] #[derive(RustcDecodable, Debug, PartialEq)]
pub enum CryptoMethod { pub enum CryptoMethod {
ChaCha20, AES256 ChaCha20, AES256
} }

View File

@ -16,7 +16,7 @@ extern {
/// The type of a tun/tap device /// The type of a tun/tap device
#[derive(RustcDecodable, Debug, Clone, Copy)] #[derive(RustcDecodable, Debug, Clone, Copy, PartialEq)]
pub enum Type { pub enum Type {
/// Tun interface: This interface transports IP packets. /// Tun interface: This interface transports IP packets.
Tun, Tun,

View File

@ -53,7 +53,7 @@ const MAGIC: HeaderMagic = *b"vpn\x01";
static USAGE: &'static str = include_str!("usage.txt"); static USAGE: &'static str = include_str!("usage.txt");
#[derive(RustcDecodable, Debug)] #[derive(RustcDecodable, Debug, Default)]
pub struct Args { pub struct Args {
flag_config: Option<String>, flag_config: Option<String>,
flag_type: Option<Type>, flag_type: Option<Type>,

View File

@ -8,9 +8,13 @@ use std::str::FromStr;
use super::MAGIC; use super::MAGIC;
use super::ethernet::{Frame, SwitchTable}; use super::ethernet::{Frame, SwitchTable};
use super::ip::{RoutingTable, Packet}; 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::udpmessage::{Message, decode, encode};
use super::crypto::{Crypto, CryptoMethod}; use super::crypto::{Crypto, CryptoMethod};
use super::config::{Config, ConfigFile};
use super::configfile;
use super::Args;
impl<'a> PartialEq for Message<'a> { impl<'a> PartialEq for Message<'a> {
@ -390,3 +394,91 @@ fn encrypt_decrypt_aes256() {
receiver.decrypt(&mut buffer[..size], &nonce2, &header).unwrap(); receiver.decrypt(&mut buffer[..size], &nonce2, &header).unwrap();
assert_eq!(msg_bytes, &buffer[..msg_bytes.len()] as &[u8]); 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::<ConfigFile>(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()
});
}

View File

@ -185,7 +185,7 @@ impl fmt::Debug for Range {
} }
#[derive(RustcDecodable, Debug, Clone, Copy)] #[derive(RustcDecodable, Debug, Clone, Copy, PartialEq)]
pub enum Mode { pub enum Mode {
Normal, Hub, Switch, Router Normal, Hub, Switch, Router
} }