Blame src/devices/tests/test-acd.c

Packit Service b23acc
// SPDX-License-Identifier: GPL-2.0+
Packit Service b23acc
/*
Packit Service b23acc
 * Copyright (C) 2015 Red Hat, Inc.
Packit Service b23acc
 */
Packit Service b23acc
Packit Service b23acc
#include "nm-default.h"
Packit Service b23acc
Packit Service b23acc
#include "n-acd/src/n-acd.h"
Packit Service b23acc
Packit Service b23acc
#include "devices/nm-acd-manager.h"
Packit Service b23acc
#include "platform/tests/test-common.h"
Packit Service b23acc
Packit Service b23acc
#define IFACE_VETH0 "nm-test-veth0"
Packit Service b23acc
#define IFACE_VETH1 "nm-test-veth1"
Packit Service b23acc
Packit Service b23acc
#define ADDR1 0x01010101
Packit Service b23acc
#define ADDR2 0x02020202
Packit Service b23acc
#define ADDR3 0x03030303
Packit Service b23acc
#define ADDR4 0x04040404
Packit Service b23acc
Packit Service b23acc
/*****************************************************************************/
Packit Service b23acc
Packit Service b23acc
static gboolean
Packit Service b23acc
_skip_acd_test_check (void)
Packit Service b23acc
{
Packit Service b23acc
	NAcd *acd;
Packit Service b23acc
	NAcdConfig *config;
Packit Service b23acc
	const guint8 hwaddr[ETH_ALEN] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
Packit Service b23acc
	int r;
Packit Service b23acc
	static int skip = -1;
Packit Service b23acc
Packit Service b23acc
	if (skip == -1) {
Packit Service b23acc
		r = n_acd_config_new (&config);
Packit Service b23acc
		g_assert (r == 0);
Packit Service b23acc
Packit Service b23acc
		n_acd_config_set_ifindex (config, 1);
Packit Service b23acc
		n_acd_config_set_transport (config, N_ACD_TRANSPORT_ETHERNET);
Packit Service b23acc
		n_acd_config_set_mac (config, hwaddr, sizeof (hwaddr));
Packit Service b23acc
Packit Service b23acc
		r = n_acd_new (&acd, config);
Packit Service b23acc
		n_acd_config_free (config);
Packit Service b23acc
		if (r == 0)
Packit Service b23acc
			n_acd_unref (acd);
Packit Service b23acc
Packit Service b23acc
		skip = (r != 0);
Packit Service b23acc
	}
Packit Service b23acc
	return skip;
Packit Service b23acc
}
Packit Service b23acc
Packit Service b23acc
#define _skip_acd_test() \
Packit Service b23acc
	({ \
Packit Service b23acc
		gboolean _skip = _skip_acd_test_check (); \
Packit Service b23acc
		\
Packit Service b23acc
		if (_skip) \
Packit Service b23acc
			g_test_skip ("Cannot create NAcd. Running under valgind?"); \
Packit Service b23acc
		_skip; \
Packit Service b23acc
	})
