diff options
author | Simon THOBY <git@nightmared.fr> | 2022-02-21 23:21:55 +0100 |
---|---|---|
committer | Simon THOBY <git@nightmared.fr> | 2022-08-26 21:52:00 +0200 |
commit | e918159bf7478652e9da41f4a873c6e46b3733ca (patch) | |
tree | 68592482fc3709d60daf8aae3be7bde73893d31a /tests | |
parent | 5d207e951f39531a97619a3bdf900f7aa16efb9e (diff) |
delete the dependency on libmnl
Diffstat (limited to 'tests')
-rw-r--r-- | tests/chain.rs | 2 | ||||
-rw-r--r-- | tests/expr.rs | 1 | ||||
-rw-r--r-- | tests/lib.rs | 49 | ||||
-rw-r--r-- | tests/rule.rs | 2 | ||||
-rw-r--r-- | tests/set.rs | 2 | ||||
-rw-r--r-- | tests/table.rs | 2 |
6 files changed, 14 insertions, 44 deletions
diff --git a/tests/chain.rs b/tests/chain.rs index 4b6da91..809936a 100644 --- a/tests/chain.rs +++ b/tests/chain.rs @@ -1,7 +1,7 @@ use std::ffi::CStr; mod sys; -use rustables::MsgType; +use rustables::{query::get_operation_from_nlmsghdr_type, MsgType}; use sys::*; mod lib; diff --git a/tests/expr.rs b/tests/expr.rs index 7950df3..cfc9c7d 100644 --- a/tests/expr.rs +++ b/tests/expr.rs @@ -3,6 +3,7 @@ use rustables::expr::{ LogGroup, LogPrefix, Lookup, Meta, Nat, NatType, Payload, Register, Reject, TcpHeaderField, TransportHeaderField, Verdict, }; +use rustables::query::{get_operation_from_nlmsghdr_type, Nfgenmsg}; use rustables::set::Set; use rustables::sys::libc::{nlmsghdr, NF_DROP}; use rustables::{ProtoFamily, Rule}; diff --git a/tests/lib.rs b/tests/lib.rs index 29c61b3..9b44a88 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -1,19 +1,11 @@ #![allow(dead_code)] -use libc::{nlmsghdr, AF_UNIX, NFNETLINK_V0, NFNL_SUBSYS_NFTABLES}; +use libc::{nlmsghdr, AF_UNIX}; +use rustables::query::{parse_nlmsg, Nfgenmsg}; use rustables::set::SetKey; use rustables::{nft_nlmsg_maxsize, Chain, MsgType, NlMsg, ProtoFamily, Rule, Set, Table}; use std::ffi::{c_void, CStr}; -use std::mem::size_of; use std::rc::Rc; -pub fn get_subsystem_from_nlmsghdr_type(x: u16) -> u8 { - ((x & 0xff00) >> 8) as u8 -} - -pub fn get_operation_from_nlmsghdr_type(x: u16) -> u8 { - (x & 0x00ff) as u8 -} - pub const TABLE_NAME: &[u8; 10] = b"mocktable\0"; pub const CHAIN_NAME: &[u8; 10] = b"mockchain\0"; pub const SET_NAME: &[u8; 8] = b"mockset\0"; @@ -89,14 +81,6 @@ impl NetlinkExpr { } } -#[repr(C)] -#[derive(Clone, Copy)] -pub struct Nfgenmsg { - family: u8, /* AF_xxx */ - version: u8, /* nfnetlink version */ - res_id: u16, /* resource id */ -} - pub fn get_test_table() -> Table { Table::new( &CStr::from_bytes_with_nul(TABLE_NAME).unwrap(), @@ -131,35 +115,20 @@ pub fn get_test_nlmsg_with_msg_type( unsafe { obj.write(buf.as_mut_ptr() as *mut c_void, 0, msg_type); - // right now the message is composed of the following parts: - // - nlmsghdr (contains the message size and type) - // - nfgenmsg (nftables header that describes the message family) - // - the raw value that we want to validate - - 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); - let raw_value = buf[size_of_hdr + size_of_nfgenmsg..nlmsghdr.nlmsg_len as usize] - .iter() - .map(|x| *x) - .collect(); + let (nlmsghdr, msg) = parse_nlmsg(&buf, 0, 0).expect("Couldn't parse the message"); + + let (nfgenmsg, raw_value) = match msg { + rustables::query::NlMsg::NfGenMsg(nfgenmsg, raw_value) => (nfgenmsg, raw_value), + _ => panic!("Invalid return value type, expected a valid message"), + }; // sanity checks on the global message (this should be very similar/factorisable for the // most part in other tests) // TODO: check the messages flags - assert_eq!( - get_subsystem_from_nlmsghdr_type(nlmsghdr.nlmsg_type), - NFNL_SUBSYS_NFTABLES 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, NFNETLINK_V0 as u8); assert_eq!(nfgenmsg.res_id.to_be(), 0); - (nlmsghdr, nfgenmsg, raw_value) + (*nlmsghdr, *nfgenmsg, raw_value.to_owned()) } } diff --git a/tests/rule.rs b/tests/rule.rs index b601a61..517db47 100644 --- a/tests/rule.rs +++ b/tests/rule.rs @@ -1,7 +1,7 @@ use std::ffi::CStr; mod sys; -use rustables::MsgType; +use rustables::{query::get_operation_from_nlmsghdr_type, MsgType}; use sys::*; mod lib; diff --git a/tests/set.rs b/tests/set.rs index d5b2ad7..4b79988 100644 --- a/tests/set.rs +++ b/tests/set.rs @@ -1,7 +1,7 @@ mod sys; use std::net::{Ipv4Addr, Ipv6Addr}; -use rustables::{set::SetKey, MsgType}; +use rustables::{query::get_operation_from_nlmsghdr_type, set::SetKey, MsgType}; use sys::*; mod lib; diff --git a/tests/table.rs b/tests/table.rs index 3d8957c..971d58b 100644 --- a/tests/table.rs +++ b/tests/table.rs @@ -1,7 +1,7 @@ use std::ffi::CStr; mod sys; -use rustables::MsgType; +use rustables::{query::get_operation_from_nlmsghdr_type, MsgType}; use sys::*; mod lib; |