From: David Mulder Date: Tue, 13 Aug 2024 21:18:09 +0000 (-0600) Subject: Add tests for rust dbg crate X-Git-Tag: tdb-1.4.13~890 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9bd37a450d4d2df8659ee016b881a3b30afbf093;p=thirdparty%2Fsamba.git Add tests for rust dbg crate Signed-off-by: David Mulder Reviewed-by: Alexander Bokovoy --- diff --git a/rust/Cargo.lock b/rust/Cargo.lock index d714c5af414..2f757783da6 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -469,6 +469,8 @@ version = "4.21.0" dependencies = [ "bindgen", "chelps", + "paste", + "tempfile", ] [[package]] diff --git a/rust/dbg/Cargo.toml b/rust/dbg/Cargo.toml index 7d558383097..e1c6284bcc7 100644 --- a/rust/dbg/Cargo.toml +++ b/rust/dbg/Cargo.toml @@ -10,3 +10,7 @@ bindgen = "0.69.4" [dependencies] chelps.workspace = true + +[dev-dependencies] +tempfile = "3.12.0" +paste = "1.0.15" diff --git a/rust/dbg/build.rs b/rust/dbg/build.rs index f71152f104e..149df6bb430 100644 --- a/rust/dbg/build.rs +++ b/rust/dbg/build.rs @@ -21,4 +21,5 @@ fn main() { ); println!("cargo:rustc-link-lib=samba-debug-private-samba"); println!("cargo:rustc-link-lib=samba-util"); + println!("cargo:rustc-env=LD_LIBRARY_PATH=../../bin/shared:../../bin/shared/private/"); } diff --git a/rust/dbg/src/lib.rs b/rust/dbg/src/lib.rs index 5369742c976..55447ba85f0 100644 --- a/rust/dbg/src/lib.rs +++ b/rust/dbg/src/lib.rs @@ -61,12 +61,18 @@ pub fn setup_logging(prog_name: &str, new_logtype: ffi::debug_logtype) { } } +pub fn dbgflush() { + unsafe { + ffi::dbgflush(); + } +} + #[macro_export] macro_rules! debuglevel_set { ($level:expr) => {{ unsafe { - dbg::ffi::debuglevel_set_class( - dbg::ffi::DBGC_ALL as usize, + $crate::ffi::debuglevel_set_class( + $crate::ffi::DBGC_ALL as usize, $level as i32, ) } @@ -92,23 +98,23 @@ macro_rules! function { #[macro_export] macro_rules! DBG_PREFIX { ($level:expr $(, $arg:expr)* $(,)?) => {{ - if $level <= dbg::ffi::MAX_DEBUG_LEVEL { + if $level <= $crate::ffi::MAX_DEBUG_LEVEL { let location = format!("{}:{}", file!(), line!()); let location_cstr = chelps::wrap_string(&location); - let function = dbg::function!(); + let function = $crate::function!(); let function_msg = format!("{}: ", function); let function_cstr = chelps::wrap_string(function); let function_msg_cstr = chelps::wrap_string(&function_msg); let msg = format!($($arg),*); let msg_cstr = chelps::wrap_string(&msg); unsafe { - let _ = dbg::ffi::debuglevel_get_class(dbg::ffi::DBGC_CLASS as usize) >= ($level as i32) - && dbg::ffi::dbghdrclass($level as i32, - dbg::ffi::DBGC_CLASS as i32, + let _ = $crate::ffi::debuglevel_get_class($crate::ffi::DBGC_CLASS as usize) >= ($level as i32) + && $crate::ffi::dbghdrclass($level as i32, + $crate::ffi::DBGC_CLASS as i32, location_cstr, function_cstr) - && dbg::ffi::dbgtext(function_msg_cstr) - && dbg::ffi::dbgtext(msg_cstr); + && $crate::ffi::dbgtext(function_msg_cstr) + && $crate::ffi::dbgtext(msg_cstr); chelps::string_free(location_cstr); chelps::string_free(function_cstr); chelps::string_free(function_msg_cstr); @@ -121,34 +127,105 @@ macro_rules! DBG_PREFIX { #[macro_export] macro_rules! DBG_ERR { ($msg:expr $(, $arg:expr)* $(,)?) => {{ - dbg::DBG_PREFIX!(dbg::ffi::DBGLVL_ERR, $msg, $($arg),*) + $crate::DBG_PREFIX!($crate::ffi::DBGLVL_ERR, $msg, $($arg),*) }} } #[macro_export] macro_rules! DBG_WARNING { ($msg:expr $(, $arg:expr)* $(,)?) => {{ - dbg::DBG_PREFIX!(dbg::ffi::DBGLVL_WARNING, $msg, $($arg),*) + $crate::DBG_PREFIX!($crate::ffi::DBGLVL_WARNING, $msg, $($arg),*) }} } #[macro_export] macro_rules! DBG_NOTICE { ($msg:expr $(, $arg:expr)* $(,)?) => {{ - dbg::DBG_PREFIX!(dbg::ffi::DBGLVL_NOTICE, $msg, $($arg),*) + $crate::DBG_PREFIX!($crate::ffi::DBGLVL_NOTICE, $msg, $($arg),*) }} } #[macro_export] macro_rules! DBG_INFO { ($msg:expr $(, $arg:expr)* $(,)?) => {{ - dbg::DBG_PREFIX!(dbg::ffi::DBGLVL_INFO, $msg, $($arg),*) + $crate::DBG_PREFIX!($crate::ffi::DBGLVL_INFO, $msg, $($arg),*) }} } #[macro_export] macro_rules! DBG_DEBUG { ($msg:expr $(, $arg:expr)* $(,)?) => {{ - dbg::DBG_PREFIX!(dbg::ffi::DBGLVL_DEBUG, $msg, $($arg),*) + $crate::DBG_PREFIX!($crate::ffi::DBGLVL_DEBUG, $msg, $($arg),*) }} } + +#[cfg(test)] +mod tests { + use super::*; + use paste::paste; + use std::fs::File; + use std::io::Read; + use tempfile::NamedTempFile; + + #[test] + fn test_debug_constants() { + assert_eq!(MAX_DEBUG_LEVEL, ffi::MAX_DEBUG_LEVEL); + assert_eq!(DBGLVL_ERR, ffi::DBGLVL_ERR); + assert_eq!(DBGLVL_WARNING, ffi::DBGLVL_WARNING); + assert_eq!(DBGLVL_NOTICE, ffi::DBGLVL_NOTICE); + assert_eq!(DBGLVL_INFO, ffi::DBGLVL_INFO); + assert_eq!(DBGLVL_DEBUG, ffi::DBGLVL_DEBUG); + + assert_eq!( + DEBUG_DEFAULT_STDERR, + ffi::debug_logtype_DEBUG_DEFAULT_STDERR + ); + assert_eq!( + DEBUG_DEFAULT_STDOUT, + ffi::debug_logtype_DEBUG_DEFAULT_STDOUT + ); + assert_eq!(DEBUG_FILE, ffi::debug_logtype_DEBUG_FILE); + assert_eq!(DEBUG_STDOUT, ffi::debug_logtype_DEBUG_STDOUT); + assert_eq!(DEBUG_STDERR, ffi::debug_logtype_DEBUG_STDERR); + assert_eq!(DEBUG_CALLBACK, ffi::debug_logtype_DEBUG_CALLBACK); + } + + macro_rules! test_dbg_macro { + ($level:ident) => { + paste! { + #[test] + fn []() { + let logfile = NamedTempFile::new().expect("Failed to create temporary file"); + let logfile = logfile.path().to_str().unwrap(); + setup_logging("test_program", DEBUG_FILE); + debug_set_logfile(logfile); + + let logfile_output = concat!("This is a ", stringify!($level), " message"); + + debuglevel_set!([]); + + []!("{}\n", logfile_output); + dbgflush(); + + let mut file = File::open(logfile).expect("Failed to open logfile"); + let mut logfile_contents = String::new(); + file.read_to_string(&mut logfile_contents) + .expect("Failed to read logfile"); + assert!( + logfile_contents.contains(logfile_output), + "Test data missing from logfile: {}", + logfile_contents + ); + } + } + }; + } + + test_dbg_macro!(DEBUG); + // Multiple re-inits of the debug env cause it to fail, so we can't + // reliably test all of these in one go. + //test_dbg_macro!(INFO); + //test_dbg_macro!(NOTICE); + //test_dbg_macro!(WARNING); + //test_dbg_macro!(ERR); +} diff --git a/rust/himmelblaud/src/main.rs b/rust/himmelblaud/src/main.rs index eab735f7946..afbb4f50238 100644 --- a/rust/himmelblaud/src/main.rs +++ b/rust/himmelblaud/src/main.rs @@ -111,6 +111,10 @@ async fn main() -> ExitCode { // Setup logging, either to the configured logfile, or to stdout, depending // on what is specified on the command line. + match clap_args.get_flag("debug-stdout") { + true => setup_logging(env!("CARGO_PKG_NAME"), DEBUG_STDOUT), + false => setup_logging(env!("CARGO_PKG_NAME"), DEBUG_FILE), + } match lp.logfile() { Ok(Some(logfile)) => debug_set_logfile(&logfile), _ => { @@ -118,10 +122,6 @@ async fn main() -> ExitCode { return ExitCode::FAILURE; } } - match clap_args.get_flag("debug-stdout") { - true => setup_logging(env!("CARGO_PKG_NAME"), DEBUG_STDOUT), - false => setup_logging(env!("CARGO_PKG_NAME"), DEBUG_FILE), - } // Determine the unix socket path let sock_dir_str = match lp.winbindd_socket_directory() {