• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_act.cc"
22 #include "main/shim/metrics_api.h"
23 #include "stack/sdp/sdp_api.cc"
24 #include "types/bluetooth/uuid.h"
25 #include "types/raw_address.h"
26 
27 namespace {
28 const RawAddress bdaddr({0x11, 0x22, 0x33, 0x44, 0x55, 0x66});
29 }  // namespace
30 
31 extern tBTA_SDP_CB bta_sdp_cb;
32 extern tBTA_SDP_CFG* p_bta_sdp_cfg;
33 
34 static tSDP_DISC_ATTR g_attr_service_class_id_list;
35 static tSDP_DISC_ATTR g_sub_attr;
36 static tSDP_DISC_ATTR g_attr_spec_id;
37 static tSDP_DISC_ATTR g_attr_vendor_id;
38 static tSDP_DISC_ATTR g_attr_vendor_id_src;
39 static tSDP_DISC_ATTR g_attr_vendor_product_id;
40 static tSDP_DISC_ATTR g_attr_vendor_product_version;
41 static tSDP_DISC_ATTR g_attr_vendor_product_primary_record;
42 static tSDP_DISC_REC g_rec;
43 
sdpu_compare_uuid_with_attr(const Uuid & uuid,tSDP_DISC_ATTR * p_attr)44 bool sdpu_compare_uuid_with_attr(const Uuid& uuid, tSDP_DISC_ATTR* p_attr) {
45   return true;
46 }
47 
sdp_dm_cback(tBTA_SDP_EVT event,tBTA_SDP * p_data,void * user_data)48 static void sdp_dm_cback(tBTA_SDP_EVT event, tBTA_SDP* p_data,
49                          void* user_data) {
50   return;
51 }
52 
CountCounterMetrics(int32_t key,int64_t count)53 bool bluetooth::shim::CountCounterMetrics(int32_t key, int64_t count) {
54   return true;
55 }
56 
57 class BtaDipTest : public ::testing::Test {
58  protected:
SetUp()59   void SetUp() override {
60     g_attr_service_class_id_list.p_next_attr = &g_attr_spec_id;
61     g_attr_service_class_id_list.attr_id = ATTR_ID_SERVICE_CLASS_ID_LIST;
62     g_attr_service_class_id_list.attr_len_type = (DATA_ELE_SEQ_DESC_TYPE<<12)|2;
63     g_attr_service_class_id_list.attr_value.v.p_sub_attr = &g_sub_attr;
64     g_sub_attr.attr_len_type = (UUID_DESC_TYPE<<12)|2;
65     g_sub_attr.attr_value.v.u16 = 0x1200;
66 
67     g_attr_spec_id.p_next_attr = &g_attr_vendor_id;
68     g_attr_spec_id.attr_id = ATTR_ID_SPECIFICATION_ID;
69     g_attr_spec_id.attr_len_type = (UINT_DESC_TYPE<<12)|2;
70     g_attr_spec_id.attr_value.v.u16 = 0x0103;
71 
72     g_attr_vendor_id.p_next_attr = &g_attr_vendor_id_src;
73     g_attr_vendor_id.attr_id = ATTR_ID_VENDOR_ID;
74     g_attr_vendor_id.attr_len_type = (UINT_DESC_TYPE<<12)|2;
75     g_attr_vendor_id.attr_value.v.u16 = 0x18d1;
76 
77     // Allocation should succeed
78     g_attr_vendor_id_src.p_next_attr = &g_attr_vendor_product_id;
79     g_attr_vendor_id_src.attr_id = ATTR_ID_VENDOR_ID_SOURCE;
80     g_attr_vendor_id_src.attr_len_type = (UINT_DESC_TYPE<<12)|2;
81     g_attr_vendor_id_src.attr_value.v.u16 = 1;
82 
83     g_attr_vendor_product_id.p_next_attr = &g_attr_vendor_product_version;
84     g_attr_vendor_product_id.attr_id = ATTR_ID_PRODUCT_ID;
85     g_attr_vendor_product_id.attr_len_type = (UINT_DESC_TYPE<<12)|2;
86     g_attr_vendor_product_id.attr_value.v.u16 = 0x1234;
87 
88     g_attr_vendor_product_version.p_next_attr = &g_attr_vendor_product_primary_record;
89     g_attr_vendor_product_version.attr_id = ATTR_ID_PRODUCT_VERSION;
90     g_attr_vendor_product_version.attr_len_type = (UINT_DESC_TYPE<<12)|2;
91     g_attr_vendor_product_version.attr_value.v.u16 = 0x0100;
92 
93     g_attr_vendor_product_primary_record.p_next_attr = &g_attr_vendor_product_primary_record;
94     g_attr_vendor_product_primary_record.attr_id = ATTR_ID_PRIMARY_RECORD;
95     g_attr_vendor_product_primary_record.attr_len_type = (BOOLEAN_DESC_TYPE<<12)|1;
96     g_attr_vendor_product_primary_record.attr_value.v.u8 = 1;
97 
98     g_rec.p_first_attr = &g_attr_service_class_id_list;
99     g_rec.p_next_rec = nullptr;
100     g_rec.remote_bd_addr = bdaddr;
101     g_rec.time_read = 0;
102 
103     bta_sdp_cb.p_dm_cback = sdp_dm_cback;
104     bta_sdp_cb.remote_addr = bdaddr;
105 
106     p_bta_sdp_cfg->p_sdp_db->p_first_rec = &g_rec;
107   }
108 
TearDown()109   void TearDown() override {}
110 };
111 
112 // 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)113 TEST_F(BtaDipTest, test_bta_create_dip_sdp_record) {
114   bluetooth_sdp_record record;
115 
116   bta_create_dip_sdp_record(&record, &g_rec);
117 
118   ASSERT_EQ(record.dip.spec_id, 0x0103);
119   ASSERT_EQ(record.dip.vendor, 0x18d1);
120   ASSERT_EQ(record.dip.vendor_id_source, 1);
121   ASSERT_EQ(record.dip.product, 0x1234);
122   ASSERT_EQ(record.dip.version, 0x0100);
123   ASSERT_EQ(record.dip.primary_record, true);
124 }
125 
126 // test for b/263958603
TEST_F(BtaDipTest,test_invalid_type_checks)127 TEST_F(BtaDipTest, test_invalid_type_checks) {
128   bluetooth_sdp_record record{};
129 
130   // here we provide the wrong types of records
131   // and verify that the provided values are not accepted
132   g_attr_spec_id.attr_len_type = (BOOLEAN_DESC_TYPE<<12)|1;
133   g_attr_spec_id.attr_value.v.u16 = 0x0103;
134 
135   g_attr_vendor_id.attr_len_type = (BOOLEAN_DESC_TYPE<<12)|2;
136   g_attr_vendor_id.attr_value.v.u16 = 0x18d1;
137 
138   g_attr_vendor_id_src.attr_len_type = (BOOLEAN_DESC_TYPE<<12)|2;
139   g_attr_vendor_id_src.attr_value.v.u16 = 1;
140 
141   g_attr_vendor_product_id.attr_len_type = (BOOLEAN_DESC_TYPE<<12)|2;
142   g_attr_vendor_product_id.attr_value.v.u16 = 0x1234;
143 
144   g_attr_vendor_product_version.attr_len_type = (BOOLEAN_DESC_TYPE<<12)|2;
145   g_attr_vendor_product_version.attr_value.v.u16 = 0x0100;
146 
147   g_attr_vendor_product_primary_record.attr_len_type = (UINT_DESC_TYPE<<12)|1;
148   g_attr_vendor_product_primary_record.attr_value.v.u8 = 1;
149 
150   bta_create_dip_sdp_record(&record, &g_rec);
151 
152   ASSERT_EQ(record.dip.spec_id, 0);
153   ASSERT_EQ(record.dip.vendor, 0);
154   ASSERT_EQ(record.dip.vendor_id_source, 0);
155   ASSERT_EQ(record.dip.product, 0);
156   ASSERT_EQ(record.dip.version, 0);
157   ASSERT_EQ(record.dip.primary_record, false);
158 }
159 
160 // test for b/263958603
TEST_F(BtaDipTest,test_invalid_size_checks)161 TEST_F(BtaDipTest, test_invalid_size_checks) {
162   bluetooth_sdp_record record{};
163 
164   // here we provide the wrong sizes of records
165   // and verify that the provided values are not accepted
166   g_attr_spec_id.attr_len_type = (UINT_DESC_TYPE<<12)|1;
167   g_attr_spec_id.attr_value.v.u16 = 0x0103;
168 
169   g_attr_vendor_id.attr_len_type = (UINT_DESC_TYPE<<12)|1;
170   g_attr_vendor_id.attr_value.v.u16 = 0x18d1;
171 
172   g_attr_vendor_id_src.attr_len_type = (UINT_DESC_TYPE<<12)|1;
173   g_attr_vendor_id_src.attr_value.v.u16 = 1;
174 
175   g_attr_vendor_product_id.attr_len_type = (UINT_DESC_TYPE<<12)|1;
176   g_attr_vendor_product_id.attr_value.v.u16 = 0x1234;
177 
178   g_attr_vendor_product_version.attr_len_type = (UINT_DESC_TYPE<<12)|1;
179   g_attr_vendor_product_version.attr_value.v.u16 = 0x0100;
180 
181   // size greater than 1 is accepted
182   g_attr_vendor_product_primary_record.attr_len_type = (BOOLEAN_DESC_TYPE<<12)|2;
183   g_attr_vendor_product_primary_record.attr_value.v.u8 = 1;
184 
185   bta_create_dip_sdp_record(&record, &g_rec);
186 
187   ASSERT_EQ(record.dip.spec_id, 0);
188   ASSERT_EQ(record.dip.vendor, 0);
189   ASSERT_EQ(record.dip.vendor_id_source, 0);
190   ASSERT_EQ(record.dip.product, 0);
191   ASSERT_EQ(record.dip.version, 0);
192   ASSERT_EQ(record.dip.primary_record, true);
193 }
194 
195 
TEST_F(BtaDipTest,test_bta_sdp_search_cback)196 TEST_F(BtaDipTest, test_bta_sdp_search_cback) {
197   Uuid* userdata = (Uuid*)malloc(sizeof(Uuid));
198 
199   memcpy(userdata, &UUID_DIP, sizeof(UUID_DIP));
200   bta_sdp_search_cback(SDP_SUCCESS, userdata);
201 }
202 
203