1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 * Copyright 2010 - 2015 Red Hat, Inc.
21 #include "nm-default.h"
25 #include "NetworkManager.h"
26 #include "nm-dbus-compat.h"
28 #if ((NETWORKMANAGER_COMPILATION) == NM_NETWORKMANAGER_COMPILATION_LIB_LEGACY)
29 #include "nm-dbus-glib-types.h"
32 #include "nm-test-libnm-utils.h"
34 /*****************************************************************************/
37 name_exists (GDBusConnection *c, const char *name)
40 gboolean exists = FALSE;
42 reply = g_dbus_connection_call_sync (c,
47 g_variant_new ("(s)", name),
49 G_DBUS_CALL_FLAGS_NO_AUTO_START,
55 g_variant_unref (reply);
61 #if ((NETWORKMANAGER_COMPILATION) == NM_NETWORKMANAGER_COMPILATION_LIB_LEGACY)
64 _libdbus_create_proxy_test (DBusGConnection *bus)
68 proxy = dbus_g_proxy_new_for_name (bus,
71 "org.freedesktop.NetworkManager.LibnmGlibTest");
74 dbus_g_proxy_set_default_timeout (proxy, G_MAXINT);
82 nmtstc_service_init (void)
84 NMTstcServiceInfo *info;
85 const char *args[2] = { TEST_NM_SERVICE, NULL };
89 info = g_malloc0 (sizeof (*info));
91 info->bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
92 g_assert_no_error (error);
94 /* Spawn the test service. info->keepalive_fd will be a pipe to the service's
95 * stdin; if it closes, the service will exit immediately. We use this to
96 * make sure the service exits if the test program crashes.
98 g_spawn_async_with_pipes (NULL, (char **) args, NULL, 0, NULL, NULL,
99 &info->pid, &info->keepalive_fd, NULL, NULL, &error);
100 g_assert_no_error (error);
102 /* Wait until the service is registered on the bus */
103 for (i = 1000; i > 0; i--) {
104 if (name_exists (info->bus, "org.freedesktop.NetworkManager"))
106 g_usleep (G_USEC_PER_SEC / 50);
110 /* Grab a proxy to our fake NM service to trigger tests */
111 info->proxy = g_dbus_proxy_new_sync (info->bus,
112 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
113 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS |
114 G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
118 "org.freedesktop.NetworkManager.LibnmGlibTest",
120 g_assert_no_error (error);
122 #if ((NETWORKMANAGER_COMPILATION) == NM_NETWORKMANAGER_COMPILATION_LIB_LEGACY)
123 info->libdbus.bus = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
124 g_assert_no_error (error);
125 g_assert (info->libdbus.bus);
131 nmtstc_service_cleanup (NMTstcServiceInfo *info)
135 g_object_unref (info->proxy);
136 kill (info->pid, SIGTERM);
138 /* Wait until the bus notices the service is gone */
139 for (i = 100; i > 0; i--) {
140 if (!name_exists (info->bus, "org.freedesktop.NetworkManager"))
142 g_usleep (G_USEC_PER_SEC / 50);
146 g_object_unref (info->bus);
147 close (info->keepalive_fd);
149 #if ((NETWORKMANAGER_COMPILATION) == NM_NETWORKMANAGER_COMPILATION_LIB_LEGACY)
150 g_clear_pointer (&info->libdbus.bus, dbus_g_connection_unref);
153 memset (info, 0, sizeof (*info));
157 #if ((NETWORKMANAGER_COMPILATION) == NM_NETWORKMANAGER_COMPILATION_LIB)
167 device_added_cb (NMClient *client,
171 AddDeviceInfo *info = user_data;
174 g_assert_cmpstr (nm_object_get_path (NM_OBJECT (device)), ==, info->path);
175 g_assert_cmpstr (nm_device_get_iface (device), ==, info->ifname);
177 info->device = device;
178 g_main_loop_quit (info->loop);
182 timeout (gpointer user_data)
184 g_assert_not_reached ();
185 return G_SOURCE_REMOVE;
189 call_add_wired_device (GDBusProxy *proxy, const char *ifname, const char *hwaddr,
190 const char **subchannels, GError **error)
192 const char *empty[] = { NULL };
199 return g_dbus_proxy_call_sync (proxy,
201 g_variant_new ("(ss^as)", ifname, hwaddr, subchannels),
202 G_DBUS_CALL_FLAGS_NO_AUTO_START,
209 call_add_device (GDBusProxy *proxy, const char *method, const char *ifname, GError **error)
211 return g_dbus_proxy_call_sync (proxy,
213 g_variant_new ("(s)", ifname),
214 G_DBUS_CALL_FLAGS_NO_AUTO_START,
221 add_device_common (NMTstcServiceInfo *sinfo, NMClient *client,
222 const char *method, const char *ifname,
223 const char *hwaddr, const char **subchannels)
226 GError *error = NULL;
230 if (g_strcmp0 (method, "AddWiredDevice") == 0)
231 ret = call_add_wired_device (sinfo->proxy, ifname, hwaddr, subchannels, &error);
233 ret = call_add_device (sinfo->proxy, method, ifname, &error);
235 g_assert_no_error (error);
237 g_assert_cmpstr (g_variant_get_type_string (ret), ==, "(o)");
238 g_variant_get (ret, "(o)", &info.path);
239 g_variant_unref (ret);
241 /* Wait for libnm to find the device */
242 info.ifname = ifname;
243 info.loop = g_main_loop_new (NULL, FALSE);
244 g_signal_connect (client, "device-added",
245 G_CALLBACK (device_added_cb), &info);
246 timeout_id = g_timeout_add_seconds (5, timeout, NULL);
247 g_main_loop_run (info.loop);
249 g_source_remove (timeout_id);
250 g_signal_handlers_disconnect_by_func (client, device_added_cb, &info);
252 g_main_loop_unref (info.loop);
258 nmtstc_service_add_device (NMTstcServiceInfo *sinfo, NMClient *client,
259 const char *method, const char *ifname)
261 return add_device_common (sinfo, client, method, ifname, NULL, NULL);
265 nmtstc_service_add_wired_device (NMTstcServiceInfo *sinfo, NMClient *client,
266 const char *ifname, const char *hwaddr,
267 const char **subchannels)
269 return add_device_common (sinfo, client, "AddWiredDevice", ifname, hwaddr, subchannels);
272 #endif /* NM_NETWORKMANAGER_COMPILATION_LIB */
275 nmtstc_service_add_connection (NMTstcServiceInfo *sinfo,
276 NMConnection *connection,
277 gboolean verify_connection,
280 #if ((NETWORKMANAGER_COMPILATION) == NM_NETWORKMANAGER_COMPILATION_LIB_LEGACY)
281 gs_unref_hashtable GHashTable *new_settings = NULL;
283 gs_free_error GError *error = NULL;
284 gs_free char *path = NULL;
285 gs_unref_object DBusGProxy *proxy = NULL;
288 g_assert (NM_IS_CONNECTION (connection));
290 new_settings = nm_connection_to_hash (connection, NM_SETTING_HASH_FLAG_ALL);
292 proxy = _libdbus_create_proxy_test (sinfo->libdbus.bus);
294 success = dbus_g_proxy_call (proxy,
297 DBUS_TYPE_G_MAP_OF_MAP_OF_VARIANT, new_settings,
298 G_TYPE_BOOLEAN, verify_connection,
300 DBUS_TYPE_G_OBJECT_PATH, &path,
302 g_assert_no_error (error);
305 g_assert (path && *path);
308 *out_path = g_strdup (path);
310 nmtstc_service_add_connection_variant (sinfo,
311 nm_connection_to_dbus (connection, NM_CONNECTION_SERIALIZE_ALL),
318 nmtstc_service_add_connection_variant (NMTstcServiceInfo *sinfo,
319 GVariant *connection,
320 gboolean verify_connection,
324 GError *error = NULL;
327 g_assert (G_IS_DBUS_PROXY (sinfo->proxy));
328 g_assert (g_variant_is_of_type (connection, G_VARIANT_TYPE ("a{sa{sv}}")));
330 result = g_dbus_proxy_call_sync (sinfo->proxy,
332 g_variant_new ("(vb)", connection, verify_connection),
333 G_DBUS_CALL_FLAGS_NO_AUTO_START,
337 g_assert_no_error (error);
338 g_assert (g_variant_is_of_type (result, G_VARIANT_TYPE ("(o)")));
340 g_variant_get (result, "(o)", out_path);
341 g_variant_unref (result);
345 nmtstc_service_update_connection (NMTstcServiceInfo *sinfo,
347 NMConnection *connection,
348 gboolean verify_connection)
351 path = nm_connection_get_path (connection);
354 #if ((NETWORKMANAGER_COMPILATION) == NM_NETWORKMANAGER_COMPILATION_LIB_LEGACY)
356 gs_unref_hashtable GHashTable *new_settings = NULL;
358 gs_free_error GError *error = NULL;
359 gs_unref_object DBusGProxy *proxy = NULL;
362 g_assert (NM_IS_CONNECTION (connection));
364 new_settings = nm_connection_to_hash (connection, NM_SETTING_HASH_FLAG_ALL);
366 proxy = _libdbus_create_proxy_test (sinfo->libdbus.bus);
368 success = dbus_g_proxy_call (proxy,
371 DBUS_TYPE_G_OBJECT_PATH, path,
372 DBUS_TYPE_G_MAP_OF_MAP_OF_VARIANT, new_settings,
373 G_TYPE_BOOLEAN, verify_connection,
376 g_assert_no_error (error);
380 nmtstc_service_update_connection_variant (sinfo,
382 nm_connection_to_dbus (connection, NM_CONNECTION_SERIALIZE_ALL),
388 nmtstc_service_update_connection_variant (NMTstcServiceInfo *sinfo,
390 GVariant *connection,
391 gboolean verify_connection)
394 GError *error = NULL;
397 g_assert (G_IS_DBUS_PROXY (sinfo->proxy));
398 g_assert (g_variant_is_of_type (connection, G_VARIANT_TYPE ("a{sa{sv}}")));
399 g_assert (path && path[0] == '/');
401 result = g_dbus_proxy_call_sync (sinfo->proxy,
403 g_variant_new ("(ovb)", path, connection, verify_connection),
404 G_DBUS_CALL_FLAGS_NO_AUTO_START,
408 g_assert_no_error (error);
409 g_assert (g_variant_is_of_type (result, G_VARIANT_TYPE ("()")));
410 g_variant_unref (result);
413 /*****************************************************************************/
415 #if ((NETWORKMANAGER_COMPILATION) == NM_NETWORKMANAGER_COMPILATION_LIB_LEGACY)
418 nmtstc_nm_client_new (void)
421 DBusGConnection *bus;
422 GError *error = NULL;
425 bus = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
426 g_assert_no_error (error);
429 client = g_object_new (NM_TYPE_CLIENT,
430 NM_OBJECT_DBUS_CONNECTION, bus,
431 NM_OBJECT_DBUS_PATH, NM_DBUS_PATH,
433 g_assert (client != NULL);
435 dbus_g_connection_unref (bus);
437 success = g_initable_init (G_INITABLE (client), NULL, &error);
438 g_assert_no_error (error);
439 g_assert (success == TRUE);
445 nmtstc_nm_remote_settings_new (void)
447 NMRemoteSettings *settings;
448 DBusGConnection *bus;
449 GError *error = NULL;
451 bus = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
452 g_assert_no_error (error);
455 settings = nm_remote_settings_new (bus);
458 dbus_g_connection_unref (bus);
463 #endif /* NM_NETWORKMANAGER_COMPILATION_LIB_LEGACY */
465 /*****************************************************************************/