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 - 2014 Red Hat, Inc.
21 #include "nm-default.h"
24 #include <sys/types.h>
27 #include "nm-test-libnm-utils.h"
29 static GMainLoop *loop = NULL;
30 static NMTstcServiceInfo *sinfo;
32 /*******************************************************************/
35 loop_quit (gpointer user_data)
37 g_main_loop_quit ((GMainLoop *) user_data);
38 return G_SOURCE_REMOVE;
41 /*******************************************************************/
44 devices_notify_cb (NMClient *c,
48 gboolean *notified = user_data;
49 const GPtrArray *devices;
52 devices = nm_client_get_devices (c);
54 g_assert_cmpint (devices->len, ==, 1);
56 device = g_ptr_array_index (devices, 0);
58 g_assert_cmpstr (nm_device_get_iface (device), ==, "eth0");
64 test_device_added (void)
67 const GPtrArray *devices;
69 gboolean notified = FALSE;
72 sinfo = nmtstc_service_init ();
73 client = nm_client_new (NULL, &error);
74 g_assert_no_error (error);
76 devices = nm_client_get_devices (client);
77 g_assert (devices->len == 0);
79 g_signal_connect (client,
81 (GCallback) devices_notify_cb,
84 /* Tell the test service to add a new device */
85 nmtstc_service_add_device (sinfo, client, "AddWiredDevice", "eth0");
87 /* coverity[loop_condition] */
89 g_main_context_iteration (NULL, TRUE);
91 g_signal_handlers_disconnect_by_func (client, devices_notify_cb, ¬ified);
93 devices = nm_client_get_devices (client);
95 g_assert_cmpint (devices->len, ==, 1);
97 device = g_ptr_array_index (devices, 0);
99 g_assert_cmpstr (nm_device_get_iface (device), ==, "eth0");
101 /* Try deleting the device via the ordinary NM interface, which should fail */
102 nm_device_delete (device, NULL, &error);
103 g_assert_error (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_NOT_SOFTWARE);
104 g_clear_error (&error);
106 g_object_unref (client);
107 g_clear_pointer (&sinfo, nmtstc_service_cleanup);
110 /*******************************************************************/
114 SIGNAL_SECOND = 0x02,
117 NOTIFY_SECOND = 0x20,
119 } DeviceSignaledAfterInitType;
122 device_sai_added_cb (NMClient *c,
126 guint *result = user_data;
129 g_assert_cmpstr (nm_device_get_iface (device), ==, "eth0");
131 g_assert ((*result & SIGNAL_MASK) == 0);
132 *result |= *result ? SIGNAL_SECOND : SIGNAL_FIRST;
136 devices_sai_notify_cb (NMClient *c,
140 guint *result = user_data;
141 const GPtrArray *devices;
144 devices = nm_client_get_devices (c);
146 g_assert_cmpint (devices->len, ==, 1);
148 device = g_ptr_array_index (devices, 0);
150 g_assert_cmpstr (nm_device_get_iface (device), ==, "eth0");
152 g_assert ((*result & NOTIFY_MASK) == 0);
153 *result |= *result ? NOTIFY_SECOND : NOTIFY_FIRST;
157 test_device_added_signal_after_init (void)
160 const GPtrArray *devices;
163 GError *error = NULL;
165 sinfo = nmtstc_service_init ();
166 client = nm_client_new (NULL, &error);
167 g_assert_no_error (error);
169 devices = nm_client_get_devices (client);
170 g_assert (devices->len == 0);
172 g_signal_connect (client,
173 NM_CLIENT_DEVICE_ADDED,
174 (GCallback) device_sai_added_cb,
177 g_signal_connect (client,
178 "notify::" NM_CLIENT_DEVICES,
179 (GCallback) devices_sai_notify_cb,
182 /* Tell the test service to add a new device */
183 nmtstc_service_add_device (sinfo, client, "AddWiredDevice", "eth0");
185 /* Ensure the 'device-added' signal doesn't show up before
186 * the 'Devices' property change notification */
187 /* coverity[loop_condition] */
188 while (!(result & SIGNAL_MASK) && !(result & NOTIFY_MASK))
189 g_main_context_iteration (NULL, TRUE);
191 g_signal_handlers_disconnect_by_func (client, device_sai_added_cb, &result);
192 g_signal_handlers_disconnect_by_func (client, devices_sai_notify_cb, &result);
194 g_assert ((result & SIGNAL_MASK) == SIGNAL_FIRST);
195 g_assert ((result & NOTIFY_MASK) == NOTIFY_SECOND);
197 devices = nm_client_get_devices (client);
199 g_assert_cmpint (devices->len, ==, 1);
201 device = g_ptr_array_index (devices, 0);
203 g_assert_cmpstr (nm_device_get_iface (device), ==, "eth0");
205 g_object_unref (client);
206 g_clear_pointer (&sinfo, nmtstc_service_cleanup);
209 /*******************************************************************/
211 static const char *expected_bssid = "66:55:44:33:22:11";
224 wifi_check_quit (WifiApInfo *info)
227 if (info->quit_count == 0) {
228 g_source_remove (info->quit_id);
230 g_main_loop_quit (info->loop);
235 got_ap_path (WifiApInfo *info, const char *path)
238 g_assert_cmpstr (info->ap_path, ==, path);
240 info->ap_path = g_strdup (path);
244 wifi_ap_added_cb (NMDeviceWifi *w,
249 g_assert_cmpstr (nm_access_point_get_bssid (ap), ==, expected_bssid);
250 got_ap_path (info, nm_object_get_path (NM_OBJECT (ap)));
252 info->signaled = TRUE;
253 wifi_check_quit (info);
257 wifi_ap_add_notify_cb (NMDeviceWifi *w,
261 const GPtrArray *aps;
264 aps = nm_device_wifi_get_access_points (w);
266 g_assert_cmpint (aps->len, ==, 1);
268 ap = g_ptr_array_index (aps, 0);
270 g_assert_cmpstr (nm_access_point_get_bssid (ap), ==, "66:55:44:33:22:11");
271 got_ap_path (info, nm_object_get_path (NM_OBJECT (ap)));
273 info->notified = TRUE;
274 wifi_check_quit (info);
278 wifi_ap_removed_cb (NMDeviceWifi *w,
283 g_assert_cmpstr (info->ap_path, ==, nm_object_get_path (NM_OBJECT (ap)));
285 info->signaled = TRUE;
286 wifi_check_quit (info);
290 wifi_ap_remove_notify_cb (NMDeviceWifi *w,
294 const GPtrArray *aps;
296 aps = nm_device_wifi_get_access_points (w);
297 g_assert (aps->len == 0);
299 info->notified = TRUE;
300 wifi_check_quit (info);
304 test_wifi_ap_added_removed (void)
308 WifiApInfo info = { loop, FALSE, FALSE, 0, 0 };
310 GError *error = NULL;
311 char *expected_path = NULL;
313 sinfo = nmtstc_service_init ();
314 client = nm_client_new (NULL, &error);
315 g_assert_no_error (error);
317 /*************************************/
318 /* Add the wifi device */
319 wifi = (NMDeviceWifi *) nmtstc_service_add_device (sinfo, client, "AddWifiDevice", "wlan0");
320 g_assert (NM_IS_DEVICE_WIFI (wifi));
322 /*************************************/
323 /* Add the wifi AP */
324 info.signaled = FALSE;
325 info.notified = FALSE;
328 ret = g_dbus_proxy_call_sync (sinfo->proxy,
330 g_variant_new ("(sss)", "wlan0", "test-ap", expected_bssid),
331 G_DBUS_CALL_FLAGS_NO_AUTO_START,
335 g_assert_no_error (error);
337 g_assert_cmpstr (g_variant_get_type_string (ret), ==, "(o)");
338 g_variant_get (ret, "(o)", &expected_path);
339 g_variant_unref (ret);
341 g_signal_connect (wifi,
342 "access-point-added",
343 (GCallback) wifi_ap_added_cb,
347 g_signal_connect (wifi,
348 "notify::access-points",
349 (GCallback) wifi_ap_add_notify_cb,
353 /* Wait for libnm to find the AP */
354 info.quit_id = g_timeout_add_seconds (5, loop_quit, loop);
355 g_main_loop_run (loop);
357 g_assert (info.signaled);
358 g_assert (info.notified);
359 g_assert (info.ap_path);
360 g_assert_cmpstr (info.ap_path, ==, expected_path);
361 g_signal_handlers_disconnect_by_func (wifi, wifi_ap_added_cb, &info);
362 g_signal_handlers_disconnect_by_func (wifi, wifi_ap_add_notify_cb, &info);
364 /*************************************/
365 /* Remove the wifi device */
366 info.signaled = FALSE;
367 info.notified = FALSE;
370 ret = g_dbus_proxy_call_sync (sinfo->proxy,
372 g_variant_new ("(so)", "wlan0", expected_path),
373 G_DBUS_CALL_FLAGS_NO_AUTO_START,
377 g_assert_no_error (error);
378 g_clear_pointer (&ret, g_variant_unref);
380 g_signal_connect (wifi,
381 "access-point-removed",
382 (GCallback) wifi_ap_removed_cb,
386 g_signal_connect (wifi,
387 "notify::access-points",
388 (GCallback) wifi_ap_remove_notify_cb,
392 /* Wait for libnm to find the AP */
393 info.quit_id = g_timeout_add_seconds (5, loop_quit, loop);
394 g_main_loop_run (loop);
396 g_assert (info.signaled);
397 g_assert (info.notified);
398 g_signal_handlers_disconnect_by_func (wifi, wifi_ap_removed_cb, &info);
399 g_signal_handlers_disconnect_by_func (wifi, wifi_ap_remove_notify_cb, &info);
401 g_free (info.ap_path);
402 g_free (expected_path);
404 g_object_unref (client);
405 g_clear_pointer (&sinfo, nmtstc_service_cleanup);
408 /*******************************************************************/
410 static const char *expected_nsp_name = "Clear";
423 wimax_check_quit (WimaxNspInfo *info)
426 if (info->quit_count == 0) {
427 g_source_remove (info->quit_id);
429 g_main_loop_quit (info->loop);
434 got_nsp_path (WimaxNspInfo *info, const char *path)
437 g_assert_cmpstr (info->nsp_path, ==, path);
439 info->nsp_path = g_strdup (path);
443 wimax_nsp_added_cb (NMDeviceWimax *w,
448 g_assert_cmpstr (nm_wimax_nsp_get_name (nsp), ==, expected_nsp_name);
449 got_nsp_path (info, nm_object_get_path (NM_OBJECT (nsp)));
451 info->signaled = TRUE;
452 wimax_check_quit (info);
456 wimax_nsp_add_notify_cb (NMDeviceWimax *w,
460 const GPtrArray *nsps;
463 nsps = nm_device_wimax_get_nsps (w);
465 g_assert_cmpint (nsps->len, ==, 1);
467 nsp = g_ptr_array_index (nsps, 0);
469 g_assert_cmpstr (nm_wimax_nsp_get_name (nsp), ==, expected_nsp_name);
470 got_nsp_path (info, nm_object_get_path (NM_OBJECT (nsp)));
472 info->notified = TRUE;
473 wimax_check_quit (info);
477 wimax_nsp_removed_cb (NMDeviceWimax *w,
482 g_assert_cmpstr (info->nsp_path, ==, nm_object_get_path (NM_OBJECT (nsp)));
484 info->signaled = TRUE;
485 wimax_check_quit (info);
489 wimax_nsp_remove_notify_cb (NMDeviceWimax *w,
493 const GPtrArray *nsps;
495 nsps = nm_device_wimax_get_nsps (w);
496 g_assert (nsps->len == 0);
498 info->notified = TRUE;
499 wimax_check_quit (info);
503 test_wimax_nsp_added_removed (void)
506 NMDeviceWimax *wimax;
507 WimaxNspInfo info = { loop, FALSE, FALSE, 0, 0 };
509 GError *error = NULL;
510 char *expected_path = NULL;
512 sinfo = nmtstc_service_init ();
513 client = nm_client_new (NULL, &error);
514 g_assert_no_error (error);
516 /*************************************/
517 /* Add the wimax device */
518 wimax = (NMDeviceWimax *) nmtstc_service_add_device (sinfo, client, "AddWimaxDevice", "wmx0");
519 g_assert (NM_IS_DEVICE_WIMAX (wimax));
521 /*************************************/
522 /* Add the wimax NSP */
523 info.signaled = FALSE;
524 info.notified = FALSE;
527 ret = g_dbus_proxy_call_sync (sinfo->proxy,
529 g_variant_new ("(ss)", "wmx0", expected_nsp_name),
530 G_DBUS_CALL_FLAGS_NO_AUTO_START,
534 g_assert_no_error (error);
536 g_assert_cmpstr (g_variant_get_type_string (ret), ==, "(o)");
537 g_variant_get (ret, "(o)", &expected_path);
538 g_variant_unref (ret);
540 g_signal_connect (wimax,
542 (GCallback) wimax_nsp_added_cb,
546 g_signal_connect (wimax,
548 (GCallback) wimax_nsp_add_notify_cb,
552 /* Wait for libnm to find the AP */
553 info.quit_id = g_timeout_add_seconds (5, loop_quit, loop);
554 g_main_loop_run (loop);
556 g_assert (info.signaled);
557 g_assert (info.notified);
558 g_assert (info.nsp_path);
559 g_assert_cmpstr (info.nsp_path, ==, expected_path);
560 g_signal_handlers_disconnect_by_func (wimax, wimax_nsp_added_cb, &info);
561 g_signal_handlers_disconnect_by_func (wimax, wimax_nsp_add_notify_cb, &info);
563 /*************************************/
564 /* Remove the wimax NSP */
565 info.signaled = FALSE;
566 info.notified = FALSE;
569 ret = g_dbus_proxy_call_sync (sinfo->proxy,
571 g_variant_new ("(so)", "wmx0", expected_path),
572 G_DBUS_CALL_FLAGS_NO_AUTO_START,
576 g_assert_no_error (error);
577 g_clear_pointer (&ret, g_variant_unref);
579 g_signal_connect (wimax,
581 (GCallback) wimax_nsp_removed_cb,
585 g_signal_connect (wimax,
587 (GCallback) wimax_nsp_remove_notify_cb,
591 /* Wait for libnm to find the AP */
592 info.quit_id = g_timeout_add_seconds (5, loop_quit, loop);
593 g_main_loop_run (loop);
595 g_assert (info.signaled);
596 g_assert (info.notified);
597 g_signal_handlers_disconnect_by_func (wimax, wimax_nsp_removed_cb, &info);
598 g_signal_handlers_disconnect_by_func (wimax, wimax_nsp_remove_notify_cb, &info);
600 g_free (info.nsp_path);
601 g_free (expected_path);
603 g_object_unref (client);
604 g_clear_pointer (&sinfo, nmtstc_service_cleanup);
607 /*******************************************************************/
618 da_check_quit (DaInfo *info)
621 if (info->quit_count == 0) {
622 g_source_remove (info->quit_id);
624 g_main_loop_quit (info->loop);
629 da_device_removed_cb (NMClient *c,
633 g_assert_cmpstr (nm_device_get_iface (device), ==, "eth0");
634 info->signaled = TRUE;
635 da_check_quit (info);
639 da_devices_notify_cb (NMClient *c,
643 const GPtrArray *devices;
648 devices = nm_client_get_devices (c);
650 g_assert_cmpint (devices->len, ==, 2);
652 for (i = 0; i < devices->len; i++) {
653 device = g_ptr_array_index (devices, i);
654 iface = nm_device_get_iface (device);
656 g_assert (!strcmp (iface, "wlan0") || !strcmp (iface, "eth1"));
659 info->notified = TRUE;
660 da_check_quit (info);
664 new_client_cb (GObject *object,
665 GAsyncResult *result,
668 NMClient **out_client = user_data;
669 GError *error = NULL;
671 *out_client = nm_client_new_finish (result, &error);
672 g_assert_no_error (error);
673 g_assert (*out_client != NULL);
675 g_main_loop_quit (loop);
679 test_devices_array (void)
681 NMClient *client = NULL;
682 DaInfo info = { loop };
683 NMDevice *wlan0, *eth0, *eth1, *device;
684 const GPtrArray *devices;
685 GError *error = NULL;
688 sinfo = nmtstc_service_init ();
690 /* Make sure that we test the async codepath in at least one test... */
691 nm_client_new_async (NULL, new_client_cb, &client);
692 g_main_loop_run (loop);
693 g_assert (client != NULL);
695 /*************************************/
696 /* Add some devices */
697 wlan0 = nmtstc_service_add_device (sinfo, client,"AddWifiDevice", "wlan0");
698 eth0 = nmtstc_service_add_device (sinfo, client, "AddWiredDevice", "eth0");
699 eth1 = nmtstc_service_add_device (sinfo, client, "AddWiredDevice", "eth1");
701 /* Ensure the devices now exist */
702 devices = nm_client_get_devices (client);
704 g_assert_cmpint (devices->len, ==, 3);
706 device = nm_client_get_device_by_iface (client, "wlan0");
707 g_assert (NM_IS_DEVICE_WIFI (device));
708 g_assert (device == wlan0);
710 device = nm_client_get_device_by_iface (client, "eth0");
711 g_assert (NM_IS_DEVICE_ETHERNET (device));
712 g_assert (device == eth0);
714 device = nm_client_get_device_by_iface (client, "eth1");
715 g_assert (NM_IS_DEVICE_ETHERNET (device));
716 g_assert (device == eth1);
718 /********************************/
719 /* Now remove the device in the middle */
720 ret = g_dbus_proxy_call_sync (sinfo->proxy,
722 g_variant_new ("(o)", nm_object_get_path (NM_OBJECT (eth0))),
723 G_DBUS_CALL_FLAGS_NO_AUTO_START,
727 g_assert_no_error (error);
729 g_variant_unref (ret);
731 g_signal_connect (client,
733 (GCallback) da_device_removed_cb,
736 g_signal_connect (client,
738 (GCallback) da_devices_notify_cb,
742 /* Wait for libnm to notice the changes */
743 info.quit_id = g_timeout_add_seconds (5, loop_quit, loop);
744 g_main_loop_run (loop);
746 g_assert_cmpint (info.quit_count, ==, 0);
747 g_signal_handlers_disconnect_by_func (client, da_device_removed_cb, &info);
748 g_signal_handlers_disconnect_by_func (client, da_devices_notify_cb, &info);
750 /* Ensure only two are left */
751 devices = nm_client_get_devices (client);
753 g_assert_cmpint (devices->len, ==, 2);
755 device = nm_client_get_device_by_iface (client, "wlan0");
756 g_assert (NM_IS_DEVICE_WIFI (device));
757 g_assert (device == wlan0);
759 device = nm_client_get_device_by_iface (client, "eth1");
760 g_assert (NM_IS_DEVICE_ETHERNET (device));
761 g_assert (device == eth1);
763 g_object_unref (client);
764 g_clear_pointer (&sinfo, nmtstc_service_cleanup);
768 nm_running_changed (GObject *client,
772 int *running_changed = user_data;
774 (*running_changed)++;
775 g_main_loop_quit (loop);
779 test_client_nm_running (void)
781 NMClient *client1, *client2;
783 int running_changed = 0;
784 GError *error = NULL;
786 client1 = nm_client_new (NULL, &error);
787 g_assert_no_error (error);
789 g_assert (!nm_client_get_nm_running (client1));
790 g_assert_cmpstr (nm_client_get_version (client1), ==, NULL);
792 g_assert (!nm_client_networking_get_enabled (client1));
793 /* This will have no effect, but it shouldn't cause any warnings either. */
794 nm_client_networking_set_enabled (client1, TRUE, NULL);
795 g_assert (!nm_client_networking_get_enabled (client1));
797 /* OTOH, this should result in an error */
798 nm_client_set_logging (client1, "DEFAULT", "INFO", &error);
799 g_assert_error (error, NM_CLIENT_ERROR, NM_CLIENT_ERROR_MANAGER_NOT_RUNNING);
800 g_clear_error (&error);
802 /* Now start the test service. */
803 sinfo = nmtstc_service_init ();
804 client2 = nm_client_new (NULL, &error);
805 g_assert_no_error (error);
807 /* client2 should know that NM is running, but the previously-created
808 * client1 hasn't gotten the news yet.
810 g_assert (!nm_client_get_nm_running (client1));
811 g_assert (nm_client_get_nm_running (client2));
813 g_signal_connect (client1, "notify::" NM_CLIENT_NM_RUNNING,
814 G_CALLBACK (nm_running_changed), &running_changed);
815 quit_id = g_timeout_add_seconds (5, loop_quit, loop);
816 g_main_loop_run (loop);
817 g_assert_cmpint (running_changed, ==, 1);
818 g_assert (nm_client_get_nm_running (client1));
819 g_source_remove (quit_id);
822 g_clear_pointer (&sinfo, nmtstc_service_cleanup);
824 g_assert (nm_client_get_nm_running (client1));
826 quit_id = g_timeout_add_seconds (5, loop_quit, loop);
827 g_main_loop_run (loop);
828 g_assert_cmpint (running_changed, ==, 2);
829 g_assert (!nm_client_get_nm_running (client1));
830 g_source_remove (quit_id);
832 g_object_unref (client1);
833 g_object_unref (client2);
838 NMActiveConnection *ac;
844 assert_ac_and_device (NMClient *client)
846 const GPtrArray *devices, *acs, *ac_devices;
847 NMDevice *device, *ac_device;
848 NMActiveConnection *ac, *device_ac;
850 acs = nm_client_get_active_connections (client);
851 g_assert (acs != NULL);
852 g_assert_cmpint (acs->len, ==, 1);
853 devices = nm_client_get_devices (client);
854 g_assert (devices != NULL);
855 g_assert_cmpint (devices->len, >=, 1);
858 ac_devices = nm_active_connection_get_devices (ac);
859 g_assert (ac_devices != NULL);
860 g_assert_cmpint (ac_devices->len, ==, 1);
861 ac_device = ac_devices->pdata[0];
862 g_assert (ac_device != NULL);
864 device = devices->pdata[0];
865 if (device != ac_device && devices->len > 1)
866 device = devices->pdata[1];
867 device_ac = nm_device_get_active_connection (device);
868 g_assert (device_ac != NULL);
870 g_assert_cmpstr (nm_object_get_path (NM_OBJECT (device)), ==, nm_object_get_path (NM_OBJECT (ac_device)));
871 g_assert (device == ac_device);
872 g_assert_cmpstr (nm_object_get_path (NM_OBJECT (ac)), ==, nm_object_get_path (NM_OBJECT (device_ac)));
873 g_assert (ac == device_ac);
877 add_and_activate_cb (GObject *object,
878 GAsyncResult *result,
881 NMClient *client = NM_CLIENT (object);
882 TestACInfo *info = user_data;
883 GError *error = NULL;
885 info->ac = nm_client_add_and_activate_connection_finish (client, result, &error);
886 g_assert_no_error (error);
887 g_assert (info->ac != NULL);
889 assert_ac_and_device (client);
892 if (!info->remaining)
893 g_main_loop_quit (info->loop);
897 client_acs_changed_cb (GObject *client,
901 TestACInfo *info = user_data;
902 const GPtrArray *acs;
904 acs = nm_client_get_active_connections (NM_CLIENT (client));
905 g_assert (acs != NULL);
906 g_assert_cmpint (acs->len, ==, 1);
909 if (!info->remaining)
910 g_main_loop_quit (info->loop);
914 device_ac_changed_cb (GObject *device,
918 TestACInfo *info = user_data;
920 g_assert (nm_device_get_active_connection (NM_DEVICE (device)) != NULL);
923 if (!info->remaining)
924 g_main_loop_quit (info->loop);
928 test_active_connections (void)
933 TestACInfo info = { loop, NULL, 0 };
934 GError *error = NULL;
936 sinfo = nmtstc_service_init ();
937 client = nm_client_new (NULL, &error);
938 g_assert_no_error (error);
940 /* Tell the test service to add a new device */
941 device = nmtstc_service_add_device (sinfo, client, "AddWiredDevice", "eth0");
943 conn = nmtst_create_minimal_connection ("test-ac", NULL, NM_SETTING_WIRED_SETTING_NAME, NULL);
944 nm_client_add_and_activate_connection_async (client, conn, device, NULL,
945 NULL, add_and_activate_cb, &info);
946 g_object_unref (conn);
948 g_signal_connect (client, "notify::" NM_CLIENT_ACTIVE_CONNECTIONS,
949 G_CALLBACK (client_acs_changed_cb), &info);
950 g_signal_connect (device, "notify::" NM_DEVICE_ACTIVE_CONNECTION,
951 G_CALLBACK (device_ac_changed_cb), &info);
953 /* Two signals plus activate_cb */
955 g_main_loop_run (loop);
956 g_signal_handlers_disconnect_by_func (client, client_acs_changed_cb, &info);
957 g_signal_handlers_disconnect_by_func (device, device_ac_changed_cb, &info);
959 g_assert (info.ac != NULL);
961 g_object_unref (info.ac);
962 g_object_unref (client);
964 /* Ensure that we can correctly resolve the recursive property link between the
965 * AC and the Device in a newly-created client.
967 client = nm_client_new (NULL, &error);
968 g_assert_no_error (error);
969 assert_ac_and_device (client);
970 g_object_unref (client);
973 nm_client_new_async (NULL, new_client_cb, &client);
974 g_main_loop_run (loop);
975 assert_ac_and_device (client);
976 g_object_unref (client);
978 g_clear_pointer (&sinfo, nmtstc_service_cleanup);
982 client_devices_changed_cb (GObject *client,
986 TestACInfo *info = user_data;
987 const GPtrArray *devices;
990 devices = nm_client_get_devices (NM_CLIENT (client));
991 g_assert (devices != NULL);
992 if (devices->len < 2)
994 g_assert_cmpint (devices->len, ==, 2);
996 if (NM_IS_DEVICE_VLAN (devices->pdata[0]))
997 device = devices->pdata[0];
998 else if (NM_IS_DEVICE_VLAN (devices->pdata[1]))
999 device = devices->pdata[1];
1001 g_assert_not_reached ();
1003 g_assert_cmpstr (nm_device_get_iface (device), ==, "eth0.1");
1005 if (!nm_device_get_active_connection (device)) {
1007 g_signal_connect (device, "notify::" NM_DEVICE_ACTIVE_CONNECTION,
1008 G_CALLBACK (device_ac_changed_cb), info);
1012 if (!info->remaining)
1013 g_main_loop_quit (info->loop);
1018 NMRemoteConnection *remote;
1019 } TestConnectionInfo;
1022 add_connection_cb (GObject *object,
1023 GAsyncResult *result,
1026 TestConnectionInfo *info = user_data;
1027 GError *error = NULL;
1029 info->remote = nm_client_add_connection_finish (NM_CLIENT (object), result, &error);
1030 g_assert_no_error (error);
1031 g_main_loop_quit (info->loop);
1035 activate_cb (GObject *object,
1036 GAsyncResult *result,
1039 NMClient *client = NM_CLIENT (object);
1040 TestACInfo *info = user_data;
1041 GError *error = NULL;
1043 info->ac = nm_client_activate_connection_finish (client, result, &error);
1044 g_assert_no_error (error);
1045 g_assert (info->ac != NULL);
1047 assert_ac_and_device (client);
1050 if (!info->remaining)
1051 g_main_loop_quit (info->loop);
1055 test_activate_virtual (void)
1059 NMSettingConnection *s_con;
1060 NMSettingVlan *s_vlan;
1061 TestACInfo info = { loop, NULL, 0 };
1062 TestConnectionInfo conn_info = { loop, NULL };
1063 GError *error = NULL;
1065 sinfo = nmtstc_service_init ();
1066 client = nm_client_new (NULL, &error);
1067 g_assert_no_error (error);
1069 nmtstc_service_add_device (sinfo, client, "AddWiredDevice", "eth0");
1071 conn = nmtst_create_minimal_connection ("test-ac", NULL, NM_SETTING_VLAN_SETTING_NAME, &s_con);
1072 g_object_set (s_con,
1073 NM_SETTING_CONNECTION_INTERFACE_NAME, "eth0.1",
1075 s_vlan = nm_connection_get_setting_vlan (conn);
1076 g_object_set (s_vlan,
1077 NM_SETTING_VLAN_ID, 1,
1078 NM_SETTING_VLAN_PARENT, "eth0",
1081 nm_client_add_connection_async (client, conn, TRUE,
1082 NULL, add_connection_cb, &conn_info);
1083 g_main_loop_run (loop);
1084 g_object_unref (conn);
1085 conn = NM_CONNECTION (conn_info.remote);
1087 nm_client_activate_connection_async (client, conn, NULL, NULL,
1088 NULL, activate_cb, &info);
1089 g_object_unref (conn);
1091 g_signal_connect (client, "notify::" NM_CLIENT_ACTIVE_CONNECTIONS,
1092 G_CALLBACK (client_acs_changed_cb), &info);
1093 g_signal_connect (client, "notify::" NM_CLIENT_DEVICES,
1094 G_CALLBACK (client_devices_changed_cb), &info);
1096 /* We're expecting a client::devices change, client::activate callback,
1097 * and a device::active-connection change.
1098 * The client::devices callback can hook a client::active-connections
1099 * change and bump this if the property is not yet loaded.
1103 g_main_loop_run (loop);
1104 g_signal_handlers_disconnect_by_func (client, client_acs_changed_cb, &info);
1105 g_signal_handlers_disconnect_by_func (client, client_devices_changed_cb, &info);
1107 g_assert (info.ac != NULL);
1109 g_object_unref (info.ac);
1110 g_object_unref (client);
1112 g_clear_pointer (&sinfo, nmtstc_service_cleanup);
1116 activate_failed_cb (GObject *object,
1117 GAsyncResult *result,
1120 NMClient *client = NM_CLIENT (object);
1121 NMActiveConnection *ac;
1122 GError *error = NULL;
1124 ac = nm_client_activate_connection_finish (client, result, &error);
1125 g_assert (ac == NULL);
1126 g_assert_error (error, NM_CLIENT_ERROR, NM_CLIENT_ERROR_OBJECT_CREATION_FAILED);
1127 g_clear_error (&error);
1129 g_main_loop_quit (loop);
1133 test_activate_failed (void)
1138 GError *error = NULL;
1140 sinfo = nmtstc_service_init ();
1141 client = nm_client_new (NULL, &error);
1142 g_assert_no_error (error);
1144 device = nmtstc_service_add_device (sinfo, client, "AddWiredDevice", "eth0");
1146 /* Note that test-networkmanager-service.py checks for this exact name */
1147 conn = nmtst_create_minimal_connection ("object-creation-failed-test", NULL,
1148 NM_SETTING_WIRED_SETTING_NAME, NULL);
1150 nm_client_add_and_activate_connection_async (client, conn, device, NULL,
1151 NULL, activate_failed_cb, NULL);
1152 g_main_loop_run (loop);
1154 g_object_unref (conn);
1155 g_object_unref (client);
1157 g_clear_pointer (&sinfo, nmtstc_service_cleanup);
1161 test_device_connection_compatibility (void)
1164 NMDevice *device1, *device2;
1166 NMSettingWired *s_wired;
1167 GError *error = NULL;
1168 const char *subchannels[] = { "0.0.8000", "0.0.8001", "0.0.8002", NULL };
1169 const char *subchannels_2[] = { "0.0.8000", "0.0.8001", NULL };
1170 const char *subchannels_x[] = { "0.0.8000", "0.0.8001", "0.0.800X", NULL };
1171 const char *hw_addr1 = "52:54:00:ab:db:23";
1172 const char *hw_addr2 = "52:54:00:ab:db:24";
1174 sinfo = nmtstc_service_init ();
1175 client = nm_client_new (NULL, &error);
1176 g_assert_no_error (error);
1178 /* Create two devices */
1179 device1 = nmtstc_service_add_wired_device (sinfo, client, "eth0", hw_addr1, subchannels);
1180 device2 = nmtstc_service_add_wired_device (sinfo, client, "eth1", hw_addr2, NULL);
1182 g_assert_cmpstr (nm_device_get_hw_address (device1), ==, hw_addr1);
1183 g_assert_cmpstr (nm_device_get_hw_address (device2), ==, hw_addr2);
1185 conn = nmtst_create_minimal_connection ("wired-matches", NULL,
1186 NM_SETTING_WIRED_SETTING_NAME, NULL);
1187 s_wired = nm_connection_get_setting_wired (conn);
1188 nm_setting_wired_add_mac_blacklist_item (s_wired, "00:11:22:33:44:55");
1190 /* device1 and conn are compatible */
1191 g_object_set (s_wired,
1192 NM_SETTING_WIRED_MAC_ADDRESS, hw_addr1,
1193 NM_SETTING_WIRED_S390_SUBCHANNELS, subchannels,
1195 nm_device_connection_compatible (device1, conn, &error);
1196 g_assert_no_error (error);
1198 /* device2 and conn differ in subchannels */
1199 g_object_set (s_wired, NM_SETTING_WIRED_S390_SUBCHANNELS, subchannels_x, NULL);
1200 nm_device_connection_compatible (device2, conn, &error);
1201 g_assert_error (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_INCOMPATIBLE_CONNECTION);
1202 g_clear_error (&error);
1204 /* device1 and conn differ in subchannels - 2 in connection, 3 in device */
1205 g_object_set (s_wired, NM_SETTING_WIRED_S390_SUBCHANNELS, subchannels_2, NULL);
1206 nm_device_connection_compatible (device1, conn, &error);
1207 g_assert_error (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_INCOMPATIBLE_CONNECTION);
1208 g_clear_error (&error);
1210 g_object_set (s_wired, NM_SETTING_WIRED_S390_SUBCHANNELS, NULL, NULL);
1212 /* device2 and conn differ in MAC address */
1213 g_object_set (s_wired, NM_SETTING_WIRED_MAC_ADDRESS, "aa:bb:cc:dd:ee:ee", NULL);
1214 nm_device_connection_compatible (device2, conn, &error);
1215 g_assert_error (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_INCOMPATIBLE_CONNECTION);
1216 g_clear_error (&error);
1217 g_object_set (s_wired, NM_SETTING_WIRED_MAC_ADDRESS, NULL, NULL);
1219 /* device1 is blacklisted in conn */
1220 nm_setting_wired_add_mac_blacklist_item (s_wired, hw_addr1);
1221 nm_device_connection_compatible (device1, conn, &error);
1222 g_assert_error (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_INCOMPATIBLE_CONNECTION);
1223 g_clear_error (&error);
1225 g_object_unref (conn);
1226 g_object_unref (client);
1228 g_clear_pointer (&sinfo, nmtstc_service_cleanup);
1231 /*******************************************************************/
1234 _test_connection_invalid_find_connections (gpointer element, gpointer needle, gpointer user_data)
1236 NMRemoteConnection *con = NM_REMOTE_CONNECTION (element);
1237 const char *path = needle;
1239 g_assert (NM_IS_REMOTE_CONNECTION (con));
1240 g_assert (path && *path);
1242 return strcmp (path, nm_connection_get_path ((NMConnection *) con)) == 0;
1245 #define ASSERT_IDX(i) \
1247 g_assert_cmpint (idx[i], >=, 0); \
1248 g_assert (path##i && *path##i); \
1249 g_assert (NM_IS_REMOTE_CONNECTION (connections->pdata[idx[i]])); \
1250 g_assert_cmpstr (nm_connection_get_path (connections->pdata[idx[i]]), ==, path##i); \
1254 test_connection_invalid (void)
1256 NMTSTC_SERVICE_INFO_SETUP (my_sinfo)
1257 gs_unref_object NMConnection *connection = NULL;
1258 NMSettingConnection *s_con;
1259 gs_unref_object NMClient *client = NULL;
1260 const GPtrArray *connections;
1261 gs_free_error GError *error = NULL;
1262 gs_free char *path0 = NULL;
1263 gs_free char *path1 = NULL;
1264 gs_free char *path2 = NULL;
1265 gs_free char *path3 = NULL;
1266 gs_free char *uuid2 = NULL;
1269 gs_unref_variant GVariant *variant = NULL;
1271 /**************************************************************************
1272 * Add three connections before starting libnm. One valid, two invalid.
1273 *************************************************************************/
1275 connection = nmtst_create_minimal_connection ("test-connection-invalid-0", NULL, NM_SETTING_WIRED_SETTING_NAME, &s_con);
1276 nmtst_connection_normalize (connection);
1277 g_object_set (s_con,
1278 NM_SETTING_CONNECTION_UUID, nmtst_uuid_generate (),
1280 nmtstc_service_add_connection (my_sinfo,
1285 nm_connection_remove_setting (connection, NM_TYPE_SETTING_WIRED);
1286 g_object_set (s_con,
1287 NM_SETTING_CONNECTION_ID, "test-connection-invalid-1",
1288 NM_SETTING_CONNECTION_TYPE, "invalid-type-1",
1289 NM_SETTING_CONNECTION_UUID, nmtst_uuid_generate (),
1291 nmtstc_service_add_connection (my_sinfo,
1296 g_object_set (s_con,
1297 NM_SETTING_CONNECTION_ID, "test-connection-invalid-2",
1298 NM_SETTING_CONNECTION_TYPE, "invalid-type-2",
1299 NM_SETTING_CONNECTION_UUID, nmtst_uuid_generate (),
1301 variant = nm_connection_to_dbus (connection, NM_CONNECTION_SERIALIZE_ALL);
1302 NMTST_VARIANT_EDITOR (variant,
1303 NMTST_VARIANT_ADD_SETTING ("invalid-type-2",
1304 nmtst_variant_new_vardict ("some-key1", g_variant_new_string ("some-value1"),
1305 "some-key2", g_variant_new_uint32 (4722))));
1306 g_variant_ref_sink (variant);
1307 nmtstc_service_add_connection_variant (my_sinfo,
1312 client = nm_client_new (NULL, &error);
1313 g_assert_no_error (error);
1315 connections = nm_client_get_connections (client);
1316 g_assert (connections);
1318 g_assert_cmpint (connections->len, ==, 3);
1319 n_found = nmtst_find_all_indexes (connections->pdata,
1321 (gpointer *) ((const char *[]) { path0, path1, path2 }),
1323 _test_connection_invalid_find_connections,
1326 g_assert_cmpint (n_found, ==, 3);
1330 nmtst_assert_connection_verifies_without_normalization (connections->pdata[idx[0]]);
1331 nmtst_assert_connection_unnormalizable (connections->pdata[idx[1]], 0, 0);
1332 nmtst_assert_connection_unnormalizable (connections->pdata[idx[2]], 0, 0);
1334 /**************************************************************************
1335 * After having the client up and running, add another invalid connection
1336 *************************************************************************/
1338 g_object_set (s_con,
1339 NM_SETTING_CONNECTION_ID, "test-connection-invalid-2",
1340 NM_SETTING_CONNECTION_TYPE, "invalid-type-2",
1341 NM_SETTING_CONNECTION_UUID, (uuid2 = g_strdup (nmtst_uuid_generate ())),
1343 nmtstc_service_add_connection (my_sinfo,
1349 nmtst_main_loop_run (loop, 100);
1352 connections = nm_client_get_connections (client);
1353 g_assert (connections);
1355 g_assert_cmpint (connections->len, ==, 4);
1356 n_found = nmtst_find_all_indexes (connections->pdata,
1358 (gpointer *) ((const char *[]) { path0, path1, path2, path3 }),
1360 _test_connection_invalid_find_connections,
1363 g_assert_cmpint (n_found, ==, 4);
1368 nmtst_assert_connection_verifies_without_normalization (connections->pdata[idx[0]]);
1369 nmtst_assert_connection_unnormalizable (connections->pdata[idx[1]], 0, 0);
1370 nmtst_assert_connection_unnormalizable (connections->pdata[idx[2]], 0, 0);
1371 nmtst_assert_connection_unnormalizable (connections->pdata[idx[3]], 0, 0);
1373 /**************************************************************************
1374 * Modify the invalid connection (still invalid)
1375 *************************************************************************/
1377 NMTST_VARIANT_EDITOR (variant,
1378 NMTST_VARIANT_CHANGE_PROPERTY ("invalid-type-2",
1379 "some-key2", "u", 4721));
1380 g_variant_ref_sink (variant);
1381 nmtstc_service_update_connection_variant (my_sinfo,
1386 nmtst_main_loop_run (loop, 100);
1388 connections = nm_client_get_connections (client);
1389 g_assert (connections);
1391 g_assert_cmpint (connections->len, ==, 4);
1392 n_found = nmtst_find_all_indexes (connections->pdata,
1394 (gpointer *) ((const char *[]) { path0, path1, path2, path3 }),
1396 _test_connection_invalid_find_connections,
1399 g_assert_cmpint (n_found, ==, 4);
1404 nmtst_assert_connection_verifies_without_normalization (connections->pdata[idx[0]]);
1405 nmtst_assert_connection_unnormalizable (connections->pdata[idx[1]], 0, 0);
1406 nmtst_assert_connection_unnormalizable (connections->pdata[idx[2]], 0, 0);
1407 nmtst_assert_connection_unnormalizable (connections->pdata[idx[3]], 0, 0);
1409 /**************************************************************************
1410 * Modify the invalid connection (becomes valid)
1411 *************************************************************************/
1413 NMTST_VARIANT_EDITOR (variant,
1414 NMTST_VARIANT_DROP_SETTING ("invalid-type-2"));
1415 NMTST_VARIANT_EDITOR (variant,
1416 NMTST_VARIANT_CHANGE_PROPERTY (NM_SETTING_CONNECTION_SETTING_NAME,
1417 NM_SETTING_CONNECTION_TYPE, "s", NM_SETTING_WIRED_SETTING_NAME));
1418 g_variant_ref_sink (variant);
1419 nmtstc_service_update_connection_variant (my_sinfo,
1424 nmtst_main_loop_run (loop, 100);
1426 connections = nm_client_get_connections (client);
1427 g_assert (connections);
1429 g_assert_cmpint (connections->len, ==, 4);
1430 n_found = nmtst_find_all_indexes (connections->pdata,
1432 (gpointer *) ((const char *[]) { path0, path1, path2, path3 }),
1434 _test_connection_invalid_find_connections,
1437 g_assert_cmpint (n_found, ==, 4);
1442 nmtst_assert_connection_verifies_without_normalization (connections->pdata[idx[0]]);
1443 nmtst_assert_connection_unnormalizable (connections->pdata[idx[1]], 0, 0);
1444 nmtst_assert_connection_verifies_after_normalization (connections->pdata[idx[2]], 0, 0);
1445 nmtst_assert_connection_unnormalizable (connections->pdata[idx[3]], 0, 0);
1447 /**************************************************************************
1448 * Modify the invalid connection (still invalid)
1449 *************************************************************************/
1451 g_object_set (s_con,
1452 NM_SETTING_CONNECTION_ID, "test-connection-invalid-2x",
1454 nmtstc_service_update_connection (my_sinfo,
1459 nmtst_main_loop_run (loop, 100);
1461 connections = nm_client_get_connections (client);
1462 g_assert (connections);
1464 g_assert_cmpint (connections->len, ==, 4);
1465 n_found = nmtst_find_all_indexes (connections->pdata,
1467 (gpointer *) ((const char *[]) { path0, path1, path2, path3 }),
1469 _test_connection_invalid_find_connections,
1472 g_assert_cmpint (n_found, ==, 4);
1477 nmtst_assert_connection_verifies_without_normalization (connections->pdata[idx[0]]);
1478 nmtst_assert_connection_unnormalizable (connections->pdata[idx[1]], 0, 0);
1479 nmtst_assert_connection_verifies_after_normalization (connections->pdata[idx[2]], 0, 0);
1480 nmtst_assert_connection_unnormalizable (connections->pdata[idx[3]], 0, 0);
1481 g_assert_cmpstr ("test-connection-invalid-2x", ==, nm_connection_get_id (connections->pdata[idx[3]]));
1483 /**************************************************************************
1484 * Modify the invalid connection (now becomes valid)
1485 *************************************************************************/
1487 g_clear_object (&connection);
1488 connection = nmtst_create_minimal_connection ("test-connection-invalid-2", NULL, NM_SETTING_WIRED_SETTING_NAME, &s_con);
1489 nmtst_connection_normalize (connection);
1490 g_object_set (s_con,
1491 NM_SETTING_CONNECTION_ID, "test-connection-invalid-2z",
1492 NM_SETTING_CONNECTION_TYPE, "802-3-ethernet",
1493 NM_SETTING_CONNECTION_UUID, uuid2,
1496 nmtstc_service_update_connection (my_sinfo,
1501 nmtst_main_loop_run (loop, 100);
1503 connections = nm_client_get_connections (client);
1504 g_assert (connections);
1506 g_assert_cmpint (connections->len, ==, 4);
1507 n_found = nmtst_find_all_indexes (connections->pdata,
1509 (gpointer *) ((const char *[]) { path0, path1, path2, path3 }),
1511 _test_connection_invalid_find_connections,
1514 g_assert_cmpint (n_found, ==, 4);
1519 nmtst_assert_connection_verifies_without_normalization (connections->pdata[idx[0]]);
1520 nmtst_assert_connection_unnormalizable (connections->pdata[idx[1]], 0, 0);
1521 nmtst_assert_connection_verifies_after_normalization (connections->pdata[idx[2]], 0, 0);
1522 nmtst_assert_connection_verifies_without_normalization (connections->pdata[idx[3]]);
1523 g_assert_cmpstr ("test-connection-invalid-2z", ==, nm_connection_get_id (connections->pdata[idx[3]]));
1525 /**************************************************************************
1526 * Modify the invalid connection and make it valid
1527 *************************************************************************/
1529 g_clear_object (&connection);
1530 connection = nmtst_create_minimal_connection ("test-connection-invalid-1", NULL, NM_SETTING_WIRED_SETTING_NAME, &s_con);
1531 nmtst_connection_normalize (connection);
1532 g_object_set (s_con,
1533 NM_SETTING_CONNECTION_ID, "test-connection-invalid-1x",
1534 NM_SETTING_CONNECTION_TYPE, "802-3-ethernet",
1535 NM_SETTING_CONNECTION_UUID, nm_connection_get_uuid (connections->pdata[idx[1]]),
1538 nmtstc_service_update_connection (my_sinfo,
1543 nmtst_main_loop_run (loop, 100);
1545 connections = nm_client_get_connections (client);
1546 g_assert (connections);
1548 g_assert_cmpint (connections->len, ==, 4);
1549 n_found = nmtst_find_all_indexes (connections->pdata,
1551 (gpointer *) ((const char *[]) { path0, path1, path2, path3 }),
1553 _test_connection_invalid_find_connections,
1556 g_assert_cmpint (n_found, ==, 4);
1561 nmtst_assert_connection_verifies_without_normalization (connections->pdata[idx[0]]);
1562 nmtst_assert_connection_verifies_without_normalization (connections->pdata[idx[1]]);
1563 nmtst_assert_connection_verifies_after_normalization (connections->pdata[idx[2]], 0, 0);
1564 nmtst_assert_connection_verifies_without_normalization (connections->pdata[idx[3]]);
1565 g_assert_cmpstr ("test-connection-invalid-1x", ==, nm_connection_get_id (connections->pdata[idx[1]]));
1566 g_assert_cmpstr ("test-connection-invalid-2z", ==, nm_connection_get_id (connections->pdata[idx[3]]));
1571 /*******************************************************************/
1576 main (int argc, char **argv)
1578 g_setenv ("LIBNM_USE_SESSION_BUS", "1", TRUE);
1580 nmtst_init (&argc, &argv, TRUE);
1582 loop = g_main_loop_new (NULL, FALSE);
1584 g_test_add_func ("/libnm/device-added", test_device_added);
1585 g_test_add_func ("/libnm/device-added-signal-after-init", test_device_added_signal_after_init);
1586 g_test_add_func ("/libnm/wifi-ap-added-removed", test_wifi_ap_added_removed);
1587 g_test_add_func ("/libnm/wimax-nsp-added-removed", test_wimax_nsp_added_removed);
1588 g_test_add_func ("/libnm/devices-array", test_devices_array);
1589 g_test_add_func ("/libnm/client-nm-running", test_client_nm_running);
1590 g_test_add_func ("/libnm/active-connections", test_active_connections);
1591 g_test_add_func ("/libnm/activate-virtual", test_activate_virtual);
1592 g_test_add_func ("/libnm/activate-failed", test_activate_failed);
1593 g_test_add_func ("/libnm/device-connection-compatibility", test_device_connection_compatibility);
1594 g_test_add_func ("/libnm/connection/invalid", test_connection_invalid);
1596 return g_test_run ();