aboutsummaryrefslogtreecommitdiff
path: root/src/tests/table.rs
blob: 39bf399a2d2a5b6753796bc976651328aa6386d9 (plain) (blame)
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
use crate::{
    nlmsg::{get_operation_from_nlmsghdr_type, nft_nlmsg_maxsize, NfNetlinkDeserializable},
    sys::{NFT_MSG_DELTABLE, NFT_MSG_NEWTABLE},
    MsgType, Table,
};

use super::{
    get_test_nlmsg, get_test_nlmsg_with_msg_type, get_test_table, get_test_table_raw_expr,
    get_test_table_with_userdata_raw_expr, TABLE_USERDATA,
};

#[test]
fn new_empty_table() {
    let mut table = get_test_table();
    let mut buf = Vec::with_capacity(nft_nlmsg_maxsize() as usize);
    let (nlmsghdr, _nfgenmsg, raw_expr) = get_test_nlmsg(&mut buf, &mut table);
    assert_eq!(
        get_operation_from_nlmsghdr_type(nlmsghdr.nlmsg_type),
        NFT_MSG_NEWTABLE as u8
    );
    assert_eq!(nlmsghdr.nlmsg_len, 44);

    assert_eq!(raw_expr, get_test_table_raw_expr().to_raw());
}

#[test]
fn new_empty_table_with_userdata() {
    let mut table = get_test_table();
    table.set_userdata(TABLE_USERDATA.as_bytes().to_vec());
    let mut buf = Vec::with_capacity(nft_nlmsg_maxsize() as usize);
    let (nlmsghdr, _nfgenmsg, raw_expr) = get_test_nlmsg(&mut buf, &mut table);
    assert_eq!(
        get_operation_from_nlmsghdr_type(nlmsghdr.nlmsg_type),
        NFT_MSG_NEWTABLE as u8
    );
    assert_eq!(nlmsghdr.nlmsg_len, 64);

    assert_eq!(raw_expr, get_test_table_with_userdata_raw_expr().to_raw());
}

#[test]
fn delete_empty_table() {
    let mut table = get_test_table();
    let mut buf = Vec::with_capacity(nft_nlmsg_maxsize() as usize);
    let (nlmsghdr, _nfgenmsg, raw_expr) =
        get_test_nlmsg_with_msg_type(&mut buf, &mut table, MsgType::Del);
    assert_eq!(
        get_operation_from_nlmsghdr_type(nlmsghdr.nlmsg_type),
        NFT_MSG_DELTABLE as u8
    );
    assert_eq!(nlmsghdr.nlmsg_len, 44);

    assert_eq!(raw_expr, get_test_table_raw_expr().to_raw());
}

#[test]
fn parse_table() {
    let mut table = get_test_table();
    table.set_userdata(TABLE_USERDATA.as_bytes().to_vec());
    let mut buf = Vec::with_capacity(nft_nlmsg_maxsize() as usize);
    let (_nlmsghdr, _nfgenmsg, _raw_expr) = get_test_nlmsg(&mut buf, &mut table);

    let (deserialized_table, remaining) =
        Table::deserialize(&buf).expect("Couldn't deserialize the object");
    assert_eq!(table, deserialized_table);
    assert_eq!(remaining.len(), 0);
}