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 connman_bool_t is_connman_running(DBusConnection *connection)
59 connman_bool_t running;
61 dbus_error_init(&error);
63 running = dbus_bus_name_has_owner(connection, CONNMAN_SERVICE, &error);
65 if (dbus_error_is_set(&error) == TRUE) {
66 fprintf(stderr, "%s\n", error.message);
67 dbus_error_free(&error);
75 static gboolean test_session_create_no_notify(gpointer data)
77 struct test_fix *fix = data;
80 util_session_create(fix, 1);
82 msg = manager_create_session(fix->session->connection,
83 fix->session->info, "/foo");
84 g_assert(msg != NULL);
85 g_assert(dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_ERROR);
87 dbus_message_unref(msg);
89 g_assert(is_connman_running(fix->session->connection) == TRUE);
90 util_idle_call(fix, util_quit_loop, util_session_destroy);
95 static gboolean test_session_destroy_no_notify(gpointer data)
97 struct test_fix *fix = data;
100 util_session_create(fix, 1);
102 msg = manager_destroy_session(fix->session->connection, "/foo");
103 g_assert(msg == NULL);
105 g_assert(is_connman_running(fix->session->connection) == TRUE);
106 util_idle_call(fix, util_quit_loop, util_session_destroy);
111 static void test_session_create_notify(struct test_session *session)
113 LOG("session %p", session);
115 g_assert(is_connman_running(session->connection) == TRUE);
116 util_idle_call(session->fix, util_quit_loop, util_session_destroy);
119 static gboolean test_session_create(gpointer data)
121 struct test_fix *fix = data;
122 struct test_session *session;
126 util_session_create(fix, 1);
127 session = fix->session;
129 session->notify_path = "/foo";
130 session->notify = test_session_create_notify;
132 err = session_notify_register(session, session->notify_path);
135 msg = manager_create_session(session->connection,
137 session->notify_path);
138 g_assert(msg != NULL);
139 g_assert(dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_ERROR);
141 dbus_message_unref(msg);
146 static gboolean test_session_create_destroy(gpointer data)
148 struct test_fix *fix = data;
149 struct test_session *session;
151 util_session_create(fix, 1);
152 session = fix->session;
154 session->notify_path = g_strdup("/foo");
156 util_session_init(fix->session);
157 util_session_cleanup(fix->session);
159 g_assert(is_connman_running(session->connection) == TRUE);
160 util_idle_call(fix, util_quit_loop, util_session_destroy);
165 static gboolean test_session_create_already_exists(gpointer data)
167 struct test_fix *fix = data;
168 struct test_session *session0, *session1;
171 util_session_create(fix, 2);
172 session0 = &fix->session[0];
173 session1 = &fix->session[1];
175 session0->notify_path = g_strdup("/foo");
176 session1->notify_path = session0->notify_path;
178 util_session_init(session0);
180 msg = manager_create_session(session1->connection,
182 session1->notify_path);
183 g_assert(msg == NULL);
185 util_session_cleanup(session0);
187 g_assert(is_connman_running(session0->connection) == TRUE);
188 util_idle_call(fix, util_quit_loop, util_session_destroy);
193 static void test_session_create_many_notify(struct test_session *session)
197 LOG("session %p", session);
199 g_assert(is_connman_running(session->connection) == TRUE);
201 nr = GPOINTER_TO_UINT(session->fix->user_data);
203 session->fix->user_data = GUINT_TO_POINTER(nr);
208 util_idle_call(session->fix, util_quit_loop, util_session_destroy);
211 static gboolean test_session_create_many(gpointer data)
213 struct test_fix *fix = data;
214 struct test_session *session;
219 fix->user_data = GUINT_TO_POINTER(max);
221 util_session_create(fix, max);
223 for (i = 0; i < max; i++) {
224 session = &fix->session[i];
226 session->notify_path = g_strdup_printf("/foo/%d", i);
227 session->notify = test_session_create_many_notify;
229 util_session_init(session);
235 static void set_session_mode(struct test_fix *fix,
236 connman_bool_t enable)
240 msg = manager_set_session_mode(fix->main_connection, enable);
241 g_assert(msg != NULL);
242 g_assert(dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_ERROR);
244 dbus_message_unref(msg);
246 util_idle_call(fix, util_quit_loop, NULL);
249 static void test_session_connect_notify(struct test_session *session)
251 LOG("session %p online %d", session, session->info->online);
253 if (session->info->online != TRUE)
256 util_session_cleanup(session);
258 g_assert(is_connman_running(session->connection) == TRUE);
259 util_idle_call(session->fix, util_quit_loop, util_session_destroy);
262 static gboolean test_session_connect(gpointer data)
264 struct test_fix *fix = data;
265 struct test_session *session;
268 util_session_create(fix, 1);
269 session = fix->session;
271 session->notify_path = g_strdup("/foo");
272 session->notify = test_session_connect_notify;
273 util_session_init(session);
275 msg = session_connect(session->connection, session);
276 g_assert(msg != NULL);
277 g_assert(dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_ERROR);
279 dbus_message_unref(msg);
284 static void test_session_disconnect_notify(struct test_session *session)
286 LOG("session %p online %d", session, session->info->online);
288 if (session->info->online != FALSE)
291 util_session_cleanup(session);
293 g_assert(is_connman_running(session->connection) == TRUE);
294 util_idle_call(session->fix, util_quit_loop, util_session_destroy);
297 static gboolean test_session_disconnect(gpointer data)
299 struct test_fix *fix = data;
300 struct test_session *session;
303 util_session_create(fix, 1);
304 session = fix->session;
306 session->notify_path = g_strdup("/foo");
307 session->notify = test_session_disconnect_notify;
308 util_session_init(session);
310 msg = session_disconnect(session->connection, session);
311 g_assert(msg != NULL);
312 dbus_message_unref(msg);
317 static void test_session_connect_disconnect_notify(struct test_session *session)
321 LOG("session %p online %d", session, session->info->online);
323 if (session->info->online != TRUE)
326 msg = session_disconnect(session->connection, session);
327 g_assert(msg != NULL);
328 dbus_message_unref(msg);
330 util_session_cleanup(session);
332 g_assert(is_connman_running(session->connection) == TRUE);
333 util_idle_call(session->fix, util_quit_loop, util_session_destroy);
336 static gboolean test_session_connect_disconnect(gpointer data)
338 struct test_fix *fix = data;
339 struct test_session *session;
342 util_session_create(fix, 1);
343 session = fix->session;
345 session->notify_path = g_strdup("/foo");
346 session->notify = test_session_connect_disconnect_notify;
347 util_session_init(session);
349 msg = session_connect(session->connection, session);
350 g_assert(msg != NULL);
351 g_assert(dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_ERROR);
353 dbus_message_unref(msg);
358 static void test_session_connect_free_ride_notify(struct test_session *session)
360 struct test_session *session0 = get_session(session, 0);
361 struct test_session *session1 = get_session(session, 1);
362 enum test_session_state state = get_session_state(session);
363 enum test_session_state next_state = state;
366 LOG("state %d session %p %s online %d", state, session,
367 session->notify_path, session->info->online);
370 case TEST_SESSION_STATE_0:
371 if (session0->info->online == FALSE &&
372 session1->info->online == FALSE) {
373 next_state = TEST_SESSION_STATE_1;
377 case TEST_SESSION_STATE_1:
378 if (session0->info->online == TRUE &&
379 session1->info->online == TRUE) {
380 next_state = TEST_SESSION_STATE_2;
384 case TEST_SESSION_STATE_2:
385 if (session0->info->online == FALSE &&
386 session1->info->online == FALSE) {
387 next_state = TEST_SESSION_STATE_3;
391 case TEST_SESSION_STATE_3:
396 if (state == next_state)
399 set_session_state(session, next_state);
401 LOG("next_state %d", next_state);
403 switch (next_state) {
404 case TEST_SESSION_STATE_0:
407 case TEST_SESSION_STATE_1:
408 msg = session_connect(session0->connection, session0);
409 g_assert(msg != NULL);
410 dbus_message_unref(msg);
414 case TEST_SESSION_STATE_2:
415 msg = session_disconnect(session0->connection, session0);
416 g_assert(msg != NULL);
417 dbus_message_unref(msg);
420 case TEST_SESSION_STATE_3:
421 util_session_cleanup(session0);
422 util_session_cleanup(session1);
424 g_assert(is_connman_running(session0->connection) == TRUE);
425 util_idle_call(session0->fix, util_quit_loop,
426 util_session_destroy);
432 static gboolean test_session_connect_free_ride(gpointer data)
434 struct test_fix *fix = data;
435 struct test_session *session0, *session1;
438 * +-------------------+
440 * +-------------------+
444 * +-------------------+
446 * +-------------------+
450 * +-------------------+
451 * | FOO-BAR-CONNECTED |
452 * +-------------------+
456 * +-------------------+
458 * +-------------------+
461 util_session_create(fix, 2);
462 session0 = &fix->session[0];
463 session1 = &fix->session[1];
465 session0->notify_path = g_strdup("/foo");
466 session1->notify_path = g_strdup("/bar");
467 session0->notify = test_session_connect_free_ride_notify;
468 session1->notify = test_session_connect_free_ride_notify;
470 util_session_init(session0);
471 util_session_init(session1);
473 set_session_state(session0, TEST_SESSION_STATE_0);
478 static gboolean enable_session_mode(gpointer data)
480 struct test_fix *fix = data;
482 set_session_mode(fix, TRUE);
487 static gboolean disable_session_mode(gpointer data)
489 struct test_fix *fix = data;
491 set_session_mode(fix, FALSE);
496 static void setup_cb(struct test_fix *fix, gconstpointer data)
498 util_setup(fix, data);
500 util_call(fix, enable_session_mode, NULL);
501 g_main_loop_run(fix->main_loop);
504 static void teardown_cb(struct test_fix *fix, gconstpointer data)
506 util_call(fix, disable_session_mode, NULL);
507 g_main_loop_run(fix->main_loop);
509 util_teardown(fix, data);
512 int main(int argc, char *argv[])
514 g_test_init(&argc, &argv, NULL);
516 util_test_add("/manager/session create no notify",
517 test_session_create_no_notify, setup_cb, teardown_cb);
518 util_test_add("/manager/session destroy no notify",
519 test_session_destroy_no_notify, setup_cb, teardown_cb);
520 util_test_add("/manager/session create",
521 test_session_create, setup_cb, teardown_cb);
522 util_test_add("/manager/session create destroy",
523 test_session_create_destroy, setup_cb, teardown_cb);
524 util_test_add("/manager/session create already exists",
525 test_session_create_already_exists, setup_cb, teardown_cb);
526 util_test_add("/manager/session create many",
527 test_session_create_many, setup_cb, teardown_cb);
529 util_test_add("/session/connect",
530 test_session_connect, setup_cb, teardown_cb);
531 util_test_add("/session/disconnect",
532 test_session_disconnect, setup_cb, teardown_cb);
533 util_test_add("/session/connect disconnect",
534 test_session_connect_disconnect, setup_cb, teardown_cb);
535 util_test_add("/session/connect free-ride",
536 test_session_connect_free_ride, setup_cb, teardown_cb);