liyujie
2025-08-28 786ff4f4ca2374bdd9177f2e24b503d43e7a3b93
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include "stack/gatt/connection_manager.h"
 
#include <base/bind.h>
#include <base/callback.h>
#include <base/location.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <memory>
#include "osi/include/alarm.h"
#include "osi/test/alarm_mock.h"
 
using testing::_;
using testing::Mock;
using testing::Return;
using testing::SaveArg;
 
using connection_manager::tAPP_ID;
 
namespace {
// convinience mock, for verifying whitelist operaitons on lower layer are
// actually scheduled
class WhiteListMock {
 public:
  MOCK_METHOD1(WhiteListAdd, bool(const RawAddress&));
  MOCK_METHOD1(WhiteListRemove, void(const RawAddress&));
  MOCK_METHOD0(WhiteListClear, void());
  MOCK_METHOD0(SetLeConnectionModeToFast, bool());
  MOCK_METHOD0(SetLeConnectionModeToSlow, void());
  MOCK_METHOD2(OnConnectionTimedOut, void(uint8_t, const RawAddress&));
};
 
std::unique_ptr<WhiteListMock> localWhiteListMock;
}  // namespace
 
RawAddress address1{{0x01, 0x01, 0x01, 0x01, 0x01, 0x01}};
RawAddress address2{{0x22, 0x22, 0x02, 0x22, 0x33, 0x22}};
 
constexpr tAPP_ID CLIENT1 = 1;
constexpr tAPP_ID CLIENT2 = 2;
constexpr tAPP_ID CLIENT3 = 3;
constexpr tAPP_ID CLIENT10 = 10;
 
// Implementation of btm_ble_bgconn.h API for test.
bool BTM_WhiteListAdd(const RawAddress& address) {
  return localWhiteListMock->WhiteListAdd(address);
}
 
void BTM_WhiteListRemove(const RawAddress& address) {
  return localWhiteListMock->WhiteListRemove(address);
}
 
void BTM_WhiteListClear() { return localWhiteListMock->WhiteListClear(); }
 
bool BTM_SetLeConnectionModeToFast() {
  return localWhiteListMock->SetLeConnectionModeToFast();
}
 
void BTM_SetLeConnectionModeToSlow() {
  localWhiteListMock->SetLeConnectionModeToSlow();
}
 
namespace connection_manager {
class BleConnectionManager : public testing::Test {
  virtual void SetUp() {
    localWhiteListMock = std::make_unique<WhiteListMock>();
  }
 
