Skip to content

Commit 3a72f19

Browse files
author
Danny Browning
committed
Tests for bpf's, upgrade futures
1 parent ff73106 commit 3a72f19

File tree

5 files changed

+47
-10
lines changed

5 files changed

+47
-10
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ edition = "2018"
77
[dependencies]
88
failure = "~0.1"
99
failure_derive = "~0.1"
10-
futures-preview = { version ="0.3.0-alpha.13", features = ["compat"] }
10+
futures-preview = { version ="0.3.0-alpha.14", features = ["compat"] }
1111
libc = "~0.2"
1212
log = "~0.4"
1313
pcap-sys = { git = "https://github.com/protectwise/pcap-sys.git" }

src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl Config {
5151
&self.bpf
5252
}
5353

54-
pub fn with_bf(&mut self, amt: String) -> &mut Self {
54+
pub fn with_bpf(&mut self, amt: String) -> &mut Self {
5555
self.bpf = Some(amt);
5656
self
5757
}

src/handle.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ impl Handle {
147147
bf_insns: std::ptr::null_mut(),
148148
};
149149

150-
let bpf_str = std::ffi::CString::new(bpf.to_string()).map_err(Error::Ffi)?;
150+
let bpf_str = std::ffi::CString::new(bpf.clone()).map_err(Error::Ffi)?;
151151

152152
if 0 != unsafe {
153153
pcap_sys::pcap_compile(
@@ -218,13 +218,17 @@ impl Handle {
218218
Ok(stats)
219219
}
220220
}
221+
222+
pub fn close(&self) {
223+
unsafe {
224+
pcap_sys::pcap_close(self.handle)
225+
}
226+
}
221227
}
222228

223229
impl Drop for Handle {
224230
fn drop(&mut self) {
225-
unsafe {
226-
pcap_sys::pcap_close(self.handle);
227-
}
231+
self.close();
228232
}
229233
}
230234

src/provider.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ impl PacketProvider {
3636
.set_buffer_size(config.buffer_size())?
3737
.activate()?;
3838

39-
if let Some(ref s) = config.bpf() {
40-
handle.set_bpf(s)?;
39+
if let Some(bpf) = config.bpf() {
40+
handle.set_bpf(bpf)?;
4141
}
4242
}
4343

@@ -111,6 +111,23 @@ mod tests {
111111
);
112112
}
113113

114+
#[test]
115+
fn packets_from_lookup_with_bpf() {
116+
let _ = env_logger::try_init();
117+
118+
let mut cfg = Config::default();
119+
cfg.with_bpf("(not (net 172.16.0.0/16 and port 443)) and (not (host 172.17.76.33 and port 443))".to_owned());
120+
121+
let handle = Handle::lookup().expect("No handle created");
122+
123+
let stream = PacketProvider::new(cfg, handle);
124+
125+
assert!(
126+
stream.is_ok(),
127+
format!("Could not build stream {}", stream.err().unwrap())
128+
);
129+
}
130+
114131
#[bench]
115132
fn bench_packets_from_large_file(b: &mut Bencher) {
116133
let _ = env_logger::try_init();

src/stream.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ impl PacketStream {
2828
.set_buffer_size(config.buffer_size())?
2929
.activate()?;
3030

31-
if let Some(ref s) = config.bpf() {
32-
handle.set_bpf(s)?;
31+
if let Some(bpf) = config.bpf() {
32+
handle.set_bpf(bpf)?;
3333
}
3434
}
3535

@@ -103,6 +103,22 @@ mod tests {
103103
);
104104
}
105105

106+
#[test]
107+
fn packets_from_lookup_with_bpf() {
108+
let _ = env_logger::try_init();
109+
110+
let mut cfg = Config::default();
111+
cfg.with_bpf("(not (net 172.16.0.0/16 and port 443)) and (not (host 172.17.76.33 and port 443))".to_owned());
112+
let handle = Handle::lookup().expect("No handle created");
113+
114+
let stream = PacketStream::new(cfg, handle);
115+
116+
assert!(
117+
stream.is_ok(),
118+
format!("Could not build stream {}", stream.err().unwrap())
119+
);
120+
}
121+
106122
#[bench]
107123
fn bench_packets_from_large_file(b: &mut Bencher) {
108124
let _ = env_logger::try_init();

0 commit comments

Comments
 (0)