tizen 2.3 release
[framework/connectivity/bluez.git] / unit / test-mgmt.c
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2012  Intel Corporation. All rights reserved.
6  *
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdbool.h>
29 #include <unistd.h>
30 #include <sys/socket.h>
31
32 #include <glib.h>
33
34 #include "lib/bluetooth.h"
35 #include "lib/mgmt.h"
36
37 #include "src/shared/mgmt.h"
38
39 struct context {
40         GMainLoop *main_loop;
41         struct mgmt *mgmt_client;
42         guint server_source;
43         GList *handler_list;
44 };
45
46 enum action {
47         ACTION_PASSED,
48         ACTION_IGNORE,
49 };
50
51 struct handler {
52         const void *cmd_data;
53         uint16_t cmd_size;
54         bool match_prefix;
55         enum action action;
56 };
57
58 static void mgmt_debug(const char *str, void *user_data)
59 {
60         const char *prefix = user_data;
61
62         g_print("%s%s\n", prefix, str);
63 }
64
65 static void context_quit(struct context *context)
66 {
67         g_main_loop_quit(context->main_loop);
68 }
69
70 static void check_actions(struct context *context,
71                                         const void *data, uint16_t size)
72 {
73         GList *list;
74
75         for (list = g_list_first(context->handler_list); list;
76                                                 list = g_list_next(list)) {
77                 struct handler *handler = list->data;
78
79                 if (handler->match_prefix) {
80                         if (size < handler->cmd_size)
81                                 continue;
82                 } else {
83                         if (size != handler->cmd_size)
84                                 continue;
85                 }
86
87                 if (memcmp(data, handler->cmd_data, handler->cmd_size))
88                         continue;
89
90                 switch (handler->action) {
91                 case ACTION_PASSED:
92                         context_quit(context);
93                         return;
94                 case ACTION_IGNORE:
95                         return;
96                 }
97         }
98
99         g_test_message("Command not handled\n");
100         g_assert_not_reached();
101 }
102
103 static gboolean server_handler(GIOChannel *channel, GIOCondition cond,
104                                                         gpointer user_data)
105 {
106         struct context *context = user_data;
107         unsigned char buf[512];
108         ssize_t result;
109         int fd;
110
111         if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
112                 return FALSE;
113
114         fd = g_io_channel_unix_get_fd(channel);
115
116         result = read(fd, buf, sizeof(buf));
117         if (result < 0)
118                 return FALSE;
119
120         check_actions(context, buf, result);
121
122         return TRUE;
123 }
124
125 static struct context *create_context(void)
126 {
127         struct context *context = g_new0(struct context, 1);
128         GIOChannel *channel;
129         int err, sv[2];
130
131         context->main_loop = g_main_loop_new(NULL, FALSE);
132         g_assert(context->main_loop);
133
134         err = socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, sv);
135         g_assert(err == 0);
136
137         channel = g_io_channel_unix_new(sv[0]);
138
139         g_io_channel_set_close_on_unref(channel, TRUE);
140         g_io_channel_set_encoding(channel, NULL, NULL);
141         g_io_channel_set_buffered(channel, FALSE);
142
143         context->server_source = g_io_add_watch(channel,
144                                 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
145                                 server_handler, context);
146         g_assert(context->server_source > 0);
147
148         g_io_channel_unref(channel);
149
150         context->mgmt_client = mgmt_new(sv[1]);
151         g_assert(context->mgmt_client);
152
153         if (g_test_verbose() == TRUE)
154                 mgmt_set_debug(context->mgmt_client,
155                                         mgmt_debug, "mgmt: ", NULL);
156
157         mgmt_set_close_on_unref(context->mgmt_client, true);
158
159         return context;
160 }
161
162 static void execute_context(struct context *context)
163 {
164         g_main_loop_run(context->main_loop);
165
166         g_list_free_full(context->handler_list, g_free);
167
168         g_source_remove(context->server_source);
169
170         mgmt_unref(context->mgmt_client);
171
172         g_main_loop_unref(context->main_loop);
173
174         g_free(context);
175 }
176
177 static void add_action(struct context *context,
178                                 const void *cmd_data, uint16_t cmd_size,
179                                 bool match_prefix, enum action action)
180 {
181         struct handler *handler = g_new0(struct handler, 1);
182
183         handler->cmd_data = cmd_data;
184         handler->cmd_size = cmd_size;
185         handler->match_prefix = match_prefix;
186         handler->action = action;
187
188         context->handler_list = g_list_append(context->handler_list, handler);
189 }
190
191 struct command_test_data {
192         uint16_t opcode;
193         uint16_t index;
194         uint16_t length;
195         const void *param;
196         const void *cmd_data;
197         uint16_t cmd_size;
198 };
199
200 static const unsigned char read_version_command[] =
201                                 { 0x01, 0x00, 0xff, 0xff, 0x00, 0x00 };
202
203 static const struct command_test_data command_test_1 = {
204         .opcode = MGMT_OP_READ_VERSION,
205         .index = MGMT_INDEX_NONE,
206         .cmd_data = read_version_command,
207         .cmd_size = sizeof(read_version_command),
208 };
209
210 static const unsigned char read_info_command[] =
211                                 { 0x04, 0x00, 0x00, 0x02, 0x00, 0x00 };
212
213 static const struct command_test_data command_test_2 = {
214         .opcode = MGMT_OP_READ_INFO,
215         .index = 512,
216         .cmd_data = read_info_command,
217         .cmd_size = sizeof(read_info_command),
218 };
219
220 static void test_command(gconstpointer data)
221 {
222         const struct command_test_data *test = data;
223         struct context *context = create_context();
224
225         add_action(context, test->cmd_data, test->cmd_size,
226                                                 false, ACTION_PASSED);
227
228         mgmt_send(context->mgmt_client, test->opcode, test->index,
229                                         test->length, test->param,
230                                                 NULL ,NULL, NULL);
231
232         execute_context(context);
233 }
234
235 int main(int argc, char *argv[])
236 {
237         g_test_init(&argc, &argv, NULL);
238
239         g_test_add_data_func("/mgmt/command/1", &command_test_1, test_command);
240         g_test_add_data_func("/mgmt/command/2", &command_test_2, test_command);
241
242         return g_test_run();
243 }