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