Add ability to load single PublicCertificate by ID.

PiperOrigin-RevId: 700754672
This commit is contained in:
Francis Tsui
2024-11-27 11:10:27 -08:00
committed by Copybara-Service
parent dd7717886a
commit 79f1a2e492
14 changed files with 247 additions and 81 deletions
-1
View File
@@ -57,7 +57,6 @@ cc_test(
srcs = [
"leveldb_data_set_test.cc",
],
shard_count = 8,
deps = [
":data_manager",
":leveldb_data_set_test_cc_proto",
+14 -9
View File
@@ -21,9 +21,9 @@
#include <vector>
#include "absl/functional/any_invocable.h"
#include "absl/strings/string_view.h"
namespace nearby {
namespace data {
namespace nearby::data {
enum class InitStatus {
kOK = 0,
@@ -42,19 +42,25 @@ class DataSet {
virtual ~DataSet() = default;
// Asynchronously initializes the object, which must have been created by the
// DataManager::GetDataSet<T> function. |callback| will be invoked on the
// DataManager::GetDataSet<T> function. `callback` will be invoked on the
// calling thread when complete.
virtual void Initialize(absl::AnyInvocable<void(InitStatus) &&> callback) = 0;
// Asynchronously loads all entries from the database and invokes |callback|
// Asynchronously loads all entries from the database and invokes `callback`
// when complete.
virtual void LoadEntries(
absl::AnyInvocable<void(bool, std::unique_ptr<std::vector<T>>) &&>
callback) = 0;
// Asynchronously saves |entries_to_save| and deletes entries from
// |keys_to_remove| from the database. |callback| will be invoked on the
// calling thread when complete. |entries_to_save| and |keys_to_remove| must
// Asynchronously loads an entry from the database with key `key` and invokes
// `callback` when complete.
virtual void LoadEntry(
absl::string_view key,
absl::AnyInvocable<void(bool, std::unique_ptr<T>) &&> callback) = 0;
// Asynchronously saves `entries_to_save` and deletes entries from
// `keys_to_remove` from the database. `callback` will be invoked on the
// calling thread when complete. `entries_to_save` and `keys_to_remove` must
// be non-null.
virtual void UpdateEntries(
std::unique_ptr<KeyEntryVector> entries_to_save,
@@ -66,7 +72,6 @@ class DataSet {
virtual void Destroy(absl::AnyInvocable<void(bool) &&> callback) = 0;
};
} // namespace data
} // namespace nearby
} // namespace nearby::data
#endif // THIRD_PARTY_NEARBY_INTERNAL_DATA_DATA_SET_H_
+34 -11
View File
@@ -47,6 +47,9 @@ class LeveldbDataSet : public DataSet<T> {
~LeveldbDataSet() override = default;
void Initialize(absl::AnyInvocable<void(InitStatus) &&> callback) override;
void LoadEntry(
absl::string_view key,
absl::AnyInvocable<void(bool, std::unique_ptr<T>) &&> callback) override;
void LoadEntries(
absl::AnyInvocable<void(bool, std::unique_ptr<std::vector<T>>) &&>
callback) override;
@@ -84,14 +87,14 @@ void LeveldbDataSet<T, isMessageLite>::Initialize(
if (status.ok()) {
status_ = InitStatus::kOK;
NEARBY_LOGS(INFO) << "Database is initialized successfully..";
LOG(INFO) << "Database is initialized successfully..";
} else if (status.IsCorruption() || status.IsIOError()) {
status_ = InitStatus::kCorrupt;
NEARBY_LOGS(INFO) << "Database is corrupt.";
LOG(INFO) << "Database is corrupt.";
} else {
status_ = InitStatus::kError;
NEARBY_LOGS(INFO) << "Failed to initialize database due to unknown error.";
LOG(INFO) << "Failed to initialize database due to unknown error.";
}
std::move(callback)(status_);
}
@@ -118,16 +121,37 @@ void LeveldbDataSet<T, isMessageLite>::LoadEntries(
}
if (it->status().ok()) {
NEARBY_LOGS(INFO) << "Loaded " << result->size()
<< " entries from database.";
LOG(INFO) << "Loaded " << result->size() << " entries from database.";
std::move(callback)(true, std::move(result));
} else {
NEARBY_LOGS(INFO) << "Failed to load entries from database.";
LOG(INFO) << "Failed to load entries from database.";
result->clear();
std::move(callback)(false, std::move(result));
}
}
template <typename T,
std::enable_if_t<std::is_base_of<proto2::MessageLite, T>::value, bool>
isMessageLite>
void LeveldbDataSet<T, isMessageLite>::LoadEntry(
absl::string_view key,
absl::AnyInvocable<void(bool, std::unique_ptr<T>) &&> callback) {
auto result = std::make_unique<T>();
if (status_ != InitStatus::kOK) {
std::move(callback)(false, std::move(result));
return;
}
std::string value;
if (!db_->Get(leveldb::ReadOptions(), std::string(key), &value).ok()) {
LOG(INFO) << "Failed to load entry from database with key: " << key;
std::move(callback)(false, std::move(result));
return;
}
Deserialize(value, *result);
std::move(callback)(true, std::move(result));
}
template <typename T,
std::enable_if_t<std::is_base_of<proto2::MessageLite, T>::value, bool>
isMessageLite>
@@ -151,11 +175,10 @@ void LeveldbDataSet<T, isMessageLite>::LoadEntriesWithKeys(
}
if (it->status().ok()) {
NEARBY_LOGS(INFO) << "Loaded " << result->size()
<< " entries from database.";
LOG(INFO) << "Loaded " << result->size() << " entries from database.";
std::move(callback)(true, std::move(result));
} else {
NEARBY_LOGS(INFO) << "Failed to load entries from database.";
LOG(INFO) << "Failed to load entries from database.";
result->clear();
std::move(callback)(false, std::move(result));
}
@@ -168,7 +191,7 @@ void LeveldbDataSet<T, isMessageLite>::UpdateEntries(
std::unique_ptr<KeyEntryVector> entries_to_save,
std::unique_ptr<std::vector<std::string>> keys_to_remove,
absl::AnyInvocable<void(bool) &&> callback) {
NEARBY_LOGS(INFO) << "UpdateEntries is called.";
LOG(INFO) << "UpdateEntries is called.";
if (status_ != InitStatus::kOK) {
std::move(callback)(false);
return;
@@ -196,7 +219,7 @@ template <typename T,
isMessageLite>
void LeveldbDataSet<T, isMessageLite>::Destroy(
absl::AnyInvocable<void(bool) &&> callback) {
NEARBY_LOGS(INFO) << "Destroy is called.";
LOG(INFO) << "Destroy is called.";
db_.reset();
leveldb::DestroyDB(path_, leveldb::Options());
std::move(callback)(true);
+32 -4
View File
@@ -34,8 +34,7 @@
#include "internal/data/data_set.h"
#include "internal/data/leveldb_data_set_test.proto.h"
namespace nearby {
namespace data {
namespace nearby::data {
namespace {
using ::testing::SizeIs;
@@ -210,6 +209,36 @@ TEST(LeveldbDataSet, LoadEntriesDiceRoll) {
EXPECT_EQ((*result)[1].nickname(), "boxcars");
}
TEST(LeveldbDataSet, LoadEntrysDiceRoll) {
std::filesystem::path path = GenerateLeveldbPath();
std::unique_ptr<LeveldbDataSet<DiceRoll>> diceroll_set =
CreateDataSet<DiceRoll>(path);
InitializeAndWait(diceroll_set);
DiceRoll diceroll1 = GenerateDiceRoll(2);
DiceRoll diceroll2 = GenerateDiceRoll(12);
auto entries = LeveldbDataSet<DiceRoll>::KeyEntryVector(
{{"id1", diceroll1}, {"id2", diceroll2}});
auto data =
std::make_unique<LeveldbDataSet<DiceRoll>::KeyEntryVector>(entries);
UpdateEntriesAndWait(diceroll_set, std::move(data), nullptr);
absl::Notification notification;
std::unique_ptr<DiceRoll> result;
diceroll_set->LoadEntry(
"id2", [&result, &notification](bool, std::unique_ptr<DiceRoll> res) {
result = std::move(res);
notification.Notify();
});
notification.WaitForNotificationWithTimeout(absl::Seconds(5));
WipeCleanAndWait(diceroll_set, path);
EXPECT_THAT(*result,
protobuf_matchers::EqualsProto<DiceRoll>("value: 12, nickname:'boxcars'"));
}
TEST(LeveldbDataSet, RemoveEntriesDiceRoll) {
std::filesystem::path path = GenerateLeveldbPath();
std::unique_ptr<LeveldbDataSet<DiceRoll>> diceroll_set =
@@ -255,5 +284,4 @@ TEST(LeveldbDataSet, RemoveEntriesDiceRoll) {
}
} // namespace
} // namespace data
} // namespace nearby
} // namespace nearby::data
@@ -14,6 +14,7 @@
#include "sharing/certificates/fake_nearby_share_certificate_storage.h"
#include <functional>
#include <memory>
#include <optional>
#include <string>
@@ -29,8 +30,7 @@
#include "sharing/internal/api/public_certificate_database.h"
#include "sharing/proto/rpc_resources.pb.h"
namespace nearby {
namespace sharing {
namespace nearby::sharing {
using ::nearby::sharing::proto::PublicCertificate;
@@ -101,6 +101,14 @@ void FakeNearbyShareCertificateStorage::GetPublicCertificates(
get_public_certificates_callbacks_.push_back(std::move(callback));
}
void FakeNearbyShareCertificateStorage::GetPublicCertificate(
absl::string_view id,
std::function<
void(bool, std::unique_ptr<nearby::sharing::proto::PublicCertificate>)>
callback) {
get_public_certificate_callback_ = std::move(callback);
}
std::optional<std::vector<NearbySharePrivateCertificate>>
FakeNearbyShareCertificateStorage::GetPrivateCertificates() const {
return private_certificates_;
@@ -152,5 +160,4 @@ void FakeNearbyShareCertificateStorage::SetNextPublicCertificateExpirationTime(
next_public_certificate_expiration_time_ = time;
}
} // namespace sharing
} // namespace nearby
} // namespace nearby::sharing
@@ -15,6 +15,7 @@
#ifndef THIRD_PARTY_NEARBY_SHARING_CERTIFICATES_FAKE_NEARBY_SHARE_CERTIFICATE_STORAGE_H_
#define THIRD_PARTY_NEARBY_SHARING_CERTIFICATES_FAKE_NEARBY_SHARE_CERTIFICATE_STORAGE_H_
#include <functional>
#include <memory>
#include <optional>
#include <string>
@@ -102,6 +103,11 @@ class FakeNearbyShareCertificateStorage : public NearbyShareCertificateStorage {
// NearbyShareCertificateStorage:
std::vector<std::string> GetPublicCertificateIds() const override;
void GetPublicCertificates(PublicCertificateCallback callback) override;
void GetPublicCertificate(
absl::string_view id,
std::function<void(
bool, std::unique_ptr<nearby::sharing::proto::PublicCertificate>)>
callback) override;
std::optional<std::vector<NearbySharePrivateCertificate>>
GetPrivateCertificates() const override;
std::optional<absl::Time> NextPublicCertificateExpirationTime()
@@ -153,6 +159,9 @@ class FakeNearbyShareCertificateStorage : public NearbyShareCertificateStorage {
std::optional<std::vector<NearbySharePrivateCertificate>>
private_certificates_;
std::vector<PublicCertificateCallback> get_public_certificates_callbacks_;
std::function<void(
bool, std::unique_ptr<nearby::sharing::proto::PublicCertificate>)>
get_public_certificate_callback_;
std::vector<AddPublicCertificatesCall> add_public_certificates_calls_;
std::vector<RemoveExpiredPublicCertificatesCall>
remove_expired_public_certificates_calls_;
@@ -21,15 +21,14 @@
#include <string>
#include <vector>
#include "absl/strings/string_view.h"
#include "absl/time/time.h"
#include "absl/types/span.h"
#include "sharing/certificates/nearby_share_private_certificate.h"
#include "sharing/common/nearby_share_enums.h"
#include "sharing/proto/enums.pb.h"
#include "sharing/proto/rpc_resources.pb.h"
namespace nearby {
namespace sharing {
namespace nearby::sharing {
// Stores local-device private certificates and remote-device public
// certificates. Provides methods to help manage certificate expiration. Due to
@@ -51,6 +50,13 @@ class NearbyShareCertificateStorage {
// Returns all public certificates currently in storage. No RPC call is made.
virtual void GetPublicCertificates(PublicCertificateCallback callback) = 0;
// Returns a single public certificate with the given id.
virtual void GetPublicCertificate(
absl::string_view id,
std::function<void(
bool, std::unique_ptr<nearby::sharing::proto::PublicCertificate>)>
callback) = 0;
// Returns all private certificates currently in storage. Will return
// absl::nullopt if deserialization from prefs fails -- not expected to happen
// under normal circumstances.
@@ -102,7 +108,6 @@ class NearbyShareCertificateStorage {
virtual void ClearPublicCertificates(ResultCallback callback) = 0;
};
} // namespace sharing
} // namespace nearby
} // namespace nearby::sharing
#endif // THIRD_PARTY_NEARBY_SHARING_CERTIFICATES_NEARBY_SHARE_CERTIFICATE_STORAGE_H_
@@ -44,8 +44,7 @@
#include "sharing/proto/rpc_resources.pb.h"
#include "sharing/proto/timestamp.pb.h"
namespace nearby {
namespace sharing {
namespace nearby::sharing {
namespace {
using ::nearby::sharing::api::PreferenceManager;
using ::nearby::sharing::api::PrivateCertificateData;
@@ -158,10 +157,10 @@ void NearbyShareCertificateStorageImpl::Initialize() {
break;
}
NL_VLOG(1) << __func__
<< ": Attempting to initialize public certificate "
"database. Number of attempts: "
<< num_initialize_attempts_;
VLOG(1) << __func__
<< ": Attempting to initialize public certificate "
"database. Number of attempts: "
<< num_initialize_attempts_;
public_certificate_database_->Initialize(
[weak_this =
weak_from_this()](PublicCertificateDatabase::InitStatus status) {
@@ -171,23 +170,23 @@ void NearbyShareCertificateStorageImpl::Initialize() {
});
break;
case InitStatus::kInitialized:
NL_LOG(INFO) << __func__ << " already initialized.";
LOG(INFO) << __func__ << " already initialized.";
break;
}
}
void NearbyShareCertificateStorageImpl::DestroyAndReinitialize() {
NL_LOG(ERROR) << __func__
<< ": Public certificate database corrupt. Erasing and "
"initializing new database.";
LOG(ERROR) << __func__
<< ": Public certificate database corrupt. Erasing and "
"initializing new database.";
init_status_ = InitStatus::kUninitialized;
public_certificate_database_->Destroy(
[weak_this = weak_from_this()](bool success) {
if (auto storage = weak_this.lock()) {
storage->OnDatabaseDestroyedReinitialize(
[&](bool result) {
NL_LOG(INFO)
<< "Destroy and reinitialize database. result: " << result;
LOG(INFO) << "Destroy and reinitialize database. result: "
<< result;
},
success);
}
@@ -197,8 +196,8 @@ void NearbyShareCertificateStorageImpl::DestroyAndReinitialize() {
void NearbyShareCertificateStorageImpl::OnDatabaseInitialized(
absl::Time initialize_start_time,
PublicCertificateDatabase::InitStatus status) {
NL_LOG(INFO) << "Database is initialized for certificates. status="
<< static_cast<int>(status);
LOG(INFO) << "Database is initialized for certificates. status="
<< static_cast<int>(status);
switch (status) {
case PublicCertificateDatabase::InitStatus::kOk:
FinishInitialization(true);
@@ -218,11 +217,11 @@ void NearbyShareCertificateStorageImpl::FinishInitialization(bool success) {
// Need to reset the initialize attempts.
num_initialize_attempts_ = 0;
NL_VLOG(1) << __func__
<< "Public certificate database initialization succeeded.";
VLOG(1) << __func__
<< "Public certificate database initialization succeeded.";
} else {
NL_LOG(ERROR) << __func__
<< "Public certificate database initialization failed.";
LOG(ERROR) << __func__
<< "Public certificate database initialization failed.";
}
// We run deferred callbacks even if initialization failed not to cause
@@ -237,8 +236,8 @@ void NearbyShareCertificateStorageImpl::FinishInitialization(bool success) {
void NearbyShareCertificateStorageImpl::OnDatabaseDestroyedReinitialize(
ResultCallback callback, bool success) {
if (!success) {
NL_LOG(ERROR) << __func__
<< ": Failed to destroy public certificate database.";
LOG(ERROR) << __func__
<< ": Failed to destroy public certificate database.";
FinishInitialization(false);
callback(false);
return;
@@ -254,8 +253,8 @@ void NearbyShareCertificateStorageImpl::OnDatabaseDestroyedReinitialize(
void NearbyShareCertificateStorageImpl::OnDatabaseDestroyed(
ResultCallback callback, bool success) {
if (!success) {
NL_LOG(ERROR) << __func__
<< ": Failed to destroy public certificate database.";
LOG(ERROR) << __func__
<< ": Failed to destroy public certificate database.";
std::move(callback)(false);
return;
}
@@ -270,11 +269,11 @@ void NearbyShareCertificateStorageImpl::AddPublicCertificatesCallback(
std::unique_ptr<ExpirationList> new_expirations, ResultCallback callback,
bool proceed) {
if (!proceed) {
NL_LOG(ERROR) << __func__ << ": Failed to add public certificates.";
LOG(ERROR) << __func__ << ": Failed to add public certificates.";
std::move(callback)(false);
return;
}
NL_VLOG(1) << __func__ << ": Successfully added public certificates.";
VLOG(1) << __func__ << ": Successfully added public certificates.";
public_certificate_expirations_ =
MergeExpirations(public_certificate_expirations_, *new_expirations);
@@ -286,13 +285,11 @@ void NearbyShareCertificateStorageImpl::RemoveExpiredPublicCertificatesCallback(
const absl::flat_hash_set<std::string>& ids_to_remove,
ResultCallback callback, bool proceed) {
if (!proceed) {
NL_LOG(ERROR) << __func__
<< ": Failed to remove expired public certificates.";
LOG(ERROR) << __func__ << ": Failed to remove expired public certificates.";
std::move(callback)(false);
return;
}
NL_VLOG(1) << __func__
<< ": Expired public certificates successfully removed.";
VLOG(1) << __func__ << ": Expired public certificates successfully removed.";
auto should_remove =
[&](const std::pair<std::string, absl::Time>& pair) -> bool {
@@ -330,10 +327,31 @@ void NearbyShareCertificateStorageImpl::GetPublicCertificates(
return;
}
NL_VLOG(1) << __func__ << ": Calling LoadEntries on database.";
VLOG(1) << __func__ << ": Calling LoadEntries on database.";
public_certificate_database_->LoadEntries(std::move(callback));
}
void NearbyShareCertificateStorageImpl::GetPublicCertificate(
absl::string_view id,
std::function<
void(bool, std::unique_ptr<nearby::sharing::proto::PublicCertificate>)>
callback) {
if (init_status_ == InitStatus::kFailed) {
std::move(callback)(false, nullptr);
return;
}
if (init_status_ == InitStatus::kUninitialized) {
deferred_callbacks_.push(
[this, id = std::string(id), callback = std::move(callback)]() mutable {
GetPublicCertificate(id, std::move(callback));
});
return;
}
VLOG(1) << __func__ << ": Calling LoadCertificate on database, key: " << id;
public_certificate_database_->LoadCertificate(id, std::move(callback));
}
std::optional<std::vector<NearbySharePrivateCertificate>>
NearbyShareCertificateStorageImpl::GetPrivateCertificates() const {
std::vector<PrivateCertificateData> list =
@@ -395,9 +413,9 @@ void NearbyShareCertificateStorageImpl::AddPublicCertificates(
}
std::sort(new_expirations.begin(), new_expirations.end(), SortBySecond);
NL_VLOG(1) << __func__
<< ": Calling UpdateEntries on public certificate database with "
<< public_certificates.size() << " certificates.";
VLOG(1) << __func__
<< ": Calling UpdateEntries on public certificate database with "
<< public_certificates.size() << " certificates.";
public_certificate_database_->AddCertificates(
public_certificates, [weak_this = weak_from_this(), new_expirations,
callback = std::move(callback)](bool success) {
@@ -443,10 +461,9 @@ void NearbyShareCertificateStorageImpl::RemoveExpiredPublicCertificates(
return;
}
NL_VLOG(1)
<< __func__
<< ": Calling UpdateEntries on public certificate database to remove "
<< ids_to_remove.size() << " expired certificates.";
VLOG(1) << __func__
<< ": Calling UpdateEntries on public certificate database to remove "
<< ids_to_remove.size() << " expired certificates.";
absl::flat_hash_set<std::string> remove_set(ids_to_remove.begin(),
ids_to_remove.end());
public_certificate_database_->RemoveCertificatesById(
@@ -467,7 +484,7 @@ void NearbyShareCertificateStorageImpl::ClearPublicCertificates(
return;
}
NL_VLOG(1) << __func__ << ": Calling Destroy on public certificate database.";
VLOG(1) << __func__ << ": Calling Destroy on public certificate database.";
init_status_ = InitStatus::kUninitialized;
public_certificate_database_->Destroy(
[weak_this = weak_from_this(),
@@ -515,5 +532,4 @@ void NearbyShareCertificateStorageImpl::SavePublicCertificateExpirations() {
prefs::kNearbySharingPublicCertificateExpirationDictName, expirations);
}
} // namespace sharing
} // namespace nearby
} // namespace nearby::sharing
@@ -34,8 +34,7 @@
#include "sharing/internal/api/public_certificate_database.h"
#include "sharing/proto/rpc_resources.pb.h"
namespace nearby {
namespace sharing {
namespace nearby::sharing {
// Implements NearbyShareCertificateStorage using Prefs to store private
// certificates and LevelDB Proto to store public certificates. Must be
@@ -74,6 +73,11 @@ class NearbyShareCertificateStorageImpl : public NearbyShareCertificateStorage,
// NearbyShareCertificateStorage
std::vector<std::string> GetPublicCertificateIds() const override;
void GetPublicCertificates(PublicCertificateCallback callback) override;
void GetPublicCertificate(
absl::string_view id,
std::function<void(
bool, std::unique_ptr<nearby::sharing::proto::PublicCertificate>)>
callback) override;
std::optional<std::vector<NearbySharePrivateCertificate>>
GetPrivateCertificates() const override;
std::optional<absl::Time> NextPublicCertificateExpirationTime()
@@ -128,7 +132,6 @@ class NearbyShareCertificateStorageImpl : public NearbyShareCertificateStorage,
std::queue<std::function<void()>> deferred_callbacks_;
};
} // namespace sharing
} // namespace nearby
} // namespace nearby::sharing
#endif // THIRD_PARTY_NEARBY_SHARING_CERTIFICATES_NEARBY_SHARE_CERTIFICATE_STORAGE_IMPL_H_
@@ -46,8 +46,7 @@
#include "sharing/proto/rpc_resources.pb.h"
#include "sharing/proto/timestamp.pb.h"
namespace nearby {
namespace sharing {
namespace nearby::sharing {
namespace {
using ::nearby::sharing::api::MockPublicCertificateDb;
using ::nearby::sharing::proto::DeviceVisibility;
@@ -428,6 +427,32 @@ TEST_F(NearbyShareCertificateStorageImplTest, GetPublicCertificates) {
EXPECT_THAT(cert_store.use_count(), Eq(1));
}
TEST_F(NearbyShareCertificateStorageImplTest, GetPublicCertificate) {
auto db = std::make_unique<nearby::FakePublicCertificateDb>(
PrepopulatePublicCertificates());
nearby::FakePublicCertificateDb* fake_db = db.get();
auto cert_store = NearbyShareCertificateStorageImpl::Factory::Create(
preference_manager_, std::move(db));
fake_db->InvokeInitStatusCallback(FakePublicCertificateDb::InitStatus::kOk);
std::unique_ptr<PublicCertificate> public_certificate;
cert_store->GetPublicCertificate(
kSecretId3, [&public_certificate](
bool success, std::unique_ptr<PublicCertificate> result) {
public_certificate = std::move(result);
});
fake_db->InvokeLoadCertificateCallback(true);
std::string expected_serialized, actual_serialized;
ASSERT_TRUE(public_certificate->SerializeToString(&actual_serialized));
ASSERT_TRUE(fake_db->GetCertificatesMap()
.find(kSecretId3)
->second.SerializeToString(&expected_serialized));
ASSERT_EQ(expected_serialized, actual_serialized);
EXPECT_THAT(cert_store.use_count(), Eq(1));
}
TEST_F(NearbyShareCertificateStorageImplTest, AddPublicCertificates) {
auto db = std::make_unique<nearby::FakePublicCertificateDb>(
PrepopulatePublicCertificates());
@@ -784,5 +809,4 @@ TEST_F(NearbyShareCertificateStorageImplTest,
EXPECT_THAT(cert_store.use_count(), Eq(1));
}
} // namespace sharing
} // namespace nearby
} // namespace nearby::sharing
@@ -21,6 +21,7 @@
#include "gmock/gmock.h"
#include "absl/functional/any_invocable.h"
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "sharing/internal/api/public_certificate_database.h"
@@ -41,6 +42,13 @@ class MockPublicCertificateDb : public PublicCertificateDatabase {
nearby::sharing::proto::PublicCertificate>>) &&>
callback),
(override));
MOCK_METHOD(
void, LoadCertificate,
(absl::string_view id,
absl::AnyInvocable<void(
bool, std::unique_ptr<nearby::sharing::proto::PublicCertificate>) &&>
callback),
(override));
MOCK_METHOD(
void, AddCertificates,
(absl::Span<const nearby::sharing::proto::PublicCertificate> certificates,
@@ -20,6 +20,7 @@
#include <vector>
#include "absl/functional/any_invocable.h"
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "sharing/proto/rpc_resources.pb.h"
@@ -53,6 +54,13 @@ class PublicCertificateDatabase {
nearby::sharing::proto::PublicCertificate>>) &&>
callback) = 0;
virtual void LoadCertificate(
absl::string_view id,
absl::AnyInvocable<
void(bool,
std::unique_ptr<nearby::sharing::proto::PublicCertificate>) &&>
callback) = 0;
// Asynchronously saves |certificates| to the database.
// |callback| can be invoked on an executor thread when complete.
virtual void AddCertificates(
@@ -21,6 +21,7 @@
#include <vector>
#include "absl/functional/any_invocable.h"
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "sharing/internal/api/public_certificate_database.h"
#include "sharing/proto/rpc_resources.pb.h"
@@ -46,6 +47,14 @@ void FakePublicCertificateDb::LoadEntries(
load_callback_ = std::move(callback);
}
void FakePublicCertificateDb::LoadCertificate(
absl::string_view id,
absl::AnyInvocable<void(bool, std::unique_ptr<PublicCertificate>) &&>
callback) {
load_certificate_id_ = id;
load_certificate_callback_ = std::move(callback);
}
void FakePublicCertificateDb::AddCertificates(
absl::Span<const PublicCertificate> certificates,
absl::AnyInvocable<void(bool) &&> callback) {
@@ -90,6 +99,16 @@ void FakePublicCertificateDb::InvokeLoadCallback(bool success) {
std::move(load_callback_)(success, std::move(result));
}
void FakePublicCertificateDb::InvokeLoadCertificateCallback(bool success) {
const auto& it = entries_.find(load_certificate_id_);
if (it == entries_.end()) {
std::move(load_certificate_callback_)(success, nullptr);
return;
}
std::move(load_certificate_callback_)(
success, std::make_unique<PublicCertificate>(it->second));
}
void FakePublicCertificateDb::InvokeAddCallback(bool success) {
std::move(add_callback_)(success);
}
@@ -21,6 +21,7 @@
#include <vector>
#include "absl/functional/any_invocable.h"
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "sharing/internal/api/public_certificate_database.h"
@@ -42,6 +43,12 @@ class FakePublicCertificateDb
void(bool, std::unique_ptr<std::vector<
nearby::sharing::proto::PublicCertificate>>) &&>
callback) override;
void LoadCertificate(
absl::string_view id,
absl::AnyInvocable<
void(bool, std::unique_ptr<nearby::sharing::proto::PublicCertificate>)
&&>
callback) override;
void AddCertificates(
absl::Span<const nearby::sharing::proto::PublicCertificate> certificates,
absl::AnyInvocable<void(bool) &&> callback) override;
@@ -59,6 +66,7 @@ class FakePublicCertificateDb
void InvokeInitStatusCallback(
nearby::sharing::api::PublicCertificateDatabase::InitStatus init_status);
void InvokeLoadCallback(bool success);
void InvokeLoadCertificateCallback(bool success);
void InvokeAddCallback(bool success);
void InvokeRemoveCallback(bool success);
void InvokeDestroyCallback(bool success);
@@ -73,6 +81,10 @@ class FakePublicCertificateDb
void(bool, std::unique_ptr<std::vector<
nearby::sharing::proto::PublicCertificate>>) &&>
load_callback_;
std::string load_certificate_id_;
absl::AnyInvocable<
void(bool, std::unique_ptr<nearby::sharing::proto::PublicCertificate>) &&>
load_certificate_callback_;
absl::AnyInvocable<void(bool) &&> add_callback_;
absl::AnyInvocable<void(bool) &&> remove_callback_;
absl::AnyInvocable<void(bool) &&> destroy_callback_;