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/device_class.h"
25 } // "C"
26
27 // Device Class is 3 bytes.
28 static const int DC_MASK = 0xffffff;
29
check_bitfield(const char * m_expr,const char * n_expr,int m,int n)30 ::testing::AssertionResult check_bitfield(const char *m_expr,
31 const char *n_expr, int m, int n) {
32 if (m == n)
33 return ::testing::AssertionSuccess();
34
35 std::stringstream ss;
36
37 ss.str("");
38 ss << std::showbase << std::hex << std::setw(8) << std::setfill('0') << m;
39 std::string expected_str = ss.str();
40
41 ss.str("");
42 ss << std::showbase << std::hex << std::setw(8) << std::setfill('0') << n;
43 std::string actual_str = ss.str();
44
45 return ::testing::AssertionFailure() << m_expr << " and " << n_expr
46 << " ( " << expected_str << " vs " << actual_str << " )";
47 }
48
49 class DeviceClassTest : public AllocationTestHarness {};
50
TEST_F(DeviceClassTest,cod_sizeof)51 TEST_F(DeviceClassTest, cod_sizeof) {
52 uint8_t dc_stream[] = { 0x00, 0x00, 0x00, 0x00};
53 bt_device_class_t dc0;
54 device_class_from_stream(&dc0, dc_stream);
55 EXPECT_EQ((size_t)3, sizeof(dc0));
56 }
57
TEST_F(DeviceClassTest,simple)58 TEST_F(DeviceClassTest, simple) {
59 uint8_t dc_stream[][sizeof(bt_device_class_t)] = {
60 { 0x00, 0x00, 0x00 },
61 { 0xff, 0xff, 0xff },
62 { 0xaa, 0x55, 0xaa },
63 { 0x01, 0x23, 0x45 },
64 { 0x20, 0x07, 0x14 },
65 };
66
67 for (size_t i = 0; i < sizeof(dc_stream)/sizeof(bt_device_class_t); i++) {
68 bt_device_class_t dc;
69 device_class_from_stream(&dc, (uint8_t*)&dc_stream[i]);
70
71 uint8_t *to_stream = (uint8_t *)&dc;
72 EXPECT_PRED_FORMAT2(check_bitfield, (unsigned)dc_stream[i][0], to_stream[0]);
73 EXPECT_PRED_FORMAT2(check_bitfield, (unsigned)dc_stream[i][1], to_stream[1]);
74 EXPECT_PRED_FORMAT2(check_bitfield, (unsigned)dc_stream[i][2], to_stream[2]);
75 }
76 }
77
TEST_F(DeviceClassTest,to_stream)78 TEST_F(DeviceClassTest, to_stream) {
79 {
80 bt_device_class_t dc;
81
82 uint8_t dc_stream0[] = { 0x00, 0x00, 0x00, 0xaa };
83 device_class_from_stream(&dc, dc_stream0);
84
85 uint8_t dc_stream1[] = { 0x00, 0x00, 0x00, 0x00 };
86 int rc = device_class_to_stream(&dc, dc_stream1, sizeof(dc_stream1));
87 EXPECT_EQ(3, rc);
88
89 uint32_t *val = (uint32_t *)&dc;
90 EXPECT_PRED_FORMAT2(check_bitfield, 0x00000000, *val & 0xffffff);
91
92 EXPECT_PRED_FORMAT2(check_bitfield, 0x00, dc_stream1[0]);
93 EXPECT_PRED_FORMAT2(check_bitfield, 0x00, dc_stream1[1]);
94 EXPECT_PRED_FORMAT2(check_bitfield, 0x00, dc_stream1[2]);
95 }
96
97 {
98 uint8_t dc_stream0[] = { 0xaa, 0x55, 0xaa, 0x55 };
99 uint8_t dc_stream1[] = { 0x00, 0x00, 0x00, 0x00 };
100
101 bt_device_class_t dc;
102 device_class_from_stream(&dc, dc_stream0);
103
104 int rc = device_class_to_stream(&dc, dc_stream1, sizeof(dc_stream1));
105 EXPECT_EQ(3, rc);
106 uint32_t *val = (uint32_t *)&dc;
107 EXPECT_PRED_FORMAT2(check_bitfield, 0x00aa55aa, *val & 0xffffff);
108
109 EXPECT_PRED_FORMAT2(check_bitfield, 0xaa, dc_stream1[0]);
110 EXPECT_PRED_FORMAT2(check_bitfield, 0x55, dc_stream1[1]);
111 EXPECT_PRED_FORMAT2(check_bitfield, 0xaa, dc_stream1[2]);
112 }
113
114 {
115 uint8_t dc_stream0[] = { 0x01, 0x23, 0x45, 0x67 };
116 uint8_t dc_stream1[] = { 0x00, 0x00, 0x00, 0x00 };
117
118 bt_device_class_t dc;
119 device_class_from_stream(&dc, dc_stream0);
120
121 int rc = device_class_to_stream(&dc, dc_stream1, sizeof(dc_stream1));
122 EXPECT_EQ(3, rc);
123 uint32_t *val = (uint32_t *)&dc;
124 EXPECT_PRED_FORMAT2(check_bitfield, 0x452301, *val & 0xffffff);
125
126 EXPECT_PRED_FORMAT2(check_bitfield, 0x01, dc_stream1[0]);
127 EXPECT_PRED_FORMAT2(check_bitfield, 0x23, dc_stream1[1]);
128 EXPECT_PRED_FORMAT2(check_bitfield, 0x45, dc_stream1[2]);
129 }
130 }
131
TEST_F(DeviceClassTest,limited_discoverable_mode)132 TEST_F(DeviceClassTest, limited_discoverable_mode) {
133 uint8_t dc_stream[] = { 0x00, 0x00, 0x00 };
134 bt_device_class_t dc;
135 device_class_from_stream(&dc, dc_stream);
136 uint32_t *test = (uint32_t *)&dc;
137
138 EXPECT_FALSE(device_class_get_limited(&dc));
139 EXPECT_EQ((unsigned)0x00000000, *test & DC_MASK);
140
141 device_class_set_limited(&dc, true);
142 EXPECT_TRUE(device_class_get_limited(&dc));
143 EXPECT_EQ((unsigned)0x00002000, *test & DC_MASK);
144
145 device_class_set_limited(&dc, false);
146 EXPECT_FALSE(device_class_get_limited(&dc));
147 EXPECT_EQ((unsigned)0x00000000, *test & DC_MASK);
148
149 device_class_set_limited(&dc, true);
150 EXPECT_PRED_FORMAT2(check_bitfield, 0x00002000, *test & DC_MASK);
151
152 device_class_set_limited(&dc, false);
153 EXPECT_PRED_FORMAT2(check_bitfield, 0x00000000, *test & DC_MASK);
154 }
155
TEST_F(DeviceClassTest,equals)156 TEST_F(DeviceClassTest, equals) {
157 uint8_t dc_stream0[] = { 0x00, 0x01, 0x02 };
158 uint8_t dc_stream1[] = { 0x00, 0x02, 0x03 };
159
160 bt_device_class_t dc0;
161 device_class_from_stream(&dc0, dc_stream0);
162 bt_device_class_t dc1;
163 device_class_from_stream(&dc1, dc_stream1);
164 EXPECT_FALSE(device_class_equals(&dc0, &dc1));
165 }
166
TEST_F(DeviceClassTest,copy)167 TEST_F(DeviceClassTest, copy) {
168 uint8_t dc_stream0[] = { 0xaa, 0x55, 0x33 };
169 bt_device_class_t dc0;
170 device_class_from_stream(&dc0, dc_stream0);
171 bt_device_class_t dc1;
172 EXPECT_TRUE(device_class_copy(&dc1, &dc0));
173 EXPECT_TRUE(device_class_equals(&dc0, &dc1));
174 }
175
TEST_F(DeviceClassTest,from_int)176 TEST_F(DeviceClassTest, from_int) {
177 bt_device_class_t dc1;
178 int cod1 = 0x5a020c; // 5898764
179 device_class_from_int(&dc1, cod1);
180
181 uint8_t dc_stream[] = { 0x0c, 0x02, 0x5a };
182 bt_device_class_t dc2;
183 device_class_from_stream(&dc2, dc_stream);
184 EXPECT_TRUE(device_class_equals(&dc1, &dc2));
185 }
186
TEST_F(DeviceClassTest,to_int)187 TEST_F(DeviceClassTest, to_int) {
188 bt_device_class_t dc1 = {{ 0x0c, 0x02, 0x5a }};
189 int cod1 = device_class_to_int(&dc1);
190
191 EXPECT_EQ(dc1._[0], 0x0c);
192 EXPECT_EQ(dc1._[1], 0x02);
193 EXPECT_EQ(dc1._[2], 0x5a);
194
195 bt_device_class_t dc2;
196 uint8_t dc_stream[] = { 0x0c, 0x02, 0x5a };
197 device_class_from_stream(&dc2, dc_stream);
198
199 EXPECT_EQ(dc2._[0], 0x0c);
200 EXPECT_EQ(dc2._[1], 0x02);
201 EXPECT_EQ(dc2._[2], 0x5a);
202
203 int cod2 = device_class_to_int(&dc2);
204 EXPECT_EQ(cod1, cod2);
205 EXPECT_EQ(cod1, 0x5a020c); // 5898764
206 }
207
TEST_F(DeviceClassTest,endian)208 TEST_F(DeviceClassTest, endian) {
209 bt_device_class_t dc;
210 int cod1 = 0x200714; // 2098964
211 device_class_from_int(&dc, cod1);
212
213 EXPECT_EQ(dc._[0], 0x14);
214 EXPECT_EQ(dc._[1], 0x07);
215 EXPECT_EQ(dc._[2], 0x20);
216
217 int cod2 = device_class_to_int(&dc);
218 EXPECT_EQ(cod1, cod2);
219 EXPECT_EQ(cod2, 0x200714); // 2098964
220 }
221