rust: keep the ffi logger out of the test binary

Rust unit tests link without libsuricata, so a C reference anywhere in
the reachable code makes the test binary unlinkable. The ffi logger
calls SCLogMessage even under cfg(test), unlike the suricata crate's
own, which has a print fallback. Any test that reaches
AppLayerTxData::update_file_flags therefore breaks cargo test --lib
whenever the debug feature is on: it fails the GitHub jobs that build
with --enable-debug or --all-features while our own CI never sees it.

Give log_message the same fallback behind cfg(any(test, feature =
"testing")), the pattern applayer.rs already uses for set_event.
pull/16514/head
Victor Julien 7 hours ago
parent 9a37bb659b
commit 1dcb549109

@ -19,7 +19,7 @@
use std::{ffi::CString, path::Path};
use suricata_sys::sys::{SCError, SCLogMessage};
use suricata_sys::sys::SCError;
use suricata_sys::sys::{SCFatalErrorOnInitStatic, SCLogLevel};
/// The Suricata log-level.
@ -66,12 +66,13 @@ pub fn sclog(level: SCLogLevel, file: &str, line: u32, function: &str, message:
}
/// SCLogMessage wrapper.
#[cfg(not(any(test, feature = "testing")))]
pub fn log_message(
level: SCLogLevel, filename: &str, line: std::os::raw::c_uint, function: &str, module: &str,
message: &str,
) -> SCError {
unsafe {
SCLogMessage(
suricata_sys::sys::SCLogMessage(
level,
to_safe_cstring(filename).as_ptr(),
line,
@ -82,6 +83,17 @@ pub fn log_message(
}
}
/// Unit tests do not link the C library: print instead of calling
/// SCLogMessage, like the fallback in the suricata crate's debug.rs.
#[cfg(any(test, feature = "testing"))]
pub fn log_message(
level: SCLogLevel, filename: &str, line: std::os::raw::c_uint, _function: &str, _module: &str,
message: &str,
) -> SCError {
println!("{}:{} <{:?}> -- {}", filename, line, level, message);
SCError::SC_OK
}
// Convert a &str into a CString by first stripping NUL bytes.
fn to_safe_cstring(val: &str) -> CString {
let mut safe = Vec::with_capacity(val.len());

Loading…
Cancel
Save