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