// Copyright © 2024 RustLogs (RLG). All rights reserved. // SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: MIT //! Tests for the log format functionality of RustLogs (RLG). #[cfg(test)] mod tests { use rlg::log_format::LogFormat; #[test] fn test_log_format_display() { assert_eq!(format!("{}", LogFormat::CLF), "CLF"); assert_eq!(format!("{}", LogFormat::JSON), "JSON"); assert_eq!(format!("{}", LogFormat::CEF), "CEF"); assert_eq!(format!("{}", LogFormat::ELF), "ELF"); assert_eq!(format!("{}", LogFormat::W3C), "W3C"); assert_eq!(format!("{}", LogFormat::GELF), "GELF"); assert_eq!( format!("{}", LogFormat::ApacheAccessLog), "Apache Access Log" ); assert_eq!(format!("{}", LogFormat::Logstash), "Logstash"); assert_eq!(format!("{}", LogFormat::Log4jXML), "Log4j XML"); assert_eq!(format!("{}", LogFormat::NDJSON), "NDJSON"); } #[test] fn test_log_format_from_str() { assert_eq!("CLF".parse::().unwrap(), LogFormat::CLF); assert_eq!( "JSON".parse::().unwrap(), LogFormat::JSON ); assert_eq!("CEF".parse::().unwrap(), LogFormat::CEF); assert_eq!("ELF".parse::().unwrap(), LogFormat::ELF); assert_eq!("W3C".parse::().unwrap(), LogFormat::W3C); assert_eq!( "GELF".parse::().unwrap(), LogFormat::GELF ); assert_eq!( "ApacheAccessLog".parse::().unwrap(), LogFormat::ApacheAccessLog ); assert_eq!( "Logstash".parse::().unwrap(), LogFormat::Logstash ); assert_eq!( "Log4jXML".parse::().unwrap(), LogFormat::Log4jXML ); assert_eq!( "NDJSON".parse::().unwrap(), LogFormat::NDJSON ); assert!("Invalid".parse::().is_err()); } #[test] fn test_log_format_validate() { assert!(LogFormat::CLF.validate( "127.0.0.1 - - [10/Oct/2000:13:55:36 -0700] \"GET /apache_pb.gif HTTP/1.0\" 200 2326" )); assert!(LogFormat::JSON.validate("{\"key\":\"value\"}")); assert!(LogFormat::CEF.validate("CEF:0|security|threat|1.0|100|Something happened|5|msg=hello")); assert!(LogFormat::W3C.validate("#Fields: date time c-ip cs-method cs-uri-stem sc-status\n2024-01-01 12:34:56 192.168.0.1 GET /index.html 200")); assert!(LogFormat::GELF.validate("{\"version\":\"1.1\",\"host\":\"localhost\",\"short_message\":\"A short message\"}")); assert!(LogFormat::Log4jXML.validate("")); // Invalid cases assert!(!LogFormat::CLF.validate("Invalid CLF log")); assert!(!LogFormat::JSON.validate("Invalid JSON")); assert!(!LogFormat::CEF.validate("Invalid CEF log")); assert!(!LogFormat::W3C.validate("Invalid W3C log")); assert!(!LogFormat::GELF.validate("Invalid GELF log")); assert!(!LogFormat::Log4jXML.validate("XML")); } #[test] fn test_log_format_format_log() { // Valid formatting assert_eq!( LogFormat::CLF .format_log( "127.0.0.1 - - [10/Oct/2000:13:55:36 -0700] \"GET /apache_pb.gif HTTP/1.0\" 200 2326" ) .unwrap(), "127.0.0.1 - - [10/Oct/2000:13:55:36 -0700] \"GET /apache_pb.gif HTTP/1.0\" 200 2326" ); assert_eq!( LogFormat::JSON.format_log("{\"key\":\"value\"}").unwrap(), "{\n \"key\": \"value\"\n}" ); assert_eq!( LogFormat::CEF .format_log("CEF:0|security|threat|1.0|100|Something happened|5|msg=hello") .unwrap(), "CEF:0|security|threat|1.0|100|Something happened|5|msg=hello" ); assert_eq!( LogFormat::Log4jXML .format_log("") .unwrap(), "" ); // Invalid JSON let invalid_json = "Invalid JSON"; assert!(LogFormat::JSON.format_log(invalid_json).is_err()); } // Additional tests for edge cases and specific format validations #[test] fn test_log_format_validate_edge_cases() { // Empty string assert!(!LogFormat::CLF.validate("")); assert!(!LogFormat::JSON.validate("")); // Very long string let long_string = "a".repeat(10000); assert!(!LogFormat::CLF.validate(&long_string)); assert!(LogFormat::JSON .validate(&format!("{{\"key\":\"{}\"}}", long_string))); // Special characters assert!(LogFormat::JSON .validate("{\"key\":\"value with spaces and 特殊字符\"}")); } #[test] fn test_log_format_case_insensitivity() { assert_eq!("clf".parse::().unwrap(), LogFormat::CLF); assert_eq!( "JSON".parse::().unwrap(), LogFormat::JSON ); assert_eq!("Cef".parse::().unwrap(), LogFormat::CEF); } #[test] fn test_log_format_error_messages() { let result = "InvalidFormat".parse::(); assert!(result.is_err()); assert_eq!( result.unwrap_err().to_string(), "Log format parse error: Unknown log format: InvalidFormat" ); } #[test] fn test_log_format_specific_validations() { // Test specific format validations assert!(LogFormat::ApacheAccessLog.validate("192.168.0.1 - - [01/Jan/2024:12:00:00 +0000] \"GET / HTTP/1.1\" 200 1234")); assert!(LogFormat::Logstash.validate("{\"@timestamp\":\"2024-01-01T12:00:00Z\",\"message\":\"Test log\",\"level\":\"INFO\"}")); // For NDJSON, we might need to adjust this based on how it's actually implemented // Option 1: If NDJSON validates each line separately assert!(LogFormat::NDJSON.validate("{\"key1\":\"value1\"}")); assert!(LogFormat::NDJSON.validate("{\"key2\":\"value2\"}")); // Option 2: If NDJSON validates the entire string as one // If this is the case, we might need to adjust the validation method // assert!(LogFormat::NDJSON.validate("{\"key1\":\"value1\"}\n{\"key2\":\"value2\"}")); // Option 3: If NDJSON validation is not yet implemented // In this case, we might want to skip this test or expect it to fail // #[should_panic(expected = "NDJSON validation not implemented")] // assert!(LogFormat::NDJSON.validate("{\"key1\":\"value1\"}\n{\"key2\":\"value2\"}")); } }