| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076 |
- // SPDX-License-Identifier: GPL-2.0-or-later
- /*
- *
- * BlueZ - Bluetooth protocol stack for Linux
- *
- * Copyright (C) 2013 Intel Corporation. All rights reserved.
- *
- *
- */
- #ifdef HAVE_CONFIG_H
- #include <config.h>
- #endif
- #include <stdlib.h>
- #include <unistd.h>
- #include <errno.h>
- #include <stdbool.h>
- #include <glib.h>
- #include "lib/bluetooth.h"
- #include "lib/l2cap.h"
- #include "lib/mgmt.h"
- #include "monitor/bt.h"
- #include "emulator/bthost.h"
- #include "emulator/hciemu.h"
- #include "src/shared/tester.h"
- #include "src/shared/mgmt.h"
- struct test_data {
- const void *test_data;
- struct mgmt *mgmt;
- uint16_t mgmt_index;
- struct hciemu *hciemu;
- enum hciemu_type hciemu_type;
- unsigned int io_id;
- uint16_t handle;
- uint16_t scid;
- uint16_t dcid;
- int sk;
- int sk2;
- bool host_disconnected;
- };
- struct l2cap_data {
- uint16_t client_psm;
- uint16_t server_psm;
- uint16_t cid;
- uint8_t mode;
- int expect_err;
- uint8_t send_cmd_code;
- const void *send_cmd;
- uint16_t send_cmd_len;
- uint8_t expect_cmd_code;
- const void *expect_cmd;
- uint16_t expect_cmd_len;
- uint16_t data_len;
- const void *read_data;
- const void *write_data;
- bool enable_ssp;
- uint8_t client_io_cap;
- int sec_level;
- bool reject_ssp;
- bool expect_pin;
- uint8_t pin_len;
- const void *pin;
- uint8_t client_pin_len;
- const void *client_pin;
- bool addr_type_avail;
- uint8_t addr_type;
- uint8_t *client_bdaddr;
- bool server_not_advertising;
- bool direct_advertising;
- bool close_1;
- bool shut_sock_wr;
- };
- static void print_debug(const char *str, void *user_data)
- {
- const char *prefix = user_data;
- tester_print("%s%s", prefix, str);
- }
- static void read_info_callback(uint8_t status, uint16_t length,
- const void *param, void *user_data)
- {
- struct test_data *data = tester_get_data();
- const struct mgmt_rp_read_info *rp = param;
- char addr[18];
- uint16_t manufacturer;
- uint32_t supported_settings, current_settings;
- tester_print("Read Info callback");
- tester_print(" Status: 0x%02x", status);
- if (status || !param) {
- tester_pre_setup_failed();
- return;
- }
- ba2str(&rp->bdaddr, addr);
- manufacturer = btohs(rp->manufacturer);
- supported_settings = btohl(rp->supported_settings);
- current_settings = btohl(rp->current_settings);
- tester_print(" Address: %s", addr);
- tester_print(" Version: 0x%02x", rp->version);
- tester_print(" Manufacturer: 0x%04x", manufacturer);
- tester_print(" Supported settings: 0x%08x", supported_settings);
- tester_print(" Current settings: 0x%08x", current_settings);
- tester_print(" Class: 0x%02x%02x%02x",
- rp->dev_class[2], rp->dev_class[1], rp->dev_class[0]);
- tester_print(" Name: %s", rp->name);
- tester_print(" Short name: %s", rp->short_name);
- if (strcmp(hciemu_get_address(data->hciemu), addr)) {
- tester_pre_setup_failed();
- return;
- }
- tester_pre_setup_complete();
- }
- static void index_added_callback(uint16_t index, uint16_t length,
- const void *param, void *user_data)
- {
- struct test_data *data = tester_get_data();
- tester_print("Index Added callback");
- tester_print(" Index: 0x%04x", index);
- data->mgmt_index = index;
- mgmt_send(data->mgmt, MGMT_OP_READ_INFO, data->mgmt_index, 0, NULL,
- read_info_callback, NULL, NULL);
- }
- static void index_removed_callback(uint16_t index, uint16_t length,
- const void *param, void *user_data)
- {
- struct test_data *data = tester_get_data();
- tester_print("Index Removed callback");
- tester_print(" Index: 0x%04x", index);
- if (index != data->mgmt_index)
- return;
- mgmt_unregister_index(data->mgmt, data->mgmt_index);
- mgmt_unref(data->mgmt);
- data->mgmt = NULL;
- tester_post_teardown_complete();
- }
- static void read_index_list_callback(uint8_t status, uint16_t length,
- const void *param, void *user_data)
- {
- struct test_data *data = tester_get_data();
- tester_print("Read Index List callback");
- tester_print(" Status: 0x%02x", status);
- if (status || !param) {
- tester_pre_setup_failed();
- return;
- }
- mgmt_register(data->mgmt, MGMT_EV_INDEX_ADDED, MGMT_INDEX_NONE,
- index_added_callback, NULL, NULL);
- mgmt_register(data->mgmt, MGMT_EV_INDEX_REMOVED, MGMT_INDEX_NONE,
- index_removed_callback, NULL, NULL);
- data->hciemu = hciemu_new(data->hciemu_type);
- if (!data->hciemu) {
- tester_warn("Failed to setup HCI emulation");
- tester_pre_setup_failed();
- }
- if (tester_use_debug())
- hciemu_set_debug(data->hciemu, print_debug, "hciemu: ", NULL);
- tester_print("New hciemu instance created");
- }
- static void test_pre_setup(const void *test_data)
- {
- struct test_data *data = tester_get_data();
- data->mgmt = mgmt_new_default();
- if (!data->mgmt) {
- tester_warn("Failed to setup management interface");
- tester_pre_setup_failed();
- return;
- }
- if (tester_use_debug())
- mgmt_set_debug(data->mgmt, print_debug, "mgmt: ", NULL);
- mgmt_send(data->mgmt, MGMT_OP_READ_INDEX_LIST, MGMT_INDEX_NONE, 0, NULL,
- read_index_list_callback, NULL, NULL);
- }
- static void test_post_teardown(const void *test_data)
- {
- struct test_data *data = tester_get_data();
- if (data->io_id > 0) {
- g_source_remove(data->io_id);
- data->io_id = 0;
- }
- hciemu_unref(data->hciemu);
- data->hciemu = NULL;
- }
- static void test_data_free(void *test_data)
- {
- struct test_data *data = test_data;
- free(data);
- }
- #define test_l2cap_bredr(name, data, setup, func) \
- do { \
- struct test_data *user; \
- user = malloc(sizeof(struct test_data)); \
- if (!user) \
- break; \
- user->hciemu_type = HCIEMU_TYPE_BREDR; \
- user->io_id = 0; \
- user->test_data = data; \
- tester_add_full(name, data, \
- test_pre_setup, setup, func, NULL, \
- test_post_teardown, 2, user, test_data_free); \
- } while (0)
- #define test_l2cap_le(name, data, setup, func) \
- do { \
- struct test_data *user; \
- user = malloc(sizeof(struct test_data)); \
- if (!user) \
- break; \
- user->hciemu_type = HCIEMU_TYPE_LE; \
- user->io_id = 0; \
- user->test_data = data; \
- tester_add_full(name, data, \
- test_pre_setup, setup, func, NULL, \
- test_post_teardown, 2, user, test_data_free); \
- } while (0)
- static uint8_t pair_device_pin[] = { 0x30, 0x30, 0x30, 0x30 }; /* "0000" */
- static const struct l2cap_data client_connect_success_test = {
- .client_psm = 0x1001,
- .server_psm = 0x1001,
- };
- static const struct l2cap_data client_connect_ssp_success_test_1 = {
- .client_psm = 0x1001,
- .server_psm = 0x1001,
- .enable_ssp = true,
- };
- static const struct l2cap_data client_connect_ssp_success_test_2 = {
- .client_psm = 0x1001,
- .server_psm = 0x1001,
- .enable_ssp = true,
- .sec_level = BT_SECURITY_HIGH,
- .client_io_cap = 0x04,
- };
- static const struct l2cap_data client_connect_pin_success_test = {
- .client_psm = 0x1001,
- .server_psm = 0x1001,
- .sec_level = BT_SECURITY_MEDIUM,
- .pin = pair_device_pin,
- .pin_len = sizeof(pair_device_pin),
- .client_pin = pair_device_pin,
- .client_pin_len = sizeof(pair_device_pin),
- };
- static uint8_t l2_data[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
- static const struct l2cap_data client_connect_read_success_test = {
- .client_psm = 0x1001,
- .server_psm = 0x1001,
- .read_data = l2_data,
- .data_len = sizeof(l2_data),
- };
- static const struct l2cap_data client_connect_write_success_test = {
- .client_psm = 0x1001,
- .server_psm = 0x1001,
- .write_data = l2_data,
- .data_len = sizeof(l2_data),
- };
- static const struct l2cap_data client_connect_shut_wr_success_test = {
- .client_psm = 0x1001,
- .server_psm = 0x1001,
- .shut_sock_wr = true,
- };
- static const struct l2cap_data client_connect_nval_psm_test_1 = {
- .client_psm = 0x1001,
- .expect_err = ECONNREFUSED,
- };
- static const struct l2cap_data client_connect_nval_psm_test_2 = {
- .client_psm = 0x0001,
- .expect_err = ECONNREFUSED,
- };
- static const struct l2cap_data client_connect_nval_psm_test_3 = {
- .client_psm = 0x0001,
- .expect_err = ECONNREFUSED,
- .enable_ssp = true,
- };
- static const uint8_t l2cap_connect_req[] = { 0x01, 0x10, 0x41, 0x00 };
- static const struct l2cap_data l2cap_server_success_test = {
- .server_psm = 0x1001,
- .send_cmd_code = BT_L2CAP_PDU_CONN_REQ,
- .send_cmd = l2cap_connect_req,
- .send_cmd_len = sizeof(l2cap_connect_req),
- .expect_cmd_code = BT_L2CAP_PDU_CONN_RSP,
- };
- static const struct l2cap_data l2cap_server_read_success_test = {
- .server_psm = 0x1001,
- .send_cmd_code = BT_L2CAP_PDU_CONN_REQ,
- .send_cmd = l2cap_connect_req,
- .send_cmd_len = sizeof(l2cap_connect_req),
- .expect_cmd_code = BT_L2CAP_PDU_CONN_RSP,
- .read_data = l2_data,
- .data_len = sizeof(l2_data),
- };
- static const struct l2cap_data l2cap_server_write_success_test = {
- .server_psm = 0x1001,
- .send_cmd_code = BT_L2CAP_PDU_CONN_REQ,
- .send_cmd = l2cap_connect_req,
- .send_cmd_len = sizeof(l2cap_connect_req),
- .expect_cmd_code = BT_L2CAP_PDU_CONN_RSP,
- .write_data = l2_data,
- .data_len = sizeof(l2_data),
- };
- static const uint8_t l2cap_sec_block_rsp[] = { 0x00, 0x00, /* dcid */
- 0x41, 0x00, /* scid */
- 0x03, 0x00, /* Sec Block */
- 0x00, 0x00 /* status */
- };
- static const struct l2cap_data l2cap_server_sec_block_test = {
- .server_psm = 0x1001,
- .send_cmd_code = BT_L2CAP_PDU_CONN_REQ,
- .send_cmd = l2cap_connect_req,
- .send_cmd_len = sizeof(l2cap_connect_req),
- .expect_cmd_code = BT_L2CAP_PDU_CONN_RSP,
- .expect_cmd = l2cap_sec_block_rsp,
- .expect_cmd_len = sizeof(l2cap_sec_block_rsp),
- .enable_ssp = true,
- };
- static const uint8_t l2cap_nval_psm_rsp[] = { 0x00, 0x00, /* dcid */
- 0x41, 0x00, /* scid */
- 0x02, 0x00, /* nval PSM */
- 0x00, 0x00 /* status */
- };
- static const struct l2cap_data l2cap_server_nval_psm_test = {
- .send_cmd_code = BT_L2CAP_PDU_CONN_REQ,
- .send_cmd = l2cap_connect_req,
- .send_cmd_len = sizeof(l2cap_connect_req),
- .expect_cmd_code = BT_L2CAP_PDU_CONN_RSP,
- .expect_cmd = l2cap_nval_psm_rsp,
- .expect_cmd_len = sizeof(l2cap_nval_psm_rsp),
- };
- static const uint8_t l2cap_nval_conn_req[] = { 0x00 };
- static const uint8_t l2cap_nval_pdu_rsp[] = { 0x00, 0x00 };
- static const struct l2cap_data l2cap_server_nval_pdu_test1 = {
- .send_cmd_code = BT_L2CAP_PDU_CONN_REQ,
- .send_cmd = l2cap_nval_conn_req,
- .send_cmd_len = sizeof(l2cap_nval_conn_req),
- .expect_cmd_code = BT_L2CAP_PDU_CMD_REJECT,
- .expect_cmd = l2cap_nval_pdu_rsp,
- .expect_cmd_len = sizeof(l2cap_nval_pdu_rsp),
- };
- static const uint8_t l2cap_nval_dc_req[] = { 0x12, 0x34, 0x56, 0x78 };
- static const uint8_t l2cap_nval_cid_rsp[] = { 0x02, 0x00,
- 0x12, 0x34, 0x56, 0x78 };
- static const struct l2cap_data l2cap_server_nval_cid_test1 = {
- .send_cmd_code = BT_L2CAP_PDU_DISCONN_REQ,
- .send_cmd = l2cap_nval_dc_req,
- .send_cmd_len = sizeof(l2cap_nval_dc_req),
- .expect_cmd_code = BT_L2CAP_PDU_CMD_REJECT,
- .expect_cmd = l2cap_nval_cid_rsp,
- .expect_cmd_len = sizeof(l2cap_nval_cid_rsp),
- };
- static const uint8_t l2cap_nval_cfg_req[] = { 0x12, 0x34, 0x00, 0x00 };
- static const uint8_t l2cap_nval_cfg_rsp[] = { 0x02, 0x00,
- 0x12, 0x34, 0x00, 0x00 };
- static const struct l2cap_data l2cap_server_nval_cid_test2 = {
- .send_cmd_code = BT_L2CAP_PDU_CONFIG_REQ,
- .send_cmd = l2cap_nval_cfg_req,
- .send_cmd_len = sizeof(l2cap_nval_cfg_req),
- .expect_cmd_code = BT_L2CAP_PDU_CMD_REJECT,
- .expect_cmd = l2cap_nval_cfg_rsp,
- .expect_cmd_len = sizeof(l2cap_nval_cfg_rsp),
- };
- static const struct l2cap_data le_client_connect_success_test_1 = {
- .client_psm = 0x0080,
- .server_psm = 0x0080,
- };
- static const struct l2cap_data le_client_connect_adv_success_test_1 = {
- .client_psm = 0x0080,
- .server_psm = 0x0080,
- .direct_advertising = true,
- };
- static const struct l2cap_data le_client_connect_success_test_2 = {
- .client_psm = 0x0080,
- .server_psm = 0x0080,
- .sec_level = BT_SECURITY_MEDIUM,
- };
- static const uint8_t cmd_reject_rsp[] = { 0x01, 0x01, 0x02, 0x00, 0x00, 0x00 };
- static const struct l2cap_data le_client_connect_reject_test_1 = {
- .client_psm = 0x0080,
- .send_cmd = cmd_reject_rsp,
- .send_cmd_len = sizeof(cmd_reject_rsp),
- .expect_err = ECONNREFUSED,
- };
- static const struct l2cap_data le_client_connect_reject_test_2 = {
- .client_psm = 0x0080,
- .addr_type_avail = true,
- .addr_type = BDADDR_LE_PUBLIC,
- };
- static uint8_t nonexisting_bdaddr[] = {0x00, 0xAA, 0x01, 0x02, 0x03, 0x00};
- static const struct l2cap_data le_client_close_socket_test_1 = {
- .client_psm = 0x0080,
- .client_bdaddr = nonexisting_bdaddr,
- };
- static const struct l2cap_data le_client_close_socket_test_2 = {
- .client_psm = 0x0080,
- .server_not_advertising = true,
- };
- static const struct l2cap_data le_client_2_same_client = {
- .client_psm = 0x0080,
- .server_psm = 0x0080,
- .server_not_advertising = true,
- };
- static const struct l2cap_data le_client_2_close_1 = {
- .client_psm = 0x0080,
- .server_psm = 0x0080,
- .server_not_advertising = true,
- .close_1 = true,
- };
- static const struct l2cap_data le_client_connect_nval_psm_test = {
- .client_psm = 0x0080,
- .expect_err = ECONNREFUSED,
- };
- static const uint8_t le_connect_req[] = { 0x80, 0x00, /* PSM */
- 0x41, 0x00, /* SCID */
- 0x20, 0x00, /* MTU */
- 0x20, 0x00, /* MPS */
- 0x05, 0x00, /* Credits */
- };
- static const uint8_t le_connect_rsp[] = { 0x40, 0x00, /* DCID */
- 0xa0, 0x02, /* MTU */
- 0xbc, 0x00, /* MPS */
- 0x04, 0x00, /* Credits */
- 0x00, 0x00, /* Result */
- };
- static const struct l2cap_data le_server_success_test = {
- .server_psm = 0x0080,
- .send_cmd_code = BT_L2CAP_PDU_LE_CONN_REQ,
- .send_cmd = le_connect_req,
- .send_cmd_len = sizeof(le_connect_req),
- .expect_cmd_code = BT_L2CAP_PDU_LE_CONN_RSP,
- .expect_cmd = le_connect_rsp,
- .expect_cmd_len = sizeof(le_connect_rsp),
- };
- static const uint8_t nval_le_connect_req[] = { 0x80, 0x00, /* PSM */
- 0x01, 0x00, /* SCID */
- 0x20, 0x00, /* MTU */
- 0x20, 0x00, /* MPS */
- 0x05, 0x00, /* Credits */
- };
- static const uint8_t nval_le_connect_rsp[] = { 0x00, 0x00, /* DCID */
- 0x00, 0x00, /* MTU */
- 0x00, 0x00, /* MPS */
- 0x00, 0x00, /* Credits */
- 0x09, 0x00, /* Result */
- };
- static const struct l2cap_data le_server_nval_scid_test = {
- .server_psm = 0x0080,
- .send_cmd_code = BT_L2CAP_PDU_LE_CONN_REQ,
- .send_cmd = nval_le_connect_req,
- .send_cmd_len = sizeof(nval_le_connect_req),
- .expect_cmd_code = BT_L2CAP_PDU_LE_CONN_RSP,
- .expect_cmd = nval_le_connect_rsp,
- .expect_cmd_len = sizeof(nval_le_connect_rsp),
- };
- static const struct l2cap_data le_att_client_connect_success_test_1 = {
- .cid = 0x0004,
- .sec_level = BT_SECURITY_LOW,
- };
- static const struct l2cap_data le_att_server_success_test_1 = {
- .cid = 0x0004,
- };
- static const struct l2cap_data ext_flowctl_client_connect_success_test_1 = {
- .client_psm = 0x0080,
- .server_psm = 0x0080,
- .mode = BT_MODE_EXT_FLOWCTL,
- };
- static const struct l2cap_data ext_flowctl_client_connect_adv_success_test_1 = {
- .client_psm = 0x0080,
- .server_psm = 0x0080,
- .mode = BT_MODE_EXT_FLOWCTL,
- .direct_advertising = true,
- };
- static const struct l2cap_data ext_flowctl_client_connect_success_test_2 = {
- .client_psm = 0x0080,
- .server_psm = 0x0080,
- .mode = BT_MODE_EXT_FLOWCTL,
- .sec_level = BT_SECURITY_MEDIUM,
- };
- static const struct l2cap_data ext_flowctl_client_connect_reject_test_1 = {
- .client_psm = 0x0080,
- .mode = BT_MODE_EXT_FLOWCTL,
- .send_cmd = cmd_reject_rsp,
- .send_cmd_len = sizeof(cmd_reject_rsp),
- .expect_err = ECONNREFUSED,
- };
- static const struct l2cap_data ext_flowctl_client_2 = {
- .client_psm = 0x0080,
- .server_psm = 0x0080,
- .mode = BT_MODE_EXT_FLOWCTL,
- .server_not_advertising = true,
- };
- static const struct l2cap_data ext_flowctl_client_2_close_1 = {
- .client_psm = 0x0080,
- .server_psm = 0x0080,
- .mode = BT_MODE_EXT_FLOWCTL,
- .server_not_advertising = true,
- .close_1 = true,
- };
- static void client_cmd_complete(uint16_t opcode, uint8_t status,
- const void *param, uint8_t len,
- void *user_data)
- {
- struct test_data *data = tester_get_data();
- const struct l2cap_data *test = data->test_data;
- struct bthost *bthost;
- bthost = hciemu_client_get_host(data->hciemu);
- switch (opcode) {
- case BT_HCI_CMD_WRITE_SCAN_ENABLE:
- case BT_HCI_CMD_LE_SET_ADV_ENABLE:
- tester_print("Client set connectable status 0x%02x", status);
- if (!status && test && test->enable_ssp) {
- bthost_write_ssp_mode(bthost, 0x01);
- return;
- }
- break;
- case BT_HCI_CMD_WRITE_SIMPLE_PAIRING_MODE:
- tester_print("Client enable SSP status 0x%02x", status);
- break;
- default:
- return;
- }
- if (status)
- tester_setup_failed();
- else
- tester_setup_complete();
- }
- static void server_cmd_complete(uint16_t opcode, uint8_t status,
- const void *param, uint8_t len,
- void *user_data)
- {
- switch (opcode) {
- case BT_HCI_CMD_WRITE_SIMPLE_PAIRING_MODE:
- tester_print("Server enable SSP status 0x%02x", status);
- break;
- default:
- return;
- }
- if (status)
- tester_setup_failed();
- else
- tester_setup_complete();
- }
- static void setup_powered_client_callback(uint8_t status, uint16_t length,
- const void *param, void *user_data)
- {
- struct test_data *data = tester_get_data();
- const struct l2cap_data *l2data = data->test_data;
- struct bthost *bthost;
- if (status != MGMT_STATUS_SUCCESS) {
- tester_setup_failed();
- return;
- }
- tester_print("Controller powered on");
- bthost = hciemu_client_get_host(data->hciemu);
- bthost_set_cmd_complete_cb(bthost, client_cmd_complete, user_data);
- if (data->hciemu_type == HCIEMU_TYPE_LE) {
- if (!l2data || !l2data->server_not_advertising)
- bthost_set_adv_enable(bthost, 0x01);
- else
- tester_setup_complete();
- } else {
- bthost_write_scan_enable(bthost, 0x03);
- }
- }
- static void setup_powered_server_callback(uint8_t status, uint16_t length,
- const void *param, void *user_data)
- {
- struct test_data *data = tester_get_data();
- const struct l2cap_data *test = data->test_data;
- struct bthost *bthost;
- if (status != MGMT_STATUS_SUCCESS) {
- tester_setup_failed();
- return;
- }
- tester_print("Controller powered on");
- if (!test->enable_ssp) {
- tester_setup_complete();
- return;
- }
- bthost = hciemu_client_get_host(data->hciemu);
- bthost_set_cmd_complete_cb(bthost, server_cmd_complete, user_data);
- bthost_write_ssp_mode(bthost, 0x01);
- }
- static void user_confirm_request_callback(uint16_t index, uint16_t length,
- const void *param,
- void *user_data)
- {
- const struct mgmt_ev_user_confirm_request *ev = param;
- struct test_data *data = tester_get_data();
- const struct l2cap_data *test = data->test_data;
- struct mgmt_cp_user_confirm_reply cp;
- uint16_t opcode;
- memset(&cp, 0, sizeof(cp));
- memcpy(&cp.addr, &ev->addr, sizeof(cp.addr));
- if (test->reject_ssp)
- opcode = MGMT_OP_USER_CONFIRM_NEG_REPLY;
- else
- opcode = MGMT_OP_USER_CONFIRM_REPLY;
- mgmt_reply(data->mgmt, opcode, data->mgmt_index, sizeof(cp), &cp,
- NULL, NULL, NULL);
- }
- static void pin_code_request_callback(uint16_t index, uint16_t length,
- const void *param, void *user_data)
- {
- const struct mgmt_ev_pin_code_request *ev = param;
- struct test_data *data = user_data;
- const struct l2cap_data *test = data->test_data;
- struct mgmt_cp_pin_code_reply cp;
- memset(&cp, 0, sizeof(cp));
- memcpy(&cp.addr, &ev->addr, sizeof(cp.addr));
- if (!test->pin) {
- mgmt_reply(data->mgmt, MGMT_OP_PIN_CODE_NEG_REPLY,
- data->mgmt_index, sizeof(cp.addr), &cp.addr,
- NULL, NULL, NULL);
- return;
- }
- cp.pin_len = test->pin_len;
- memcpy(cp.pin_code, test->pin, test->pin_len);
- mgmt_reply(data->mgmt, MGMT_OP_PIN_CODE_REPLY, data->mgmt_index,
- sizeof(cp), &cp, NULL, NULL, NULL);
- }
- static void bthost_send_rsp(const void *buf, uint16_t len, void *user_data)
- {
- struct test_data *data = tester_get_data();
- const struct l2cap_data *l2data = data->test_data;
- struct bthost *bthost;
- if (l2data->expect_cmd_len && len != l2data->expect_cmd_len) {
- tester_test_failed();
- return;
- }
- if (l2data->expect_cmd && memcmp(buf, l2data->expect_cmd,
- l2data->expect_cmd_len)) {
- tester_test_failed();
- return;
- }
- if (!l2data->send_cmd)
- return;
- bthost = hciemu_client_get_host(data->hciemu);
- bthost_send_cid(bthost, data->handle, data->dcid,
- l2data->send_cmd, l2data->send_cmd_len);
- }
- static void send_rsp_new_conn(uint16_t handle, void *user_data)
- {
- struct test_data *data = user_data;
- struct bthost *bthost;
- tester_print("New connection with handle 0x%04x", handle);
- data->handle = handle;
- if (data->hciemu_type == HCIEMU_TYPE_LE)
- data->dcid = 0x0005;
- else
- data->dcid = 0x0001;
- bthost = hciemu_client_get_host(data->hciemu);
- bthost_add_cid_hook(bthost, data->handle, data->dcid,
- bthost_send_rsp, NULL);
- }
- static void setup_powered_common(void)
- {
- struct test_data *data = tester_get_data();
- const struct l2cap_data *test = data->test_data;
- struct bthost *bthost = hciemu_client_get_host(data->hciemu);
- unsigned char param[] = { 0x01 };
- mgmt_register(data->mgmt, MGMT_EV_USER_CONFIRM_REQUEST,
- data->mgmt_index, user_confirm_request_callback,
- NULL, NULL);
- if (test && (test->pin || test->expect_pin))
- mgmt_register(data->mgmt, MGMT_EV_PIN_CODE_REQUEST,
- data->mgmt_index, pin_code_request_callback,
- data, NULL);
- if (test && test->client_io_cap)
- bthost_set_io_capability(bthost, test->client_io_cap);
- if (test && test->client_pin)
- bthost_set_pin_code(bthost, test->client_pin,
- test->client_pin_len);
- if (test && test->reject_ssp)
- bthost_set_reject_user_confirm(bthost, true);
- if (data->hciemu_type == HCIEMU_TYPE_LE)
- mgmt_send(data->mgmt, MGMT_OP_SET_LE, data->mgmt_index,
- sizeof(param), param, NULL, NULL, NULL);
- if (test && test->enable_ssp)
- mgmt_send(data->mgmt, MGMT_OP_SET_SSP, data->mgmt_index,
- sizeof(param), param, NULL, NULL, NULL);
- mgmt_send(data->mgmt, MGMT_OP_SET_BONDABLE, data->mgmt_index,
- sizeof(param), param, NULL, NULL, NULL);
- }
- static void setup_powered_client(const void *test_data)
- {
- struct test_data *data = tester_get_data();
- const struct l2cap_data *test = data->test_data;
- unsigned char param[] = { 0x01 };
- setup_powered_common();
- tester_print("Powering on controller");
- if (test && (test->expect_cmd || test->send_cmd)) {
- struct bthost *bthost = hciemu_client_get_host(data->hciemu);
- bthost_set_connect_cb(bthost, send_rsp_new_conn, data);
- }
- if (test && test->direct_advertising)
- mgmt_send(data->mgmt, MGMT_OP_SET_ADVERTISING,
- data->mgmt_index, sizeof(param), param,
- NULL, NULL, NULL);
- mgmt_send(data->mgmt, MGMT_OP_SET_POWERED, data->mgmt_index,
- sizeof(param), param, setup_powered_client_callback,
- NULL, NULL);
- }
- static void setup_powered_server(const void *test_data)
- {
- struct test_data *data = tester_get_data();
- unsigned char param[] = { 0x01 };
- setup_powered_common();
- tester_print("Powering on controller");
- mgmt_send(data->mgmt, MGMT_OP_SET_CONNECTABLE, data->mgmt_index,
- sizeof(param), param, NULL, NULL, NULL);
- if (data->hciemu_type != HCIEMU_TYPE_BREDR)
- mgmt_send(data->mgmt, MGMT_OP_SET_ADVERTISING,
- data->mgmt_index, sizeof(param), param, NULL,
- NULL, NULL);
- mgmt_send(data->mgmt, MGMT_OP_SET_POWERED, data->mgmt_index,
- sizeof(param), param, setup_powered_server_callback,
- NULL, NULL);
- }
- static void test_basic(const void *test_data)
- {
- int sk;
- sk = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
- if (sk < 0) {
- tester_warn("Can't create socket: %s (%d)", strerror(errno),
- errno);
- tester_test_failed();
- return;
- }
- close(sk);
- tester_test_passed();
- }
- static gboolean client_received_data(GIOChannel *io, GIOCondition cond,
- gpointer user_data)
- {
- struct test_data *data = tester_get_data();
- const struct l2cap_data *l2data = data->test_data;
- char buf[1024];
- int sk;
- sk = g_io_channel_unix_get_fd(io);
- if (read(sk, buf, l2data->data_len) != l2data->data_len) {
- tester_warn("Unable to read %u bytes", l2data->data_len);
- tester_test_failed();
- return FALSE;
- }
- if (memcmp(buf, l2data->read_data, l2data->data_len))
- tester_test_failed();
- else
- tester_test_passed();
- return FALSE;
- }
- static gboolean server_received_data(GIOChannel *io, GIOCondition cond,
- gpointer user_data)
- {
- struct test_data *data = tester_get_data();
- const struct l2cap_data *l2data = data->test_data;
- char buf[1024];
- int sk;
- sk = g_io_channel_unix_get_fd(io);
- if (read(sk, buf, l2data->data_len) != l2data->data_len) {
- tester_warn("Unable to read %u bytes", l2data->data_len);
- tester_test_failed();
- return FALSE;
- }
- if (memcmp(buf, l2data->read_data, l2data->data_len))
- tester_test_failed();
- else
- tester_test_passed();
- return FALSE;
- }
- static void bthost_received_data(const void *buf, uint16_t len,
- void *user_data)
- {
- struct test_data *data = tester_get_data();
- const struct l2cap_data *l2data = data->test_data;
- if (len != l2data->data_len) {
- tester_test_failed();
- return;
- }
- if (memcmp(buf, l2data->write_data, l2data->data_len))
- tester_test_failed();
- else
- tester_test_passed();
- }
- static void server_bthost_received_data(const void *buf, uint16_t len,
- void *user_data)
- {
- struct test_data *data = tester_get_data();
- const struct l2cap_data *l2data = data->test_data;
- if (len != l2data->data_len) {
- tester_test_failed();
- return;
- }
- if (memcmp(buf, l2data->write_data, l2data->data_len))
- tester_test_failed();
- else
- tester_test_passed();
- }
- static gboolean socket_closed_cb(GIOChannel *io, GIOCondition cond,
- gpointer user_data)
- {
- struct test_data *data = tester_get_data();
- const struct l2cap_data *l2data = data->test_data;
- if (l2data->shut_sock_wr) {
- /* if socket is closed using SHUT_WR, L2CAP disconnection
- * response must be received first before G_IO_HUP event.
- */
- if (data->host_disconnected)
- tester_test_passed();
- else {
- tester_warn("G_IO_HUP received before receiving L2CAP disconnection");
- tester_test_failed();
- }
- }
- return FALSE;
- }
- static bool check_mtu(struct test_data *data, int sk)
- {
- const struct l2cap_data *l2data = data->test_data;
- struct l2cap_options l2o;
- socklen_t len;
- memset(&l2o, 0, sizeof(l2o));
- if (data->hciemu_type == HCIEMU_TYPE_LE &&
- (l2data->client_psm || l2data->server_psm)) {
- /* LE CoC enabled kernels should support BT_RCVMTU and
- * BT_SNDMTU.
- */
- len = sizeof(l2o.imtu);
- if (getsockopt(sk, SOL_BLUETOOTH, BT_RCVMTU,
- &l2o.imtu, &len) < 0) {
- tester_warn("getsockopt(BT_RCVMTU): %s (%d)",
- strerror(errno), errno);
- return false;
- }
- len = sizeof(l2o.omtu);
- if (getsockopt(sk, SOL_BLUETOOTH, BT_SNDMTU,
- &l2o.omtu, &len) < 0) {
- tester_warn("getsockopt(BT_SNDMTU): %s (%d)",
- strerror(errno), errno);
- return false;
- }
- } else {
- /* For non-LE CoC enabled kernels we need to fall back to
- * L2CAP_OPTIONS, so test support for it as well */
- len = sizeof(l2o);
- if (getsockopt(sk, SOL_L2CAP, L2CAP_OPTIONS, &l2o, &len) < 0) {
- tester_warn("getsockopt(L2CAP_OPTIONS): %s (%d)",
- strerror(errno), errno);
- return false;
- }
- }
- return true;
- }
- static gboolean l2cap_connect_cb(GIOChannel *io, GIOCondition cond,
- gpointer user_data)
- {
- struct test_data *data = tester_get_data();
- const struct l2cap_data *l2data = data->test_data;
- int err, sk_err, sk;
- socklen_t len = sizeof(sk_err);
- data->io_id = 0;
- sk = g_io_channel_unix_get_fd(io);
- if (getsockopt(sk, SOL_SOCKET, SO_ERROR, &sk_err, &len) < 0)
- err = -errno;
- else
- err = -sk_err;
- if (err < 0) {
- tester_warn("Connect failed: %s (%d)", strerror(-err), -err);
- goto failed;
- }
- tester_print("Successfully connected");
- if (!check_mtu(data, sk)) {
- tester_test_failed();
- return FALSE;
- }
- if (l2data->read_data) {
- struct bthost *bthost;
- bthost = hciemu_client_get_host(data->hciemu);
- g_io_add_watch(io, G_IO_IN, client_received_data, NULL);
- bthost_send_cid(bthost, data->handle, data->dcid,
- l2data->read_data, l2data->data_len);
- return FALSE;
- } else if (l2data->write_data) {
- struct bthost *bthost;
- ssize_t ret;
- bthost = hciemu_client_get_host(data->hciemu);
- bthost_add_cid_hook(bthost, data->handle, data->dcid,
- bthost_received_data, NULL);
- ret = write(sk, l2data->write_data, l2data->data_len);
- if (ret != l2data->data_len) {
- tester_warn("Unable to write all data");
- tester_test_failed();
- }
- return FALSE;
- } else if (l2data->shut_sock_wr) {
- g_io_add_watch(io, G_IO_HUP, socket_closed_cb, NULL);
- shutdown(sk, SHUT_WR);
- return FALSE;
- }
- failed:
- if (-err != l2data->expect_err)
- tester_test_failed();
- else
- tester_test_passed();
- return FALSE;
- }
- static int create_l2cap_sock(struct test_data *data, uint16_t psm,
- uint16_t cid, int sec_level, uint8_t mode)
- {
- const struct l2cap_data *l2data = data->test_data;
- const uint8_t *central_bdaddr;
- struct sockaddr_l2 addr;
- int sk, err;
- sk = socket(PF_BLUETOOTH, SOCK_SEQPACKET | SOCK_NONBLOCK,
- BTPROTO_L2CAP);
- if (sk < 0) {
- err = -errno;
- tester_warn("Can't create socket: %s (%d)", strerror(errno),
- errno);
- return err;
- }
- central_bdaddr = hciemu_get_central_bdaddr(data->hciemu);
- if (!central_bdaddr) {
- tester_warn("No central bdaddr");
- close(sk);
- return -ENODEV;
- }
- memset(&addr, 0, sizeof(addr));
- addr.l2_family = AF_BLUETOOTH;
- addr.l2_psm = htobs(psm);
- addr.l2_cid = htobs(cid);
- bacpy(&addr.l2_bdaddr, (void *) central_bdaddr);
- if (l2data && l2data->addr_type_avail)
- addr.l2_bdaddr_type = l2data->addr_type;
- else if (data->hciemu_type == HCIEMU_TYPE_LE)
- addr.l2_bdaddr_type = BDADDR_LE_PUBLIC;
- else
- addr.l2_bdaddr_type = BDADDR_BREDR;
- if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
- err = -errno;
- tester_warn("Can't bind socket: %s (%d)", strerror(errno),
- errno);
- close(sk);
- return err;
- }
- if (sec_level) {
- struct bt_security sec;
- memset(&sec, 0, sizeof(sec));
- sec.level = sec_level;
- if (setsockopt(sk, SOL_BLUETOOTH, BT_SECURITY, &sec,
- sizeof(sec)) < 0) {
- err = -errno;
- tester_warn("Can't set security level: %s (%d)",
- strerror(errno), errno);
- close(sk);
- return err;
- }
- }
- if (mode) {
- if (setsockopt(sk, SOL_BLUETOOTH, BT_MODE, &mode,
- sizeof(mode)) < 0) {
- err = -errno;
- tester_warn("Can't set mode: %s (%d)", strerror(errno),
- errno);
- close(sk);
- return err;
- }
- }
- return sk;
- }
- static int connect_l2cap_impl(int sk, const uint8_t *bdaddr,
- uint8_t bdaddr_type, uint16_t psm, uint16_t cid)
- {
- struct sockaddr_l2 addr;
- int err;
- if (!bdaddr) {
- tester_warn("No client bdaddr");
- return -ENODEV;
- }
- memset(&addr, 0, sizeof(addr));
- addr.l2_family = AF_BLUETOOTH;
- bacpy(&addr.l2_bdaddr, (void *) bdaddr);
- addr.l2_bdaddr_type = bdaddr_type;
- addr.l2_psm = htobs(psm);
- addr.l2_cid = htobs(cid);
- err = connect(sk, (struct sockaddr *) &addr, sizeof(addr));
- if (err < 0 && !(errno == EAGAIN || errno == EINPROGRESS)) {
- err = -errno;
- tester_warn("Can't connect socket: %s (%d)", strerror(errno),
- errno);
- return err;
- }
- return 0;
- }
- static int connect_l2cap_sock(struct test_data *data, int sk, uint16_t psm,
- uint16_t cid)
- {
- const struct l2cap_data *l2data = data->test_data;
- const uint8_t *client_bdaddr;
- uint8_t bdaddr_type;
- if (l2data->client_bdaddr != NULL)
- client_bdaddr = l2data->client_bdaddr;
- else
- client_bdaddr = hciemu_get_client_bdaddr(data->hciemu);
- if (!client_bdaddr) {
- tester_warn("No client bdaddr");
- return -ENODEV;
- }
- if (l2data && l2data->addr_type_avail)
- bdaddr_type = l2data->addr_type;
- else if (data->hciemu_type == HCIEMU_TYPE_LE)
- bdaddr_type = BDADDR_LE_PUBLIC;
- else
- bdaddr_type = BDADDR_BREDR;
- return connect_l2cap_impl(sk, client_bdaddr, bdaddr_type, psm, cid);
- }
- static void client_l2cap_connect_cb(uint16_t handle, uint16_t cid,
- void *user_data)
- {
- struct test_data *data = user_data;
- data->dcid = cid;
- data->handle = handle;
- }
- static void client_l2cap_disconnect_cb(void *user_data)
- {
- struct test_data *data = user_data;
- data->host_disconnected = true;
- }
- static void direct_adv_cmd_complete(uint16_t opcode, const void *param,
- uint8_t len, void *user_data)
- {
- struct test_data *data = tester_get_data();
- const struct bt_hci_cmd_le_set_adv_parameters *cp;
- const uint8_t *expect_bdaddr;
- if (opcode != BT_HCI_CMD_LE_SET_ADV_PARAMETERS)
- return;
- tester_print("Received advertising parameters HCI command");
- cp = param;
- /* Advertising as client should be direct advertising */
- if (cp->type != 0x01) {
- tester_warn("Invalid advertising type");
- tester_test_failed();
- return;
- }
- expect_bdaddr = hciemu_get_client_bdaddr(data->hciemu);
- if (memcmp(expect_bdaddr, cp->direct_addr, 6)) {
- tester_warn("Invalid direct address in adv params");
- tester_test_failed();
- return;
- }
- tester_test_passed();
- }
- static void test_connect(const void *test_data)
- {
- struct test_data *data = tester_get_data();
- const struct l2cap_data *l2data = data->test_data;
- GIOChannel *io;
- int sk;
- if (l2data->server_psm) {
- struct bthost *bthost = hciemu_client_get_host(data->hciemu);
- bthost_l2cap_connect_cb host_connect_cb = NULL;
- bthost_l2cap_disconnect_cb host_disconnect_cb = NULL;
- if (l2data->data_len)
- host_connect_cb = client_l2cap_connect_cb;
- if (l2data->shut_sock_wr)
- host_disconnect_cb = client_l2cap_disconnect_cb;
- bthost_add_l2cap_server(bthost, l2data->server_psm,
- host_connect_cb, host_disconnect_cb,
- data);
- }
- if (l2data->direct_advertising)
- hciemu_add_central_post_command_hook(data->hciemu,
- direct_adv_cmd_complete, NULL);
- sk = create_l2cap_sock(data, 0, l2data->cid, l2data->sec_level,
- l2data->mode);
- if (sk < 0) {
- if (sk == -ENOPROTOOPT)
- tester_test_abort();
- else
- tester_test_failed();
- return;
- }
- if (connect_l2cap_sock(data, sk, l2data->client_psm,
- l2data->cid) < 0) {
- close(sk);
- tester_test_failed();
- return;
- }
- io = g_io_channel_unix_new(sk);
- g_io_channel_set_close_on_unref(io, TRUE);
- data->io_id = g_io_add_watch(io, G_IO_OUT, l2cap_connect_cb, NULL);
- g_io_channel_unref(io);
- tester_print("Connect in progress");
- }
- static void test_connect_reject(const void *test_data)
- {
- struct test_data *data = tester_get_data();
- const struct l2cap_data *l2data = data->test_data;
- int sk;
- sk = create_l2cap_sock(data, 0, l2data->cid, l2data->sec_level,
- l2data->mode);
- if (sk < 0) {
- tester_test_failed();
- return;
- }
- if (connect_l2cap_sock(data, sk, l2data->client_psm,
- l2data->cid) < 0)
- tester_test_passed();
- else
- tester_test_failed();
- close(sk);
- }
- static int connect_socket(const uint8_t *client_bdaddr, GIOFunc connect_cb,
- bool defer)
- {
- struct test_data *data = tester_get_data();
- const struct l2cap_data *l2data = data->test_data;
- GIOChannel *io;
- int sk;
- sk = create_l2cap_sock(data, 0, l2data->cid, l2data->sec_level,
- l2data->mode);
- if (sk < 0) {
- tester_print("Error in create_l2cap_sock");
- if (sk == -ENOPROTOOPT)
- tester_test_abort();
- else
- tester_test_failed();
- return -1;
- }
- if (defer) {
- int opt = 1;
- if (setsockopt(sk, SOL_BLUETOOTH, BT_DEFER_SETUP, &opt,
- sizeof(opt)) < 0) {
- tester_print("Can't enable deferred setup: %s (%d)",
- strerror(errno), errno);
- tester_test_failed();
- return -1;
- }
- }
- if (connect_l2cap_impl(sk, client_bdaddr, BDADDR_LE_PUBLIC,
- l2data->client_psm, l2data->cid) < 0) {
- tester_print("Error in connect_l2cap_sock");
- close(sk);
- tester_test_failed();
- return -1;
- }
- if (connect_cb) {
- io = g_io_channel_unix_new(sk);
- g_io_channel_set_close_on_unref(io, TRUE);
- data->io_id = g_io_add_watch(io, G_IO_OUT, connect_cb, NULL);
- g_io_channel_unref(io);
- }
- tester_print("Connect in progress, sk = %d %s", sk,
- defer ? "(deferred)" : "");
- return sk;
- }
- static gboolean test_close_socket_1_part_3(gpointer arg)
- {
- struct test_data *data = tester_get_data();
- tester_print("Checking whether scan was properly stopped...");
- if (data->sk != -1) {
- tester_print("Error - scan was not enabled yet");
- tester_test_failed();
- return FALSE;
- }
- if (hciemu_get_central_le_scan_enable(data->hciemu)) {
- tester_print("Delayed check whether scann is off failed");
- tester_test_failed();
- return FALSE;
- }
- tester_test_passed();
- return FALSE;
- }
- static gboolean test_close_socket_1_part_2(gpointer args)
- {
- struct test_data *data = tester_get_data();
- int sk = data->sk;
- tester_print("Will close socket during scan phase...");
- /* We tried to conect to LE device that is not advertising. It
- * was added to kernel accept list, and scan was started. We
- * should be still scanning.
- */
- if (!hciemu_get_central_le_scan_enable(data->hciemu)) {
- tester_print("Error - should be still scanning");
- tester_test_failed();
- return FALSE;
- }
- /* Calling close() should remove device from accept list, and stop
- * the scan.
- */
- if (close(sk) < 0) {
- tester_print("Error when closing socket");
- tester_test_failed();
- return FALSE;
- }
- data->sk = -1;
- /* tester_test_passed will be called when scan is stopped. */
- return FALSE;
- }
- static gboolean test_close_socket_2_part_3(gpointer arg)
- {
- struct test_data *data = tester_get_data();
- int sk = data->sk;
- int err;
- /* Scan should be already over, we're trying to create connection */
- if (hciemu_get_central_le_scan_enable(data->hciemu)) {
- tester_print("Error - should no longer scan");
- tester_test_failed();
- return FALSE;
- }
- /* Calling close() should eventually cause CMD_LE_CREATE_CONN_CANCEL */
- err = close(sk);
- if (err < 0) {
- tester_print("Error when closing socket");
- tester_test_failed();
- return FALSE;
- }
- /* CMD_LE_CREATE_CONN_CANCEL will trigger test pass. */
- return FALSE;
- }
- static bool test_close_socket_cc_hook(const void *data, uint16_t len,
- void *user_data)
- {
- return false;
- }
- static gboolean test_close_socket_2_part_2(gpointer arg)
- {
- struct test_data *data = tester_get_data();
- struct bthost *bthost = hciemu_client_get_host(data->hciemu);
- /* Make sure CMD_LE_CREATE_CONN will not immediately result in
- * BT_HCI_EVT_CONN_COMPLETE.
- */
- hciemu_add_hook(data->hciemu, HCIEMU_HOOK_PRE_EVT,
- BT_HCI_CMD_LE_CREATE_CONN, test_close_socket_cc_hook, NULL);
- /* Advertise once. After that, kernel should stop scanning, and trigger
- * BT_HCI_CMD_LE_CREATE_CONN_CANCEL.
- */
- bthost_set_adv_enable(bthost, 0x01);
- bthost_set_adv_enable(bthost, 0x00);
- return FALSE;
- }
- static void test_close_socket_scan_enabled(void)
- {
- struct test_data *data = tester_get_data();
- const struct l2cap_data *l2data = data->test_data;
- if (l2data == &le_client_close_socket_test_1)
- g_idle_add(test_close_socket_1_part_2, NULL);
- else if (l2data == &le_client_close_socket_test_2)
- g_idle_add(test_close_socket_2_part_2, NULL);
- }
- static void test_close_socket_scan_disabled(void)
- {
- struct test_data *data = tester_get_data();
- const struct l2cap_data *l2data = data->test_data;
- if (l2data == &le_client_close_socket_test_1)
- g_idle_add(test_close_socket_1_part_3, NULL);
- else if (l2data == &le_client_close_socket_test_2)
- g_idle_add(test_close_socket_2_part_3, NULL);
- }
- static void test_close_socket_conn_cancel(void)
- {
- struct test_data *data = tester_get_data();
- const struct l2cap_data *l2data = data->test_data;
- if (l2data == &le_client_close_socket_test_2)
- tester_test_passed();
- }
- static void test_close_socket_router(uint16_t opcode, const void *param,
- uint8_t length, void *user_data)
- {
- /* tester_print("HCI Command 0x%04x length %u", opcode, length); */
- if (opcode == BT_HCI_CMD_LE_SET_SCAN_ENABLE) {
- const struct bt_hci_cmd_le_set_scan_enable *scan_params = param;
- if (scan_params->enable == true)
- test_close_socket_scan_enabled();
- else
- test_close_socket_scan_disabled();
- } else if (opcode == BT_HCI_CMD_LE_CREATE_CONN_CANCEL) {
- test_close_socket_conn_cancel();
- }
- }
- static void test_close_socket(const void *test_data)
- {
- struct test_data *data = tester_get_data();
- const struct l2cap_data *l2data = data->test_data;
- const uint8_t *client_bdaddr;
- hciemu_add_central_post_command_hook(data->hciemu,
- test_close_socket_router, data);
- if (l2data->client_bdaddr != NULL)
- client_bdaddr = l2data->client_bdaddr;
- else
- client_bdaddr = hciemu_get_client_bdaddr(data->hciemu);
- data->sk = connect_socket(client_bdaddr, NULL, false);
- }
- static uint8_t test_2_connect_cb_cnt;
- static gboolean test_2_connect_cb(GIOChannel *io, GIOCondition cond,
- gpointer user_data)
- {
- struct test_data *data = tester_get_data();
- const struct l2cap_data *l2data = data->test_data;
- int err, sk_err, sk;
- socklen_t len = sizeof(sk_err);
- data->io_id = 0;
- sk = g_io_channel_unix_get_fd(io);
- if (getsockopt(sk, SOL_SOCKET, SO_ERROR, &sk_err, &len) < 0)
- err = -errno;
- else
- err = -sk_err;
- if (err < 0) {
- tester_warn("Connect failed: %s (%d)", strerror(-err), -err);
- tester_test_failed();
- return FALSE;
- }
- tester_print("Successfully connected");
- test_2_connect_cb_cnt++;
- if (test_2_connect_cb_cnt == 2) {
- close(data->sk);
- close(data->sk2);
- tester_test_passed();
- }
- if (l2data->close_1 && test_2_connect_cb_cnt == 1) {
- close(data->sk2);
- tester_test_passed();
- }
- return FALSE;
- }
- static gboolean enable_advertising(gpointer args)
- {
- struct test_data *data = tester_get_data();
- struct bthost *bthost = hciemu_client_get_host(data->hciemu);
- bthost_set_adv_enable(bthost, 0x01);
- return FALSE;
- }
- static void test_connect_2_part_2(void)
- {
- struct test_data *data = tester_get_data();
- const struct l2cap_data *l2data = data->test_data;
- const uint8_t *client_bdaddr;
- client_bdaddr = hciemu_get_client_bdaddr(data->hciemu);
- data->sk2 = connect_socket(client_bdaddr, test_2_connect_cb, false);
- if (l2data->close_1) {
- tester_print("Closing first socket! %d", data->sk);
- close(data->sk);
- }
- g_idle_add(enable_advertising, NULL);
- }
- static uint8_t test_scan_enable_counter;
- static void test_connect_2_router(uint16_t opcode, const void *param,
- uint8_t length, void *user_data)
- {
- const struct bt_hci_cmd_le_set_scan_enable *scan_params = param;
- tester_print("HCI Command 0x%04x length %u", opcode, length);
- if (opcode == BT_HCI_CMD_LE_SET_SCAN_ENABLE &&
- scan_params->enable == true) {
- test_scan_enable_counter++;
- if (test_scan_enable_counter == 1)
- test_connect_2_part_2();
- else if (test_scan_enable_counter == 2)
- g_idle_add(enable_advertising, NULL);
- }
- }
- static void test_connect_2(const void *test_data)
- {
- struct test_data *data = tester_get_data();
- const struct l2cap_data *l2data = data->test_data;
- const uint8_t *client_bdaddr;
- bool defer;
- test_2_connect_cb_cnt = 0;
- test_scan_enable_counter = 0;
- hciemu_add_central_post_command_hook(data->hciemu,
- test_connect_2_router, data);
- if (l2data->server_psm) {
- struct bthost *bthost = hciemu_client_get_host(data->hciemu);
- if (!l2data->data_len)
- bthost_add_l2cap_server(bthost, l2data->server_psm,
- NULL, NULL, NULL);
- }
- defer = (l2data->mode == BT_MODE_EXT_FLOWCTL);
- client_bdaddr = hciemu_get_client_bdaddr(data->hciemu);
- if (l2data->close_1)
- data->sk = connect_socket(client_bdaddr, NULL, defer);
- else
- data->sk = connect_socket(client_bdaddr, test_2_connect_cb,
- defer);
- }
- static gboolean l2cap_listen_cb(GIOChannel *io, GIOCondition cond,
- gpointer user_data)
- {
- struct test_data *data = tester_get_data();
- const struct l2cap_data *l2data = data->test_data;
- int sk, new_sk;
- data->io_id = 0;
- sk = g_io_channel_unix_get_fd(io);
- new_sk = accept(sk, NULL, NULL);
- if (new_sk < 0) {
- tester_warn("accept failed: %s (%u)", strerror(errno), errno);
- tester_test_failed();
- return FALSE;
- }
- if (!check_mtu(data, new_sk)) {
- tester_test_failed();
- return FALSE;
- }
- if (l2data->read_data) {
- struct bthost *bthost;
- GIOChannel *new_io;
- new_io = g_io_channel_unix_new(new_sk);
- g_io_channel_set_close_on_unref(new_io, TRUE);
- bthost = hciemu_client_get_host(data->hciemu);
- g_io_add_watch(new_io, G_IO_IN, server_received_data, NULL);
- bthost_send_cid(bthost, data->handle, data->dcid,
- l2data->read_data, l2data->data_len);
- g_io_channel_unref(new_io);
- return FALSE;
- } else if (l2data->write_data) {
- struct bthost *bthost;
- ssize_t ret;
- bthost = hciemu_client_get_host(data->hciemu);
- bthost_add_cid_hook(bthost, data->handle, data->scid,
- server_bthost_received_data, NULL);
- ret = write(new_sk, l2data->write_data, l2data->data_len);
- close(new_sk);
- if (ret != l2data->data_len) {
- tester_warn("Unable to write all data");
- tester_test_failed();
- }
- return FALSE;
- }
- tester_print("Successfully connected");
- close(new_sk);
- tester_test_passed();
- return FALSE;
- }
- static void client_l2cap_rsp(uint8_t code, const void *data, uint16_t len,
- void *user_data)
- {
- struct test_data *test_data = user_data;
- const struct l2cap_data *l2data = test_data->test_data;
- tester_print("Client received response code 0x%02x", code);
- if (code != l2data->expect_cmd_code) {
- tester_warn("Unexpected L2CAP response code (expected 0x%02x)",
- l2data->expect_cmd_code);
- return;
- }
- if (code == BT_L2CAP_PDU_CONN_RSP) {
- const struct bt_l2cap_pdu_conn_rsp *rsp = data;
- if (len == sizeof(rsp) && !rsp->result && !rsp->status)
- return;
- test_data->dcid = rsp->dcid;
- test_data->scid = rsp->scid;
- if (l2data->data_len)
- return;
- }
- if (!l2data->expect_cmd) {
- tester_test_passed();
- return;
- }
- if (l2data->expect_cmd_len != len) {
- tester_warn("Unexpected L2CAP response length (%u != %u)",
- len, l2data->expect_cmd_len);
- goto failed;
- }
- if (memcmp(l2data->expect_cmd, data, len) != 0) {
- tester_warn("Unexpected L2CAP response");
- goto failed;
- }
- tester_test_passed();
- return;
- failed:
- tester_test_failed();
- }
- static void send_req_new_conn(uint16_t handle, void *user_data)
- {
- struct test_data *data = user_data;
- const struct l2cap_data *l2data = data->test_data;
- struct bthost *bthost;
- tester_print("New client connection with handle 0x%04x", handle);
- data->handle = handle;
- if (l2data->send_cmd) {
- bthost_l2cap_rsp_cb cb;
- if (l2data->expect_cmd_code)
- cb = client_l2cap_rsp;
- else
- cb = NULL;
- tester_print("Sending L2CAP Request from client");
- bthost = hciemu_client_get_host(data->hciemu);
- bthost_l2cap_req(bthost, handle, l2data->send_cmd_code,
- l2data->send_cmd, l2data->send_cmd_len,
- cb, data);
- }
- }
- static void test_server(const void *test_data)
- {
- struct test_data *data = tester_get_data();
- const struct l2cap_data *l2data = data->test_data;
- const uint8_t *central_bdaddr;
- uint8_t addr_type;
- struct bthost *bthost;
- GIOChannel *io;
- int sk;
- if (l2data->server_psm || l2data->cid) {
- sk = create_l2cap_sock(data, l2data->server_psm,
- l2data->cid, l2data->sec_level,
- l2data->mode);
- if (sk < 0) {
- tester_test_failed();
- return;
- }
- if (listen(sk, 5) < 0) {
- tester_warn("listening on socket failed: %s (%u)",
- strerror(errno), errno);
- tester_test_failed();
- close(sk);
- return;
- }
- io = g_io_channel_unix_new(sk);
- g_io_channel_set_close_on_unref(io, TRUE);
- data->io_id = g_io_add_watch(io, G_IO_IN, l2cap_listen_cb,
- NULL);
- g_io_channel_unref(io);
- tester_print("Listening for connections");
- }
- central_bdaddr = hciemu_get_central_bdaddr(data->hciemu);
- if (!central_bdaddr) {
- tester_warn("No central bdaddr");
- tester_test_failed();
- return;
- }
- bthost = hciemu_client_get_host(data->hciemu);
- bthost_set_connect_cb(bthost, send_req_new_conn, data);
- if (data->hciemu_type == HCIEMU_TYPE_BREDR)
- addr_type = BDADDR_BREDR;
- else
- addr_type = BDADDR_LE_PUBLIC;
- bthost_hci_connect(bthost, central_bdaddr, addr_type);
- }
- static void test_getpeername_not_connected(const void *test_data)
- {
- struct test_data *data = tester_get_data();
- struct sockaddr_l2 addr;
- socklen_t len;
- int sk;
- sk = create_l2cap_sock(data, 0, 0, 0, 0);
- if (sk < 0) {
- tester_test_failed();
- return;
- }
- len = sizeof(addr);
- if (getpeername(sk, (struct sockaddr *) &addr, &len) == 0) {
- tester_warn("getpeername succeeded on non-connected socket");
- tester_test_failed();
- goto done;
- }
- if (errno != ENOTCONN) {
- tester_warn("Unexpexted getpeername error: %s (%d)",
- strerror(errno), errno);
- tester_test_failed();
- goto done;
- }
- tester_test_passed();
- done:
- close(sk);
- }
- int main(int argc, char *argv[])
- {
- tester_init(&argc, &argv);
- test_l2cap_bredr("Basic L2CAP Socket - Success", NULL,
- setup_powered_client, test_basic);
- test_l2cap_bredr("Non-connected getpeername - Failure", NULL,
- setup_powered_client,
- test_getpeername_not_connected);
- test_l2cap_bredr("L2CAP BR/EDR Client - Success",
- &client_connect_success_test,
- setup_powered_client, test_connect);
- test_l2cap_bredr("L2CAP BR/EDR Client SSP - Success 1",
- &client_connect_ssp_success_test_1,
- setup_powered_client, test_connect);
- test_l2cap_bredr("L2CAP BR/EDR Client SSP - Success 2",
- &client_connect_ssp_success_test_2,
- setup_powered_client, test_connect);
- test_l2cap_bredr("L2CAP BR/EDR Client PIN Code - Success",
- &client_connect_pin_success_test,
- setup_powered_client, test_connect);
- test_l2cap_bredr("L2CAP BR/EDR Client - Read Success",
- &client_connect_read_success_test,
- setup_powered_client, test_connect);
- test_l2cap_bredr("L2CAP BR/EDR Client - Write Success",
- &client_connect_write_success_test,
- setup_powered_client, test_connect);
- test_l2cap_bredr("L2CAP BR/EDR Client - Invalid PSM 1",
- &client_connect_nval_psm_test_1,
- setup_powered_client, test_connect);
- test_l2cap_bredr("L2CAP BR/EDR Client - Invalid PSM 2",
- &client_connect_nval_psm_test_2,
- setup_powered_client, test_connect);
- test_l2cap_bredr("L2CAP BR/EDR Client - Invalid PSM 3",
- &client_connect_nval_psm_test_3,
- setup_powered_client, test_connect);
- test_l2cap_bredr("L2CAP BR/EDR Client - Socket Shut WR Success",
- &client_connect_shut_wr_success_test,
- setup_powered_client, test_connect);
- test_l2cap_bredr("L2CAP BR/EDR Server - Success",
- &l2cap_server_success_test,
- setup_powered_server, test_server);
- test_l2cap_bredr("L2CAP BR/EDR Server - Read Success",
- &l2cap_server_read_success_test,
- setup_powered_server, test_server);
- test_l2cap_bredr("L2CAP BR/EDR Server - Write Success",
- &l2cap_server_write_success_test,
- setup_powered_server, test_server);
- test_l2cap_bredr("L2CAP BR/EDR Server - Security Block",
- &l2cap_server_sec_block_test,
- setup_powered_server, test_server);
- test_l2cap_bredr("L2CAP BR/EDR Server - Invalid PSM",
- &l2cap_server_nval_psm_test,
- setup_powered_server, test_server);
- test_l2cap_bredr("L2CAP BR/EDR Server - Invalid PDU",
- &l2cap_server_nval_pdu_test1,
- setup_powered_server, test_server);
- test_l2cap_bredr("L2CAP BR/EDR Server - Invalid Disconnect CID",
- &l2cap_server_nval_cid_test1,
- setup_powered_server, test_server);
- test_l2cap_bredr("L2CAP BR/EDR Server - Invalid Config CID",
- &l2cap_server_nval_cid_test2,
- setup_powered_server, test_server);
- test_l2cap_le("L2CAP LE Client - Success",
- &le_client_connect_success_test_1,
- setup_powered_client, test_connect);
- test_l2cap_le("L2CAP LE Client, Direct Advertising - Success",
- &le_client_connect_adv_success_test_1,
- setup_powered_client, test_connect);
- test_l2cap_le("L2CAP LE Client SMP - Success",
- &le_client_connect_success_test_2,
- setup_powered_client, test_connect);
- test_l2cap_le("L2CAP LE Client - Command Reject",
- &le_client_connect_reject_test_1,
- setup_powered_client, test_connect);
- test_l2cap_bredr("L2CAP LE Client - Connection Reject",
- &le_client_connect_reject_test_2,
- setup_powered_client, test_connect_reject);
- test_l2cap_le("L2CAP LE Client - Close socket 1",
- &le_client_close_socket_test_1,
- setup_powered_client,
- test_close_socket);
- test_l2cap_le("L2CAP LE Client - Close socket 2",
- &le_client_close_socket_test_2,
- setup_powered_client,
- test_close_socket);
- test_l2cap_le("L2CAP LE Client - Open two sockets",
- &le_client_2_same_client,
- setup_powered_client,
- test_connect_2);
- test_l2cap_le("L2CAP LE Client - Open two sockets close one",
- &le_client_2_close_1,
- setup_powered_client,
- test_connect_2);
- test_l2cap_le("L2CAP LE Client - Invalid PSM",
- &le_client_connect_nval_psm_test,
- setup_powered_client, test_connect);
- test_l2cap_le("L2CAP LE Server - Success", &le_server_success_test,
- setup_powered_server, test_server);
- test_l2cap_le("L2CAP LE Server - Nval SCID", &le_server_nval_scid_test,
- setup_powered_server, test_server);
- test_l2cap_le("L2CAP Ext-Flowctl Client - Success",
- &ext_flowctl_client_connect_success_test_1,
- setup_powered_client, test_connect);
- test_l2cap_le("L2CAP Ext-Flowctl Client, Direct Advertising - Success",
- &ext_flowctl_client_connect_adv_success_test_1,
- setup_powered_client, test_connect);
- test_l2cap_le("L2CAP Ext-Flowctl Client SMP - Success",
- &ext_flowctl_client_connect_success_test_2,
- setup_powered_client, test_connect);
- test_l2cap_le("L2CAP Ext-Flowctl Client - Command Reject",
- &ext_flowctl_client_connect_reject_test_1,
- setup_powered_client, test_connect);
- test_l2cap_le("L2CAP Ext-Flowctl Client - Open two sockets",
- &ext_flowctl_client_2,
- setup_powered_client,
- test_connect_2);
- test_l2cap_le("L2CAP Ext-Flowctl Client - Open two sockets close one",
- &ext_flowctl_client_2_close_1,
- setup_powered_client,
- test_connect_2);
- test_l2cap_le("L2CAP LE ATT Client - Success",
- &le_att_client_connect_success_test_1,
- setup_powered_client, test_connect);
- test_l2cap_le("L2CAP LE ATT Server - Success",
- &le_att_server_success_test_1,
- setup_powered_server, test_server);
- return tester_run();
- }
|