core: add test for NMArpingManager
authorBeniamino Galvani <bgalvani@redhat.com>
Wed, 23 Dec 2015 16:07:05 +0000 (17:07 +0100)
committerBeniamino Galvani <bgalvani@redhat.com>
Wed, 20 Jan 2016 10:53:47 +0000 (11:53 +0100)
src/devices/tests/Makefile.am
src/devices/tests/test-arping.c [new file with mode: 0644]

index 330377b..e4f4d3f 100644 (file)
@@ -23,7 +23,8 @@ DEVICES_LDADD = \
 @GNOME_CODE_COVERAGE_RULES@
 
 noinst_PROGRAMS = \
-       test-lldp
+       test-lldp \
+       test-arping
 
 test_lldp_SOURCES = \
        test-lldp.c \
@@ -37,6 +38,19 @@ test_lldp_CPPFLAGS = \
 
 test_lldp_LDADD = $(DEVICES_LDADD)
 
+test_arping_SOURCES = \
+       test-arping.c \
+       ../nm-arping-manager.c \
+       $(top_srcdir)/src/platform/tests/test-common.c
+
+test_arping_CPPFLAGS = \
+       $(AM_CPPFLAGS) \
+       -I$(top_srcdir)/src/platform/tests \
+       -DSETUP=nm_linux_platform_setup
+
+test_arping_LDADD = $(DEVICES_LDADD)
+
 @VALGRIND_RULES@
 TESTS = \
-       test-lldp
+       test-lldp \
+       test-arping
diff --git a/src/devices/tests/test-arping.c b/src/devices/tests/test-arping.c
new file mode 100644 (file)
index 0000000..cf70b14
--- /dev/null
@@ -0,0 +1,140 @@
+/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
+/* nm-platform.c - Handle runtime kernel networking configuration
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Copyright (C) 2015 Red Hat, Inc.
+ */
+
+#include "config.h"
+
+#include "nm-default.h"
+#include "nm-arping-manager.h"
+#include "test-common.h"
+
+#define IFACE_VETH0 "nm-test-veth0"
+#define IFACE_VETH1 "nm-test-veth1"
+
+#define ADDR1 0x01010101
+#define ADDR2 0x02020202
+#define ADDR3 0x03030303
+#define ADDR4 0x04040404
+
+typedef struct {
+       int ifindex0;
+       int ifindex1;
+} test_fixture;
+
+static void
+fixture_setup (test_fixture *fixture, gconstpointer user_data)
+{
+       /* create veth pair. */
+       nmtstp_run_command_check ("ip link add dev %s type veth peer name %s", IFACE_VETH0, IFACE_VETH1);
+       fixture->ifindex0 = nmtstp_assert_wait_for_link (IFACE_VETH0, NM_LINK_TYPE_VETH, 100)->ifindex;
+       fixture->ifindex1 = nmtstp_assert_wait_for_link (IFACE_VETH1, NM_LINK_TYPE_VETH, 100)->ifindex;
+
+       g_assert (nm_platform_link_set_up (NM_PLATFORM_GET, fixture->ifindex0, NULL));
+       g_assert (nm_platform_link_set_up (NM_PLATFORM_GET, fixture->ifindex1, NULL));
+}
+
+typedef struct {
+       in_addr_t addresses[8];
+       in_addr_t peer_addresses[8];
+       gboolean expected_result[8];
+} TestInfo;
+
+static void
+arping_manager_probe_terminated (NMArpingManager *arping_manager, GMainLoop *loop)
+{
+       g_main_loop_quit (loop);
+}
+
+static void
+test_arping_common (test_fixture *fixture, TestInfo *info)
+{
+       gs_unref_object NMArpingManager *manager = NULL;
+       GMainLoop *loop;
+       int i;
+
+       if (!nm_utils_find_helper ("arping", NULL, NULL)) {
+               g_test_skip ("arping binary is missing");
+               return;
+       }
+
+       manager = nm_arping_manager_new (fixture->ifindex0);
+       g_assert (manager != NULL);
+
+       for (i = 0; info->addresses[i]; i++)
+               g_assert (nm_arping_manager_add_address (manager, info->addresses[i]));
+
+       for (i = 0; info->peer_addresses[i]; i++) {
+               nmtstp_ip4_address_add (FALSE, fixture->ifindex1, info->peer_addresses[i],
+                                       24, 0, 3600, 1800, NULL);
+       }
+
+       loop = g_main_loop_new (NULL, FALSE);
+       g_signal_connect (manager, NM_ARPING_MANAGER_PROBE_TERMINATED,
+                         G_CALLBACK (arping_manager_probe_terminated), loop);
+       g_assert (nm_arping_manager_start_probe (manager, 100, NULL));
+       g_assert (nmtst_main_loop_run (loop, 1000));
+
+       for (i = 0; info->addresses[i]; i++) {
+               g_assert_cmpint (nm_arping_manager_check_address (manager, info->addresses[i]),
+                                ==,
+                                info->expected_result[i]);
+       }
+
+       g_main_loop_unref (loop);
+}
+
+static void
+test_arping_1 (test_fixture *fixture, gconstpointer user_data)
+{
+       TestInfo info = { .addresses       = { ADDR1, ADDR2, ADDR3 },
+                         .peer_addresses  = { ADDR4 },
+                         .expected_result = { TRUE, TRUE, TRUE } };
+
+       test_arping_common (fixture, &info);
+}
+
+static void
+test_arping_2 (test_fixture *fixture, gconstpointer user_data)
+{
+       TestInfo info = { .addresses       = { ADDR1, ADDR2, ADDR3, ADDR4 },
+                         .peer_addresses  = { ADDR3, ADDR2 },
+                         .expected_result = { TRUE, FALSE, FALSE, TRUE } };
+
+       test_arping_common (fixture, &info);
+}
+
+static void
+fixture_teardown (test_fixture *fixture, gconstpointer user_data)
+{
+       nm_platform_link_delete (NM_PLATFORM_GET, fixture->ifindex0);
+       nm_platform_link_delete (NM_PLATFORM_GET, fixture->ifindex1);
+}
+
+void
+init_tests (int *argc, char ***argv)
+{
+       nmtst_init_with_logging (argc, argv, NULL, "ALL");
+}
+
+void
+setup_tests (void)
+{
+       g_test_add ("/arping/1", test_fixture, NULL, fixture_setup, test_arping_1, fixture_teardown);
+       g_test_add ("/arping/2", test_fixture, NULL, fixture_setup, test_arping_2, fixture_teardown);
+}