use netflow_parser::NetflowParser;
use netflow_parser::variable_versions::ttl::TtlConfig;
use std::time::Duration;
#[test]
fn test_default_parser_creation() {
let parser = NetflowParser::default();
assert!(parser.allowed_versions()[5]);
assert!(parser.allowed_versions()[7]);
assert!(parser.allowed_versions()[9]);
assert!(parser.allowed_versions()[10]);
assert!(!parser.allowed_versions()[0]);
}
#[test]
fn test_parser_builder_with_cache_size() {
let parser = NetflowParser::builder()
.with_cache_size(2000)
.build()
.expect("Failed to build parser");
let stats = parser.v9_cache_info();
assert_eq!(stats.max_size_per_cache, 2000);
let ipfix_info = parser.ipfix_cache_info();
assert_eq!(ipfix_info.max_size_per_cache, 2000);
}
#[test]
fn test_parser_builder_with_different_cache_sizes() {
let parser = NetflowParser::builder()
.with_v9_cache_size(1000)
.with_ipfix_cache_size(3000)
.build()
.expect("Failed to build parser");
let v9_info = parser.v9_cache_info();
assert_eq!(v9_info.max_size_per_cache, 1000);
let ipfix_info = parser.ipfix_cache_info();
assert_eq!(ipfix_info.max_size_per_cache, 3000);
}
#[test]
fn test_parser_builder_with_ttl() {
let parser = NetflowParser::builder()
.with_ttl(TtlConfig::new(Duration::from_secs(3600)))
.build()
.expect("Failed to build parser");
assert!(parser.allowed_versions()[5]);
assert!(parser.allowed_versions()[9]);
}
#[test]
fn test_parser_builder_with_allowed_versions() {
let parser = NetflowParser::builder()
.with_allowed_versions(&[5, 9])
.build()
.expect("Failed to build parser");
assert!(parser.allowed_versions()[5]);
assert!(parser.allowed_versions()[9]);
assert!(!parser.allowed_versions()[7]);
}
#[test]
fn test_parser_builder_with_max_error_sample_size() {
let parser = NetflowParser::builder()
.with_max_error_sample_size(512)
.build()
.expect("Failed to build parser");
assert_eq!(parser.max_error_sample_size(), 512);
}
#[test]
fn test_parser_builder_with_field_count_limits() {
let mut parser = NetflowParser::builder()
.with_max_field_count(2)
.build()
.expect("Failed to build parser");
#[rustfmt::skip]
let v9_3field_template: Vec<u8> = vec![
0x00, 0x09, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x14, 0x01, 0x00, 0x00, 0x03, 0x00, 0x08, 0x00, 0x04, 0x00, 0x0c, 0x00, 0x04, 0x00, 0x07, 0x00, 0x02, ];
let _result = parser.parse_bytes(&v9_3field_template);
assert!(
!parser.has_v9_template(256),
"Template with 3 fields should be rejected when max_field_count=2"
);
#[rustfmt::skip]
let v9_2field_template: Vec<u8> = vec![
0x00, 0x09, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x10, 0x01, 0x01, 0x00, 0x02, 0x00, 0x08, 0x00, 0x04, 0x00, 0x0c, 0x00, 0x04, ];
let result = parser.parse_bytes(&v9_2field_template);
assert!(
parser.has_v9_template(257),
"Template with 2 fields should be accepted when max_field_count=2"
);
drop(result);
}
#[test]
fn test_parser_builder_comprehensive() {
let mut parser = NetflowParser::builder()
.with_v9_cache_size(1500)
.with_ipfix_cache_size(2500)
.with_v9_ttl(TtlConfig::new(Duration::from_secs(1800)))
.with_ipfix_ttl(TtlConfig::new(Duration::from_secs(3600)))
.with_allowed_versions(&[5, 9, 10])
.with_max_error_sample_size(512)
.with_v9_max_field_count(2)
.with_ipfix_max_field_count(10000)
.build()
.expect("Failed to build parser");
let v9_info = parser.v9_cache_info();
assert_eq!(v9_info.max_size_per_cache, 1500);
let ipfix_info = parser.ipfix_cache_info();
assert_eq!(ipfix_info.max_size_per_cache, 2500);
assert!(parser.allowed_versions()[5]);
assert!(parser.allowed_versions()[9]);
assert!(parser.allowed_versions()[10]);
assert_eq!(parser.max_error_sample_size(), 512);
#[rustfmt::skip]
let v9_3field_template: Vec<u8> = vec![
0x00, 0x09, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x14, 0x01, 0x00, 0x00, 0x03, 0x00, 0x08, 0x00, 0x04,
0x00, 0x0c, 0x00, 0x04,
0x00, 0x07, 0x00, 0x02,
];
let result = parser.parse_bytes(&v9_3field_template);
assert!(
!parser.has_v9_template(256),
"V9 field count limit should reject 3-field template when max=2"
);
drop(result);
}