Skip to content

Commit

Permalink
vsock_proxy: add tests
Browse files Browse the repository at this point in the history
Add tests for the dns module to improve coverage.

Signed-off-by: Erdem Meydanli <[email protected]>
  • Loading branch information
meerd committed Apr 10, 2024
1 parent 00363b8 commit 82328df
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions vsock_proxy/src/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,86 @@ pub fn resolve_single(addr: &str, ip_addr_type: IpAddrType) -> VsockProxyResult<
// We already know that rresults containt at least one item.
Ok(rresults.first().unwrap().clone())
}

#[cfg(test)]
mod tests {
use super::*;

use ctor::ctor;
use std::sync::Once;
use std::env;

static TEST_INIT: Once = Once::new();

static mut INVALID_TEST_DOMAIN: &'static str = "invalid-domain";
static mut IPV4_ONLY_TEST_DOMAIN: &'static str = "v4.ipv6test.app";
static mut IPV6_ONLY_TEST_DOMAIN: &'static str = "v6.ipv6test.app";
static mut DUAL_IP_TEST_DOMAIN: &'static str = "ipv6test.app";

#[test]
#[ctor]
fn init() {
// *** To use nonlocal domain names, set TEST_NONLOCAL_DOMAINS variable. ***
// *** TEST_NONLOCAL_DOMAINS=1 cargo test ***
TEST_INIT.call_once(|| {
if env::var_os("TEST_NONLOCAL_DOMAINS").is_none() {
eprintln!("[warn] dns: using 'localhost' for testing.");
unsafe {
IPV4_ONLY_TEST_DOMAIN = "localhost";
IPV6_ONLY_TEST_DOMAIN = "::1";
DUAL_IP_TEST_DOMAIN = "localhost";
}
}
});
}

#[test]
fn test_resolve_valid_domain() {
let domain = unsafe { IPV4_ONLY_TEST_DOMAIN };
let rresults = resolve(domain, IpAddrType::IPAddrMixed).unwrap();
assert!(!rresults.is_empty());
}

#[test]
fn test_resolve_valid_dual_ip_domain() {
let domain = unsafe { DUAL_IP_TEST_DOMAIN };
let rresults = resolve(domain, IpAddrType::IPAddrMixed).unwrap();
assert!(!rresults.is_empty());
}

#[test]
fn test_resolve_invalid_domain() {
let domain = unsafe { INVALID_TEST_DOMAIN };
let rresults = resolve(domain, IpAddrType::IPAddrMixed);
assert!(rresults.is_err() && rresults.err().unwrap().eq("DNS lookup failed!"));
}

#[test]
fn test_resolve_ipv4_only() {
let domain = unsafe { IPV4_ONLY_TEST_DOMAIN };
let rresults = resolve(domain, IpAddrType::IPAddrV4Only).unwrap();
assert!(rresults.iter().all(|item| item.ip.is_ipv4()));
}

#[test]
fn test_resolve_ipv6_only() {
let domain = unsafe { IPV6_ONLY_TEST_DOMAIN };
let rresults = resolve(domain, IpAddrType::IPAddrV6Only).unwrap();
assert!(rresults.iter().all(|item| item.ip.is_ipv6()));
}

#[test]
fn test_resolve_no_accepted_ip() {
let domain = unsafe { IPV4_ONLY_TEST_DOMAIN };
let rresults = resolve(domain, IpAddrType::IPAddrV6Only);
assert!(rresults.is_err() && rresults.err().unwrap().eq("No accepted IP was found."));
}

#[test]
fn test_resolve_single_address() {
let domain = unsafe { IPV4_ONLY_TEST_DOMAIN };
let rresult = resolve_single(domain, IpAddrType::IPAddrMixed).unwrap();
assert!(rresult.ip.is_ipv4());
assert!(rresult.ttl != 0);
}
}

0 comments on commit 82328df

Please sign in to comment.