diff options
author | Beniamino Galvani <bgalvani@redhat.com> | 2015-11-04 11:29:09 +0100 |
---|---|---|
committer | Beniamino Galvani <bgalvani@redhat.com> | 2015-11-04 13:39:37 +0100 |
commit | d195ee98a6eac38a61f488a880b101b48812e9ee (patch) | |
tree | 2e613d1ffa814c754c094f779f58f7c0a1736ee8 | |
parent | 3f7e08d06ca16a326a9b269d58c26383716011f6 (diff) | |
download | NetworkManager-bg/lldp-test-bgo757005.tar.gz |
fixup! lldp: add test casebg/lldp-test-bgo757005
-rw-r--r-- | src/devices/tests/Makefile.am | 39 | ||||
-rw-r--r-- | src/devices/tests/test-lldp.c | 102 |
2 files changed, 66 insertions, 75 deletions
diff --git a/src/devices/tests/Makefile.am b/src/devices/tests/Makefile.am index a2f8779b2f..4b1595e7b4 100644 --- a/src/devices/tests/Makefile.am +++ b/src/devices/tests/Makefile.am @@ -1,24 +1,20 @@ AM_CPPFLAGS = \ - -I${top_srcdir} \ - -I${top_srcdir}/include \ - -I${top_builddir}/include \ - -I${top_srcdir}/src \ - -I${top_builddir}/src \ - -I${top_srcdir}/libnm-core \ - -I${top_builddir}/libnm-core \ - -I${srcdir}/.. \ + -I$(top_srcdir)/include \ + -I$(top_builddir)/include \ + -I$(top_srcdir)/libnm-core \ + -I$(top_builddir)/libnm-core \ + -I$(top_srcdir)/src/platform \ + -I$(top_srcdir)/src/devices \ + -I$(top_srcdir)/src/systemd/ \ + -I$(top_srcdir)/src/systemd/src/systemd/ \ + -I$(top_srcdir)/src/systemd/src/libsystemd-network/ \ + -I$(top_srcdir)/src \ + -I$(top_builddir)/src \ -DG_LOG_DOMAIN=\""NetworkManager"\" \ -DNETWORKMANAGER_COMPILATION=NM_NETWORKMANAGER_COMPILATION_INSIDE_DAEMON \ -DNM_VERSION_MAX_ALLOWED=NM_VERSION_NEXT_STABLE \ - $(GLIB_CFLAGS) \ + $(GLIB_CFLAGS) -if REQUIRE_ROOT_TESTS -AM_CPPFLAGS += -DREQUIRE_ROOT_TESTS=1 -endif - -DEVICES_SOURCES = \ - ../nm-lldp-listener.c \ -TEST_SOURCES = $(DEVICE_SOURCES) AM_CFLAGS = $(CODE_COVERAGE_CFLAGS) AM_LDFLAGS = $(GLIB_LIBS) $(CODE_COVERAGE_LDFLAGS) DEVICES_LDADD = \ @@ -29,7 +25,16 @@ DEVICES_LDADD = \ noinst_PROGRAMS = \ test-lldp -test_lldp_SOURCES = test-lldp.c $(TEST_SOURCES) +test_lldp_SOURCES = \ + test-lldp.c \ + ../nm-lldp-listener.c \ + $(top_srcdir)/src/platform/tests/test-common.c + +test_lldp_CPPFLAGS = \ + $(AM_CPPFLAGS) \ + -I$(top_srcdir)/src/platform/tests \ + -DSETUP=nm_linux_platform_setup + test_lldp_LDADD = $(DEVICES_LDADD) @VALGRIND_RULES@ diff --git a/src/devices/tests/test-lldp.c b/src/devices/tests/test-lldp.c index 54c6227111..8d73ac150d 100644 --- a/src/devices/tests/test-lldp.c +++ b/src/devices/tests/test-lldp.c @@ -22,54 +22,49 @@ #include <fcntl.h> #include <linux/if_tun.h> -#include <net/if.h> #include <sys/ioctl.h> #include <sys/stat.h> #include <sys/types.h> #include "nm-default.h" #include "nm-lldp-listener.h" +#include "test-common.h" -#include "nm-test-utils.h" +typedef struct { + int ifindex; + int fd; + guint8 mac[ETH_ALEN]; +} test_fixture; + +#define TEST_IFNAME "nm-tap-test0" -static int -create_tap (char *name, int *out_ifindex, guint8 *out_mac) +static void +fixture_setup (test_fixture *fixture, gconstpointer user_data) { + const NMPlatformLink *link; struct ifreq ifr = { }; - int fd, tmp = -1; + int fd, s; fd = open ("/dev/net/tun", O_RDWR); - if (fd < 0) - return -1; + g_assert (fd >= 0); ifr.ifr_flags = IFF_TAP | IFF_NO_PI; - strncpy (ifr.ifr_name, name, IFNAMSIZ); - if (ioctl (fd, TUNSETIFF, &ifr) < 0) - goto out_err; - - *out_ifindex = if_nametoindex (name); + strncpy (ifr.ifr_name, TEST_IFNAME, IFNAMSIZ); + g_assert (ioctl (fd, TUNSETIFF, &ifr) >= 0); /* Bring the interface up */ - tmp = socket (AF_INET, SOCK_DGRAM, 0); + s = socket (AF_INET, SOCK_DGRAM, 0); + g_assert (s >= 0); ifr.ifr_flags |= IFF_UP; - if (ioctl (tmp, SIOCSIFFLAGS, &ifr) < 0) - goto out_err; - - /* Get its MAC address */ - if (ioctl (tmp, SIOCGIFHWADDR, &ifr) < 0) - goto out_err; - - memcpy (out_mac, ifr.ifr_addr.sa_data, ETH_ALEN); - close (tmp); - - return fd; - -out_err: - if (fd >= 0) - close (fd); - if (tmp >= 0) - close (tmp); - return -1; + g_assert (ioctl (s, SIOCSIFFLAGS, &ifr) >= 0); + close (s); + + nm_platform_process_events (NM_PLATFORM_GET); + link = nm_platform_link_get_by_ifname (NM_PLATFORM_GET, TEST_IFNAME); + g_assert (link); + fixture->ifindex = link->ifindex; + fixture->fd = fd; + memcpy (fixture->mac, link->addr.data, ETH_ALEN); } typedef struct { @@ -140,11 +135,9 @@ get_lldp_neighbor_attribute (GVariant *neighbors, } static void -test_receive_frame (void) +test_receive_frame (test_fixture *fixture, gconstpointer user_data) { NMLldpListener *listener; - int fd, ifindex = -1; - guint8 mac[ETH_ALEN]; GMainLoop *loop; TestInfo info = { }; GVariant *neighbors, *attr; @@ -165,21 +158,17 @@ test_receive_frame (void) 0x00, 0x00 /* End Of LLDPDU */ }; - fd = create_tap ("nm-test-tap", &ifindex, mac); - g_assert_cmpint (fd, >=, 0); - g_assert_cmpint (ifindex, >=, 0); - listener = nm_lldp_listener_new (); g_assert (listener != NULL); - g_assert (nm_lldp_listener_start (listener, ifindex, "nm-test-tap", mac, ETH_ALEN, NULL)); + g_assert (nm_lldp_listener_start (listener, fixture->ifindex, TEST_IFNAME, fixture->mac, ETH_ALEN, NULL)); g_signal_connect (listener, "notify::" NM_LLDP_LISTENER_NEIGHBORS, (GCallback) lldp_neighbors_changed, &info); loop = g_main_loop_new (NULL, FALSE); g_timeout_add_seconds (1, loop_quit, loop); - g_assert (write (fd, frame, sizeof (frame)) == sizeof (frame)); - g_assert (write (fd, frame, sizeof (frame)) == sizeof (frame)); + g_assert (write (fixture->fd, frame, sizeof (frame)) == sizeof (frame)); + g_assert (write (fixture->fd, frame, sizeof (frame)) == sizeof (frame)); g_main_loop_run (loop); @@ -212,24 +201,21 @@ test_receive_frame (void) g_clear_pointer (&loop, g_main_loop_unref); } -NMTST_DEFINE (); - -int -main (int argc, char **argv) +static void +fixture_teardown (test_fixture *fixture, gconstpointer user_data) { - nmtst_init_assert_logging (&argc, &argv, "INFO", "DEFAULT"); - - if (geteuid () != 0 || getegid () != 0) { -#ifdef REQUIRE_ROOT_TESTS - g_print ("Fail test: requires root privileges (%s)\n", argv[0]); - return EXIT_FAILURE; -#else - g_print ("Skipping test: requires root privileges (%s)\n", argv[0]); - return g_test_run (); -#endif - } + nm_platform_link_delete (NM_PLATFORM_GET, fixture->ifindex); +} - g_test_add_func ("/lldp/receive_frame", test_receive_frame); +void +init_tests (int *argc, char ***argv) +{ + nmtst_init_assert_logging (argc, argv, "WARN", "ALL"); +} - return g_test_run (); +void +setup_tests (void) +{ + g_test_add ("/lldp/receive_frame", test_fixture, NULL, fixture_setup, + test_receive_frame, fixture_teardown); } |