@@ -23,8 +23,8 @@ use std::path::PathBuf;
2323use chrono:: Utc ;
2424use serde:: { Deserialize , Serialize } ;
2525
26- pub mod reporter;
2726pub 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 {
7676impl 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+ }
0 commit comments