| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 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_adapter_profile_bluez.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/message_loop/message_loop.h" | |
| 11 #include "device/bluetooth/bluetooth_adapter.h" | |
| 12 #include "device/bluetooth/bluetooth_adapter_bluez.h" | |
| 13 #include "device/bluetooth/bluetooth_adapter_factory.h" | |
| 14 #include "device/bluetooth/bluetooth_uuid.h" | |
| 15 #include "device/bluetooth/dbus/bluetooth_profile_service_provider.h" | |
| 16 #include "device/bluetooth/dbus/bluez_dbus_manager.h" | |
| 17 #include "device/bluetooth/dbus/fake_bluetooth_adapter_client.h" | |
| 18 #include "device/bluetooth/dbus/fake_bluetooth_agent_manager_client.h" | |
| 19 #include "device/bluetooth/dbus/fake_bluetooth_device_client.h" | |
| 20 #include "device/bluetooth/dbus/fake_bluetooth_profile_manager_client.h" | |
| 21 #include "testing/gtest/include/gtest/gtest.h" | |
| 22 | |
| 23 using device::BluetoothAdapter; | |
| 24 using device::BluetoothUUID; | |
| 25 | |
| 26 namespace bluez { | |
| 27 | |
| 28 class BluetoothAdapterProfileBlueZTest : public testing::Test { | |
| 29 public: | |
| 30 BluetoothAdapterProfileBlueZTest() | |
| 31 : success_callback_count_(0), | |
| 32 error_callback_count_(0), | |
| 33 fake_delegate_paired_( | |
| 34 bluez::FakeBluetoothDeviceClient::kPairedDevicePath), | |
| 35 fake_delegate_autopair_( | |
| 36 bluez::FakeBluetoothDeviceClient::kLegacyAutopairPath), | |
| 37 fake_delegate_listen_(""), | |
| 38 profile_user_ptr_(nullptr) {} | |
| 39 | |
| 40 void SetUp() override { | |
| 41 std::unique_ptr<bluez::BluezDBusManagerSetter> dbus_setter = | |
| 42 bluez::BluezDBusManager::GetSetterForTesting(); | |
| 43 | |
| 44 dbus_setter->SetBluetoothAdapterClient( | |
| 45 std::unique_ptr<bluez::BluetoothAdapterClient>( | |
| 46 new bluez::FakeBluetoothAdapterClient)); | |
| 47 dbus_setter->SetBluetoothAgentManagerClient( | |
| 48 std::unique_ptr<bluez::BluetoothAgentManagerClient>( | |
| 49 new bluez::FakeBluetoothAgentManagerClient)); | |
| 50 dbus_setter->SetBluetoothDeviceClient( | |
| 51 std::unique_ptr<bluez::BluetoothDeviceClient>( | |
| 52 new bluez::FakeBluetoothDeviceClient)); | |
| 53 dbus_setter->SetBluetoothProfileManagerClient( | |
| 54 std::unique_ptr<bluez::BluetoothProfileManagerClient>( | |
| 55 new bluez::FakeBluetoothProfileManagerClient)); | |
| 56 | |
| 57 // Grab a pointer to the adapter. | |
| 58 device::BluetoothAdapterFactory::GetAdapter( | |
| 59 base::Bind(&BluetoothAdapterProfileBlueZTest::AdapterCallback, | |
| 60 base::Unretained(this))); | |
| 61 base::MessageLoop::current()->Run(); | |
| 62 ASSERT_TRUE(adapter_.get() != nullptr); | |
| 63 ASSERT_TRUE(adapter_->IsInitialized()); | |
| 64 ASSERT_TRUE(adapter_->IsPresent()); | |
| 65 | |
| 66 // Turn on the adapter. | |
| 67 adapter_->SetPowered(true, base::Bind(&base::DoNothing), | |
| 68 base::Bind(&base::DoNothing)); | |
| 69 ASSERT_TRUE(adapter_->IsPowered()); | |
| 70 } | |
| 71 | |
| 72 void TearDown() override { | |
| 73 profile_.reset(); | |
| 74 adapter_ = nullptr; | |
| 75 bluez::BluezDBusManager::Shutdown(); | |
| 76 } | |
| 77 | |
| 78 void AdapterCallback(scoped_refptr<BluetoothAdapter> adapter) { | |
| 79 adapter_ = adapter; | |
| 80 if (base::MessageLoop::current() && | |
| 81 base::MessageLoop::current()->is_running()) { | |
| 82 base::MessageLoop::current()->QuitWhenIdle(); | |
| 83 } | |
| 84 } | |
| 85 | |
| 86 class FakeDelegate : public bluez::BluetoothProfileServiceProvider::Delegate { | |
| 87 public: | |
| 88 FakeDelegate(const std::string& device_path) : connections_(0) { | |
| 89 device_path_ = dbus::ObjectPath(device_path); | |
| 90 } | |
| 91 | |
| 92 // bluez::BluetoothProfileServiceProvider::Delegate: | |
| 93 void Released() override { | |
| 94 // noop | |
| 95 } | |
| 96 | |
| 97 void NewConnection( | |
| 98 const dbus::ObjectPath& device_path, | |
| 99 std::unique_ptr<dbus::FileDescriptor> fd, | |
| 100 const bluez::BluetoothProfileServiceProvider::Delegate::Options& | |
| 101 options, | |
| 102 const ConfirmationCallback& callback) override { | |
| 103 ++connections_; | |
| 104 fd->CheckValidity(); | |
| 105 close(fd->TakeValue()); | |
| 106 callback.Run(SUCCESS); | |
| 107 if (device_path_.value() != "") | |
| 108 ASSERT_EQ(device_path_, device_path); | |
| 109 } | |
| 110 | |
| 111 void RequestDisconnection(const dbus::ObjectPath& device_path, | |
| 112 const ConfirmationCallback& callback) override { | |
| 113 ++disconnections_; | |
| 114 } | |
| 115 | |
| 116 void Cancel() override { | |
| 117 // noop | |
| 118 } | |
| 119 | |
| 120 unsigned int connections_; | |
| 121 unsigned int disconnections_; | |
| 122 dbus::ObjectPath device_path_; | |
| 123 }; | |
| 124 | |
| 125 void ProfileSuccessCallback( | |
| 126 std::unique_ptr<BluetoothAdapterProfileBlueZ> profile) { | |
| 127 profile_.swap(profile); | |
| 128 ++success_callback_count_; | |
| 129 } | |
| 130 | |
| 131 void ProfileUserSuccessCallback(BluetoothAdapterProfileBlueZ* profile) { | |
| 132 profile_user_ptr_ = profile; | |
| 133 ++success_callback_count_; | |
| 134 } | |
| 135 | |
| 136 void MatchedProfileCallback(BluetoothAdapterProfileBlueZ* profile) { | |
| 137 ASSERT_EQ(profile_user_ptr_, profile); | |
| 138 ++success_callback_count_; | |
| 139 } | |
| 140 | |
| 141 void DBusConnectSuccessCallback() { ++success_callback_count_; } | |
| 142 | |
| 143 void DBusErrorCallback(const std::string& error_name, | |
| 144 const std::string& error_message) { | |
| 145 ++error_callback_count_; | |
| 146 } | |
| 147 | |
| 148 void BasicErrorCallback(const std::string& error_message) { | |
| 149 ++error_callback_count_; | |
| 150 } | |
| 151 | |
| 152 protected: | |
| 153 base::MessageLoop message_loop_; | |
| 154 | |
| 155 scoped_refptr<BluetoothAdapter> adapter_; | |
| 156 | |
| 157 unsigned int success_callback_count_; | |
| 158 unsigned int error_callback_count_; | |
| 159 | |
| 160 FakeDelegate fake_delegate_paired_; | |
| 161 FakeDelegate fake_delegate_autopair_; | |
| 162 FakeDelegate fake_delegate_listen_; | |
| 163 | |
| 164 std::unique_ptr<BluetoothAdapterProfileBlueZ> profile_; | |
| 165 | |
| 166 // unowned pointer as expected to be used by clients of | |
| 167 // BluetoothAdapterBlueZ::UseProfile like BluetoothSocketBlueZ | |
| 168 BluetoothAdapterProfileBlueZ* profile_user_ptr_; | |
| 169 }; | |
| 170 | |
| 171 TEST_F(BluetoothAdapterProfileBlueZTest, DelegateCount) { | |
| 172 BluetoothUUID uuid(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid); | |
| 173 bluez::BluetoothProfileManagerClient::Options options; | |
| 174 | |
| 175 options.require_authentication.reset(new bool(false)); | |
| 176 | |
| 177 BluetoothAdapterProfileBlueZ::Register( | |
| 178 uuid, options, | |
| 179 base::Bind(&BluetoothAdapterProfileBlueZTest::ProfileSuccessCallback, | |
| 180 base::Unretained(this)), | |
| 181 base::Bind(&BluetoothAdapterProfileBlueZTest::DBusErrorCallback, | |
| 182 base::Unretained(this))); | |
| 183 | |
| 184 message_loop_.RunUntilIdle(); | |
| 185 | |
| 186 EXPECT_TRUE(profile_); | |
| 187 EXPECT_EQ(1U, success_callback_count_); | |
| 188 EXPECT_EQ(0U, error_callback_count_); | |
| 189 | |
| 190 EXPECT_EQ(0U, profile_->DelegateCount()); | |
| 191 | |
| 192 profile_->SetDelegate(fake_delegate_paired_.device_path_, | |
| 193 &fake_delegate_paired_); | |
| 194 | |
| 195 EXPECT_EQ(1U, profile_->DelegateCount()); | |
| 196 | |
| 197 profile_->RemoveDelegate(fake_delegate_autopair_.device_path_, | |
| 198 base::Bind(&base::DoNothing)); | |
| 199 | |
| 200 EXPECT_EQ(1U, profile_->DelegateCount()); | |
| 201 | |
| 202 profile_->RemoveDelegate(fake_delegate_paired_.device_path_, | |
| 203 base::Bind(&base::DoNothing)); | |
| 204 | |
| 205 EXPECT_EQ(0U, profile_->DelegateCount()); | |
| 206 } | |
| 207 | |
| 208 TEST_F(BluetoothAdapterProfileBlueZTest, BlackHole) { | |
| 209 BluetoothUUID uuid(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid); | |
| 210 bluez::BluetoothProfileManagerClient::Options options; | |
| 211 | |
| 212 options.require_authentication.reset(new bool(false)); | |
| 213 | |
| 214 BluetoothAdapterProfileBlueZ::Register( | |
| 215 uuid, options, | |
| 216 base::Bind(&BluetoothAdapterProfileBlueZTest::ProfileSuccessCallback, | |
| 217 base::Unretained(this)), | |
| 218 base::Bind(&BluetoothAdapterProfileBlueZTest::DBusErrorCallback, | |
| 219 base::Unretained(this))); | |
| 220 | |
| 221 message_loop_.RunUntilIdle(); | |
| 222 | |
| 223 EXPECT_TRUE(profile_); | |
| 224 EXPECT_EQ(1U, success_callback_count_); | |
| 225 EXPECT_EQ(0U, error_callback_count_); | |
| 226 | |
| 227 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->ConnectProfile( | |
| 228 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kPairedDevicePath), | |
| 229 bluez::FakeBluetoothProfileManagerClient::kRfcommUuid, | |
| 230 base::Bind(&BluetoothAdapterProfileBlueZTest::DBusConnectSuccessCallback, | |
| 231 base::Unretained(this)), | |
| 232 base::Bind(&BluetoothAdapterProfileBlueZTest::DBusErrorCallback, | |
| 233 base::Unretained(this))); | |
| 234 | |
| 235 message_loop_.RunUntilIdle(); | |
| 236 | |
| 237 EXPECT_EQ(1U, success_callback_count_); | |
| 238 EXPECT_EQ(1U, error_callback_count_); | |
| 239 | |
| 240 EXPECT_EQ(0U, fake_delegate_paired_.connections_); | |
| 241 } | |
| 242 | |
| 243 TEST_F(BluetoothAdapterProfileBlueZTest, Routing) { | |
| 244 BluetoothUUID uuid(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid); | |
| 245 bluez::BluetoothProfileManagerClient::Options options; | |
| 246 | |
| 247 options.require_authentication.reset(new bool(false)); | |
| 248 | |
| 249 BluetoothAdapterProfileBlueZ::Register( | |
| 250 uuid, options, | |
| 251 base::Bind(&BluetoothAdapterProfileBlueZTest::ProfileSuccessCallback, | |
| 252 base::Unretained(this)), | |
| 253 base::Bind(&BluetoothAdapterProfileBlueZTest::DBusErrorCallback, | |
| 254 base::Unretained(this))); | |
| 255 | |
| 256 message_loop_.RunUntilIdle(); | |
| 257 | |
| 258 ASSERT_TRUE(profile_); | |
| 259 ASSERT_EQ(1U, success_callback_count_); | |
| 260 ASSERT_EQ(0U, error_callback_count_); | |
| 261 | |
| 262 profile_->SetDelegate(fake_delegate_paired_.device_path_, | |
| 263 &fake_delegate_paired_); | |
| 264 profile_->SetDelegate(fake_delegate_autopair_.device_path_, | |
| 265 &fake_delegate_autopair_); | |
| 266 profile_->SetDelegate(fake_delegate_listen_.device_path_, | |
| 267 &fake_delegate_listen_); | |
| 268 | |
| 269 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->ConnectProfile( | |
| 270 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kPairedDevicePath), | |
| 271 bluez::FakeBluetoothProfileManagerClient::kRfcommUuid, | |
| 272 base::Bind(&BluetoothAdapterProfileBlueZTest::DBusConnectSuccessCallback, | |
| 273 base::Unretained(this)), | |
| 274 base::Bind(&BluetoothAdapterProfileBlueZTest::DBusErrorCallback, | |
| 275 base::Unretained(this))); | |
| 276 | |
| 277 message_loop_.RunUntilIdle(); | |
| 278 | |
| 279 EXPECT_EQ(2U, success_callback_count_); | |
| 280 EXPECT_EQ(0U, error_callback_count_); | |
| 281 | |
| 282 EXPECT_EQ(1U, fake_delegate_paired_.connections_); | |
| 283 | |
| 284 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->ConnectProfile( | |
| 285 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kLegacyAutopairPath), | |
| 286 bluez::FakeBluetoothProfileManagerClient::kRfcommUuid, | |
| 287 base::Bind(&BluetoothAdapterProfileBlueZTest::DBusConnectSuccessCallback, | |
| 288 base::Unretained(this)), | |
| 289 base::Bind(&BluetoothAdapterProfileBlueZTest::DBusErrorCallback, | |
| 290 base::Unretained(this))); | |
| 291 | |
| 292 message_loop_.RunUntilIdle(); | |
| 293 | |
| 294 EXPECT_EQ(3U, success_callback_count_); | |
| 295 EXPECT_EQ(0U, error_callback_count_); | |
| 296 | |
| 297 EXPECT_EQ(1U, fake_delegate_autopair_.connections_); | |
| 298 | |
| 299 // Incoming connections look the same from BlueZ. | |
| 300 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->ConnectProfile( | |
| 301 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kDisplayPinCodePath), | |
| 302 bluez::FakeBluetoothProfileManagerClient::kRfcommUuid, | |
| 303 base::Bind(&BluetoothAdapterProfileBlueZTest::DBusConnectSuccessCallback, | |
| 304 base::Unretained(this)), | |
| 305 base::Bind(&BluetoothAdapterProfileBlueZTest::DBusErrorCallback, | |
| 306 base::Unretained(this))); | |
| 307 | |
| 308 message_loop_.RunUntilIdle(); | |
| 309 | |
| 310 EXPECT_EQ(4U, success_callback_count_); | |
| 311 EXPECT_EQ(0U, error_callback_count_); | |
| 312 | |
| 313 EXPECT_EQ(1U, fake_delegate_listen_.connections_); | |
| 314 } | |
| 315 | |
| 316 TEST_F(BluetoothAdapterProfileBlueZTest, SimultaneousRegister) { | |
| 317 BluetoothUUID uuid(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid); | |
| 318 bluez::BluetoothProfileManagerClient::Options options; | |
| 319 BluetoothAdapterBlueZ* adapter = | |
| 320 static_cast<BluetoothAdapterBlueZ*>(adapter_.get()); | |
| 321 | |
| 322 options.require_authentication.reset(new bool(false)); | |
| 323 | |
| 324 success_callback_count_ = 0; | |
| 325 error_callback_count_ = 0; | |
| 326 | |
| 327 adapter->UseProfile( | |
| 328 uuid, fake_delegate_paired_.device_path_, options, &fake_delegate_paired_, | |
| 329 base::Bind(&BluetoothAdapterProfileBlueZTest::ProfileUserSuccessCallback, | |
| 330 base::Unretained(this)), | |
| 331 base::Bind(&BluetoothAdapterProfileBlueZTest::BasicErrorCallback, | |
| 332 base::Unretained(this))); | |
| 333 | |
| 334 adapter->UseProfile( | |
| 335 uuid, fake_delegate_autopair_.device_path_, options, | |
| 336 &fake_delegate_autopair_, | |
| 337 base::Bind(&BluetoothAdapterProfileBlueZTest::MatchedProfileCallback, | |
| 338 base::Unretained(this)), | |
| 339 base::Bind(&BluetoothAdapterProfileBlueZTest::BasicErrorCallback, | |
| 340 base::Unretained(this))); | |
| 341 | |
| 342 message_loop_.RunUntilIdle(); | |
| 343 | |
| 344 EXPECT_TRUE(profile_user_ptr_); | |
| 345 EXPECT_EQ(2U, success_callback_count_); | |
| 346 EXPECT_EQ(0U, error_callback_count_); | |
| 347 | |
| 348 adapter->ReleaseProfile(fake_delegate_paired_.device_path_, | |
| 349 profile_user_ptr_); | |
| 350 adapter->ReleaseProfile(fake_delegate_autopair_.device_path_, | |
| 351 profile_user_ptr_); | |
| 352 | |
| 353 message_loop_.RunUntilIdle(); | |
| 354 } | |
| 355 | |
| 356 TEST_F(BluetoothAdapterProfileBlueZTest, SimultaneousRegisterFail) { | |
| 357 BluetoothUUID uuid( | |
| 358 bluez::FakeBluetoothProfileManagerClient::kUnregisterableUuid); | |
| 359 bluez::BluetoothProfileManagerClient::Options options; | |
| 360 BluetoothAdapterBlueZ* adapter = | |
| 361 static_cast<BluetoothAdapterBlueZ*>(adapter_.get()); | |
| 362 | |
| 363 options.require_authentication.reset(new bool(false)); | |
| 364 | |
| 365 success_callback_count_ = 0; | |
| 366 error_callback_count_ = 0; | |
| 367 | |
| 368 adapter->UseProfile( | |
| 369 uuid, fake_delegate_paired_.device_path_, options, &fake_delegate_paired_, | |
| 370 base::Bind(&BluetoothAdapterProfileBlueZTest::ProfileUserSuccessCallback, | |
| 371 base::Unretained(this)), | |
| 372 base::Bind(&BluetoothAdapterProfileBlueZTest::BasicErrorCallback, | |
| 373 base::Unretained(this))); | |
| 374 | |
| 375 adapter->UseProfile( | |
| 376 uuid, fake_delegate_autopair_.device_path_, options, | |
| 377 &fake_delegate_autopair_, | |
| 378 base::Bind(&BluetoothAdapterProfileBlueZTest::MatchedProfileCallback, | |
| 379 base::Unretained(this)), | |
| 380 base::Bind(&BluetoothAdapterProfileBlueZTest::BasicErrorCallback, | |
| 381 base::Unretained(this))); | |
| 382 | |
| 383 message_loop_.RunUntilIdle(); | |
| 384 | |
| 385 EXPECT_FALSE(profile_user_ptr_); | |
| 386 EXPECT_EQ(0U, success_callback_count_); | |
| 387 EXPECT_EQ(2U, error_callback_count_); | |
| 388 } | |
| 389 | |
| 390 } // namespace bluez | |
| OLD | NEW |