1 /*
2 * Copyright 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <arpa/inet.h> // htons
18 #include <dlfcn.h>
19 #include <gtest/gtest.h>
20
21 #include "stack/include/avrc_api.h"
22 #include "stack/include/bt_types.h"
23
24 class StackAvrcpTest : public ::testing::Test {
25 protected:
26 StackAvrcpTest() = default;
27
28 virtual ~StackAvrcpTest() = default;
29 };
30
TEST_F(StackAvrcpTest,test_avrcp_ctrl_parse_vendor_rsp)31 TEST_F(StackAvrcpTest, test_avrcp_ctrl_parse_vendor_rsp) {
32 uint8_t scratch_buf[512]{};
33 uint16_t scratch_buf_len = 512;
34 tAVRC_MSG msg{};
35 tAVRC_RESPONSE result{};
36 uint8_t vendor_rsp_buf[512]{};
37
38 msg.hdr.opcode = AVRC_OP_VENDOR;
39 msg.hdr.ctype = AVRC_CMD_STATUS;
40
41 memset(vendor_rsp_buf, 0, sizeof(vendor_rsp_buf));
42 vendor_rsp_buf[0] = AVRC_PDU_GET_ELEMENT_ATTR;
43 uint8_t* p = &vendor_rsp_buf[2];
44 UINT16_TO_BE_STREAM(p, 0x0009); // parameter length
45 UINT8_TO_STREAM(p, 0x01); // number of attributes
46 UINT32_TO_STREAM(p, 0x00000000); // attribute ID
47 UINT16_TO_STREAM(p, 0x0000); // character set ID
48 UINT16_TO_STREAM(p, 0xffff); // attribute value length
49 msg.vendor.p_vendor_data = vendor_rsp_buf;
50 msg.vendor.vendor_len = 13;
51 EXPECT_EQ(
52 AVRC_Ctrl_ParsResponse(&msg, &result, scratch_buf, &scratch_buf_len),
53 AVRC_STS_INTERNAL_ERR);
54 }
55
TEST_F(StackAvrcpTest,test_avrcp_parse_browse_rsp)56 TEST_F(StackAvrcpTest, test_avrcp_parse_browse_rsp) {
57 uint8_t scratch_buf[512]{};
58 uint16_t scratch_buf_len = 512;
59 tAVRC_MSG msg{};
60 tAVRC_RESPONSE result{};
61 uint8_t browse_rsp_buf[512]{};
62
63 msg.hdr.opcode = AVRC_OP_BROWSE;
64
65 memset(browse_rsp_buf, 0, sizeof(browse_rsp_buf));
66 browse_rsp_buf[0] = AVRC_PDU_GET_ITEM_ATTRIBUTES;
67 uint8_t* p = &browse_rsp_buf[1];
68 UINT16_TO_BE_STREAM(p, 0x000a); // parameter length;
69 UINT8_TO_STREAM(p, 0x04); // status
70 UINT8_TO_STREAM(p, 0x01); // number of attribute
71 UINT32_TO_STREAM(p, 0x00000000); // attribute ID
72 UINT16_TO_STREAM(p, 0x0000); // character set ID
73 UINT16_TO_STREAM(p, 0xffff); // attribute value length
74 msg.browse.p_browse_data = browse_rsp_buf;
75 msg.browse.browse_len = 13;
76 EXPECT_EQ(
77 AVRC_Ctrl_ParsResponse(&msg, &result, scratch_buf, &scratch_buf_len),
78 AVRC_STS_BAD_CMD);
79 }
80
TEST_F(StackAvrcpTest,test_avrcp_parse_browse_cmd)81 TEST_F(StackAvrcpTest, test_avrcp_parse_browse_cmd) {
82 uint8_t scratch_buf[512]{};
83 tAVRC_MSG msg{};
84 tAVRC_COMMAND result{};
85 uint8_t browse_cmd_buf[512]{};
86
87 msg.hdr.opcode = AVRC_OP_BROWSE;
88 msg.browse.p_browse_data = browse_cmd_buf;
89 msg.browse.browse_len = 2;
90 EXPECT_EQ(AVRC_ParsCommand(&msg, &result, scratch_buf, sizeof(scratch_buf)),
91 AVRC_STS_BAD_CMD);
92
93 memset(browse_cmd_buf, 0, sizeof(browse_cmd_buf));
94 browse_cmd_buf[0] = AVRC_PDU_SET_BROWSED_PLAYER;
95 msg.browse.browse_len = 3;
96 EXPECT_EQ(AVRC_ParsCommand(&msg, &result, scratch_buf, sizeof(scratch_buf)),
97 AVRC_STS_BAD_CMD);
98
99 msg.browse.browse_len = 5;
100 EXPECT_EQ(AVRC_ParsCommand(&msg, &result, scratch_buf, sizeof(scratch_buf)),
101 AVRC_STS_NO_ERROR);
102
103 memset(browse_cmd_buf, 0, sizeof(browse_cmd_buf));
104 browse_cmd_buf[0] = AVRC_PDU_GET_FOLDER_ITEMS;
105 msg.browse.browse_len = 3;
106 EXPECT_EQ(AVRC_ParsCommand(&msg, &result, scratch_buf, sizeof(scratch_buf)),
107 AVRC_STS_BAD_CMD);
108
109 msg.browse.browse_len = 13;
110 uint8_t* p = &browse_cmd_buf[3];
111 UINT8_TO_STREAM(p, AVRC_SCOPE_NOW_PLAYING); // scope
112 UINT32_TO_STREAM(p, 0x00000001); // start_item
113 UINT32_TO_STREAM(p, 0x00000002); // end_item
114 browse_cmd_buf[12] = 0; // attr_count
115 EXPECT_EQ(AVRC_ParsCommand(&msg, &result, scratch_buf, sizeof(scratch_buf)),
116 AVRC_STS_NO_ERROR);
117
118 memset(browse_cmd_buf, 0, sizeof(browse_cmd_buf));
119 browse_cmd_buf[0] = AVRC_PDU_CHANGE_PATH;
120 msg.browse.browse_len = 3;
121 EXPECT_EQ(AVRC_ParsCommand(&msg, &result, scratch_buf, sizeof(scratch_buf)),
122 AVRC_STS_BAD_CMD);
123
124 msg.browse.browse_len = 14;
125 p = &browse_cmd_buf[3];
126 UINT16_TO_STREAM(p, 0x1234); // uid_counter
127 UINT8_TO_STREAM(p, AVRC_DIR_UP); // direction
128 UINT8_TO_STREAM(p, 0); // attr_count
129 EXPECT_EQ(AVRC_ParsCommand(&msg, &result, scratch_buf, sizeof(scratch_buf)),
130 AVRC_STS_NO_ERROR);
131
132 memset(browse_cmd_buf, 0, sizeof(browse_cmd_buf));
133 browse_cmd_buf[0] = AVRC_PDU_GET_ITEM_ATTRIBUTES;
134 msg.browse.browse_len = 3;
135 EXPECT_EQ(AVRC_ParsCommand(&msg, &result, scratch_buf, sizeof(scratch_buf)),
136 AVRC_STS_BAD_CMD);
137
138 msg.browse.browse_len = 15;
139 EXPECT_EQ(AVRC_ParsCommand(&msg, &result, scratch_buf, sizeof(scratch_buf)),
140 AVRC_STS_NO_ERROR);
141
142 memset(browse_cmd_buf, 0, sizeof(browse_cmd_buf));
143 browse_cmd_buf[0] = AVRC_PDU_GET_TOTAL_NUM_OF_ITEMS;
144 msg.browse.browse_len = 3;
145 EXPECT_EQ(AVRC_ParsCommand(&msg, &result, scratch_buf, sizeof(scratch_buf)),
146 AVRC_STS_BAD_CMD);
147
148 msg.browse.browse_len = 4;
149 EXPECT_EQ(AVRC_ParsCommand(&msg, &result, scratch_buf, sizeof(scratch_buf)),
150 AVRC_STS_NO_ERROR);
151
152 memset(browse_cmd_buf, 0, sizeof(browse_cmd_buf));
153 browse_cmd_buf[0] = AVRC_PDU_SEARCH;
154 msg.browse.browse_len = 3;
155 EXPECT_EQ(AVRC_ParsCommand(&msg, &result, scratch_buf, sizeof(scratch_buf)),
156 AVRC_STS_BAD_CMD);
157
158 p = &browse_cmd_buf[3];
159 UINT16_TO_STREAM(p, 0x0000); // charset_id
160 UINT16_TO_STREAM(p, 0x0000); // str_len
161 msg.browse.browse_len = 7;
162 EXPECT_EQ(AVRC_ParsCommand(&msg, &result, scratch_buf, sizeof(scratch_buf)),
163 AVRC_STS_NO_ERROR);
164 }
165
TEST_F(StackAvrcpTest,test_avrcp_pdu_register_notification)166 TEST_F(StackAvrcpTest, test_avrcp_pdu_register_notification) {
167 ASSERT_EQ(htons(0x500), 5);
168
169 struct {
170 uint8_t pdu;
171 uint8_t reserved;
172 uint16_t len;
173 struct {
174 uint8_t event_id;
175 uint32_t param;
176 } payload;
177 } data = {
178 AVRC_PDU_REGISTER_NOTIFICATION,
179 0, // reserved
180 htons(sizeof(data.payload)),
181 .payload =
182 {
183 .event_id = 0,
184 .param = 0x1234,
185 },
186 };
187
188 tAVRC_MSG msg = {
189 .vendor =
190 {
191 .hdr =
192 {
193 .ctype = AVRC_CMD_NOTIF,
194 .opcode = AVRC_OP_VENDOR,
195 },
196 .p_vendor_data = (uint8_t*)&data,
197 .vendor_len = sizeof(data),
198 },
199 };
200 tAVRC_COMMAND result{};
201
202 // Run through all possible event ids
203 uint8_t id = 0;
204 do {
205 data.payload.event_id = id;
206 ASSERT_EQ((id == 0 || id > AVRC_NUM_NOTIF_EVENTS) ? AVRC_STS_BAD_PARAM
207 : AVRC_STS_NO_ERROR,
208 AVRC_Ctrl_ParsCommand(&msg, &result));
209 } while (++id != 0);
210 }
211