]> granicus.if.org Git - libnl/commitdiff
tests: Add basic attribute unit tests
authorThomas Graf <tgraf@suug.ch>
Sun, 28 Apr 2013 10:31:52 +0000 (12:31 +0200)
committerThomas Graf <tgraf@suug.ch>
Sun, 28 Apr 2013 10:31:52 +0000 (12:31 +0200)
Signed-off-by: Thomas Graf <tgraf@suug.ch>
tests/Makefile.am
tests/check-all.c
tests/check-attr.c [new file with mode: 0644]
tests/util.h [new file with mode: 0644]

index 3e9eafefc9ea57e30c42323e1f3662a890fee044..5536bc8e44e40168c55692665b7f6db9aef6fa06 100644 (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
index e8010036cb8bb8e4538c910f1cccfad9724090ef..e43180246c65d725de102a7dfdf3078047dda749 100644 (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 */
 
diff --git a/tests/check-attr.c b/tests/check-attr.c
new file mode 100644 (file)
index 0000000..d862230
--- /dev/null
@@ -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;
+}
diff --git a/tests/util.h b/tests/util.h
new file mode 100644 (file)
index 0000000..c675383
--- /dev/null
@@ -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))