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/fake_connection.h" |
19 #include "components/cryptauth/remote_device.h" | 19 #include "components/cryptauth/remote_device.h" |
| 20 #include "components/prefs/testing_pref_service.h" |
20 #include "components/proximity_auth/logging/logging.h" | 21 #include "components/proximity_auth/logging/logging.h" |
| 22 #include "components/proximity_auth/proximity_auth_pref_manager.h" |
21 #include "components/proximity_auth/proximity_monitor_observer.h" | 23 #include "components/proximity_auth/proximity_monitor_observer.h" |
22 #include "device/bluetooth/bluetooth_adapter_factory.h" | 24 #include "device/bluetooth/bluetooth_adapter_factory.h" |
23 #include "device/bluetooth/test/mock_bluetooth_adapter.h" | 25 #include "device/bluetooth/test/mock_bluetooth_adapter.h" |
24 #include "testing/gmock/include/gmock/gmock.h" | 26 #include "testing/gmock/include/gmock/gmock.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
26 | 28 |
27 using device::BluetoothDevice; | 29 using device::BluetoothDevice; |
28 using testing::_; | 30 using testing::_; |
29 using testing::NiceMock; | 31 using testing::NiceMock; |
30 using testing::Return; | 32 using testing::Return; |
31 using testing::SaveArg; | 33 using testing::SaveArg; |
32 | 34 |
33 namespace proximity_auth { | 35 namespace proximity_auth { |
34 namespace { | 36 namespace { |
35 | 37 |
36 const char kRemoteDeviceUserId[] = "example@gmail.com"; | 38 const char kRemoteDeviceUserId[] = "example@gmail.com"; |
37 const char kRemoteDevicePublicKey[] = "Remote Public Key"; | 39 const char kRemoteDevicePublicKey[] = "Remote Public Key"; |
38 const char kRemoteDeviceName[] = "LGE Nexus 5"; | 40 const char kRemoteDeviceName[] = "LGE Nexus 5"; |
39 const char kBluetoothAddress[] = "AA:BB:CC:DD:EE:FF"; | 41 const char kBluetoothAddress[] = "AA:BB:CC:DD:EE:FF"; |
40 const char kPersistentSymmetricKey[] = "PSK"; | 42 const char kPersistentSymmetricKey[] = "PSK"; |
| 43 |
| 44 // The proximity threshold corresponds to a RSSI of -70. |
| 45 const ProximityAuthPrefManager::ProximityThreshold |
| 46 kProximityThresholdPrefValue = |
| 47 ProximityAuthPrefManager::ProximityThreshold::kFar; |
41 const int kRssiThreshold = -70; | 48 const int kRssiThreshold = -70; |
42 | 49 |
43 class MockProximityMonitorObserver : public ProximityMonitorObserver { | 50 class MockProximityMonitorObserver : public ProximityMonitorObserver { |
44 public: | 51 public: |
45 MockProximityMonitorObserver() {} | 52 MockProximityMonitorObserver() {} |
46 ~MockProximityMonitorObserver() override {} | 53 ~MockProximityMonitorObserver() override {} |
47 | 54 |
48 MOCK_METHOD0(OnProximityStateChanged, void()); | 55 MOCK_METHOD0(OnProximityStateChanged, void()); |
49 | 56 |
50 private: | 57 private: |
51 DISALLOW_COPY_AND_ASSIGN(MockProximityMonitorObserver); | 58 DISALLOW_COPY_AND_ASSIGN(MockProximityMonitorObserver); |
52 }; | 59 }; |
53 | 60 |
| 61 class MockProximityAuthPrefManager : public ProximityAuthPrefManager { |
| 62 public: |
| 63 MockProximityAuthPrefManager() : ProximityAuthPrefManager(nullptr) {} |
| 64 ~MockProximityAuthPrefManager() override {} |
| 65 |
| 66 MOCK_CONST_METHOD0(GetProximityThreshold, |
| 67 ProximityAuthPrefManager::ProximityThreshold(void)); |
| 68 |
| 69 private: |
| 70 DISALLOW_COPY_AND_ASSIGN(MockProximityAuthPrefManager); |
| 71 }; |
| 72 |
54 // Creates a mock Bluetooth adapter and sets it as the global adapter for | 73 // Creates a mock Bluetooth adapter and sets it as the global adapter for |
55 // testing. | 74 // testing. |
56 scoped_refptr<device::MockBluetoothAdapter> | 75 scoped_refptr<device::MockBluetoothAdapter> |
57 CreateAndRegisterMockBluetoothAdapter() { | 76 CreateAndRegisterMockBluetoothAdapter() { |
58 scoped_refptr<device::MockBluetoothAdapter> adapter = | 77 scoped_refptr<device::MockBluetoothAdapter> adapter = |
59 new NiceMock<device::MockBluetoothAdapter>(); | 78 new NiceMock<device::MockBluetoothAdapter>(); |
60 device::BluetoothAdapterFactory::SetAdapterForTesting(adapter); | 79 device::BluetoothAdapterFactory::SetAdapterForTesting(adapter); |
61 return adapter; | 80 return adapter; |
62 } | 81 } |
63 | 82 |
(...skipping 10 matching lines...) Expand all Loading... |
74 "", | 93 "", |
75 false /* paired */, | 94 false /* paired */, |
76 true /* connected */), | 95 true /* connected */), |
77 remote_device_(kRemoteDeviceUserId, | 96 remote_device_(kRemoteDeviceUserId, |
78 kRemoteDeviceName, | 97 kRemoteDeviceName, |
79 kRemoteDevicePublicKey, | 98 kRemoteDevicePublicKey, |
80 kBluetoothAddress, | 99 kBluetoothAddress, |
81 kPersistentSymmetricKey, | 100 kPersistentSymmetricKey, |
82 std::string()), | 101 std::string()), |
83 connection_(remote_device_), | 102 connection_(remote_device_), |
84 monitor_(&connection_, base::WrapUnique(clock_)), | 103 pref_manager_(new NiceMock<MockProximityAuthPrefManager>()), |
| 104 monitor_(&connection_, base::WrapUnique(clock_), pref_manager_.get()), |
85 task_runner_(new base::TestSimpleTaskRunner()), | 105 task_runner_(new base::TestSimpleTaskRunner()), |
86 thread_task_runner_handle_(task_runner_) { | 106 thread_task_runner_handle_(task_runner_) { |
87 ON_CALL(*bluetooth_adapter_, GetDevice(kBluetoothAddress)) | 107 ON_CALL(*bluetooth_adapter_, GetDevice(kBluetoothAddress)) |
88 .WillByDefault(Return(&remote_bluetooth_device_)); | 108 .WillByDefault(Return(&remote_bluetooth_device_)); |
89 ON_CALL(remote_bluetooth_device_, GetConnectionInfo(_)) | 109 ON_CALL(remote_bluetooth_device_, GetConnectionInfo(_)) |
90 .WillByDefault(SaveArg<0>(&connection_info_callback_)); | 110 .WillByDefault(SaveArg<0>(&connection_info_callback_)); |
91 monitor_.AddObserver(&observer_); | 111 monitor_.AddObserver(&observer_); |
| 112 ON_CALL(*pref_manager_, GetProximityThreshold()) |
| 113 .WillByDefault(Return(kProximityThresholdPrefValue)); |
92 } | 114 } |
93 | 115 |
94 ~ProximityAuthProximityMonitorImplTest() override {} | 116 ~ProximityAuthProximityMonitorImplTest() override {} |
95 | 117 |
96 void RunPendingTasks() { task_runner_->RunPendingTasks(); } | 118 void RunPendingTasks() { task_runner_->RunPendingTasks(); } |
97 | 119 |
98 void ProvideConnectionInfo( | 120 void ProvideConnectionInfo( |
99 const BluetoothDevice::ConnectionInfo& connection_info) { | 121 const BluetoothDevice::ConnectionInfo& connection_info) { |
100 RunPendingTasks(); | 122 RunPendingTasks(); |
101 connection_info_callback_.Run(connection_info); | 123 connection_info_callback_.Run(connection_info); |
102 | 124 |
103 // Reset the callback to ensure that tests correctly only respond at most | 125 // Reset the callback to ensure that tests correctly only respond at most |
104 // once per call to GetConnectionInfo(). | 126 // once per call to GetConnectionInfo(). |
105 connection_info_callback_ = BluetoothDevice::ConnectionInfoCallback(); | 127 connection_info_callback_ = BluetoothDevice::ConnectionInfoCallback(); |
106 } | 128 } |
107 | 129 |
108 protected: | 130 protected: |
109 // Mock for verifying interactions with the proximity monitor's observer. | 131 // Mock for verifying interactions with the proximity monitor's observer. |
110 NiceMock<MockProximityMonitorObserver> observer_; | 132 NiceMock<MockProximityMonitorObserver> observer_; |
111 | 133 |
112 // Clock used for verifying time calculations. Owned by the monitor_. | 134 // Clock used for verifying time calculations. Owned by the monitor_. |
113 base::SimpleTestTickClock* clock_; | 135 base::SimpleTestTickClock* clock_; |
114 | 136 |
115 // Mocks used for verifying interactions with the Bluetooth subsystem. | 137 // Mocks used for verifying interactions with the Bluetooth subsystem. |
116 scoped_refptr<device::MockBluetoothAdapter> bluetooth_adapter_; | 138 scoped_refptr<device::MockBluetoothAdapter> bluetooth_adapter_; |
117 NiceMock<device::MockBluetoothDevice> remote_bluetooth_device_; | 139 NiceMock<device::MockBluetoothDevice> remote_bluetooth_device_; |
118 cryptauth::RemoteDevice remote_device_; | 140 cryptauth::RemoteDevice remote_device_; |
119 cryptauth::FakeConnection connection_; | 141 cryptauth::FakeConnection connection_; |
120 | 142 |
| 143 // ProximityAuthPrefManager mock. |
| 144 std::unique_ptr<NiceMock<MockProximityAuthPrefManager>> pref_manager_; |
| 145 |
121 // The proximity monitor under test. | 146 // The proximity monitor under test. |
122 ProximityMonitorImpl monitor_; | 147 ProximityMonitorImpl monitor_; |
123 | 148 |
124 private: | 149 private: |
125 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 150 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
126 base::ThreadTaskRunnerHandle thread_task_runner_handle_; | 151 base::ThreadTaskRunnerHandle thread_task_runner_handle_; |
127 BluetoothDevice::ConnectionInfoCallback connection_info_callback_; | 152 BluetoothDevice::ConnectionInfoCallback connection_info_callback_; |
128 ScopedDisableLoggingForTesting disable_logging_; | 153 ScopedDisableLoggingForTesting disable_logging_; |
129 }; | 154 }; |
130 | 155 |
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
335 | 360 |
336 TEST_F(ProximityAuthProximityMonitorImplTest, | 361 TEST_F(ProximityAuthProximityMonitorImplTest, |
337 RecordProximityMetricsOnAuthSuccess_UnknownValues) { | 362 RecordProximityMetricsOnAuthSuccess_UnknownValues) { |
338 // Note: A device without a recorded name will have "Unknown" as its name. | 363 // Note: A device without a recorded name will have "Unknown" as its name. |
339 cryptauth::RemoteDevice unnamed_remote_device( | 364 cryptauth::RemoteDevice unnamed_remote_device( |
340 kRemoteDeviceUserId, "" /* name */, kRemoteDevicePublicKey, | 365 kRemoteDeviceUserId, "" /* name */, kRemoteDevicePublicKey, |
341 kBluetoothAddress, kPersistentSymmetricKey, std::string()); | 366 kBluetoothAddress, kPersistentSymmetricKey, std::string()); |
342 cryptauth::FakeConnection connection(unnamed_remote_device); | 367 cryptauth::FakeConnection connection(unnamed_remote_device); |
343 | 368 |
344 std::unique_ptr<base::TickClock> clock(new base::SimpleTestTickClock()); | 369 std::unique_ptr<base::TickClock> clock(new base::SimpleTestTickClock()); |
345 ProximityMonitorImpl monitor(&connection, std::move(clock)); | 370 ProximityMonitorImpl monitor(&connection, std::move(clock), |
| 371 pref_manager_.get()); |
346 monitor.AddObserver(&observer_); | 372 monitor.AddObserver(&observer_); |
347 monitor.Start(); | 373 monitor.Start(); |
348 ProvideConnectionInfo({127, 127, 127}); | 374 ProvideConnectionInfo({127, 127, 127}); |
349 | 375 |
350 base::HistogramTester histogram_tester; | 376 base::HistogramTester histogram_tester; |
351 monitor.RecordProximityMetricsOnAuthSuccess(); | 377 monitor.RecordProximityMetricsOnAuthSuccess(); |
352 histogram_tester.ExpectUniqueSample("EasyUnlock.AuthProximity.RollingRssi", | 378 histogram_tester.ExpectUniqueSample("EasyUnlock.AuthProximity.RollingRssi", |
353 127, 1); | 379 127, 1); |
354 histogram_tester.ExpectUniqueSample( | 380 histogram_tester.ExpectUniqueSample( |
355 "EasyUnlock.AuthProximity.RemoteDeviceModelHash", | 381 "EasyUnlock.AuthProximity.RemoteDeviceModelHash", |
356 -1808066424 /* hash of "Unknown" */, 1); | 382 -1808066424 /* hash of "Unknown" */, 1); |
357 } | 383 } |
358 | 384 |
359 } // namespace proximity_auth | 385 } // namespace proximity_auth |
OLD | NEW |