Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dvolodin7 committed Jan 10, 2024
1 parent 053aaac commit 96dc6fd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 143 deletions.
82 changes: 9 additions & 73 deletions src/snmp/msg/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ mod tests {
SnmpOid::from(vec![1, 3, 6, 1, 2, 1, 1, 2]),
];
let msg = SnmpV1Message::try_from(data.as_ref())?;
// Check version
assert_eq!(msg.version, SnmpVersion::V1);
// community == public
assert_eq!(msg.community, [0x70u8, 0x75, 0x62, 0x6c, 0x69, 0x63]);
// Analyze PDU
Expand All @@ -99,38 +97,10 @@ mod tests {
}

#[test]
fn test_parse_snmp_v2c_get() -> Result<(), SnmpError> {
let data = [
0x30u8, 0x35, 0x02, 0x01, 0x01, 0x04, 0x06, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0xa0,
0x28, 0x02, 0x04, 0x63, 0xcc, 0xac, 0x7d, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x30,
0x1a, 0x30, 0x0b, 0x06, 0x07, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x01, 0x03, 0x05, 0x00,
0x30, 0x0b, 0x06, 0x07, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x01, 0x02, 0x05, 0x00,
];
let vars: Vec<SnmpOid> = vec![
SnmpOid::from(vec![1, 3, 6, 1, 2, 1, 1, 3]),
SnmpOid::from(vec![1, 3, 6, 1, 2, 1, 1, 2]),
];
let msg = SnmpV1Message::try_from(data.as_ref())?;
// Check version
assert_eq!(msg.version, SnmpVersion::V2C);
// community == public
assert_eq!(msg.community, [0x70u8, 0x75, 0x62, 0x6c, 0x69, 0x63]);
// Analyze PDU
match msg.pdu {
SnmpPdu::GetRequest(pdu) => {
assert_eq!(pdu.request_id, 0x63ccac7d);
assert_eq!(pdu.vars, vars);
}
_ => return Err(SnmpError::InvalidPdu),
}
Ok(())
}

