Store metadata in FastPairDevice

Add DeviceMetadata to FastPairDevice after downloading it.
It simplifies the code and prevents fetching the same metadata several times.
FastPairDataEncryptorImpl does not download the metadata anymore. Metadata
must have already been downloaded, otherwise we wouldn't know what FP protocol
to use.

PiperOrigin-RevId: 542490742
This commit is contained in:
Janusz Sobczak
2023-06-22 01:48:56 -07:00
committed by Copybara-Service
parent f9123d544b
commit 3cf20ee3bc
35 changed files with 239 additions and 171 deletions
+1
View File
@@ -28,6 +28,7 @@ cc_library(
"//fastpair/common",
"//fastpair/handshake",
"//fastpair/message_stream",
"//fastpair/server_access",
"//internal/base",
"//internal/platform:comm",
"//internal/platform:types",
+4 -1
View File
@@ -17,10 +17,12 @@ cc_library(
"account_key_filter.h",
"battery_notification.h",
"constant.h",
"device_metadata.h",
"fast_pair_device.h",
"fast_pair_http_result.h",
"fast_pair_prefs.h",
"fast_pair_switches.h",
"fast_pair_version.h",
"non_discoverable_advertisement.h",
"pair_failure.h",
"protocol.h",
@@ -29,6 +31,7 @@ cc_library(
"//fastpair:__subpackages__",
],
deps = [
"//fastpair/proto:fastpair_cc_proto",
"//internal/crypto",
"//internal/platform:logging",
"//internal/preferences",
@@ -64,9 +67,9 @@ cc_test(
shard_count = 16,
deps = [
":common",
"//fastpair/proto:fastpair_cc_proto",
"//internal/platform/implementation/g3", # build_cleaner: keep
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_absl//absl/strings",
"@com_google_googletest//:gtest_main",
],
)
@@ -17,8 +17,8 @@
#include <utility>
#include "fastpair/common/fast_pair_version.h"
#include "fastpair/proto/fastpair_rpcs.proto.h"
#include "fastpair/common/fast_pair_device.h"
namespace nearby {
namespace fastpair {
@@ -27,9 +27,9 @@ class DeviceMetadata {
explicit DeviceMetadata(const proto::GetObservedDeviceResponse response)
: response_(std::move(response)) {}
DeviceMetadata(DeviceMetadata &&) = default;
DeviceMetadata(const DeviceMetadata &) = delete;
DeviceMetadata &operator=(const DeviceMetadata &) = delete;
DeviceMetadata &operator=(DeviceMetadata &&) = delete;
DeviceMetadata(const DeviceMetadata &) = default;
DeviceMetadata &operator=(const DeviceMetadata &) = default;
DeviceMetadata &operator=(DeviceMetadata &&) = default;
~DeviceMetadata() = default;
const proto::Device &GetDetails() const { return response_.device(); }
const proto::GetObservedDeviceResponse &GetResponse() const {
@@ -46,7 +46,7 @@ class DeviceMetadata {
}
private:
const proto::GetObservedDeviceResponse response_;
proto::GetObservedDeviceResponse response_;
};
} // namespace fastpair
} // namespace nearby
+13 -9
View File
@@ -25,16 +25,13 @@
#include "absl/container/flat_hash_map.h"
#include "absl/strings/string_view.h"
#include "fastpair/common/account_key.h"
#include "fastpair/common/device_metadata.h"
#include "fastpair/common/fast_pair_version.h"
#include "fastpair/common/protocol.h"
namespace nearby {
namespace fastpair {
enum class DeviceFastPairVersion {
kV1,
kHigherThanV1,
};
// Thin class which is used by the higher level components of the Fast Pair
// system to represent a device.
class FastPairDevice {
@@ -64,10 +61,11 @@ class FastPairDevice {
display_name_ = std::string(display_name);
}
std::optional<DeviceFastPairVersion> GetVersion() { return version_; }
void SetVersion(std::optional<DeviceFastPairVersion> version) {
version_ = version;
std::optional<DeviceFastPairVersion> GetVersion() const {
if (metadata_) {
return metadata_->GetFastPairVersion();
}
return std::nullopt;
}
const AccountKey& GetAccountKey() const { return account_key_; }
@@ -88,6 +86,10 @@ class FastPairDevice {
Protocol GetProtocol() const { return protocol_; }
void SetMetadata(const DeviceMetadata& metadata) { metadata_ = metadata; }
const std::optional<DeviceMetadata>& GetMetadata() const { return metadata_; }
const std::string& GetUniqueId() const {
if (public_address_.has_value()) {
return *public_address_;
@@ -122,6 +124,8 @@ class FastPairDevice {
// for eligible devices (V2 or higher) and used for detecting subsequent
// pairing scenarios.
AccountKey account_key_;
std::optional<DeviceMetadata> metadata_;
};
std::ostream& operator<<(std::ostream& stream, const FastPairDevice& device);
+30
View File
@@ -23,7 +23,9 @@
#include "protobuf-matchers/protocol-buffer-matchers.h"
#include "gtest/gtest.h"
#include "fastpair/common/account_key.h"
#include "fastpair/common/device_metadata.h"
#include "fastpair/common/protocol.h"
#include "fastpair/proto/fastpair_rpcs.proto.h"
namespace nearby {
namespace fastpair {
@@ -76,6 +78,34 @@ TEST(FastPairDevice, GetAndPublicAddress) {
EXPECT_EQ(device.GetPublicAddress().value(), new_test_GetPublicAddress);
}
TEST(FastPairDevice, GetVersionUnset) {
FastPairDevice device("model_id", "ble_address",
Protocol::kFastPairInitialPairing);
EXPECT_FALSE(device.GetVersion());
}
TEST(FastPairDevice, GetVersionV1) {
FastPairDevice device("model_id", "ble_address",
Protocol::kFastPairInitialPairing);
proto::GetObservedDeviceResponse response;
device.SetMetadata(DeviceMetadata(response));
EXPECT_EQ(device.GetVersion(), DeviceFastPairVersion::kV1);
}
TEST(FastPairDevice, GetVersionHigherThanV1) {
FastPairDevice device("model_id", "ble_address",
Protocol::kFastPairInitialPairing);
proto::GetObservedDeviceResponse response;
std::string anti_spoofing_key(kPublicKeyByteSize, 0);
response.mutable_device()->mutable_anti_spoofing_key_pair()->set_public_key(
anti_spoofing_key);
device.SetMetadata(DeviceMetadata(response));
EXPECT_EQ(device.GetVersion(), DeviceFastPairVersion::kHigherThanV1);
}
} // namespace
} // namespace fastpair
} // namespace nearby
+29
View File
@@ -0,0 +1,29 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef THIRD_PARTY_NEARBY_FASTPAIR_COMMON_FAST_PAIR_VERSION_H_
#define THIRD_PARTY_NEARBY_FASTPAIR_COMMON_FAST_PAIR_VERSION_H_
namespace nearby {
namespace fastpair {
enum class DeviceFastPairVersion {
kV1,
kHigherThanV1,
};
}
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_FASTPAIR_COMMON_FAST_PAIR_VERSION_H_
+21 -8
View File
@@ -24,6 +24,7 @@
#include "fastpair/common/protocol.h"
#include "fastpair/handshake/fast_pair_data_encryptor_impl.h"
#include "fastpair/message_stream/message_stream.h"
#include "fastpair/server_access/fast_pair_repository.h"
#include "internal/platform/bluetooth_adapter.h"
#include "internal/platform/bluetooth_classic.h"
#include "internal/platform/single_thread_executor.h"
@@ -91,18 +92,30 @@ FastPairController::GetDataEncryptor() {
if (!encryptor_) {
encryptor_ =
std::make_unique<Future<std::shared_ptr<FastPairDataEncryptor>>>();
FastPairDataEncryptorImpl::Factory::CreateAsync(
device_, [borrowable = lender_.GetBorrowable()](
std::unique_ptr<FastPairDataEncryptor> encryptor) mutable {
auto borrowed = borrowable.Borrow();
if (borrowed) {
(*borrowed)->SetDataEncryptor(std::move(encryptor));
}
});
if (device_.GetMetadata()) {
CreateDataEncryptor();
} else {
FastPairRepository::Get()->GetDeviceMetadata(
device_.GetModelId(), [this](DeviceMetadata& metadata) {
device_.SetMetadata(metadata);
CreateDataEncryptor();
});
}
}
return *encryptor_;
}
void FastPairController::CreateDataEncryptor() {
FastPairDataEncryptorImpl::Factory::CreateAsync(
device_, [borrowable = lender_.GetBorrowable()](
std::unique_ptr<FastPairDataEncryptor> encryptor) mutable {
auto borrowed = borrowable.Borrow();
if (borrowed) {
(*borrowed)->SetDataEncryptor(std::move(encryptor));
}
});
}
Future<FastPairController::GattClientRef>
FastPairController::GetGattClientRef() {
Future<FastPairController::GattClientRef> result;
+1
View File
@@ -175,6 +175,7 @@ class FastPairController : public MessageStream::Observer {
CHECK(gatt_client_ != nullptr);
return gatt_client_.get();
}
void CreateDataEncryptor();
Mediums* mediums_;
FastPairDevice device_;
SingleThreadExecutor* executor_;
+1
View File
@@ -139,6 +139,7 @@ cc_test(
deps = [
":handshake",
"//fastpair/common",
"//fastpair/proto:fastpair_cc_proto",
"//fastpair/server_access:test_support",
"//internal/platform:base",
"//internal/platform:test_util",
@@ -29,6 +29,7 @@
#include "absl/strings/string_view.h"
#include "fastpair/common/account_key.h"
#include "fastpair/common/constant.h"
#include "fastpair/common/device_metadata.h"
#include "fastpair/common/protocol.h"
#include "fastpair/crypto/decrypted_passkey.h"
#include "fastpair/crypto/decrypted_response.h"
@@ -36,7 +37,6 @@
#include "fastpair/crypto/fast_pair_key_pair.h"
#include "fastpair/dataparser/fast_pair_data_parser.h"
#include "fastpair/handshake/fast_pair_data_encryptor.h"
#include "fastpair/repository/device_metadata.h"
#include "fastpair/server_access/fast_pair_repository.h"
#include "internal/platform/logging.h"
@@ -88,27 +88,12 @@ void FastPairDataEncryptorImpl::Factory::CreateAsyncWithKeyExchange(
const FastPairDevice& device,
absl::AnyInvocable<void(std::unique_ptr<FastPairDataEncryptor>)>
on_get_instance_callback) {
// We first have to get the metadata in order to get the public key to use
// to generate the new secret key pair.
NEARBY_LOGS(INFO) << __func__ << ": Attempting to get device metadata.";
FastPairRepository::Get()->GetDeviceMetadata(
device.GetModelId(),
[on_get_instance_callback = std::move(on_get_instance_callback)](
DeviceMetadata& metadata) mutable {
FastPairDataEncryptorImpl::Factory::DeviceMetadataRetrieved(
std::move(on_get_instance_callback), metadata);
});
}
void FastPairDataEncryptorImpl::Factory::DeviceMetadataRetrieved(
absl::AnyInvocable<void(std::unique_ptr<FastPairDataEncryptor>)>
on_get_instance_callback,
DeviceMetadata& device_metadata) {
NEARBY_LOGS(INFO) << __func__;
DCHECK(&device_metadata);
NEARBY_LOGS(VERBOSE) << __func__;
auto& metadata = device.GetMetadata();
DCHECK(metadata);
std::optional<KeyPair> key_pair =
FastPairEncryption::GenerateKeysWithEcdhKeyAgreement(
device_metadata.GetDetails().anti_spoofing_key_pair().public_key());
metadata->GetDetails().anti_spoofing_key_pair().public_key());
if (!key_pair.has_value()) {
NEARBY_LOGS(INFO) << "Fail to generate key pair";
std::move(on_get_instance_callback)(nullptr);
@@ -25,10 +25,10 @@
#include "absl/functional/any_invocable.h"
#include "fastpair/common/constant.h"
#include "fastpair/common/device_metadata.h"
#include "fastpair/common/fast_pair_device.h"
#include "fastpair/crypto/fast_pair_key_pair.h"
#include "fastpair/handshake/fast_pair_data_encryptor.h"
#include "fastpair/repository/device_metadata.h"
namespace nearby {
namespace fastpair {
@@ -26,11 +26,11 @@
#include "absl/strings/escaping.h"
#include "fastpair/common/account_key.h"
#include "fastpair/common/constant.h"
#include "fastpair/common/device_metadata.h"
#include "fastpair/common/protocol.h"
#include "fastpair/crypto/fast_pair_encryption.h"
#include "fastpair/dataparser/fast_pair_data_parser.h"
#include "fastpair/handshake/fast_pair_data_encryptor.h"
#include "fastpair/server_access/fake_fast_pair_repository.h"
#include "internal/platform/count_down_latch.h"
namespace nearby {
@@ -57,27 +57,15 @@ class FastPairDataEncryptorImplTest : public testing::Test {
public:
void TearDown() override { data_encryptor_.reset(); }
void FailedSetUpRepositoryWithNoDeviceMetadata() {
CountDownLatch latch(1);
repository_ = std::make_unique<FakeFastPairRepository>();
FastPairDevice device(kValidModelId, kTestAddress,
Protocol::kFastPairInitialPairing);
FastPairDataEncryptorImpl::Factory::CreateAsync(
device, absl::bind_front(
&FastPairDataEncryptorImplTest::OnDataEncryptorCreateAsync,
this, latch));
EXPECT_FALSE(latch.Await(kWaitTimeout).GetResult());
}
void FailedSetUpRepositoryWithNoPublicKey() {
repository_ = std::make_unique<FakeFastPairRepository>();
proto::Device metadata;
void FailedSetUpDeviceWithNoPublicKey() {
proto::GetObservedDeviceResponse response;
std::string decoded_key;
absl::Base64Unescape(kInvalidPublicAntiSpoof, &decoded_key);
metadata.mutable_anti_spoofing_key_pair()->set_public_key(decoded_key);
repository_->SetFakeMetadata(kValidModelId, metadata);
response.mutable_device()->mutable_anti_spoofing_key_pair()->set_public_key(
decoded_key);
FastPairDevice device(kValidModelId, kTestAddress,
Protocol::kFastPairInitialPairing);
device.SetMetadata(DeviceMetadata(response));
CountDownLatch latch(1);
FastPairDataEncryptorImpl::Factory::CreateAsync(
device, absl::bind_front(
@@ -87,14 +75,14 @@ class FastPairDataEncryptorImplTest : public testing::Test {
}
void SuccessCreateFastPairDataEncryptorWithKeyExchange() {
repository_ = std::make_unique<FakeFastPairRepository>();
proto::Device metadata;
proto::GetObservedDeviceResponse response;
std::string decoded_key;
absl::Base64Unescape(kPublicAntiSpoof, &decoded_key);
metadata.mutable_anti_spoofing_key_pair()->set_public_key(decoded_key);
repository_->SetFakeMetadata(kValidModelId, metadata);
response.mutable_device()->mutable_anti_spoofing_key_pair()->set_public_key(
decoded_key);
FastPairDevice device(kValidModelId, kTestAddress,
Protocol::kFastPairInitialPairing);
device.SetMetadata(DeviceMetadata(response));
CountDownLatch latch(1);
FastPairDataEncryptorImpl::Factory::CreateAsync(
device, absl::bind_front(
@@ -218,17 +206,10 @@ class FastPairDataEncryptorImplTest : public testing::Test {
private:
std::unique_ptr<FastPairDevice> device_;
std::unique_ptr<FakeFastPairRepository> repository_;
};
TEST_F(FastPairDataEncryptorImplTest, FailedSetUpNoMetadata) {
EXPECT_FALSE(data_encryptor_);
FailedSetUpRepositoryWithNoDeviceMetadata();
EXPECT_FALSE(data_encryptor_);
}
TEST_F(FastPairDataEncryptorImplTest, NoKeyPair) {
FailedSetUpRepositoryWithNoPublicKey();
FailedSetUpDeviceWithNoPublicKey();
EXPECT_FALSE(data_encryptor_);
}
@@ -28,10 +28,12 @@
#include "absl/status/status.h"
#include "absl/strings/escaping.h"
#include "fastpair/common/constant.h"
#include "fastpair/common/device_metadata.h"
#include "fastpair/common/fast_pair_device.h"
#include "fastpair/common/pair_failure.h"
#include "fastpair/common/protocol.h"
#include "fastpair/handshake/fast_pair_gatt_service_client_impl.h"
#include "fastpair/proto/fastpair_rpcs.proto.h"
#include "fastpair/server_access/fake_fast_pair_repository.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/count_down_latch.h"
@@ -139,22 +141,22 @@ class FastPairHandshakeImplTest : public testing::Test {
absl::OkStatus();
}
void SetUpFastPairRepository() {
repository_ = std::make_unique<FakeFastPairRepository>();
proto::Device metadata;
void SetUpValidAntiSpoofingKey(FastPairDevice* device) {
proto::GetObservedDeviceResponse response;
std::string decoded_key;
absl::Base64Unescape(kPublicAntiSpoof, &decoded_key);
metadata.mutable_anti_spoofing_key_pair()->set_public_key(decoded_key);
repository_->SetFakeMetadata(kMetadataId, metadata);
response.mutable_device()->mutable_anti_spoofing_key_pair()->set_public_key(
decoded_key);
device->SetMetadata(DeviceMetadata(response));
}
void FailedFastPairRepository() {
repository_ = std::make_unique<FakeFastPairRepository>();
proto::Device metadata;
void SetUpInvalidAntiSpoofingKey(FastPairDevice* device) {
proto::GetObservedDeviceResponse response;
std::string decoded_key;
absl::Base64Unescape(kInvalidPublicAntiSpoof, &decoded_key);
metadata.mutable_anti_spoofing_key_pair()->set_public_key(decoded_key);
repository_->SetFakeMetadata(kMetadataId, metadata);
response.mutable_device()->mutable_anti_spoofing_key_pair()->set_public_key(
decoded_key);
device->SetMetadata(DeviceMetadata(response));
}
absl::Status TriggerKeyBasedGattChanged() {
@@ -208,10 +210,10 @@ TEST_F(FastPairHandshakeImplTest, Success) {
notified = true;
EXPECT_OK(TriggerKeyBasedGattChanged());
});
SetUpFastPairRepository();
InsertCorrectGattCharacteristics();
fast_pair_device_ = std::make_unique<FastPairDevice>(
kMetadataId, provider_address_, Protocol::kFastPairInitialPairing);
SetUpValidAntiSpoofingKey(fast_pair_device_.get());
CountDownLatch latch(1);
handshake_ = std::make_unique<FastPairHandshakeImpl>(
*fast_pair_device_, mediums_,
@@ -233,9 +235,9 @@ TEST_F(FastPairHandshakeImplTest, GattError) {
notified = true;
EXPECT_OK(TriggerKeyBasedGattChanged());
});
SetUpFastPairRepository();
fast_pair_device_ = std::make_unique<FastPairDevice>(
kMetadataId, provider_address_, Protocol::kFastPairInitialPairing);
SetUpValidAntiSpoofingKey(fast_pair_device_.get());
CountDownLatch latch(1);
handshake_ = std::make_unique<FastPairHandshakeImpl>(
*fast_pair_device_, mediums_,
@@ -256,10 +258,10 @@ TEST_F(FastPairHandshakeImplTest, DataEncryptorCreateError) {
notified = true;
EXPECT_OK(TriggerKeyBasedGattChanged());
});
FailedFastPairRepository();
InsertCorrectGattCharacteristics();
fast_pair_device_ = std::make_unique<FastPairDevice>(
kMetadataId, provider_address_, Protocol::kFastPairInitialPairing);
SetUpInvalidAntiSpoofingKey(fast_pair_device_.get());
CountDownLatch latch(1);
handshake_ = std::make_unique<FastPairHandshakeImpl>(
*fast_pair_device_, mediums_,
@@ -276,10 +278,10 @@ TEST_F(FastPairHandshakeImplTest, DataEncryptorCreateError) {
TEST_F(FastPairHandshakeImplTest, WriteResponseError) {
StartGattServer([]() {});
SetUpFastPairRepository();
InsertCorrectGattCharacteristics();
fast_pair_device_ = std::make_unique<FastPairDevice>(
kMetadataId, provider_address_, Protocol::kFastPairInitialPairing);
SetUpValidAntiSpoofingKey(fast_pair_device_.get());
CountDownLatch latch(1);
handshake_ = std::make_unique<FastPairHandshakeImpl>(
*fast_pair_device_, mediums_,
@@ -300,10 +302,10 @@ TEST_F(FastPairHandshakeImplTest, WriteResponseWrongSize) {
notified = true;
EXPECT_OK(TriggerKeyBasedGattChangedWithWrongSizeResponse());
});
SetUpFastPairRepository();
InsertCorrectGattCharacteristics();
fast_pair_device_ = std::make_unique<FastPairDevice>(
kMetadataId, provider_address_, Protocol::kFastPairInitialPairing);
SetUpValidAntiSpoofingKey(fast_pair_device_.get());
CountDownLatch latch(1);
handshake_ = std::make_unique<FastPairHandshakeImpl>(
*fast_pair_device_, mediums_,
@@ -325,10 +327,10 @@ TEST_F(FastPairHandshakeImplTest, ParseResponseError) {
notified = true;
EXPECT_OK(TriggerKeyBasedGattChangedWithWrongResponse());
});
SetUpFastPairRepository();
InsertCorrectGattCharacteristics();
fast_pair_device_ = std::make_unique<FastPairDevice>(
kMetadataId, provider_address_, Protocol::kFastPairInitialPairing);
SetUpValidAntiSpoofingKey(fast_pair_device_.get());
CountDownLatch latch(1);
handshake_ = std::make_unique<FastPairHandshakeImpl>(
*fast_pair_device_, mediums_,
@@ -35,7 +35,9 @@
#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/handshake/fast_pair_data_encryptor_impl.h"
#include "fastpair/handshake/fast_pair_handshake_impl.h"
@@ -133,13 +135,18 @@ class FastPairPairerImplTest : public testing::Test {
void CreateMockDevice(DeviceFastPairVersion version, Protocol protocol) {
device_ = std::make_unique<FastPairDevice>(
kMetadataId, remote_device_->GetMacAddress(), protocol);
device_->SetVersion(version);
if (version == DeviceFastPairVersion::kV1) {
device_->SetPublicAddress(remote_device_->GetMacAddress());
}
if (protocol == Protocol::kFastPairSubsequentPairing) {
device_->SetAccountKey(AccountKey(account_key_));
}
CountDownLatch latch(1);
repository_->GetDeviceMetadata(kMetadataId, [&](DeviceMetadata& metadata) {
device_->SetMetadata(std::move(metadata));
latch.CountDown();
});
latch.Await();
}
void ConfigurePairingContext() {
@@ -190,8 +197,11 @@ class FastPairPairerImplTest : public testing::Test {
}
// Sets up provider's metadata information.
void SetUpFastPairRepository() {
repository_ = FakeFastPairRepository::Create(kMetadataId, kPublicAntiSpoof);
void SetUpFastPairRepository(DeviceFastPairVersion version) {
repository_ = FakeFastPairRepository::Create(
kMetadataId, version == DeviceFastPairVersion::kHigherThanV1
? kPublicAntiSpoof
: "");
}
// Sets upprovider's gatt_server.
@@ -352,12 +362,12 @@ class FastPairPairerImplTest : public testing::Test {
TEST_F(FastPairPairerImplTest,
SuccessInitialPairingWithDeviceVersionHigherThanV1) {
ConfigurePairingContext();
CreateMockDevice(DeviceFastPairVersion::kHigherThanV1,
Protocol::kFastPairInitialPairing);
bool triggered_keybase_value_change = false;
bool triggered_passkey_value_change = false;
SetUpFastPairRepository();
SetUpFastPairRepository(DeviceFastPairVersion::kHigherThanV1);
CreateMockDevice(DeviceFastPairVersion::kHigherThanV1,
Protocol::kFastPairInitialPairing);
SetupProviderGattServer(
[&]() {
triggered_keybase_value_change = true;
@@ -403,11 +413,11 @@ TEST_F(FastPairPairerImplTest,
TEST_F(FastPairPairerImplTest, SuccessInitialPairingWithDeviceV1) {
ConfigurePairingContext();
CreateMockDevice(DeviceFastPairVersion::kV1,
Protocol::kFastPairInitialPairing);
bool triggered_keybase_value_change = false;
bool triggered_passkey_value_change = false;
SetUpFastPairRepository();
SetUpFastPairRepository(DeviceFastPairVersion::kV1);
CreateMockDevice(DeviceFastPairVersion::kV1,
Protocol::kFastPairInitialPairing);
SetupProviderGattServer(
[&]() {
triggered_keybase_value_change = true;
@@ -451,11 +461,11 @@ TEST_F(FastPairPairerImplTest, SuccessInitialPairingWithDeviceV1) {
TEST_F(FastPairPairerImplTest, SuccessSubsequentPairingWithDevice) {
ConfigurePairingContext();
CreateMockDevice(DeviceFastPairVersion::kHigherThanV1,
Protocol::kFastPairSubsequentPairing);
bool triggered_keybase_value_change = false;
bool triggered_passkey_value_change = false;
SetUpFastPairRepository();
SetUpFastPairRepository(DeviceFastPairVersion::kHigherThanV1);
CreateMockDevice(DeviceFastPairVersion::kHigherThanV1,
Protocol::kFastPairSubsequentPairing);
SetupProviderGattServer(
[&]() {
triggered_keybase_value_change = true;
@@ -498,11 +508,11 @@ TEST_F(FastPairPairerImplTest, SuccessSubsequentPairingWithDevice) {
TEST_F(FastPairPairerImplTest, SuccessRetroactivePairingWithDevice) {
ConfigurePairingContext();
CreateMockDevice(DeviceFastPairVersion::kHigherThanV1,
Protocol::kFastPairRetroactivePairing);
bool triggered_keybase_value_change = false;
bool triggered_passkey_value_change = false;
SetUpFastPairRepository();
SetUpFastPairRepository(DeviceFastPairVersion::kHigherThanV1);
CreateMockDevice(DeviceFastPairVersion::kHigherThanV1,
Protocol::kFastPairRetroactivePairing);
SetupProviderGattServer(
[&]() {
triggered_keybase_value_change = true;
@@ -546,11 +556,11 @@ TEST_F(FastPairPairerImplTest, SuccessRetroactivePairingWithDevice) {
}
TEST_F(FastPairPairerImplTest, FailedToUnPair) {
CreateMockDevice(DeviceFastPairVersion::kHigherThanV1,
Protocol::kFastPairInitialPairing);
bool triggered_keybase_value_change = false;
bool triggered_passkey_value_change = false;
SetUpFastPairRepository();
SetUpFastPairRepository(DeviceFastPairVersion::kHigherThanV1);
CreateMockDevice(DeviceFastPairVersion::kHigherThanV1,
Protocol::kFastPairInitialPairing);
SetupProviderGattServer(
[&]() {
triggered_keybase_value_change = true;
@@ -595,11 +605,11 @@ TEST_F(FastPairPairerImplTest, FailedToUnPair) {
TEST_F(FastPairPairerImplTest, FailedToPairingWithAuthTimeout) {
ConfigurePairingContext();
CreateMockDevice(DeviceFastPairVersion::kHigherThanV1,
Protocol::kFastPairInitialPairing);
bool triggered_keybase_value_change = false;
bool triggered_passkey_value_change = false;
SetUpFastPairRepository();
SetUpFastPairRepository(DeviceFastPairVersion::kHigherThanV1);
CreateMockDevice(DeviceFastPairVersion::kHigherThanV1,
Protocol::kFastPairInitialPairing);
SetupProviderGattServer(
[&]() {
triggered_keybase_value_change = true;
@@ -645,11 +655,11 @@ TEST_F(FastPairPairerImplTest, FailedToPairingWithAuthTimeout) {
TEST_F(FastPairPairerImplTest, NoPasskeyResponse) {
ConfigurePairingContext();
CreateMockDevice(DeviceFastPairVersion::kHigherThanV1,
Protocol::kFastPairInitialPairing);
bool triggered_keybase_value_change = false;
bool triggered_passkey_value_change = false;
SetUpFastPairRepository();
SetUpFastPairRepository(DeviceFastPairVersion::kHigherThanV1);
CreateMockDevice(DeviceFastPairVersion::kHigherThanV1,
Protocol::kFastPairInitialPairing);
SetupProviderGattServer(
[&]() {
triggered_keybase_value_change = true;
@@ -692,11 +702,11 @@ TEST_F(FastPairPairerImplTest, NoPasskeyResponse) {
TEST_F(FastPairPairerImplTest, PasskeyMismatch) {
ConfigurePairingContext();
CreateMockDevice(DeviceFastPairVersion::kHigherThanV1,
Protocol::kFastPairInitialPairing);
bool triggered_keybase_value_change = false;
bool triggered_passkey_value_change = false;
SetUpFastPairRepository();
SetUpFastPairRepository(DeviceFastPairVersion::kHigherThanV1);
CreateMockDevice(DeviceFastPairVersion::kHigherThanV1,
Protocol::kFastPairInitialPairing);
SetupProviderGattServer(
[&]() {
triggered_keybase_value_change = true;
@@ -742,11 +752,11 @@ TEST_F(FastPairPairerImplTest, PasskeyMismatch) {
TEST_F(FastPairPairerImplTest, ReceiveWithWrongPasskeyResponse) {
ConfigurePairingContext();
CreateMockDevice(DeviceFastPairVersion::kHigherThanV1,
Protocol::kFastPairInitialPairing);
bool triggered_keybase_value_change = false;
bool triggered_passkey_value_change = false;
SetUpFastPairRepository();
SetUpFastPairRepository(DeviceFastPairVersion::kHigherThanV1);
CreateMockDevice(DeviceFastPairVersion::kHigherThanV1,
Protocol::kFastPairInitialPairing);
SetupProviderGattServer(
[&]() {
triggered_keybase_value_change = true;
@@ -792,11 +802,11 @@ TEST_F(FastPairPairerImplTest, ReceiveWithWrongPasskeyResponse) {
TEST_F(FastPairPairerImplTest, ReceiveWithWrongPasskeyMessageType) {
ConfigurePairingContext();
CreateMockDevice(DeviceFastPairVersion::kHigherThanV1,
Protocol::kFastPairInitialPairing);
bool triggered_keybase_value_change = false;
bool triggered_passkey_value_change = false;
SetUpFastPairRepository();
SetUpFastPairRepository(DeviceFastPairVersion::kHigherThanV1);
CreateMockDevice(DeviceFastPairVersion::kHigherThanV1,
Protocol::kFastPairInitialPairing);
SetupProviderGattServer(
[&]() {
triggered_keybase_value_change = true;
@@ -843,11 +853,11 @@ TEST_F(FastPairPairerImplTest, ReceiveWithWrongPasskeyMessageType) {
TEST_F(FastPairPairerImplTest,
SuccessPairingWithDeviceButFailedToWriteAccountkey) {
ConfigurePairingContext();
CreateMockDevice(DeviceFastPairVersion::kHigherThanV1,
Protocol::kFastPairInitialPairing);
bool triggered_keybase_value_change = false;
bool triggered_passkey_value_change = false;
SetUpFastPairRepository();
SetUpFastPairRepository(DeviceFastPairVersion::kHigherThanV1);
CreateMockDevice(DeviceFastPairVersion::kHigherThanV1,
Protocol::kFastPairInitialPairing);
SetupProviderGattServer(
[&]() {
triggered_keybase_value_change = true;
@@ -893,11 +903,11 @@ TEST_F(FastPairPairerImplTest,
TEST_F(FastPairPairerImplTest, TestCancelPairing) {
ConfigurePairingContext();
CreateMockDevice(DeviceFastPairVersion::kHigherThanV1,
Protocol::kFastPairInitialPairing);
bool triggered_keybase_value_change = false;
bool triggered_passkey_value_change = false;
SetUpFastPairRepository();
SetUpFastPairRepository(DeviceFastPairVersion::kHigherThanV1);
CreateMockDevice(DeviceFastPairVersion::kHigherThanV1,
Protocol::kFastPairInitialPairing);
SetupProviderGattServer(
[&]() {
triggered_keybase_value_change = true;
+22 -14
View File
@@ -179,13 +179,18 @@ class PairerBrokerImplTest : public testing::Test {
void CreateMockDevice(DeviceFastPairVersion version, Protocol protocol) {
device_ = std::make_unique<FastPairDevice>(
kMetadataId, remote_device_->GetMacAddress(), protocol);
device_->SetVersion(version);
if (version == DeviceFastPairVersion::kV1) {
device_->SetPublicAddress(remote_device_->GetMacAddress());
}
if (protocol == Protocol::kFastPairSubsequentPairing) {
device_->SetAccountKey(AccountKey(account_key_));
}
CountDownLatch latch(1);
repository_->GetDeviceMetadata(kMetadataId, [&](DeviceMetadata& metadata) {
device_->SetMetadata(std::move(metadata));
latch.CountDown();
});
latch.Await();
}
void ConfigurePairingContext() {
@@ -236,8 +241,11 @@ class PairerBrokerImplTest : public testing::Test {
}
// Sets up provider's metadata information.
void SetUpFastPairRepository() {
repository_ = FakeFastPairRepository::Create(kMetadataId, kPublicAntiSpoof);
void SetUpFastPairRepository(DeviceFastPairVersion version) {
repository_ = FakeFastPairRepository::Create(
kMetadataId, version == DeviceFastPairVersion::kHigherThanV1
? kPublicAntiSpoof
: "");
}
// Sets upprovider's gatt_server.
@@ -411,11 +419,11 @@ class PairerBrokerImplTest : public testing::Test {
TEST_F(PairerBrokerImplTest, SuccessInitialPairingWithDeviceV1) {
ConfigurePairingContext();
CreateMockDevice(DeviceFastPairVersion::kV1,
Protocol::kFastPairInitialPairing);
bool triggered_keybase_value_change = false;
bool triggered_passkey_value_change = false;
SetUpFastPairRepository();
SetUpFastPairRepository(DeviceFastPairVersion::kV1);
CreateMockDevice(DeviceFastPairVersion::kV1,
Protocol::kFastPairInitialPairing);
SetupProviderGattServer(
[&]() {
triggered_keybase_value_change = true;
@@ -455,9 +463,9 @@ TEST_F(PairerBrokerImplTest, SuccessInitialPairingWithDevice) {
CountDownLatch pairing_completed_latch(1);
CountDownLatch pairing_failure_latch(1);
ConfigurePairingContext();
SetUpFastPairRepository(DeviceFastPairVersion::kHigherThanV1);
CreateMockDevice(DeviceFastPairVersion::kHigherThanV1,
Protocol::kFastPairInitialPairing);
SetUpFastPairRepository();
SetupProviderGattServer(
[&]() {
triggered_keybase_value_change = true;
@@ -498,9 +506,9 @@ TEST_F(PairerBrokerImplTest, SuccessSubsequentPairingWithDevice) {
CountDownLatch pairing_completed_latch(1);
CountDownLatch pairing_failure_latch(1);
ConfigurePairingContext();
SetUpFastPairRepository(DeviceFastPairVersion::kHigherThanV1);
CreateMockDevice(DeviceFastPairVersion::kHigherThanV1,
Protocol::kFastPairSubsequentPairing);
SetUpFastPairRepository();
pairer_broker_ = std::make_unique<PairerBrokerImpl>(*mediums_, &executor_);
PairerBrokerObserver pairer_broker_observer(
pairer_broker_.get(), &device_paired_latch, &account_key_writed_latch,
@@ -535,11 +543,11 @@ TEST_F(PairerBrokerImplTest, SuccessSubsequentPairingWithDevice) {
TEST_F(PairerBrokerImplTest, SuccessRetroactivePairingWithDevice) {
ConfigurePairingContext();
CreateMockDevice(DeviceFastPairVersion::kHigherThanV1,
Protocol::kFastPairRetroactivePairing);
bool triggered_keybase_value_change = false;
bool triggered_passkey_value_change = false;
SetUpFastPairRepository();
SetUpFastPairRepository(DeviceFastPairVersion::kHigherThanV1);
CreateMockDevice(DeviceFastPairVersion::kHigherThanV1,
Protocol::kFastPairRetroactivePairing);
SetupProviderGattServer(
[&]() {
triggered_keybase_value_change = true;
@@ -579,9 +587,9 @@ TEST_F(PairerBrokerImplTest, FaileToCreateHandshakeRetryThreeTimes) {
CountDownLatch pairing_completed_latch(1);
CountDownLatch pairing_failure_latch(1);
ConfigurePairingContext();
SetUpFastPairRepository(DeviceFastPairVersion::kHigherThanV1);
CreateMockDevice(DeviceFastPairVersion::kHigherThanV1,
Protocol::kFastPairSubsequentPairing);
SetUpFastPairRepository();
pairer_broker_ = std::make_unique<PairerBrokerImpl>(*mediums_, &executor_);
PairerBrokerObserver pairer_broker_observer(
pairer_broker_.get(), &device_paired_latch, &account_key_writed_latch,
@@ -614,9 +622,9 @@ TEST_F(PairerBrokerImplTest, FaileToWriteAccountkey) {
CountDownLatch pairing_completed_latch(1);
CountDownLatch pairing_failure_latch(1);
ConfigurePairingContext();
SetUpFastPairRepository(DeviceFastPairVersion::kHigherThanV1);
CreateMockDevice(DeviceFastPairVersion::kHigherThanV1,
Protocol::kFastPairInitialPairing);
SetUpFastPairRepository();
SetupProviderGattServer(
[&]() {
triggered_keybase_value_change = true;
@@ -657,9 +665,9 @@ TEST_F(PairerBrokerImplTest, FailToPairRetryThreeTimes) {
CountDownLatch pairing_completed_latch(1);
CountDownLatch pairing_failure_latch(1);
ConfigurePairingContext();
SetUpFastPairRepository(DeviceFastPairVersion::kHigherThanV1);
CreateMockDevice(DeviceFastPairVersion::kHigherThanV1,
Protocol::kFastPairInitialPairing);
SetUpFastPairRepository();
SetupProviderGattServer(
[&]() {
triggered_keybase_value_change = true;
-1
View File
@@ -7,7 +7,6 @@ cc_library(
"fast_pair_metadata_repository_impl.cc",
],
hdrs = [
"device_metadata.h",
"fast_pair_metadata_fetcher.h",
"fast_pair_metadata_fetcher_impl.h",
"fast_pair_metadata_repository.h",
@@ -181,7 +181,7 @@ void FastPairDiscoverableScannerImpl::OnDeviceMetadataRetrieved(
}
auto fast_pair_device = std::make_unique<FastPairDevice>(
model_id, address, Protocol::kFastPairInitialPairing);
fast_pair_device->SetVersion(device_metadata.GetFastPairVersion());
fast_pair_device->SetMetadata(device_metadata);
executor_->Execute(
"add-device",
@@ -20,8 +20,8 @@
#include <string>
#include <vector>
#include "fastpair/common/device_metadata.h"
#include "fastpair/common/fast_pair_device.h"
#include "fastpair/repository/device_metadata.h"
#include "fastpair/repository/fast_pair_device_repository.h"
#include "fastpair/scanning/fastpair/fast_pair_discoverable_scanner.h"
#include "fastpair/scanning/fastpair/fast_pair_scanner.h"
-2
View File
@@ -61,7 +61,6 @@ cc_library(
":server_access",
"//fastpair/common",
"//fastpair/proto:fastpair_cc_proto",
"//fastpair/repository",
"//internal/platform:types",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/strings",
@@ -97,7 +96,6 @@ cc_test(
":server_access",
"//fastpair/common",
"//fastpair/proto:fastpair_cc_proto",
"//fastpair/repository",
"//fastpair/repository:fake_fast_pair_metadata_repository",
"//internal/platform/implementation/g3",
"@com_github_protobuf_matchers//protobuf-matchers",
@@ -18,7 +18,9 @@
#include <string>
#include <utility>
#include "absl/strings/escaping.h"
#include "absl/strings/string_view.h"
#include "fastpair/common/constant.h"
#include "fastpair/proto/fastpair_rpcs.proto.h"
namespace nearby {
@@ -48,7 +50,9 @@ std::unique_ptr<FakeFastPairRepository> FakeFastPairRepository::Create(
absl::string_view model_id, absl::string_view public_anti_spoof_key) {
proto::Device metadata;
auto repository = std::make_unique<FakeFastPairRepository>();
if (public_anti_spoof_key.length() == kPublicKeyByteSize) {
if (public_anti_spoof_key.empty()) {
// Missing ASK is fine for V1 devices.
} else if (public_anti_spoof_key.length() == kPublicKeyByteSize) {
metadata.mutable_anti_spoofing_key_pair()->set_public_key(
public_anti_spoof_key);
} else {
@@ -20,7 +20,7 @@
#include "absl/container/flat_hash_map.h"
#include "absl/strings/string_view.h"
#include "fastpair/repository/device_metadata.h"
#include "fastpair/common/device_metadata.h"
#include "fastpair/server_access/fast_pair_repository.h"
#include "internal/platform/single_thread_executor.h"
@@ -18,10 +18,10 @@
#include <string>
#include <utility>
#include "fastpair/proto/fastpair_rpcs.pb.h"
#include "fastpair/repository/device_metadata.h"
#include "internal/platform/logging.h"
#include "absl/strings/string_view.h"
#include "fastpair/common/device_metadata.h"
#include "fastpair/proto/fastpair_rpcs.pb.h"
#include "internal/platform/logging.h"
namespace nearby {
namespace fastpair {
@@ -21,7 +21,7 @@
#include "absl/functional/any_invocable.h"
#include "absl/strings/string_view.h"
#include "fastpair/repository/device_metadata.h"
#include "fastpair/common/device_metadata.h"
namespace nearby {
namespace fastpair {
@@ -23,7 +23,7 @@
#include "absl/strings/numbers.h"
#include "absl/strings/string_view.h"
#include "fastpair/repository/device_metadata.h"
#include "fastpair/common/device_metadata.h"
#include "fastpair/repository/fast_pair_metadata_repository.h"
#include "fastpair/server_access/fast_pair_metadata_downloader.h"
#include "internal/platform/logging.h"
@@ -22,9 +22,9 @@
#include "gtest/gtest.h"
#include "absl/strings/string_view.h"
#include "fastpair/common/device_metadata.h"
#include "fastpair/common/fast_pair_http_result.h"
#include "fastpair/proto/fastpair_rpcs.proto.h"
#include "fastpair/repository/device_metadata.h"
#include "fastpair/repository/fake_fast_pair_metadata_repository.h"
#include "fastpair/server_access/fast_pair_metadata_downloader.h"
@@ -21,7 +21,7 @@
#include "absl/functional/any_invocable.h"
#include "absl/strings/string_view.h"
#include "fastpair/repository/device_metadata.h"
#include "fastpair/common/device_metadata.h"
namespace nearby {
namespace fastpair {
@@ -23,8 +23,8 @@
#include "gmock/gmock.h"
#include "protobuf-matchers/protocol-buffer-matchers.h"
#include "gtest/gtest.h"
#include "fastpair/common/device_metadata.h"
#include "fastpair/proto/fastpair_rpcs.proto.h"
#include "fastpair/repository/device_metadata.h"
#include "fastpair/repository/fake_fast_pair_metadata_repository.h"
#include "fastpair/server_access/fast_pair_metadata_downloader.h"
#include "fastpair/server_access/fast_pair_metadata_downloader_impl.h"
@@ -53,7 +53,7 @@ class FastPairRepositoryImplTest : public ::testing::Test {
void GetObservedDataRequestSuccess(
const proto::GetObservedDeviceResponse& response) {
FakeFastPairMetadataRepository* repository =
fake_repository_factory_->fake_repository();
fake_repository_factory_->fake_repository();
std::move(repository->get_observed_device_request()->callback)(response);
}
@@ -64,8 +64,7 @@ class FastPairRepositoryImplTest : public ::testing::Test {
}
std::optional<Result> result_;
FakeFastPairMetadataRepositoryFactory*
fake_repository_factory_;
FakeFastPairMetadataRepositoryFactory* fake_repository_factory_;
std::unique_ptr<FastPairRepositoryImpl> repository_;
};
+1 -1
View File
@@ -88,7 +88,7 @@ cc_test(
deps = [
":fake_fast_pair_ui",
":fast_pair_ui",
"//fastpair/repository",
"//fastpair/common",
"//internal/platform/implementation/g3", # build_cleaner: keep
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_googletest//:gtest_main",
@@ -20,7 +20,7 @@
#include <string>
#include <vector>
#include "fastpair/repository/device_metadata.h"
#include "fastpair/common/device_metadata.h"
#include "fastpair/ui/fast_pair/fast_pair_notification_controller.h"
#include "internal/platform/count_down_latch.h"
@@ -17,7 +17,7 @@
#include <utility>
#include "absl/functional/any_invocable.h"
#include "fastpair/repository/device_metadata.h"
#include "fastpair/common/device_metadata.h"
#include "fastpair/ui/actions.h"
#include "internal/platform/logging.h"
@@ -17,7 +17,7 @@
#include "absl/functional/any_invocable.h"
#include "absl/strings/string_view.h"
#include "fastpair/repository/device_metadata.h"
#include "fastpair/common/device_metadata.h"
#include "fastpair/ui/actions.h"
#include "internal/base/observer_list.h"
@@ -22,7 +22,7 @@
#include "gmock/gmock.h"
#include "protobuf-matchers/protocol-buffer-matchers.h"
#include "gtest/gtest.h"
#include "fastpair/repository/device_metadata.h"
#include "fastpair/common/device_metadata.h"
#include "fastpair/ui/actions.h"
#include "fastpair/ui/fast_pair/fake_fast_pair_notification_controller_observer.h"
@@ -18,8 +18,8 @@
#include <optional>
#include <utility>
#include "fastpair/common/device_metadata.h"
#include "fastpair/common/fast_pair_device.h"
#include "fastpair/repository/device_metadata.h"
#include "fastpair/server_access/fast_pair_repository.h"
#include "fastpair/ui/fast_pair/fast_pair_notification_controller.h"
#include "internal/platform/logging.h"
@@ -64,8 +64,8 @@ void FastPairPresenterImpl::ShowDiscovery(
void FastPairPresenterImpl::OnDiscoveryMetadataRetrieved(
FastPairDevice& device, const DeviceMetadata& device_metadata,
FastPairNotificationController& notification_controller) {
device.SetVersion(device_metadata.GetFastPairVersion());
notification_controller.ShowGuestDiscoveryNotification(device_metadata,
device.SetMetadata(device_metadata);
notification_controller.ShowGuestDiscoveryNotification(*device.GetMetadata(),
std::move(callback_));
}
} // namespace fastpair
@@ -17,8 +17,8 @@
#include <memory>
#include "fastpair/common/device_metadata.h"
#include "fastpair/common/fast_pair_device.h"
#include "fastpair/repository/device_metadata.h"
#include "fastpair/ui/fast_pair/fast_pair_notification_controller.h"
#include "fastpair/ui/fast_pair/fast_pair_presenter.h"
@@ -16,7 +16,7 @@
#define THIRD_PARTY_NEARBY_FASTPAIR_UI_FAST_PAIR_MOCK_FAST_PAIR_NOTIFICATION_CONTROLLER_H_
#include "gmock/gmock.h"
#include "fastpair/repository/device_metadata.h"
#include "fastpair/common/device_metadata.h"
#include "fastpair/ui/actions.h"
#include "fastpair/ui/fast_pair/fast_pair_notification_controller.h"