kernel-netlink: Add test cases for successful and timing out retransmissions
authorMartin Willi <martin@revosec.ch>
Tue, 15 Jul 2014 13:47:03 +0000 (15:47 +0200)
committerMartin Willi <martin@revosec.ch>
Fri, 21 Nov 2014 09:55:45 +0000 (10:55 +0100)
src/libhydra/plugins/kernel_netlink/Makefile.am
src/libhydra/plugins/kernel_netlink/suites/test_socket.c

index aeffaad..cc88554 100644 (file)
@@ -35,6 +35,7 @@ tests_SOURCES = \
 tests_CFLAGS = \
        -I$(top_srcdir)/src/libstrongswan \
        -I$(top_srcdir)/src/libstrongswan/tests \
+       -DNETLINK_MSG_LOSS_HOOK=netlink_msg_loss \
        @COVERAGE_CFLAGS@
 
 tests_LDFLAGS = @COVERAGE_LDFLAGS@
index 415a936..ceea653 100644 (file)
 
 #include "../kernel_netlink_shared.h"
 
+/**
+ * Netlink message drop configuration
+ */
+static int drop_interval = 0;
+
+/**
+ * Netlink message drop hook
+ */
+bool netlink_msg_loss(struct nlmsghdr *hdr)
+{
+       static refcount_t i;
+
+       if (drop_interval)
+       {
+               return ref_get(&i) % drop_interval == drop_interval - 1;
+       }
+       return FALSE;
+}
+
 START_TEST(test_echo)
 {
        netlink_socket_t *s;
@@ -110,6 +129,71 @@ START_TEST(test_stress)
 }
 END_TEST
 
+START_TEST(test_retransmit_success)
+{
+       netlink_socket_t *s;
+       struct nlmsghdr *out;
+       struct rtgenmsg *msg;
+       size_t len;
+       netlink_buf_t request = {
+               .hdr = {
+                       .nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
+                       .nlmsg_flags = NLM_F_REQUEST | NLM_F_MATCH | NLM_F_ROOT,
+                       .nlmsg_type = RTM_GETLINK,
+               },
+       };
+
+       drop_interval = 2;
+
+       lib->settings->set_int(lib->settings,
+                                                       "%s.plugins.kernel-netlink.timeout", 100, lib->ns);
+       lib->settings->set_int(lib->settings,
+                                                       "%s.plugins.kernel-netlink.retries", 1, lib->ns);
+
+       s = netlink_socket_create(NETLINK_ROUTE, NULL);
+       msg = NLMSG_DATA(&request.hdr);
+       msg->rtgen_family = AF_UNSPEC;
+
+       ck_assert(s->send(s, &request.hdr, &out, &len) == SUCCESS);
+       free(out);
+       s->destroy(s);
+
+       drop_interval = 0;
+}
+END_TEST
+
+START_TEST(test_retransmit_fail)
+{
+       netlink_socket_t *s;
+       struct nlmsghdr *out;
+       struct rtgenmsg *msg;
+       size_t len;
+       netlink_buf_t request = {
+               .hdr = {
+                       .nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
+                       .nlmsg_flags = NLM_F_REQUEST | NLM_F_MATCH | NLM_F_ROOT,
+                       .nlmsg_type = RTM_GETLINK,
+               },
+       };
+
+       drop_interval = 1;
+
+       lib->settings->set_int(lib->settings,
+                                                       "%s.plugins.kernel-netlink.timeout", 50, lib->ns);
+       lib->settings->set_int(lib->settings,
+                                                       "%s.plugins.kernel-netlink.retries", 3, lib->ns);
+
+       s = netlink_socket_create(NETLINK_ROUTE, NULL);
+       msg = NLMSG_DATA(&request.hdr);
+       msg->rtgen_family = AF_UNSPEC;
+
+       ck_assert(s->send(s, &request.hdr, &out, &len) == OUT_OF_RES);
+       s->destroy(s);
+
+       drop_interval = 0;
+}
+END_TEST
+
 Suite *socket_suite_create()
 {
        Suite *s;
@@ -125,5 +209,10 @@ Suite *socket_suite_create()
        tcase_add_test(tc, test_stress);
        suite_add_tcase(s, tc);
 
+       tc = tcase_create("retransmit");
+       tcase_add_test(tc, test_retransmit_success);
+       tcase_add_test(tc, test_retransmit_fail);
+       suite_add_tcase(s, tc);
+
        return s;
 }