diff options
Diffstat (limited to 'rust/vendor/snmp-parser/tests')
-rw-r--r-- | rust/vendor/snmp-parser/tests/v1.rs | 95 | ||||
-rw-r--r-- | rust/vendor/snmp-parser/tests/v2c.rs | 53 | ||||
-rw-r--r-- | rust/vendor/snmp-parser/tests/v3.rs | 86 |
3 files changed, 234 insertions, 0 deletions
diff --git a/rust/vendor/snmp-parser/tests/v1.rs b/rust/vendor/snmp-parser/tests/v1.rs new file mode 100644 index 0000000..fac9c60 --- /dev/null +++ b/rust/vendor/snmp-parser/tests/v1.rs @@ -0,0 +1,95 @@ +#[macro_use] +extern crate hex_literal; +extern crate nom; +extern crate snmp_parser; + +use asn1_rs::Oid; +use snmp_parser::*; +use std::net::Ipv4Addr; + +const SNMPV1_RESPONSE: &[u8] = &hex!( + " +30 82 00 59 02 01 00 04 06 70 75 62 6c 69 63 a2 +82 00 4a 02 02 26 72 02 01 00 02 01 00 30 82 00 +3c 30 82 00 10 06 0b 2b 06 01 02 01 19 03 02 01 +05 01 02 01 03 30 82 00 10 06 0b 2b 06 01 02 01 +19 03 05 01 01 01 02 01 03 30 82 00 10 06 0b 2b +06 01 02 01 19 03 05 01 02 01 04 01 a0 +" +); + +#[test] +fn test_snmp_v1_response() { + let bytes: &[u8] = SNMPV1_RESPONSE; + match parse_snmp_v1(bytes) { + Ok((rem, pdu)) => { + // println!("pdu: {:?}", pdu); + assert!(rem.is_empty()); + assert_eq!(pdu.version, 0); + assert_eq!(&pdu.community as &str, "public"); + assert_eq!(pdu.pdu_type(), PduType::Response); + } + e => panic!("Error: {:?}", e), + } +} + +static SNMPV1_REQ: &[u8] = include_bytes!("../assets/snmpv1_req.bin"); + +#[test] +fn test_snmp_v1_req() { + let bytes = SNMPV1_REQ; + let expected = SnmpMessage { + version: 0, + community: String::from("public"), + pdu: SnmpPdu::Generic(SnmpGenericPdu { + pdu_type: PduType::GetRequest, + req_id: 38, + err: ErrorStatus(0), + err_index: 0, + var: vec![SnmpVariable { + oid: Oid::from(&[1, 3, 6, 1, 2, 1, 1, 2, 0]).unwrap(), + val: VarBindValue::Unspecified, + }], + }), + }; + let (rem, r) = parse_snmp_v1(bytes).expect("parsing failed"); + // debug!("r: {:?}",r); + eprintln!( + "SNMP: v={}, c={:?}, pdu_type={:?}", + r.version, + r.community, + r.pdu_type() + ); + // debug!("PDU: type={}, {:?}", pdu_type, pdu_res); + for v in r.vars_iter() { + eprintln!("v: {:?}", v); + } + assert!(rem.is_empty()); + assert_eq!(r, expected); +} + +static SNMPV1_TRAP_COLDSTART: &[u8] = include_bytes!("../assets/snmpv1_trap_coldstart.bin"); + +#[test] +fn test_snmp_v1_trap_coldstart() { + let bytes = SNMPV1_TRAP_COLDSTART; + let (rem, pdu) = parse_snmp_v1(bytes).expect("parsing failed"); + // println!("pdu: {:?}", pdu); + assert!(rem.is_empty()); + assert_eq!(pdu.version, 0); + assert_eq!(&pdu.community as &str, "public"); + assert_eq!(pdu.pdu_type(), PduType::TrapV1); + match pdu.pdu { + SnmpPdu::TrapV1(trap) => { + assert_eq!( + trap.enterprise, + Oid::from(&[1, 3, 6, 1, 4, 1, 4, 1, 2, 21]).unwrap() + ); + assert_eq!( + trap.agent_addr, + NetworkAddress::IPv4(Ipv4Addr::new(127, 0, 0, 1)) + ); + } + _ => panic!("unexpected pdu type"), + } +} diff --git a/rust/vendor/snmp-parser/tests/v2c.rs b/rust/vendor/snmp-parser/tests/v2c.rs new file mode 100644 index 0000000..e15c6e2 --- /dev/null +++ b/rust/vendor/snmp-parser/tests/v2c.rs @@ -0,0 +1,53 @@ +#[macro_use] +extern crate pretty_assertions; +extern crate nom; +extern crate snmp_parser; + +use asn1_rs::Oid; +use snmp_parser::*; + +static SNMPV2_GET: &[u8] = include_bytes!("../assets/snmpv2c-get-response.bin"); + +#[test] +fn test_snmp_v2_get() { + let bytes = SNMPV2_GET; + let expected = SnmpMessage { + version: 1, + community: String::from("public"), + pdu: SnmpPdu::Generic(SnmpGenericPdu { + pdu_type: PduType::Response, + req_id: 97083662, + err: ErrorStatus(0), + err_index: 0, + var: vec![ + SnmpVariable { + oid: Oid::from(&[1, 3, 6, 1, 2, 1, 25, 1, 1, 0]).unwrap(), + val: VarBindValue::Value(ObjectSyntax::TimeTicks(970069)), + }, + SnmpVariable { + oid: Oid::from(&[1, 3, 6, 1, 2, 1, 25, 1, 5, 0]).unwrap(), + val: VarBindValue::Value(ObjectSyntax::Gauge32(3)), + }, + SnmpVariable { + oid: Oid::from(&[1, 3, 6, 1, 2, 1, 25, 1, 5, 1]).unwrap(), + val: VarBindValue::NoSuchInstance, + }, + ], + }), + }; + let (rem, r) = parse_snmp_v2c(bytes).expect("parsing failed"); + + // debug!("r: {:?}",r); + eprintln!( + "SNMP: v={}, c={:?}, pdu_type={:?}", + r.version, + r.community, + r.pdu_type() + ); + // debug!("PDU: type={}, {:?}", pdu_type, pdu_res); + for v in r.vars_iter() { + eprintln!("v: {:?}", v); + } + assert!(rem.is_empty()); + assert_eq!(r, expected); +} diff --git a/rust/vendor/snmp-parser/tests/v3.rs b/rust/vendor/snmp-parser/tests/v3.rs new file mode 100644 index 0000000..bad61d7 --- /dev/null +++ b/rust/vendor/snmp-parser/tests/v3.rs @@ -0,0 +1,86 @@ +#[macro_use] +extern crate pretty_assertions; + +extern crate nom; +extern crate snmp_parser; + +use snmp_parser::*; + +static SNMPV3_REQ: &[u8] = include_bytes!("../assets/snmpv3_req.bin"); + +#[test] +fn test_snmp_v3_req() { + let bytes = SNMPV3_REQ; + let sp = SecurityParameters::USM(UsmSecurityParameters { + msg_authoritative_engine_id: b"", + msg_authoritative_engine_boots: 0, + msg_authoritative_engine_time: 0, + msg_user_name: String::from(""), + msg_authentication_parameters: b"", + msg_privacy_parameters: b"", + }); + let cei = [ + 0x80, 0x00, 0x1f, 0x88, 0x80, 0x59, 0xdc, 0x48, 0x61, 0x45, 0xa2, 0x63, 0x22, + ]; + let data = SnmpPdu::Generic(SnmpGenericPdu { + pdu_type: PduType::GetRequest, + req_id: 2098071598, + err: ErrorStatus::NoError, + err_index: 0, + var: vec![], + }); + let expected = SnmpV3Message { + version: 3, + header_data: HeaderData { + msg_id: 821490644, + msg_max_size: 65507, + msg_flags: 4, + msg_security_model: SecurityModel::USM, + }, + security_params: sp, + data: ScopedPduData::Plaintext(ScopedPdu { + ctx_engine_id: &cei, + ctx_engine_name: b"", + data, + }), + }; + let (rem, res) = parse_snmp_v3(bytes).expect("parsing failed"); + // eprintln!("{:?}", res); + assert!(rem.is_empty()); + assert_eq!(res, expected); +} + +#[test] +fn test_snmp_v3_req_encrypted() { + let bytes = include_bytes!("../assets/snmpv3_req_encrypted.bin"); + let (rem, msg) = parse_snmp_v3(bytes).expect("parsing failed"); + // eprintln!("{:?}", res); + assert!(rem.is_empty()); + assert_eq!(msg.version, 3); + assert_eq!(msg.header_data.msg_security_model, SecurityModel::USM); +} + +#[test] +fn test_snmp_v3_report() { + let bytes = include_bytes!("../assets/snmpv3-report.bin"); + let (rem, msg) = parse_snmp_v3(bytes).expect("parsing failed"); + // eprintln!("{:?}", res); + assert!(rem.is_empty()); + assert_eq!(msg.version, 3); + assert_eq!(msg.header_data.msg_security_model, SecurityModel::USM); +} + +#[test] +fn test_snmp_v3_generic() { + let bytes = SNMPV3_REQ; + let res = parse_snmp_generic_message(bytes); + // eprintln!("{:?}", res); + let (rem, m) = res.expect("parse_snmp_generic_message"); + assert!(rem.is_empty()); + if let SnmpGenericMessage::V3(msg) = m { + assert_eq!(msg.version, 3); + assert_eq!(msg.header_data.msg_security_model, SecurityModel::USM); + } else { + panic!("unexpected PDU type"); + } +} |