Skip to content

Commit 28b30f0

Browse files
fix(RLG): 🐛 Add more complex test cases for the validate method, including edge cases and malformed logs
1 parent fe21b88 commit 28b30f0

File tree

1 file changed

+62
-1
lines changed

1 file changed

+62
-1
lines changed

tests/test_log_format.rs

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// SPDX-License-Identifier: MIT
44

5-
#[cfg(test)]
5+
//! Tests for the log format functionality of RustLogs (RLG).
66
7+
#[cfg(test)]
78
mod tests {
89
use rlg::log_format::LogFormat;
910

@@ -112,4 +113,64 @@ mod tests {
112113
let invalid_json = "Invalid JSON";
113114
assert!(LogFormat::JSON.format_log(invalid_json).is_err());
114115
}
116+
117+
// Additional tests for edge cases and specific format validations
118+
119+
#[test]
120+
fn test_log_format_validate_edge_cases() {
121+
// Empty string
122+
assert!(!LogFormat::CLF.validate(""));
123+
assert!(!LogFormat::JSON.validate(""));
124+
125+
// Very long string
126+
let long_string = "a".repeat(10000);
127+
assert!(!LogFormat::CLF.validate(&long_string));
128+
assert!(LogFormat::JSON
129+
.validate(&format!("{{\"key\":\"{}\"}}", long_string)));
130+
131+
// Special characters
132+
assert!(LogFormat::JSON
133+
.validate("{\"key\":\"value with spaces and 特殊字符\"}"));
134+
}
135+
136+
#[test]
137+
fn test_log_format_case_insensitivity() {
138+
assert_eq!("clf".parse::<LogFormat>().unwrap(), LogFormat::CLF);
139+
assert_eq!(
140+
"JSON".parse::<LogFormat>().unwrap(),
141+
LogFormat::JSON
142+
);
143+
assert_eq!("Cef".parse::<LogFormat>().unwrap(), LogFormat::CEF);
144+
}
145+
146+
#[test]
147+
fn test_log_format_error_messages() {
148+
let result = "InvalidFormat".parse::<LogFormat>();
149+
assert!(result.is_err());
150+
assert_eq!(
151+
result.unwrap_err().to_string(),
152+
"Log format parse error: Unknown log format: InvalidFormat"
153+
);
154+
}
155+
156+
#[test]
157+
fn test_log_format_specific_validations() {
158+
// Test specific format validations
159+
assert!(LogFormat::ApacheAccessLog.validate("192.168.0.1 - - [01/Jan/2024:12:00:00 +0000] \"GET / HTTP/1.1\" 200 1234"));
160+
assert!(LogFormat::Logstash.validate("{\"@timestamp\":\"2024-01-01T12:00:00Z\",\"message\":\"Test log\",\"level\":\"INFO\"}"));
161+
162+
// For NDJSON, we might need to adjust this based on how it's actually implemented
163+
// Option 1: If NDJSON validates each line separately
164+
assert!(LogFormat::NDJSON.validate("{\"key1\":\"value1\"}"));
165+
assert!(LogFormat::NDJSON.validate("{\"key2\":\"value2\"}"));
166+
167+
// Option 2: If NDJSON validates the entire string as one
168+
// If this is the case, we might need to adjust the validation method
169+
// assert!(LogFormat::NDJSON.validate("{\"key1\":\"value1\"}\n{\"key2\":\"value2\"}"));
170+
171+
// Option 3: If NDJSON validation is not yet implemented
172+
// In this case, we might want to skip this test or expect it to fail
173+
// #[should_panic(expected = "NDJSON validation not implemented")]
174+
// assert!(LogFormat::NDJSON.validate("{\"key1\":\"value1\"}\n{\"key2\":\"value2\"}"));
175+
}
115176
}

0 commit comments

Comments
 (0)