diff --git a/shared/nm-utils/nm-test-utils.h b/shared/nm-utils/nm-test-utils.h index 0dfdfce6c6..055542bb70 100644 --- a/shared/nm-utils/nm-test-utils.h +++ b/shared/nm-utils/nm-test-utils.h @@ -647,13 +647,18 @@ nmtst_test_quick (void) typedef struct _NmtstTestData NmtstTestData; -typedef void (*NmtstTestDataRelease) (const NmtstTestData *test_data); +typedef void (*NmtstTestHandler) (const NmtstTestData *test_data); struct _NmtstTestData { - const char *testpath; - NmtstTestDataRelease fcn_release; + union { + const char *testpath; + char *_testpath; + }; gsize n_args; - gpointer args[1]; + gpointer *args; + NmtstTestHandler _func_setup; + GTestDataFunc _func_test; + NmtstTestHandler _func_teardown; }; static inline void @@ -670,8 +675,8 @@ _nmtst_test_data_unpack (const NmtstTestData *test_data, gsize n_args, ...) for (i = 0; i < n_args; i++) { p = va_arg (ap, gpointer *); - g_assert (p); - *p = test_data->args[i]; + if (p) + *p = test_data->args[i]; } va_end (ap); } @@ -684,25 +689,42 @@ _nmtst_test_data_free (gpointer data) g_assert (test_data); - if (test_data->fcn_release) - test_data->fcn_release (test_data); - - g_free ((gpointer) test_data->testpath); + g_free (test_data->_testpath); g_free (test_data); } static inline void -_nmtst_add_test_func_full (const char *testpath, GTestDataFunc test_func, NmtstTestDataRelease fcn_release, gsize n_args, ...) +_nmtst_test_run (gconstpointer data) +{ + const NmtstTestData *test_data = data; + + if (test_data->_func_setup) + test_data->_func_setup (test_data); + + test_data->_func_test (test_data); + + if (test_data->_func_teardown) + test_data->_func_teardown (test_data); +} + +static inline void +_nmtst_add_test_func_full (const char *testpath, GTestDataFunc func_test, NmtstTestHandler func_setup, NmtstTestHandler func_teardown, gsize n_args, ...) { gsize i; NmtstTestData *data; va_list ap; - data = g_malloc (G_STRUCT_OFFSET (NmtstTestData, args) + sizeof (gpointer) * (n_args + 1)); + g_assert (testpath && testpath[0]); + g_assert (func_test); - data->testpath = g_strdup (testpath); - data->fcn_release = fcn_release; + data = g_malloc0 (sizeof (NmtstTestData) + (sizeof (gpointer) * (n_args + 1))); + + data->_testpath = g_strdup (testpath); + data->_func_test = func_test; + data->_func_setup = func_setup; + data->_func_teardown = func_teardown; data->n_args = n_args; + data->args = (gpointer) &data[1]; va_start (ap, n_args); for (i = 0; i < n_args; i++) data->args[i] = va_arg (ap, gpointer); @@ -711,11 +733,11 @@ _nmtst_add_test_func_full (const char *testpath, GTestDataFunc test_func, NmtstT g_test_add_data_func_full (testpath, data, - test_func, + _nmtst_test_run, _nmtst_test_data_free); } -#define nmtst_add_test_func_full(testpath, test_func, fcn_release, ...) _nmtst_add_test_func_full(testpath, test_func, fcn_release, NM_NARG (__VA_ARGS__), ##__VA_ARGS__) -#define nmtst_add_test_func(testpath, test_func, ...) nmtst_add_test_func_full(testpath, test_func, NULL, ##__VA_ARGS__) +#define nmtst_add_test_func_full(testpath, func_test, func_setup, func_teardown, ...) _nmtst_add_test_func_full(testpath, func_test, func_setup, func_teardown, NM_NARG (__VA_ARGS__), ##__VA_ARGS__) +#define nmtst_add_test_func(testpath, func_test, ...) nmtst_add_test_func_full(testpath, func_test, NULL, NULL, ##__VA_ARGS__) /*****************************************************************************/ diff --git a/src/devices/nm-device-veth.c b/src/devices/nm-device-veth.c index 11916c59f3..a8c4bcc855 100644 --- a/src/devices/nm-device-veth.c +++ b/src/devices/nm-device-veth.c @@ -37,12 +37,8 @@ _LOG_DECLARE_SELF(NMDeviceVeth); /*****************************************************************************/ -typedef struct { -} NMDeviceVethPrivate; - struct _NMDeviceVeth { NMDeviceEthernet parent; - NMDeviceVethPrivate _priv; }; struct _NMDeviceVethClass { @@ -57,8 +53,6 @@ NM_GOBJECT_PROPERTIES_DEFINE (NMDeviceVeth, G_DEFINE_TYPE (NMDeviceVeth, nm_device_veth, NM_TYPE_DEVICE_ETHERNET) -#define NM_DEVICE_VETH_GET_PRIVATE(self) _NM_GET_PRIVATE(self, NMDeviceVeth, NM_IS_DEVICE_VETH) - /*****************************************************************************/ static void diff --git a/src/devices/tests/test-arping.c b/src/devices/tests/test-arping.c index 59223f11fd..6e29777619 100644 --- a/src/devices/tests/test-arping.c +++ b/src/devices/tests/test-arping.c @@ -40,9 +40,8 @@ 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 (NM_PLATFORM_GET, IFACE_VETH0, NM_LINK_TYPE_VETH, 100)->ifindex; - fixture->ifindex1 = nmtstp_assert_wait_for_link (NM_PLATFORM_GET, IFACE_VETH1, NM_LINK_TYPE_VETH, 100)->ifindex; + fixture->ifindex0 = nmtstp_link_veth_add (NM_PLATFORM_GET, -1, IFACE_VETH0, IFACE_VETH1)->ifindex; + fixture->ifindex1 = nmtstp_link_get_typed (NM_PLATFORM_GET, -1, IFACE_VETH1, NM_LINK_TYPE_VETH)->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)); diff --git a/src/platform/nm-fake-platform.c b/src/platform/nm-fake-platform.c index 835b50cdfb..27fde50d61 100644 --- a/src/platform/nm-fake-platform.c +++ b/src/platform/nm-fake-platform.c @@ -323,12 +323,14 @@ static gboolean link_add (NMPlatform *platform, const char *name, NMLinkType type, + const char *veth_peer, const void *address, size_t address_len, const NMPlatformLink **out_link) { NMFakePlatformPrivate *priv = NM_FAKE_PLATFORM_GET_PRIVATE ((NMFakePlatform *) platform); NMFakePlatformLink device; + NMFakePlatformLink device_veth = { }; NMFakePlatformLink *new_device; link_init (&device, priv->links->len, type, name); @@ -342,14 +344,29 @@ link_add (NMPlatform *platform, g_array_append_val (priv->links, device); new_device = &g_array_index (priv->links, NMFakePlatformLink, priv->links->len - 1); + if (veth_peer) { + link_init (&device_veth, priv->links->len, type, veth_peer); + g_array_append_val (priv->links, device_veth); + new_device = &g_array_index (priv->links, NMFakePlatformLink, priv->links->len - 2); + } else + g_assert (type != NM_LINK_TYPE_VETH); + + + if (out_link) + *out_link = &new_device->link; + if (device.link.ifindex) { g_signal_emit_by_name (platform, NM_PLATFORM_SIGNAL_LINK_CHANGED, (int) NMP_OBJECT_TYPE_LINK, device.link.ifindex, &device, (int) NM_PLATFORM_SIGNAL_ADDED); + link_changed (platform, new_device, FALSE); + } + + if (veth_peer) { + g_signal_emit_by_name (platform, NM_PLATFORM_SIGNAL_LINK_CHANGED, (int) NMP_OBJECT_TYPE_LINK, device_veth.link.ifindex, &device_veth, (int) NM_PLATFORM_SIGNAL_ADDED); + link_changed (platform, &g_array_index (priv->links, NMFakePlatformLink, priv->links->len - 1), FALSE); } - if (out_link) - *out_link = &new_device->link; return TRUE; } @@ -702,7 +719,7 @@ vlan_add (NMPlatform *platform, const char *name, int parent, int vlan_id, guint { NMFakePlatformLink *device; - if (!link_add (platform, name, NM_LINK_TYPE_VLAN, NULL, 0, out_link)) + if (!link_add (platform, name, NM_LINK_TYPE_VLAN, NULL, NULL, 0, out_link)) return FALSE; device = link_get (platform, nm_platform_link_get_ifindex (platform, name)); @@ -742,7 +759,7 @@ link_vxlan_add (NMPlatform *platform, { NMFakePlatformLink *device; - if (!link_add (platform, name, NM_LINK_TYPE_VXLAN, NULL, 0, out_link)) + if (!link_add (platform, name, NM_LINK_TYPE_VXLAN, NULL, NULL, 0, out_link)) return FALSE; device = link_get (platform, nm_platform_link_get_ifindex (platform, name)); @@ -770,7 +787,7 @@ infiniband_partition_add (NMPlatform *platform, int parent, int p_key, const NMP nm_utils_new_infiniband_name (name, parent_device->link.name, p_key); - if (!link_add (platform, name, NM_LINK_TYPE_INFINIBAND, NULL, 0, out_link)) + if (!link_add (platform, name, NM_LINK_TYPE_INFINIBAND, NULL, NULL, 0, out_link)) return FALSE; device = link_get (platform, nm_platform_link_get_ifindex (platform, name)); @@ -1428,15 +1445,15 @@ nm_fake_platform_setup (void) nm_platform_setup (platform); /* skip zero element */ - link_add (platform, NULL, NM_LINK_TYPE_NONE, NULL, 0, NULL); + link_add (platform, NULL, NM_LINK_TYPE_NONE, NULL, NULL, 0, NULL); /* add loopback interface */ - link_add (platform, "lo", NM_LINK_TYPE_LOOPBACK, NULL, 0, NULL); + link_add (platform, "lo", NM_LINK_TYPE_LOOPBACK, NULL, NULL, 0, NULL); /* add some ethernets */ - link_add (platform, "eth0", NM_LINK_TYPE_ETHERNET, NULL, 0, NULL); - link_add (platform, "eth1", NM_LINK_TYPE_ETHERNET, NULL, 0, NULL); - link_add (platform, "eth2", NM_LINK_TYPE_ETHERNET, NULL, 0, NULL); + link_add (platform, "eth0", NM_LINK_TYPE_ETHERNET, NULL, NULL, 0, NULL); + link_add (platform, "eth1", NM_LINK_TYPE_ETHERNET, NULL, NULL, 0, NULL); + link_add (platform, "eth2", NM_LINK_TYPE_ETHERNET, NULL, NULL, 0, NULL); } static void diff --git a/src/platform/nm-linux-platform.c b/src/platform/nm-linux-platform.c index ca20ddf484..8d4df98616 100644 --- a/src/platform/nm-linux-platform.c +++ b/src/platform/nm-linux-platform.c @@ -2135,12 +2135,14 @@ nla_put_failure: static gboolean _nl_msg_new_link_set_linkinfo (struct nl_msg *msg, - NMLinkType link_type) + NMLinkType link_type, + const char *veth_peer) { struct nlattr *info; const char *kind; nm_assert (msg); + nm_assert (!!veth_peer == (link_type == NM_LINK_TYPE_VETH)); kind = nm_link_type_to_rtnl_type_string (link_type); if (!kind) @@ -2151,11 +2153,26 @@ _nl_msg_new_link_set_linkinfo (struct nl_msg *msg, NLA_PUT_STRING (msg, IFLA_INFO_KIND, kind); + if (veth_peer) { + struct ifinfomsg ifi = { }; + struct nlattr *data, *info_peer; + + if (!(data = nla_nest_start (msg, IFLA_INFO_DATA))) + goto nla_put_failure; + if (!(info_peer = nla_nest_start (msg, 1 /*VETH_INFO_PEER*/))) + goto nla_put_failure; + if (nlmsg_append (msg, &ifi, sizeof (ifi), NLMSG_ALIGNTO) < 0) + goto nla_put_failure; + NLA_PUT_STRING (msg, IFLA_IFNAME, veth_peer); + nla_nest_end (msg, info_peer); + nla_nest_end (msg, data); + } + nla_nest_end (msg, info); return TRUE; nla_put_failure: - return FALSE; + g_return_val_if_reached (FALSE); } static gboolean @@ -4319,6 +4336,7 @@ static gboolean link_add (NMPlatform *platform, const char *name, NMLinkType type, + const char *veth_peer, const void *address, size_t address_len, const NMPlatformLink **out_link) @@ -4352,7 +4370,7 @@ link_add (NMPlatform *platform, if (address && address_len) NLA_PUT (nlmsg, IFLA_ADDRESS, address_len, address); - if (!_nl_msg_new_link_set_linkinfo (nlmsg, type)) + if (!_nl_msg_new_link_set_linkinfo (nlmsg, type, veth_peer)) return FALSE; return do_add_link_with_lookup (platform, type, name, nlmsg, out_link); diff --git a/src/platform/nm-platform.c b/src/platform/nm-platform.c index 7af109dfd7..7f12fb743b 100644 --- a/src/platform/nm-platform.c +++ b/src/platform/nm-platform.c @@ -662,6 +662,7 @@ _link_add_check_existing (NMPlatform *self, const char *name, NMLinkType type, c * @self: platform instance * @name: Interface name * @type: Interface type + * @veth_peer: For veths, the peer name * @address: (allow-none): set the mac address of the link * @address_len: the length of the @address * @out_link: on success, the link object @@ -680,6 +681,7 @@ static NMPlatformError nm_platform_link_add (NMPlatform *self, const char *name, NMLinkType type, + const char *veth_peer, const void *address, size_t address_len, const NMPlatformLink **out_link) @@ -690,17 +692,27 @@ nm_platform_link_add (NMPlatform *self, g_return_val_if_fail (name, NM_PLATFORM_ERROR_BUG); g_return_val_if_fail ( (address != NULL) ^ (address_len == 0) , NM_PLATFORM_ERROR_BUG); + g_return_val_if_fail ((!!veth_peer) == (type == NM_LINK_TYPE_VETH), NM_PLATFORM_ERROR_BUG); plerr = _link_add_check_existing (self, name, type, out_link); if (plerr != NM_PLATFORM_ERROR_SUCCESS) return plerr; _LOGD ("link: adding %s '%s'", nm_link_type_to_string (type), name); - if (!klass->link_add (self, name, type, address, address_len, out_link)) + if (!klass->link_add (self, name, type, veth_peer, address, address_len, out_link)) return NM_PLATFORM_ERROR_UNSPECIFIED; return NM_PLATFORM_ERROR_SUCCESS; } +NMPlatformError +nm_platform_link_veth_add (NMPlatform *self, + const char *name, + const char *peer, + const NMPlatformLink **out_link) +{ + return nm_platform_link_add (self, name, NM_LINK_TYPE_VETH, peer, NULL, 0, out_link); +} + /** * nm_platform_link_dummy_add: * @self: platform instance @@ -714,7 +726,7 @@ nm_platform_link_dummy_add (NMPlatform *self, const char *name, const NMPlatformLink **out_link) { - return nm_platform_link_add (self, name, NM_LINK_TYPE_DUMMY, NULL, 0, out_link); + return nm_platform_link_add (self, name, NM_LINK_TYPE_DUMMY, NULL, NULL, 0, out_link); } /** @@ -1616,7 +1628,7 @@ nm_platform_link_bridge_add (NMPlatform *self, size_t address_len, const NMPlatformLink **out_link) { - return nm_platform_link_add (self, name, NM_LINK_TYPE_BRIDGE, address, address_len, out_link); + return nm_platform_link_add (self, name, NM_LINK_TYPE_BRIDGE, NULL, address, address_len, out_link); } /** @@ -1632,7 +1644,7 @@ nm_platform_link_bond_add (NMPlatform *self, const char *name, const NMPlatformLink **out_link) { - return nm_platform_link_add (self, name, NM_LINK_TYPE_BOND, NULL, 0, out_link); + return nm_platform_link_add (self, name, NM_LINK_TYPE_BOND, NULL, NULL, 0, out_link); } /** @@ -1648,7 +1660,7 @@ nm_platform_link_team_add (NMPlatform *self, const char *name, const NMPlatformLink **out_link) { - return nm_platform_link_add (self, name, NM_LINK_TYPE_TEAM, NULL, 0, out_link); + return nm_platform_link_add (self, name, NM_LINK_TYPE_TEAM, NULL, NULL, 0, out_link); } /** diff --git a/src/platform/nm-platform.h b/src/platform/nm-platform.h index d6e86d4645..97c8f6ccf2 100644 --- a/src/platform/nm-platform.h +++ b/src/platform/nm-platform.h @@ -527,6 +527,7 @@ typedef struct { gboolean (*link_add) (NMPlatform *, const char *name, NMLinkType type, + const char *veth_peer, const void *address, size_t address_len, const NMPlatformLink **out_link); @@ -768,6 +769,8 @@ NMPlatformError nm_platform_link_dummy_add (NMPlatform *self, const char *name, NMPlatformError nm_platform_link_bridge_add (NMPlatform *self, const char *name, const void *address, size_t address_len, const NMPlatformLink **out_link); NMPlatformError nm_platform_link_bond_add (NMPlatform *self, const char *name, const NMPlatformLink **out_link); NMPlatformError nm_platform_link_team_add (NMPlatform *self, const char *name, const NMPlatformLink **out_link); +NMPlatformError nm_platform_link_veth_add (NMPlatform *self, const char *name, const char *peer, const NMPlatformLink **out_link); + gboolean nm_platform_link_delete (NMPlatform *self, int ifindex); gboolean nm_platform_link_set_netns (NMPlatform *self, int ifindex, int netns_fd); diff --git a/src/platform/tests/test-address.c b/src/platform/tests/test-address.c index ae8cfc547c..2ff07b8dc4 100644 --- a/src/platform/tests/test-address.c +++ b/src/platform/tests/test-address.c @@ -22,7 +22,6 @@ #include "test-common.h" -#define DEVICE_NAME "nm-test-device" #define IP4_ADDRESS "192.0.2.1" #define IP4_ADDRESS_PEER "192.0.2.2" #define IP4_ADDRESS_PEER2 "192.0.3.1" @@ -30,8 +29,8 @@ #define IP6_ADDRESS "2001:db8:a:b:1:2:3:4" #define IP6_PLEN 64 -static int DEVICE_IFINDEX = -1; -static int EX = -1; +#define DEVICE_IFINDEX NMTSTP_ENV1_IFINDEX +#define EX NMTSTP_ENV1_EX /*****************************************************************************/ @@ -366,62 +365,16 @@ _nmtstp_init_tests (int *argc, char ***argv) * SETUP TESTS *****************************************************************************/ -typedef struct { - const char *testpath; - GTestFunc test_func; -} TestSetup; - -static void -_g_test_run (gconstpointer user_data) -{ - const TestSetup *s = user_data; - int ifindex; - - _LOGT ("TEST: start %s", s->testpath); - - nm_platform_link_delete (NM_PLATFORM_GET, nm_platform_link_get_ifindex (NM_PLATFORM_GET, DEVICE_NAME)); - g_assert (!nm_platform_link_get_by_ifname (NM_PLATFORM_GET, DEVICE_NAME)); - g_assert_cmpint (nm_platform_link_dummy_add (NM_PLATFORM_GET, DEVICE_NAME, NULL), ==, NM_PLATFORM_ERROR_SUCCESS); - - ifindex = nm_platform_link_get_ifindex (NM_PLATFORM_GET, DEVICE_NAME); - g_assert_cmpint (ifindex, >, 0); - g_assert_cmpint (DEVICE_IFINDEX, ==, -1); - - DEVICE_IFINDEX = ifindex; - EX = nmtstp_run_command_check_external_global (); - - s->test_func (); - - g_assert_cmpint (DEVICE_IFINDEX, ==, ifindex); - DEVICE_IFINDEX = -1; - - g_assert_cmpint (ifindex, ==, nm_platform_link_get_ifindex (NM_PLATFORM_GET, DEVICE_NAME)); - g_assert (nm_platform_link_delete (NM_PLATFORM_GET, ifindex)); - _LOGT ("TEST: finished %s", s->testpath); -} - -static void -_g_test_add_func (const char *testpath, - GTestFunc test_func) -{ - TestSetup *s; - - s = g_new0 (TestSetup, 1); - s->testpath = testpath; - s->test_func = test_func; - - g_test_add_data_func_full (testpath, s, _g_test_run, g_free); -} - void _nmtstp_setup_tests (void) { - _g_test_add_func ("/address/ipv4/general", test_ip4_address_general); - _g_test_add_func ("/address/ipv6/general", test_ip6_address_general); +#define add_test_func(testpath, test_func) nmtstp_env1_add_test_func(testpath, test_func, FALSE) + add_test_func ("/address/ipv4/general", test_ip4_address_general); + add_test_func ("/address/ipv6/general", test_ip6_address_general); - _g_test_add_func ("/address/ipv4/general-2", test_ip4_address_general_2); - _g_test_add_func ("/address/ipv6/general-2", test_ip6_address_general_2); + add_test_func ("/address/ipv4/general-2", test_ip4_address_general_2); + add_test_func ("/address/ipv6/general-2", test_ip6_address_general_2); - _g_test_add_func ("/address/ipv4/peer", test_ip4_address_peer); - _g_test_add_func ("/address/ipv4/peer/zero", test_ip4_address_peer_zero); + add_test_func ("/address/ipv4/peer", test_ip4_address_peer); + add_test_func ("/address/ipv4/peer/zero", test_ip4_address_peer_zero); } diff --git a/src/platform/tests/test-cleanup.c b/src/platform/tests/test-cleanup.c index 71a92cbfae..26c8c2b4bd 100644 --- a/src/platform/tests/test-cleanup.c +++ b/src/platform/tests/test-cleanup.c @@ -22,8 +22,6 @@ #include "test-common.h" -#define DEVICE_NAME "nm-test-device" - static void test_cleanup_internal (void) { diff --git a/src/platform/tests/test-common.c b/src/platform/tests/test-common.c index 76c18a187d..2abf6b2cea 100644 --- a/src/platform/tests/test-common.c +++ b/src/platform/tests/test-common.c @@ -30,6 +30,9 @@ #define SIGNAL_DATA_FMT "'%s-%s' ifindex %d%s%s%s (%d times received)" #define SIGNAL_DATA_ARG(data) (data)->name, nm_platform_signal_change_type_to_string ((data)->change_type), (data)->ifindex, (data)->ifname ? " ifname '" : "", (data)->ifname ? (data)->ifname : "", (data)->ifname ? "'" : "", (data)->received_count +int NMTSTP_ENV1_IFINDEX = -1; +int NMTSTP_ENV1_EX = -1; + /*****************************************************************************/ void @@ -973,6 +976,36 @@ nmtstp_ip6_address_del (NMPlatform *platform, } \ } G_STMT_END +const NMPlatformLink * +nmtstp_link_veth_add (NMPlatform *platform, + gboolean external_command, + const char *name, + const char *peer) +{ + const NMPlatformLink *pllink = NULL; + gboolean success; + + g_assert (nm_utils_is_valid_iface_name (name, NULL)); + + external_command = nmtstp_run_command_check_external (external_command); + + _init_platform (&platform, external_command); + + if (external_command) { + success = !nmtstp_run_command ("ip link add dev %s type veth peer name %s", + name, peer); + if (success) { + pllink = nmtstp_assert_wait_for_link (platform, name, NM_LINK_TYPE_VETH, 100); + nmtstp_assert_wait_for_link (platform, peer, NM_LINK_TYPE_VETH, 10); + } + } else + success = nm_platform_link_veth_add (platform, name, peer, &pllink) == NM_PLATFORM_ERROR_SUCCESS; + + g_assert (success); + _assert_pllink (platform, success, pllink, name, NM_LINK_TYPE_VETH); + return pllink; +} + const NMPlatformLink * nmtstp_link_dummy_add (NMPlatform *platform, gboolean external_command, diff --git a/src/platform/tests/test-common.h b/src/platform/tests/test-common.h index a52a5db571..373a1fe0da 100644 --- a/src/platform/tests/test-common.h +++ b/src/platform/tests/test-common.h @@ -197,6 +197,10 @@ void nmtstp_link_set_updown (NMPlatform *platform, int ifindex, gboolean up); +const NMPlatformLink *nmtstp_link_veth_add (NMPlatform *platform, + gboolean external_command, + const char *name, + const char *peer); const NMPlatformLink *nmtstp_link_dummy_add (NMPlatform *platform, gboolean external_command, const char *name); @@ -231,6 +235,100 @@ void nmtstp_link_del (NMPlatform *platform, int ifindex, const char *name); +/*****************************************************************************/ + +extern int NMTSTP_ENV1_IFINDEX; +extern int NMTSTP_ENV1_EX; + +static inline void +_nmtstp_env1_wrapper_setup (const NmtstTestData *test_data) +{ + int *p_ifindex; + gpointer p_ifup; + + nmtst_test_data_unpack (test_data, &p_ifindex, NULL, NULL, NULL, &p_ifup); + + g_assert (p_ifindex && *p_ifindex == -1); + + _LOGT ("TEST[%s]: setup", test_data->testpath); + + nm_platform_link_delete (NM_PLATFORM_GET, nm_platform_link_get_ifindex (NM_PLATFORM_GET, DEVICE_NAME)); + g_assert (!nm_platform_link_get_by_ifname (NM_PLATFORM_GET, DEVICE_NAME)); + g_assert_cmpint (nm_platform_link_dummy_add (NM_PLATFORM_GET, DEVICE_NAME, NULL), ==, NM_PLATFORM_ERROR_SUCCESS); + + *p_ifindex = nm_platform_link_get_ifindex (NM_PLATFORM_GET, DEVICE_NAME); + g_assert_cmpint (*p_ifindex, >, 0); + g_assert_cmpint (NMTSTP_ENV1_IFINDEX, ==, -1); + + if (GPOINTER_TO_INT (p_ifup)) + g_assert (nm_platform_link_set_up (NM_PLATFORM_GET, *p_ifindex, NULL)); + + nm_platform_process_events (NM_PLATFORM_GET); + + NMTSTP_ENV1_IFINDEX = *p_ifindex; + NMTSTP_ENV1_EX = nmtstp_run_command_check_external_global (); +} + +static inline void +_nmtstp_env1_wrapper_run (gconstpointer user_data) +{ + const NmtstTestData *test_data = user_data; + GTestDataFunc test_func_data; + GTestFunc test_func; + gconstpointer d; + + nmtst_test_data_unpack (test_data, NULL, &test_func, &test_func_data, &d, NULL); + + _LOGT ("TEST[%s]: run", test_data->testpath); + if (test_func) + test_func (); + else + test_func_data (d); +} + +static inline void +_nmtstp_env1_wrapper_teardown (const NmtstTestData *test_data) +{ + int *p_ifindex; + + nmtst_test_data_unpack (test_data, &p_ifindex, NULL, NULL, NULL, NULL); + + g_assert_cmpint (NMTSTP_ENV1_IFINDEX, ==, *p_ifindex); + NMTSTP_ENV1_IFINDEX = -1; + + _LOGT ("TEST[%s]: teardown", test_data->testpath); + + g_assert_cmpint (*p_ifindex, ==, nm_platform_link_get_ifindex (NM_PLATFORM_GET, DEVICE_NAME)); + g_assert (nm_platform_link_delete (NM_PLATFORM_GET, *p_ifindex)); + + nm_platform_process_events (NM_PLATFORM_GET); + + _LOGT ("TEST[%s]: finished", test_data->testpath); + + *p_ifindex = -1; +} + +/* add test function, that set's up a particular environment, consisting + * of a dummy device with ifindex NMTSTP_ENV1_IFINDEX. */ +#define _nmtstp_env1_add_test_func_full(testpath, test_func, test_data_func, arg, ifup) \ + nmtst_add_test_func_full (testpath, \ + _nmtstp_env1_wrapper_run, \ + _nmtstp_env1_wrapper_setup, \ + _nmtstp_env1_wrapper_teardown, \ + ({ static int _ifindex = -1; &_ifindex; }), \ + ({ GTestFunc _test_func = (test_func); _test_func; }), \ + ({ GTestDataFunc _test_func = (test_data_func); _test_func; }), \ + (arg), \ + ({ gboolean _ifup = (ifup); GINT_TO_POINTER (_ifup);})) + +#define nmtstp_env1_add_test_func_data(testpath, test_func, arg, ifup) \ + _nmtstp_env1_add_test_func_full(testpath, NULL, test_func, arg, ifup) + +#define nmtstp_env1_add_test_func(testpath, test_func, ifup) \ + _nmtstp_env1_add_test_func_full(testpath, test_func, NULL, NULL, ifup) + +/*****************************************************************************/ + typedef void (*NMTstpSetupFunc) (void); extern NMTstpSetupFunc const _nmtstp_setup_platform_func; diff --git a/src/platform/tests/test-link.c b/src/platform/tests/test-link.c index ed435567f0..df204b4683 100644 --- a/src/platform/tests/test-link.c +++ b/src/platform/tests/test-link.c @@ -1713,9 +1713,8 @@ test_nl_bugs_veth (void) NMTstpNamespaceHandle *ns_handle = NULL; /* create veth pair. */ - nmtstp_run_command_check ("ip link add dev %s type veth peer name %s", IFACE_VETH0, IFACE_VETH1); - ifindex_veth0 = nmtstp_assert_wait_for_link (NM_PLATFORM_GET, IFACE_VETH0, NM_LINK_TYPE_VETH, 100)->ifindex; - ifindex_veth1 = nmtstp_assert_wait_for_link (NM_PLATFORM_GET, IFACE_VETH1, NM_LINK_TYPE_VETH, 100)->ifindex; + ifindex_veth0 = nmtstp_link_veth_add (NM_PLATFORM_GET, -1, IFACE_VETH0, IFACE_VETH1)->ifindex; + ifindex_veth1 = nmtstp_link_get_typed (NM_PLATFORM_GET, -1, IFACE_VETH1, NM_LINK_TYPE_VETH)->ifindex; /* assert that nm_platform_link_veth_get_properties() returns the expected peer ifindexes. */ g_assert (nm_platform_link_veth_get_properties (NM_PLATFORM_GET, ifindex_veth0, &i)); diff --git a/src/platform/tests/test-route.c b/src/platform/tests/test-route.c index 257f4062b4..258cfc1b25 100644 --- a/src/platform/tests/test-route.c +++ b/src/platform/tests/test-route.c @@ -27,7 +27,8 @@ #include "test-common.h" -#define DEVICE_NAME "nm-test-device" +#define DEVICE_IFINDEX NMTSTP_ENV1_IFINDEX +#define EX NMTSTP_ENV1_EX static void ip4_route_callback (NMPlatform *platform, int obj_type_i, int ifindex, const NMPlatformIP4Route *received, int change_type_i, SignalData *data) @@ -428,56 +429,104 @@ test_ip4_route_options (void) static void -test_ip6_route_options (void) +test_ip6_route_options (gconstpointer test_data) { - int ifindex = nm_platform_link_get_ifindex (NM_PLATFORM_GET, DEVICE_NAME); - NMPlatformIP6Route route = { }; - struct in6_addr network; - GArray *routes; - NMPlatformIP6Route rts[3]; + const int TEST_IDX = GPOINTER_TO_INT (test_data); + const int IFINDEX = nm_platform_link_get_ifindex (NM_PLATFORM_GET, DEVICE_NAME); + gs_unref_array GArray *routes = NULL; +#define RTS_MAX 1 + NMPlatformIP6Route rts_add[RTS_MAX] = { }; + NMPlatformIP6Route rts_cmp[RTS_MAX] = { }; + NMPlatformIP6Address addr[1] = { }; + guint rts_n = 0; + guint addr_n = 0; + guint i; - inet_pton (AF_INET6, "2001:db8:a:b:0:0:0:0", &network); + switch (TEST_IDX) { + case 1: + rts_add[rts_n++] = ((NMPlatformIP6Route) { + .ifindex = IFINDEX, + .rt_source = NM_IP_CONFIG_SOURCE_USER, + .network = *nmtst_inet6_from_string ("2001:db8:a:b:0:0:0:0"), + .plen = 64, + .gateway = in6addr_any, + .metric = 1024, + .window = 20000, + .cwnd = 8, + .initcwnd = 22, + .initrwnd = 33, + .mtu = 1300, + .lock_mtu = TRUE, + }); + break; + case 2: + addr[addr_n++] = ((NMPlatformIP6Address) { + .ifindex = IFINDEX, + .address = *nmtst_inet6_from_string ("2000::2"), + .plen = 128, + .peer_address = in6addr_any, + .lifetime = NM_PLATFORM_LIFETIME_PERMANENT, + .preferred = NM_PLATFORM_LIFETIME_PERMANENT, + .n_ifa_flags = 0, + }); + rts_add[rts_n++] = ((NMPlatformIP6Route) { + .ifindex = IFINDEX, + .rt_source = NM_IP_CONFIG_SOURCE_USER, + .network = *nmtst_inet6_from_string ("1010::1"), + .plen = 128, + .gateway = in6addr_any, + .metric = 256, + .pref_src = *nmtst_inet6_from_string ("2000::2"), + }); + break; + default: + g_assert_not_reached (); + } - route.ifindex = ifindex; - route.rt_source = NM_IP_CONFIG_SOURCE_USER; - route.network = network; - route.plen = 64; - route.gateway = in6addr_any; - route.metric = 1024; - route.window = 20000; - route.cwnd = 8; - route.initcwnd = 22; - route.initrwnd = 33; - route.mtu = 1300; - route.lock_mtu = TRUE; + for (i = 0; i < addr_n; i++) { + g_assert (nm_platform_ip6_address_add (NM_PLATFORM_GET, IFINDEX, + addr[i].address, + addr[i].plen, + addr[i].peer_address, + addr[i].lifetime, + addr[i].preferred, + addr[i].n_ifa_flags)); + } - g_assert (nm_platform_ip6_route_add (NM_PLATFORM_GET, &route)); + for (i = 0; i < rts_n; i++) + g_assert (nm_platform_ip6_route_add (NM_PLATFORM_GET, &rts_add[i])); - /* Test route listing */ - routes = nm_platform_ip6_route_get_all (NM_PLATFORM_GET, ifindex, + routes = nm_platform_ip6_route_get_all (NM_PLATFORM_GET, IFINDEX, NM_PLATFORM_GET_ROUTE_FLAGS_WITH_DEFAULT | NM_PLATFORM_GET_ROUTE_FLAGS_WITH_NON_DEFAULT); - memset (rts, 0, sizeof (rts)); - rts[0].rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER); - rts[0].network = network; - rts[0].plen = 64; - rts[0].ifindex = ifindex; - rts[0].gateway = in6addr_any; - rts[0].metric = 1024; - rts[0].window = 20000; - rts[0].cwnd = 8; - rts[0].initcwnd = 22; - rts[0].initrwnd = 33; - rts[0].mtu = 1300; - rts[0].lock_mtu = TRUE; + switch (TEST_IDX) { + case 1: + case 2: + for (i = 0; i < rts_n; i++) { + rts_cmp[i] = rts_add[i]; + rts_cmp[i].rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER); + } + break; + default: + g_assert_not_reached (); + } - g_assert_cmpint (routes->len, ==, 1); - nmtst_platform_ip6_routes_equal ((NMPlatformIP6Route *) routes->data, rts, routes->len, TRUE); + g_assert_cmpint (routes->len, ==, rts_n); + nmtst_platform_ip6_routes_equal ((const NMPlatformIP6Route *) routes->data, rts_cmp, rts_n, TRUE); - /* Remove route */ - g_assert (nm_platform_ip6_route_delete (NM_PLATFORM_GET, ifindex, network, 64, 1024)); + for (i = 0; i < rts_n; i++) { + g_assert (nm_platform_ip6_route_delete (NM_PLATFORM_GET, IFINDEX, + rts_add[i].network, rts_add[i].plen, + rts_add[i].metric)); + } - g_array_unref (routes); + for (i = 0; i < addr_n; i++) { + nmtstp_ip6_address_del (NM_PLATFORM_GET, + EX, + IFINDEX, + rts_add[i].network, + rts_add[i].plen); + } } /*****************************************************************************/ @@ -493,22 +542,15 @@ _nmtstp_init_tests (int *argc, char ***argv) void _nmtstp_setup_tests (void) { - SignalData *link_added = add_signal_ifname (NM_PLATFORM_SIGNAL_LINK_CHANGED, NM_PLATFORM_SIGNAL_ADDED, link_callback, DEVICE_NAME); - - nm_platform_link_delete (NM_PLATFORM_GET, nm_platform_link_get_ifindex (NM_PLATFORM_GET, DEVICE_NAME)); - g_assert (!nm_platform_link_get_by_ifname (NM_PLATFORM_GET, DEVICE_NAME)); - g_assert (nm_platform_link_dummy_add (NM_PLATFORM_GET, DEVICE_NAME, NULL) == NM_PLATFORM_ERROR_SUCCESS); - accept_signal (link_added); - free_signal (link_added); - - g_assert (nm_platform_link_set_up (NM_PLATFORM_GET, nm_platform_link_get_ifindex (NM_PLATFORM_GET, DEVICE_NAME), NULL)); - - g_test_add_func ("/route/ip4", test_ip4_route); - g_test_add_func ("/route/ip6", test_ip6_route); - g_test_add_func ("/route/ip4_metric0", test_ip4_route_metric0); - g_test_add_func ("/route/ip4_options", test_ip4_route_options); - g_test_add_func ("/route/ip6_options", test_ip6_route_options); +#define add_test_func(testpath, test_func) nmtstp_env1_add_test_func(testpath, test_func, TRUE) +#define add_test_func_data(testpath, test_func, arg) nmtstp_env1_add_test_func_data(testpath, test_func, arg, TRUE) + add_test_func ("/route/ip4", test_ip4_route); + add_test_func ("/route/ip6", test_ip6_route); + add_test_func ("/route/ip4_metric0", test_ip4_route_metric0); + add_test_func ("/route/ip4_options", test_ip4_route_options); + add_test_func_data ("/route/ip6_options/1", test_ip6_route_options, GINT_TO_POINTER (1)); + add_test_func_data ("/route/ip6_options/2", test_ip6_route_options, GINT_TO_POINTER (2)); if (nmtstp_is_root_test ()) - g_test_add_func ("/route/ip4_zero_gateway", test_ip4_zero_gateway); + add_test_func ("/route/ip4_zero_gateway", test_ip4_zero_gateway); }