OSDN Git Service

tools/l2cap-tester: Fix NULL-dereference for tests with no data
[android-x86/external-bluetooth-bluez.git] / tools / l2cap-tester.c
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2013  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 <stdlib.h>
29 #include <unistd.h>
30 #include <errno.h>
31 #include <stdbool.h>
32
33 #include <glib.h>
34
35 #include "lib/bluetooth.h"
36 #include "lib/l2cap.h"
37 #include "lib/mgmt.h"
38
39 #include "monitor/bt.h"
40 #include "emulator/bthost.h"
41 #include "emulator/hciemu.h"
42
43 #include "src/shared/tester.h"
44 #include "src/shared/mgmt.h"
45
46 struct test_data {
47         const void *test_data;
48         struct mgmt *mgmt;
49         uint16_t mgmt_index;
50         struct hciemu *hciemu;
51         enum hciemu_type hciemu_type;
52         unsigned int io_id;
53         uint16_t handle;
54         uint16_t scid;
55         uint16_t dcid;
56         int sk;
57         int sk2;
58 };
59
60 struct l2cap_data {
61         uint16_t client_psm;
62         uint16_t server_psm;
63         uint16_t cid;
64         int expect_err;
65
66         uint8_t send_cmd_code;
67         const void *send_cmd;
68         uint16_t send_cmd_len;
69         uint8_t expect_cmd_code;
70         const void *expect_cmd;
71         uint16_t expect_cmd_len;
72
73         uint16_t data_len;
74         const void *read_data;
75         const void *write_data;
76
77         bool enable_ssp;
78         uint8_t client_io_cap;
79         int sec_level;
80         bool reject_ssp;
81
82         bool expect_pin;
83         uint8_t pin_len;
84         const void *pin;
85         uint8_t client_pin_len;
86         const void *client_pin;
87
88         bool addr_type_avail;
89         uint8_t addr_type;
90
91         uint8_t *client_bdaddr;
92         bool server_not_advertising;
93         bool direct_advertising;
94         bool close_one_socket;
95 };
96
97 static void mgmt_debug(const char *str, void *user_data)
98 {
99         const char *prefix = user_data;
100
101         tester_print("%s%s", prefix, str);
102 }
103
104 static void read_info_callback(uint8_t status, uint16_t length,
105                                         const void *param, void *user_data)
106 {
107         struct test_data *data = tester_get_data();
108         const struct mgmt_rp_read_info *rp = param;
109         char addr[18];
110         uint16_t manufacturer;
111         uint32_t supported_settings, current_settings;
112
113         tester_print("Read Info callback");
114         tester_print("  Status: 0x%02x", status);
115
116         if (status || !param) {
117                 tester_pre_setup_failed();
118                 return;
119         }
120
121         ba2str(&rp->bdaddr, addr);
122         manufacturer = btohs(rp->manufacturer);
123         supported_settings = btohl(rp->supported_settings);
124         current_settings = btohl(rp->current_settings);
125
126         tester_print("  Address: %s", addr);
127         tester_print("  Version: 0x%02x", rp->version);
128         tester_print("  Manufacturer: 0x%04x", manufacturer);
129         tester_print("  Supported settings: 0x%08x", supported_settings);
130         tester_print("  Current settings: 0x%08x", current_settings);
131         tester_print("  Class: 0x%02x%02x%02x",
132                         rp->dev_class[2], rp->dev_class[1], rp->dev_class[0]);
133         tester_print("  Name: %s", rp->name);
134         tester_print("  Short name: %s", rp->short_name);
135
136         if (strcmp(hciemu_get_address(data->hciemu), addr)) {
137                 tester_pre_setup_failed();
138                 return;
139         }
140
141         tester_pre_setup_complete();
142 }
143
144 static void index_added_callback(uint16_t index, uint16_t length,
145                                         const void *param, void *user_data)
146 {
147         struct test_data *data = tester_get_data();
148
149         tester_print("Index Added callback");
150         tester_print("  Index: 0x%04x", index);
151
152         data->mgmt_index = index;
153
154         mgmt_send(data->mgmt, MGMT_OP_READ_INFO, data->mgmt_index, 0, NULL,
155                                         read_info_callback, NULL, NULL);
156 }
157
158 static void index_removed_callback(uint16_t index, uint16_t length,
159                                         const void *param, void *user_data)
160 {
161         struct test_data *data = tester_get_data();
162
163         tester_print("Index Removed callback");
164         tester_print("  Index: 0x%04x", index);
165
166         if (index != data->mgmt_index)
167                 return;
168
169         mgmt_unregister_index(data->mgmt, data->mgmt_index);
170
171         mgmt_unref(data->mgmt);
172         data->mgmt = NULL;
173
174         tester_post_teardown_complete();
175 }
176
177 static void read_index_list_callback(uint8_t status, uint16_t length,
178                                         const void *param, void *user_data)
179 {
180         struct test_data *data = tester_get_data();
181
182         tester_print("Read Index List callback");
183         tester_print("  Status: 0x%02x", status);
184
185         if (status || !param) {
186                 tester_pre_setup_failed();
187                 return;
188         }
189
190         mgmt_register(data->mgmt, MGMT_EV_INDEX_ADDED, MGMT_INDEX_NONE,
191                                         index_added_callback, NULL, NULL);
192
193         mgmt_register(data->mgmt, MGMT_EV_INDEX_REMOVED, MGMT_INDEX_NONE,
194                                         index_removed_callback, NULL, NULL);
195
196         data->hciemu = hciemu_new(data->hciemu_type);
197         if (!data->hciemu) {
198                 tester_warn("Failed to setup HCI emulation");
199                 tester_pre_setup_failed();
200         }
201
202         tester_print("New hciemu instance created");
203 }
204
205 static void test_pre_setup(const void *test_data)
206 {
207         struct test_data *data = tester_get_data();
208
209         data->mgmt = mgmt_new_default();
210         if (!data->mgmt) {
211                 tester_warn("Failed to setup management interface");
212                 tester_pre_setup_failed();
213                 return;
214         }
215
216         if (tester_use_debug())
217                 mgmt_set_debug(data->mgmt, mgmt_debug, "mgmt: ", NULL);
218
219         mgmt_send(data->mgmt, MGMT_OP_READ_INDEX_LIST, MGMT_INDEX_NONE, 0, NULL,
220                                         read_index_list_callback, NULL, NULL);
221 }
222
223 static void test_post_teardown(const void *test_data)
224 {
225         struct test_data *data = tester_get_data();
226
227         if (data->io_id > 0) {
228                 g_source_remove(data->io_id);
229                 data->io_id = 0;
230         }
231
232         hciemu_unref(data->hciemu);
233         data->hciemu = NULL;
234 }
235
236 static void test_data_free(void *test_data)
237 {
238         struct test_data *data = test_data;
239
240         free(data);
241 }
242
243 #define test_l2cap_bredr(name, data, setup, func) \
244         do { \
245                 struct test_data *user; \
246                 user = malloc(sizeof(struct test_data)); \
247                 if (!user) \
248                         break; \
249                 user->hciemu_type = HCIEMU_TYPE_BREDR; \
250                 user->io_id = 0; \
251                 user->test_data = data; \
252                 tester_add_full(name, data, \
253                                 test_pre_setup, setup, func, NULL, \
254                                 test_post_teardown, 2, user, test_data_free); \
255         } while (0)
256
257 #define test_l2cap_le(name, data, setup, func) \
258         do { \
259                 struct test_data *user; \
260                 user = malloc(sizeof(struct test_data)); \
261                 if (!user) \
262                         break; \
263                 user->hciemu_type = HCIEMU_TYPE_LE; \
264                 user->io_id = 0; \
265                 user->test_data = data; \
266                 tester_add_full(name, data, \
267                                 test_pre_setup, setup, func, NULL, \
268                                 test_post_teardown, 2, user, test_data_free); \
269         } while (0)
270
271 static uint8_t pair_device_pin[] = { 0x30, 0x30, 0x30, 0x30 }; /* "0000" */
272
273 static const struct l2cap_data client_connect_success_test = {
274         .client_psm = 0x1001,
275         .server_psm = 0x1001,
276 };
277
278 static const struct l2cap_data client_connect_ssp_success_test_1 = {
279         .client_psm = 0x1001,
280         .server_psm = 0x1001,
281         .enable_ssp = true,
282 };
283
284 static const struct l2cap_data client_connect_ssp_success_test_2 = {
285         .client_psm = 0x1001,
286         .server_psm = 0x1001,
287         .enable_ssp = true,
288         .sec_level  = BT_SECURITY_HIGH,
289         .client_io_cap = 0x04,
290 };
291
292 static const struct l2cap_data client_connect_pin_success_test = {
293         .client_psm = 0x1001,
294         .server_psm = 0x1001,
295         .sec_level  = BT_SECURITY_MEDIUM,
296         .pin = pair_device_pin,
297         .pin_len = sizeof(pair_device_pin),
298         .client_pin = pair_device_pin,
299         .client_pin_len = sizeof(pair_device_pin),
300 };
301
302 static uint8_t l2_data[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
303
304 static const struct l2cap_data client_connect_read_success_test = {
305         .client_psm = 0x1001,
306         .server_psm = 0x1001,
307         .read_data = l2_data,
308         .data_len = sizeof(l2_data),
309 };
310
311 static const struct l2cap_data client_connect_write_success_test = {
312         .client_psm = 0x1001,
313         .server_psm = 0x1001,
314         .write_data = l2_data,
315         .data_len = sizeof(l2_data),
316 };
317
318 static const struct l2cap_data client_connect_nval_psm_test_1 = {
319         .client_psm = 0x1001,
320         .expect_err = ECONNREFUSED,
321 };
322
323 static const struct l2cap_data client_connect_nval_psm_test_2 = {
324         .client_psm = 0x0001,
325         .expect_err = ECONNREFUSED,
326 };
327
328 static const struct l2cap_data client_connect_nval_psm_test_3 = {
329         .client_psm = 0x0001,
330         .expect_err = ECONNREFUSED,
331         .enable_ssp = true,
332 };
333
334 static const uint8_t l2cap_connect_req[] = { 0x01, 0x10, 0x41, 0x00 };
335
336 static const struct l2cap_data l2cap_server_success_test = {
337         .server_psm = 0x1001,
338         .send_cmd_code = BT_L2CAP_PDU_CONN_REQ,
339         .send_cmd = l2cap_connect_req,
340         .send_cmd_len = sizeof(l2cap_connect_req),
341         .expect_cmd_code = BT_L2CAP_PDU_CONN_RSP,
342 };
343
344 static const struct l2cap_data l2cap_server_read_success_test = {
345         .server_psm = 0x1001,
346         .send_cmd_code = BT_L2CAP_PDU_CONN_REQ,
347         .send_cmd = l2cap_connect_req,
348         .send_cmd_len = sizeof(l2cap_connect_req),
349         .expect_cmd_code = BT_L2CAP_PDU_CONN_RSP,
350         .read_data = l2_data,
351         .data_len = sizeof(l2_data),
352 };
353
354 static const struct l2cap_data l2cap_server_write_success_test = {
355         .server_psm = 0x1001,
356         .send_cmd_code = BT_L2CAP_PDU_CONN_REQ,
357         .send_cmd = l2cap_connect_req,
358         .send_cmd_len = sizeof(l2cap_connect_req),
359         .expect_cmd_code = BT_L2CAP_PDU_CONN_RSP,
360         .write_data = l2_data,
361         .data_len = sizeof(l2_data),
362 };
363
364 static const uint8_t l2cap_sec_block_rsp[] = {  0x00, 0x00,     /* dcid */
365                                                 0x41, 0x00,     /* scid */
366                                                 0x03, 0x00,     /* Sec Block */
367                                                 0x00, 0x00      /* status */
368                                         };
369
370 static const struct l2cap_data l2cap_server_sec_block_test = {
371         .server_psm = 0x1001,
372         .send_cmd_code = BT_L2CAP_PDU_CONN_REQ,
373         .send_cmd = l2cap_connect_req,
374         .send_cmd_len = sizeof(l2cap_connect_req),
375         .expect_cmd_code = BT_L2CAP_PDU_CONN_RSP,
376         .expect_cmd = l2cap_sec_block_rsp,
377         .expect_cmd_len = sizeof(l2cap_sec_block_rsp),
378         .enable_ssp = true,
379 };
380
381 static const uint8_t l2cap_nval_psm_rsp[] = {   0x00, 0x00,     /* dcid */
382                                                 0x41, 0x00,     /* scid */
383                                                 0x02, 0x00,     /* nval PSM */
384                                                 0x00, 0x00      /* status */
385                                         };
386
387 static const struct l2cap_data l2cap_server_nval_psm_test = {
388         .send_cmd_code = BT_L2CAP_PDU_CONN_REQ,
389         .send_cmd = l2cap_connect_req,
390         .send_cmd_len = sizeof(l2cap_connect_req),
391         .expect_cmd_code = BT_L2CAP_PDU_CONN_RSP,
392         .expect_cmd = l2cap_nval_psm_rsp,
393         .expect_cmd_len = sizeof(l2cap_nval_psm_rsp),
394 };
395
396 static const uint8_t l2cap_nval_conn_req[] = { 0x00 };
397 static const uint8_t l2cap_nval_pdu_rsp[] = { 0x00, 0x00 };
398
399 static const struct l2cap_data l2cap_server_nval_pdu_test1 = {
400         .send_cmd_code = BT_L2CAP_PDU_CONN_REQ,
401         .send_cmd = l2cap_nval_conn_req,
402         .send_cmd_len = sizeof(l2cap_nval_conn_req),
403         .expect_cmd_code = BT_L2CAP_PDU_CMD_REJECT,
404         .expect_cmd = l2cap_nval_pdu_rsp,
405         .expect_cmd_len = sizeof(l2cap_nval_pdu_rsp),
406 };
407
408 static const uint8_t l2cap_nval_dc_req[] = { 0x12, 0x34, 0x56, 0x78 };
409 static const uint8_t l2cap_nval_cid_rsp[] = { 0x02, 0x00,
410                                                 0x12, 0x34, 0x56, 0x78 };
411
412 static const struct l2cap_data l2cap_server_nval_cid_test1 = {
413         .send_cmd_code = BT_L2CAP_PDU_DISCONN_REQ,
414         .send_cmd = l2cap_nval_dc_req,
415         .send_cmd_len = sizeof(l2cap_nval_dc_req),
416         .expect_cmd_code = BT_L2CAP_PDU_CMD_REJECT,
417         .expect_cmd = l2cap_nval_cid_rsp,
418         .expect_cmd_len = sizeof(l2cap_nval_cid_rsp),
419 };
420
421 static const uint8_t l2cap_nval_cfg_req[] = { 0x12, 0x34, 0x00, 0x00 };
422 static const uint8_t l2cap_nval_cfg_rsp[] = { 0x02, 0x00,
423                                                 0x12, 0x34, 0x00, 0x00 };
424
425 static const struct l2cap_data l2cap_server_nval_cid_test2 = {
426         .send_cmd_code = BT_L2CAP_PDU_CONFIG_REQ,
427         .send_cmd = l2cap_nval_cfg_req,
428         .send_cmd_len = sizeof(l2cap_nval_cfg_req),
429         .expect_cmd_code = BT_L2CAP_PDU_CMD_REJECT,
430         .expect_cmd = l2cap_nval_cfg_rsp,
431         .expect_cmd_len = sizeof(l2cap_nval_cfg_rsp),
432 };
433
434 static const struct l2cap_data le_client_connect_success_test_1 = {
435         .client_psm = 0x0080,
436         .server_psm = 0x0080,
437 };
438
439 static const struct l2cap_data le_client_connect_adv_success_test_1 = {
440         .client_psm = 0x0080,
441         .server_psm = 0x0080,
442         .direct_advertising = true,
443 };
444
445 static const struct l2cap_data le_client_connect_success_test_2 = {
446         .client_psm = 0x0080,
447         .server_psm = 0x0080,
448         .sec_level  = BT_SECURITY_MEDIUM,
449 };
450
451 static const uint8_t cmd_reject_rsp[] = { 0x01, 0x01, 0x02, 0x00, 0x00, 0x00 };
452
453 static const struct l2cap_data le_client_connect_reject_test_1 = {
454         .client_psm = 0x0080,
455         .send_cmd = cmd_reject_rsp,
456         .send_cmd_len = sizeof(cmd_reject_rsp),
457         .expect_err = ECONNREFUSED,
458 };
459
460 static const struct l2cap_data le_client_connect_reject_test_2 = {
461         .client_psm = 0x0080,
462         .addr_type_avail = true,
463         .addr_type = BDADDR_LE_PUBLIC,
464 };
465
466 static uint8_t nonexisting_bdaddr[] = {0x00, 0xAA, 0x01, 0x02, 0x03, 0x00};
467 static const struct l2cap_data le_client_close_socket_test_1 = {
468         .client_psm = 0x0080,
469         .client_bdaddr = nonexisting_bdaddr,
470 };
471
472 static const struct l2cap_data le_client_close_socket_test_2 = {
473         .client_psm = 0x0080,
474         .server_not_advertising = true,
475 };
476
477 static const struct l2cap_data le_client_two_sockets_same_client = {
478         .client_psm = 0x0080,
479         .server_psm = 0x0080,
480         .server_not_advertising = true,
481 };
482
483 static const struct l2cap_data le_client_two_sockets_close_one = {
484         .client_psm = 0x0080,
485         .server_psm = 0x0080,
486         .server_not_advertising = true,
487         .close_one_socket = true,
488 };
489
490 static const struct l2cap_data le_client_connect_nval_psm_test = {
491         .client_psm = 0x0080,
492         .expect_err = ECONNREFUSED,
493 };
494
495 static const uint8_t le_connect_req[] = {       0x80, 0x00, /* PSM */
496                                                 0x41, 0x00, /* SCID */
497                                                 0x20, 0x00, /* MTU */
498                                                 0x20, 0x00, /* MPS */
499                                                 0x05, 0x00, /* Credits */
500 };
501
502 static const uint8_t le_connect_rsp[] = {       0x40, 0x00, /* DCID */
503                                                 0xa0, 0x02, /* MTU */
504                                                 0xe6, 0x00, /* MPS */
505                                                 0x0a, 0x00, /* Credits */
506                                                 0x00, 0x00, /* Result */
507 };
508
509 static const struct l2cap_data le_server_success_test = {
510         .server_psm = 0x0080,
511         .send_cmd_code = BT_L2CAP_PDU_LE_CONN_REQ,
512         .send_cmd = le_connect_req,
513         .send_cmd_len = sizeof(le_connect_req),
514         .expect_cmd_code = BT_L2CAP_PDU_LE_CONN_RSP,
515         .expect_cmd = le_connect_rsp,
516         .expect_cmd_len = sizeof(le_connect_rsp),
517 };
518
519 static const uint8_t nval_le_connect_req[] = {  0x80, 0x00, /* PSM */
520                                                 0x01, 0x00, /* SCID */
521                                                 0x20, 0x00, /* MTU */
522                                                 0x20, 0x00, /* MPS */
523                                                 0x05, 0x00, /* Credits */
524 };
525
526 static const uint8_t nval_le_connect_rsp[] = {  0x00, 0x00, /* DCID */
527                                                 0x00, 0x00, /* MTU */
528                                                 0x00, 0x00, /* MPS */
529                                                 0x00, 0x00, /* Credits */
530                                                 0x09, 0x00, /* Result */
531 };
532
533 static const struct l2cap_data le_server_nval_scid_test = {
534         .server_psm = 0x0080,
535         .send_cmd_code = BT_L2CAP_PDU_LE_CONN_REQ,
536         .send_cmd = nval_le_connect_req,
537         .send_cmd_len = sizeof(nval_le_connect_req),
538         .expect_cmd_code = BT_L2CAP_PDU_LE_CONN_RSP,
539         .expect_cmd = nval_le_connect_rsp,
540         .expect_cmd_len = sizeof(nval_le_connect_rsp),
541 };
542
543 static const struct l2cap_data le_att_client_connect_success_test_1 = {
544         .cid = 0x0004,
545         .sec_level = BT_SECURITY_LOW,
546 };
547
548 static const struct l2cap_data le_att_server_success_test_1 = {
549         .cid = 0x0004,
550 };
551
552 static void client_cmd_complete(uint16_t opcode, uint8_t status,
553                                         const void *param, uint8_t len,
554                                         void *user_data)
555 {
556         struct test_data *data = tester_get_data();
557         const struct l2cap_data *test = data->test_data;
558         struct bthost *bthost;
559
560         bthost = hciemu_client_get_host(data->hciemu);
561
562         switch (opcode) {
563         case BT_HCI_CMD_WRITE_SCAN_ENABLE:
564         case BT_HCI_CMD_LE_SET_ADV_ENABLE:
565                 tester_print("Client set connectable status 0x%02x", status);
566                 if (!status && test && test->enable_ssp) {
567                         bthost_write_ssp_mode(bthost, 0x01);
568                         return;
569                 }
570                 break;
571         case BT_HCI_CMD_WRITE_SIMPLE_PAIRING_MODE:
572                 tester_print("Client enable SSP status 0x%02x", status);
573                 break;
574         default:
575                 return;
576         }
577
578
579         if (status)
580                 tester_setup_failed();
581         else
582                 tester_setup_complete();
583 }
584
585 static void server_cmd_complete(uint16_t opcode, uint8_t status,
586                                         const void *param, uint8_t len,
587                                         void *user_data)
588 {
589         switch (opcode) {
590         case BT_HCI_CMD_WRITE_SIMPLE_PAIRING_MODE:
591                 tester_print("Server enable SSP status 0x%02x", status);
592                 break;
593         default:
594                 return;
595         }
596
597         if (status)
598                 tester_setup_failed();
599         else
600                 tester_setup_complete();
601 }
602
603 static void setup_powered_client_callback(uint8_t status, uint16_t length,
604                                         const void *param, void *user_data)
605 {
606         struct test_data *data = tester_get_data();
607         const struct l2cap_data *l2data = data->test_data;
608         struct bthost *bthost;
609
610         if (status != MGMT_STATUS_SUCCESS) {
611                 tester_setup_failed();
612                 return;
613         }
614
615         tester_print("Controller powered on");
616
617         bthost = hciemu_client_get_host(data->hciemu);
618         bthost_set_cmd_complete_cb(bthost, client_cmd_complete, user_data);
619
620         if (data->hciemu_type == HCIEMU_TYPE_LE) {
621                 if (!l2data || !l2data->server_not_advertising)
622                         bthost_set_adv_enable(bthost, 0x01);
623                 else
624                         tester_setup_complete();
625         } else {
626                 bthost_write_scan_enable(bthost, 0x03);
627         }
628 }
629
630 static void setup_powered_server_callback(uint8_t status, uint16_t length,
631                                         const void *param, void *user_data)
632 {
633         struct test_data *data = tester_get_data();
634         const struct l2cap_data *test = data->test_data;
635         struct bthost *bthost;
636
637         if (status != MGMT_STATUS_SUCCESS) {
638                 tester_setup_failed();
639                 return;
640         }
641
642         tester_print("Controller powered on");
643
644         if (!test->enable_ssp) {
645                 tester_setup_complete();
646                 return;
647         }
648
649         bthost = hciemu_client_get_host(data->hciemu);
650         bthost_set_cmd_complete_cb(bthost, server_cmd_complete, user_data);
651         bthost_write_ssp_mode(bthost, 0x01);
652 }
653
654 static void user_confirm_request_callback(uint16_t index, uint16_t length,
655                                                         const void *param,
656                                                         void *user_data)
657 {
658         const struct mgmt_ev_user_confirm_request *ev = param;
659         struct test_data *data = tester_get_data();
660         const struct l2cap_data *test = data->test_data;
661         struct mgmt_cp_user_confirm_reply cp;
662         uint16_t opcode;
663
664         memset(&cp, 0, sizeof(cp));
665         memcpy(&cp.addr, &ev->addr, sizeof(cp.addr));
666
667         if (test->reject_ssp)
668                 opcode = MGMT_OP_USER_CONFIRM_NEG_REPLY;
669         else
670                 opcode = MGMT_OP_USER_CONFIRM_REPLY;
671
672         mgmt_reply(data->mgmt, opcode, data->mgmt_index, sizeof(cp), &cp,
673                                                         NULL, NULL, NULL);
674 }
675
676 static void pin_code_request_callback(uint16_t index, uint16_t length,
677                                         const void *param, void *user_data)
678 {
679         const struct mgmt_ev_pin_code_request *ev = param;
680         struct test_data *data = user_data;
681         const struct l2cap_data *test = data->test_data;
682         struct mgmt_cp_pin_code_reply cp;
683
684         memset(&cp, 0, sizeof(cp));
685         memcpy(&cp.addr, &ev->addr, sizeof(cp.addr));
686
687         if (!test->pin) {
688                 mgmt_reply(data->mgmt, MGMT_OP_PIN_CODE_NEG_REPLY,
689                                 data->mgmt_index, sizeof(cp.addr), &cp.addr,
690                                 NULL, NULL, NULL);
691                 return;
692         }
693
694         cp.pin_len = test->pin_len;
695         memcpy(cp.pin_code, test->pin, test->pin_len);
696
697         mgmt_reply(data->mgmt, MGMT_OP_PIN_CODE_REPLY, data->mgmt_index,
698                         sizeof(cp), &cp, NULL, NULL, NULL);
699 }
700
701 static void bthost_send_rsp(const void *buf, uint16_t len, void *user_data)
702 {
703         struct test_data *data = tester_get_data();
704         const struct l2cap_data *l2data = data->test_data;
705         struct bthost *bthost;
706
707         if (l2data->expect_cmd_len && len != l2data->expect_cmd_len) {
708                 tester_test_failed();
709                 return;
710         }
711
712         if (l2data->expect_cmd && memcmp(buf, l2data->expect_cmd,
713                                                 l2data->expect_cmd_len)) {
714                 tester_test_failed();
715                 return;
716         }
717
718         if (!l2data->send_cmd)
719                 return;
720
721         bthost = hciemu_client_get_host(data->hciemu);
722         bthost_send_cid(bthost, data->handle, data->dcid,
723                                 l2data->send_cmd, l2data->send_cmd_len);
724 }
725
726 static void send_rsp_new_conn(uint16_t handle, void *user_data)
727 {
728         struct test_data *data = user_data;
729         struct bthost *bthost;
730
731         tester_print("New connection with handle 0x%04x", handle);
732
733         data->handle = handle;
734
735         if (data->hciemu_type == HCIEMU_TYPE_LE)
736                 data->dcid = 0x0005;
737         else
738                 data->dcid = 0x0001;
739
740         bthost = hciemu_client_get_host(data->hciemu);
741         bthost_add_cid_hook(bthost, data->handle, data->dcid,
742                                                 bthost_send_rsp, NULL);
743 }
744
745 static void setup_powered_common(void)
746 {
747         struct test_data *data = tester_get_data();
748         const struct l2cap_data *test = data->test_data;
749         struct bthost *bthost = hciemu_client_get_host(data->hciemu);
750         unsigned char param[] = { 0x01 };
751
752         mgmt_register(data->mgmt, MGMT_EV_USER_CONFIRM_REQUEST,
753                         data->mgmt_index, user_confirm_request_callback,
754                         NULL, NULL);
755
756         if (test && (test->pin || test->expect_pin))
757                 mgmt_register(data->mgmt, MGMT_EV_PIN_CODE_REQUEST,
758                                 data->mgmt_index, pin_code_request_callback,
759                                 data, NULL);
760
761         if (test && test->client_io_cap)
762                 bthost_set_io_capability(bthost, test->client_io_cap);
763
764         if (test && test->client_pin)
765                 bthost_set_pin_code(bthost, test->client_pin,
766                                                         test->client_pin_len);
767         if (test && test->reject_ssp)
768                 bthost_set_reject_user_confirm(bthost, true);
769
770         if (data->hciemu_type == HCIEMU_TYPE_LE)
771                 mgmt_send(data->mgmt, MGMT_OP_SET_LE, data->mgmt_index,
772                                 sizeof(param), param, NULL, NULL, NULL);
773
774         if (test && test->enable_ssp)
775                 mgmt_send(data->mgmt, MGMT_OP_SET_SSP, data->mgmt_index,
776                                 sizeof(param), param, NULL, NULL, NULL);
777
778         mgmt_send(data->mgmt, MGMT_OP_SET_BONDABLE, data->mgmt_index,
779                                 sizeof(param), param, NULL, NULL, NULL);
780 }
781
782 static void setup_powered_client(const void *test_data)
783 {
784         struct test_data *data = tester_get_data();
785         const struct l2cap_data *test = data->test_data;
786         unsigned char param[] = { 0x01 };
787
788         setup_powered_common();
789
790         tester_print("Powering on controller");
791
792         if (test && (test->expect_cmd || test->send_cmd)) {
793                 struct bthost *bthost = hciemu_client_get_host(data->hciemu);
794                 bthost_set_connect_cb(bthost, send_rsp_new_conn, data);
795         }
796
797         if (test && test->direct_advertising)
798                 mgmt_send(data->mgmt, MGMT_OP_SET_ADVERTISING,
799                                 data->mgmt_index, sizeof(param), param,
800                                 NULL, NULL, NULL);
801
802         mgmt_send(data->mgmt, MGMT_OP_SET_POWERED, data->mgmt_index,
803                         sizeof(param), param, setup_powered_client_callback,
804                         NULL, NULL);
805 }
806
807 static void setup_powered_server(const void *test_data)
808 {
809         struct test_data *data = tester_get_data();
810         unsigned char param[] = { 0x01 };
811
812         setup_powered_common();
813
814         tester_print("Powering on controller");
815
816         mgmt_send(data->mgmt, MGMT_OP_SET_CONNECTABLE, data->mgmt_index,
817                                 sizeof(param), param, NULL, NULL, NULL);
818
819         if (data->hciemu_type != HCIEMU_TYPE_BREDR)
820                 mgmt_send(data->mgmt, MGMT_OP_SET_ADVERTISING,
821                                 data->mgmt_index, sizeof(param), param, NULL,
822                                 NULL, NULL);
823
824         mgmt_send(data->mgmt, MGMT_OP_SET_POWERED, data->mgmt_index,
825                         sizeof(param), param, setup_powered_server_callback,
826                         NULL, NULL);
827 }
828
829 static void test_basic(const void *test_data)
830 {
831         int sk;
832
833         sk = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
834         if (sk < 0) {
835                 tester_warn("Can't create socket: %s (%d)", strerror(errno),
836                                                                         errno);
837                 tester_test_failed();
838                 return;
839         }
840
841         close(sk);
842
843         tester_test_passed();
844 }
845
846 static gboolean client_received_data(GIOChannel *io, GIOCondition cond,
847                                                         gpointer user_data)
848 {
849         struct test_data *data = tester_get_data();
850         const struct l2cap_data *l2data = data->test_data;
851         char buf[1024];
852         int sk;
853
854         sk = g_io_channel_unix_get_fd(io);
855         if (read(sk, buf, l2data->data_len) != l2data->data_len) {
856                 tester_warn("Unable to read %u bytes", l2data->data_len);
857                 tester_test_failed();
858                 return FALSE;
859         }
860
861         if (memcmp(buf, l2data->read_data, l2data->data_len))
862                 tester_test_failed();
863         else
864                 tester_test_passed();
865
866         return FALSE;
867 }
868
869 static gboolean server_received_data(GIOChannel *io, GIOCondition cond,
870                                                         gpointer user_data)
871 {
872         struct test_data *data = tester_get_data();
873         const struct l2cap_data *l2data = data->test_data;
874         char buf[1024];
875         int sk;
876
877         sk = g_io_channel_unix_get_fd(io);
878         if (read(sk, buf, l2data->data_len) != l2data->data_len) {
879                 tester_warn("Unable to read %u bytes", l2data->data_len);
880                 tester_test_failed();
881                 return FALSE;
882         }
883
884         if (memcmp(buf, l2data->read_data, l2data->data_len))
885                 tester_test_failed();
886         else
887                 tester_test_passed();
888
889         return FALSE;
890 }
891
892 static void bthost_received_data(const void *buf, uint16_t len,
893                                                         void *user_data)
894 {
895         struct test_data *data = tester_get_data();
896         const struct l2cap_data *l2data = data->test_data;
897
898         if (len != l2data->data_len) {
899                 tester_test_failed();
900                 return;
901         }
902
903         if (memcmp(buf, l2data->write_data, l2data->data_len))
904                 tester_test_failed();
905         else
906                 tester_test_passed();
907 }
908
909 static void server_bthost_received_data(const void *buf, uint16_t len,
910                                                         void *user_data)
911 {
912         struct test_data *data = tester_get_data();
913         const struct l2cap_data *l2data = data->test_data;
914
915         if (len != l2data->data_len) {
916                 tester_test_failed();
917                 return;
918         }
919
920         if (memcmp(buf, l2data->write_data, l2data->data_len))
921                 tester_test_failed();
922         else
923                 tester_test_passed();
924 }
925
926 static bool check_mtu(struct test_data *data, int sk)
927 {
928         const struct l2cap_data *l2data = data->test_data;
929         struct l2cap_options l2o;
930         socklen_t len;
931
932         memset(&l2o, 0, sizeof(l2o));
933
934         if (data->hciemu_type == HCIEMU_TYPE_LE &&
935                                 (l2data->client_psm || l2data->server_psm)) {
936                 /* LE CoC enabled kernels should support BT_RCVMTU and
937                  * BT_SNDMTU.
938                  */
939                 len = sizeof(l2o.imtu);
940                 if (getsockopt(sk, SOL_BLUETOOTH, BT_RCVMTU,
941                                                         &l2o.imtu, &len) < 0) {
942                         tester_warn("getsockopt(BT_RCVMTU): %s (%d)",
943                                         strerror(errno), errno);
944                         return false;
945                 }
946
947                 len = sizeof(l2o.omtu);
948                 if (getsockopt(sk, SOL_BLUETOOTH, BT_SNDMTU,
949                                                         &l2o.omtu, &len) < 0) {
950                         tester_warn("getsockopt(BT_SNDMTU): %s (%d)",
951                                         strerror(errno), errno);
952                         return false;
953                 }
954         } else {
955                 /* For non-LE CoC enabled kernels we need to fall back to
956                  * L2CAP_OPTIONS, so test support for it as well */
957                 len = sizeof(l2o);
958                 if (getsockopt(sk, SOL_L2CAP, L2CAP_OPTIONS, &l2o, &len) < 0) {
959                          tester_warn("getsockopt(L2CAP_OPTIONS): %s (%d)",
960                                                 strerror(errno), errno);
961                          return false;
962                  }
963         }
964
965         return true;
966 }
967
968 static gboolean l2cap_connect_cb(GIOChannel *io, GIOCondition cond,
969                                                         gpointer user_data)
970 {
971         struct test_data *data = tester_get_data();
972         const struct l2cap_data *l2data = data->test_data;
973         int err, sk_err, sk;
974         socklen_t len = sizeof(sk_err);
975
976         data->io_id = 0;
977
978         sk = g_io_channel_unix_get_fd(io);
979
980         if (getsockopt(sk, SOL_SOCKET, SO_ERROR, &sk_err, &len) < 0)
981                 err = -errno;
982         else
983                 err = -sk_err;
984
985         if (err < 0) {
986                 tester_warn("Connect failed: %s (%d)", strerror(-err), -err);
987                 goto failed;
988         }
989
990         tester_print("Successfully connected");
991
992         if (!check_mtu(data, sk)) {
993                 tester_test_failed();
994                 return FALSE;
995         }
996
997         if (l2data->read_data) {
998                 struct bthost *bthost;
999
1000                 bthost = hciemu_client_get_host(data->hciemu);
1001                 g_io_add_watch(io, G_IO_IN, client_received_data, NULL);
1002
1003                 bthost_send_cid(bthost, data->handle, data->dcid,
1004                                         l2data->read_data, l2data->data_len);
1005
1006                 return FALSE;
1007         } else if (l2data->write_data) {
1008                 struct bthost *bthost;
1009                 ssize_t ret;
1010
1011                 bthost = hciemu_client_get_host(data->hciemu);
1012                 bthost_add_cid_hook(bthost, data->handle, data->dcid,
1013                                         bthost_received_data, NULL);
1014
1015                 ret = write(sk, l2data->write_data, l2data->data_len);
1016                 if (ret != l2data->data_len) {
1017                         tester_warn("Unable to write all data");
1018                         tester_test_failed();
1019                 }
1020
1021                 return FALSE;
1022         }
1023
1024 failed:
1025         if (-err != l2data->expect_err)
1026                 tester_test_failed();
1027         else
1028                 tester_test_passed();
1029
1030         return FALSE;
1031 }
1032
1033 static int create_l2cap_sock(struct test_data *data, uint16_t psm,
1034                                                 uint16_t cid, int sec_level)
1035 {
1036         const struct l2cap_data *l2data = data->test_data;
1037         const uint8_t *master_bdaddr;
1038         struct sockaddr_l2 addr;
1039         int sk, err;
1040
1041         sk = socket(PF_BLUETOOTH, SOCK_SEQPACKET | SOCK_NONBLOCK,
1042                                                         BTPROTO_L2CAP);
1043         if (sk < 0) {
1044                 err = -errno;
1045                 tester_warn("Can't create socket: %s (%d)", strerror(errno),
1046                                                                         errno);
1047                 return err;
1048         }
1049
1050         master_bdaddr = hciemu_get_master_bdaddr(data->hciemu);
1051         if (!master_bdaddr) {
1052                 tester_warn("No master bdaddr");
1053                 close(sk);
1054                 return -ENODEV;
1055         }
1056
1057         memset(&addr, 0, sizeof(addr));
1058         addr.l2_family = AF_BLUETOOTH;
1059         addr.l2_psm = htobs(psm);
1060         addr.l2_cid = htobs(cid);
1061         bacpy(&addr.l2_bdaddr, (void *) master_bdaddr);
1062
1063         if (l2data && l2data->addr_type_avail)
1064                 addr.l2_bdaddr_type = l2data->addr_type;
1065         else if (data->hciemu_type == HCIEMU_TYPE_LE)
1066                 addr.l2_bdaddr_type = BDADDR_LE_PUBLIC;
1067         else
1068                 addr.l2_bdaddr_type = BDADDR_BREDR;
1069
1070         if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1071                 err = -errno;
1072                 tester_warn("Can't bind socket: %s (%d)", strerror(errno),
1073                                                                         errno);
1074                 close(sk);
1075                 return err;
1076         }
1077
1078         if (sec_level) {
1079                 struct bt_security sec;
1080
1081                 memset(&sec, 0, sizeof(sec));
1082                 sec.level = sec_level;
1083
1084                 if (setsockopt(sk, SOL_BLUETOOTH, BT_SECURITY, &sec,
1085                                                         sizeof(sec)) < 0) {
1086                         err = -errno;
1087                         tester_warn("Can't set security level: %s (%d)",
1088                                                 strerror(errno), errno);
1089                         close(sk);
1090                         return err;
1091                 }
1092         }
1093
1094         return sk;
1095 }
1096
1097 static int connect_l2cap_impl(int sk, const uint8_t *bdaddr,
1098                                 uint8_t bdaddr_type, uint16_t psm, uint16_t cid)
1099 {
1100         struct sockaddr_l2 addr;
1101         int err;
1102
1103         if (!bdaddr) {
1104                 tester_warn("No client bdaddr");
1105                 return -ENODEV;
1106         }
1107
1108         memset(&addr, 0, sizeof(addr));
1109         addr.l2_family = AF_BLUETOOTH;
1110         bacpy(&addr.l2_bdaddr, (void *) bdaddr);
1111         addr.l2_bdaddr_type = bdaddr_type;
1112         addr.l2_psm = htobs(psm);
1113         addr.l2_cid = htobs(cid);
1114
1115         err = connect(sk, (struct sockaddr *) &addr, sizeof(addr));
1116         if (err < 0 && !(errno == EAGAIN || errno == EINPROGRESS)) {
1117                 err = -errno;
1118                 tester_warn("Can't connect socket: %s (%d)", strerror(errno),
1119                                                                         errno);
1120                 return err;
1121         }
1122
1123         return 0;
1124 }
1125
1126 static int connect_l2cap_sock(struct test_data *data, int sk, uint16_t psm,
1127                                                                 uint16_t cid)
1128 {
1129         const struct l2cap_data *l2data = data->test_data;
1130         const uint8_t *client_bdaddr;
1131         uint8_t bdaddr_type;
1132
1133         if (l2data->client_bdaddr != NULL)
1134                 client_bdaddr = l2data->client_bdaddr;
1135         else
1136                 client_bdaddr = hciemu_get_client_bdaddr(data->hciemu);
1137
1138         if (!client_bdaddr) {
1139                 tester_warn("No client bdaddr");
1140                 return -ENODEV;
1141         }
1142
1143         if (l2data && l2data->addr_type_avail)
1144                 bdaddr_type = l2data->addr_type;
1145         else if (data->hciemu_type == HCIEMU_TYPE_LE)
1146                 bdaddr_type = BDADDR_LE_PUBLIC;
1147         else
1148                 bdaddr_type = BDADDR_BREDR;
1149
1150         return connect_l2cap_impl(sk, client_bdaddr, bdaddr_type, psm, cid);
1151 }
1152
1153 static void client_l2cap_connect_cb(uint16_t handle, uint16_t cid,
1154                                                         void *user_data)
1155 {
1156         struct test_data *data = user_data;
1157
1158         data->dcid = cid;
1159         data->handle = handle;
1160 }
1161
1162 static void direct_adv_cmd_complete(uint16_t opcode, const void *param,
1163                                                 uint8_t len, void *user_data)
1164 {
1165         struct test_data *data = tester_get_data();
1166         const struct bt_hci_cmd_le_set_adv_parameters *cp;
1167         const uint8_t *expect_bdaddr;
1168
1169         if (opcode != BT_HCI_CMD_LE_SET_ADV_PARAMETERS)
1170                 return;
1171
1172         tester_print("Received advertising parameters HCI command");
1173
1174         cp = param;
1175
1176         /* Advertising as client should be direct advertising */
1177         if (cp->type != 0x01) {
1178                 tester_warn("Invalid advertising type");
1179                 tester_test_failed();
1180                 return;
1181         }
1182
1183         expect_bdaddr = hciemu_get_client_bdaddr(data->hciemu);
1184         if (memcmp(expect_bdaddr, cp->direct_addr, 6)) {
1185                 tester_warn("Invalid direct address in adv params");
1186                 tester_test_failed();
1187                 return;
1188         }
1189
1190         tester_test_passed();
1191 }
1192
1193 static void test_connect(const void *test_data)
1194 {
1195         struct test_data *data = tester_get_data();
1196         const struct l2cap_data *l2data = data->test_data;
1197         GIOChannel *io;
1198         int sk;
1199
1200         if (l2data->server_psm) {
1201                 struct bthost *bthost = hciemu_client_get_host(data->hciemu);
1202
1203                 if (!l2data->data_len)
1204                         bthost_add_l2cap_server(bthost, l2data->server_psm,
1205                                                 NULL, NULL);
1206                 else
1207                         bthost_add_l2cap_server(bthost, l2data->server_psm,
1208                                                 client_l2cap_connect_cb, data);
1209         }
1210
1211         if (l2data->direct_advertising)
1212                 hciemu_add_master_post_command_hook(data->hciemu,
1213                                                 direct_adv_cmd_complete, NULL);
1214
1215         sk = create_l2cap_sock(data, 0, l2data->cid, l2data->sec_level);
1216         if (sk < 0) {
1217                 tester_test_failed();
1218                 return;
1219         }
1220
1221         if (connect_l2cap_sock(data, sk, l2data->client_psm,
1222                                                         l2data->cid) < 0) {
1223                 close(sk);
1224                 tester_test_failed();
1225                 return;
1226         }
1227
1228         io = g_io_channel_unix_new(sk);
1229         g_io_channel_set_close_on_unref(io, TRUE);
1230
1231         data->io_id = g_io_add_watch(io, G_IO_OUT, l2cap_connect_cb, NULL);
1232
1233         g_io_channel_unref(io);
1234
1235         tester_print("Connect in progress");
1236 }
1237
1238 static void test_connect_reject(const void *test_data)
1239 {
1240         struct test_data *data = tester_get_data();
1241         const struct l2cap_data *l2data = data->test_data;
1242         int sk;
1243
1244         sk = create_l2cap_sock(data, 0, l2data->cid, l2data->sec_level);
1245         if (sk < 0) {
1246                 tester_test_failed();
1247                 return;
1248         }
1249
1250         if (connect_l2cap_sock(data, sk, l2data->client_psm,
1251                                                         l2data->cid) < 0)
1252                 tester_test_passed();
1253         else
1254                 tester_test_failed();
1255
1256         close(sk);
1257 }
1258
1259 static void connect_socket(const uint8_t *client_bdaddr, int *sk_holder,
1260                                                         GIOFunc connect_cb)
1261 {
1262         struct test_data *data = tester_get_data();
1263         const struct l2cap_data *l2data = data->test_data;
1264         GIOChannel *io;
1265         int sk;
1266
1267         sk = create_l2cap_sock(data, 0, l2data->cid, l2data->sec_level);
1268         if (sk < 0) {
1269                 tester_print("Error in create_l2cap_sock");
1270                 tester_test_failed();
1271                 return;
1272         }
1273
1274         *sk_holder = sk;
1275
1276         if (connect_l2cap_impl(sk, client_bdaddr, BDADDR_LE_PUBLIC,
1277                         l2data->client_psm, l2data->cid) < 0) {
1278                 tester_print("Error in connect_l2cap_sock");
1279                 close(sk);
1280                 tester_test_failed();
1281                 return;
1282         }
1283
1284         if (connect_cb) {
1285                 io = g_io_channel_unix_new(sk);
1286                 g_io_channel_set_close_on_unref(io, TRUE);
1287
1288                 data->io_id = g_io_add_watch(io, G_IO_OUT, connect_cb, NULL);
1289
1290                 g_io_channel_unref(io);
1291         }
1292
1293         tester_print("Connect in progress, sk = %d", sk);
1294 }
1295
1296 static gboolean test_close_socket_1_part_3(gpointer arg)
1297 {
1298         struct test_data *data = tester_get_data();
1299
1300         tester_print("Checking whether scan was properly stopped...");
1301
1302         if (data->sk != -1) {
1303                 tester_print("Error - scan was not enabled yet");
1304                 tester_test_failed();
1305                 return FALSE;
1306         }
1307
1308         if (hciemu_is_master_le_scan_enabled(data->hciemu)) {
1309                 tester_print("Delayed check whether scann is off failed");
1310                 tester_test_failed();
1311                 return FALSE;
1312         }
1313
1314         tester_test_passed();
1315         return FALSE;
1316 }
1317
1318 static gboolean test_close_socket_1_part_2(gpointer args)
1319 {
1320         struct test_data *data = tester_get_data();
1321         int sk = data->sk;
1322
1323         tester_print("Will close socket during scan phase...");
1324
1325         /* We tried to conect to LE device that is not advertising. It was added
1326          * to kernel whitelist, and scan was started. We should be still
1327          * scanning.
1328          */
1329         if (!hciemu_is_master_le_scan_enabled(data->hciemu)) {
1330                 tester_print("Error - should be still scanning");
1331                 tester_test_failed();
1332                 return FALSE;
1333         }
1334
1335         /* Calling close() should remove device from  whitelist, and stop
1336          * the scan.
1337          */
1338         if (close(sk) < 0) {
1339                 tester_print("Error when closing socket");
1340                 tester_test_failed();
1341                 return FALSE;
1342         }
1343
1344         data->sk = -1;
1345         /* tester_test_passed will be called when scan is stopped. */
1346         return FALSE;
1347 }
1348
1349 static gboolean test_close_socket_2_part_3(gpointer arg)
1350 {
1351         struct test_data *data = tester_get_data();
1352         int sk = data->sk;
1353         int err;
1354
1355         /* Scan should be already over, we're trying to create connection */
1356         if (hciemu_is_master_le_scan_enabled(data->hciemu)) {
1357                 tester_print("Error - should no longer scan");
1358                 tester_test_failed();
1359                 return FALSE;
1360         }
1361
1362         /* Calling close() should eventually cause CMD_LE_CREATE_CONN_CANCEL */
1363         err = close(sk);
1364         if (err < 0) {
1365                 tester_print("Error when closing socket");
1366                 tester_test_failed();
1367                 return FALSE;
1368         }
1369
1370         /* CMD_LE_CREATE_CONN_CANCEL will trigger test pass. */
1371         return FALSE;
1372 }
1373
1374 static bool test_close_socket_cc_hook(const void *data, uint16_t len,
1375                                                         void *user_data)
1376 {
1377         return false;
1378 }
1379
1380 static gboolean test_close_socket_2_part_2(gpointer arg)
1381 {
1382         struct test_data *data = tester_get_data();
1383         struct bthost *bthost = hciemu_client_get_host(data->hciemu);
1384
1385         /* Make sure CMD_LE_CREATE_CONN will not immediately result in
1386          * BT_HCI_EVT_CONN_COMPLETE.
1387          */
1388         hciemu_add_hook(data->hciemu, HCIEMU_HOOK_PRE_EVT,
1389                 BT_HCI_CMD_LE_CREATE_CONN, test_close_socket_cc_hook, NULL);
1390
1391         /* Advertise once. After that, kernel should stop scanning, and trigger
1392          * BT_HCI_CMD_LE_CREATE_CONN_CANCEL.
1393          */
1394         bthost_set_adv_enable(bthost, 0x01);
1395         bthost_set_adv_enable(bthost, 0x00);
1396         return FALSE;
1397 }
1398
1399 static void test_close_socket_scan_enabled(void)
1400 {
1401         struct test_data *data = tester_get_data();
1402         const struct l2cap_data *l2data = data->test_data;
1403
1404         if (l2data == &le_client_close_socket_test_1)
1405                 g_idle_add(test_close_socket_1_part_2, NULL);
1406         else if (l2data == &le_client_close_socket_test_2)
1407                 g_idle_add(test_close_socket_2_part_2, NULL);
1408 }
1409
1410 static void test_close_socket_scan_disabled(void)
1411 {
1412         struct test_data *data = tester_get_data();
1413         const struct l2cap_data *l2data = data->test_data;
1414
1415         if (l2data == &le_client_close_socket_test_1)
1416                 g_idle_add(test_close_socket_1_part_3, NULL);
1417         else if (l2data == &le_client_close_socket_test_2)
1418                 g_idle_add(test_close_socket_2_part_3, NULL);
1419 }
1420
1421 static void test_close_socket_conn_cancel(void)
1422 {
1423         struct test_data *data = tester_get_data();
1424         const struct l2cap_data *l2data = data->test_data;
1425
1426         if (l2data == &le_client_close_socket_test_2)
1427                 tester_test_passed();
1428 }
1429
1430 static void test_close_socket_router(uint16_t opcode, const void *param,
1431                                         uint8_t length, void *user_data)
1432 {
1433         /* tester_print("HCI Command 0x%04x length %u", opcode, length); */
1434         if (opcode == BT_HCI_CMD_LE_SET_SCAN_ENABLE) {
1435                 const struct bt_hci_cmd_le_set_scan_enable *scan_params = param;
1436
1437                 if (scan_params->enable == true)
1438                         test_close_socket_scan_enabled();
1439                 else
1440                         test_close_socket_scan_disabled();
1441         } else if (opcode == BT_HCI_CMD_LE_CREATE_CONN_CANCEL) {
1442                 test_close_socket_conn_cancel();
1443         }
1444 }
1445
1446 static void test_close_socket(const void *test_data)
1447 {
1448         struct test_data *data = tester_get_data();
1449         const struct l2cap_data *l2data = data->test_data;
1450         const uint8_t *client_bdaddr;
1451
1452         hciemu_add_master_post_command_hook(data->hciemu,
1453                                         test_close_socket_router, data);
1454
1455         if (l2data->client_bdaddr != NULL)
1456                 client_bdaddr = l2data->client_bdaddr;
1457         else
1458                 client_bdaddr = hciemu_get_client_bdaddr(data->hciemu);
1459
1460         connect_socket(client_bdaddr, &data->sk, NULL);
1461 }
1462
1463 static uint8_t test_two_sockets_connect_cb_cnt;
1464 static gboolean test_two_sockets_connect_cb(GIOChannel *io, GIOCondition cond,
1465                                                         gpointer user_data)
1466 {
1467         struct test_data *data = tester_get_data();
1468         const struct l2cap_data *l2data = data->test_data;
1469         int err, sk_err, sk;
1470         socklen_t len = sizeof(sk_err);
1471
1472         data->io_id = 0;
1473
1474         sk = g_io_channel_unix_get_fd(io);
1475
1476         if (getsockopt(sk, SOL_SOCKET, SO_ERROR, &sk_err, &len) < 0)
1477                 err = -errno;
1478         else
1479                 err = -sk_err;
1480
1481         if (err < 0) {
1482                 tester_warn("Connect failed: %s (%d)", strerror(-err), -err);
1483                 tester_test_failed();
1484                 return FALSE;
1485         }
1486
1487         tester_print("Successfully connected");
1488         test_two_sockets_connect_cb_cnt++;
1489
1490         if (test_two_sockets_connect_cb_cnt == 2) {
1491                 close(data->sk);
1492                 close(data->sk2);
1493                 tester_test_passed();
1494         }
1495
1496         if (l2data->close_one_socket && test_two_sockets_connect_cb_cnt == 1) {
1497                 close(data->sk2);
1498                 tester_test_passed();
1499         }
1500
1501         return FALSE;
1502 }
1503
1504 static gboolean enable_advertising(gpointer args)
1505 {
1506         struct test_data *data = tester_get_data();
1507         struct bthost *bthost = hciemu_client_get_host(data->hciemu);
1508
1509         bthost_set_adv_enable(bthost, 0x01);
1510         return FALSE;
1511 }
1512
1513 static void test_connect_two_sockets_part_2(void)
1514 {
1515         struct test_data *data = tester_get_data();
1516         const struct l2cap_data *l2data = data->test_data;
1517         const uint8_t *client_bdaddr;
1518
1519         client_bdaddr = hciemu_get_client_bdaddr(data->hciemu);
1520         connect_socket(client_bdaddr, &data->sk2, test_two_sockets_connect_cb);
1521
1522         if (l2data->close_one_socket) {
1523                 tester_print("Closing first socket! %d", data->sk);
1524                 close(data->sk);
1525         }
1526
1527         g_idle_add(enable_advertising, NULL);
1528 }
1529
1530 static uint8_t test_scan_enable_counter;
1531 static void test_connect_two_sockets_router(uint16_t opcode, const void *param,
1532                                         uint8_t length, void *user_data)
1533 {
1534         const struct bt_hci_cmd_le_set_scan_enable *scan_params = param;
1535
1536         tester_print("HCI Command 0x%04x length %u", opcode, length);
1537         if (opcode == BT_HCI_CMD_LE_SET_SCAN_ENABLE &&
1538                                                 scan_params->enable == true) {
1539                 test_scan_enable_counter++;
1540                 if (test_scan_enable_counter == 1)
1541                         test_connect_two_sockets_part_2();
1542                 else if (test_scan_enable_counter == 2)
1543                         g_idle_add(enable_advertising, NULL);
1544         }
1545 }
1546
1547 static void test_connect_two_sockets(const void *test_data)
1548 {
1549         struct test_data *data = tester_get_data();
1550         const struct l2cap_data *l2data = data->test_data;
1551         const uint8_t *client_bdaddr;
1552
1553         test_two_sockets_connect_cb_cnt = 0;
1554         test_scan_enable_counter = 0;
1555
1556         hciemu_add_master_post_command_hook(data->hciemu,
1557                                 test_connect_two_sockets_router, data);
1558
1559         if (l2data->server_psm) {
1560                 struct bthost *bthost = hciemu_client_get_host(data->hciemu);
1561
1562                 if (!l2data->data_len)
1563                         bthost_add_l2cap_server(bthost, l2data->server_psm,
1564                                                 NULL, NULL);
1565         }
1566
1567         client_bdaddr = hciemu_get_client_bdaddr(data->hciemu);
1568         if (l2data->close_one_socket)
1569                 connect_socket(client_bdaddr, &data->sk, NULL);
1570         else
1571                 connect_socket(client_bdaddr, &data->sk,
1572                                                 test_two_sockets_connect_cb);
1573 }
1574
1575 static gboolean l2cap_listen_cb(GIOChannel *io, GIOCondition cond,
1576                                                         gpointer user_data)
1577 {
1578         struct test_data *data = tester_get_data();
1579         const struct l2cap_data *l2data = data->test_data;
1580         int sk, new_sk;
1581
1582         data->io_id = 0;
1583
1584         sk = g_io_channel_unix_get_fd(io);
1585
1586         new_sk = accept(sk, NULL, NULL);
1587         if (new_sk < 0) {
1588                 tester_warn("accept failed: %s (%u)", strerror(errno), errno);
1589                 tester_test_failed();
1590                 return FALSE;
1591         }
1592
1593         if (!check_mtu(data, new_sk)) {
1594                 tester_test_failed();
1595                 return FALSE;
1596         }
1597
1598         if (l2data->read_data) {
1599                 struct bthost *bthost;
1600                 GIOChannel *new_io;
1601
1602                 new_io = g_io_channel_unix_new(new_sk);
1603                 g_io_channel_set_close_on_unref(new_io, TRUE);
1604
1605                 bthost = hciemu_client_get_host(data->hciemu);
1606                 g_io_add_watch(new_io, G_IO_IN, server_received_data, NULL);
1607                 bthost_send_cid(bthost, data->handle, data->dcid,
1608                                         l2data->read_data, l2data->data_len);
1609
1610                 g_io_channel_unref(new_io);
1611
1612                 return FALSE;
1613         } else if (l2data->write_data) {
1614                 struct bthost *bthost;
1615                 ssize_t ret;
1616
1617                 bthost = hciemu_client_get_host(data->hciemu);
1618                 bthost_add_cid_hook(bthost, data->handle, data->scid,
1619                                         server_bthost_received_data, NULL);
1620
1621                 ret = write(new_sk, l2data->write_data, l2data->data_len);
1622                 close(new_sk);
1623
1624                 if (ret != l2data->data_len) {
1625                         tester_warn("Unable to write all data");
1626                         tester_test_failed();
1627                 }
1628
1629                 return FALSE;
1630         }
1631
1632         tester_print("Successfully connected");
1633
1634         close(new_sk);
1635
1636         tester_test_passed();
1637
1638         return FALSE;
1639 }
1640
1641 static void client_l2cap_rsp(uint8_t code, const void *data, uint16_t len,
1642                                                         void *user_data)
1643 {
1644         struct test_data *test_data = user_data;
1645         const struct l2cap_data *l2data = test_data->test_data;
1646
1647         tester_print("Client received response code 0x%02x", code);
1648
1649         if (code != l2data->expect_cmd_code) {
1650                 tester_warn("Unexpected L2CAP response code (expected 0x%02x)",
1651                                                 l2data->expect_cmd_code);
1652                 return;
1653         }
1654
1655         if (code == BT_L2CAP_PDU_CONN_RSP) {
1656
1657                 const struct bt_l2cap_pdu_conn_rsp *rsp = data;
1658                 if (len == sizeof(rsp) && !rsp->result && !rsp->status)
1659                         return;
1660
1661                 test_data->dcid = rsp->dcid;
1662                 test_data->scid = rsp->scid;
1663
1664                 if (l2data->data_len)
1665                         return;
1666         }
1667
1668         if (!l2data->expect_cmd) {
1669                 tester_test_passed();
1670                 return;
1671         }
1672
1673         if (l2data->expect_cmd_len != len) {
1674                 tester_warn("Unexpected L2CAP response length (%u != %u)",
1675                                                 len, l2data->expect_cmd_len);
1676                 goto failed;
1677         }
1678
1679         if (memcmp(l2data->expect_cmd, data, len) != 0) {
1680                 tester_warn("Unexpected L2CAP response");
1681                 goto failed;
1682         }
1683
1684         tester_test_passed();
1685         return;
1686
1687 failed:
1688         tester_test_failed();
1689 }
1690
1691 static void send_req_new_conn(uint16_t handle, void *user_data)
1692 {
1693         struct test_data *data = user_data;
1694         const struct l2cap_data *l2data = data->test_data;
1695         struct bthost *bthost;
1696
1697         tester_print("New client connection with handle 0x%04x", handle);
1698
1699         data->handle = handle;
1700
1701         if (l2data->send_cmd) {
1702                 bthost_l2cap_rsp_cb cb;
1703
1704                 if (l2data->expect_cmd_code)
1705                         cb = client_l2cap_rsp;
1706                 else
1707                         cb = NULL;
1708
1709                 tester_print("Sending L2CAP Request from client");
1710
1711                 bthost = hciemu_client_get_host(data->hciemu);
1712                 bthost_l2cap_req(bthost, handle, l2data->send_cmd_code,
1713                                         l2data->send_cmd, l2data->send_cmd_len,
1714                                         cb, data);
1715         }
1716 }
1717
1718 static void test_server(const void *test_data)
1719 {
1720         struct test_data *data = tester_get_data();
1721         const struct l2cap_data *l2data = data->test_data;
1722         const uint8_t *master_bdaddr;
1723         uint8_t addr_type;
1724         struct bthost *bthost;
1725         GIOChannel *io;
1726         int sk;
1727
1728         if (l2data->server_psm || l2data->cid) {
1729                 sk = create_l2cap_sock(data, l2data->server_psm,
1730                                         l2data->cid, l2data->sec_level);
1731                 if (sk < 0) {
1732                         tester_test_failed();
1733                         return;
1734                 }
1735
1736                 if (listen(sk, 5) < 0) {
1737                         tester_warn("listening on socket failed: %s (%u)",
1738                                         strerror(errno), errno);
1739                         tester_test_failed();
1740                         close(sk);
1741                         return;
1742                 }
1743
1744                 io = g_io_channel_unix_new(sk);
1745                 g_io_channel_set_close_on_unref(io, TRUE);
1746
1747                 data->io_id = g_io_add_watch(io, G_IO_IN, l2cap_listen_cb,
1748                                                                         NULL);
1749                 g_io_channel_unref(io);
1750
1751                 tester_print("Listening for connections");
1752         }
1753
1754         master_bdaddr = hciemu_get_master_bdaddr(data->hciemu);
1755         if (!master_bdaddr) {
1756                 tester_warn("No master bdaddr");
1757                 tester_test_failed();
1758                 return;
1759         }
1760
1761         bthost = hciemu_client_get_host(data->hciemu);
1762         bthost_set_connect_cb(bthost, send_req_new_conn, data);
1763
1764         if (data->hciemu_type == HCIEMU_TYPE_BREDR)
1765                 addr_type = BDADDR_BREDR;
1766         else
1767                 addr_type = BDADDR_LE_PUBLIC;
1768
1769         bthost_hci_connect(bthost, master_bdaddr, addr_type);
1770 }
1771
1772 static void test_getpeername_not_connected(const void *test_data)
1773 {
1774         struct test_data *data = tester_get_data();
1775         struct sockaddr_l2 addr;
1776         socklen_t len;
1777         int sk;
1778
1779         sk = create_l2cap_sock(data, 0, 0, 0);
1780         if (sk < 0) {
1781                 tester_test_failed();
1782                 return;
1783         }
1784
1785         len = sizeof(addr);
1786         if (getpeername(sk, (struct sockaddr *) &addr, &len) == 0) {
1787                 tester_warn("getpeername succeeded on non-connected socket");
1788                 tester_test_failed();
1789                 goto done;
1790         }
1791
1792         if (errno != ENOTCONN) {
1793                 tester_warn("Unexpexted getpeername error: %s (%d)",
1794                                                 strerror(errno), errno);
1795                 tester_test_failed();
1796                 goto done;
1797         }
1798
1799         tester_test_passed();
1800
1801 done:
1802         close(sk);
1803 }
1804
1805 int main(int argc, char *argv[])
1806 {
1807         tester_init(&argc, &argv);
1808
1809         test_l2cap_bredr("Basic L2CAP Socket - Success", NULL,
1810                                         setup_powered_client, test_basic);
1811         test_l2cap_bredr("Non-connected getpeername - Failure", NULL,
1812                                         setup_powered_client,
1813                                         test_getpeername_not_connected);
1814
1815         test_l2cap_bredr("L2CAP BR/EDR Client - Success",
1816                                         &client_connect_success_test,
1817                                         setup_powered_client, test_connect);
1818
1819         test_l2cap_bredr("L2CAP BR/EDR Client SSP - Success 1",
1820                                         &client_connect_ssp_success_test_1,
1821                                         setup_powered_client, test_connect);
1822         test_l2cap_bredr("L2CAP BR/EDR Client SSP - Success 2",
1823                                         &client_connect_ssp_success_test_2,
1824                                         setup_powered_client, test_connect);
1825         test_l2cap_bredr("L2CAP BR/EDR Client PIN Code - Success",
1826                                         &client_connect_pin_success_test,
1827                                         setup_powered_client, test_connect);
1828
1829         test_l2cap_bredr("L2CAP BR/EDR Client - Read Success",
1830                                         &client_connect_read_success_test,
1831                                         setup_powered_client, test_connect);
1832
1833         test_l2cap_bredr("L2CAP BR/EDR Client - Write Success",
1834                                         &client_connect_write_success_test,
1835                                         setup_powered_client, test_connect);
1836
1837         test_l2cap_bredr("L2CAP BR/EDR Client - Invalid PSM 1",
1838                                         &client_connect_nval_psm_test_1,
1839                                         setup_powered_client, test_connect);
1840
1841         test_l2cap_bredr("L2CAP BR/EDR Client - Invalid PSM 2",
1842                                         &client_connect_nval_psm_test_2,
1843                                         setup_powered_client, test_connect);
1844
1845         test_l2cap_bredr("L2CAP BR/EDR Client - Invalid PSM 3",
1846                                         &client_connect_nval_psm_test_3,
1847                                         setup_powered_client, test_connect);
1848
1849         test_l2cap_bredr("L2CAP BR/EDR Server - Success",
1850                                         &l2cap_server_success_test,
1851                                         setup_powered_server, test_server);
1852
1853         test_l2cap_bredr("L2CAP BR/EDR Server - Read Success",
1854                                         &l2cap_server_read_success_test,
1855                                         setup_powered_server, test_server);
1856
1857         test_l2cap_bredr("L2CAP BR/EDR Server - Write Success",
1858                                         &l2cap_server_write_success_test,
1859                                         setup_powered_server, test_server);
1860
1861         test_l2cap_bredr("L2CAP BR/EDR Server - Security Block",
1862                                         &l2cap_server_sec_block_test,
1863                                         setup_powered_server, test_server);
1864
1865         test_l2cap_bredr("L2CAP BR/EDR Server - Invalid PSM",
1866                                         &l2cap_server_nval_psm_test,
1867                                         setup_powered_server, test_server);
1868         test_l2cap_bredr("L2CAP BR/EDR Server - Invalid PDU",
1869                                 &l2cap_server_nval_pdu_test1,
1870                                 setup_powered_server, test_server);
1871         test_l2cap_bredr("L2CAP BR/EDR Server - Invalid Disconnect CID",
1872                                 &l2cap_server_nval_cid_test1,
1873                                 setup_powered_server, test_server);
1874         test_l2cap_bredr("L2CAP BR/EDR Server - Invalid Config CID",
1875                                 &l2cap_server_nval_cid_test2,
1876                                 setup_powered_server, test_server);
1877
1878         test_l2cap_le("L2CAP LE Client - Success",
1879                                 &le_client_connect_success_test_1,
1880                                 setup_powered_client, test_connect);
1881         test_l2cap_le("L2CAP LE Client, Direct Advertising - Success",
1882                                 &le_client_connect_adv_success_test_1,
1883                                 setup_powered_client, test_connect);
1884         test_l2cap_le("L2CAP LE Client SMP - Success",
1885                                 &le_client_connect_success_test_2,
1886                                 setup_powered_client, test_connect);
1887         test_l2cap_le("L2CAP LE Client - Command Reject",
1888                                         &le_client_connect_reject_test_1,
1889                                         setup_powered_client, test_connect);
1890         test_l2cap_bredr("L2CAP LE Client - Connection Reject",
1891                                 &le_client_connect_reject_test_2,
1892                                 setup_powered_client, test_connect_reject);
1893
1894         test_l2cap_le("L2CAP LE Client - Close socket 1",
1895                                 &le_client_close_socket_test_1,
1896                                 setup_powered_client,
1897                                 test_close_socket);
1898
1899         test_l2cap_le("L2CAP LE Client - Close socket 2",
1900                                 &le_client_close_socket_test_2,
1901                                 setup_powered_client,
1902                                 test_close_socket);
1903
1904         test_l2cap_le("L2CAP LE Client - Open two sockets",
1905                                 &le_client_two_sockets_same_client,
1906                                 setup_powered_client,
1907                                 test_connect_two_sockets);
1908
1909         test_l2cap_le("L2CAP LE Client - Open two sockets close one",
1910                                 &le_client_two_sockets_close_one,
1911                                 setup_powered_client,
1912                                 test_connect_two_sockets);
1913
1914         test_l2cap_le("L2CAP LE Client - Invalid PSM",
1915                                         &le_client_connect_nval_psm_test,
1916                                         setup_powered_client, test_connect);
1917         test_l2cap_le("L2CAP LE Server - Success", &le_server_success_test,
1918                                         setup_powered_server, test_server);
1919         test_l2cap_le("L2CAP LE Server - Nval SCID", &le_server_nval_scid_test,
1920                                         setup_powered_server, test_server);
1921
1922
1923         test_l2cap_le("L2CAP LE ATT Client - Success",
1924                                 &le_att_client_connect_success_test_1,
1925                                 setup_powered_client, test_connect);
1926         test_l2cap_le("L2CAP LE ATT Server - Success",
1927                                 &le_att_server_success_test_1,
1928                                 setup_powered_server, test_server);
1929
1930         return tester_run();
1931 }