diff --git a/fastpair/pairing/BUILD b/fastpair/pairing/BUILD index 9fa08255..44cb524c 100644 --- a/fastpair/pairing/BUILD +++ b/fastpair/pairing/BUILD @@ -51,9 +51,12 @@ cc_test( deps = [ ":pairing", "//fastpair/common", + "//fastpair/crypto", "//fastpair/handshake", + "//fastpair/handshake:test_support", "//fastpair/internal/mediums", "//fastpair/pairing/fastpair:pairing", + "//fastpair/proto:fastpair_cc_proto", "//fastpair/repository:test_support", "//internal/base:bluetooth_address", "//internal/platform:comm", diff --git a/fastpair/pairing/pairer_broker_impl_test.cc b/fastpair/pairing/pairer_broker_impl_test.cc index 1e7c352a..313d21e5 100644 --- a/fastpair/pairing/pairer_broker_impl_test.cc +++ b/fastpair/pairing/pairer_broker_impl_test.cc @@ -14,7 +14,6 @@ #include "fastpair/pairing/pairer_broker_impl.h" -#include #include #include #include @@ -22,23 +21,24 @@ #include #include -#include "gmock/gmock.h" -#include "protobuf-matchers/protocol-buffer-matchers.h" #include "gtest/gtest.h" #include "absl/functional/bind_front.h" #include "fastpair//handshake/fast_pair_handshake_lookup.h" #include "fastpair/common/pair_failure.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/handshake/fast_pair_handshake_lookup.h" #include "fastpair/internal/mediums/mediums.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/count_down_latch.h" #include "internal/platform/medium_environment.h" -#include namespace nearby { namespace fastpair { @@ -52,9 +52,6 @@ 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); @@ -62,14 +59,57 @@ constexpr Uuid kPasskeyCharacteristicUuidV2(0xFE2C123583664814, 0x8EB001DE32100BEA); constexpr Uuid kAccountKeyCharacteristicUuidV2(0xFE2C123683664814, 0x8EB001DE32100BEA); +constexpr absl::string_view kKeyBasedResponse("keybasedresponse"); +constexpr absl::string_view kPasskeyResponse("passkeyresponse"); constexpr absl::string_view kPasskey("123456"); -constexpr absl::string_view kWrongResponse("wrongresponse"); +constexpr std::array salt = {0x08, 0x09, 0x0A, 0x0B, 0x0C, + 0x0D, 0x0E, 0x0F, 0x00}; constexpr absl::Duration kWaitTimeout = absl::Milliseconds(200); +} // 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 PairerBrokerObserver : public PairerBroker::Observer { public: @@ -129,6 +169,11 @@ class PairerBrokerObserver : public PairerBroker::Observer { class PairerBrokerImplTest : public testing::Test { public: + PairerBrokerImplTest() { + FastPairDataEncryptorImpl::Factory::SetFactoryForTesting( + &fake_data_encryptor_factory_); + } + void SetUp() override { env_.Start(); // Setups seeker device. @@ -160,8 +205,6 @@ class PairerBrokerImplTest : 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; @@ -179,20 +222,21 @@ class PairerBrokerImplTest : 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() { @@ -212,17 +256,11 @@ class PairerBrokerImplTest : 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( @@ -233,27 +271,22 @@ class PairerBrokerImplTest : 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=*/{ @@ -263,19 +296,22 @@ class PairerBrokerImplTest : 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(); - } 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_); @@ -294,101 +330,48 @@ class PairerBrokerImplTest : 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); - - 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; - 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)); - } - - // 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) { - 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); - - 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; - RAND_bytes(raw_response.data(), kAesBlockByteSize); - raw_response[0] = fast_pair_message_type; - uint32_t passkey = 0; - passkey = std::stoi(std::string(pin_code)); - - // 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))); - } - bool SetPairingResult( std::optional error) { return env_.SetPairingResult(&remote_device_->GetImpl(), error); } + void SetNotifyResponse(GattCharacteristic characteristic, + absl::string_view response) { + MutexLock lock(&mutex_); + CHECK(characteristics_.find(characteristic) != characteristics_.end()); + characteristics_[characteristic].notify_response = response; + } + + void SetDecryptedResponse() { + std::array address_bytes; + device::ParseBluetoothAddress( + 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)); + } + + 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}; + + DecryptedPasskey decrypted_passkey(message_type, + std::stoi(std::string(passkey)), salt); + fake_data_encryptor_factory_.SetPasskey(std::move(decrypted_passkey)); + } + 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"); } @@ -402,46 +385,35 @@ class PairerBrokerImplTest : public testing::Test { BluetoothDevice* remote_device_ = nullptr; SingleThreadExecutor executor_; std::unique_ptr pairer_broker_; + std::optional key_based_characteristic_; + std::optional passkey_characteristic_; + std::optional accountkey_characteristic_; + FastPairFakeDataEncryptorImplFactory fake_data_encryptor_factory_; 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; }; TEST_F(PairerBrokerImplTest, 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 device_paired_latch(1); CountDownLatch account_key_writed_latch(1); CountDownLatch pairing_completed_latch(1); CountDownLatch pairing_failure_latch(1); - EXPECT_FALSE(device_->GetAccountKey().Ok()); - pairer_broker_ = std::make_unique(*mediums_, &executor_); PairerBrokerObserver pairer_broker_observer( pairer_broker_.get(), &device_paired_latch, &account_key_writed_latch, @@ -452,35 +424,26 @@ TEST_F(PairerBrokerImplTest, SuccessInitialPairingWithDeviceV1) { pairing_completed_latch.Await(); EXPECT_FALSE(account_key_writed_latch.Await(kWaitTimeout).result()); EXPECT_FALSE(pairing_failure_latch.Await(kWaitTimeout).result()); - EXPECT_FALSE(triggered_keybase_value_change); - EXPECT_FALSE(triggered_passkey_value_change); - EXPECT_FALSE(device_->GetAccountKey().Ok()); } TEST_F(PairerBrokerImplTest, SuccessInitialPairingWithDevice) { - bool triggered_keybase_value_change = false; - bool triggered_passkey_value_change = false; + ConfigurePairingContext(); + SetPairingResult(std::nullopt); + CreateMockDevice(DeviceFastPairVersion::kHigherThanV1, + Protocol::kFastPairInitialPairing); + SetupProviderGattServer(); + SetNotifyResponse(*key_based_characteristic_, kKeyBasedResponse); + SetNotifyResponse(*passkey_characteristic_, kPasskeyResponse); + SetDecryptedResponse(); + SetDecryptedPasskey(); + CreateFastPairHandshakeInstanceForDevice(); + + EXPECT_FALSE(device_->GetAccountKey().Ok()); + CountDownLatch device_paired_latch(1); CountDownLatch account_key_writed_latch(1); CountDownLatch pairing_completed_latch(1); CountDownLatch pairing_failure_latch(1); - ConfigurePairingContext(); - 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)); - }); - CreateFastPairHandshakeInstanceForDevice(); - SetPairingResult(std::nullopt); - - EXPECT_FALSE(device_->GetAccountKey().Ok()); pairer_broker_ = std::make_unique(*mediums_, &executor_); PairerBrokerObserver pairer_broker_observer( @@ -492,8 +455,6 @@ TEST_F(PairerBrokerImplTest, SuccessInitialPairingWithDevice) { pairing_completed_latch.Await(); account_key_writed_latch.Await(); EXPECT_FALSE(pairing_failure_latch.Await(kWaitTimeout).result()); - EXPECT_TRUE(triggered_keybase_value_change); - EXPECT_TRUE(triggered_passkey_value_change); EXPECT_TRUE(device_->GetAccountKey().Ok()); pairer_broker_->StopPairing(); @@ -501,70 +462,48 @@ TEST_F(PairerBrokerImplTest, SuccessInitialPairingWithDevice) { } TEST_F(PairerBrokerImplTest, SuccessSubsequentPairingWithDevice) { - bool triggered_keybase_value_change = false; - bool triggered_passkey_value_change = false; + ConfigurePairingContext(); + SetPairingResult(std::nullopt); + CreateMockDevice(DeviceFastPairVersion::kHigherThanV1, + Protocol::kFastPairSubsequentPairing); + SetupProviderGattServer(); + SetNotifyResponse(*key_based_characteristic_, kKeyBasedResponse); + SetNotifyResponse(*passkey_characteristic_, kPasskeyResponse); + SetDecryptedResponse(); + SetDecryptedPasskey(); + CountDownLatch device_paired_latch(1); CountDownLatch account_key_writed_latch(1); CountDownLatch pairing_completed_latch(1); CountDownLatch pairing_failure_latch(1); - ConfigurePairingContext(); - SetUpFastPairRepository(DeviceFastPairVersion::kHigherThanV1); - CreateMockDevice(DeviceFastPairVersion::kHigherThanV1, - Protocol::kFastPairSubsequentPairing); + pairer_broker_ = std::make_unique(*mediums_, &executor_); PairerBrokerObserver pairer_broker_observer( pairer_broker_.get(), &device_paired_latch, &account_key_writed_latch, &pairing_completed_latch, &pairing_failure_latch); - SetupProviderGattServer( - [&]() { - triggered_keybase_value_change = true; - EXPECT_OK(TriggerKeyBasedGattChanged()); - }, - [&]() { - triggered_passkey_value_change = true; - EXPECT_OK(TriggerPasskeyGattChanged(kPasskey, kProviderPasskeyType)); - }); - SetPairingResult(std::nullopt); - pairer_broker_->PairDevice(*device_); - // TODO(qinwangz): The logic in `pairer_broker_->IsPairing()` may return false - // after a call to `PairDevice()`. Please fix. - // executor_.Execute([&]() { - // // Test pairing duplicate devices. - // EXPECT_TRUE(pairer_broker_->IsPairing()); - // pairer_broker_->PairDevice(*device_); - // }); + device_paired_latch.Await(); pairing_completed_latch.Await(); EXPECT_FALSE(account_key_writed_latch.Await(kWaitTimeout).result()); EXPECT_FALSE(pairing_failure_latch.Await(kWaitTimeout).result()); EXPECT_TRUE(pairer_broker_observer.handshake_completed_); - EXPECT_TRUE(triggered_keybase_value_change); - EXPECT_TRUE(triggered_passkey_value_change); } TEST_F(PairerBrokerImplTest, 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 device_paired_latch(1); CountDownLatch account_key_writed_latch(1); CountDownLatch pairing_completed_latch(1); CountDownLatch pairing_failure_latch(1); + EXPECT_FALSE(device_->GetAccountKey().Ok()); pairer_broker_ = std::make_unique(*mediums_, &executor_); @@ -576,68 +515,53 @@ TEST_F(PairerBrokerImplTest, SuccessRetroactivePairingWithDevice) { pairing_completed_latch.Await(); account_key_writed_latch.Await(); EXPECT_FALSE(pairing_failure_latch.Await(kWaitTimeout).result()); - EXPECT_TRUE(triggered_keybase_value_change); - EXPECT_FALSE(triggered_passkey_value_change); EXPECT_TRUE(device_->GetAccountKey().Ok()); } TEST_F(PairerBrokerImplTest, FaileToCreateHandshakeRetryThreeTimes) { - bool triggered_keybase_value_change = false; - bool triggered_passkey_value_change = false; + ConfigurePairingContext(); + SetPairingResult(std::nullopt); + CreateMockDevice(DeviceFastPairVersion::kHigherThanV1, + Protocol::kFastPairSubsequentPairing); + SetupProviderGattServer(); + CountDownLatch device_paired_latch(1); CountDownLatch account_key_writed_latch(1); CountDownLatch pairing_completed_latch(1); CountDownLatch pairing_failure_latch(1); - ConfigurePairingContext(); - SetUpFastPairRepository(DeviceFastPairVersion::kHigherThanV1); - CreateMockDevice(DeviceFastPairVersion::kHigherThanV1, - Protocol::kFastPairSubsequentPairing); + pairer_broker_ = std::make_unique(*mediums_, &executor_); PairerBrokerObserver pairer_broker_observer( pairer_broker_.get(), &device_paired_latch, &account_key_writed_latch, &pairing_completed_latch, &pairing_failure_latch); - SetupProviderGattServer( - [&]() { triggered_keybase_value_change = true; }, - [&]() { - triggered_passkey_value_change = true; - EXPECT_OK(TriggerPasskeyGattChanged(kPasskey, kProviderPasskeyType)); - }); - SetPairingResult(std::nullopt); - pairer_broker_->PairDevice(*device_); + EXPECT_FALSE(device_paired_latch.Await(kWaitTimeout).result()); EXPECT_FALSE(pairing_completed_latch.Await(kWaitTimeout).result()); EXPECT_FALSE(account_key_writed_latch.Await(kWaitTimeout).result()); pairing_failure_latch.Await(); EXPECT_FALSE(pairer_broker_observer.handshake_completed_); - EXPECT_TRUE(triggered_keybase_value_change); - EXPECT_FALSE(triggered_passkey_value_change); + EXPECT_EQ(pairer_broker_observer.pair_failure_, PairFailure::kKeyBasedPairingResponseTimeout); } TEST_F(PairerBrokerImplTest, FaileToWriteAccountkey) { - bool triggered_keybase_value_change = false; - bool triggered_passkey_value_change = false; + ConfigurePairingContext(); + SetPairingResult(std::nullopt); + CreateMockDevice(DeviceFastPairVersion::kHigherThanV1, + Protocol::kFastPairInitialPairing); + SetupProviderGattServer(); + SetNotifyResponse(*key_based_characteristic_, kKeyBasedResponse); + SetNotifyResponse(*passkey_characteristic_, kPasskeyResponse); + SetDecryptedResponse(); + SetDecryptedPasskey(); + CreateFastPairHandshakeInstanceForDevice(); + CountDownLatch device_paired_latch(1); CountDownLatch account_key_writed_latch(1); CountDownLatch pairing_completed_latch(1); CountDownLatch pairing_failure_latch(1); - ConfigurePairingContext(); - 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)); - }); - CreateFastPairHandshakeInstanceForDevice(); - SetPairingResult(std::nullopt); SetAccountkeyCharacteristicsWriteResultToFailure(); EXPECT_FALSE(device_->GetAccountKey().Ok()); @@ -652,35 +576,27 @@ TEST_F(PairerBrokerImplTest, FaileToWriteAccountkey) { EXPECT_FALSE(pairing_completed_latch.Await(kWaitTimeout).result()); account_key_writed_latch.Await(); EXPECT_FALSE(pairing_failure_latch.Await(kWaitTimeout).result()); - EXPECT_TRUE(triggered_keybase_value_change); - EXPECT_TRUE(triggered_passkey_value_change); EXPECT_FALSE(device_->GetAccountKey().Ok()); EXPECT_EQ(pairer_broker_observer.account_key_failure_, PairFailure::kAccountKeyCharacteristicWrite); } TEST_F(PairerBrokerImplTest, FailToPairRetryThreeTimes) { - bool triggered_keybase_value_change = false; - bool triggered_passkey_value_change = false; + ConfigurePairingContext(); + SetPairingResult(std::nullopt); + CreateMockDevice(DeviceFastPairVersion::kHigherThanV1, + Protocol::kFastPairInitialPairing); + SetupProviderGattServer(); + SetNotifyResponse(*key_based_characteristic_, kKeyBasedResponse); + SetNotifyResponse(*passkey_characteristic_, kPasskeyResponse); + SetDecryptedResponse(); + SetDecryptedPasskey("654321"); + CreateFastPairHandshakeInstanceForDevice(); + CountDownLatch device_paired_latch(1); CountDownLatch account_key_writed_latch(1); CountDownLatch pairing_completed_latch(1); CountDownLatch pairing_failure_latch(1); - ConfigurePairingContext(); - 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("654321", kProviderPasskeyType)); - }); - CreateFastPairHandshakeInstanceForDevice(); - SetPairingResult(std::nullopt); EXPECT_FALSE(device_->GetAccountKey().Ok()); @@ -694,8 +610,6 @@ TEST_F(PairerBrokerImplTest, FailToPairRetryThreeTimes) { EXPECT_FALSE(pairing_completed_latch.Await(kWaitTimeout).result()); EXPECT_FALSE(account_key_writed_latch.Await(kWaitTimeout).result()); pairing_failure_latch.Await(); - EXPECT_TRUE(triggered_keybase_value_change); - EXPECT_TRUE(triggered_passkey_value_change); EXPECT_FALSE(device_->GetAccountKey().Ok()); EXPECT_EQ(pairer_broker_observer.pair_failure_, PairFailure::kPairingAndConnect);