Skip to content

Commit

Permalink
Fix case of tagged empty variable in Get request and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
chifflier committed Dec 20, 2018
1 parent 86e6493 commit 6151480
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
Binary file added assets/snmpv2c-get-response.bin
Binary file not shown.
1 change: 1 addition & 0 deletions src/snmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ fn parse_objectsyntax<'a>(i:&'a[u8]) -> IResult<&'a[u8],ObjectSyntax> {
_ => Err(Err::Error(error_position!(i, ErrorKind::Custom(DER_TAG_ERROR)))),
}
} else {
if hdr.len == 0 { return Ok((rem, ObjectSyntax::Empty)); }
map_res!(
rem,
apply!(der_read_element_content_as, hdr.tag, hdr.len as usize),
Expand Down
54 changes: 54 additions & 0 deletions tests/v2c.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#[macro_use] extern crate pretty_assertions;
extern crate der_parser;
extern crate snmp_parser;
extern crate nom;

use nom::IResult;
use snmp_parser::*;
use der_parser::oid::Oid;

static SNMPV2_GET: &'static [u8] = include_bytes!("../assets/snmpv2c-get-response.bin");

#[test]
fn test_snmp_v2_get() {
let empty = &b""[..];
let bytes = SNMPV2_GET;
let expected = Ok((empty,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]),
val: ObjectSyntax::Ticks(970069)
},
SnmpVariable{
oid: Oid::from(&[1, 3, 6, 1, 2, 1, 25, 1, 5, 0]),
val: ObjectSyntax::Gauge(3)
},
SnmpVariable{
oid: Oid::from(&[1, 3, 6, 1, 2, 1, 25, 1, 5, 1]),
val: ObjectSyntax::Empty
}
],
}),
}));
let res = parse_snmp_v1(&bytes);
match &res {
&Ok((_,ref r)) => {
// debug!("r: {:?}",r);
eprintln!("SNMP: v={}, c={:?}, pdu_type={:?}",r.version,r.community,r.pdu_type());
// debug!("PDU: type={}, {:?}", pdu_type, pdu_res);
for ref v in r.vars_iter() {
eprintln!("v: {:?}",v);
}
},
_ => (),
};
assert_eq!(res, expected);
}

0 comments on commit 6151480

Please sign in to comment.