OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/proximity_auth/proximity_monitor_impl.h" | 5 #include "components/proximity_auth/proximity_monitor_impl.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/test/histogram_tester.h" | 13 #include "base/test/histogram_tester.h" |
14 #include "base/test/simple_test_tick_clock.h" | 14 #include "base/test/simple_test_tick_clock.h" |
15 #include "base/test/test_simple_task_runner.h" | 15 #include "base/test/test_simple_task_runner.h" |
16 #include "base/threading/thread_task_runner_handle.h" | 16 #include "base/threading/thread_task_runner_handle.h" |
17 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 18 #include "components/cryptauth/fake_connection.h" |
18 #include "components/cryptauth/remote_device.h" | 19 #include "components/cryptauth/remote_device.h" |
19 #include "components/proximity_auth/logging/logging.h" | 20 #include "components/proximity_auth/logging/logging.h" |
20 #include "components/proximity_auth/proximity_monitor_observer.h" | 21 #include "components/proximity_auth/proximity_monitor_observer.h" |
21 #include "device/bluetooth/bluetooth_adapter_factory.h" | 22 #include "device/bluetooth/bluetooth_adapter_factory.h" |
22 #include "device/bluetooth/test/mock_bluetooth_adapter.h" | 23 #include "device/bluetooth/test/mock_bluetooth_adapter.h" |
23 #include "testing/gmock/include/gmock/gmock.h" | 24 #include "testing/gmock/include/gmock/gmock.h" |
24 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
25 | 26 |
26 using device::BluetoothDevice; | 27 using device::BluetoothDevice; |
27 using testing::_; | 28 using testing::_; |
28 using testing::NiceMock; | 29 using testing::NiceMock; |
29 using testing::Return; | 30 using testing::Return; |
30 using testing::SaveArg; | 31 using testing::SaveArg; |
31 | 32 |
32 namespace proximity_auth { | 33 namespace proximity_auth { |
33 namespace { | 34 namespace { |
34 | 35 |
35 const char kRemoteDeviceUserId[] = "example@gmail.com"; | 36 const char kRemoteDeviceUserId[] = "example@gmail.com"; |
36 const char kBluetoothAddress[] = "AA:BB:CC:DD:EE:FF"; | |
37 const char kRemoteDevicePublicKey[] = "Remote Public Key"; | 37 const char kRemoteDevicePublicKey[] = "Remote Public Key"; |
38 const char kRemoteDeviceName[] = "LGE Nexus 5"; | 38 const char kRemoteDeviceName[] = "LGE Nexus 5"; |
| 39 const char kBluetoothAddress[] = "AA:BB:CC:DD:EE:FF"; |
39 const char kPersistentSymmetricKey[] = "PSK"; | 40 const char kPersistentSymmetricKey[] = "PSK"; |
40 | 41 const int kRssiThreshold = -45; |
41 class TestProximityMonitorImpl : public ProximityMonitorImpl { | |
42 public: | |
43 explicit TestProximityMonitorImpl( | |
44 const cryptauth::RemoteDevice& remote_device, | |
45 std::unique_ptr<base::TickClock> clock) | |
46 : ProximityMonitorImpl(remote_device, std::move(clock)) {} | |
47 ~TestProximityMonitorImpl() override {} | |
48 | |
49 using ProximityMonitorImpl::SetStrategy; | |
50 | |
51 private: | |
52 DISALLOW_COPY_AND_ASSIGN(TestProximityMonitorImpl); | |
53 }; | |
54 | 42 |
55 class MockProximityMonitorObserver : public ProximityMonitorObserver { | 43 class MockProximityMonitorObserver : public ProximityMonitorObserver { |
56 public: | 44 public: |
57 MockProximityMonitorObserver() {} | 45 MockProximityMonitorObserver() {} |
58 ~MockProximityMonitorObserver() override {} | 46 ~MockProximityMonitorObserver() override {} |
59 | 47 |
60 MOCK_METHOD0(OnProximityStateChanged, void()); | 48 MOCK_METHOD0(OnProximityStateChanged, void()); |
61 | 49 |
62 private: | 50 private: |
63 DISALLOW_COPY_AND_ASSIGN(MockProximityMonitorObserver); | 51 DISALLOW_COPY_AND_ASSIGN(MockProximityMonitorObserver); |
(...skipping 12 matching lines...) Expand all Loading... |
76 } // namespace | 64 } // namespace |
77 | 65 |
78 class ProximityAuthProximityMonitorImplTest : public testing::Test { | 66 class ProximityAuthProximityMonitorImplTest : public testing::Test { |
79 public: | 67 public: |
80 ProximityAuthProximityMonitorImplTest() | 68 ProximityAuthProximityMonitorImplTest() |
81 : clock_(new base::SimpleTestTickClock()), | 69 : clock_(new base::SimpleTestTickClock()), |
82 bluetooth_adapter_(CreateAndRegisterMockBluetoothAdapter()), | 70 bluetooth_adapter_(CreateAndRegisterMockBluetoothAdapter()), |
83 remote_bluetooth_device_(&*bluetooth_adapter_, | 71 remote_bluetooth_device_(&*bluetooth_adapter_, |
84 0, | 72 0, |
85 kRemoteDeviceName, | 73 kRemoteDeviceName, |
86 kBluetoothAddress, | 74 "", |
87 false /* paired */, | 75 false /* paired */, |
88 true /* connected */), | 76 true /* connected */), |
89 monitor_( | 77 remote_device_(kRemoteDeviceUserId, |
90 cryptauth::RemoteDevice(kRemoteDeviceUserId, | 78 kRemoteDeviceName, |
91 kRemoteDeviceName, | 79 kRemoteDevicePublicKey, |
92 kRemoteDevicePublicKey, | 80 kBluetoothAddress, |
93 kBluetoothAddress, | 81 kPersistentSymmetricKey, |
94 kPersistentSymmetricKey, | 82 std::string()), |
95 std::string()), | 83 connection_(remote_device_), |
96 base::WrapUnique(clock_)), | 84 monitor_(&connection_, base::WrapUnique(clock_)), |
97 task_runner_(new base::TestSimpleTaskRunner()), | 85 task_runner_(new base::TestSimpleTaskRunner()), |
98 thread_task_runner_handle_(task_runner_) { | 86 thread_task_runner_handle_(task_runner_) { |
99 ON_CALL(*bluetooth_adapter_, GetDevice(kBluetoothAddress)) | 87 ON_CALL(*bluetooth_adapter_, GetDevice(kBluetoothAddress)) |
100 .WillByDefault(Return(&remote_bluetooth_device_)); | 88 .WillByDefault(Return(&remote_bluetooth_device_)); |
101 ON_CALL(remote_bluetooth_device_, GetConnectionInfo(_)) | 89 ON_CALL(remote_bluetooth_device_, GetConnectionInfo(_)) |
102 .WillByDefault(SaveArg<0>(&connection_info_callback_)); | 90 .WillByDefault(SaveArg<0>(&connection_info_callback_)); |
103 monitor_.AddObserver(&observer_); | 91 monitor_.AddObserver(&observer_); |
104 } | 92 } |
| 93 |
105 ~ProximityAuthProximityMonitorImplTest() override {} | 94 ~ProximityAuthProximityMonitorImplTest() override {} |
106 | 95 |
107 void RunPendingTasks() { task_runner_->RunPendingTasks(); } | 96 void RunPendingTasks() { task_runner_->RunPendingTasks(); } |
108 | 97 |
109 void ProvideConnectionInfo( | 98 void ProvideConnectionInfo( |
110 const BluetoothDevice::ConnectionInfo& connection_info) { | 99 const BluetoothDevice::ConnectionInfo& connection_info) { |
111 RunPendingTasks(); | 100 RunPendingTasks(); |
112 connection_info_callback_.Run(connection_info); | 101 connection_info_callback_.Run(connection_info); |
113 | 102 |
114 // Reset the callback to ensure that tests correctly only respond at most | 103 // Reset the callback to ensure that tests correctly only respond at most |
115 // once per call to GetConnectionInfo(). | 104 // once per call to GetConnectionInfo(). |
116 connection_info_callback_ = BluetoothDevice::ConnectionInfoCallback(); | 105 connection_info_callback_ = BluetoothDevice::ConnectionInfoCallback(); |
117 } | 106 } |
118 | 107 |
119 protected: | 108 protected: |
120 // Mock for verifying interactions with the proximity monitor's observer. | 109 // Mock for verifying interactions with the proximity monitor's observer. |
121 NiceMock<MockProximityMonitorObserver> observer_; | 110 NiceMock<MockProximityMonitorObserver> observer_; |
122 | 111 |
123 // Clock used for verifying time calculations. Owned by the monitor_. | 112 // Clock used for verifying time calculations. Owned by the monitor_. |
124 base::SimpleTestTickClock* clock_; | 113 base::SimpleTestTickClock* clock_; |
125 | 114 |
126 // Mocks used for verifying interactions with the Bluetooth subsystem. | 115 // Mocks used for verifying interactions with the Bluetooth subsystem. |
127 scoped_refptr<device::MockBluetoothAdapter> bluetooth_adapter_; | 116 scoped_refptr<device::MockBluetoothAdapter> bluetooth_adapter_; |
128 NiceMock<device::MockBluetoothDevice> remote_bluetooth_device_; | 117 NiceMock<device::MockBluetoothDevice> remote_bluetooth_device_; |
| 118 cryptauth::RemoteDevice remote_device_; |
| 119 cryptauth::FakeConnection connection_; |
129 | 120 |
130 // The proximity monitor under test. | 121 // The proximity monitor under test. |
131 TestProximityMonitorImpl monitor_; | 122 ProximityMonitorImpl monitor_; |
132 | 123 |
133 private: | 124 private: |
134 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 125 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
135 base::ThreadTaskRunnerHandle thread_task_runner_handle_; | 126 base::ThreadTaskRunnerHandle thread_task_runner_handle_; |
136 BluetoothDevice::ConnectionInfoCallback connection_info_callback_; | 127 BluetoothDevice::ConnectionInfoCallback connection_info_callback_; |
137 ScopedDisableLoggingForTesting disable_logging_; | 128 ScopedDisableLoggingForTesting disable_logging_; |
138 }; | 129 }; |
139 | 130 |
140 TEST_F(ProximityAuthProximityMonitorImplTest, GetStrategy) { | 131 TEST_F(ProximityAuthProximityMonitorImplTest, IsUnlockAllowed_NeverStarted) { |
141 EXPECT_EQ(ProximityMonitor::Strategy::NONE, monitor_.GetStrategy()); | |
142 | |
143 monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_TRANSMIT_POWER); | |
144 EXPECT_EQ(ProximityMonitor::Strategy::CHECK_TRANSMIT_POWER, | |
145 monitor_.GetStrategy()); | |
146 } | |
147 | |
148 TEST_F(ProximityAuthProximityMonitorImplTest, ProximityState_StrategyIsNone) { | |
149 monitor_.SetStrategy(ProximityMonitor::Strategy::NONE); | |
150 | |
151 EXPECT_TRUE(monitor_.IsUnlockAllowed()); | |
152 EXPECT_FALSE(monitor_.IsInRssiRange()); | |
153 } | |
154 | |
155 TEST_F(ProximityAuthProximityMonitorImplTest, ProximityState_NeverStarted) { | |
156 monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_RSSI); | |
157 | |
158 EXPECT_FALSE(monitor_.IsUnlockAllowed()); | |
159 EXPECT_FALSE(monitor_.IsInRssiRange()); | |
160 } | |
161 | |
162 TEST_F(ProximityAuthProximityMonitorImplTest, | |
163 ProximityState_Started_NoConnectionInfoReceivedYet) { | |
164 monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_RSSI); | |
165 monitor_.Start(); | |
166 | |
167 EXPECT_FALSE(monitor_.IsUnlockAllowed()); | |
168 EXPECT_FALSE(monitor_.IsInRssiRange()); | |
169 } | |
170 | |
171 TEST_F(ProximityAuthProximityMonitorImplTest, | |
172 ProximityState_InformsObserverOfChanges) { | |
173 monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_TRANSMIT_POWER); | |
174 | |
175 // Initially, the device is not in proximity. | |
176 monitor_.Start(); | |
177 EXPECT_FALSE(monitor_.IsUnlockAllowed()); | |
178 | |
179 // Simulate a reading indicating proximity. | |
180 EXPECT_CALL(observer_, OnProximityStateChanged()).Times(1); | |
181 ProvideConnectionInfo({0, 0, 4}); | |
182 EXPECT_TRUE(monitor_.IsUnlockAllowed()); | |
183 | |
184 // Simulate a reading indicating non-proximity. | |
185 EXPECT_CALL(observer_, OnProximityStateChanged()).Times(1); | |
186 ProvideConnectionInfo({0, 4, 4}); | |
187 EXPECT_FALSE(monitor_.IsUnlockAllowed()); | 132 EXPECT_FALSE(monitor_.IsUnlockAllowed()); |
188 } | 133 } |
189 | 134 |
190 TEST_F(ProximityAuthProximityMonitorImplTest, | 135 TEST_F(ProximityAuthProximityMonitorImplTest, |
191 ProximityState_CheckRssi_RssiIndicatesProximity_TxPowerDoesNot) { | 136 IsUnlockAllowed_Started_NoConnectionInfoReceivedYet) { |
192 monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_RSSI); | |
193 monitor_.Start(); | 137 monitor_.Start(); |
194 | 138 EXPECT_FALSE(monitor_.IsUnlockAllowed()); |
195 ProvideConnectionInfo({0, 4, 4}); | |
196 | |
197 EXPECT_TRUE(monitor_.IsUnlockAllowed()); | |
198 EXPECT_TRUE(monitor_.IsInRssiRange()); | |
199 } | 139 } |
200 | 140 |
201 TEST_F(ProximityAuthProximityMonitorImplTest, | 141 TEST_F(ProximityAuthProximityMonitorImplTest, IsUnlockAllowed_RssiInRange) { |
202 ProximityState_CheckRssi_TxPowerIndicatesProximity_RssiDoesNot) { | |
203 monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_RSSI); | |
204 monitor_.Start(); | 142 monitor_.Start(); |
205 | 143 ProvideConnectionInfo({0, 4, 4}); |
206 ProvideConnectionInfo({-10, 0, 4}); | 144 EXPECT_TRUE(monitor_.IsUnlockAllowed()); |
207 | |
208 EXPECT_FALSE(monitor_.IsUnlockAllowed()); | |
209 EXPECT_FALSE(monitor_.IsInRssiRange()); | |
210 } | 145 } |
211 | 146 |
212 TEST_F(ProximityAuthProximityMonitorImplTest, | 147 TEST_F(ProximityAuthProximityMonitorImplTest, IsUnlockAllowed_UnknownRssi) { |
213 ProximityState_CheckRssi_NeitherRssiNorTxPowerIndicatesProximity) { | |
214 monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_RSSI); | |
215 monitor_.Start(); | |
216 | |
217 ProvideConnectionInfo({-10, 4, 4}); | |
218 | |
219 EXPECT_FALSE(monitor_.IsUnlockAllowed()); | |
220 EXPECT_FALSE(monitor_.IsInRssiRange()); | |
221 } | |
222 | |
223 TEST_F(ProximityAuthProximityMonitorImplTest, | |
224 ProximityState_CheckRssi_BothRssiAndTxPowerIndicateProximity) { | |
225 monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_RSSI); | |
226 monitor_.Start(); | |
227 | |
228 ProvideConnectionInfo({0, 0, 4}); | |
229 | |
230 EXPECT_TRUE(monitor_.IsUnlockAllowed()); | |
231 EXPECT_TRUE(monitor_.IsInRssiRange()); | |
232 } | |
233 | |
234 TEST_F(ProximityAuthProximityMonitorImplTest, | |
235 ProximityState_CheckRssi_UnknownRssi) { | |
236 monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_RSSI); | |
237 monitor_.Start(); | 148 monitor_.Start(); |
238 | 149 |
239 ProvideConnectionInfo({0, 0, 4}); | 150 ProvideConnectionInfo({0, 0, 4}); |
240 ProvideConnectionInfo({BluetoothDevice::kUnknownPower, 0, 4}); | 151 ProvideConnectionInfo({BluetoothDevice::kUnknownPower, 0, 4}); |
241 | 152 |
242 EXPECT_FALSE(monitor_.IsUnlockAllowed()); | 153 EXPECT_FALSE(monitor_.IsUnlockAllowed()); |
243 EXPECT_FALSE(monitor_.IsInRssiRange()); | |
244 } | 154 } |
245 | 155 |
246 TEST_F(ProximityAuthProximityMonitorImplTest, | 156 TEST_F(ProximityAuthProximityMonitorImplTest, |
247 ProximityState_CheckRssi_UnknownTxPower) { | 157 IsUnlockAllowed_InformsObserverOfChanges) { |
248 monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_RSSI); | 158 // Initially, the device is not in proximity. |
| 159 monitor_.Start(); |
| 160 EXPECT_FALSE(monitor_.IsUnlockAllowed()); |
| 161 |
| 162 // Simulate receiving an RSSI reading in proximity. |
| 163 EXPECT_CALL(observer_, OnProximityStateChanged()).Times(1); |
| 164 ProvideConnectionInfo({kRssiThreshold / 2, 4, 4}); |
| 165 EXPECT_TRUE(monitor_.IsUnlockAllowed()); |
| 166 |
| 167 // Simulate a reading indicating non-proximity. |
| 168 EXPECT_CALL(observer_, OnProximityStateChanged()).Times(1); |
| 169 ProvideConnectionInfo({2 * kRssiThreshold, 4, 4}); |
| 170 ProvideConnectionInfo({2 * kRssiThreshold, 4, 4}); |
| 171 EXPECT_FALSE(monitor_.IsUnlockAllowed()); |
| 172 } |
| 173 |
| 174 TEST_F(ProximityAuthProximityMonitorImplTest, IsUnlockAllowed_StartThenStop) { |
249 monitor_.Start(); | 175 monitor_.Start(); |
250 | 176 |
251 ProvideConnectionInfo({0, 0, 4}); | 177 ProvideConnectionInfo({0, 0, 4}); |
252 ProvideConnectionInfo({0, BluetoothDevice::kUnknownPower, 4}); | 178 EXPECT_TRUE(monitor_.IsUnlockAllowed()); |
253 | 179 |
| 180 monitor_.Stop(); |
254 EXPECT_FALSE(monitor_.IsUnlockAllowed()); | 181 EXPECT_FALSE(monitor_.IsUnlockAllowed()); |
255 EXPECT_FALSE(monitor_.IsInRssiRange()); | |
256 } | 182 } |
257 | 183 |
258 TEST_F(ProximityAuthProximityMonitorImplTest, | 184 TEST_F(ProximityAuthProximityMonitorImplTest, |
259 ProximityState_CheckRssi_UnknownMaxTxPower) { | 185 IsUnlockAllowed_StartThenStopThenStartAgain) { |
260 monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_RSSI); | |
261 monitor_.Start(); | 186 monitor_.Start(); |
262 | 187 ProvideConnectionInfo({kRssiThreshold / 2, 4, 4}); |
263 ProvideConnectionInfo({0, 0, 4}); | 188 ProvideConnectionInfo({kRssiThreshold / 2, 4, 4}); |
264 ProvideConnectionInfo({0, 0, BluetoothDevice::kUnknownPower}); | 189 ProvideConnectionInfo({kRssiThreshold / 2, 4, 4}); |
265 | 190 ProvideConnectionInfo({kRssiThreshold / 2, 4, 4}); |
266 EXPECT_FALSE(monitor_.IsUnlockAllowed()); | 191 ProvideConnectionInfo({kRssiThreshold / 2, 4, 4}); |
267 EXPECT_FALSE(monitor_.IsInRssiRange()); | |
268 } | |
269 | |
270 TEST_F(ProximityAuthProximityMonitorImplTest, | |
271 ProximityState_CheckTxPower_RssiIndicatesProximity_TxPowerDoesNot) { | |
272 monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_TRANSMIT_POWER); | |
273 monitor_.Start(); | |
274 | |
275 ProvideConnectionInfo({0, 4, 4}); | |
276 | |
277 EXPECT_FALSE(monitor_.IsUnlockAllowed()); | |
278 EXPECT_TRUE(monitor_.IsInRssiRange()); | |
279 } | |
280 | |
281 TEST_F(ProximityAuthProximityMonitorImplTest, | |
282 ProximityState_CheckTxPower_TxPowerIndicatesProximity_RssiDoesNot) { | |
283 monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_TRANSMIT_POWER); | |
284 monitor_.Start(); | |
285 | |
286 ProvideConnectionInfo({-10, 0, 4}); | |
287 | |
288 EXPECT_TRUE(monitor_.IsUnlockAllowed()); | 192 EXPECT_TRUE(monitor_.IsUnlockAllowed()); |
289 EXPECT_FALSE(monitor_.IsInRssiRange()); | |
290 } | |
291 | |
292 TEST_F(ProximityAuthProximityMonitorImplTest, | |
293 ProximityState_CheckTxPower_NeitherRssiNorTxPowerIndicatesProximity) { | |
294 monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_TRANSMIT_POWER); | |
295 monitor_.Start(); | |
296 | |
297 ProvideConnectionInfo({-10, 4, 4}); | |
298 | |
299 EXPECT_FALSE(monitor_.IsUnlockAllowed()); | |
300 EXPECT_FALSE(monitor_.IsInRssiRange()); | |
301 } | |
302 | |
303 TEST_F(ProximityAuthProximityMonitorImplTest, | |
304 ProximityState_CheckTxPower_BothRssiAndTxPowerIndicateProximity) { | |
305 monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_TRANSMIT_POWER); | |
306 monitor_.Start(); | |
307 | |
308 ProvideConnectionInfo({0, 0, 4}); | |
309 | |
310 EXPECT_TRUE(monitor_.IsUnlockAllowed()); | |
311 EXPECT_TRUE(monitor_.IsInRssiRange()); | |
312 } | |
313 | |
314 TEST_F(ProximityAuthProximityMonitorImplTest, | |
315 ProximityState_CheckTxPower_UnknownRssi) { | |
316 monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_TRANSMIT_POWER); | |
317 monitor_.Start(); | |
318 | |
319 ProvideConnectionInfo({0, 0, 4}); | |
320 ProvideConnectionInfo({BluetoothDevice::kUnknownPower, 0, 4}); | |
321 | |
322 EXPECT_FALSE(monitor_.IsUnlockAllowed()); | |
323 EXPECT_FALSE(monitor_.IsInRssiRange()); | |
324 } | |
325 | |
326 TEST_F(ProximityAuthProximityMonitorImplTest, | |
327 ProximityState_CheckTxPower_UnknownTxPower) { | |
328 monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_TRANSMIT_POWER); | |
329 monitor_.Start(); | |
330 | |
331 ProvideConnectionInfo({0, 0, 4}); | |
332 ProvideConnectionInfo({0, BluetoothDevice::kUnknownPower, 4}); | |
333 | |
334 EXPECT_FALSE(monitor_.IsUnlockAllowed()); | |
335 EXPECT_FALSE(monitor_.IsInRssiRange()); | |
336 } | |
337 | |
338 TEST_F(ProximityAuthProximityMonitorImplTest, | |
339 ProximityState_CheckTxPower_UnknownMaxTxPower) { | |
340 monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_TRANSMIT_POWER); | |
341 monitor_.Start(); | |
342 | |
343 ProvideConnectionInfo({0, 0, 4}); | |
344 ProvideConnectionInfo({0, 0, BluetoothDevice::kUnknownPower}); | |
345 | |
346 EXPECT_FALSE(monitor_.IsUnlockAllowed()); | |
347 EXPECT_FALSE(monitor_.IsInRssiRange()); | |
348 } | |
349 | |
350 TEST_F(ProximityAuthProximityMonitorImplTest, ProximityState_StartThenStop) { | |
351 monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_RSSI); | |
352 | |
353 monitor_.Start(); | |
354 ProvideConnectionInfo({0, 0, 4}); | |
355 monitor_.Stop(); | |
356 | |
357 EXPECT_FALSE(monitor_.IsUnlockAllowed()); | |
358 EXPECT_FALSE(monitor_.IsInRssiRange()); | |
359 } | |
360 | |
361 TEST_F(ProximityAuthProximityMonitorImplTest, | |
362 ProximityState_StartThenStopThenStartAgain) { | |
363 monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_RSSI); | |
364 | |
365 monitor_.Start(); | |
366 ProvideConnectionInfo({-10, 4, 4}); | |
367 ProvideConnectionInfo({-10, 4, 4}); | |
368 ProvideConnectionInfo({-10, 4, 4}); | |
369 ProvideConnectionInfo({-10, 4, 4}); | |
370 ProvideConnectionInfo({-10, 4, 4}); | |
371 monitor_.Stop(); | 193 monitor_.Stop(); |
372 | 194 |
373 // Restarting the monitor should immediately reset the proximity state, rather | 195 // Restarting the monitor should immediately reset the proximity state, rather |
374 // than building on the previous rolling average. | 196 // than building on the previous rolling average. |
375 monitor_.Start(); | 197 monitor_.Start(); |
376 ProvideConnectionInfo({0, 4, 4}); | 198 ProvideConnectionInfo({kRssiThreshold - 1, 4, 4}); |
377 | 199 |
378 EXPECT_TRUE(monitor_.IsUnlockAllowed()); | 200 EXPECT_FALSE(monitor_.IsUnlockAllowed()); |
379 EXPECT_TRUE(monitor_.IsInRssiRange()); | |
380 } | 201 } |
381 | 202 |
382 TEST_F(ProximityAuthProximityMonitorImplTest, | 203 TEST_F(ProximityAuthProximityMonitorImplTest, |
383 ProximityState_RemoteDeviceRemainsInProximity) { | 204 IsUnlockAllowed_RemoteDeviceRemainsInProximity) { |
384 monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_RSSI); | |
385 | |
386 monitor_.Start(); | 205 monitor_.Start(); |
387 ProvideConnectionInfo({0, 4, 4}); | 206 ProvideConnectionInfo({kRssiThreshold / 2 + 1, 4, 4}); |
388 ProvideConnectionInfo({-1, 4, 4}); | 207 ProvideConnectionInfo({kRssiThreshold / 2 - 1, 4, 4}); |
389 ProvideConnectionInfo({0, 4, 4}); | 208 ProvideConnectionInfo({kRssiThreshold / 2 + 2, 4, 4}); |
390 ProvideConnectionInfo({-2, 4, 4}); | 209 ProvideConnectionInfo({kRssiThreshold / 2 - 3, 4, 4}); |
391 ProvideConnectionInfo({-1, 4, 4}); | |
392 | 210 |
393 EXPECT_TRUE(monitor_.IsUnlockAllowed()); | 211 EXPECT_TRUE(monitor_.IsUnlockAllowed()); |
394 EXPECT_TRUE(monitor_.IsInRssiRange()); | |
395 | 212 |
396 // Brief drops in RSSI should be handled by weighted averaging. | 213 // Brief drops in RSSI should be handled by weighted averaging. |
397 ProvideConnectionInfo({-10, 4, 4}); | 214 ProvideConnectionInfo({kRssiThreshold - 5, 4, 4}); |
398 | 215 |
399 EXPECT_TRUE(monitor_.IsUnlockAllowed()); | 216 EXPECT_TRUE(monitor_.IsUnlockAllowed()); |
400 EXPECT_TRUE(monitor_.IsInRssiRange()); | |
401 } | 217 } |
402 | 218 |
403 TEST_F(ProximityAuthProximityMonitorImplTest, | 219 TEST_F(ProximityAuthProximityMonitorImplTest, |
404 ProximityState_RemoteDeviceLeavesProximity) { | 220 IsUnlockAllowed_RemoteDeviceLeavesProximity) { |
405 monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_RSSI); | |
406 monitor_.Start(); | 221 monitor_.Start(); |
407 | 222 |
408 // Start with a device in proximity. | 223 // Start with a device in proximity. |
409 ProvideConnectionInfo({0, 4, 4}); | 224 ProvideConnectionInfo({0, 4, 4}); |
410 EXPECT_TRUE(monitor_.IsUnlockAllowed()); | 225 EXPECT_TRUE(monitor_.IsUnlockAllowed()); |
411 EXPECT_TRUE(monitor_.IsInRssiRange()); | |
412 | 226 |
413 // Simulate readings for the remote device leaving proximity. | 227 // Simulate readings for the remote device leaving proximity. |
414 ProvideConnectionInfo({-1, 4, 4}); | 228 ProvideConnectionInfo({-1, 4, 4}); |
415 ProvideConnectionInfo({-4, 4, 4}); | 229 ProvideConnectionInfo({-4, 4, 4}); |
416 ProvideConnectionInfo({0, 4, 4}); | 230 ProvideConnectionInfo({0, 4, 4}); |
417 ProvideConnectionInfo({-10, 4, 4}); | 231 ProvideConnectionInfo({-10, 4, 4}); |
418 ProvideConnectionInfo({-8, 4, 4}); | |
419 ProvideConnectionInfo({-15, 4, 4}); | 232 ProvideConnectionInfo({-15, 4, 4}); |
420 ProvideConnectionInfo({-10, 4, 4}); | 233 ProvideConnectionInfo({-20, 4, 4}); |
421 ProvideConnectionInfo({-10, 4, 4}); | 234 ProvideConnectionInfo({kRssiThreshold, 4, 4}); |
422 ProvideConnectionInfo({-10, 4, 4}); | 235 ProvideConnectionInfo({kRssiThreshold - 10, 4, 4}); |
423 ProvideConnectionInfo({-10, 4, 4}); | 236 ProvideConnectionInfo({kRssiThreshold - 20, 4, 4}); |
| 237 ProvideConnectionInfo({kRssiThreshold - 20, 4, 4}); |
424 | 238 |
425 EXPECT_FALSE(monitor_.IsUnlockAllowed()); | 239 EXPECT_FALSE(monitor_.IsUnlockAllowed()); |
426 EXPECT_FALSE(monitor_.IsInRssiRange()); | |
427 } | 240 } |
428 | 241 |
429 TEST_F(ProximityAuthProximityMonitorImplTest, | 242 TEST_F(ProximityAuthProximityMonitorImplTest, |
430 ProximityState_RemoteDeviceEntersProximity) { | 243 IsUnlockAllowed_RemoteDeviceEntersProximity) { |
431 monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_RSSI); | |
432 monitor_.Start(); | 244 monitor_.Start(); |
433 | 245 |
434 // Start with a device in proximity. | 246 // Start with a device out of proximity. |
435 ProvideConnectionInfo({-20, 4, 4}); | 247 ProvideConnectionInfo({2 * kRssiThreshold, 4, 4}); |
436 EXPECT_FALSE(monitor_.IsUnlockAllowed()); | 248 EXPECT_FALSE(monitor_.IsUnlockAllowed()); |
437 EXPECT_FALSE(monitor_.IsInRssiRange()); | |
438 | 249 |
439 // Simulate readings for the remote device entering proximity. | 250 // Simulate readings for the remote device entering proximity. |
440 ProvideConnectionInfo({-15, 4, 4}); | 251 ProvideConnectionInfo({-15, 4, 4}); |
441 ProvideConnectionInfo({-8, 4, 4}); | 252 ProvideConnectionInfo({-8, 4, 4}); |
442 ProvideConnectionInfo({-12, 4, 4}); | 253 ProvideConnectionInfo({-12, 4, 4}); |
443 ProvideConnectionInfo({-18, 4, 4}); | 254 ProvideConnectionInfo({-18, 4, 4}); |
444 ProvideConnectionInfo({-7, 4, 4}); | 255 ProvideConnectionInfo({-7, 4, 4}); |
445 ProvideConnectionInfo({-3, 4, 4}); | 256 ProvideConnectionInfo({-3, 4, 4}); |
446 ProvideConnectionInfo({-2, 4, 4}); | 257 ProvideConnectionInfo({-2, 4, 4}); |
447 ProvideConnectionInfo({0, 4, 4}); | 258 ProvideConnectionInfo({0, 4, 4}); |
448 ProvideConnectionInfo({0, 4, 4}); | 259 ProvideConnectionInfo({0, 4, 4}); |
449 | 260 |
450 EXPECT_TRUE(monitor_.IsUnlockAllowed()); | 261 EXPECT_TRUE(monitor_.IsUnlockAllowed()); |
451 EXPECT_TRUE(monitor_.IsInRssiRange()); | |
452 } | 262 } |
453 | 263 |
454 TEST_F(ProximityAuthProximityMonitorImplTest, | 264 TEST_F(ProximityAuthProximityMonitorImplTest, |
455 ProximityState_DeviceNotKnownToAdapter) { | 265 IsUnlockAllowed_DeviceNotKnownToAdapter) { |
456 monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_RSSI); | |
457 monitor_.Start(); | 266 monitor_.Start(); |
458 | 267 |
459 // Start with the device known to the adapter and in proximity. | 268 // Start with the device known to the adapter and in proximity. |
460 ProvideConnectionInfo({0, 4, 4}); | 269 ProvideConnectionInfo({0, 4, 4}); |
461 EXPECT_TRUE(monitor_.IsUnlockAllowed()); | 270 EXPECT_TRUE(monitor_.IsUnlockAllowed()); |
462 EXPECT_TRUE(monitor_.IsInRssiRange()); | |
463 | 271 |
464 // Simulate it being forgotten. | 272 // Simulate it being forgotten. |
465 ON_CALL(*bluetooth_adapter_, GetDevice(kBluetoothAddress)) | 273 ON_CALL(*bluetooth_adapter_, GetDevice(kBluetoothAddress)) |
466 .WillByDefault(Return(nullptr)); | 274 .WillByDefault(Return(nullptr)); |
467 EXPECT_CALL(observer_, OnProximityStateChanged()); | 275 EXPECT_CALL(observer_, OnProximityStateChanged()); |
468 RunPendingTasks(); | 276 RunPendingTasks(); |
469 | 277 |
470 EXPECT_FALSE(monitor_.IsUnlockAllowed()); | 278 EXPECT_FALSE(monitor_.IsUnlockAllowed()); |
471 EXPECT_FALSE(monitor_.IsInRssiRange()); | |
472 } | 279 } |
473 | 280 |
474 TEST_F(ProximityAuthProximityMonitorImplTest, | 281 TEST_F(ProximityAuthProximityMonitorImplTest, |
475 ProximityState_DeviceNotConnected) { | 282 IsUnlockAllowed_DeviceNotConnected) { |
476 monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_RSSI); | |
477 monitor_.Start(); | 283 monitor_.Start(); |
478 | 284 |
479 // Start with the device connected and in proximity. | 285 // Start with the device connected and in proximity. |
480 ProvideConnectionInfo({0, 4, 4}); | 286 ProvideConnectionInfo({0, 4, 4}); |
481 EXPECT_TRUE(monitor_.IsUnlockAllowed()); | 287 EXPECT_TRUE(monitor_.IsUnlockAllowed()); |
482 EXPECT_TRUE(monitor_.IsInRssiRange()); | |
483 | 288 |
484 // Simulate it disconnecting. | 289 // Simulate it disconnecting. |
485 ON_CALL(remote_bluetooth_device_, IsConnected()).WillByDefault(Return(false)); | 290 ON_CALL(remote_bluetooth_device_, IsConnected()).WillByDefault(Return(false)); |
486 EXPECT_CALL(observer_, OnProximityStateChanged()); | 291 EXPECT_CALL(observer_, OnProximityStateChanged()); |
487 RunPendingTasks(); | 292 RunPendingTasks(); |
488 | 293 |
489 EXPECT_FALSE(monitor_.IsUnlockAllowed()); | 294 EXPECT_FALSE(monitor_.IsUnlockAllowed()); |
490 EXPECT_FALSE(monitor_.IsInRssiRange()); | |
491 } | 295 } |
492 | 296 |
493 TEST_F(ProximityAuthProximityMonitorImplTest, | 297 TEST_F(ProximityAuthProximityMonitorImplTest, |
494 ProximityState_ConnectionInfoReceivedAfterStopping) { | 298 IsUnlockAllowed_ConnectionInfoReceivedAfterStopping) { |
495 monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_RSSI); | |
496 | |
497 monitor_.Start(); | 299 monitor_.Start(); |
498 monitor_.Stop(); | 300 monitor_.Stop(); |
499 ProvideConnectionInfo({0, 4, 4}); | 301 ProvideConnectionInfo({0, 4, 4}); |
500 | |
501 EXPECT_FALSE(monitor_.IsUnlockAllowed()); | 302 EXPECT_FALSE(monitor_.IsUnlockAllowed()); |
502 EXPECT_FALSE(monitor_.IsInRssiRange()); | |
503 } | 303 } |
504 | 304 |
505 TEST_F(ProximityAuthProximityMonitorImplTest, | 305 TEST_F(ProximityAuthProximityMonitorImplTest, |
506 RecordProximityMetricsOnAuthSuccess_NormalValues) { | 306 RecordProximityMetricsOnAuthSuccess_NormalValues) { |
507 monitor_.Start(); | 307 monitor_.Start(); |
508 ProvideConnectionInfo({0, 0, 4}); | 308 ProvideConnectionInfo({0, 0, 4}); |
509 | 309 |
510 clock_->Advance(base::TimeDelta::FromMilliseconds(101)); | 310 clock_->Advance(base::TimeDelta::FromMilliseconds(101)); |
511 ProvideConnectionInfo({-20, 3, 4}); | 311 ProvideConnectionInfo({-20, 3, 4}); |
512 | 312 |
513 clock_->Advance(base::TimeDelta::FromMilliseconds(203)); | 313 clock_->Advance(base::TimeDelta::FromMilliseconds(203)); |
514 base::HistogramTester histogram_tester; | 314 base::HistogramTester histogram_tester; |
515 monitor_.RecordProximityMetricsOnAuthSuccess(); | 315 monitor_.RecordProximityMetricsOnAuthSuccess(); |
516 histogram_tester.ExpectUniqueSample("EasyUnlock.AuthProximity.RollingRssi", | 316 histogram_tester.ExpectUniqueSample("EasyUnlock.AuthProximity.RollingRssi", |
517 -6, 1); | 317 -6, 1); |
518 histogram_tester.ExpectUniqueSample( | 318 histogram_tester.ExpectUniqueSample( |
519 "EasyUnlock.AuthProximity.TransmitPowerDelta", -1, 1); | |
520 histogram_tester.ExpectUniqueSample( | |
521 "EasyUnlock.AuthProximity.TimeSinceLastZeroRssi", 304, 1); | |
522 histogram_tester.ExpectUniqueSample( | |
523 "EasyUnlock.AuthProximity.RemoteDeviceModelHash", | 319 "EasyUnlock.AuthProximity.RemoteDeviceModelHash", |
524 1881443083 /* hash of "LGE Nexus 5" */, 1); | 320 1881443083 /* hash of "LGE Nexus 5" */, 1); |
525 } | 321 } |
526 | 322 |
527 TEST_F(ProximityAuthProximityMonitorImplTest, | 323 TEST_F(ProximityAuthProximityMonitorImplTest, |
528 RecordProximityMetricsOnAuthSuccess_ClampedValues) { | 324 RecordProximityMetricsOnAuthSuccess_ClampedValues) { |
529 monitor_.Start(); | 325 monitor_.Start(); |
530 ProvideConnectionInfo({-99999, 99999, 12345}); | 326 ProvideConnectionInfo({-99999, 99999, 12345}); |
531 | 327 |
532 base::HistogramTester histogram_tester; | 328 base::HistogramTester histogram_tester; |
533 monitor_.RecordProximityMetricsOnAuthSuccess(); | 329 monitor_.RecordProximityMetricsOnAuthSuccess(); |
534 histogram_tester.ExpectUniqueSample("EasyUnlock.AuthProximity.RollingRssi", | 330 histogram_tester.ExpectUniqueSample("EasyUnlock.AuthProximity.RollingRssi", |
535 -100, 1); | 331 -100, 1); |
536 histogram_tester.ExpectUniqueSample( | |
537 "EasyUnlock.AuthProximity.TransmitPowerDelta", 50, 1); | |
538 } | 332 } |
539 | 333 |
540 TEST_F(ProximityAuthProximityMonitorImplTest, | 334 TEST_F(ProximityAuthProximityMonitorImplTest, |
541 RecordProximityMetricsOnAuthSuccess_UnknownValues) { | 335 RecordProximityMetricsOnAuthSuccess_UnknownValues) { |
542 // Note: A device without a recorded name will have its Bluetooth address as | 336 // Note: A device without a recorded name will have "Unknown" as its name. |
543 // its name. | |
544 cryptauth::RemoteDevice unnamed_remote_device( | 337 cryptauth::RemoteDevice unnamed_remote_device( |
545 kRemoteDeviceUserId, kBluetoothAddress, kRemoteDevicePublicKey, | 338 kRemoteDeviceUserId, "" /* name */, kRemoteDevicePublicKey, |
546 kBluetoothAddress, kPersistentSymmetricKey, std::string()); | 339 kBluetoothAddress, kPersistentSymmetricKey, std::string()); |
| 340 cryptauth::FakeConnection connection(unnamed_remote_device); |
547 | 341 |
548 std::unique_ptr<base::TickClock> clock(new base::SimpleTestTickClock()); | 342 std::unique_ptr<base::TickClock> clock(new base::SimpleTestTickClock()); |
549 ProximityMonitorImpl monitor(unnamed_remote_device, std::move(clock)); | 343 ProximityMonitorImpl monitor(&connection, std::move(clock)); |
550 monitor.AddObserver(&observer_); | 344 monitor.AddObserver(&observer_); |
551 monitor.Start(); | 345 monitor.Start(); |
552 ProvideConnectionInfo({127, 127, 127}); | 346 ProvideConnectionInfo({127, 127, 127}); |
553 | 347 |
554 base::HistogramTester histogram_tester; | 348 base::HistogramTester histogram_tester; |
555 monitor.RecordProximityMetricsOnAuthSuccess(); | 349 monitor.RecordProximityMetricsOnAuthSuccess(); |
556 histogram_tester.ExpectUniqueSample("EasyUnlock.AuthProximity.RollingRssi", | 350 histogram_tester.ExpectUniqueSample("EasyUnlock.AuthProximity.RollingRssi", |
557 127, 1); | 351 127, 1); |
558 histogram_tester.ExpectUniqueSample( | 352 histogram_tester.ExpectUniqueSample( |
559 "EasyUnlock.AuthProximity.TransmitPowerDelta", 127, 1); | |
560 histogram_tester.ExpectUniqueSample( | |
561 "EasyUnlock.AuthProximity.TimeSinceLastZeroRssi", | |
562 base::TimeDelta::FromSeconds(10).InMilliseconds(), 1); | |
563 histogram_tester.ExpectUniqueSample( | |
564 "EasyUnlock.AuthProximity.RemoteDeviceModelHash", | 353 "EasyUnlock.AuthProximity.RemoteDeviceModelHash", |
565 -1808066424 /* hash of "Unknown" */, 1); | 354 -1808066424 /* hash of "Unknown" */, 1); |
566 } | 355 } |
567 | 356 |
568 } // namespace proximity_auth | 357 } // namespace proximity_auth |
OLD | NEW |