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") {
deps = [
"//base",
"//base/util/values:values_util",
"//chrome/browser/nearby_sharing/common",
"//chrome/browser/nearby_sharing/logging",
"//chrome/browser/nearby_sharing/proto",
"//components/leveldb_proto",
......@@ -73,6 +74,7 @@ source_set("unit_tests") {
"//base",
"//base/test:test_support",
"//base/util/values:values_util",
"//chrome/browser/nearby_sharing/common",
"//chrome/browser/nearby_sharing/proto",
"//components/leveldb_proto:test_support",
"//components/prefs:test_support",
......
......@@ -25,3 +25,4 @@ const size_t kNearbyShareMaxNumMetadataEncryptionKeySalts = 32768;
const size_t kNearbyShareMaxNumMetadataEncryptionKeySaltGenerationRetries = 128;
const char kNearbyShareSenderVerificationPrefix = 0x01;
const char kNearbyShareReceiverVerificationPrefix = 0x02;
const size_t kNearbyShareCertificateStorageMaxNumInitializeAttempts = 3;
......@@ -88,4 +88,7 @@ extern const char kNearbyShareSenderVerificationPrefix;
// signing.
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_
......@@ -17,19 +17,14 @@
#include "base/threading/sequenced_task_runner_handle.h"
#include "base/util/values/values_util.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/common/nearby_share_prefs.h"
#include "chrome/browser/nearby_sharing/proto/rpc_resources.pb.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
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 encoded_string;
base::Base64UrlEncode(unencoded_string,
......@@ -114,20 +109,13 @@ NearbyShareCertificateStorageImpl::NearbyShareCertificateStorageImpl(
NearbyShareCertificateStorageImpl::~NearbyShareCertificateStorageImpl() =
default;
// static
void NearbyShareCertificateStorageImpl::RegisterPrefs(
PrefRegistrySimple* registry) {
registry->RegisterDictionaryPref(
kNearbySharePublicCertificateExpirationDictPref);
registry->RegisterListPref(kNearbySharePrivateCertificateListPref);
}
void NearbyShareCertificateStorageImpl::Initialize() {
switch (init_status_) {
case InitStatus::kUninitialized:
case InitStatus::kFailed:
num_initialize_attempts_++;
if (num_initialize_attempts_ > kMaxNumInitializeAttempts) {
if (num_initialize_attempts_ >
kNearbyShareCertificateStorageMaxNumInitializeAttempts) {
FinishInitialization(false);
break;
}
......@@ -294,8 +282,7 @@ NearbyShareCertificateStorageImpl::GetPublicCertificateIds() const {
void NearbyShareCertificateStorageImpl::GetPublicCertificates(
PublicCertificateCallback callback) {
if (init_status_ == InitStatus::kFailed) {
base::SequencedTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), false, nullptr));
std::move(callback).Run(false, nullptr);
return;
}
......@@ -312,7 +299,7 @@ void NearbyShareCertificateStorageImpl::GetPublicCertificates(
base::Optional<std::vector<NearbySharePrivateCertificate>>
NearbyShareCertificateStorageImpl::GetPrivateCertificates() const {
const base::Value* list =
pref_service_->Get(kNearbySharePrivateCertificateListPref);
pref_service_->Get(prefs::kNearbySharingPrivateCertificateListPrefName);
std::vector<NearbySharePrivateCertificate> certs;
for (const base::Value& cert_dict : list->GetList()) {
base::Optional<NearbySharePrivateCertificate> cert(
......@@ -329,7 +316,7 @@ base::Optional<base::Time>
NearbyShareCertificateStorageImpl::NextPrivateCertificateExpirationTime()
const {
const base::Value* list =
pref_service_->Get(kNearbySharePrivateCertificateListPref);
pref_service_->Get(prefs::kNearbySharingPrivateCertificateListPrefName);
if (!list || list->GetList().empty())
return base::nullopt;
......@@ -359,7 +346,7 @@ void NearbyShareCertificateStorageImpl::ReplacePrivateCertificates(
for (const NearbySharePrivateCertificate& cert : private_certificates) {
list.Append(cert.ToDictionary());
}
pref_service_->Set(kNearbySharePrivateCertificateListPref, list);
pref_service_->Set(prefs::kNearbySharingPrivateCertificateListPrefName, list);
}
void NearbyShareCertificateStorageImpl::ReplacePublicCertificates(
......@@ -367,8 +354,7 @@ void NearbyShareCertificateStorageImpl::ReplacePublicCertificates(
public_certificates,
ResultCallback callback) {
if (init_status_ == InitStatus::kFailed) {
base::SequencedTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), false));
std::move(callback).Run(false);
return;
}
......@@ -401,8 +387,7 @@ void NearbyShareCertificateStorageImpl::AddPublicCertificates(
public_certificates,
ResultCallback callback) {
if (init_status_ == InitStatus::kFailed) {
base::SequencedTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), false));
std::move(callback).Run(false);
return;
}
......@@ -436,8 +421,7 @@ void NearbyShareCertificateStorageImpl::RemoveExpiredPublicCertificates(
base::Time now,
ResultCallback callback) {
if (init_status_ == InitStatus::kFailed) {
base::SequencedTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), false));
std::move(callback).Run(false);
return;
}
......@@ -475,14 +459,13 @@ void NearbyShareCertificateStorageImpl::RemoveExpiredPublicCertificates(
}
void NearbyShareCertificateStorageImpl::ClearPrivateCertificates() {
pref_service_->ClearPref(kNearbySharePrivateCertificateListPref);
pref_service_->ClearPref(prefs::kNearbySharingPrivateCertificateListPrefName);
}
void NearbyShareCertificateStorageImpl::ClearPublicCertificates(
ResultCallback callback) {
if (init_status_ == InitStatus::kFailed) {
base::SequencedTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), false));
std::move(callback).Run(false);
return;
}
......@@ -499,8 +482,8 @@ void NearbyShareCertificateStorageImpl::ClearPublicCertificates(
}
bool NearbyShareCertificateStorageImpl::FetchPublicCertificateExpirations() {
const base::Value* dict =
pref_service_->Get(kNearbySharePublicCertificateExpirationDictPref);
const base::Value* dict = pref_service_->Get(
prefs::kNearbySharingPublicCertificateExpirationDictPrefName);
public_certificate_expirations_.clear();
if (!dict) {
return false;
......@@ -526,5 +509,6 @@ void NearbyShareCertificateStorageImpl::SavePublicCertificateExpirations() {
dict.SetKey(EncodeString(pair.first), util::TimeToValue(pair.second));
}
pref_service_->Set(kNearbySharePublicCertificateExpirationDictPref, dict);
pref_service_->Set(
prefs::kNearbySharingPublicCertificateExpirationDictPrefName, dict);
}
......@@ -11,7 +11,6 @@
#include "components/leveldb_proto/public/proto_database.h"
class NearbySharePrivateCertificate;
class PrefRegistrySimple;
class PrefService;
namespace nearbyshare {
......@@ -49,9 +48,6 @@ class NearbyShareCertificateStorageImpl : public NearbyShareCertificateStorage {
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(NearbyShareCertificateStorageImpl&) =
delete;
......
......@@ -32,6 +32,10 @@ const char kNearbySharingSchedulerDownloadDeviceDataPrefName[] =
"nearby_sharing.scheduler.download_device_data";
const char kNearbySharingSchedulerUploadDeviceNamePrefName[] =
"nearby_sharing.scheduler.upload_device_name";
const char kNearbySharingPublicCertificateExpirationDictPrefName[] =
"nearbyshare.public_certificate_expiration_dict";
const char kNearbySharingPrivateCertificateListPrefName[] =
"nearbyshare.private_certificate_list";
} // namespace prefs
......@@ -64,6 +68,10 @@ void RegisterNearbySharingPrefs(PrefRegistrySimple* registry) {
registry->RegisterTimePref(
prefs::kNearbySharingOnboardingDismissedTimePrefName,
/*default_value=*/base::Time());
registry->RegisterDictionaryPref(
prefs::kNearbySharingPublicCertificateExpirationDictPrefName);
registry->RegisterListPref(
prefs::kNearbySharingPrivateCertificateListPrefName);
}
void RegisterNearbySharingLocalPrefs(PrefRegistrySimple* local_state) {
......
......@@ -21,6 +21,8 @@ extern const char kNearbySharingIconUrlPrefName[];
extern const char kNearbySharingOnboardingDismissedTimePrefName[];
extern const char kNearbySharingSchedulerDownloadDeviceDataPrefName[];
extern const char kNearbySharingSchedulerUploadDeviceNamePrefName[];
extern const char kNearbySharingPublicCertificateExpirationDictPrefName[];
extern const char kNearbySharingPrivateCertificateListPrefName[];
} // 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