Add basic test

This commit is contained in:
Matteo Settenvini 2022-08-13 17:04:22 +02:00
parent 355134e0dd
commit 0f11e871ef
Signed by: matteo
GPG key ID: 8576CC1AD97D42DF
7 changed files with 125 additions and 4 deletions

47
tests/common.rs Normal file
View file

@ -0,0 +1,47 @@
// SPDX-FileCopyrightText: 2022 Matteo Settenvini <matteo.settenvini@montecristosoftware.eu>
// SPDX-License-Identifier: GPL-3.0-or-later
use {
anyhow::{ensure, Result},
std::ffi::CString,
std::os::raw::c_int,
std::os::unix::fs::symlink,
std::path::PathBuf,
std::sync::Once,
};
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
static SETUP: Once = Once::new();
pub fn setup() -> Result<()> {
unsafe {
static mut NSS_CONFIG_STATUS: c_int = 0;
SETUP.call_once(|| {
let out_dir = PathBuf::from(env!("OUT_DIR"));
let library_path = test_cdylib::build_current_project();
let mut library_filename = library_path.file_name().unwrap().to_owned();
library_filename.push(".2"); // required for NSS 2 modules
let dest = out_dir.join(library_filename);
symlink(&library_path, &dest).expect(&format!(
"Unable to create symlink to library ({} -> {})",
library_path.to_string_lossy(),
dest.to_string_lossy()
));
std::env::set_var("LD_LIBRARY_PATH", out_dir);
let db = CString::new("hosts").unwrap();
let resolvers = CString::new("files malcontent [UNAVAIL=return] dns").unwrap();
NSS_CONFIG_STATUS = __nss_configure_lookup(db.as_ptr(), resolvers.as_ptr());
});
ensure!(
NSS_CONFIG_STATUS == 0,
"Unable to configure NSS to load module: __nss_configure_lookup() returned {}",
NSS_CONFIG_STATUS
);
Ok(())
}
}

15
tests/integration_test.rs Normal file
View file

@ -0,0 +1,15 @@
// SPDX-FileCopyrightText: 2022 Matteo Settenvini <matteo.settenvini@montecristosoftware.eu>
// SPDX-License-Identifier: GPL-3.0-or-later
mod common;
use anyhow::Result;
#[test]
fn nss_module_is_loaded() -> Result<()> {
use std::net::ToSocketAddrs;
common::setup()?;
"www.google.com:443".to_socket_addrs()?;
Ok(())
}