Upgrade bluez5_37 :Merge the code from private
[platform/upstream/bluez.git] / unit / test-gattrib.c
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2014  Google, Inc.
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 <unistd.h>
29 #include <stdlib.h>
30 #include <stdbool.h>
31 #include <inttypes.h>
32 #include <string.h>
33 #include <fcntl.h>
34 #include <sys/socket.h>
35
36 #include <glib.h>
37
38 #include "src/shared/util.h"
39 #include "lib/bluetooth.h"
40 #include "lib/uuid.h"
41 #include "attrib/att.h"
42 #include "attrib/gattrib.h"
43 #include "src/log.h"
44
45 #define DEFAULT_MTU 23
46
47 #define data(args...) ((const unsigned char[]) { args })
48
49 struct test_pdu {
50         bool valid;
51         bool sent;
52         bool received;
53         const uint8_t *data;
54         size_t size;
55 };
56
57 #define pdu(args...)                            \
58         {                                       \
59                 .valid = true,                  \
60                 .sent = false,                  \
61                 .received = false,              \
62                 .data = data(args),             \
63                 .size = sizeof(data(args)),     \
64         }
65
66 struct context {
67         GMainLoop *main_loop;
68         GIOChannel *att_io;
69         GIOChannel *server_io;
70         GAttrib *att;
71 };
72
73 static void setup_context(struct context *cxt, gconstpointer data)
74 {
75         int err, sv[2];
76
77         cxt->main_loop = g_main_loop_new(NULL, FALSE);
78         g_assert(cxt->main_loop != NULL);
79
80         err = socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, sv);
81         g_assert(err == 0);
82
83         cxt->att_io = g_io_channel_unix_new(sv[0]);
84         g_assert(cxt->att_io != NULL);
85
86         g_io_channel_set_close_on_unref(cxt->att_io, TRUE);
87
88         cxt->server_io = g_io_channel_unix_new(sv[1]);
89         g_assert(cxt->server_io != NULL);
90
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);
94
95         cxt->att = g_attrib_new(cxt->att_io, DEFAULT_MTU, false);
96         g_assert(cxt->att != NULL);
97 }
98
99 static void teardown_context(struct context *cxt, gconstpointer data)
100 {
101         if (cxt->att)
102                 g_attrib_unref(cxt->att);
103
104         g_io_channel_unref(cxt->server_io);
105
106         g_io_channel_unref(cxt->att_io);
107
108         g_main_loop_unref(cxt->main_loop);
109 }
110
111
112 static void test_debug(const char *str, void *user_data)
113 {
114         const char *prefix = user_data;
115
116         g_print("%s%s\n", prefix, str);
117 }
118
119 static void destroy_canary_increment(gpointer data)
120 {
121         int *canary = data;
122         (*canary)++;
123 }
124
125 static void test_refcount(struct context *cxt, gconstpointer unused)
126 {
127         GAttrib *extra_ref;
128         int destroy_canary = 0;
129
130         g_attrib_set_destroy_function(cxt->att, destroy_canary_increment,
131                                                                &destroy_canary);
132
133         extra_ref = g_attrib_ref(cxt->att);
134
135         g_assert(extra_ref == cxt->att);
136
137         g_assert(destroy_canary == 0);
138
139         g_attrib_unref(extra_ref);
140
141         g_assert(destroy_canary == 0);
142
143         g_attrib_unref(cxt->att);
144
145         g_assert(destroy_canary == 1);
146
147         /* Avoid a double-free from the teardown function */
148         cxt->att = NULL;
149 }
150
151 static void test_get_channel(struct context *cxt, gconstpointer unused)
152 {
153         GIOChannel *chan;
154
155         chan = g_attrib_get_channel(cxt->att);
156
157         g_assert(chan == cxt->att_io);
158 }
159
160 struct expect_response {
161         struct test_pdu expect;
162         struct test_pdu respond;
163         GSourceFunc receive_cb;
164         gpointer user_data;
165 };
166
167 static gboolean test_client(GIOChannel *channel, GIOCondition cond,
168                                                                   gpointer data)
169 {
170         struct expect_response *cr = data;
171         int fd;
172         uint8_t buf[256];
173         ssize_t len;
174         int cmp;
175
176         if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
177                 return FALSE;
178
179         fd = g_io_channel_unix_get_fd(channel);
180
181         len = read(fd, buf, sizeof(buf));
182
183         g_assert(len > 0);
184         g_assert_cmpint(len, ==, cr->expect.size);
185
186         if (g_test_verbose())
187                 util_hexdump('?', cr->expect.data,  cr->expect.size,
188                                                    test_debug, "test_client: ");
189
190         cmp = memcmp(cr->expect.data, buf, len);
191
192         g_assert(cmp == 0);
193
194         cr->expect.received = true;
195
196         if (cr->receive_cb != NULL)
197                 cr->receive_cb(cr->user_data);
198
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);
204
205                 g_assert_cmpint(len, ==, cr->respond.size);
206
207                 cr->respond.sent = true;
208         }
209
210         return TRUE;
211 }
212
213 struct result_data {
214         guint8 status;
215         guint8 *pdu;
216         guint16 len;
217         GSourceFunc complete_cb;
218         gpointer user_data;
219 };
220
221 static void result_canary(guint8 status, const guint8 *pdu, guint16 len,
222                                                                 gpointer data)
223 {
224         struct result_data *result = data;
225
226         result->status = status;
227         result->pdu = g_malloc0(len);
228         memcpy(result->pdu, pdu, len);
229         result->len = len;
230
231         if (g_test_verbose())
232                 util_hexdump('<', pdu, len, test_debug, "result_canary: ");
233
234         if (result->complete_cb != NULL)
235                 result->complete_cb(result->user_data);
236 }
237
238 static gboolean context_stop_main_loop(gpointer user_data)
239 {
240         struct context *cxt = user_data;
241
242         g_main_loop_quit(cxt->main_loop);
243         return FALSE;
244 }
245
246 static void test_send(struct context *cxt, gconstpointer unused)
247 {
248         int cmp;
249         struct result_data results;
250         struct expect_response data = {
251                 .expect = pdu(0x02, 0x00, 0x02),
252                 .respond = pdu(0x03, 0x02, 0x03, 0x04),
253                 .receive_cb = NULL,
254                 .user_data = NULL,
255         };
256
257         g_io_add_watch(cxt->server_io, G_IO_IN | G_IO_HUP | G_IO_ERR |
258                                                 G_IO_NVAL, test_client, &data);
259
260         results.complete_cb = context_stop_main_loop;
261         results.user_data = cxt;
262
263         g_attrib_send(cxt->att, 0, data.expect.data, data.expect.size,
264                                       result_canary, (gpointer) &results, NULL);
265
266         g_main_loop_run(cxt->main_loop);
267
268         g_assert(results.pdu != NULL);
269
270         g_assert_cmpint(results.len, ==, data.respond.size);
271
272         cmp = memcmp(results.pdu, data.respond.data, results.len);
273
274         g_assert(cmp == 0);
275
276         g_free(results.pdu);
277 }
278
279 struct event_info {
280         struct context *context;
281         int event_id;
282 };
283
284 static gboolean cancel_existing_attrib_event(gpointer user_data)
285 {
286         struct event_info *info = user_data;
287         gboolean canceled;
288
289         canceled = g_attrib_cancel(info->context->att, info->event_id);
290
291         g_assert(canceled);
292
293         g_idle_add(context_stop_main_loop, info->context);
294
295         return FALSE;
296 }
297
298 static void test_cancel(struct context *cxt, gconstpointer unused)
299 {
300         gboolean canceled;
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),
306         };
307
308         g_io_add_watch(cxt->server_io,
309                                       G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
310                                                             test_client, &data);
311
312         results.pdu = NULL;
313
314         info.context = cxt;
315         info.event_id = g_attrib_send(cxt->att, 0, data.expect.data,
316                                                 data.expect.size, result_canary,
317                                                                 &results, NULL);
318
319         data.receive_cb = cancel_existing_attrib_event;
320         data.user_data = &info;
321
322         g_main_loop_run(cxt->main_loop);
323
324         g_assert(results.pdu == NULL);
325
326         results.pdu = NULL;
327         data.expect.received = false;
328         data.respond.sent = false;
329
330         info.event_id = g_attrib_send(cxt->att, 0, data.expect.data,
331                                                 data.expect.size, result_canary,
332                                                                 &results, NULL);
333
334         canceled = g_attrib_cancel(cxt->att, info.event_id);
335         g_assert(canceled);
336
337         g_idle_add(context_stop_main_loop, info.context);
338
339         g_main_loop_run(cxt->main_loop);
340
341         g_assert(!data.expect.received);
342         g_assert(!data.respond.sent);
343         g_assert(results.pdu == NULL);
344
345         /* Invalid ID */
346         canceled = g_attrib_cancel(cxt->att, 42);
347         g_assert(!canceled);
348 }
349
350 static void send_test_pdus(gpointer context, struct test_pdu *pdus)
351 {
352         struct context *cxt = context;
353         size_t len;
354         int fd;
355         struct test_pdu *cur_pdu;
356
357         fd = g_io_channel_unix_get_fd(cxt->server_io);
358
359         for (cur_pdu = pdus; cur_pdu->valid; cur_pdu++)
360                 cur_pdu->sent = false;
361
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;
369         }
370
371         g_idle_add(context_stop_main_loop, cxt);
372         g_main_loop_run(cxt->main_loop);
373 }
374
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)
381
382 struct expect_test_data {
383         struct test_pdu *expected;
384         GAttrib *att;
385 };
386
387 static void notify_canary_expect(const guint8 *pdu, guint16 len, gpointer data)
388 {
389         struct expect_test_data *expect = data;
390         struct test_pdu *expected = expect->expected;
391         int cmp;
392
393         if (g_test_verbose())
394                 util_hexdump('<', pdu, len, test_debug,
395                                                       "notify_canary_expect: ");
396
397         while (expected->valid && expected->received)
398                 expected++;
399
400         g_assert(expected->valid);
401
402         if (g_test_verbose())
403                 util_hexdump('?', expected->data, expected->size, test_debug,
404                                                       "notify_canary_expect: ");
405
406         g_assert_cmpint(expected->size, ==, len);
407
408         cmp = memcmp(pdu, expected->data, expected->size);
409
410         g_assert(cmp == 0);
411
412         expected->received = true;
413
414         if (pdu[0] == ATT_OP_FIND_INFO_REQ) {
415                 struct test_pdu no_attributes = PDU_NO_ATT_ERR;
416                 int reqid;
417
418                 reqid = g_attrib_send(expect->att, 0, no_attributes.data,
419                                           no_attributes.size, NULL, NULL, NULL);
420                 g_assert(reqid != 0);
421         }
422 }
423
424 static void test_register(struct context *cxt, gconstpointer user_data)
425 {
426         guint reg_id;
427         gboolean canceled;
428         struct test_pdu pdus[] = {
429                 /*
430                  * Unmatched PDU opcode
431                  * Unmatched handle (GATTRIB_ALL_REQS) */
432                 PDU_FIND_INFO_REQ,
433                 /*
434                  * Matched PDU opcode
435                  * Unmatched handle (GATTRIB_ALL_HANDLES) */
436                 PDU_IND_NODATA,
437                 /*
438                  * Matched PDU opcode
439                  * Invalid length? */
440                 PDU_INVALID_IND,
441                 /*
442                  * Matched PDU opcode
443                  * Matched handle */
444                 PDU_IND_DATA,
445                 { },
446         };
447         struct test_pdu req_pdus[] = { PDU_FIND_INFO_REQ, { } };
448         struct test_pdu all_ind_pdus[] = {
449                 PDU_IND_NODATA,
450                 PDU_INVALID_IND,
451                 PDU_IND_DATA,
452                 { },
453         };
454         struct test_pdu followed_ind_pdus[] = { PDU_IND_DATA, { } };
455         struct test_pdu *current_pdu;
456         struct expect_test_data expect;
457
458         expect.att = cxt->att;
459
460         /*
461          * Without registering anything, should be able to ignore everything but
462          * an unexpected response. */
463         send_test_pdus(cxt, pdus);
464
465         if (g_test_verbose())
466                 g_print("ALL_REQS, ALL_HANDLES\r\n");
467
468         expect.expected = req_pdus;
469         reg_id = g_attrib_register(cxt->att, GATTRIB_ALL_REQS,
470                                       GATTRIB_ALL_HANDLES, notify_canary_expect,
471                                                                  &expect, NULL);
472
473         send_test_pdus(cxt, pdus);
474
475         canceled = g_attrib_unregister(cxt->att, reg_id);
476
477         g_assert(canceled);
478
479         for (current_pdu = req_pdus; current_pdu->valid; current_pdu++)
480                 g_assert(current_pdu->received);
481
482         if (g_test_verbose())
483                 g_print("IND, ALL_HANDLES\r\n");
484
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,
488                                                                  &expect, NULL);
489
490         send_test_pdus(cxt, pdus);
491
492         canceled = g_attrib_unregister(cxt->att, reg_id);
493
494         g_assert(canceled);
495
496         for (current_pdu = all_ind_pdus; current_pdu->valid; current_pdu++)
497                 g_assert(current_pdu->received);
498
499         if (g_test_verbose())
500                 g_print("IND, 0x0014\r\n");
501
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);
505
506         send_test_pdus(cxt, pdus);
507
508         canceled = g_attrib_unregister(cxt->att, reg_id);
509
510         g_assert(canceled);
511
512         for (current_pdu = followed_ind_pdus; current_pdu->valid; current_pdu++)
513                 g_assert(current_pdu->received);
514
515         canceled = g_attrib_unregister(cxt->att, reg_id);
516
517         g_assert(!canceled);
518 }
519
520 static void test_buffers(struct context *cxt, gconstpointer unused)
521 {
522         size_t buflen;
523         uint8_t *buf;
524         gboolean success;
525
526         buf = g_attrib_get_buffer(cxt->att, &buflen);
527         g_assert(buf != 0);
528         g_assert_cmpint(buflen, ==, DEFAULT_MTU);
529
530         success = g_attrib_set_mtu(cxt->att, 5);
531         g_assert(!success);
532
533         success = g_attrib_set_mtu(cxt->att, 255);
534         g_assert(success);
535
536         buf = g_attrib_get_buffer(cxt->att, &buflen);
537         g_assert(buf != 0);
538         g_assert_cmpint(buflen, ==, 255);
539 }
540
541 int main(int argc, char *argv[])
542 {
543         g_test_init(&argc, &argv, NULL);
544
545         if (g_test_verbose())
546                 __btd_log_init("*", 0);
547
548         /*
549          * Test the GAttrib API behavior
550          */
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);
563
564         return g_test_run();
565 }