Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/capture/activated/active.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ mod tests {
.return_once(|_, _, _| 0);

let result = capture.sendpacket(buffer);
assert!(result.is_ok());
result.unwrap();

let ctx = pcap_sendpacket_context();
ctx.checkpoint();
Expand All @@ -95,7 +95,7 @@ mod tests {
let _err = geterr_expect(pcap);

let result = capture.sendpacket(buffer);
assert!(result.is_err());
result.unwrap_err();
}

#[test]
Expand Down Expand Up @@ -135,7 +135,7 @@ mod tests {
.return_once(|_, _, _| -1);

let result = capture.setnonblock();
assert!(result.is_err());
result.unwrap_err();
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions src/capture/activated/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ mod tests {
.return_once(|_| {});

let result = Capture::dead(Linktype::ETHERNET);
assert!(result.is_ok());
result.unwrap();
}

#[test]
Expand All @@ -80,6 +80,6 @@ mod tests {
.return_once(|_| {});

let result = Capture::dead_with_precision(Linktype::ETHERNET, Precision::Nano);
assert!(result.is_ok());
result.unwrap();
}
}
36 changes: 19 additions & 17 deletions src/capture/activated/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ impl<T: Activated> From<Capture<T>> for Capture<dyn Activated> {
}

/// Abstraction for writing pcap savefiles, which can be read afterwards via `Capture::from_file()`.
#[derive(Debug)]
pub struct Savefile {
handle: NonNull<raw::pcap_dumper_t>,
}
Expand Down Expand Up @@ -369,6 +370,7 @@ impl Drop for Savefile {
#[repr(transparent)]
pub struct BpfInstruction(raw::bpf_insn);
#[repr(transparent)]
#[derive(Debug)]
pub struct BpfProgram(raw::bpf_program);

impl BpfProgram {
Expand Down Expand Up @@ -513,7 +515,7 @@ mod tests {
let _err = geterr_expect(pcap);

let result = capture.list_datalinks();
assert!(result.is_err());
result.unwrap_err();

let mut datalinks: [i32; 4] = [0, 1, 2, 3];
let links: *mut i32 = datalinks.as_mut_ptr();
Expand Down Expand Up @@ -555,7 +557,7 @@ mod tests {
.return_once(|_, _| 0);

let result = capture.set_datalink(Linktype::ETHERNET);
assert!(result.is_ok());
result.unwrap();

let ctx = raw::pcap_set_datalink_context();
ctx.checkpoint();
Expand All @@ -566,7 +568,7 @@ mod tests {
let _err = geterr_expect(pcap);

let result = capture.set_datalink(Linktype::ETHERNET);
assert!(result.is_err());
result.unwrap_err();
}

#[test]
Expand Down Expand Up @@ -637,7 +639,7 @@ mod tests {
.return_once(|_| {});

let result = capture.savefile("path/to/nowhere");
assert!(result.is_ok());
result.unwrap();
}

#[test]
Expand Down Expand Up @@ -665,7 +667,7 @@ mod tests {
.return_once(|_| {});

let result = capture.savefile_append("path/to/nowhere");
assert!(result.is_ok());
result.unwrap();
}

#[test]
Expand All @@ -686,7 +688,7 @@ mod tests {
let _err = geterr_expect(pcap);

let result = capture.savefile("path/to/nowhere");
assert!(result.is_err());
result.unwrap_err();
}

#[test]
Expand All @@ -708,7 +710,7 @@ mod tests {
let _err = geterr_expect(pcap);

let result = capture.savefile_append("path/to/nowhere");
assert!(result.is_err());
result.unwrap_err();
}

#[test]
Expand Down Expand Up @@ -740,7 +742,7 @@ mod tests {
.return_once(|_| 0);

let result = savefile.flush();
assert!(result.is_ok());
result.unwrap();

let ctx = raw::pcap_dump_flush_context();
ctx.checkpoint();
Expand All @@ -749,7 +751,7 @@ mod tests {
.return_once(|_| -1);

let result = savefile.flush();
assert!(result.is_err());
result.unwrap_err();
}

#[test]
Expand All @@ -768,7 +770,7 @@ mod tests {
.return_once(|_, _| 0);

let result = capture.direction(Direction::Out);
assert!(result.is_ok());
result.unwrap();

let ctx = raw::pcap_setdirection_context();
ctx.checkpoint();
Expand All @@ -779,7 +781,7 @@ mod tests {
let _err = geterr_expect(pcap);

let result = capture.direction(Direction::Out);
assert!(result.is_err());
result.unwrap_err();

// For code coverage of the derive line.
assert_ne!(Direction::In, Direction::InOut);
Expand Down Expand Up @@ -840,7 +842,7 @@ mod tests {
let _err = geterr_expect(pcap);

let result = capture.next_packet();
assert!(result.is_err());
result.unwrap_err();
}

#[test]
Expand Down Expand Up @@ -883,7 +885,7 @@ mod tests {
ctx.expect().return_once(|_| {});

let result = capture.compile("some bpf program", false);
assert!(result.is_err());
result.unwrap_err();

let ctx = raw::pcap_compile_context();
ctx.checkpoint();
Expand All @@ -896,7 +898,7 @@ mod tests {
ctx.expect().return_once(|_| {});

let result = capture.compile("some bpf program", false);
assert!(result.is_ok());
result.unwrap();
}

#[test]
Expand Down Expand Up @@ -925,7 +927,7 @@ mod tests {
ctx.expect().return_once(|_| {});

let result = capture.filter("some bpf program", false);
assert!(result.is_err());
result.unwrap_err();

let ctx = raw::pcap_compile_context();
ctx.checkpoint();
Expand All @@ -944,7 +946,7 @@ mod tests {
ctx.expect().return_once(|_| {});

let result = capture.compile("some bpf program", false);
assert!(result.is_ok());
result.unwrap();
}

#[test]
Expand Down Expand Up @@ -983,7 +985,7 @@ mod tests {
let _err = geterr_expect(pcap);

let result = capture.stats();
assert!(result.is_err());
result.unwrap_err();
}

#[test]
Expand Down
28 changes: 12 additions & 16 deletions src/capture/activated/offline.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
use std::path::Path;

#[cfg(not(windows))]
use std::os::unix::io::RawFd;
use std::path::Path;

#[cfg(not(windows))]
use crate::capture::activated::open_raw_fd;
#[cfg(libpcap_1_5_0)]
use crate::capture::Precision;
use crate::{
capture::{Capture, Offline},
raw, Error,
};

#[cfg(libpcap_1_5_0)]
use crate::capture::Precision;

#[cfg(not(windows))]
use crate::capture::activated::open_raw_fd;

impl Capture<Offline> {
/// Opens an offline capture handle from a pcap dump file, given a path.
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Capture<Offline>, Error> {
Capture::new_raw(path.as_ref().to_str(), |path, err| unsafe {
Capture::new_raw_with_path(path.as_ref(), |path, err| unsafe {
raw::pcap_open_offline(path, err)
})
}
Expand All @@ -29,7 +26,7 @@ impl Capture<Offline> {
path: P,
precision: Precision,
) -> Result<Capture<Offline>, Error> {
Capture::new_raw(path.as_ref().to_str(), |path, err| unsafe {
Capture::new_raw_with_path(path.as_ref(), |path, err| unsafe {
raw::pcap_open_offline_with_tstamp_precision(path, precision as _, err)
})
}
Expand All @@ -42,7 +39,7 @@ impl Capture<Offline> {
#[cfg(not(windows))]
pub unsafe fn from_raw_fd(fd: RawFd) -> Result<Capture<Offline>, Error> {
open_raw_fd(fd, b'r')
.and_then(|file| Capture::new_raw(None, |_, err| raw::pcap_fopen_offline(file, err)))
.and_then(|file| Capture::new_raw(|err| raw::pcap_fopen_offline(file, err)))
}

/// Opens an offline capture handle from a pcap dump file, given a file descriptor. Takes an
Expand All @@ -57,7 +54,7 @@ impl Capture<Offline> {
precision: Precision,
) -> Result<Capture<Offline>, Error> {
open_raw_fd(fd, b'r').and_then(|file| {
Capture::new_raw(None, |_, err| {
Capture::new_raw(|err| {
raw::pcap_fopen_offline_with_tstamp_precision(file, precision as _, err)
})
})
Expand All @@ -84,13 +81,12 @@ mod tests {
#[cfg(libpcap_1_5_0)]
use mockall::predicate;

use super::*;
use crate::{
capture::testmod::test_capture,
raw::testmod::{as_pcap_t, RAWMTX},
};

use super::*;

#[test]
fn test_from_file() {
let _m = RAWMTX.lock();
Expand All @@ -107,7 +103,7 @@ mod tests {
.return_once(|_| {});

let result = Capture::from_file("path/to/nowhere");
assert!(result.is_ok());
result.unwrap();
}

#[test]
Expand All @@ -129,7 +125,7 @@ mod tests {
.return_once(|_| {});

let result = Capture::from_file_with_precision("path/to/nowhere", Precision::Nano);
assert!(result.is_ok());
result.unwrap();
}

#[test]
Expand Down
12 changes: 6 additions & 6 deletions src/capture/inactive.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::mem;
use std::{mem, path::Path};

use crate::{
capture::{Active, Capture, Inactive},
Expand Down Expand Up @@ -32,7 +32,7 @@ impl Capture<Inactive> {
/// ```
pub fn from_device<D: Into<Device>>(device: D) -> Result<Capture<Inactive>, Error> {
let device: Device = device.into();
Capture::new_raw(Some(&device.name), |name, err| unsafe {
Capture::new_raw_with_path(Path::new(&device.name), |name, err| unsafe {
raw::pcap_create(name, err)
})
}
Expand Down Expand Up @@ -206,7 +206,7 @@ mod tests {
.return_once(|_| {});

let result = Capture::from_device("some_device");
assert!(result.is_ok());
result.unwrap();
}

#[test]
Expand All @@ -217,7 +217,7 @@ mod tests {
ctx.expect().return_once_st(|_, _| std::ptr::null_mut());

let result = Capture::from_device("some_device");
assert!(result.is_err());
result.unwrap_err();
}

#[test]
Expand All @@ -236,7 +236,7 @@ mod tests {
.return_once(|_| 0);

let result = capture.open();
assert!(result.is_ok());
result.unwrap();
}

#[test]
Expand All @@ -257,7 +257,7 @@ mod tests {
let _err = geterr_expect(pcap);

let result = capture.open();
assert!(result.is_err());
result.unwrap_err();
}

#[test]
Expand Down
Loading
Loading