Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(136)

Side by Side Diff: components/proximity_auth/proximity_monitor_impl_unittest.cc

Issue 1941923002: bluetooth: Return int8_t and use -128 for unknown tx power. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@my-origin
Patch Set: Fix extensions tests Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/optional.h"
13 #include "base/test/histogram_tester.h" 14 #include "base/test/histogram_tester.h"
14 #include "base/test/simple_test_tick_clock.h" 15 #include "base/test/simple_test_tick_clock.h"
15 #include "base/test/test_simple_task_runner.h" 16 #include "base/test/test_simple_task_runner.h"
16 #include "base/thread_task_runner_handle.h" 17 #include "base/thread_task_runner_handle.h"
17 #include "base/time/time.h" 18 #include "base/time/time.h"
18 #include "components/proximity_auth/logging/logging.h" 19 #include "components/proximity_auth/logging/logging.h"
19 #include "components/proximity_auth/proximity_monitor_observer.h" 20 #include "components/proximity_auth/proximity_monitor_observer.h"
20 #include "components/proximity_auth/remote_device.h" 21 #include "components/proximity_auth/remote_device.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"
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after
229 EXPECT_TRUE(monitor_.IsUnlockAllowed()); 230 EXPECT_TRUE(monitor_.IsUnlockAllowed());
230 EXPECT_TRUE(monitor_.IsInRssiRange()); 231 EXPECT_TRUE(monitor_.IsInRssiRange());
231 } 232 }
232 233
233 TEST_F(ProximityAuthProximityMonitorImplTest, 234 TEST_F(ProximityAuthProximityMonitorImplTest,
234 ProximityState_CheckRssi_UnknownRssi) { 235 ProximityState_CheckRssi_UnknownRssi) {
235 monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_RSSI); 236 monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_RSSI);
236 monitor_.Start(); 237 monitor_.Start();
237 238
238 ProvideConnectionInfo({0, 0, 4}); 239 ProvideConnectionInfo({0, 0, 4});
239 ProvideConnectionInfo({BluetoothDevice::kUnknownPower, 0, 4}); 240 ProvideConnectionInfo({base::nullopt, 0, 4});
240 241
241 EXPECT_FALSE(monitor_.IsUnlockAllowed()); 242 EXPECT_FALSE(monitor_.IsUnlockAllowed());
242 EXPECT_FALSE(monitor_.IsInRssiRange()); 243 EXPECT_FALSE(monitor_.IsInRssiRange());
243 } 244 }
244 245
245 TEST_F(ProximityAuthProximityMonitorImplTest, 246 TEST_F(ProximityAuthProximityMonitorImplTest,
246 ProximityState_CheckRssi_UnknownTxPower) { 247 ProximityState_CheckRssi_UnknownTxPower) {
247 monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_RSSI); 248 monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_RSSI);
248 monitor_.Start(); 249 monitor_.Start();
249 250
250 ProvideConnectionInfo({0, 0, 4}); 251 ProvideConnectionInfo({0, 0, 4});
251 ProvideConnectionInfo({0, BluetoothDevice::kUnknownPower, 4}); 252 ProvideConnectionInfo({0, base::nullopt, 4});
252 253
253 EXPECT_FALSE(monitor_.IsUnlockAllowed()); 254 EXPECT_FALSE(monitor_.IsUnlockAllowed());
254 EXPECT_FALSE(monitor_.IsInRssiRange()); 255 EXPECT_FALSE(monitor_.IsInRssiRange());
255 } 256 }
256 257
257 TEST_F(ProximityAuthProximityMonitorImplTest, 258 TEST_F(ProximityAuthProximityMonitorImplTest,
258 ProximityState_CheckRssi_UnknownMaxTxPower) { 259 ProximityState_CheckRssi_UnknownMaxTxPower) {
259 monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_RSSI); 260 monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_RSSI);
260 monitor_.Start(); 261 monitor_.Start();
261 262
262 ProvideConnectionInfo({0, 0, 4}); 263 ProvideConnectionInfo({0, 0, 4});
263 ProvideConnectionInfo({0, 0, BluetoothDevice::kUnknownPower}); 264 ProvideConnectionInfo({0, 0, base::nullopt});
264 265
265 EXPECT_FALSE(monitor_.IsUnlockAllowed()); 266 EXPECT_FALSE(monitor_.IsUnlockAllowed());
266 EXPECT_FALSE(monitor_.IsInRssiRange()); 267 EXPECT_FALSE(monitor_.IsInRssiRange());
267 } 268 }
268 269
269 TEST_F(ProximityAuthProximityMonitorImplTest, 270 TEST_F(ProximityAuthProximityMonitorImplTest,
270 ProximityState_CheckTxPower_RssiIndicatesProximity_TxPowerDoesNot) { 271 ProximityState_CheckTxPower_RssiIndicatesProximity_TxPowerDoesNot) {
271 monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_TRANSMIT_POWER); 272 monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_TRANSMIT_POWER);
272 monitor_.Start(); 273 monitor_.Start();
273 274
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
309 EXPECT_TRUE(monitor_.IsUnlockAllowed()); 310 EXPECT_TRUE(monitor_.IsUnlockAllowed());
310 EXPECT_TRUE(monitor_.IsInRssiRange()); 311 EXPECT_TRUE(monitor_.IsInRssiRange());
311 } 312 }
312 313
313 TEST_F(ProximityAuthProximityMonitorImplTest, 314 TEST_F(ProximityAuthProximityMonitorImplTest,
314 ProximityState_CheckTxPower_UnknownRssi) { 315 ProximityState_CheckTxPower_UnknownRssi) {
315 monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_TRANSMIT_POWER); 316 monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_TRANSMIT_POWER);
316 monitor_.Start(); 317 monitor_.Start();
317 318
318 ProvideConnectionInfo({0, 0, 4}); 319 ProvideConnectionInfo({0, 0, 4});
319 ProvideConnectionInfo({BluetoothDevice::kUnknownPower, 0, 4}); 320 ProvideConnectionInfo({base::nullopt, 0, 4});
320 321
321 EXPECT_FALSE(monitor_.IsUnlockAllowed()); 322 EXPECT_FALSE(monitor_.IsUnlockAllowed());
322 EXPECT_FALSE(monitor_.IsInRssiRange()); 323 EXPECT_FALSE(monitor_.IsInRssiRange());
323 } 324 }
324 325
325 TEST_F(ProximityAuthProximityMonitorImplTest, 326 TEST_F(ProximityAuthProximityMonitorImplTest,
326 ProximityState_CheckTxPower_UnknownTxPower) { 327 ProximityState_CheckTxPower_UnknownTxPower) {
327 monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_TRANSMIT_POWER); 328 monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_TRANSMIT_POWER);
328 monitor_.Start(); 329 monitor_.Start();
329 330
330 ProvideConnectionInfo({0, 0, 4}); 331 ProvideConnectionInfo({0, 0, 4});
331 ProvideConnectionInfo({0, BluetoothDevice::kUnknownPower, 4}); 332 ProvideConnectionInfo({0, base::nullopt, 4});
332 333
333 EXPECT_FALSE(monitor_.IsUnlockAllowed()); 334 EXPECT_FALSE(monitor_.IsUnlockAllowed());
334 EXPECT_FALSE(monitor_.IsInRssiRange()); 335 EXPECT_FALSE(monitor_.IsInRssiRange());
335 } 336 }
336 337
337 TEST_F(ProximityAuthProximityMonitorImplTest, 338 TEST_F(ProximityAuthProximityMonitorImplTest,
338 ProximityState_CheckTxPower_UnknownMaxTxPower) { 339 ProximityState_CheckTxPower_UnknownMaxTxPower) {
339 monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_TRANSMIT_POWER); 340 monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_TRANSMIT_POWER);
340 monitor_.Start(); 341 monitor_.Start();
341 342
342 ProvideConnectionInfo({0, 0, 4}); 343 ProvideConnectionInfo({0, 0, 4});
343 ProvideConnectionInfo({0, 0, BluetoothDevice::kUnknownPower}); 344 ProvideConnectionInfo({0, 0, base::nullopt});
344 345
345 EXPECT_FALSE(monitor_.IsUnlockAllowed()); 346 EXPECT_FALSE(monitor_.IsUnlockAllowed());
346 EXPECT_FALSE(monitor_.IsInRssiRange()); 347 EXPECT_FALSE(monitor_.IsInRssiRange());
347 } 348 }
348 349
349 TEST_F(ProximityAuthProximityMonitorImplTest, ProximityState_StartThenStop) { 350 TEST_F(ProximityAuthProximityMonitorImplTest, ProximityState_StartThenStop) {
350 monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_RSSI); 351 monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_RSSI);
351 352
352 monitor_.Start(); 353 monitor_.Start();
353 ProvideConnectionInfo({0, 0, 4}); 354 ProvideConnectionInfo({0, 0, 4});
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
517 histogram_tester.ExpectUniqueSample( 518 histogram_tester.ExpectUniqueSample(
518 "EasyUnlock.AuthProximity.TransmitPowerDelta", -1, 1); 519 "EasyUnlock.AuthProximity.TransmitPowerDelta", -1, 1);
519 histogram_tester.ExpectUniqueSample( 520 histogram_tester.ExpectUniqueSample(
520 "EasyUnlock.AuthProximity.TimeSinceLastZeroRssi", 304, 1); 521 "EasyUnlock.AuthProximity.TimeSinceLastZeroRssi", 304, 1);
521 histogram_tester.ExpectUniqueSample( 522 histogram_tester.ExpectUniqueSample(
522 "EasyUnlock.AuthProximity.RemoteDeviceModelHash", 523 "EasyUnlock.AuthProximity.RemoteDeviceModelHash",
523 1881443083 /* hash of "LGE Nexus 5" */, 1); 524 1881443083 /* hash of "LGE Nexus 5" */, 1);
524 } 525 }
525 526
526 TEST_F(ProximityAuthProximityMonitorImplTest, 527 TEST_F(ProximityAuthProximityMonitorImplTest,
527 RecordProximityMetricsOnAuthSuccess_ClampedValues) { 528 RecordProximityMetricsOnAuthSuccess_ClampedValues_LowerThreshold) {
529 // Tests that we clamp values lower than -100 before histograming.
530
531 int8_t rssi = -128;
532 int8_t tx_power = -128;
533 int8_t max_tx_power = 0;
534 // The resulting TransmitPowerDelta that we try to record is -128.
535
528 monitor_.Start(); 536 monitor_.Start();
529 ProvideConnectionInfo({-99999, 99999, 12345}); 537 ProvideConnectionInfo({rssi, tx_power, max_tx_power});
530 538
531 base::HistogramTester histogram_tester; 539 base::HistogramTester histogram_tester;
532 monitor_.RecordProximityMetricsOnAuthSuccess(); 540 monitor_.RecordProximityMetricsOnAuthSuccess();
533 histogram_tester.ExpectUniqueSample("EasyUnlock.AuthProximity.RollingRssi", 541 histogram_tester.ExpectUniqueSample("EasyUnlock.AuthProximity.RollingRssi",
534 -100, 1); 542 -100, 1);
535 histogram_tester.ExpectUniqueSample( 543 histogram_tester.ExpectUniqueSample(
544 "EasyUnlock.AuthProximity.TransmitPowerDelta", -100, 1);
545 }
546
547 TEST_F(ProximityAuthProximityMonitorImplTest,
548 RecordProximityMetricsOnAuthSuccess_ClampedValues_UpperThreshold) {
549 // Tests that we clamp values larger than 50 before histograming.
550
551 int8_t rssi = 126;
552 int8_t tx_power = 126;
553 int8_t max_tx_power = 0;
554 // The resulting TransmitPowerDelta that we try to record is 126.
555
556 monitor_.Start();
557 ProvideConnectionInfo({rssi, tx_power, max_tx_power});
558
559 base::HistogramTester histogram_tester;
560 monitor_.RecordProximityMetricsOnAuthSuccess();
561 histogram_tester.ExpectUniqueSample("EasyUnlock.AuthProximity.RollingRssi",
562 50, 1);
563 histogram_tester.ExpectUniqueSample(
536 "EasyUnlock.AuthProximity.TransmitPowerDelta", 50, 1); 564 "EasyUnlock.AuthProximity.TransmitPowerDelta", 50, 1);
537 } 565 }
538 566
539 TEST_F(ProximityAuthProximityMonitorImplTest, 567 TEST_F(ProximityAuthProximityMonitorImplTest,
540 RecordProximityMetricsOnAuthSuccess_UnknownValues) { 568 RecordProximityMetricsOnAuthSuccess_UnknownValues) {
541 // Note: A device without a recorded name will have its Bluetooth address as 569 // Note: A device without a recorded name will have its Bluetooth address as
542 // its name. 570 // its name.
543 RemoteDevice unnamed_remote_device( 571 RemoteDevice unnamed_remote_device(
544 kRemoteDeviceUserId, kBluetoothAddress, kRemoteDevicePublicKey, 572 kRemoteDeviceUserId, kBluetoothAddress, kRemoteDevicePublicKey,
545 RemoteDevice::BLUETOOTH_CLASSIC, kBluetoothAddress, 573 RemoteDevice::BLUETOOTH_CLASSIC, kBluetoothAddress,
546 kPersistentSymmetricKey, std::string()); 574 kPersistentSymmetricKey, std::string());
547 575
548 std::unique_ptr<base::TickClock> clock(new base::SimpleTestTickClock()); 576 std::unique_ptr<base::TickClock> clock(new base::SimpleTestTickClock());
549 ProximityMonitorImpl monitor(unnamed_remote_device, std::move(clock)); 577 ProximityMonitorImpl monitor(unnamed_remote_device, std::move(clock));
550 monitor.AddObserver(&observer_); 578 monitor.AddObserver(&observer_);
551 monitor.Start(); 579 monitor.Start();
552 ProvideConnectionInfo({127, 127, 127}); 580 ProvideConnectionInfo({base::nullopt, base::nullopt, base::nullopt});
553 581
554 base::HistogramTester histogram_tester; 582 base::HistogramTester histogram_tester;
555 monitor.RecordProximityMetricsOnAuthSuccess(); 583 monitor.RecordProximityMetricsOnAuthSuccess();
556 histogram_tester.ExpectUniqueSample("EasyUnlock.AuthProximity.RollingRssi", 584 histogram_tester.ExpectUniqueSample("EasyUnlock.AuthProximity.RollingRssi",
557 127, 1); 585 127, 1);
558 histogram_tester.ExpectUniqueSample( 586 histogram_tester.ExpectUniqueSample(
559 "EasyUnlock.AuthProximity.TransmitPowerDelta", 127, 1); 587 "EasyUnlock.AuthProximity.TransmitPowerDelta", 127, 1);
560 histogram_tester.ExpectUniqueSample( 588 histogram_tester.ExpectUniqueSample(
561 "EasyUnlock.AuthProximity.TimeSinceLastZeroRssi", 589 "EasyUnlock.AuthProximity.TimeSinceLastZeroRssi",
562 base::TimeDelta::FromSeconds(10).InMilliseconds(), 1); 590 base::TimeDelta::FromSeconds(10).InMilliseconds(), 1);
563 histogram_tester.ExpectUniqueSample( 591 histogram_tester.ExpectUniqueSample(
564 "EasyUnlock.AuthProximity.RemoteDeviceModelHash", 592 "EasyUnlock.AuthProximity.RemoteDeviceModelHash",
565 -1808066424 /* hash of "Unknown" */, 1); 593 -1808066424 /* hash of "Unknown" */, 1);
566 } 594 }
567 595
568 } // namespace proximity_auth 596 } // namespace proximity_auth
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698