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
75
76
77
78
79
80
81
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, [])
}
|