Skip to content

Commit

Permalink
Change: Improve integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
jjnicola committed Nov 23, 2022
1 parent 352bcba commit 5cfcac3
Showing 1 changed file with 26 additions and 62 deletions.
88 changes: 26 additions & 62 deletions rust/nvtcache/tests/nvtcache_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,79 +13,47 @@ mod test {
use super::*;

#[test]
// This is an integration test and requires a running redis instance.
// Use cargo test --features=redis_test.
// Also, set the environment variables REDIS_SOCKET and PLUGIN_PATH with valid paths
#[cfg(feature = "redis_test")]
fn test_nvtcache() -> Result<()> {
fn integration_test_nvtcache() -> Result<()> {
let mut nvtcache: nvtcache::NvtCache;

let redis_default_socket = |_| "unix:///run/redis/redis-server.sock".to_string();
let redis_socket = env::var("REDIS_SOCKET").unwrap_or_else(redis_default_socket);
let default_plugin_path = |_| "/var/lib/openvas/plugins/".to_string();
let plugin_path = env::var("PLUGIN_PATH").unwrap_or_else(default_plugin_path);
let n = nvtcache::NvtCache::init(&redis_socket, &plugin_path);
match n {
Ok(nc) => nvtcache = nc,
Err(e) => {
println!("{}", e);
panic!("Error")
}
}

if nvtcache.is_init() {
println!("Is initialized");
}

match nvtcache.cache.get_namespace() {
Ok(ok) => println!("The namespace: {}", ok),
Err(e) => println!("Error:{}", e),
}
nvtcache = nvtcache::NvtCache::init(&redis_socket, &plugin_path)?;
assert_eq!(nvtcache.is_init(), true);

// Test get_namespace()
assert!(nvtcache.cache.get_namespace()? > 0);

let mut key = "key int value";
let val: u64 = 42;
match nvtcache.cache.redis_set_key(key, val) {
Ok(_) => println!("Key {} set with {}", key, val),
Err(e) => println!("Error:{}", e),
}
let res = nvtcache.cache.redis_get_key(key);
match res {
Ok(k) => println!("{}", k),
Err(_) => println!("Error"),
}

// Test redis_set_key() generic with an uint value
assert_eq!(nvtcache.cache.redis_set_key(key, val)?, ());

// Test redis_get_key() with the recently stored key
assert_eq!(nvtcache.cache.redis_get_key(key)?, val.to_string());

// Test redis_set_key() generic with an string value
key = "key string value";
let val = "Some string";
match nvtcache.cache.redis_set_key(key, val) {
Ok(_) => println!("Key {} set with {}", key, val),
Err(e) => println!("Error:{}", e),
}

let res = nvtcache.cache.redis_get_key(key);
match res {
Ok(k) => println!("{}", k),
Err(_) => println!("Error"),
}

let _ = nvtcache.set_version("202212101125");
let updated = nvtcache.check_feed("202212101125");
match updated {
Ok(ret) => {
assert_eq!(ret, true);
println!("Feed up-to-date");
}
Err(_) => println!("Error"),
}
assert_eq!(nvtcache.cache.redis_set_key(key, val)?, ());

assert_eq!(nvtcache.cache.redis_get_key(key)?, val);

let _ = nvtcache.set_version("202212101125")?;
assert_eq!(nvtcache.check_feed("202212101125")?, true);

let mut fake_nvt: Nvt;
let res = Nvt::new();
match res {
Ok(ok) => fake_nvt = ok,
Err(_) => panic!("No Nvt"),
}
fake_nvt = Nvt::new()?;

let oid = "1234".to_owned();
match fake_nvt.set_oid(oid) {
Ok(_) => (),
Err(_) => println!("Error"),
}
fake_nvt.set_oid(oid);
fake_nvt.set_name("Custom Script for the vulnerability 1".to_owned());

let pref = NvtPref::new(
Expand All @@ -109,14 +77,10 @@ mod test {
assert_eq!(tag, &expected);

let filename = "custom.nasl".to_owned();
match nvtcache.add_nvt(fake_nvt, filename) {
Ok(_) => println!("Nvt successfully added"),
Err(_) => println!("Error"),
}
assert_eq!(nvtcache.add_nvt(fake_nvt, filename)?, ());

let mut item = nvtcache.get_nvt_field("1234".to_owned(), KbNvtPos::NvtFilenamePos)?;
assert_eq!(item, "custom.nasl");
println!("The filename was fetch successfully: {}", item);

item = nvtcache.get_nvt_field("1234".to_owned(), KbNvtPos::NvtNamePos)?;
assert_eq!(item, "Custom Script for the vulnerability 1");
Expand All @@ -126,6 +90,6 @@ mod test {

let _ = nvtcache.reset();

return Ok(());
Ok(())
}
}

0 comments on commit 5cfcac3

Please sign in to comment.