unit: Use D-Bus watch for testing ConnMan running
[framework/connectivity/connman.git] / unit / utils.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2011  BWM CarIT GmbH. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdlib.h>
27
28 #include "gdbus/gdbus.h"
29
30 #include "test-connman.h"
31
32 #define ENABLE_WRAPPER 1
33
34 gboolean util_quit_loop(gpointer data)
35 {
36         struct test_fix *fix = data;
37
38         g_main_loop_quit(fix->main_loop);
39
40         return FALSE;
41 }
42
43 guint util_idle_call(struct test_fix *fix, GSourceFunc func,
44                         GDestroyNotify notify)
45 {
46         GSource *source;
47         guint id;
48
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);
53
54         return id;
55 }
56
57 static void connman_died(DBusConnection *connection, void *user_data)
58 {
59         g_assert(FALSE);
60 }
61
62 guint util_call(struct test_fix *fix, GSourceFunc func,
63                 GDestroyNotify notify)
64 {
65         GSource *source;
66         guint id;
67
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);
72
73         return id;
74 }
75
76 void util_setup(struct test_fix *fix, gconstpointer data)
77 {
78         fix->main_loop = g_main_loop_new(NULL, FALSE);
79         fix->main_connection = g_dbus_setup_private(DBUS_BUS_SYSTEM,
80                                                         NULL, NULL);
81         fix->watch = g_dbus_add_service_watch(fix->main_connection,
82                                                 CONNMAN_SERVICE,
83                                                 NULL,
84                                                 connman_died,
85                                                 NULL, NULL);
86 }
87
88 void util_teardown(struct test_fix *fix, gconstpointer data)
89 {
90         g_dbus_remove_watch(fix->main_connection, fix->watch);
91         dbus_connection_close(fix->main_connection);
92         dbus_connection_unref(fix->main_connection);
93
94         g_main_loop_unref(fix->main_loop);
95 }
96
97 static void util_wrapper(struct test_fix *fix, gconstpointer data)
98 {
99         GSourceFunc func = data;
100 #if ENABLE_WRAPPER
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);
104                 exit(0);
105         }
106
107         g_test_trap_assert_passed();
108 #else
109         util_call(fix, func, NULL);
110         g_main_loop_run(fix->main_loop);
111 #endif
112 }
113
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)
117 {
118         g_test_add(test_name, struct test_fix, test_func,
119                 setup_cb, util_wrapper, teardown_cb);
120 }
121
122 void util_session_create(struct test_fix *fix, unsigned int max_sessions)
123 {
124         unsigned int i;
125
126         fix->max_sessions = max_sessions;
127         fix->session = g_try_new0(struct test_session, max_sessions);
128
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);
134         }
135 }
136
137 void util_session_destroy(gpointer data)
138 {
139         struct test_fix *fix = data;
140
141         unsigned int i;
142
143         for (i = 0; i < fix->max_sessions; i++) {
144                 dbus_connection_close(fix->session[i].connection);
145                 g_free(fix->session[i].info);
146         }
147
148         g_free(fix->session);
149 }
150
151 void util_session_init(struct test_session *session)
152 {
153         DBusMessage *msg;
154         DBusMessageIter iter;
155         const char *path;
156         int err;
157
158         err = session_notify_register(session, session->notify_path);
159         g_assert(err == 0);
160
161         msg = manager_create_session(session->connection,
162                                         session->info,
163                                         session->notify_path);
164         g_assert(msg != NULL);
165         dbus_message_iter_init(msg, &iter);
166
167         dbus_message_iter_get_basic(&iter, &path);
168         session->session_path = g_strdup(path);
169
170         dbus_message_unref(msg);
171 }
172
173 void util_session_cleanup(struct test_session *session)
174 {
175         DBusMessage *msg;
176         int err;
177
178         msg = manager_destroy_session(session->connection,
179                                         session->session_path);
180         g_assert(msg != NULL);
181         dbus_message_unref(msg);
182
183         err = session_notify_unregister(session,
184                                         session->notify_path);
185         g_assert(err == 0);
186
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);
193
194         session->notify = NULL;
195         g_free(session->notify_path);
196         g_free(session->session_path);
197 }