unit: Fix wrong include of common header
[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 guint util_call(struct test_fix *fix, GSourceFunc func,
58                 GDestroyNotify notify)
59 {
60         GSource *source;
61         guint id;
62
63         source = g_timeout_source_new(0);
64         g_source_set_callback(source, func, fix, notify);
65         id = g_source_attach(source, g_main_loop_get_context(fix->main_loop));
66         g_source_unref(source);
67
68         return id;
69 }
70
71 void util_setup(struct test_fix *fix, gconstpointer data)
72 {
73         fix->main_loop = g_main_loop_new(NULL, FALSE);
74         fix->main_connection = g_dbus_setup_private(DBUS_BUS_SYSTEM,
75                                                         NULL, NULL);
76 }
77
78 void util_teardown(struct test_fix *fix, gconstpointer data)
79 {
80         dbus_connection_close(fix->main_connection);
81         dbus_connection_unref(fix->main_connection);
82
83         g_main_loop_unref(fix->main_loop);
84 }
85
86 static void util_wrapper(struct test_fix *fix, gconstpointer data)
87 {
88         GSourceFunc func = data;
89 #if ENABLE_WRAPPER
90         if (g_test_trap_fork(60 * 1000 * 1000, 0) == TRUE) {
91                 util_call(fix, func, NULL);
92                 g_main_loop_run(fix->main_loop);
93                 exit(0);
94         }
95
96         g_test_trap_assert_passed();
97 #else
98         util_call(fix, func, NULL);
99         g_main_loop_run(fix->main_loop);
100 #endif
101 }
102
103 void util_test_add(const char *test_name, GSourceFunc test_func,
104                         util_test_setup_cb setup_cb,
105                         util_test_teardown_cb teardown_cb)
106 {
107         g_test_add(test_name, struct test_fix, test_func,
108                 setup_cb, util_wrapper, teardown_cb);
109 }
110
111 void util_session_create(struct test_fix *fix, unsigned int max_sessions)
112 {
113         unsigned int i;
114
115         fix->max_sessions = max_sessions;
116         fix->session = g_try_new0(struct test_session, max_sessions);
117
118         for (i = 0; i < max_sessions; i++) {
119                 fix->session[i].fix = fix;
120                 fix->session[i].info = g_try_new0(struct test_session_info, 1);
121                 fix->session[i].connection = g_dbus_setup_private(
122                                                 DBUS_BUS_SYSTEM, NULL, NULL);
123         }
124 }
125
126 void util_session_destroy(gpointer data)
127 {
128         struct test_fix *fix = data;
129
130         unsigned int i;
131
132         for (i = 0; i < fix->max_sessions; i++) {
133                 dbus_connection_close(fix->session[i].connection);
134                 g_free(fix->session[i].info);
135         }
136
137         g_free(fix->session);
138 }
139
140 void util_session_init(struct test_session *session)
141 {
142         DBusMessage *msg;
143         DBusMessageIter iter;
144         const char *path;
145         int err;
146
147         err = session_notify_register(session, session->notify_path);
148         g_assert(err == 0);
149
150         msg = manager_create_session(session->connection,
151                                         session->info,
152                                         session->notify_path);
153         g_assert(msg != NULL);
154         dbus_message_iter_init(msg, &iter);
155
156         dbus_message_iter_get_basic(&iter, &path);
157         session->session_path = g_strdup(path);
158
159         dbus_message_unref(msg);
160 }
161
162 void util_session_cleanup(struct test_session *session)
163 {
164         DBusMessage *msg;
165         int err;
166
167         msg = manager_destroy_session(session->connection,
168                                         session->session_path);
169         g_assert(msg != NULL);
170         dbus_message_unref(msg);
171
172         err = session_notify_unregister(session,
173                                         session->notify_path);
174         g_assert(err == 0);
175
176         g_free(session->info->bearer);
177         g_free(session->info->name);
178         g_free(session->info->interface);
179         g_slist_foreach(session->info->allowed_bearers,
180                         bearer_info_cleanup, NULL);
181         g_slist_free(session->info->allowed_bearers);
182
183         session->notify = NULL;
184         g_free(session->notify_path);
185         g_free(session->session_path);
186 }