30f1b45f180853ffe7a6c4096c13ad5f6217072f
[framework/connectivity/connman.git] / unit / test-session.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 <stdio.h>
27
28 #include "gdbus/gdbus.h"
29
30 #include "test-connman.h"
31
32 static connman_bool_t is_connman_running(DBusConnection *connection)
33 {
34         DBusError error;
35         connman_bool_t running;
36
37         dbus_error_init(&error);
38
39         running = dbus_bus_name_has_owner(connection, CONNMAN_SERVICE, &error);
40
41         if (dbus_error_is_set(&error) == TRUE) {
42                 fprintf(stderr, "%s\n", error.message);
43                 dbus_error_free(&error);
44
45                 return FALSE;
46         }
47
48         return running;
49 }
50
51 static gboolean test_session_create_no_notify(gpointer data)
52 {
53         struct test_fix *fix = data;
54         DBusMessage *msg;
55
56         util_session_create(fix, 1);
57
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);
62
63         dbus_message_unref(msg);
64
65         g_assert(is_connman_running(fix->session->connection) == TRUE);
66         util_idle_call(fix, util_quit_loop, util_session_destroy);
67
68         return FALSE;
69 }
70
71 static gboolean test_session_destroy_no_notify(gpointer data)
72 {
73         struct test_fix *fix = data;
74         DBusMessage *msg;
75
76         util_session_create(fix, 1);
77
78         msg = manager_destroy_session(fix->session->connection, "/foo");
79         g_assert(msg == NULL);
80
81         g_assert(is_connman_running(fix->session->connection) == TRUE);
82         util_idle_call(fix, util_quit_loop, util_session_destroy);
83
84         return FALSE;
85 }
86
87 static void test_session_create_notify(struct test_session *session)
88 {
89         LOG("session %p", session);
90
91         g_assert(is_connman_running(session->connection) == TRUE);
92         util_idle_call(session->fix, util_quit_loop, util_session_destroy);
93 }
94
95 static gboolean test_session_create(gpointer data)
96 {
97         struct test_fix *fix = data;
98         struct test_session *session;
99         DBusMessage *msg;
100         int err;
101
102         util_session_create(fix, 1);
103         session = fix->session;
104
105         session->notify_path = "/foo";
106         session->notify = test_session_create_notify;
107
108         err = session_notify_register(session, session->notify_path);
109         g_assert(err == 0);
110
111         msg = manager_create_session(session->connection,
112                                         session->info,
113                                         session->notify_path);
114         g_assert(msg != NULL);
115         g_assert(dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_ERROR);
116
117         dbus_message_unref(msg);
118
119         return FALSE;
120 }
121
122 static gboolean test_session_create_destroy(gpointer data)
123 {
124         struct test_fix *fix = data;
125         struct test_session *session;
126
127         util_session_create(fix, 1);
128         session = fix->session;
129
130         session->notify_path = g_strdup("/foo");
131
132         util_session_init(fix->session);
133         util_session_cleanup(fix->session);
134
135         g_assert(is_connman_running(session->connection) == TRUE);
136         util_idle_call(fix, util_quit_loop, util_session_destroy);
137
138         return FALSE;
139 }
140
141 static gboolean test_session_create_already_exists(gpointer data)
142 {
143         struct test_fix *fix = data;
144         struct test_session *session0, *session1;
145         DBusMessage *msg;
146
147         util_session_create(fix, 2);
148         session0 = &fix->session[0];
149         session1 = &fix->session[1];
150
151         session0->notify_path = g_strdup("/foo");
152         session1->notify_path = session0->notify_path;
153
154         util_session_init(session0);
155
156         msg = manager_create_session(session1->connection,
157                                         session1->info,
158                                         session1->notify_path);
159         g_assert(msg == NULL);
160
161         util_session_cleanup(session0);
162
163         g_assert(is_connman_running(session0->connection) == TRUE);
164         util_idle_call(fix, util_quit_loop, util_session_destroy);
165
166         return FALSE;
167 }
168
169 static void test_session_create_many_notify(struct test_session *session)
170 {
171         unsigned int nr;
172
173         LOG("session %p", session);
174
175         g_assert(is_connman_running(session->connection) == TRUE);
176
177         nr = GPOINTER_TO_UINT(session->fix->user_data);
178         nr--;
179         session->fix->user_data = GUINT_TO_POINTER(nr);
180
181         if (nr > 0)
182                 return;
183
184         util_idle_call(session->fix, util_quit_loop, util_session_destroy);
185 }
186
187 static gboolean test_session_create_many(gpointer data)
188 {
189         struct test_fix *fix = data;
190         struct test_session *session;
191         unsigned int i, max;
192
193         max = 100;
194
195         fix->user_data = GUINT_TO_POINTER(max);
196
197         util_session_create(fix, max);
198
199         for (i = 0; i < max; i++) {
200                 session = &fix->session[i];
201
202                 session->notify_path = g_strdup_printf("/foo/%d", i);
203                 session->notify = test_session_create_many_notify;
204
205                 util_session_init(session);
206         }
207
208         return FALSE;
209 }
210
211 static void set_session_mode(struct test_fix *fix,
212                                         connman_bool_t enable)
213 {
214         DBusMessage *msg;
215
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);
219
220         dbus_message_unref(msg);
221
222         util_idle_call(fix, util_quit_loop, NULL);
223 }
224
225 static void test_session_connect_notify(struct test_session *session)
226 {
227         LOG("session %p online %d", session, session->info->online);
228
229         if (session->info->online != TRUE)
230                 return;
231
232         util_session_cleanup(session);
233
234         g_assert(is_connman_running(session->connection) == TRUE);
235         util_idle_call(session->fix, util_quit_loop, util_session_destroy);
236 }
237
238 static gboolean test_session_connect(gpointer data)
239 {
240         struct test_fix *fix = data;
241         struct test_session *session;
242         DBusMessage *msg;
243
244         util_session_create(fix, 1);
245         session = fix->session;
246
247         session->notify_path = g_strdup("/foo");
248         session->notify =  test_session_connect_notify;
249         util_session_init(session);
250
251         msg = session_connect(session->connection, session);
252         g_assert(msg != NULL);
253         g_assert(dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_ERROR);
254
255         dbus_message_unref(msg);
256
257         return FALSE;
258 }
259
260 static void test_session_disconnect_notify(struct test_session *session)
261 {
262         LOG("session %p online %d", session, session->info->online);
263
264         if (session->info->online != FALSE)
265                 return;
266
267         util_session_cleanup(session);
268
269         g_assert(is_connman_running(session->connection) == TRUE);
270         util_idle_call(session->fix, util_quit_loop, util_session_destroy);
271 }
272
273 static gboolean test_session_disconnect(gpointer data)
274 {
275         struct test_fix *fix = data;
276         struct test_session *session;
277         DBusMessage *msg;
278
279         util_session_create(fix, 1);
280         session = fix->session;
281
282         session->notify_path = g_strdup("/foo");
283         session->notify =  test_session_disconnect_notify;
284         util_session_init(session);
285
286         msg = session_disconnect(session->connection, session);
287         g_assert(msg != NULL);
288         dbus_message_unref(msg);
289
290         return FALSE;
291 }
292
293 static void test_session_connect_disconnect_notify(struct test_session *session)
294 {
295         DBusMessage *msg;
296
297         LOG("session %p online %d", session, session->info->online);
298
299         if (session->info->online != TRUE)
300                 return;
301
302         msg = session_disconnect(session->connection, session);
303         g_assert(msg != NULL);
304         dbus_message_unref(msg);
305
306         util_session_cleanup(session);
307
308         g_assert(is_connman_running(session->connection) == TRUE);
309         util_idle_call(session->fix, util_quit_loop, util_session_destroy);
310 }
311
312 static gboolean test_session_connect_disconnect(gpointer data)
313 {
314         struct test_fix *fix = data;
315         struct test_session *session;
316         DBusMessage *msg;
317
318         util_session_create(fix, 1);
319         session = fix->session;
320
321         session->notify_path = g_strdup("/foo");
322         session->notify =  test_session_connect_disconnect_notify;
323         util_session_init(session);
324
325         msg = session_connect(session->connection, session);
326         g_assert(msg != NULL);
327         g_assert(dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_ERROR);
328
329         dbus_message_unref(msg);
330
331         return FALSE;
332 }
333
334 static gboolean enable_session_mode(gpointer data)
335 {
336         struct test_fix *fix = data;
337
338         set_session_mode(fix, TRUE);
339
340         return FALSE;
341 }
342
343 static gboolean disable_session_mode(gpointer data)
344 {
345         struct test_fix *fix = data;
346
347         set_session_mode(fix, FALSE);
348
349         return FALSE;
350 }
351
352 static void setup_cb(struct test_fix *fix, gconstpointer data)
353 {
354         util_setup(fix, data);
355
356         util_call(fix, enable_session_mode, NULL);
357         g_main_loop_run(fix->main_loop);
358 }
359
360 static void teardown_cb(struct test_fix *fix, gconstpointer data)
361 {
362         util_call(fix, disable_session_mode, NULL);
363         g_main_loop_run(fix->main_loop);
364
365         util_teardown(fix, data);
366 }
367
368 int main(int argc, char *argv[])
369 {
370         g_test_init(&argc, &argv, NULL);
371
372         util_test_add("/manager/session create no notify",
373                 test_session_create_no_notify, setup_cb, teardown_cb);
374         util_test_add("/manager/session destroy no notify",
375                 test_session_destroy_no_notify, setup_cb, teardown_cb);
376         util_test_add("/manager/session create",
377                 test_session_create, setup_cb, teardown_cb);
378         util_test_add("/manager/session create destroy",
379                 test_session_create_destroy, setup_cb, teardown_cb);
380         util_test_add("/manager/session create already exists",
381                 test_session_create_already_exists, setup_cb, teardown_cb);
382         util_test_add("/manager/session create many",
383                 test_session_create_many, setup_cb, teardown_cb);
384
385         util_test_add("/session/connect",
386                 test_session_connect, setup_cb, teardown_cb);
387         util_test_add("/session/disconnect",
388                 test_session_disconnect, setup_cb, teardown_cb);
389         util_test_add("/session/connect disconnect",
390                 test_session_connect_disconnect, setup_cb, teardown_cb);
391
392         return g_test_run();
393 }