1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
|
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Copyright 2010 - 2014 Red Hat, Inc.
*
*/
#include "config.h"
#include <glib.h>
#include <string.h>
#include <sys/types.h>
#include <signal.h>
#include <NetworkManager.h>
#include "nm-glib-compat.h"
#include "nm-test-utils.h"
#include "common.h"
/*******************************************************************/
enum {
SECRET_REQUESTED,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL] = { 0 };
typedef NMSecretAgent TestSecretAgent;
typedef NMSecretAgentClass TestSecretAgentClass;
GType test_secret_agent_get_type (void);
G_DEFINE_TYPE (TestSecretAgent, test_secret_agent, NM_TYPE_SECRET_AGENT)
static void
test_secret_agent_init (TestSecretAgent *agent)
{
}
static void
test_secret_agent_get_secrets (NMSecretAgent *agent,
NMConnection *connection,
const char *connection_path,
const char *setting_name,
const char **hints,
NMSecretAgentGetSecretsFlags flags,
NMSecretAgentGetSecretsFunc callback,
gpointer callback_data)
{
NMSettingWirelessSecurity *s_wsec;
GVariant *secrets = NULL;
GVariantBuilder secrets_builder, setting_builder;
char *secret = NULL;
GError *error = NULL;
g_assert_cmpstr (setting_name, ==, NM_SETTING_WIRELESS_SECURITY_SETTING_NAME);
s_wsec = nm_connection_get_setting_wireless_security (connection);
g_assert (s_wsec);
g_assert_cmpstr (nm_setting_wireless_security_get_key_mgmt (s_wsec), ==, "wpa-psk");
g_assert_cmpstr (nm_setting_wireless_security_get_psk (s_wsec), ==, NULL);
g_signal_emit (agent, signals[SECRET_REQUESTED], 0,
connection,
connection_path,
NM_SETTING_WIRELESS_SECURITY_SETTING_NAME,
NM_SETTING_WIRELESS_SECURITY_PSK,
&secret);
if (!secret) {
error = g_error_new (NM_SECRET_AGENT_ERROR, NM_SECRET_AGENT_ERROR_NO_SECRETS,
"No secrets");
goto done;
}
if (!strcmp (secret, "CANCEL")) {
error = g_error_new (NM_SECRET_AGENT_ERROR, NM_SECRET_AGENT_ERROR_USER_CANCELED,
"User canceled");
goto done;
}
g_variant_builder_init (&setting_builder, NM_VARIANT_TYPE_SETTING);
g_variant_builder_add (&setting_builder, "{sv}",
NM_SETTING_WIRELESS_SECURITY_PSK,
g_variant_new_string (secret));
g_variant_builder_init (&secrets_builder, NM_VARIANT_TYPE_CONNECTION);
g_variant_builder_add (&secrets_builder, "{sa{sv}}",
setting_name,
&setting_builder);
secrets = g_variant_ref_sink (g_variant_builder_end (&secrets_builder));
done:
callback (agent, connection, secrets, error, callback_data);
g_clear_error (&error);
g_clear_pointer (&secrets, g_variant_unref);
g_free (secret);
}
static void
test_secret_agent_cancel_get_secrets (NMSecretAgent *agent,
const gchar *connection_path,
const gchar *setting_name)
{
g_assert_not_reached ();
}
static void
test_secret_agent_save_secrets (NMSecretAgent *agent,
NMConnection *connection,
const gchar *connection_path,
NMSecretAgentSaveSecretsFunc callback,
gpointer callback_data)
{
g_assert_not_reached ();
}
static void
test_secret_agent_delete_secrets (NMSecretAgent *agent,
NMConnection *connection,
const gchar *connection_path,
NMSecretAgentDeleteSecretsFunc callback,
gpointer callback_data)
{
g_assert_not_reached ();
}
static void
test_secret_agent_class_init (TestSecretAgentClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
NMSecretAgentClass *agent_class = NM_SECRET_AGENT_CLASS (klass);
agent_class->get_secrets = test_secret_agent_get_secrets;
agent_class->cancel_get_secrets = test_secret_agent_cancel_get_secrets;
agent_class->save_secrets = test_secret_agent_save_secrets;
agent_class->delete_secrets = test_secret_agent_delete_secrets;
signals[SECRET_REQUESTED] =
g_signal_new ("secret-requested",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_LAST,
0, NULL, NULL, NULL,
G_TYPE_STRING, 4,
NM_TYPE_CONNECTION,
G_TYPE_STRING,
G_TYPE_STRING,
G_TYPE_STRING);
}
static NMSecretAgent *
test_secret_agent_new (void)
{
NMSecretAgent *agent;
GError *error = NULL;
agent = g_initable_new (test_secret_agent_get_type (), NULL, &error,
NM_SECRET_AGENT_IDENTIFIER, "test-secret-agent",
NM_SECRET_AGENT_AUTO_REGISTER, FALSE,
NULL);
g_assert_no_error (error);
return agent;
}
/*******************************************************************/
typedef struct {
NMTestServiceInfo *sinfo;
NMClient *client;
NMSecretAgent *agent;
NMDevice *device;
NMConnection *connection;
GMainLoop *loop;
guint timeout_id;
char *ifname;
char *con_id;
int secrets_requested;
} TestSecretAgentData;
static gboolean
timeout_assert (gpointer user_data)
{
g_assert_not_reached ();
}
static void
connection_added_cb (GObject *s,
GAsyncResult *result,
gpointer user_data)
{
TestSecretAgentData *sadata = user_data;
NMRemoteConnection *connection;
GError *error = NULL;
connection = nm_client_add_connection_finish (sadata->client, result, &error);
g_assert_no_error (error);
g_assert_cmpstr (nm_connection_get_id (NM_CONNECTION (connection)), ==, sadata->con_id);
sadata->connection = NM_CONNECTION (connection);
g_main_loop_quit (sadata->loop);
}
static void
register_cb (GObject *object, GAsyncResult *result, gpointer user_data)
{
TestSecretAgentData *sadata = user_data;
GError *error = NULL;
nm_secret_agent_register_finish (sadata->agent, result, &error);
g_assert_no_error (error);
g_assert (nm_secret_agent_get_registered (sadata->agent));
g_main_loop_quit (sadata->loop);
}
#define TEST_CON_ID_PREFIX "test-secret-agent"
static void
test_setup (TestSecretAgentData *sadata, gconstpointer test_data)
{
static int counter = 0;
const char *agent_notes = test_data;
NMConnection *connection;
NMSettingConnection *s_con;
NMSettingWireless *s_wireless;
GBytes *ssid;
NMSetting *s_wsec;
GError *error = NULL;
sadata->sinfo = nm_test_service_init ();
sadata->client = nm_client_new (NULL, &error);
g_assert_no_error (error);
sadata->loop = g_main_loop_new (NULL, FALSE);
sadata->timeout_id = g_timeout_add_seconds (5, timeout_assert, NULL);
sadata->ifname = g_strdup_printf ("wlan%d", counter);
sadata->con_id = g_strdup_printf ("%s-%d", TEST_CON_ID_PREFIX, counter);
counter++;
/* Create the device */
sadata->device = nm_test_service_add_device (sadata->sinfo, sadata->client,
"AddWifiDevice", sadata->ifname);
/* Create the connection */
connection = nmtst_create_minimal_connection (sadata->con_id, NULL, NM_SETTING_WIRELESS_SETTING_NAME, &s_con);
g_object_set (s_con,
NM_SETTING_CONNECTION_INTERFACE_NAME, sadata->ifname,
NULL);
s_wireless = nm_connection_get_setting_wireless (connection);
ssid = g_bytes_new ("foo", 3);
g_object_set (s_wireless,
NM_SETTING_WIRELESS_SSID, ssid,
NULL);
g_bytes_unref (ssid);
s_wsec = g_object_new (NM_TYPE_SETTING_WIRELESS_SECURITY,
NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "wpa-psk",
NULL);
nm_connection_add_setting (connection, s_wsec);
nm_client_add_connection_async (sadata->client,
connection,
TRUE,
NULL,
connection_added_cb,
sadata);
g_object_unref (connection);
g_main_loop_run (sadata->loop);
g_assert (sadata->connection);
if (agent_notes) {
sadata->agent = test_secret_agent_new ();
if (!strcmp (agent_notes, "sync")) {
nm_secret_agent_register (sadata->agent, NULL, &error);
g_assert_no_error (error);
g_assert (nm_secret_agent_get_registered (sadata->agent));
} else {
nm_secret_agent_register_async (sadata->agent, NULL,
register_cb, sadata);
g_main_loop_run (sadata->loop);
}
}
}
static void
test_cleanup (TestSecretAgentData *sadata, gconstpointer test_data)
{
GVariant *ret;
GError *error = NULL;
if (sadata->agent) {
if (nm_secret_agent_get_registered (sadata->agent)) {
nm_secret_agent_unregister (sadata->agent, NULL, &error);
g_assert_no_error (error);
}
g_object_unref (sadata->agent);
}
ret = g_dbus_proxy_call_sync (sadata->sinfo->proxy,
"RemoveDevice",
g_variant_new ("(s)", nm_object_get_path (NM_OBJECT (sadata->device))),
G_DBUS_CALL_FLAGS_NO_AUTO_START,
3000,
NULL,
&error);
g_assert_no_error (error);
g_variant_unref (ret);
g_object_unref (sadata->connection);
g_object_unref (sadata->client);
nm_test_service_cleanup (sadata->sinfo);
g_source_remove (sadata->timeout_id);
g_main_loop_unref (sadata->loop);
g_free (sadata->ifname);
g_free (sadata->con_id);
}
/*******************************************************************/
static void
connection_activated_none_cb (GObject *c,
GAsyncResult *result,
gpointer user_data)
{
TestSecretAgentData *sadata = user_data;
NMActiveConnection *ac;
GError *error = NULL;
ac = nm_client_activate_connection_finish (sadata->client, result, &error);
g_assert_error (error, NM_AGENT_MANAGER_ERROR, NM_AGENT_MANAGER_ERROR_NO_SECRETS);
g_main_loop_quit (sadata->loop);
}
static void
test_secret_agent_none (TestSecretAgentData *sadata, gconstpointer test_data)
{
nm_client_activate_connection_async (sadata->client,
sadata->connection,
sadata->device,
NULL,
NULL,
connection_activated_none_cb,
sadata);
g_main_loop_run (sadata->loop);
}
/*******************************************************************/
static char *
secrets_requested_no_secrets_cb (TestSecretAgent *agent,
NMConnection *connection,
const char *connection_path,
const char *setting_name,
const char *secret_name,
gpointer user_data)
{
TestSecretAgentData *sadata = user_data;
g_assert_cmpstr (connection_path, ==, nm_connection_get_path (sadata->connection));
sadata->secrets_requested++;
return NULL;
}
static void
connection_activated_no_secrets_cb (GObject *c,
GAsyncResult *result,
gpointer user_data)
{
TestSecretAgentData *sadata = user_data;
NMActiveConnection *ac;
GError *error = NULL;
ac = nm_client_activate_connection_finish (sadata->client, result, &error);
g_assert_error (error, NM_AGENT_MANAGER_ERROR, NM_AGENT_MANAGER_ERROR_NO_SECRETS);
g_main_loop_quit (sadata->loop);
}
static void
test_secret_agent_no_secrets (TestSecretAgentData *sadata, gconstpointer test_data)
{
g_signal_connect (sadata->agent, "secret-requested",
G_CALLBACK (secrets_requested_no_secrets_cb),
sadata);
nm_client_activate_connection_async (sadata->client,
sadata->connection,
sadata->device,
NULL,
NULL,
connection_activated_no_secrets_cb,
sadata);
g_main_loop_run (sadata->loop);
g_assert_cmpint (sadata->secrets_requested, ==, 1);
}
/*******************************************************************/
static void
connection_activated_cancel_cb (GObject *c,
GAsyncResult *result,
gpointer user_data)
{
TestSecretAgentData *sadata = user_data;
NMActiveConnection *ac;
GError *error = NULL;
ac = nm_client_activate_connection_finish (sadata->client, result, &error);
g_assert_error (error, NM_AGENT_MANAGER_ERROR, NM_AGENT_MANAGER_ERROR_USER_CANCELED);
g_main_loop_quit (sadata->loop);
}
static char *
secrets_requested_cancel_cb (TestSecretAgent *agent,
NMConnection *connection,
const char *connection_path,
const char *setting_name,
const char *secret_name,
gpointer user_data)
{
TestSecretAgentData *sadata = user_data;
g_assert_cmpstr (connection_path, ==, nm_connection_get_path (sadata->connection));
sadata->secrets_requested++;
return g_strdup ("CANCEL");
}
static void
test_secret_agent_cancel (TestSecretAgentData *sadata, gconstpointer test_data)
{
g_signal_connect (sadata->agent, "secret-requested",
G_CALLBACK (secrets_requested_cancel_cb),
sadata);
nm_client_activate_connection_async (sadata->client,
sadata->connection,
sadata->device,
NULL,
NULL,
connection_activated_cancel_cb,
sadata);
g_main_loop_run (sadata->loop);
g_assert_cmpint (sadata->secrets_requested, ==, 1);
}
/*******************************************************************/
static void
connection_activated_good_cb (GObject *c,
GAsyncResult *result,
gpointer user_data)
{
TestSecretAgentData *sadata = user_data;
NMActiveConnection *ac;
GError *error = NULL;
ac = nm_client_activate_connection_finish (sadata->client, result, &error);
g_assert_no_error (error);
g_object_unref (ac);
g_main_loop_quit (sadata->loop);
}
static char *
secrets_requested_good_cb (TestSecretAgent *agent,
NMConnection *connection,
const char *connection_path,
const char *setting_name,
const char *secret_name,
gpointer user_data)
{
TestSecretAgentData *sadata = user_data;
g_assert_cmpstr (connection_path, ==, nm_connection_get_path (sadata->connection));
sadata->secrets_requested++;
return g_strdup ("password");
}
static void
test_secret_agent_good (TestSecretAgentData *sadata, gconstpointer test_data)
{
g_signal_connect (sadata->agent, "secret-requested",
G_CALLBACK (secrets_requested_good_cb),
sadata);
nm_client_activate_connection_async (sadata->client,
sadata->connection,
sadata->device,
NULL,
NULL,
connection_activated_good_cb,
sadata);
g_main_loop_run (sadata->loop);
g_assert_cmpint (sadata->secrets_requested, ==, 1);
}
static void
async_init_cb (GObject *object, GAsyncResult *result, gpointer user_data)
{
GMainLoop *loop = user_data;
GError *error = NULL;
GObject *agent;
agent = g_async_initable_new_finish (G_ASYNC_INITABLE (object), result, &error);
g_assert_error (error, NM_SECRET_AGENT_ERROR, NM_SECRET_AGENT_ERROR_FAILED);
g_assert (agent == NULL);
g_clear_error (&error);
g_main_loop_quit (loop);
}
static void
test_secret_agent_nm_not_running (void)
{
NMSecretAgent *agent;
GMainLoop *loop;
GError *error = NULL;
agent = g_initable_new (test_secret_agent_get_type (), NULL, &error,
NM_SECRET_AGENT_IDENTIFIER, "test-secret-agent",
NULL);
g_assert_error (error, NM_SECRET_AGENT_ERROR, NM_SECRET_AGENT_ERROR_FAILED);
g_assert (agent == NULL);
g_clear_error (&error);
loop = g_main_loop_new (NULL, FALSE);
g_async_initable_new_async (test_secret_agent_get_type (),
G_PRIORITY_DEFAULT,
NULL, async_init_cb, loop,
NM_SECRET_AGENT_IDENTIFIER, "test-secret-agent",
NULL);
g_main_loop_run (loop);
g_main_loop_unref (loop);
}
static void
registered_changed (GObject *object, GParamSpec *pspec, gpointer user_data)
{
GMainLoop *loop = user_data;
g_main_loop_quit (loop);
}
static void
test_secret_agent_auto_register (void)
{
NMTestServiceInfo *sinfo;
NMSecretAgent *agent;
GMainLoop *loop;
GError *error = NULL;
sinfo = nm_test_service_init ();
loop = g_main_loop_new (NULL, FALSE);
agent = test_secret_agent_new ();
g_object_set (agent,
NM_SECRET_AGENT_AUTO_REGISTER, TRUE,
NULL);
g_signal_connect (agent, "notify::" NM_SECRET_AGENT_REGISTERED,
G_CALLBACK (registered_changed), loop);
g_assert (!nm_secret_agent_get_registered (agent));
nm_secret_agent_register (agent, NULL, &error);
g_assert_no_error (error);
g_assert (nm_secret_agent_get_registered (agent));
/* Shut down test service */
nm_test_service_cleanup (sinfo);
g_main_loop_run (loop);
g_assert (!nm_secret_agent_get_registered (agent));
/* Restart test service */
sinfo = nm_test_service_init ();
g_main_loop_run (loop);
g_assert (nm_secret_agent_get_registered (agent));
/* Shut down test service again */
nm_test_service_cleanup (sinfo);
g_main_loop_run (loop);
g_assert (!nm_secret_agent_get_registered (agent));
g_object_unref (agent);
g_main_loop_unref (loop);
}
/*******************************************************************/
int
main (int argc, char **argv)
{
int ret;
g_setenv ("LIBNM_USE_SESSION_BUS", "1", TRUE);
#if !GLIB_CHECK_VERSION (2, 35, 0)
g_type_init ();
#endif
g_test_init (&argc, &argv, NULL);
g_test_add ("/libnm/secret-agent/none", TestSecretAgentData, NULL,
test_setup, test_secret_agent_none, test_cleanup);
g_test_add ("/libnm/secret-agent/no-secrets", TestSecretAgentData, "sync",
test_setup, test_secret_agent_no_secrets, test_cleanup);
g_test_add ("/libnm/secret-agent/cancel", TestSecretAgentData, "async",
test_setup, test_secret_agent_cancel, test_cleanup);
g_test_add ("/libnm/secret-agent/good", TestSecretAgentData, "async",
test_setup, test_secret_agent_good, test_cleanup);
g_test_add_func ("/libnm/secret-agent/nm-not-running", test_secret_agent_nm_not_running);
g_test_add_func ("/libnm/secret-agent/auto-register", test_secret_agent_auto_register);
ret = g_test_run ();
return ret;
}
|