mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2026-03-21 21:30:39 +01:00
libnm/tests: move common testing code to nm-test-libnm-utils
The unit tests for libnm and libnm-glib use a NetworkManager stub service written in Python (test-networkmanager-service.py). As they share the same server, it makes sense to also share the same utility code to drive the stub. Move the common code to include/. Note that contrary to "nm-test-utils.h", "nm-test-libnm-utils.h" is not a header-only file. Instead its implementation is in "nm-test-utils-impl.c". The reason for that this split is, if we later have yet another non-header-only test-utility, then all the implementations are in "nm-test-utils-impl.c", requiring the tests to link only one object file.
This commit is contained in:
parent
70713ee197
commit
fa3093e167
11 changed files with 19 additions and 304 deletions
|
|
@ -22,6 +22,8 @@
|
|||
|
||||
#include "nm-default.h"
|
||||
|
||||
#include "nm-test-utils.h"
|
||||
|
||||
typedef struct {
|
||||
GDBusConnection *bus;
|
||||
GDBusProxy *proxy;
|
||||
|
|
@ -14,7 +14,7 @@
|
|||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Copyright 2010 - 2014 Red Hat, Inc.
|
||||
* Copyright 2010 - 2015 Red Hat, Inc.
|
||||
*
|
||||
*/
|
||||
|
||||
|
|
@ -26,7 +26,7 @@
|
|||
#include "NetworkManager.h"
|
||||
#include "nm-dbus-compat.h"
|
||||
|
||||
#include "common.h"
|
||||
#include "nm-test-libnm-utils.h"
|
||||
|
||||
static gboolean
|
||||
name_exists (GDBusConnection *c, const char *name)
|
||||
|
|
@ -24,8 +24,8 @@ TESTS = test-nm-client test-remote-settings-client
|
|||
####### NMClient and non-settings tests #######
|
||||
|
||||
test_nm_client_SOURCES = \
|
||||
common.c \
|
||||
common.h \
|
||||
$(top_builddir)/include/nm-test-utils-impl.c \
|
||||
$(top_builddir)/include/nm-test-libnm-utils.h \
|
||||
test-nm-client.c
|
||||
|
||||
test_nm_client_LDADD = \
|
||||
|
|
@ -37,8 +37,8 @@ test_nm_client_LDADD = \
|
|||
####### remote settings client test #######
|
||||
|
||||
test_remote_settings_client_SOURCES = \
|
||||
common.c \
|
||||
common.h \
|
||||
$(top_builddir)/include/nm-test-utils-impl.c \
|
||||
$(top_builddir)/include/nm-test-libnm-utils.h \
|
||||
test-remote-settings-client.c
|
||||
|
||||
test_remote_settings_client_LDADD = \
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@
|
|||
|
||||
#include "nm-test-utils.h"
|
||||
|
||||
#include "common.h"
|
||||
#include "nm-test-libnm-utils.h"
|
||||
|
||||
static GMainLoop *loop = NULL;
|
||||
static NMTestServiceInfo *sinfo;
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@
|
|||
|
||||
#include "nm-default.h"
|
||||
#include "nm-remote-settings.h"
|
||||
#include "common.h"
|
||||
#include "nm-test-libnm-utils.h"
|
||||
|
||||
#include "nm-test-utils.h"
|
||||
|
||||
|
|
|
|||
|
|
@ -26,18 +26,18 @@ endif
|
|||
TESTS = test-nm-client test-remote-settings-client test-secret-agent
|
||||
|
||||
test_nm_client_SOURCES = \
|
||||
common.c \
|
||||
common.h \
|
||||
$(top_builddir)/include/nm-test-utils-impl.c \
|
||||
$(top_builddir)/include/nm-test-libnm-utils.h \
|
||||
test-nm-client.c
|
||||
|
||||
test_remote_settings_client_SOURCES = \
|
||||
common.c \
|
||||
common.h \
|
||||
$(top_builddir)/include/nm-test-utils-impl.c \
|
||||
$(top_builddir)/include/nm-test-libnm-utils.h \
|
||||
test-remote-settings-client.c
|
||||
|
||||
test_secret_agent_SOURCES = \
|
||||
common.c \
|
||||
common.h \
|
||||
$(top_builddir)/include/nm-test-utils-impl.c \
|
||||
$(top_builddir)/include/nm-test-libnm-utils.h \
|
||||
test-secret-agent.c
|
||||
endif
|
||||
|
||||
|
|
|
|||
|
|
@ -1,238 +0,0 @@
|
|||
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
|
||||
/*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2, or (at your option)
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Copyright 2010 - 2014 Red Hat, Inc.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "nm-default.h"
|
||||
#include "NetworkManager.h"
|
||||
#include "nm-dbus-compat.h"
|
||||
|
||||
#include "common.h"
|
||||
|
||||
static gboolean
|
||||
name_exists (GDBusConnection *c, const char *name)
|
||||
{
|
||||
GVariant *reply;
|
||||
gboolean exists = FALSE;
|
||||
|
||||
reply = g_dbus_connection_call_sync (c,
|
||||
DBUS_SERVICE_DBUS,
|
||||
DBUS_PATH_DBUS,
|
||||
DBUS_INTERFACE_DBUS,
|
||||
"GetNameOwner",
|
||||
g_variant_new ("(s)", name),
|
||||
NULL,
|
||||
G_DBUS_CALL_FLAGS_NO_AUTO_START,
|
||||
-1,
|
||||
NULL,
|
||||
NULL);
|
||||
if (reply != NULL) {
|
||||
exists = TRUE;
|
||||
g_variant_unref (reply);
|
||||
}
|
||||
|
||||
return exists;
|
||||
}
|
||||
|
||||
NMTestServiceInfo *
|
||||
nm_test_service_init (void)
|
||||
{
|
||||
NMTestServiceInfo *info;
|
||||
const char *args[2] = { TEST_NM_SERVICE, NULL };
|
||||
GError *error = NULL;
|
||||
int i;
|
||||
|
||||
info = g_malloc0 (sizeof (*info));
|
||||
|
||||
info->bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
|
||||
g_assert_no_error (error);
|
||||
|
||||
/* Spawn the test service. info->keepalive_fd will be a pipe to the service's
|
||||
* stdin; if it closes, the service will exit immediately. We use this to
|
||||
* make sure the service exits if the test program crashes.
|
||||
*/
|
||||
g_spawn_async_with_pipes (NULL, (char **) args, NULL, 0, NULL, NULL,
|
||||
&info->pid, &info->keepalive_fd, NULL, NULL, &error);
|
||||
g_assert_no_error (error);
|
||||
|
||||
/* Wait until the service is registered on the bus */
|
||||
for (i = 1000; i > 0; i--) {
|
||||
if (name_exists (info->bus, "org.freedesktop.NetworkManager"))
|
||||
break;
|
||||
g_usleep (G_USEC_PER_SEC / 50);
|
||||
}
|
||||
g_assert (i > 0);
|
||||
|
||||
/* Grab a proxy to our fake NM service to trigger tests */
|
||||
info->proxy = g_dbus_proxy_new_sync (info->bus,
|
||||
G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
|
||||
G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS |
|
||||
G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
|
||||
NULL,
|
||||
NM_DBUS_SERVICE,
|
||||
NM_DBUS_PATH,
|
||||
"org.freedesktop.NetworkManager.LibnmGlibTest",
|
||||
NULL, &error);
|
||||
g_assert_no_error (error);
|
||||
|
||||
return info;
|
||||
}
|
||||
|
||||
void
|
||||
nm_test_service_cleanup (NMTestServiceInfo *info)
|
||||
{
|
||||
int i;
|
||||
|
||||
g_object_unref (info->proxy);
|
||||
kill (info->pid, SIGTERM);
|
||||
|
||||
/* Wait until the bus notices the service is gone */
|
||||
for (i = 100; i > 0; i--) {
|
||||
if (!name_exists (info->bus, "org.freedesktop.NetworkManager"))
|
||||
break;
|
||||
g_usleep (G_USEC_PER_SEC / 50);
|
||||
}
|
||||
g_assert (i > 0);
|
||||
|
||||
g_object_unref (info->bus);
|
||||
close (info->keepalive_fd);
|
||||
|
||||
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 */
|
||||
|
|
@ -1,49 +0,0 @@
|
|||
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
|
||||
/*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the
|
||||
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||||
* Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Copyright 2014 Red Hat, Inc.
|
||||
*/
|
||||
|
||||
#include <NetworkManager.h>
|
||||
|
||||
#include "nm-default.h"
|
||||
|
||||
typedef struct {
|
||||
GDBusConnection *bus;
|
||||
GDBusProxy *proxy;
|
||||
GPid pid;
|
||||
int keepalive_fd;
|
||||
} NMTestServiceInfo;
|
||||
|
||||
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 */
|
||||
|
||||
|
|
@ -27,7 +27,7 @@
|
|||
#include <NetworkManager.h>
|
||||
|
||||
#include "nm-default.h"
|
||||
#include "common.h"
|
||||
#include "nm-test-libnm-utils.h"
|
||||
|
||||
#include "nm-test-utils.h"
|
||||
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@
|
|||
#include <NetworkManager.h>
|
||||
|
||||
#include "nm-default.h"
|
||||
#include "common.h"
|
||||
#include "nm-test-libnm-utils.h"
|
||||
|
||||
#include "nm-test-utils.h"
|
||||
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@
|
|||
#include <nm-secret-agent-old.h>
|
||||
|
||||
#include "nm-default.h"
|
||||
#include "common.h"
|
||||
#include "nm-test-libnm-utils.h"
|
||||
|
||||
#include "nm-test-utils.h"
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue