Skip to content

Commit 46dd0bd

Browse files
committed
new_raw will now use UTF-16 for path name
1 parent b2fe759 commit 46dd0bd

File tree

15 files changed

+166
-81
lines changed

15 files changed

+166
-81
lines changed

src/capture/activated/active.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ mod tests {
8484
.return_once(|_, _, _| 0);
8585

8686
let result = capture.sendpacket(buffer);
87-
assert!(result.is_ok());
87+
result.unwrap();
8888

8989
let ctx = pcap_sendpacket_context();
9090
ctx.checkpoint();
@@ -95,7 +95,7 @@ mod tests {
9595
let _err = geterr_expect(pcap);
9696

9797
let result = capture.sendpacket(buffer);
98-
assert!(result.is_err());
98+
result.unwrap_err();
9999
}
100100

101101
#[test]
@@ -135,7 +135,7 @@ mod tests {
135135
.return_once(|_, _, _| -1);
136136

137137
let result = capture.setnonblock();
138-
assert!(result.is_err());
138+
result.unwrap_err();
139139
}
140140

141141
#[test]

src/capture/activated/dead.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ mod tests {
5858
.return_once(|_| {});
5959

6060
let result = Capture::dead(Linktype::ETHERNET);
61-
assert!(result.is_ok());
61+
result.unwrap();
6262
}
6363

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

8282
let result = Capture::dead_with_precision(Linktype::ETHERNET, Precision::Nano);
83-
assert!(result.is_ok());
83+
result.unwrap();
8484
}
8585
}

src/capture/activated/mod.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ impl<T: Activated> From<Capture<T>> for Capture<dyn Activated> {
323323
}
324324

325325
/// Abstraction for writing pcap savefiles, which can be read afterwards via `Capture::from_file()`.
326+
#[derive(Debug)]
326327
pub struct Savefile {
327328
handle: NonNull<raw::pcap_dumper_t>,
328329
}
@@ -369,6 +370,7 @@ impl Drop for Savefile {
369370
#[repr(transparent)]
370371
pub struct BpfInstruction(raw::bpf_insn);
371372
#[repr(transparent)]
373+
#[derive(Debug)]
372374
pub struct BpfProgram(raw::bpf_program);
373375

374376
impl BpfProgram {
@@ -513,7 +515,7 @@ mod tests {
513515
let _err = geterr_expect(pcap);
514516

515517
let result = capture.list_datalinks();
516-
assert!(result.is_err());
518+
result.unwrap_err();
517519

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

557559
let result = capture.set_datalink(Linktype::ETHERNET);
558-
assert!(result.is_ok());
560+
result.unwrap();
559561

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

568570
let result = capture.set_datalink(Linktype::ETHERNET);
569-
assert!(result.is_err());
571+
result.unwrap_err();
570572
}
571573

572574
#[test]
@@ -637,7 +639,7 @@ mod tests {
637639
.return_once(|_| {});
638640

639641
let result = capture.savefile("path/to/nowhere");
640-
assert!(result.is_ok());
642+
result.unwrap();
641643
}
642644

643645
#[test]
@@ -665,7 +667,7 @@ mod tests {
665667
.return_once(|_| {});
666668

667669
let result = capture.savefile_append("path/to/nowhere");
668-
assert!(result.is_ok());
670+
result.unwrap();
669671
}
670672

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

688690
let result = capture.savefile("path/to/nowhere");
689-
assert!(result.is_err());
691+
result.unwrap_err();
690692
}
691693

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

710712
let result = capture.savefile_append("path/to/nowhere");
711-
assert!(result.is_err());
713+
result.unwrap_err();
712714
}
713715

714716
#[test]
@@ -740,7 +742,7 @@ mod tests {
740742
.return_once(|_| 0);
741743

742744
let result = savefile.flush();
743-
assert!(result.is_ok());
745+
result.unwrap();
744746

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

751753
let result = savefile.flush();
752-
assert!(result.is_err());
754+
result.unwrap_err();
753755
}
754756

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

770772
let result = capture.direction(Direction::Out);
771-
assert!(result.is_ok());
773+
result.unwrap();
772774

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

781783
let result = capture.direction(Direction::Out);
782-
assert!(result.is_err());
784+
result.unwrap_err();
783785

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

842844
let result = capture.next_packet();
843-
assert!(result.is_err());
845+
result.unwrap_err();
844846
}
845847

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

885887
let result = capture.compile("some bpf program", false);
886-
assert!(result.is_err());
888+
result.unwrap_err();
887889

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

898900
let result = capture.compile("some bpf program", false);
899-
assert!(result.is_ok());
901+
result.unwrap();
900902
}
901903

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

927929
let result = capture.filter("some bpf program", false);
928-
assert!(result.is_err());
930+
result.unwrap_err();
929931

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

946948
let result = capture.compile("some bpf program", false);
947-
assert!(result.is_ok());
949+
result.unwrap();
948950
}
949951

950952
#[test]
@@ -983,7 +985,7 @@ mod tests {
983985
let _err = geterr_expect(pcap);
984986

985987
let result = capture.stats();
986-
assert!(result.is_err());
988+
result.unwrap_err();
987989
}
988990

989991
#[test]

src/capture/activated/offline.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
1-
use std::path::Path;
2-
31
#[cfg(not(windows))]
42
use std::os::unix::io::RawFd;
3+
use std::path::Path;
54

5+
#[cfg(not(windows))]
6+
use crate::capture::activated::open_raw_fd;
7+
#[cfg(libpcap_1_5_0)]
8+
use crate::capture::Precision;
69
use crate::{
710
capture::{Capture, Offline},
811
raw, Error,
912
};
1013

11-
#[cfg(libpcap_1_5_0)]
12-
use crate::capture::Precision;
13-
14-
#[cfg(not(windows))]
15-
use crate::capture::activated::open_raw_fd;
16-
1714
impl Capture<Offline> {
1815
/// Opens an offline capture handle from a pcap dump file, given a path.
1916
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Capture<Offline>, Error> {
20-
Capture::new_raw(path.as_ref().to_str(), |path, err| unsafe {
17+
Capture::new_raw_with_path(path.as_ref(), |path, err| unsafe {
2118
raw::pcap_open_offline(path, err)
2219
})
2320
}
@@ -29,7 +26,7 @@ impl Capture<Offline> {
2926
path: P,
3027
precision: Precision,
3128
) -> Result<Capture<Offline>, Error> {
32-
Capture::new_raw(path.as_ref().to_str(), |path, err| unsafe {
29+
Capture::new_raw_with_path(path.as_ref(), |path, err| unsafe {
3330
raw::pcap_open_offline_with_tstamp_precision(path, precision as _, err)
3431
})
3532
}
@@ -42,7 +39,7 @@ impl Capture<Offline> {
4239
#[cfg(not(windows))]
4340
pub unsafe fn from_raw_fd(fd: RawFd) -> Result<Capture<Offline>, Error> {
4441
open_raw_fd(fd, b'r')
45-
.and_then(|file| Capture::new_raw(None, |_, err| raw::pcap_fopen_offline(file, err)))
42+
.and_then(|file| Capture::new_raw(|err| raw::pcap_fopen_offline(file, err)))
4643
}
4744

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

84+
use super::*;
8785
use crate::{
8886
capture::testmod::test_capture,
8987
raw::testmod::{as_pcap_t, RAWMTX},
9088
};
9189

92-
use super::*;
93-
9490
#[test]
9591
fn test_from_file() {
9692
let _m = RAWMTX.lock();
@@ -107,7 +103,7 @@ mod tests {
107103
.return_once(|_| {});
108104

109105
let result = Capture::from_file("path/to/nowhere");
110-
assert!(result.is_ok());
106+
result.unwrap();
111107
}
112108

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

131127
let result = Capture::from_file_with_precision("path/to/nowhere", Precision::Nano);
132-
assert!(result.is_ok());
128+
result.unwrap();
133129
}
134130

135131
#[test]

src/capture/inactive.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::mem;
1+
use std::{mem, path::Path};
22

33
use crate::{
44
capture::{Active, Capture, Inactive},
@@ -32,7 +32,7 @@ impl Capture<Inactive> {
3232
/// ```
3333
pub fn from_device<D: Into<Device>>(device: D) -> Result<Capture<Inactive>, Error> {
3434
let device: Device = device.into();
35-
Capture::new_raw(Some(&device.name), |name, err| unsafe {
35+
Capture::new_raw_with_path(Path::new(&device.name), |name, err| unsafe {
3636
raw::pcap_create(name, err)
3737
})
3838
}
@@ -206,7 +206,7 @@ mod tests {
206206
.return_once(|_| {});
207207

208208
let result = Capture::from_device("some_device");
209-
assert!(result.is_ok());
209+
result.unwrap();
210210
}
211211

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

219219
let result = Capture::from_device("some_device");
220-
assert!(result.is_err());
220+
result.unwrap_err();
221221
}
222222

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

238238
let result = capture.open();
239-
assert!(result.is_ok());
239+
result.unwrap();
240240
}
241241

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

259259
let result = capture.open();
260-
assert!(result.is_err());
260+
result.unwrap_err();
261261
}
262262

263263
#[test]

0 commit comments

Comments
 (0)