1 /******************************************************************************
2  *
3  *  Copyright (C) 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 extern "C" {
24 #include "btcore/include/property.h"
25 }  // "C"
26 
27 class PropertyTest : public AllocationTestHarness {};
28 
TEST_F(PropertyTest,addr)29 TEST_F(PropertyTest, addr) {
30   bt_bdaddr_t 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(bt_bdaddr_t), property->len);
41 
42   const bt_bdaddr_t *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   bt_uuid_t uuid0 = {
137     {
138       0x00, 0x11, 0x22, 0x33,
139       0x44, 0x55, 0x66, 0x77,
140       0x88, 0x99, 0xaa, 0xbb,
141       0xcc, 0xdd, 0xee, 0xff,
142     }
143   };
144   bt_property_t *property = property_new_uuids(&uuid0, 1);
145 
146   EXPECT_EQ(0, strcmp((const char *)uuid0.uu, (char *)property->val));
147   EXPECT_EQ(BT_PROPERTY_UUIDS, property->type);
148   EXPECT_EQ((int)sizeof(bt_uuid_t), property->len);
149 
150   size_t uuid_cnt1;
151   const bt_uuid_t *uuid1 = property_as_uuids(property, &uuid_cnt1);
152   EXPECT_EQ(0, memcmp(uuid1->uu, uuid1->uu, sizeof(bt_uuid_t)));
153 
154   property_free(property);
155 }
156 
TEST_F(PropertyTest,copy)157 TEST_F(PropertyTest, copy) {
158   {
159     bt_uuid_t uuids[] = {
160       {{
161          0x00, 0x11, 0x22, 0x33,
162          0x44, 0x55, 0x66, 0x77,
163          0x88, 0x99, 0xaa, 0xbb,
164          0xcc, 0xdd, 0xee, 0xff,
165        }},
166       {{
167          0xf0, 0xe1, 0xd2, 0xc3,
168          0xf4, 0xe5, 0xd6, 0xc7,
169          0xf8, 0xe9, 0xda, 0xcb,
170          0xfc, 0xed, 0xde, 0xcf,
171        }},
172     };
173 
174     bt_property_t *property0 = property_new_uuids(uuids, sizeof(bt_uuid_t)/sizeof(uuids));
175 
176     bt_property_t property1;
177     property_copy(&property1, property0);
178     EXPECT_TRUE(property_equals(property0, &property1));
179 
180     property_free(property0);
181   }
182 }
183 
TEST_F(PropertyTest,equals)184 TEST_F(PropertyTest, equals) {
185   {
186     bt_bdaddr_t addr0 = {{0x1, 0x2, 0x3, 0x4, 0x5, 0x6}};
187     bt_property_t *property0 = property_new_addr(&addr0);
188 
189     bt_device_class_t dc0 = {{ 0x01, 0x23, 0x45 }};
190     bt_property_t *property1 = property_new_device_class(&dc0);
191 
192     EXPECT_FALSE(property_equals(property0, property1));
193 
194     property_free(property0);
195     property_free(property1);
196   }
197 
198   {
199     bt_bdaddr_t addr = {{0x1, 0x2, 0x3, 0x4, 0x5, 0x6}};
200     bt_property_t *property0 = property_new_addr(&addr);
201     bt_property_t *property1 = property_new_addr(&addr);
202 
203     EXPECT_TRUE(property_equals(property0, property1));
204 
205     property_free(property0);
206     property_free(property1);
207   }
208 
209   {
210     bt_bdaddr_t addr0 = {{0x1, 0x2, 0x3, 0x4, 0x5, 0x6}};
211     bt_property_t *property0 = property_new_addr(&addr0);
212 
213     bt_bdaddr_t addr1 = {{0x1, 0x2, 0x3, 0x4, 0x5, 0xff}};
214     bt_property_t *property1 = property_new_addr(&addr1);
215 
216     EXPECT_FALSE(property_equals(property0, property1));
217 
218     property_free(property0);
219     property_free(property1);
220   }
221 
222   {
223     const char *name0 = "My btcore name";
224     bt_property_t *property0 = property_new_name(name0);
225 
226     const char *name1 = "My btcore name";
227     bt_property_t *property1 = property_new_name(name1);
228 
229     EXPECT_TRUE(property_equals(property0, property1));
230 
231     property_free(property0);
232     property_free(property1);
233   }
234 
235   {
236     const char *name0 = "My btcore name";
237     bt_property_t *property0 = property_new_name(name0);
238 
239     const char *name1 = "My btcore name     ";
240     bt_property_t *property1 = property_new_name(name1);
241 
242     EXPECT_FALSE(property_equals(property0, property1));
243 
244     property_free(property0);
245     property_free(property1);
246   }
247 }
248