diff --git a/src/layers/arp.rs b/src/layers/arp.rs index 8ba0b0b..9fe4175 100644 --- a/src/layers/arp.rs +++ b/src/layers/arp.rs @@ -78,21 +78,23 @@ impl Layer for ARP { #[cfg(test)] mod tests { - #[test] - fn parse_arp_simple() { - use crate::layers; - use crate::{Packet, ENCAP_TYPE_ETH}; + wasm_tests! { + #[test] + fn parse_arp_simple() { + use crate::layers; + use crate::{Packet, ENCAP_TYPE_ETH}; - let _ = layers::register_defaults(); + let _ = layers::register_defaults(); - let array = hex::decode("c402326b0000c4013258000008060001080006040001c401325800000a000001c402326b00000a000002000000000000000000000000000000000000"); - assert!(array.is_ok()); - let array = array.unwrap(); + let array = hex::decode("c402326b0000c4013258000008060001080006040001c401325800000a000001c402326b00000a000002000000000000000000000000000000000000"); + assert!(array.is_ok()); + let array = array.unwrap(); - let p = Packet::from_bytes(&array, ENCAP_TYPE_ETH); - assert!(p.is_ok(), "{:?}", p.err()); + let p = Packet::from_bytes(&array, ENCAP_TYPE_ETH); + assert!(p.is_ok(), "{:?}", p.err()); - let p = p.unwrap(); - assert!(p.layers.len() == 2, "{:?}", p); + let p = p.unwrap(); + assert!(p.layers.len() == 2, "{:?}", p); + } } } diff --git a/src/layers/dns.rs b/src/layers/dns.rs index dbca3e0..aa8296f 100644 --- a/src/layers/dns.rs +++ b/src/layers/dns.rs @@ -256,7 +256,9 @@ impl DNS { let (name, _) = Self::dns_name_from_bytes(bytes, offset, remaining)?; DNSRecordData::CNAME(name) } - 6 => /* SOA */ { + 6 => + /* SOA */ + { // FIXME: into an inline function? let mut offset = offset; let mut remaining = remaining; @@ -432,72 +434,74 @@ mod tests { use crate::layers; use crate::{Layer, Packet, ENCAP_TYPE_ETH}; - #[test] - fn parse_valid_dns_packet() { - let _ = layers::register_defaults(); - - let dns_query = vec![ - 0x52, 0x54, 0x00, 0xbd, 0x1c, 0x70, 0xfe, 0x54, /* RT...p.T */ - 0x00, 0x3e, 0x00, 0x96, 0x08, 0x00, 0x45, 0x00, /* .>....E. */ - 0x00, 0xe0, 0x00, 0x00, 0x40, 0x00, 0x40, 0x11, /* ....@.@. */ - 0xc4, 0x74, 0xc0, 0xa8, 0x7a, 0x01, 0xc0, 0xa8, /* .t..z... */ - 0x7a, 0x46, 0x00, 0x35, 0xdb, 0x13, 0x00, 0xcc, /* zF.5.... */ - 0x76, 0x76, /* DNS */ 0xf3, 0x03, 0x81, 0x80, 0x00, 0x01, /* vv...... */ - 0x00, 0x01, 0x00, 0x04, 0x00, 0x04, 0x03, 0x77, /* .......w */ - 0x77, 0x77, 0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, /* ww.googl */ - 0x65, 0x03, 0x63, 0x6f, 0x6d, 0x00, 0x00, 0x1c, /* e.com... */ - 0x00, 0x01, 0xc0, 0x0c, 0x00, 0x1c, 0x00, 0x01, /* ........ */ - 0x00, 0x00, 0x01, 0x2c, 0x00, 0x10, 0x2a, 0x00, /* ...,..*. */ - 0x14, 0x50, 0x40, 0x0c, 0x0c, 0x01, 0x00, 0x00, /* .P@..... */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0xc0, 0x10, /* .....i.. */ - 0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, /* ........ */ - 0x00, 0x06, 0x03, 0x6e, 0x73, 0x34, 0xc0, 0x10, /* ...ns4.. */ - 0xc0, 0x10, 0x00, 0x02, 0x00, 0x01, 0x00, 0x02, /* ........ */ - 0xa3, 0x00, 0x00, 0x06, 0x03, 0x6e, 0x73, 0x32, /* .....ns2 */ - 0xc0, 0x10, 0xc0, 0x10, 0x00, 0x02, 0x00, 0x01, /* ........ */ - 0x00, 0x02, 0xa3, 0x00, 0x00, 0x06, 0x03, 0x6e, /* .......n */ - 0x73, 0x31, 0xc0, 0x10, 0xc0, 0x10, 0x00, 0x02, /* s1...... */ - 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, 0x00, 0x06, /* ........ */ - 0x03, 0x6e, 0x73, 0x33, 0xc0, 0x10, 0xc0, 0x6c, /* .ns3...l */ - 0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, /* ........ */ - 0x00, 0x04, 0xd8, 0xef, 0x20, 0x0a, 0xc0, 0x5a, /* .... ..Z */ - 0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, /* ........ */ - 0x00, 0x04, 0xd8, 0xef, 0x22, 0x0a, 0xc0, 0x7e, /* ...."..~ */ - 0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, /* ........ */ - 0x00, 0x04, 0xd8, 0xef, 0x24, 0x0a, 0xc0, 0x48, /* ....$..H */ - 0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, /* ........ */ - 0x00, 0x04, 0xd8, 0xef, 0x26, 0x0a, /* ....&. */ - ]; - - let mut dns: Box = Box::new(super::DNS::default()); - - let p = dns.decode_bytes(&dns_query[42..]); - assert!(p.is_ok(), "{:#?}", dns); - } + wasm_tests! { + #[test] + fn parse_valid_dns_packet() { + let _ = layers::register_defaults(); + + let dns_query = vec![ + 0x52, 0x54, 0x00, 0xbd, 0x1c, 0x70, 0xfe, 0x54, /* RT...p.T */ + 0x00, 0x3e, 0x00, 0x96, 0x08, 0x00, 0x45, 0x00, /* .>....E. */ + 0x00, 0xe0, 0x00, 0x00, 0x40, 0x00, 0x40, 0x11, /* ....@.@. */ + 0xc4, 0x74, 0xc0, 0xa8, 0x7a, 0x01, 0xc0, 0xa8, /* .t..z... */ + 0x7a, 0x46, 0x00, 0x35, 0xdb, 0x13, 0x00, 0xcc, /* zF.5.... */ + 0x76, 0x76, /* DNS */ 0xf3, 0x03, 0x81, 0x80, 0x00, 0x01, /* vv...... */ + 0x00, 0x01, 0x00, 0x04, 0x00, 0x04, 0x03, 0x77, /* .......w */ + 0x77, 0x77, 0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, /* ww.googl */ + 0x65, 0x03, 0x63, 0x6f, 0x6d, 0x00, 0x00, 0x1c, /* e.com... */ + 0x00, 0x01, 0xc0, 0x0c, 0x00, 0x1c, 0x00, 0x01, /* ........ */ + 0x00, 0x00, 0x01, 0x2c, 0x00, 0x10, 0x2a, 0x00, /* ...,..*. */ + 0x14, 0x50, 0x40, 0x0c, 0x0c, 0x01, 0x00, 0x00, /* .P@..... */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0xc0, 0x10, /* .....i.. */ + 0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, /* ........ */ + 0x00, 0x06, 0x03, 0x6e, 0x73, 0x34, 0xc0, 0x10, /* ...ns4.. */ + 0xc0, 0x10, 0x00, 0x02, 0x00, 0x01, 0x00, 0x02, /* ........ */ + 0xa3, 0x00, 0x00, 0x06, 0x03, 0x6e, 0x73, 0x32, /* .....ns2 */ + 0xc0, 0x10, 0xc0, 0x10, 0x00, 0x02, 0x00, 0x01, /* ........ */ + 0x00, 0x02, 0xa3, 0x00, 0x00, 0x06, 0x03, 0x6e, /* .......n */ + 0x73, 0x31, 0xc0, 0x10, 0xc0, 0x10, 0x00, 0x02, /* s1...... */ + 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, 0x00, 0x06, /* ........ */ + 0x03, 0x6e, 0x73, 0x33, 0xc0, 0x10, 0xc0, 0x6c, /* .ns3...l */ + 0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, /* ........ */ + 0x00, 0x04, 0xd8, 0xef, 0x20, 0x0a, 0xc0, 0x5a, /* .... ..Z */ + 0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, /* ........ */ + 0x00, 0x04, 0xd8, 0xef, 0x22, 0x0a, 0xc0, 0x7e, /* ...."..~ */ + 0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, /* ........ */ + 0x00, 0x04, 0xd8, 0xef, 0x24, 0x0a, 0xc0, 0x48, /* ....$..H */ + 0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, /* ........ */ + 0x00, 0x04, 0xd8, 0xef, 0x26, 0x0a, /* ....&. */ + ]; + + let mut dns: Box = Box::new(super::DNS::default()); + + let p = dns.decode_bytes(&dns_query[42..]); + assert!(p.is_ok(), "{:#?}", dns); + } - #[test] - fn test_dns_parse_gopacket_regression() { - let _ = layers::register_defaults(); - - // testPacketDNSRegression is the packet: - // 11:08:05.708342 IP 109.194.160.4.57766 > 95.211.92.14.53: 63000% [1au] A? picslife.ru. (40) - // 0x0000: 0022 19b6 7e22 000f 35bb 0b40 0800 4500 ."..~"..5..@..E. - // 0x0010: 0044 89c4 0000 3811 2f3d 6dc2 a004 5fd3 .D....8./=m..._. - // 0x0020: 5c0e e1a6 0035 0030 a597 f618 0010 0001 \....5.0........ - // 0x0030: 0000 0000 0001 0870 6963 736c 6966 6502 .......picslife. - // 0x0040: 7275 0000 0100 0100 0029 1000 0000 8000 ru.......)...... - // 0x0050: 0000 .. - let test_packet_dns_regression = vec![ - 0x00, 0x22, 0x19, 0xb6, 0x7e, 0x22, 0x00, 0x0f, 0x35, 0xbb, 0x0b, 0x40, 0x08, 0x00, - 0x45, 0x00, 0x00, 0x44, 0x89, 0xc4, 0x00, 0x00, 0x38, 0x11, 0x2f, 0x3d, 0x6d, 0xc2, - 0xa0, 0x04, 0x5f, 0xd3, 0x5c, 0x0e, 0xe1, 0xa6, 0x00, 0x35, 0x00, 0x30, 0xa5, 0x97, - 0xf6, 0x18, 0x00, 0x10, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x08, 0x70, - 0x69, 0x63, 0x73, 0x6c, 0x69, 0x66, 0x65, 0x02, 0x72, 0x75, 0x00, 0x00, 0x01, 0x00, - 0x01, 0x00, 0x00, 0x29, 0x10, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, - ]; - let p = Packet::from_bytes(&test_packet_dns_regression, ENCAP_TYPE_ETH); - assert!(p.is_ok()); - let p = p.unwrap(); - assert!(p.layers.len() == 4, "{:#?}", p); + #[test] + fn test_dns_parse_gopacket_regression() { + let _ = layers::register_defaults(); + + // testPacketDNSRegression is the packet: + // 11:08:05.708342 IP 109.194.160.4.57766 > 95.211.92.14.53: 63000% [1au] A? picslife.ru. (40) + // 0x0000: 0022 19b6 7e22 000f 35bb 0b40 0800 4500 ."..~"..5..@..E. + // 0x0010: 0044 89c4 0000 3811 2f3d 6dc2 a004 5fd3 .D....8./=m..._. + // 0x0020: 5c0e e1a6 0035 0030 a597 f618 0010 0001 \....5.0........ + // 0x0030: 0000 0000 0001 0870 6963 736c 6966 6502 .......picslife. + // 0x0040: 7275 0000 0100 0100 0029 1000 0000 8000 ru.......)...... + // 0x0050: 0000 .. + let test_packet_dns_regression = vec![ + 0x00, 0x22, 0x19, 0xb6, 0x7e, 0x22, 0x00, 0x0f, 0x35, 0xbb, 0x0b, 0x40, 0x08, 0x00, + 0x45, 0x00, 0x00, 0x44, 0x89, 0xc4, 0x00, 0x00, 0x38, 0x11, 0x2f, 0x3d, 0x6d, 0xc2, + 0xa0, 0x04, 0x5f, 0xd3, 0x5c, 0x0e, 0xe1, 0xa6, 0x00, 0x35, 0x00, 0x30, 0xa5, 0x97, + 0xf6, 0x18, 0x00, 0x10, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x08, 0x70, + 0x69, 0x63, 0x73, 0x6c, 0x69, 0x66, 0x65, 0x02, 0x72, 0x75, 0x00, 0x00, 0x01, 0x00, + 0x01, 0x00, 0x00, 0x29, 0x10, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, + ]; + let p = Packet::from_bytes(&test_packet_dns_regression, ENCAP_TYPE_ETH); + assert!(p.is_ok()); + let p = p.unwrap(); + assert!(p.layers.len() == 4, "{:#?}", p); + } } } diff --git a/src/layers/icmp.rs b/src/layers/icmp.rs index 3433a2b..f6f89e9 100644 --- a/src/layers/icmp.rs +++ b/src/layers/icmp.rs @@ -231,172 +231,174 @@ mod tests { use crate::layers; use crate::{Layer, Packet, ENCAP_TYPE_ETH}; - #[test] - fn parse_valid_icmp_packet() { - let _ = layers::register_defaults(); - - let icmp_packet = vec![ - 0x00, 0x20, 0x78, 0xe1, 0x5a, 0x80, 0x00, 0x10, 0x7b, 0x81, 0x43, 0xe3, 0x08, 0x00, - 0x45, 0x00, 0x00, 0x38, 0x62, 0x3d, 0x00, 0x00, 0xff, 0x01, 0xd8, 0x1e, 0x0a, 0x02, - 0x63, 0x63, 0x0a, 0x02, 0x0a, 0x02, 0x05, 0x01, 0x38, 0x3e, 0x0a, 0x02, 0x63, 0x62, - 0x45, 0x00, 0x00, 0x3c, 0x3a, 0x00, 0x00, 0x00, 0x1f, 0x01, 0xfc, 0xb3, 0x0a, 0x02, - 0x0a, 0x02, 0x0a, 0x03, 0x47, 0x07, 0x08, 0x00, 0x1a, 0x5c, 0x02, 0x00, 0x31, 0x00, - ]; - - let mut icmp: Box = Box::new(super::ICMP::default()); - - let p = icmp.decode_bytes(&icmp_packet[34..]); - assert!(p.is_ok(), "{:#?}", icmp); - } + wasm_tests! { + #[test] + fn parse_valid_icmp_packet() { + let _ = layers::register_defaults(); + + let icmp_packet = vec![ + 0x00, 0x20, 0x78, 0xe1, 0x5a, 0x80, 0x00, 0x10, 0x7b, 0x81, 0x43, 0xe3, 0x08, 0x00, + 0x45, 0x00, 0x00, 0x38, 0x62, 0x3d, 0x00, 0x00, 0xff, 0x01, 0xd8, 0x1e, 0x0a, 0x02, + 0x63, 0x63, 0x0a, 0x02, 0x0a, 0x02, 0x05, 0x01, 0x38, 0x3e, 0x0a, 0x02, 0x63, 0x62, + 0x45, 0x00, 0x00, 0x3c, 0x3a, 0x00, 0x00, 0x00, 0x1f, 0x01, 0xfc, 0xb3, 0x0a, 0x02, + 0x0a, 0x02, 0x0a, 0x03, 0x47, 0x07, 0x08, 0x00, 0x1a, 0x5c, 0x02, 0x00, 0x31, 0x00, + ]; + + let mut icmp: Box = Box::new(super::ICMP::default()); + + let p = icmp.decode_bytes(&icmp_packet[34..]); + assert!(p.is_ok(), "{:#?}", icmp); + } - #[test] - fn test_icmp_parse_regression() { - let _ = layers::register_defaults(); - - let test_icmp_packet = vec![ - 0x00, 0x20, 0x78, 0xe1, 0x5a, 0x80, 0x00, 0x10, 0x7b, 0x81, 0x43, 0xe3, 0x08, 0x00, - 0x45, 0x00, 0x00, 0x38, 0x62, 0x3d, 0x00, 0x00, 0xff, 0x01, 0xd8, 0x1e, 0x0a, 0x02, - 0x63, 0x63, 0x0a, 0x02, 0x0a, 0x02, 0x05, 0x01, 0x38, 0x3e, 0x0a, 0x02, 0x63, 0x62, - 0x45, 0x00, 0x00, 0x3c, 0x3a, 0x00, 0x00, 0x00, 0x1f, 0x01, 0xfc, 0xb3, 0x0a, 0x02, - 0x0a, 0x02, 0x0a, 0x03, 0x47, 0x07, 0x08, 0x00, 0x1a, 0x5c, 0x02, 0x00, 0x31, 0x00, - ]; - - let p = Packet::from_bytes(&test_icmp_packet, ENCAP_TYPE_ETH); - assert!(p.is_ok()); - let p = p.unwrap(); - assert!(p.layers.len() == 3, "{:#?}", p); - } + #[test] + fn test_icmp_parse_regression() { + let _ = layers::register_defaults(); + + let test_icmp_packet = vec![ + 0x00, 0x20, 0x78, 0xe1, 0x5a, 0x80, 0x00, 0x10, 0x7b, 0x81, 0x43, 0xe3, 0x08, 0x00, + 0x45, 0x00, 0x00, 0x38, 0x62, 0x3d, 0x00, 0x00, 0xff, 0x01, 0xd8, 0x1e, 0x0a, 0x02, + 0x63, 0x63, 0x0a, 0x02, 0x0a, 0x02, 0x05, 0x01, 0x38, 0x3e, 0x0a, 0x02, 0x63, 0x62, + 0x45, 0x00, 0x00, 0x3c, 0x3a, 0x00, 0x00, 0x00, 0x1f, 0x01, 0xfc, 0xb3, 0x0a, 0x02, + 0x0a, 0x02, 0x0a, 0x03, 0x47, 0x07, 0x08, 0x00, 0x1a, 0x5c, 0x02, 0x00, 0x31, 0x00, + ]; + + let p = Packet::from_bytes(&test_icmp_packet, ENCAP_TYPE_ETH); + assert!(p.is_ok()); + let p = p.unwrap(); + assert!(p.layers.len() == 3, "{:#?}", p); + } - #[test] - fn parse_icmp_redirect_packet() { - let _ = layers::register_defaults(); - - let icmp_redirect_packet = vec![ - 0x00, 0x20, 0x78, 0xe1, 0x5a, 0x80, 0x00, 0x10, 0x7b, 0x81, 0x43, 0xe3, 0x08, 0x00, - 0x45, 0x00, 0x00, 0x38, 0x62, 0x3d, 0x00, 0x00, 0xff, 0x01, 0xd8, 0x1e, 0x0a, 0x02, - 0x63, 0x63, 0x0a, 0x02, 0x0a, 0x02, 0x05, 0x01, 0x38, 0x3e, 0x0a, 0x02, 0x63, 0x62, - 0x45, 0x00, 0x00, 0x3c, 0x3a, 0x00, 0x00, 0x00, 0x1f, 0x01, 0xfc, 0xb3, 0x0a, 0x02, - 0x0a, 0x02, 0x0a, 0x03, 0x47, 0x07, 0x08, 0x00, 0x1a, 0x5c, 0x02, 0x00, 0x31, 0x00, - ]; - - let p = Packet::from_bytes(&icmp_redirect_packet, ENCAP_TYPE_ETH); - assert!(p.is_ok()); - let p = p.unwrap(); - - let icmp_packet = serde_json::to_value(&p.layers[2]).unwrap(); - assert_eq!(icmp_packet.get("type"), Some(&json!(5))); - assert_eq!(icmp_packet.get("code"), Some(&json!(1))); - assert_eq!(icmp_packet.get("checksum"), Some(&json!("0x383e"))); - assert_eq!( - icmp_packet.get("gateway_address"), - Some(&json!("10.2.99.98")) - ); - } + #[test] + fn parse_icmp_redirect_packet() { + let _ = layers::register_defaults(); + + let icmp_redirect_packet = vec![ + 0x00, 0x20, 0x78, 0xe1, 0x5a, 0x80, 0x00, 0x10, 0x7b, 0x81, 0x43, 0xe3, 0x08, 0x00, + 0x45, 0x00, 0x00, 0x38, 0x62, 0x3d, 0x00, 0x00, 0xff, 0x01, 0xd8, 0x1e, 0x0a, 0x02, + 0x63, 0x63, 0x0a, 0x02, 0x0a, 0x02, 0x05, 0x01, 0x38, 0x3e, 0x0a, 0x02, 0x63, 0x62, + 0x45, 0x00, 0x00, 0x3c, 0x3a, 0x00, 0x00, 0x00, 0x1f, 0x01, 0xfc, 0xb3, 0x0a, 0x02, + 0x0a, 0x02, 0x0a, 0x03, 0x47, 0x07, 0x08, 0x00, 0x1a, 0x5c, 0x02, 0x00, 0x31, 0x00, + ]; + + let p = Packet::from_bytes(&icmp_redirect_packet, ENCAP_TYPE_ETH); + assert!(p.is_ok()); + let p = p.unwrap(); + + let icmp_packet = serde_json::to_value(&p.layers[2]).unwrap(); + assert_eq!(icmp_packet.get("type"), Some(&json!(5))); + assert_eq!(icmp_packet.get("code"), Some(&json!(1))); + assert_eq!(icmp_packet.get("checksum"), Some(&json!("0x383e"))); + assert_eq!( + icmp_packet.get("gateway_address"), + Some(&json!("10.2.99.98")) + ); + } - #[test] - fn parse_icmp_echo_packet() { - let _ = layers::register_defaults(); - - let icmp_echo_packet = vec![ - 0x50, 0xeb, 0x1a, 0x90, 0x61, 0x32, 0xd4, 0x6a, 0x6a, 0xb4, 0x62, 0x0d, 0x08, 0x00, - 0x45, 0x00, 0x00, 0x54, 0x92, 0xe0, 0x40, 0x00, 0x40, 0x01, 0x49, 0x56, 0x0a, 0x64, - 0x01, 0xb0, 0x8e, 0xfa, 0xc3, 0x64, 0x08, 0x00, 0x98, 0xff, 0x00, 0x02, 0x00, 0x02, - 0x9d, 0x25, 0xda, 0x65, 0x00, 0x00, 0x00, 0x00, 0x20, 0x9e, 0x08, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, - 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, - 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, - ]; - - let p = Packet::from_bytes(&icmp_echo_packet, ENCAP_TYPE_ETH); - assert!(p.is_ok()); - let p = p.unwrap(); - - let icmp_packet = serde_json::to_value(&p.layers[2]).unwrap(); - assert_eq!(icmp_packet.get("type"), Some(&json!(8))); - assert_eq!(icmp_packet.get("code"), Some(&json!(0))); - assert_eq!(icmp_packet.get("checksum"), Some(&json!("0x98ff"))); - assert_eq!(icmp_packet.get("identifier"), Some(&json!(2))); - assert_eq!(icmp_packet.get("sequence_number"), Some(&json!(2))); - } + #[test] + fn parse_icmp_echo_packet() { + let _ = layers::register_defaults(); + + let icmp_echo_packet = vec![ + 0x50, 0xeb, 0x1a, 0x90, 0x61, 0x32, 0xd4, 0x6a, 0x6a, 0xb4, 0x62, 0x0d, 0x08, 0x00, + 0x45, 0x00, 0x00, 0x54, 0x92, 0xe0, 0x40, 0x00, 0x40, 0x01, 0x49, 0x56, 0x0a, 0x64, + 0x01, 0xb0, 0x8e, 0xfa, 0xc3, 0x64, 0x08, 0x00, 0x98, 0xff, 0x00, 0x02, 0x00, 0x02, + 0x9d, 0x25, 0xda, 0x65, 0x00, 0x00, 0x00, 0x00, 0x20, 0x9e, 0x08, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, + 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, + 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + ]; + + let p = Packet::from_bytes(&icmp_echo_packet, ENCAP_TYPE_ETH); + assert!(p.is_ok()); + let p = p.unwrap(); + + let icmp_packet = serde_json::to_value(&p.layers[2]).unwrap(); + assert_eq!(icmp_packet.get("type"), Some(&json!(8))); + assert_eq!(icmp_packet.get("code"), Some(&json!(0))); + assert_eq!(icmp_packet.get("checksum"), Some(&json!("0x98ff"))); + assert_eq!(icmp_packet.get("identifier"), Some(&json!(2))); + assert_eq!(icmp_packet.get("sequence_number"), Some(&json!(2))); + } - #[test] - fn parse_icmp_destination_unreachable_packet() { - let _ = layers::register_defaults(); - - let icmp_destination_unreachable_packet = vec![ - 0xca, 0x01, 0x0c, 0x68, 0x00, 0x08, 0xca, 0x00, 0x0c, 0x68, 0x00, 0x08, 0x08, 0x00, - 0x45, 0x00, 0x00, 0x38, 0x00, 0x03, 0x00, 0x00, 0xff, 0x01, 0xa3, 0xbd, 0x0a, 0x01, - 0x02, 0x01, 0x0a, 0x01, 0x02, 0x02, 0x03, 0x01, 0x2e, 0xfc, 0x00, 0x00, 0x00, 0x00, - 0x45, 0x00, 0x00, 0x64, 0x00, 0x05, 0x00, 0x00, 0xfe, 0x01, 0xae, 0x8f, 0x0a, 0x01, - 0x02, 0x02, 0x01, 0x01, 0x01, 0x01, 0x08, 0x00, 0xc6, 0x01, 0x00, 0x01, 0x00, 0x00, - ]; - - let p = Packet::from_bytes(&icmp_destination_unreachable_packet, ENCAP_TYPE_ETH); - assert!(p.is_ok()); - let p = p.unwrap(); - - let icmp_packet = serde_json::to_value(&p.layers[2]).unwrap(); - assert_eq!(icmp_packet.get("type"), Some(&json!(3))); - assert_eq!(icmp_packet.get("code"), Some(&json!(1))); - assert_eq!(icmp_packet.get("checksum"), Some(&json!("0x2efc"))); - } + #[test] + fn parse_icmp_destination_unreachable_packet() { + let _ = layers::register_defaults(); + + let icmp_destination_unreachable_packet = vec![ + 0xca, 0x01, 0x0c, 0x68, 0x00, 0x08, 0xca, 0x00, 0x0c, 0x68, 0x00, 0x08, 0x08, 0x00, + 0x45, 0x00, 0x00, 0x38, 0x00, 0x03, 0x00, 0x00, 0xff, 0x01, 0xa3, 0xbd, 0x0a, 0x01, + 0x02, 0x01, 0x0a, 0x01, 0x02, 0x02, 0x03, 0x01, 0x2e, 0xfc, 0x00, 0x00, 0x00, 0x00, + 0x45, 0x00, 0x00, 0x64, 0x00, 0x05, 0x00, 0x00, 0xfe, 0x01, 0xae, 0x8f, 0x0a, 0x01, + 0x02, 0x02, 0x01, 0x01, 0x01, 0x01, 0x08, 0x00, 0xc6, 0x01, 0x00, 0x01, 0x00, 0x00, + ]; + + let p = Packet::from_bytes(&icmp_destination_unreachable_packet, ENCAP_TYPE_ETH); + assert!(p.is_ok()); + let p = p.unwrap(); + + let icmp_packet = serde_json::to_value(&p.layers[2]).unwrap(); + assert_eq!(icmp_packet.get("type"), Some(&json!(3))); + assert_eq!(icmp_packet.get("code"), Some(&json!(1))); + assert_eq!(icmp_packet.get("checksum"), Some(&json!("0x2efc"))); + } - #[test] - fn parse_timestamp_icmp_type() { - let _ = layers::register_defaults(); - - let icmp_timestamp_packet = vec![ - 0x00, 0x11, 0x2f, 0x36, 0x8c, 0xda, 0x00, 0xa0, 0xd1, 0xbe, 0x97, 0xdd, 0x08, 0x00, - 0x45, 0x00, 0x00, 0x28, 0x0f, 0x3c, 0x00, 0x00, 0x80, 0x01, 0xa9, 0x7b, 0xc0, 0xa8, - 0x00, 0x67, 0xc0, 0xa8, 0x00, 0x66, 0x0e, 0x00, 0xd3, 0x11, 0x39, 0x30, 0x00, 0x00, - 0x00, 0xf6, 0x36, 0x59, 0x61, 0x36, 0xf6, 0x00, 0x61, 0x36, 0xf6, 0x00, - ]; - - let p = Packet::from_bytes(&icmp_timestamp_packet, ENCAP_TYPE_ETH); - assert!(p.is_ok()); - let p = p.unwrap(); - - let icmp_packet = serde_json::to_value(&p.layers[2]).unwrap(); - assert_eq!(icmp_packet.get("type"), Some(&json!(14))); - assert_eq!(icmp_packet.get("code"), Some(&json!(0))); - assert_eq!(icmp_packet.get("checksum"), Some(&json!("0xd311"))); - assert_eq!(icmp_packet.get("identifier"), Some(&json!(14640))); - assert_eq!(icmp_packet.get("sequence_number"), Some(&json!(0))); - assert_eq!( - icmp_packet.get("originate_timestamp"), - Some(&json!(16135769)) - ); - assert_eq!( - icmp_packet.get("recieve_timestamp"), - Some(&json!(1630991872)) - ); - assert_eq!( - icmp_packet.get("transmit_timestamp"), - Some(&json!(1630991872)) - ); - } + #[test] + fn parse_timestamp_icmp_type() { + let _ = layers::register_defaults(); + + let icmp_timestamp_packet = vec![ + 0x00, 0x11, 0x2f, 0x36, 0x8c, 0xda, 0x00, 0xa0, 0xd1, 0xbe, 0x97, 0xdd, 0x08, 0x00, + 0x45, 0x00, 0x00, 0x28, 0x0f, 0x3c, 0x00, 0x00, 0x80, 0x01, 0xa9, 0x7b, 0xc0, 0xa8, + 0x00, 0x67, 0xc0, 0xa8, 0x00, 0x66, 0x0e, 0x00, 0xd3, 0x11, 0x39, 0x30, 0x00, 0x00, + 0x00, 0xf6, 0x36, 0x59, 0x61, 0x36, 0xf6, 0x00, 0x61, 0x36, 0xf6, 0x00, + ]; + + let p = Packet::from_bytes(&icmp_timestamp_packet, ENCAP_TYPE_ETH); + assert!(p.is_ok()); + let p = p.unwrap(); + + let icmp_packet = serde_json::to_value(&p.layers[2]).unwrap(); + assert_eq!(icmp_packet.get("type"), Some(&json!(14))); + assert_eq!(icmp_packet.get("code"), Some(&json!(0))); + assert_eq!(icmp_packet.get("checksum"), Some(&json!("0xd311"))); + assert_eq!(icmp_packet.get("identifier"), Some(&json!(14640))); + assert_eq!(icmp_packet.get("sequence_number"), Some(&json!(0))); + assert_eq!( + icmp_packet.get("originate_timestamp"), + Some(&json!(16135769)) + ); + assert_eq!( + icmp_packet.get("recieve_timestamp"), + Some(&json!(1630991872)) + ); + assert_eq!( + icmp_packet.get("transmit_timestamp"), + Some(&json!(1630991872)) + ); + } - #[test] - fn parse_unsupported_icmp_type() { - let _ = layers::register_defaults(); - - let icmp_unsupported_packet = vec![ - 0x00, 0xa0, 0xd1, 0xbe, 0x97, 0xdd, 0x00, 0x11, 0x2f, 0x36, 0x8c, 0xda, 0x08, 0x00, - 0x45, 0x00, 0x00, 0x28, 0x8d, 0xff, 0x00, 0x00, 0x80, 0x01, 0x2a, 0xb8, 0xc0, 0xa8, - 0x00, 0x66, 0xc0, 0xa8, 0x00, 0x67, 0x0c, 0x00, 0x94, 0xe3, 0x39, 0x30, 0x00, 0x00, - 0x00, 0xf6, 0x23, 0xf6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - ]; - - let p = Packet::from_bytes(&icmp_unsupported_packet, ENCAP_TYPE_ETH); - assert!(p.is_ok()); - let p = p.unwrap(); - let icmp_packet = serde_json::to_value(&p.layers[2]).unwrap(); - assert_eq!(icmp_packet.get("type"), Some(&json!(12))); - assert_eq!(icmp_packet.get("code"), Some(&json!(0))); - assert_eq!(icmp_packet.get("checksum"), Some(&json!("0x94e3"))); - assert_eq!( - icmp_packet.get("unsupported"), - Some(&json!("3930000000f623f60000000000000000")) - ); + #[test] + fn parse_unsupported_icmp_type() { + let _ = layers::register_defaults(); + + let icmp_unsupported_packet = vec![ + 0x00, 0xa0, 0xd1, 0xbe, 0x97, 0xdd, 0x00, 0x11, 0x2f, 0x36, 0x8c, 0xda, 0x08, 0x00, + 0x45, 0x00, 0x00, 0x28, 0x8d, 0xff, 0x00, 0x00, 0x80, 0x01, 0x2a, 0xb8, 0xc0, 0xa8, + 0x00, 0x66, 0xc0, 0xa8, 0x00, 0x67, 0x0c, 0x00, 0x94, 0xe3, 0x39, 0x30, 0x00, 0x00, + 0x00, 0xf6, 0x23, 0xf6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + ]; + + let p = Packet::from_bytes(&icmp_unsupported_packet, ENCAP_TYPE_ETH); + assert!(p.is_ok()); + let p = p.unwrap(); + let icmp_packet = serde_json::to_value(&p.layers[2]).unwrap(); + assert_eq!(icmp_packet.get("type"), Some(&json!(12))); + assert_eq!(icmp_packet.get("code"), Some(&json!(0))); + assert_eq!(icmp_packet.get("checksum"), Some(&json!("0x94e3"))); + assert_eq!( + icmp_packet.get("unsupported"), + Some(&json!("3930000000f623f60000000000000000")) + ); + } } } diff --git a/src/layers/icmpv6.rs b/src/layers/icmpv6.rs index 2e456a2..7798b67 100644 --- a/src/layers/icmpv6.rs +++ b/src/layers/icmpv6.rs @@ -418,172 +418,174 @@ mod tests { use crate::layers; use crate::{Layer, Packet, ENCAP_TYPE_ETH}; - #[test] - fn parse_valid_icmpv6_packet() { - let _ = layers::register_defaults(); - - let icmpv6_packet = vec![ - 0x00, 0x50, 0x56, 0x8a, 0x22, 0x80, 0x00, 0x50, 0x56, 0x8a, 0x0f, 0xe9, 0x86, 0xdd, - 0x60, 0x00, 0x00, 0x00, 0x00, 0x40, 0x3a, 0x40, 0x20, 0x01, 0x05, 0x00, 0x01, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x20, 0x01, 0x05, 0x00, - 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x25, 0x80, 0x00, - 0xc9, 0x2a, 0x0e, 0x20, 0x00, 0x01, 0x1c, 0xcc, 0x53, 0x4f, 0x00, 0x00, 0x00, 0x00, - 0x1e, 0x03, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, - 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, - 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, - 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, - ]; - - let mut icmpv6: Box = Box::new(super::ICMPv6::default()); - - let p = icmpv6.decode_bytes(&icmpv6_packet[34..]); - assert!(p.is_ok(), "{:#?}", icmpv6); - } + wasm_tests! { + #[test] + fn parse_valid_icmpv6_packet() { + let _ = layers::register_defaults(); + + let icmpv6_packet = vec![ + 0x00, 0x50, 0x56, 0x8a, 0x22, 0x80, 0x00, 0x50, 0x56, 0x8a, 0x0f, 0xe9, 0x86, 0xdd, + 0x60, 0x00, 0x00, 0x00, 0x00, 0x40, 0x3a, 0x40, 0x20, 0x01, 0x05, 0x00, 0x01, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x20, 0x01, 0x05, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x25, 0x80, 0x00, + 0xc9, 0x2a, 0x0e, 0x20, 0x00, 0x01, 0x1c, 0xcc, 0x53, 0x4f, 0x00, 0x00, 0x00, 0x00, + 0x1e, 0x03, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, + 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, + 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, + 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + ]; + + let mut icmpv6: Box = Box::new(super::ICMPv6::default()); + + let p = icmpv6.decode_bytes(&icmpv6_packet[34..]); + assert!(p.is_ok(), "{:#?}", icmpv6); + } - #[test] - fn test_icmpv6_parse_regression() { - let _ = layers::register_defaults(); - - let test_icmpv6_packet = vec![ - 0x00, 0x50, 0x56, 0x8a, 0x22, 0x80, 0x00, 0x50, 0x56, 0x8a, 0x0f, 0xe9, 0x86, 0xdd, - 0x60, 0x00, 0x00, 0x00, 0x00, 0x40, 0x3a, 0x40, 0x20, 0x01, 0x05, 0x00, 0x01, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x20, 0x01, 0x05, 0x00, - 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x25, 0x80, 0x00, - 0xc9, 0x2a, 0x0e, 0x20, 0x00, 0x01, 0x1c, 0xcc, 0x53, 0x4f, 0x00, 0x00, 0x00, 0x00, - 0x1e, 0x03, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, - 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, - 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, - 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, - ]; - - let p = Packet::from_bytes(&test_icmpv6_packet, ENCAP_TYPE_ETH); - assert!(p.is_ok()); - let p = p.unwrap(); - assert!(p.layers.len() == 3, "{:#?}", p); - } + #[test] + fn test_icmpv6_parse_regression() { + let _ = layers::register_defaults(); + + let test_icmpv6_packet = vec![ + 0x00, 0x50, 0x56, 0x8a, 0x22, 0x80, 0x00, 0x50, 0x56, 0x8a, 0x0f, 0xe9, 0x86, 0xdd, + 0x60, 0x00, 0x00, 0x00, 0x00, 0x40, 0x3a, 0x40, 0x20, 0x01, 0x05, 0x00, 0x01, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x20, 0x01, 0x05, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x25, 0x80, 0x00, + 0xc9, 0x2a, 0x0e, 0x20, 0x00, 0x01, 0x1c, 0xcc, 0x53, 0x4f, 0x00, 0x00, 0x00, 0x00, + 0x1e, 0x03, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, + 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, + 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, + 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + ]; + + let p = Packet::from_bytes(&test_icmpv6_packet, ENCAP_TYPE_ETH); + assert!(p.is_ok()); + let p = p.unwrap(); + assert!(p.layers.len() == 3, "{:#?}", p); + } - #[test] - fn parse_icmpv6_redirect_packet() { - let _ = layers::register_defaults(); - - let icmpv6_echo_request_packet = vec![ - 0x00, 0x0c, 0x29, 0x23, 0x16, 0x87, 0x00, 0x0c, 0x29, 0x25, 0xcf, 0xa1, 0x86, 0xdd, - 0x6e, 0x00, 0x00, 0x00, 0x00, 0xa0, 0x3a, 0xff, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x02, 0x0c, 0x29, 0xff, 0xfe, 0x25, 0xcf, 0xa1, 0x20, 0x01, 0x0d, 0xb8, - 0x00, 0x01, 0x00, 0x00, 0x99, 0x77, 0xf3, 0x9e, 0x80, 0xcb, 0x4e, 0xa6, 0x89, 0x00, - 0xc0, 0x7e, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x02, 0x0c, 0x29, 0xff, 0xfe, 0xfc, 0x2c, 0x3b, 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x02, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x01, 0x00, 0x0c, - 0x29, 0xfc, 0x2c, 0x3b, 0x04, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x08, - 0xb9, 0x2f, 0x00, 0x40, 0x3a, 0x3f, 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x01, 0x00, 0x00, - 0x99, 0x77, 0xf3, 0x9e, 0x80, 0xcb, 0x4e, 0xa6, 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x02, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0xe7, 0x43, - 0x1d, 0x85, 0x00, 0x01, 0x72, 0x6b, 0xf8, 0x5c, 0x00, 0x00, 0x00, 0x00, 0x91, 0x21, - 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, - 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, - 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, - 0x34, 0x35, 0x36, 0x37, - ]; - - let p = Packet::from_bytes(&icmpv6_echo_request_packet, ENCAP_TYPE_ETH); - assert!(p.is_ok()); - let p = p.unwrap(); - let icmpv6_packet = serde_json::to_value(&p.layers[2]).unwrap(); - assert_eq!(icmpv6_packet.get("type"), Some(&json!(137))); - assert_eq!(icmpv6_packet.get("code"), Some(&json!(0))); - assert_eq!(icmpv6_packet.get("checksum"), Some(&json!("0xc07e"))); - assert_eq!( - icmpv6_packet.get("target_address"), - Some(&json!("fe80::20c:29ff:fefc:2c3b")) - ); - assert_eq!( - icmpv6_packet.get("destination_address"), - Some(&json!("2001:db8:2::1")) - ); - } + #[test] + fn parse_icmpv6_redirect_packet() { + let _ = layers::register_defaults(); + + let icmpv6_echo_request_packet = vec![ + 0x00, 0x0c, 0x29, 0x23, 0x16, 0x87, 0x00, 0x0c, 0x29, 0x25, 0xcf, 0xa1, 0x86, 0xdd, + 0x6e, 0x00, 0x00, 0x00, 0x00, 0xa0, 0x3a, 0xff, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x02, 0x0c, 0x29, 0xff, 0xfe, 0x25, 0xcf, 0xa1, 0x20, 0x01, 0x0d, 0xb8, + 0x00, 0x01, 0x00, 0x00, 0x99, 0x77, 0xf3, 0x9e, 0x80, 0xcb, 0x4e, 0xa6, 0x89, 0x00, + 0xc0, 0x7e, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x02, 0x0c, 0x29, 0xff, 0xfe, 0xfc, 0x2c, 0x3b, 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x02, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x01, 0x00, 0x0c, + 0x29, 0xfc, 0x2c, 0x3b, 0x04, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x08, + 0xb9, 0x2f, 0x00, 0x40, 0x3a, 0x3f, 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x01, 0x00, 0x00, + 0x99, 0x77, 0xf3, 0x9e, 0x80, 0xcb, 0x4e, 0xa6, 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x02, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0xe7, 0x43, + 0x1d, 0x85, 0x00, 0x01, 0x72, 0x6b, 0xf8, 0x5c, 0x00, 0x00, 0x00, 0x00, 0x91, 0x21, + 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, + 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, + 0x34, 0x35, 0x36, 0x37, + ]; + + let p = Packet::from_bytes(&icmpv6_echo_request_packet, ENCAP_TYPE_ETH); + assert!(p.is_ok()); + let p = p.unwrap(); + let icmpv6_packet = serde_json::to_value(&p.layers[2]).unwrap(); + assert_eq!(icmpv6_packet.get("type"), Some(&json!(137))); + assert_eq!(icmpv6_packet.get("code"), Some(&json!(0))); + assert_eq!(icmpv6_packet.get("checksum"), Some(&json!("0xc07e"))); + assert_eq!( + icmpv6_packet.get("target_address"), + Some(&json!("fe80::20c:29ff:fefc:2c3b")) + ); + assert_eq!( + icmpv6_packet.get("destination_address"), + Some(&json!("2001:db8:2::1")) + ); + } - #[test] - fn icmpv6_echo_request_packet() { - let _ = layers::register_defaults(); - - let icmpv6_echo_request_packet = vec![ - 0x00, 0x50, 0x56, 0x8a, 0x22, 0x80, 0x00, 0x50, 0x56, 0x8a, 0x0f, 0xe9, 0x86, 0xdd, - 0x60, 0x00, 0x00, 0x00, 0x00, 0x40, 0x3a, 0x40, 0x20, 0x01, 0x05, 0x00, 0x01, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x20, 0x01, 0x05, 0x00, - 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x25, 0x80, 0x00, - 0xc9, 0x2a, 0x0e, 0x20, 0x00, 0x01, 0x1c, 0xcc, 0x53, 0x4f, 0x00, 0x00, 0x00, 0x00, - 0x1e, 0x03, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, - 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, - 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, - 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, - ]; - - let p = Packet::from_bytes(&icmpv6_echo_request_packet, ENCAP_TYPE_ETH); - assert!(p.is_ok()); - let p = p.unwrap(); - - let icmpv6_packet = serde_json::to_value(&p.layers[2]).unwrap(); - assert_eq!(icmpv6_packet.get("type"), Some(&json!(128))); - assert_eq!(icmpv6_packet.get("code"), Some(&json!(0))); - assert_eq!(icmpv6_packet.get("checksum"), Some(&json!("0xc92a"))); - assert_eq!(icmpv6_packet.get("identifier"), Some(&json!(3616))); - assert_eq!(icmpv6_packet.get("sequence_number"), Some(&json!(1))); - } + #[test] + fn icmpv6_echo_request_packet() { + let _ = layers::register_defaults(); + + let icmpv6_echo_request_packet = vec![ + 0x00, 0x50, 0x56, 0x8a, 0x22, 0x80, 0x00, 0x50, 0x56, 0x8a, 0x0f, 0xe9, 0x86, 0xdd, + 0x60, 0x00, 0x00, 0x00, 0x00, 0x40, 0x3a, 0x40, 0x20, 0x01, 0x05, 0x00, 0x01, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x20, 0x01, 0x05, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x25, 0x80, 0x00, + 0xc9, 0x2a, 0x0e, 0x20, 0x00, 0x01, 0x1c, 0xcc, 0x53, 0x4f, 0x00, 0x00, 0x00, 0x00, + 0x1e, 0x03, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, + 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, + 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, + 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + ]; + + let p = Packet::from_bytes(&icmpv6_echo_request_packet, ENCAP_TYPE_ETH); + assert!(p.is_ok()); + let p = p.unwrap(); + + let icmpv6_packet = serde_json::to_value(&p.layers[2]).unwrap(); + assert_eq!(icmpv6_packet.get("type"), Some(&json!(128))); + assert_eq!(icmpv6_packet.get("code"), Some(&json!(0))); + assert_eq!(icmpv6_packet.get("checksum"), Some(&json!("0xc92a"))); + assert_eq!(icmpv6_packet.get("identifier"), Some(&json!(3616))); + assert_eq!(icmpv6_packet.get("sequence_number"), Some(&json!(1))); + } - #[test] - fn parse_icmpv6_neighbor_solicitation_packet() { - let _ = layers::register_defaults(); - - let icmpv6_neighbor_solicitation_packet = vec![ - 0x33, 0x33, 0xff, 0x00, 0x00, 0x25, 0x00, 0x50, 0x56, 0x8a, 0x0f, 0xe9, 0x86, 0xdd, - 0x60, 0x00, 0x00, 0x00, 0x00, 0x20, 0x3a, 0xff, 0x20, 0x01, 0x05, 0x00, 0x01, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0xff, 0x02, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0x00, 0x00, 0x25, 0x87, 0x00, - 0xc6, 0x6e, 0x00, 0x00, 0x00, 0x00, 0x20, 0x01, 0x05, 0x00, 0x01, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x25, 0x01, 0x01, 0x00, 0x50, 0x56, 0x8a, - 0x0f, 0xe9, - ]; - - let p = Packet::from_bytes(&icmpv6_neighbor_solicitation_packet, ENCAP_TYPE_ETH); - assert!(p.is_ok()); - let p = p.unwrap(); - - let icmpv6_packet = serde_json::to_value(&p.layers[2]).unwrap(); - assert_eq!(icmpv6_packet.get("type"), Some(&json!(135))); - assert_eq!(icmpv6_packet.get("code"), Some(&json!(0))); - assert_eq!(icmpv6_packet.get("checksum"), Some(&json!("0xc66e"))); - assert_eq!( - icmpv6_packet.get("target_address"), - Some(&json!("2001:500:100::25")) - ); - } + #[test] + fn parse_icmpv6_neighbor_solicitation_packet() { + let _ = layers::register_defaults(); + + let icmpv6_neighbor_solicitation_packet = vec![ + 0x33, 0x33, 0xff, 0x00, 0x00, 0x25, 0x00, 0x50, 0x56, 0x8a, 0x0f, 0xe9, 0x86, 0xdd, + 0x60, 0x00, 0x00, 0x00, 0x00, 0x20, 0x3a, 0xff, 0x20, 0x01, 0x05, 0x00, 0x01, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0xff, 0x02, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0x00, 0x00, 0x25, 0x87, 0x00, + 0xc6, 0x6e, 0x00, 0x00, 0x00, 0x00, 0x20, 0x01, 0x05, 0x00, 0x01, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x25, 0x01, 0x01, 0x00, 0x50, 0x56, 0x8a, + 0x0f, 0xe9, + ]; + + let p = Packet::from_bytes(&icmpv6_neighbor_solicitation_packet, ENCAP_TYPE_ETH); + assert!(p.is_ok()); + let p = p.unwrap(); + + let icmpv6_packet = serde_json::to_value(&p.layers[2]).unwrap(); + assert_eq!(icmpv6_packet.get("type"), Some(&json!(135))); + assert_eq!(icmpv6_packet.get("code"), Some(&json!(0))); + assert_eq!(icmpv6_packet.get("checksum"), Some(&json!("0xc66e"))); + assert_eq!( + icmpv6_packet.get("target_address"), + Some(&json!("2001:500:100::25")) + ); + } - #[test] - fn parse_unsupported_icmp_type() { - let _ = layers::register_defaults(); - - let icmpv6_destination_unreachable_packet = vec![ - 0x00, 0x50, 0x56, 0x8a, 0x0f, 0xe9, 0x00, 0x50, 0x56, 0x8a, 0x22, 0x80, 0x86, 0xdd, - 0x60, 0x00, 0x00, 0x00, 0x00, 0x20, 0x3a, 0xff, 0x20, 0x01, 0x05, 0x00, 0x01, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x25, 0x20, 0x01, 0x05, 0x00, - 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x10, 0x00, - 0x29, 0xdb, 0x60, 0x00, 0x00, 0x00, 0x20, 0x01, 0x05, 0x00, 0x01, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x25, 0x02, 0x01, 0x00, 0x50, 0x56, 0x8a, - 0x22, 0x80, - ]; - let p = Packet::from_bytes(&icmpv6_destination_unreachable_packet, ENCAP_TYPE_ETH); - assert!(p.is_ok()); - let p = p.unwrap(); - - let icmpv6_packet = serde_json::to_value(&p.layers[2]).unwrap(); - assert_eq!(icmpv6_packet.get("type"), Some(&json!(16))); - assert_eq!(icmpv6_packet.get("code"), Some(&json!(0))); - assert_eq!(icmpv6_packet.get("checksum"), Some(&json!("0x29db"))); - assert_eq!( - icmpv6_packet.get("unsupported"), - Some(&json!( - "600000002001050001000000000000000000002502010050568a2280" - )) - ); + #[test] + fn parse_unsupported_icmp_type() { + let _ = layers::register_defaults(); + + let icmpv6_destination_unreachable_packet = vec![ + 0x00, 0x50, 0x56, 0x8a, 0x0f, 0xe9, 0x00, 0x50, 0x56, 0x8a, 0x22, 0x80, 0x86, 0xdd, + 0x60, 0x00, 0x00, 0x00, 0x00, 0x20, 0x3a, 0xff, 0x20, 0x01, 0x05, 0x00, 0x01, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x25, 0x20, 0x01, 0x05, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x10, 0x00, + 0x29, 0xdb, 0x60, 0x00, 0x00, 0x00, 0x20, 0x01, 0x05, 0x00, 0x01, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x25, 0x02, 0x01, 0x00, 0x50, 0x56, 0x8a, + 0x22, 0x80, + ]; + let p = Packet::from_bytes(&icmpv6_destination_unreachable_packet, ENCAP_TYPE_ETH); + assert!(p.is_ok()); + let p = p.unwrap(); + + let icmpv6_packet = serde_json::to_value(&p.layers[2]).unwrap(); + assert_eq!(icmpv6_packet.get("type"), Some(&json!(16))); + assert_eq!(icmpv6_packet.get("code"), Some(&json!(0))); + assert_eq!(icmpv6_packet.get("checksum"), Some(&json!("0x29db"))); + assert_eq!( + icmpv6_packet.get("unsupported"), + Some(&json!( + "600000002001050001000000000000000000002502010050568a2280" + )) + ); + } } } diff --git a/src/layers/ipv4.rs b/src/layers/ipv4.rs index 470ec4e..7272fa3 100644 --- a/src/layers/ipv4.rs +++ b/src/layers/ipv4.rs @@ -379,63 +379,65 @@ mod tests { assert_eq!(ipv4.options.as_slice(), options); } - #[test] - fn parse_ipv4_option_packet_1() { - let ipv4_packet = hex::decode("08003715e6bc00123f4a33d208004600004caa1d0000801111caac1f1336ac1f1349010101003e3000a10034fa4e302a02010004067075626c6963a01d02012a02010002010030123010060c2b060102012b0e01010601050500").unwrap(); - let options = [ - super::IPOption::NOP, - super::IPOption::NOP, - super::IPOption::NOP, - super::IPOption::EOOL, - ]; - - test_options(&ipv4_packet[14..], &options); - } + wasm_tests! { + #[test] + fn parse_ipv4_option_packet_1() { + let ipv4_packet = hex::decode("08003715e6bc00123f4a33d208004600004caa1d0000801111caac1f1336ac1f1349010101003e3000a10034fa4e302a02010004067075626c6963a01d02012a02010002010030123010060c2b060102012b0e01010601050500").unwrap(); + let options = [ + super::IPOption::NOP, + super::IPOption::NOP, + super::IPOption::NOP, + super::IPOption::EOOL, + ]; + + test_options(&ipv4_packet[14..], &options); + } - #[test] - fn parse_ipv4_option_packet_2() { - let ipv4_packet = hex::decode("08003715e6bc00123f4a33d208004600004caa1d0000801111caac1f1336ac1f13495f03ff003e3000a10034fa4e302a02010004067075626c6963a01d02012a02010002010030123010060c2b060102012b0e01010601050500").unwrap(); - let options = [ - super::IPOption::Other { - value: 0x5f, - len: 3, - data: vec![0xff], - }, - super::IPOption::EOOL, - ]; - - test_options(&ipv4_packet[14..], &options); - } + #[test] + fn parse_ipv4_option_packet_2() { + let ipv4_packet = hex::decode("08003715e6bc00123f4a33d208004600004caa1d0000801111caac1f1336ac1f13495f03ff003e3000a10034fa4e302a02010004067075626c6963a01d02012a02010002010030123010060c2b060102012b0e01010601050500").unwrap(); + let options = [ + super::IPOption::Other { + value: 0x5f, + len: 3, + data: vec![0xff], + }, + super::IPOption::EOOL, + ]; + + test_options(&ipv4_packet[14..], &options); + } - #[test] - fn parse_ipv4_option_packet_3() { - let ipv4_packet = hex::decode("08003715e6bc00123f4a33d2080047000050aa1d0000801111caac1f1336ac1f1349070707deadbeef003e3000a10034fa4e302a02010004067075626c6963a01d02012a02010002010030123010060c2b060102012b0e01010601050500").unwrap(); - let options = [ - super::IPOption::RR { - len: 7, - ptr: 7, - route: vec![[0xde, 0xad, 0xbe, 0xef].into()], - }, - super::IPOption::EOOL, - ]; - - test_options(&ipv4_packet[14..], &options); - } + #[test] + fn parse_ipv4_option_packet_3() { + let ipv4_packet = hex::decode("08003715e6bc00123f4a33d2080047000050aa1d0000801111caac1f1336ac1f1349070707deadbeef003e3000a10034fa4e302a02010004067075626c6963a01d02012a02010002010030123010060c2b060102012b0e01010601050500").unwrap(); + let options = [ + super::IPOption::RR { + len: 7, + ptr: 7, + route: vec![[0xde, 0xad, 0xbe, 0xef].into()], + }, + super::IPOption::EOOL, + ]; + + test_options(&ipv4_packet[14..], &options); + } - #[test] - fn parse_ipv4_option_packet_4() { - let ipv4_packet = hex::decode("08003715e6bc00123f4a33d2080047000050aa1d0000801111caac1f1336ac1f13490b04dead0c04beef3e3000a10034fa4e302a02010004067075626c6963a01d02012a02010002010030123010060c2b060102012b0e01010601050500").unwrap(); - let options = [ - super::IPOption::MTUP { - len: 4, - value: 57005, - }, - super::IPOption::MTUR { - len: 4, - value: 48879, - }, - ]; - - test_options(&ipv4_packet[14..], &options); + #[test] + fn parse_ipv4_option_packet_4() { + let ipv4_packet = hex::decode("08003715e6bc00123f4a33d2080047000050aa1d0000801111caac1f1336ac1f13490b04dead0c04beef3e3000a10034fa4e302a02010004067075626c6963a01d02012a02010002010030123010060c2b060102012b0e01010601050500").unwrap(); + let options = [ + super::IPOption::MTUP { + len: 4, + value: 57005, + }, + super::IPOption::MTUR { + len: 4, + value: 48879, + }, + ]; + + test_options(&ipv4_packet[14..], &options); + } } } diff --git a/src/layers/mpls.rs b/src/layers/mpls.rs index 0b0e62c..bf6f03f 100644 --- a/src/layers/mpls.rs +++ b/src/layers/mpls.rs @@ -99,46 +99,48 @@ mod tests { use crate::layers; use crate::{Layer, Packet, ENCAP_TYPE_ETH}; - #[test] - fn parse_valid_mpls_packet() { - let _ = layers::register_defaults(); - - let mpls_packet = vec![ - 0x00, 0x30, 0x96, 0xe6, 0xfc, 0x39, 0x00, 0x30, 0x96, 0x05, 0x28, 0x38, 0x88, 0x47, - 0x00, 0x01, 0xdd, 0xff, 0x45, 0xc0, 0x00, 0x28, 0x00, 0x03, 0x00, 0x00, 0xff, 0x06, - 0xa4, 0xe8, 0x0a, 0x01, 0x02, 0x01, 0x0a, 0x22, 0x00, 0x01, 0x2a, 0xf9, 0x00, 0x17, - 0x98, 0x32, 0x10, 0x05, 0x8d, 0xd5, 0x8e, 0xa5, 0x50, 0x10, 0x10, 0x20, 0x99, 0xcd, - 0x00, 0x00, 0x00, 0x00, - ]; - - let mut mpls: Box = Box::new(super::MPLS::default()); - - let p = mpls.decode_bytes(&mpls_packet[28..]); - assert!(p.is_ok(), "{:#?}", mpls); - } + wasm_tests! { + #[test] + fn parse_valid_mpls_packet() { + let _ = layers::register_defaults(); + + let mpls_packet = vec![ + 0x00, 0x30, 0x96, 0xe6, 0xfc, 0x39, 0x00, 0x30, 0x96, 0x05, 0x28, 0x38, 0x88, 0x47, + 0x00, 0x01, 0xdd, 0xff, 0x45, 0xc0, 0x00, 0x28, 0x00, 0x03, 0x00, 0x00, 0xff, 0x06, + 0xa4, 0xe8, 0x0a, 0x01, 0x02, 0x01, 0x0a, 0x22, 0x00, 0x01, 0x2a, 0xf9, 0x00, 0x17, + 0x98, 0x32, 0x10, 0x05, 0x8d, 0xd5, 0x8e, 0xa5, 0x50, 0x10, 0x10, 0x20, 0x99, 0xcd, + 0x00, 0x00, 0x00, 0x00, + ]; + + let mut mpls: Box = Box::new(super::MPLS::default()); + + let p = mpls.decode_bytes(&mpls_packet[28..]); + assert!(p.is_ok(), "{:#?}", mpls); + } - #[test] - fn test_mpls_parse_regression() { - let _ = layers::register_defaults(); - - // testPacketMPLS is the packet: - // Ethernet II, Src: Cisco_05:28:38 (00:30:96:05:28:38), Dst: Cisco_e6:fc:39 (00:30:96:e6:fc:39) - // 0x0000 00 30 96 e6 fc 39 00 30 96 05 28 38 88 47 00 01 .0...9.0..(8.G.. - // 0x0010 dd ff 45 c0 00 28 00 03 00 00 ff 06 a4 e8 0a 01 ..E..(.......... - // 0x0020 02 01 0a 22 00 01 2a f9 00 17 98 32 10 05 8d d5 ..."..*....2.... - // 0x0030 8e a5 50 10 10 20 99 cd 00 00 00 00 ..P.. ...... - - let test_packet_mpls = vec![ - 0x00, 0x30, 0x96, 0xe6, 0xfc, 0x39, 0x00, 0x30, 0x96, 0x05, 0x28, 0x38, 0x88, 0x47, - 0x00, 0x01, 0xdd, 0xff, 0x45, 0xc0, 0x00, 0x28, 0x00, 0x03, 0x00, 0x00, 0xff, 0x06, - 0xa4, 0xe8, 0x0a, 0x01, 0x02, 0x01, 0x0a, 0x22, 0x00, 0x01, 0x2a, 0xf9, 0x00, 0x17, - 0x98, 0x32, 0x10, 0x05, 0x8d, 0xd5, 0x8e, 0xa5, 0x50, 0x10, 0x10, 0x20, 0x99, 0xcd, - 0x00, 0x00, 0x00, 0x00, - ]; - - let p = Packet::from_bytes(&test_packet_mpls, ENCAP_TYPE_ETH); - assert!(p.is_ok()); - let p = p.unwrap(); - assert!(p.layers.len() == 4, "{:#?}", p); + #[test] + fn test_mpls_parse_regression() { + let _ = layers::register_defaults(); + + // testPacketMPLS is the packet: + // Ethernet II, Src: Cisco_05:28:38 (00:30:96:05:28:38), Dst: Cisco_e6:fc:39 (00:30:96:e6:fc:39) + // 0x0000 00 30 96 e6 fc 39 00 30 96 05 28 38 88 47 00 01 .0...9.0..(8.G.. + // 0x0010 dd ff 45 c0 00 28 00 03 00 00 ff 06 a4 e8 0a 01 ..E..(.......... + // 0x0020 02 01 0a 22 00 01 2a f9 00 17 98 32 10 05 8d d5 ..."..*....2.... + // 0x0030 8e a5 50 10 10 20 99 cd 00 00 00 00 ..P.. ...... + + let test_packet_mpls = vec![ + 0x00, 0x30, 0x96, 0xe6, 0xfc, 0x39, 0x00, 0x30, 0x96, 0x05, 0x28, 0x38, 0x88, 0x47, + 0x00, 0x01, 0xdd, 0xff, 0x45, 0xc0, 0x00, 0x28, 0x00, 0x03, 0x00, 0x00, 0xff, 0x06, + 0xa4, 0xe8, 0x0a, 0x01, 0x02, 0x01, 0x0a, 0x22, 0x00, 0x01, 0x2a, 0xf9, 0x00, 0x17, + 0x98, 0x32, 0x10, 0x05, 0x8d, 0xd5, 0x8e, 0xa5, 0x50, 0x10, 0x10, 0x20, 0x99, 0xcd, + 0x00, 0x00, 0x00, 0x00, + ]; + + let p = Packet::from_bytes(&test_packet_mpls, ENCAP_TYPE_ETH); + assert!(p.is_ok()); + let p = p.unwrap(); + assert!(p.layers.len() == 4, "{:#?}", p); + } } } diff --git a/src/layers/sctp.rs b/src/layers/sctp.rs index 9dd2e37..2c8eb51 100644 --- a/src/layers/sctp.rs +++ b/src/layers/sctp.rs @@ -2,7 +2,7 @@ use core::convert::TryInto; use std::collections::HashMap; -use std::sync::{RwLock,OnceLock}; +use std::sync::{OnceLock, RwLock}; use serde::{ser::SerializeStruct as _, Serialize, Serializer}; @@ -16,32 +16,33 @@ const UNKNOWN_CHUNK_TYPE: &str = "Unknown Chunk"; fn get_chunk_display_map() -> &'static HashMap { static CHUNK_DISPLAY_MAP: OnceLock> = OnceLock::new(); CHUNK_DISPLAY_MAP.get_or_init(|| { - // A Map of Chunk Type to Display String - let mut m = HashMap::new(); - m.insert(0, "DATA Chunk"); - m.insert(1, "INIT Chunk"); - m.insert(2, "INIT-ACK Chunk"); - m.insert(3, "SACK Chunk"); - m.insert(4, "HEARTBEAT Chunk"); - m.insert(5, "HEARTBEAT-ACK Chunk"); - m.insert(6, "ABORT Chunk"); - m.insert(7, "SHUTDOWN Chunk"); - m.insert(8, "SHUTDOWN-ACK Chunk"); - m.insert(9, "ERROR Chunk"); - m.insert(10, "COOKIE-ECHO Chunk"); - m.insert(11, "COOKIE-ACK Chunk"); - m.insert(12, "ECNE Chunk"); - m.insert(13, "CWR Chunk"); - m.insert(14, "SHUTDOWN_COMPLETE Chunk"); - m.insert(15, "AUTH Chunk"); - m.insert(64, "I-DATA Chunk"); - m.insert(128, "ASCONF-ACK Chunk"); - m.insert(130, "RE-CONFIG Chunk"); - m.insert(132, "PAD Chunk"); - m.insert(192, "FORWARD-TSN Chunk"); - m.insert(193, "ASCONF Chunk"); - m.insert(195, "I-FORWARD-TSN Chunk"); - m}) + // A Map of Chunk Type to Display String + let mut m = HashMap::new(); + m.insert(0, "DATA Chunk"); + m.insert(1, "INIT Chunk"); + m.insert(2, "INIT-ACK Chunk"); + m.insert(3, "SACK Chunk"); + m.insert(4, "HEARTBEAT Chunk"); + m.insert(5, "HEARTBEAT-ACK Chunk"); + m.insert(6, "ABORT Chunk"); + m.insert(7, "SHUTDOWN Chunk"); + m.insert(8, "SHUTDOWN-ACK Chunk"); + m.insert(9, "ERROR Chunk"); + m.insert(10, "COOKIE-ECHO Chunk"); + m.insert(11, "COOKIE-ACK Chunk"); + m.insert(12, "ECNE Chunk"); + m.insert(13, "CWR Chunk"); + m.insert(14, "SHUTDOWN_COMPLETE Chunk"); + m.insert(15, "AUTH Chunk"); + m.insert(64, "I-DATA Chunk"); + m.insert(128, "ASCONF-ACK Chunk"); + m.insert(130, "RE-CONFIG Chunk"); + m.insert(132, "PAD Chunk"); + m.insert(192, "FORWARD-TSN Chunk"); + m.insert(193, "ASCONF Chunk"); + m.insert(195, "I-FORWARD-TSN Chunk"); + m + }) } fn get_sctp_protocols_map() -> &'static RwLock> { @@ -312,18 +313,20 @@ mod tests { use crate::layers; use crate::{Packet, ENCAP_TYPE_ETH}; - #[test] - fn test_basic_sctp_decode() { - let _ = layers::register_defaults(); - let array = hex::decode( -"00005096523a0026cb39f4c00800450000a8da490000fa844bf6585206860aad300d189f0b5add68d33d0f7373ab030000100629beaa0000fa000000000000030028d42b4897000000050000000301000202000000180012000800000a43000600080000045600030028d42b4898000000060000000301000202000000180012000800000a42000600080000045600030028d42b4899000000070000000301000202000000180012000800000fa20006000800000456"); - assert!(array.is_ok()); + wasm_tests! { + #[test] + fn test_basic_sctp_decode() { + let _ = layers::register_defaults(); + let array = hex::decode( + "00005096523a0026cb39f4c00800450000a8da490000fa844bf6585206860aad300d189f0b5add68d33d0f7373ab030000100629beaa0000fa000000000000030028d42b4897000000050000000301000202000000180012000800000a43000600080000045600030028d42b4898000000060000000301000202000000180012000800000a42000600080000045600030028d42b4899000000070000000301000202000000180012000800000fa20006000800000456"); + assert!(array.is_ok()); - let array = array.unwrap(); - let p = Packet::from_bytes(&array, ENCAP_TYPE_ETH); - assert!(p.is_ok(), "{:?}", p.err()); + let array = array.unwrap(); + let p = Packet::from_bytes(&array, ENCAP_TYPE_ETH); + assert!(p.is_ok(), "{:?}", p.err()); - let p = p.unwrap(); - assert!(p.layers.len() == 3, "{:#?}", p); + let p = p.unwrap(); + assert!(p.layers.len() == 3, "{:#?}", p); + } } } diff --git a/src/layers/vxlan.rs b/src/layers/vxlan.rs index a10ab89..a315121 100644 --- a/src/layers/vxlan.rs +++ b/src/layers/vxlan.rs @@ -70,30 +70,32 @@ mod tests { use scalpel::layers; - #[test] - fn parse_valid_vxlan_packet() { - let _ = layers::register_defaults(); - - let vxlan_packet = hex::decode("00005e00531100005e005303080045000096002c4000fd1177250202020201010101504f12b500820000080000000013940000005e00536500005e00536608004500006400280000ff012155c0a80c66c0a80c6500005c27000c000000000000007c299babcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd") - .unwrap(); - - let p = scalpel::Packet::from_bytes(&vxlan_packet, scalpel::ENCAP_TYPE_ETH); - assert!(p.is_ok()); - - let p = p.unwrap(); - - let layer_type_names = [ - "Ethernet", "IPv4", "UDP", "VXLAN", "Ethernet", - "IPv4", - // TODO: Uncomment when ICMP gets supported - // "ICMP", - ]; - p.layers - .iter() - .map(|layer| layer.name()) - .zip(layer_type_names) - .for_each(|(layer_name, type_name)| { - assert_eq!(layer_name, type_name, "{}", layer_name); - }); + wasm_tests! { + #[test] + fn parse_valid_vxlan_packet() { + let _ = layers::register_defaults(); + + let vxlan_packet = hex::decode("00005e00531100005e005303080045000096002c4000fd1177250202020201010101504f12b500820000080000000013940000005e00536500005e00536608004500006400280000ff012155c0a80c66c0a80c6500005c27000c000000000000007c299babcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd") + .unwrap(); + + let p = scalpel::Packet::from_bytes(&vxlan_packet, scalpel::ENCAP_TYPE_ETH); + assert!(p.is_ok()); + + let p = p.unwrap(); + + let layer_type_names = [ + "Ethernet", "IPv4", "UDP", "VXLAN", "Ethernet", + "IPv4", + // TODO: Uncomment when ICMP gets supported + // "ICMP", + ]; + p.layers + .iter() + .map(|layer| layer.name()) + .zip(layer_type_names) + .for_each(|(layer_name, type_name)| { + assert_eq!(layer_name, type_name, "{}", layer_name); + }); + } } } diff --git a/src/types/ipaddr.rs b/src/types/ipaddr.rs index a773e59..4b3e5df 100644 --- a/src/types/ipaddr.rs +++ b/src/types/ipaddr.rs @@ -259,59 +259,61 @@ mod tests { use super::*; use std::net::Ipv6Addr; - #[test] - fn ipv6_addr_tests() { - struct IPv6AddressTestCase<'s> { - input: &'s str, - valid: bool, - } + wasm_tests! { + #[test] + fn ipv6_addr_tests() { + struct IPv6AddressTestCase<'s> { + input: &'s str, + valid: bool, + } - let test_cases = vec![ - IPv6AddressTestCase { - input: "fe80::1", - valid: true, - }, - IPv6AddressTestCase { - input: "::", - valid: true, - }, - IPv6AddressTestCase { - input: "::1", - valid: true, - }, - IPv6AddressTestCase { - input: "::ffff:0:0", - valid: true, - }, - IPv6AddressTestCase { - input: "::ffff:ff:ff", - valid: true, - }, - IPv6AddressTestCase { - input: "64:ff9b::", - valid: true, - }, - IPv6AddressTestCase { - input: "2a03:2880:f12f:183:face:b00c:0:25de", - valid: true, - }, - IPv6AddressTestCase { - input: "1:0:1:0:1:0:1:0", - valid: true, - }, - IPv6AddressTestCase { - input: "2404:6800:4003:c04::1b", - valid: true, - }, - ]; - - for test_case in test_cases { - let ipv6: Result = - test_case.input.parse::().unwrap().segments()[..].try_into(); - assert!(ipv6.is_ok(), "{}", test_case.valid); - if test_case.valid { - let ipv6 = ipv6.unwrap(); - assert_eq!(test_case.input, format!("{}", ipv6)); + let test_cases = vec![ + IPv6AddressTestCase { + input: "fe80::1", + valid: true, + }, + IPv6AddressTestCase { + input: "::", + valid: true, + }, + IPv6AddressTestCase { + input: "::1", + valid: true, + }, + IPv6AddressTestCase { + input: "::ffff:0:0", + valid: true, + }, + IPv6AddressTestCase { + input: "::ffff:ff:ff", + valid: true, + }, + IPv6AddressTestCase { + input: "64:ff9b::", + valid: true, + }, + IPv6AddressTestCase { + input: "2a03:2880:f12f:183:face:b00c:0:25de", + valid: true, + }, + IPv6AddressTestCase { + input: "1:0:1:0:1:0:1:0", + valid: true, + }, + IPv6AddressTestCase { + input: "2404:6800:4003:c04::1b", + valid: true, + }, + ]; + + for test_case in test_cases { + let ipv6: Result = + test_case.input.parse::().unwrap().segments()[..].try_into(); + assert!(ipv6.is_ok(), "{}", test_case.valid); + if test_case.valid { + let ipv6 = ipv6.unwrap(); + assert_eq!(test_case.input, format!("{}", ipv6)); + } } } } diff --git a/src/types/macaddr.rs b/src/types/macaddr.rs index beb0dfd..60836ae 100644 --- a/src/types/macaddr.rs +++ b/src/types/macaddr.rs @@ -87,29 +87,31 @@ mod tests { use crate::errors::Error as CrateError; use core::convert::TryInto; - #[test] - fn byte_array_too_small_fail() { - let mac_address: Result = [00u8, 01u8, 02u8][..].try_into(); - assert!(mac_address.is_err()); - assert!( - mac_address.err().unwrap() == CrateError::ParseError("MacAddress: 000102".to_string()) - ); - } + wasm_tests! { + #[test] + fn byte_array_too_small_fail() { + let mac_address: Result = [00u8, 01u8, 02u8][..].try_into(); + assert!(mac_address.is_err()); + assert!( + mac_address.err().unwrap() == CrateError::ParseError("MacAddress: 000102".to_string()) + ); + } - #[test] - fn byte_array_too_large_fail() { - let mac_address: Result = [00u8; 10].as_ref().try_into(); - assert!(mac_address.is_err()); - assert!( - mac_address.err().unwrap() - == CrateError::ParseError("MacAddress: 00000000000000000000".to_string()) - ); - } + #[test] + fn byte_array_too_large_fail() { + let mac_address: Result = [00u8; 10].as_ref().try_into(); + assert!(mac_address.is_err()); + assert!( + mac_address.err().unwrap() + == CrateError::ParseError("MacAddress: 00000000000000000000".to_string()) + ); + } - #[test] - fn str_always_fail() { - let mac_address: Result = "".try_into(); - assert!(mac_address.is_err()); - assert!(mac_address.err().unwrap() == CrateError::ParseError("MacAddress: ".to_string())); + #[test] + fn str_always_fail() { + let mac_address: Result = "".try_into(); + assert!(mac_address.is_err()); + assert!(mac_address.err().unwrap() == CrateError::ParseError("MacAddress: ".to_string())); + } } }