diff options
author | Simon THOBY <git@nightmared.fr> | 2022-10-02 16:01:19 +0200 |
---|---|---|
committer | Simon THOBY <git@nightmared.fr> | 2022-10-02 16:01:19 +0200 |
commit | 3371865506cad4a795f07bce4495eb00d199f4a6 (patch) | |
tree | 7f468905fe7673dbb2d966f04ac0a535704c4db5 /tests/batch.rs | |
parent | ac59df84eb292f8e907fdae3436e589df164e826 (diff) |
Add some initial support for tests with the full-rust engine
Diffstat (limited to 'tests/batch.rs')
-rw-r--r-- | tests/batch.rs | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/tests/batch.rs b/tests/batch.rs new file mode 100644 index 0000000..dbf444f --- /dev/null +++ b/tests/batch.rs @@ -0,0 +1,82 @@ +mod sys; +use libc::NFNL_MSG_BATCH_BEGIN; +use nix::libc::NFNL_MSG_BATCH_END; +use rustables::nlmsg::NfNetlinkObject; +use rustables::parser::{get_operation_from_nlmsghdr_type, parse_nlmsg, parse_object}; +use rustables::{Batch, MsgType, Table}; + +mod lib; +use lib::*; + +#[test] +fn batch_empty() { + let batch = Batch::new(); + let buf = batch.finalize(); + + let (hdr, msg) = parse_nlmsg(&buf).expect("Invalid nlmsg message"); + let op = get_operation_from_nlmsghdr_type(hdr.nlmsg_type) as i32; + assert_eq!(op, NFNL_MSG_BATCH_BEGIN); + let (_nfgenmsg, attrs, remaining_data) = + parse_object(hdr, msg, &buf).expect("Could not parse the batch message"); + + assert_eq!(attrs.get_raw_data(), []); + + let (hdr, msg) = parse_nlmsg(&remaining_data).expect("Invalid nlmsg message"); + let op = get_operation_from_nlmsghdr_type(hdr.nlmsg_type) as i32; + assert_eq!(op, NFNL_MSG_BATCH_END); + let (_nfgenmsg, attrs, remaining_data) = + parse_object(hdr, msg, &remaining_data).expect("Could not parse the batch message"); + + assert_eq!(attrs.get_raw_data(), []); + + assert_eq!(remaining_data, []) +} + +#[test] +fn batch_with_objects() { + let mut original_tables = vec![]; + for i in 0..10 { + let mut table = get_test_table(); + table.set_userdata(vec![i as u8]); + original_tables.push(table); + } + + let mut batch = Batch::new(); + for i in 0..10 { + batch.add( + &original_tables[i], + if i % 2 == 0 { + MsgType::Add + } else { + MsgType::Del + }, + ); + } + let buf = batch.finalize(); + + let (hdr, msg) = parse_nlmsg(&buf).expect("Invalid nlmsg message"); + let op = get_operation_from_nlmsghdr_type(hdr.nlmsg_type) as i32; + assert_eq!(op, NFNL_MSG_BATCH_BEGIN); + let (_nfgenmsg, attrs, mut remaining_data) = + parse_object(hdr, msg, &buf).expect("Could not parse the batch message"); + + assert_eq!(attrs.get_raw_data(), []); + + for i in 0..10 { + let (deserialized_table, rest) = + Table::deserialize(remaining_data).expect("could not deserialize a table"); + remaining_data = rest; + + assert_eq!(deserialized_table, original_tables[i]); + } + + let (hdr, msg) = parse_nlmsg(&remaining_data).expect("Invalid nlmsg message"); + let op = get_operation_from_nlmsghdr_type(hdr.nlmsg_type) as i32; + assert_eq!(op, NFNL_MSG_BATCH_END); + let (_nfgenmsg, attrs, remaining_data) = + parse_object(hdr, msg, &remaining_data).expect("Could not parse the batch message"); + + assert_eq!(attrs.get_raw_data(), []); + + assert_eq!(remaining_data, []) +} |