1 /******************************************************************************
2 *
3 * Copyright 2021 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 #include <gtest/gtest.h>
20
21 #include "bta/sdp/bta_sdp_int.h"
22 #include "btif/include/btif_sock_sdp.h"
23 #include "main/shim/metrics_api.h"
24 #include "stack/include/sdpdefs.h"
25 #include "test/mock/mock_stack_sdp_api.h"
26 #include "types/bluetooth/uuid.h"
27 #include "types/raw_address.h"
28
29 namespace {
30 const RawAddress bdaddr({0x11, 0x22, 0x33, 0x44, 0x55, 0x66});
31 } // namespace
32
33 tBTA_SDP_CB bta_sdp_cb;
34
35 static tSDP_DISC_ATTR g_attr_service_class_id_list;
36 static tSDP_DISC_ATTR g_sub_attr;
37 static tSDP_DISC_ATTR g_attr_spec_id;
38 static tSDP_DISC_ATTR g_attr_vendor_id;
39 static tSDP_DISC_ATTR g_attr_vendor_id_src;
40 static tSDP_DISC_ATTR g_attr_vendor_product_id;
41 static tSDP_DISC_ATTR g_attr_vendor_product_version;
42 static tSDP_DISC_ATTR g_attr_vendor_product_primary_record;
43 static tSDP_DISC_REC g_rec;
44
sdp_dm_cback(tBTA_SDP_EVT event,tBTA_SDP * p_data,void * user_data)45 static void sdp_dm_cback(tBTA_SDP_EVT event, tBTA_SDP* p_data,
46 void* user_data) {
47 return;
48 }
49
50 class BtaDipTest : public ::testing::Test {
51 protected:
SetUp()52 void SetUp() override {
53 g_attr_service_class_id_list.p_next_attr = &g_attr_spec_id;
54 g_attr_service_class_id_list.attr_id = ATTR_ID_SERVICE_CLASS_ID_LIST;
55 g_attr_service_class_id_list.attr_len_type = (DATA_ELE_SEQ_DESC_TYPE<<12)|2;
56 g_attr_service_class_id_list.attr_value.v.p_sub_attr = &g_sub_attr;
57 g_sub_attr.attr_len_type = (UUID_DESC_TYPE<<12)|2;
58 g_sub_attr.attr_value.v.u16 = 0x1200;
59
60 g_attr_spec_id.p_next_attr = &g_attr_vendor_id;
61 g_attr_spec_id.attr_id = ATTR_ID_SPECIFICATION_ID;
62 g_attr_spec_id.attr_len_type = (UINT_DESC_TYPE<<12)|2;
63 g_attr_spec_id.attr_value.v.u16 = 0x0103;
64
65 g_attr_vendor_id.p_next_attr = &g_attr_vendor_id_src;
66 g_attr_vendor_id.attr_id = ATTR_ID_VENDOR_ID;
67 g_attr_vendor_id.attr_len_type = (UINT_DESC_TYPE<<12)|2;
68 g_attr_vendor_id.attr_value.v.u16 = 0x18d1;
69
70 // Allocation should succeed
71 g_attr_vendor_id_src.p_next_attr = &g_attr_vendor_product_id;
72 g_attr_vendor_id_src.attr_id = ATTR_ID_VENDOR_ID_SOURCE;
73 g_attr_vendor_id_src.attr_len_type = (UINT_DESC_TYPE<<12)|2;
74 g_attr_vendor_id_src.attr_value.v.u16 = 1;
75
76 g_attr_vendor_product_id.p_next_attr = &g_attr_vendor_product_version;
77 g_attr_vendor_product_id.attr_id = ATTR_ID_PRODUCT_ID;
78 g_attr_vendor_product_id.attr_len_type = (UINT_DESC_TYPE<<12)|2;
79 g_attr_vendor_product_id.attr_value.v.u16 = 0x1234;
80
81 g_attr_vendor_product_version.p_next_attr = &g_attr_vendor_product_primary_record;
82 g_attr_vendor_product_version.attr_id = ATTR_ID_PRODUCT_VERSION;
83 g_attr_vendor_product_version.attr_len_type = (UINT_DESC_TYPE<<12)|2;
84 g_attr_vendor_product_version.attr_value.v.u16 = 0x0100;
85
86 g_attr_vendor_product_primary_record.p_next_attr = &g_attr_vendor_product_primary_record;
87 g_attr_vendor_product_primary_record.attr_id = ATTR_ID_PRIMARY_RECORD;
88 g_attr_vendor_product_primary_record.attr_len_type = (BOOLEAN_DESC_TYPE<<12)|1;
89 g_attr_vendor_product_primary_record.attr_value.v.u8 = 1;
90
91 g_rec.p_first_attr = &g_attr_service_class_id_list;
92 g_rec.p_next_rec = nullptr;
93 g_rec.remote_bd_addr = bdaddr;
94 g_rec.time_read = 0;
95
96 bta_sdp_cb.p_dm_cback = sdp_dm_cback;
97 bta_sdp_cb.remote_addr = bdaddr;
98
99 p_bta_sdp_cfg->p_sdp_db->p_first_rec = &g_rec;
100 }
101
TearDown()102 void TearDown() override {}
103 };
104
105 namespace bluetooth {
106 namespace testing {
107
108 void bta_create_dip_sdp_record(bluetooth_sdp_record* record,
109 tSDP_DISC_REC* p_rec);
110 void bta_sdp_search_cback(Uuid uuid, const RawAddress& bd_addr,
111 tSDP_RESULT result);
112
113 } // namespace testing
114 } // namespace bluetooth
115
116 // Test that bta_create_dip_sdp_record can parse sdp record to bluetooth_sdp_record correctly
TEST_F(BtaDipTest,test_bta_create_dip_sdp_record)117 TEST_F(BtaDipTest, test_bta_create_dip_sdp_record) {
118 bluetooth_sdp_record record;
119
120 bluetooth::testing::bta_create_dip_sdp_record(&record, &g_rec);
121
122 ASSERT_EQ(record.dip.spec_id, 0x0103);
123 ASSERT_EQ(record.dip.vendor, 0x18d1);
124 ASSERT_EQ(record.dip.vendor_id_source, 1);
125 ASSERT_EQ(record.dip.product, 0x1234);
126 ASSERT_EQ(record.dip.version, 0x0100);
127 ASSERT_EQ(record.dip.primary_record, true);
128 }
129
130 // test for b/263958603
TEST_F(BtaDipTest,test_invalid_type_checks)131 TEST_F(BtaDipTest, test_invalid_type_checks) {
132 bluetooth_sdp_record record{};
133
134 // here we provide the wrong types of records
135 // and verify that the provided values are not accepted
136 g_attr_spec_id.attr_len_type = (BOOLEAN_DESC_TYPE<<12)|1;
137 g_attr_spec_id.attr_value.v.u16 = 0x0103;
138
139 g_attr_vendor_id.attr_len_type = (BOOLEAN_DESC_TYPE<<12)|2;
140 g_attr_vendor_id.attr_value.v.u16 = 0x18d1;
141
142 g_attr_vendor_id_src.attr_len_type = (BOOLEAN_DESC_TYPE<<12)|2;
143 g_attr_vendor_id_src.attr_value.v.u16 = 1;
144
145 g_attr_vendor_product_id.attr_len_type = (BOOLEAN_DESC_TYPE<<12)|2;
146 g_attr_vendor_product_id.attr_value.v.u16 = 0x1234;
147
148 g_attr_vendor_product_version.attr_len_type = (BOOLEAN_DESC_TYPE<<12)|2;
149 g_attr_vendor_product_version.attr_value.v.u16 = 0x0100;
150
151 g_attr_vendor_product_primary_record.attr_len_type = (UINT_DESC_TYPE<<12)|1;
152 g_attr_vendor_product_primary_record.attr_value.v.u8 = 1;
153
154 bluetooth::testing::bta_create_dip_sdp_record(&record, &g_rec);
155
156 ASSERT_EQ(record.dip.spec_id, 0);
157 ASSERT_EQ(record.dip.vendor, 0);
158 ASSERT_EQ(record.dip.vendor_id_source, 0);
159 ASSERT_EQ(record.dip.product, 0);
160 ASSERT_EQ(record.dip.version, 0);
161 ASSERT_EQ(record.dip.primary_record, false);
162 }
163
164 // test for b/263958603
TEST_F(BtaDipTest,test_invalid_size_checks)165 TEST_F(BtaDipTest, test_invalid_size_checks) {
166 bluetooth_sdp_record record{};
167
168 // here we provide the wrong sizes of records
169 // and verify that the provided values are not accepted
170 g_attr_spec_id.attr_len_type = (UINT_DESC_TYPE<<12)|1;
171 g_attr_spec_id.attr_value.v.u16 = 0x0103;
172
173 g_attr_vendor_id.attr_len_type = (UINT_DESC_TYPE<<12)|1;
174 g_attr_vendor_id.attr_value.v.u16 = 0x18d1;
175
176 g_attr_vendor_id_src.attr_len_type = (UINT_DESC_TYPE<<12)|1;
177 g_attr_vendor_id_src.attr_value.v.u16 = 1;
178
179 g_attr_vendor_product_id.attr_len_type = (UINT_DESC_TYPE<<12)|1;
180 g_attr_vendor_product_id.attr_value.v.u16 = 0x1234;
181
182 g_attr_vendor_product_version.attr_len_type = (UINT_DESC_TYPE<<12)|1;
183 g_attr_vendor_product_version.attr_value.v.u16 = 0x0100;
184
185 // size greater than 1 is accepted
186 g_attr_vendor_product_primary_record.attr_len_type = (BOOLEAN_DESC_TYPE<<12)|2;
187 g_attr_vendor_product_primary_record.attr_value.v.u8 = 1;
188
189 bluetooth::testing::bta_create_dip_sdp_record(&record, &g_rec);
190
191 ASSERT_EQ(record.dip.spec_id, 0);
192 ASSERT_EQ(record.dip.vendor, 0);
193 ASSERT_EQ(record.dip.vendor_id_source, 0);
194 ASSERT_EQ(record.dip.product, 0);
195 ASSERT_EQ(record.dip.version, 0);
196 ASSERT_EQ(record.dip.primary_record, true);
197
198 // a size zero for boolean won't be accepted
199 g_attr_vendor_product_primary_record.attr_len_type =
200 (BOOLEAN_DESC_TYPE << 12) | 0;
201
202 record = {};
203
204 g_attr_vendor_product_primary_record.attr_value.v.u8 = 1;
205 bluetooth::testing::bta_create_dip_sdp_record(&record, &g_rec);
206 ASSERT_EQ(record.dip.primary_record, false);
207 }
208
209
TEST_F(BtaDipTest,test_bta_sdp_search_cback)210 TEST_F(BtaDipTest, test_bta_sdp_search_cback) {
211 bluetooth::testing::bta_sdp_search_cback(UUID_DIP, RawAddress::kEmpty,
212 SDP_SUCCESS);
213 }
214