use std::net::IpAddr;
pub trait KeyFields {
fn src_ip(&self) -> Option<IpAddr> {
None
}
fn src_port(&self) -> Option<u16> {
None
}
fn dest_ip(&self) -> Option<IpAddr> {
None
}
fn dest_port(&self) -> Option<u16> {
None
}
fn proto_str(&self) -> Option<&'static str> {
None
}
fn app_proto_str(&self) -> Option<&'static str> {
None
}
}
pub trait AnomalyFields {
fn anomaly_type(&self) -> Option<&'static str> {
None
}
fn anomaly_event(&self) -> Option<&'static str> {
None
}
}
#[cfg(test)]
mod tests {
use super::*;
struct CustomKey {
src: IpAddr,
dst: IpAddr,
}
impl KeyFields for CustomKey {
fn src_ip(&self) -> Option<IpAddr> {
Some(self.src)
}
fn dest_ip(&self) -> Option<IpAddr> {
Some(self.dst)
}
}
#[test]
fn key_fields_default_impls_return_none() {
struct Empty;
impl KeyFields for Empty {}
let e = Empty;
assert!(e.src_ip().is_none());
assert!(e.src_port().is_none());
assert!(e.dest_ip().is_none());
assert!(e.dest_port().is_none());
assert!(e.proto_str().is_none());
assert!(e.app_proto_str().is_none());
}
#[test]
fn anomaly_fields_default_impls_return_none() {
struct Empty;
impl AnomalyFields for Empty {}
let e = Empty;
assert!(e.anomaly_type().is_none());
assert!(e.anomaly_event().is_none());
}
#[test]
fn custom_key_overrides_only_chosen_fields() {
let k = CustomKey {
src: "10.0.0.1".parse().unwrap(),
dst: "10.0.0.2".parse().unwrap(),
};
assert_eq!(k.src_ip(), Some("10.0.0.1".parse().unwrap()));
assert_eq!(k.dest_ip(), Some("10.0.0.2".parse().unwrap()));
assert!(k.src_port().is_none());
assert!(k.proto_str().is_none());
}
}