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 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "device/bluetooth/floss/bluetooth_socket_floss.h"
#include <memory>
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/task_environment.h"
#include "device/bluetooth/bluetooth_adapter.h"
#include "device/bluetooth/bluetooth_device.h"
#include "device/bluetooth/bluetooth_socket.h"
#include "device/bluetooth/bluetooth_socket_thread.h"
#include "device/bluetooth/floss/bluetooth_adapter_floss.h"
#include "device/bluetooth/floss/fake_floss_adapter_client.h"
#include "device/bluetooth/floss/fake_floss_manager_client.h"
#include "device/bluetooth/floss/fake_floss_socket_manager.h"
#include "device/bluetooth/floss/floss_dbus_client.h"
#include "device/bluetooth/floss/floss_dbus_manager.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
using ::device::BluetoothAdapter;
} // namespace
namespace floss {
class BluetoothSocketFlossTest : public testing::Test {
public:
void SetUp() override {
std::unique_ptr<floss::FlossDBusManagerSetter> dbus_setter =
floss::FlossDBusManager::GetSetterForTesting();
auto fake_floss_manager_client = std::make_unique<FakeFlossManagerClient>();
fake_floss_manager_client_ = fake_floss_manager_client.get();
dbus_setter->SetFlossManagerClient(std::move(fake_floss_manager_client));
InitializeAndEnableAdapter();
}
void TearDown() override {
adapter_ = nullptr;
device::BluetoothSocketThread::CleanupForTesting();
}
void InitializeAndEnableAdapter() {
adapter_ = BluetoothAdapterFloss::CreateAdapter();
fake_floss_manager_client_->SetDefaultEnabled(true);
base::RunLoop run_loop;
adapter_->Initialize(run_loop.QuitClosure());
run_loop.Run();
ASSERT_TRUE(adapter_);
ASSERT_TRUE(adapter_->IsInitialized());
ASSERT_TRUE(adapter_->IsPowered());
ASSERT_TRUE(adapter_.get() != nullptr);
fake_floss_manager_client_->NotifyObservers(
base::BindLambdaForTesting([](FlossManagerClient::Observer* observer) {
observer->AdapterEnabledChanged(/*adapter=*/0, /*enabled=*/true);
}));
base::RunLoop().RunUntilIdle();
// Get the socket thread.
device::BluetoothSocketThread::Get();
}
void AcceptSuccessCallback(base::OnceClosure exitloop,
const device::BluetoothDevice* device,
scoped_refptr<device::BluetoothSocket> socket) {
success_callback_count_++;
last_socket_ = std::move(socket);
std::move(exitloop).Run();
}
void ConnectToServiceSuccessCallback(
base::OnceClosure exitloop,
scoped_refptr<device::BluetoothSocket> socket) {
success_callback_count_++;
last_socket_ = std::move(socket);
std::move(exitloop).Run();
}
void CreateServiceSuccessCallback(
base::OnceClosure exitloop,
scoped_refptr<device::BluetoothSocket> socket) {
success_callback_count_++;
last_socket_ = std::move(socket);
std::move(exitloop).Run();
}
void DisconnectSuccessCallback(base::OnceClosure exitloop) {
std::move(exitloop).Run();
}
void ErrorCallback(base::OnceClosure exitloop, const std::string& message) {
LOG(ERROR) << "ErrorCallback: " << message;
error_callback_count_++;
last_message_ = message;
std::move(exitloop).Run();
}
void ImmediateSuccessCallback() { success_callback_count_++; }
void SendSuccessCallback(base::OnceClosure exitloop, int bytes_sent) {
++success_callback_count_;
last_bytes_sent_ = bytes_sent;
std::move(exitloop).Run();
}
// Clear counters for test.
void ClearCounters() {
last_bytes_sent_ = 0;
last_bytes_received_ = 0;
success_callback_count_ = 0;
error_callback_count_ = 0;
last_socket_ = nullptr;
}
void DisconnectSocket(device::BluetoothSocket* socket) {
base::RunLoop run_loop;
socket->Disconnect(base::BindOnce(
&BluetoothSocketFlossTest::DisconnectSuccessCallback,
weak_ptr_factory_.GetWeakPtr(), run_loop.QuitWhenIdleClosure()));
run_loop.Run();
}
FakeFlossSocketManager* GetFakeFlossSocketManager() {
return static_cast<FakeFlossSocketManager*>(
FlossDBusManager::Get()->GetSocketManager());
}
base::test::TaskEnvironment task_environment_;
scoped_refptr<BluetoothAdapter> adapter_;
std::string last_message_;
int last_bytes_sent_ = 0;
int last_bytes_received_ = 0;
int success_callback_count_ = 0;
int error_callback_count_ = 0;
scoped_refptr<device::BluetoothSocket> last_socket_;
// Holds pointer to FakeFloss*Client's so that we can manipulate the fake
// within tests.
raw_ptr<FakeFlossManagerClient> fake_floss_manager_client_;
base::WeakPtrFactory<BluetoothSocketFlossTest> weak_ptr_factory_{this};
};
TEST_F(BluetoothSocketFlossTest, Connect) {
device::BluetoothDevice* device =
adapter_->GetDevice(FakeFlossAdapterClient::kBondedAddress1);
ASSERT_TRUE(device != nullptr);
// First establish a connection.
{
base::RunLoop run_loop;
device->ConnectToService(
device::BluetoothUUID(FakeFlossSocketManager::kRfcommUuid),
base::BindOnce(
&BluetoothSocketFlossTest::ConnectToServiceSuccessCallback,
weak_ptr_factory_.GetWeakPtr(), run_loop.QuitWhenIdleClosure()),
base::BindOnce(&BluetoothSocketFlossTest::ErrorCallback,
weak_ptr_factory_.GetWeakPtr(),
run_loop.QuitWhenIdleClosure()));
run_loop.Run();
}
EXPECT_EQ(1, success_callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_TRUE(last_socket_.get() != nullptr);
// Take ownership of socket.
scoped_refptr<device::BluetoothSocket> socket = std::move(last_socket_);
ClearCounters();
auto write_buffer = base::MakeRefCounted<net::StringIOBuffer>("test");
{
base::RunLoop run_loop;
socket->Send(write_buffer.get(), write_buffer->size(),
base::BindOnce(&BluetoothSocketFlossTest::SendSuccessCallback,
weak_ptr_factory_.GetWeakPtr(),
run_loop.QuitWhenIdleClosure()),
base::BindOnce(&BluetoothSocketFlossTest::ErrorCallback,
weak_ptr_factory_.GetWeakPtr(),
run_loop.QuitWhenIdleClosure()));
run_loop.Run();
}
EXPECT_EQ(1, success_callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_EQ(last_bytes_sent_, write_buffer->size());
ClearCounters();
// Clean up the socket
DisconnectSocket(socket.get());
socket = nullptr;
}
// TODO (crbug.com/1412530) Test is failing on ASan bots
TEST_F(BluetoothSocketFlossTest, Listen) {
// Get socket id for next returned socket.
FlossSocketManager::SocketId id = GetFakeFlossSocketManager()->GetNextId();
// First create the service.
{
base::RunLoop run_loop;
adapter_->CreateRfcommService(
device::BluetoothUUID(FakeFlossSocketManager::kRfcommUuid),
BluetoothAdapter::ServiceOptions(),
base::BindOnce(&BluetoothSocketFlossTest::CreateServiceSuccessCallback,
weak_ptr_factory_.GetWeakPtr(),
run_loop.QuitWhenIdleClosure()),
base::BindOnce(&BluetoothSocketFlossTest::ErrorCallback,
weak_ptr_factory_.GetWeakPtr(),
run_loop.QuitWhenIdleClosure()));
// Mark the socket as ready. This should trigger the success callback and an
// accept.
GetFakeFlossSocketManager()->SendSocketReady(
id, device::BluetoothUUID(FakeFlossSocketManager::kRfcommUuid),
FlossDBusClient::BtifStatus::kSuccess);
run_loop.Run();
}
EXPECT_EQ(1, success_callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_TRUE(last_socket_.get() != nullptr);
// Take ownership of server socket.
scoped_refptr<device::BluetoothSocket> server_socket =
std::move(last_socket_);
ClearCounters();
// Simulate incoming connection. This queues one up to be accepted later.
FlossDeviceId device = {.address = FakeFlossAdapterClient::kBondedAddress1,
.name = "Foobar"};
GetFakeFlossSocketManager()->SendIncomingConnection(
id, device, device::BluetoothUUID(FakeFlossSocketManager::kRfcommUuid));
// Accept a connection and verify there is something there.
{
base::RunLoop run_loop;
server_socket->Accept(
base::BindOnce(&BluetoothSocketFlossTest::AcceptSuccessCallback,
weak_ptr_factory_.GetWeakPtr(),
run_loop.QuitWhenIdleClosure()),
base::BindOnce(&BluetoothSocketFlossTest::ErrorCallback,
weak_ptr_factory_.GetWeakPtr(),
run_loop.QuitWhenIdleClosure()));
run_loop.Run();
}
EXPECT_EQ(1, success_callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_TRUE(last_socket_.get() != nullptr);
// Take ownership of the client socket and close it.
scoped_refptr<device::BluetoothSocket> client_socket =
std::move(last_socket_);
ClearCounters();
DisconnectSocket(client_socket.get());
client_socket = nullptr;
ClearCounters();
// Accept a connection when there's nothing there and then receives connection
// failed.
{
base::RunLoop run_loop;
server_socket->Accept(
base::BindOnce(&BluetoothSocketFlossTest::AcceptSuccessCallback,
weak_ptr_factory_.GetWeakPtr(),
run_loop.QuitWhenIdleClosure()),
base::BindOnce(&BluetoothSocketFlossTest::ErrorCallback,
weak_ptr_factory_.GetWeakPtr(),
run_loop.QuitWhenIdleClosure()));
run_loop.RunUntilIdle();
// No sockets found to accept.
EXPECT_EQ(0, success_callback_count_);
EXPECT_EQ(0, error_callback_count_);
GetFakeFlossSocketManager()->SendSocketReady(
id, device::BluetoothUUID(FakeFlossSocketManager::kRfcommUuid),
FlossDBusClient::BtifStatus::kFail);
EXPECT_EQ(1, error_callback_count_);
EXPECT_EQ(0, success_callback_count_);
EXPECT_TRUE(last_socket_.get() == nullptr);
ClearCounters();
}
// Accept a connection when there's nothing there and then send connection.
{
// First runloop will push the accept callbacks into socket.
// Second runloop will actually trigger when incoming connection occurs.
base::RunLoop outer_loop;
base::RunLoop inner_loop;
server_socket->Accept(
base::BindOnce(&BluetoothSocketFlossTest::AcceptSuccessCallback,
weak_ptr_factory_.GetWeakPtr(),
inner_loop.QuitWhenIdleClosure()),
base::BindOnce(&BluetoothSocketFlossTest::ErrorCallback,
weak_ptr_factory_.GetWeakPtr(),
inner_loop.QuitWhenIdleClosure()));
outer_loop.RunUntilIdle();
// No sockets found to accept.
EXPECT_EQ(0, success_callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_TRUE(last_socket_.get() == nullptr);
GetFakeFlossSocketManager()->SendIncomingConnection(
id, device, device::BluetoothUUID(FakeFlossSocketManager::kRfcommUuid));
inner_loop.Run();
EXPECT_EQ(0, error_callback_count_);
EXPECT_EQ(1, success_callback_count_);
EXPECT_TRUE(last_socket_.get() != nullptr);
// Disconnect last connecting socket
client_socket = std::move(last_socket_);
DisconnectSocket(client_socket.get());
client_socket = nullptr;
last_socket_ = nullptr;
}
// Try to accept twice and make sure the second one fails.
ClearCounters();
{
base::RunLoop run_loop;
server_socket->Accept(
base::BindOnce(&BluetoothSocketFlossTest::AcceptSuccessCallback,
weak_ptr_factory_.GetWeakPtr(),
run_loop.QuitWhenIdleClosure()),
base::BindOnce(&BluetoothSocketFlossTest::ErrorCallback,
weak_ptr_factory_.GetWeakPtr(),
run_loop.QuitWhenIdleClosure()));
run_loop.RunUntilIdle();
// No change after first one
EXPECT_EQ(0, error_callback_count_);
server_socket->Accept(
base::BindOnce(&BluetoothSocketFlossTest::AcceptSuccessCallback,
weak_ptr_factory_.GetWeakPtr(),
run_loop.QuitWhenIdleClosure()),
base::BindOnce(&BluetoothSocketFlossTest::ErrorCallback,
weak_ptr_factory_.GetWeakPtr(),
run_loop.QuitWhenIdleClosure()));
run_loop.RunUntilIdle();
// Second one should fail
EXPECT_EQ(1, error_callback_count_);
ClearCounters();
}
// Clean up server socket at end.
DisconnectSocket(server_socket.get());
}
} // namespace floss
|