| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "device/bluetooth/bluetooth_socket_bluez.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/memory/ref_counted.h" | |
| 11 #include "base/message_loop/message_loop.h" | |
| 12 #include "device/bluetooth/bluetooth_adapter.h" | |
| 13 #include "device/bluetooth/bluetooth_adapter_bluez.h" | |
| 14 #include "device/bluetooth/bluetooth_adapter_factory.h" | |
| 15 #include "device/bluetooth/bluetooth_device.h" | |
| 16 #include "device/bluetooth/bluetooth_device_bluez.h" | |
| 17 #include "device/bluetooth/bluetooth_socket.h" | |
| 18 #include "device/bluetooth/bluetooth_socket_thread.h" | |
| 19 #include "device/bluetooth/bluetooth_uuid.h" | |
| 20 #include "device/bluetooth/dbus/bluez_dbus_manager.h" | |
| 21 #include "device/bluetooth/dbus/fake_bluetooth_adapter_client.h" | |
| 22 #include "device/bluetooth/dbus/fake_bluetooth_agent_manager_client.h" | |
| 23 #include "device/bluetooth/dbus/fake_bluetooth_device_client.h" | |
| 24 #include "device/bluetooth/dbus/fake_bluetooth_gatt_service_client.h" | |
| 25 #include "device/bluetooth/dbus/fake_bluetooth_input_client.h" | |
| 26 #include "device/bluetooth/dbus/fake_bluetooth_profile_manager_client.h" | |
| 27 #include "device/bluetooth/dbus/fake_bluetooth_profile_service_provider.h" | |
| 28 #include "net/base/io_buffer.h" | |
| 29 #include "net/base/net_errors.h" | |
| 30 #include "testing/gtest/include/gtest/gtest.h" | |
| 31 | |
| 32 using device::BluetoothAdapter; | |
| 33 using device::BluetoothDevice; | |
| 34 using device::BluetoothSocket; | |
| 35 using device::BluetoothSocketThread; | |
| 36 using device::BluetoothUUID; | |
| 37 | |
| 38 namespace { | |
| 39 | |
| 40 void DoNothingDBusErrorCallback(const std::string& error_name, | |
| 41 const std::string& error_message) {} | |
| 42 | |
| 43 } // namespace | |
| 44 | |
| 45 namespace bluez { | |
| 46 | |
| 47 class BluetoothSocketBlueZTest : public testing::Test { | |
| 48 public: | |
| 49 BluetoothSocketBlueZTest() | |
| 50 : success_callback_count_(0), | |
| 51 error_callback_count_(0), | |
| 52 last_bytes_sent_(0), | |
| 53 last_bytes_received_(0), | |
| 54 last_reason_(BluetoothSocket::kSystemError) {} | |
| 55 | |
| 56 void SetUp() override { | |
| 57 std::unique_ptr<bluez::BluezDBusManagerSetter> dbus_setter = | |
| 58 bluez::BluezDBusManager::GetSetterForTesting(); | |
| 59 | |
| 60 dbus_setter->SetBluetoothAdapterClient( | |
| 61 std::unique_ptr<bluez::BluetoothAdapterClient>( | |
| 62 new bluez::FakeBluetoothAdapterClient)); | |
| 63 dbus_setter->SetBluetoothAgentManagerClient( | |
| 64 std::unique_ptr<bluez::BluetoothAgentManagerClient>( | |
| 65 new bluez::FakeBluetoothAgentManagerClient)); | |
| 66 dbus_setter->SetBluetoothDeviceClient( | |
| 67 std::unique_ptr<bluez::BluetoothDeviceClient>( | |
| 68 new bluez::FakeBluetoothDeviceClient)); | |
| 69 dbus_setter->SetBluetoothGattServiceClient( | |
| 70 std::unique_ptr<bluez::BluetoothGattServiceClient>( | |
| 71 new bluez::FakeBluetoothGattServiceClient)); | |
| 72 dbus_setter->SetBluetoothInputClient( | |
| 73 std::unique_ptr<bluez::BluetoothInputClient>( | |
| 74 new bluez::FakeBluetoothInputClient)); | |
| 75 dbus_setter->SetBluetoothProfileManagerClient( | |
| 76 std::unique_ptr<bluez::BluetoothProfileManagerClient>( | |
| 77 new bluez::FakeBluetoothProfileManagerClient)); | |
| 78 | |
| 79 BluetoothSocketThread::Get(); | |
| 80 | |
| 81 // Grab a pointer to the adapter. | |
| 82 device::BluetoothAdapterFactory::GetAdapter(base::Bind( | |
| 83 &BluetoothSocketBlueZTest::AdapterCallback, base::Unretained(this))); | |
| 84 | |
| 85 base::MessageLoop::current()->Run(); | |
| 86 | |
| 87 ASSERT_TRUE(adapter_.get() != nullptr); | |
| 88 ASSERT_TRUE(adapter_->IsInitialized()); | |
| 89 ASSERT_TRUE(adapter_->IsPresent()); | |
| 90 | |
| 91 // Turn on the adapter. | |
| 92 adapter_->SetPowered(true, base::Bind(&base::DoNothing), | |
| 93 base::Bind(&base::DoNothing)); | |
| 94 ASSERT_TRUE(adapter_->IsPowered()); | |
| 95 } | |
| 96 | |
| 97 void TearDown() override { | |
| 98 adapter_ = nullptr; | |
| 99 BluetoothSocketThread::CleanupForTesting(); | |
| 100 bluez::BluezDBusManager::Shutdown(); | |
| 101 } | |
| 102 | |
| 103 void AdapterCallback(scoped_refptr<BluetoothAdapter> adapter) { | |
| 104 adapter_ = adapter; | |
| 105 if (base::MessageLoop::current() && | |
| 106 base::MessageLoop::current()->is_running()) { | |
| 107 base::MessageLoop::current()->QuitWhenIdle(); | |
| 108 } | |
| 109 } | |
| 110 | |
| 111 void SuccessCallback() { | |
| 112 ++success_callback_count_; | |
| 113 message_loop_.QuitWhenIdle(); | |
| 114 } | |
| 115 | |
| 116 void ErrorCallback(const std::string& message) { | |
| 117 ++error_callback_count_; | |
| 118 last_message_ = message; | |
| 119 | |
| 120 if (message_loop_.is_running()) | |
| 121 message_loop_.QuitWhenIdle(); | |
| 122 } | |
| 123 | |
| 124 void ConnectToServiceSuccessCallback(scoped_refptr<BluetoothSocket> socket) { | |
| 125 ++success_callback_count_; | |
| 126 last_socket_ = socket; | |
| 127 | |
| 128 message_loop_.QuitWhenIdle(); | |
| 129 } | |
| 130 | |
| 131 void SendSuccessCallback(int bytes_sent) { | |
| 132 ++success_callback_count_; | |
| 133 last_bytes_sent_ = bytes_sent; | |
| 134 | |
| 135 message_loop_.QuitWhenIdle(); | |
| 136 } | |
| 137 | |
| 138 void ReceiveSuccessCallback(int bytes_received, | |
| 139 scoped_refptr<net::IOBuffer> io_buffer) { | |
| 140 ++success_callback_count_; | |
| 141 last_bytes_received_ = bytes_received; | |
| 142 last_io_buffer_ = io_buffer; | |
| 143 | |
| 144 message_loop_.QuitWhenIdle(); | |
| 145 } | |
| 146 | |
| 147 void ReceiveErrorCallback(BluetoothSocket::ErrorReason reason, | |
| 148 const std::string& error_message) { | |
| 149 ++error_callback_count_; | |
| 150 last_reason_ = reason; | |
| 151 last_message_ = error_message; | |
| 152 | |
| 153 message_loop_.QuitWhenIdle(); | |
| 154 } | |
| 155 | |
| 156 void CreateServiceSuccessCallback(scoped_refptr<BluetoothSocket> socket) { | |
| 157 ++success_callback_count_; | |
| 158 last_socket_ = socket; | |
| 159 | |
| 160 if (message_loop_.is_running()) | |
| 161 message_loop_.QuitWhenIdle(); | |
| 162 } | |
| 163 | |
| 164 void AcceptSuccessCallback(const BluetoothDevice* device, | |
| 165 scoped_refptr<BluetoothSocket> socket) { | |
| 166 ++success_callback_count_; | |
| 167 last_device_ = device; | |
| 168 last_socket_ = socket; | |
| 169 | |
| 170 message_loop_.QuitWhenIdle(); | |
| 171 } | |
| 172 | |
| 173 void ImmediateSuccessCallback() { ++success_callback_count_; } | |
| 174 | |
| 175 protected: | |
| 176 base::MessageLoop message_loop_; | |
| 177 | |
| 178 scoped_refptr<BluetoothAdapter> adapter_; | |
| 179 | |
| 180 unsigned int success_callback_count_; | |
| 181 unsigned int error_callback_count_; | |
| 182 | |
| 183 std::string last_message_; | |
| 184 scoped_refptr<BluetoothSocket> last_socket_; | |
| 185 int last_bytes_sent_; | |
| 186 int last_bytes_received_; | |
| 187 scoped_refptr<net::IOBuffer> last_io_buffer_; | |
| 188 BluetoothSocket::ErrorReason last_reason_; | |
| 189 const BluetoothDevice* last_device_; | |
| 190 }; | |
| 191 | |
| 192 TEST_F(BluetoothSocketBlueZTest, Connect) { | |
| 193 BluetoothDevice* device = adapter_->GetDevice( | |
| 194 bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress); | |
| 195 ASSERT_TRUE(device != nullptr); | |
| 196 | |
| 197 device->ConnectToService( | |
| 198 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid), | |
| 199 base::Bind(&BluetoothSocketBlueZTest::ConnectToServiceSuccessCallback, | |
| 200 base::Unretained(this)), | |
| 201 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, | |
| 202 base::Unretained(this))); | |
| 203 message_loop_.Run(); | |
| 204 | |
| 205 EXPECT_EQ(1U, success_callback_count_); | |
| 206 EXPECT_EQ(0U, error_callback_count_); | |
| 207 EXPECT_TRUE(last_socket_.get() != nullptr); | |
| 208 | |
| 209 // Take ownership of the socket for the remainder of the test. | |
| 210 scoped_refptr<BluetoothSocket> socket = last_socket_; | |
| 211 last_socket_ = nullptr; | |
| 212 success_callback_count_ = 0; | |
| 213 error_callback_count_ = 0; | |
| 214 | |
| 215 // Send data to the socket, expect all of the data to be sent. | |
| 216 scoped_refptr<net::StringIOBuffer> write_buffer( | |
| 217 new net::StringIOBuffer("test")); | |
| 218 | |
| 219 socket->Send(write_buffer.get(), write_buffer->size(), | |
| 220 base::Bind(&BluetoothSocketBlueZTest::SendSuccessCallback, | |
| 221 base::Unretained(this)), | |
| 222 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, | |
| 223 base::Unretained(this))); | |
| 224 message_loop_.Run(); | |
| 225 | |
| 226 EXPECT_EQ(1U, success_callback_count_); | |
| 227 EXPECT_EQ(0U, error_callback_count_); | |
| 228 EXPECT_EQ(last_bytes_sent_, write_buffer->size()); | |
| 229 | |
| 230 success_callback_count_ = 0; | |
| 231 error_callback_count_ = 0; | |
| 232 | |
| 233 // Receive data from the socket, and fetch the buffer from the callback; since | |
| 234 // the fake is an echo server, we expect to receive what we wrote. | |
| 235 socket->Receive(4096, | |
| 236 base::Bind(&BluetoothSocketBlueZTest::ReceiveSuccessCallback, | |
| 237 base::Unretained(this)), | |
| 238 base::Bind(&BluetoothSocketBlueZTest::ReceiveErrorCallback, | |
| 239 base::Unretained(this))); | |
| 240 message_loop_.Run(); | |
| 241 | |
| 242 EXPECT_EQ(1U, success_callback_count_); | |
| 243 EXPECT_EQ(0U, error_callback_count_); | |
| 244 EXPECT_EQ(4, last_bytes_received_); | |
| 245 EXPECT_TRUE(last_io_buffer_.get() != nullptr); | |
| 246 | |
| 247 // Take ownership of the received buffer. | |
| 248 scoped_refptr<net::IOBuffer> read_buffer = last_io_buffer_; | |
| 249 last_io_buffer_ = nullptr; | |
| 250 success_callback_count_ = 0; | |
| 251 error_callback_count_ = 0; | |
| 252 | |
| 253 std::string data = std::string(read_buffer->data(), last_bytes_received_); | |
| 254 EXPECT_EQ("test", data); | |
| 255 | |
| 256 read_buffer = nullptr; | |
| 257 | |
| 258 // Receive data again; the socket will have been closed, this should cause a | |
| 259 // disconnected error to be returned via the error callback. | |
| 260 socket->Receive(4096, | |
| 261 base::Bind(&BluetoothSocketBlueZTest::ReceiveSuccessCallback, | |
| 262 base::Unretained(this)), | |
| 263 base::Bind(&BluetoothSocketBlueZTest::ReceiveErrorCallback, | |
| 264 base::Unretained(this))); | |
| 265 message_loop_.Run(); | |
| 266 | |
| 267 EXPECT_EQ(0U, success_callback_count_); | |
| 268 EXPECT_EQ(1U, error_callback_count_); | |
| 269 EXPECT_EQ(BluetoothSocket::kDisconnected, last_reason_); | |
| 270 EXPECT_EQ(net::ErrorToString(net::OK), last_message_); | |
| 271 | |
| 272 success_callback_count_ = 0; | |
| 273 error_callback_count_ = 0; | |
| 274 | |
| 275 // Send data again; since the socket is closed we should get a system error | |
| 276 // equivalent to the connection reset error. | |
| 277 write_buffer = new net::StringIOBuffer("second test"); | |
| 278 | |
| 279 socket->Send(write_buffer.get(), write_buffer->size(), | |
| 280 base::Bind(&BluetoothSocketBlueZTest::SendSuccessCallback, | |
| 281 base::Unretained(this)), | |
| 282 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, | |
| 283 base::Unretained(this))); | |
| 284 message_loop_.Run(); | |
| 285 | |
| 286 EXPECT_EQ(0U, success_callback_count_); | |
| 287 EXPECT_EQ(1U, error_callback_count_); | |
| 288 EXPECT_EQ(net::ErrorToString(net::ERR_CONNECTION_RESET), last_message_); | |
| 289 | |
| 290 success_callback_count_ = 0; | |
| 291 error_callback_count_ = 0; | |
| 292 | |
| 293 // Close our end of the socket. | |
| 294 socket->Disconnect(base::Bind(&BluetoothSocketBlueZTest::SuccessCallback, | |
| 295 base::Unretained(this))); | |
| 296 | |
| 297 message_loop_.Run(); | |
| 298 EXPECT_EQ(1U, success_callback_count_); | |
| 299 } | |
| 300 | |
| 301 TEST_F(BluetoothSocketBlueZTest, Listen) { | |
| 302 adapter_->CreateRfcommService( | |
| 303 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid), | |
| 304 BluetoothAdapter::ServiceOptions(), | |
| 305 base::Bind(&BluetoothSocketBlueZTest::CreateServiceSuccessCallback, | |
| 306 base::Unretained(this)), | |
| 307 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, | |
| 308 base::Unretained(this))); | |
| 309 | |
| 310 message_loop_.Run(); | |
| 311 | |
| 312 EXPECT_EQ(1U, success_callback_count_); | |
| 313 EXPECT_EQ(0U, error_callback_count_); | |
| 314 EXPECT_TRUE(last_socket_.get() != nullptr); | |
| 315 | |
| 316 // Take ownership of the socket for the remainder of the test. | |
| 317 scoped_refptr<BluetoothSocket> server_socket = last_socket_; | |
| 318 last_socket_ = nullptr; | |
| 319 success_callback_count_ = 0; | |
| 320 error_callback_count_ = 0; | |
| 321 | |
| 322 // Simulate an incoming connection by just calling the ConnectProfile method | |
| 323 // of the underlying fake device client (from the BlueZ point of view, | |
| 324 // outgoing and incoming look the same). | |
| 325 // | |
| 326 // This is done before the Accept() call to simulate a pending call at the | |
| 327 // point that Accept() is called. | |
| 328 bluez::FakeBluetoothDeviceClient* fake_bluetooth_device_client = | |
| 329 static_cast<bluez::FakeBluetoothDeviceClient*>( | |
| 330 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()); | |
| 331 BluetoothDevice* device = adapter_->GetDevice( | |
| 332 bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress); | |
| 333 ASSERT_TRUE(device != nullptr); | |
| 334 fake_bluetooth_device_client->ConnectProfile( | |
| 335 static_cast<BluetoothDeviceBlueZ*>(device)->object_path(), | |
| 336 bluez::FakeBluetoothProfileManagerClient::kRfcommUuid, | |
| 337 base::Bind(&base::DoNothing), base::Bind(&DoNothingDBusErrorCallback)); | |
| 338 | |
| 339 message_loop_.RunUntilIdle(); | |
| 340 | |
| 341 server_socket->Accept( | |
| 342 base::Bind(&BluetoothSocketBlueZTest::AcceptSuccessCallback, | |
| 343 base::Unretained(this)), | |
| 344 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, | |
| 345 base::Unretained(this))); | |
| 346 | |
| 347 message_loop_.Run(); | |
| 348 | |
| 349 EXPECT_EQ(1U, success_callback_count_); | |
| 350 EXPECT_EQ(0U, error_callback_count_); | |
| 351 EXPECT_TRUE(last_socket_.get() != nullptr); | |
| 352 | |
| 353 // Take ownership of the client socket for the remainder of the test. | |
| 354 scoped_refptr<BluetoothSocket> client_socket = last_socket_; | |
| 355 last_socket_ = nullptr; | |
| 356 success_callback_count_ = 0; | |
| 357 error_callback_count_ = 0; | |
| 358 | |
| 359 // Close our end of the client socket. | |
| 360 client_socket->Disconnect(base::Bind( | |
| 361 &BluetoothSocketBlueZTest::SuccessCallback, base::Unretained(this))); | |
| 362 | |
| 363 message_loop_.Run(); | |
| 364 | |
| 365 EXPECT_EQ(1U, success_callback_count_); | |
| 366 client_socket = nullptr; | |
| 367 success_callback_count_ = 0; | |
| 368 error_callback_count_ = 0; | |
| 369 | |
| 370 // Run a second connection test, this time calling Accept() before the | |
| 371 // incoming connection comes in. | |
| 372 server_socket->Accept( | |
| 373 base::Bind(&BluetoothSocketBlueZTest::AcceptSuccessCallback, | |
| 374 base::Unretained(this)), | |
| 375 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, | |
| 376 base::Unretained(this))); | |
| 377 | |
| 378 message_loop_.RunUntilIdle(); | |
| 379 | |
| 380 fake_bluetooth_device_client->ConnectProfile( | |
| 381 static_cast<BluetoothDeviceBlueZ*>(device)->object_path(), | |
| 382 bluez::FakeBluetoothProfileManagerClient::kRfcommUuid, | |
| 383 base::Bind(&base::DoNothing), base::Bind(&DoNothingDBusErrorCallback)); | |
| 384 | |
| 385 message_loop_.Run(); | |
| 386 | |
| 387 EXPECT_EQ(1U, success_callback_count_); | |
| 388 EXPECT_EQ(0U, error_callback_count_); | |
| 389 EXPECT_TRUE(last_socket_.get() != nullptr); | |
| 390 | |
| 391 // Take ownership of the client socket for the remainder of the test. | |
| 392 client_socket = last_socket_; | |
| 393 last_socket_ = nullptr; | |
| 394 success_callback_count_ = 0; | |
| 395 error_callback_count_ = 0; | |
| 396 | |
| 397 // Close our end of the client socket. | |
| 398 client_socket->Disconnect(base::Bind( | |
| 399 &BluetoothSocketBlueZTest::SuccessCallback, base::Unretained(this))); | |
| 400 | |
| 401 message_loop_.Run(); | |
| 402 | |
| 403 EXPECT_EQ(1U, success_callback_count_); | |
| 404 client_socket = nullptr; | |
| 405 success_callback_count_ = 0; | |
| 406 error_callback_count_ = 0; | |
| 407 | |
| 408 // Now close the server socket. | |
| 409 server_socket->Disconnect( | |
| 410 base::Bind(&BluetoothSocketBlueZTest::ImmediateSuccessCallback, | |
| 411 base::Unretained(this))); | |
| 412 | |
| 413 message_loop_.RunUntilIdle(); | |
| 414 | |
| 415 EXPECT_EQ(1U, success_callback_count_); | |
| 416 } | |
| 417 | |
| 418 TEST_F(BluetoothSocketBlueZTest, ListenBeforeAdapterStart) { | |
| 419 // Start off with an invisible adapter, register the profile, then make | |
| 420 // the adapter visible. | |
| 421 bluez::FakeBluetoothAdapterClient* fake_bluetooth_adapter_client = | |
| 422 static_cast<bluez::FakeBluetoothAdapterClient*>( | |
| 423 bluez::BluezDBusManager::Get()->GetBluetoothAdapterClient()); | |
| 424 fake_bluetooth_adapter_client->SetVisible(false); | |
| 425 | |
| 426 adapter_->CreateRfcommService( | |
| 427 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid), | |
| 428 BluetoothAdapter::ServiceOptions(), | |
| 429 base::Bind(&BluetoothSocketBlueZTest::CreateServiceSuccessCallback, | |
| 430 base::Unretained(this)), | |
| 431 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, | |
| 432 base::Unretained(this))); | |
| 433 message_loop_.Run(); | |
| 434 | |
| 435 EXPECT_EQ(1U, success_callback_count_); | |
| 436 EXPECT_EQ(0U, error_callback_count_); | |
| 437 EXPECT_TRUE(last_socket_.get() != nullptr); | |
| 438 | |
| 439 // Take ownership of the socket for the remainder of the test. | |
| 440 scoped_refptr<BluetoothSocket> socket = last_socket_; | |
| 441 last_socket_ = nullptr; | |
| 442 success_callback_count_ = 0; | |
| 443 error_callback_count_ = 0; | |
| 444 | |
| 445 // But there shouldn't be a profile registered yet. | |
| 446 bluez::FakeBluetoothProfileManagerClient* | |
| 447 fake_bluetooth_profile_manager_client = | |
| 448 static_cast<bluez::FakeBluetoothProfileManagerClient*>( | |
| 449 bluez::BluezDBusManager::Get() | |
| 450 ->GetBluetoothProfileManagerClient()); | |
| 451 bluez::FakeBluetoothProfileServiceProvider* profile_service_provider = | |
| 452 fake_bluetooth_profile_manager_client->GetProfileServiceProvider( | |
| 453 bluez::FakeBluetoothProfileManagerClient::kRfcommUuid); | |
| 454 EXPECT_TRUE(profile_service_provider == nullptr); | |
| 455 | |
| 456 // Make the adapter visible. This should register a profile. | |
| 457 fake_bluetooth_adapter_client->SetVisible(true); | |
| 458 | |
| 459 message_loop_.RunUntilIdle(); | |
| 460 | |
| 461 profile_service_provider = | |
| 462 fake_bluetooth_profile_manager_client->GetProfileServiceProvider( | |
| 463 bluez::FakeBluetoothProfileManagerClient::kRfcommUuid); | |
| 464 EXPECT_TRUE(profile_service_provider != nullptr); | |
| 465 | |
| 466 // Cleanup the socket. | |
| 467 socket->Disconnect( | |
| 468 base::Bind(&BluetoothSocketBlueZTest::ImmediateSuccessCallback, | |
| 469 base::Unretained(this))); | |
| 470 | |
| 471 message_loop_.RunUntilIdle(); | |
| 472 | |
| 473 EXPECT_EQ(1U, success_callback_count_); | |
| 474 } | |
| 475 | |
| 476 TEST_F(BluetoothSocketBlueZTest, ListenAcrossAdapterRestart) { | |
| 477 // The fake adapter starts off visible by default. | |
| 478 bluez::FakeBluetoothAdapterClient* fake_bluetooth_adapter_client = | |
| 479 static_cast<bluez::FakeBluetoothAdapterClient*>( | |
| 480 bluez::BluezDBusManager::Get()->GetBluetoothAdapterClient()); | |
| 481 | |
| 482 adapter_->CreateRfcommService( | |
| 483 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid), | |
| 484 BluetoothAdapter::ServiceOptions(), | |
| 485 base::Bind(&BluetoothSocketBlueZTest::CreateServiceSuccessCallback, | |
| 486 base::Unretained(this)), | |
| 487 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, | |
| 488 base::Unretained(this))); | |
| 489 message_loop_.Run(); | |
| 490 | |
| 491 EXPECT_EQ(1U, success_callback_count_); | |
| 492 EXPECT_EQ(0U, error_callback_count_); | |
| 493 EXPECT_TRUE(last_socket_.get() != nullptr); | |
| 494 | |
| 495 // Take ownership of the socket for the remainder of the test. | |
| 496 scoped_refptr<BluetoothSocket> socket = last_socket_; | |
| 497 last_socket_ = nullptr; | |
| 498 success_callback_count_ = 0; | |
| 499 error_callback_count_ = 0; | |
| 500 | |
| 501 // Make sure the profile was registered with the daemon. | |
| 502 bluez::FakeBluetoothProfileManagerClient* | |
| 503 fake_bluetooth_profile_manager_client = | |
| 504 static_cast<bluez::FakeBluetoothProfileManagerClient*>( | |
| 505 bluez::BluezDBusManager::Get() | |
| 506 ->GetBluetoothProfileManagerClient()); | |
| 507 bluez::FakeBluetoothProfileServiceProvider* profile_service_provider = | |
| 508 fake_bluetooth_profile_manager_client->GetProfileServiceProvider( | |
| 509 bluez::FakeBluetoothProfileManagerClient::kRfcommUuid); | |
| 510 EXPECT_TRUE(profile_service_provider != nullptr); | |
| 511 | |
| 512 // Make the adapter invisible, and fiddle with the profile fake to unregister | |
| 513 // the profile since this doesn't happen automatically. | |
| 514 fake_bluetooth_adapter_client->SetVisible(false); | |
| 515 | |
| 516 message_loop_.RunUntilIdle(); | |
| 517 | |
| 518 // Then make the adapter visible again. This should re-register the profile. | |
| 519 fake_bluetooth_adapter_client->SetVisible(true); | |
| 520 | |
| 521 message_loop_.RunUntilIdle(); | |
| 522 | |
| 523 profile_service_provider = | |
| 524 fake_bluetooth_profile_manager_client->GetProfileServiceProvider( | |
| 525 bluez::FakeBluetoothProfileManagerClient::kRfcommUuid); | |
| 526 EXPECT_TRUE(profile_service_provider != nullptr); | |
| 527 | |
| 528 // Cleanup the socket. | |
| 529 socket->Disconnect( | |
| 530 base::Bind(&BluetoothSocketBlueZTest::ImmediateSuccessCallback, | |
| 531 base::Unretained(this))); | |
| 532 | |
| 533 message_loop_.RunUntilIdle(); | |
| 534 | |
| 535 EXPECT_EQ(1U, success_callback_count_); | |
| 536 } | |
| 537 | |
| 538 TEST_F(BluetoothSocketBlueZTest, PairedConnectFails) { | |
| 539 BluetoothDevice* device = adapter_->GetDevice( | |
| 540 bluez::FakeBluetoothDeviceClient::kPairedUnconnectableDeviceAddress); | |
| 541 ASSERT_TRUE(device != nullptr); | |
| 542 | |
| 543 device->ConnectToService( | |
| 544 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid), | |
| 545 base::Bind(&BluetoothSocketBlueZTest::ConnectToServiceSuccessCallback, | |
| 546 base::Unretained(this)), | |
| 547 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, | |
| 548 base::Unretained(this))); | |
| 549 message_loop_.Run(); | |
| 550 | |
| 551 EXPECT_EQ(0U, success_callback_count_); | |
| 552 EXPECT_EQ(1U, error_callback_count_); | |
| 553 EXPECT_TRUE(last_socket_.get() == nullptr); | |
| 554 | |
| 555 device->ConnectToService( | |
| 556 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid), | |
| 557 base::Bind(&BluetoothSocketBlueZTest::ConnectToServiceSuccessCallback, | |
| 558 base::Unretained(this)), | |
| 559 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, | |
| 560 base::Unretained(this))); | |
| 561 message_loop_.Run(); | |
| 562 | |
| 563 EXPECT_EQ(0U, success_callback_count_); | |
| 564 EXPECT_EQ(2U, error_callback_count_); | |
| 565 EXPECT_TRUE(last_socket_.get() == nullptr); | |
| 566 } | |
| 567 | |
| 568 TEST_F(BluetoothSocketBlueZTest, SocketListenTwice) { | |
| 569 adapter_->CreateRfcommService( | |
| 570 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid), | |
| 571 BluetoothAdapter::ServiceOptions(), | |
| 572 base::Bind(&BluetoothSocketBlueZTest::CreateServiceSuccessCallback, | |
| 573 base::Unretained(this)), | |
| 574 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, | |
| 575 base::Unretained(this))); | |
| 576 | |
| 577 message_loop_.Run(); | |
| 578 | |
| 579 EXPECT_EQ(1U, success_callback_count_); | |
| 580 EXPECT_EQ(0U, error_callback_count_); | |
| 581 EXPECT_TRUE(last_socket_.get() != nullptr); | |
| 582 | |
| 583 // Take control of this socket. | |
| 584 scoped_refptr<BluetoothSocket> server_socket; | |
| 585 server_socket.swap(last_socket_); | |
| 586 | |
| 587 server_socket->Accept( | |
| 588 base::Bind(&BluetoothSocketBlueZTest::AcceptSuccessCallback, | |
| 589 base::Unretained(this)), | |
| 590 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, | |
| 591 base::Unretained(this))); | |
| 592 | |
| 593 server_socket->Close(); | |
| 594 | |
| 595 server_socket = nullptr; | |
| 596 | |
| 597 message_loop_.RunUntilIdle(); | |
| 598 | |
| 599 EXPECT_EQ(1U, success_callback_count_); | |
| 600 EXPECT_EQ(1U, error_callback_count_); | |
| 601 | |
| 602 adapter_->CreateRfcommService( | |
| 603 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid), | |
| 604 BluetoothAdapter::ServiceOptions(), | |
| 605 base::Bind(&BluetoothSocketBlueZTest::CreateServiceSuccessCallback, | |
| 606 base::Unretained(this)), | |
| 607 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, | |
| 608 base::Unretained(this))); | |
| 609 | |
| 610 message_loop_.Run(); | |
| 611 | |
| 612 EXPECT_EQ(2U, success_callback_count_); | |
| 613 EXPECT_EQ(1U, error_callback_count_); | |
| 614 EXPECT_TRUE(last_socket_.get() != nullptr); | |
| 615 | |
| 616 // Take control of this socket. | |
| 617 server_socket.swap(last_socket_); | |
| 618 | |
| 619 server_socket->Accept( | |
| 620 base::Bind(&BluetoothSocketBlueZTest::AcceptSuccessCallback, | |
| 621 base::Unretained(this)), | |
| 622 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, | |
| 623 base::Unretained(this))); | |
| 624 | |
| 625 server_socket->Close(); | |
| 626 | |
| 627 server_socket = nullptr; | |
| 628 | |
| 629 message_loop_.RunUntilIdle(); | |
| 630 | |
| 631 EXPECT_EQ(2U, success_callback_count_); | |
| 632 EXPECT_EQ(2U, error_callback_count_); | |
| 633 } | |
| 634 | |
| 635 } // namespace bluez | |
| OLD | NEW |