Skip to content

Commit

Permalink
Merge pull request #5 from courvoif/add_endianness_tests
Browse files Browse the repository at this point in the history
Add endianness tests
  • Loading branch information
courvoif authored Feb 20, 2018
2 parents 38f8bc9 + 844d29a commit f02050f
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ mod packet;
pub use packet::{Packet, PacketHeader};

mod pcap_header;
pub use pcap_header::{DataLink, PcapHeader};
pub use pcap_header::{DataLink, PcapHeader, Endianness};

pub mod peek_reader;

Expand Down
2 changes: 1 addition & 1 deletion src/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use byteorder::*;
use errors::*;

/// Describes a pcap packet header.
#[derive(Copy, Clone, Default, Debug)]
#[derive(Copy, Clone, Default, Debug, Eq, PartialEq)]
pub struct PacketHeader {

/// Timestamp in seconds
Expand Down
8 changes: 4 additions & 4 deletions src/pcap_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use byteorder::*;
use errors::*;

/// Struct that represents the global Pcap header of a Pcap file
#[derive(Copy, Clone, Debug)]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct PcapHeader {

/// Magic number
Expand Down Expand Up @@ -151,22 +151,22 @@ impl PcapHeader {
}

/// Represents the endianness of the global header
#[derive(Copy, Clone, Debug)]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Endianness {
Big,
Little
}

/// Represents each possible timestamp resolution of the global header
#[derive(Copy, Clone, Debug)]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum TsResolution {
MicroSecond,
NanoSecond
}

/// Represents each possible Pcap datalink
#[allow(non_camel_case_types)]
#[derive(Copy, Clone, Debug)]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum DataLink {

NULL,
Expand Down
Binary file added tests/big_endian.pcap
Binary file not shown.
File renamed without changes.
66 changes: 65 additions & 1 deletion tests/read_write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ extern crate pcap_file;

use pcap_file::{PcapReader, PcapWriter};

static DATA: &'static[u8; 1455] = include_bytes!("test_in.pcap");
static DATA: &'static[u8; 1455] = include_bytes!("little_endian.pcap");

#[test]
fn read() {
Expand Down Expand Up @@ -39,4 +39,68 @@ fn read_write() {
out = pcap_writer.into_writer();

assert_eq!(&DATA[..], &out[..]);
}

#[test]
fn big_endian() {

let data = include_bytes!("big_endian.pcap");

//Global header test
let mut pcap_reader = PcapReader::new(&data[..]).unwrap();
let header = pcap_file::PcapHeader {
magic_number: 0xa1b2c3d4,
version_major: 2,
version_minor: 4,
ts_correction: 0,
ts_accuracy: 0,
snaplen: 0xffff,
datalink: pcap_file::DataLink::ETHERNET,
};

assert_eq!(pcap_reader.header, header);
assert_eq!(pcap_reader.header.endianness(), pcap_file::Endianness::Big);

//Packet header test
let packet = pcap_reader.next().unwrap().unwrap();
let pkt_hdr = pcap_file::PacketHeader {
ts_sec: 0x4fa11b29,
ts_usec: 0x00025436,
incl_len: 0x62,
orig_len: 0x62,
};

assert_eq!(packet.header, pkt_hdr);
}

#[test]
fn little_endian() {

let data = include_bytes!("little_endian.pcap");

//Global header test
let mut pcap_reader = PcapReader::new(&data[..]).unwrap();
let header = pcap_file::PcapHeader {
magic_number: 0xd4c3b2a1,
version_major: 2,
version_minor: 4,
ts_correction: 0,
ts_accuracy: 0,
snaplen: 0x1000,
datalink: pcap_file::DataLink::ETHERNET,
};

assert_eq!(pcap_reader.header, header);
assert_eq!(pcap_reader.header.endianness(), pcap_file::Endianness::Little);

//Packet header test
let packet = pcap_reader.next().unwrap().unwrap();
let pkt_hdr = pcap_file::PacketHeader {
ts_sec: 0x4f633248,
ts_usec: 0x0,
incl_len: 0x75,
orig_len: 0x75,
};

assert_eq!(packet.header, pkt_hdr);
}

0 comments on commit f02050f

Please sign in to comment.