Ensure loading of NSS module in tests
This commit is contained in:
parent
0f11e871ef
commit
9ce503e052
8 changed files with 92 additions and 62 deletions
|
@ -1,11 +1,15 @@
|
|||
// SPDX-FileCopyrightText: 2022 Matteo Settenvini <matteo.settenvini@montecristosoftware.eu>
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#![allow(non_upper_case_globals)]
|
||||
#![allow(non_camel_case_types)]
|
||||
#![allow(non_snake_case)]
|
||||
#![allow(dead_code)]
|
||||
|
||||
use {
|
||||
anyhow::{ensure, Result},
|
||||
std::env,
|
||||
std::ffi::CString,
|
||||
std::os::raw::c_int,
|
||||
std::os::unix::fs::symlink,
|
||||
std::path::PathBuf,
|
||||
std::sync::Once,
|
||||
};
|
||||
|
@ -15,33 +19,27 @@ 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;
|
||||
let out_dir = PathBuf::from(env!("OUT_DIR"));
|
||||
let nss_config_status = unsafe {
|
||||
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
|
||||
library_filename.push(".2"); // required for NSS 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());
|
||||
std::fs::copy(library_path, dest).unwrap();
|
||||
});
|
||||
|
||||
ensure!(
|
||||
NSS_CONFIG_STATUS == 0,
|
||||
"Unable to configure NSS to load module: __nss_configure_lookup() returned {}",
|
||||
NSS_CONFIG_STATUS
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
let db = CString::new("hosts").unwrap();
|
||||
let resolvers = CString::new("malcontent [UNAVAIL=return] dns").unwrap();
|
||||
__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(())
|
||||
}
|
||||
|
|
|
@ -3,13 +3,33 @@
|
|||
|
||||
mod common;
|
||||
|
||||
use anyhow::Result;
|
||||
use {
|
||||
anyhow::{bail, Result},
|
||||
libc::{freeaddrinfo, gai_strerror, getaddrinfo},
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn nss_module_is_loaded() -> Result<()> {
|
||||
use std::net::ToSocketAddrs;
|
||||
#[should_panic(expected = "not yet implemented")]
|
||||
fn nss_module_is_loaded() {
|
||||
common::setup().unwrap();
|
||||
|
||||
common::setup()?;
|
||||
"www.google.com:443".to_socket_addrs()?;
|
||||
Ok(())
|
||||
let hostname = std::ffi::CString::new("gnome.org").unwrap();
|
||||
unsafe {
|
||||
let mut addr = std::ptr::null_mut();
|
||||
match getaddrinfo(
|
||||
hostname.as_ptr(),
|
||||
std::ptr::null(),
|
||||
std::ptr::null(),
|
||||
&mut addr,
|
||||
) {
|
||||
0 => freeaddrinfo(addr),
|
||||
status => {
|
||||
let error = std::ffi::CStr::from_ptr(gai_strerror(status));
|
||||
panic!(
|
||||
"Unable to resolve hostname, getaddrinfo returned {}",
|
||||
error.to_str().unwrap()
|
||||
)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue