unit: Fix wrong include of common header
[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 enum test_session_state {
33         TEST_SESSION_STATE_0 = 0,
34         TEST_SESSION_STATE_1 = 1,
35         TEST_SESSION_STATE_2 = 2,
36         TEST_SESSION_STATE_3 = 3,
37 };
38
39 static enum test_session_state get_session_state(struct test_session *session)
40 {
41         return GPOINTER_TO_UINT(session->fix->user_data);
42 }
43
44 static void set_session_state(struct test_session *session,
45                                 enum test_session_state state)
46 {
47         session->fix->user_data = GUINT_TO_POINTER(state);
48 }
49
50 static struct test_session *get_session(struct test_session *session,
51                                         unsigned int index)
52 {
53         return &session->fix->session[index];
54 }
55
56 static connman_bool_t is_connman_running(DBusConnection *connection)
57 {
58         DBusError error;
59         connman_bool_t running;
60
61         dbus_error_init(&error);
62
63         running = dbus_bus_name_has_owner(connection, CONNMAN_SERVICE, &error);
64
65         if (dbus_error_is_set(&error) == TRUE) {
66                 fprintf(stderr, "%s\n", error.message);
67                 dbus_error_free(&error);
68
69                 return FALSE;
70         }
71
72         return running;
73 }
74
75 static gboolean test_session_create_no_notify(gpointer data)
76 {
77         struct test_fix *fix = data;
78         DBusMessage *msg;
79
80         util_session_create(fix, 1);
81
82         msg = manager_create_session(fix->session->connection,
83                                         fix->session->info, "/foo");
84         g_assert(msg != NULL);
85         g_assert(dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_ERROR);
86
87         dbus_message_unref(msg);
88
89         g_assert(is_connman_running(fix->session->connection) == TRUE);
90         util_idle_call(fix, util_quit_loop, util_session_destroy);
91
92         return FALSE;
93 }
94
95 static gboolean test_session_destroy_no_notify(gpointer data)
96 {
97         struct test_fix *fix = data;
98         DBusMessage *msg;
99
100         util_session_create(fix, 1);
101
102         msg = manager_destroy_session(fix->session->connection, "/foo");
103         g_assert(msg == NULL);
104
105         g_assert(is_connman_running(fix->session->connection) == TRUE);
106         util_idle_call(fix, util_quit_loop, util_session_destroy);
107
108         return FALSE;
109 }
110
111 static void test_session_create_notify(struct test_session *session)
112 {
113         LOG("session %p", session);
114
115         g_assert(is_connman_running(session->connection) == TRUE);
116         util_idle_call(session->fix, util_quit_loop, util_session_destroy);
117 }
118
119 static gboolean test_session_create(gpointer data)
120 {
121         struct test_fix *fix = data;
122         struct test_session *session;
123         DBusMessage *msg;
124         int err;
125
126         util_session_create(fix, 1);
127         session = fix->session;
128
129         session->notify_path = "/foo";
130         session->notify = test_session_create_notify;
131
132         err = session_notify_register(session, session->notify_path);
133         g_assert(err == 0);
134
135         msg = manager_create_session(session->connection,
136                                         session->info,
137                                         session->notify_path);
138         g_assert(msg != NULL);
139         g_assert(dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_ERROR);
140
141         dbus_message_unref(msg);
142
143         return FALSE;
144 }
145
146 static gboolean test_session_create_destroy(gpointer data)
147 {
148         struct test_fix *fix = data;
149         struct test_session *session;
150
151         util_session_create(fix, 1);
152         session = fix->session;
153
154         session->notify_path = g_strdup("/foo");
155
156         util_session_init(fix->session);
157         util_session_cleanup(fix->session);
158
159         g_assert(is_connman_running(session->connection) == TRUE);
160         util_idle_call(fix, util_quit_loop, util_session_destroy);
161
162         return FALSE;
163 }
164
165 static gboolean test_session_create_already_exists(gpointer data)
166 {
167         struct test_fix *fix = data;
168         struct test_session *session0, *session1;
169         DBusMessage *msg;
170
171         util_session_create(fix, 2);
172         session0 = &fix->session[0];
173         session1 = &fix->session[1];
174
175         session0->notify_path = g_strdup("/foo");
176         session1->notify_path = session0->notify_path;
177
178         util_session_init(session0);
179
180         msg = manager_create_session(session1->connection,
181                                         session1->info,
182                                         session1->notify_path);
183         g_assert(msg == NULL);
184
185         util_session_cleanup(session0);
186
187         g_assert(is_connman_running(session0->connection) == TRUE);
188         util_idle_call(fix, util_quit_loop, util_session_destroy);
189
190         return FALSE;
191 }
192
193 static void test_session_create_many_notify(struct test_session *session)
194 {
195         unsigned int nr;
196
197         LOG("session %p", session);
198
199         g_assert(is_connman_running(session->connection) == TRUE);
200
201         nr = GPOINTER_TO_UINT(session->fix->user_data);
202         nr--;
203         session->fix->user_data = GUINT_TO_POINTER(nr);
204
205         if (nr > 0)
206                 return;
207
208         util_idle_call(session->fix, util_quit_loop, util_session_destroy);
209 }
210
211 static gboolean test_session_create_many(gpointer data)
212 {
213         struct test_fix *fix = data;
214         struct test_session *session;
215         unsigned int i, max;
216
217         max = 100;
218
219         fix->user_data = GUINT_TO_POINTER(max);
220
221         util_session_create(fix, max);
222
223         for (i = 0; i < max; i++) {
224                 session = &fix->session[i];
225
226                 session->notify_path = g_strdup_printf("/foo/%d", i);
227                 session->notify = test_session_create_many_notify;
228
229                 util_session_init(session);
230         }
231
232         return FALSE;
233 }
234
235 static void set_session_mode(struct test_fix *fix,
236                                         connman_bool_t enable)
237 {
238         DBusMessage *msg;
239
240         msg = manager_set_session_mode(fix->main_connection, enable);
241         g_assert(msg != NULL);
242         g_assert(dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_ERROR);
243
244         dbus_message_unref(msg);
245
246         util_idle_call(fix, util_quit_loop, NULL);
247 }
248
249 static void test_session_connect_notify(struct test_session *session)
250 {
251         LOG("session %p online %d", session, session->info->online);
252
253         if (session->info->online != TRUE)
254                 return;
255
256         util_session_cleanup(session);
257
258         g_assert(is_connman_running(session->connection) == TRUE);
259         util_idle_call(session->fix, util_quit_loop, util_session_destroy);
260 }
261
262 static gboolean test_session_connect(gpointer data)
263 {
264         struct test_fix *fix = data;
265         struct test_session *session;
266         DBusMessage *msg;
267
268         util_session_create(fix, 1);
269         session = fix->session;
270
271         session->notify_path = g_strdup("/foo");
272         session->notify =  test_session_connect_notify;
273         util_session_init(session);
274
275         msg = session_connect(session->connection, session);
276         g_assert(msg != NULL);
277         g_assert(dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_ERROR);
278
279         dbus_message_unref(msg);
280
281         return FALSE;
282 }
283
284 static void test_session_disconnect_notify(struct test_session *session)
285 {
286         LOG("session %p online %d", session, session->info->online);
287
288         if (session->info->online != FALSE)
289                 return;
290
291         util_session_cleanup(session);
292
293         g_assert(is_connman_running(session->connection) == TRUE);
294         util_idle_call(session->fix, util_quit_loop, util_session_destroy);
295 }
296
297 static gboolean test_session_disconnect(gpointer data)
298 {
299         struct test_fix *fix = data;
300         struct test_session *session;
301         DBusMessage *msg;
302
303         util_session_create(fix, 1);
304         session = fix->session;
305
306         session->notify_path = g_strdup("/foo");
307         session->notify =  test_session_disconnect_notify;
308         util_session_init(session);
309
310         msg = session_disconnect(session->connection, session);
311         g_assert(msg != NULL);
312         dbus_message_unref(msg);
313
314         return FALSE;
315 }
316
317 static void test_session_connect_disconnect_notify(struct test_session *session)
318 {
319         DBusMessage *msg;
320
321         LOG("session %p online %d", session, session->info->online);
322
323         if (session->info->online != TRUE)
324                 return;
325
326         msg = session_disconnect(session->connection, session);
327         g_assert(msg != NULL);
328         dbus_message_unref(msg);
329
330         util_session_cleanup(session);
331
332         g_assert(is_connman_running(session->connection) == TRUE);
333         util_idle_call(session->fix, util_quit_loop, util_session_destroy);
334 }
335
336 static gboolean test_session_connect_disconnect(gpointer data)
337 {
338         struct test_fix *fix = data;
339         struct test_session *session;
340         DBusMessage *msg;
341
342         util_session_create(fix, 1);
343         session = fix->session;
344
345         session->notify_path = g_strdup("/foo");
346         session->notify =  test_session_connect_disconnect_notify;
347         util_session_init(session);
348
349         msg = session_connect(session->connection, session);
350         g_assert(msg != NULL);
351         g_assert(dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_ERROR);
352
353         dbus_message_unref(msg);
354
355         return FALSE;
356 }
357
358 static void test_session_connect_free_ride_notify(struct test_session *session)
359 {
360         struct test_session *session0 = get_session(session, 0);
361         struct test_session *session1 = get_session(session, 1);
362         enum test_session_state state = get_session_state(session);
363         enum test_session_state next_state = state;
364         DBusMessage *msg;
365
366         LOG("state %d session %p %s online %d", state, session,
367                 session->notify_path, session->info->online);
368
369         switch (state) {
370         case TEST_SESSION_STATE_0:
371                 if (session0->info->online == FALSE &&
372                                 session1->info->online == FALSE) {
373                         next_state = TEST_SESSION_STATE_1;
374                 }
375
376                 break;
377         case TEST_SESSION_STATE_1:
378                 if (session0->info->online == TRUE &&
379                                 session1->info->online == TRUE) {
380                         next_state = TEST_SESSION_STATE_2;
381                 }
382
383                 break;
384         case TEST_SESSION_STATE_2:
385                 if (session0->info->online == FALSE &&
386                                 session1->info->online == FALSE) {
387                         next_state = TEST_SESSION_STATE_3;
388                 }
389
390                 break;
391         case TEST_SESSION_STATE_3:
392
393                 return;
394         }
395
396         if (state == next_state)
397                 return;
398
399         set_session_state(session, next_state);
400
401         LOG("next_state %d", next_state);
402
403         switch (next_state) {
404         case TEST_SESSION_STATE_0:
405
406                 return;
407         case TEST_SESSION_STATE_1:
408                 msg = session_connect(session0->connection, session0);
409                 g_assert(msg != NULL);
410                 dbus_message_unref(msg);
411
412                 return;
413
414         case TEST_SESSION_STATE_2:
415                 msg = session_disconnect(session0->connection, session0);
416                 g_assert(msg != NULL);
417                 dbus_message_unref(msg);
418
419                 return;
420         case TEST_SESSION_STATE_3:
421                 util_session_cleanup(session0);
422                 util_session_cleanup(session1);
423
424                 g_assert(is_connman_running(session0->connection) == TRUE);
425                 util_idle_call(session0->fix, util_quit_loop,
426                                 util_session_destroy);
427
428                 return;
429         }
430 }
431
432 static gboolean test_session_connect_free_ride(gpointer data)
433 {
434         struct test_fix *fix = data;
435         struct test_session *session0, *session1;
436
437         /*
438          * +-------------------+
439          * |       START       |
440          * +-------------------+
441          *   |
442          *   | connect foo
443          *   v
444          * +-------------------+
445          * |   FOO-CONNECTED   |
446          * +-------------------+
447          *   |
448          *   | free-ride bar
449          *   v
450          * +-------------------+
451          * | FOO-BAR-CONNECTED |
452          * +-------------------+
453          *  |
454          *  | disconnect foo
455          *  v
456          * +-------------------+
457          * |        END        |
458          * +-------------------+
459          */
460
461         util_session_create(fix, 2);
462         session0 = &fix->session[0];
463         session1 = &fix->session[1];
464
465         session0->notify_path = g_strdup("/foo");
466         session1->notify_path = g_strdup("/bar");
467         session0->notify = test_session_connect_free_ride_notify;
468         session1->notify = test_session_connect_free_ride_notify;
469
470         util_session_init(session0);
471         util_session_init(session1);
472
473         set_session_state(session0, TEST_SESSION_STATE_0);
474
475         return FALSE;
476 }
477
478 static gboolean enable_session_mode(gpointer data)
479 {
480         struct test_fix *fix = data;
481
482         set_session_mode(fix, TRUE);
483
484         return FALSE;
485 }
486
487 static gboolean disable_session_mode(gpointer data)
488 {
489         struct test_fix *fix = data;
490
491         set_session_mode(fix, FALSE);
492
493         return FALSE;
494 }
495
496 static void setup_cb(struct test_fix *fix, gconstpointer data)
497 {
498         util_setup(fix, data);
499
500         util_call(fix, enable_session_mode, NULL);
501         g_main_loop_run(fix->main_loop);
502 }
503
504 static void teardown_cb(struct test_fix *fix, gconstpointer data)
505 {
506         util_call(fix, disable_session_mode, NULL);
507         g_main_loop_run(fix->main_loop);
508
509         util_teardown(fix, data);
510 }
511
512 int main(int argc, char *argv[])
513 {
514         g_test_init(&argc, &argv, NULL);
515
516         util_test_add("/manager/session create no notify",
517                 test_session_create_no_notify, setup_cb, teardown_cb);
518         util_test_add("/manager/session destroy no notify",
519                 test_session_destroy_no_notify, setup_cb, teardown_cb);
520         util_test_add("/manager/session create",
521                 test_session_create, setup_cb, teardown_cb);
522         util_test_add("/manager/session create destroy",
523                 test_session_create_destroy, setup_cb, teardown_cb);
524         util_test_add("/manager/session create already exists",
525                 test_session_create_already_exists, setup_cb, teardown_cb);
526         util_test_add("/manager/session create many",
527                 test_session_create_many, setup_cb, teardown_cb);
528
529         util_test_add("/session/connect",
530                 test_session_connect, setup_cb, teardown_cb);
531         util_test_add("/session/disconnect",
532                 test_session_disconnect, setup_cb, teardown_cb);
533         util_test_add("/session/connect disconnect",
534                 test_session_connect_disconnect, setup_cb, teardown_cb);
535         util_test_add("/session/connect free-ride",
536                 test_session_connect_free_ride, setup_cb, teardown_cb);
537
538         return g_test_run();
539 }