Commit 57af1d6e authored by Thomas Haller's avatar Thomas Haller

lldp/tests/trivial: some renaming and moving of code

parent 3802fd46
......@@ -32,16 +32,67 @@
#include "nm-test-utils.h"
/*****************************************************************************/
static GVariant *
get_lldp_neighbor_attribute (GVariant *neighbors,
const char *chassis, const char *port,
const char *name)
{
GVariantIter iter, attrs_iter;
GVariant *variant, *attr_variant;
const char *attr_name;
g_return_val_if_fail (g_variant_is_of_type (neighbors,
G_VARIANT_TYPE ("aa{sv}")),
NULL);
g_variant_iter_init (&iter, neighbors);
while (g_variant_iter_next (&iter, "@a{sv}", &variant)) {
gs_unref_variant GVariant *chassis_v = NULL;
gs_unref_variant GVariant *port_v = NULL;
gs_unref_variant GVariant *attr_v = NULL;
g_variant_iter_init (&attrs_iter, variant);
while (g_variant_iter_next (&attrs_iter, "{&sv}", &attr_name, &attr_variant)) {
if (!g_strcmp0 (attr_name, NM_LLDP_ATTR_CHASSIS_ID)) {
g_assert (!chassis_v);
chassis_v = attr_variant;
} else if (!g_strcmp0 (attr_name, NM_LLDP_ATTR_PORT_ID)) {
g_assert (!port_v);
port_v = attr_variant;
} else if (!g_strcmp0 (attr_name, name)) {
g_assert (!attr_v);
attr_v = attr_variant;
} else
g_variant_unref (attr_variant);
}
g_variant_unref (variant);
if ( chassis_v
&& port_v
&& g_variant_is_of_type (chassis_v, G_VARIANT_TYPE_STRING)
&& g_variant_is_of_type (port_v, G_VARIANT_TYPE_STRING)
&& !g_strcmp0 (chassis, g_variant_get_string (chassis_v, NULL))
&& !g_strcmp0 (port, g_variant_get_string (port_v, NULL)))
return g_variant_ref (attr_v);
}
/* neighbor not found */
return NULL;
}
typedef struct {
int ifindex;
int fd;
guint8 mac[ETH_ALEN];
} test_fixture;
} TestRecvFixture;
#define TEST_IFNAME "nm-tap-test0"
static void
fixture_setup (test_fixture *fixture, gconstpointer user_data)
_test_recv_fixture_setup (TestRecvFixture *fixture, gconstpointer user_data)
{
const NMPlatformLink *link;
struct ifreq ifr = { };
......@@ -71,7 +122,7 @@ fixture_setup (test_fixture *fixture, gconstpointer user_data)
typedef struct {
int num_called;
} TestInfo;
} TestRecvCallbackInfo;
static gboolean
loop_quit (gpointer user_data)
......@@ -84,66 +135,17 @@ static void
lldp_neighbors_changed (NMLldpListener *lldp_listener, GParamSpec *pspec,
gpointer user_data)
{
TestInfo *info = user_data;
TestRecvCallbackInfo *info = user_data;
info->num_called++;
}
static GVariant *
get_lldp_neighbor_attribute (GVariant *neighbors,
const char *chassis, const char *port,
const char *name)
{
GVariantIter iter, attrs_iter;
GVariant *variant, *attr_variant;
const char *attr_name;
g_return_val_if_fail (g_variant_is_of_type (neighbors,
G_VARIANT_TYPE ("aa{sv}")),
NULL);
g_variant_iter_init (&iter, neighbors);
while (g_variant_iter_next (&iter, "@a{sv}", &variant)) {
gs_unref_variant GVariant *chassis_v = NULL;
gs_unref_variant GVariant *port_v = NULL;
gs_unref_variant GVariant *attr_v = NULL;
g_variant_iter_init (&attrs_iter, variant);
while (g_variant_iter_next (&attrs_iter, "{&sv}", &attr_name, &attr_variant)) {
if (!g_strcmp0 (attr_name, NM_LLDP_ATTR_CHASSIS_ID)) {
g_assert (!chassis_v);
chassis_v = attr_variant;
} else if (!g_strcmp0 (attr_name, NM_LLDP_ATTR_PORT_ID)) {
g_assert (!port_v);
port_v = attr_variant;
} else if (!g_strcmp0 (attr_name, name)) {
g_assert (!attr_v);
attr_v = attr_variant;
} else
g_variant_unref (attr_variant);
}
g_variant_unref (variant);
if ( chassis_v
&& port_v
&& g_variant_is_of_type (chassis_v, G_VARIANT_TYPE_STRING)
&& g_variant_is_of_type (port_v, G_VARIANT_TYPE_STRING)
&& !g_strcmp0 (chassis, g_variant_get_string (chassis_v, NULL))
&& !g_strcmp0 (port, g_variant_get_string (port_v, NULL)))
return g_variant_ref (attr_v);
}
/* neighbor not found */
return NULL;
}
static void
test_receive_frame (test_fixture *fixture, gconstpointer user_data)
test_recv (TestRecvFixture *fixture, gconstpointer user_data)
{
gs_unref_object NMLldpListener *listener = NULL;
GMainLoop *loop;
TestInfo info = { };
TestRecvCallbackInfo info = { };
GVariant *neighbors, *attr;
uint8_t frame[] = {
/* Ethernet header */
......@@ -209,11 +211,13 @@ test_receive_frame (test_fixture *fixture, gconstpointer user_data)
}
static void
fixture_teardown (test_fixture *fixture, gconstpointer user_data)
_test_recv_fixture_teardown (TestRecvFixture *fixture, gconstpointer user_data)
{
nm_platform_link_delete (NM_PLATFORM_GET, fixture->ifindex);
}
/*****************************************************************************/
void
init_tests (int *argc, char ***argv)
{
......@@ -223,6 +227,5 @@ init_tests (int *argc, char ***argv)
void
setup_tests (void)
{
g_test_add ("/lldp/receive_frame", test_fixture, NULL, fixture_setup,
test_receive_frame, fixture_teardown);
g_test_add ("/lldp/recv", TestRecvFixture, NULL, _test_recv_fixture_setup, test_recv, _test_recv_fixture_teardown);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment