1 /* main.c - Application main entry point */
2
3 /*
4 * Copyright (c) 2023-2024 Nordic Semiconductor ASA
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
9 #include <errno.h>
10 #include <stddef.h>
11 #include <stdlib.h>
12 #include <string.h>
13
14 #include <zephyr/autoconf.h>
15 #include <zephyr/bluetooth/audio/cap.h>
16 #include <zephyr/bluetooth/hci_types.h>
17 #include <zephyr/fff.h>
18 #include <zephyr/sys/util.h>
19
20 #include "cap_initiator.h"
21 #include "conn.h"
22 #include "expects_util.h"
23 #include "test_common.h"
24 #include "ztest_assert.h"
25 #include "ztest_test.h"
26
mock_init_rule_before(const struct ztest_unit_test * test,void * fixture)27 static void mock_init_rule_before(const struct ztest_unit_test *test, void *fixture)
28 {
29 test_mocks_init();
30 }
31
mock_destroy_rule_after(const struct ztest_unit_test * test,void * fixture)32 static void mock_destroy_rule_after(const struct ztest_unit_test *test, void *fixture)
33 {
34 test_mocks_cleanup();
35 }
36
37 ZTEST_RULE(mock_rule, mock_init_rule_before, mock_destroy_rule_after);
38
39 struct cap_initiator_test_suite_fixture {
40 struct bt_conn conns[CONFIG_BT_MAX_CONN];
41 };
42
cap_initiator_test_suite_fixture_init(struct cap_initiator_test_suite_fixture * fixture)43 static void cap_initiator_test_suite_fixture_init(struct cap_initiator_test_suite_fixture *fixture)
44 {
45 for (size_t i = 0; i < ARRAY_SIZE(fixture->conns); i++) {
46 test_conn_init(&fixture->conns[i]);
47 }
48 }
49
cap_initiator_test_suite_setup(void)50 static void *cap_initiator_test_suite_setup(void)
51 {
52 struct cap_initiator_test_suite_fixture *fixture;
53
54 fixture = malloc(sizeof(*fixture));
55 zassert_not_null(fixture);
56
57 return fixture;
58 }
59
cap_initiator_test_suite_before(void * f)60 static void cap_initiator_test_suite_before(void *f)
61 {
62 memset(f, 0, sizeof(struct cap_initiator_test_suite_fixture));
63 cap_initiator_test_suite_fixture_init(f);
64 }
65
cap_initiator_test_suite_after(void * f)66 static void cap_initiator_test_suite_after(void *f)
67 {
68 struct cap_initiator_test_suite_fixture *fixture = f;
69
70 bt_cap_initiator_unregister_cb(&mock_cap_initiator_cb);
71
72 for (size_t i = 0; i < ARRAY_SIZE(fixture->conns); i++) {
73 mock_bt_conn_disconnected(&fixture->conns[i], BT_HCI_ERR_REMOTE_USER_TERM_CONN);
74 }
75 }
76
cap_initiator_test_suite_teardown(void * f)77 static void cap_initiator_test_suite_teardown(void *f)
78 {
79 free(f);
80 }
81
82 ZTEST_SUITE(cap_initiator_test_suite, NULL, cap_initiator_test_suite_setup,
83 cap_initiator_test_suite_before, cap_initiator_test_suite_after,
84 cap_initiator_test_suite_teardown);
85
ZTEST_F(cap_initiator_test_suite,test_initiator_register_cb)86 ZTEST_F(cap_initiator_test_suite, test_initiator_register_cb)
87 {
88 int err;
89
90 err = bt_cap_initiator_register_cb(&mock_cap_initiator_cb);
91 zassert_equal(0, err, "Unexpected return value %d", err);
92 }
93
ZTEST_F(cap_initiator_test_suite,test_initiator_register_cb_inval_param_null)94 ZTEST_F(cap_initiator_test_suite, test_initiator_register_cb_inval_param_null)
95 {
96 int err;
97
98 err = bt_cap_initiator_register_cb(NULL);
99 zassert_equal(-EINVAL, err, "Unexpected return value %d", err);
100 }
101
ZTEST_F(cap_initiator_test_suite,test_initiator_register_cb_inval_double_register)102 ZTEST_F(cap_initiator_test_suite, test_initiator_register_cb_inval_double_register)
103 {
104 int err;
105
106 err = bt_cap_initiator_register_cb(&mock_cap_initiator_cb);
107 zassert_equal(0, err, "Unexpected return value %d", err);
108
109 err = bt_cap_initiator_register_cb(&mock_cap_initiator_cb);
110 zassert_equal(-EALREADY, err, "Unexpected return value %d", err);
111 }
112
ZTEST_F(cap_initiator_test_suite,test_initiator_unregister_cb)113 ZTEST_F(cap_initiator_test_suite, test_initiator_unregister_cb)
114 {
115 int err;
116
117 err = bt_cap_initiator_register_cb(&mock_cap_initiator_cb);
118 zassert_equal(0, err, "Unexpected return value %d", err);
119
120 err = bt_cap_initiator_unregister_cb(&mock_cap_initiator_cb);
121 zassert_equal(0, err, "Unexpected return value %d", err);
122 }
123
ZTEST_F(cap_initiator_test_suite,test_initiator_unregister_cb_inval_param_null)124 ZTEST_F(cap_initiator_test_suite, test_initiator_unregister_cb_inval_param_null)
125 {
126 int err;
127
128 err = bt_cap_initiator_unregister_cb(NULL);
129 zassert_equal(-EINVAL, err, "Unexpected return value %d", err);
130 }
131
ZTEST_F(cap_initiator_test_suite,test_initiator_unregister_cb_inval_double_unregister)132 ZTEST_F(cap_initiator_test_suite, test_initiator_unregister_cb_inval_double_unregister)
133 {
134 int err;
135
136 err = bt_cap_initiator_register_cb(&mock_cap_initiator_cb);
137 zassert_equal(0, err, "Unexpected return value %d", err);
138
139 err = bt_cap_initiator_unregister_cb(&mock_cap_initiator_cb);
140 zassert_equal(0, err, "Unexpected return value %d", err);
141
142 err = bt_cap_initiator_unregister_cb(&mock_cap_initiator_cb);
143 zassert_equal(-EINVAL, err, "Unexpected return value %d", err);
144 }
145
ZTEST_F(cap_initiator_test_suite,test_initiator_discover)146 ZTEST_F(cap_initiator_test_suite, test_initiator_discover)
147 {
148 int err;
149
150 err = bt_cap_initiator_register_cb(&mock_cap_initiator_cb);
151 zassert_equal(0, err, "Unexpected return value %d", err);
152
153 for (size_t i = 0; i < ARRAY_SIZE(fixture->conns); i++) {
154 err = bt_cap_initiator_unicast_discover(&fixture->conns[i]);
155 zassert_equal(0, err, "Unexpected return value %d", err);
156 }
157
158 zexpect_call_count("bt_cap_initiator_cb.discovery_complete", ARRAY_SIZE(fixture->conns),
159 mock_cap_initiator_unicast_discovery_complete_cb_fake.call_count);
160 }
161
ZTEST_F(cap_initiator_test_suite,test_initiator_discover_inval_param_null)162 ZTEST_F(cap_initiator_test_suite, test_initiator_discover_inval_param_null)
163 {
164 int err;
165
166 err = bt_cap_initiator_register_cb(&mock_cap_initiator_cb);
167 zassert_equal(0, err, "Unexpected return value %d", err);
168
169 err = bt_cap_initiator_unicast_discover(NULL);
170 zassert_equal(-EINVAL, err, "Unexpected return value %d", err);
171 }
172