Skip to content

Commit

Permalink
tests strategic skip journalreader utmpx on macos windows
Browse files Browse the repository at this point in the history
  • Loading branch information
jtmoon79 committed Nov 5, 2023
1 parent e0353bb commit 5618d05
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 69 deletions.
2 changes: 1 addition & 1 deletion src/data/utmpx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ cfg_supports_uapi_not!{

// end copied code from crate `uapi` under MIT license

/// [`size_of::<utmpx>`] (384).
/// [`size_of::<utmpx>`]; 384 typically, 640 on Mac OS.
///
/// [`size_of::<utmpx>`]: std::mem::size_of
pub const UTMPX_SZ: usize = size_of::<utmpx>();
Expand Down
36 changes: 35 additions & 1 deletion src/libload/systemd_dlopen2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::bindings::sd_journal_h::{
size_t,
sd_journal,
};

use std::fmt;
use std::sync::{RwLock, Arc};

#[cfg(not(windows))]
Expand Down Expand Up @@ -278,6 +278,40 @@ pub enum LoadLibraryError {
PrevErr,
}

impl PartialEq for LoadLibraryError {
/// allow `Err` == `PrevErr`
fn eq(&self, other: &LoadLibraryError) -> bool {
match (self, other) {
(&LoadLibraryError::Ok, &LoadLibraryError::Ok) |
(&LoadLibraryError::Err(_), &LoadLibraryError::Err(_)) |
(&LoadLibraryError::Err(_), &LoadLibraryError::PrevErr) |
(&LoadLibraryError::PrevErr, &LoadLibraryError::Err(_)) |
(&LoadLibraryError::PrevErr, &LoadLibraryError::PrevErr) => true,
_ => false,
}
}
}
impl Eq for LoadLibraryError {}

impl fmt::Debug for LoadLibraryError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
LoadLibraryError::Ok => {
f.debug_struct("LoadLibraryError::Ok")
.finish()
}
LoadLibraryError::Err(_) => {
f.debug_struct("LoadLibraryError::Err")
.finish()
}
LoadLibraryError::PrevErr => {
f.debug_struct("LoadLibraryError::PrevErr")
.finish()
}
}
}
}

