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