From 0cd8aaab2585939e1648afda3e5e5bcc670329e4 Mon Sep 17 00:00:00 2001 From: Qin Wang Date: Thu, 20 Jul 2023 11:37:45 -0700 Subject: [PATCH] Write account association to footprints PiperOrigin-RevId: 549693215 --- fastpair/common/pair_failure.cc | 3 + fastpair/common/pair_failure.h | 3 +- fastpair/common/pair_failure_test.cc | 1 + fastpair/pairing/fastpair/BUILD | 2 + .../pairing/fastpair/fast_pair_pairer_impl.cc | 20 +- .../fastpair/fast_pair_pairer_impl_test.cc | 205 ++++++++---------- fastpair/pairing/pairer_broker_impl_test.cc | 42 +++- fastpair/repository/BUILD | 1 + .../repository/fake_fast_pair_repository.cc | 25 +++ .../repository/fake_fast_pair_repository.h | 13 +- 10 files changed, 198 insertions(+), 117 deletions(-) diff --git a/fastpair/common/pair_failure.cc b/fastpair/common/pair_failure.cc index ae678ce0..eade8563 100644 --- a/fastpair/common/pair_failure.cc +++ b/fastpair/common/pair_failure.cc @@ -105,6 +105,9 @@ std::ostream& operator<<(std::ostream& stream, PairFailure failure) { case PairFailure::kPairingTimeout: stream << "[Potential pairing failed with timeout.]"; break; + case PairFailure::kWriteAccountKeyToFootprints: + stream << "[Failed to write Account Key to Footprints.]"; + break; } return stream; diff --git a/fastpair/common/pair_failure.h b/fastpair/common/pair_failure.h index b305e424..c4d71009 100644 --- a/fastpair/common/pair_failure.h +++ b/fastpair/common/pair_failure.h @@ -77,7 +77,8 @@ enum class PairFailure { kPairingAndConnect = 24, // Potential pairing timeout. kPairingTimeout = 25, - kMaxValue = kPairingTimeout, + kWriteAccountKeyToFootprints = 26, + kMaxValue = kWriteAccountKeyToFootprints, }; std::ostream& operator<<(std::ostream& stream, PairFailure failure); diff --git a/fastpair/common/pair_failure_test.cc b/fastpair/common/pair_failure_test.cc index f5c993d2..01cc6ff9 100644 --- a/fastpair/common/pair_failure_test.cc +++ b/fastpair/common/pair_failure_test.cc @@ -64,6 +64,7 @@ TEST(PairFailureTest, PairFailureValue) { EXPECT_EQ(static_cast(PairFailure::kDeviceLostMidPairing), 23); EXPECT_EQ(static_cast(PairFailure::kPairingAndConnect), 24); EXPECT_EQ(static_cast(PairFailure::kPairingTimeout), 25); + EXPECT_EQ(static_cast(PairFailure::kWriteAccountKeyToFootprints), 26); } } // namespace } // namespace fastpair diff --git a/fastpair/pairing/fastpair/BUILD b/fastpair/pairing/fastpair/BUILD index 1e35df2f..eb6a212b 100644 --- a/fastpair/pairing/fastpair/BUILD +++ b/fastpair/pairing/fastpair/BUILD @@ -33,6 +33,7 @@ cc_library( "//fastpair/crypto", "//fastpair/handshake", "//fastpair/internal/mediums", + "//fastpair/repository", "//internal/platform:comm", "//internal/platform:types", "@com_google_absl//absl/functional:any_invocable", @@ -64,6 +65,7 @@ cc_test( "@com_github_protobuf_matchers//protobuf-matchers", "@com_google_absl//absl/functional:any_invocable", "@com_google_absl//absl/functional:bind_front", + "@com_google_absl//absl/status", "@com_google_absl//absl/strings", "@com_google_absl//absl/time", "@com_google_googletest//:gtest_main", diff --git a/fastpair/pairing/fastpair/fast_pair_pairer_impl.cc b/fastpair/pairing/fastpair/fast_pair_pairer_impl.cc index 134425b4..b1ca0724 100644 --- a/fastpair/pairing/fastpair/fast_pair_pairer_impl.cc +++ b/fastpair/pairing/fastpair/fast_pair_pairer_impl.cc @@ -27,6 +27,7 @@ #include "fastpair/crypto/fast_pair_message_type.h" #include "fastpair/handshake/fast_pair_handshake_lookup.h" #include "fastpair/internal/mediums/mediums.h" +#include "fastpair/repository/fast_pair_repository.h" #include "internal/platform/bluetooth_classic.h" #include "internal/platform/single_thread_executor.h" @@ -302,8 +303,23 @@ void FastPairPairerImpl::OnWriteAccountKey( return; } device_.SetAccountKey(account_key.value()); - // // TODO(b/281785681): Write account association to footprints - NotifyPairingCompleted(); + + // Devices in the Retroactive Pair scenario are not written to Footprints + // on account key write, but when the user hits 'Save' on the retroactive pair + // notification. + if (device_.GetProtocol() == Protocol::kFastPairRetroactivePairing) { + NotifyPairingCompleted(); + return; + } + + FastPairRepository::Get()->WriteAccountAssociationToFootprints( + device_, [&](absl::Status status) { + if (status.ok()) { + NotifyPairingCompleted(); + } else { + NotifyAccountKeyFailure(PairFailure::kWriteAccountKeyToFootprints); + } + }); } void FastPairPairerImpl::NotifyPaired() { diff --git a/fastpair/pairing/fastpair/fast_pair_pairer_impl_test.cc b/fastpair/pairing/fastpair/fast_pair_pairer_impl_test.cc index 9d97b48a..729d70c1 100644 --- a/fastpair/pairing/fastpair/fast_pair_pairer_impl_test.cc +++ b/fastpair/pairing/fastpair/fast_pair_pairer_impl_test.cc @@ -27,6 +27,7 @@ #include "gtest/gtest.h" #include "absl/functional/any_invocable.h" #include "absl/functional/bind_front.h" +#include "absl/status/status.h" #include "absl/strings/string_view.h" #include "absl/time/time.h" #include "fastpair//handshake/fast_pair_handshake_lookup.h" @@ -44,6 +45,7 @@ #include "fastpair/handshake/fast_pair_handshake_impl.h" #include "fastpair/pairing/fastpair/fast_pair_pairer.h" #include "fastpair/proto/fastpair_rpcs.proto.h" +#include "fastpair/repository/fake_fast_pair_repository.h" #include "internal/base/bluetooth_address.h" #include "internal/platform/ble_v2.h" #include "internal/platform/bluetooth_adapter.h" @@ -373,6 +375,8 @@ class FastPairPairerImplTest : public testing::Test { TEST_F(FastPairPairerImplTest, SuccessInitialPairingWithDeviceVersionHigherThanV1) { + auto repository = std::make_unique(); + repository->SetResultOfWriteAccountAssociationToFootprints(absl::OkStatus()); ConfigurePairingContext(); SetPairingResult(std::nullopt); CreateMockDevice(DeviceFastPairVersion::kHigherThanV1, @@ -386,29 +390,25 @@ TEST_F(FastPairPairerImplTest, 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(); }, [&](FastPairDevice& device, PairFailure failure) { - failure_latch.CountDown(); + FAIL() << "Unexpected pairing failure " << failure; }, [&](FastPairDevice& device, PairFailure failure) { - account_failure_latch.CountDown(); + FAIL() << "Unexpected pairing failure " << failure; }, [&](FastPairDevice& device) { EXPECT_TRUE(device.GetAccountKey().Ok()); complete_latch.CountDown(); }); fast_pair_pairer_->StartPairing(); - paired_latch.Await(); - EXPECT_FALSE(failure_latch.Await(kWaitTimeout).result()); - EXPECT_FALSE(account_failure_latch.Await(kWaitTimeout).result()); - complete_latch.Await(); + paired_latch.Await(); + complete_latch.Await(); EXPECT_TRUE(fast_pair_pairer_->IsPaired()); EXPECT_TRUE(device_->GetAccountKey().Ok()); } @@ -422,26 +422,23 @@ TEST_F(FastPairPairerImplTest, SuccessInitialPairingWithDeviceV1) { CountDownLatch paired_latch(1); CountDownLatch complete_latch(1); - CountDownLatch failure_latch(1); - CountDownLatch account_failure_latch(1); fast_pair_pairer_ = FastPairPairerImpl::Factory::Create( *device_, *mediums_, &executor_, [&](FastPairDevice& cb_device) { paired_latch.CountDown(); }, [&](FastPairDevice& device, PairFailure failure) { - failure_latch.CountDown(); + FAIL() << "Unexpected pairing failure " << failure; }, [&](FastPairDevice& device, PairFailure failure) { - account_failure_latch.CountDown(); + FAIL() << "Unexpected pairing failure " << failure; }, [&](FastPairDevice& device) { EXPECT_FALSE(device.GetAccountKey().Ok()); complete_latch.CountDown(); }); fast_pair_pairer_->StartPairing(); + paired_latch.Await(); - EXPECT_FALSE(failure_latch.Await(kWaitTimeout).result()); - EXPECT_FALSE(account_failure_latch.Await(kWaitTimeout).result()); complete_latch.Await(); EXPECT_TRUE(fast_pair_pairer_->IsPaired()); } @@ -460,26 +457,23 @@ TEST_F(FastPairPairerImplTest, SuccessSubsequentPairingWithDevice) { CountDownLatch paired_latch(1); CountDownLatch complete_latch(1); - CountDownLatch failure_latch(1); - CountDownLatch account_failure_latch(1); fast_pair_pairer_ = FastPairPairerImpl::Factory::Create( *device_, *mediums_, &executor_, [&](FastPairDevice& cb_device) { paired_latch.CountDown(); }, [&](FastPairDevice& device, PairFailure failure) { - failure_latch.CountDown(); + FAIL() << "Unexpected pairing failure " << failure; }, [&](FastPairDevice& device, PairFailure failure) { - account_failure_latch.CountDown(); + FAIL() << "Unexpected pairing failure " << failure; }, [&](FastPairDevice& device) { EXPECT_TRUE(device.GetAccountKey().Ok()); complete_latch.CountDown(); }); fast_pair_pairer_->StartPairing(); + paired_latch.Await(); - EXPECT_FALSE(failure_latch.Await(kWaitTimeout).result()); - EXPECT_FALSE(account_failure_latch.Await(kWaitTimeout).result()); complete_latch.Await(); EXPECT_TRUE(fast_pair_pairer_->IsPaired()); } @@ -494,30 +488,25 @@ TEST_F(FastPairPairerImplTest, SuccessRetroactivePairingWithDevice) { SetDecryptedResponse(); CreateFastPairHandshakeInstanceForDevice(); - 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(); }, + [&](FastPairDevice& cb_device) { FAIL() << "Unexpected callback"; }, [&](FastPairDevice& device, PairFailure failure) { - failure_latch.CountDown(); + FAIL() << "Unexpected pairing failure " << failure; }, [&](FastPairDevice& device, PairFailure failure) { - account_failure_latch.CountDown(); + FAIL() << "Unexpected pairing failure " << failure; }, [&](FastPairDevice& device) { EXPECT_TRUE(device.GetAccountKey().Ok()); complete_latch.CountDown(); }); fast_pair_pairer_->StartPairing(); - EXPECT_FALSE(paired_latch.Await(kWaitTimeout).result()); - EXPECT_FALSE(failure_latch.Await(kWaitTimeout).result()); - EXPECT_FALSE(account_failure_latch.Await(kWaitTimeout).result()); + complete_latch.Await(); EXPECT_TRUE(device_->GetAccountKey().Ok()); } @@ -530,32 +519,27 @@ TEST_F(FastPairPairerImplTest, FailedToUnPair) { SetDecryptedResponse(); CreateFastPairHandshakeInstanceForDevice(); - 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(); }, + [&](FastPairDevice& cb_device) { FAIL() << "Unexpected callback"; }, [&](FastPairDevice& device, PairFailure failure) { EXPECT_EQ(failure, PairFailure::kPairingAndConnect); failure_latch.CountDown(); }, [&](FastPairDevice& device, PairFailure failure) { - account_failure_latch.CountDown(); + FAIL() << "Unexpected pairing failure " << failure; }, [&](FastPairDevice& device) { EXPECT_TRUE(device.GetAccountKey().Ok()); - complete_latch.CountDown(); + FAIL() << "Unexpected callback"; }); fast_pair_pairer_->StartPairing(); - EXPECT_FALSE(paired_latch.Await(kWaitTimeout).result()); + failure_latch.Await(); - EXPECT_FALSE(complete_latch.Await(kWaitTimeout).result()); - EXPECT_FALSE(account_failure_latch.Await(kWaitTimeout).result()); + EXPECT_FALSE(fast_pair_pairer_->IsPaired()); EXPECT_FALSE(device_->GetAccountKey().Ok()); } @@ -572,32 +556,25 @@ TEST_F(FastPairPairerImplTest, FailedToPairingWithAuthTimeout) { SetDecryptedPasskey(); CreateFastPairHandshakeInstanceForDevice(); - 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(); }, + [&](FastPairDevice& cb_device) { FAIL() << "Unexpected callback"; }, [&](FastPairDevice& device, PairFailure failure) { EXPECT_EQ(failure, PairFailure::kPairingAndConnect); failure_latch.CountDown(); }, [&](FastPairDevice& device, PairFailure failure) { - account_failure_latch.CountDown(); + FAIL() << "Unexpected pairing failure " << failure; }, - [&](FastPairDevice& device) { - EXPECT_TRUE(device.GetAccountKey().Ok()); - complete_latch.CountDown(); - }); + [&](FastPairDevice& device) { FAIL() << "Unexpected callback"; }); fast_pair_pairer_->StartPairing(); - EXPECT_FALSE(paired_latch.Await(kWaitTimeout).result()); + failure_latch.Await(); - EXPECT_FALSE(complete_latch.Await(kWaitTimeout).result()); - EXPECT_FALSE(account_failure_latch.Await(kWaitTimeout).result()); + EXPECT_FALSE(fast_pair_pairer_->IsPaired()); EXPECT_FALSE(device_->GetAccountKey().Ok()); } @@ -612,32 +589,25 @@ TEST_F(FastPairPairerImplTest, NoPasskeyResponse) { SetDecryptedResponse(); CreateFastPairHandshakeInstanceForDevice(); - 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(); }, + [&](FastPairDevice& cb_device) { FAIL() << "Unexpected callback"; }, [&](FastPairDevice& device, PairFailure failure) { EXPECT_EQ(failure, PairFailure::kPasskeyResponseTimeout); failure_latch.CountDown(); }, [&](FastPairDevice& device, PairFailure failure) { - account_failure_latch.CountDown(); + FAIL() << "Unexpected pairing failure " << failure; }, - [&](FastPairDevice& device) { - EXPECT_TRUE(device.GetAccountKey().Ok()); - complete_latch.CountDown(); - }); + [&](FastPairDevice& device) { FAIL() << "Unexpected callback"; }); fast_pair_pairer_->StartPairing(); - EXPECT_FALSE(paired_latch.Await(kWaitTimeout).result()); + failure_latch.Await(); - EXPECT_FALSE(complete_latch.Await(kWaitTimeout).result()); - EXPECT_FALSE(account_failure_latch.Await(kWaitTimeout).result()); + EXPECT_FALSE(fast_pair_pairer_->IsPaired()); EXPECT_FALSE(device_->GetAccountKey().Ok()); } @@ -654,32 +624,25 @@ TEST_F(FastPairPairerImplTest, PasskeyMismatch) { SetDecryptedPasskey("654321"); CreateFastPairHandshakeInstanceForDevice(); - 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(); }, + [&](FastPairDevice& cb_device) { FAIL() << "Unexpected callback"; }, [&](FastPairDevice& device, PairFailure failure) { EXPECT_EQ(failure, PairFailure::kPasskeyMismatch); failure_latch.CountDown(); }, [&](FastPairDevice& device, PairFailure failure) { - account_failure_latch.CountDown(); + FAIL() << "Unexpected pairing failure " << failure; }, - [&](FastPairDevice& device) { - EXPECT_TRUE(device.GetAccountKey().Ok()); - complete_latch.CountDown(); - }); + [&](FastPairDevice& device) { FAIL() << "Unexpected callback"; }); fast_pair_pairer_->StartPairing(); - EXPECT_FALSE(paired_latch.Await(kWaitTimeout).result()); + failure_latch.Await(); - EXPECT_FALSE(complete_latch.Await(kWaitTimeout).result()); - EXPECT_FALSE(account_failure_latch.Await(kWaitTimeout).result()); + EXPECT_FALSE(fast_pair_pairer_->IsPaired()); EXPECT_FALSE(device_->GetAccountKey().Ok()); } @@ -696,32 +659,26 @@ TEST_F(FastPairPairerImplTest, ReceiveWithWrongPasskeyResponse) { 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(); }, + [&](FastPairDevice& cb_device) { FAIL() << "Unexpected callback"; }, [&](FastPairDevice& device, PairFailure failure) { EXPECT_EQ(failure, PairFailure::kPasskeyDecryptFailure); failure_latch.CountDown(); }, [&](FastPairDevice& device, PairFailure failure) { - account_failure_latch.CountDown(); + FAIL() << "Unexpected pairing failure " << failure; }, - [&](FastPairDevice& device) { - EXPECT_TRUE(device.GetAccountKey().Ok()); - complete_latch.CountDown(); - }); + [&](FastPairDevice& device) { FAIL() << "Unexpected callback"; }); fast_pair_pairer_->StartPairing(); - EXPECT_FALSE(paired_latch.Await(kWaitTimeout).result()); + failure_latch.Await(); - EXPECT_FALSE(complete_latch.Await(kWaitTimeout).result()); - EXPECT_FALSE(account_failure_latch.Await(kWaitTimeout).result()); + EXPECT_FALSE(fast_pair_pairer_->IsPaired()); EXPECT_FALSE(device_->GetAccountKey().Ok()); } @@ -739,38 +696,32 @@ TEST_F(FastPairPairerImplTest, ReceiveWithWrongPasskeyMessageType) { 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(); }, + [&](FastPairDevice& cb_device) { FAIL() << "Unexpected callback"; }, [&](FastPairDevice& device, PairFailure failure) { EXPECT_EQ(failure, PairFailure::kIncorrectPasskeyResponseType); failure_latch.CountDown(); }, [&](FastPairDevice& device, PairFailure failure) { - account_failure_latch.CountDown(); + FAIL() << "Unexpected pairing failure " << failure; }, - [&](FastPairDevice& device) { - EXPECT_TRUE(device.GetAccountKey().Ok()); - complete_latch.CountDown(); - }); + [&](FastPairDevice& device) { FAIL() << "Unexpected callback"; }); fast_pair_pairer_->StartPairing(); - EXPECT_FALSE(paired_latch.Await(kWaitTimeout).result()); + failure_latch.Await(); - EXPECT_FALSE(complete_latch.Await(kWaitTimeout).result()); - EXPECT_FALSE(account_failure_latch.Await(kWaitTimeout).result()); + EXPECT_FALSE(fast_pair_pairer_->IsPaired()); EXPECT_FALSE(device_->GetAccountKey().Ok()); } TEST_F(FastPairPairerImplTest, - SuccessPairingWithDeviceButFailedToWriteAccountkey) { + SuccessPairingWithDeviceButFailedToWriteAccountkeyToRemoteDevice) { ConfigurePairingContext(); SetPairingResult(std::nullopt); CreateMockDevice(DeviceFastPairVersion::kHigherThanV1, @@ -797,6 +748,7 @@ TEST_F(FastPairPairerImplTest, failure_latch.CountDown(); }, [&](FastPairDevice& device, PairFailure failure) { + EXPECT_EQ(failure, PairFailure::kAccountKeyCharacteristicWrite); account_failure_latch.CountDown(); }, [&](FastPairDevice& device) { @@ -812,6 +764,45 @@ TEST_F(FastPairPairerImplTest, EXPECT_FALSE(device_->GetAccountKey().Ok()); } +TEST_F(FastPairPairerImplTest, + SuccessPairingWithDeviceButFailedToWriteAccountkeyToFootprints) { + auto repository = std::make_unique(); + repository->SetResultOfWriteAccountAssociationToFootprints( + absl::InternalError("Failed to write account key to foot prints")); + ConfigurePairingContext(); + SetPairingResult(std::nullopt); + CreateMockDevice(DeviceFastPairVersion::kHigherThanV1, + Protocol::kFastPairInitialPairing); + SetupProviderGattServer(); + SetNotifyResponse(*key_based_characteristic_, kKeyBasedResponse); + SetNotifyResponse(*passkey_characteristic_, kPasskeyResponse); + SetDecryptedResponse(); + SetDecryptedPasskey(); + CreateFastPairHandshakeInstanceForDevice(); + + CountDownLatch paired_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(); }, + [&](FastPairDevice& device, PairFailure failure) { + FAIL() << "Unexpected pairing failure " << failure; + }, + [&](FastPairDevice& device, PairFailure failure) { + EXPECT_EQ(failure, PairFailure::kWriteAccountKeyToFootprints); + account_failure_latch.CountDown(); + }, + [&](FastPairDevice& device) { FAIL() << "Unexpected callback"; }); + fast_pair_pairer_->StartPairing(); + + paired_latch.Await(); + account_failure_latch.Await(); + EXPECT_TRUE(fast_pair_pairer_->IsPaired()); + EXPECT_TRUE(device_->GetAccountKey().Ok()); +} + TEST_F(FastPairPairerImplTest, TestCancelPairing) { ConfigurePairingContext(); SetPairingResult(std::nullopt); @@ -823,29 +814,25 @@ TEST_F(FastPairPairerImplTest, TestCancelPairing) { CreateFastPairHandshakeInstanceForDevice(); SetTryToCancelOngoingPairing(true); - 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(); }, + [&](FastPairDevice& cb_device) { FAIL() << "Unexpected callback"; }, [&](FastPairDevice& device, PairFailure failure) { EXPECT_EQ(failure, PairFailure::kPairingAndConnect); failure_latch.CountDown(); }, [&](FastPairDevice& device, PairFailure failure) { - account_failure_latch.CountDown(); + FAIL() << "Unexpected pairing failure " << failure; }, - [&](FastPairDevice& device) { complete_latch.CountDown(); }); + [&](FastPairDevice& device) { FAIL() << "Unexpected callback"; }); fast_pair_pairer_->StartPairing(); - EXPECT_FALSE(paired_latch.Await(kWaitTimeout).result()); + failure_latch.Await(); - EXPECT_FALSE(account_failure_latch.Await(kWaitTimeout).result()); - EXPECT_FALSE(complete_latch.Await(kWaitTimeout).result()); + EXPECT_FALSE(device_->GetAccountKey().Ok()); } } // namespace fastpair diff --git a/fastpair/pairing/pairer_broker_impl_test.cc b/fastpair/pairing/pairer_broker_impl_test.cc index 313d21e5..9db64e52 100644 --- a/fastpair/pairing/pairer_broker_impl_test.cc +++ b/fastpair/pairing/pairer_broker_impl_test.cc @@ -35,6 +35,7 @@ #include "fastpair/handshake/fast_pair_handshake_lookup.h" #include "fastpair/internal/mediums/mediums.h" #include "fastpair/proto/fastpair_rpcs.proto.h" +#include "fastpair/repository/fake_fast_pair_repository.h" #include "internal/base/bluetooth_address.h" #include "internal/platform/ble_v2.h" #include "internal/platform/count_down_latch.h" @@ -427,6 +428,8 @@ TEST_F(PairerBrokerImplTest, SuccessInitialPairingWithDeviceV1) { } TEST_F(PairerBrokerImplTest, SuccessInitialPairingWithDevice) { + auto repository = std::make_unique(); + repository->SetResultOfWriteAccountAssociationToFootprints(absl::OkStatus()); ConfigurePairingContext(); SetPairingResult(std::nullopt); CreateMockDevice(DeviceFastPairVersion::kHigherThanV1, @@ -546,7 +549,7 @@ TEST_F(PairerBrokerImplTest, FaileToCreateHandshakeRetryThreeTimes) { PairFailure::kKeyBasedPairingResponseTimeout); } -TEST_F(PairerBrokerImplTest, FaileToWriteAccountkey) { +TEST_F(PairerBrokerImplTest, FaileToWriteAccountkeyToRemoteDevice) { ConfigurePairingContext(); SetPairingResult(std::nullopt); CreateMockDevice(DeviceFastPairVersion::kHigherThanV1, @@ -581,6 +584,43 @@ TEST_F(PairerBrokerImplTest, FaileToWriteAccountkey) { PairFailure::kAccountKeyCharacteristicWrite); } +TEST_F(PairerBrokerImplTest, FaileToWriteAccountkeyToFootprints) { + auto repository = std::make_unique(); + repository->SetResultOfWriteAccountAssociationToFootprints( + absl::InternalError("Failed to write account key to foot prints")); + 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); + + 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, + &pairing_completed_latch, &pairing_failure_latch); + pairer_broker_->PairDevice(*device_); + + device_paired_latch.Await(); + EXPECT_FALSE(pairing_completed_latch.Await(kWaitTimeout).result()); + account_key_writed_latch.Await(); + EXPECT_FALSE(pairing_failure_latch.Await(kWaitTimeout).result()); + EXPECT_TRUE(device_->GetAccountKey().Ok()); + EXPECT_EQ(pairer_broker_observer.account_key_failure_, + PairFailure::kWriteAccountKeyToFootprints); +} + TEST_F(PairerBrokerImplTest, FailToPairRetryThreeTimes) { ConfigurePairingContext(); SetPairingResult(std::nullopt); diff --git a/fastpair/repository/BUILD b/fastpair/repository/BUILD index 37bf567a..c6b3dff2 100644 --- a/fastpair/repository/BUILD +++ b/fastpair/repository/BUILD @@ -83,6 +83,7 @@ cc_library( "//fastpair/proto:fastpair_cc_proto", "//internal/platform:types", "@com_google_absl//absl/container:flat_hash_map", + "@com_google_absl//absl/status", "@com_google_absl//absl/strings", ], ) diff --git a/fastpair/repository/fake_fast_pair_repository.cc b/fastpair/repository/fake_fast_pair_repository.cc index 98329e3e..31da9be8 100644 --- a/fastpair/repository/fake_fast_pair_repository.cc +++ b/fastpair/repository/fake_fast_pair_repository.cc @@ -23,6 +23,7 @@ #include "absl/strings/string_view.h" #include "fastpair/common/account_key.h" #include "fastpair/common/constant.h" +#include "fastpair/common/fast_pair_device.h" #include "fastpair/proto/fastpair_rpcs.proto.h" namespace nearby { @@ -45,6 +46,16 @@ void FakeFastPairRepository::SetResultOfCheckIfAssociatedWithCurrentAccount( model_id_ = std::move(model_id); } +void FakeFastPairRepository::SetResultOfWriteAccountAssociationToFootprints( + absl::Status status) { + write_account_association_to_footprints_ = status; +} + +void FakeFastPairRepository::SetResultOfDeleteAssociatedDeviceByAccountKey( + absl::Status status) { + deleted_associated_device_ = status; +} + void FakeFastPairRepository::GetDeviceMetadata( absl::string_view hex_model_id, DeviceMetadataCallback callback) { executor_.Execute([this, callback = std::move(callback), @@ -57,6 +68,20 @@ void FakeFastPairRepository::GetDeviceMetadata( }); } +void FakeFastPairRepository::WriteAccountAssociationToFootprints( + FastPairDevice& device, OperationToFootprintsCallback callback) { + executor_.Execute([callback = std::move(callback), this]() mutable { + callback(write_account_association_to_footprints_); + }); +} + +void FakeFastPairRepository::DeleteAssociatedDeviceByAccountKey( + const AccountKey& account_key, OperationToFootprintsCallback callback) { + executor_.Execute([callback = std::move(callback), this]() mutable { + callback(deleted_associated_device_); + }); +} + void FakeFastPairRepository::CheckIfAssociatedWithCurrentAccount( AccountKeyFilter& account_key_filter, CheckAccountKeysCallback callback) { executor_.Execute([this, callback = std::move(callback)]() mutable { diff --git a/fastpair/repository/fake_fast_pair_repository.h b/fastpair/repository/fake_fast_pair_repository.h index 801ab80b..a1ca8428 100644 --- a/fastpair/repository/fake_fast_pair_repository.h +++ b/fastpair/repository/fake_fast_pair_repository.h @@ -20,6 +20,7 @@ #include #include "absl/container/flat_hash_map.h" +#include "absl/status/status.h" #include "absl/strings/string_view.h" #include "fastpair/common/account_key.h" #include "fastpair/common/device_metadata.h" @@ -40,7 +41,8 @@ class FakeFastPairRepository : public FastPairRepository { void SetFakeMetadata(absl::string_view hex_model_id, proto::Device metadata); void ClearFakeMetadata(absl::string_view hex_model_id); - + void SetResultOfWriteAccountAssociationToFootprints(absl::Status status); + void SetResultOfDeleteAssociatedDeviceByAccountKey(absl::Status status); void SetResultOfCheckIfAssociatedWithCurrentAccount( std::optional account_key, std::optional model_id); @@ -55,12 +57,11 @@ class FakeFastPairRepository : public FastPairRepository { void GetUserSavedDevices() override{}; void WriteAccountAssociationToFootprints( - FastPairDevice& device, - OperationToFootprintsCallback callback) override{}; + FastPairDevice& device, OperationToFootprintsCallback callback) override; void DeleteAssociatedDeviceByAccountKey( const AccountKey& account_key, - OperationToFootprintsCallback callback) override{}; + OperationToFootprintsCallback callback) override; void CheckIfAssociatedWithCurrentAccount( AccountKeyFilter& account_key_filter, @@ -72,6 +73,10 @@ class FakeFastPairRepository : public FastPairRepository { // Results of CheckIfAssociatedWithCurrentAccount std::optional account_key_; std::optional model_id_; + // Results of WriteAccountAssociationToFootprints + absl::Status write_account_association_to_footprints_; + // Results of DeleteAssociatedDeviceByAccountKey + absl::Status deleted_associated_device_; SingleThreadExecutor executor_; }; } // namespace fastpair