From 4a4b417ef8f8edbbf25e7c0caf14eafaa3ac4bec Mon Sep 17 00:00:00 2001 From: David McDonald Date: Sat, 21 Dec 2024 17:22:32 -0600 Subject: [PATCH] Updates tests --- tests/big_sur_tests.rs | 276 +++++++++++-------------------------- tests/high_sierra_tests.rs | 110 +++++---------- tests/monterey_tests.rs | 74 ++++------ 3 files changed, 136 insertions(+), 324 deletions(-) diff --git a/tests/big_sur_tests.rs b/tests/big_sur_tests.rs index 1b35fe9..7104b38 100755 --- a/tests/big_sur_tests.rs +++ b/tests/big_sur_tests.rs @@ -5,35 +5,31 @@ // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and limitations under the License. -use std::{fs, path::PathBuf}; +use std::{fs::File, path::PathBuf}; use macos_unifiedlogs::{ + filesystem::LogarchiveProvider, parser::{build_log, collect_shared_strings, collect_strings, collect_timesync, parse_log}, + traits::FileProvider, unified_log::{LogData, UnifiedLogData}, }; use regex::Regex; -fn collect_logs(path: &str) -> Vec { - let paths = fs::read_dir(path).unwrap(); - - let mut log_data_vec: Vec = Vec::new(); - for path in paths { - let data = path.unwrap(); - let full_path = data.path().display().to_string(); - let log_data = parse_log(&full_path).unwrap(); - log_data_vec.push(log_data); - } - - return log_data_vec; +fn collect_logs(provider: &dyn FileProvider) -> Vec { + provider + .tracev3_files() + .map(|mut file| parse_log(file.reader()).unwrap()) + .collect() } #[test] fn test_parse_log_big_sur() { let mut test_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); test_path.push("tests/test_data/system_logs_big_sur.logarchive"); - test_path.push("Persist/0000000000000004.tracev3"); - let log_data = parse_log(&test_path.display().to_string()).unwrap(); + + let handle = File::open(test_path.as_path()).unwrap(); + let log_data = parse_log(handle).unwrap(); assert_eq!(log_data.catalog_data[0].firehose.len(), 82); assert_eq!(log_data.catalog_data[0].simpledump.len(), 0); @@ -52,18 +48,15 @@ fn test_parse_log_big_sur() { fn test_big_sur_livedata() { let mut test_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); test_path.push("tests/test_data/system_logs_big_sur.logarchive"); - let string_results = collect_strings(&test_path.display().to_string()).unwrap(); - test_path.push("dsc"); - let shared_strings_results = collect_shared_strings(&test_path.display().to_string()).unwrap(); - test_path.pop(); - - test_path.push("timesync"); - let timesync_data = collect_timesync(&test_path.display().to_string()).unwrap(); - test_path.pop(); + let provider = LogarchiveProvider::new(test_path.as_path()); + let string_results = collect_strings(&provider).unwrap(); + let shared_strings_results = collect_shared_strings(&provider).unwrap(); + let timesync_data = collect_timesync(&provider).unwrap(); test_path.push("logdata.LiveData.tracev3"); - let results = parse_log(&test_path.display().to_string()).unwrap(); + let handle = File::open(test_path.as_path()).unwrap(); + let results = parse_log(handle).unwrap(); test_path.pop(); let exclude_missing = false; @@ -107,19 +100,17 @@ fn test_big_sur_livedata() { fn test_build_log_big_sur() { let mut test_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); test_path.push("tests/test_data/system_logs_big_sur.logarchive"); - let string_results = collect_strings(&test_path.display().to_string()).unwrap(); - test_path.push("dsc"); - let shared_strings_results = collect_shared_strings(&test_path.display().to_string()).unwrap(); - test_path.pop(); - - test_path.push("timesync"); - let timesync_data = collect_timesync(&test_path.display().to_string()).unwrap(); - test_path.pop(); + let provider = LogarchiveProvider::new(test_path.as_path()); + let string_results = collect_strings(&provider).unwrap(); + let shared_strings_results = collect_shared_strings(&provider).unwrap(); + let timesync_data = collect_timesync(&provider).unwrap(); test_path.push("Persist/0000000000000004.tracev3"); - let log_data = parse_log(&test_path.display().to_string()).unwrap(); + let handle = File::open(test_path.as_path()).unwrap(); + + let log_data = parse_log(handle).unwrap(); let exclude_missing = false; let (results, _) = build_log( @@ -159,40 +150,12 @@ fn test_build_log_big_sur() { fn test_parse_all_logs_big_sur() { let mut test_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); test_path.push("tests/test_data/system_logs_big_sur.logarchive"); - let string_results = collect_strings(&test_path.display().to_string()).unwrap(); - - test_path.push("dsc"); - let shared_strings_results = collect_shared_strings(&test_path.display().to_string()).unwrap(); - test_path.pop(); - - test_path.push("timesync"); - let timesync_data = collect_timesync(&test_path.display().to_string()).unwrap(); - test_path.pop(); - - test_path.push("Persist"); - - let mut log_data = collect_logs(&test_path.display().to_string()); - test_path.pop(); - - test_path.push("HighVolume"); - let mut results = collect_logs(&test_path.display().to_string()); - log_data.append(&mut results); - test_path.pop(); - - test_path.push("Special"); - let mut results = collect_logs(&test_path.display().to_string()); - log_data.append(&mut results); - test_path.pop(); - - test_path.push("Signpost"); - let mut results = collect_logs(&test_path.display().to_string()); - log_data.append(&mut results); - test_path.pop(); - test_path.push("logdata.LiveData.tracev3"); - let results = parse_log(&test_path.display().to_string()).unwrap(); - log_data.push(results); - test_path.pop(); + let provider = LogarchiveProvider::new(test_path.as_path()); + let string_results = collect_strings(&provider).unwrap(); + let shared_strings_results = collect_shared_strings(&provider).unwrap(); + let timesync_data = collect_timesync(&provider).unwrap(); + let log_data = collect_logs(&provider); let mut log_data_vec: Vec = Vec::new(); let exclude_missing = false; @@ -317,35 +280,12 @@ fn test_parse_all_logs_big_sur() { fn test_parse_all_persist_logs_with_network_big_sur() { let mut test_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); test_path.push("tests/test_data/system_logs_big_sur.logarchive"); - let string_results = collect_strings(&test_path.display().to_string()).unwrap(); - - test_path.push("dsc"); - let shared_strings_results = collect_shared_strings(&test_path.display().to_string()).unwrap(); - test_path.pop(); - - test_path.push("timesync"); - let timesync_data = collect_timesync(&test_path.display().to_string()).unwrap(); - test_path.pop(); - - test_path.push("Persist"); - - let mut log_data = collect_logs(&test_path.display().to_string()); - test_path.pop(); - - test_path.push("HighVolume"); - let mut results = collect_logs(&test_path.display().to_string()); - log_data.append(&mut results); - test_path.pop(); - test_path.push("Special"); - let mut results = collect_logs(&test_path.display().to_string()); - log_data.append(&mut results); - test_path.pop(); - - test_path.push("logdata.LiveData.tracev3"); - let results = parse_log(&test_path.display().to_string()).unwrap(); - log_data.push(results); - test_path.pop(); + let provider = LogarchiveProvider::new(test_path.as_path()); + let string_results = collect_strings(&provider).unwrap(); + let shared_strings_results = collect_shared_strings(&provider).unwrap(); + let timesync_data = collect_timesync(&provider).unwrap(); + let log_data = collect_logs(&provider); let mut log_data_vec: Vec = Vec::new(); let exclude_missing = false; @@ -437,40 +377,12 @@ fn test_parse_all_persist_logs_with_network_big_sur() { fn test_parse_all_logs_private_big_sur() { let mut test_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); test_path.push("tests/test_data/system_logs_big_sur_private_enabled.logarchive"); - let string_results = collect_strings(&test_path.display().to_string()).unwrap(); - - test_path.push("dsc"); - let shared_strings_results = collect_shared_strings(&test_path.display().to_string()).unwrap(); - test_path.pop(); - - test_path.push("timesync"); - let timesync_data = collect_timesync(&test_path.display().to_string()).unwrap(); - test_path.pop(); - test_path.push("Persist"); - - let mut log_data = collect_logs(&test_path.display().to_string()); - test_path.pop(); - - test_path.push("HighVolume"); - let mut results = collect_logs(&test_path.display().to_string()); - log_data.append(&mut results); - test_path.pop(); - - test_path.push("Special"); - let mut results = collect_logs(&test_path.display().to_string()); - log_data.append(&mut results); - test_path.pop(); - - test_path.push("Signpost"); - let mut results = collect_logs(&test_path.display().to_string()); - log_data.append(&mut results); - test_path.pop(); - - test_path.push("logdata.LiveData.tracev3"); - let results = parse_log(&test_path.display().to_string()).unwrap(); - log_data.push(results); - test_path.pop(); + let provider = LogarchiveProvider::new(test_path.as_path()); + let string_results = collect_strings(&provider).unwrap(); + let shared_strings_results = collect_shared_strings(&provider).unwrap(); + let timesync_data = collect_timesync(&provider).unwrap(); + let log_data = collect_logs(&provider); let mut log_data_vec: Vec = Vec::new(); let exclude_missing = false; @@ -511,40 +423,12 @@ fn test_parse_all_logs_private_big_sur() { fn test_parse_all_logs_private_with_public_mix_big_sur() { let mut test_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); test_path.push("tests/test_data/system_logs_big_sur_public_private_data_mix.logarchive"); - let string_results = collect_strings(&test_path.display().to_string()).unwrap(); - - test_path.push("dsc"); - let shared_strings_results = collect_shared_strings(&test_path.display().to_string()).unwrap(); - test_path.pop(); - - test_path.push("timesync"); - let timesync_data = collect_timesync(&test_path.display().to_string()).unwrap(); - test_path.pop(); - - test_path.push("Persist"); - - let mut log_data = collect_logs(&test_path.display().to_string()); - test_path.pop(); - - test_path.push("HighVolume"); - let mut results = collect_logs(&test_path.display().to_string()); - log_data.append(&mut results); - test_path.pop(); - - test_path.push("Special"); - let mut results = collect_logs(&test_path.display().to_string()); - log_data.append(&mut results); - test_path.pop(); - test_path.push("Signpost"); - let mut results = collect_logs(&test_path.display().to_string()); - log_data.append(&mut results); - test_path.pop(); - - test_path.push("logdata.LiveData.tracev3"); - let results = parse_log(&test_path.display().to_string()).unwrap(); - log_data.push(results); - test_path.pop(); + let provider = LogarchiveProvider::new(test_path.as_path()); + let string_results = collect_strings(&provider).unwrap(); + let shared_strings_results = collect_shared_strings(&provider).unwrap(); + let timesync_data = collect_timesync(&provider).unwrap(); + let log_data = collect_logs(&provider); let mut log_data_vec: Vec = Vec::new(); let exclude_missing = false; @@ -607,19 +491,17 @@ fn test_parse_all_logs_private_with_public_mix_big_sur() { fn test_parse_all_logs_private_with_public_mix_big_sur_single_file() { let mut test_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); test_path.push("tests/test_data/system_logs_big_sur_public_private_data_mix.logarchive"); - let string_results = collect_strings(&test_path.display().to_string()).unwrap(); - test_path.push("dsc"); - let shared_strings_results = collect_shared_strings(&test_path.display().to_string()).unwrap(); - test_path.pop(); - - test_path.push("timesync"); - let timesync_data = collect_timesync(&test_path.display().to_string()).unwrap(); - test_path.pop(); + let provider = LogarchiveProvider::new(test_path.as_path()); + let string_results = collect_strings(&provider).unwrap(); + let shared_strings_results = collect_shared_strings(&provider).unwrap(); + let timesync_data = collect_timesync(&provider).unwrap(); test_path.push("Persist/0000000000000009.tracev3"); - let log_data = parse_log(&test_path.display().to_string()).unwrap(); + let handle = File::open(test_path.as_path()).unwrap(); + + let log_data = parse_log(handle).unwrap(); let exclude_missing = false; let (results, _) = build_log( @@ -661,19 +543,17 @@ fn test_parse_all_logs_private_with_public_mix_big_sur_single_file() { fn test_parse_all_logs_private_with_public_mix_big_sur_special_file() { let mut test_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); test_path.push("tests/test_data/system_logs_big_sur_public_private_data_mix.logarchive"); - let string_results = collect_strings(&test_path.display().to_string()).unwrap(); - test_path.push("dsc"); - let shared_strings_results = collect_shared_strings(&test_path.display().to_string()).unwrap(); - test_path.pop(); - - test_path.push("timesync"); - let timesync_data = collect_timesync(&test_path.display().to_string()).unwrap(); - test_path.pop(); + let provider = LogarchiveProvider::new(test_path.as_path()); + let string_results = collect_strings(&provider).unwrap(); + let shared_strings_results = collect_shared_strings(&provider).unwrap(); + let timesync_data = collect_timesync(&provider).unwrap(); test_path.push("Special/0000000000000008.tracev3"); - let log_data = parse_log(&test_path.display().to_string()).unwrap(); + let handle = File::open(test_path.as_path()).unwrap(); + + let log_data = parse_log(handle).unwrap(); let exclude_missing = false; let (results, _) = build_log( @@ -716,24 +596,22 @@ fn test_parse_all_logs_private_with_public_mix_big_sur_special_file() { fn test_big_sur_missing_oversize_strings() { let mut test_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); test_path.push("tests/test_data/system_logs_big_sur.logarchive"); - let string_results = collect_strings(&test_path.display().to_string()).unwrap(); - - test_path.push("dsc"); - let shared_strings_results = collect_shared_strings(&test_path.display().to_string()).unwrap(); - test_path.pop(); - test_path.push("timesync"); - let timesync_data = collect_timesync(&test_path.display().to_string()).unwrap(); - test_path.pop(); + let provider = LogarchiveProvider::new(test_path.as_path()); + let string_results = collect_strings(&provider).unwrap(); + let shared_strings_results = collect_shared_strings(&provider).unwrap(); + let timesync_data = collect_timesync(&provider).unwrap(); // livedata may have oversize string data in other tracev3 on disk test_path.push("logdata.LiveData.tracev3"); - let results = parse_log(&test_path.display().to_string()).unwrap(); + let handle = File::open(test_path.as_path()).unwrap(); + + let log_data = parse_log(handle).unwrap(); test_path.pop(); let exclude_missing = false; let (data, _) = build_log( - &results, + &log_data, &string_results, &shared_strings_results, ×ync_data, @@ -756,30 +634,30 @@ fn test_big_sur_missing_oversize_strings() { fn test_big_sur_oversize_strings_in_another_file() { let mut test_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); test_path.push("tests/test_data/system_logs_big_sur.logarchive"); - let string_results = collect_strings(&test_path.display().to_string()).unwrap(); - test_path.push("dsc"); - let shared_strings_results = collect_shared_strings(&test_path.display().to_string()).unwrap(); - test_path.pop(); - - test_path.push("timesync"); - let timesync_data = collect_timesync(&test_path.display().to_string()).unwrap(); - test_path.pop(); + let provider = LogarchiveProvider::new(test_path.as_path()); + let string_results = collect_strings(&provider).unwrap(); + let shared_strings_results = collect_shared_strings(&provider).unwrap(); + let timesync_data = collect_timesync(&provider).unwrap(); // Get most recent Persist tracev3 file could contain oversize log entries test_path.push("Persist/0000000000000005.tracev3"); - let mut log_data = parse_log(&test_path.display().to_string()).unwrap(); + let handle = File::open(test_path.as_path()).unwrap(); + + let mut log_data = parse_log(handle).unwrap(); test_path.pop(); test_path.pop(); // Get most recent Special tracev3 file that could contain oversize log entries test_path.push("Special/0000000000000005.tracev3"); - let mut special_data = parse_log(&test_path.display().to_string()).unwrap(); + let handle = File::open(test_path.as_path()).unwrap(); + let mut special_data = parse_log(handle).unwrap(); test_path.pop(); test_path.pop(); test_path.push("logdata.LiveData.tracev3"); - let mut results = parse_log(&test_path.display().to_string()).unwrap(); + let handle = File::open(test_path.as_path()).unwrap(); + let mut results = parse_log(handle).unwrap(); test_path.pop(); results.oversize.append(&mut log_data.oversize); diff --git a/tests/high_sierra_tests.rs b/tests/high_sierra_tests.rs index 8ffc50c..2bd0ae3 100755 --- a/tests/high_sierra_tests.rs +++ b/tests/high_sierra_tests.rs @@ -1,30 +1,24 @@ // Copyright 2022 Mandiant, Inc. All Rights Reserved -// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at -// http://www.apache.org/licenses/LICENSE-2.0 +// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 // Unless required by applicable law or agreed to in writing, software distributed under the License // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and limitations under the License. -use std::{fs, path::PathBuf}; +use std::{fs::File, path::PathBuf}; use macos_unifiedlogs::{ + filesystem::LogarchiveProvider, parser::{build_log, collect_shared_strings, collect_strings, collect_timesync, parse_log}, - unified_log::{LogData, UnifiedLogData}, + traits::FileProvider, + unified_log::UnifiedLogData, }; use regex::Regex; -fn collect_logs(path: &str) -> Vec { - let paths = fs::read_dir(path).unwrap(); - - let mut log_data_vec: Vec = Vec::new(); - for path in paths { - let data = path.unwrap(); - let full_path = data.path().display().to_string(); - let log_data = parse_log(&full_path).unwrap(); - log_data_vec.push(log_data); - } - - return log_data_vec; +fn collect_logs(provider: &dyn FileProvider) -> Vec { + provider + .tracev3_files() + .map(|mut file| parse_log(file.reader()).unwrap()) + .collect() } #[test] @@ -33,7 +27,8 @@ fn test_parse_log_high_sierra() { test_path.push("tests/test_data/system_logs_high_sierra.logarchive"); test_path.push("Persist/0000000000000001.tracev3"); - let log_data = parse_log(&test_path.display().to_string()).unwrap(); + let handle = File::open(test_path).unwrap(); + let log_data = parse_log(handle).unwrap(); assert_eq!(log_data.catalog_data[0].firehose.len(), 172); assert_eq!(log_data.catalog_data[0].simpledump.len(), 0); @@ -52,19 +47,16 @@ fn test_parse_log_high_sierra() { fn test_build_log_high_sierra() { let mut test_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); test_path.push("tests/test_data/system_logs_high_sierra.logarchive"); - let string_results = collect_strings(&test_path.display().to_string()).unwrap(); - - test_path.push("dsc"); - let shared_strings_results = collect_shared_strings(&test_path.display().to_string()).unwrap(); - test_path.pop(); - test_path.push("timesync"); - let timesync_data = collect_timesync(&test_path.display().to_string()).unwrap(); - test_path.pop(); + let provider = LogarchiveProvider::new(test_path.as_path()); + let string_results = collect_strings(&provider).unwrap(); + let shared_strings_results = collect_shared_strings(&provider).unwrap(); + let timesync_data = collect_timesync(&provider).unwrap(); test_path.push("Persist/0000000000000001.tracev3"); - let log_data = parse_log(&test_path.display().to_string()).unwrap(); + let handle = File::open(test_path.as_path()).unwrap(); + let log_data = parse_log(handle).unwrap(); let exclude_missing = false; let (results, _) = build_log( @@ -103,19 +95,16 @@ fn test_build_log_high_sierra() { fn test_build_log_complex_format_high_sierra() { let mut test_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); test_path.push("tests/test_data/system_logs_high_sierra.logarchive"); - let string_results = collect_strings(&test_path.display().to_string()).unwrap(); - test_path.push("dsc"); - let shared_strings_results = collect_shared_strings(&test_path.display().to_string()).unwrap(); - test_path.pop(); - - test_path.push("timesync"); - let timesync_data = collect_timesync(&test_path.display().to_string()).unwrap(); - test_path.pop(); + let provider = LogarchiveProvider::new(test_path.as_path()); + let string_results = collect_strings(&provider).unwrap(); + let shared_strings_results = collect_shared_strings(&provider).unwrap(); + let timesync_data = collect_timesync(&provider).unwrap(); test_path.push("Persist/0000000000000001.tracev3"); - let log_data = parse_log(&test_path.display().to_string()).unwrap(); + let handle = File::open(test_path.as_path()).unwrap(); + let log_data = parse_log(handle).unwrap(); let exclude_missing = false; let (results, _) = build_log( @@ -159,19 +148,16 @@ fn test_build_log_complex_format_high_sierra() { fn test_build_log_negative_number_high_sierra() { let mut test_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); test_path.push("tests/test_data/system_logs_high_sierra.logarchive"); - let string_results = collect_strings(&test_path.display().to_string()).unwrap(); - - test_path.push("dsc"); - let shared_strings_results = collect_shared_strings(&test_path.display().to_string()).unwrap(); - test_path.pop(); - test_path.push("timesync"); - let timesync_data = collect_timesync(&test_path.display().to_string()).unwrap(); - test_path.pop(); + let provider = LogarchiveProvider::new(test_path.as_path()); + let string_results = collect_strings(&provider).unwrap(); + let shared_strings_results = collect_shared_strings(&provider).unwrap(); + let timesync_data = collect_timesync(&provider).unwrap(); test_path.push("Special/0000000000000003.tracev3"); + let handle = File::open(test_path.as_path()).unwrap(); - let log_data = parse_log(&test_path.display().to_string()).unwrap(); + let log_data = parse_log(handle).unwrap(); let exclude_missing = false; let (results, _) = build_log( @@ -201,37 +187,13 @@ fn test_build_log_negative_number_high_sierra() { fn test_parse_all_logs_high_sierra() { let mut test_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); test_path.push("tests/test_data/system_logs_high_sierra.logarchive"); - let string_results = collect_strings(&test_path.display().to_string()).unwrap(); - - test_path.push("dsc"); - let shared_strings_results = collect_shared_strings(&test_path.display().to_string()).unwrap(); - test_path.pop(); - - test_path.push("timesync"); - let timesync_data = collect_timesync(&test_path.display().to_string()).unwrap(); - test_path.pop(); - - test_path.push("Persist"); - - let mut log_data = collect_logs(&test_path.display().to_string()); - test_path.pop(); - - test_path.push("HighVolume"); - let mut results = collect_logs(&test_path.display().to_string()); - log_data.append(&mut results); - test_path.pop(); - - test_path.push("Special"); - let mut results = collect_logs(&test_path.display().to_string()); - log_data.append(&mut results); - test_path.pop(); - - test_path.push("logdata.LiveData.tracev3"); - let results = parse_log(&test_path.display().to_string()).unwrap(); - log_data.push(results); - test_path.pop(); + let provider = LogarchiveProvider::new(test_path.as_path()); + let string_results = collect_strings(&provider).unwrap(); + let shared_strings_results = collect_shared_strings(&provider).unwrap(); + let timesync_data = collect_timesync(&provider).unwrap(); + let log_data = collect_logs(&provider); + let mut log_data_vec = Vec::new(); - let mut log_data_vec: Vec = Vec::new(); let exclude_missing = false; for logs in &log_data { let (mut data, _) = build_log( diff --git a/tests/monterey_tests.rs b/tests/monterey_tests.rs index 9153067..8cba8a6 100755 --- a/tests/monterey_tests.rs +++ b/tests/monterey_tests.rs @@ -5,26 +5,21 @@ // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and limitations under the License. -use std::{fs, path::PathBuf}; +use std::{fs::File, path::PathBuf}; use macos_unifiedlogs::{ + filesystem::LogarchiveProvider, parser::{build_log, collect_shared_strings, collect_strings, collect_timesync, parse_log}, + traits::FileProvider, unified_log::{LogData, UnifiedLogData}, }; use regex::Regex; -fn collect_logs(path: &str) -> Vec { - let paths = fs::read_dir(path).unwrap(); - - let mut log_data_vec: Vec = Vec::new(); - for path in paths { - let data = path.unwrap(); - let full_path = data.path().display().to_string(); - let log_data = parse_log(&full_path).unwrap(); - log_data_vec.push(log_data); - } - - return log_data_vec; +fn collect_logs(provider: &dyn FileProvider) -> Vec { + provider + .tracev3_files() + .map(|mut file| parse_log(file.reader()).unwrap()) + .collect() } #[test] @@ -33,7 +28,9 @@ fn test_parse_log_monterey() { test_path.push("tests/test_data/system_logs_monterey.logarchive"); test_path.push("Persist/000000000000000a.tracev3"); - let log_data = parse_log(&test_path.display().to_string()).unwrap(); + let handle = File::open(test_path.as_path()).unwrap(); + + let log_data = parse_log(handle).unwrap(); assert_eq!(log_data.catalog_data[0].firehose.len(), 17); assert_eq!(log_data.catalog_data[0].simpledump.len(), 383); @@ -52,19 +49,17 @@ fn test_parse_log_monterey() { fn test_build_log_monterey() { let mut test_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); test_path.push("tests/test_data/system_logs_monterey.logarchive"); - let string_results = collect_strings(&test_path.display().to_string()).unwrap(); - - test_path.push("dsc"); - let shared_strings_results = collect_shared_strings(&test_path.display().to_string()).unwrap(); - test_path.pop(); - test_path.push("timesync"); - let timesync_data = collect_timesync(&test_path.display().to_string()).unwrap(); - test_path.pop(); + let provider = LogarchiveProvider::new(test_path.as_path()); + let string_results = collect_strings(&provider).unwrap(); + let shared_strings_results = collect_shared_strings(&provider).unwrap(); + let timesync_data = collect_timesync(&provider).unwrap(); test_path.push("Persist/000000000000000a.tracev3"); - let log_data = parse_log(&test_path.display().to_string()).unwrap(); + let handle = File::open(test_path.as_path()).unwrap(); + + let log_data = parse_log(handle).unwrap(); let exclude_missing = false; let (results, _) = build_log( @@ -104,35 +99,12 @@ fn test_build_log_monterey() { fn test_parse_all_logs_monterey() { let mut test_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); test_path.push("tests/test_data/system_logs_monterey.logarchive"); - let string_results = collect_strings(&test_path.display().to_string()).unwrap(); - - test_path.push("dsc"); - let shared_strings_results = collect_shared_strings(&test_path.display().to_string()).unwrap(); - test_path.pop(); - - test_path.push("timesync"); - let timesync_data = collect_timesync(&test_path.display().to_string()).unwrap(); - test_path.pop(); - - test_path.push("Persist"); - - let mut log_data = collect_logs(&test_path.display().to_string()); - test_path.pop(); - - test_path.push("HighVolume"); - let mut results = collect_logs(&test_path.display().to_string()); - log_data.append(&mut results); - test_path.pop(); - - test_path.push("Special"); - let mut results = collect_logs(&test_path.display().to_string()); - log_data.append(&mut results); - test_path.pop(); - test_path.push("logdata.LiveData.tracev3"); - let results = parse_log(&test_path.display().to_string()).unwrap(); - log_data.push(results); - test_path.pop(); + let provider = LogarchiveProvider::new(test_path.as_path()); + let string_results = collect_strings(&provider).unwrap(); + let shared_strings_results = collect_shared_strings(&provider).unwrap(); + let timesync_data = collect_timesync(&provider).unwrap(); + let log_data = collect_logs(&provider); let mut log_data_vec: Vec = Vec::new(); let exclude_missing = false;