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 #define ENABLE_WRAPPER 1
34 gboolean util_quit_loop(gpointer data)
36 struct test_fix *fix = data;
38 g_main_loop_quit(fix->main_loop);
43 guint util_idle_call(struct test_fix *fix, GSourceFunc func,
44 GDestroyNotify notify)
49 source = g_idle_source_new();
50 g_source_set_callback(source, func, fix, notify);
51 id = g_source_attach(source, g_main_loop_get_context(fix->main_loop));
52 g_source_unref(source);
57 static void connman_died(DBusConnection *connection, void *user_data)
62 guint util_call(struct test_fix *fix, GSourceFunc func,
63 GDestroyNotify notify)
68 source = g_timeout_source_new(0);
69 g_source_set_callback(source, func, fix, notify);
70 id = g_source_attach(source, g_main_loop_get_context(fix->main_loop));
71 g_source_unref(source);
76 void util_setup(struct test_fix *fix, gconstpointer data)
78 fix->main_loop = g_main_loop_new(NULL, FALSE);
79 fix->main_connection = g_dbus_setup_private(DBUS_BUS_SYSTEM,
81 fix->watch = g_dbus_add_service_watch(fix->main_connection,
88 void util_teardown(struct test_fix *fix, gconstpointer data)
90 g_dbus_remove_watch(fix->main_connection, fix->watch);
91 dbus_connection_close(fix->main_connection);
92 dbus_connection_unref(fix->main_connection);
94 g_main_loop_unref(fix->main_loop);
97 static void util_wrapper(struct test_fix *fix, gconstpointer data)
99 GSourceFunc func = data;
101 if (g_test_trap_fork(60 * 1000 * 1000, 0) == TRUE) {
102 util_call(fix, func, NULL);
103 g_main_loop_run(fix->main_loop);
107 g_test_trap_assert_passed();
109 util_call(fix, func, NULL);
110 g_main_loop_run(fix->main_loop);
114 void util_test_add(const char *test_name, GSourceFunc test_func,
115 util_test_setup_cb setup_cb,
116 util_test_teardown_cb teardown_cb)
118 g_test_add(test_name, struct test_fix, test_func,
119 setup_cb, util_wrapper, teardown_cb);
122 void util_session_create(struct test_fix *fix, unsigned int max_sessions)
126 fix->max_sessions = max_sessions;
127 fix->session = g_try_new0(struct test_session, max_sessions);
129 for (i = 0; i < max_sessions; i++) {
130 fix->session[i].fix = fix;
131 fix->session[i].info = g_try_new0(struct test_session_info, 1);
132 fix->session[i].connection = g_dbus_setup_private(
133 DBUS_BUS_SYSTEM, NULL, NULL);
137 void util_session_destroy(gpointer data)
139 struct test_fix *fix = data;
143 for (i = 0; i < fix->max_sessions; i++) {
144 dbus_connection_close(fix->session[i].connection);
145 g_free(fix->session[i].info);
148 g_free(fix->session);
151 void util_session_init(struct test_session *session)
154 DBusMessageIter iter;
158 err = session_notify_register(session, session->notify_path);
161 msg = manager_create_session(session->connection,
163 session->notify_path);
164 g_assert(msg != NULL);
165 dbus_message_iter_init(msg, &iter);
167 dbus_message_iter_get_basic(&iter, &path);
168 session->session_path = g_strdup(path);
170 dbus_message_unref(msg);
173 void util_session_cleanup(struct test_session *session)
178 msg = manager_destroy_session(session->connection,
179 session->session_path);
180 g_assert(msg != NULL);
181 dbus_message_unref(msg);
183 err = session_notify_unregister(session,
184 session->notify_path);
187 g_free(session->info->bearer);
188 g_free(session->info->name);
189 g_free(session->info->interface);
190 g_slist_foreach(session->info->allowed_bearers,
191 bearer_info_cleanup, NULL);
192 g_slist_free(session->info->allowed_bearers);
194 session->notify = NULL;
195 g_free(session->notify_path);
196 g_free(session->session_path);