| 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/throttled_bluetooth_connection_finder.h" | 5 #include "components/proximity_auth/throttled_bluetooth_connection_finder.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 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/test/test_simple_task_runner.h" | 12 #include "base/test/test_simple_task_runner.h" |
| 13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 14 #include "components/cryptauth/bluetooth_throttler.h" |
| 15 #include "components/cryptauth/fake_connection.h" |
| 14 #include "components/cryptauth/remote_device.h" | 16 #include "components/cryptauth/remote_device.h" |
| 17 #include "components/cryptauth/wire_message.h" |
| 15 #include "components/proximity_auth/bluetooth_connection_finder.h" | 18 #include "components/proximity_auth/bluetooth_connection_finder.h" |
| 16 #include "components/proximity_auth/bluetooth_throttler.h" | |
| 17 #include "components/proximity_auth/fake_connection.h" | |
| 18 #include "components/proximity_auth/wire_message.h" | |
| 19 #include "device/bluetooth/bluetooth_uuid.h" | 19 #include "device/bluetooth/bluetooth_uuid.h" |
| 20 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 22 | 22 |
| 23 using testing::NiceMock; | 23 using testing::NiceMock; |
| 24 using testing::Return; | 24 using testing::Return; |
| 25 using testing::_; | 25 using testing::_; |
| 26 | 26 |
| 27 namespace proximity_auth { | 27 namespace proximity_auth { |
| 28 namespace { | 28 namespace { |
| 29 | 29 |
| 30 const int kPollingIntervalSeconds = 7; | 30 const int kPollingIntervalSeconds = 7; |
| 31 const char kUuid[] = "DEADBEEF-CAFE-FEED-FOOD-D15EA5EBEEF"; | 31 const char kUuid[] = "DEADBEEF-CAFE-FEED-FOOD-D15EA5EBEEF"; |
| 32 | 32 |
| 33 // A callback that stores a found |connection| into |out|. | 33 // A callback that stores a found |connection| into |out|. |
| 34 void SaveConnection(std::unique_ptr<Connection>* out, | 34 void SaveConnection(std::unique_ptr<cryptauth::Connection>* out, |
| 35 std::unique_ptr<Connection> connection) { | 35 std::unique_ptr<cryptauth::Connection> connection) { |
| 36 *out = std::move(connection); | 36 *out = std::move(connection); |
| 37 } | 37 } |
| 38 | 38 |
| 39 class MockBluetoothThrottler : public BluetoothThrottler { | 39 class MockBluetoothThrottler : public cryptauth::BluetoothThrottler { |
| 40 public: | 40 public: |
| 41 MockBluetoothThrottler() {} | 41 MockBluetoothThrottler() {} |
| 42 ~MockBluetoothThrottler() override {} | 42 ~MockBluetoothThrottler() override {} |
| 43 | 43 |
| 44 MOCK_CONST_METHOD0(GetDelay, base::TimeDelta()); | 44 MOCK_CONST_METHOD0(GetDelay, base::TimeDelta()); |
| 45 MOCK_METHOD1(OnConnection, void(Connection* connection)); | 45 MOCK_METHOD1(OnConnection, void(cryptauth::Connection* connection)); |
| 46 | 46 |
| 47 private: | 47 private: |
| 48 DISALLOW_COPY_AND_ASSIGN(MockBluetoothThrottler); | 48 DISALLOW_COPY_AND_ASSIGN(MockBluetoothThrottler); |
| 49 }; | 49 }; |
| 50 | 50 |
| 51 class FakeBluetoothConnectionFinder : public BluetoothConnectionFinder { | 51 class FakeBluetoothConnectionFinder : public BluetoothConnectionFinder { |
| 52 public: | 52 public: |
| 53 FakeBluetoothConnectionFinder() | 53 FakeBluetoothConnectionFinder() |
| 54 : BluetoothConnectionFinder( | 54 : BluetoothConnectionFinder( |
| 55 cryptauth::RemoteDevice(), | 55 cryptauth::RemoteDevice(), |
| 56 device::BluetoothUUID(kUuid), | 56 device::BluetoothUUID(kUuid), |
| 57 base::TimeDelta::FromSeconds(kPollingIntervalSeconds)) {} | 57 base::TimeDelta::FromSeconds(kPollingIntervalSeconds)) {} |
| 58 ~FakeBluetoothConnectionFinder() override {} | 58 ~FakeBluetoothConnectionFinder() override {} |
| 59 | 59 |
| 60 void Find(const ConnectionCallback& connection_callback) override { | 60 void Find(const cryptauth::ConnectionFinder::ConnectionCallback& |
| 61 connection_callback) override { |
| 61 connection_callback.Run( | 62 connection_callback.Run( |
| 62 base::MakeUnique<FakeConnection>(cryptauth::RemoteDevice())); | 63 base::MakeUnique<cryptauth::FakeConnection>(cryptauth::RemoteDevice())); |
| 63 } | 64 } |
| 64 | 65 |
| 65 private: | 66 private: |
| 66 DISALLOW_COPY_AND_ASSIGN(FakeBluetoothConnectionFinder); | 67 DISALLOW_COPY_AND_ASSIGN(FakeBluetoothConnectionFinder); |
| 67 }; | 68 }; |
| 68 | 69 |
| 69 } // namespace | 70 } // namespace |
| 70 | 71 |
| 71 class ProximityAuthThrottledBluetoothConnectionFinderTest | 72 class ProximityAuthThrottledBluetoothConnectionFinderTest |
| 72 : public testing::Test { | 73 : public testing::Test { |
| 73 public: | 74 public: |
| 74 ProximityAuthThrottledBluetoothConnectionFinderTest() | 75 ProximityAuthThrottledBluetoothConnectionFinderTest() |
| 75 : task_runner_(new base::TestSimpleTaskRunner) {} | 76 : task_runner_(new base::TestSimpleTaskRunner) {} |
| 76 | 77 |
| 77 protected: | 78 protected: |
| 78 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 79 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 79 NiceMock<MockBluetoothThrottler> throttler_; | 80 NiceMock<MockBluetoothThrottler> throttler_; |
| 80 }; | 81 }; |
| 81 | 82 |
| 82 TEST_F(ProximityAuthThrottledBluetoothConnectionFinderTest, | 83 TEST_F(ProximityAuthThrottledBluetoothConnectionFinderTest, |
| 83 Find_ExecutesImmediatelyWhenUnthrottled) { | 84 Find_ExecutesImmediatelyWhenUnthrottled) { |
| 84 ON_CALL(throttler_, GetDelay()).WillByDefault(Return(base::TimeDelta())); | 85 ON_CALL(throttler_, GetDelay()).WillByDefault(Return(base::TimeDelta())); |
| 85 | 86 |
| 86 ThrottledBluetoothConnectionFinder connection_finder( | 87 ThrottledBluetoothConnectionFinder connection_finder( |
| 87 base::WrapUnique(new FakeBluetoothConnectionFinder), task_runner_, | 88 base::WrapUnique(new FakeBluetoothConnectionFinder), task_runner_, |
| 88 &throttler_); | 89 &throttler_); |
| 89 std::unique_ptr<Connection> connection; | 90 std::unique_ptr<cryptauth::Connection> connection; |
| 90 connection_finder.Find(base::Bind(&SaveConnection, &connection)); | 91 connection_finder.Find(base::Bind(&SaveConnection, &connection)); |
| 91 EXPECT_TRUE(connection); | 92 EXPECT_TRUE(connection); |
| 92 } | 93 } |
| 93 | 94 |
| 94 TEST_F(ProximityAuthThrottledBluetoothConnectionFinderTest, | 95 TEST_F(ProximityAuthThrottledBluetoothConnectionFinderTest, |
| 95 Find_ExecutesAfterADelayWhenThrottled) { | 96 Find_ExecutesAfterADelayWhenThrottled) { |
| 96 ON_CALL(throttler_, GetDelay()) | 97 ON_CALL(throttler_, GetDelay()) |
| 97 .WillByDefault(Return(base::TimeDelta::FromSeconds(1))); | 98 .WillByDefault(Return(base::TimeDelta::FromSeconds(1))); |
| 98 | 99 |
| 99 ThrottledBluetoothConnectionFinder connection_finder( | 100 ThrottledBluetoothConnectionFinder connection_finder( |
| 100 base::WrapUnique(new FakeBluetoothConnectionFinder), task_runner_, | 101 base::WrapUnique(new FakeBluetoothConnectionFinder), task_runner_, |
| 101 &throttler_); | 102 &throttler_); |
| 102 std::unique_ptr<Connection> connection; | 103 std::unique_ptr<cryptauth::Connection> connection; |
| 103 connection_finder.Find(base::Bind(&SaveConnection, &connection)); | 104 connection_finder.Find(base::Bind(&SaveConnection, &connection)); |
| 104 EXPECT_FALSE(connection); | 105 EXPECT_FALSE(connection); |
| 105 | 106 |
| 106 // The connection should be found once the throttling period has elapsed. | 107 // The connection should be found once the throttling period has elapsed. |
| 107 ON_CALL(throttler_, GetDelay()).WillByDefault(Return(base::TimeDelta())); | 108 ON_CALL(throttler_, GetDelay()).WillByDefault(Return(base::TimeDelta())); |
| 108 task_runner_->RunUntilIdle(); | 109 task_runner_->RunUntilIdle(); |
| 109 EXPECT_TRUE(connection); | 110 EXPECT_TRUE(connection); |
| 110 } | 111 } |
| 111 | 112 |
| 112 TEST_F(ProximityAuthThrottledBluetoothConnectionFinderTest, | 113 TEST_F(ProximityAuthThrottledBluetoothConnectionFinderTest, |
| 113 OnConnection_ForwardsNotificationToThrottler) { | 114 OnConnection_ForwardsNotificationToThrottler) { |
| 114 ON_CALL(throttler_, GetDelay()).WillByDefault(Return(base::TimeDelta())); | 115 ON_CALL(throttler_, GetDelay()).WillByDefault(Return(base::TimeDelta())); |
| 115 | 116 |
| 116 ThrottledBluetoothConnectionFinder connection_finder( | 117 ThrottledBluetoothConnectionFinder connection_finder( |
| 117 base::WrapUnique(new FakeBluetoothConnectionFinder), task_runner_, | 118 base::WrapUnique(new FakeBluetoothConnectionFinder), task_runner_, |
| 118 &throttler_); | 119 &throttler_); |
| 119 std::unique_ptr<Connection> connection; | 120 std::unique_ptr<cryptauth::Connection> connection; |
| 120 EXPECT_CALL(throttler_, OnConnection(_)); | 121 EXPECT_CALL(throttler_, OnConnection(_)); |
| 121 connection_finder.Find(base::Bind(&SaveConnection, &connection)); | 122 connection_finder.Find(base::Bind(&SaveConnection, &connection)); |
| 122 EXPECT_TRUE(connection); | 123 EXPECT_TRUE(connection); |
| 123 } | 124 } |
| 124 | 125 |
| 125 } // namespace proximity_auth | 126 } // namespace proximity_auth |
| OLD | NEW |