Skip to content

Commit

Permalink
Merge pull request #7 from 0xphen/feat/deep-parser
Browse files Browse the repository at this point in the history
Refactor tests
  • Loading branch information
0xphen authored Oct 28, 2023
2 parents eb190fc + ffd20ee commit c2050e5
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/aggregator/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@

pub trait Parser {
fn parse_next_layer(&self) -> Option<Box<dyn Parser>>;
}
2 changes: 1 addition & 1 deletion src/parsers/icmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl IcmpPacket {
/// # Returns:
///
/// * `Result<Self, ParserError>` - An IcmpPacket instance or a ParserError.
pub fn new(packets: &[u8]) -> Result<Self, ParserError> {
pub fn from_bytes(packets: &[u8]) -> Result<Self, ParserError> {
if packets.len() < DATA_OFFSET_OR_MIN_SIZE {
return Err(ParserError::InvalidLength);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/ethernet_frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ struct EthernetFrameValues {
}

#[test]
fn can_create_ethernet_frame_without_qtag() {
fn can_parse_ethernet_frame_without_qtag() {
let frame = generate_mock_frame(
DEFAULT_DEST_MAC,
DEFAULT_SRC_MAC,
Expand Down Expand Up @@ -98,7 +98,7 @@ fn can_create_ethernet_frame_without_qtag() {
}

#[test]
fn can_create_ethernet_frame_with_qtag() {
fn can_parse_ethernet_frame_with_qtag() {
let frame = generate_mock_frame(
DEFAULT_DEST_MAC,
DEFAULT_SRC_MAC,
Expand Down
18 changes: 18 additions & 0 deletions tests/icmp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use net_sift::parsers::{errors::ParserError, icmp::IcmpPacket};

const DEFAULT_PACKET: [u8; 12] = [8, 12, 94, 4, 0, 0, 0, 0, 12, 10, 0, 5];

#[test]
fn can_decode_icmp_packet() {
let icmp_packet = IcmpPacket::from_bytes(&DEFAULT_PACKET).unwrap();
assert_eq!(icmp_packet.icmp_type, 8);
assert_eq!(icmp_packet.icmp_code, 12);
assert_eq!(icmp_packet.checksum, 24068);
assert_eq!(icmp_packet.data, [12, 10, 0, 5])
}

#[test]
fn fails_if_packet_is_malformed() {
let result = IcmpPacket::from_bytes(&[9, 12, 34, 5]);
assert!(matches!(result, Err(ParserError::InvalidLength)))
}
2 changes: 1 addition & 1 deletion tests/ipv4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ fn can_create_ipv4_without_options() {
}

#[test]
fn can_create_ipv4_with_options() {
fn can_parse_ipv4_packet_with_options() {
let packets = generate_mock_packet(
DEFAULT_VERSION_IHL_WITH_OPTIONS,
DEFAULT_TOS,
Expand Down
2 changes: 1 addition & 1 deletion tests/ipv6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ fn addr(v: &[u8; 16]) -> Ipv6Addr {
// TESTS

#[test]
fn can_create_ipv6() {
fn can_parse_ipv6_packet() {
let packets = generate_mock_packet();
let ipv6 = Ipv6Packet::from_bytes(&packets);

Expand Down
6 changes: 3 additions & 3 deletions tests/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const MOCK_MALFORMED_PACKET: [u8; 19] = [
fn generate_mock_segment(data_offset_reserved_flags_window: [u8; 4]) -> Vec<u8> {
let v = u32::from_be_bytes(data_offset_reserved_flags_window);
let l = v >> 28;
let cap: usize = ((l * 32 / 8) as usize + DEFAULT_DATA.len());
let cap: usize = (l * 32 / 8) as usize + DEFAULT_DATA.len();

let mut segment: Vec<u8> = vec![0; cap];

Expand Down Expand Up @@ -92,7 +92,7 @@ fn validate_tcp(tcp: tcp::TcpSegment, expected_tcp: TcpValues) {
// TEST

#[test]
fn can_create_tcp_without_options() {
fn can_parse_tcp_packet_without_options() {
let segment = generate_mock_segment(DEFAULT_ZERO_OPTIONS_DATA_OFFSET_RESERVED_FLAGS_WINDOW);
let tcp = tcp::TcpSegment::from_bytes(&segment).unwrap();
let data_offset =
Expand All @@ -102,7 +102,7 @@ fn can_create_tcp_without_options() {
}

#[test]
fn can_create_tcp_with_options() {
fn can_parse_tcp_packet_with_options() {
let segment = generate_mock_segment(DEFAULT_OPTIONS_DATA_OFFSET_RESERVED_FLAGS_WINDOW);
let tcp = tcp::TcpSegment::from_bytes(&segment).unwrap();
let data_offset =
Expand Down

0 comments on commit c2050e5

Please sign in to comment.