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