PageRenderTime 59ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/device/bluetooth/bluez/bluetooth_adapter_profile_bluez_unittest.cc

http://github.com/chromium/chromium
C++ | 391 lines | 295 code | 85 blank | 11 comment | 2 complexity | 204a4fbdf63f65bf13408bb030e60826 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.0, BSD-2-Clause, LGPL-2.1, MPL-2.0, 0BSD, EPL-1.0, MPL-2.0-no-copyleft-exception, GPL-2.0, BitTorrent-1.0, CPL-1.0, LGPL-3.0, Unlicense, BSD-3-Clause, CC0-1.0, JSON, MIT, GPL-3.0, CC-BY-SA-3.0, AGPL-1.0
  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. #include "device/bluetooth/bluez/bluetooth_adapter_profile_bluez.h"
  5. #include <memory>
  6. #include "base/bind.h"
  7. #include "base/bind_helpers.h"
  8. #include "base/run_loop.h"
  9. #include "base/test/bind_test_util.h"
  10. #include "base/test/task_environment.h"
  11. #include "device/bluetooth/bluetooth_adapter.h"
  12. #include "device/bluetooth/bluetooth_adapter_factory.h"
  13. #include "device/bluetooth/bluez/bluetooth_adapter_bluez.h"
  14. #include "device/bluetooth/dbus/bluetooth_profile_service_provider.h"
  15. #include "device/bluetooth/dbus/bluez_dbus_manager.h"
  16. #include "device/bluetooth/dbus/fake_bluetooth_adapter_client.h"
  17. #include "device/bluetooth/dbus/fake_bluetooth_agent_manager_client.h"
  18. #include "device/bluetooth/dbus/fake_bluetooth_device_client.h"
  19. #include "device/bluetooth/dbus/fake_bluetooth_profile_manager_client.h"
  20. #include "device/bluetooth/public/cpp/bluetooth_uuid.h"
  21. #include "testing/gtest/include/gtest/gtest.h"
  22. using device::BluetoothAdapter;
  23. using device::BluetoothUUID;
  24. namespace bluez {
  25. class BluetoothAdapterProfileBlueZTest : public testing::Test {
  26. public:
  27. BluetoothAdapterProfileBlueZTest()
  28. : success_callback_count_(0),
  29. error_callback_count_(0),
  30. fake_delegate_paired_(
  31. bluez::FakeBluetoothDeviceClient::kPairedDevicePath),
  32. fake_delegate_autopair_(
  33. bluez::FakeBluetoothDeviceClient::kLegacyAutopairPath),
  34. fake_delegate_listen_(""),
  35. profile_user_ptr_(nullptr) {}
  36. void SetUp() override {
  37. std::unique_ptr<bluez::BluezDBusManagerSetter> dbus_setter =
  38. bluez::BluezDBusManager::GetSetterForTesting();
  39. dbus_setter->SetBluetoothAdapterClient(
  40. std::unique_ptr<bluez::BluetoothAdapterClient>(
  41. new bluez::FakeBluetoothAdapterClient));
  42. dbus_setter->SetBluetoothAgentManagerClient(
  43. std::unique_ptr<bluez::BluetoothAgentManagerClient>(
  44. new bluez::FakeBluetoothAgentManagerClient));
  45. dbus_setter->SetBluetoothDeviceClient(
  46. std::unique_ptr<bluez::BluetoothDeviceClient>(
  47. new bluez::FakeBluetoothDeviceClient));
  48. dbus_setter->SetBluetoothProfileManagerClient(
  49. std::unique_ptr<bluez::BluetoothProfileManagerClient>(
  50. new bluez::FakeBluetoothProfileManagerClient));
  51. // Grab a pointer to the adapter.
  52. base::RunLoop run_loop;
  53. device::BluetoothAdapterFactory::Get()->GetAdapter(
  54. base::BindLambdaForTesting(
  55. [&](scoped_refptr<BluetoothAdapter> adapter) {
  56. adapter_ = std::move(adapter);
  57. run_loop.Quit();
  58. }));
  59. run_loop.Run();
  60. ASSERT_TRUE(adapter_);
  61. ASSERT_TRUE(adapter_->IsInitialized());
  62. ASSERT_TRUE(adapter_->IsPresent());
  63. // Turn on the adapter.
  64. adapter_->SetPowered(true, base::DoNothing(), base::DoNothing());
  65. ASSERT_TRUE(adapter_->IsPowered());
  66. }
  67. void TearDown() override {
  68. profile_.reset();
  69. adapter_ = nullptr;
  70. bluez::BluezDBusManager::Shutdown();
  71. }
  72. class FakeDelegate : public bluez::BluetoothProfileServiceProvider::Delegate {
  73. public:
  74. FakeDelegate(const std::string& device_path) : connections_(0) {
  75. device_path_ = dbus::ObjectPath(device_path);
  76. }
  77. // bluez::BluetoothProfileServiceProvider::Delegate:
  78. void Released() override {
  79. // noop
  80. }
  81. void NewConnection(
  82. const dbus::ObjectPath& device_path,
  83. base::ScopedFD fd,
  84. const bluez::BluetoothProfileServiceProvider::Delegate::Options&
  85. options,
  86. ConfirmationCallback callback) override {
  87. ++connections_;
  88. fd.reset();
  89. std::move(callback).Run(SUCCESS);
  90. if (device_path_.value() != "")
  91. ASSERT_EQ(device_path_, device_path);
  92. }
  93. void RequestDisconnection(const dbus::ObjectPath& device_path,
  94. ConfirmationCallback callback) override {
  95. ++disconnections_;
  96. }
  97. void Cancel() override {
  98. // noop
  99. }
  100. unsigned int connections_;
  101. unsigned int disconnections_;
  102. dbus::ObjectPath device_path_;
  103. };
  104. void ProfileSuccessCallback(
  105. std::unique_ptr<BluetoothAdapterProfileBlueZ> profile) {
  106. profile_.swap(profile);
  107. ++success_callback_count_;
  108. }
  109. void ProfileUserSuccessCallback(BluetoothAdapterProfileBlueZ* profile) {
  110. profile_user_ptr_ = profile;
  111. ++success_callback_count_;
  112. }
  113. void MatchedProfileCallback(BluetoothAdapterProfileBlueZ* profile) {
  114. ASSERT_EQ(profile_user_ptr_, profile);
  115. ++success_callback_count_;
  116. }
  117. void DBusConnectSuccessCallback() { ++success_callback_count_; }
  118. void DBusErrorCallback(const std::string& error_name,
  119. const std::string& error_message) {
  120. ++error_callback_count_;
  121. }
  122. void BasicErrorCallback(const std::string& error_message) {
  123. ++error_callback_count_;
  124. }
  125. protected:
  126. base::test::TaskEnvironment task_environment_;
  127. scoped_refptr<BluetoothAdapter> adapter_;
  128. unsigned int success_callback_count_;
  129. unsigned int error_callback_count_;
  130. FakeDelegate fake_delegate_paired_;
  131. FakeDelegate fake_delegate_autopair_;
  132. FakeDelegate fake_delegate_listen_;
  133. std::unique_ptr<BluetoothAdapterProfileBlueZ> profile_;
  134. // unowned pointer as expected to be used by clients of
  135. // BluetoothAdapterBlueZ::UseProfile like BluetoothSocketBlueZ
  136. BluetoothAdapterProfileBlueZ* profile_user_ptr_;
  137. };
  138. TEST_F(BluetoothAdapterProfileBlueZTest, DelegateCount) {
  139. BluetoothUUID uuid(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid);
  140. bluez::BluetoothProfileManagerClient::Options options;
  141. options.require_authentication.reset(new bool(false));
  142. BluetoothAdapterProfileBlueZ::Register(
  143. uuid, options,
  144. base::Bind(&BluetoothAdapterProfileBlueZTest::ProfileSuccessCallback,
  145. base::Unretained(this)),
  146. base::Bind(&BluetoothAdapterProfileBlueZTest::DBusErrorCallback,
  147. base::Unretained(this)));
  148. base::RunLoop().RunUntilIdle();
  149. EXPECT_TRUE(profile_);
  150. EXPECT_EQ(1U, success_callback_count_);
  151. EXPECT_EQ(0U, error_callback_count_);
  152. EXPECT_EQ(0U, profile_->DelegateCount());
  153. profile_->SetDelegate(fake_delegate_paired_.device_path_,
  154. &fake_delegate_paired_);
  155. EXPECT_EQ(1U, profile_->DelegateCount());
  156. profile_->RemoveDelegate(fake_delegate_autopair_.device_path_,
  157. base::DoNothing());
  158. EXPECT_EQ(1U, profile_->DelegateCount());
  159. profile_->RemoveDelegate(fake_delegate_paired_.device_path_,
  160. base::DoNothing());
  161. EXPECT_EQ(0U, profile_->DelegateCount());
  162. }
  163. TEST_F(BluetoothAdapterProfileBlueZTest, BlackHole) {
  164. BluetoothUUID uuid(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid);
  165. bluez::BluetoothProfileManagerClient::Options options;
  166. options.require_authentication.reset(new bool(false));
  167. BluetoothAdapterProfileBlueZ::Register(
  168. uuid, options,
  169. base::Bind(&BluetoothAdapterProfileBlueZTest::ProfileSuccessCallback,
  170. base::Unretained(this)),
  171. base::Bind(&BluetoothAdapterProfileBlueZTest::DBusErrorCallback,
  172. base::Unretained(this)));
  173. base::RunLoop().RunUntilIdle();
  174. EXPECT_TRUE(profile_);
  175. EXPECT_EQ(1U, success_callback_count_);
  176. EXPECT_EQ(0U, error_callback_count_);
  177. bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->ConnectProfile(
  178. dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kPairedDevicePath),
  179. bluez::FakeBluetoothProfileManagerClient::kRfcommUuid,
  180. base::BindOnce(
  181. &BluetoothAdapterProfileBlueZTest::DBusConnectSuccessCallback,
  182. base::Unretained(this)),
  183. base::BindOnce(&BluetoothAdapterProfileBlueZTest::DBusErrorCallback,
  184. base::Unretained(this)));
  185. base::RunLoop().RunUntilIdle();
  186. EXPECT_EQ(1U, success_callback_count_);
  187. EXPECT_EQ(1U, error_callback_count_);
  188. EXPECT_EQ(0U, fake_delegate_paired_.connections_);
  189. }
  190. TEST_F(BluetoothAdapterProfileBlueZTest, Routing) {
  191. BluetoothUUID uuid(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid);
  192. bluez::BluetoothProfileManagerClient::Options options;
  193. options.require_authentication.reset(new bool(false));
  194. BluetoothAdapterProfileBlueZ::Register(
  195. uuid, options,
  196. base::Bind(&BluetoothAdapterProfileBlueZTest::ProfileSuccessCallback,
  197. base::Unretained(this)),
  198. base::Bind(&BluetoothAdapterProfileBlueZTest::DBusErrorCallback,
  199. base::Unretained(this)));
  200. base::RunLoop().RunUntilIdle();
  201. ASSERT_TRUE(profile_);
  202. ASSERT_EQ(1U, success_callback_count_);
  203. ASSERT_EQ(0U, error_callback_count_);
  204. profile_->SetDelegate(fake_delegate_paired_.device_path_,
  205. &fake_delegate_paired_);
  206. profile_->SetDelegate(fake_delegate_autopair_.device_path_,
  207. &fake_delegate_autopair_);
  208. profile_->SetDelegate(fake_delegate_listen_.device_path_,
  209. &fake_delegate_listen_);
  210. bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->ConnectProfile(
  211. dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kPairedDevicePath),
  212. bluez::FakeBluetoothProfileManagerClient::kRfcommUuid,
  213. base::BindOnce(
  214. &BluetoothAdapterProfileBlueZTest::DBusConnectSuccessCallback,
  215. base::Unretained(this)),
  216. base::BindOnce(&BluetoothAdapterProfileBlueZTest::DBusErrorCallback,
  217. base::Unretained(this)));
  218. base::RunLoop().RunUntilIdle();
  219. EXPECT_EQ(2U, success_callback_count_);
  220. EXPECT_EQ(0U, error_callback_count_);
  221. EXPECT_EQ(1U, fake_delegate_paired_.connections_);
  222. bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->ConnectProfile(
  223. dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kLegacyAutopairPath),
  224. bluez::FakeBluetoothProfileManagerClient::kRfcommUuid,
  225. base::BindOnce(
  226. &BluetoothAdapterProfileBlueZTest::DBusConnectSuccessCallback,
  227. base::Unretained(this)),
  228. base::BindOnce(&BluetoothAdapterProfileBlueZTest::DBusErrorCallback,
  229. base::Unretained(this)));
  230. base::RunLoop().RunUntilIdle();
  231. EXPECT_EQ(3U, success_callback_count_);
  232. EXPECT_EQ(0U, error_callback_count_);
  233. EXPECT_EQ(1U, fake_delegate_autopair_.connections_);
  234. // Incoming connections look the same from BlueZ.
  235. bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->ConnectProfile(
  236. dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kDisplayPinCodePath),
  237. bluez::FakeBluetoothProfileManagerClient::kRfcommUuid,
  238. base::BindOnce(
  239. &BluetoothAdapterProfileBlueZTest::DBusConnectSuccessCallback,
  240. base::Unretained(this)),
  241. base::BindOnce(&BluetoothAdapterProfileBlueZTest::DBusErrorCallback,
  242. base::Unretained(this)));
  243. base::RunLoop().RunUntilIdle();
  244. EXPECT_EQ(4U, success_callback_count_);
  245. EXPECT_EQ(0U, error_callback_count_);
  246. EXPECT_EQ(1U, fake_delegate_listen_.connections_);
  247. }
  248. TEST_F(BluetoothAdapterProfileBlueZTest, SimultaneousRegister) {
  249. BluetoothUUID uuid(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid);
  250. bluez::BluetoothProfileManagerClient::Options options;
  251. BluetoothAdapterBlueZ* adapter =
  252. static_cast<BluetoothAdapterBlueZ*>(adapter_.get());
  253. options.require_authentication.reset(new bool(false));
  254. success_callback_count_ = 0;
  255. error_callback_count_ = 0;
  256. adapter->UseProfile(
  257. uuid, fake_delegate_paired_.device_path_, options, &fake_delegate_paired_,
  258. base::Bind(&BluetoothAdapterProfileBlueZTest::ProfileUserSuccessCallback,
  259. base::Unretained(this)),
  260. base::Bind(&BluetoothAdapterProfileBlueZTest::BasicErrorCallback,
  261. base::Unretained(this)));
  262. adapter->UseProfile(
  263. uuid, fake_delegate_autopair_.device_path_, options,
  264. &fake_delegate_autopair_,
  265. base::Bind(&BluetoothAdapterProfileBlueZTest::MatchedProfileCallback,
  266. base::Unretained(this)),
  267. base::Bind(&BluetoothAdapterProfileBlueZTest::BasicErrorCallback,
  268. base::Unretained(this)));
  269. base::RunLoop().RunUntilIdle();
  270. EXPECT_TRUE(profile_user_ptr_);
  271. EXPECT_EQ(2U, success_callback_count_);
  272. EXPECT_EQ(0U, error_callback_count_);
  273. adapter->ReleaseProfile(fake_delegate_paired_.device_path_,
  274. profile_user_ptr_);
  275. adapter->ReleaseProfile(fake_delegate_autopair_.device_path_,
  276. profile_user_ptr_);
  277. base::RunLoop().RunUntilIdle();
  278. }
  279. TEST_F(BluetoothAdapterProfileBlueZTest, SimultaneousRegisterFail) {
  280. BluetoothUUID uuid(
  281. bluez::FakeBluetoothProfileManagerClient::kUnregisterableUuid);
  282. bluez::BluetoothProfileManagerClient::Options options;
  283. BluetoothAdapterBlueZ* adapter =
  284. static_cast<BluetoothAdapterBlueZ*>(adapter_.get());
  285. options.require_authentication.reset(new bool(false));
  286. success_callback_count_ = 0;
  287. error_callback_count_ = 0;
  288. adapter->UseProfile(
  289. uuid, fake_delegate_paired_.device_path_, options, &fake_delegate_paired_,
  290. base::Bind(&BluetoothAdapterProfileBlueZTest::ProfileUserSuccessCallback,
  291. base::Unretained(this)),
  292. base::Bind(&BluetoothAdapterProfileBlueZTest::BasicErrorCallback,
  293. base::Unretained(this)));
  294. adapter->UseProfile(
  295. uuid, fake_delegate_autopair_.device_path_, options,
  296. &fake_delegate_autopair_,
  297. base::Bind(&BluetoothAdapterProfileBlueZTest::MatchedProfileCallback,
  298. base::Unretained(this)),
  299. base::Bind(&BluetoothAdapterProfileBlueZTest::BasicErrorCallback,
  300. base::Unretained(this)));
  301. base::RunLoop().RunUntilIdle();
  302. EXPECT_FALSE(profile_user_ptr_);
  303. EXPECT_EQ(0U, success_callback_count_);
  304. EXPECT_EQ(2U, error_callback_count_);
  305. }
  306. } // namespace bluez