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
28 #include "gdbus/gdbus.h"
30 #include "test-connman.h"
32 enum test_session_state {
33 TEST_SESSION_STATE_0 = 0,
34 TEST_SESSION_STATE_1 = 1,
35 TEST_SESSION_STATE_2 = 2,
36 TEST_SESSION_STATE_3 = 3,
39 static enum test_session_state get_session_state(struct test_session *session)
41 return GPOINTER_TO_UINT(session->fix->user_data);
44 static void set_session_state(struct test_session *session,
45 enum test_session_state state)
47 session->fix->user_data = GUINT_TO_POINTER(state);
50 static struct test_session *get_session(struct test_session *session,
53 return &session->fix->session[index];
56 static gboolean test_session_create_no_notify(gpointer data)
58 struct test_fix *fix = data;
61 util_session_create(fix, 1);
63 msg = manager_create_session(fix->session->connection,
64 fix->session->info, "/foo");
65 g_assert(msg != NULL);
66 g_assert(dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_ERROR);
68 dbus_message_unref(msg);
70 util_idle_call(fix, util_quit_loop, util_session_destroy);
75 static gboolean test_session_destroy_no_notify(gpointer data)
77 struct test_fix *fix = data;
80 util_session_create(fix, 1);
82 msg = manager_destroy_session(fix->session->connection, "/foo");
83 g_assert(msg == NULL);
85 util_idle_call(fix, util_quit_loop, util_session_destroy);
90 static void test_session_create_notify(struct test_session *session)
92 LOG("session %p", session);
94 util_idle_call(session->fix, util_quit_loop, util_session_destroy);
97 static gboolean test_session_create(gpointer data)
99 struct test_fix *fix = data;
100 struct test_session *session;
104 util_session_create(fix, 1);
105 session = fix->session;
107 session->notify_path = "/foo";
108 session->notify = test_session_create_notify;
110 err = session_notify_register(session, session->notify_path);
113 msg = manager_create_session(session->connection,
115 session->notify_path);
116 g_assert(msg != NULL);
117 g_assert(dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_ERROR);
119 dbus_message_unref(msg);
124 static gboolean test_session_create_destroy(gpointer data)
126 struct test_fix *fix = data;
127 struct test_session *session;
129 util_session_create(fix, 1);
130 session = fix->session;
132 session->notify_path = g_strdup("/foo");
134 util_session_init(fix->session);
135 util_session_cleanup(fix->session);
137 util_idle_call(fix, util_quit_loop, util_session_destroy);
142 static gboolean test_session_create_already_exists(gpointer data)
144 struct test_fix *fix = data;
145 struct test_session *session0, *session1;
148 util_session_create(fix, 2);
149 session0 = &fix->session[0];
150 session1 = &fix->session[1];
152 session0->notify_path = g_strdup("/foo");
153 session1->notify_path = session0->notify_path;
155 util_session_init(session0);
157 msg = manager_create_session(session1->connection,
159 session1->notify_path);
160 g_assert(msg == NULL);
162 util_session_cleanup(session0);
164 util_idle_call(fix, util_quit_loop, util_session_destroy);
169 static void test_session_create_many_notify(struct test_session *session)
173 LOG("session %p", session);
175 nr = GPOINTER_TO_UINT(session->fix->user_data);
177 session->fix->user_data = GUINT_TO_POINTER(nr);
182 util_idle_call(session->fix, util_quit_loop, util_session_destroy);
185 static gboolean test_session_create_many(gpointer data)
187 struct test_fix *fix = data;
188 struct test_session *session;
193 fix->user_data = GUINT_TO_POINTER(max);
195 util_session_create(fix, max);
197 for (i = 0; i < max; i++) {
198 session = &fix->session[i];
200 session->notify_path = g_strdup_printf("/foo/%d", i);
201 session->notify = test_session_create_many_notify;
203 util_session_init(session);
209 static void set_session_mode(struct test_fix *fix,
210 connman_bool_t enable)
214 msg = manager_set_session_mode(fix->main_connection, enable);
215 g_assert(msg != NULL);
216 g_assert(dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_ERROR);
218 dbus_message_unref(msg);
221 static void test_session_connect_notify(struct test_session *session)
223 LOG("session %p state %d", session, session->info->state);
225 if (session->info->state == CONNMAN_SESSION_STATE_DISCONNECTED)
228 util_session_cleanup(session);
230 util_idle_call(session->fix, util_quit_loop, util_session_destroy);
233 static gboolean test_session_connect(gpointer data)
235 struct test_fix *fix = data;
236 struct test_session *session;
239 util_session_create(fix, 1);
240 session = fix->session;
242 session->notify_path = g_strdup("/foo");
243 session->notify = test_session_connect_notify;
244 util_session_init(session);
246 msg = session_connect(session->connection, session);
247 g_assert(msg != NULL);
248 g_assert(dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_ERROR);
250 dbus_message_unref(msg);
255 static void test_session_disconnect_notify(struct test_session *session)
257 LOG("session %p state %d", session, session->info->state);
259 if (session->info->state >= CONNMAN_SESSION_STATE_CONNECTED)
262 util_session_cleanup(session);
264 util_idle_call(session->fix, util_quit_loop, util_session_destroy);
267 static gboolean test_session_disconnect(gpointer data)
269 struct test_fix *fix = data;
270 struct test_session *session;
273 util_session_create(fix, 1);
274 session = fix->session;
276 session->notify_path = g_strdup("/foo");
277 session->notify = test_session_disconnect_notify;
278 util_session_init(session);
280 msg = session_disconnect(session->connection, session);
281 g_assert(msg != NULL);
282 dbus_message_unref(msg);
287 static void test_session_connect_disconnect_notify(struct test_session *session)
289 enum test_session_state state = get_session_state(session);
290 enum test_session_state next_state = state;
293 LOG("state %d session %p %s state %d", state, session,
294 session->notify_path, session->info->state);
297 case TEST_SESSION_STATE_0:
298 if (session->info->state == CONNMAN_SESSION_STATE_DISCONNECTED)
299 next_state = TEST_SESSION_STATE_1;
301 case TEST_SESSION_STATE_1:
302 if (session->info->state >= CONNMAN_SESSION_STATE_CONNECTED)
303 next_state = TEST_SESSION_STATE_2;
305 case TEST_SESSION_STATE_2:
306 if (session->info->state == CONNMAN_SESSION_STATE_DISCONNECTED)
307 next_state = TEST_SESSION_STATE_3;
312 if (state == next_state)
315 set_session_state(session, next_state);
317 LOG("next_state %d", next_state);
319 switch (next_state) {
320 case TEST_SESSION_STATE_1:
321 msg = session_connect(session->connection, session);
322 g_assert(msg != NULL);
323 dbus_message_unref(msg);
325 case TEST_SESSION_STATE_2:
326 msg = session_disconnect(session->connection, session);
327 g_assert(msg != NULL);
328 dbus_message_unref(msg);
330 case TEST_SESSION_STATE_3:
331 util_session_cleanup(session);
332 util_idle_call(session->fix, util_quit_loop,
333 util_session_destroy);
340 static gboolean test_session_connect_disconnect(gpointer data)
342 struct test_fix *fix = data;
343 struct test_session *session;
346 * +-------------------+
348 * +-------------------+
352 * +-------------------+
354 * +-------------------+
358 * +-------------------+
360 * +-------------------+
363 util_session_create(fix, 1);
364 session = fix->session;
366 session->notify_path = g_strdup("/foo");
367 session->notify = test_session_connect_disconnect_notify;
369 util_session_init(session);
371 set_session_state(session, TEST_SESSION_STATE_0);
376 static void test_session_connect_free_ride_notify(struct test_session *session)
378 struct test_session *session0 = get_session(session, 0);
379 struct test_session *session1 = get_session(session, 1);
380 enum test_session_state state = get_session_state(session);
381 enum test_session_state next_state = state;
384 LOG("state %d session %p %s state %d", state, session,
385 session->notify_path, session->info->state);
388 case TEST_SESSION_STATE_0:
389 if (session0->info->state == CONNMAN_SESSION_STATE_DISCONNECTED
390 && session1->info->state ==
391 CONNMAN_SESSION_STATE_DISCONNECTED) {
392 next_state = TEST_SESSION_STATE_1;
396 case TEST_SESSION_STATE_1:
397 if (session0->info->state >= CONNMAN_SESSION_STATE_CONNECTED &&
398 session1->info->state >=
399 CONNMAN_SESSION_STATE_CONNECTED) {
400 next_state = TEST_SESSION_STATE_2;
404 case TEST_SESSION_STATE_2:
405 if (session0->info->state == CONNMAN_SESSION_STATE_DISCONNECTED
406 && session1->info->state ==
407 CONNMAN_SESSION_STATE_DISCONNECTED) {
408 next_state = TEST_SESSION_STATE_3;
412 case TEST_SESSION_STATE_3:
417 if (state == next_state)
420 set_session_state(session, next_state);
422 LOG("next_state %d", next_state);
424 switch (next_state) {
425 case TEST_SESSION_STATE_0:
428 case TEST_SESSION_STATE_1:
429 msg = session_connect(session0->connection, session0);
430 g_assert(msg != NULL);
431 dbus_message_unref(msg);
435 case TEST_SESSION_STATE_2:
436 msg = session_disconnect(session0->connection, session0);
437 g_assert(msg != NULL);
438 dbus_message_unref(msg);
441 case TEST_SESSION_STATE_3:
442 util_session_cleanup(session0);
443 util_session_cleanup(session1);
445 util_idle_call(session0->fix, util_quit_loop,
446 util_session_destroy);
452 static gboolean test_session_connect_free_ride(gpointer data)
454 struct test_fix *fix = data;
455 struct test_session *session0, *session1;
458 * +-------------------+
460 * +-------------------+
464 * +-------------------+
466 * +-------------------+
470 * +-------------------+
471 * | FOO-BAR-CONNECTED |
472 * +-------------------+
476 * +-------------------+
478 * +-------------------+
481 util_session_create(fix, 2);
482 session0 = &fix->session[0];
483 session1 = &fix->session[1];
485 session0->notify_path = g_strdup("/foo");
486 session1->notify_path = g_strdup("/bar");
487 session0->notify = test_session_connect_free_ride_notify;
488 session1->notify = test_session_connect_free_ride_notify;
490 util_session_init(session0);
491 util_session_init(session1);
493 set_session_state(session0, TEST_SESSION_STATE_0);
498 static connman_bool_t is_online(struct test_fix *fix)
500 if (g_strcmp0(fix->manager.state, "online") == 0)
506 static gboolean enable_session_mode(gpointer data)
508 struct test_fix *fix = data;
510 set_session_mode(fix, TRUE);
512 if (is_online(fix) == FALSE)
513 util_idle_call(fix, util_quit_loop, NULL);
518 static gboolean manager_state_changed(gpointer data)
520 struct test_fix *fix = data;
522 if (is_online(fix) == FALSE) {
523 fix->manager_changed = NULL;
524 util_idle_call(fix, util_quit_loop, NULL);
530 static gboolean disable_session_mode(gpointer data)
532 struct test_fix *fix = data;
534 set_session_mode(fix, FALSE);
539 static void setup_cb(struct test_fix *fix, gconstpointer data)
541 fix->manager_changed = manager_state_changed;
543 util_setup(fix, data);
544 util_call(fix, enable_session_mode, NULL);
546 g_main_loop_run(fix->main_loop);
548 fix->manager_changed = NULL;
551 static void teardown_cb(struct test_fix *fix, gconstpointer data)
553 util_call(fix, disable_session_mode, NULL);
554 util_idle_call(fix, util_quit_loop, NULL);
556 g_main_loop_run(fix->main_loop);
558 util_teardown(fix, data);
561 int main(int argc, char *argv[])
563 g_test_init(&argc, &argv, NULL);
565 util_test_add("/manager/session create no notify",
566 test_session_create_no_notify, setup_cb, teardown_cb);
567 util_test_add("/manager/session destroy no notify",
568 test_session_destroy_no_notify, setup_cb, teardown_cb);
569 util_test_add("/manager/session create",
570 test_session_create, setup_cb, teardown_cb);
571 util_test_add("/manager/session create destroy",
572 test_session_create_destroy, setup_cb, teardown_cb);
573 util_test_add("/manager/session create already exists",
574 test_session_create_already_exists, setup_cb, teardown_cb);
575 util_test_add("/manager/session create many",
576 test_session_create_many, setup_cb, teardown_cb);
578 util_test_add("/session/connect",
579 test_session_connect, setup_cb, teardown_cb);
580 util_test_add("/session/disconnect",
581 test_session_disconnect, setup_cb, teardown_cb);
582 util_test_add("/session/connect disconnect",
583 test_session_connect_disconnect, setup_cb, teardown_cb);
584 util_test_add("/session/connect free-ride",
585 test_session_connect_free_ride, setup_cb, teardown_cb);