#[test]
fn test_parse_snmpv2_getresponse_exception() -> Result<(), SnmpError> {
fn test_parse_snmp_getresponse_exception() -> Result<(), SnmpError> {
let data = [
48u8, 40, // Seq 40 bytes
2, 1, 1, // INTEGER 1, v2C
2, 1, 0, // INTEGER 1, v1
4, 6, 112, 117, 98, 108, 105, 99, // "public"
162, 27, // 27 bytes
2, 4, 94, 189, 217, 172, // Request id, 0x5ebdd9ac
Expand All @@ -142,8 +112,6 @@ mod tests {
129, 0, // NoSuchObject
];
let msg = SnmpV1Message::try_from(data.as_ref())?;
// Check version
assert_eq!(msg.version, SnmpVersion::V2C);
// community == public
assert_eq!(msg.community, [0x70u8, 0x75, 0x62, 0x6c, 0x69, 0x63]);
//
Expand All @@ -162,10 +130,10 @@ mod tests {
Ok(())
}
#[test]
fn test_parse_snmpv2_getresponse() -> Result<(), SnmpError> {
fn test_parse_snmp_getresponse() -> Result<(), SnmpError> {
let data = [
48u8, 55, // Seq 55 bytes
2, 1, 1, // INTEGER, v2c
2, 1, 0, // INTEGER, v1
4, 6, 112, 117, 98, 108, 105, 99, // "public"
162, 42, // PDU, Get-Response, 42 bytes
2, 4, 40, 86, 116, 146, // Request id
Expand All @@ -177,8 +145,6 @@ mod tests {
4, 14, 71, 117, 102, 111, 32, 83, 78, 77, 80, 32, 84, 101, 115, 116, // String
];
let msg = SnmpV1Message::try_from(data.as_ref())?;
// Check version
assert_eq!(msg.version, SnmpVersion::V2C);
// community == public
assert_eq!(msg.community, [0x70u8, 0x75, 0x62, 0x6c, 0x69, 0x63]);
//
Expand All @@ -202,10 +168,10 @@ mod tests {
Ok(())
}
#[test]
fn test_parse_snmpv2_getresponse_many() -> Result<(), SnmpError> {
fn test_parse_snmp_getresponse_many() -> Result<(), SnmpError> {
let data = [
48u8, 129, 134, // Sequence, 134 bytes
2, 1, 1, // ITEGER, v2c
2, 1, 0, // ITEGER, v1
4, 6, 112, 117, 98, 108, 105, 99, // OCTET STRING, "public"
162, 121, // PDU, Get-Response, 121 byte
2, 4, 91, 63, 155, 39, // Request ID, 0x5B3F9B27
Expand All @@ -229,8 +195,6 @@ mod tests {
101, 46, 99, 111, 109, 62, // OCTET STRING
];
let msg = SnmpV1Message::try_from(data.as_ref())?;
// Check version
assert_eq!(msg.version, SnmpVersion::V2C);
// community == public
assert_eq!(msg.community, [0x70u8, 0x75, 0x62, 0x6c, 0x69, 0x63]);
//
Expand Down Expand Up @@ -272,10 +236,10 @@ mod tests {
Ok(())
}
#[test]
fn test_parse_snmpv2_getresponse_many_rel() -> Result<(), SnmpError> {
fn test_parse_snmp_getresponse_many_rel() -> Result<(), SnmpError> {
let data = [
48u8, 116, // Sequence, 116 bytes
2, 1, 1, // ITEGER, v2c
2, 1, 0, // ITEGER, v1
4, 6, 112, 117, 98, 108, 105, 99, // OCTET STRING, "public"
162, 103, // PDU, Get-Response, 103 byte
2, 4, 91, 63, 155, 39, // Request ID, 0x5B3F9B27
Expand All @@ -299,8 +263,6 @@ mod tests {
101, 46, 99, 111, 109, 62, // OCTET STRING
];
let msg = SnmpV1Message::try_from(data.as_ref())?;
// Check version
assert_eq!(msg.version, SnmpVersion::V2C);
// community == public
assert_eq!(msg.community, [0x70u8, 0x75, 0x62, 0x6c, 0x69, 0x63]);
//
Expand Down Expand Up @@ -342,7 +304,7 @@ mod tests {
Ok(())
}
#[test]
fn test_encode_snmp_v1_get() -> Result<(), SnmpError> {
fn test_encode_snmp_get() -> Result<(), SnmpError> {
let expected = [
0x30u8, 0x35, 0x02, 0x01, 0x00, 0x04, 0x06, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0xa0,
0x28, 0x02, 0x04, 0x63, 0xcc, 0xac, 0x7d, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x30,
Expand All @@ -351,32 +313,6 @@ mod tests {
];
let community = [0x70u8, 0x75, 0x62, 0x6c, 0x69, 0x63];
let msg = SnmpV1Message {
version: SnmpVersion::V1,
community: &community,
pdu: SnmpPdu::GetRequest(SnmpGet {
request_id: 0x63ccac7d,
vars: vec![
SnmpOid::from(vec![1, 3, 6, 1, 2, 1, 1, 3]),
SnmpOid::from(vec![1, 3, 6, 1, 2, 1, 1, 2]),
],
}),
};
let mut buf = Buffer::default();
msg.push_ber(&mut buf)?;
assert_eq!(buf.data(), &expected);
Ok(())
}
#[test]
fn test_encode_snmp_v2c_get() -> Result<(), SnmpError> {
let expected = [
0x30u8, 0x35, 0x02, 0x01, 0x01, 0x04, 0x06, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0xa0,
0x28, 0x02, 0x04, 0x63, 0xcc, 0xac, 0x7d, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x30,
0x1a, 0x30, 0x0b, 0x06, 0x07, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x01, 0x03, 0x05, 0x00,
0x30, 0x0b, 0x06, 0x07, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x01, 0x02, 0x05, 0x00,
];
let community = [0x70u8, 0x75, 0x62, 0x6c, 0x69, 0x63];
let msg = SnmpV1Message {
version: SnmpVersion::V2C,
community: &community,
pdu: SnmpPdu::GetRequest(SnmpGet {
request_id: 0x63ccac7d,
Expand Down
77 changes: 7 additions & 70 deletions src/snmp/msg/v2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,35 +71,7 @@ mod tests {
use crate::snmp::value::SnmpValue;

#[test]
fn test_parse_snmp_v1_get() -> Result<(), SnmpError> {
let data = [
0x30u8, 0x35, 0x02, 0x01, 0x00, 0x04, 0x06, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0xa0,
0x28, 0x02, 0x04, 0x63, 0xcc, 0xac, 0x7d, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x30,
0x1a, 0x30, 0x0b, 0x06, 0x07, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x01, 0x03, 0x05, 0x00,
0x30, 0x0b, 0x06, 0x07, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x01, 0x02, 0x05, 0x00,
];
let vars: Vec<SnmpOid> = vec![
SnmpOid::from(vec![1, 3, 6, 1, 2, 1, 1, 3]),
SnmpOid::from(vec![1, 3, 6, 1, 2, 1, 1, 2]),
];
let msg = SnmpV2cMessage::try_from(data.as_ref())?;
// Check version
assert_eq!(msg.version, SnmpVersion::V1);
// community == public
assert_eq!(msg.community, [0x70u8, 0x75, 0x62, 0x6c, 0x69, 0x63]);
// Analyze PDU
match msg.pdu {
SnmpPdu::GetRequest(pdu) => {
assert_eq!(pdu.request_id, 0x63ccac7d);
assert_eq!(pdu.vars, vars);
}
_ => return Err(SnmpError::InvalidPdu),
}
Ok(())
}

#[test]
fn test_parse_snmp_v2c_get() -> Result<(), SnmpError> {
fn test_parse_snmp_get() -> Result<(), SnmpError> {
let data = [
0x30u8, 0x35, 0x02, 0x01, 0x01, 0x04, 0x06, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0xa0,
0x28, 0x02, 0x04, 0x63, 0xcc, 0xac, 0x7d, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x30,
Expand All @@ -111,8 +83,6 @@ mod tests {
SnmpOid::from(vec![1, 3, 6, 1, 2, 1, 1, 2]),
];
let msg = SnmpV2cMessage::try_from(data.as_ref())?;
// Check version
assert_eq!(msg.version, SnmpVersion::V2C);
// community == public
assert_eq!(msg.community, [0x70u8, 0x75, 0x62, 0x6c, 0x69, 0x63]);
// Analyze PDU
Expand All @@ -127,7 +97,7 @@ mod tests {
}

#[test]
fn test_parse_snmpv2_getresponse_exception() -> Result<(), SnmpError> {
fn test_parse_snmp_getresponse_exception() -> Result<(), SnmpError> {
let data = [
48u8, 40, // Seq 40 bytes
2, 1, 1, // INTEGER 1, v2C
Expand All @@ -142,8 +112,6 @@ mod tests {
129, 0, // NoSuchObject
];
let msg = SnmpV2cMessage::try_from(data.as_ref())?;
// Check version
assert_eq!(msg.version, SnmpVersion::V2C);
// community == public
assert_eq!(msg.community, [0x70u8, 0x75, 0x62, 0x6c, 0x69, 0x63]);
//
Expand All @@ -162,7 +130,7 @@ mod tests {
Ok(())
}
#[test]
fn test_parse_snmpv2_getresponse() -> Result<(), SnmpError> {
fn test_parse_snmp_getresponse() -> Result<(), SnmpError> {
let data = [
48u8, 55, // Seq 55 bytes
2, 1, 1, // INTEGER, v2c
Expand All @@ -177,8 +145,6 @@ mod tests {
4, 14, 71, 117, 102, 111, 32, 83, 78, 77, 80, 32, 84, 101, 115, 116, // String
];
let msg = SnmpV2cMessage::try_from(data.as_ref())?;
// Check version
assert_eq!(msg.version, SnmpVersion::V2C);
// community == public
assert_eq!(msg.community, [0x70u8, 0x75, 0x62, 0x6c, 0x69, 0x63]);
//
Expand All @@ -202,7 +168,7 @@ mod tests {
Ok(())
}
#[test]
fn test_parse_snmpv2_getresponse_many() -> Result<(), SnmpError> {
fn test_parse_snmp_getresponse_many() -> Result<(), SnmpError> {
let data = [
48u8, 129, 134, // Sequence, 134 bytes
2, 1, 1, // ITEGER, v2c
Expand All @@ -229,8 +195,6 @@ mod tests {
101, 46, 99, 111, 109, 62, // OCTET STRING
];
let msg = SnmpV2cMessage::try_from(data.as_ref())?;
// Check version
assert_eq!(msg.version, SnmpVersion::V2C);
// community == public
assert_eq!(msg.community, [0x70u8, 0x75, 0x62, 0x6c, 0x69, 0x63]);
//
Expand Down Expand Up @@ -272,7 +236,7 @@ mod tests {
Ok(())
}
#[test]
fn test_parse_snmpv2_getresponse_many_rel() -> Result<(), SnmpError> {
fn test_parse_snmp_getresponse_many_rel() -> Result<(), SnmpError> {
let data = [
48u8, 116, // Sequence, 116 bytes
2, 1, 1, // ITEGER, v2c
Expand All @@ -299,8 +263,6 @@ mod tests {
101, 46, 99, 111, 109, 62, // OCTET STRING
];
let msg = SnmpV2cMessage::try_from(data.as_ref())?;
// Check version
assert_eq!(msg.version, SnmpVersion::V2C);
// community == public
assert_eq!(msg.community, [0x70u8, 0x75, 0x62, 0x6c, 0x69, 0x63]);
//
Expand Down Expand Up @@ -341,33 +303,9 @@ mod tests {
}
Ok(())
}

#[test]
fn test_encode_snmp_v1_get() -> Result<(), SnmpError> {
let expected = [
0x30u8, 0x35, 0x02, 0x01, 0x00, 0x04, 0x06, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0xa0,
0x28, 0x02, 0x04, 0x63, 0xcc, 0xac, 0x7d, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x30,
0x1a, 0x30, 0x0b, 0x06, 0x07, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x01, 0x03, 0x05, 0x00,
0x30, 0x0b, 0x06, 0x07, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x01, 0x02, 0x05, 0x00,
];
let community = [0x70u8, 0x75, 0x62, 0x6c, 0x69, 0x63];
let msg = SnmpV2cMessage {
version: SnmpVersion::V1,
community: &community,
pdu: SnmpPdu::GetRequest(SnmpGet {
request_id: 0x63ccac7d,
vars: vec![
SnmpOid::from(vec![1, 3, 6, 1, 2, 1, 1, 3]),
SnmpOid::from(vec![1, 3, 6, 1, 2, 1, 1, 2]),
],
}),
};
let mut buf = Buffer::default();
msg.push_ber(&mut buf)?;
assert_eq!(buf.data(), &expected);
Ok(())
}
#[test]
fn test_encode_snmp_v2c_get() -> Result<(), SnmpError> {
fn test_encode_snmp_get() -> Result<(), SnmpError> {
let expected = [
0x30u8, 0x35, 0x02, 0x01, 0x01, 0x04, 0x06, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0xa0,
0x28, 0x02, 0x04, 0x63, 0xcc, 0xac, 0x7d, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x30,
Expand All @@ -376,7 +314,6 @@ mod tests {
];
let community = [0x70u8, 0x75, 0x62, 0x6c, 0x69, 0x63];
let msg = SnmpV2cMessage {
version: SnmpVersion::V2C,
community: &community,
pdu: SnmpPdu::GetRequest(SnmpGet {
request_id: 0x63ccac7d,
Expand Down

0 comments on commit 96dc6fd

Please sign in to comment.