l2cap-tester.c 52 KB

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