5 * Copyright (C) 2011 BWM CarIT GmbH. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 #include <sys/types.h>
33 #include "session-test.h"
35 #define POLICYDIR STORAGEDIR "/session_policy_local"
37 enum test_session_state {
38 TEST_SESSION_STATE_0 = 0,
39 TEST_SESSION_STATE_1 = 1,
40 TEST_SESSION_STATE_2 = 2,
41 TEST_SESSION_STATE_3 = 3,
44 static enum test_session_state get_session_state(struct test_session *session)
46 return GPOINTER_TO_UINT(session->fix->user_data);
49 static void set_session_state(struct test_session *session,
50 enum test_session_state state)
52 session->fix->user_data = GUINT_TO_POINTER(state);
55 static struct test_session *get_session(struct test_session *session,
58 return &session->fix->session[index];
61 static gboolean test_session_create_no_notify(gpointer data)
63 struct test_fix *fix = data;
66 util_session_create(fix, 1);
68 msg = manager_create_session(fix->session->connection,
69 fix->session->info, "/foo");
70 g_assert(msg != NULL);
71 g_assert(dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_ERROR);
73 dbus_message_unref(msg);
75 util_idle_call(fix, util_quit_loop, util_session_destroy);
80 static gboolean test_session_destroy_no_notify(gpointer data)
82 struct test_fix *fix = data;
85 util_session_create(fix, 1);
87 msg = manager_destroy_session(fix->session->connection, "/foo");
88 g_assert(msg == NULL);
90 util_idle_call(fix, util_quit_loop, util_session_destroy);
95 static void test_session_create_notify(struct test_session *session)
97 LOG("session %p", session);
99 util_idle_call(session->fix, util_quit_loop, util_session_destroy);
102 static gboolean test_session_create(gpointer data)
104 struct test_fix *fix = data;
105 struct test_session *session;
109 util_session_create(fix, 1);
110 session = fix->session;
112 session->notify_path = "/foo";
113 session->notify = test_session_create_notify;
115 err = session_notify_register(session, session->notify_path);
118 msg = manager_create_session(session->connection,
120 session->notify_path);
121 g_assert(msg != NULL);
122 g_assert(dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_ERROR);
124 dbus_message_unref(msg);
129 static gboolean test_session_create_destroy(gpointer data)
131 struct test_fix *fix = data;
132 struct test_session *session;
134 util_session_create(fix, 1);
135 session = fix->session;
137 session->notify_path = g_strdup("/foo");
139 util_session_init(fix->session);
140 util_session_cleanup(fix->session);
142 util_idle_call(fix, util_quit_loop, util_session_destroy);
147 static gboolean test_session_create_already_exists(gpointer data)
149 struct test_fix *fix = data;
150 struct test_session *session0, *session1;
153 util_session_create(fix, 2);
154 session0 = &fix->session[0];
155 session1 = &fix->session[1];
157 session0->notify_path = g_strdup("/foo");
158 session1->notify_path = session0->notify_path;
160 util_session_init(session0);
162 msg = manager_create_session(session1->connection,
164 session1->notify_path);
165 g_assert(msg == NULL);
167 util_session_cleanup(session0);
169 util_idle_call(fix, util_quit_loop, util_session_destroy);
174 static void test_session_create_many_notify(struct test_session *session)
178 LOG("session %p", session);
180 nr = GPOINTER_TO_UINT(session->fix->user_data);
182 session->fix->user_data = GUINT_TO_POINTER(nr);
187 util_idle_call(session->fix, util_quit_loop, util_session_destroy);
190 static gboolean test_session_create_many(gpointer data)
192 struct test_fix *fix = data;
193 struct test_session *session;
198 fix->user_data = GUINT_TO_POINTER(max);
200 util_session_create(fix, max);
202 for (i = 0; i < max; i++) {
203 session = &fix->session[i];
205 session->notify_path = g_strdup_printf("/foo/%d", i);
206 session->notify = test_session_create_many_notify;
208 util_session_init(session);
214 static void set_session_mode(struct test_fix *fix,
215 connman_bool_t enable)
219 msg = manager_set_session_mode(fix->main_connection, enable);
220 g_assert(msg != NULL);
221 g_assert(dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_ERROR);
223 dbus_message_unref(msg);
226 static void test_session_connect_notify(struct test_session *session)
228 LOG("session %p state %d", session, session->info->state);
230 if (session->info->state == CONNMAN_SESSION_STATE_DISCONNECTED)
233 util_session_cleanup(session);
235 util_idle_call(session->fix, util_quit_loop, util_session_destroy);
238 static gboolean test_session_connect(gpointer data)
240 struct test_fix *fix = data;
241 struct test_session *session;
244 util_session_create(fix, 1);
245 session = fix->session;
247 session->notify_path = g_strdup("/foo");
248 session->notify = test_session_connect_notify;
249 util_session_init(session);
251 msg = session_connect(session->connection, session);
252 g_assert(msg != NULL);
253 g_assert(dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_ERROR);
255 dbus_message_unref(msg);
260 static void test_session_disconnect_notify(struct test_session *session)
262 LOG("session %p state %d", session, session->info->state);
264 if (session->info->state >= CONNMAN_SESSION_STATE_CONNECTED)
267 util_session_cleanup(session);
269 util_idle_call(session->fix, util_quit_loop, util_session_destroy);
272 static gboolean test_session_disconnect(gpointer data)
274 struct test_fix *fix = data;
275 struct test_session *session;
278 util_session_create(fix, 1);
279 session = fix->session;
281 session->notify_path = g_strdup("/foo");
282 session->notify = test_session_disconnect_notify;
283 util_session_init(session);
285 msg = session_disconnect(session->connection, session);
286 g_assert(msg != NULL);
287 dbus_message_unref(msg);
292 static void test_session_connect_disconnect_notify(struct test_session *session)
294 enum test_session_state state = get_session_state(session);
295 enum test_session_state next_state = state;
298 LOG("state %d session %p %s state %d", state, session,
299 session->notify_path, session->info->state);
302 case TEST_SESSION_STATE_0:
303 if (session->info->state == CONNMAN_SESSION_STATE_DISCONNECTED)
304 next_state = TEST_SESSION_STATE_1;
306 case TEST_SESSION_STATE_1:
307 if (session->info->state >= CONNMAN_SESSION_STATE_CONNECTED)
308 next_state = TEST_SESSION_STATE_2;
310 case TEST_SESSION_STATE_2:
311 if (session->info->state == CONNMAN_SESSION_STATE_DISCONNECTED)
312 next_state = TEST_SESSION_STATE_3;
317 if (state == next_state)
320 set_session_state(session, next_state);
322 LOG("next_state %d", next_state);
324 switch (next_state) {
325 case TEST_SESSION_STATE_1:
326 msg = session_connect(session->connection, session);
327 g_assert(msg != NULL);
328 dbus_message_unref(msg);
330 case TEST_SESSION_STATE_2:
331 msg = session_disconnect(session->connection, session);
332 g_assert(msg != NULL);
333 dbus_message_unref(msg);
335 case TEST_SESSION_STATE_3:
336 util_session_cleanup(session);
337 util_idle_call(session->fix, util_quit_loop,
338 util_session_destroy);
345 static gboolean test_session_connect_disconnect(gpointer data)
347 struct test_fix *fix = data;
348 struct test_session *session;
351 * +-------------------+
353 * +-------------------+
357 * +-------------------+
359 * +-------------------+
363 * +-------------------+
365 * +-------------------+
368 util_session_create(fix, 1);
369 session = fix->session;
371 session->notify_path = g_strdup("/foo");
372 session->notify = test_session_connect_disconnect_notify;
374 util_session_init(session);
376 set_session_state(session, TEST_SESSION_STATE_0);
381 static void test_session_connect_free_ride_notify(struct test_session *session)
383 struct test_session *session0 = get_session(session, 0);
384 struct test_session *session1 = get_session(session, 1);
385 enum test_session_state state = get_session_state(session);
386 enum test_session_state next_state = state;
389 LOG("state %d session %p %s state %d", state, session,
390 session->notify_path, session->info->state);
393 case TEST_SESSION_STATE_0:
394 if (session0->info->state == CONNMAN_SESSION_STATE_DISCONNECTED
395 && session1->info->state ==
396 CONNMAN_SESSION_STATE_DISCONNECTED) {
397 next_state = TEST_SESSION_STATE_1;
401 case TEST_SESSION_STATE_1:
402 if (session0->info->state >= CONNMAN_SESSION_STATE_CONNECTED &&
403 session1->info->state >=
404 CONNMAN_SESSION_STATE_CONNECTED) {
405 next_state = TEST_SESSION_STATE_2;
409 case TEST_SESSION_STATE_2:
410 if (session0->info->state == CONNMAN_SESSION_STATE_DISCONNECTED
411 && session1->info->state ==
412 CONNMAN_SESSION_STATE_DISCONNECTED) {
413 next_state = TEST_SESSION_STATE_3;
417 case TEST_SESSION_STATE_3:
422 if (state == next_state)
425 set_session_state(session, next_state);
427 LOG("next_state %d", next_state);
429 switch (next_state) {
430 case TEST_SESSION_STATE_0:
433 case TEST_SESSION_STATE_1:
434 msg = session_connect(session0->connection, session0);
435 g_assert(msg != NULL);
436 dbus_message_unref(msg);
440 case TEST_SESSION_STATE_2:
441 msg = session_disconnect(session0->connection, session0);
442 g_assert(msg != NULL);
443 dbus_message_unref(msg);
446 case TEST_SESSION_STATE_3:
447 util_session_cleanup(session0);
448 util_session_cleanup(session1);
450 util_idle_call(session0->fix, util_quit_loop,
451 util_session_destroy);
457 static gboolean test_session_connect_free_ride(gpointer data)
459 struct test_fix *fix = data;
460 struct test_session *session0, *session1;
463 * +-------------------+
465 * +-------------------+
469 * +-------------------+
471 * +-------------------+
475 * +-------------------+
476 * | FOO-BAR-CONNECTED |
477 * +-------------------+
481 * +-------------------+
483 * +-------------------+
486 util_session_create(fix, 2);
487 session0 = &fix->session[0];
488 session1 = &fix->session[1];
490 session0->notify_path = g_strdup("/foo");
491 session1->notify_path = g_strdup("/bar");
492 session0->notify = test_session_connect_free_ride_notify;
493 session1->notify = test_session_connect_free_ride_notify;
495 util_session_init(session0);
496 util_session_init(session1);
498 set_session_state(session0, TEST_SESSION_STATE_0);
503 static void policy_save(GKeyFile *keyfile, char *pathname)
507 GError *error = NULL;
509 data = g_key_file_to_data(keyfile, &length, NULL);
511 if(!g_file_set_contents(pathname, data, length, &error)) {
512 DBG("Failed to store information: %s", error->message);
520 static void policy_allowed_bearers(const char *allowed_bearers)
527 LOG("update to '%s'", allowed_bearers);
531 g_assert(pwd != NULL);
533 keyfile = g_key_file_new();
534 g_key_file_set_string(keyfile, "policy_foo", "uid", pwd->pw_name);
535 g_key_file_set_string(keyfile, "policy_foo", "AllowedBearers",
538 pathname = g_strdup_printf("%s/foo.policy", POLICYDIR);
539 policy_save(keyfile, pathname);
542 g_key_file_free(keyfile);
545 static void policy_remove_file(void)
549 pathname = g_strdup_printf("%s/foo.policy", POLICYDIR);
554 static void test_session_policy_notify(struct test_session *session)
556 enum test_session_state state = get_session_state(session);
557 enum test_session_state next_state = state;
560 LOG("state %d session %p %s state %d", state, session,
561 session->notify_path, session->info->state);
564 case TEST_SESSION_STATE_0:
565 if (session->info->state == CONNMAN_SESSION_STATE_DISCONNECTED)
566 next_state = TEST_SESSION_STATE_1;
568 case TEST_SESSION_STATE_1:
569 if (session->info->state >= CONNMAN_SESSION_STATE_CONNECTED)
570 next_state = TEST_SESSION_STATE_2;
572 case TEST_SESSION_STATE_2:
573 if (session->info->state == CONNMAN_SESSION_STATE_DISCONNECTED)
574 next_state = TEST_SESSION_STATE_3;
579 if (state == next_state)
582 set_session_state(session, next_state);
584 LOG("next_state %d", next_state);
586 switch (next_state) {
587 case TEST_SESSION_STATE_1:
588 policy_allowed_bearers("ethernet");
590 msg = session_connect(session->connection, session);
591 g_assert(msg != NULL);
592 dbus_message_unref(msg);
594 case TEST_SESSION_STATE_2:
595 policy_allowed_bearers("");
597 case TEST_SESSION_STATE_3:
598 policy_remove_file();
599 util_session_cleanup(session);
600 util_idle_call(session->fix, util_quit_loop,
601 util_session_destroy);
608 static gboolean test_session_policy(gpointer data)
610 struct test_fix *fix = data;
611 struct test_session *session;
614 * +-------------------+
616 * +-------------------+
618 * | write policy AllowedBearers = ethernet
620 * +-------------------+
622 * +-------------------+
624 * | write policy AllowedBearers =
626 * +-------------------+
628 * +-------------------+
631 policy_remove_file();
633 util_session_create(fix, 1);
634 session = fix->session;
636 session->notify_path = g_strdup("/foo");
637 session->notify = test_session_policy_notify;
639 util_session_init(session);
641 set_session_state(session, TEST_SESSION_STATE_0);
646 static connman_bool_t is_online(struct test_fix *fix)
648 if (g_strcmp0(fix->manager.state, "online") == 0)
654 static gboolean enable_session_mode(gpointer data)
656 struct test_fix *fix = data;
658 set_session_mode(fix, TRUE);
660 if (is_online(fix) == FALSE)
661 util_idle_call(fix, util_quit_loop, NULL);
666 static gboolean manager_state_changed(gpointer data)
668 struct test_fix *fix = data;
670 if (is_online(fix) == FALSE) {
671 fix->manager_changed = NULL;
672 util_idle_call(fix, util_quit_loop, NULL);
678 static gboolean disable_session_mode(gpointer data)
680 struct test_fix *fix = data;
682 set_session_mode(fix, FALSE);
687 static void setup_cb(struct test_fix *fix, gconstpointer data)
689 fix->manager_changed = manager_state_changed;
691 util_setup(fix, data);
692 util_call(fix, enable_session_mode, NULL);
694 g_main_loop_run(fix->main_loop);
696 fix->manager_changed = NULL;
699 static void teardown_cb(struct test_fix *fix, gconstpointer data)
701 util_call(fix, disable_session_mode, NULL);
702 util_idle_call(fix, util_quit_loop, NULL);
704 g_main_loop_run(fix->main_loop);
706 util_teardown(fix, data);
709 int main(int argc, char *argv[])
711 g_test_init(&argc, &argv, NULL);
713 util_test_add("/manager/session create no notify",
714 test_session_create_no_notify, setup_cb, teardown_cb);
715 util_test_add("/manager/session destroy no notify",
716 test_session_destroy_no_notify, setup_cb, teardown_cb);
717 util_test_add("/manager/session create",
718 test_session_create, setup_cb, teardown_cb);
719 util_test_add("/manager/session create destroy",
720 test_session_create_destroy, setup_cb, teardown_cb);
721 util_test_add("/manager/session create already exists",
722 test_session_create_already_exists, setup_cb, teardown_cb);
723 util_test_add("/manager/session create many",
724 test_session_create_many, setup_cb, teardown_cb);
726 util_test_add("/session/connect",
727 test_session_connect, setup_cb, teardown_cb);
728 util_test_add("/session/disconnect",
729 test_session_disconnect, setup_cb, teardown_cb);
730 util_test_add("/session/connect disconnect",
731 test_session_connect_disconnect, setup_cb, teardown_cb);
732 util_test_add("/session/connect free-ride",
733 test_session_connect_free_ride, setup_cb, teardown_cb);
735 util_test_add("/session/policy",
736 test_session_policy, setup_cb, teardown_cb);