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
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
|
/*
*
* Connection Manager
*
* Copyright (C) 2011 BMW Car IT GmbH.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* 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 St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <unistd.h>
#include <pwd.h>
#include <sys/types.h>
#include <gdbus.h>
#include "session-test.h"
#define POLICYDIR STORAGEDIR "/session_policy_local"
enum test_session_state {
TEST_SESSION_STATE_0 = 0,
TEST_SESSION_STATE_1 = 1,
TEST_SESSION_STATE_2 = 2,
TEST_SESSION_STATE_3 = 3,
};
static enum test_session_state get_session_state(struct test_session *session)
{
return GPOINTER_TO_UINT(session->fix->user_data);
}
static void set_session_state(struct test_session *session,
enum test_session_state state)
{
session->fix->user_data = GUINT_TO_POINTER(state);
}
static struct test_session *get_session(struct test_session *session,
unsigned int index)
{
return &session->fix->session[index];
}
static void test_session_create_no_notify(struct test_fix *fix)
{
DBusMessage *msg;
util_session_create(fix, 1);
msg = manager_create_session(fix->session->connection,
fix->session->info, "/foo");
g_assert(msg);
g_assert(dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_ERROR);
dbus_message_unref(msg);
util_idle_call(fix, util_quit_loop, util_session_destroy);
}
static void test_session_destroy_no_notify(struct test_fix *fix)
{
DBusMessage *msg;
util_session_create(fix, 1);
msg = manager_destroy_session(fix->session->connection, "/foo");
g_assert(!msg);
util_idle_call(fix, util_quit_loop, util_session_destroy);
}
static void test_session_create_notify(struct test_session *session)
{
LOG("session %p", session);
util_idle_call(session->fix, util_quit_loop, util_session_destroy);
}
static void test_session_create(struct test_fix *fix)
{
struct test_session *session;
DBusMessage *msg;
int err;
util_session_create(fix, 1);
session = fix->session;
session->notify_path = "/foo";
session->notify = test_session_create_notify;
err = session_notify_register(session, session->notify_path);
g_assert(err == 0);
msg = manager_create_session(session->connection,
session->info,
session->notify_path);
g_assert(msg);
g_assert(dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_ERROR);
dbus_message_unref(msg);
}
static void test_session_create_destroy(struct test_fix *fix)
{
struct test_session *session;
util_session_create(fix, 1);
session = fix->session;
session->notify_path = g_strdup("/foo");
util_session_init(fix->session);
util_session_cleanup(fix->session);
util_idle_call(fix, util_quit_loop, util_session_destroy);
}
static void test_session_create_dup_notification(struct test_fix *fix)
{
struct test_session *session0, *session1;
DBusMessage *msg;
util_session_create(fix, 2);
session0 = &fix->session[0];
session1 = &fix->session[1];
session0->notify_path = g_strdup("/foo");
session1->notify_path = session0->notify_path;
util_session_init(session0);
msg = manager_create_session(session1->connection,
session1->info,
session1->notify_path);
g_assert(msg);
util_session_cleanup(session0);
util_idle_call(fix, util_quit_loop, util_session_destroy);
}
static void test_session_create_many_notify(struct test_session *session)
{
unsigned int nr;
LOG("session %p", session);
nr = GPOINTER_TO_UINT(session->fix->user_data);
nr--;
session->fix->user_data = GUINT_TO_POINTER(nr);
if (nr > 0)
return;
util_idle_call(session->fix, util_quit_loop, util_session_destroy);
}
static void test_session_create_many(struct test_fix *fix)
{
struct test_session *session;
unsigned int i, max;
max = 100;
fix->user_data = GUINT_TO_POINTER(max);
util_session_create(fix, max);
for (i = 0; i < max; i++) {
session = &fix->session[i];
session->notify_path = g_strdup_printf("/foo/%d", i);
session->notify = test_session_create_many_notify;
util_session_init(session);
}
}
static void set_session_mode(struct test_fix *fix,
bool enable)
{
DBusMessage *msg;
msg = manager_set_session_mode(fix->main_connection, enable);
g_assert(msg);
g_assert(dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_ERROR);
dbus_message_unref(msg);
}
static void test_session_connect_notify(struct test_session *session)
{
LOG("session %p state %d", session, session->info->state);
if (session->info->state == CONNMAN_SESSION_STATE_DISCONNECTED)
return;
util_session_cleanup(session);
util_idle_call(session->fix, util_quit_loop, util_session_destroy);
}
static void test_session_connect(struct test_fix *fix)
{
struct test_session *session;
DBusMessage *msg;
util_session_create(fix, 1);
session = fix->session;
session->notify_path = g_strdup("/foo");
session->notify = test_session_connect_notify;
util_session_init(session);
msg = session_connect(session->connection, session);
g_assert(msg);
g_assert(dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_ERROR);
dbus_message_unref(msg);
}
static void test_session_disconnect_notify(struct test_session *session)
{
LOG("session %p state %d", session, session->info->state);
if (session->info->state >= CONNMAN_SESSION_STATE_CONNECTED)
return;
util_session_cleanup(session);
util_idle_call(session->fix, util_quit_loop, util_session_destroy);
}
static void test_session_disconnect(struct test_fix *fix)
{
struct test_session *session;
DBusMessage *msg;
util_session_create(fix, 1);
session = fix->session;
session->notify_path = g_strdup("/foo");
session->notify = test_session_disconnect_notify;
util_session_init(session);
msg = session_disconnect(session->connection, session);
g_assert(msg);
dbus_message_unref(msg);
}
static void test_session_connect_disconnect_notify(struct test_session *session)
{
enum test_session_state state = get_session_state(session);
enum test_session_state next_state = state;
DBusMessage *msg;
LOG("state %d session %p %s state %d", state, session,
session->notify_path, session->info->state);
switch (state) {
case TEST_SESSION_STATE_0:
if (session->info->state == CONNMAN_SESSION_STATE_DISCONNECTED)
next_state = TEST_SESSION_STATE_1;
if (session->info->state == CONNMAN_SESSION_STATE_CONNECTED) {
LOG("state was already connected, continuing");
next_state = TEST_SESSION_STATE_2;
}
break;
case TEST_SESSION_STATE_1:
if (session->info->state >= CONNMAN_SESSION_STATE_CONNECTED)
next_state = TEST_SESSION_STATE_2;
break;
case TEST_SESSION_STATE_2:
if (session->info->state == CONNMAN_SESSION_STATE_DISCONNECTED)
next_state = TEST_SESSION_STATE_3;
default:
break;
}
if (state == next_state)
return;
set_session_state(session, next_state);
LOG("next_state %d", next_state);
switch (next_state) {
case TEST_SESSION_STATE_1:
msg = session_connect(session->connection, session);
g_assert(msg);
dbus_message_unref(msg);
return;
case TEST_SESSION_STATE_2:
msg = session_disconnect(session->connection, session);
g_assert(msg);
dbus_message_unref(msg);
return;
case TEST_SESSION_STATE_3:
util_session_cleanup(session);
util_idle_call(session->fix, util_quit_loop,
util_session_destroy);
return;
default:
return;
}
}
static void test_session_connect_disconnect(struct test_fix *fix)
{
struct test_session *session;
/*
* +-------------------+
* | START |
* +-------------------+
* |
* | connect foo
* v
* +-------------------+
* | FOO-CONNECTED |
* +-------------------+
* |
* | disconnect foo
* v
* +-------------------+
* | END |
* +-------------------+
*/
util_session_create(fix, 1);
session = fix->session;
session->notify_path = g_strdup("/foo");
session->notify = test_session_connect_disconnect_notify;
util_session_init(session);
set_session_state(session, TEST_SESSION_STATE_0);
}
static void test_session_connect_free_ride_notify(struct test_session *session)
{
struct test_session *session0 = get_session(session, 0);
struct test_session *session1 = get_session(session, 1);
enum test_session_state state = get_session_state(session);
enum test_session_state next_state = state;
DBusMessage *msg;
LOG("state %d session %p %s state %d", state, session,
session->notify_path, session->info->state);
switch (state) {
case TEST_SESSION_STATE_0:
if (session0->info->state == CONNMAN_SESSION_STATE_DISCONNECTED
&& session1->info->state ==
CONNMAN_SESSION_STATE_DISCONNECTED) {
next_state = TEST_SESSION_STATE_1;
}
if (session0->info->state == CONNMAN_SESSION_STATE_CONNECTED &&
session1->info->state ==
CONNMAN_SESSION_STATE_CONNECTED) {
LOG("state was already connected, continuing");
next_state = TEST_SESSION_STATE_2;
}
break;
case TEST_SESSION_STATE_1:
if (session0->info->state >= CONNMAN_SESSION_STATE_CONNECTED &&
session1->info->state >=
CONNMAN_SESSION_STATE_CONNECTED) {
next_state = TEST_SESSION_STATE_2;
}
break;
case TEST_SESSION_STATE_2:
if (session0->info->state == CONNMAN_SESSION_STATE_DISCONNECTED
&& (session1->info->state ==
CONNMAN_SESSION_STATE_DISCONNECTED ||
session1->info->state ==
CONNMAN_SESSION_STATE_CONNECTED) ) {
LOG("session0 /foo is disconnected, session1 /bar "
"can be either connected or disconnected");
next_state = TEST_SESSION_STATE_3;
}
break;
case TEST_SESSION_STATE_3:
return;
}
if (state == next_state)
return;
set_session_state(session, next_state);
LOG("next_state %d", next_state);
switch (next_state) {
case TEST_SESSION_STATE_0:
return;
case TEST_SESSION_STATE_1:
msg = session_connect(session0->connection, session0);
g_assert(msg);
dbus_message_unref(msg);
return;
case TEST_SESSION_STATE_2:
msg = session_disconnect(session0->connection, session0);
g_assert(msg);
dbus_message_unref(msg);
return;
case TEST_SESSION_STATE_3:
util_session_cleanup(session0);
util_session_cleanup(session1);
util_idle_call(session0->fix, util_quit_loop,
util_session_destroy);
return;
}
}
static void test_session_connect_free_ride(struct test_fix *fix)
{
struct test_session *session0, *session1;
/*
* +-------------------+
* | START |
* +-------------------+
* |
* | connect foo
* v
* +-------------------+
* | FOO-CONNECTED |
* +-------------------+
* |
* | free-ride bar
* v
* +-------------------+
* | FOO-BAR-CONNECTED |
* +-------------------+
* |
* | disconnect foo
* v
* +-------------------+
* | END |
* +-------------------+
*/
util_session_create(fix, 2);
session0 = &fix->session[0];
session1 = &fix->session[1];
session0->notify_path = g_strdup("/foo");
session1->notify_path = g_strdup("/bar");
session0->notify = test_session_connect_free_ride_notify;
session1->notify = test_session_connect_free_ride_notify;
util_session_init(session0);
util_session_init(session1);
set_session_state(session0, TEST_SESSION_STATE_0);
}
static void policy_save(GKeyFile *keyfile, char *pathname)
{
gchar *data = NULL;
gsize length = 0;
GError *error = NULL;
data = g_key_file_to_data(keyfile, &length, NULL);
if (!g_file_set_contents(pathname, data, length, &error)) {
DBG("Failed to store information: %s", error->message);
g_error_free(error);
g_assert(0);
}
g_free(data);
}
static void policy_allowed_bearers(const char *allowed_bearers)
{
struct passwd *pwd;
uid_t uid;
char *pathname;
GKeyFile *keyfile;
LOG("update to '%s'", allowed_bearers);
uid = getuid();
pwd = getpwuid(uid);
g_assert(pwd);
keyfile = g_key_file_new();
g_key_file_set_string(keyfile, "policy_foo", "uid", pwd->pw_name);
g_key_file_set_string(keyfile, "policy_foo", "AllowedBearers",
allowed_bearers);
pathname = g_strdup_printf("%s/foo.policy", POLICYDIR);
policy_save(keyfile, pathname);
g_free(pathname);
g_key_file_free(keyfile);
}
static void policy_remove_file(void)
{
char *pathname;
pathname = g_strdup_printf("%s/foo.policy", POLICYDIR);
unlink(pathname);
g_free(pathname);
}
static void test_session_policy_notify(struct test_session *session)
{
enum test_session_state state = get_session_state(session);
enum test_session_state next_state = state;
DBusMessage *msg;
LOG("state %d session %p %s state %d", state, session,
session->notify_path, session->info->state);
switch (state) {
case TEST_SESSION_STATE_0:
if (session->info->state == CONNMAN_SESSION_STATE_DISCONNECTED)
next_state = TEST_SESSION_STATE_1;
break;
case TEST_SESSION_STATE_1:
if (session->info->state >= CONNMAN_SESSION_STATE_CONNECTED)
next_state = TEST_SESSION_STATE_2;
break;
case TEST_SESSION_STATE_2:
if (session->info->state == CONNMAN_SESSION_STATE_DISCONNECTED)
next_state = TEST_SESSION_STATE_3;
default:
break;
}
if (state == next_state)
return;
set_session_state(session, next_state);
LOG("next_state %d", next_state);
switch (next_state) {
case TEST_SESSION_STATE_1:
policy_allowed_bearers("ethernet");
msg = session_connect(session->connection, session);
g_assert(msg);
dbus_message_unref(msg);
return;
case TEST_SESSION_STATE_2:
policy_allowed_bearers("");
return;
case TEST_SESSION_STATE_3:
policy_remove_file();
util_session_cleanup(session);
util_idle_call(session->fix, util_quit_loop,
util_session_destroy);
return;
default:
return;
}
}
static void test_session_policy(struct test_fix *fix)
{
struct test_session *session;
/*
* +-------------------+
* | START |
* +-------------------+
* |
* | write policy AllowedBearers = ethernet
* v
* +-------------------+
* | FOO-CONNECTED |
* +-------------------+
* |
* | write policy AllowedBearers =
* v
* +-------------------+
* | END |
* +-------------------+
*/
policy_remove_file();
util_session_create(fix, 1);
session = fix->session;
session->notify_path = g_strdup("/foo");
session->notify = test_session_policy_notify;
util_session_init(session);
set_session_state(session, TEST_SESSION_STATE_0);
}
static bool is_online(struct test_fix *fix)
{
if (g_strcmp0(fix->manager.state, "online") == 0)
return true;
return false;
}
static void enable_session_mode(struct test_fix *fix)
{
set_session_mode(fix, true);
if (!is_online(fix))
util_idle_call(fix, util_quit_loop, NULL);
}
static void manager_state_changed(struct test_fix *fix)
{
if (!is_online(fix)) {
fix->manager_changed = NULL;
util_idle_call(fix, util_quit_loop, NULL);
}
}
static void disable_session_mode(struct test_fix *fix)
{
set_session_mode(fix, false);
}
static void setup_cb(struct test_fix *fix)
{
fix->manager_changed = manager_state_changed;
util_call(fix, enable_session_mode, NULL);
}
static void teardown_cb(struct test_fix *fix)
{
util_call(fix, disable_session_mode, NULL);
util_idle_call(fix, util_quit_loop, NULL);
}
int main(int argc, char *argv[])
{
g_test_init(&argc, &argv, NULL);
util_test_add("/manager/session create no notify",
test_session_create_no_notify, setup_cb, teardown_cb);
util_test_add("/manager/session destroy no notify",
test_session_destroy_no_notify, setup_cb, teardown_cb);
util_test_add("/manager/session create",
test_session_create, setup_cb, teardown_cb);
util_test_add("/manager/session create destroy",
test_session_create_destroy, setup_cb, teardown_cb);
util_test_add("/manager/session create duplicate notification",
test_session_create_dup_notification, setup_cb, teardown_cb);
util_test_add("/manager/session create many",
test_session_create_many, setup_cb, teardown_cb);
util_test_add("/session/connect",
test_session_connect, setup_cb, teardown_cb);
util_test_add("/session/disconnect",
test_session_disconnect, setup_cb, teardown_cb);
util_test_add("/session/connect disconnect",
test_session_connect_disconnect, setup_cb, teardown_cb);
util_test_add("/session/connect free-ride",
test_session_connect_free_ride, setup_cb, teardown_cb);
util_test_add("/session/policy",
test_session_policy, setup_cb, teardown_cb);
return g_test_run();
}
|