  virtual void TearDown() {
    connection_manager::reset(true);
    AlarmMock::Reset();
    localWhiteListMock.reset();
  }
};
 
void on_connection_timed_out(uint8_t app_id, const RawAddress& address) {
  localWhiteListMock->OnConnectionTimedOut(app_id, address);
}
 
/** Verify that app can add a device to white list, it is returned as interested
 * app, and then can remove the device later. */
TEST_F(BleConnectionManager, test_background_connection_add_remove) {
  EXPECT_CALL(*localWhiteListMock, WhiteListAdd(address1))
      .WillOnce(Return(true));
  EXPECT_CALL(*localWhiteListMock, WhiteListRemove(_)).Times(0);
 
  EXPECT_TRUE(background_connect_add(CLIENT1, address1));
 
  Mock::VerifyAndClearExpectations(localWhiteListMock.get());
 
  std::set<tAPP_ID> apps = get_apps_connecting_to(address1);
  EXPECT_EQ(apps.size(), 1UL);
  EXPECT_EQ(apps.count(CLIENT1), 1UL);
 
  EXPECT_CALL(*localWhiteListMock, WhiteListAdd(_)).Times(0);
  EXPECT_CALL(*localWhiteListMock, WhiteListRemove(address1)).Times(1);
 
  EXPECT_TRUE(background_connect_remove(CLIENT1, address1));
 
  EXPECT_EQ(get_apps_connecting_to(address1).size(), 0UL);
 
  Mock::VerifyAndClearExpectations(localWhiteListMock.get());
}
 
/** Verify that multiple clients adding same device multiple times, result in
 * device being added to whtie list only once, also, that device is removed only
 * after last client removes it. */
TEST_F(BleConnectionManager, test_background_connection_multiple_clients) {
  EXPECT_CALL(*localWhiteListMock, WhiteListAdd(address1))
      .WillOnce(Return(true));
  EXPECT_CALL(*localWhiteListMock, WhiteListRemove(_)).Times(0);
  EXPECT_TRUE(background_connect_add(CLIENT1, address1));
  EXPECT_TRUE(background_connect_add(CLIENT1, address1));
  EXPECT_TRUE(background_connect_add(CLIENT2, address1));
  EXPECT_TRUE(background_connect_add(CLIENT3, address1));
 
  EXPECT_EQ(get_apps_connecting_to(address1).size(), 3UL);
 
  Mock::VerifyAndClearExpectations(localWhiteListMock.get());
 
  EXPECT_CALL(*localWhiteListMock, WhiteListAdd(_)).Times(0);
 
  // removing from nonexisting client, should fail
  EXPECT_FALSE(background_connect_remove(CLIENT10, address1));
 
  EXPECT_TRUE(background_connect_remove(CLIENT1, address1));
  // already removed,  removing from same client twice should return false;
  EXPECT_FALSE(background_connect_remove(CLIENT1, address1));
  EXPECT_TRUE(background_connect_remove(CLIENT2, address1));
 
  EXPECT_CALL(*localWhiteListMock, WhiteListRemove(address1)).Times(1);
  EXPECT_TRUE(background_connect_remove(CLIENT3, address1));
 
  EXPECT_EQ(get_apps_connecting_to(address1).size(), 0UL);
 
  Mock::VerifyAndClearExpectations(localWhiteListMock.get());
}
 
/** Verify adding/removing device to direct connection. */
TEST_F(BleConnectionManager, test_direct_connection_client) {
  // Direct connect attempt: use faster scan parameters, add to white list,
  // start 30 timeout
  EXPECT_CALL(*localWhiteListMock, SetLeConnectionModeToFast()).Times(1);
  EXPECT_CALL(*localWhiteListMock, WhiteListAdd(address1))
      .WillOnce(Return(true));
  EXPECT_CALL(*localWhiteListMock, WhiteListRemove(_)).Times(0);
  EXPECT_CALL(*AlarmMock::Get(), AlarmNew(_)).Times(1);
  EXPECT_CALL(*AlarmMock::Get(), AlarmSetOnMloop(_, _, _, _)).Times(1);
  EXPECT_TRUE(direct_connect_add(CLIENT1, address1));
 
  // App already doing a direct connection, attempt to re-add result in failure
  EXPECT_FALSE(direct_connect_add(CLIENT1, address1));
 
  // Client that don't do direct connection should fail attempt to stop it
  EXPECT_FALSE(direct_connect_remove(CLIENT2, address1));
 
  Mock::VerifyAndClearExpectations(localWhiteListMock.get());
 
  EXPECT_CALL(*localWhiteListMock, SetLeConnectionModeToSlow()).Times(1);
  EXPECT_CALL(*localWhiteListMock, WhiteListRemove(_)).Times(1);
  EXPECT_CALL(*AlarmMock::Get(), AlarmFree(_)).Times(1);
 
  // Removal should lower the connection parameters, and free the alarm.
  // Even though we call WhiteListRemove, it won't be executed over HCI until
  // whitelist is in use, i.e. next connection attempt
  EXPECT_TRUE(direct_connect_remove(CLIENT1, address1));
 
  Mock::VerifyAndClearExpectations(localWhiteListMock.get());
}
 
/** Verify direct connection timeout does remove device from white list, and
 * lower the connection scan parameters */
TEST_F(BleConnectionManager, test_direct_connect_timeout) {
  EXPECT_CALL(*localWhiteListMock, SetLeConnectionModeToFast()).Times(1);
  EXPECT_CALL(*localWhiteListMock, WhiteListAdd(address1))
      .WillOnce(Return(true));
  EXPECT_CALL(*AlarmMock::Get(), AlarmNew(_)).Times(1);
  alarm_callback_t alarm_callback = nullptr;
  void* alarm_data = nullptr;
 
  EXPECT_CALL(*AlarmMock::Get(), AlarmSetOnMloop(_, _, _, _))
      .Times(1)
      .WillOnce(DoAll(SaveArg<2>(&alarm_callback), SaveArg<3>(&alarm_data)));
 
  // Start direct connect attempt...
  EXPECT_TRUE(direct_connect_add(CLIENT1, address1));
 
  Mock::VerifyAndClearExpectations(localWhiteListMock.get());
 
  EXPECT_CALL(*localWhiteListMock, SetLeConnectionModeToSlow()).Times(1);
  EXPECT_CALL(*localWhiteListMock, WhiteListRemove(_)).Times(1);
  EXPECT_CALL(*localWhiteListMock, OnConnectionTimedOut(CLIENT1, address1)).Times(1);
  EXPECT_CALL(*AlarmMock::Get(), AlarmFree(_)).Times(1);
 
  // simulate timeout seconds passed, alarm executing
  alarm_callback(alarm_data);
 
  Mock::VerifyAndClearExpectations(localWhiteListMock.get());
}
 
/** Verify that we properly handle successfull direct connection */
TEST_F(BleConnectionManager, test_direct_connection_success) {
  EXPECT_CALL(*localWhiteListMock, SetLeConnectionModeToFast()).Times(1);
  EXPECT_CALL(*localWhiteListMock, WhiteListAdd(address1))
      .WillOnce(Return(true));
  EXPECT_CALL(*AlarmMock::Get(), AlarmNew(_)).Times(1);
  EXPECT_CALL(*AlarmMock::Get(), AlarmSetOnMloop(_, _, _, _)).Times(1);
 
  // Start direct connect attempt...
  EXPECT_TRUE(direct_connect_add(CLIENT1, address1));
 
  Mock::VerifyAndClearExpectations(localWhiteListMock.get());
 
  EXPECT_CALL(*localWhiteListMock, SetLeConnectionModeToSlow()).Times(1);
  EXPECT_CALL(*localWhiteListMock, WhiteListRemove(address1)).Times(1);
  EXPECT_CALL(*AlarmMock::Get(), AlarmFree(_)).Times(1);
  // simulate event from lower layers - connections was established
  // successfully.
  on_connection_complete(address1);
}
 
/** Verify that we properly handle application unregistration */
TEST_F(BleConnectionManager, test_app_unregister) {
  /* Test scenario:
   * - Client 1 connecting to address1 and address2.
   * - Client 2 connecting to address2
   * - unregistration of Client1 should trigger address1 removal from white list
   * - unregistration of Client2 should trigger address2 removal
   */
 
  EXPECT_CALL(*localWhiteListMock, WhiteListAdd(address1))
      .WillOnce(Return(true));
  EXPECT_CALL(*localWhiteListMock, WhiteListAdd(address2))
      .WillOnce(Return(true));
  EXPECT_TRUE(direct_connect_add(CLIENT1, address1));
  EXPECT_TRUE(background_connect_add(CLIENT1, address2));
  EXPECT_TRUE(direct_connect_add(CLIENT2, address2));
  Mock::VerifyAndClearExpectations(localWhiteListMock.get());
 
  EXPECT_CALL(*localWhiteListMock, WhiteListRemove(address1)).Times(1);
  on_app_deregistered(CLIENT1);
  Mock::VerifyAndClearExpectations(localWhiteListMock.get());
 
  EXPECT_CALL(*localWhiteListMock, WhiteListRemove(address2)).Times(1);
  on_app_deregistered(CLIENT2);
}
 
/** Verify adding device to both direct connection and background connection. */
TEST_F(BleConnectionManager, test_direct_and_background_connect) {
  EXPECT_CALL(*localWhiteListMock, SetLeConnectionModeToFast()).Times(1);
  EXPECT_CALL(*localWhiteListMock, WhiteListAdd(address1))
      .WillOnce(Return(true));
  EXPECT_CALL(*localWhiteListMock, WhiteListRemove(_)).Times(0);
  EXPECT_CALL(*AlarmMock::Get(), AlarmNew(_)).Times(1);
  EXPECT_CALL(*AlarmMock::Get(), AlarmSetOnMloop(_, _, _, _)).Times(1);
  // add device as both direct and background connection
  EXPECT_TRUE(direct_connect_add(CLIENT1, address1));
  EXPECT_TRUE(background_connect_add(CLIENT1, address1));
 
  Mock::VerifyAndClearExpectations(localWhiteListMock.get());
 
  EXPECT_CALL(*localWhiteListMock, SetLeConnectionModeToSlow()).Times(1);
  EXPECT_CALL(*AlarmMock::Get(), AlarmFree(_)).Times(1);
  // not removing from white list yet, as the background connection is still
  // pending.
  EXPECT_TRUE(direct_connect_remove(CLIENT1, address1));
 
  // remove from white list, because no more interest in device.
  EXPECT_CALL(*localWhiteListMock, WhiteListRemove(_)).Times(1);
  EXPECT_TRUE(background_connect_remove(CLIENT1, address1));
 
  Mock::VerifyAndClearExpectations(localWhiteListMock.get());
}
 
}  // namespace connection_manager