fast_pair_gatt_service_client_unittest.cc 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. // Copyright 2021 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 "ash/quick_pair/fast_pair_handshake/fast_pair_gatt_service_client_impl.h"
  5. #include <stddef.h>
  6. #include "ash/quick_pair/common/constants.h"
  7. #include "ash/quick_pair/common/logging.h"
  8. #include "ash/quick_pair/common/pair_failure.h"
  9. #include "ash/quick_pair/fast_pair_handshake/fake_fast_pair_data_encryptor.h"
  10. #include "ash/quick_pair/fast_pair_handshake/fast_pair_data_encryptor.h"
  11. #include "ash/quick_pair/fast_pair_handshake/fast_pair_data_encryptor_impl.h"
  12. #include "base/bind.h"
  13. #include "base/callback.h"
  14. #include "base/memory/weak_ptr.h"
  15. #include "base/test/bind.h"
  16. #include "base/test/metrics/histogram_tester.h"
  17. #include "base/test/task_environment.h"
  18. #include "base/threading/thread_task_runner_handle.h"
  19. #include "base/time/time.h"
  20. #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h"
  21. #include "device/bluetooth/test/mock_bluetooth_adapter.h"
  22. #include "device/bluetooth/test/mock_bluetooth_device.h"
  23. #include "device/bluetooth/test/mock_bluetooth_gatt_characteristic.h"
  24. #include "device/bluetooth/test/mock_bluetooth_gatt_connection.h"
  25. #include "device/bluetooth/test/mock_bluetooth_gatt_notify_session.h"
  26. #include "device/bluetooth/test/mock_bluetooth_gatt_service.h"
  27. #include "testing/gtest/include/gtest/gtest.h"
  28. #include "third_party/abseil-cpp/absl/types/optional.h"
  29. #include "third_party/boringssl/src/include/openssl/rand.h"
  30. namespace {
  31. using NotifySessionCallback = base::OnceCallback<void(
  32. std::unique_ptr<device::BluetoothGattNotifySession>)>;
  33. using ErrorCallback =
  34. base::OnceCallback<void(device::BluetoothGattService::GattErrorCode)>;
  35. const char kTotalGattConnectionTime[] =
  36. "Bluetooth.ChromeOS.FastPair.TotalGattConnectionTime";
  37. const char kGattConnectionResult[] =
  38. "Bluetooth.ChromeOS.FastPair.GattConnection.Result";
  39. const char kGattConnectionErrorMetric[] =
  40. "Bluetooth.ChromeOS.FastPair.GattConnection.ErrorReason";
  41. const char kWriteKeyBasedCharacteristicGattError[] =
  42. "Bluetooth.ChromeOS.FastPair.KeyBasedPairing.Write.GattErrorReason";
  43. const char kNotifyKeyBasedCharacteristicTime[] =
  44. "Bluetooth.ChromeOS.FastPair.KeyBasedPairing.NotifyTime";
  45. const char kWritePasskeyCharacteristicGattError[] =
  46. "Bluetooth.ChromeOS.FastPair.Passkey.Write.GattErrorReason";
  47. const char kNotifyPasskeyCharacteristicTime[] =
  48. "Bluetooth.ChromeOS.FastPair.Passkey.NotifyTime";
  49. const char kWriteAccountKeyCharacteristicGattError[] =
  50. "Bluetooth.ChromeOS.FastPair.AccountKey.Write.GattErrorReason";
  51. const char kWriteAccountKeyTimeMetric[] =
  52. "Bluetooth.ChromeOS.FastPair.AccountKey.Write.TotalTime";
  53. constexpr base::TimeDelta kConnectingTestTimeout = base::Seconds(5);
  54. // Below constants are used to construct MockBluetoothDevice for testing.
  55. constexpr char kTestBleDeviceAddress[] = "11:12:13:14:15:16";
  56. const char kTestServiceId[] = "service_id1";
  57. const std::string kUUIDString1 = "keybased";
  58. const std::string kUUIDString2 = "passkey";
  59. const std::string kUUIDString3 = "accountkey";
  60. const device::BluetoothUUID kNonFastPairUuid("0xFE2B");
  61. const device::BluetoothUUID kKeyBasedCharacteristicUuid1("1234");
  62. const device::BluetoothUUID kKeyBasedCharacteristicUuid2(
  63. "FE2C1234-8366-4814-8EB0-01DE32100BEA");
  64. const device::BluetoothUUID kPasskeyCharacteristicUuid1("1235");
  65. const device::BluetoothUUID kPasskeyCharacteristicUuid2(
  66. "FE2C1235-8366-4814-8EB0-01DE32100BEA");
  67. const device::BluetoothUUID kAccountKeyCharacteristicUuid1("1236");
  68. const device::BluetoothUUID kAccountKeyCharacteristicUuid2(
  69. "FE2C1236-8366-4814-8EB0-01DE32100BEA");
  70. const uint8_t kMessageType = 0x00;
  71. const uint8_t kFlags = 0x00;
  72. const std::string kProviderAddress = "abcde";
  73. const std::string kSeekersAddress = "abcde";
  74. const std::vector<uint8_t>& kTestWriteResponse{0x01, 0x03, 0x02, 0x01, 0x02};
  75. const uint8_t kSeekerPasskey = 0x02;
  76. const uint32_t kPasskey = 13;
  77. const std::array<uint8_t, 16> kAccountKey = {0x04, 0x01, 0x01, 0x01, 0x01, 0x01,
  78. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  79. 0x01, 0x01, 0x01, 0x01};
  80. const std::array<uint8_t, 64> kPublicKey = {
  81. 0x01, 0x5E, 0x3F, 0x45, 0x61, 0xC3, 0x32, 0x1D, 0x01, 0x5E, 0x3F,
  82. 0x45, 0x61, 0xC3, 0x32, 0x1D, 0x01, 0x5E, 0x3F, 0x45, 0x61, 0xC3,
  83. 0x32, 0x1D, 0x01, 0x5E, 0x3F, 0x45, 0x61, 0xC3, 0x32, 0x1D, 0x01,
  84. 0x5E, 0x3F, 0x45, 0x61, 0xC3, 0x32, 0x1D, 0x01, 0x5E, 0x3F, 0x45,
  85. 0x61, 0xC3, 0x32, 0x1D, 0x01, 0x5E, 0x3F, 0x45, 0x61, 0xC3, 0x32,
  86. 0x1D, 0x01, 0x5E, 0x3F, 0x45, 0x61, 0xC3, 0x32, 0x1D};
  87. const device::BluetoothRemoteGattCharacteristic::Properties kProperties =
  88. device::BluetoothRemoteGattCharacteristic::PROPERTY_READ |
  89. device::BluetoothRemoteGattCharacteristic::PROPERTY_WRITE_WITHOUT_RESPONSE |
  90. device::BluetoothRemoteGattCharacteristic::PROPERTY_INDICATE;
  91. const device::BluetoothRemoteGattCharacteristic::Permissions kPermissions =
  92. device::BluetoothRemoteGattCharacteristic::PERMISSION_READ_ENCRYPTED |
  93. device::BluetoothRemoteGattCharacteristic::PERMISSION_WRITE_ENCRYPTED;
  94. class FakeBluetoothAdapter
  95. : public testing::NiceMock<device::MockBluetoothAdapter> {
  96. public:
  97. FakeBluetoothAdapter() = default;
  98. // Move-only class
  99. FakeBluetoothAdapter(const FakeBluetoothAdapter&) = delete;
  100. FakeBluetoothAdapter& operator=(const FakeBluetoothAdapter&) = delete;
  101. device::BluetoothDevice* GetDevice(const std::string& address) override {
  102. for (const auto& it : mock_devices_) {
  103. if (it->GetAddress() == address)
  104. return it.get();
  105. }
  106. return nullptr;
  107. }
  108. void NotifyGattDiscoveryCompleteForService(
  109. device::BluetoothRemoteGattService* service) {
  110. device::BluetoothAdapter::NotifyGattDiscoveryComplete(service);
  111. }
  112. void NotifyGattCharacteristicValueChanged(
  113. device::BluetoothRemoteGattCharacteristic* characteristic) {
  114. device::BluetoothAdapter::NotifyGattCharacteristicValueChanged(
  115. characteristic, kTestWriteResponse);
  116. }
  117. protected:
  118. ~FakeBluetoothAdapter() override = default;
  119. };
  120. class FakeBluetoothDevice
  121. : public testing::NiceMock<device::MockBluetoothDevice> {
  122. public:
  123. FakeBluetoothDevice(FakeBluetoothAdapter* adapter, const std::string& address)
  124. : testing::NiceMock<device::MockBluetoothDevice>(adapter,
  125. /*bluetooth_class=*/0u,
  126. /*name=*/"Test Device",
  127. address,
  128. /*paired=*/true,
  129. /*connected=*/true),
  130. fake_adapter_(adapter) {}
  131. void CreateGattConnection(
  132. device::BluetoothDevice::GattConnectionCallback callback,
  133. absl::optional<device::BluetoothUUID> service_uuid =
  134. absl::nullopt) override {
  135. gatt_connection_ = std::make_unique<
  136. testing::NiceMock<device::MockBluetoothGattConnection>>(
  137. fake_adapter_, kTestBleDeviceAddress);
  138. if (has_gatt_connection_error_) {
  139. std::move(callback).Run(std::move(gatt_connection_),
  140. /*error_code=*/device::BluetoothDevice::
  141. ConnectErrorCode::ERROR_FAILED);
  142. } else {
  143. ON_CALL(*gatt_connection_.get(), IsConnected)
  144. .WillByDefault(testing::Return(true));
  145. std::move(callback).Run(std::move(gatt_connection_), absl::nullopt);
  146. }
  147. }
  148. void SetError(bool has_gatt_connection_error) {
  149. has_gatt_connection_error_ = has_gatt_connection_error;
  150. }
  151. // Move-only class
  152. FakeBluetoothDevice(const FakeBluetoothDevice&) = delete;
  153. FakeBluetoothDevice& operator=(const FakeBluetoothDevice&) = delete;
  154. protected:
  155. std::unique_ptr<testing::NiceMock<device::MockBluetoothGattConnection>>
  156. gatt_connection_;
  157. bool has_gatt_connection_error_ = false;
  158. FakeBluetoothAdapter* fake_adapter_;
  159. };
  160. class FakeBluetoothGattCharacteristic
  161. : public testing::NiceMock<device::MockBluetoothGattCharacteristic> {
  162. public:
  163. FakeBluetoothGattCharacteristic(device::MockBluetoothGattService* service,
  164. const std::string& identifier,
  165. const device::BluetoothUUID& uuid,
  166. Properties properties,
  167. Permissions permissions)
  168. : testing::NiceMock<device::MockBluetoothGattCharacteristic>(
  169. service,
  170. identifier,
  171. uuid,
  172. properties,
  173. permissions) {}
  174. // Move-only class
  175. FakeBluetoothGattCharacteristic(const FakeBluetoothGattCharacteristic&) =
  176. delete;
  177. FakeBluetoothGattCharacteristic operator=(
  178. const FakeBluetoothGattCharacteristic&) = delete;
  179. void StartNotifySession(NotifySessionCallback callback,
  180. ErrorCallback error_callback) override {
  181. if (notify_session_error_) {
  182. std::move(error_callback)
  183. .Run(device::BluetoothGattService::GATT_ERROR_NOT_PERMITTED);
  184. return;
  185. }
  186. auto fake_notify_session = std::make_unique<
  187. testing::NiceMock<device::MockBluetoothGattNotifySession>>(
  188. GetWeakPtr());
  189. if (notify_timeout_)
  190. task_environment_->FastForwardBy(kConnectingTestTimeout);
  191. std::move(callback).Run(std::move(fake_notify_session));
  192. }
  193. void WriteRemoteCharacteristic(const std::vector<uint8_t>& value,
  194. WriteType write_type,
  195. base::OnceClosure callback,
  196. ErrorCallback error_callback) override {
  197. if (write_remote_error_) {
  198. std::move(error_callback)
  199. .Run(device::BluetoothGattService::GATT_ERROR_NOT_PERMITTED);
  200. return;
  201. }
  202. if (write_timeout_)
  203. task_environment_->FastForwardBy(kConnectingTestTimeout);
  204. std::move(callback).Run();
  205. }
  206. void SetWriteError(bool write_remote_error) {
  207. write_remote_error_ = write_remote_error;
  208. }
  209. void SetWriteTimeout(bool write_timeout,
  210. base::test::TaskEnvironment* task_environment) {
  211. write_timeout_ = write_timeout;
  212. task_environment_ = task_environment;
  213. }
  214. void SetNotifySessionError(bool notify_session_error) {
  215. notify_session_error_ = notify_session_error;
  216. }
  217. void SetNotifySessionTimeout(bool timeout,
  218. base::test::TaskEnvironment* task_environment) {
  219. notify_timeout_ = timeout;
  220. task_environment_ = task_environment;
  221. }
  222. private:
  223. bool notify_session_error_ = false;
  224. bool write_remote_error_ = false;
  225. bool notify_timeout_ = false;
  226. bool write_timeout_ = false;
  227. base::test::TaskEnvironment* task_environment_ = nullptr;
  228. };
  229. std::unique_ptr<FakeBluetoothDevice> CreateTestBluetoothDevice(
  230. FakeBluetoothAdapter* adapter,
  231. device::BluetoothUUID uuid) {
  232. auto mock_device = std::make_unique<FakeBluetoothDevice>(
  233. /*adapter=*/adapter, kTestBleDeviceAddress);
  234. mock_device->AddUUID(uuid);
  235. mock_device->SetServiceDataForUUID(uuid, {1, 2, 3});
  236. return mock_device;
  237. }
  238. } // namespace
  239. namespace ash {
  240. namespace quick_pair {
  241. class FastPairGattServiceClientTest : public testing::Test {
  242. public:
  243. void SetUp() override { fast_pair_data_encryptor_->public_key(kPublicKey); }
  244. void SuccessfulGattConnectionSetUp() {
  245. adapter_ = base::MakeRefCounted<FakeBluetoothAdapter>();
  246. device_ = CreateTestBluetoothDevice(
  247. adapter_.get(), ash::quick_pair::kFastPairBluetoothUuid);
  248. adapter_->AddMockDevice(std::move(device_));
  249. gatt_service_client_ = FastPairGattServiceClientImpl::Factory::Create(
  250. adapter_->GetDevice(kTestBleDeviceAddress), adapter_.get(),
  251. base::BindRepeating(&::ash::quick_pair::FastPairGattServiceClientTest::
  252. InitializedTestCallback,
  253. weak_ptr_factory_.GetWeakPtr()));
  254. }
  255. void FailedGattConnectionSetUp() {
  256. adapter_ = base::MakeRefCounted<FakeBluetoothAdapter>();
  257. device_ = CreateTestBluetoothDevice(
  258. adapter_.get(), ash::quick_pair::kFastPairBluetoothUuid);
  259. device_->SetError(true);
  260. adapter_->AddMockDevice(std::move(device_));
  261. gatt_service_client_ = FastPairGattServiceClientImpl::Factory::Create(
  262. adapter_->GetDevice(kTestBleDeviceAddress), adapter_.get(),
  263. base::BindRepeating(&::ash::quick_pair::FastPairGattServiceClientTest::
  264. InitializedTestCallback,
  265. weak_ptr_factory_.GetWeakPtr()));
  266. }
  267. void NonFastPairServiceDataSetUp() {
  268. adapter_ = base::MakeRefCounted<FakeBluetoothAdapter>();
  269. device_ = CreateTestBluetoothDevice(adapter_.get(), kNonFastPairUuid);
  270. adapter_->AddMockDevice(std::move(device_));
  271. gatt_service_client_ = FastPairGattServiceClientImpl::Factory::Create(
  272. adapter_->GetDevice(kTestBleDeviceAddress), adapter_.get(),
  273. base::BindRepeating(&::ash::quick_pair::FastPairGattServiceClientTest::
  274. InitializedTestCallback,
  275. weak_ptr_factory_.GetWeakPtr()));
  276. }
  277. void NotifyGattDiscoveryCompleteForService() {
  278. auto gatt_service =
  279. std::make_unique<testing::NiceMock<device::MockBluetoothGattService>>(
  280. CreateTestBluetoothDevice(adapter_.get(),
  281. ash::quick_pair::kFastPairBluetoothUuid)
  282. .get(),
  283. kTestServiceId, ash::quick_pair::kFastPairBluetoothUuid,
  284. /*is_primary=*/true);
  285. gatt_service_ = std::move(gatt_service);
  286. ON_CALL(*(gatt_service_.get()), GetDevice)
  287. .WillByDefault(
  288. testing::Return(adapter_->GetDevice(kTestBleDeviceAddress)));
  289. if (!keybased_char_error_) {
  290. fake_key_based_characteristic_ =
  291. std::make_unique<FakeBluetoothGattCharacteristic>(
  292. gatt_service_.get(), kUUIDString1, kKeyBasedCharacteristicUuid1,
  293. kProperties, kPermissions);
  294. if (keybased_notify_session_error_)
  295. fake_key_based_characteristic_->SetNotifySessionError(true);
  296. if (keybased_notify_session_timeout_) {
  297. fake_key_based_characteristic_->SetNotifySessionTimeout(
  298. true, &task_environment_);
  299. }
  300. if (key_based_write_error_) {
  301. fake_key_based_characteristic_->SetWriteError(true);
  302. }
  303. if (key_based_write_timeout_) {
  304. fake_key_based_characteristic_->SetWriteTimeout(true,
  305. &task_environment_);
  306. }
  307. temp_fake_key_based_characteristic_ =
  308. fake_key_based_characteristic_.get();
  309. gatt_service_->AddMockCharacteristic(
  310. std::move(fake_key_based_characteristic_));
  311. }
  312. if (!passkey_char_error_) {
  313. fake_passkey_characteristic_ =
  314. std::make_unique<FakeBluetoothGattCharacteristic>(
  315. gatt_service_.get(), kUUIDString2, kPasskeyCharacteristicUuid1,
  316. kProperties, kPermissions);
  317. if (passkey_notify_session_error_)
  318. fake_passkey_characteristic_->SetNotifySessionError(true);
  319. if (passkey_notify_session_timeout_) {
  320. fake_passkey_characteristic_->SetNotifySessionTimeout(
  321. true, &task_environment_);
  322. }
  323. if (passkey_write_error_) {
  324. fake_passkey_characteristic_->SetWriteError(true);
  325. }
  326. if (passkey_write_timeout_) {
  327. fake_passkey_characteristic_->SetWriteTimeout(true, &task_environment_);
  328. }
  329. temp_passkey_based_characteristic_ = fake_passkey_characteristic_.get();
  330. gatt_service_->AddMockCharacteristic(
  331. std::move(fake_passkey_characteristic_));
  332. }
  333. auto fake_account_key_characteristic =
  334. std::make_unique<FakeBluetoothGattCharacteristic>(
  335. gatt_service_.get(), kUUIDString3, kAccountKeyCharacteristicUuid1,
  336. kProperties, kPermissions);
  337. if (account_key_write_error_) {
  338. fake_account_key_characteristic->SetWriteError(true);
  339. }
  340. gatt_service_->AddMockCharacteristic(
  341. std::move(fake_account_key_characteristic));
  342. adapter_->NotifyGattDiscoveryCompleteForService(gatt_service_.get());
  343. }
  344. bool ServiceIsSet() {
  345. if (!gatt_service_client_->gatt_service())
  346. return false;
  347. return gatt_service_client_->gatt_service() == gatt_service_.get();
  348. }
  349. void SetPasskeyCharacteristicError(bool passkey_char_error) {
  350. passkey_char_error_ = passkey_char_error;
  351. }
  352. void SetKeybasedCharacteristicError(bool keybased_char_error) {
  353. keybased_char_error_ = keybased_char_error;
  354. }
  355. void SetAccountKeyCharacteristicWriteError(bool account_key_write_error) {
  356. account_key_write_error_ = account_key_write_error;
  357. }
  358. void SetPasskeyNotifySessionError(bool passkey_notify_session_error) {
  359. passkey_notify_session_error_ = passkey_notify_session_error;
  360. }
  361. void SetKeybasedNotifySessionError(bool keybased_notify_session_error) {
  362. keybased_notify_session_error_ = keybased_notify_session_error;
  363. }
  364. void InitializedTestCallback(absl::optional<PairFailure> failure) {
  365. initalized_failure_ = failure;
  366. }
  367. absl::optional<PairFailure> GetInitializedCallbackResult() {
  368. return initalized_failure_;
  369. }
  370. void WriteTestCallback(std::vector<uint8_t> response,
  371. absl::optional<PairFailure> failure) {
  372. write_failure_ = failure;
  373. }
  374. void AccountKeyCallback(
  375. absl::optional<device::BluetoothGattService::GattErrorCode> error) {
  376. gatt_error_ = error;
  377. }
  378. absl::optional<device::BluetoothGattService::GattErrorCode>
  379. GetAccountKeyCallback() {
  380. return gatt_error_;
  381. }
  382. absl::optional<PairFailure> GetWriteCallbackResult() {
  383. return write_failure_;
  384. }
  385. void SetPasskeyNotifySessionTimeout(bool timeout) {
  386. passkey_notify_session_timeout_ = timeout;
  387. }
  388. void SetKeybasedNotifySessionTimeout(bool timeout) {
  389. keybased_notify_session_timeout_ = timeout;
  390. }
  391. void FastForwardTimeByConnetingTimeout() {
  392. task_environment_.FastForwardBy(kConnectingTestTimeout);
  393. }
  394. void WriteRequestToKeyBased() {
  395. gatt_service_client_->WriteRequestAsync(
  396. kMessageType, kFlags, kProviderAddress, kSeekersAddress,
  397. fast_pair_data_encryptor_.get(),
  398. base::BindRepeating(&::ash::quick_pair::FastPairGattServiceClientTest::
  399. WriteTestCallback,
  400. weak_ptr_factory_.GetWeakPtr()));
  401. }
  402. void WriteRequestToPasskey() {
  403. gatt_service_client_->WritePasskeyAsync(
  404. kSeekerPasskey, kPasskey, fast_pair_data_encryptor_.get(),
  405. base::BindRepeating(&::ash::quick_pair::FastPairGattServiceClientTest::
  406. WriteTestCallback,
  407. weak_ptr_factory_.GetWeakPtr()));
  408. }
  409. void WriteAccountKey() {
  410. gatt_service_client_->WriteAccountKey(
  411. kAccountKey, fast_pair_data_encryptor_.get(),
  412. base::BindRepeating(&::ash::quick_pair::FastPairGattServiceClientTest::
  413. AccountKeyCallback,
  414. weak_ptr_factory_.GetWeakPtr()));
  415. }
  416. void TriggerKeyBasedGattChanged() {
  417. adapter_->NotifyGattCharacteristicValueChanged(
  418. temp_fake_key_based_characteristic_);
  419. }
  420. void TriggerPasskeyGattChanged() {
  421. adapter_->NotifyGattCharacteristicValueChanged(
  422. temp_passkey_based_characteristic_);
  423. }
  424. void SetKeyBasedWriteError() { key_based_write_error_ = true; }
  425. void SetPasskeyWriteError() { passkey_write_error_ = true; }
  426. void SetWriteRequestTimeout() { key_based_write_timeout_ = true; }
  427. void SetWritePasskeyTimeout() { passkey_write_timeout_ = true; }
  428. base::HistogramTester& histogram_tester() { return histogram_tester_; }
  429. protected:
  430. base::test::TaskEnvironment task_environment_{
  431. base::test::TaskEnvironment::TimeSource::MOCK_TIME};
  432. base::HistogramTester histogram_tester_;
  433. std::unique_ptr<FastPairGattServiceClient> gatt_service_client_;
  434. private:
  435. // We need temporary pointers to use for write/ready requests because we
  436. // move the unique pointers when we notify the session.
  437. FakeBluetoothGattCharacteristic* temp_fake_key_based_characteristic_;
  438. FakeBluetoothGattCharacteristic* temp_passkey_based_characteristic_;
  439. absl::optional<device::BluetoothGattService::GattErrorCode> gatt_error_ =
  440. absl::nullopt;
  441. bool passkey_char_error_ = false;
  442. bool keybased_char_error_ = false;
  443. bool account_key_write_error_ = false;
  444. bool passkey_notify_session_error_ = false;
  445. bool keybased_notify_session_error_ = false;
  446. bool passkey_notify_session_timeout_ = false;
  447. bool keybased_notify_session_timeout_ = false;
  448. bool key_based_write_error_ = false;
  449. bool key_based_write_timeout_ = false;
  450. bool passkey_write_error_ = false;
  451. bool passkey_write_timeout_ = false;
  452. absl::optional<PairFailure> initalized_failure_;
  453. absl::optional<PairFailure> write_failure_;
  454. scoped_refptr<FakeBluetoothAdapter> adapter_;
  455. std::unique_ptr<FakeBluetoothDevice> device_;
  456. std::unique_ptr<FakeBluetoothGattCharacteristic>
  457. fake_key_based_characteristic_;
  458. std::unique_ptr<FakeFastPairDataEncryptor> fast_pair_data_encryptor_ =
  459. std::make_unique<FakeFastPairDataEncryptor>();
  460. std::unique_ptr<FakeBluetoothGattCharacteristic> fake_passkey_characteristic_;
  461. std::unique_ptr<testing::NiceMock<device::MockBluetoothGattService>>
  462. gatt_service_;
  463. base::WeakPtrFactory<FastPairGattServiceClientTest> weak_ptr_factory_{this};
  464. };
  465. TEST_F(FastPairGattServiceClientTest, GattServiceDiscoveryTimeout) {
  466. histogram_tester().ExpectTotalCount(kTotalGattConnectionTime, 0);
  467. histogram_tester().ExpectTotalCount(kGattConnectionResult, 0);
  468. histogram_tester().ExpectTotalCount(kWriteKeyBasedCharacteristicGattError, 0);
  469. histogram_tester().ExpectTotalCount(kWritePasskeyCharacteristicGattError, 0);
  470. histogram_tester().ExpectTotalCount(kNotifyKeyBasedCharacteristicTime, 0);
  471. histogram_tester().ExpectTotalCount(kNotifyPasskeyCharacteristicTime, 0);
  472. SuccessfulGattConnectionSetUp();
  473. FastForwardTimeByConnetingTimeout();
  474. NotifyGattDiscoveryCompleteForService();
  475. EXPECT_EQ(GetInitializedCallbackResult(),
  476. PairFailure::kGattServiceDiscoveryTimeout);
  477. EXPECT_FALSE(ServiceIsSet());
  478. histogram_tester().ExpectTotalCount(kTotalGattConnectionTime, 1);
  479. histogram_tester().ExpectTotalCount(kGattConnectionResult, 1);
  480. histogram_tester().ExpectTotalCount(kWriteKeyBasedCharacteristicGattError, 0);
  481. histogram_tester().ExpectTotalCount(kNotifyKeyBasedCharacteristicTime, 0);
  482. histogram_tester().ExpectTotalCount(kWritePasskeyCharacteristicGattError, 0);
  483. histogram_tester().ExpectTotalCount(kNotifyPasskeyCharacteristicTime, 0);
  484. }
  485. TEST_F(FastPairGattServiceClientTest, FailedGattConnection) {
  486. histogram_tester().ExpectTotalCount(kTotalGattConnectionTime, 0);
  487. histogram_tester().ExpectTotalCount(kWritePasskeyCharacteristicGattError, 0);
  488. histogram_tester().ExpectTotalCount(kGattConnectionResult, 0);
  489. histogram_tester().ExpectTotalCount(kGattConnectionErrorMetric, 0);
  490. histogram_tester().ExpectTotalCount(kWriteKeyBasedCharacteristicGattError, 0);
  491. histogram_tester().ExpectTotalCount(kNotifyKeyBasedCharacteristicTime, 0);
  492. histogram_tester().ExpectTotalCount(kNotifyPasskeyCharacteristicTime, 0);
  493. FailedGattConnectionSetUp();
  494. EXPECT_EQ(GetInitializedCallbackResult(), PairFailure::kCreateGattConnection);
  495. EXPECT_FALSE(ServiceIsSet());
  496. histogram_tester().ExpectTotalCount(kTotalGattConnectionTime, 0);
  497. histogram_tester().ExpectTotalCount(kGattConnectionResult, 1);
  498. histogram_tester().ExpectTotalCount(kGattConnectionErrorMetric, 1);
  499. histogram_tester().ExpectTotalCount(kWriteKeyBasedCharacteristicGattError, 0);
  500. histogram_tester().ExpectTotalCount(kTotalGattConnectionTime, 0);
  501. histogram_tester().ExpectTotalCount(kNotifyKeyBasedCharacteristicTime, 0);
  502. histogram_tester().ExpectTotalCount(kWritePasskeyCharacteristicGattError, 0);
  503. histogram_tester().ExpectTotalCount(kNotifyPasskeyCharacteristicTime, 0);
  504. }
  505. TEST_F(FastPairGattServiceClientTest, GattConnectionSuccess) {
  506. histogram_tester().ExpectTotalCount(kTotalGattConnectionTime, 0);
  507. histogram_tester().ExpectTotalCount(kGattConnectionResult, 0);
  508. histogram_tester().ExpectTotalCount(kGattConnectionErrorMetric, 0);
  509. histogram_tester().ExpectTotalCount(kWriteKeyBasedCharacteristicGattError, 0);
  510. histogram_tester().ExpectTotalCount(kNotifyKeyBasedCharacteristicTime, 0);
  511. histogram_tester().ExpectTotalCount(kWritePasskeyCharacteristicGattError, 0);
  512. histogram_tester().ExpectTotalCount(kNotifyPasskeyCharacteristicTime, 0);
  513. SuccessfulGattConnectionSetUp();
  514. NotifyGattDiscoveryCompleteForService();
  515. EXPECT_TRUE(gatt_service_client_->IsConnected());
  516. histogram_tester().ExpectTotalCount(kTotalGattConnectionTime, 1);
  517. histogram_tester().ExpectTotalCount(kGattConnectionResult, 1);
  518. histogram_tester().ExpectTotalCount(kGattConnectionErrorMetric, 0);
  519. histogram_tester().ExpectTotalCount(kWriteKeyBasedCharacteristicGattError, 0);
  520. histogram_tester().ExpectTotalCount(kNotifyKeyBasedCharacteristicTime, 0);
  521. histogram_tester().ExpectTotalCount(kWritePasskeyCharacteristicGattError, 0);
  522. histogram_tester().ExpectTotalCount(kNotifyPasskeyCharacteristicTime, 0);
  523. }
  524. TEST_F(FastPairGattServiceClientTest, IgnoreNonFastPairServices) {
  525. NonFastPairServiceDataSetUp();
  526. EXPECT_EQ(GetInitializedCallbackResult(), absl::nullopt);
  527. EXPECT_FALSE(ServiceIsSet());
  528. }
  529. TEST_F(FastPairGattServiceClientTest, FailedKeyBasedCharacteristics) {
  530. histogram_tester().ExpectTotalCount(kNotifyKeyBasedCharacteristicTime, 0);
  531. SetKeybasedCharacteristicError(true);
  532. SuccessfulGattConnectionSetUp();
  533. NotifyGattDiscoveryCompleteForService();
  534. EXPECT_EQ(GetInitializedCallbackResult(),
  535. PairFailure::kKeyBasedPairingCharacteristicDiscovery);
  536. EXPECT_FALSE(ServiceIsSet());
  537. histogram_tester().ExpectTotalCount(kNotifyKeyBasedCharacteristicTime, 0);
  538. }
  539. TEST_F(FastPairGattServiceClientTest, FailedPasskeyCharacteristics) {
  540. SetPasskeyCharacteristicError(true);
  541. SuccessfulGattConnectionSetUp();
  542. NotifyGattDiscoveryCompleteForService();
  543. EXPECT_EQ(GetInitializedCallbackResult(),
  544. PairFailure::kPasskeyCharacteristicDiscovery);
  545. EXPECT_FALSE(ServiceIsSet());
  546. }
  547. TEST_F(FastPairGattServiceClientTest, SuccessfulCharacteristicsStartNotify) {
  548. histogram_tester().ExpectTotalCount(kNotifyKeyBasedCharacteristicTime, 0);
  549. SetKeybasedCharacteristicError(false);
  550. SetPasskeyCharacteristicError(false);
  551. SuccessfulGattConnectionSetUp();
  552. NotifyGattDiscoveryCompleteForService();
  553. EXPECT_EQ(GetInitializedCallbackResult(), absl::nullopt);
  554. EXPECT_TRUE(ServiceIsSet());
  555. histogram_tester().ExpectTotalCount(kNotifyKeyBasedCharacteristicTime, 0);
  556. }
  557. TEST_F(FastPairGattServiceClientTest, StartNotifyPasskeyFailure) {
  558. histogram_tester().ExpectTotalCount(kNotifyPasskeyCharacteristicTime, 0);
  559. SuccessfulGattConnectionSetUp();
  560. SetPasskeyNotifySessionError(true);
  561. NotifyGattDiscoveryCompleteForService();
  562. EXPECT_EQ(GetInitializedCallbackResult(),
  563. PairFailure::kPasskeyCharacteristicNotifySession);
  564. EXPECT_FALSE(ServiceIsSet());
  565. histogram_tester().ExpectTotalCount(kNotifyPasskeyCharacteristicTime, 0);
  566. }
  567. TEST_F(FastPairGattServiceClientTest, StartNotifyKeybasedFailure) {
  568. histogram_tester().ExpectTotalCount(kNotifyKeyBasedCharacteristicTime, 0);
  569. SuccessfulGattConnectionSetUp();
  570. SetKeybasedNotifySessionError(true);
  571. NotifyGattDiscoveryCompleteForService();
  572. EXPECT_EQ(GetInitializedCallbackResult(),
  573. PairFailure::kKeyBasedPairingCharacteristicNotifySession);
  574. EXPECT_FALSE(ServiceIsSet());
  575. histogram_tester().ExpectTotalCount(kNotifyKeyBasedCharacteristicTime, 0);
  576. }
  577. TEST_F(FastPairGattServiceClientTest, PasskeyStartNotifyTimeout) {
  578. histogram_tester().ExpectTotalCount(kNotifyPasskeyCharacteristicTime, 0);
  579. SetPasskeyNotifySessionTimeout(true);
  580. SuccessfulGattConnectionSetUp();
  581. NotifyGattDiscoveryCompleteForService();
  582. EXPECT_EQ(GetInitializedCallbackResult(),
  583. PairFailure::kPasskeyCharacteristicNotifySessionTimeout);
  584. EXPECT_FALSE(ServiceIsSet());
  585. histogram_tester().ExpectTotalCount(kNotifyPasskeyCharacteristicTime, 0);
  586. }
  587. TEST_F(FastPairGattServiceClientTest, KeyBasedStartNotifyTimeout) {
  588. histogram_tester().ExpectTotalCount(kNotifyKeyBasedCharacteristicTime, 0);
  589. SetKeybasedNotifySessionTimeout(true);
  590. SuccessfulGattConnectionSetUp();
  591. NotifyGattDiscoveryCompleteForService();
  592. EXPECT_EQ(GetInitializedCallbackResult(),
  593. PairFailure::kKeyBasedPairingCharacteristicNotifySessionTimeout);
  594. EXPECT_FALSE(ServiceIsSet());
  595. histogram_tester().ExpectTotalCount(kNotifyKeyBasedCharacteristicTime, 0);
  596. }
  597. TEST_F(FastPairGattServiceClientTest, MultipleNotifyTimeout) {
  598. histogram_tester().ExpectTotalCount(kNotifyKeyBasedCharacteristicTime, 0);
  599. SetKeybasedNotifySessionTimeout(true);
  600. SetPasskeyNotifySessionTimeout(true);
  601. SuccessfulGattConnectionSetUp();
  602. NotifyGattDiscoveryCompleteForService();
  603. EXPECT_EQ(GetInitializedCallbackResult(),
  604. PairFailure::kKeyBasedPairingCharacteristicNotifySessionTimeout);
  605. EXPECT_FALSE(ServiceIsSet());
  606. histogram_tester().ExpectTotalCount(kNotifyKeyBasedCharacteristicTime, 0);
  607. }
  608. TEST_F(FastPairGattServiceClientTest, WriteKeyBasedRequest) {
  609. histogram_tester().ExpectTotalCount(kWriteKeyBasedCharacteristicGattError, 0);
  610. histogram_tester().ExpectTotalCount(kNotifyKeyBasedCharacteristicTime, 0);
  611. SuccessfulGattConnectionSetUp();
  612. NotifyGattDiscoveryCompleteForService();
  613. EXPECT_EQ(GetInitializedCallbackResult(), absl::nullopt);
  614. EXPECT_TRUE(ServiceIsSet());
  615. WriteRequestToKeyBased();
  616. TriggerKeyBasedGattChanged();
  617. EXPECT_EQ(GetWriteCallbackResult(), absl::nullopt);
  618. histogram_tester().ExpectTotalCount(kWriteKeyBasedCharacteristicGattError, 0);
  619. histogram_tester().ExpectTotalCount(kNotifyKeyBasedCharacteristicTime, 1);
  620. }
  621. TEST_F(FastPairGattServiceClientTest, WriteKeyBasedRequestError) {
  622. histogram_tester().ExpectTotalCount(kWriteKeyBasedCharacteristicGattError, 0);
  623. SetKeyBasedWriteError();
  624. SuccessfulGattConnectionSetUp();
  625. NotifyGattDiscoveryCompleteForService();
  626. EXPECT_EQ(GetInitializedCallbackResult(), absl::nullopt);
  627. EXPECT_TRUE(ServiceIsSet());
  628. WriteRequestToKeyBased();
  629. TriggerKeyBasedGattChanged();
  630. EXPECT_EQ(GetWriteCallbackResult(),
  631. PairFailure::kKeyBasedPairingCharacteristicWrite);
  632. histogram_tester().ExpectTotalCount(kWriteKeyBasedCharacteristicGattError, 1);
  633. }
  634. TEST_F(FastPairGattServiceClientTest, WriteKeyBasedRequestTimeout) {
  635. SetWriteRequestTimeout();
  636. SuccessfulGattConnectionSetUp();
  637. NotifyGattDiscoveryCompleteForService();
  638. EXPECT_EQ(GetInitializedCallbackResult(), absl::nullopt);
  639. EXPECT_TRUE(ServiceIsSet());
  640. WriteRequestToKeyBased();
  641. TriggerKeyBasedGattChanged();
  642. EXPECT_TRUE(ServiceIsSet());
  643. EXPECT_EQ(GetWriteCallbackResult(),
  644. PairFailure::kKeyBasedPairingResponseTimeout);
  645. }
  646. TEST_F(FastPairGattServiceClientTest, WritePasskeyRequest) {
  647. histogram_tester().ExpectTotalCount(kWritePasskeyCharacteristicGattError, 0);
  648. histogram_tester().ExpectTotalCount(kNotifyPasskeyCharacteristicTime, 0);
  649. SuccessfulGattConnectionSetUp();
  650. NotifyGattDiscoveryCompleteForService();
  651. EXPECT_EQ(GetInitializedCallbackResult(), absl::nullopt);
  652. EXPECT_TRUE(ServiceIsSet());
  653. WriteRequestToPasskey();
  654. TriggerPasskeyGattChanged();
  655. EXPECT_EQ(GetWriteCallbackResult(), absl::nullopt);
  656. histogram_tester().ExpectTotalCount(kWritePasskeyCharacteristicGattError, 0);
  657. histogram_tester().ExpectTotalCount(kNotifyPasskeyCharacteristicTime, 1);
  658. }
  659. TEST_F(FastPairGattServiceClientTest, WritePasskeyRequestError) {
  660. histogram_tester().ExpectTotalCount(kWritePasskeyCharacteristicGattError, 0);
  661. SetPasskeyWriteError();
  662. SuccessfulGattConnectionSetUp();
  663. NotifyGattDiscoveryCompleteForService();
  664. EXPECT_EQ(GetInitializedCallbackResult(), absl::nullopt);
  665. EXPECT_TRUE(ServiceIsSet());
  666. WriteRequestToPasskey();
  667. TriggerKeyBasedGattChanged();
  668. EXPECT_EQ(GetWriteCallbackResult(),
  669. PairFailure::kPasskeyPairingCharacteristicWrite);
  670. histogram_tester().ExpectTotalCount(kWritePasskeyCharacteristicGattError, 1);
  671. }
  672. TEST_F(FastPairGattServiceClientTest, WritePasskeyRequestTimeout) {
  673. histogram_tester().ExpectTotalCount(kWritePasskeyCharacteristicGattError, 0);
  674. SetWritePasskeyTimeout();
  675. SuccessfulGattConnectionSetUp();
  676. NotifyGattDiscoveryCompleteForService();
  677. EXPECT_EQ(GetInitializedCallbackResult(), absl::nullopt);
  678. EXPECT_TRUE(ServiceIsSet());
  679. WriteRequestToPasskey();
  680. TriggerKeyBasedGattChanged();
  681. EXPECT_EQ(GetWriteCallbackResult(), PairFailure::kPasskeyResponseTimeout);
  682. histogram_tester().ExpectTotalCount(kWritePasskeyCharacteristicGattError, 0);
  683. }
  684. TEST_F(FastPairGattServiceClientTest, WriteAccountKey) {
  685. histogram_tester().ExpectTotalCount(kWriteAccountKeyCharacteristicGattError,
  686. 0);
  687. histogram_tester().ExpectTotalCount(kWriteAccountKeyTimeMetric, 0);
  688. SuccessfulGattConnectionSetUp();
  689. NotifyGattDiscoveryCompleteForService();
  690. EXPECT_EQ(GetInitializedCallbackResult(), absl::nullopt);
  691. EXPECT_TRUE(ServiceIsSet());
  692. WriteRequestToKeyBased();
  693. TriggerKeyBasedGattChanged();
  694. EXPECT_EQ(GetWriteCallbackResult(), absl::nullopt);
  695. WriteAccountKey();
  696. EXPECT_EQ(GetAccountKeyCallback(), absl::nullopt);
  697. histogram_tester().ExpectTotalCount(kWriteAccountKeyCharacteristicGattError,
  698. 0);
  699. histogram_tester().ExpectTotalCount(kWriteAccountKeyTimeMetric, 1);
  700. }
  701. TEST_F(FastPairGattServiceClientTest, WriteAccountKeyFailure) {
  702. histogram_tester().ExpectTotalCount(kWriteAccountKeyCharacteristicGattError,
  703. 0);
  704. histogram_tester().ExpectTotalCount(kWriteAccountKeyTimeMetric, 0);
  705. SetAccountKeyCharacteristicWriteError(true);
  706. SuccessfulGattConnectionSetUp();
  707. NotifyGattDiscoveryCompleteForService();
  708. EXPECT_EQ(GetInitializedCallbackResult(), absl::nullopt);
  709. EXPECT_TRUE(ServiceIsSet());
  710. WriteRequestToKeyBased();
  711. TriggerKeyBasedGattChanged();
  712. EXPECT_EQ(GetWriteCallbackResult(), absl::nullopt);
  713. WriteAccountKey();
  714. EXPECT_NE(GetAccountKeyCallback(), absl::nullopt);
  715. histogram_tester().ExpectTotalCount(kWriteAccountKeyCharacteristicGattError,
  716. 1);
  717. histogram_tester().ExpectTotalCount(kWriteAccountKeyTimeMetric, 0);
  718. }
  719. } // namespace quick_pair
  720. } // namespace ash