Packit Service b23acc
Packit Service b23acc
/*****************************************************************************/
Packit Service b23acc
Packit Service b23acc
typedef struct {
Packit Service b23acc
	int ifindex0;
Packit Service b23acc
	int ifindex1;
Packit Service b23acc
	const guint8 *hwaddr0;
Packit Service b23acc
	const guint8 *hwaddr1;
Packit Service b23acc
	size_t hwaddr0_len;
Packit Service b23acc
	size_t hwaddr1_len;
Packit Service b23acc
} test_fixture;
Packit Service b23acc
Packit Service b23acc
static void
Packit Service b23acc
fixture_setup (test_fixture *fixture, gconstpointer user_data)
Packit Service b23acc
{
Packit Service b23acc
	/* create veth pair. */
Packit Service b23acc
	fixture->ifindex0 = nmtstp_link_veth_add (NM_PLATFORM_GET, -1, IFACE_VETH0, IFACE_VETH1)->ifindex;
Packit Service b23acc
	fixture->ifindex1 = nmtstp_link_get_typed (NM_PLATFORM_GET, -1, IFACE_VETH1, NM_LINK_TYPE_VETH)->ifindex;
Packit Service b23acc
Packit Service b23acc
	g_assert (nm_platform_link_set_up (NM_PLATFORM_GET, fixture->ifindex0, NULL));
Packit Service b23acc
	g_assert (nm_platform_link_set_up (NM_PLATFORM_GET, fixture->ifindex1, NULL));
Packit Service b23acc
Packit Service b23acc
	fixture->hwaddr0 = nm_platform_link_get_address (NM_PLATFORM_GET, fixture->ifindex0, &fixture->hwaddr0_len);
Packit Service b23acc
	fixture->hwaddr1 = nm_platform_link_get_address (NM_PLATFORM_GET, fixture->ifindex1, &fixture->hwaddr1_len);
Packit Service b23acc
}
Packit Service b23acc
Packit Service b23acc
typedef struct {
Packit Service b23acc
	in_addr_t addresses[8];
Packit Service b23acc
	in_addr_t peer_addresses[8];
Packit Service b23acc
	gboolean expected_result[8];
Packit Service b23acc
} TestInfo;
Packit Service b23acc
Packit Service b23acc
static void
Packit Service b23acc
acd_manager_probe_terminated (NMAcdManager *acd_manager, gpointer user_data)
Packit Service b23acc
{
Packit Service b23acc
	g_main_loop_quit (user_data);
Packit Service b23acc
}
Packit Service b23acc
Packit Service b23acc
static void
Packit Service b23acc
test_acd_common (test_fixture *fixture, TestInfo *info)
Packit Service b23acc
{
Packit Service b23acc
	nm_auto_free_acdmgr NMAcdManager *manager = NULL;
Packit Service b23acc
	nm_auto_unref_gmainloop GMainLoop *loop = NULL;
Packit Service b23acc
	int i;
Packit Service b23acc
	const guint WAIT_TIME_OPTIMISTIC = 50;
Packit Service b23acc
	guint wait_time;
Packit Service b23acc
	static const NMAcdCallbacks callbacks = {
Packit Service b23acc
		.probe_terminated_callback = acd_manager_probe_terminated,
Packit Service b23acc
		.user_data_destroy         = (GDestroyNotify) g_main_loop_unref,
Packit Service b23acc
	};
Packit Service b23acc
	int r;
Packit Service b23acc
Packit Service b23acc
	if (_skip_acd_test ())
Packit Service b23acc
		return;
Packit Service b23acc
Packit Service b23acc
	/* first, try with a short waittime. We hope that this is long enough
Packit Service b23acc
	 * to successfully complete the test. Only if that's not the case, we
Packit Service b23acc
	 * assume the computer is currently busy (high load) and we retry with
Packit Service b23acc
	 * a longer timeout. */
Packit Service b23acc
	wait_time = WAIT_TIME_OPTIMISTIC;
Packit Service b23acc
again:
Packit Service b23acc
Packit Service b23acc
	nm_clear_pointer (&loop, g_main_loop_unref);
Packit Service b23acc
	loop = g_main_loop_new (NULL, FALSE);
Packit Service b23acc
Packit Service b23acc
	nm_clear_pointer (&manager, nm_acd_manager_free);
Packit Service b23acc
	manager = nm_acd_manager_new (fixture->ifindex0,
Packit Service b23acc
	                              fixture->hwaddr0,
Packit Service b23acc
	                              fixture->hwaddr0_len,
Packit Service b23acc
	                              &callbacks,
Packit Service b23acc
	                              g_main_loop_ref (loop));
Packit Service b23acc
	g_assert (manager != NULL);
Packit Service b23acc
Packit Service b23acc
	for (i = 0; info->addresses[i]; i++)
Packit Service b23acc
		g_assert (nm_acd_manager_add_address (manager, info->addresses[i]));
Packit Service b23acc
Packit Service b23acc
	for (i = 0; info->peer_addresses[i]; i++) {
Packit Service b23acc
		nmtstp_ip4_address_add (NULL, FALSE, fixture->ifindex1, info->peer_addresses[i],
Packit Service b23acc
		                        24, 0, 3600, 1800, 0, NULL);
Packit Service b23acc
	}
Packit Service b23acc
Packit Service b23acc
	r = nm_acd_manager_start_probe (manager, wait_time);
Packit Service b23acc
	g_assert_cmpint (r, ==, 0);
Packit Service b23acc
Packit Service b23acc
	g_assert (nmtst_main_loop_run (loop, 2000));
Packit Service b23acc
Packit Service b23acc
	for (i = 0; info->addresses[i]; i++) {
Packit Service b23acc
		gboolean val;
Packit Service b23acc
		char sbuf[NM_UTILS_INET_ADDRSTRLEN];
Packit Service b23acc
Packit Service b23acc
		val = nm_acd_manager_check_address (manager, info->addresses[i]);
Packit Service b23acc
		if (val == info->expected_result[i])
Packit Service b23acc
			continue;
Packit Service b23acc
Packit Service b23acc
		if (wait_time == WAIT_TIME_OPTIMISTIC) {
Packit Service b23acc
			/* probably we just had a glitch and the system took longer than
Packit Service b23acc
			 * expected. Re-verify with a large timeout this time. */
Packit Service b23acc
			wait_time = 1000;
Packit Service b23acc
			goto again;
Packit Service b23acc
		}
Packit Service b23acc
Packit Service b23acc
		g_error ("expected check for address #%d (%s) to %s, but it didn't",
Packit Service b23acc
		         i, _nm_utils_inet4_ntop (info->addresses[i], sbuf),
Packit Service b23acc
		         info->expected_result[i] ? "detect no duplicated" : "detect a duplicate");
Packit Service b23acc
	}
Packit Service b23acc
}
Packit Service b23acc
Packit Service b23acc
static void
Packit Service b23acc
test_acd_probe_1 (test_fixture *fixture, gconstpointer user_data)
Packit Service b23acc
{
Packit Service b23acc
	TestInfo info = { .addresses       = { ADDR1, ADDR2, ADDR3 },
Packit Service b23acc
	                  .peer_addresses  = { ADDR4 },
Packit Service b23acc
	                  .expected_result = { TRUE, TRUE, TRUE } };
Packit Service b23acc
Packit Service b23acc
	test_acd_common (fixture, &info;;
Packit Service b23acc
}
Packit Service b23acc
Packit Service b23acc
static void
Packit Service b23acc
test_acd_probe_2 (test_fixture *fixture, gconstpointer user_data)
Packit Service b23acc
{
Packit Service b23acc
	TestInfo info = { .addresses       = { ADDR1, ADDR2, ADDR3, ADDR4 },
Packit Service b23acc
	                  .peer_addresses  = { ADDR3, ADDR2 },
Packit Service b23acc
	                  .expected_result = { TRUE, FALSE, FALSE, TRUE } };
Packit Service b23acc
Packit Service b23acc
	test_acd_common (fixture, &info;;
Packit Service b23acc
}
Packit Service b23acc
Packit Service b23acc
static void
Packit Service b23acc
test_acd_announce (test_fixture *fixture, gconstpointer user_data)
Packit Service b23acc
{
Packit Service b23acc
	nm_auto_free_acdmgr NMAcdManager *manager = NULL;
Packit Service b23acc
	nm_auto_unref_gmainloop GMainLoop *loop = NULL;
Packit Service b23acc
	int r;
Packit Service b23acc
Packit Service b23acc
	if (_skip_acd_test ())
Packit Service b23acc
		return;
Packit Service b23acc
Packit Service b23acc
	manager = nm_acd_manager_new (fixture->ifindex0,
Packit Service b23acc
	                              fixture->hwaddr0,
Packit Service b23acc
	                              fixture->hwaddr0_len,
Packit Service b23acc
	                              NULL,
Packit Service b23acc
	                              NULL);
Packit Service b23acc
	g_assert (manager != NULL);
Packit Service b23acc
Packit Service b23acc
	g_assert (nm_acd_manager_add_address (manager, ADDR1));
Packit Service b23acc
	g_assert (nm_acd_manager_add_address (manager, ADDR2));
Packit Service b23acc
Packit Service b23acc
	loop = g_main_loop_new (NULL, FALSE);
Packit Service b23acc
	r = nm_acd_manager_announce_addresses (manager);
Packit Service b23acc
	g_assert_cmpint (r, ==, 0);
Packit Service b23acc
	g_assert (!nmtst_main_loop_run (loop, 200));
Packit Service b23acc
}
Packit Service b23acc
Packit Service b23acc
static void
Packit Service b23acc
fixture_teardown (test_fixture *fixture, gconstpointer user_data)
Packit Service b23acc
{
Packit Service b23acc
	nm_platform_link_delete (NM_PLATFORM_GET, fixture->ifindex0);
Packit Service b23acc
	nm_platform_link_delete (NM_PLATFORM_GET, fixture->ifindex1);
Packit Service b23acc
}
Packit Service b23acc
Packit Service b23acc
NMTstpSetupFunc const _nmtstp_setup_platform_func = nm_linux_platform_setup;
Packit Service b23acc
Packit Service b23acc
void
Packit Service b23acc
_nmtstp_init_tests (int *argc, char ***argv)
Packit Service b23acc
{
Packit Service b23acc
	nmtst_init_with_logging (argc, argv, NULL, "ALL");
Packit Service b23acc
}
Packit Service b23acc
Packit Service b23acc
void
Packit Service b23acc
_nmtstp_setup_tests (void)
Packit Service b23acc
{
Packit Service b23acc
	g_test_add ("/acd/probe/1", test_fixture, NULL, fixture_setup, test_acd_probe_1, fixture_teardown);
Packit Service b23acc
	g_test_add ("/acd/probe/2", test_fixture, NULL, fixture_setup, test_acd_probe_2, fixture_teardown);
Packit Service b23acc
	g_test_add ("/acd/announce", test_fixture, NULL, fixture_setup, test_acd_announce, fixture_teardown);
Packit Service b23acc
}