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
33 #define PROPERTY_CHANGED "PropertyChanged"
35 gboolean util_quit_loop(gpointer data)
37 struct test_fix *fix = data;
39 g_main_loop_quit(fix->main_loop);
44 guint util_idle_call(struct test_fix *fix, GSourceFunc func,
45 GDestroyNotify notify)
50 source = g_idle_source_new();
51 g_source_set_callback(source, func, fix, notify);
52 id = g_source_attach(source, g_main_loop_get_context(fix->main_loop));
53 g_source_unref(source);
58 static void connman_died(DBusConnection *connection, void *user_data)
63 static void manager_changed(struct test_fix *fix,
64 DBusMessageIter *entry)
71 dbus_message_iter_get_basic(entry, &key);
75 dbus_message_iter_next(entry);
77 dbus_message_iter_recurse(entry, &iter);
79 type = dbus_message_iter_get_arg_type(&iter);
81 if (type != DBUS_TYPE_STRING)
84 dbus_message_iter_get_basic(&iter, &value);
86 if (g_str_equal(key, "State") == TRUE) {
87 LOG("State %s", value);
89 if (fix->manager.state != NULL)
90 g_free(fix->manager.state);
92 fix->manager.state = g_strdup(value);
95 if (fix->manager_changed != NULL)
96 fix->manager_changed(fix);
99 static gboolean handle_manager_changed(DBusConnection *connection,
100 DBusMessage *message,
103 struct test_fix *fix = user_data;
105 DBusMessageIter iter;
107 if (dbus_message_iter_init(message, &iter))
108 manager_changed(fix, &iter);
113 guint util_call(struct test_fix *fix, GSourceFunc func,
114 GDestroyNotify notify)
119 source = g_timeout_source_new(0);
120 g_source_set_callback(source, func, fix, notify);
121 id = g_source_attach(source, g_main_loop_get_context(fix->main_loop));
122 g_source_unref(source);
127 void util_setup(struct test_fix *fix, gconstpointer data)
131 fix->main_loop = g_main_loop_new(NULL, FALSE);
132 fix->main_connection = g_dbus_setup_private(DBUS_BUS_SYSTEM,
134 fix->watch = g_dbus_add_service_watch(fix->main_connection,
139 fix->manager_watch = g_dbus_add_signal_watch(fix->main_connection,
141 CONNMAN_MANAGER_INTERFACE,
143 handle_manager_changed,
146 msg = manager_get_properties(fix->main_connection);
147 manager_parse_properties(msg, &fix->manager);
148 dbus_message_unref(msg);
151 void util_teardown(struct test_fix *fix, gconstpointer data)
153 g_dbus_remove_watch(fix->main_connection, fix->watch);
154 g_dbus_remove_watch(fix->main_connection, fix->manager_watch);
155 dbus_connection_close(fix->main_connection);
156 dbus_connection_unref(fix->main_connection);
158 g_main_loop_unref(fix->main_loop);
161 static void util_wrapper(struct test_fix *fix, gconstpointer data)
163 GSourceFunc func = data;
165 if (g_test_trap_fork(60 * 1000 * 1000, 0) == TRUE) {
166 util_call(fix, func, NULL);
167 g_main_loop_run(fix->main_loop);
171 g_test_trap_assert_passed();
173 util_call(fix, func, NULL);
174 g_main_loop_run(fix->main_loop);
178 void util_test_add(const char *test_name, GSourceFunc test_func,
179 util_test_setup_cb setup_cb,
180 util_test_teardown_cb teardown_cb)
182 g_test_add(test_name, struct test_fix, test_func,
183 setup_cb, util_wrapper, teardown_cb);
186 void util_session_create(struct test_fix *fix, unsigned int max_sessions)
190 fix->max_sessions = max_sessions;
191 fix->session = g_try_new0(struct test_session, max_sessions);
193 for (i = 0; i < max_sessions; i++) {
194 fix->session[i].fix = fix;
195 fix->session[i].info = g_try_new0(struct test_session_info, 1);
196 fix->session[i].connection = g_dbus_setup_private(
197 DBUS_BUS_SYSTEM, NULL, NULL);
201 void util_session_destroy(gpointer data)
203 struct test_fix *fix = data;
207 for (i = 0; i < fix->max_sessions; i++) {
208 dbus_connection_close(fix->session[i].connection);
209 g_free(fix->session[i].info);
212 g_free(fix->session);
215 void util_session_init(struct test_session *session)
218 DBusMessageIter iter;
222 err = session_notify_register(session, session->notify_path);
225 msg = manager_create_session(session->connection,
227 session->notify_path);
228 g_assert(msg != NULL);
229 dbus_message_iter_init(msg, &iter);
231 dbus_message_iter_get_basic(&iter, &path);
232 session->session_path = g_strdup(path);
234 dbus_message_unref(msg);
237 void util_session_cleanup(struct test_session *session)
242 msg = manager_destroy_session(session->connection,
243 session->session_path);
244 g_assert(msg != NULL);
245 dbus_message_unref(msg);
247 err = session_notify_unregister(session,
248 session->notify_path);
251 g_free(session->info->bearer);
252 g_free(session->info->name);
253 g_free(session->info->interface);
254 g_slist_foreach(session->info->allowed_bearers,
255 bearer_info_cleanup, NULL);
256 g_slist_free(session->info->allowed_bearers);
258 session->notify = NULL;
259 g_free(session->notify_path);
260 g_free(session->session_path);