liyujie
2025-08-28 d9927380ed7c8366f762049be9f3fee225860833
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/******************************************************************************
 *
 *  Copyright 2014 Google, Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 ******************************************************************************/
 
#include <arpa/inet.h>
#include <gtest/gtest.h>
#include "osi/test/AllocationTestHarness.h"
 
#include "btcore/include/device_class.h"
 
::testing::AssertionResult check_bitfield(const char* m_expr,
                                          const char* n_expr, int m, int n) {
  if (m == n) return ::testing::AssertionSuccess();
 
  std::stringstream ss;
 
  ss.str("");
  ss << std::showbase << std::hex << std::setw(8) << std::setfill('0') << m;
  std::string expected_str = ss.str();
 
  ss.str("");
  ss << std::showbase << std::hex << std::setw(8) << std::setfill('0') << n;
  std::string actual_str = ss.str();
 
  return ::testing::AssertionFailure() << m_expr << " and " << n_expr << " ( "
                                       << expected_str << " vs " << actual_str
                                       << " )";
}
 
class DeviceClassTest : public AllocationTestHarness {};
 
TEST_F(DeviceClassTest, cod_sizeof) {
  uint8_t dc_stream[] = {0x00, 0x00, 0x00, 0x00};
  bt_device_class_t dc0;
  device_class_from_stream(&dc0, dc_stream);
  EXPECT_EQ((size_t)3, sizeof(dc0));
}
 
TEST_F(DeviceClassTest, simple) {
  uint8_t dc_stream[][sizeof(bt_device_class_t)] = {
      {0x00, 0x00, 0x00}, {0xff, 0xff, 0xff}, {0xaa, 0x55, 0xaa},
      {0x01, 0x23, 0x45}, {0x20, 0x07, 0x14},
  };
 
  for (size_t i = 0; i < sizeof(dc_stream) / sizeof(bt_device_class_t); i++) {
    bt_device_class_t dc;
    device_class_from_stream(&dc, (uint8_t*)&dc_stream[i]);
 
    uint8_t* to_stream = (uint8_t*)&dc;
    EXPECT_PRED_FORMAT2(check_bitfield, (unsigned)dc_stream[i][0],
                        to_stream[0]);
    EXPECT_PRED_FORMAT2(check_bitfield, (unsigned)dc_stream[i][1],
                        to_stream[1]);
    EXPECT_PRED_FORMAT2(check_bitfield, (unsigned)dc_stream[i][2],
                        to_stream[2]);
  }
}
 
TEST_F(DeviceClassTest, to_stream) {
  {
    bt_device_class_t dc;
 
    uint8_t dc_stream0[] = {0x00, 0x00, 0x00, 0xaa};
    device_class_from_stream(&dc, dc_stream0);
 
    uint8_t dc_stream1[] = {0x00, 0x00, 0x00, 0x00};
    int rc = device_class_to_stream(&dc, dc_stream1, sizeof(dc_stream1));
    EXPECT_EQ(3, rc);
 
    uint32_t val = 0;
    memcpy(&val, &dc, sizeof(dc));
    EXPECT_PRED_FORMAT2(check_bitfield, 0x00000000, val);
 
    EXPECT_PRED_FORMAT2(check_bitfield, 0x00, dc_stream1[0]);
    EXPECT_PRED_FORMAT2(check_bitfield, 0x00, dc_stream1[1]);
    EXPECT_PRED_FORMAT2(check_bitfield, 0x00, dc_stream1[2]);
  }
 
  {
    uint8_t dc_stream0[] = {0xaa, 0x55, 0xaa, 0x55};
    uint8_t dc_stream1[] = {0x00, 0x00, 0x00, 0x00};
 
    bt_device_class_t dc;
    device_class_from_stream(&dc, dc_stream0);
 
    int rc = device_class_to_stream(&dc, dc_stream1, sizeof(dc_stream1));
    EXPECT_EQ(3, rc);
    uint32_t val = 0;
    memcpy(&val, &dc, sizeof(dc));
    EXPECT_PRED_FORMAT2(check_bitfield, 0x00aa55aa, val);
 
    EXPECT_PRED_FORMAT2(check_bitfield, 0xaa, dc_stream1[0]);
    EXPECT_PRED_FORMAT2(check_bitfield, 0x55, dc_stream1[1]);
    EXPECT_PRED_FORMAT2(check_bitfield, 0xaa, dc_stream1[2]);
  }
 
  {
    uint8_t dc_stream0[] = {0x01, 0x23, 0x45, 0x67};
    uint8_t dc_stream1[] = {0x00, 0x00, 0x00, 0x00};
 
    bt_device_class_t dc;
    device_class_from_stream(&dc, dc_stream0);
 
    int rc = device_class_to_stream(&dc, dc_stream1, sizeof(dc_stream1));
    EXPECT_EQ(3, rc);
    uint32_t val = 0;
    memcpy(&val, &dc, sizeof(dc));
    EXPECT_PRED_FORMAT2(check_bitfield, 0x452301, val);
 
    EXPECT_PRED_FORMAT2(check_bitfield, 0x01, dc_stream1[0]);
    EXPECT_PRED_FORMAT2(check_bitfield, 0x23, dc_stream1[1]);
    EXPECT_PRED_FORMAT2(check_bitfield, 0x45, dc_stream1[2]);
  }
}
 
