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