unit: Add create and destroy test case
[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 void set_session_mode(struct test_fix *fix,
142                                         connman_bool_t enable)
143 {
144         DBusMessage *msg;
145
146         msg = manager_set_session_mode(fix->main_connection, enable);
147         g_assert(msg != NULL);
148         g_assert(dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_ERROR);
149
150         dbus_message_unref(msg);
151
152         util_idle_call(fix, util_quit_loop, NULL);
153 }
154
155 static gboolean enable_session_mode(gpointer data)
156 {
157         struct test_fix *fix = data;
158
159         set_session_mode(fix, TRUE);
160
161         return FALSE;
162 }
163
164 static gboolean disable_session_mode(gpointer data)
165 {
166         struct test_fix *fix = data;
167
168         set_session_mode(fix, FALSE);
169
170         return FALSE;
171 }
172
173 static void setup_cb(struct test_fix *fix, gconstpointer data)
174 {
175         util_setup(fix, data);
176
177         util_call(fix, enable_session_mode, NULL);
178         g_main_loop_run(fix->main_loop);
179 }
180
181 static void teardown_cb(struct test_fix *fix, gconstpointer data)
182 {
183         util_call(fix, disable_session_mode, NULL);
184         g_main_loop_run(fix->main_loop);
185
186         util_teardown(fix, data);
187 }
188
189 int main(int argc, char *argv[])
190 {
191         g_test_init(&argc, &argv, NULL);
192
193         util_test_add("/manager/session create no notify",
194                 test_session_create_no_notify, setup_cb, teardown_cb);
195         util_test_add("/manager/session destroy no notify",
196                 test_session_destroy_no_notify, setup_cb, teardown_cb);
197         util_test_add("/manager/session create",
198                 test_session_create, setup_cb, teardown_cb);
199         util_test_add("/manager/session create destroy",
200                 test_session_create_destroy, setup_cb, teardown_cb);
201
202         return g_test_run();
203 }