diff --git a/sharing/certificates/nearby_share_certificate_storage_impl_test.cc b/sharing/certificates/nearby_share_certificate_storage_impl_test.cc index 3101f555..2d2f5665 100644 --- a/sharing/certificates/nearby_share_certificate_storage_impl_test.cc +++ b/sharing/certificates/nearby_share_certificate_storage_impl_test.cc @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -28,9 +29,7 @@ #include "gmock/gmock.h" #include "protobuf-matchers/protocol-buffer-matchers.h" #include "gtest/gtest.h" -#include "absl/container/flat_hash_map.h" #include "absl/functional/any_invocable.h" -#include "absl/meta/type_traits.h" #include "absl/strings/escaping.h" #include "absl/strings/string_view.h" #include "absl/time/clock.h" @@ -166,7 +165,7 @@ class NearbyShareCertificateStorageImplTest : public ::testing::Test { prefs::kNearbySharingPrivateCertificateListName); } - void PrepopulatePublicCertificates(nearby::FakePublicCertificateDb* db) { + std::map PrepopulatePublicCertificates() { std::vector pub_certs; pub_certs.emplace_back(CreatePublicCertificate( kSecretId1, kSecretKey1, kPublicKey1, kStartSeconds1, kStartNanos1, @@ -183,16 +182,18 @@ class NearbyShareCertificateStorageImplTest : public ::testing::Test { kEndSeconds3, kEndNanos3, kForSelectedContacts3, kMetadataEncryptionKey3, kEncryptedMetadataBytes3, kMetadataEncryptionKeyTag3)); - db->AddCertificates(pub_certs, [](bool) {}); + std::map entries; std::vector> expirations; for (const auto& cert : pub_certs) { expirations.emplace_back( EncodeString(cert.secret_id()), absl::ToUnixNanos(TimestampToTime(cert.end_time()))); + entries.emplace(cert.secret_id(), std::move(cert)); } preference_manager_.SetCertificateExpirationArray( prefs::kNearbySharingPublicCertificateExpirationDictName, expirations); + return entries; } void CaptureBoolCallback(bool* dest, bool src) { *dest = src; } @@ -383,12 +384,12 @@ TEST_F(NearbyShareCertificateStorageImplTest, DeferredCallbackQueue) { } TEST_F(NearbyShareCertificateStorageImplTest, GetPublicCertificateIds) { - auto db = std::make_unique(); + auto db = std::make_unique( + PrepopulatePublicCertificates()); nearby::FakePublicCertificateDb* fake_db = db.get(); - PrepopulatePublicCertificates(fake_db); - auto cert_store = NearbyShareCertificateStorageImpl::Factory::Create( preference_manager_, std::move(db)); + fake_db->InvokeInitStatusCallback(FakePublicCertificateDb::InitStatus::kOk); auto ids = cert_store->GetPublicCertificateIds(); ASSERT_EQ(ids.size(), 3u); @@ -399,12 +400,13 @@ TEST_F(NearbyShareCertificateStorageImplTest, GetPublicCertificateIds) { } TEST_F(NearbyShareCertificateStorageImplTest, GetPublicCertificates) { - auto db = std::make_unique(); + auto db = std::make_unique( + PrepopulatePublicCertificates()); nearby::FakePublicCertificateDb* fake_db = db.get(); - PrepopulatePublicCertificates(fake_db); auto cert_store = NearbyShareCertificateStorageImpl::Factory::Create( preference_manager_, std::move(db)); + fake_db->InvokeInitStatusCallback(FakePublicCertificateDb::InitStatus::kOk); std::vector public_certificates; cert_store->GetPublicCertificates([this, &public_certificates, complete = [] { @@ -412,6 +414,7 @@ TEST_F(NearbyShareCertificateStorageImplTest, GetPublicCertificates) { PublicCertificateCallback(&public_certificates, std::move(complete), success, std::move(result)); }); + fake_db->InvokeLoadCallback(true); ASSERT_EQ(3u, public_certificates.size()); for (const PublicCertificate& cert : public_certificates) { @@ -426,9 +429,9 @@ TEST_F(NearbyShareCertificateStorageImplTest, GetPublicCertificates) { } TEST_F(NearbyShareCertificateStorageImplTest, ReplacePublicCertificates) { - auto db = std::make_unique(); + auto db = std::make_unique( + PrepopulatePublicCertificates()); nearby::FakePublicCertificateDb* fake_db = db.get(); - PrepopulatePublicCertificates(fake_db); std::vector new_certs = { CreatePublicCertificate(kSecretId4, kSecretKey4, kPublicKey4, kStartSeconds4, kStartNanos4, kEndSeconds4, @@ -438,12 +441,15 @@ TEST_F(NearbyShareCertificateStorageImplTest, ReplacePublicCertificates) { }; auto cert_store = NearbyShareCertificateStorageImpl::Factory::Create( preference_manager_, std::move(db)); + fake_db->InvokeInitStatusCallback(FakePublicCertificateDb::InitStatus::kOk); bool succeeded = false; cert_store->ReplacePublicCertificates( new_certs, [this, &succeeded](bool success) { CaptureBoolCallback(&succeeded, success); }); + fake_db->InvokeDestroyCallback(true); + fake_db->InvokeAddCallback(true); ASSERT_TRUE(succeeded); auto cert_map = fake_db->GetCertificatesMap(); @@ -464,9 +470,9 @@ TEST_F(NearbyShareCertificateStorageImplTest, ReplacePublicCertificates) { } TEST_F(NearbyShareCertificateStorageImplTest, AddPublicCertificates) { - auto db = std::make_unique(); + auto db = std::make_unique( + PrepopulatePublicCertificates()); nearby::FakePublicCertificateDb* fake_db = db.get(); - PrepopulatePublicCertificates(fake_db); std::vector new_certs = { CreatePublicCertificate(kSecretId3, kSecretKey2, kPublicKey2, kStartSeconds2, kStartNanos2, kEndSeconds2, @@ -482,12 +488,14 @@ TEST_F(NearbyShareCertificateStorageImplTest, AddPublicCertificates) { auto cert_store = NearbyShareCertificateStorageImpl::Factory::Create( preference_manager_, std::move(db)); + fake_db->InvokeInitStatusCallback(FakePublicCertificateDb::InitStatus::kOk); bool succeeded = false; cert_store->AddPublicCertificates(new_certs, [this, &succeeded](bool success) { CaptureBoolCallback(&succeeded, success); }); + fake_db->InvokeAddCallback(true); ASSERT_TRUE(succeeded); auto cert_map = fake_db->GetCertificatesMap(); @@ -520,17 +528,19 @@ TEST_F(NearbyShareCertificateStorageImplTest, AddPublicCertificates) { } TEST_F(NearbyShareCertificateStorageImplTest, ClearPublicCertificates) { - auto db = std::make_unique(); + auto db = std::make_unique( + PrepopulatePublicCertificates()); nearby::FakePublicCertificateDb* fake_db = db.get(); - PrepopulatePublicCertificates(fake_db); auto cert_store = NearbyShareCertificateStorageImpl::Factory::Create( preference_manager_, std::move(db)); + fake_db->InvokeInitStatusCallback(FakePublicCertificateDb::InitStatus::kOk); bool succeeded = false; cert_store->ClearPublicCertificates([this, &succeeded](bool success) { CaptureBoolCallback(&succeeded, success); }); + fake_db->InvokeDestroyCallback(true); ASSERT_TRUE(succeeded); ASSERT_EQ(0u, fake_db->GetCertificatesMap().size()); @@ -539,12 +549,13 @@ TEST_F(NearbyShareCertificateStorageImplTest, ClearPublicCertificates) { TEST_F(NearbyShareCertificateStorageImplTest, RemoveExpiredPrivateCertificates) { - auto db = std::make_unique(); + auto db = std::make_unique( + PrepopulatePublicCertificates()); nearby::FakePublicCertificateDb* fake_db = db.get(); - PrepopulatePublicCertificates(fake_db); auto cert_store = NearbyShareCertificateStorageImpl::Factory::Create( preference_manager_, std::move(db)); + fake_db->InvokeInitStatusCallback(FakePublicCertificateDb::InitStatus::kOk); std::vector certs = CreatePrivateCertificates( 3, DeviceVisibility::DEVICE_VISIBILITY_ALL_CONTACTS); @@ -571,12 +582,13 @@ TEST_F(NearbyShareCertificateStorageImplTest, } TEST_F(NearbyShareCertificateStorageImplTest, RemoveExpiredPublicCertificates) { - auto db = std::make_unique(); + auto db = std::make_unique( + PrepopulatePublicCertificates()); nearby::FakePublicCertificateDb* fake_db = db.get(); - PrepopulatePublicCertificates(fake_db); auto cert_store = NearbyShareCertificateStorageImpl::Factory::Create( preference_manager_, std::move(db)); + fake_db->InvokeInitStatusCallback(FakePublicCertificateDb::InitStatus::kOk); std::vector expiration_times; for (const auto& pair : fake_db->GetCertificatesMap()) { @@ -595,6 +607,7 @@ TEST_F(NearbyShareCertificateStorageImplTest, RemoveExpiredPublicCertificates) { now, [this, &succeeded](bool success) { CaptureBoolCallback(&succeeded, success); }); + fake_db->InvokeRemoveCallback(true); ASSERT_TRUE(succeeded); auto cert_map = fake_db->GetCertificatesMap(); @@ -607,12 +620,13 @@ TEST_F(NearbyShareCertificateStorageImplTest, RemoveExpiredPublicCertificates) { } TEST_F(NearbyShareCertificateStorageImplTest, ReplaceGetPrivateCertificates) { - auto db = std::make_unique(); + auto db = std::make_unique( + PrepopulatePublicCertificates()); nearby::FakePublicCertificateDb* fake_db = db.get(); - PrepopulatePublicCertificates(fake_db); auto cert_store = NearbyShareCertificateStorageImpl::Factory::Create( preference_manager_, std::move(db)); + fake_db->InvokeInitStatusCallback(FakePublicCertificateDb::InitStatus::kOk); auto certs_before = CreatePrivateCertificates( 3, DeviceVisibility::DEVICE_VISIBILITY_ALL_CONTACTS); @@ -641,12 +655,13 @@ TEST_F(NearbyShareCertificateStorageImplTest, ReplaceGetPrivateCertificates) { } TEST_F(NearbyShareCertificateStorageImplTest, UpdatePrivateCertificates) { - auto db = std::make_unique(); + auto db = std::make_unique( + PrepopulatePublicCertificates()); nearby::FakePublicCertificateDb* fake_db = db.get(); - PrepopulatePublicCertificates(fake_db); auto cert_store = NearbyShareCertificateStorageImpl::Factory::Create( preference_manager_, std::move(db)); + fake_db->InvokeInitStatusCallback(FakePublicCertificateDb::InitStatus::kOk); std::vector initial_certs = CreatePrivateCertificates( @@ -676,12 +691,13 @@ TEST_F(NearbyShareCertificateStorageImplTest, UpdatePrivateCertificates) { TEST_F(NearbyShareCertificateStorageImplTest, NextPrivateCertificateExpirationTime) { - auto db = std::make_unique(); + auto db = std::make_unique( + PrepopulatePublicCertificates()); nearby::FakePublicCertificateDb* fake_db = db.get(); - PrepopulatePublicCertificates(fake_db); auto cert_store = NearbyShareCertificateStorageImpl::Factory::Create( preference_manager_, std::move(db)); + fake_db->InvokeInitStatusCallback(FakePublicCertificateDb::InitStatus::kOk); auto certs = CreatePrivateCertificates( 3, DeviceVisibility::DEVICE_VISIBILITY_ALL_CONTACTS); @@ -701,12 +717,13 @@ TEST_F(NearbyShareCertificateStorageImplTest, TEST_F(NearbyShareCertificateStorageImplTest, NextPublicCertificateExpirationTime) { - auto db = std::make_unique(); + auto db = std::make_unique( + PrepopulatePublicCertificates()); nearby::FakePublicCertificateDb* fake_db = db.get(); - PrepopulatePublicCertificates(fake_db); auto cert_store = NearbyShareCertificateStorageImpl::Factory::Create( preference_manager_, std::move(db)); + fake_db->InvokeInitStatusCallback(FakePublicCertificateDb::InitStatus::kOk); std::optional next_expiration = cert_store->NextPublicCertificateExpirationTime(); @@ -723,12 +740,13 @@ TEST_F(NearbyShareCertificateStorageImplTest, } TEST_F(NearbyShareCertificateStorageImplTest, ClearPrivateCertificates) { - auto db = std::make_unique(); + auto db = std::make_unique( + PrepopulatePublicCertificates()); nearby::FakePublicCertificateDb* fake_db = db.get(); - PrepopulatePublicCertificates(fake_db); auto cert_store = NearbyShareCertificateStorageImpl::Factory::Create( preference_manager_, std::move(db)); + fake_db->InvokeInitStatusCallback(FakePublicCertificateDb::InitStatus::kOk); std::vector certs_before = CreatePrivateCertificates( @@ -744,12 +762,13 @@ TEST_F(NearbyShareCertificateStorageImplTest, ClearPrivateCertificates) { TEST_F(NearbyShareCertificateStorageImplTest, ClearPrivateCertificatesOfVisibility) { - auto db = std::make_unique(); + auto db = std::make_unique( + PrepopulatePublicCertificates()); nearby::FakePublicCertificateDb* fake_db = db.get(); - PrepopulatePublicCertificates(fake_db); auto cert_store = NearbyShareCertificateStorageImpl::Factory::Create( preference_manager_, std::move(db)); + fake_db->InvokeInitStatusCallback(FakePublicCertificateDb::InitStatus::kOk); std::vector certs_all_contacts = CreatePrivateCertificates( diff --git a/sharing/internal/test/fake_public_certificate_db.cc b/sharing/internal/test/fake_public_certificate_db.cc index 21494484..1d19451e 100644 --- a/sharing/internal/test/fake_public_certificate_db.cc +++ b/sharing/internal/test/fake_public_certificate_db.cc @@ -14,12 +14,12 @@ #include "sharing/internal/test/fake_public_certificate_db.h" +#include #include #include #include #include -#include "absl/container/flat_hash_map.h" #include "absl/functional/any_invocable.h" #include "absl/types/span.h" #include "sharing/internal/api/public_certificate_database.h" @@ -29,36 +29,33 @@ namespace nearby { using ::nearby::sharing::proto::PublicCertificate; +FakePublicCertificateDb::FakePublicCertificateDb( + std::map entries) + : entries_(std::move(entries)) {} + void FakePublicCertificateDb::Initialize( absl::AnyInvocable callback) { - std::move(callback)(PublicCertificateDatabase::InitStatus::kOk); + init_status_callback_ = std::move(callback); } void FakePublicCertificateDb::LoadEntries( absl::AnyInvocable>) &&> callback) { - auto result = std::make_unique>(); - auto it = entries_.begin(); - while (it != entries_.end()) { - result->push_back(it->second); - ++it; - } - - std::move(callback)(true, std::move(result)); + load_callback_ = std::move(callback); } void FakePublicCertificateDb::AddCertificates( absl::Span certificates, absl::AnyInvocable callback) { for (const auto& cert : certificates) { - if (entries_.contains(cert.secret_id())) { + if (entries_.find(cert.secret_id()) != entries_.end()) { entries_.erase(cert.secret_id()); } entries_.emplace(cert.secret_id(), cert); } - std::move(callback)(true); + add_callback_ = std::move(callback); } void FakePublicCertificateDb::RemoveCertificatesById( @@ -69,13 +66,41 @@ void FakePublicCertificateDb::RemoveCertificatesById( entries_.erase(*it); ++it; } - std::move(callback)(true); + remove_callback_ = std::move(callback); } void FakePublicCertificateDb::Destroy( absl::AnyInvocable callback) { entries_.clear(); - std::move(callback)(true); + destroy_callback_ = std::move(callback); } +void FakePublicCertificateDb::InvokeInitStatusCallback( + PublicCertificateDatabase::InitStatus init_status) { + std::move(init_status_callback_)(init_status); +} + +void FakePublicCertificateDb::InvokeLoadCallback(bool success) { + auto result = std::make_unique>(); + auto it = entries_.begin(); + while (it != entries_.end()) { + result->push_back(it->second); + ++it; + } + std::move(load_callback_)(success, std::move(result)); +} + +void FakePublicCertificateDb::InvokeAddCallback(bool success) { + std::move(add_callback_)(success); +} + +void FakePublicCertificateDb::InvokeRemoveCallback(bool success) { + std::move(remove_callback_)(success); +} + +void FakePublicCertificateDb::InvokeDestroyCallback(bool success) { + std::move(destroy_callback_)(success); +} + + } // namespace nearby diff --git a/sharing/internal/test/fake_public_certificate_db.h b/sharing/internal/test/fake_public_certificate_db.h index e555d910..6cb417cd 100644 --- a/sharing/internal/test/fake_public_certificate_db.h +++ b/sharing/internal/test/fake_public_certificate_db.h @@ -15,11 +15,11 @@ #ifndef THIRD_PARTY_NEARBY_SHARING_INTERNAL_TEST_FAKE_PUBLIC_CERTIFICATE_DB_H_ #define THIRD_PARTY_NEARBY_SHARING_INTERNAL_TEST_FAKE_PUBLIC_CERTIFICATE_DB_H_ +#include #include #include #include -#include "absl/container/flat_hash_map.h" #include "absl/functional/any_invocable.h" #include "absl/types/span.h" #include "sharing/internal/api/public_certificate_database.h" @@ -29,7 +29,8 @@ namespace nearby { class FakePublicCertificateDb : public nearby::sharing::api::PublicCertificateDatabase { public: - FakePublicCertificateDb() = default; + explicit FakePublicCertificateDb( + std::map entries); ~FakePublicCertificateDb() override = default; void Initialize( @@ -49,14 +50,32 @@ class FakePublicCertificateDb absl::AnyInvocable callback) override; void Destroy(absl::AnyInvocable callback) override; - absl::flat_hash_map + std::map GetCertificatesMap() { return entries_; } + // Invoke callbacks + void InvokeInitStatusCallback( + nearby::sharing::api::PublicCertificateDatabase::InitStatus init_status); + void InvokeLoadCallback(bool success); + void InvokeAddCallback(bool success); + void InvokeRemoveCallback(bool success); + void InvokeDestroyCallback(bool success); + private: - absl::flat_hash_map + std::map entries_; + absl::AnyInvocable< + void(nearby::sharing::api::PublicCertificateDatabase::InitStatus) &&> + init_status_callback_; + absl::AnyInvocable< + void(bool, std::unique_ptr>) &&> + load_callback_; + absl::AnyInvocable add_callback_; + absl::AnyInvocable remove_callback_; + absl::AnyInvocable destroy_callback_; }; } // namespace nearby