• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright 2014 Google, Inc.
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 <arpa/inet.h>
20 #include <gtest/gtest.h>
21 #include "osi/test/AllocationTestHarness.h"
22 
23 #include "btcore/include/property.h"
24 
25 using bluetooth::Uuid;
26 
27 class PropertyTest : public AllocationTestHarness {};
28 
TEST_F(PropertyTest,addr)29 TEST_F(PropertyTest, addr) {
30   RawAddress addr0 = {{0x1, 0x2, 0x3, 0x4, 0x5, 0x6}};
31   bt_property_t* property = property_new_addr(&addr0);
32 
33   EXPECT_EQ(addr0.address[0], ((uint8_t*)property->val)[0]);
34   EXPECT_EQ(addr0.address[1], ((uint8_t*)property->val)[1]);
35   EXPECT_EQ(addr0.address[2], ((uint8_t*)property->val)[2]);
36   EXPECT_EQ(addr0.address[3], ((uint8_t*)property->val)[3]);
37   EXPECT_EQ(addr0.address[4], ((uint8_t*)property->val)[4]);
38   EXPECT_EQ(addr0.address[5], ((uint8_t*)property->val)[5]);
39   EXPECT_EQ(BT_PROPERTY_BDADDR, property->type);
40   EXPECT_EQ((int)sizeof(RawAddress), property->len);
41 
42   const RawAddress* addr1 = property_as_addr(property);
43   EXPECT_EQ(addr0.address[0], addr1->address[0]);
44 
45   property_free(property);
46 }
47 
TEST_F(PropertyTest,device_class)48 TEST_F(PropertyTest, device_class) {
49   bt_device_class_t dc0 = {{0x01, 0x23, 0x45}};
50   bt_property_t* property = property_new_device_class(&dc0);
51 
52   EXPECT_EQ(dc0._[0], ((uint8_t*)property->val)[0]);
53   EXPECT_EQ(dc0._[1], ((uint8_t*)property->val)[1]);
54   EXPECT_EQ(dc0._[2], ((uint8_t*)property->val)[2]);
55   EXPECT_EQ(BT_PROPERTY_CLASS_OF_DEVICE, property->type);
56   EXPECT_EQ((int)sizeof(bt_device_class_t), property->len);
57 
58   const bt_device_class_t* dc1 = property_as_device_class(property);
59   int dc_int = device_class_to_int(dc1);
60   EXPECT_EQ(0x452301, dc_int);
61 
62   property_free(property);
63 }
64 
TEST_F(PropertyTest,device_type)65 TEST_F(PropertyTest, device_type) {
66   bt_device_type_t dt0 = (bt_device_type_t)1;
67   bt_property_t* property = property_new_device_type(dt0);
68 
69   EXPECT_EQ((int)dt0, *(int*)property->val);
70   EXPECT_EQ(BT_PROPERTY_TYPE_OF_DEVICE, property->type);
71   EXPECT_EQ((int)sizeof(bt_device_type_t), property->len);
72 
73   bt_device_type_t dt1 = property_as_device_type(property);
74   EXPECT_EQ(1, (int)dt1);
75 
76   property_free(property);
77 }
78 
TEST_F(PropertyTest,discovery_timeout)79 TEST_F(PropertyTest, discovery_timeout) {
80   uint32_t timeout0 = 12345;
81   bt_property_t* property = property_new_discovery_timeout(timeout0);
82 
83   EXPECT_EQ(timeout0, *(uint32_t*)property->val);
84   EXPECT_EQ(BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT, property->type);
85   EXPECT_EQ((int)sizeof(uint32_t), property->len);
86 
87   uint32_t timeout1 = property_as_discovery_timeout(property);
88   EXPECT_EQ(timeout0, timeout1);
89 
90   property_free(property);
91 }
92 
TEST_F(PropertyTest,name)93 TEST_F(PropertyTest, name) {
94   const char* name0 = "My btcore name";
95   bt_property_t* property = property_new_name(name0);
96 
97   EXPECT_EQ(0, strcmp((char*)name0, (char*)property->val));
98   EXPECT_EQ(BT_PROPERTY_BDNAME, property->type);
99   EXPECT_EQ((int)sizeof(bt_bdname_t), property->len);
100 
101   const bt_bdname_t* name1 = property_as_name(property);
102   EXPECT_EQ(0, strcmp((char*)name0, (char*)name1->name));
103 
104   property_free(property);
105 }
106 
TEST_F(PropertyTest,rssi)107 TEST_F(PropertyTest, rssi) {
108   int8_t rssi0 = -56;
109   bt_property_t* property = property_new_rssi(rssi0);
110 
111   EXPECT_EQ(*(int8_t*)property->val, rssi0);
112   EXPECT_EQ(BT_PROPERTY_REMOTE_RSSI, property->type);
113   EXPECT_EQ((int)sizeof(int8_t), property->len);
114 
115   int8_t rss1 = property_as_rssi(property);
116   EXPECT_EQ(rssi0, rss1);
117 
118   property_free(property);
119 }
120 
TEST_F(PropertyTest,scan_mode)121 TEST_F(PropertyTest, scan_mode) {
122   bt_scan_mode_t mode0 = (bt_scan_mode_t)3;
123   bt_property_t* property = property_new_scan_mode(mode0);
124 
125   EXPECT_EQ(*(int*)property->val, mode0);
126   EXPECT_EQ(BT_PROPERTY_ADAPTER_SCAN_MODE, property->type);
127   EXPECT_EQ((int)sizeof(int), property->len);
128 
129   bt_scan_mode_t mode1 = property_as_scan_mode(property);
130   EXPECT_EQ((int)mode0, (int)mode1);
131 
132   property_free(property);
133 }
134 
TEST_F(PropertyTest,uuids)135 TEST_F(PropertyTest, uuids) {
136   Uuid uuid0 = Uuid::From128BitBE({{
137       0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb,
138       0xcc, 0xdd, 0xee, 0xff,
139   }});
140   bt_property_t* property = property_new_uuids(&uuid0, 1);
141 
142   EXPECT_EQ(0, memcmp(uuid0.To128BitBE().data(), property->val, sizeof(Uuid)));
143   EXPECT_EQ(BT_PROPERTY_UUIDS, property->type);
144   EXPECT_EQ((int)sizeof(Uuid), property->len);
145 
146   size_t uuid_cnt1;
147   const Uuid* uuid1 = property_as_uuids(property, &uuid_cnt1);
148   EXPECT_EQ(uuid0, *uuid1);
149 
150   property_free(property);
151 }
152 
TEST_F(PropertyTest,copy)153 TEST_F(PropertyTest, copy) {
154   {
155     Uuid uuids[] = {
156         Uuid::From128BitBE({{
157             0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa,
158             0xbb, 0xcc, 0xdd, 0xee, 0xff,
159         }}),
160         Uuid::From128BitBE({{
161             0xf0, 0xe1, 0xd2, 0xc3, 0xf4, 0xe5, 0xd6, 0xc7, 0xf8, 0xe9, 0xda,
162             0xcb, 0xfc, 0xed, 0xde, 0xcf,
163         }}),
164     };
165 
166     bt_property_t* property0 =
167         property_new_uuids(uuids, sizeof(uuids) / sizeof(Uuid));
168 
169     bt_property_t property1;
170     property_copy(&property1, property0);
171     EXPECT_TRUE(property_equals(property0, &property1));
172 
173     property_free(property0);
174   }
175 }
176 
TEST_F(PropertyTest,equals)177 TEST_F(PropertyTest, equals) {
178   {
179     RawAddress addr0 = {{0x1, 0x2, 0x3, 0x4, 0x5, 0x6}};
180     bt_property_t* property0 = property_new_addr(&addr0);
181 
182     bt_device_class_t dc0 = {{0x01, 0x23, 0x45}};
183     bt_property_t* property1 = property_new_device_class(&dc0);
184 
185     EXPECT_FALSE(property_equals(property0, property1));
186 
187     property_free(property0);
188     property_free(property1);
189   }
190 
191   {
192     RawAddress addr = {{0x1, 0x2, 0x3, 0x4, 0x5, 0x6}};
193     bt_property_t* property0 = property_new_addr(&addr);
194     bt_property_t* property1 = property_new_addr(&addr);
195 
196     EXPECT_TRUE(property_equals(property0, property1));
197 
198     property_free(property0);
199     property_free(property1);
200   }
201 
202   {
203     RawAddress addr0 = {{0x1, 0x2, 0x3, 0x4, 0x5, 0x6}};
204     bt_property_t* property0 = property_new_addr(&addr0);
205 
206     RawAddress addr1 = {{0x1, 0x2, 0x3, 0x4, 0x5, 0xff}};
207     bt_property_t* property1 = property_new_addr(&addr1);
208 
209     EXPECT_FALSE(property_equals(property0, property1));
210 
211     property_free(property0);
212     property_free(property1);
213   }
214 
215   {
216     const char* name0 = "My btcore name";
217     bt_property_t* property0 = property_new_name(name0);
218 
219     const char* name1 = "My btcore name";
220     bt_property_t* property1 = property_new_name(name1);
221 
222     EXPECT_TRUE(property_equals(property0, property1));
223 
224     property_free(property0);
225     property_free(property1);
226   }
227 
228   {
229     const char* name0 = "My btcore name";
230     bt_property_t* property0 = property_new_name(name0);
231 
232     const char* name1 = "My btcore name     ";
233     bt_property_t* property1 = property_new_name(name1);
234 
235     EXPECT_FALSE(property_equals(property0, property1));
236 
237     property_free(property0);
238     property_free(property1);
239   }
240 }
241