platform/tests: move test fixture "env1" from test-address.c to test-common.h

nmtstp_env1_add_test_func() allows to register test functions in a
particular test environment ("env1", for lack of a better name).

It will be reused for "test-route.c"
This commit is contained in:
Thomas Haller 2017-05-25 22:20:59 +02:00
parent 1056a28231
commit 0f905a0b88
3 changed files with 106 additions and 55 deletions

View file

@ -30,8 +30,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 +366,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);
}

View file

@ -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

View file

@ -231,6 +231,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;