From 4e9aa6a9a61c565a0caacbaf30ab73082f1cc397 Mon Sep 17 00:00:00 2001 From: Thomas Graf Date: Sun, 28 Apr 2013 12:31:52 +0200 Subject: [PATCH] tests: Add basic attribute unit tests Signed-off-by: Thomas Graf --- tests/Makefile.am | 3 +- tests/check-all.c | 2 ++ tests/check-attr.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++ tests/util.h | 5 +++ 4 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 tests/check-attr.c create mode 100644 tests/util.h diff --git a/tests/Makefile.am b/tests/Makefile.am index 3e9eafe..5536bc8 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -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 diff --git a/tests/check-all.c b/tests/check-all.c index e801003..e431802 100644 --- a/tests/check-all.c +++ b/tests/check-all.c @@ -12,6 +12,7 @@ #include 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 */ diff --git a/tests/check-attr.c b/tests/check-attr.c new file mode 100644 index 0000000..d862230 --- /dev/null +++ b/tests/check-attr.c @@ -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 + */ + +#include "util.h" +#include +#include + +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; +} diff --git a/tests/util.h b/tests/util.h new file mode 100644 index 0000000..c675383 --- /dev/null +++ b/tests/util.h @@ -0,0 +1,5 @@ +#include + +#define nl_fail_if(condition, error, message) \ + fail_if((condition), "nlerr=%d (%s): %s", \ + (error), nl_geterror(error), (message))