1 /*
2 * tests/check-attr.c nla_attr unit tests
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation version 2.1
7 * of the License.
8 *
9 * Copyright (c) 2013 Thomas Graf <tgraf@suug.ch>
10 */
11
12 #include "util.h"
13 #include <netlink/attr.h>
14 #include <netlink/msg.h>
15
16 #include <linux/netlink.h>
17
START_TEST(attr_size)18 START_TEST(attr_size)
19 {
20 fail_if(nla_attr_size(0) != NLA_HDRLEN,
21 "Length of empty attribute should match header size");
22 fail_if(nla_attr_size(1) != NLA_HDRLEN + 1,
23 "Length of 1 bytes payload should be NLA_HDRLEN + 1");
24 fail_if(nla_attr_size(2) != NLA_HDRLEN + 2,
25 "Length of 2 bytes payload should be NLA_HDRLEN + 2");
26 fail_if(nla_attr_size(3) != NLA_HDRLEN + 3,
27 "Length of 3 bytes payload should be NLA_HDRLEN + 3");
28 fail_if(nla_attr_size(4) != NLA_HDRLEN + 4,
29 "Length of 4 bytes payload should be NLA_HDRLEN + 4");
30
31 fail_if(nla_total_size(1) != NLA_HDRLEN + 4,
32 "Total size of 1 bytes payload should result in 8 bytes");
33 fail_if(nla_total_size(2) != NLA_HDRLEN + 4,
34 "Total size of 2 bytes payload should result in 8 bytes");
35 fail_if(nla_total_size(3) != NLA_HDRLEN + 4,
36 "Total size of 3 bytes payload should result in 8 bytes");
37 fail_if(nla_total_size(4) != NLA_HDRLEN + 4,
38 "Total size of 4 bytes payload should result in 8 bytes");
39
40 fail_if(nla_padlen(1) != 3,
41 "2 bytes of payload should result in 3 padding bytes");
42 fail_if(nla_padlen(2) != 2,
43 "2 bytes of payload should result in 2 padding bytes");
44 fail_if(nla_padlen(3) != 1,
45 "3 bytes of payload should result in 1 padding bytes");
46 fail_if(nla_padlen(4) != 0,
47 "4 bytes of payload should result in 0 padding bytes");
48 fail_if(nla_padlen(5) != 3,
49 "5 bytes of payload should result in 3 padding bytes");
50 }
51 END_TEST
52
START_TEST(msg_construct)53 START_TEST(msg_construct)
54 {
55 struct nl_msg *msg;
56 struct nlmsghdr *nlh;
57 struct nlattr *a;
58 int i, rem;
59
60 msg = nlmsg_alloc();
61 fail_if(!msg, "Unable to allocate netlink message");
62
63 for (i = 1; i < 256; i++) {
64 fail_if(nla_put_u32(msg, i, i+1) != 0,
65 "Unable to add attribute %d", i);
66 }
67
68 nlh = nlmsg_hdr(msg);
69 i = 1;
70 nlmsg_for_each_attr(a, nlh, 0, rem) {
71 fail_if(nla_type(a) != i, "Expected attribute %d", i);
72 i++;
73 fail_if(nla_get_u32(a) != i, "Expected attribute value %d", i);
74 }
75
76 nlmsg_free(msg);
77 }
78 END_TEST
79
make_nl_attr_suite(void)80 Suite *make_nl_attr_suite(void)
81 {
82 Suite *suite = suite_create("Netlink attributes");
83
84 TCase *nl_attr = tcase_create("Core");
85 tcase_add_test(nl_attr, attr_size);
86 tcase_add_test(nl_attr, msg_construct);
87 suite_add_tcase(suite, nl_attr);
88
89 return suite;
90 }
91