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 <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 "test_common.h"
19 
20 DEFINE_FFF_GLOBALS;
21 
mock_init_rule_before(const struct ztest_unit_test * test,void * fixture)22 static void mock_init_rule_before(const struct ztest_unit_test *test, void *fixture)
23 {
24 	test_mocks_init();
25 }
26 
mock_destroy_rule_after(const struct ztest_unit_test * test,void * fixture)27 static void mock_destroy_rule_after(const struct ztest_unit_test *test, void *fixture)
28 {
29 	test_mocks_cleanup();
30 }
31 
32 ZTEST_RULE(mock_rule, mock_init_rule_before, mock_destroy_rule_after);
33 
34 struct cap_commander_test_suite_fixture {
35 	struct bt_conn conns[CONFIG_BT_MAX_CONN];
36 };
37 
cap_commander_test_suite_fixture_init(struct cap_commander_test_suite_fixture * fixture)38 static void cap_commander_test_suite_fixture_init(struct cap_commander_test_suite_fixture *fixture)
39 {
40 	for (size_t i = 0; i < ARRAY_SIZE(fixture->conns); i++) {
41 		test_conn_init(&fixture->conns[i]);
42 	}
43 }
44 
cap_commander_test_suite_setup(void)45 static void *cap_commander_test_suite_setup(void)
46 {
47 	struct cap_commander_test_suite_fixture *fixture;
48 
49 	fixture = malloc(sizeof(*fixture));
50 	zassert_not_null(fixture);
51 
52 	return fixture;
53 }
54 
cap_commander_test_suite_before(void * f)55 static void cap_commander_test_suite_before(void *f)
56 {
57 	memset(f, 0, sizeof(struct cap_commander_test_suite_fixture));
58 	cap_commander_test_suite_fixture_init(f);
59 }
60 
cap_commander_test_suite_after(void * f)61 static void cap_commander_test_suite_after(void *f)
62 {
63 	struct cap_commander_test_suite_fixture *fixture = f;
64 
65 	bt_cap_commander_unregister_cb(&mock_cap_commander_cb);
66 
67 	for (size_t i = 0; i < ARRAY_SIZE(fixture->conns); i++) {
68 		mock_bt_conn_disconnected(&fixture->conns[i], BT_HCI_ERR_REMOTE_USER_TERM_CONN);
69 	}
70 }
71 
cap_commander_test_suite_teardown(void * f)72 static void cap_commander_test_suite_teardown(void *f)
73 {
74 	free(f);
75 }
76 
77 ZTEST_SUITE(cap_commander_test_suite, NULL, cap_commander_test_suite_setup,
78 	    cap_commander_test_suite_before, cap_commander_test_suite_after,
79 	    cap_commander_test_suite_teardown);
80 
ZTEST_F(cap_commander_test_suite,test_commander_register_cb)81 ZTEST_F(cap_commander_test_suite, test_commander_register_cb)
82 {
83 	int err;
84 
85 	err = bt_cap_commander_register_cb(&mock_cap_commander_cb);
86 	zassert_equal(0, err, "Unexpected return value %d", err);
87 }
88 
ZTEST_F(cap_commander_test_suite,test_commander_register_cb_inval_param_null)89 ZTEST_F(cap_commander_test_suite, test_commander_register_cb_inval_param_null)
90 {
91 	int err;
92 
93 	err = bt_cap_commander_register_cb(NULL);
94 	zassert_equal(-EINVAL, err, "Unexpected return value %d", err);
95 }
96 
ZTEST_F(cap_commander_test_suite,test_commander_register_cb_inval_double_register)97 ZTEST_F(cap_commander_test_suite, test_commander_register_cb_inval_double_register)
98 {
99 	int err;
100 
101 	err = bt_cap_commander_register_cb(&mock_cap_commander_cb);
102 	zassert_equal(0, err, "Unexpected return value %d", err);
103 
104 	err = bt_cap_commander_register_cb(&mock_cap_commander_cb);
105 	zassert_equal(-EALREADY, err, "Unexpected return value %d", err);
106 }
107 
ZTEST_F(cap_commander_test_suite,test_commander_unregister_cb)108 ZTEST_F(cap_commander_test_suite, test_commander_unregister_cb)
109 {
110 	int err;
111 
112 	err = bt_cap_commander_register_cb(&mock_cap_commander_cb);
113 	zassert_equal(0, err, "Unexpected return value %d", err);
114 
115 	err = bt_cap_commander_unregister_cb(&mock_cap_commander_cb);
116 	zassert_equal(0, err, "Unexpected return value %d", err);
117 }
118 
ZTEST_F(cap_commander_test_suite,test_commander_unregister_cb_inval_param_null)119 ZTEST_F(cap_commander_test_suite, test_commander_unregister_cb_inval_param_null)
120 {
121 	int err;
122 
123 	err = bt_cap_commander_unregister_cb(NULL);
124 	zassert_equal(-EINVAL, err, "Unexpected return value %d", err);
125 }
126 
ZTEST_F(cap_commander_test_suite,test_commander_unregister_cb_inval_double_unregister)127 ZTEST_F(cap_commander_test_suite, test_commander_unregister_cb_inval_double_unregister)
128 {
129 	int err;
130 
131 	err = bt_cap_commander_register_cb(&mock_cap_commander_cb);
132 	zassert_equal(0, err, "Unexpected return value %d", err);
133 
134 	err = bt_cap_commander_unregister_cb(&mock_cap_commander_cb);
135 	zassert_equal(0, err, "Unexpected return value %d", err);
136 
137 	err = bt_cap_commander_unregister_cb(&mock_cap_commander_cb);
138 	zassert_equal(-EINVAL, err, "Unexpected return value %d", err);
139 }
140 
ZTEST_F(cap_commander_test_suite,test_commander_discover)141 ZTEST_F(cap_commander_test_suite, test_commander_discover)
142 {
143 	int err;
144 
145 	err = bt_cap_commander_register_cb(&mock_cap_commander_cb);
146 	zassert_equal(0, err, "Unexpected return value %d", err);
147 
148 	for (size_t i = 0; i < ARRAY_SIZE(fixture->conns); i++) {
149 		err = bt_cap_commander_discover(&fixture->conns[i]);
150 		zassert_equal(0, err, "Unexpected return value %d", err);
151 	}
152 
153 	zexpect_call_count("bt_cap_commander_cb.discovery_complete", ARRAY_SIZE(fixture->conns),
154 			   mock_cap_commander_discovery_complete_cb_fake.call_count);
155 }
156 
ZTEST_F(cap_commander_test_suite,test_commander_discover_inval_param_null)157 ZTEST_F(cap_commander_test_suite, test_commander_discover_inval_param_null)
158 {
159 	int err;
160 
161 	err = bt_cap_commander_register_cb(&mock_cap_commander_cb);
162 	zassert_equal(0, err, "Unexpected return value %d", err);
163 
164 	err = bt_cap_commander_discover(NULL);
165 	zassert_equal(-EINVAL, err, "Unexpected return value %d", err);
166 }
167