aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon THOBY <git@nightmared.fr>2021-10-25 22:06:56 +0200
committerlafleur <lafleur@boum.org>2021-11-05 11:21:27 +0100
commit77fd69af12242ab577b0ab1b3a3539fefd3110d8 (patch)
tree92a15f278d8a9eb5bd90ee72efea28dff7494bdd
parent06e667d3ab7d2d34d436c035f460d8ece811c444 (diff)
check the raw netlink message instead of the in-memory reprensentation of the library
-rw-r--r--rustables/src/expr/counter.rs102
1 files changed, 79 insertions, 23 deletions
diff --git a/rustables/src/expr/counter.rs b/rustables/src/expr/counter.rs
index 11b8482..1997433 100644
--- a/rustables/src/expr/counter.rs
+++ b/rustables/src/expr/counter.rs
@@ -47,9 +47,28 @@ impl Expression for Counter {
#[cfg(test)]
mod tests {
- use super::Expression;
- use std::ffi::CString;
+ use crate::{nft_nlmsg_maxsize, NlMsg};
+
+ use rustables_sys as sys;
+ use std::ffi::{c_void, CString};
+ use std::mem::size_of;
use std::rc::Rc;
+ use sys::libc::{nlmsghdr, AF_UNIX, NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWRULE};
+
+ fn get_subsystem_from_nlmsghdr_type(x: u16) -> u8 {
+ ((x & 0xff00) >> 8) as u8
+ }
+ fn get_operation_from_nlmsghdr_type(x: u16) -> u8 {
+ (x & 0x00ff) as u8
+ }
+
+ #[repr(C)]
+ #[derive(Clone, Copy)]
+ struct nfgenmsg {
+ family: u8, /* AF_xxx */
+ version: u8, /* nfnetlink version */
+ res_id: u16, /* resource id */
+ }
#[test]
fn counter_expr_is_valid() {
@@ -57,28 +76,65 @@ mod tests {
counter.nb_bytes = 0;
counter.nb_packets = 0;
let table = Rc::new(crate::Table::new(
- &CString::new("mocktable").unwrap(),
- crate::ProtoFamily::Inet)
- );
+ &CString::new("mocktable").unwrap(),
+ crate::ProtoFamily::Inet,
+ ));
let chain = Rc::new(crate::Chain::new(
- &CString::new("mockchain").unwrap(),
- Rc::clone(&table))
- );
- let rule = crate::Rule::new(Rc::clone(&chain));
- let view = &counter.to_expr(&rule) as *const _ as *const u8;
- let slice = unsafe {
- std::slice::from_raw_parts(view, std::mem::size_of::<super::Counter>())
+ &CString::new("mockchain").unwrap(),
+ Rc::clone(&table),
+ ));
+ let mut rule = crate::Rule::new(Rc::clone(&chain));
+ rule.add_expr(&counter);
+ let mut buf = vec![0u8; nft_nlmsg_maxsize() as usize];
+ let (nlmsghdr, nfgenmsg, raw_expr) = unsafe {
+ rule.write(buf.as_mut_ptr() as *mut c_void, 0, crate::MsgType::Add);
+
+ // right now the message is composed of the following parts:
+ // - nlmsghdr (contain the message size and type)
+ // - nfgenmsg (nftables header that describe the family)
+ // - the raw expression that we want to check
+
+ let size_of_hdr = size_of::<nlmsghdr>();
+ let size_of_nfgenmsg = size_of::<nfgenmsg>();
+ let nlmsghdr = *(buf[0..size_of_hdr].as_ptr() as *const nlmsghdr);
+ let nfgenmsg =
+ *(buf[size_of_hdr..size_of_hdr + size_of_nfgenmsg].as_ptr() as *const nfgenmsg);
+ (
+ nlmsghdr,
+ nfgenmsg,
+ &buf[size_of_hdr + size_of_nfgenmsg..nlmsghdr.nlmsg_len as usize],
+ )
};
- assert_eq!(slice[0], 64);
- assert_eq!(slice[1], 15);
- assert_eq!(slice[2], 0);
- assert_eq!(slice[5], 127);
- assert_eq!(slice[6], 0);
- assert_eq!(slice[7], 0);
- assert_eq!(slice[8], 200);
- assert_eq!(slice[13], 127);
- assert_eq!(slice[14], 0);
- assert_eq!(slice[15], 0);
- //assert_eq!(slice, [64, 15, 0, 1, 1, 127, 0, 0, 200, 1, 1, 1, 1, 127, 0, 0]);
+
+ // sanity checks on the global message (this should be very similar/factorisable for the
+ // most part in other tests)
+ assert_eq!(nlmsghdr.nlmsg_len, 100);
+ // TODO: check the messages flags
+ assert_eq!(
+ get_subsystem_from_nlmsghdr_type(nlmsghdr.nlmsg_type),
+ NFNL_SUBSYS_NFTABLES as u8
+ );
+ assert_eq!(
+ get_operation_from_nlmsghdr_type(nlmsghdr.nlmsg_type),
+ NFT_MSG_NEWRULE as u8
+ );
+ assert_eq!(nlmsghdr.nlmsg_seq, 0);
+ assert_eq!(nlmsghdr.nlmsg_pid, 0);
+ assert_eq!(nfgenmsg.family, AF_UNIX as u8);
+ assert_eq!(nfgenmsg.version, 0);
+ assert_eq!(nfgenmsg.res_id.to_be(), 0);
+
+ // check the expression content itself
+ assert_eq!(
+ raw_expr,
+ &[
+ 0xe, 0x0, 0x1, 0x0, 0x6d, 0x6f, 0x63, 0x6b, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x0, 0x0,
+ 0x0, 0xe, 0x0, 0x2, 0x0, 0x6d, 0x6f, 0x63, 0x6b, 0x63, 0x68, 0x61, 0x69, 0x6e, 0x0,
+ 0x0, 0x0, 0x30, 0x0, 0x4, 0x80, 0x2c, 0x0, 0x1, 0x80, 0xc, 0x0, 0x1, 0x0, 0x63,
+ 0x6f, 0x75, 0x6e, 0x74, 0x65, 0x72, 0x0, 0x1c, 0x0, 0x2, 0x80, 0xc, 0x0, 0x1, 0x0,
+ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc, 0x0, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0,
+ 0x0, 0x0, 0x0, 0x0
+ ]
+ );
}
}