Skip to content

Commit 857e7df

Browse files
committed
rust: intercept envelope serialization tested
1 parent 0aa9fe2 commit 857e7df

File tree

5 files changed

+71
-130
lines changed

5 files changed

+71
-130
lines changed

rust/intercept/Cargo.toml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@ wrapper = []
1818
name = "intercept"
1919
path = "src/lib.rs"
2020

21-
# TODO: remove this when bear implements the entry point
22-
[[bin]]
23-
name = "intercept"
24-
path = "src/bin/intercept.rs"
25-
2621
# TODO: create this as a separate crate
2722
[[bin]]
2823
name = "wrapper"

rust/intercept/src/bin/intercept.rs

Lines changed: 0 additions & 118 deletions
This file was deleted.

rust/intercept/src/collector.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ impl EventCollectorOnTcp {
4545
let (control_input, control_output) = bounded(0);
4646
let listener = TcpListener::bind("127.0.0.1:0")?;
4747

48-
let result = EventCollectorOnTcp { control_input, control_output, listener };
48+
let result = EventCollectorOnTcp {
49+
control_input,
50+
control_output,
51+
listener,
52+
};
4953

5054
Ok(result)
5155
}

rust/intercept/src/lib.rs

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ use std::path::PathBuf;
2323
use chrono::Utc;
2424
use serde::{Deserialize, Serialize};
2525

26-
pub mod reporter;
2726
pub mod collector;
27+
pub mod reporter;
2828

2929
// Reporter id is a unique identifier for a reporter.
3030
//
@@ -59,7 +59,7 @@ pub enum Event {
5959
execution: Execution,
6060
},
6161
Terminated {
62-
status: i64
62+
status: i64,
6363
},
6464
Signaled {
6565
signal: i32,
@@ -76,10 +76,14 @@ pub struct Envelope {
7676
impl Envelope {
7777
pub fn new(rid: &ReporterId, event: Event) -> Self {
7878
let timestamp = Utc::now().timestamp_millis() as u64;
79-
Envelope { rid: rid.clone(), timestamp, event }
79+
Envelope {
80+
rid: rid.clone(),
81+
timestamp,
82+
event,
83+
}
8084
}
8185

82-
pub fn read_from(mut reader: impl Read) -> Result<Self, anyhow::Error> {
86+
pub fn read_from(reader: &mut impl Read) -> Result<Self, anyhow::Error> {
8387
let mut length_bytes = [0; 4];
8488
reader.read_exact(&mut length_bytes)?;
8589
let length = u32::from_be_bytes(length_bytes) as usize;
@@ -91,7 +95,7 @@ impl Envelope {
9195
Ok(envelope)
9296
}
9397

94-
pub fn write_into(&self, mut writer: impl Write) -> Result<u32, anyhow::Error> {
98+
pub fn write_into(&self, writer: &mut impl Write) -> Result<u32, anyhow::Error> {
9599
let serialized_envelope = serde_json::to_string(&self)?;
96100
let bytes = serialized_envelope.into_bytes();
97101
let length = bytes.len() as u32;
@@ -102,3 +106,55 @@ impl Envelope {
102106
Ok(length)
103107
}
104108
}
109+
110+
#[cfg(test)]
111+
mod test {
112+
use super::*;
113+
use lazy_static::lazy_static;
114+
use std::io::Cursor;
115+
116+
#[test]
117+
fn read_write_works() {
118+
let mut writer = Cursor::new(vec![0; 1024]);
119+
for envelope in ENVELOPES.iter() {
120+
let result = Envelope::write_into(envelope, &mut writer);
121+
assert!(result.is_ok());
122+
}
123+
124+
let mut reader = Cursor::new(writer.get_ref());
125+
for envelope in ENVELOPES.iter() {
126+
let result = Envelope::read_from(&mut reader);
127+
assert!(result.is_ok());
128+
assert_eq!(result.unwrap(), *envelope.clone());
129+
}
130+
}
131+
132+
lazy_static! {
133+
static ref ENVELOPES: Vec<Envelope> = vec![
134+
Envelope {
135+
rid: ReporterId(1),
136+
timestamp: 0,
137+
event: Event::Started {
138+
pid: ProcessId(1),
139+
ppid: ProcessId(0),
140+
execution: Execution {
141+
executable: PathBuf::from("/usr/bin/ls"),
142+
arguments: vec!["-l".to_string()],
143+
working_dir: PathBuf::from("/tmp"),
144+
environment: HashMap::new(),
145+
},
146+
},
147+
},
148+
Envelope {
149+
rid: ReporterId(1),
150+
timestamp: 0,
151+
event: Event::Terminated { status: 0 },
152+
},
153+
Envelope {
154+
rid: ReporterId(1),
155+
timestamp: 0,
156+
event: Event::Signaled { signal: 15 },
157+
},
158+
];
159+
}
160+
}

rust/intercept/src/reporter.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ impl TcpReporter {
4949
pub fn new(destination: String) -> Result<Self, anyhow::Error> {
5050
let socket = TcpStream::connect(destination.clone())?;
5151
let reporter_id = ReporterId::new();
52-
let result = TcpReporter { socket, destination, reporter_id };
52+
let result = TcpReporter {
53+
socket,
54+
destination,
55+
reporter_id,
56+
};
5357
Ok(result)
5458
}
5559
}

0 commit comments

Comments
 (0)