diff --git a/fastpair/pairing/fastpair/BUILD b/fastpair/pairing/fastpair/BUILD index 7bdf5090..1e35df2f 100644 --- a/fastpair/pairing/fastpair/BUILD +++ b/fastpair/pairing/fastpair/BUILD @@ -50,7 +50,10 @@ cc_test( deps = [ ":pairing", "//fastpair/common", + "//fastpair/crypto", "//fastpair/handshake", + "//fastpair/handshake:test_support", + "//fastpair/proto:fastpair_cc_proto", "//fastpair/repository:test_support", "//internal/base:bluetooth_address", "//internal/platform:comm", diff --git a/fastpair/pairing/fastpair/fast_pair_pairer_impl_test.cc b/fastpair/pairing/fastpair/fast_pair_pairer_impl_test.cc index bb421585..9d97b48a 100644 --- a/fastpair/pairing/fastpair/fast_pair_pairer_impl_test.cc +++ b/fastpair/pairing/fastpair/fast_pair_pairer_impl_test.cc @@ -16,7 +16,6 @@ #include -#include #include #include #include @@ -25,8 +24,6 @@ #include #include -#include "gmock/gmock.h" -#include "protobuf-matchers/protocol-buffer-matchers.h" #include "gtest/gtest.h" #include "absl/functional/any_invocable.h" #include "absl/functional/bind_front.h" @@ -34,22 +31,25 @@ #include "absl/time/time.h" #include "fastpair//handshake/fast_pair_handshake_lookup.h" #include "fastpair/common/account_key.h" -#include "fastpair/common/constant.h" #include "fastpair/common/device_metadata.h" #include "fastpair/common/fast_pair_device.h" #include "fastpair/common/fast_pair_version.h" #include "fastpair/common/protocol.h" +#include "fastpair/crypto/decrypted_passkey.h" +#include "fastpair/crypto/decrypted_response.h" +#include "fastpair/crypto/fast_pair_message_type.h" +#include "fastpair/handshake/fake_fast_pair_data_encryptor.h" +#include "fastpair/handshake/fast_pair_data_encryptor.h" #include "fastpair/handshake/fast_pair_data_encryptor_impl.h" #include "fastpair/handshake/fast_pair_handshake_impl.h" #include "fastpair/pairing/fastpair/fast_pair_pairer.h" -#include "fastpair/repository/fake_fast_pair_repository.h" +#include "fastpair/proto/fastpair_rpcs.proto.h" #include "internal/base/bluetooth_address.h" #include "internal/platform/ble_v2.h" #include "internal/platform/bluetooth_adapter.h" #include "internal/platform/count_down_latch.h" #include "internal/platform/medium_environment.h" #include "internal/platform/single_thread_executor.h" -#include namespace nearby { namespace fastpair { @@ -59,13 +59,11 @@ using Permission = nearby::api::ble_v2::GattCharacteristic::Permission; using ::nearby::api::ble_v2::GattCharacteristic; using DiscoveryCallback = BluetoothClassicMedium::DiscoveryCallback; +constexpr absl::Duration kWaitTimeout = absl::Milliseconds(200); constexpr absl::string_view kMetadataId("718c17"); constexpr absl::string_view kPublicAntiSpoof = "Wuyr48lD3txnUhGiMF1IfzlTwRxxe+wMB1HLzP+" "0wVcljfT3XPoiy1fntlneziyLD5knDVAJSE+RM/zlPRP/Jg=="; -constexpr std::array kRawResponseBytes = { - 0x01, 0x5E, 0x3F, 0x45, 0x61, 0xC3, 0x32, 0x1D, - 0xA0, 0xBA, 0xF0, 0xBB, 0x95, 0x1F, 0xF7, 0xB6}; constexpr Uuid kFastPairServiceUuid(0x0000FE2C00001000, 0x800000805F9B34FB); constexpr Uuid kKeyBasedCharacteristicUuidV2(0xFE2C123483664814, 0x8EB001DE32100BEA); @@ -73,18 +71,64 @@ constexpr Uuid kPasskeyCharacteristicUuidV2(0xFE2C123583664814, 0x8EB001DE32100BEA); constexpr Uuid kAccountKeyCharacteristicUuidV2(0xFE2C123683664814, 0x8EB001DE32100BEA); -constexpr absl::string_view kPasskey("123456"); +constexpr absl::string_view kKeyBasedResponse("keybasedresponse"); +constexpr absl::string_view kPasskeyResponse("passkeyresponse"); constexpr absl::string_view kWrongResponse("wrongresponse"); -constexpr absl::Duration kWaitTimeout = absl::Milliseconds(200); +constexpr absl::string_view kPasskey("123456"); +constexpr std::array salt = {0x08, 0x09, 0x0A, 0x0B, 0x0C, + 0x0D, 0x0E, 0x0F, 0x00}; +} // namespace + +class FastPairFakeDataEncryptorImplFactory + : public FastPairDataEncryptorImpl::Factory { + public: + void CreateInstance( + const FastPairDevice& device, + absl::AnyInvocable)> + on_get_instance_callback) override { + if (!successful_retrieval_) { + std::move(on_get_instance_callback)(nullptr); + return; + } + + auto data_encryptor = std::make_unique(); + data_encryptor_ = data_encryptor.get(); + data_encryptor->SetResponse(response_); + data_encryptor->SetPasskey(passkey_); + std::move(on_get_instance_callback)(std::move(data_encryptor)); + } + + FakeFastPairDataEncryptor* data_encryptor() { return data_encryptor_; } + + void SetFailedRetrieval() { successful_retrieval_ = false; } + + void SetResponse(std::optional response) { + response_ = std::move(response); + } + + void SetPasskey(std::optional passkey) { + passkey_ = std::move(passkey); + } + + private: + FakeFastPairDataEncryptor* data_encryptor_ = nullptr; + bool successful_retrieval_ = true; + std::optional response_; + std::optional passkey_; +}; struct CharacteristicData { // Write result returned to the gatt client. - absl::Status write_result = absl::OkStatus(); + absl::Status write_result; + std::optional notify_response; }; -} // namespace class FastPairPairerImplTest : public testing::Test { public: + FastPairPairerImplTest() { + FastPairDataEncryptorImpl::Factory::SetFactoryForTesting( + &fake_data_encryptor_factory_); + } void SetUp() override { env_.Start(); // Setups seeker device. @@ -106,6 +150,7 @@ class FastPairPairerImplTest : public testing::Test { found_latch.CountDown(); }}); found_latch.Await(); + SetTryToCancelOngoingPairing(false); env_.Sync(); } @@ -116,11 +161,10 @@ class FastPairPairerImplTest : public testing::Test { FastPairHandshakeLookup::GetInstance()->Clear(); mediums_.reset(); device_.reset(); - repository_.reset(); - handshake_ = nullptr; remote_device_ = nullptr; key_based_characteristic_ = std::nullopt; passkey_characteristic_ = std::nullopt; + SetTryToCancelOngoingPairing(false); adapter_provider_->SetStatus(BluetoothAdapter::Status::kDisabled); gatt_server_->Stop(); @@ -135,20 +179,21 @@ class FastPairPairerImplTest : public testing::Test { void CreateMockDevice(DeviceFastPairVersion version, Protocol protocol) { device_ = std::make_unique( kMetadataId, remote_device_->GetMacAddress(), protocol); + proto::GetObservedDeviceResponse response; + auto metadata = response.mutable_device(); + if (version == DeviceFastPairVersion::kHigherThanV1) { + std::string decoded_key; + absl::Base64Unescape(kPublicAntiSpoof, &decoded_key); + CHECK_EQ(decoded_key.length(), kPublicKeyByteSize); + metadata->mutable_anti_spoofing_key_pair()->set_public_key(decoded_key); + } + device_->SetMetadata(DeviceMetadata(response)); if (version == DeviceFastPairVersion::kV1) { device_->SetPublicAddress(remote_device_->GetMacAddress()); } if (protocol == Protocol::kFastPairSubsequentPairing) { device_->SetAccountKey(AccountKey(account_key_)); } - CountDownLatch latch(1); - repository_->GetDeviceMetadata( - kMetadataId, [&](std::optional metadata) { - EXPECT_TRUE(metadata.has_value()); - device_->SetMetadata(std::move(metadata.value())); - latch.CountDown(); - }); - latch.Await(); } void ConfigurePairingContext() { @@ -168,17 +213,11 @@ class FastPairPairerImplTest : public testing::Test { EXPECT_TRUE(FastPairHandshakeLookup::GetInstance()->Create( *device_, *mediums_, [&](FastPairDevice& cb_device, std::optional failure) { - EXPECT_EQ(device_.get(), &cb_device); - EXPECT_EQ(failure, std::nullopt); latch.CountDown(); }, &executor_)); latch.Await(); EXPECT_TRUE(FastPairHandshakeLookup::GetInstance()->Get(device_.get())); - EXPECT_TRUE(handshake_->completed_successfully()); - EXPECT_EQ( - device_->GetPublicAddress().value(), - device::CanonicalizeBluetoothAddress(remote_device_->GetMacAddress())); } std::unique_ptr CreateConnectedHandshake( @@ -189,27 +228,22 @@ class FastPairPairerImplTest : public testing::Test { device, mediums, [&](FastPairDevice& callback_device, std::optional failure) { + EXPECT_EQ(device_.get(), &callback_device); + EXPECT_EQ(failure, std::nullopt); callback(callback_device, failure); latch.CountDown(); }, &executor_); - handshake_ = handshake.get(); latch.Await(); + EXPECT_TRUE(handshake->completed_successfully()); + EXPECT_EQ( + device_->GetPublicAddress().value(), + device::CanonicalizeBluetoothAddress(remote_device_->GetMacAddress())); return handshake; } - // Sets up provider's metadata information. - void SetUpFastPairRepository(DeviceFastPairVersion version) { - repository_ = FakeFastPairRepository::Create( - kMetadataId, version == DeviceFastPairVersion::kHigherThanV1 - ? kPublicAntiSpoof - : ""); - } - // Sets upprovider's gatt_server. - void SetupProviderGattServer( - absl::AnyInvocable trigger_keybase_value_change, - absl::AnyInvocable trigger_passkey_value_change) { + void SetupProviderGattServer() { ble_provider_ = std::make_unique(*adapter_provider_); gatt_server_ = ble_provider_->StartGattServer( /*ServerGattConnectionCallback=*/{ @@ -219,19 +253,26 @@ class FastPairPairerImplTest : public testing::Test { int offset, absl::string_view data, BleV2Medium::ServerGattConnectionCallback:: WriteValueCallback callback) { + MutexLock lock(&mutex_); auto it = characteristics_.find(characteristic); if (it == characteristics_.end()) { callback(absl::NotFoundError("characteristic not found")); return; } - if (characteristic == *key_based_characteristic_) { - trigger_keybase_value_change(); - } else if (characteristic == *passkey_characteristic_) { - trigger_passkey_value_change(); + // Try to cancel an ongoing pairing + if (try_to_cancel_ongoing_pairing_ && + characteristic == *passkey_characteristic_) { + fast_pair_pairer_->CancelPairing(); } callback(it->second.write_result); + if (it->second.notify_response.has_value()) { + auto ignored = gatt_server_->NotifyCharacteristicChanged( + characteristic, false, + ByteArray(*it->second.notify_response)); + } }}); // Insert fast pair related gatt characteristics + MutexLock lock(&mutex_); key_based_characteristic_ = gatt_server_->CreateCharacteristic( kFastPairServiceUuid, kKeyBasedCharacteristicUuidV2, permissions_, properties_); @@ -250,73 +291,33 @@ class FastPairPairerImplTest : public testing::Test { absl::OkStatus(); } - // Triggers provider's gatt_server to response with public address - absl::Status TriggerKeyBasedGattChanged() { - std::unique_ptr fast_pair_data_encryptor_unique_ptr; - FastPairDataEncryptor* fast_pair_data_encryptor_; - if (device_->GetAccountKey().Ok()) { - CountDownLatch latch(1); - FastPairDataEncryptorImpl::Factory::CreateAsync( - *device_, - [&](std::unique_ptr fast_pair_data_encryptor) { - fast_pair_data_encryptor_unique_ptr = - std::move(fast_pair_data_encryptor); + void SetNotifyResponse(GattCharacteristic characteristic, + absl::string_view response) { + MutexLock lock(&mutex_); + CHECK(characteristics_.find(characteristic) != characteristics_.end()); + characteristics_[characteristic].notify_response = response; + } - latch.CountDown(); - }); - latch.Await(); - fast_pair_data_encryptor_ = fast_pair_data_encryptor_unique_ptr.get(); - } else { - fast_pair_data_encryptor_ = handshake_->fast_pair_data_encryptor(); - } - std::array raw_response = kRawResponseBytes; - std::array provider_address_bytes; + void SetDecryptedResponse() { + std::array address_bytes; device::ParseBluetoothAddress( - device_->GetBleAddress(), - absl::MakeSpan(provider_address_bytes.data(), - provider_address_bytes.size())); - std::copy(provider_address_bytes.begin(), provider_address_bytes.end(), - std::begin(raw_response) + 1); - std::array encryptedResponse = - fast_pair_data_encryptor_->EncryptBytes(raw_response); - std::array response; - std::copy(encryptedResponse.begin(), encryptedResponse.end(), - response.begin()); - return gatt_server_->NotifyCharacteristicChanged( - key_based_characteristic_.value(), false, ByteArray(response)); + device::CanonicalizeBluetoothAddress(remote_device_->GetMacAddress()), + absl::MakeSpan(address_bytes.data(), address_bytes.size())); + DecryptedResponse decrypted_response( + FastPairMessageType::kKeyBasedPairingResponse, address_bytes, salt); + fake_data_encryptor_factory_.SetResponse(std::move(decrypted_response)); } - // Triggers provider's gatt_server to response with passkey - // success == true, response with correct passkey, - // otherwise, response with wrong passkey. - absl::Status TriggerPasskeyGattChanged(absl::string_view pin_code, - uint8_t fast_pair_message_type) { - FastPairDataEncryptor* fast_pair_data_encryptor_ = - handshake_->fast_pair_data_encryptor(); - std::array raw_response; - RAND_bytes(raw_response.data(), kAesBlockByteSize); - raw_response[0] = fast_pair_message_type; - uint32_t passkey = 0; - passkey = std::stoi(std::string(pin_code)); + void SetDecryptedPasskey(absl::string_view passkey = kPasskey, + FastPairMessageType message_type = + FastPairMessageType::kProvidersPasskey) { + // Random salt + std::array salt = {0x08, 0x09, 0x0A, 0x08, 0x09, 0x0E, + 0x0A, 0x0C, 0x0D, 0x0E, 0x05, 0x02}; - // Need to convert the uint_32 to uint_8 to use in our data vector. - raw_response[1] = (passkey & 0x00ff0000) >> 16; - raw_response[2] = (passkey & 0x0000ff00) >> 8; - raw_response[3] = passkey & 0x000000ff; - - std::array encryptedResponse = - fast_pair_data_encryptor_->EncryptBytes(raw_response); - std::array response; - std::copy(encryptedResponse.begin(), encryptedResponse.end(), - response.begin()); - return gatt_server_->NotifyCharacteristicChanged( - passkey_characteristic_.value(), false, ByteArray(response)); - } - - absl::Status TriggerPasskeyGattChangedWithWrongResponse() { - return gatt_server_->NotifyCharacteristicChanged( - passkey_characteristic_.value(), false, - ByteArray(std::string(kWrongResponse))); + DecryptedPasskey decrypted_passkey(message_type, + std::stoi(std::string(passkey)), salt); + fake_data_encryptor_factory_.SetPasskey(std::move(decrypted_passkey)); } bool SetPairingResult( @@ -325,15 +326,22 @@ class FastPairPairerImplTest : public testing::Test { } void SetPasskeyCharacteristicsWriteResultToFailure() { + MutexLock lock(&mutex_); auto it = characteristics_.find(*passkey_characteristic_); it->second.write_result = absl::UnknownError("Failed to write account key"); } void SetAccountkeyCharacteristicsWriteResultToFailure() { + MutexLock lock(&mutex_); auto it = characteristics_.find(*accountkey_characteristic_); it->second.write_result = absl::UnknownError("Failed to write account key"); } + void SetTryToCancelOngoingPairing(bool enable) { + MutexLock lock(&mutex_); + try_to_cancel_ongoing_pairing_ = enable; + } + protected: const std::vector account_key_{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0xAA, 0xBB, @@ -342,21 +350,23 @@ class FastPairPairerImplTest : public testing::Test { std::unique_ptr device_; BluetoothDevice* remote_device_ = nullptr; std::unique_ptr fast_pair_pairer_; - + FastPairFakeDataEncryptorImplFactory fake_data_encryptor_factory_; SingleThreadExecutor executor_; + std::optional key_based_characteristic_; + std::optional passkey_characteristic_; + std::optional accountkey_characteristic_; + bool try_to_cancel_ongoing_pairing_ ABSL_GUARDED_BY(mutex_); private: MediumEnvironment& env_{MediumEnvironment::Instance()}; - std::unique_ptr repository_; + Mutex mutex_; std::unique_ptr bt_provider_; std::unique_ptr adapter_provider_; std::unique_ptr gatt_server_; std::unique_ptr ble_provider_; - FastPairHandshake* handshake_ = nullptr; - std::optional key_based_characteristic_; - std::optional passkey_characteristic_; - std::optional accountkey_characteristic_; - absl::flat_hash_map characteristics_; + + absl::flat_hash_map characteristics_ + ABSL_GUARDED_BY(mutex_); Property properties_ = Property::kWrite | Property::kNotify; Permission permissions_ = Permission::kWrite; }; @@ -364,30 +374,22 @@ class FastPairPairerImplTest : public testing::Test { TEST_F(FastPairPairerImplTest, SuccessInitialPairingWithDeviceVersionHigherThanV1) { ConfigurePairingContext(); - - bool triggered_keybase_value_change = false; - bool triggered_passkey_value_change = false; - SetUpFastPairRepository(DeviceFastPairVersion::kHigherThanV1); + SetPairingResult(std::nullopt); CreateMockDevice(DeviceFastPairVersion::kHigherThanV1, Protocol::kFastPairInitialPairing); - SetupProviderGattServer( - [&]() { - triggered_keybase_value_change = true; - EXPECT_OK(TriggerKeyBasedGattChanged()); - }, - [&]() { - triggered_passkey_value_change = true; - EXPECT_OK(TriggerPasskeyGattChanged(kPasskey, kProviderPasskeyType)); - }); + SetupProviderGattServer(); + SetNotifyResponse(*key_based_characteristic_, kKeyBasedResponse); + SetNotifyResponse(*passkey_characteristic_, kPasskeyResponse); + SetDecryptedResponse(); + SetDecryptedPasskey(); CreateFastPairHandshakeInstanceForDevice(); - SetPairingResult(std::nullopt); + CountDownLatch paired_latch(1); CountDownLatch complete_latch(1); CountDownLatch failure_latch(1); CountDownLatch account_failure_latch(1); EXPECT_FALSE(device_->GetAccountKey().Ok()); - fast_pair_pairer_ = FastPairPairerImpl::Factory::Create( *device_, *mediums_, &executor_, [&](FastPairDevice& cb_device) { paired_latch.CountDown(); }, @@ -407,36 +409,22 @@ TEST_F(FastPairPairerImplTest, EXPECT_FALSE(account_failure_latch.Await(kWaitTimeout).result()); complete_latch.Await(); - EXPECT_TRUE(triggered_keybase_value_change); - EXPECT_TRUE(triggered_passkey_value_change); EXPECT_TRUE(fast_pair_pairer_->IsPaired()); EXPECT_TRUE(device_->GetAccountKey().Ok()); } TEST_F(FastPairPairerImplTest, SuccessInitialPairingWithDeviceV1) { ConfigurePairingContext(); - bool triggered_keybase_value_change = false; - bool triggered_passkey_value_change = false; - SetUpFastPairRepository(DeviceFastPairVersion::kV1); + SetPairingResult(std::nullopt); CreateMockDevice(DeviceFastPairVersion::kV1, Protocol::kFastPairInitialPairing); - SetupProviderGattServer( - [&]() { - triggered_keybase_value_change = true; - EXPECT_OK(TriggerKeyBasedGattChanged()); - }, - [&]() { - triggered_passkey_value_change = true; - EXPECT_OK(TriggerPasskeyGattChanged(kPasskey, kProviderPasskeyType)); - }); - SetPairingResult(std::nullopt); + SetupProviderGattServer(); + CountDownLatch paired_latch(1); CountDownLatch complete_latch(1); CountDownLatch failure_latch(1); CountDownLatch account_failure_latch(1); - EXPECT_FALSE(device_->GetAccountKey().Ok()); - fast_pair_pairer_ = FastPairPairerImpl::Factory::Create( *device_, *mediums_, &executor_, [&](FastPairDevice& cb_device) { paired_latch.CountDown(); }, @@ -455,30 +443,21 @@ TEST_F(FastPairPairerImplTest, SuccessInitialPairingWithDeviceV1) { EXPECT_FALSE(failure_latch.Await(kWaitTimeout).result()); EXPECT_FALSE(account_failure_latch.Await(kWaitTimeout).result()); complete_latch.Await(); - EXPECT_FALSE(triggered_keybase_value_change); - EXPECT_FALSE(triggered_passkey_value_change); EXPECT_TRUE(fast_pair_pairer_->IsPaired()); - EXPECT_FALSE(device_->GetAccountKey().Ok()); } TEST_F(FastPairPairerImplTest, SuccessSubsequentPairingWithDevice) { ConfigurePairingContext(); - bool triggered_keybase_value_change = false; - bool triggered_passkey_value_change = false; - SetUpFastPairRepository(DeviceFastPairVersion::kHigherThanV1); + SetPairingResult(std::nullopt); CreateMockDevice(DeviceFastPairVersion::kHigherThanV1, Protocol::kFastPairSubsequentPairing); - SetupProviderGattServer( - [&]() { - triggered_keybase_value_change = true; - EXPECT_OK(TriggerKeyBasedGattChanged()); - }, - [&]() { - triggered_passkey_value_change = true; - EXPECT_OK(TriggerPasskeyGattChanged(kPasskey, kProviderPasskeyType)); - }); + SetupProviderGattServer(); + SetNotifyResponse(*key_based_characteristic_, kKeyBasedResponse); + SetNotifyResponse(*passkey_characteristic_, kPasskeyResponse); + SetDecryptedResponse(); + SetDecryptedPasskey(); CreateFastPairHandshakeInstanceForDevice(); - SetPairingResult(std::nullopt); + CountDownLatch paired_latch(1); CountDownLatch complete_latch(1); CountDownLatch failure_latch(1); @@ -502,30 +481,19 @@ TEST_F(FastPairPairerImplTest, SuccessSubsequentPairingWithDevice) { EXPECT_FALSE(failure_latch.Await(kWaitTimeout).result()); EXPECT_FALSE(account_failure_latch.Await(kWaitTimeout).result()); complete_latch.Await(); - - EXPECT_TRUE(triggered_keybase_value_change); - EXPECT_TRUE(triggered_passkey_value_change); EXPECT_TRUE(fast_pair_pairer_->IsPaired()); } TEST_F(FastPairPairerImplTest, SuccessRetroactivePairingWithDevice) { ConfigurePairingContext(); - bool triggered_keybase_value_change = false; - bool triggered_passkey_value_change = false; - SetUpFastPairRepository(DeviceFastPairVersion::kHigherThanV1); + SetPairingResult(std::nullopt); CreateMockDevice(DeviceFastPairVersion::kHigherThanV1, Protocol::kFastPairRetroactivePairing); - SetupProviderGattServer( - [&]() { - triggered_keybase_value_change = true; - EXPECT_OK(TriggerKeyBasedGattChanged()); - }, - [&]() { - triggered_passkey_value_change = true; - EXPECT_OK(TriggerPasskeyGattChanged(kPasskey, kProviderPasskeyType)); - }); + SetupProviderGattServer(); + SetNotifyResponse(*key_based_characteristic_, kKeyBasedResponse); + SetDecryptedResponse(); CreateFastPairHandshakeInstanceForDevice(); - SetPairingResult(std::nullopt); + CountDownLatch paired_latch(1); CountDownLatch complete_latch(1); CountDownLatch failure_latch(1); @@ -551,28 +519,17 @@ TEST_F(FastPairPairerImplTest, SuccessRetroactivePairingWithDevice) { EXPECT_FALSE(failure_latch.Await(kWaitTimeout).result()); EXPECT_FALSE(account_failure_latch.Await(kWaitTimeout).result()); complete_latch.Await(); - - EXPECT_TRUE(triggered_keybase_value_change); - EXPECT_FALSE(triggered_passkey_value_change); EXPECT_TRUE(device_->GetAccountKey().Ok()); } TEST_F(FastPairPairerImplTest, FailedToUnPair) { - bool triggered_keybase_value_change = false; - bool triggered_passkey_value_change = false; - SetUpFastPairRepository(DeviceFastPairVersion::kHigherThanV1); CreateMockDevice(DeviceFastPairVersion::kHigherThanV1, Protocol::kFastPairInitialPairing); - SetupProviderGattServer( - [&]() { - triggered_keybase_value_change = true; - EXPECT_OK(TriggerKeyBasedGattChanged()); - }, - [&]() { - triggered_passkey_value_change = true; - EXPECT_OK(TriggerPasskeyGattChanged(kPasskey, kProviderPasskeyType)); - }); + SetupProviderGattServer(); + SetNotifyResponse(*key_based_characteristic_, kKeyBasedResponse); + SetDecryptedResponse(); CreateFastPairHandshakeInstanceForDevice(); + CountDownLatch paired_latch(1); CountDownLatch complete_latch(1); CountDownLatch failure_latch(1); @@ -599,30 +556,22 @@ TEST_F(FastPairPairerImplTest, FailedToUnPair) { failure_latch.Await(); EXPECT_FALSE(complete_latch.Await(kWaitTimeout).result()); EXPECT_FALSE(account_failure_latch.Await(kWaitTimeout).result()); - EXPECT_TRUE(triggered_keybase_value_change); - EXPECT_FALSE(triggered_passkey_value_change); EXPECT_FALSE(fast_pair_pairer_->IsPaired()); EXPECT_FALSE(device_->GetAccountKey().Ok()); } TEST_F(FastPairPairerImplTest, FailedToPairingWithAuthTimeout) { ConfigurePairingContext(); - bool triggered_keybase_value_change = false; - bool triggered_passkey_value_change = false; - SetUpFastPairRepository(DeviceFastPairVersion::kHigherThanV1); + SetPairingResult(api::BluetoothPairingCallback::PairingError::kAuthTimeout); CreateMockDevice(DeviceFastPairVersion::kHigherThanV1, Protocol::kFastPairInitialPairing); - SetupProviderGattServer( - [&]() { - triggered_keybase_value_change = true; - EXPECT_OK(TriggerKeyBasedGattChanged()); - }, - [&]() { - triggered_passkey_value_change = true; - EXPECT_OK(TriggerPasskeyGattChanged(kPasskey, kProviderPasskeyType)); - }); + SetupProviderGattServer(); + SetNotifyResponse(*key_based_characteristic_, kKeyBasedResponse); + SetNotifyResponse(*passkey_characteristic_, kPasskeyResponse); + SetDecryptedResponse(); + SetDecryptedPasskey(); CreateFastPairHandshakeInstanceForDevice(); - SetPairingResult(api::BluetoothPairingCallback::PairingError::kAuthTimeout); + CountDownLatch paired_latch(1); CountDownLatch complete_latch(1); CountDownLatch failure_latch(1); @@ -649,27 +598,20 @@ TEST_F(FastPairPairerImplTest, FailedToPairingWithAuthTimeout) { failure_latch.Await(); EXPECT_FALSE(complete_latch.Await(kWaitTimeout).result()); EXPECT_FALSE(account_failure_latch.Await(kWaitTimeout).result()); - EXPECT_TRUE(triggered_keybase_value_change); - EXPECT_TRUE(triggered_passkey_value_change); EXPECT_FALSE(fast_pair_pairer_->IsPaired()); EXPECT_FALSE(device_->GetAccountKey().Ok()); } TEST_F(FastPairPairerImplTest, NoPasskeyResponse) { ConfigurePairingContext(); - bool triggered_keybase_value_change = false; - bool triggered_passkey_value_change = false; - SetUpFastPairRepository(DeviceFastPairVersion::kHigherThanV1); + SetPairingResult(std::nullopt); CreateMockDevice(DeviceFastPairVersion::kHigherThanV1, Protocol::kFastPairInitialPairing); - SetupProviderGattServer( - [&]() { - triggered_keybase_value_change = true; - EXPECT_OK(TriggerKeyBasedGattChanged()); - }, - [&]() { triggered_passkey_value_change = true; }); + SetupProviderGattServer(); + SetNotifyResponse(*key_based_characteristic_, kKeyBasedResponse); + SetDecryptedResponse(); CreateFastPairHandshakeInstanceForDevice(); - SetPairingResult(std::nullopt); + CountDownLatch paired_latch(1); CountDownLatch complete_latch(1); CountDownLatch failure_latch(1); @@ -696,30 +638,22 @@ TEST_F(FastPairPairerImplTest, NoPasskeyResponse) { failure_latch.Await(); EXPECT_FALSE(complete_latch.Await(kWaitTimeout).result()); EXPECT_FALSE(account_failure_latch.Await(kWaitTimeout).result()); - EXPECT_TRUE(triggered_keybase_value_change); - EXPECT_TRUE(triggered_passkey_value_change); EXPECT_FALSE(fast_pair_pairer_->IsPaired()); EXPECT_FALSE(device_->GetAccountKey().Ok()); } TEST_F(FastPairPairerImplTest, PasskeyMismatch) { ConfigurePairingContext(); - bool triggered_keybase_value_change = false; - bool triggered_passkey_value_change = false; - SetUpFastPairRepository(DeviceFastPairVersion::kHigherThanV1); + SetPairingResult(std::nullopt); CreateMockDevice(DeviceFastPairVersion::kHigherThanV1, Protocol::kFastPairInitialPairing); - SetupProviderGattServer( - [&]() { - triggered_keybase_value_change = true; - EXPECT_OK(TriggerKeyBasedGattChanged()); - }, - [&]() { - triggered_passkey_value_change = true; - EXPECT_OK(TriggerPasskeyGattChanged("654321", kProviderPasskeyType)); - }); + SetupProviderGattServer(); + SetNotifyResponse(*key_based_characteristic_, kKeyBasedResponse); + SetNotifyResponse(*passkey_characteristic_, kPasskeyResponse); + SetDecryptedResponse(); + SetDecryptedPasskey("654321"); CreateFastPairHandshakeInstanceForDevice(); - SetPairingResult(std::nullopt); + CountDownLatch paired_latch(1); CountDownLatch complete_latch(1); CountDownLatch failure_latch(1); @@ -746,29 +680,21 @@ TEST_F(FastPairPairerImplTest, PasskeyMismatch) { failure_latch.Await(); EXPECT_FALSE(complete_latch.Await(kWaitTimeout).result()); EXPECT_FALSE(account_failure_latch.Await(kWaitTimeout).result()); - EXPECT_TRUE(triggered_keybase_value_change); - EXPECT_TRUE(triggered_passkey_value_change); EXPECT_FALSE(fast_pair_pairer_->IsPaired()); EXPECT_FALSE(device_->GetAccountKey().Ok()); } TEST_F(FastPairPairerImplTest, ReceiveWithWrongPasskeyResponse) { ConfigurePairingContext(); - bool triggered_keybase_value_change = false; - bool triggered_passkey_value_change = false; - SetUpFastPairRepository(DeviceFastPairVersion::kHigherThanV1); + SetPairingResult(std::nullopt); CreateMockDevice(DeviceFastPairVersion::kHigherThanV1, Protocol::kFastPairInitialPairing); - SetupProviderGattServer( - [&]() { - triggered_keybase_value_change = true; - EXPECT_OK(TriggerKeyBasedGattChanged()); - }, - [&]() { - triggered_passkey_value_change = true; - EXPECT_OK(TriggerPasskeyGattChangedWithWrongResponse()); - }); + SetupProviderGattServer(); + SetNotifyResponse(*key_based_characteristic_, kKeyBasedResponse); + SetNotifyResponse(*passkey_characteristic_, kWrongResponse); + SetDecryptedResponse(); CreateFastPairHandshakeInstanceForDevice(); + SetPairingResult(std::nullopt); CountDownLatch paired_latch(1); CountDownLatch complete_latch(1); @@ -796,29 +722,22 @@ TEST_F(FastPairPairerImplTest, ReceiveWithWrongPasskeyResponse) { failure_latch.Await(); EXPECT_FALSE(complete_latch.Await(kWaitTimeout).result()); EXPECT_FALSE(account_failure_latch.Await(kWaitTimeout).result()); - EXPECT_TRUE(triggered_keybase_value_change); - EXPECT_TRUE(triggered_passkey_value_change); EXPECT_FALSE(fast_pair_pairer_->IsPaired()); EXPECT_FALSE(device_->GetAccountKey().Ok()); } TEST_F(FastPairPairerImplTest, ReceiveWithWrongPasskeyMessageType) { ConfigurePairingContext(); - bool triggered_keybase_value_change = false; - bool triggered_passkey_value_change = false; - SetUpFastPairRepository(DeviceFastPairVersion::kHigherThanV1); + SetPairingResult(std::nullopt); CreateMockDevice(DeviceFastPairVersion::kHigherThanV1, Protocol::kFastPairInitialPairing); - SetupProviderGattServer( - [&]() { - triggered_keybase_value_change = true; - EXPECT_OK(TriggerKeyBasedGattChanged()); - }, - [&]() { - triggered_passkey_value_change = true; - EXPECT_OK(TriggerPasskeyGattChanged(kPasskey, kSeekerPasskeyType)); - }); + SetupProviderGattServer(); + SetNotifyResponse(*key_based_characteristic_, kKeyBasedResponse); + SetNotifyResponse(*passkey_characteristic_, kPasskeyResponse); + SetDecryptedResponse(); + SetDecryptedPasskey(kPasskey, FastPairMessageType::kSeekersPasskey); CreateFastPairHandshakeInstanceForDevice(); + SetPairingResult(std::nullopt); CountDownLatch paired_latch(1); CountDownLatch complete_latch(1); @@ -846,8 +765,6 @@ TEST_F(FastPairPairerImplTest, ReceiveWithWrongPasskeyMessageType) { failure_latch.Await(); EXPECT_FALSE(complete_latch.Await(kWaitTimeout).result()); EXPECT_FALSE(account_failure_latch.Await(kWaitTimeout).result()); - EXPECT_TRUE(triggered_keybase_value_change); - EXPECT_TRUE(triggered_passkey_value_change); EXPECT_FALSE(fast_pair_pairer_->IsPaired()); EXPECT_FALSE(device_->GetAccountKey().Ok()); } @@ -855,23 +772,17 @@ TEST_F(FastPairPairerImplTest, ReceiveWithWrongPasskeyMessageType) { TEST_F(FastPairPairerImplTest, SuccessPairingWithDeviceButFailedToWriteAccountkey) { ConfigurePairingContext(); - bool triggered_keybase_value_change = false; - bool triggered_passkey_value_change = false; - SetUpFastPairRepository(DeviceFastPairVersion::kHigherThanV1); + SetPairingResult(std::nullopt); CreateMockDevice(DeviceFastPairVersion::kHigherThanV1, Protocol::kFastPairInitialPairing); - SetupProviderGattServer( - [&]() { - triggered_keybase_value_change = true; - EXPECT_OK(TriggerKeyBasedGattChanged()); - }, - [&]() { - triggered_passkey_value_change = true; - EXPECT_OK(TriggerPasskeyGattChanged(kPasskey, kProviderPasskeyType)); - }); + SetupProviderGattServer(); + SetNotifyResponse(*key_based_characteristic_, kKeyBasedResponse); + SetNotifyResponse(*passkey_characteristic_, kPasskeyResponse); + SetDecryptedResponse(); + SetDecryptedPasskey(); CreateFastPairHandshakeInstanceForDevice(); - SetPairingResult(std::nullopt); SetAccountkeyCharacteristicsWriteResultToFailure(); + CountDownLatch paired_latch(1); CountDownLatch complete_latch(1); CountDownLatch failure_latch(1); @@ -897,30 +808,21 @@ TEST_F(FastPairPairerImplTest, EXPECT_FALSE(failure_latch.Await(kWaitTimeout).result()); EXPECT_FALSE(complete_latch.Await(kWaitTimeout).result()); account_failure_latch.Await(); - EXPECT_TRUE(triggered_keybase_value_change); - EXPECT_TRUE(triggered_passkey_value_change); EXPECT_TRUE(fast_pair_pairer_->IsPaired()); EXPECT_FALSE(device_->GetAccountKey().Ok()); } TEST_F(FastPairPairerImplTest, TestCancelPairing) { ConfigurePairingContext(); - bool triggered_keybase_value_change = false; - bool triggered_passkey_value_change = false; - SetUpFastPairRepository(DeviceFastPairVersion::kHigherThanV1); + SetPairingResult(std::nullopt); CreateMockDevice(DeviceFastPairVersion::kHigherThanV1, Protocol::kFastPairInitialPairing); - SetupProviderGattServer( - [&]() { - triggered_keybase_value_change = true; - EXPECT_OK(TriggerKeyBasedGattChanged()); - }, - [&]() { - triggered_passkey_value_change = true; - fast_pair_pairer_->CancelPairing(); - }); + SetupProviderGattServer(); + SetNotifyResponse(*key_based_characteristic_, kKeyBasedResponse); + SetDecryptedResponse(); CreateFastPairHandshakeInstanceForDevice(); - SetPairingResult(std::nullopt); + SetTryToCancelOngoingPairing(true); + CountDownLatch paired_latch(1); CountDownLatch complete_latch(1); CountDownLatch failure_latch(1); @@ -944,9 +846,6 @@ TEST_F(FastPairPairerImplTest, TestCancelPairing) { failure_latch.Await(); EXPECT_FALSE(account_failure_latch.Await(kWaitTimeout).result()); EXPECT_FALSE(complete_latch.Await(kWaitTimeout).result()); - - EXPECT_TRUE(triggered_keybase_value_change); - EXPECT_TRUE(triggered_passkey_value_change); EXPECT_FALSE(device_->GetAccountKey().Ok()); } } // namespace fastpair