dect
/
libnl
Archived
13
0
Fork 0

tests: Add basic attribute unit tests

Signed-off-by: Thomas Graf <tgraf@suug.ch>
This commit is contained in:
Thomas Graf 2013-04-28 12:31:52 +02:00
parent ffc0ee3540
commit 4e9aa6a9a6
4 changed files with 97 additions and 1 deletions

View File

@ -46,5 +46,6 @@ test_complex_HTB_with_hash_filters_SOURCES = test-complex-HTB-with-hash-filters.
# Unit tests
check_all_SOURCES = \
check-all.c \
check-addr.c
check-addr.c \
check-attr.c
endif

View File

@ -12,6 +12,7 @@
#include <check.h>
extern Suite *make_nl_addr_suite(void);
extern Suite *make_nl_attr_suite(void);
static Suite *main_suite(void)
{
@ -30,6 +31,7 @@ int main(int argc, char *argv[])
/* Add testsuites below */
srunner_add_suite(runner, make_nl_addr_suite());
srunner_add_suite(runner, make_nl_attr_suite());
/* Do not add testsuites below this line */

88
tests/check-attr.c Normal file
View File

@ -0,0 +1,88 @@
/*
* tests/check-attr.c nla_attr unit tests
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* Copyright (c) 2013 Thomas Graf <tgraf@suug.ch>
*/
#include "util.h"
#include <netlink/attr.h>
#include <netlink/msg.h>
START_TEST(attr_size)
{
fail_if(nla_attr_size(0) != NLA_HDRLEN,
"Length of empty attribute should match header size");
fail_if(nla_attr_size(1) != NLA_HDRLEN + 1,
"Length of 1 bytes payload should be NLA_HDRLEN + 1");
fail_if(nla_attr_size(2) != NLA_HDRLEN + 2,
"Length of 2 bytes payload should be NLA_HDRLEN + 2");
fail_if(nla_attr_size(3) != NLA_HDRLEN + 3,
"Length of 3 bytes payload should be NLA_HDRLEN + 3");
fail_if(nla_attr_size(4) != NLA_HDRLEN + 4,
"Length of 4 bytes payload should be NLA_HDRLEN + 4");
fail_if(nla_total_size(1) != NLA_HDRLEN + 4,
"Total size of 1 bytes payload should result in 8 bytes");
fail_if(nla_total_size(2) != NLA_HDRLEN + 4,
"Total size of 2 bytes payload should result in 8 bytes");
fail_if(nla_total_size(3) != NLA_HDRLEN + 4,
"Total size of 3 bytes payload should result in 8 bytes");
fail_if(nla_total_size(4) != NLA_HDRLEN + 4,
"Total size of 4 bytes payload should result in 8 bytes");
fail_if(nla_padlen(1) != 3,
"2 bytes of payload should result in 3 padding bytes");
fail_if(nla_padlen(2) != 2,
"2 bytes of payload should result in 2 padding bytes");
fail_if(nla_padlen(3) != 1,
"3 bytes of payload should result in 1 padding bytes");
fail_if(nla_padlen(4) != 0,
"4 bytes of payload should result in 0 padding bytes");
fail_if(nla_padlen(5) != 3,
"5 bytes of payload should result in 3 padding bytes");
}
END_TEST
START_TEST(msg_construct)
{
struct nl_msg *msg;
struct nlmsghdr *nlh;
struct nlattr *a;
int i, rem;
msg = nlmsg_alloc();
fail_if(!msg, "Unable to allocate netlink message");
for (i = 1; i < 256; i++) {
fail_if(nla_put_u32(msg, i, i+1) != 0,
"Unable to add attribute %d", i);
}
nlh = nlmsg_hdr(msg);
i = 1;
nlmsg_for_each_attr(a, nlh, 0, rem) {
fail_if(nla_type(a) != i, "Expected attribute %d", i);
i++;
fail_if(nla_get_u32(a) != i, "Expected attribute value %d", i);
}
nlmsg_free(msg);
}
END_TEST
Suite *make_nl_attr_suite(void)
{
Suite *suite = suite_create("Netlink attributes");
TCase *nl_attr = tcase_create("Core");
tcase_add_test(nl_attr, attr_size);
tcase_add_test(nl_attr, msg_construct);
suite_add_tcase(suite, nl_attr);
return suite;
}

5
tests/util.h Normal file
View File

@ -0,0 +1,5 @@
#include <check.h>
#define nl_fail_if(condition, error, message) \
fail_if((condition), "nlerr=%d (%s): %s", \
(error), nl_geterror(error), (message))