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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
// Copyright 2014 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/bluetooth_connection_finder.h"
#include "base/bind.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/time/time.h"
#include "components/proximity_auth/remote_device.h"
#include "components/proximity_auth/wire_message.h"
#include "device/bluetooth/bluetooth_adapter_factory.h"
#include "device/bluetooth/bluetooth_uuid.h"
#include "device/bluetooth/test/mock_bluetooth_adapter.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using testing::_;
using testing::NiceMock;
using testing::Return;
using testing::StrictMock;
namespace proximity_auth {
namespace {
const char kDeviceName[] = "Device name";
const char kBluetoothAddress[] = "11:22:33:44:55:66";
const RemoteDevice kRemoteDevice = {kDeviceName, kBluetoothAddress};
const char kUuid[] = "DEADBEEF-CAFE-FEED-FOOD-D15EA5EBEEF";
class MockConnection : public Connection {
public:
MockConnection() : Connection(kRemoteDevice) {}
virtual ~MockConnection() {}
MOCK_METHOD0(Connect, void());
using Connection::SetStatus;
private:
void Disconnect() {}
void SendMessageImpl(scoped_ptr<WireMessage> message) {}
DISALLOW_COPY_AND_ASSIGN(MockConnection);
};
class MockBluetoothConnectionFinder : public BluetoothConnectionFinder {
public:
MockBluetoothConnectionFinder()
: BluetoothConnectionFinder(kRemoteDevice,
device::BluetoothUUID(kUuid),
base::TimeDelta()) {}
virtual ~MockBluetoothConnectionFinder() {}
MOCK_METHOD0(CreateConnectionProxy, Connection*());
// Creates a mock connection and sets an expectation that the mock connection
// finder's CreateConnection() method will be called and will return the
// created connection. Returns a reference to the created connection.
// NOTE: The returned connection's lifetime is managed by the connection
// finder.
MockConnection* ExpectCreateConnection() {
scoped_ptr<MockConnection> connection(new NiceMock<MockConnection>());
MockConnection* connection_alias = connection.get();
EXPECT_CALL(*this, CreateConnectionProxy())
.WillOnce(Return(connection.release()));
return connection_alias;
}
using BluetoothConnectionFinder::AdapterPresentChanged;
using BluetoothConnectionFinder::AdapterPoweredChanged;
protected:
virtual scoped_ptr<Connection> CreateConnection() override {
return make_scoped_ptr(CreateConnectionProxy());
}
private:
DISALLOW_COPY_AND_ASSIGN(MockBluetoothConnectionFinder);
};
} // namespace
class ProximityAuthBluetoothConnectionFinderTest : public testing::Test {
protected:
ProximityAuthBluetoothConnectionFinderTest()
: adapter_(new NiceMock<device::MockBluetoothAdapter>),
connection_callback_(base::Bind(
&ProximityAuthBluetoothConnectionFinderTest::OnConnectionFound,
base::Unretained(this))) {
device::BluetoothAdapterFactory::SetAdapterForTesting(adapter_);
// By default, configure the environment to allow polling. Individual tests
// can override this as needed.
ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(true));
ON_CALL(*adapter_, IsPowered()).WillByDefault(Return(true));
}
MOCK_METHOD1(OnConnectionFoundProxy, void(Connection* connection));
void OnConnectionFound(scoped_ptr<Connection> connection) {
OnConnectionFoundProxy(connection.get());
last_found_connection_ = connection.Pass();
}
scoped_refptr<device::MockBluetoothAdapter> adapter_;
ConnectionFinder::ConnectionCallback connection_callback_;
private:
// Save a pointer to the last found connection, to extend its lifetime.
scoped_ptr<Connection> last_found_connection_;
base::MessageLoop message_loop_;
};
TEST_F(ProximityAuthBluetoothConnectionFinderTest,
ConstructAndDestroyDoesntCrash) {
// Destroying a BluetoothConnectionFinder for which Find() has not been called
// should not crash.
BluetoothConnectionFinder connection_finder(
kRemoteDevice,
device::BluetoothUUID(kUuid),
base::TimeDelta::FromMilliseconds(1));
}
TEST_F(ProximityAuthBluetoothConnectionFinderTest, Find_NoBluetoothAdapter) {
// Some platforms do not support Bluetooth. This test is only meaningful on
// those platforms.
adapter_ = NULL;
if (device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable())
return;
// The StrictMock will verify that no connection is created.
StrictMock<MockBluetoothConnectionFinder> connection_finder;
connection_finder.Find(connection_callback_);
}
TEST_F(ProximityAuthBluetoothConnectionFinderTest,
Find_BluetoothAdapterNotPresent) {
// The StrictMock will verify that no connection is created.
StrictMock<MockBluetoothConnectionFinder> connection_finder;
ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(false));
connection_finder.Find(connection_callback_);
}
TEST_F(ProximityAuthBluetoothConnectionFinderTest,
Find_BluetoothAdapterNotPowered) {
// The StrictMock will verify that no connection is created.
StrictMock<MockBluetoothConnectionFinder> connection_finder;
ON_CALL(*adapter_, IsPowered()).WillByDefault(Return(false));
connection_finder.Find(connection_callback_);
}
TEST_F(ProximityAuthBluetoothConnectionFinderTest, Find_ConnectionSucceeds) {
StrictMock<MockBluetoothConnectionFinder> connection_finder;
MockConnection* connection = connection_finder.ExpectCreateConnection();
connection_finder.Find(connection_callback_);
connection->SetStatus(Connection::IN_PROGRESS);
EXPECT_CALL(*this, OnConnectionFoundProxy(_));
connection->SetStatus(Connection::CONNECTED);
}
TEST_F(ProximityAuthBluetoothConnectionFinderTest,
Find_ConnectionSucceeds_UnregistersAsObserver) {
StrictMock<MockBluetoothConnectionFinder> connection_finder;
MockConnection* connection = connection_finder.ExpectCreateConnection();
connection_finder.Find(connection_callback_);
connection->SetStatus(Connection::IN_PROGRESS);
EXPECT_CALL(*this, OnConnectionFoundProxy(_));
EXPECT_CALL(*adapter_, RemoveObserver(&connection_finder));
connection->SetStatus(Connection::CONNECTED);
// If for some reason the connection sends more status updates, they should be
// ignored.
EXPECT_CALL(*this, OnConnectionFoundProxy(_)).Times(0);
connection->SetStatus(Connection::IN_PROGRESS);
connection->SetStatus(Connection::CONNECTED);
}
TEST_F(ProximityAuthBluetoothConnectionFinderTest,
Find_ConnectionFails_PostsTaskToPollAgain) {
StrictMock<MockBluetoothConnectionFinder> connection_finder;
MockConnection* connection = connection_finder.ExpectCreateConnection();
connection_finder.Find(connection_callback_);
// Simulate a connection that fails to connect.
connection->SetStatus(Connection::IN_PROGRESS);
connection->SetStatus(Connection::DISCONNECTED);
// A task should have been posted to poll again.
base::RunLoop run_loop;
connection_finder.ExpectCreateConnection();
run_loop.RunUntilIdle();
}
TEST_F(ProximityAuthBluetoothConnectionFinderTest, Find_PollsOnAdapterPresent) {
StrictMock<MockBluetoothConnectionFinder> connection_finder;
ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(false));
EXPECT_CALL(connection_finder, CreateConnectionProxy()).Times(0);
connection_finder.Find(connection_callback_);
ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(true));
connection_finder.ExpectCreateConnection();
connection_finder.AdapterPresentChanged(adapter_.get(), true);
}
TEST_F(ProximityAuthBluetoothConnectionFinderTest, Find_PollsOnAdapterPowered) {
StrictMock<MockBluetoothConnectionFinder> connection_finder;
ON_CALL(*adapter_, IsPowered()).WillByDefault(Return(false));
EXPECT_CALL(connection_finder, CreateConnectionProxy()).Times(0);
connection_finder.Find(connection_callback_);
ON_CALL(*adapter_, IsPowered()).WillByDefault(Return(true));
connection_finder.ExpectCreateConnection();
connection_finder.AdapterPoweredChanged(adapter_.get(), true);
}
TEST_F(ProximityAuthBluetoothConnectionFinderTest,
Find_DoesNotPollIfConnectionPending) {
StrictMock<MockBluetoothConnectionFinder> connection_finder;
MockConnection* connection = connection_finder.ExpectCreateConnection();
connection_finder.Find(connection_callback_);
connection->SetStatus(Connection::IN_PROGRESS);
// At this point, there is a pending connection in progress. Hence, an event
// that would normally trigger a new polling iteration should not do so now,
// because the delay interval between successive polling attempts has not yet
// expired.
EXPECT_CALL(connection_finder, CreateConnectionProxy()).Times(0);
connection_finder.AdapterPresentChanged(adapter_.get(), true);
}
TEST_F(ProximityAuthBluetoothConnectionFinderTest,
Find_ConnectionFails_PostsTaskToPollAgain_PollWaitsForTask) {
StrictMock<MockBluetoothConnectionFinder> connection_finder;
MockConnection* connection = connection_finder.ExpectCreateConnection();
connection_finder.Find(connection_callback_);
connection->SetStatus(Connection::IN_PROGRESS);
connection->SetStatus(Connection::DISCONNECTED);
// At this point, there is a pending poll scheduled. Hence, an event that
// would normally trigger a new polling iteration should not do so now,
// because the delay interval between successive polling attempts has not yet
// expired.
EXPECT_CALL(connection_finder, CreateConnectionProxy()).Times(0);
connection_finder.AdapterPresentChanged(adapter_.get(), true);
// Now, allow the pending task to run, but fail early, so that no new task is
// posted.
ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(false));
base::RunLoop run_loop;
run_loop.RunUntilIdle();
ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(true));
// Now that there is no pending task, events should once again trigger new
// polling iterations.
connection_finder.ExpectCreateConnection();
connection_finder.AdapterPresentChanged(adapter_.get(), true);
}
} // namespace proximity_auth
|