/// Wrapper to set the global static variables.
fn set_systemd_journal_api(container: JournalApiContainer) {
defñ!();
Expand Down
88 changes: 57 additions & 31 deletions src/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2321,20 +2321,31 @@ const UTMPX1: utmpx = utmpx {
*/

pub const SYSTEMD_NOT_AVAILABLE: bool = cfg!(target_os = "windows") || cfg!(target_os = "macos");
pub const UTMPX_NON_STANDARD: bool = cfg!(target_os = "macos");

// utmpx file formats differ among Unixii
// See this difference in Mac OS and Linux
// https://github.com/libyal/dtformats/blob/55007dcac48efff42c497e739208ebfb88e4048d/documentation/Utmp%20login%20records%20format.asciidoc
// See Issue #217

/// scraped `/var/log/utmp` file, manually modified
///
/// ut_type INIT_PROCESS 5
/// ut_pid 41908
/// ut_line 'pts/1'
/// ut_id 'ts/1'
/// ut_user 'admin'
/// ut_host '192.168.1.5'
/// ut_session 25
/// addr_v6 2F7CA8C0:B5:0:0
/// e_termination 7
/// e_exit 1
/// tv_sec.tv_usec 1577836800.119284 (2020-01-01T12:00:00.000119284+00:00)
/// 384 bytes
///
/// - ut_type INIT_PROCESS 5
/// - ut_pid 41908
/// - ut_line 'pts/1'
/// - ut_id 'ts/1'
/// - ut_user 'admin'
/// - ut_host '192.168.1.5'
/// - ut_session 25
/// - addr_v6 2F7CA8C0:B5:0:0
/// - e_termination 7
/// - e_exit 1
/// - tv_sec.tv_usec 1577836800.119284 (2020-01-01T12:00:00.000119284+00:00)
///
#[cfg(not(target_os = "macos"))]
pub const UTMPX_BUFFER1: [u8; UTMPX_SZ] = [
0x05, 0x00, 0x02, 0x00, 0xb4, 0xa3, 0x00, 0x00, b'p', b't',
b's', b'/', b'1', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Expand Down Expand Up @@ -2376,20 +2387,26 @@ pub const UTMPX_BUFFER1: [u8; UTMPX_SZ] = [
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
];
/// same as the other `UTMPX_BUFFER1` but 640 bytes
#[cfg(target_os = "macos")]
pub const UTMPX_BUFFER1: [u8; UTMPX_SZ] = [0; UTMPX_SZ];

/// two seconds after `UTMPX_BUFFER1`
///
/// ut_type USER_PROCESS 7
/// ut_pid 13236
/// ut_line 'pts/0'
/// ut_id 'ts/0'
/// ut_user 'root'
/// ut_host '192.168.1.4'
/// ut_session 5
/// e_termination 7
/// e_exit 3
/// tv_sec.tv_usec 1577836802.000123636 (2020-01-01T12:00:02.000123636+00:00)
/// 384 bytes
///
/// - ut_type USER_PROCESS 7
/// - ut_pid 13236
/// - ut_line 'pts/0'
/// - ut_id 'ts/0'
/// - ut_user 'root'
/// - ut_host '192.168.1.4'
/// - ut_session 5
/// - e_termination 7
/// - e_exit 3
/// - tv_sec.tv_usec 1577836802.000123636 (2020-01-01T12:00:02.000123636+00:00)
///
#[cfg(not(target_os = "macos"))]
pub const UTMPX_BUFFER2: [u8; UTMPX_SZ] = [
0x07, 0x00, 0x00, 0x00, 0xb4, 0x33, 0x00, 0x00, 0x70, 0x74,
0x73, 0x2f, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Expand Down Expand Up @@ -2431,20 +2448,26 @@ pub const UTMPX_BUFFER2: [u8; UTMPX_SZ] = [
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
];
/// same as the other `UTMPX_BUFFER2` but 640 bytes
#[cfg(target_os = "macos")]
pub const UTMPX_BUFFER2: [u8; UTMPX_SZ] = [0; UTMPX_SZ];

/// four seconds after `UTMPX_BUFFER1`
///
/// ut_type USER_PROCESS 7
/// ut_pid 13236
/// ut_line 'pts/0'
/// ut_id 'ts/0'
/// ut_user 'root'
/// ut_host '192.168.1.4'
/// ut_session 5
/// e_termination 7
/// e_exit 3
/// tv_sec.tv_usec 1577836804.000123636 (2020-01-01T12:00:04.000123636+00:00)
/// 384 bytes
///
/// - ut_type USER_PROCESS 7
/// - ut_pid 13236
/// - ut_line 'pts/0'
/// - ut_id 'ts/0'
/// - ut_user 'root'
/// - ut_host '192.168.1.4'
/// - ut_session 5
/// - e_termination 7
/// - e_exit 3
/// - tv_sec.tv_usec 1577836804.000123636 (2020-01-01T12:00:04.000123636+00:00)
///
#[cfg(not(target_os = "macos"))]
pub const UTMPX_BUFFER3: [u8; UTMPX_SZ] = [
0x07, 0x00, 0x00, 0x00, 0xb4, 0x33, 0x00, 0x00, 0x70, 0x74,
0x73, 0x2f, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Expand Down Expand Up @@ -2486,6 +2509,9 @@ pub const UTMPX_BUFFER3: [u8; UTMPX_SZ] = [
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
];
/// same as the other `UTMPX_BUFFER3` but 640 bytes
#[cfg(target_os = "macos")]
pub const UTMPX_BUFFER3: [u8; UTMPX_SZ] = [0; UTMPX_SZ];

pub const UTMPX_BUFFER_00: [u8; UTMPX_SZ] = [0; UTMPX_SZ];
pub const UTMPX_BUFFER_55: [u8; UTMPX_SZ] = [0x55; UTMPX_SZ];
Expand Down
78 changes: 42 additions & 36 deletions src/tests/journalreader_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,18 @@ use crate::tests::common::{
JOURNAL_FILE_UBUNTU_22_SYSTEM_EVENT_FILESZ,
JOURNAL_FILE_UBUNTU_22_SYSTEM_ENTRY_FIRST_DT,
JOURNAL_FILE_UBUNTU_22_SYSTEM_ENTRY_LAST_DT,
SYSTEMD_NOT_AVAILABLE,
};

use bstr::ByteSlice;
use ::criterion::black_box;
use ::test_case::test_case;
use ::si_trace_print::{
defn,
defo,
defx,
defñ,
};

// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Expand Down Expand Up @@ -182,10 +189,14 @@ fn test_errno_to_errorkind() {
assert_eq!(ErrorKind::PermissionDenied, ek);
}

#[cfg(not(target_os="windows"))]
#[test_case(&JOURNAL_FILE_RHE_91_SYSTEM_FPATH)]
#[test_case(&JOURNAL_FILE_UBUNTU_22_SYSTEM_FPATH)]
fn test_mtime(path: &FPath) {
if SYSTEMD_NOT_AVAILABLE
{
defñ!("skip");
return;
}
load_library_systemd();
let jr1 = JournalReader::new(
path.clone(),
Expand All @@ -197,13 +208,27 @@ fn test_mtime(path: &FPath) {
}

/// test creating a new `JournalReader`
#[cfg(not(target_os="windows"))]
#[test_case(&NTF_JOURNAL_EMPTY_FPATH, false)]
#[test_case(&FPath::from("BAD PATH"), false)]
#[test_case(&*JOURNAL_FILE_RHE_91_SYSTEM_FPATH, true)]
#[test_case(&*JOURNAL_FILE_UBUNTU_22_SYSTEM_FPATH, true)]
fn test_JournalReader_new_(path: &FPath, ok: bool) {
assert!(matches!(load_library_systemd(), LoadLibraryError::Ok));
defn!();
let load = load_library_systemd();
defo!("load_library_systemd() returned {:?}", load);
if SYSTEMD_NOT_AVAILABLE
{
match load {
LoadLibraryError::Ok => {
panic!("Unexpected match LoadLibraryError::Ok")
}
LoadLibraryError::Err(_) => {}
LoadLibraryError::PrevErr => {}
}
defx!("successfully failed");
return;
}
assert!(matches!(load, LoadLibraryError::Ok));
match JournalReader::new(
path.clone(),
JournalOutput::Short,
Expand All @@ -216,39 +241,10 @@ fn test_JournalReader_new_(path: &FPath, ok: bool) {
assert!(!ok, "JournalReader::new({:?}) should have succeeded", path);
}
}
}

/// test loading systemd on Windows
#[cfg(target_os="windows")]
#[test]
fn test_load_library_systemd_windows() {
match load_library_systemd() {
LoadLibraryError::Err(_err) => {}
_ => {
panic!("load_library_systemd succeeded on Windows which is unexpected")
}
}
}

/// test creating a new `JournalReader` on Windows
#[cfg(target_os="windows")]
#[test]
#[should_panic]
fn test_JournalReader_new_windows() {
match JournalReader::new(
NTF_JOURNAL_EMPTY_FPATH.clone(),
JournalOutput::Short,
*FO_0,
) {
Ok(_) => {
panic!("JournalReader::new succeeded on Windows which is unexpected")
}
Err(_err) => {}
}
defx!();
}

/// test the output of the first entry returned by `JournalReader::next()`
#[cfg(not(target_os="windows"))]
#[test_case(
&*JOURNAL_FILE_RHE_91_SYSTEM_PATH,
JournalOutput::Short,
Expand Down Expand Up @@ -315,7 +311,12 @@ fn test_JournalReader_entry1_output(
journal_output: JournalOutput,
expect_data: &str,
) {
assert!(matches!(load_library_systemd(), LoadLibraryError::Ok));
if SYSTEMD_NOT_AVAILABLE
{
defñ!("skip");
return;
}
load_library_systemd();
let fpath = path_to_fpath(path);
let mut journalreader = JournalReader::new(
fpath,
Expand Down Expand Up @@ -347,11 +348,10 @@ fn test_JournalReader_entry1_output(
"\nje.as_bytes():\n{:?}\nexpect_data:\n{:?}\n",
je.as_bytes().to_str(), expect_data
);

defx!();
}

/// test the summary statistics after processing the entire file
#[cfg(not(target_os="windows"))]
#[test_case(
&*JOURNAL_FILE_RHE_91_SYSTEM_PATH,
*JOURNAL_FILE_RHE_91_SYSTEM_EVENT_FILESZ,
Expand Down Expand Up @@ -421,6 +421,11 @@ fn test_JournalReader_next_summary(
api_call_errors: Count,
range_error_opt: ForceErrorRangeOpt,
) {
if SYSTEMD_NOT_AVAILABLE
{
defñ!("skip");
return;
}
assert!(matches!(load_library_systemd(), LoadLibraryError::Ok));
let fpath = path_to_fpath(path);
let fpath2 = fpath.clone();
Expand Down Expand Up @@ -502,4 +507,5 @@ fn test_JournalReader_next_summary(
panic!("summary_c.readerdata() should be SummaryReaderData::Journal");
}
}
defx!();
}
3 changes: 3 additions & 0 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ pub mod printers_tests;
pub mod sysline_tests;
pub mod syslinereader_tests;
pub mod syslogprocessor_tests;
// XXX: skip all UTMP tests until proper fix for Issue #217
#[cfg(not(target_os="macos"))]
pub mod utmpxreader_tests;
// XXX: skip all UTMP tests until proper fix for Issue #217
#[cfg(not(target_os="macos"))]
pub mod utmpx_tests;

0 comments on commit 5618d05

Please sign in to comment.