| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442 |
- // SPDX-License-Identifier: GPL-2.0-or-later
- /*
- *
- * BlueZ - Bluetooth protocol stack for Linux
- *
- * Copyright (C) 2012 Intel Corporation. All rights reserved.
- *
- *
- */
- #ifdef HAVE_CONFIG_H
- #include <config.h>
- #endif
- #include <stdbool.h>
- #include <unistd.h>
- #include <sys/socket.h>
- #include <glib.h>
- #include "lib/bluetooth.h"
- #include "lib/mgmt.h"
- #include "src/shared/mgmt.h"
- struct context {
- GMainLoop *main_loop;
- int fd;
- struct mgmt *mgmt_client;
- guint server_source;
- GList *handler_list;
- };
- enum action {
- ACTION_PASSED,
- ACTION_IGNORE,
- ACTION_RESPOND,
- };
- struct handler {
- const void *cmd_data;
- uint16_t cmd_size;
- const void *rsp_data;
- uint16_t rsp_size;
- uint8_t rsp_status;
- bool match_prefix;
- enum action action;
- };
- static void mgmt_debug(const char *str, void *user_data)
- {
- const char *prefix = user_data;
- g_print("%s%s\n", prefix, str);
- }
- static void context_quit(struct context *context)
- {
- g_main_loop_quit(context->main_loop);
- }
- static void check_actions(struct context *context, int fd,
- const void *data, uint16_t size)
- {
- GList *list;
- for (list = g_list_first(context->handler_list); list;
- list = g_list_next(list)) {
- struct handler *handler = list->data;
- int ret;
- if (handler->match_prefix) {
- if (size < handler->cmd_size)
- continue;
- } else {
- if (size != handler->cmd_size)
- continue;
- }
- if (memcmp(data, handler->cmd_data, handler->cmd_size))
- continue;
- switch (handler->action) {
- case ACTION_PASSED:
- context_quit(context);
- return;
- case ACTION_RESPOND:
- ret = write(fd, handler->rsp_data, handler->rsp_size);
- g_assert(ret >= 0);
- return;
- case ACTION_IGNORE:
- return;
- }
- }
- g_test_message("Command not handled\n");
- g_assert_not_reached();
- }
- static gboolean server_handler(GIOChannel *channel, GIOCondition cond,
- gpointer user_data)
- {
- struct context *context = user_data;
- unsigned char buf[512];
- ssize_t result;
- int fd;
- if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
- return FALSE;
- fd = g_io_channel_unix_get_fd(channel);
- result = read(fd, buf, sizeof(buf));
- if (result < 0)
- return FALSE;
- check_actions(context, fd, buf, result);
- return TRUE;
- }
- static struct context *create_context(void)
- {
- struct context *context = g_new0(struct context, 1);
- GIOChannel *channel;
- int err, sv[2];
- context->main_loop = g_main_loop_new(NULL, FALSE);
- g_assert(context->main_loop);
- err = socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, sv);
- g_assert(err == 0);
- context->fd = sv[0];
- channel = g_io_channel_unix_new(sv[0]);
- g_io_channel_set_close_on_unref(channel, TRUE);
- g_io_channel_set_encoding(channel, NULL, NULL);
- g_io_channel_set_buffered(channel, FALSE);
- context->server_source = g_io_add_watch(channel,
- G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
- server_handler, context);
- g_assert(context->server_source > 0);
- g_io_channel_unref(channel);
- context->mgmt_client = mgmt_new(sv[1]);
- g_assert(context->mgmt_client);
- if (g_test_verbose() == TRUE)
- mgmt_set_debug(context->mgmt_client,
- mgmt_debug, "mgmt: ", NULL);
- mgmt_set_close_on_unref(context->mgmt_client, true);
- return context;
- }
- static void execute_context(struct context *context)
- {
- g_main_loop_run(context->main_loop);
- g_list_free_full(context->handler_list, g_free);
- g_source_remove(context->server_source);
- mgmt_unref(context->mgmt_client);
- g_main_loop_unref(context->main_loop);
- g_free(context);
- }
- static void add_action(struct context *context,
- const void *cmd_data, uint16_t cmd_size,
- const void *rsp_data, uint16_t rsp_size,
- uint8_t rsp_status, bool match_prefix,
- enum action action)
- {
- struct handler *handler = g_new0(struct handler, 1);
- handler->cmd_data = cmd_data;
- handler->cmd_size = cmd_size;
- handler->rsp_data = rsp_data;
- handler->rsp_size = rsp_size;
- handler->rsp_status = rsp_status;
- handler->match_prefix = match_prefix;
- handler->action = action;
- context->handler_list = g_list_append(context->handler_list, handler);
- }
- struct command_test_data {
- uint16_t opcode;
- uint16_t index;
- uint16_t length;
- const void *param;
- const void *cmd_data;
- uint16_t cmd_size;
- const void *rsp_data;
- uint16_t rsp_size;
- uint8_t rsp_status;
- };
- static const unsigned char read_version_command[] =
- { 0x01, 0x00, 0xff, 0xff, 0x00, 0x00 };
- static const unsigned char read_version_response[] =
- { 0x01, 0x00, 0xff, 0xff, 0x06, 0x00,
- 0x01, 0x00, 0x00, 0x01, 0x06, 0x00 };
- static const struct command_test_data command_test_1 = {
- .opcode = MGMT_OP_READ_VERSION,
- .index = MGMT_INDEX_NONE,
- .cmd_data = read_version_command,
- .cmd_size = sizeof(read_version_command),
- .rsp_data = read_version_response,
- .rsp_size = sizeof(read_version_response),
- .rsp_status = MGMT_STATUS_SUCCESS,
- };
- static const unsigned char read_info_command[] =
- { 0x04, 0x00, 0x00, 0x02, 0x00, 0x00 };
- static const struct command_test_data command_test_2 = {
- .opcode = MGMT_OP_READ_INFO,
- .index = 512,
- .cmd_data = read_info_command,
- .cmd_size = sizeof(read_info_command),
- };
- static const unsigned char invalid_index_response[] =
- { 0x02, 0x00, 0xff, 0xff, 0x03, 0x00,
- 0x01, 0x00, 0x11 };
- static const struct command_test_data command_test_3 = {
- .opcode = MGMT_OP_READ_VERSION,
- .index = MGMT_INDEX_NONE,
- .cmd_data = read_version_command,
- .cmd_size = sizeof(read_version_command),
- .rsp_data = invalid_index_response,
- .rsp_size = sizeof(invalid_index_response),
- .rsp_status = MGMT_STATUS_INVALID_INDEX,
- };
- static const unsigned char event_index_added[] =
- { 0x04, 0x00, 0x01, 0x00, 0x00, 0x00 };
- static const struct command_test_data event_test_1 = {
- .opcode = MGMT_EV_INDEX_ADDED,
- .index = MGMT_INDEX_NONE,
- .cmd_data = event_index_added,
- .cmd_size = sizeof(event_index_added),
- };
- static void test_command(gconstpointer data)
- {
- const struct command_test_data *test = data;
- struct context *context = create_context();
- add_action(context, test->cmd_data, test->cmd_size,
- test->rsp_data, test->rsp_size, test->rsp_status,
- false, ACTION_PASSED);
- mgmt_send(context->mgmt_client, test->opcode, test->index,
- test->length, test->param,
- NULL ,NULL, NULL);
- execute_context(context);
- }
- static void response_cb(uint8_t status, uint16_t length, const void *param,
- void *user_data)
- {
- struct context *context = user_data;
- struct handler *handler = context->handler_list->data;
- g_assert_cmpint(status, ==, handler->rsp_status);
- context_quit(context);
- }
- static void test_response(gconstpointer data)
- {
- const struct command_test_data *test = data;
- struct context *context = create_context();
- add_action(context, test->cmd_data, test->cmd_size,
- test->rsp_data, test->rsp_size, test->rsp_status,
- false, ACTION_RESPOND);
- mgmt_send(context->mgmt_client, test->opcode, test->index,
- test->length, test->param,
- response_cb, context, NULL);
- execute_context(context);
- }
- static void event_cb(uint16_t index, uint16_t length, const void *param,
- void *user_data)
- {
- struct context *context = user_data;
- if (g_test_verbose())
- printf("Event received\n");
- context_quit(context);
- }
- static void test_event(gconstpointer data)
- {
- const struct command_test_data *test = data;
- struct context *context = create_context();
- mgmt_register(context->mgmt_client, test->opcode, test->index,
- event_cb, context, NULL);
- g_assert_cmpint(write(context->fd, test->cmd_data, test->cmd_size), ==,
- test->cmd_size);
- execute_context(context);
- }
- static void test_event2(gconstpointer data)
- {
- const struct command_test_data *test = data;
- struct context *context = create_context();
- mgmt_register(context->mgmt_client, test->opcode, test->index,
- event_cb, context, NULL);
- mgmt_register(context->mgmt_client, test->opcode, test->index,
- event_cb, context, NULL);
- g_assert_cmpint(write(context->fd, test->cmd_data, test->cmd_size), ==,
- test->cmd_size);
- execute_context(context);
- }
- static void unregister_all_cb(uint16_t index, uint16_t length,
- const void *param, void *user_data)
- {
- struct context *context = user_data;
- mgmt_unregister_all(context->mgmt_client);
- context_quit(context);
- }
- static void test_unregister_all(gconstpointer data)
- {
- const struct command_test_data *test = data;
- struct context *context = create_context();
- mgmt_register(context->mgmt_client, test->opcode, test->index,
- unregister_all_cb, context, NULL);
- mgmt_register(context->mgmt_client, test->opcode, test->index,
- event_cb, context, NULL);
- g_assert_cmpint(write(context->fd, test->cmd_data, test->cmd_size), ==,
- test->cmd_size);
- execute_context(context);
- }
- static void unregister_index_cb(uint16_t index, uint16_t length,
- const void *param, void *user_data)
- {
- struct context *context = user_data;
- mgmt_unregister_index(context->mgmt_client, index);
- context_quit(context);
- }
- static void destroy_cb(uint16_t index, uint16_t length, const void *param,
- void *user_data)
- {
- struct context *context = user_data;
- mgmt_unref(context->mgmt_client);
- context->mgmt_client = NULL;
- context_quit(context);
- }
- static void test_unregister_index(gconstpointer data)
- {
- const struct command_test_data *test = data;
- struct context *context = create_context();
- mgmt_register(context->mgmt_client, test->opcode, test->index,
- unregister_index_cb, context, NULL);
- mgmt_register(context->mgmt_client, test->opcode, test->index,
- event_cb, context, NULL);
- g_assert_cmpint(write(context->fd, test->cmd_data, test->cmd_size), ==,
- test->cmd_size);
- execute_context(context);
- }
- static void test_destroy(gconstpointer data)
- {
- const struct command_test_data *test = data;
- struct context *context = create_context();
- mgmt_register(context->mgmt_client, test->opcode, test->index,
- destroy_cb, context, NULL);
- mgmt_register(context->mgmt_client, test->opcode, test->index,
- event_cb, context, NULL);
- g_assert_cmpint(write(context->fd, test->cmd_data, test->cmd_size), ==,
- test->cmd_size);
- execute_context(context);
- }
- int main(int argc, char *argv[])
- {
- g_test_init(&argc, &argv, NULL);
- g_test_add_data_func("/mgmt/command/1", &command_test_1, test_command);
- g_test_add_data_func("/mgmt/command/2", &command_test_2, test_command);
- g_test_add_data_func("/mgmt/response/1", &command_test_1,
- test_response);
- g_test_add_data_func("/mgmt/response/2", &command_test_3,
- test_response);
- g_test_add_data_func("/mgmt/event/1", &event_test_1, test_event);
- g_test_add_data_func("/mgmt/event/2", &event_test_1, test_event2);
- g_test_add_data_func("/mgmt/unregister/1", &event_test_1,
- test_unregister_all);
- g_test_add_data_func("/mgmt/unregister/2", &event_test_1,
- test_unregister_index);
- g_test_add_data_func("/mgmt/destroy/1", &event_test_1, test_destroy);
- return g_test_run();
- }
|