Skip to content

Commit

Permalink
Added tests for different layers inside wasm_tests!
Browse files Browse the repository at this point in the history
These tests are run for `wasm` targets as well.

Signed-off-by: Abhijit Gadgil <gabhijit@iitbombay.org>
  • Loading branch information
gabhijit committed Jun 27, 2024
1 parent 29f2bec commit 38ad3b9
Show file tree
Hide file tree
Showing 10 changed files with 657 additions and 634 deletions.
26 changes: 14 additions & 12 deletions src/layers/arp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
138 changes: 71 additions & 67 deletions src/layers/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<dyn Layer> = 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<dyn Layer> = 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);
}
}
}
Loading

0 comments on commit 38ad3b9

Please sign in to comment.