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 static connman_bool_t is_connman_running(DBusConnection *connection)
35 connman_bool_t running;
37 dbus_error_init(&error);
39 running = dbus_bus_name_has_owner(connection, CONNMAN_SERVICE, &error);
41 if (dbus_error_is_set(&error) == TRUE) {
42 fprintf(stderr, "%s\n", error.message);
43 dbus_error_free(&error);
51 static gboolean test_session_create_no_notify(gpointer data)
53 struct test_fix *fix = data;
56 util_session_create(fix, 1);
58 msg = manager_create_session(fix->session->connection,
59 fix->session->info, "/foo");
60 g_assert(msg != NULL);
61 g_assert(dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_ERROR);
63 dbus_message_unref(msg);
65 g_assert(is_connman_running(fix->session->connection) == TRUE);
66 util_idle_call(fix, util_quit_loop, util_session_destroy);
71 static gboolean test_session_destroy_no_notify(gpointer data)
73 struct test_fix *fix = data;
76 util_session_create(fix, 1);
78 msg = manager_destroy_session(fix->session->connection, "/foo");
79 g_assert(msg == NULL);
81 g_assert(is_connman_running(fix->session->connection) == TRUE);
82 util_idle_call(fix, util_quit_loop, util_session_destroy);
87 static void test_session_create_notify(struct test_session *session)
89 LOG("session %p", session);
91 g_assert(is_connman_running(session->connection) == TRUE);
92 util_idle_call(session->fix, util_quit_loop, util_session_destroy);
95 static gboolean test_session_create(gpointer data)
97 struct test_fix *fix = data;
98 struct test_session *session;
102 util_session_create(fix, 1);
103 session = fix->session;
105 session->notify_path = "/foo";
106 session->notify = test_session_create_notify;
108 err = session_notify_register(session, session->notify_path);
111 msg = manager_create_session(session->connection,
113 session->notify_path);
114 g_assert(msg != NULL);
115 g_assert(dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_ERROR);
117 dbus_message_unref(msg);
122 static gboolean test_session_create_destroy(gpointer data)
124 struct test_fix *fix = data;
125 struct test_session *session;
127 util_session_create(fix, 1);
128 session = fix->session;
130 session->notify_path = g_strdup("/foo");
132 util_session_init(fix->session);
133 util_session_cleanup(fix->session);
135 g_assert(is_connman_running(session->connection) == TRUE);
136 util_idle_call(fix, util_quit_loop, util_session_destroy);
141 static gboolean test_session_create_already_exists(gpointer data)
143 struct test_fix *fix = data;
144 struct test_session *session0, *session1;
147 util_session_create(fix, 2);
148 session0 = &fix->session[0];
149 session1 = &fix->session[1];
151 session0->notify_path = g_strdup("/foo");
152 session1->notify_path = session0->notify_path;
154 util_session_init(session0);
156 msg = manager_create_session(session1->connection,
158 session1->notify_path);
159 g_assert(msg == NULL);
161 util_session_cleanup(session0);
163 g_assert(is_connman_running(session0->connection) == TRUE);
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 g_assert(is_connman_running(session->connection) == TRUE);
177 nr = GPOINTER_TO_UINT(session->fix->user_data);
179 session->fix->user_data = GUINT_TO_POINTER(nr);
184 util_idle_call(session->fix, util_quit_loop, util_session_destroy);
187 static gboolean test_session_create_many(gpointer data)
189 struct test_fix *fix = data;
190 struct test_session *session;
195 fix->user_data = GUINT_TO_POINTER(max);
197 util_session_create(fix, max);
199 for (i = 0; i < max; i++) {
200 session = &fix->session[i];
202 session->notify_path = g_strdup_printf("/foo/%d", i);
203 session->notify = test_session_create_many_notify;
205 util_session_init(session);
211 static void set_session_mode(struct test_fix *fix,
212 connman_bool_t enable)
216 msg = manager_set_session_mode(fix->main_connection, enable);
217 g_assert(msg != NULL);
218 g_assert(dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_ERROR);
220 dbus_message_unref(msg);
222 util_idle_call(fix, util_quit_loop, NULL);
225 static void test_session_connect_notify(struct test_session *session)
227 LOG("session %p online %d", session, session->info->online);
229 if (session->info->online != TRUE)
232 util_session_cleanup(session);
234 g_assert(is_connman_running(session->connection) == TRUE);
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 online %d", session, session->info->online);
264 if (session->info->online != FALSE)
267 util_session_cleanup(session);
269 g_assert(is_connman_running(session->connection) == TRUE);
270 util_idle_call(session->fix, util_quit_loop, util_session_destroy);
273 static gboolean test_session_disconnect(gpointer data)
275 struct test_fix *fix = data;
276 struct test_session *session;
279 util_session_create(fix, 1);
280 session = fix->session;
282 session->notify_path = g_strdup("/foo");
283 session->notify = test_session_disconnect_notify;
284 util_session_init(session);
286 msg = session_disconnect(session->connection, session);
287 g_assert(msg != NULL);
288 dbus_message_unref(msg);
293 static gboolean enable_session_mode(gpointer data)
295 struct test_fix *fix = data;
297 set_session_mode(fix, TRUE);
302 static gboolean disable_session_mode(gpointer data)
304 struct test_fix *fix = data;
306 set_session_mode(fix, FALSE);
311 static void setup_cb(struct test_fix *fix, gconstpointer data)
313 util_setup(fix, data);
315 util_call(fix, enable_session_mode, NULL);
316 g_main_loop_run(fix->main_loop);
319 static void teardown_cb(struct test_fix *fix, gconstpointer data)
321 util_call(fix, disable_session_mode, NULL);
322 g_main_loop_run(fix->main_loop);
324 util_teardown(fix, data);
327 int main(int argc, char *argv[])
329 g_test_init(&argc, &argv, NULL);
331 util_test_add("/manager/session create no notify",
332 test_session_create_no_notify, setup_cb, teardown_cb);
333 util_test_add("/manager/session destroy no notify",
334 test_session_destroy_no_notify, setup_cb, teardown_cb);
335 util_test_add("/manager/session create",
336 test_session_create, setup_cb, teardown_cb);
337 util_test_add("/manager/session create destroy",
338 test_session_create_destroy, setup_cb, teardown_cb);
339 util_test_add("/manager/session create already exists",
340 test_session_create_already_exists, setup_cb, teardown_cb);
341 util_test_add("/manager/session create many",
342 test_session_create_many, setup_cb, teardown_cb);
344 util_test_add("/session/connect",
345 test_session_connect, setup_cb, teardown_cb);
346 util_test_add("/session/disconnect",
347 test_session_disconnect, setup_cb, teardown_cb);