test-gattrib.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2014 Google, Inc.
  7. *
  8. *
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. #include <unistd.h>
  14. #include <stdlib.h>
  15. #include <stdbool.h>
  16. #include <inttypes.h>
  17. #include <string.h>
  18. #include <fcntl.h>
  19. #include <sys/socket.h>
  20. #include <glib.h>
  21. #include "src/shared/util.h"
  22. #include "lib/bluetooth.h"
  23. #include "lib/uuid.h"
  24. #include "attrib/att.h"
  25. #include "attrib/gattrib.h"
  26. #include "src/log.h"
  27. #define DEFAULT_MTU 23
  28. #define data(args...) ((const unsigned char[]) { args })
  29. struct test_pdu {
  30. bool valid;
  31. bool sent;
  32. bool received;
  33. const uint8_t *data;
  34. size_t size;
  35. };
  36. #define pdu(args...) \
  37. { \
  38. .valid = true, \
  39. .sent = false, \
  40. .received = false, \
  41. .data = data(args), \
  42. .size = sizeof(data(args)), \
  43. }
  44. struct context {
  45. GMainLoop *main_loop;
  46. GIOChannel *att_io;
  47. GIOChannel *server_io;
  48. GAttrib *att;
  49. };
  50. static void setup_context(struct context *cxt, gconstpointer data)
  51. {
  52. int err, sv[2];
  53. cxt->main_loop = g_main_loop_new(NULL, FALSE);
  54. g_assert(cxt->main_loop != NULL);
  55. err = socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, sv);
  56. g_assert(err == 0);
  57. cxt->att_io = g_io_channel_unix_new(sv[0]);
  58. g_assert(cxt->att_io != NULL);
  59. g_io_channel_set_close_on_unref(cxt->att_io, TRUE);
  60. cxt->server_io = g_io_channel_unix_new(sv[1]);
  61. g_assert(cxt->server_io != NULL);
  62. g_io_channel_set_close_on_unref(cxt->server_io, TRUE);
  63. g_io_channel_set_encoding(cxt->server_io, NULL, NULL);
  64. g_io_channel_set_buffered(cxt->server_io, FALSE);
  65. cxt->att = g_attrib_new(cxt->att_io, DEFAULT_MTU, false);
  66. g_assert(cxt->att != NULL);
  67. }
  68. static void teardown_context(struct context *cxt, gconstpointer data)
  69. {
  70. if (cxt->att)
  71. g_attrib_unref(cxt->att);
  72. g_io_channel_unref(cxt->server_io);
  73. g_io_channel_unref(cxt->att_io);
  74. g_main_loop_unref(cxt->main_loop);
  75. }
  76. static void test_debug(const char *str, void *user_data)
  77. {
  78. const char *prefix = user_data;
  79. g_print("%s%s\n", prefix, str);
  80. }
  81. static void destroy_canary_increment(gpointer data)
  82. {
  83. int *canary = data;
  84. (*canary)++;
  85. }
  86. static void test_refcount(struct context *cxt, gconstpointer unused)
  87. {
  88. GAttrib *extra_ref;
  89. int destroy_canary = 0;
  90. g_attrib_set_destroy_function(cxt->att, destroy_canary_increment,
  91. &destroy_canary);
  92. extra_ref = g_attrib_ref(cxt->att);
  93. g_assert(extra_ref == cxt->att);
  94. g_assert(destroy_canary == 0);
  95. g_attrib_unref(extra_ref);
  96. g_assert(destroy_canary == 0);
  97. g_attrib_unref(cxt->att);
  98. g_assert(destroy_canary == 1);
  99. /* Avoid a double-free from the teardown function */
  100. cxt->att = NULL;
  101. }
  102. static void test_get_channel(struct context *cxt, gconstpointer unused)
  103. {
  104. GIOChannel *chan;
  105. chan = g_attrib_get_channel(cxt->att);
  106. g_assert(chan == cxt->att_io);
  107. }
  108. struct expect_response {
  109. struct test_pdu expect;
  110. struct test_pdu respond;
  111. GSourceFunc receive_cb;
  112. gpointer user_data;
  113. };
  114. static gboolean test_client(GIOChannel *channel, GIOCondition cond,
  115. gpointer data)
  116. {
  117. struct expect_response *cr = data;
  118. int fd;
  119. uint8_t buf[256];
  120. ssize_t len;
  121. int cmp;
  122. if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
  123. return FALSE;
  124. fd = g_io_channel_unix_get_fd(channel);
  125. len = read(fd, buf, sizeof(buf));
  126. g_assert(len > 0);
  127. g_assert_cmpint(len, ==, cr->expect.size);
  128. if (g_test_verbose())
  129. util_hexdump('?', cr->expect.data, cr->expect.size,
  130. test_debug, "test_client: ");
  131. cmp = memcmp(cr->expect.data, buf, len);
  132. g_assert(cmp == 0);
  133. cr->expect.received = true;
  134. if (cr->receive_cb != NULL)
  135. cr->receive_cb(cr->user_data);
  136. if (cr->respond.valid) {
  137. if (g_test_verbose())
  138. util_hexdump('<', cr->respond.data, cr->respond.size,
  139. test_debug, "test_client: ");
  140. len = write(fd, cr->respond.data, cr->respond.size);
  141. g_assert_cmpint(len, ==, cr->respond.size);
  142. cr->respond.sent = true;
  143. }
  144. return TRUE;
  145. }
  146. struct result_data {
  147. guint8 status;
  148. guint8 *pdu;
  149. guint16 len;
  150. GSourceFunc complete_cb;
  151. gpointer user_data;
  152. };
  153. static void result_canary(guint8 status, const guint8 *pdu, guint16 len,
  154. gpointer data)
  155. {
  156. struct result_data *result = data;
  157. result->status = status;
  158. result->pdu = g_malloc0(len);
  159. memcpy(result->pdu, pdu, len);
  160. result->len = len;
  161. if (g_test_verbose())
  162. util_hexdump('<', pdu, len, test_debug, "result_canary: ");
  163. if (result->complete_cb != NULL)
  164. result->complete_cb(result->user_data);
  165. }
  166. static gboolean context_stop_main_loop(gpointer user_data)
  167. {
  168. struct context *cxt = user_data;
  169. g_main_loop_quit(cxt->main_loop);
  170. return FALSE;
  171. }
  172. static void test_send(struct context *cxt, gconstpointer unused)
  173. {
  174. int cmp;
  175. struct result_data results;
  176. struct expect_response data = {
  177. .expect = pdu(0x02, 0x00, 0x02),
  178. .respond = pdu(0x03, 0x02, 0x03, 0x04),
  179. .receive_cb = NULL,
  180. .user_data = NULL,
  181. };
  182. g_io_add_watch(cxt->server_io, G_IO_IN | G_IO_HUP | G_IO_ERR |
  183. G_IO_NVAL, test_client, &data);
  184. results.complete_cb = context_stop_main_loop;
  185. results.user_data = cxt;
  186. g_attrib_send(cxt->att, 0, data.expect.data, data.expect.size,
  187. result_canary, (gpointer) &results, NULL);
  188. g_main_loop_run(cxt->main_loop);
  189. g_assert(results.pdu != NULL);
  190. g_assert_cmpint(results.len, ==, data.respond.size);
  191. cmp = memcmp(results.pdu, data.respond.data, results.len);
  192. g_assert(cmp == 0);
  193. g_free(results.pdu);
  194. }
  195. struct event_info {
  196. struct context *context;
  197. int event_id;
  198. };
  199. static gboolean cancel_existing_attrib_event(gpointer user_data)
  200. {
  201. struct event_info *info = user_data;
  202. gboolean canceled;
  203. canceled = g_attrib_cancel(info->context->att, info->event_id);
  204. g_assert(canceled);
  205. g_idle_add(context_stop_main_loop, info->context);
  206. return FALSE;
  207. }
  208. static void test_cancel(struct context *cxt, gconstpointer unused)
  209. {
  210. gboolean canceled;
  211. struct result_data results;
  212. struct event_info info;
  213. struct expect_response data = {
  214. .expect = pdu(0x02, 0x00, 0x02),
  215. .respond = pdu(0x03, 0x02, 0x03, 0x04),
  216. };
  217. g_io_add_watch(cxt->server_io,
  218. G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
  219. test_client, &data);
  220. results.pdu = NULL;
  221. info.context = cxt;
  222. info.event_id = g_attrib_send(cxt->att, 0, data.expect.data,
  223. data.expect.size, result_canary,
  224. &results, NULL);
  225. data.receive_cb = cancel_existing_attrib_event;
  226. data.user_data = &info;
  227. g_main_loop_run(cxt->main_loop);
  228. g_assert(results.pdu == NULL);
  229. results.pdu = NULL;
  230. data.expect.received = false;
  231. data.respond.sent = false;
  232. info.event_id = g_attrib_send(cxt->att, 0, data.expect.data,
  233. data.expect.size, result_canary,
  234. &results, NULL);
  235. canceled = g_attrib_cancel(cxt->att, info.event_id);
  236. g_assert(canceled);
  237. g_idle_add(context_stop_main_loop, info.context);
  238. g_main_loop_run(cxt->main_loop);
  239. g_assert(!data.expect.received);
  240. g_assert(!data.respond.sent);
  241. g_assert(results.pdu == NULL);
  242. /* Invalid ID */
  243. canceled = g_attrib_cancel(cxt->att, 42);
  244. g_assert(!canceled);
  245. }
  246. static void send_test_pdus(gpointer context, struct test_pdu *pdus)
  247. {
  248. struct context *cxt = context;
  249. size_t len;
  250. int fd;
  251. struct test_pdu *cur_pdu;
  252. fd = g_io_channel_unix_get_fd(cxt->server_io);
  253. for (cur_pdu = pdus; cur_pdu->valid; cur_pdu++)
  254. cur_pdu->sent = false;
  255. for (cur_pdu = pdus; cur_pdu->valid; cur_pdu++) {
  256. if (g_test_verbose())
  257. util_hexdump('>', cur_pdu->data, cur_pdu->size,
  258. test_debug, "send_test_pdus: ");
  259. len = write(fd, cur_pdu->data, cur_pdu->size);
  260. g_assert_cmpint(len, ==, cur_pdu->size);
  261. cur_pdu->sent = true;
  262. }
  263. g_idle_add(context_stop_main_loop, cxt);
  264. g_main_loop_run(cxt->main_loop);
  265. }
  266. #define PDU_MTU_RESP pdu(ATT_OP_MTU_RESP, 0x17)
  267. #define PDU_FIND_INFO_REQ pdu(ATT_OP_FIND_INFO_REQ, 0x01, 0x00, 0xFF, 0xFF)
  268. #define PDU_NO_ATT_ERR pdu(ATT_OP_ERROR, ATT_OP_FIND_INFO_REQ, 0x00, 0x00, 0x0A)
  269. #define PDU_IND_NODATA pdu(ATT_OP_HANDLE_IND, 0x01, 0x00)
  270. #define PDU_INVALID_IND pdu(ATT_OP_HANDLE_IND, 0x14)
  271. #define PDU_IND_DATA pdu(ATT_OP_HANDLE_IND, 0x14, 0x00, 0x01)
  272. struct expect_test_data {
  273. struct test_pdu *expected;
  274. GAttrib *att;
  275. };
  276. static void notify_canary_expect(const guint8 *pdu, guint16 len, gpointer data)
  277. {
  278. struct expect_test_data *expect = data;
  279. struct test_pdu *expected = expect->expected;
  280. int cmp;
  281. if (g_test_verbose())
  282. util_hexdump('<', pdu, len, test_debug,
  283. "notify_canary_expect: ");
  284. while (expected->valid && expected->received)
  285. expected++;
  286. g_assert(expected->valid);
  287. if (g_test_verbose())
  288. util_hexdump('?', expected->data, expected->size, test_debug,
  289. "notify_canary_expect: ");
  290. g_assert_cmpint(expected->size, ==, len);
  291. cmp = memcmp(pdu, expected->data, expected->size);
  292. g_assert(cmp == 0);
  293. expected->received = true;
  294. if (pdu[0] == ATT_OP_FIND_INFO_REQ) {
  295. struct test_pdu no_attributes = PDU_NO_ATT_ERR;
  296. int reqid;
  297. reqid = g_attrib_send(expect->att, 0, no_attributes.data,
  298. no_attributes.size, NULL, NULL, NULL);
  299. g_assert(reqid != 0);
  300. }
  301. }
  302. static void test_register(struct context *cxt, gconstpointer user_data)
  303. {
  304. guint reg_id;
  305. gboolean canceled;
  306. struct test_pdu pdus[] = {
  307. /*
  308. * Unmatched PDU opcode
  309. * Unmatched handle (GATTRIB_ALL_REQS) */
  310. PDU_FIND_INFO_REQ,
  311. /*
  312. * Matched PDU opcode
  313. * Unmatched handle (GATTRIB_ALL_HANDLES) */
  314. PDU_IND_NODATA,
  315. /*
  316. * Matched PDU opcode
  317. * Invalid length? */
  318. PDU_INVALID_IND,
  319. /*
  320. * Matched PDU opcode
  321. * Matched handle */
  322. PDU_IND_DATA,
  323. { },
  324. };
  325. struct test_pdu req_pdus[] = { PDU_FIND_INFO_REQ, { } };
  326. struct test_pdu all_ind_pdus[] = {
  327. PDU_IND_NODATA,
  328. PDU_INVALID_IND,
  329. PDU_IND_DATA,
  330. { },
  331. };
  332. struct test_pdu followed_ind_pdus[] = { PDU_IND_DATA, { } };
  333. struct test_pdu *current_pdu;
  334. struct expect_test_data expect;
  335. expect.att = cxt->att;
  336. /*
  337. * Without registering anything, should be able to ignore everything but
  338. * an unexpected response. */
  339. send_test_pdus(cxt, pdus);
  340. if (g_test_verbose())
  341. g_print("ALL_REQS, ALL_HANDLES\r\n");
  342. expect.expected = req_pdus;
  343. reg_id = g_attrib_register(cxt->att, GATTRIB_ALL_REQS,
  344. GATTRIB_ALL_HANDLES, notify_canary_expect,
  345. &expect, NULL);
  346. send_test_pdus(cxt, pdus);
  347. canceled = g_attrib_unregister(cxt->att, reg_id);
  348. g_assert(canceled);
  349. for (current_pdu = req_pdus; current_pdu->valid; current_pdu++)
  350. g_assert(current_pdu->received);
  351. if (g_test_verbose())
  352. g_print("IND, ALL_HANDLES\r\n");
  353. expect.expected = all_ind_pdus;
  354. reg_id = g_attrib_register(cxt->att, ATT_OP_HANDLE_IND,
  355. GATTRIB_ALL_HANDLES, notify_canary_expect,
  356. &expect, NULL);
  357. send_test_pdus(cxt, pdus);
  358. canceled = g_attrib_unregister(cxt->att, reg_id);
  359. g_assert(canceled);
  360. for (current_pdu = all_ind_pdus; current_pdu->valid; current_pdu++)
  361. g_assert(current_pdu->received);
  362. if (g_test_verbose())
  363. g_print("IND, 0x0014\r\n");
  364. expect.expected = followed_ind_pdus;
  365. reg_id = g_attrib_register(cxt->att, ATT_OP_HANDLE_IND, 0x0014,
  366. notify_canary_expect, &expect, NULL);
  367. send_test_pdus(cxt, pdus);
  368. canceled = g_attrib_unregister(cxt->att, reg_id);
  369. g_assert(canceled);
  370. for (current_pdu = followed_ind_pdus; current_pdu->valid; current_pdu++)
  371. g_assert(current_pdu->received);
  372. canceled = g_attrib_unregister(cxt->att, reg_id);
  373. g_assert(!canceled);
  374. }
  375. static void test_buffers(struct context *cxt, gconstpointer unused)
  376. {
  377. size_t buflen;
  378. uint8_t *buf;
  379. gboolean success;
  380. buf = g_attrib_get_buffer(cxt->att, &buflen);
  381. g_assert(buf != 0);
  382. g_assert_cmpint(buflen, ==, DEFAULT_MTU);
  383. success = g_attrib_set_mtu(cxt->att, 5);
  384. g_assert(!success);
  385. success = g_attrib_set_mtu(cxt->att, 255);
  386. g_assert(success);
  387. buf = g_attrib_get_buffer(cxt->att, &buflen);
  388. g_assert(buf != 0);
  389. g_assert_cmpint(buflen, ==, 255);
  390. }
  391. int main(int argc, char *argv[])
  392. {
  393. g_test_init(&argc, &argv, NULL);
  394. if (g_test_verbose())
  395. __btd_log_init("*", 0);
  396. /*
  397. * Test the GAttrib API behavior
  398. */
  399. g_test_add("/gattrib/refcount", struct context, NULL, setup_context,
  400. test_refcount, teardown_context);
  401. g_test_add("/gattrib/get_channel", struct context, NULL, setup_context,
  402. test_get_channel, teardown_context);
  403. g_test_add("/gattrib/send", struct context, NULL, setup_context,
  404. test_send, teardown_context);
  405. g_test_add("/gattrib/cancel", struct context, NULL, setup_context,
  406. test_cancel, teardown_context);
  407. g_test_add("/gattrib/register", struct context, NULL, setup_context,
  408. test_register, teardown_context);
  409. g_test_add("/gattrib/buffers", struct context, NULL, setup_context,
  410. test_buffers, teardown_context);
  411. return g_test_run();
  412. }