Commit a4e61c1c authored by Curt Clemens's avatar Curt Clemens Committed by Commit Bot

[Nearby Share] Certificate Storage: Add unit tests for initialization

Add unit tests for a few scenarios around initializing LevelDB in
NearbyShareCertificateStorage, including the deferred callback queueing
mechanism and failure retry.

Change-Id: I005da00cd0ee85c969f10875bd77d3412ad855a5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2339824Reviewed-by: default avatarJosh Nohle <nohle@chromium.org>
Commit-Queue: Curt Clemens <cclem@google.com>
Cr-Commit-Position: refs/heads/master@{#795487}
parent 191a023a
...@@ -27,6 +27,7 @@ source_set("certificates") { ...@@ -27,6 +27,7 @@ source_set("certificates") {
deps = [ deps = [
"//base", "//base",
"//base/util/values:values_util", "//base/util/values:values_util",
"//chrome/browser/nearby_sharing/common",
"//chrome/browser/nearby_sharing/logging", "//chrome/browser/nearby_sharing/logging",
"//chrome/browser/nearby_sharing/proto", "//chrome/browser/nearby_sharing/proto",
"//components/leveldb_proto", "//components/leveldb_proto",
...@@ -73,6 +74,7 @@ source_set("unit_tests") { ...@@ -73,6 +74,7 @@ source_set("unit_tests") {
"//base", "//base",
"//base/test:test_support", "//base/test:test_support",
"//base/util/values:values_util", "//base/util/values:values_util",
"//chrome/browser/nearby_sharing/common",
"//chrome/browser/nearby_sharing/proto", "//chrome/browser/nearby_sharing/proto",
"//components/leveldb_proto:test_support", "//components/leveldb_proto:test_support",
"//components/prefs:test_support", "//components/prefs:test_support",
......
...@@ -25,3 +25,4 @@ const size_t kNearbyShareMaxNumMetadataEncryptionKeySalts = 32768; ...@@ -25,3 +25,4 @@ const size_t kNearbyShareMaxNumMetadataEncryptionKeySalts = 32768;
const size_t kNearbyShareMaxNumMetadataEncryptionKeySaltGenerationRetries = 128; const size_t kNearbyShareMaxNumMetadataEncryptionKeySaltGenerationRetries = 128;
const char kNearbyShareSenderVerificationPrefix = 0x01; const char kNearbyShareSenderVerificationPrefix = 0x01;
const char kNearbyShareReceiverVerificationPrefix = 0x02; const char kNearbyShareReceiverVerificationPrefix = 0x02;
const size_t kNearbyShareCertificateStorageMaxNumInitializeAttempts = 3;
...@@ -88,4 +88,7 @@ extern const char kNearbyShareSenderVerificationPrefix; ...@@ -88,4 +88,7 @@ extern const char kNearbyShareSenderVerificationPrefix;
// signing. // signing.
extern const char kNearbyShareReceiverVerificationPrefix; extern const char kNearbyShareReceiverVerificationPrefix;
// The maximum number of attempts to initialize LevelDB in Certificate Storage.
extern const size_t kNearbyShareCertificateStorageMaxNumInitializeAttempts;
#endif // CHROME_BROWSER_NEARBY_SHARING_CERTIFICATES_CONSTANTS_H_ #endif // CHROME_BROWSER_NEARBY_SHARING_CERTIFICATES_CONSTANTS_H_
...@@ -17,19 +17,14 @@ ...@@ -17,19 +17,14 @@
#include "base/threading/sequenced_task_runner_handle.h" #include "base/threading/sequenced_task_runner_handle.h"
#include "base/util/values/values_util.h" #include "base/util/values/values_util.h"
#include "base/values.h" #include "base/values.h"
#include "chrome/browser/nearby_sharing/certificates/constants.h"
#include "chrome/browser/nearby_sharing/certificates/nearby_share_private_certificate.h" #include "chrome/browser/nearby_sharing/certificates/nearby_share_private_certificate.h"
#include "chrome/browser/nearby_sharing/common/nearby_share_prefs.h"
#include "chrome/browser/nearby_sharing/proto/rpc_resources.pb.h" #include "chrome/browser/nearby_sharing/proto/rpc_resources.pb.h"
#include "components/prefs/pref_registry_simple.h" #include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h" #include "components/prefs/pref_service.h"
namespace { namespace {
const size_t kMaxNumInitializeAttempts = 3;
const char kNearbySharePublicCertificateExpirationDictPref[] =
"nearbyshare.public_certificate_expiration_dict";
const char kNearbySharePrivateCertificateListPref[] =
"nearbyshare.private_certificate_list";
std::string EncodeString(const std::string& unencoded_string) { std::string EncodeString(const std::string& unencoded_string) {
std::string encoded_string; std::string encoded_string;
base::Base64UrlEncode(unencoded_string, base::Base64UrlEncode(unencoded_string,
...@@ -114,20 +109,13 @@ NearbyShareCertificateStorageImpl::NearbyShareCertificateStorageImpl( ...@@ -114,20 +109,13 @@ NearbyShareCertificateStorageImpl::NearbyShareCertificateStorageImpl(
NearbyShareCertificateStorageImpl::~NearbyShareCertificateStorageImpl() = NearbyShareCertificateStorageImpl::~NearbyShareCertificateStorageImpl() =
default; default;
// static
void NearbyShareCertificateStorageImpl::RegisterPrefs(
PrefRegistrySimple* registry) {
registry->RegisterDictionaryPref(
kNearbySharePublicCertificateExpirationDictPref);
registry->RegisterListPref(kNearbySharePrivateCertificateListPref);
}
void NearbyShareCertificateStorageImpl::Initialize() { void NearbyShareCertificateStorageImpl::Initialize() {
switch (init_status_) { switch (init_status_) {
case InitStatus::kUninitialized: case InitStatus::kUninitialized:
case InitStatus::kFailed: case InitStatus::kFailed:
num_initialize_attempts_++; num_initialize_attempts_++;
if (num_initialize_attempts_ > kMaxNumInitializeAttempts) { if (num_initialize_attempts_ >
kNearbyShareCertificateStorageMaxNumInitializeAttempts) {
FinishInitialization(false); FinishInitialization(false);
break; break;
} }
...@@ -294,8 +282,7 @@ NearbyShareCertificateStorageImpl::GetPublicCertificateIds() const { ...@@ -294,8 +282,7 @@ NearbyShareCertificateStorageImpl::GetPublicCertificateIds() const {
void NearbyShareCertificateStorageImpl::GetPublicCertificates( void NearbyShareCertificateStorageImpl::GetPublicCertificates(
PublicCertificateCallback callback) { PublicCertificateCallback callback) {
if (init_status_ == InitStatus::kFailed) { if (init_status_ == InitStatus::kFailed) {
base::SequencedTaskRunnerHandle::Get()->PostTask( std::move(callback).Run(false, nullptr);
FROM_HERE, base::BindOnce(std::move(callback), false, nullptr));
return; return;
} }
...@@ -312,7 +299,7 @@ void NearbyShareCertificateStorageImpl::GetPublicCertificates( ...@@ -312,7 +299,7 @@ void NearbyShareCertificateStorageImpl::GetPublicCertificates(
base::Optional<std::vector<NearbySharePrivateCertificate>> base::Optional<std::vector<NearbySharePrivateCertificate>>
NearbyShareCertificateStorageImpl::GetPrivateCertificates() const { NearbyShareCertificateStorageImpl::GetPrivateCertificates() const {
const base::Value* list = const base::Value* list =
pref_service_->Get(kNearbySharePrivateCertificateListPref); pref_service_->Get(prefs::kNearbySharingPrivateCertificateListPrefName);
std::vector<NearbySharePrivateCertificate> certs; std::vector<NearbySharePrivateCertificate> certs;
for (const base::Value& cert_dict : list->GetList()) { for (const base::Value& cert_dict : list->GetList()) {
base::Optional<NearbySharePrivateCertificate> cert( base::Optional<NearbySharePrivateCertificate> cert(
...@@ -329,7 +316,7 @@ base::Optional<base::Time> ...@@ -329,7 +316,7 @@ base::Optional<base::Time>
NearbyShareCertificateStorageImpl::NextPrivateCertificateExpirationTime() NearbyShareCertificateStorageImpl::NextPrivateCertificateExpirationTime()
const { const {
const base::Value* list = const base::Value* list =
pref_service_->Get(kNearbySharePrivateCertificateListPref); pref_service_->Get(prefs::kNearbySharingPrivateCertificateListPrefName);
if (!list || list->GetList().empty()) if (!list || list->GetList().empty())
return base::nullopt; return base::nullopt;
...@@ -359,7 +346,7 @@ void NearbyShareCertificateStorageImpl::ReplacePrivateCertificates( ...@@ -359,7 +346,7 @@ void NearbyShareCertificateStorageImpl::ReplacePrivateCertificates(
for (const NearbySharePrivateCertificate& cert : private_certificates) { for (const NearbySharePrivateCertificate& cert : private_certificates) {
list.Append(cert.ToDictionary()); list.Append(cert.ToDictionary());
} }
pref_service_->Set(kNearbySharePrivateCertificateListPref, list); pref_service_->Set(prefs::kNearbySharingPrivateCertificateListPrefName, list);
} }
void NearbyShareCertificateStorageImpl::ReplacePublicCertificates( void NearbyShareCertificateStorageImpl::ReplacePublicCertificates(
...@@ -367,8 +354,7 @@ void NearbyShareCertificateStorageImpl::ReplacePublicCertificates( ...@@ -367,8 +354,7 @@ void NearbyShareCertificateStorageImpl::ReplacePublicCertificates(
public_certificates, public_certificates,
ResultCallback callback) { ResultCallback callback) {
if (init_status_ == InitStatus::kFailed) { if (init_status_ == InitStatus::kFailed) {
base::SequencedTaskRunnerHandle::Get()->PostTask( std::move(callback).Run(false);
FROM_HERE, base::BindOnce(std::move(callback), false));
return; return;
} }
...@@ -401,8 +387,7 @@ void NearbyShareCertificateStorageImpl::AddPublicCertificates( ...@@ -401,8 +387,7 @@ void NearbyShareCertificateStorageImpl::AddPublicCertificates(
public_certificates, public_certificates,
ResultCallback callback) { ResultCallback callback) {
if (init_status_ == InitStatus::kFailed) { if (init_status_ == InitStatus::kFailed) {
base::SequencedTaskRunnerHandle::Get()->PostTask( std::move(callback).Run(false);
FROM_HERE, base::BindOnce(std::move(callback), false));
return; return;
} }
...@@ -436,8 +421,7 @@ void NearbyShareCertificateStorageImpl::RemoveExpiredPublicCertificates( ...@@ -436,8 +421,7 @@ void NearbyShareCertificateStorageImpl::RemoveExpiredPublicCertificates(
base::Time now, base::Time now,
ResultCallback callback) { ResultCallback callback) {
if (init_status_ == InitStatus::kFailed) { if (init_status_ == InitStatus::kFailed) {
base::SequencedTaskRunnerHandle::Get()->PostTask( std::move(callback).Run(false);
FROM_HERE, base::BindOnce(std::move(callback), false));
return; return;
} }
...@@ -475,14 +459,13 @@ void NearbyShareCertificateStorageImpl::RemoveExpiredPublicCertificates( ...@@ -475,14 +459,13 @@ void NearbyShareCertificateStorageImpl::RemoveExpiredPublicCertificates(
} }
void NearbyShareCertificateStorageImpl::ClearPrivateCertificates() { void NearbyShareCertificateStorageImpl::ClearPrivateCertificates() {
pref_service_->ClearPref(kNearbySharePrivateCertificateListPref); pref_service_->ClearPref(prefs::kNearbySharingPrivateCertificateListPrefName);
} }
void NearbyShareCertificateStorageImpl::ClearPublicCertificates( void NearbyShareCertificateStorageImpl::ClearPublicCertificates(
ResultCallback callback) { ResultCallback callback) {
if (init_status_ == InitStatus::kFailed) { if (init_status_ == InitStatus::kFailed) {
base::SequencedTaskRunnerHandle::Get()->PostTask( std::move(callback).Run(false);
FROM_HERE, base::BindOnce(std::move(callback), false));
return; return;
} }
...@@ -499,8 +482,8 @@ void NearbyShareCertificateStorageImpl::ClearPublicCertificates( ...@@ -499,8 +482,8 @@ void NearbyShareCertificateStorageImpl::ClearPublicCertificates(
} }
bool NearbyShareCertificateStorageImpl::FetchPublicCertificateExpirations() { bool NearbyShareCertificateStorageImpl::FetchPublicCertificateExpirations() {
const base::Value* dict = const base::Value* dict = pref_service_->Get(
pref_service_->Get(kNearbySharePublicCertificateExpirationDictPref); prefs::kNearbySharingPublicCertificateExpirationDictPrefName);
public_certificate_expirations_.clear(); public_certificate_expirations_.clear();
if (!dict) { if (!dict) {
return false; return false;
...@@ -526,5 +509,6 @@ void NearbyShareCertificateStorageImpl::SavePublicCertificateExpirations() { ...@@ -526,5 +509,6 @@ void NearbyShareCertificateStorageImpl::SavePublicCertificateExpirations() {
dict.SetKey(EncodeString(pair.first), util::TimeToValue(pair.second)); dict.SetKey(EncodeString(pair.first), util::TimeToValue(pair.second));
} }
pref_service_->Set(kNearbySharePublicCertificateExpirationDictPref, dict); pref_service_->Set(
prefs::kNearbySharingPublicCertificateExpirationDictPrefName, dict);
} }
...@@ -11,7 +11,6 @@ ...@@ -11,7 +11,6 @@
#include "components/leveldb_proto/public/proto_database.h" #include "components/leveldb_proto/public/proto_database.h"
class NearbySharePrivateCertificate; class NearbySharePrivateCertificate;
class PrefRegistrySimple;
class PrefService; class PrefService;
namespace nearbyshare { namespace nearbyshare {
...@@ -49,9 +48,6 @@ class NearbyShareCertificateStorageImpl : public NearbyShareCertificateStorage { ...@@ -49,9 +48,6 @@ class NearbyShareCertificateStorageImpl : public NearbyShareCertificateStorage {
using ExpirationList = std::vector<std::pair<std::string, base::Time>>; using ExpirationList = std::vector<std::pair<std::string, base::Time>>;
// Registers the prefs used by this class to the given |registry|.
static void RegisterPrefs(PrefRegistrySimple* registry);
~NearbyShareCertificateStorageImpl() override; ~NearbyShareCertificateStorageImpl() override;
NearbyShareCertificateStorageImpl(NearbyShareCertificateStorageImpl&) = NearbyShareCertificateStorageImpl(NearbyShareCertificateStorageImpl&) =
delete; delete;
......
...@@ -32,6 +32,10 @@ const char kNearbySharingSchedulerDownloadDeviceDataPrefName[] = ...@@ -32,6 +32,10 @@ const char kNearbySharingSchedulerDownloadDeviceDataPrefName[] =
"nearby_sharing.scheduler.download_device_data"; "nearby_sharing.scheduler.download_device_data";
const char kNearbySharingSchedulerUploadDeviceNamePrefName[] = const char kNearbySharingSchedulerUploadDeviceNamePrefName[] =
"nearby_sharing.scheduler.upload_device_name"; "nearby_sharing.scheduler.upload_device_name";
const char kNearbySharingPublicCertificateExpirationDictPrefName[] =
"nearbyshare.public_certificate_expiration_dict";
const char kNearbySharingPrivateCertificateListPrefName[] =
"nearbyshare.private_certificate_list";
} // namespace prefs } // namespace prefs
...@@ -64,6 +68,10 @@ void RegisterNearbySharingPrefs(PrefRegistrySimple* registry) { ...@@ -64,6 +68,10 @@ void RegisterNearbySharingPrefs(PrefRegistrySimple* registry) {
registry->RegisterTimePref( registry->RegisterTimePref(
prefs::kNearbySharingOnboardingDismissedTimePrefName, prefs::kNearbySharingOnboardingDismissedTimePrefName,
/*default_value=*/base::Time()); /*default_value=*/base::Time());
registry->RegisterDictionaryPref(
prefs::kNearbySharingPublicCertificateExpirationDictPrefName);
registry->RegisterListPref(
prefs::kNearbySharingPrivateCertificateListPrefName);
} }
void RegisterNearbySharingLocalPrefs(PrefRegistrySimple* local_state) { void RegisterNearbySharingLocalPrefs(PrefRegistrySimple* local_state) {
......
...@@ -21,6 +21,8 @@ extern const char kNearbySharingIconUrlPrefName[]; ...@@ -21,6 +21,8 @@ extern const char kNearbySharingIconUrlPrefName[];
extern const char kNearbySharingOnboardingDismissedTimePrefName[]; extern const char kNearbySharingOnboardingDismissedTimePrefName[];
extern const char kNearbySharingSchedulerDownloadDeviceDataPrefName[]; extern const char kNearbySharingSchedulerDownloadDeviceDataPrefName[];
extern const char kNearbySharingSchedulerUploadDeviceNamePrefName[]; extern const char kNearbySharingSchedulerUploadDeviceNamePrefName[];
extern const char kNearbySharingPublicCertificateExpirationDictPrefName[];
extern const char kNearbySharingPrivateCertificateListPrefName[];
} // namespace prefs } // namespace prefs
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment