|
| 1 | +/* test_cancel.c - unit test for cancel command */ |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright (c) 2024 Nordic Semiconductor ASA |
| 5 | + * |
| 6 | + * SPDX-License-Identifier: Apache-2.0 |
| 7 | + */ |
| 8 | + |
| 9 | +#include <stdlib.h> |
| 10 | + |
| 11 | +#include <zephyr/bluetooth/audio/cap.h> |
| 12 | +#include <zephyr/fff.h> |
| 13 | + |
| 14 | +#include "bluetooth.h" |
| 15 | +#include "cap_commander.h" |
| 16 | +#include "conn.h" |
| 17 | +#include "expects_util.h" |
| 18 | +#include "cap_mocks.h" |
| 19 | +#include "test_common.h" |
| 20 | +#include "bap_broadcast_assistant.h" |
| 21 | + |
| 22 | +#define FFF_GLOBALS |
| 23 | + |
| 24 | +struct cap_commander_test_cancel_fixture { |
| 25 | + struct bt_conn conns[CONFIG_BT_MAX_CONN]; |
| 26 | + |
| 27 | + struct bt_bap_bass_subgroup subgroups[CONFIG_BT_BAP_BASS_MAX_SUBGROUPS]; |
| 28 | + struct bt_cap_commander_broadcast_reception_start_member_param |
| 29 | + start_member_params[CONFIG_BT_MAX_CONN]; |
| 30 | + struct bt_cap_commander_broadcast_reception_start_param start_param; |
| 31 | +}; |
| 32 | + |
| 33 | +static void test_start_param_init(void *f) |
| 34 | +{ |
| 35 | + struct cap_commander_test_cancel_fixture *fixture = f; |
| 36 | + int err; |
| 37 | + |
| 38 | + fixture->start_param.type = BT_CAP_SET_TYPE_AD_HOC; |
| 39 | + fixture->start_param.param = fixture->start_member_params; |
| 40 | + |
| 41 | + fixture->start_param.count = ARRAY_SIZE(fixture->start_member_params); |
| 42 | + |
| 43 | + for (size_t i = 0; i < ARRAY_SIZE(fixture->subgroups); i++) { |
| 44 | + fixture->subgroups[i].bis_sync = BIT(i); |
| 45 | + fixture->subgroups[i].metadata_len = 0; |
| 46 | + } |
| 47 | + |
| 48 | + for (size_t i = 0U; i < ARRAY_SIZE(fixture->start_member_params); i++) { |
| 49 | + fixture->start_member_params[i].member.member = &fixture->conns[i]; |
| 50 | + bt_addr_le_copy(&fixture->start_member_params[i].addr, BT_ADDR_LE_ANY); |
| 51 | + fixture->start_member_params[i].adv_sid = 0; |
| 52 | + fixture->start_member_params[i].pa_interval = 10; |
| 53 | + fixture->start_member_params[i].broadcast_id = 0; |
| 54 | + memcpy(fixture->start_member_params[i].subgroups, &fixture->subgroups[0], |
| 55 | + sizeof(struct bt_bap_bass_subgroup) * CONFIG_BT_BAP_BASS_MAX_SUBGROUPS); |
| 56 | + fixture->start_member_params[i].num_subgroups = CONFIG_BT_BAP_BASS_MAX_SUBGROUPS; |
| 57 | + } |
| 58 | + |
| 59 | + for (size_t i = 0; i < ARRAY_SIZE(fixture->conns); i++) { |
| 60 | + err = bt_cap_commander_discover(&fixture->conns[i]); |
| 61 | + zassert_equal(0, err, "Unexpected return value %d", err); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +static void |
| 66 | +cap_commander_test_cancel_fixture_init(struct cap_commander_test_cancel_fixture *fixture) |
| 67 | +{ |
| 68 | + for (size_t i = 0; i < ARRAY_SIZE(fixture->conns); i++) { |
| 69 | + test_conn_init(&fixture->conns[i]); |
| 70 | + } |
| 71 | + |
| 72 | + test_start_param_init(fixture); |
| 73 | +} |
| 74 | + |
| 75 | +static void *cap_commander_test_cancel_setup(void) |
| 76 | +{ |
| 77 | + struct cap_commander_test_cancel_fixture *fixture; |
| 78 | + |
| 79 | + fixture = malloc(sizeof(*fixture)); |
| 80 | + zassert_not_null(fixture); |
| 81 | + |
| 82 | + return fixture; |
| 83 | +} |
| 84 | + |
| 85 | +static void cap_commander_test_cancel_before(void *f) |
| 86 | +{ |
| 87 | + struct cap_commander_test_cancel_fixture *fixture = f; |
| 88 | + |
| 89 | + memset(f, 0, sizeof(struct cap_commander_test_cancel_fixture)); |
| 90 | + cap_commander_test_cancel_fixture_init(fixture); |
| 91 | +} |
| 92 | + |
| 93 | +static void cap_commander_test_cancel_after(void *f) |
| 94 | +{ |
| 95 | + struct cap_commander_test_cancel_fixture *fixture = f; |
| 96 | + |
| 97 | + bt_cap_commander_unregister_cb(&mock_cap_commander_cb); |
| 98 | + |
| 99 | + for (size_t i = 0; i < ARRAY_SIZE(fixture->conns); i++) { |
| 100 | + mock_bt_conn_disconnected(&fixture->conns[i], BT_HCI_ERR_REMOTE_USER_TERM_CONN); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +static void cap_commander_test_cancel_teardown(void *f) |
| 105 | +{ |
| 106 | + free(f); |
| 107 | +} |
| 108 | + |
| 109 | +static void test_cancel(void) |
| 110 | +{ |
| 111 | + int err; |
| 112 | + |
| 113 | + err = bt_cap_commander_cancel(); |
| 114 | + zassert_equal(0, err, "Unexpected return value %d", err); |
| 115 | + |
| 116 | + zexpect_call_count("bt_cap_commander_cb.broadcast_reception_start", 1, |
| 117 | + mock_cap_commander_broadcast_reception_start_cb_fake.call_count); |
| 118 | + zassert_equal(-ECANCELED, |
| 119 | + mock_cap_commander_broadcast_reception_start_cb_fake.arg1_history[0]); |
| 120 | +} |
| 121 | + |
| 122 | +ZTEST_SUITE(cap_commander_test_cancel, NULL, cap_commander_test_cancel_setup, |
| 123 | + cap_commander_test_cancel_before, cap_commander_test_cancel_after, |
| 124 | + cap_commander_test_cancel_teardown); |
| 125 | + |
| 126 | +ZTEST_F(cap_commander_test_cancel, test_commander_cancel) |
| 127 | +{ |
| 128 | + int err; |
| 129 | + |
| 130 | + if (CONFIG_BT_MAX_CONN == 1) { |
| 131 | + ztest_test_skip(); |
| 132 | + } |
| 133 | + |
| 134 | + err = bt_cap_commander_register_cb(&mock_cap_commander_cb); |
| 135 | + zassert_equal(0, err, "Unexpected return value %d", err); |
| 136 | + |
| 137 | + /* Do not run the add_src callback, so that the broadcast reception start procedure does not |
| 138 | + * run until completion |
| 139 | + */ |
| 140 | + set_skip_add_src(1); |
| 141 | + |
| 142 | + /* initiate a CAP procedure; for this test we use broadcast reception start*/ |
| 143 | + err = bt_cap_commander_broadcast_reception_start(&fixture->start_param); |
| 144 | + zassert_equal(0, err, "Could not start CAP procedure: %d", err); |
| 145 | + |
| 146 | + test_cancel(); |
| 147 | +} |
| 148 | + |
| 149 | +ZTEST_F(cap_commander_test_cancel, test_commander_cancel_double) |
| 150 | +{ |
| 151 | + int err; |
| 152 | + |
| 153 | + if (CONFIG_BT_MAX_CONN == 1) { |
| 154 | + ztest_test_skip(); |
| 155 | + } |
| 156 | + |
| 157 | + err = bt_cap_commander_register_cb(&mock_cap_commander_cb); |
| 158 | + zassert_equal(0, err, "Unexpected return value %d", err); |
| 159 | + |
| 160 | + set_skip_add_src(1); |
| 161 | + err = bt_cap_commander_broadcast_reception_start(&fixture->start_param); |
| 162 | + zassert_equal(0, err, "Could not start CAP procedure: %d", err); |
| 163 | + |
| 164 | + test_cancel(); |
| 165 | + |
| 166 | + err = bt_cap_commander_cancel(); |
| 167 | + zassert_equal(-EALREADY, err, "Unexpected return value %d", err); |
| 168 | +} |
| 169 | + |
| 170 | +ZTEST_F(cap_commander_test_cancel, test_commander_cancel_no_proc_in_progress) |
| 171 | +{ |
| 172 | + int err; |
| 173 | + |
| 174 | + err = bt_cap_commander_register_cb(&mock_cap_commander_cb); |
| 175 | + zassert_equal(0, err, "Unexpected return value %d", err); |
| 176 | + |
| 177 | + err = bt_cap_commander_cancel(); |
| 178 | + zassert_equal(-EALREADY, err, "Unexpected return value %d", err); |
| 179 | +} |
0 commit comments