3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2014 Google, Inc.
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.
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.
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
34 #include <sys/socket.h>
38 #include "src/shared/util.h"
39 #include "lib/bluetooth.h"
41 #include "attrib/att.h"
42 #include "attrib/gattrib.h"
45 #define DEFAULT_MTU 23
47 #define data(args...) ((const unsigned char[]) { args })
57 #define pdu(args...) \
63 .size = sizeof(data(args)), \
69 GIOChannel *server_io;
73 static void setup_context(struct context *cxt, gconstpointer data)
77 cxt->main_loop = g_main_loop_new(NULL, FALSE);
78 g_assert(cxt->main_loop != NULL);
80 err = socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, sv);
83 cxt->att_io = g_io_channel_unix_new(sv[0]);
84 g_assert(cxt->att_io != NULL);
86 g_io_channel_set_close_on_unref(cxt->att_io, TRUE);
88 cxt->server_io = g_io_channel_unix_new(sv[1]);
89 g_assert(cxt->server_io != NULL);
91 g_io_channel_set_close_on_unref(cxt->server_io, TRUE);
92 g_io_channel_set_encoding(cxt->server_io, NULL, NULL);
93 g_io_channel_set_buffered(cxt->server_io, FALSE);
95 cxt->att = g_attrib_new(cxt->att_io, DEFAULT_MTU, false);
96 g_assert(cxt->att != NULL);
99 static void teardown_context(struct context *cxt, gconstpointer data)
102 g_attrib_unref(cxt->att);
104 g_io_channel_unref(cxt->server_io);
106 g_io_channel_unref(cxt->att_io);
108 g_main_loop_unref(cxt->main_loop);
112 static void test_debug(const char *str, void *user_data)
114 const char *prefix = user_data;
116 g_print("%s%s\n", prefix, str);
119 static void destroy_canary_increment(gpointer data)
125 static void test_refcount(struct context *cxt, gconstpointer unused)
128 int destroy_canary = 0;
130 g_attrib_set_destroy_function(cxt->att, destroy_canary_increment,
133 extra_ref = g_attrib_ref(cxt->att);
135 g_assert(extra_ref == cxt->att);
137 g_assert(destroy_canary == 0);
139 g_attrib_unref(extra_ref);
141 g_assert(destroy_canary == 0);
143 g_attrib_unref(cxt->att);
145 g_assert(destroy_canary == 1);
147 /* Avoid a double-free from the teardown function */
151 static void test_get_channel(struct context *cxt, gconstpointer unused)
155 chan = g_attrib_get_channel(cxt->att);
157 g_assert(chan == cxt->att_io);
160 struct expect_response {
161 struct test_pdu expect;
162 struct test_pdu respond;
163 GSourceFunc receive_cb;
167 static gboolean test_client(GIOChannel *channel, GIOCondition cond,
170 struct expect_response *cr = data;
176 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
179 fd = g_io_channel_unix_get_fd(channel);
181 len = read(fd, buf, sizeof(buf));
184 g_assert_cmpint(len, ==, cr->expect.size);
186 if (g_test_verbose())
187 util_hexdump('?', cr->expect.data, cr->expect.size,
188 test_debug, "test_client: ");
190 cmp = memcmp(cr->expect.data, buf, len);
194 cr->expect.received = true;
196 if (cr->receive_cb != NULL)
197 cr->receive_cb(cr->user_data);
199 if (cr->respond.valid) {
200 if (g_test_verbose())
201 util_hexdump('<', cr->respond.data, cr->respond.size,
202 test_debug, "test_client: ");
203 len = write(fd, cr->respond.data, cr->respond.size);
205 g_assert_cmpint(len, ==, cr->respond.size);
207 cr->respond.sent = true;
217 GSourceFunc complete_cb;
221 static void result_canary(guint8 status, const guint8 *pdu, guint16 len,
224 struct result_data *result = data;
226 result->status = status;
227 result->pdu = g_malloc0(len);
228 memcpy(result->pdu, pdu, len);
231 if (g_test_verbose())
232 util_hexdump('<', pdu, len, test_debug, "result_canary: ");
234 if (result->complete_cb != NULL)
235 result->complete_cb(result->user_data);
238 static gboolean context_stop_main_loop(gpointer user_data)
240 struct context *cxt = user_data;
242 g_main_loop_quit(cxt->main_loop);
246 static void test_send(struct context *cxt, gconstpointer unused)
249 struct result_data results;
250 struct expect_response data = {
251 .expect = pdu(0x02, 0x00, 0x02),
252 .respond = pdu(0x03, 0x02, 0x03, 0x04),
257 g_io_add_watch(cxt->server_io, G_IO_IN | G_IO_HUP | G_IO_ERR |
258 G_IO_NVAL, test_client, &data);
260 results.complete_cb = context_stop_main_loop;
261 results.user_data = cxt;
263 g_attrib_send(cxt->att, 0, data.expect.data, data.expect.size,
264 result_canary, (gpointer) &results, NULL);
266 g_main_loop_run(cxt->main_loop);
268 g_assert(results.pdu != NULL);
270 g_assert_cmpint(results.len, ==, data.respond.size);
272 cmp = memcmp(results.pdu, data.respond.data, results.len);
280 struct context *context;
284 static gboolean cancel_existing_attrib_event(gpointer user_data)
286 struct event_info *info = user_data;
289 canceled = g_attrib_cancel(info->context->att, info->event_id);
293 g_idle_add(context_stop_main_loop, info->context);
298 static void test_cancel(struct context *cxt, gconstpointer unused)
301 struct result_data results;
302 struct event_info info;
303 struct expect_response data = {
304 .expect = pdu(0x02, 0x00, 0x02),
305 .respond = pdu(0x03, 0x02, 0x03, 0x04),
308 g_io_add_watch(cxt->server_io,
309 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
315 info.event_id = g_attrib_send(cxt->att, 0, data.expect.data,
316 data.expect.size, result_canary,
319 data.receive_cb = cancel_existing_attrib_event;
320 data.user_data = &info;
322 g_main_loop_run(cxt->main_loop);
324 g_assert(results.pdu == NULL);
327 data.expect.received = false;
328 data.respond.sent = false;
330 info.event_id = g_attrib_send(cxt->att, 0, data.expect.data,
331 data.expect.size, result_canary,
334 canceled = g_attrib_cancel(cxt->att, info.event_id);
337 g_idle_add(context_stop_main_loop, info.context);
339 g_main_loop_run(cxt->main_loop);
341 g_assert(!data.expect.received);
342 g_assert(!data.respond.sent);
343 g_assert(results.pdu == NULL);
346 canceled = g_attrib_cancel(cxt->att, 42);
350 static void send_test_pdus(gpointer context, struct test_pdu *pdus)
352 struct context *cxt = context;
355 struct test_pdu *cur_pdu;
357 fd = g_io_channel_unix_get_fd(cxt->server_io);
359 for (cur_pdu = pdus; cur_pdu->valid; cur_pdu++)
360 cur_pdu->sent = false;
362 for (cur_pdu = pdus; cur_pdu->valid; cur_pdu++) {
363 if (g_test_verbose())
364 util_hexdump('>', cur_pdu->data, cur_pdu->size,
365 test_debug, "send_test_pdus: ");
366 len = write(fd, cur_pdu->data, cur_pdu->size);
367 g_assert_cmpint(len, ==, cur_pdu->size);
368 cur_pdu->sent = true;
371 g_idle_add(context_stop_main_loop, cxt);
372 g_main_loop_run(cxt->main_loop);
375 #define PDU_MTU_RESP pdu(ATT_OP_MTU_RESP, 0x17)
376 #define PDU_FIND_INFO_REQ pdu(ATT_OP_FIND_INFO_REQ, 0x01, 0x00, 0xFF, 0xFF)
377 #define PDU_NO_ATT_ERR pdu(ATT_OP_ERROR, ATT_OP_FIND_INFO_REQ, 0x00, 0x00, 0x0A)
378 #define PDU_IND_NODATA pdu(ATT_OP_HANDLE_IND, 0x01, 0x00)
379 #define PDU_INVALID_IND pdu(ATT_OP_HANDLE_IND, 0x14)
380 #define PDU_IND_DATA pdu(ATT_OP_HANDLE_IND, 0x14, 0x00, 0x01)
382 struct expect_test_data {
383 struct test_pdu *expected;
387 static void notify_canary_expect(const guint8 *pdu, guint16 len, gpointer data)
389 struct expect_test_data *expect = data;
390 struct test_pdu *expected = expect->expected;
393 if (g_test_verbose())
394 util_hexdump('<', pdu, len, test_debug,
395 "notify_canary_expect: ");
397 while (expected->valid && expected->received)
400 g_assert(expected->valid);
402 if (g_test_verbose())
403 util_hexdump('?', expected->data, expected->size, test_debug,
404 "notify_canary_expect: ");
406 g_assert_cmpint(expected->size, ==, len);
408 cmp = memcmp(pdu, expected->data, expected->size);
412 expected->received = true;
414 if (pdu[0] == ATT_OP_FIND_INFO_REQ) {
415 struct test_pdu no_attributes = PDU_NO_ATT_ERR;
418 reqid = g_attrib_send(expect->att, 0, no_attributes.data,
419 no_attributes.size, NULL, NULL, NULL);
420 g_assert(reqid != 0);
424 static void test_register(struct context *cxt, gconstpointer user_data)
428 struct test_pdu pdus[] = {
430 * Unmatched PDU opcode
431 * Unmatched handle (GATTRIB_ALL_REQS) */
435 * Unmatched handle (GATTRIB_ALL_HANDLES) */
447 struct test_pdu req_pdus[] = { PDU_FIND_INFO_REQ, { } };
448 struct test_pdu all_ind_pdus[] = {
454 struct test_pdu followed_ind_pdus[] = { PDU_IND_DATA, { } };
455 struct test_pdu *current_pdu;
456 struct expect_test_data expect;
458 expect.att = cxt->att;
461 * Without registering anything, should be able to ignore everything but
462 * an unexpected response. */
463 send_test_pdus(cxt, pdus);
465 if (g_test_verbose())
466 g_print("ALL_REQS, ALL_HANDLES\r\n");
468 expect.expected = req_pdus;
469 reg_id = g_attrib_register(cxt->att, GATTRIB_ALL_REQS,
470 GATTRIB_ALL_HANDLES, notify_canary_expect,
473 send_test_pdus(cxt, pdus);
475 canceled = g_attrib_unregister(cxt->att, reg_id);
479 for (current_pdu = req_pdus; current_pdu->valid; current_pdu++)
480 g_assert(current_pdu->received);
482 if (g_test_verbose())
483 g_print("IND, ALL_HANDLES\r\n");
485 expect.expected = all_ind_pdus;
486 reg_id = g_attrib_register(cxt->att, ATT_OP_HANDLE_IND,
487 GATTRIB_ALL_HANDLES, notify_canary_expect,
490 send_test_pdus(cxt, pdus);
492 canceled = g_attrib_unregister(cxt->att, reg_id);
496 for (current_pdu = all_ind_pdus; current_pdu->valid; current_pdu++)
497 g_assert(current_pdu->received);
499 if (g_test_verbose())
500 g_print("IND, 0x0014\r\n");
502 expect.expected = followed_ind_pdus;
503 reg_id = g_attrib_register(cxt->att, ATT_OP_HANDLE_IND, 0x0014,
504 notify_canary_expect, &expect, NULL);
506 send_test_pdus(cxt, pdus);
508 canceled = g_attrib_unregister(cxt->att, reg_id);
512 for (current_pdu = followed_ind_pdus; current_pdu->valid; current_pdu++)
513 g_assert(current_pdu->received);
515 canceled = g_attrib_unregister(cxt->att, reg_id);
520 static void test_buffers(struct context *cxt, gconstpointer unused)
526 buf = g_attrib_get_buffer(cxt->att, &buflen);
528 g_assert_cmpint(buflen, ==, DEFAULT_MTU);
530 success = g_attrib_set_mtu(cxt->att, 5);
533 success = g_attrib_set_mtu(cxt->att, 255);
536 buf = g_attrib_get_buffer(cxt->att, &buflen);
538 g_assert_cmpint(buflen, ==, 255);
541 int main(int argc, char *argv[])
543 g_test_init(&argc, &argv, NULL);
545 if (g_test_verbose())
546 __btd_log_init("*", 0);
549 * Test the GAttrib API behavior
551 g_test_add("/gattrib/refcount", struct context, NULL, setup_context,
552 test_refcount, teardown_context);
553 g_test_add("/gattrib/get_channel", struct context, NULL, setup_context,
554 test_get_channel, teardown_context);
555 g_test_add("/gattrib/send", struct context, NULL, setup_context,
556 test_send, teardown_context);
557 g_test_add("/gattrib/cancel", struct context, NULL, setup_context,
558 test_cancel, teardown_context);
559 g_test_add("/gattrib/register", struct context, NULL, setup_context,
560 test_register, teardown_context);
561 g_test_add("/gattrib/buffers", struct context, NULL, setup_context,
562 test_buffers, teardown_context);