diff options
author | Dan Williams <dcbw@redhat.com> | 2015-04-09 15:39:00 -0500 |
---|---|---|
committer | Dan Williams <dcbw@redhat.com> | 2015-05-01 16:35:45 -0500 |
commit | 32058d1928d060e9531b53b107fc4786d95a4dde (patch) | |
tree | e7069aa6d982e8d73a333babc025072995d101b6 | |
parent | 527eeb6d686c31bc402530c03ed0ae8d85042591 (diff) | |
download | NetworkManager-32058d1928d060e9531b53b107fc4786d95a4dde.tar.gz |
rdisc: split fake & linux test code; add testcases
-rw-r--r-- | src/rdisc/nm-fake-rdisc.c | 377 | ||||
-rw-r--r-- | src/rdisc/nm-fake-rdisc.h | 45 | ||||
-rw-r--r-- | src/rdisc/tests/Makefile.am | 17 | ||||
-rw-r--r-- | src/rdisc/tests/test-rdisc-fake.c | 283 | ||||
-rw-r--r-- | src/rdisc/tests/test-rdisc-linux.c (renamed from src/rdisc/tests/rdisc.c) | 44 |
5 files changed, 648 insertions, 118 deletions
diff --git a/src/rdisc/nm-fake-rdisc.c b/src/rdisc/nm-fake-rdisc.c index f2efb785fa..a281b72252 100644 --- a/src/rdisc/nm-fake-rdisc.c +++ b/src/rdisc/nm-fake-rdisc.c @@ -33,113 +33,304 @@ #define error(...) nm_log_err (LOGD_IP6, __VA_ARGS__) typedef struct { - guint ra_received_id; + guint id; + guint when; + + NMRDiscDHCPLevel dhcp_level; + GArray *gateways; + GArray *addresses; + GArray *routes; + GArray *dns_servers; + GArray *dns_domains; + int hop_limit; + guint32 mtu; +} FakeRa; + +typedef struct { + guint receive_ra_id; + GSList *ras; } NMFakeRDiscPrivate; #define NM_FAKE_RDISC_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_FAKE_RDISC, NMFakeRDiscPrivate)) G_DEFINE_TYPE (NMFakeRDisc, nm_fake_rdisc, NM_TYPE_RDISC) +enum { + RS_SENT, + LAST_SIGNAL +}; +static guint signals[LAST_SIGNAL] = { 0 }; + /******************************************************************/ -static gboolean -ra_received (gpointer user_data) +static void +fake_ra_free (gpointer data) { - NMFakeRDisc *self = NM_FAKE_RDISC (user_data); - NMRDisc *rdisc = NM_RDISC (self); - NMRDiscConfigMap changed = 0; - guint32 now = nm_utils_get_monotonic_timestamp_s (); - NMRDiscGateway gateway; - NMRDiscAddress address; - NMRDiscRoute route; - NMRDiscDNSServer dns_server; - NMRDiscDNSDomain dns_domain; - - NM_FAKE_RDISC_GET_PRIVATE (self)->ra_received_id = 0; - - debug ("(%s): received router advertisement at %u", NM_RDISC (self)->ifname, now); - - rdisc->dhcp_level = NM_RDISC_DHCP_LEVEL_NONE; - - memset (&gateway, 0, sizeof (gateway)); - inet_pton (AF_INET6, "fe80::1", &gateway.address); - if (nm_rdisc_add_gateway (rdisc, &gateway)) - changed |= NM_RDISC_CONFIG_GATEWAYS; - inet_pton (AF_INET6, "fe80::2", &gateway.address); - if (nm_rdisc_add_gateway (rdisc, &gateway)) - changed |= NM_RDISC_CONFIG_GATEWAYS; - inet_pton (AF_INET6, "fe80::3", &gateway.address); - if (nm_rdisc_add_gateway (rdisc, &gateway)) - changed |= NM_RDISC_CONFIG_GATEWAYS; - - memset (&address, 0, sizeof (address)); - inet_pton (AF_INET6, "2001:db8:a:a::1", &address.address); - if (nm_rdisc_add_address (rdisc, &address)) - changed |= NM_RDISC_CONFIG_ADDRESSES; - inet_pton (AF_INET6, "2001:db8:a:a::2", &address.address); - if (nm_rdisc_add_address (rdisc, &address)) - changed |= NM_RDISC_CONFIG_ADDRESSES; - inet_pton (AF_INET6, "2001:db8:f:f::1", &address.address); - if (nm_rdisc_add_address (rdisc, &address)) - changed |= NM_RDISC_CONFIG_ADDRESSES; - - memset (&route, 0, sizeof (route)); - route.plen = 64; - inet_pton (AF_INET6, "2001:db8:a:a::", &route.network); - if (nm_rdisc_add_route (rdisc, &route)) - changed |= NM_RDISC_CONFIG_ROUTES; - inet_pton (AF_INET6, "2001:db8:b:b::", &route.network); - if (nm_rdisc_add_route (rdisc, &route)) - changed |= NM_RDISC_CONFIG_ROUTES; - - memset (&dns_server, 0, sizeof (dns_server)); - inet_pton (AF_INET6, "2001:db8:c:c::1", &dns_server.address); - if (nm_rdisc_add_dns_server (rdisc, &dns_server)) - changed |= NM_RDISC_CONFIG_DNS_SERVERS; - inet_pton (AF_INET6, "2001:db8:c:c::2", &dns_server.address); - if (nm_rdisc_add_dns_server (rdisc, &dns_server)) - changed |= NM_RDISC_CONFIG_DNS_SERVERS; - inet_pton (AF_INET6, "2001:db8:c:c::3", &dns_server.address); - if (nm_rdisc_add_dns_server (rdisc, &dns_server)) - changed |= NM_RDISC_CONFIG_DNS_SERVERS; - inet_pton (AF_INET6, "2001:db8:c:c::4", &dns_server.address); - if (nm_rdisc_add_dns_server (rdisc, &dns_server)) - changed |= NM_RDISC_CONFIG_DNS_SERVERS; - inet_pton (AF_INET6, "2001:db8:c:c::5", &dns_server.address); - if (nm_rdisc_add_dns_server (rdisc, &dns_server)) - changed |= NM_RDISC_CONFIG_DNS_SERVERS; - - memset (&dns_domain, 0, sizeof (dns_domain)); - dns_domain.domain = g_strdup ("example.net"); - if (nm_rdisc_add_dns_domain (rdisc, &dns_domain)) - changed |= NM_RDISC_CONFIG_DNS_DOMAINS; - dns_domain.domain = g_strdup ("example.com"); - if (nm_rdisc_add_dns_domain (rdisc, &dns_domain)) - changed |= NM_RDISC_CONFIG_DNS_DOMAINS; - dns_domain.domain = g_strdup ("example.org"); - if (nm_rdisc_add_dns_domain (rdisc, &dns_domain)) - changed |= NM_RDISC_CONFIG_DNS_DOMAINS; + FakeRa *ra = data; + + g_array_free (ra->gateways, TRUE); + g_array_free (ra->addresses, TRUE); + g_array_free (ra->routes, TRUE); + g_array_free (ra->dns_servers, TRUE); + g_array_free (ra->dns_domains, TRUE); + g_free (ra); +} - nm_rdisc_ra_received (NM_RDISC (self), now, changed); - return G_SOURCE_REMOVE; +static void +ra_dns_domain_free (gpointer data) +{ + g_free (((NMRDiscDNSDomain *)(data))->domain); +} + +static FakeRa * +find_ra (GSList *ras, guint id) +{ + GSList *iter; + + for (iter = ras; iter; iter = iter->next) { + if (((FakeRa *) iter->data)->id == id) + return iter->data; + } + return NULL; +} + +guint +nm_fake_rdisc_add_ra (NMFakeRDisc *self, + guint seconds_after_previous, + NMRDiscDHCPLevel dhcp_level, + int hop_limit, + guint32 mtu) +{ + NMFakeRDiscPrivate *priv = NM_FAKE_RDISC_GET_PRIVATE (self); + static guint counter = 1; + FakeRa *ra; + + ra = g_malloc0 (sizeof (*ra)); + ra->id = counter++; + ra->when = seconds_after_previous; + ra->dhcp_level = dhcp_level; + ra->hop_limit = hop_limit; + ra->mtu = mtu; + ra->gateways = g_array_new (FALSE, FALSE, sizeof (NMRDiscGateway)); + ra->addresses = g_array_new (FALSE, FALSE, sizeof (NMRDiscAddress)); + ra->routes = g_array_new (FALSE, FALSE, sizeof (NMRDiscRoute)); + ra->dns_servers = g_array_new (FALSE, FALSE, sizeof (NMRDiscDNSServer)); + ra->dns_domains = g_array_new (FALSE, FALSE, sizeof (NMRDiscDNSDomain)); + g_array_set_clear_func (ra->dns_domains, ra_dns_domain_free); + + priv->ras = g_slist_append (priv->ras, ra); + return ra->id; } +void +nm_fake_rdisc_add_gateway (NMFakeRDisc *self, + guint ra_id, + const char *addr, + guint32 timestamp, + guint32 lifetime, + NMRDiscPreference preference) +{ + NMFakeRDiscPrivate *priv = NM_FAKE_RDISC_GET_PRIVATE (self); + FakeRa *ra = find_ra (priv->ras, ra_id); + NMRDiscGateway *gw; + + g_assert (ra); + g_array_set_size (ra->gateways, ra->gateways->len + 1); + gw = &g_array_index (ra->gateways, NMRDiscGateway, ra->gateways->len - 1); + g_assert (inet_pton (AF_INET6, addr, &gw->address) == 1); + gw->timestamp = timestamp; + gw->lifetime = lifetime; + gw->preference = preference; +} + +void +nm_fake_rdisc_add_address (NMFakeRDisc *self, + guint ra_id, + const char *addr, + guint32 timestamp, + guint32 lifetime, + guint32 preferred) +{ + NMFakeRDiscPrivate *priv = NM_FAKE_RDISC_GET_PRIVATE (self); + FakeRa *ra = find_ra (priv->ras, ra_id); + NMRDiscAddress *a; + + g_assert (ra); + g_array_set_size (ra->addresses, ra->addresses->len + 1); + a = &g_array_index (ra->addresses, NMRDiscAddress, ra->addresses->len - 1); + g_assert (inet_pton (AF_INET6, addr, &a->address) == 1); + a->timestamp = timestamp; + a->lifetime = lifetime; + a->preferred = preferred; +} + +void +nm_fake_rdisc_add_route (NMFakeRDisc *self, + guint ra_id, + const char *network, + guint plen, + const char *gateway, + guint32 timestamp, + guint32 lifetime, + NMRDiscPreference preference) +{ + NMFakeRDiscPrivate *priv = NM_FAKE_RDISC_GET_PRIVATE (self); + FakeRa *ra = find_ra (priv->ras, ra_id); + NMRDiscRoute *route; + + g_assert (ra); + g_array_set_size (ra->routes, ra->routes->len + 1); + route = &g_array_index (ra->routes, NMRDiscRoute, ra->routes->len - 1); + g_assert (inet_pton (AF_INET6, network, &route->network) == 1); + g_assert (inet_pton (AF_INET6, gateway, &route->gateway) == 1); + route->plen = plen; + route->timestamp = timestamp; + route->lifetime = lifetime; + route->preference = preference; +} + +void +nm_fake_rdisc_add_dns_server (NMFakeRDisc *self, + guint ra_id, + const char *address, + guint32 timestamp, + guint32 lifetime) +{ + NMFakeRDiscPrivate *priv = NM_FAKE_RDISC_GET_PRIVATE (self); + FakeRa *ra = find_ra (priv->ras, ra_id); + NMRDiscDNSServer *dns; + + g_assert (ra); + g_array_set_size (ra->dns_servers, ra->dns_servers->len + 1); + dns = &g_array_index (ra->dns_servers, NMRDiscDNSServer, ra->dns_servers->len - 1); + g_assert (inet_pton (AF_INET6, address, &dns->address) == 1); + dns->timestamp = timestamp; + dns->lifetime = lifetime; +} + +void +nm_fake_rdisc_add_dns_domain (NMFakeRDisc *self, + guint ra_id, + const char *domain, + guint32 timestamp, + guint32 lifetime) +{ + NMFakeRDiscPrivate *priv = NM_FAKE_RDISC_GET_PRIVATE (self); + FakeRa *ra = find_ra (priv->ras, ra_id); + NMRDiscDNSDomain *dns; + + g_assert (ra); + g_array_set_size (ra->dns_domains, ra->dns_domains->len + 1); + dns = &g_array_index (ra->dns_domains, NMRDiscDNSDomain, ra->dns_domains->len - 1); + dns->domain = g_strdup (domain); + dns->timestamp = timestamp; + dns->lifetime = lifetime; +} + +gboolean +nm_fake_rdisc_done (NMFakeRDisc *self) +{ + return !NM_FAKE_RDISC_GET_PRIVATE (self)->ras; +} + +/******************************************************************/ + static gboolean send_rs (NMRDisc *rdisc) { - NMFakeRDiscPrivate *priv = NM_FAKE_RDISC_GET_PRIVATE (rdisc); + g_signal_emit (rdisc, signals[RS_SENT], 0); + return TRUE; +} - if (priv->ra_received_id) - g_source_remove (priv->ra_received_id); - priv->ra_received_id = g_timeout_add_seconds (3, ra_received, rdisc); +static gboolean +receive_ra (gpointer user_data) +{ + NMFakeRDisc *self = user_data; + NMFakeRDiscPrivate *priv = NM_FAKE_RDISC_GET_PRIVATE (self); + NMRDisc *rdisc = NM_RDISC (self); + FakeRa *ra = priv->ras->data; + NMRDiscConfigMap changed = 0; + guint32 now = nm_utils_get_monotonic_timestamp_s (); + guint i; - return TRUE; + priv->receive_ra_id = 0; + + if (rdisc->dhcp_level != ra->dhcp_level) { + rdisc->dhcp_level = ra->dhcp_level; + changed |= NM_RDISC_CONFIG_DHCP_LEVEL; + } + + for (i = 0; i < ra->gateways->len; i++) { + NMRDiscGateway *item = &g_array_index (ra->gateways, NMRDiscGateway, i); + + if (nm_rdisc_add_gateway (rdisc, item)) + changed |= NM_RDISC_CONFIG_GATEWAYS; + } + + for (i = 0; i < ra->addresses->len; i++) { + NMRDiscAddress *item = &g_array_index (ra->addresses, NMRDiscAddress, i); + + if (nm_rdisc_add_address (rdisc, item)) + changed |= NM_RDISC_CONFIG_ADDRESSES; + } + + for (i = 0; i < ra->routes->len; i++) { + NMRDiscRoute *item = &g_array_index (ra->routes, NMRDiscRoute, i); + + if (nm_rdisc_add_route (rdisc, item)) + changed |= NM_RDISC_CONFIG_ROUTES; + } + + for (i = 0; i < ra->dns_servers->len; i++) { + NMRDiscDNSServer *item = &g_array_index (ra->dns_servers, NMRDiscDNSServer, i); + + if (nm_rdisc_add_dns_server (rdisc, item)) + changed |= NM_RDISC_CONFIG_DNS_SERVERS; + } + + for (i = 0; i < ra->dns_domains->len; i++) { + NMRDiscDNSDomain *item = &g_array_index (ra->dns_domains, NMRDiscDNSDomain, i); + + if (nm_rdisc_add_dns_domain (rdisc, item)) + changed |= NM_RDISC_CONFIG_DNS_DOMAINS; + } + + if (rdisc->mtu != ra->mtu) { + rdisc->mtu = ra->mtu; + changed |= NM_RDISC_CONFIG_MTU; + } + + if (rdisc->hop_limit != ra->hop_limit) { + rdisc->hop_limit = ra->hop_limit; + changed |= NM_RDISC_CONFIG_HOP_LIMIT; + } + + priv->ras = g_slist_remove (priv->ras, priv->ras->data); + fake_ra_free (ra); + + nm_rdisc_ra_received (NM_RDISC (self), now, changed); + + /* Schedule next RA */ + if (priv->ras) { + ra = priv->ras->data; + priv->receive_ra_id = g_timeout_add_seconds (ra->when, receive_ra, self); + } + + return G_SOURCE_REMOVE; } static void start (NMRDisc *rdisc) { - nm_rdisc_solicit (rdisc); + NMFakeRDiscPrivate *priv = NM_FAKE_RDISC_GET_PRIVATE (rdisc); + FakeRa *ra; + + /* Queue up the first fake RA */ + g_assert (priv->ras); + ra = NM_FAKE_RDISC_GET_PRIVATE (rdisc)->ras->data; + + g_assert (!priv->receive_ra_id); + priv->receive_ra_id = g_timeout_add_seconds (ra->when, receive_ra, rdisc); } /******************************************************************/ @@ -168,11 +359,14 @@ dispose (GObject *object) { NMFakeRDiscPrivate *priv = NM_FAKE_RDISC_GET_PRIVATE (object); - if (priv->ra_received_id) { - g_source_remove (priv->ra_received_id); - priv->ra_received_id = 0; + if (priv->receive_ra_id) { + g_source_remove (priv->receive_ra_id); + priv->receive_ra_id = 0; } + g_slist_free_full (priv->ras, fake_ra_free); + priv->ras = NULL; + G_OBJECT_CLASS (nm_fake_rdisc_parent_class)->dispose (object); } @@ -187,4 +381,11 @@ nm_fake_rdisc_class_init (NMFakeRDiscClass *klass) object_class->dispose = dispose; rdisc_class->start = start; rdisc_class->send_rs = send_rs; + + signals[RS_SENT] = g_signal_new ( + NM_FAKE_RDISC_RS_SENT, + G_OBJECT_CLASS_TYPE (klass), + G_SIGNAL_RUN_FIRST, + 0, NULL, NULL, NULL, + G_TYPE_NONE, 0); } diff --git a/src/rdisc/nm-fake-rdisc.h b/src/rdisc/nm-fake-rdisc.h index b4eff7ecf9..5a8daefb21 100644 --- a/src/rdisc/nm-fake-rdisc.h +++ b/src/rdisc/nm-fake-rdisc.h @@ -30,7 +30,7 @@ #define NM_IS_FAKE_RDISC_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), NM_TYPE_FAKE_RDISC)) #define NM_FAKE_RDISC_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_FAKE_RDISC, NMFakeRDiscClass)) -/******************************************************************/ +#define NM_FAKE_RDISC_RS_SENT "rs-sent" typedef struct { NMRDisc parent; @@ -46,4 +46,47 @@ GType nm_fake_rdisc_get_type (void); NMRDisc *nm_fake_rdisc_new (int ifindex, const char *ifname); +guint nm_fake_rdisc_add_ra (NMFakeRDisc *self, + guint seconds, + NMRDiscDHCPLevel dhcp_level, + int hop_limit, + guint32 mtu); + +void nm_fake_rdisc_add_gateway (NMFakeRDisc *self, + guint ra_id, + const char *addr, + guint32 timestamp, + guint32 lifetime, + NMRDiscPreference preference); + +void nm_fake_rdisc_add_address (NMFakeRDisc *self, + guint ra_id, + const char *addr, + guint32 timestamp, + guint32 lifetime, + guint32 preferred); + +void nm_fake_rdisc_add_route (NMFakeRDisc *self, + guint ra_id, + const char *network, + guint plen, + const char *gateway, + guint32 timestamp, + guint32 lifetime, + NMRDiscPreference preference); + +void nm_fake_rdisc_add_dns_server (NMFakeRDisc *self, + guint ra_id, + const char *address, + guint32 timestamp, + guint32 lifetime); + +void nm_fake_rdisc_add_dns_domain (NMFakeRDisc *self, + guint ra_id, + const char *domain, + guint32 timestamp, + guint32 lifetime); + +gboolean nm_fake_rdisc_done (NMFakeRDisc *self); + #endif /* __NETWORKMANAGER_FAKE_RDISC_H__ */ diff --git a/src/rdisc/tests/Makefile.am b/src/rdisc/tests/Makefile.am index e6fe136968..5d6f8b5bcb 100644 --- a/src/rdisc/tests/Makefile.am +++ b/src/rdisc/tests/Makefile.am @@ -20,11 +20,18 @@ AM_LDFLAGS = \ @GNOME_CODE_COVERAGE_RULES@ -noinst_PROGRAMS = \ - rdisc +noinst_PROGRAMS = test-rdisc-linux test-rdisc-fake -rdisc_SOURCES = \ - rdisc.c -rdisc_LDADD = \ +test_rdisc_linux_SOURCES = \ + test-rdisc-linux.c +test_rdisc_linux_LDADD = \ $(top_builddir)/src/libNetworkManager.la +test_rdisc_fake_SOURCES = \ + test-rdisc-fake.c +test_rdisc_fake_LDADD = \ + $(top_builddir)/src/libNetworkManager.la + +@VALGRIND_RULES@ +TESTS = test-rdisc-fake + diff --git a/src/rdisc/tests/test-rdisc-fake.c b/src/rdisc/tests/test-rdisc-fake.c new file mode 100644 index 0000000000..e15bad4bb6 --- /dev/null +++ b/src/rdisc/tests/test-rdisc-fake.c @@ -0,0 +1,283 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* rdisc.c - test program + * + * 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 <string.h> +#include <syslog.h> + +#include "nm-rdisc.h" +#include "nm-fake-rdisc.h" +#include "nm-logging.h" + +#include "nm-fake-platform.h" + +#include "nm-test-utils.h" + +static NMFakeRDisc * +rdisc_new (void) +{ + NMRDisc *rdisc; + const int ifindex = 1; + const char *ifname = nm_platform_link_get_name (NM_PLATFORM_GET, ifindex); + + rdisc = nm_fake_rdisc_new (ifindex, ifname); + g_assert (rdisc); + return NM_FAKE_RDISC (rdisc); +} + +static void +match_gateway (GArray *array, guint idx, const char *addr, guint32 ts, guint32 lt, NMRDiscPreference pref) +{ + NMRDiscGateway *gw = &g_array_index (array, NMRDiscGateway, idx); + char buf[INET6_ADDRSTRLEN]; + + g_assert_cmpstr (inet_ntop (AF_INET6, &gw->address, buf, sizeof (buf)), ==, addr); + g_assert_cmpint (gw->timestamp, ==, ts); + g_assert_cmpint (gw->lifetime, ==, lt); + g_assert_cmpint (gw->preference, ==, pref); +} + +static void +match_address (GArray *array, guint idx, const char *addr, guint32 ts, guint32 lt, guint32 preferred) +{ + NMRDiscAddress *a = &g_array_index (array, NMRDiscAddress, idx); + char buf[INET6_ADDRSTRLEN]; + + g_assert_cmpstr (inet_ntop (AF_INET6, &a->address, buf, sizeof (buf)), ==, addr); + g_assert_cmpint (a->timestamp, ==, ts); + g_assert_cmpint (a->lifetime, ==, lt); + g_assert_cmpint (a->preferred, ==, preferred); +} + +static void +match_route (GArray *array, guint idx, const char *nw, int plen, const char *gw, guint32 ts, guint32 lt, NMRDiscPreference pref) +{ + NMRDiscRoute *route = &g_array_index (array, NMRDiscRoute, idx); + char buf[INET6_ADDRSTRLEN]; + + g_assert_cmpstr (inet_ntop (AF_INET6, &route->network, buf, sizeof (buf)), ==, nw); + g_assert_cmpint (route->plen, ==, plen); + g_assert_cmpstr (inet_ntop (AF_INET6, &route->gateway, buf, sizeof (buf)), ==, gw); + g_assert_cmpint (route->timestamp, ==, ts); + g_assert_cmpint (route->lifetime, ==, lt); + g_assert_cmpint (route->preference, ==, pref); +} + +static void +match_dns_server (GArray *array, guint idx, const char *addr, guint32 ts, guint32 lt) +{ + NMRDiscDNSServer *dns = &g_array_index (array, NMRDiscDNSServer, idx); + char buf[INET6_ADDRSTRLEN]; + + g_assert_cmpstr (inet_ntop (AF_INET6, &dns->address, buf, sizeof (buf)), ==, addr); + g_assert_cmpint (dns->timestamp, ==, ts); + g_assert_cmpint (dns->lifetime, ==, lt); +} + +static void +match_dns_domain (GArray *array, guint idx, const char *domain, guint32 ts, guint32 lt) +{ + NMRDiscDNSDomain *dns = &g_array_index (array, NMRDiscDNSDomain, idx); + + g_assert_cmpstr (dns->domain, ==, domain); + g_assert_cmpint (dns->timestamp, ==, ts); + g_assert_cmpint (dns->lifetime, ==, lt); +} + +typedef struct { + GMainLoop *loop; + guint counter; + guint rs_counter; + guint32 timestamp1; +} TestData; + +static void +test_simple_changed (NMRDisc *rdisc, NMRDiscConfigMap changed, TestData *data) +{ + g_assert_cmpint (changed, ==, NM_RDISC_CONFIG_DHCP_LEVEL | + NM_RDISC_CONFIG_GATEWAYS | + NM_RDISC_CONFIG_ADDRESSES | + NM_RDISC_CONFIG_ROUTES | + NM_RDISC_CONFIG_DNS_SERVERS | + NM_RDISC_CONFIG_DNS_DOMAINS | + NM_RDISC_CONFIG_HOP_LIMIT | + NM_RDISC_CONFIG_MTU); + g_assert_cmpint (rdisc->dhcp_level, ==, NM_RDISC_DHCP_LEVEL_OTHERCONF); + match_gateway (rdisc->gateways, 0, "fe80::1", data->timestamp1, 10, NM_RDISC_PREFERENCE_MEDIUM); + match_address (rdisc->addresses, 0, "2001:db8:a:a::1", data->timestamp1, 10, 10); + match_route (rdisc->routes, 0, "2001:db8:a:a::", 64, "fe80::1", data->timestamp1, 10, 10); + match_dns_server (rdisc->dns_servers, 0, "2001:db8:c:c::1", data->timestamp1, 10); + match_dns_domain (rdisc->dns_domains, 0, "foobar.com", data->timestamp1, 10); + + g_assert (nm_fake_rdisc_done (NM_FAKE_RDISC (rdisc))); + data->counter++; + g_main_loop_quit (data->loop); +} + +static void +test_simple (void) +{ + NMFakeRDisc *rdisc = rdisc_new (); + guint32 now = nm_utils_get_monotonic_timestamp_s (); + TestData data = { g_main_loop_new (NULL, FALSE), 0, 0, now }; + guint id; + + id = nm_fake_rdisc_add_ra (rdisc, 1, NM_RDISC_DHCP_LEVEL_OTHERCONF, 4, 1500); + g_assert (id); + nm_fake_rdisc_add_gateway (rdisc, id, "fe80::1", now, 10, NM_RDISC_PREFERENCE_MEDIUM); + nm_fake_rdisc_add_address (rdisc, id, "2001:db8:a:a::1", now, 10, 10); + nm_fake_rdisc_add_route (rdisc, id, "2001:db8:a:a::", 64, "fe80::1", now, 10, 10); + nm_fake_rdisc_add_dns_server (rdisc, id, "2001:db8:c:c::1", now, 10); + nm_fake_rdisc_add_dns_domain (rdisc, id, "foobar.com", now, 10); + + g_signal_connect (rdisc, + NM_RDISC_CONFIG_CHANGED, + G_CALLBACK (test_simple_changed), + &data); + + nm_rdisc_start (NM_RDISC (rdisc)); + g_main_loop_run (data.loop); + g_assert_cmpint (data.counter, ==, 1); + + g_object_unref (rdisc); + g_main_loop_unref (data.loop); +} + +static void +test_everything_rs_sent (NMRDisc *rdisc, TestData *data) +{ + g_assert_cmpint (data->rs_counter, ==, 0); + data->rs_counter++; +} + +static void +test_everything_changed (NMRDisc *rdisc, NMRDiscConfigMap changed, TestData *data) +{ + if (data->counter == 0) { + g_assert_cmpint (data->rs_counter, ==, 1); + g_assert_cmpint (changed, ==, NM_RDISC_CONFIG_DHCP_LEVEL | + NM_RDISC_CONFIG_GATEWAYS | + NM_RDISC_CONFIG_ADDRESSES | + NM_RDISC_CONFIG_ROUTES | + NM_RDISC_CONFIG_DNS_SERVERS | + NM_RDISC_CONFIG_DNS_DOMAINS | + NM_RDISC_CONFIG_HOP_LIMIT | + NM_RDISC_CONFIG_MTU); + match_gateway (rdisc->gateways, 0, "fe80::1", data->timestamp1, 10, NM_RDISC_PREFERENCE_MEDIUM); + match_address (rdisc->addresses, 0, "2001:db8:a:a::1", data->timestamp1, 10, 10); + match_route (rdisc->routes, 0, "2001:db8:a:a::", 64, "fe80::1", data->timestamp1, 10, 10); + match_dns_server (rdisc->dns_servers, 0, "2001:db8:c:c::1", data->timestamp1, 10); + match_dns_domain (rdisc->dns_domains, 0, "foobar.com", data->timestamp1, 10); + } else if (data->counter == 1) { + g_assert_cmpint (changed, ==, NM_RDISC_CONFIG_GATEWAYS | + NM_RDISC_CONFIG_ADDRESSES | + NM_RDISC_CONFIG_ROUTES | + NM_RDISC_CONFIG_DNS_SERVERS | + NM_RDISC_CONFIG_DNS_DOMAINS); + + g_assert_cmpint (rdisc->gateways->len, ==, 1); + match_gateway (rdisc->gateways, 0, "fe80::2", data->timestamp1, 10, NM_RDISC_PREFERENCE_MEDIUM); + g_assert_cmpint (rdisc->addresses->len, ==, 1); + match_address (rdisc->addresses, 0, "2001:db8:a:a::2", data->timestamp1, 10, 10); + g_assert_cmpint (rdisc->routes->len, ==, 1); + match_route (rdisc->routes, 0, "2001:db8:a:b::", 64, "fe80::2", data->timestamp1, 10, 10); + g_assert_cmpint (rdisc->dns_servers->len, ==, 1); + match_dns_server (rdisc->dns_servers, 0, "2001:db8:c:c::2", data->timestamp1, 10); + g_assert_cmpint (rdisc->dns_domains->len, ==, 1); + match_dns_domain (rdisc->dns_domains, 0, "foobar2.com", data->timestamp1, 10); + + g_assert (nm_fake_rdisc_done (NM_FAKE_RDISC (rdisc))); + g_main_loop_quit (data->loop); + } else + g_assert_not_reached (); + + data->counter++; +} + +static void +test_everything (void) +{ + NMFakeRDisc *rdisc = rdisc_new (); + guint32 now = nm_utils_get_monotonic_timestamp_s (); + TestData data = { g_main_loop_new (NULL, FALSE), 0, 0, now }; + guint id; + + id = nm_fake_rdisc_add_ra (rdisc, 1, NM_RDISC_DHCP_LEVEL_NONE, 4, 1500); + g_assert (id); + nm_fake_rdisc_add_gateway (rdisc, id, "fe80::1", now, 10, NM_RDISC_PREFERENCE_MEDIUM); + nm_fake_rdisc_add_address (rdisc, id, "2001:db8:a:a::1", now, 10, 10); + nm_fake_rdisc_add_route (rdisc, id, "2001:db8:a:a::", 64, "fe80::1", now, 10, 10); + nm_fake_rdisc_add_dns_server (rdisc, id, "2001:db8:c:c::1", now, 10); + nm_fake_rdisc_add_dns_domain (rdisc, id, "foobar.com", now, 10); + + /* expire everything from the first RA in the second */ + id = nm_fake_rdisc_add_ra (rdisc, 1, NM_RDISC_DHCP_LEVEL_NONE, 4, 1500); + g_assert (id); + nm_fake_rdisc_add_gateway (rdisc, id, "fe80::1", now, 0, NM_RDISC_PREFERENCE_MEDIUM); + nm_fake_rdisc_add_address (rdisc, id, "2001:db8:a:a::1", now, 0, 0); + nm_fake_rdisc_add_route (rdisc, id, "2001:db8:a:a::", 64, "fe80::1", now, 0, 0); + nm_fake_rdisc_add_dns_server (rdisc, id, "2001:db8:c:c::1", now, 0); + nm_fake_rdisc_add_dns_domain (rdisc, id, "foobar.com", now, 0); + + /* and add some new stuff */ + nm_fake_rdisc_add_gateway (rdisc, id, "fe80::2", now, 10, NM_RDISC_PREFERENCE_MEDIUM); + nm_fake_rdisc_add_address (rdisc, id, "2001:db8:a:a::2", now, 10, 10); + nm_fake_rdisc_add_route (rdisc, id, "2001:db8:a:b::", 64, "fe80::2", now, 10, 10); + nm_fake_rdisc_add_dns_server (rdisc, id, "2001:db8:c:c::2", now, 10); + nm_fake_rdisc_add_dns_domain (rdisc, id, "foobar2.com", now, 10); + + g_signal_connect (rdisc, + NM_RDISC_CONFIG_CHANGED, + G_CALLBACK (test_everything_changed), + &data); + g_signal_connect (rdisc, + NM_FAKE_RDISC_RS_SENT, + G_CALLBACK (test_everything_rs_sent), + &data); + + nm_rdisc_start (NM_RDISC (rdisc)); + g_main_loop_run (data.loop); + g_assert_cmpint (data.counter, ==, 2); + g_assert_cmpint (data.rs_counter, ==, 1); + + g_object_unref (rdisc); + g_main_loop_unref (data.loop); +} + +NMTST_DEFINE (); + +int +main (int argc, char **argv) +{ + nmtst_init_with_logging (&argc, &argv, NULL, "DEFAULT"); + + if (nmtst_test_quick ()) { + g_print ("Skipping test: don't run long running test %s (NMTST_DEBUG=slow)\n", str_if_set (g_get_prgname (), "test-rdisc-fake")); + return EXIT_SKIP; + } + + nm_fake_platform_setup (); + + g_test_add_func ("/rdisc/simple", test_simple); + g_test_add_func ("/rdisc/everything-changed", test_everything); + + return g_test_run (); +} diff --git a/src/rdisc/tests/rdisc.c b/src/rdisc/tests/test-rdisc-linux.c index e05f34f970..eddb3b0eb4 100644 --- a/src/rdisc/tests/rdisc.c +++ b/src/rdisc/tests/test-rdisc-linux.c @@ -24,51 +24,47 @@ #include <syslog.h> #include "nm-rdisc.h" -#include "nm-fake-rdisc.h" #include "nm-lndp-rdisc.h" #include "nm-logging.h" -#include "nm-fake-platform.h" #include "nm-linux-platform.h" +#include "nm-test-utils.h" + +NMTST_DEFINE (); + int main (int argc, char **argv) { GMainLoop *loop; NMRDisc *rdisc; - NMRDisc *(*new) (int ifindex, const char *ifname); int ifindex = 1; const char *ifname; -#if !GLIB_CHECK_VERSION (2, 35, 0) - g_type_init (); -#endif + nmtst_init_with_logging (&argc, &argv, NULL, "DEFAULT"); - loop = g_main_loop_new (NULL, FALSE); - nm_logging_setup ("debug", "ip6", NULL, NULL); - openlog (G_LOG_DOMAIN, LOG_CONS | LOG_PERROR, LOG_DAEMON); - - argv++; - if (!g_strcmp0 (argv[0], "--fake")) { - new = nm_fake_rdisc_new; - nm_fake_platform_setup (); - argv++; - } else { - new = nm_lndp_rdisc_new; - nm_linux_platform_setup (); + if (getuid () != 0) { + g_print ("Missing permission: must run as root\n"); + return EXIT_FAILURE; } - if (argv[0]) { - ifname = argv[0]; + loop = g_main_loop_new (NULL, FALSE); + + nm_linux_platform_setup (); + + if (argv[1]) { + ifname = argv[1]; ifindex = nm_platform_link_get_ifindex (NM_PLATFORM_GET, ifname); } else { - ifindex = 1; - ifname = nm_platform_link_get_name (NM_PLATFORM_GET, ifindex); + g_print ("Missing command line argument \"interface-name\"\n"); + return EXIT_FAILURE; } - rdisc = new (ifindex, ifname); - if (!rdisc) + rdisc = nm_lndp_rdisc_new (ifindex, ifname); + if (!rdisc) { + g_print ("Failed to create NMRDisc instance\n"); return EXIT_FAILURE; + } nm_rdisc_start (rdisc); g_main_loop_run (loop); |