libnm/tests: extend tests for handling invalid connections in NMClient
[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_STMT_START { \
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); \
1251         } G_STMT_END
1252
1253 static void
1254 test_connection_invalid (void)
1255 {
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;
1267         gsize n_found;
1268         gssize idx[4];
1269         gs_unref_variant GVariant *variant = NULL;
1270
1271         /**************************************************************************
1272          * Add three connections before starting libnm. One valid, two invalid.
1273          *************************************************************************/
1274
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 (),
1279                       NULL);
1280         nmtstc_service_add_connection (my_sinfo,
1281                                        connection,
1282                                        TRUE,
1283                                        &path0);
1284
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 (),
1290                       NULL);
1291         nmtstc_service_add_connection (my_sinfo,
1292                                        connection,
1293                                        FALSE,
1294                                        &path1);
1295
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 (),
1300                       NULL);
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,
1308                                                variant,
1309                                                FALSE,
1310                                                &path2);
1311
1312         g_test_expect_message ("libnm", G_LOG_LEVEL_WARNING, "*replace_settings: error updating connection /org/freedesktop/NetworkManager/Settings/Connection/3 settings: *");
1313         client = nm_client_new (NULL, &error);
1314         g_test_assert_expected_messages ();
1315         g_assert_no_error (error);
1316
1317         connections = nm_client_get_connections (client);
1318         g_assert (connections);
1319
1320         g_assert_cmpint (connections->len, ==, 3);
1321         n_found = nmtst_find_all_indexes (connections->pdata,
1322                                           connections->len,
1323                                           (gpointer *) ((const char *[]) { path0, path1, path2 }),
1324                                           3,
1325                                           _test_connection_invalid_find_connections,
1326                                           NULL,
1327                                           idx);
1328         g_assert_cmpint (n_found, ==, 3);
1329         ASSERT_IDX (0);
1330         ASSERT_IDX (1);
1331         ASSERT_IDX (2);
1332         nmtst_assert_connection_verifies_without_normalization (connections->pdata[idx[0]]);
1333         nmtst_assert_connection_unnormalizable (connections->pdata[idx[1]], 0, 0);
1334         nmtst_assert_connection_unnormalizable (connections->pdata[idx[2]], 0, 0);
1335
1336         /**************************************************************************
1337          * After having the client up and running, add another invalid connection
1338          *************************************************************************/
1339
1340         g_object_set (s_con,
1341                       NM_SETTING_CONNECTION_ID, "test-connection-invalid-2",
1342                       NM_SETTING_CONNECTION_TYPE, "invalid-type-2",
1343                       NM_SETTING_CONNECTION_UUID, (uuid2 = g_strdup (nmtst_uuid_generate ())),
1344                       NULL);
1345         nmtstc_service_add_connection (my_sinfo,
1346                                        connection,
1347                                        FALSE,
1348                                        &path3);
1349
1350
1351         nmtst_main_loop_run (loop, 100);
1352
1353
1354         connections = nm_client_get_connections (client);
1355         g_assert (connections);
1356
1357         g_assert_cmpint (connections->len, ==, 4);
1358         n_found = nmtst_find_all_indexes (connections->pdata,
1359                                           connections->len,
1360                                           (gpointer *) ((const char *[]) { path0, path1, path2, path3 }),
1361                                           4,
1362                                           _test_connection_invalid_find_connections,
1363                                           NULL,
1364                                           idx);
1365         g_assert_cmpint (n_found, ==, 4);
1366         ASSERT_IDX (0);
1367         ASSERT_IDX (1);
1368         ASSERT_IDX (2);
1369         ASSERT_IDX (3);
1370         nmtst_assert_connection_verifies_without_normalization (connections->pdata[idx[0]]);
1371         nmtst_assert_connection_unnormalizable (connections->pdata[idx[1]], 0, 0);
1372         nmtst_assert_connection_unnormalizable (connections->pdata[idx[2]], 0, 0);
1373         nmtst_assert_connection_unnormalizable (connections->pdata[idx[3]], 0, 0);
1374
1375         /**************************************************************************
1376          * Modify the invalid connection (still invalid)
1377          *************************************************************************/
1378
1379         NMTST_VARIANT_EDITOR (variant,
1380                               NMTST_VARIANT_CHANGE_PROPERTY ("invalid-type-2",
1381                                                              "some-key2", "u", 4721));
1382         g_variant_ref_sink (variant);
1383         nmtstc_service_update_connection_variant (my_sinfo,
1384                                                   path2,
1385                                                   variant,
1386                                                   FALSE);
1387
1388         g_test_expect_message ("libnm", G_LOG_LEVEL_WARNING, "*replace_settings: error updating connection /org/freedesktop/NetworkManager/Settings/Connection/3 settings: *");
1389         nmtst_main_loop_run (loop, 100);
1390         g_test_assert_expected_messages ();
1391
1392         connections = nm_client_get_connections (client);
1393         g_assert (connections);
1394
1395         g_assert_cmpint (connections->len, ==, 4);
1396         n_found = nmtst_find_all_indexes (connections->pdata,
1397                                           connections->len,
1398                                           (gpointer *) ((const char *[]) { path0, path1, path2, path3 }),
1399                                           4,
1400                                           _test_connection_invalid_find_connections,
1401                                           NULL,
1402                                           idx);
1403         g_assert_cmpint (n_found, ==, 4);
1404         ASSERT_IDX (0);
1405         ASSERT_IDX (1);
1406         ASSERT_IDX (2);
1407         ASSERT_IDX (3);
1408         nmtst_assert_connection_verifies_without_normalization (connections->pdata[idx[0]]);
1409         nmtst_assert_connection_unnormalizable (connections->pdata[idx[1]], 0, 0);
1410         nmtst_assert_connection_unnormalizable (connections->pdata[idx[2]], 0, 0);
1411         nmtst_assert_connection_unnormalizable (connections->pdata[idx[3]], 0, 0);
1412
1413         /**************************************************************************
1414          * Modify the invalid connection (becomes valid)
1415          *************************************************************************/
1416
1417         NMTST_VARIANT_EDITOR (variant,
1418                               NMTST_VARIANT_DROP_SETTING ("invalid-type-2"));
1419         NMTST_VARIANT_EDITOR (variant,
1420                               NMTST_VARIANT_CHANGE_PROPERTY (NM_SETTING_CONNECTION_SETTING_NAME,
1421                                                              NM_SETTING_CONNECTION_TYPE, "s", NM_SETTING_WIRED_SETTING_NAME));
1422         g_variant_ref_sink (variant);
1423         nmtstc_service_update_connection_variant (my_sinfo,
1424                                                   path2,
1425                                                   variant,
1426                                                   FALSE);
1427
1428         nmtst_main_loop_run (loop, 100);
1429
1430         connections = nm_client_get_connections (client);
1431         g_assert (connections);
1432
1433         g_assert_cmpint (connections->len, ==, 4);
1434         n_found = nmtst_find_all_indexes (connections->pdata,
1435                                           connections->len,
1436                                           (gpointer *) ((const char *[]) { path0, path1, path2, path3 }),
1437                                           4,
1438                                           _test_connection_invalid_find_connections,
1439                                           NULL,
1440                                           idx);
1441         g_assert_cmpint (n_found, ==, 4);
1442         ASSERT_IDX (0);
1443         ASSERT_IDX (1);
1444         ASSERT_IDX (2);
1445         ASSERT_IDX (3);
1446         nmtst_assert_connection_verifies_without_normalization (connections->pdata[idx[0]]);
1447         nmtst_assert_connection_unnormalizable (connections->pdata[idx[1]], 0, 0);
1448         nmtst_assert_connection_verifies_after_normalization (connections->pdata[idx[2]], 0, 0);
1449         nmtst_assert_connection_unnormalizable (connections->pdata[idx[3]], 0, 0);
1450
1451         /**************************************************************************
1452          * Modify the invalid connection (still invalid)
1453          *************************************************************************/
1454
1455         g_object_set (s_con,
1456                       NM_SETTING_CONNECTION_ID, "test-connection-invalid-2x",
1457                       NULL);
1458         nmtstc_service_update_connection (my_sinfo,
1459                                           path3,
1460                                           connection,
1461                                           FALSE);
1462
1463         nmtst_main_loop_run (loop, 100);
1464
1465         connections = nm_client_get_connections (client);
1466         g_assert (connections);
1467
1468         g_assert_cmpint (connections->len, ==, 4);
1469         n_found = nmtst_find_all_indexes (connections->pdata,
1470                                           connections->len,
1471                                           (gpointer *) ((const char *[]) { path0, path1, path2, path3 }),
1472                                           4,
1473                                           _test_connection_invalid_find_connections,
1474                                           NULL,
1475                                           idx);
1476         g_assert_cmpint (n_found, ==, 4);
1477         ASSERT_IDX (0);
1478         ASSERT_IDX (1);
1479         ASSERT_IDX (2);
1480         ASSERT_IDX (3);
1481         nmtst_assert_connection_verifies_without_normalization (connections->pdata[idx[0]]);
1482         nmtst_assert_connection_unnormalizable (connections->pdata[idx[1]], 0, 0);
1483         nmtst_assert_connection_verifies_after_normalization (connections->pdata[idx[2]], 0, 0);
1484         nmtst_assert_connection_unnormalizable (connections->pdata[idx[3]], 0, 0);
1485         g_assert_cmpstr ("test-connection-invalid-2x", ==, nm_connection_get_id (connections->pdata[idx[3]]));
1486
1487         /**************************************************************************
1488          * Modify the invalid connection (now becomes valid)
1489          *************************************************************************/
1490
1491         g_clear_object (&connection);
1492         connection = nmtst_create_minimal_connection ("test-connection-invalid-2", NULL, NM_SETTING_WIRED_SETTING_NAME, &s_con);
1493         nmtst_connection_normalize (connection);
1494         g_object_set (s_con,
1495                       NM_SETTING_CONNECTION_ID, "test-connection-invalid-2z",
1496                       NM_SETTING_CONNECTION_TYPE, "802-3-ethernet",
1497                       NM_SETTING_CONNECTION_UUID, uuid2,
1498                       NULL);
1499
1500         nmtstc_service_update_connection (my_sinfo,
1501                                           path3,
1502                                           connection,
1503                                           FALSE);
1504
1505         nmtst_main_loop_run (loop, 100);
1506
1507         connections = nm_client_get_connections (client);
1508         g_assert (connections);
1509
1510         g_assert_cmpint (connections->len, ==, 4);
1511         n_found = nmtst_find_all_indexes (connections->pdata,
1512                                           connections->len,
1513                                           (gpointer *) ((const char *[]) { path0, path1, path2, path3 }),
1514                                           4,
1515                                           _test_connection_invalid_find_connections,
1516                                           NULL,
1517                                           idx);
1518         g_assert_cmpint (n_found, ==, 4);
1519         ASSERT_IDX (0);
1520         ASSERT_IDX (1);
1521         ASSERT_IDX (2);
1522         ASSERT_IDX (3);
1523         nmtst_assert_connection_verifies_without_normalization (connections->pdata[idx[0]]);
1524         nmtst_assert_connection_unnormalizable (connections->pdata[idx[1]], 0, 0);
1525         nmtst_assert_connection_verifies_after_normalization (connections->pdata[idx[2]], 0, 0);
1526         nmtst_assert_connection_verifies_without_normalization (connections->pdata[idx[3]]);
1527         g_assert_cmpstr ("test-connection-invalid-2z", ==, nm_connection_get_id (connections->pdata[idx[3]]));
1528
1529         /**************************************************************************
1530          * Modify the invalid connection and make it valid
1531          *************************************************************************/
1532
1533         g_clear_object (&connection);
1534         connection = nmtst_create_minimal_connection ("test-connection-invalid-1", NULL, NM_SETTING_WIRED_SETTING_NAME, &s_con);
1535         nmtst_connection_normalize (connection);
1536         g_object_set (s_con,
1537                       NM_SETTING_CONNECTION_ID, "test-connection-invalid-1x",
1538                       NM_SETTING_CONNECTION_TYPE, "802-3-ethernet",
1539                       NM_SETTING_CONNECTION_UUID, nm_connection_get_uuid (connections->pdata[idx[1]]),
1540                       NULL);
1541
1542         nmtstc_service_update_connection (my_sinfo,
1543                                           path1,
1544                                           connection,
1545                                           FALSE);
1546
1547         nmtst_main_loop_run (loop, 100);
1548
1549         connections = nm_client_get_connections (client);
1550         g_assert (connections);
1551
1552         g_assert_cmpint (connections->len, ==, 4);
1553         n_found = nmtst_find_all_indexes (connections->pdata,
1554                                           connections->len,
1555                                           (gpointer *) ((const char *[]) { path0, path1, path2, path3 }),
1556                                           4,
1557                                           _test_connection_invalid_find_connections,
1558                                           NULL,
1559                                           idx);
1560         g_assert_cmpint (n_found, ==, 4);
1561         ASSERT_IDX (0);
1562         ASSERT_IDX (1);
1563         ASSERT_IDX (2);
1564         ASSERT_IDX (3);
1565         nmtst_assert_connection_verifies_without_normalization (connections->pdata[idx[0]]);
1566         nmtst_assert_connection_verifies_without_normalization (connections->pdata[idx[1]]);
1567         nmtst_assert_connection_verifies_after_normalization (connections->pdata[idx[2]], 0, 0);
1568         nmtst_assert_connection_verifies_without_normalization (connections->pdata[idx[3]]);
1569         g_assert_cmpstr ("test-connection-invalid-1x", ==, nm_connection_get_id (connections->pdata[idx[1]]));
1570         g_assert_cmpstr ("test-connection-invalid-2z", ==, nm_connection_get_id (connections->pdata[idx[3]]));
1571
1572 #undef ASSERT_IDX
1573 }
1574
1575 /*******************************************************************/
1576
1577 NMTST_DEFINE ();
1578
1579 int
1580 main (int argc, char **argv)
1581 {
1582         g_setenv ("LIBNM_USE_SESSION_BUS", "1", TRUE);
1583
1584         nmtst_init (&argc, &argv, TRUE);
1585
1586         loop = g_main_loop_new (NULL, FALSE);
1587
1588         g_test_add_func ("/libnm/device-added", test_device_added);
1589         g_test_add_func ("/libnm/device-added-signal-after-init", test_device_added_signal_after_init);
1590         g_test_add_func ("/libnm/wifi-ap-added-removed", test_wifi_ap_added_removed);
1591         g_test_add_func ("/libnm/wimax-nsp-added-removed", test_wimax_nsp_added_removed);
1592         g_test_add_func ("/libnm/devices-array", test_devices_array);
1593         g_test_add_func ("/libnm/client-nm-running", test_client_nm_running);
1594         g_test_add_func ("/libnm/active-connections", test_active_connections);
1595         g_test_add_func ("/libnm/activate-virtual", test_activate_virtual);
1596         g_test_add_func ("/libnm/activate-failed", test_activate_failed);
1597         g_test_add_func ("/libnm/device-connection-compatibility", test_device_connection_compatibility);
1598         g_test_add_func ("/libnm/connection/invalid", test_connection_invalid);
1599
1600         return g_test_run ();
1601 }
1602