107cc58871c6f5f1a31de09209461285e666f2f5
[NetworkManager.git] / libnm / tests / test-nm-client.c
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /*
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)
6  * any later version.
7  *
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.
12  *
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.
16  *
17  * Copyright 2010 - 2014 Red Hat, Inc.
18  *
19  */
20
21 #include "nm-default.h"
22
23 #include <string.h>
24 #include <sys/types.h>
25 #include <signal.h>
26
27 #include "nm-test-libnm-utils.h"
28
29 static GMainLoop *loop = NULL;
30 static NMTstcServiceInfo *sinfo;
31
32 /*******************************************************************/
33
34 static gboolean
35 loop_quit (gpointer user_data)
36 {
37         g_main_loop_quit ((GMainLoop *) user_data);
38         return G_SOURCE_REMOVE;
39 }
40
41 /*******************************************************************/
42
43 static void
44 devices_notify_cb (NMClient *c,
45                    GParamSpec *pspec,
46                    gpointer user_data)
47 {
48         gboolean *notified = user_data;
49         const GPtrArray *devices;
50         NMDevice *device;
51
52         devices = nm_client_get_devices (c);
53         g_assert (devices);
54         g_assert_cmpint (devices->len, ==, 1);
55
56         device = g_ptr_array_index (devices, 0);
57         g_assert (device);
58         g_assert_cmpstr (nm_device_get_iface (device), ==, "eth0");
59
60         *notified = TRUE;
61 }
62
63 static void
64 test_device_added (void)
65 {
66         NMClient *client;
67         const GPtrArray *devices;
68         NMDevice *device;
69         gboolean notified = FALSE;
70         GError *error = NULL;
71
72         sinfo = nmtstc_service_init ();
73         client = nm_client_new (NULL, &error);
74         g_assert_no_error (error);
75
76         devices = nm_client_get_devices (client);
77         g_assert (devices->len == 0);
78
79         g_signal_connect (client,
80                           "notify::devices",
81                           (GCallback) devices_notify_cb,
82                           &notified);
83
84         /* Tell the test service to add a new device */
85         nmtstc_service_add_device (sinfo, client, "AddWiredDevice", "eth0");
86
87         /* coverity[loop_condition] */
88         while (!notified)
89                 g_main_context_iteration (NULL, TRUE);
90
91         g_signal_handlers_disconnect_by_func (client, devices_notify_cb, &notified);
92
93         devices = nm_client_get_devices (client);
94         g_assert (devices);
95         g_assert_cmpint (devices->len, ==, 1);
96
97         device = g_ptr_array_index (devices, 0);
98         g_assert (device);
99         g_assert_cmpstr (nm_device_get_iface (device), ==, "eth0");
100
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);
105
106         g_object_unref (client);
107         g_clear_pointer (&sinfo, nmtstc_service_cleanup);
108 }
109
110 /*******************************************************************/
111
112 typedef enum {
113         SIGNAL_FIRST  = 0x01,
114         SIGNAL_SECOND = 0x02,
115         SIGNAL_MASK   = 0x0F,
116         NOTIFY_FIRST  = 0x10,
117         NOTIFY_SECOND = 0x20,
118         NOTIFY_MASK   = 0xF0
119 } DeviceSignaledAfterInitType;
120
121 static void
122 device_sai_added_cb (NMClient *c,
123                      NMDevice *device,
124                      gpointer user_data)
125 {
126         guint *result = user_data;
127
128         g_assert (device);
129         g_assert_cmpstr (nm_device_get_iface (device), ==, "eth0");
130
131         g_assert ((*result & SIGNAL_MASK) == 0);
132         *result |= *result ? SIGNAL_SECOND : SIGNAL_FIRST;
133 }
134
135 static void
136 devices_sai_notify_cb (NMClient *c,
137                        GParamSpec *pspec,
138                        gpointer user_data)
139 {
140         guint *result = user_data;
141         const GPtrArray *devices;
142         NMDevice *device;
143
144         devices = nm_client_get_devices (c);
145         g_assert (devices);
146         g_assert_cmpint (devices->len, ==, 1);
147
148         device = g_ptr_array_index (devices, 0);
149         g_assert (device);
150         g_assert_cmpstr (nm_device_get_iface (device), ==, "eth0");
151
152         g_assert ((*result & NOTIFY_MASK) == 0);
153         *result |= *result ? NOTIFY_SECOND : NOTIFY_FIRST;
154 }
155
156 static void
157 test_device_added_signal_after_init (void)
158 {
159         NMClient *client;
160         const GPtrArray *devices;
161         NMDevice *device;
162         guint result = 0;
163         GError *error = NULL;
164
165         sinfo = nmtstc_service_init ();
166         client = nm_client_new (NULL, &error);
167         g_assert_no_error (error);
168
169         devices = nm_client_get_devices (client);
170         g_assert (devices->len == 0);
171
172         g_signal_connect (client,
173                           NM_CLIENT_DEVICE_ADDED,
174                           (GCallback) device_sai_added_cb,
175                           &result);
176
177         g_signal_connect (client,
178                           "notify::" NM_CLIENT_DEVICES,
179                           (GCallback) devices_sai_notify_cb,
180                           &result);
181
182         /* Tell the test service to add a new device */
183         nmtstc_service_add_device (sinfo, client, "AddWiredDevice", "eth0");
184
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);
190
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);
193
194         g_assert ((result & SIGNAL_MASK) == SIGNAL_FIRST);
195         g_assert ((result & NOTIFY_MASK) == NOTIFY_SECOND);
196
197         devices = nm_client_get_devices (client);
198         g_assert (devices);
199         g_assert_cmpint (devices->len, ==, 1);
200
201         device = g_ptr_array_index (devices, 0);
202         g_assert (device);
203         g_assert_cmpstr (nm_device_get_iface (device), ==, "eth0");
204
205         g_object_unref (client);
206         g_clear_pointer (&sinfo, nmtstc_service_cleanup);
207 }
208
209 /*******************************************************************/
210
211 static const char *expected_bssid = "66:55:44:33:22:11";
212
213 typedef struct {
214         GMainLoop *loop;
215         gboolean found;
216         char *ap_path;
217         gboolean signaled;
218         gboolean notified;
219         guint quit_id;
220         guint quit_count;
221 } WifiApInfo;
222
223 static void
224 wifi_check_quit (WifiApInfo *info)
225 {
226         info->quit_count--;
227         if (info->quit_count == 0) {
228                 g_source_remove (info->quit_id);
229                 info->quit_id = 0;
230                 g_main_loop_quit (info->loop);
231         }
232 }
233
234 static void
235 got_ap_path (WifiApInfo *info, const char *path)
236 {
237         if (info->ap_path)
238                 g_assert_cmpstr (info->ap_path, ==, path);
239         else
240                 info->ap_path = g_strdup (path);
241 }
242
243 static void
244 wifi_ap_added_cb (NMDeviceWifi *w,
245                   NMAccessPoint *ap,
246                   WifiApInfo *info)
247 {
248         g_assert (ap);
249         g_assert_cmpstr (nm_access_point_get_bssid (ap), ==, expected_bssid);
250         got_ap_path (info, nm_object_get_path (NM_OBJECT (ap)));
251
252         info->signaled = TRUE;
253         wifi_check_quit (info);
254 }
255
256 static void
257 wifi_ap_add_notify_cb (NMDeviceWifi *w,
258                        GParamSpec *pspec,
259                        WifiApInfo *info)
260 {
261         const GPtrArray *aps;
262         NMAccessPoint *ap;
263
264         aps = nm_device_wifi_get_access_points (w);
265         g_assert (aps);
266         g_assert_cmpint (aps->len, ==, 1);
267
268         ap = g_ptr_array_index (aps, 0);
269         g_assert (ap);
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)));
272
273         info->notified = TRUE;
274         wifi_check_quit (info);
275 }
276
277 static void
278 wifi_ap_removed_cb (NMDeviceWifi *w,
279                     NMAccessPoint *ap,
280                     WifiApInfo *info)
281 {
282         g_assert (ap);
283         g_assert_cmpstr (info->ap_path, ==, nm_object_get_path (NM_OBJECT (ap)));
284
285         info->signaled = TRUE;
286         wifi_check_quit (info);
287 }
288
289 static void
290 wifi_ap_remove_notify_cb (NMDeviceWifi *w,
291                           GParamSpec *pspec,
292                           WifiApInfo *info)
293 {
294         const GPtrArray *aps;
295
296         aps = nm_device_wifi_get_access_points (w);
297         g_assert (aps->len == 0);
298
299         info->notified = TRUE;
300         wifi_check_quit (info);
301 }
302
303 static void
304 test_wifi_ap_added_removed (void)
305 {
306         NMClient *client;
307         NMDeviceWifi *wifi;
308         WifiApInfo info = { loop, FALSE, FALSE, 0, 0 };
309         GVariant *ret;
310         GError *error = NULL;
311         char *expected_path = NULL;
312
313         sinfo = nmtstc_service_init ();
314         client = nm_client_new (NULL, &error);
315         g_assert_no_error (error);
316
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));
321
322         /*************************************/
323         /* Add the wifi AP */
324         info.signaled =  FALSE;
325         info.notified = FALSE;
326         info.quit_id = 0;
327
328         ret = g_dbus_proxy_call_sync (sinfo->proxy,
329                                       "AddWifiAp",
330                                       g_variant_new ("(sss)", "wlan0", "test-ap", expected_bssid),
331                                       G_DBUS_CALL_FLAGS_NO_AUTO_START,
332                                       3000,
333                                       NULL,
334                                       &error);
335         g_assert_no_error (error);
336         g_assert (ret);
337         g_assert_cmpstr (g_variant_get_type_string (ret), ==, "(o)");
338         g_variant_get (ret, "(o)", &expected_path);
339         g_variant_unref (ret);
340
341         g_signal_connect (wifi,
342                           "access-point-added",
343                           (GCallback) wifi_ap_added_cb,
344                           &info);
345         info.quit_count = 1;
346
347         g_signal_connect (wifi,
348                           "notify::access-points",
349                           (GCallback) wifi_ap_add_notify_cb,
350                           &info);
351         info.quit_count++;
352
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);
356
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);
363
364         /*************************************/
365         /* Remove the wifi device */
366         info.signaled =  FALSE;
367         info.notified = FALSE;
368         info.quit_id = 0;
369
370         ret = g_dbus_proxy_call_sync (sinfo->proxy,
371                                       "RemoveWifiAp",
372                                       g_variant_new ("(so)", "wlan0", expected_path),
373                                       G_DBUS_CALL_FLAGS_NO_AUTO_START,
374                                       3000,
375                                       NULL,
376                                       &error);
377         g_assert_no_error (error);
378         g_clear_pointer (&ret, g_variant_unref);
379
380         g_signal_connect (wifi,
381                           "access-point-removed",
382                           (GCallback) wifi_ap_removed_cb,
383                           &info);
384         info.quit_count = 1;
385
386         g_signal_connect (wifi,
387                           "notify::access-points",
388                           (GCallback) wifi_ap_remove_notify_cb,
389                           &info);
390         info.quit_count++;
391
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);
395
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);
400
401         g_free (info.ap_path);
402         g_free (expected_path);
403
404         g_object_unref (client);
405         g_clear_pointer (&sinfo, nmtstc_service_cleanup);
406 }
407
408 /*******************************************************************/
409
410 static const char *expected_nsp_name = "Clear";
411
412 typedef struct {
413         GMainLoop *loop;
414         gboolean found;
415         char *nsp_path;
416         gboolean signaled;
417         gboolean notified;
418         guint quit_id;
419         guint quit_count;
420 } WimaxNspInfo;
421
422 static void
423 wimax_check_quit (WimaxNspInfo *info)
424 {
425         info->quit_count--;
426         if (info->quit_count == 0) {
427                 g_source_remove (info->quit_id);
428                 info->quit_id = 0;
429                 g_main_loop_quit (info->loop);
430         }
431 }
432
433 static void
434 got_nsp_path (WimaxNspInfo *info, const char *path)
435 {
436         if (info->nsp_path)
437                 g_assert_cmpstr (info->nsp_path, ==, path);
438         else
439                 info->nsp_path = g_strdup (path);
440 }
441
442 static void
443 wimax_nsp_added_cb (NMDeviceWimax *w,
444                     NMWimaxNsp *nsp,
445                     WimaxNspInfo *info)
446 {
447         g_assert (nsp);
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)));
450
451         info->signaled = TRUE;
452         wimax_check_quit (info);
453 }
454
455 static void
456 wimax_nsp_add_notify_cb (NMDeviceWimax *w,
457                          GParamSpec *pspec,
458                          WimaxNspInfo *info)
459 {
460         const GPtrArray *nsps;
461         NMWimaxNsp *nsp;
462
463         nsps = nm_device_wimax_get_nsps (w);
464         g_assert (nsps);
465         g_assert_cmpint (nsps->len, ==, 1);
466
467         nsp = g_ptr_array_index (nsps, 0);
468         g_assert (nsp);
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)));
471
472         info->notified = TRUE;
473         wimax_check_quit (info);
474 }
475
476 static void
477 wimax_nsp_removed_cb (NMDeviceWimax *w,
478                       NMWimaxNsp *nsp,
479                       WimaxNspInfo *info)
480 {
481         g_assert (nsp);
482         g_assert_cmpstr (info->nsp_path, ==, nm_object_get_path (NM_OBJECT (nsp)));
483
484         info->signaled = TRUE;
485         wimax_check_quit (info);
486 }
487
488 static void
489 wimax_nsp_remove_notify_cb (NMDeviceWimax *w,
490                             GParamSpec *pspec,
491                             WimaxNspInfo *info)
492 {
493         const GPtrArray *nsps;
494
495         nsps = nm_device_wimax_get_nsps (w);
496         g_assert (nsps->len == 0);
497
498         info->notified = TRUE;
499         wimax_check_quit (info);
500 }
501
502 static void
503 test_wimax_nsp_added_removed (void)
504 {
505         NMClient *client;
506         NMDeviceWimax *wimax;
507         WimaxNspInfo info = { loop, FALSE, FALSE, 0, 0 };
508         GVariant *ret;
509         GError *error = NULL;
510         char *expected_path = NULL;
511
512         sinfo = nmtstc_service_init ();
513         client = nm_client_new (NULL, &error);
514         g_assert_no_error (error);
515
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));
520
521         /*************************************/
522         /* Add the wimax NSP */
523         info.signaled =  FALSE;
524         info.notified = FALSE;
525         info.quit_id = 0;
526
527         ret = g_dbus_proxy_call_sync (sinfo->proxy,
528                                       "AddWimaxNsp",
529                                       g_variant_new ("(ss)", "wmx0", expected_nsp_name),
530                                       G_DBUS_CALL_FLAGS_NO_AUTO_START,
531                                       3000,
532                                       NULL,
533                                       &error);
534         g_assert_no_error (error);
535         g_assert (ret);
536         g_assert_cmpstr (g_variant_get_type_string (ret), ==, "(o)");
537         g_variant_get (ret, "(o)", &expected_path);
538         g_variant_unref (ret);
539
540         g_signal_connect (wimax,
541                           "nsp-added",
542                           (GCallback) wimax_nsp_added_cb,
543                           &info);
544         info.quit_count = 1;
545
546         g_signal_connect (wimax,
547                           "notify::nsps",
548                           (GCallback) wimax_nsp_add_notify_cb,
549                           &info);
550         info.quit_count++;
551
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);
555
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);
562
563         /*************************************/
564         /* Remove the wimax NSP */
565         info.signaled =  FALSE;
566         info.notified = FALSE;
567         info.quit_id = 0;
568
569         ret = g_dbus_proxy_call_sync (sinfo->proxy,
570                                       "RemoveWimaxNsp",
571                                       g_variant_new ("(so)", "wmx0", expected_path),
572                                       G_DBUS_CALL_FLAGS_NO_AUTO_START,
573                                       3000,
574                                       NULL,
575                                       &error);
576         g_assert_no_error (error);
577         g_clear_pointer (&ret, g_variant_unref);
578
579         g_signal_connect (wimax,
580                           "nsp-removed",
581                           (GCallback) wimax_nsp_removed_cb,
582                           &info);
583         info.quit_count = 1;
584
585         g_signal_connect (wimax,
586                           "notify::nsps",
587                           (GCallback) wimax_nsp_remove_notify_cb,
588                           &info);
589         info.quit_count++;
590
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);
594
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);
599
600         g_free (info.nsp_path);
601         g_free (expected_path);
602
603         g_object_unref (client);
604         g_clear_pointer (&sinfo, nmtstc_service_cleanup);
605 }
606
607 /*******************************************************************/
608
609 typedef struct {
610         GMainLoop *loop;
611         gboolean signaled;
612         gboolean notified;
613         guint quit_count;
614         guint quit_id;
615 } DaInfo;
616
617 static void
618 da_check_quit (DaInfo *info)
619 {
620         info->quit_count--;
621         if (info->quit_count == 0) {
622                 g_source_remove (info->quit_id);
623                 info->quit_id = 0;
624                 g_main_loop_quit (info->loop);
625         }
626 }
627
628 static void
629 da_device_removed_cb (NMClient *c,
630                       NMDevice *device,
631                       DaInfo *info)
632 {
633         g_assert_cmpstr (nm_device_get_iface (device), ==, "eth0");
634         info->signaled = TRUE;
635         da_check_quit (info);
636 }
637
638 static void
639 da_devices_notify_cb (NMClient *c,
640                       GParamSpec *pspec,
641                       DaInfo *info)
642 {
643         const GPtrArray *devices;
644         NMDevice *device;
645         guint i;
646         const char *iface;
647
648         devices = nm_client_get_devices (c);
649         g_assert (devices);
650         g_assert_cmpint (devices->len, ==, 2);
651
652         for (i = 0; i < devices->len; i++) {
653                 device = g_ptr_array_index (devices, i);
654                 iface = nm_device_get_iface (device);
655
656                 g_assert (!strcmp (iface, "wlan0") || !strcmp (iface, "eth1"));
657         }
658
659         info->notified = TRUE;
660         da_check_quit (info);
661 }
662
663 static void
664 new_client_cb (GObject *object,
665                GAsyncResult *result,
666                gpointer user_data)
667 {
668         NMClient **out_client = user_data;
669         GError *error = NULL;
670
671         *out_client = nm_client_new_finish (result, &error);
672         g_assert_no_error (error);
673         g_assert (*out_client != NULL);
674
675         g_main_loop_quit (loop);
676 }
677
678 static void
679 test_devices_array (void)
680 {
681         NMClient *client = NULL;
682         DaInfo info = { loop };
683         NMDevice *wlan0, *eth0, *eth1, *device;
684         const GPtrArray *devices;
685         GError *error = NULL;
686         GVariant *ret;
687
688         sinfo = nmtstc_service_init ();
689
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);
694
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");
700
701         /* Ensure the devices now exist */
702         devices = nm_client_get_devices (client);
703         g_assert (devices);
704         g_assert_cmpint (devices->len, ==, 3);
705
706         device = nm_client_get_device_by_iface (client, "wlan0");
707         g_assert (NM_IS_DEVICE_WIFI (device));
708         g_assert (device == wlan0);
709
710         device = nm_client_get_device_by_iface (client, "eth0");
711         g_assert (NM_IS_DEVICE_ETHERNET (device));
712         g_assert (device == eth0);
713
714         device = nm_client_get_device_by_iface (client, "eth1");
715         g_assert (NM_IS_DEVICE_ETHERNET (device));
716         g_assert (device == eth1);
717
718         /********************************/
719         /* Now remove the device in the middle */
720         ret = g_dbus_proxy_call_sync (sinfo->proxy,
721                                       "RemoveDevice",
722                                       g_variant_new ("(o)", nm_object_get_path (NM_OBJECT (eth0))),
723                                       G_DBUS_CALL_FLAGS_NO_AUTO_START,
724                                       3000,
725                                       NULL,
726                                       &error);
727         g_assert_no_error (error);
728         g_assert (ret);
729         g_variant_unref (ret);
730
731         g_signal_connect (client,
732                           "device-removed",
733                           (GCallback) da_device_removed_cb,
734                           &info);
735
736         g_signal_connect (client,
737                           "notify::devices",
738                           (GCallback) da_devices_notify_cb,
739                           &info);
740         info.quit_count = 2;
741
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);
745
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);
749
750         /* Ensure only two are left */
751         devices = nm_client_get_devices (client);
752         g_assert (devices);
753         g_assert_cmpint (devices->len, ==, 2);
754
755         device = nm_client_get_device_by_iface (client, "wlan0");
756         g_assert (NM_IS_DEVICE_WIFI (device));
757         g_assert (device == wlan0);
758
759         device = nm_client_get_device_by_iface (client, "eth1");
760         g_assert (NM_IS_DEVICE_ETHERNET (device));
761         g_assert (device == eth1);
762
763         g_object_unref (client);
764         g_clear_pointer (&sinfo, nmtstc_service_cleanup);
765 }
766
767 static void
768 nm_running_changed (GObject *client,
769                     GParamSpec *pspec,
770                     gpointer user_data)
771 {
772         int *running_changed = user_data;
773
774         (*running_changed)++;
775         g_main_loop_quit (loop);
776 }
777
778 static void
779 test_client_nm_running (void)
780 {
781         NMClient *client1, *client2;
782         guint quit_id;
783         int running_changed = 0;
784         GError *error = NULL;
785
786         client1 = nm_client_new (NULL, &error);
787         g_assert_no_error (error);
788
789         g_assert (!nm_client_get_nm_running (client1));
790         g_assert_cmpstr (nm_client_get_version (client1), ==, NULL);
791
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));
796
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);
801
802         /* Now start the test service. */
803         sinfo = nmtstc_service_init ();
804         client2 = nm_client_new (NULL, &error);
805         g_assert_no_error (error);
806
807         /* client2 should know that NM is running, but the previously-created
808          * client1 hasn't gotten the news yet.
809          */
810         g_assert (!nm_client_get_nm_running (client1));
811         g_assert (nm_client_get_nm_running (client2));
812
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);
820
821         /* And kill it */
822         g_clear_pointer (&sinfo, nmtstc_service_cleanup);
823
824         g_assert (nm_client_get_nm_running (client1));
825
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);
831
832         g_object_unref (client1);
833         g_object_unref (client2);
834 }
835
836 typedef struct {
837         GMainLoop *loop;
838         NMActiveConnection *ac;
839
840         int remaining;
841 } TestACInfo;
842
843 static void
844 assert_ac_and_device (NMClient *client)
845 {
846         const GPtrArray *devices, *acs, *ac_devices;
847         NMDevice *device, *ac_device;
848         NMActiveConnection *ac, *device_ac;
849
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);
856
857         ac = acs->pdata[0];
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);
863
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);
869
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);
874 }
875
876 static void
877 add_and_activate_cb (GObject *object,
878                      GAsyncResult *result,
879                      gpointer user_data)
880 {
881         NMClient *client = NM_CLIENT (object);
882         TestACInfo *info = user_data;
883         GError *error = NULL;
884
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);
888
889         assert_ac_and_device (client);
890
891         info->remaining--;
892         if (!info->remaining)
893                 g_main_loop_quit (info->loop);
894 }
895
896 static void
897 client_acs_changed_cb (GObject *client,
898                        GParamSpec *pspec,
899                        gpointer user_data)
900 {
901         TestACInfo *info = user_data;
902         const GPtrArray *acs;
903
904         acs = nm_client_get_active_connections (NM_CLIENT (client));
905         g_assert (acs != NULL);
906         g_assert_cmpint (acs->len, ==, 1);
907
908         info->remaining--;
909         if (!info->remaining)
910                 g_main_loop_quit (info->loop);
911 }
912
913 static void
914 device_ac_changed_cb (GObject *device,
915                       GParamSpec *pspec,
916                       gpointer user_data)
917 {
918         TestACInfo *info = user_data;
919
920         g_assert (nm_device_get_active_connection (NM_DEVICE (device)) != NULL);
921
922         info->remaining--;
923         if (!info->remaining)
924                 g_main_loop_quit (info->loop);
925 }
926
927 static void
928 test_active_connections (void)
929 {
930         NMClient *client;
931         NMDevice *device;
932         NMConnection *conn;
933         TestACInfo info = { loop, NULL, 0 };
934         GError *error = NULL;
935
936         sinfo = nmtstc_service_init ();
937         client = nm_client_new (NULL, &error);
938         g_assert_no_error (error);
939
940         /* Tell the test service to add a new device */
941         device = nmtstc_service_add_device (sinfo, client, "AddWiredDevice", "eth0");
942
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);
947
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);
952
953         /* Two signals plus activate_cb */
954         info.remaining = 3;
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);
958
959         g_assert (info.ac != NULL);
960
961         g_object_unref (info.ac);
962         g_object_unref (client);
963
964         /* Ensure that we can correctly resolve the recursive property link between the
965          * AC and the Device in a newly-created client.
966          */
967         client = nm_client_new (NULL, &error);
968         g_assert_no_error (error);
969         assert_ac_and_device (client);
970         g_object_unref (client);
971
972         client = NULL;
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);
977
978         g_clear_pointer (&sinfo, nmtstc_service_cleanup);
979 }
980
981 static void
982 client_devices_changed_cb (GObject *client,
983                            GParamSpec *pspec,
984                            gpointer user_data)
985 {
986         TestACInfo *info = user_data;
987         const GPtrArray *devices;
988         NMDevice *device;
989
990         devices = nm_client_get_devices (NM_CLIENT (client));
991         g_assert (devices != NULL);
992         if (devices->len < 2)
993                 return;
994         g_assert_cmpint (devices->len, ==, 2);
995
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];
1000         else
1001                 g_assert_not_reached ();
1002
1003         g_assert_cmpstr (nm_device_get_iface (device), ==, "eth0.1");
1004
1005         if (!nm_device_get_active_connection (device)) {
1006                 info->remaining++;
1007                 g_signal_connect (device, "notify::" NM_DEVICE_ACTIVE_CONNECTION,
1008                                   G_CALLBACK (device_ac_changed_cb), info);
1009         }
1010
1011         info->remaining--;
1012         if (!info->remaining)
1013                 g_main_loop_quit (info->loop);
1014 }
1015
1016 typedef struct {
1017         GMainLoop *loop;
1018         NMRemoteConnection *remote;
1019 } TestConnectionInfo;
1020
1021 static void
1022 add_connection_cb (GObject *object,
1023                    GAsyncResult *result,
1024                    gpointer user_data)
1025 {
1026         TestConnectionInfo *info = user_data;
1027         GError *error = NULL;
1028
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);
1032 }
1033
1034 static void
1035 activate_cb (GObject *object,
1036              GAsyncResult *result,
1037              gpointer user_data)
1038 {
1039         NMClient *client = NM_CLIENT (object);
1040         TestACInfo *info = user_data;
1041         GError *error = NULL;
1042
1043         info->ac = nm_client_activate_connection_finish (client, result, &error);
1044         g_assert_no_error (error);
1045         g_assert (info->ac != NULL);
1046
1047         assert_ac_and_device (client);
1048
1049         info->remaining--;
1050         if (!info->remaining)
1051                 g_main_loop_quit (info->loop);
1052 }
1053
1054 static void
1055 test_activate_virtual (void)
1056 {
1057         NMClient *client;
1058         NMConnection *conn;
1059         NMSettingConnection *s_con;
1060         NMSettingVlan *s_vlan;
1061         TestACInfo info = { loop, NULL, 0 };
1062         TestConnectionInfo conn_info = { loop, NULL };
1063         GError *error = NULL;
1064
1065         sinfo = nmtstc_service_init ();
1066         client = nm_client_new (NULL, &error);
1067         g_assert_no_error (error);
1068
1069         nmtstc_service_add_device (sinfo, client, "AddWiredDevice", "eth0");
1070
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",
1074                       NULL);
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",
1079                       NULL);
1080
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);
1086
1087         nm_client_activate_connection_async (client, conn, NULL, NULL,
1088                                              NULL, activate_cb, &info);
1089         g_object_unref (conn);
1090
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);
1095
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.
1100          */
1101         info.remaining = 3;
1102
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);
1106
1107         g_assert (info.ac != NULL);
1108
1109         g_object_unref (info.ac);
1110         g_object_unref (client);
1111
1112         g_clear_pointer (&sinfo, nmtstc_service_cleanup);
1113 }
1114
1115 static void
1116 activate_failed_cb (GObject *object,
1117                     GAsyncResult *result,
1118                     gpointer user_data)
1119 {
1120         NMClient *client = NM_CLIENT (object);
1121         NMActiveConnection *ac;
1122         GError *error = NULL;
1123
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);
1128
1129         g_main_loop_quit (loop);
1130 }
1131
1132 static void
1133 test_activate_failed (void)
1134 {
1135         NMClient *client;
1136         NMDevice *device;
1137         NMConnection *conn;
1138         GError *error = NULL;
1139
1140         sinfo = nmtstc_service_init ();
1141         client = nm_client_new (NULL, &error);
1142         g_assert_no_error (error);
1143
1144         device = nmtstc_service_add_device (sinfo, client, "AddWiredDevice", "eth0");
1145
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);
1149
1150         nm_client_add_and_activate_connection_async (client, conn, device, NULL,
1151                                                      NULL, activate_failed_cb, NULL);
1152         g_main_loop_run (loop);
1153
1154         g_object_unref (conn);
1155         g_object_unref (client);
1156
1157         g_clear_pointer (&sinfo, nmtstc_service_cleanup);
1158 }
1159
1160 static void
1161 test_device_connection_compatibility (void)
1162 {
1163         NMClient *client;
1164         NMDevice *device1, *device2;
1165         NMConnection *conn;
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";
1173
1174         sinfo = nmtstc_service_init ();
1175         client = nm_client_new (NULL, &error);
1176         g_assert_no_error (error);
1177
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);
1181
1182         g_assert_cmpstr (nm_device_get_hw_address (device1), ==, hw_addr1);
1183         g_assert_cmpstr (nm_device_get_hw_address (device2), ==, hw_addr2);
1184
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");
1189
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,
1194                       NULL);
1195         nm_device_connection_compatible (device1, conn, &error);
1196         g_assert_no_error (error);
1197
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);
1203
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);
1209
1210         g_object_set (s_wired, NM_SETTING_WIRED_S390_SUBCHANNELS, NULL, NULL);
1211
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);
1218
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);
1224
1225         g_object_unref (conn);
1226         g_object_unref (client);
1227
1228         g_clear_pointer (&sinfo, nmtstc_service_cleanup);
1229 }
1230
1231 /*******************************************************************/
1232
1233 static gboolean
1234 _test_connection_invalid_find_connections (gpointer element, gpointer needle, gpointer user_data)
1235 {
1236         NMRemoteConnection *con = NM_REMOTE_CONNECTION (element);
1237         const char *path = needle;
1238
1239         g_assert (NM_IS_REMOTE_CONNECTION (con));
1240         g_assert (path && *path);
1241
1242         return strcmp (path, nm_connection_get_path ((NMConnection *) con)) == 0;
1243 }
1244
1245 #define ASSERT_IDX(i) \
1246         g_assert_cmpint (idx[i], >=, 0); \
1247         g_assert (path##i && *path##i); \
1248         g_assert (NM_IS_REMOTE_CONNECTION (connections->pdata[idx[i]])); \
1249         g_assert_cmpstr (nm_connection_get_path (connections->pdata[idx[i]]), ==, path##i);
1250
1251 static void
1252 test_connection_invalid (void)
1253 {
1254         NMTSTC_SERVICE_INFO_SETUP (my_sinfo)
1255         gs_unref_object NMConnection *connection = NULL;
1256         NMSettingConnection *s_con;
1257         gs_unref_object NMClient *client = NULL;
1258         const GPtrArray *connections;
1259         gs_free_error GError *error = NULL;
1260         gs_free char *path0 = NULL;
1261         gs_free char *path1 = NULL;
1262         gs_free char *path2 = NULL;
1263         gs_free char *uuid2 = NULL;
1264         gsize n_found;
1265         gssize idx[3];
1266
1267         /**************************************************************************
1268          * Add two connection before starting libnm. One valid, one invalid.
1269          *************************************************************************/
1270
1271         connection = nmtst_create_minimal_connection ("test-connection-invalid-0", NULL, NM_SETTING_WIRED_SETTING_NAME, &s_con);
1272         nmtst_connection_normalize (connection);
1273         g_object_set (s_con,
1274                       NM_SETTING_CONNECTION_UUID, nmtst_uuid_generate (),
1275                       NULL);
1276         nmtstc_service_add_connection (my_sinfo,
1277                                        connection,
1278                                        TRUE,
1279                                        &path0);
1280
1281         nm_connection_remove_setting (connection, NM_TYPE_SETTING_WIRED);
1282         g_object_set (s_con,
1283                       NM_SETTING_CONNECTION_ID, "test-connection-invalid-1",
1284                       NM_SETTING_CONNECTION_TYPE, "invalid-type-1",
1285                       NM_SETTING_CONNECTION_UUID, nmtst_uuid_generate (),
1286                       NULL);
1287         nmtstc_service_add_connection (my_sinfo,
1288                                        connection,
1289                                        FALSE,
1290                                        &path1);
1291
1292
1293         client = nm_client_new (NULL, &error);
1294         g_assert_no_error (error);
1295
1296         connections = nm_client_get_connections (client);
1297         g_assert (connections);
1298
1299         g_assert_cmpint (connections->len, ==, 2);
1300         n_found = nmtst_find_all_indexes (connections->pdata,
1301                                           connections->len,
1302                                           (gpointer *) ((const char *[]) { path0, path1 }),
1303                                           2,
1304                                           _test_connection_invalid_find_connections,
1305                                           NULL,
1306                                           idx);
1307         g_assert_cmpint (n_found, ==, 2);
1308         ASSERT_IDX (0);
1309         ASSERT_IDX (1);
1310         nmtst_assert_connection_verifies_without_normalization (connections->pdata[idx[0]]);
1311         nmtst_assert_connection_unnormalizable (connections->pdata[idx[1]], 0, 0);
1312
1313         /**************************************************************************
1314          * After having the client up and running, add another invalid connection
1315          *************************************************************************/
1316
1317         g_object_set (s_con,
1318                       NM_SETTING_CONNECTION_ID, "test-connection-invalid-2",
1319                       NM_SETTING_CONNECTION_TYPE, "invalid-type-2",
1320                       NM_SETTING_CONNECTION_UUID, (uuid2 = g_strdup (nmtst_uuid_generate ())),
1321                       NULL);
1322         nmtstc_service_add_connection (my_sinfo,
1323                                        connection,
1324                                        FALSE,
1325                                        &path2);
1326
1327
1328         nmtst_main_loop_run (loop, 100);
1329
1330
1331         connections = nm_client_get_connections (client);
1332         g_assert (connections);
1333
1334         g_assert_cmpint (connections->len, ==, 3);
1335         n_found = nmtst_find_all_indexes (connections->pdata,
1336                                           connections->len,
1337                                           (gpointer *) ((const char *[]) { path0, path1, path2 }),
1338                                           3,
1339                                           _test_connection_invalid_find_connections,
1340                                           NULL,
1341                                           idx);
1342         g_assert_cmpint (n_found, ==, 3);
1343         ASSERT_IDX (0);
1344         ASSERT_IDX (1);
1345         ASSERT_IDX (2);
1346         nmtst_assert_connection_verifies_without_normalization (connections->pdata[idx[0]]);
1347         nmtst_assert_connection_unnormalizable (connections->pdata[idx[1]], 0, 0);
1348         nmtst_assert_connection_unnormalizable (connections->pdata[idx[2]], 0, 0);
1349
1350         /**************************************************************************
1351          * Modify the invalid connection (still invalid)
1352          *************************************************************************/
1353
1354         g_object_set (s_con,
1355                       NM_SETTING_CONNECTION_ID, "test-connection-invalid-2x",
1356                       NULL);
1357         nmtstc_service_update_connection (my_sinfo,
1358                                           path2,
1359                                           connection,
1360                                           FALSE);
1361
1362         nmtst_main_loop_run (loop, 100);
1363
1364         connections = nm_client_get_connections (client);
1365         g_assert (connections);
1366
1367         g_assert_cmpint (connections->len, ==, 3);
1368         n_found = nmtst_find_all_indexes (connections->pdata,
1369                                           connections->len,
1370                                           (gpointer *) ((const char *[]) { path0, path1, path2 }),
1371                                           3,
1372                                           _test_connection_invalid_find_connections,
1373                                           NULL,
1374                                           idx);
1375         g_assert_cmpint (n_found, ==, 3);
1376         ASSERT_IDX (0);
1377         ASSERT_IDX (1);
1378         ASSERT_IDX (2);
1379         nmtst_assert_connection_verifies_without_normalization (connections->pdata[idx[0]]);
1380         nmtst_assert_connection_unnormalizable (connections->pdata[idx[1]], 0, 0);
1381         nmtst_assert_connection_unnormalizable (connections->pdata[idx[2]], 0, 0);
1382         g_assert_cmpstr ("test-connection-invalid-2x", ==, nm_connection_get_id (connections->pdata[idx[2]]));
1383
1384         /**************************************************************************
1385          * Modify the invalid connection (now becomes valid)
1386          *************************************************************************/
1387
1388         g_clear_object (&connection);
1389         connection = nmtst_create_minimal_connection ("test-connection-invalid-2", NULL, NM_SETTING_WIRED_SETTING_NAME, &s_con);
1390         nmtst_connection_normalize (connection);
1391         g_object_set (s_con,
1392                       NM_SETTING_CONNECTION_ID, "test-connection-invalid-2z",
1393                       NM_SETTING_CONNECTION_TYPE, "802-3-ethernet",
1394                       NM_SETTING_CONNECTION_UUID, uuid2,
1395                       NULL);
1396
1397         nmtstc_service_update_connection (my_sinfo,
1398                                           path2,
1399                                           connection,
1400                                           FALSE);
1401
1402         nmtst_main_loop_run (loop, 100);
1403
1404         connections = nm_client_get_connections (client);
1405         g_assert (connections);
1406
1407         g_assert_cmpint (connections->len, ==, 3);
1408         n_found = nmtst_find_all_indexes (connections->pdata,
1409                                           connections->len,
1410                                           (gpointer *) ((const char *[]) { path0, path1, path2 }),
1411                                           3,
1412                                           _test_connection_invalid_find_connections,
1413                                           NULL,
1414                                           idx);
1415         g_assert_cmpint (n_found, ==, 3);
1416         ASSERT_IDX (0);
1417         ASSERT_IDX (1);
1418         ASSERT_IDX (2);
1419         nmtst_assert_connection_verifies_without_normalization (connections->pdata[idx[0]]);
1420         nmtst_assert_connection_unnormalizable (connections->pdata[idx[1]], 0, 0);
1421         nmtst_assert_connection_verifies_without_normalization (connections->pdata[idx[2]]);
1422         g_assert_cmpstr ("test-connection-invalid-2z", ==, nm_connection_get_id (connections->pdata[idx[2]]));
1423
1424         /**************************************************************************
1425          * Modify the invalid connection and make it valid
1426          *************************************************************************/
1427
1428         g_clear_object (&connection);
1429         connection = nmtst_create_minimal_connection ("test-connection-invalid-1", NULL, NM_SETTING_WIRED_SETTING_NAME, &s_con);
1430         nmtst_connection_normalize (connection);
1431         g_object_set (s_con,
1432                       NM_SETTING_CONNECTION_ID, "test-connection-invalid-1x",
1433                       NM_SETTING_CONNECTION_TYPE, "802-3-ethernet",
1434                       NM_SETTING_CONNECTION_UUID, nm_connection_get_uuid (connections->pdata[idx[1]]),
1435                       NULL);
1436
1437         nmtstc_service_update_connection (my_sinfo,
1438                                           path1,
1439                                           connection,
1440                                           FALSE);
1441
1442         nmtst_main_loop_run (loop, 100);
1443
1444         connections = nm_client_get_connections (client);
1445         g_assert (connections);
1446
1447         g_assert_cmpint (connections->len, ==, 3);
1448         n_found = nmtst_find_all_indexes (connections->pdata,
1449                                           connections->len,
1450                                           (gpointer *) ((const char *[]) { path0, path1, path2 }),
1451                                           3,
1452                                           _test_connection_invalid_find_connections,
1453                                           NULL,
1454                                           idx);
1455         g_assert_cmpint (n_found, ==, 3);
1456         ASSERT_IDX (0);
1457         ASSERT_IDX (1);
1458         ASSERT_IDX (2);
1459         nmtst_assert_connection_verifies_without_normalization (connections->pdata[idx[0]]);
1460         nmtst_assert_connection_verifies_without_normalization (connections->pdata[idx[1]]);
1461         nmtst_assert_connection_verifies_without_normalization (connections->pdata[idx[2]]);
1462         g_assert_cmpstr ("test-connection-invalid-1x", ==, nm_connection_get_id (connections->pdata[idx[1]]));
1463         g_assert_cmpstr ("test-connection-invalid-2z", ==, nm_connection_get_id (connections->pdata[idx[2]]));
1464
1465 #undef ASSERT_IDX
1466 }
1467
1468 /*******************************************************************/
1469
1470 NMTST_DEFINE ();
1471
1472 int
1473 main (int argc, char **argv)
1474 {
1475         g_setenv ("LIBNM_USE_SESSION_BUS", "1", TRUE);
1476
1477         nmtst_init (&argc, &argv, TRUE);
1478
1479         loop = g_main_loop_new (NULL, FALSE);
1480
1481         g_test_add_func ("/libnm/device-added", test_device_added);
1482         g_test_add_func ("/libnm/device-added-signal-after-init", test_device_added_signal_after_init);
1483         g_test_add_func ("/libnm/wifi-ap-added-removed", test_wifi_ap_added_removed);
1484         g_test_add_func ("/libnm/wimax-nsp-added-removed", test_wimax_nsp_added_removed);
1485         g_test_add_func ("/libnm/devices-array", test_devices_array);
1486         g_test_add_func ("/libnm/client-nm-running", test_client_nm_running);
1487         g_test_add_func ("/libnm/active-connections", test_active_connections);
1488         g_test_add_func ("/libnm/activate-virtual", test_activate_virtual);
1489         g_test_add_func ("/libnm/activate-failed", test_activate_failed);
1490         g_test_add_func ("/libnm/device-connection-compatibility", test_device_connection_compatibility);
1491         g_test_add_func ("/libnm/connection/invalid", test_connection_invalid);
1492
1493         return g_test_run ();
1494 }
1495