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