diff options
author | Chelsea Holland Komlo <me@chelseakomlo.com> | 2017-12-21 14:47:41 -0500 |
---|---|---|
committer | Chelsea Holland Komlo <me@chelseakomlo.com> | 2017-12-21 19:01:30 -0500 |
commit | 4cdf0c8899796582b058d289bcd21b95dd1bd452 (patch) | |
tree | 54cfa9f269e92acc13624f5d9ecb42d46b40b942 /src/rust/tor_log | |
parent | d4be1b1e76a8f31bfd38109f91cffda949c1ebba (diff) | |
download | tor-4cdf0c8899796582b058d289bcd21b95dd1bd452.tar.gz tor-4cdf0c8899796582b058d289bcd21b95dd1bd452.zip |
add testing implementation for tor logging
Diffstat (limited to 'src/rust/tor_log')
-rw-r--r-- | src/rust/tor_log/lib.rs | 1 | ||||
-rw-r--r-- | src/rust/tor_log/tor_log.rs | 121 |
2 files changed, 96 insertions, 26 deletions
diff --git a/src/rust/tor_log/lib.rs b/src/rust/tor_log/lib.rs index 915910d003..72f9e38339 100644 --- a/src/rust/tor_log/lib.rs +++ b/src/rust/tor_log/lib.rs @@ -14,4 +14,3 @@ extern crate tor_allocate; mod tor_log; pub use tor_log::*; -pub use tor_log::log::*; diff --git a/src/rust/tor_log/tor_log.rs b/src/rust/tor_log/tor_log.rs index f583ac6dcc..9380934f33 100644 --- a/src/rust/tor_log/tor_log.rs +++ b/src/rust/tor_log/tor_log.rs @@ -86,31 +86,6 @@ pub fn tor_log_msg_impl( unsafe { log::tor_log_string(c_severity, c_domain, func_ptr, msg_ptr) } } -/// This module exposes no-op functionality for testing other Rust modules -/// without linking to C. -#[cfg(any(test, feature = "testing"))] -pub mod log { - use libc::{c_char, c_int}; - use super::LogDomain; - use super::LogSeverity; - - pub unsafe fn tor_log_string<'a>( - _severity: c_int, - _domain: u32, - _function: *const c_char, - _message: *const c_char, - ) { - } - - pub unsafe fn translate_domain(_domain: LogDomain) -> u32 { - 1 - } - - pub unsafe fn translate_severity(_severity: LogSeverity) -> c_int { - 1 - } -} - /// This implementation is used when compiling for actual use, as opposed to /// testing. #[cfg(all(not(test), not(feature = "testing")))] @@ -164,3 +139,99 @@ pub mod log { ); } } + +/// This module exposes no-op functionality for testing other Rust modules +/// without linking to C. +#[cfg(any(test, feature = "testing"))] +pub mod log { + use libc::{c_char, c_int}; + use super::LogDomain; + use super::LogSeverity; + + pub static mut LAST_LOGGED_FUNCTION: *mut String = 0 as *mut String; + pub static mut LAST_LOGGED_MESSAGE: *mut String = 0 as *mut String; + + pub unsafe fn tor_log_string<'a>( + _severity: c_int, + _domain: u32, + function: *const c_char, + message: *const c_char, + ) { + use std::ffi::CStr; + + let f = CStr::from_ptr(function); + let fct = match f.to_str() { + Ok(n) => n, + Err(_) => "", + }; + LAST_LOGGED_FUNCTION = Box::into_raw(Box::new(String::from(fct))); + + let m = CStr::from_ptr(message); + let msg = match m.to_str() { + Ok(n) => n, + Err(_) => "", + }; + LAST_LOGGED_MESSAGE = Box::into_raw(Box::new(String::from(msg))); + } + + pub unsafe fn translate_domain(_domain: LogDomain) -> u32 { + 1 + } + + pub unsafe fn translate_severity(_severity: LogSeverity) -> c_int { + 1 + } +} + +#[cfg(test)] +mod test { + use tor_log::*; + use tor_log::log::{LAST_LOGGED_FUNCTION, LAST_LOGGED_MESSAGE}; + + #[test] + fn test_get_log_message() { + { + fn test_macro() { + tor_log_msg!( + LogSeverity::Warn, + LogDomain::LdNet, + "test_macro", + "test log message {}", + "a", + ); + } + + test_macro(); + + let function = unsafe { Box::from_raw(LAST_LOGGED_FUNCTION) }; + assert_eq!("test_macro", *function); + + let message = unsafe { Box::from_raw(LAST_LOGGED_MESSAGE) }; + assert_eq!("test log message a", *message); + } + + { + fn test_macro() { + tor_log_msg!( + LogSeverity::Warn, + LogDomain::LdNet, + "next_test_macro", + "test log message {} {} {} {} {}", + 1, + 2, + 3, + 4, + 5 + ); + } + + test_macro(); + + let function = unsafe { Box::from_raw(LAST_LOGGED_FUNCTION) }; + assert_eq!("next_test_macro", *function); + + let message = unsafe { Box::from_raw(LAST_LOGGED_MESSAGE) }; + assert_eq!("test log message 1 2 3 4 5", *message); + } + } +} |