From aeb58ae1ea1e1376c34d32335b77d4dc003e33f4 Mon Sep 17 00:00:00 2001 From: jrx Date: Thu, 14 Nov 2024 14:38:17 +0100 Subject: [PATCH 1/7] added a rustfmt.toml file according to the existing defaults in this repo. => really helpful when having this as a git submodule of another workspace ;-) --- rustfmt.toml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 rustfmt.toml diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..57d44b4 --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,5 @@ +newline_style = "Unix" +tab_spaces = 4 +max_width = 100 +chain_width = 60 +use_small_heuristics = "Default" From 897f0aa828cb66f75c24247dfeaf208052365b99 Mon Sep 17 00:00:00 2001 From: jrx Date: Thu, 14 Nov 2024 14:38:34 +0100 Subject: [PATCH 2/7] file case typo --- src/dsc.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dsc.rs b/src/dsc.rs index 3391846..43e837e 100755 --- a/src/dsc.rs +++ b/src/dsc.rs @@ -317,7 +317,7 @@ mod tests { #[should_panic(expected = "Incomplete(Unknown)")] fn test_bad_file() { let mut test_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); - test_path.push("tests/test_data/Bad Data/DSC/badfile"); + test_path.push("tests/test_data/Bad Data/DSC/Badfile"); let buffer = fs::read(test_path).unwrap(); let (_, _) = SharedCacheStrings::parse_dsc(&buffer).unwrap(); From ce00e4c3539ef0eb2703e5e4e508613138746ea9 Mon Sep 17 00:00:00 2001 From: jrx Date: Thu, 14 Nov 2024 14:39:33 +0100 Subject: [PATCH 3/7] - filesystem tests ignored on non macos systems - test values changed according to the downloadable test data --- src/parser.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index 85af918..7942880 100755 --- a/src/parser.rs +++ b/src/parser.rs @@ -375,18 +375,21 @@ mod tests { use std::path::PathBuf; #[test] + #[cfg(target_os = "macos")] fn test_collect_strings_system() { let uuidtext_results = collect_strings_system().unwrap(); assert!(uuidtext_results.len() > 100); } #[test] + #[cfg(target_os = "macos")] fn test_collect_timesync_system() { let timesync_results = collect_timesync_system().unwrap(); assert!(timesync_results.len() > 1); } #[test] + #[cfg(target_os = "macos")] fn test_collect_timesync_archive() { let mut test_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); test_path.push("tests/test_data/system_logs_big_sur.logarchive/timesync"); @@ -409,6 +412,7 @@ mod tests { } #[test] + #[cfg(target_os = "macos")] fn test_collect_shared_strings_system() { let shared_strings_results = collect_shared_strings_system().unwrap(); assert!(shared_strings_results[0].ranges.len() > 1); @@ -444,9 +448,9 @@ mod tests { let strings_results = collect_strings(&test_path.display().to_string()).unwrap(); assert_eq!(strings_results.len(), 536); assert_eq!(strings_results[0].signature, 1719109785); - assert_eq!(strings_results[0].uuid, "5283D7FC2531558F2C1ACE9AF26A0F"); + assert_eq!(strings_results[0].uuid, "D9B97EA2CD39C7A9AF1888E041B9E1"); assert_eq!(strings_results[0].entry_descriptors.len(), 2); - assert_eq!(strings_results[0].footer_data.len(), 48096); + assert_eq!(strings_results[0].footer_data.len(), 238974); assert_eq!(strings_results[0].number_entries, 2); assert_eq!(strings_results[0].unknown_minor_version, 1); assert_eq!(strings_results[0].unknown_major_version, 2); From 9994b2410634f3c162e126285071a392f5e024c8 Mon Sep 17 00:00:00 2001 From: jrx Date: Thu, 14 Nov 2024 15:16:52 +0100 Subject: [PATCH 4/7] test_collect_strings_archive assertions back to previous values --- src/parser.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index 7942880..5750375 100755 --- a/src/parser.rs +++ b/src/parser.rs @@ -448,9 +448,9 @@ mod tests { let strings_results = collect_strings(&test_path.display().to_string()).unwrap(); assert_eq!(strings_results.len(), 536); assert_eq!(strings_results[0].signature, 1719109785); - assert_eq!(strings_results[0].uuid, "D9B97EA2CD39C7A9AF1888E041B9E1"); + assert_eq!(strings_results[0].uuid, "5283D7FC2531558F2C1ACE9AF26A0F"); assert_eq!(strings_results[0].entry_descriptors.len(), 2); - assert_eq!(strings_results[0].footer_data.len(), 238974); + assert_eq!(strings_results[0].footer_data.len(), 48096); assert_eq!(strings_results[0].number_entries, 2); assert_eq!(strings_results[0].unknown_minor_version, 1); assert_eq!(strings_results[0].unknown_major_version, 2); From 6f76f6d59ba5cdb923854f0af3d928692a8697ed Mon Sep 17 00:00:00 2001 From: jrx Date: Thu, 14 Nov 2024 15:40:44 +0100 Subject: [PATCH 5/7] collect_strings ordering --- src/parser.rs | 51 +++++++++++++++++++++++++-------------------------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index 5750375..9d1d45b 100755 --- a/src/parser.rs +++ b/src/parser.rs @@ -100,31 +100,24 @@ pub fn build_log( pub fn collect_strings(path: &str) -> Result, ParserError> { let paths_results = fs::read_dir(path); - let paths = match paths_results { - Ok(path) => path, - Err(err) => { - error!( - "[macos-unifiedlogs] Failed to read directory path: {:?}", - err - ); - return Err(ParserError::Dir); - } - }; - - let mut uuidtext_vec: Vec = Vec::new(); + let paths = paths_results.map_err(|err| { + error!("[macos-unifiedlogs] Failed to read directory path: {err:?}"); + ParserError::Dir + })?; + + let mut entries = paths + .flat_map(|path| { + path.inspect_err(|err| { + error!("[macos-unifiedlogs] Failed to get directory entry: {err:?}",) + }) + .ok() + }) + .collect::>(); + entries.sort_by(|a, b| a.file_name().as_os_str().cmp(b.file_name().as_os_str())); + + let mut uuidtext_vec: Vec = Vec::with_capacity(entries.len()); // Start process to read a directory containing subdirectories that contain the uuidtext files - for path in paths { - let dir_entry = match path { - Ok(entry) => entry, - Err(err) => { - error!( - "[macos-unifiedlogs] Failed to get directory entry: {:?}", - err - ); - continue; - } - }; - + for dir_entry in entries { let type_results = dir_entry.file_type(); let entry_type = match type_results { Ok(dir_type) => dir_type, @@ -448,12 +441,18 @@ mod tests { let strings_results = collect_strings(&test_path.display().to_string()).unwrap(); assert_eq!(strings_results.len(), 536); assert_eq!(strings_results[0].signature, 1719109785); - assert_eq!(strings_results[0].uuid, "5283D7FC2531558F2C1ACE9AF26A0F"); + assert_eq!(strings_results[0].uuid, "B6B65F4DC53ED38FEB0DDF61809853"); assert_eq!(strings_results[0].entry_descriptors.len(), 2); - assert_eq!(strings_results[0].footer_data.len(), 48096); + assert_eq!(strings_results[0].footer_data.len(), 1707); assert_eq!(strings_results[0].number_entries, 2); assert_eq!(strings_results[0].unknown_minor_version, 1); assert_eq!(strings_results[0].unknown_major_version, 2); + + assert_eq!(strings_results[1].uuid, "D9B97EA2CD39C7A9AF1888E041B9E1"); + assert_eq!(strings_results[1].footer_data.len(), 238974); + + assert_eq!(strings_results[2].uuid, "2578ECF07936A6A882574764C7C785"); + assert_eq!(strings_results[2].footer_data.len(), 68714); } #[test] From 7c3ece8a434049ca4534c5373af535c49fb7bec9 Mon Sep 17 00:00:00 2001 From: jrx Date: Thu, 14 Nov 2024 15:42:05 +0100 Subject: [PATCH 6/7] made clippy happy --- src/parser.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index 9d1d45b..b526785 100755 --- a/src/parser.rs +++ b/src/parser.rs @@ -361,10 +361,12 @@ pub fn collect_timesync(path: &str) -> Result, ParserError> { #[cfg(test)] mod tests { use crate::parser::{ - build_log, collect_shared_strings, collect_shared_strings_system, collect_strings, - collect_strings_system, collect_timesync, collect_timesync_system, parse_log, + build_log, collect_shared_strings, collect_strings, collect_timesync, parse_log, + }; + #[cfg(target_os = "macos")] + use crate::parser::{ + collect_shared_strings_system, collect_strings_system, collect_timesync_system, }; - use std::path::PathBuf; #[test] From d2acbb3606f0f12fff9fbd25988b7ece75625a30 Mon Sep 17 00:00:00 2001 From: jrx Date: Tue, 19 Nov 2024 09:48:56 +0100 Subject: [PATCH 7/7] parser tests => sort in the test --- src/parser.rs | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index b526785..09cc789 100755 --- a/src/parser.rs +++ b/src/parser.rs @@ -98,14 +98,12 @@ pub fn build_log( /// Parse all UUID files in provided directory. The directory should follow the same layout as the live system (ex: path/to/files//) pub fn collect_strings(path: &str) -> Result, ParserError> { - let paths_results = fs::read_dir(path); - - let paths = paths_results.map_err(|err| { + let paths = fs::read_dir(path).map_err(|err| { error!("[macos-unifiedlogs] Failed to read directory path: {err:?}"); ParserError::Dir })?; - let mut entries = paths + let entries = paths .flat_map(|path| { path.inspect_err(|err| { error!("[macos-unifiedlogs] Failed to get directory entry: {err:?}",) @@ -113,7 +111,6 @@ pub fn collect_strings(path: &str) -> Result, ParserError> { .ok() }) .collect::>(); - entries.sort_by(|a, b| a.file_name().as_os_str().cmp(b.file_name().as_os_str())); let mut uuidtext_vec: Vec = Vec::with_capacity(entries.len()); // Start process to read a directory containing subdirectories that contain the uuidtext files @@ -440,21 +437,24 @@ mod tests { let mut test_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); test_path.push("tests/test_data/system_logs_big_sur.logarchive"); - let strings_results = collect_strings(&test_path.display().to_string()).unwrap(); + let mut strings_results = collect_strings(&test_path.display().to_string()).unwrap(); assert_eq!(strings_results.len(), 536); + + strings_results.sort_by(|a, b| a.uuid.cmp(&b.uuid)); + assert_eq!(strings_results[0].signature, 1719109785); - assert_eq!(strings_results[0].uuid, "B6B65F4DC53ED38FEB0DDF61809853"); - assert_eq!(strings_results[0].entry_descriptors.len(), 2); - assert_eq!(strings_results[0].footer_data.len(), 1707); - assert_eq!(strings_results[0].number_entries, 2); + assert_eq!(strings_results[0].uuid, "004EAF1C2B310DA0383BE3D60B80E8"); + assert_eq!(strings_results[0].entry_descriptors.len(), 1); + assert_eq!(strings_results[0].footer_data.len(), 2847); + assert_eq!(strings_results[0].number_entries, 1); assert_eq!(strings_results[0].unknown_minor_version, 1); assert_eq!(strings_results[0].unknown_major_version, 2); - assert_eq!(strings_results[1].uuid, "D9B97EA2CD39C7A9AF1888E041B9E1"); - assert_eq!(strings_results[1].footer_data.len(), 238974); + assert_eq!(strings_results[1].uuid, "00B3D870FB3AE8BDC1BA3A60D0B9A0"); + assert_eq!(strings_results[1].footer_data.len(), 2164); - assert_eq!(strings_results[2].uuid, "2578ECF07936A6A882574764C7C785"); - assert_eq!(strings_results[2].footer_data.len(), 68714); + assert_eq!(strings_results[2].uuid, "014C44534A3A748476ABD88D376918"); + assert_eq!(strings_results[2].footer_data.len(), 19011); } #[test]