falco_event/events/
to_bytes.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use std::io::Write;

pub trait EventToBytes {
    fn write<W: Write>(&self, writer: W) -> std::io::Result<()>;
}

impl EventToBytes for &[u8] {
    fn write<W: Write>(&self, mut writer: W) -> std::io::Result<()> {
        writer.write_all(self)
    }
}

#[cfg(test)]
mod tests {
    use crate::events::types::{AnyEvent, PPME_SYSCALL_OPEN_X};
    use crate::events::{Event, EventMetadata, EventToBytes, RawEvent};
    use crate::fields::types::{PT_FLAGS32_file_flags, PT_FD, PT_FSPATH};

    #[test]
    fn test_event_to_bytes() {
        let evt = Event {
            metadata: EventMetadata { ts: 1, tid: 1 },
            params: PPME_SYSCALL_OPEN_X {
                fd: Some(PT_FD(5)),
                name: Some(PT_FSPATH::new("/etc/passwd")),
                flags: Some(PT_FLAGS32_file_flags::O_RDWR),
                mode: Some(0o644),
                dev: Some(0),
                ino: Some(0),
            },
        };

        let mut buf = Vec::new();
        evt.write(&mut buf).unwrap();

        let evt2 = RawEvent::from(buf.as_slice()).unwrap();
        let evt2 = evt2.load::<PPME_SYSCALL_OPEN_X>().unwrap();

        assert_eq!(evt2.params.fd, Some(PT_FD(5)));
        assert_eq!(evt2.params.name, Some(PT_FSPATH::new("/etc/passwd")));
        assert_eq!(evt2.params.flags, Some(PT_FLAGS32_file_flags::O_RDWR));
        assert_eq!(evt2.params.mode, Some(0o644));
        assert_eq!(evt2.params.dev, Some(0));
        assert_eq!(evt2.params.ino, Some(0));
    }

    #[test]
    fn test_any_event_to_bytes() {
        let evt = Event {
            metadata: EventMetadata { ts: 1, tid: 1 },
            params: AnyEvent::SYSCALL_OPEN_X(PPME_SYSCALL_OPEN_X {
                fd: Some(PT_FD(5)),
                name: Some(PT_FSPATH::new("/etc/passwd")),
                flags: Some(PT_FLAGS32_file_flags::O_RDWR),
                mode: Some(0o644),
                dev: Some(0),
                ino: Some(0),
            }),
        };

        let mut buf = Vec::new();
        evt.write(&mut buf).unwrap();

        let evt2 = RawEvent::from(buf.as_slice()).unwrap();
        let evt2 = evt2.load::<PPME_SYSCALL_OPEN_X>().unwrap();

        assert_eq!(evt2.params.fd, Some(PT_FD(5)));
        assert_eq!(evt2.params.name, Some(PT_FSPATH::new("/etc/passwd")));
        assert_eq!(evt2.params.flags, Some(PT_FLAGS32_file_flags::O_RDWR));
        assert_eq!(evt2.params.mode, Some(0o644));
        assert_eq!(evt2.params.dev, Some(0));
        assert_eq!(evt2.params.ino, Some(0));
    }
}