summaryrefslogtreecommitdiffstats
path: root/rust/vendor/snmp-parser/tests/v1.rs
blob: fac9c60fcd74b4843da79de4775fe15d2a8d3494 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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"),
    }
}