libnm/tests: unify common test code for libnm and libnm-glib

Unify the common test code to drive the D-Bus stub service
test-networkmanager-service.py. They will be merged in the next
commit.
This commit is contained in:
Thomas Haller 2015-12-20 16:43:01 +01:00
parent 45951bca50
commit 70713ee197
6 changed files with 146 additions and 2 deletions

View file

@ -6,6 +6,7 @@ AM_CPPFLAGS = \
-I$(top_srcdir)/libnm-util \
-I$(top_builddir)/libnm-util \
-I$(top_srcdir)/libnm-glib \
-DNETWORKMANAGER_COMPILATION=NM_NETWORKMANAGER_COMPILATION_LIB_LEGACY \
-DNM_VERSION_MAX_ALLOWED=NM_VERSION_NEXT_STABLE \
-DTEST_NM_SERVICE=\"$(abs_top_srcdir)/tools/test-networkmanager-service.py\" \
$(GLIB_CFLAGS) \

View file

@ -20,11 +20,11 @@
#include "config.h"
#include <dbus/dbus.h>
#include <string.h>
#include "nm-default.h"
#include "NetworkManager.h"
#include "nm-dbus-compat.h"
#include "common.h"
@ -119,3 +119,120 @@ nm_test_service_cleanup (NMTestServiceInfo *info)
memset (info, 0, sizeof (*info));
g_free (info);
}
#if ((NETWORKMANAGER_COMPILATION) == NM_NETWORKMANAGER_COMPILATION_LIB)
typedef struct {
GMainLoop *loop;
const char *ifname;
char *path;
NMDevice *device;
} AddDeviceInfo;
static void
device_added_cb (NMClient *client,
NMDevice *device,
gpointer user_data)
{
AddDeviceInfo *info = user_data;
g_assert (device);
g_assert_cmpstr (nm_object_get_path (NM_OBJECT (device)), ==, info->path);
g_assert_cmpstr (nm_device_get_iface (device), ==, info->ifname);
info->device = device;
g_main_loop_quit (info->loop);
}
static gboolean
timeout (gpointer user_data)
{
g_assert_not_reached ();
return G_SOURCE_REMOVE;
}
static GVariant *
call_add_wired_device (GDBusProxy *proxy, const char *ifname, const char *hwaddr,
const char **subchannels, GError **error)
{
const char *empty[] = { NULL };
if (!hwaddr)
hwaddr = "/";
if (!subchannels)
subchannels = empty;
return g_dbus_proxy_call_sync (proxy,
"AddWiredDevice",
g_variant_new ("(ss^as)", ifname, hwaddr, subchannels),
G_DBUS_CALL_FLAGS_NO_AUTO_START,
3000,
NULL,
error);
}
static GVariant *
call_add_device (GDBusProxy *proxy, const char *method, const char *ifname, GError **error)
{
return g_dbus_proxy_call_sync (proxy,
method,
g_variant_new ("(s)", ifname),
G_DBUS_CALL_FLAGS_NO_AUTO_START,
3000,
NULL,
error);
}
static NMDevice *
add_device_common (NMTestServiceInfo *sinfo, NMClient *client,
const char *method, const char *ifname,
const char *hwaddr, const char **subchannels)
{
AddDeviceInfo info;
GError *error = NULL;
GVariant *ret;
guint timeout_id;
if (g_strcmp0 (method, "AddWiredDevice") == 0)
ret = call_add_wired_device (sinfo->proxy, ifname, hwaddr, subchannels, &error);
else
ret = call_add_device (sinfo->proxy, method, ifname, &error);
g_assert_no_error (error);
g_assert (ret);
g_assert_cmpstr (g_variant_get_type_string (ret), ==, "(o)");
g_variant_get (ret, "(o)", &info.path);
g_variant_unref (ret);
/* Wait for libnm to find the device */
info.ifname = ifname;
info.loop = g_main_loop_new (NULL, FALSE);
g_signal_connect (client, "device-added",
G_CALLBACK (device_added_cb), &info);
timeout_id = g_timeout_add_seconds (5, timeout, NULL);
g_main_loop_run (info.loop);
g_source_remove (timeout_id);
g_signal_handlers_disconnect_by_func (client, device_added_cb, &info);
g_free (info.path);
g_main_loop_unref (info.loop);
return info.device;
}
NMDevice *
nm_test_service_add_device (NMTestServiceInfo *sinfo, NMClient *client,
const char *method, const char *ifname)
{
return add_device_common (sinfo, client, method, ifname, NULL, NULL);
}
NMDevice *
nm_test_service_add_wired_device (NMTestServiceInfo *sinfo, NMClient *client,
const char *ifname, const char *hwaddr,
const char **subchannels)
{
return add_device_common (sinfo, client, "AddWiredDevice", ifname, hwaddr, subchannels);
}
#endif /* NM_NETWORKMANAGER_COMPILATION_LIB */

