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