TEST_F(DeviceClassTest, limited_discoverable_mode) {
  uint8_t dc_stream[] = {0x00, 0x00, 0x00};
  bt_device_class_t dc;
  device_class_from_stream(&dc, dc_stream);
  uint32_t test = 0;
  memcpy(&test, &dc, sizeof(dc));
 
  EXPECT_FALSE(device_class_get_limited(&dc));
  EXPECT_EQ((unsigned)0x00000000, test);
 
  device_class_set_limited(&dc, true);
  test = 0;
  memcpy(&test, &dc, sizeof(dc));
  EXPECT_TRUE(device_class_get_limited(&dc));
  EXPECT_EQ((unsigned)0x00002000, test);
 
  device_class_set_limited(&dc, false);
  test = 0;
  memcpy(&test, &dc, sizeof(dc));
  EXPECT_FALSE(device_class_get_limited(&dc));
  EXPECT_EQ((unsigned)0x00000000, test);
 
  device_class_set_limited(&dc, true);
  test = 0;
  memcpy(&test, &dc, sizeof(dc));
  EXPECT_PRED_FORMAT2(check_bitfield, 0x00002000, test);
 
  device_class_set_limited(&dc, false);
  test = 0;
  memcpy(&test, &dc, sizeof(dc));
  EXPECT_PRED_FORMAT2(check_bitfield, 0x00000000, test);
}
 
TEST_F(DeviceClassTest, equals) {
  uint8_t dc_stream0[] = {0x00, 0x01, 0x02};
  uint8_t dc_stream1[] = {0x00, 0x02, 0x03};
 
  bt_device_class_t dc0;
  device_class_from_stream(&dc0, dc_stream0);
  bt_device_class_t dc1;
  device_class_from_stream(&dc1, dc_stream1);
  EXPECT_FALSE(device_class_equals(&dc0, &dc1));
}
 
TEST_F(DeviceClassTest, copy) {
  uint8_t dc_stream0[] = {0xaa, 0x55, 0x33};
  bt_device_class_t dc0;
  device_class_from_stream(&dc0, dc_stream0);
  bt_device_class_t dc1;
  EXPECT_TRUE(device_class_copy(&dc1, &dc0));
  EXPECT_TRUE(device_class_equals(&dc0, &dc1));
}
 
TEST_F(DeviceClassTest, from_int) {
  bt_device_class_t dc1;
  int cod1 = 0x5a020c;  // 5898764
  device_class_from_int(&dc1, cod1);
 
  uint8_t dc_stream[] = {0x0c, 0x02, 0x5a};
  bt_device_class_t dc2;
  device_class_from_stream(&dc2, dc_stream);
  EXPECT_TRUE(device_class_equals(&dc1, &dc2));
}
 
TEST_F(DeviceClassTest, to_int) {
  bt_device_class_t dc1 = {{0x0c, 0x02, 0x5a}};
  int cod1 = device_class_to_int(&dc1);
 
  EXPECT_EQ(dc1._[0], 0x0c);
  EXPECT_EQ(dc1._[1], 0x02);
  EXPECT_EQ(dc1._[2], 0x5a);
 
  bt_device_class_t dc2;
  uint8_t dc_stream[] = {0x0c, 0x02, 0x5a};
  device_class_from_stream(&dc2, dc_stream);
 
  EXPECT_EQ(dc2._[0], 0x0c);
  EXPECT_EQ(dc2._[1], 0x02);
  EXPECT_EQ(dc2._[2], 0x5a);
 
  int cod2 = device_class_to_int(&dc2);
  EXPECT_EQ(cod1, cod2);
  EXPECT_EQ(cod1, 0x5a020c);  // 5898764
}
 
TEST_F(DeviceClassTest, endian) {
  bt_device_class_t dc;
  int cod1 = 0x200714;  // 2098964
  device_class_from_int(&dc, cod1);
 
  EXPECT_EQ(dc._[0], 0x14);
  EXPECT_EQ(dc._[1], 0x07);
  EXPECT_EQ(dc._[2], 0x20);
 
  int cod2 = device_class_to_int(&dc);
  EXPECT_EQ(cod1, cod2);
  EXPECT_EQ(cod2, 0x200714);  // 2098964
}