Commit ecbdf319 authored by Josh Nohle's avatar Josh Nohle Committed by Commit Bot

[Nearby] Use fake cert manager in service tests

Bug: 1113850
Change-Id: I9d82f7a8074b0a0d2fe918bff8125f046e120ad8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2353876
Commit-Queue: Josh Nohle <nohle@chromium.org>
Reviewed-by: default avatarHimanshu Jaju <himanshujaju@chromium.org>
Cr-Commit-Position: refs/heads/master@{#798066}
parent 6e7df486
...@@ -21,7 +21,8 @@ class FakeNearbyShareCertificateManager : public NearbyShareCertificateManager { ...@@ -21,7 +21,8 @@ class FakeNearbyShareCertificateManager : public NearbyShareCertificateManager {
// Factory that creates FakeNearbyShareCertificateManager instances. Use in // Factory that creates FakeNearbyShareCertificateManager instances. Use in
// NearbyShareCertificateManagerImpl::Factor::SetFactoryForTesting() in unit // NearbyShareCertificateManagerImpl::Factor::SetFactoryForTesting() in unit
// tests. // tests.
class Factory : NearbyShareCertificateManagerImpl::Factory { class Factory : public NearbyShareCertificateManagerImpl::Factory {
public:
Factory(); Factory();
~Factory() override; ~Factory() override;
...@@ -75,6 +76,14 @@ class FakeNearbyShareCertificateManager : public NearbyShareCertificateManager { ...@@ -75,6 +76,14 @@ class FakeNearbyShareCertificateManager : public NearbyShareCertificateManager {
using NearbyShareCertificateManager::NotifyPrivateCertificatesChanged; using NearbyShareCertificateManager::NotifyPrivateCertificatesChanged;
using NearbyShareCertificateManager::NotifyPublicCertificatesDownloaded; using NearbyShareCertificateManager::NotifyPublicCertificatesDownloaded;
size_t num_get_valid_private_certificate_calls() {
return num_get_valid_private_certificate_calls_;
}
size_t num_download_public_certificates_calls() {
return num_download_public_certificates_calls_;
}
std::vector<GetDecryptedPublicCertificateCall>& std::vector<GetDecryptedPublicCertificateCall>&
get_decrypted_public_certificate_calls() { get_decrypted_public_certificate_calls() {
return get_decrypted_public_certificate_calls_; return get_decrypted_public_certificate_calls_;
......
...@@ -278,3 +278,12 @@ GetNearbyShareTestPublicCertificate() { ...@@ -278,3 +278,12 @@ GetNearbyShareTestPublicCertificate() {
}()); }());
return *cert; return *cert;
} }
const NearbyShareDecryptedPublicCertificate&
GetNearbyShareTestDecryptedPublicCertificate() {
static const base::NoDestructor<NearbyShareDecryptedPublicCertificate> cert(
*NearbyShareDecryptedPublicCertificate::DecryptPublicCertificate(
GetNearbyShareTestPublicCertificate(),
GetNearbyShareTestEncryptedMetadataKey()));
return *cert;
}
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include <vector> #include <vector>
#include "base/time/time.h" #include "base/time/time.h"
#include "chrome/browser/nearby_sharing/certificates/nearby_share_decrypted_public_certificate.h"
#include "chrome/browser/nearby_sharing/certificates/nearby_share_encrypted_metadata_key.h" #include "chrome/browser/nearby_sharing/certificates/nearby_share_encrypted_metadata_key.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/proto/encrypted_metadata.pb.h" #include "chrome/browser/nearby_sharing/proto/encrypted_metadata.pb.h"
...@@ -46,4 +47,7 @@ NearbySharePrivateCertificate GetNearbyShareTestPrivateCertificate( ...@@ -46,4 +47,7 @@ NearbySharePrivateCertificate GetNearbyShareTestPrivateCertificate(
const nearbyshare::proto::PublicCertificate& const nearbyshare::proto::PublicCertificate&
GetNearbyShareTestPublicCertificate(); GetNearbyShareTestPublicCertificate();
const NearbyShareDecryptedPublicCertificate&
GetNearbyShareTestDecryptedPublicCertificate();
#endif // CHROME_BROWSER_NEARBY_SHARING_CERTIFICATES_TEST_UTIL_H_ #endif // CHROME_BROWSER_NEARBY_SHARING_CERTIFICATES_TEST_UTIL_H_
...@@ -24,7 +24,8 @@ class FakeNearbyShareContactManager : public NearbyShareContactManager { ...@@ -24,7 +24,8 @@ class FakeNearbyShareContactManager : public NearbyShareContactManager {
// Factory that creates FakeNearbyShareContactManager instances. Use in // Factory that creates FakeNearbyShareContactManager instances. Use in
// NearbyShareContactManagerImpl::Factor::SetFactoryForTesting() in unit // NearbyShareContactManagerImpl::Factor::SetFactoryForTesting() in unit
// tests. // tests.
class Factory : NearbyShareContactManagerImpl::Factory { class Factory : public NearbyShareContactManagerImpl::Factory {
public:
Factory(); Factory();
~Factory() override; ~Factory() override;
......
...@@ -10,7 +10,6 @@ ...@@ -10,7 +10,6 @@
#include "base/memory/singleton.h" #include "base/memory/singleton.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "chrome/browser/browser_features.h" #include "chrome/browser/browser_features.h"
#include "chrome/browser/nearby_sharing/certificates/nearby_share_certificate_manager_impl.h"
#include "chrome/browser/nearby_sharing/common/nearby_share_prefs.h" #include "chrome/browser/nearby_sharing/common/nearby_share_prefs.h"
#include "chrome/browser/nearby_sharing/logging/logging.h" #include "chrome/browser/nearby_sharing/logging/logging.h"
#include "chrome/browser/nearby_sharing/nearby_connections_manager.h" #include "chrome/browser/nearby_sharing/nearby_connections_manager.h"
...@@ -74,8 +73,7 @@ KeyedService* NearbySharingServiceFactory::BuildServiceInstanceFor( ...@@ -74,8 +73,7 @@ KeyedService* NearbySharingServiceFactory::BuildServiceInstanceFor(
NS_LOG(VERBOSE) << __func__ << ": creating NearbySharingService."; NS_LOG(VERBOSE) << __func__ << ": creating NearbySharingService.";
return new NearbySharingServiceImpl( return new NearbySharingServiceImpl(
pref_service, notification_display_service, profile, pref_service, notification_display_service, profile,
std::move(nearby_connections_manager), &process_manager, std::move(nearby_connections_manager), &process_manager);
NearbyShareCertificateManagerImpl::Factory::Create());
} }
content::BrowserContext* NearbySharingServiceFactory::GetBrowserContextToUse( content::BrowserContext* NearbySharingServiceFactory::GetBrowserContextToUse(
......
...@@ -161,8 +161,7 @@ NearbySharingServiceImpl::NearbySharingServiceImpl( ...@@ -161,8 +161,7 @@ NearbySharingServiceImpl::NearbySharingServiceImpl(
NotificationDisplayService* notification_display_service, NotificationDisplayService* notification_display_service,
Profile* profile, Profile* profile,
std::unique_ptr<NearbyConnectionsManager> nearby_connections_manager, std::unique_ptr<NearbyConnectionsManager> nearby_connections_manager,
NearbyProcessManager* process_manager, NearbyProcessManager* process_manager)
std::unique_ptr<NearbyShareCertificateManager> certificate_manager)
: profile_(profile), : profile_(profile),
settings_(prefs), settings_(prefs),
nearby_connections_manager_(std::move(nearby_connections_manager)), nearby_connections_manager_(std::move(nearby_connections_manager)),
...@@ -176,10 +175,10 @@ NearbySharingServiceImpl::NearbySharingServiceImpl( ...@@ -176,10 +175,10 @@ NearbySharingServiceImpl::NearbySharingServiceImpl(
prefs, prefs,
http_client_factory_.get())), http_client_factory_.get())),
contact_manager_(NearbyShareContactManagerImpl::Factory::Create()), contact_manager_(NearbyShareContactManagerImpl::Factory::Create()),
certificate_manager_(std::move(certificate_manager)) { certificate_manager_(
NearbyShareCertificateManagerImpl::Factory::Create()) {
DCHECK(profile_); DCHECK(profile_);
DCHECK(nearby_connections_manager_); DCHECK(nearby_connections_manager_);
DCHECK(certificate_manager_);
nearby_process_observer_.Add(process_manager_); nearby_process_observer_.Add(process_manager_);
......
...@@ -63,8 +63,7 @@ class NearbySharingServiceImpl ...@@ -63,8 +63,7 @@ class NearbySharingServiceImpl
NotificationDisplayService* notification_display_service, NotificationDisplayService* notification_display_service,
Profile* profile, Profile* profile,
std::unique_ptr<NearbyConnectionsManager> nearby_connections_manager, std::unique_ptr<NearbyConnectionsManager> nearby_connections_manager,
NearbyProcessManager* process_manager, NearbyProcessManager* process_manager);
std::unique_ptr<NearbyShareCertificateManager> certificate_manager);
~NearbySharingServiceImpl() override; ~NearbySharingServiceImpl() override;
// NearbySharingService: // NearbySharingService:
...@@ -187,8 +186,7 @@ class NearbySharingServiceImpl ...@@ -187,8 +186,7 @@ class NearbySharingServiceImpl
base::Optional<NearbyShareDecryptedPublicCertificate> certificate); base::Optional<NearbyShareDecryptedPublicCertificate> certificate);
void ReceiveIntroduction(ShareTarget share_target, void ReceiveIntroduction(ShareTarget share_target,
base::Optional<std::string> token); base::Optional<std::string> token);
void OnReceivedIntroduction( void OnReceivedIntroduction(ShareTarget share_target,
ShareTarget share_target,
base::Optional<std::string> token, base::Optional<std::string> token,
base::Optional<sharing::mojom::V1FramePtr> frame); base::Optional<sharing::mojom::V1FramePtr> frame);
void OnFrameRead(ShareTarget share_target, void OnFrameRead(ShareTarget share_target,
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
#include "base/run_loop.h" #include "base/run_loop.h"
#include "base/test/bind_test_util.h" #include "base/test/bind_test_util.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "chrome/browser/nearby_sharing/certificates/nearby_share_certificate_manager.h" #include "chrome/browser/nearby_sharing/certificates/fake_nearby_share_certificate_manager.h"
#include "chrome/browser/nearby_sharing/certificates/nearby_share_decrypted_public_certificate.h" #include "chrome/browser/nearby_sharing/certificates/nearby_share_decrypted_public_certificate.h"
#include "chrome/browser/nearby_sharing/certificates/test_util.h" #include "chrome/browser/nearby_sharing/certificates/test_util.h"
#include "chrome/browser/nearby_sharing/fake_nearby_connection.h" #include "chrome/browser/nearby_sharing/fake_nearby_connection.h"
...@@ -66,25 +66,6 @@ class MockIncomingFramesReader : public IncomingFramesReader { ...@@ -66,25 +66,6 @@ class MockIncomingFramesReader : public IncomingFramesReader {
(override)); (override));
}; };
class MockNearbyShareCertificateManager : public NearbyShareCertificateManager {
public:
MOCK_METHOD(NearbySharePrivateCertificate,
GetValidPrivateCertificate,
(NearbyShareVisibility visibility),
(override));
MOCK_METHOD(void,
GetDecryptedPublicCertificate,
(base::span<const uint8_t> encrypted_metadata_key,
base::span<const uint8_t> salt,
CertDecryptedCallback callback),
(override));
MOCK_METHOD(void, DownloadPublicCertificates, (), (override));
protected:
MOCK_METHOD(void, OnStart, (), (override));
MOCK_METHOD(void, OnStop, (), (override));
};
PairedKeyVerificationRunner::PairedKeyVerificationResult Merge( PairedKeyVerificationRunner::PairedKeyVerificationResult Merge(
PairedKeyVerificationRunner::PairedKeyVerificationResult local_result, PairedKeyVerificationRunner::PairedKeyVerificationResult local_result,
sharing::mojom::PairedKeyResultFrame::Status remote_result) { sharing::mojom::PairedKeyResultFrame::Status remote_result) {
...@@ -120,12 +101,36 @@ class PairedKeyVerificationRunnerTest : public testing::Test { ...@@ -120,12 +101,36 @@ class PairedKeyVerificationRunnerTest : public testing::Test {
: frames_reader_(&mock_nearby_process_manager_, &profile_, &connection_) { : frames_reader_(&mock_nearby_process_manager_, &profile_, &connection_) {
} }
void SetUp() override { void SetUp() override { share_target_.is_incoming = true; }
share_target.is_incoming = true;
void RunVerification(bool use_valid_public_certificate,
bool restricted_to_contacts,
PairedKeyVerificationRunner::PairedKeyVerificationResult
expected_result) {
base::Optional<NearbyShareDecryptedPublicCertificate> public_certificate =
use_valid_public_certificate
? base::make_optional<NearbyShareDecryptedPublicCertificate>(
GetNearbyShareTestDecryptedPublicCertificate())
: base::nullopt;
EXPECT_CALL(certificate_manager_, GetValidPrivateCertificate(testing::_)) PairedKeyVerificationRunner runner(
.WillRepeatedly(testing::Return(GetNearbyShareTestPrivateCertificate( share_target_, kEndpointId, kAuthToken, &connection_,
NearbyShareVisibility::kAllContacts))); std::move(public_certificate), &certificate_manager_,
nearby_share::mojom::Visibility::kAllContacts, restricted_to_contacts,
&frames_reader_, kTimeout);
base::RunLoop run_loop;
runner.Run(base::BindLambdaForTesting(
[&](PairedKeyVerificationRunner::PairedKeyVerificationResult result) {
EXPECT_EQ(expected_result, result);
run_loop.Quit();
}));
run_loop.Run();
// The private certificate is at least always immediately retrieved in order
// to create the signature for the sent PairedKeyEncryptionFrame.
EXPECT_GE(certificate_manager_.num_get_valid_private_certificate_calls(),
1u);
} }
void SetUpPairedKeyEncryptionFrame(ReturnFrameType frame_type) { void SetUpPairedKeyEncryptionFrame(ReturnFrameType frame_type) {
...@@ -135,11 +140,20 @@ class PairedKeyVerificationRunnerTest : public testing::Test { ...@@ -135,11 +140,20 @@ class PairedKeyVerificationRunnerTest : public testing::Test {
testing::Eq(sharing::mojom::V1Frame::Tag::PAIRED_KEY_ENCRYPTION), testing::Eq(sharing::mojom::V1Frame::Tag::PAIRED_KEY_ENCRYPTION),
testing::_, testing::Eq(kTimeout))) testing::_, testing::Eq(kTimeout)))
.WillOnce(testing::WithArg<1>(testing::Invoke( .WillOnce(testing::WithArg<1>(testing::Invoke(
[frame_type]( [frame_type,
base::OnceCallback<void( this](base::OnceCallback<void(
base::Optional<sharing::mojom::V1FramePtr>)> callback) { base::Optional<sharing::mojom::V1FramePtr>)> callback) {
// A private certificate retrieval will only be necessary if we
// receive a frame that needs verification.
size_t initial_num_private_cert_gets =
certificate_manager_
.num_get_valid_private_certificate_calls();
if (frame_type == ReturnFrameType::kNull) { if (frame_type == ReturnFrameType::kNull) {
std::move(callback).Run(base::nullopt); std::move(callback).Run(base::nullopt);
EXPECT_EQ(initial_num_private_cert_gets,
certificate_manager_
.num_get_valid_private_certificate_calls());
return; return;
} }
...@@ -157,6 +171,9 @@ class PairedKeyVerificationRunnerTest : public testing::Test { ...@@ -157,6 +171,9 @@ class PairedKeyVerificationRunnerTest : public testing::Test {
} }
std::move(callback).Run(std::move(mojo_v1frame)); std::move(callback).Run(std::move(mojo_v1frame));
EXPECT_EQ(initial_num_private_cert_gets + 1,
certificate_manager_
.num_get_valid_private_certificate_calls());
}))); })));
} }
...@@ -218,59 +235,37 @@ class PairedKeyVerificationRunnerTest : public testing::Test { ...@@ -218,59 +235,37 @@ class PairedKeyVerificationRunnerTest : public testing::Test {
testing::NiceMock<MockNearbyProcessManager> mock_nearby_process_manager_; testing::NiceMock<MockNearbyProcessManager> mock_nearby_process_manager_;
TestingProfile profile_; TestingProfile profile_;
FakeNearbyConnection connection_; FakeNearbyConnection connection_;
testing::NiceMock<MockNearbyShareCertificateManager> certificate_manager_; FakeNearbyShareCertificateManager certificate_manager_;
testing::NiceMock<MockIncomingFramesReader> frames_reader_; testing::NiceMock<MockIncomingFramesReader> frames_reader_;
ShareTarget share_target; ShareTarget share_target_;
}; };
TEST_F(PairedKeyVerificationRunnerTest, TEST_F(PairedKeyVerificationRunnerTest,
NullCertificate_InvalidPairedKeyEncryptionFrame_RestrictToContacts) { NullCertificate_InvalidPairedKeyEncryptionFrame_RestrictToContacts) {
PairedKeyVerificationRunner runner(
share_target, kEndpointId, kAuthToken, &connection_,
/*certificate=*/base::nullopt, &certificate_manager_,
nearby_share::mojom::Visibility::kAllContacts,
/*restrict_to_contacts=*/true, &frames_reader_, kTimeout);
// Empty key encryption frame fails the certificate verification. // Empty key encryption frame fails the certificate verification.
SetUpPairedKeyEncryptionFrame(ReturnFrameType::kEmpty); SetUpPairedKeyEncryptionFrame(ReturnFrameType::kEmpty);
base::RunLoop run_loop; RunVerification(
runner.Run(base::BindLambdaForTesting( /*use_valid_public_certificate=*/false,
[&](PairedKeyVerificationRunner::PairedKeyVerificationResult result) { /*restricted_to_contacts=*/true,
EXPECT_EQ( /*expected_result=*/
PairedKeyVerificationRunner::PairedKeyVerificationResult::kFail, PairedKeyVerificationRunner::PairedKeyVerificationResult::kFail);
result);
run_loop.Quit();
}));
run_loop.Run();
ExpectPairedKeyEncryptionFrameSent(); ExpectPairedKeyEncryptionFrameSent();
} }
TEST_F(PairedKeyVerificationRunnerTest, TEST_F(PairedKeyVerificationRunnerTest,
ValidPairedKeyEncryptionFrame_ResultFrameTimedOut) { ValidPairedKeyEncryptionFrame_ResultFrameTimedOut) {
PairedKeyVerificationRunner runner(
share_target, kEndpointId, kAuthToken, &connection_,
NearbyShareDecryptedPublicCertificate::DecryptPublicCertificate(
GetNearbyShareTestPublicCertificate(),
GetNearbyShareTestEncryptedMetadataKey()),
&certificate_manager_, nearby_share::mojom::Visibility::kAllContacts,
/*restrict_to_contacts=*/false, &frames_reader_, kTimeout);
SetUpPairedKeyEncryptionFrame(ReturnFrameType::kValid); SetUpPairedKeyEncryptionFrame(ReturnFrameType::kValid);
// Null result frame fails the certificate verification process. // Null result frame fails the certificate verification process.
SetUpPairedKeyResultFrame(ReturnFrameType::kNull); SetUpPairedKeyResultFrame(ReturnFrameType::kNull);
base::RunLoop run_loop; RunVerification(
runner.Run(base::BindLambdaForTesting( /*use_valid_public_certificate=*/true,
[&](PairedKeyVerificationRunner::PairedKeyVerificationResult result) { /*restricted_to_contacts=*/false,
EXPECT_EQ( /*expected_result=*/
PairedKeyVerificationRunner::PairedKeyVerificationResult::kFail, PairedKeyVerificationRunner::PairedKeyVerificationResult::kFail);
result);
run_loop.Quit();
}));
run_loop.Run();
ExpectPairedKeyEncryptionFrameSent(); ExpectPairedKeyEncryptionFrameSent();
ExpectPairedKeyResultFrameSent(sharing::nearby::PairedKeyResultFrame::UNABLE); ExpectPairedKeyResultFrameSent(sharing::nearby::PairedKeyResultFrame::UNABLE);
...@@ -308,32 +303,15 @@ TEST_P(ParameterisedPairedKeyVerificationRunnerTest, ...@@ -308,32 +303,15 @@ TEST_P(ParameterisedPairedKeyVerificationRunnerTest,
PairedKeyVerificationRunner::PairedKeyVerificationResult expected_result = PairedKeyVerificationRunner::PairedKeyVerificationResult expected_result =
Merge(params.result, status); Merge(params.result, status);
share_target.is_known = params.is_target_known; share_target_.is_known = params.is_target_known;
base::Optional<NearbyShareDecryptedPublicCertificate> certificate;
if (params.is_valid_certificate) {
certificate =
NearbyShareDecryptedPublicCertificate::DecryptPublicCertificate(
GetNearbyShareTestPublicCertificate(),
GetNearbyShareTestEncryptedMetadataKey());
}
PairedKeyVerificationRunner runner(
share_target, kEndpointId, kAuthToken, &connection_,
std::move(certificate), &certificate_manager_,
nearby_share::mojom::Visibility::kAllContacts,
/*restricted_to_contacts=*/false, &frames_reader_, kTimeout);
SetUpPairedKeyEncryptionFrame(params.encryption_frame_type); SetUpPairedKeyEncryptionFrame(params.encryption_frame_type);
SetUpPairedKeyResultFrame( SetUpPairedKeyResultFrame(
PairedKeyVerificationRunnerTest::ReturnFrameType::kValid, status); PairedKeyVerificationRunnerTest::ReturnFrameType::kValid, status);
base::RunLoop run_loop; RunVerification(
runner.Run(base::BindLambdaForTesting( /*use_valid_public_certificate=*/params.is_valid_certificate,
[&](PairedKeyVerificationRunner::PairedKeyVerificationResult result) { /*restricted_to_contacts=*/false, expected_result);
EXPECT_EQ(expected_result, result);
run_loop.Quit();
}));
run_loop.Run();
ExpectPairedKeyEncryptionFrameSent(); ExpectPairedKeyEncryptionFrameSent();
if (params.encryption_frame_type == if (params.encryption_frame_type ==
......
...@@ -4423,8 +4423,11 @@ test("unit_tests") { ...@@ -4423,8 +4423,11 @@ test("unit_tests") {
} }
deps += [ deps += [
"//chrome/browser/nearby_sharing/certificates:test_support",
"//chrome/browser/nearby_sharing/certificates:unit_tests", "//chrome/browser/nearby_sharing/certificates:unit_tests",
"//chrome/browser/nearby_sharing/client:unit_tests", "//chrome/browser/nearby_sharing/client:unit_tests",
"//chrome/browser/nearby_sharing/contacts:test_support",
"//chrome/browser/nearby_sharing/local_device_data:test_support",
"//chrome/browser/nearby_sharing/local_device_data:unit_tests", "//chrome/browser/nearby_sharing/local_device_data:unit_tests",
"//chrome/browser/nearby_sharing/logging:unit_tests", "//chrome/browser/nearby_sharing/logging:unit_tests",
"//chrome/browser/nearby_sharing/proto", "//chrome/browser/nearby_sharing/proto",
......
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