View file

@ -18,6 +18,8 @@
* Copyright 2014 Red Hat, Inc.
*/
#include <NetworkManager.h>
#include "nm-default.h"
typedef struct {
@ -29,3 +31,19 @@ typedef struct {
NMTestServiceInfo *nm_test_service_init (void);
void nm_test_service_cleanup (NMTestServiceInfo *info);
#if ((NETWORKMANAGER_COMPILATION) == NM_NETWORKMANAGER_COMPILATION_LIB)
NMDevice *nm_test_service_add_device (NMTestServiceInfo *info,
NMClient *client,
const char *method,
const char *ifname);
NMDevice * nm_test_service_add_wired_device (NMTestServiceInfo *sinfo,
NMClient *client,
const char *ifname,
const char *hwaddr,
const char **subchannels);
#endif /* NM_NETWORKMANAGER_COMPILATION_LIB */

View file

@ -7,7 +7,7 @@ AM_CPPFLAGS = \
-I$(top_builddir)/libnm \
-I$(top_srcdir)/libnm-core \
-I$(top_builddir)/libnm-core \
-DNETWORKMANAGER_COMPILATION \
-DNETWORKMANAGER_COMPILATION=NM_NETWORKMANAGER_COMPILATION_LIB \
-DNM_VERSION_MAX_ALLOWED=NM_VERSION_NEXT_STABLE \
-DTEST_NM_SERVICE=\"$(abs_top_srcdir)/tools/test-networkmanager-service.py\" \
$(GLIB_CFLAGS)

View file

@ -120,6 +120,8 @@ nm_test_service_cleanup (NMTestServiceInfo *info)
g_free (info);
}
#if ((NETWORKMANAGER_COMPILATION) == NM_NETWORKMANAGER_COMPILATION_LIB)
typedef struct {
GMainLoop *loop;
const char *ifname;
@ -232,3 +234,5 @@ nm_test_service_add_wired_device (NMTestServiceInfo *sinfo, NMClient *client,
{
return add_device_common (sinfo, client, "AddWiredDevice", ifname, hwaddr, subchannels);
}
#endif /* NM_NETWORKMANAGER_COMPILATION_LIB */

View file

@ -32,6 +32,8 @@ typedef struct {
NMTestServiceInfo *nm_test_service_init (void);
void nm_test_service_cleanup (NMTestServiceInfo *info);
#if ((NETWORKMANAGER_COMPILATION) == NM_NETWORKMANAGER_COMPILATION_LIB)
NMDevice *nm_test_service_add_device (NMTestServiceInfo *info,
NMClient *client,
const char *method,
@ -43,3 +45,5 @@ NMDevice * nm_test_service_add_wired_device (NMTestServiceInfo *sinfo,
const char *hwaddr,
const char **subchannels);
#endif /* NM_NETWORKMANAGER_COMPILATION_LIB */