Commit 0086b1f3 authored by Himanshu Jaju's avatar Himanshu Jaju Committed by Commit Bot

Refactor NearbySharingServiceImplUnittest for IncomingConnections

- Extract common set up for incoming connections
- Extract common set up for accept / reject flows
- Add a TODO for fetching certificate for outgoing share target based on parent CL

Bug: 1085068
Change-Id: I3b3e322026c3cfe2fe86808f92bfd0b1d810f94b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2344604
Commit-Queue: Himanshu Jaju <himanshujaju@chromium.org>
Reviewed-by: default avatarRichard Knoll <knollr@chromium.org>
Cr-Commit-Position: refs/heads/master@{#796503}
parent 66aa2d4c
...@@ -568,9 +568,10 @@ void NearbySharingServiceImpl::OnOutgoingAdvertisementDecoded( ...@@ -568,9 +568,10 @@ void NearbySharingServiceImpl::OnOutgoingAdvertisementDecoded(
// Once get the advertisement, first thing to do is to decrypt its device name // Once get the advertisement, first thing to do is to decrypt its device name
// based on its visibility and create a ShareTarget to represent this remote // based on its visibility and create a ShareTarget to represent this remote
// device. // device.
base::Optional<ShareTarget> share_target = // TODO(nmusgrave): Use valid certificate from certificate manager.
CreateShareTarget(endpoint_id, std::move(advertisement), base::nullopt, base::Optional<ShareTarget> share_target = CreateShareTarget(
/*is_incoming=*/false); endpoint_id, std::move(advertisement), /*certificate=*/base::nullopt,
/*is_incoming=*/false);
if (!share_target) { if (!share_target) {
NS_LOG(VERBOSE) << __func__ NS_LOG(VERBOSE) << __func__
<< ": Failed to convert advertisement to share target from " << ": Failed to convert advertisement to share target from "
......
...@@ -185,8 +185,13 @@ const char kServiceId[] = "NearbySharing"; ...@@ -185,8 +185,13 @@ const char kServiceId[] = "NearbySharing";
const char kDeviceName[] = "test_device_name"; const char kDeviceName[] = "test_device_name";
const char kEndpointId[] = "test_endpoint_id"; const char kEndpointId[] = "test_endpoint_id";
const std::vector<uint8_t> kValidV1EndpointInfo = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 10, 100, 101, 118, 105, 99, 101, 78, 97, 109, 101};
sharing::mojom::FramePtr GetValidIntroductionFrame() { sharing::mojom::FramePtr GetValidIntroductionFrame() {
std::vector<sharing::mojom::TextMetadataPtr> mojo_text_metadatas; std::vector<sharing::mojom::TextMetadataPtr> mojo_text_metadatas;
// TODO(himanshujaju) - Parameterise number of text and file metadatas.
for (int i = 1; i <= 3; i++) { for (int i = 1; i <= 3; i++) {
mojo_text_metadatas.push_back(sharing::mojom::TextMetadata::New( mojo_text_metadatas.push_back(sharing::mojom::TextMetadata::New(
"title " + base::NumberToString(i), "title " + base::NumberToString(i),
...@@ -204,18 +209,14 @@ sharing::mojom::FramePtr GetValidIntroductionFrame() { ...@@ -204,18 +209,14 @@ sharing::mojom::FramePtr GetValidIntroductionFrame() {
return mojo_frame; return mojo_frame;
} }
// Length sharing::Advertisement::kSaltSize sharing::mojom::FramePtr GetEmptyIntroductionFrame() {
const uint8_t kSalt[] = {0x0c, 0x08}; sharing::mojom::V1FramePtr mojo_v1frame = sharing::mojom::V1Frame::New();
// Length sharing::Advertisement::kMetadataEncryptionKeyHashByteSize mojo_v1frame->set_introduction(sharing::mojom::IntroductionFrame::New());
const uint8_t kMetadataEncryptionKeyHashByte[] = {0x07, 0x01, 0x08, 0x08, 0x04,
0x09, 0x06, 0x00, 0x0b, 0x0f, sharing::mojom::FramePtr mojo_frame = sharing::mojom::Frame::New();
0x0c, 0x09, 0x03, 0x0d}; mojo_frame->set_v1(std::move(mojo_v1frame));
const int kEndpointInfoSize = return mojo_frame;
1 + sharing::Advertisement::kSaltSize + }
sharing::Advertisement::kMetadataEncryptionKeyHashByteSize;
const uint8_t kEndpointInfo[] = {0x00, 0x0c, 0x08, 0x07, 0x01, 0x08,
0x08, 0x04, 0x09, 0x06, 0x00, 0x0b,
0x0f, 0x0c, 0x09, 0x03, 0x0d};
class NearbySharingServiceImplTest : public testing::Test { class NearbySharingServiceImplTest : public testing::Test {
public: public:
...@@ -245,6 +246,10 @@ class NearbySharingServiceImplTest : public testing::Test { ...@@ -245,6 +246,10 @@ class NearbySharingServiceImplTest : public testing::Test {
service_ = CreateService("name"); service_ = CreateService("name");
SetFakeFastInitiationManagerFactory(/*should_succeed_on_start=*/true); SetFakeFastInitiationManagerFactory(/*should_succeed_on_start=*/true);
EXPECT_CALL(mock_nearby_process_manager(),
GetOrStartNearbySharingDecoder(testing::_))
.WillRepeatedly(testing::Return(&mock_decoder_));
} }
void TearDown() override { profile_manager_.DeleteAllTestingProfiles(); } void TearDown() override { profile_manager_.DeleteAllTestingProfiles(); }
...@@ -311,10 +316,7 @@ class NearbySharingServiceImplTest : public testing::Test { ...@@ -311,10 +316,7 @@ class NearbySharingServiceImplTest : public testing::Test {
EXPECT_TRUE(fake_nearby_connections_manager_->IsAdvertising()); EXPECT_TRUE(fake_nearby_connections_manager_->IsAdvertising());
} }
void SetUpCertificateManager( void SetUpCertificateManager(bool return_empty_certificate) {
const std::vector<uint8_t>& expected_encrypted_metadata,
const std::vector<uint8_t>& expected_salt,
bool return_empty_certificate) {
EXPECT_CALL(certificate_manager(), GetDecryptedPublicCertificate( EXPECT_CALL(certificate_manager(), GetDecryptedPublicCertificate(
testing::_, testing::_, testing::_)) testing::_, testing::_, testing::_))
.WillOnce(testing::Invoke([=](base::span<const uint8_t> .WillOnce(testing::Invoke([=](base::span<const uint8_t>
...@@ -322,60 +324,71 @@ class NearbySharingServiceImplTest : public testing::Test { ...@@ -322,60 +324,71 @@ class NearbySharingServiceImplTest : public testing::Test {
base::span<const uint8_t> input_salt, base::span<const uint8_t> input_salt,
MockNearbyShareCertificateManager:: MockNearbyShareCertificateManager::
CertDecryptedCallback callback) { CertDecryptedCallback callback) {
EXPECT_TRUE(std::equal(expected_salt.begin(), expected_salt.end(), std::vector<uint8_t> encrypted_metadata =
input_salt.begin(), input_salt.end())); GetNearbyShareTestEncryptedMetadata();
EXPECT_TRUE(std::equal(expected_encrypted_metadata.begin(), std::vector<uint8_t> salt = GetNearbyShareTestSalt();
expected_encrypted_metadata.end(),
EXPECT_TRUE(std::equal(salt.begin(), salt.end(), input_salt.begin(),
input_salt.end()));
EXPECT_TRUE(std::equal(encrypted_metadata.begin(),
encrypted_metadata.end(),
input_encrypted_metadata_key.begin(), input_encrypted_metadata_key.begin(),
input_encrypted_metadata_key.end())); input_encrypted_metadata_key.end()));
if (return_empty_certificate) if (return_empty_certificate) {
std::move(callback).Run(base::nullopt); std::move(callback).Run(base::nullopt);
else return;
std::move(callback).Run( }
NearbyShareDecryptedPublicCertificate::DecryptPublicCertificate(
GetNearbyShareTestPublicCertificate(), std::move(callback).Run(
GetNearbyShareTestEncryptedMetadataKey())); NearbyShareDecryptedPublicCertificate::DecryptPublicCertificate(
GetNearbyShareTestPublicCertificate(),
GetNearbyShareTestEncryptedMetadataKey()));
})); }));
} }
ShareTarget SetUpIncomingConnection( void SetUpAdvertisementDecoder(const std::vector<uint8_t>& endpoint_info,
FakeNearbyConnection& connection, bool return_empty_advertisement) {
NiceMock<MockTransferUpdateCallback>& callback) { EXPECT_CALL(mock_decoder_,
NiceMock<MockNearbySharingDecoder> mock_decoder; DecodeAdvertisement(testing::Eq(endpoint_info), testing::_))
EXPECT_CALL(mock_nearby_process_manager(),
GetOrStartNearbySharingDecoder(testing::_))
.WillRepeatedly(testing::Return(&mock_decoder));
std::vector<uint8_t> encrypted_metadata =
GetNearbyShareTestEncryptedMetadata();
std::vector<uint8_t> salt = GetNearbyShareTestSalt();
std::vector<uint8_t> v1EndpointInfo = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 10, 100, 101, 118, 105, 99, 101, 78, 97, 109, 101};
EXPECT_CALL(mock_decoder,
DecodeAdvertisement(testing::Eq(v1EndpointInfo), testing::_))
.WillOnce(testing::Invoke( .WillOnce(testing::Invoke(
[&](const std::vector<uint8_t>& data, [=](const std::vector<uint8_t>& data,
MockNearbySharingDecoder::DecodeAdvertisementCallback MockNearbySharingDecoder::DecodeAdvertisementCallback
callback) { callback) {
if (return_empty_advertisement) {
std::move(callback).Run(nullptr);
return;
}
sharing::mojom::AdvertisementPtr advertisement = sharing::mojom::AdvertisementPtr advertisement =
sharing::mojom::Advertisement::New(salt, encrypted_metadata, sharing::mojom::Advertisement::New(
kDeviceName); GetNearbyShareTestSalt(),
GetNearbyShareTestEncryptedMetadata(), kDeviceName);
std::move(callback).Run(std::move(advertisement)); std::move(callback).Run(std::move(advertisement));
})); }));
}
void SetUpIntroductionFrameDecoder(bool return_empty_introduction_frame) {
std::string intro = "introduction_frame"; std::string intro = "introduction_frame";
std::vector<uint8_t> bytes(intro.begin(), intro.end()); std::vector<uint8_t> bytes(intro.begin(), intro.end());
EXPECT_CALL(mock_decoder, DecodeFrame(testing::Eq(bytes), testing::_)) EXPECT_CALL(mock_decoder_, DecodeFrame(testing::Eq(bytes), testing::_))
.WillOnce(testing::Invoke( .WillOnce(testing::Invoke(
[&](const std::vector<uint8_t>& data, [=](const std::vector<uint8_t>& data,
MockNearbySharingDecoder::DecodeFrameCallback callback) { MockNearbySharingDecoder::DecodeFrameCallback callback) {
std::move(callback).Run(GetValidIntroductionFrame()); std::move(callback).Run(return_empty_introduction_frame
? GetEmptyIntroductionFrame()
: GetValidIntroductionFrame());
})); }));
connection_.AppendReadableData(bytes);
}
ShareTarget SetUpIncomingConnection(
NiceMock<MockTransferUpdateCallback>& callback) {
SetUpAdvertisementDecoder(kValidV1EndpointInfo,
/*return_empty_advertisement=*/false);
SetUpIntroductionFrameDecoder(/*return_empty_introduction_frame=*/false);
ShareTarget share_target; ShareTarget share_target;
connection.AppendReadableData(bytes);
ui::ScopedSetIdleState unlocked(ui::IDLE_STATE_IDLE); ui::ScopedSetIdleState unlocked(ui::IDLE_STATE_IDLE);
SetConnectionType(net::NetworkChangeNotifier::CONNECTION_WIFI); SetConnectionType(net::NetworkChangeNotifier::CONNECTION_WIFI);
base::RunLoop run_loop; base::RunLoop run_loop;
...@@ -388,11 +401,11 @@ class NearbySharingServiceImplTest : public testing::Test { ...@@ -388,11 +401,11 @@ class NearbySharingServiceImplTest : public testing::Test {
run_loop.Quit(); run_loop.Quit();
})); }));
SetUpCertificateManager(encrypted_metadata, salt, SetUpCertificateManager(/*return_empty_certificate=*/false);
/*return_empty_certificate=*/false);
SetUpReceiveSurface(callback); SetUpReceiveSurface(callback);
service_->OnIncomingConnection(kEndpointId, v1EndpointInfo, &connection); service_->OnIncomingConnection(kEndpointId, kValidV1EndpointInfo,
&connection_);
run_loop.Run(); run_loop.Run();
return share_target; return share_target;
...@@ -417,6 +430,8 @@ class NearbySharingServiceImplTest : public testing::Test { ...@@ -417,6 +430,8 @@ class NearbySharingServiceImplTest : public testing::Test {
std::unique_ptr<net::test::MockNetworkChangeNotifier> network_notifier_ = std::unique_ptr<net::test::MockNetworkChangeNotifier> network_notifier_ =
net::test::MockNetworkChangeNotifier::Create(); net::test::MockNetworkChangeNotifier::Create();
NiceMock<MockNearbyShareCertificateManager>* certificate_manager_ = nullptr; NiceMock<MockNearbyShareCertificateManager>* certificate_manager_ = nullptr;
NiceMock<MockNearbySharingDecoder> mock_decoder_;
FakeNearbyConnection connection_;
}; };
struct ValidSendSurfaceTestData { struct ValidSendSurfaceTestData {
...@@ -697,28 +712,8 @@ TEST_F(NearbySharingServiceImplTest, ...@@ -697,28 +712,8 @@ TEST_F(NearbySharingServiceImplTest,
SetConnectionType(net::NetworkChangeNotifier::CONNECTION_WIFI); SetConnectionType(net::NetworkChangeNotifier::CONNECTION_WIFI);
// Ensure decoder parses a valid endpoint advertisement. // Ensure decoder parses a valid endpoint advertisement.
NiceMock<MockNearbySharingDecoder> mock_decoder; SetUpAdvertisementDecoder(kValidV1EndpointInfo,
std::vector<uint8_t> endpoint_info(kEndpointInfo, /*return_empty_advertisement=*/false);
kEndpointInfo + kEndpointInfoSize);
std::vector<uint8_t> metadata_encryption_key(
kMetadataEncryptionKeyHashByte,
kMetadataEncryptionKeyHashByte +
sharing::Advertisement::kMetadataEncryptionKeyHashByteSize);
std::vector<uint8_t> salt(kSalt, kSalt + sharing::Advertisement::kSaltSize);
EXPECT_CALL(mock_decoder,
DecodeAdvertisement(testing::Eq(endpoint_info), testing::_))
.WillOnce(testing::Invoke(
[&salt, &metadata_encryption_key](
const std::vector<uint8_t>& data,
MockNearbySharingDecoder::DecodeAdvertisementCallback callback) {
sharing::mojom::AdvertisementPtr mojo_adv =
sharing::mojom::Advertisement::New(
salt, metadata_encryption_key, kDeviceName);
std::move(callback).Run(std::move(mojo_adv));
}));
EXPECT_CALL(mock_nearby_process_manager(),
GetOrStartNearbySharingDecoder(testing::_))
.WillRepeatedly(testing::Return(&mock_decoder));
// Start discovering, to ensure a discovery listener is registered. // Start discovering, to ensure a discovery listener is registered.
base::RunLoop run_loop; base::RunLoop run_loop;
...@@ -739,7 +734,7 @@ TEST_F(NearbySharingServiceImplTest, ...@@ -739,7 +734,7 @@ TEST_F(NearbySharingServiceImplTest,
fake_nearby_connections_manager_->OnEndpointFound( fake_nearby_connections_manager_->OnEndpointFound(
kEndpointId, kEndpointId,
location::nearby::connections::mojom::DiscoveredEndpointInfo::New( location::nearby::connections::mojom::DiscoveredEndpointInfo::New(
endpoint_info, kServiceId)); kValidV1EndpointInfo, kServiceId));
run_loop.Run(); run_loop.Run();
// Register another send surface, which will automatically catch up discovered // Register another send surface, which will automatically catch up discovered
...@@ -1265,40 +1260,20 @@ TEST_F(NearbySharingServiceImplTest, UnregisterReceiveSurfaceNeverRegistered) { ...@@ -1265,40 +1260,20 @@ TEST_F(NearbySharingServiceImplTest, UnregisterReceiveSurfaceNeverRegistered) {
TEST_F(NearbySharingServiceImplTest, TEST_F(NearbySharingServiceImplTest,
IncomingConnection_ClosedReadingIntroduction) { IncomingConnection_ClosedReadingIntroduction) {
NiceMock<MockNearbySharingDecoder> mock_decoder; SetUpAdvertisementDecoder(kValidV1EndpointInfo,
EXPECT_CALL(mock_nearby_process_manager(), /*return_empty_advertisement=*/false);
GetOrStartNearbySharingDecoder(testing::_))
.WillRepeatedly(testing::Return(&mock_decoder));
std::vector<uint8_t> encrypted_metadata =
GetNearbyShareTestEncryptedMetadata();
std::vector<uint8_t> salt = GetNearbyShareTestSalt();
std::vector<uint8_t> v1EndpointInfo = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 10, 100, 101, 118, 105, 99, 101, 78, 97, 109, 101};
EXPECT_CALL(mock_decoder,
DecodeAdvertisement(testing::Eq(v1EndpointInfo), testing::_))
.WillOnce(testing::Invoke(
[&](const std::vector<uint8_t>& data,
MockNearbySharingDecoder::DecodeAdvertisementCallback callback) {
sharing::mojom::AdvertisementPtr advertisement =
sharing::mojom::Advertisement::New(salt, encrypted_metadata,
kDeviceName);
std::move(callback).Run(std::move(advertisement));
}));
FakeNearbyConnection connection;
ui::ScopedSetIdleState unlocked(ui::IDLE_STATE_IDLE); ui::ScopedSetIdleState unlocked(ui::IDLE_STATE_IDLE);
SetConnectionType(net::NetworkChangeNotifier::CONNECTION_WIFI); SetConnectionType(net::NetworkChangeNotifier::CONNECTION_WIFI);
NiceMock<MockTransferUpdateCallback> callback; NiceMock<MockTransferUpdateCallback> callback;
EXPECT_CALL(callback, OnTransferUpdate(testing::_, testing::_)).Times(0); EXPECT_CALL(callback, OnTransferUpdate(testing::_, testing::_)).Times(0);
SetUpCertificateManager(encrypted_metadata, salt, SetUpCertificateManager(/*return_empty_certificate=*/true);
/*return_empty_certificate=*/true);
SetUpReceiveSurface(callback); SetUpReceiveSurface(callback);
service_->OnIncomingConnection(kEndpointId, v1EndpointInfo, &connection); service_->OnIncomingConnection(kEndpointId, kValidV1EndpointInfo,
connection.Close(); &connection_);
connection_.Close();
// Introduction is ignored without any side effect. // Introduction is ignored without any side effect.
...@@ -1308,46 +1283,10 @@ TEST_F(NearbySharingServiceImplTest, ...@@ -1308,46 +1283,10 @@ TEST_F(NearbySharingServiceImplTest,
TEST_F(NearbySharingServiceImplTest, TEST_F(NearbySharingServiceImplTest,
IncomingConnection_EmptyIntroductionFrame) { IncomingConnection_EmptyIntroductionFrame) {
NiceMock<MockNearbySharingDecoder> mock_decoder; SetUpAdvertisementDecoder(kValidV1EndpointInfo,
EXPECT_CALL(mock_nearby_process_manager(), /*return_empty_advertisement=*/false);
GetOrStartNearbySharingDecoder(testing::_)) SetUpIntroductionFrameDecoder(/*return_empty_introduction_frame=*/true);
.WillRepeatedly(testing::Return(&mock_decoder));
std::vector<uint8_t> encrypted_metadata =
GetNearbyShareTestEncryptedMetadata();
std::vector<uint8_t> salt = GetNearbyShareTestSalt();
std::vector<uint8_t> v1EndpointInfo = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 10, 100, 101, 118, 105, 99, 101, 78, 97, 109, 101};
EXPECT_CALL(mock_decoder,
DecodeAdvertisement(testing::Eq(v1EndpointInfo), testing::_))
.WillOnce(testing::Invoke(
[&](const std::vector<uint8_t>& data,
MockNearbySharingDecoder::DecodeAdvertisementCallback callback) {
sharing::mojom::AdvertisementPtr advertisement =
sharing::mojom::Advertisement::New(salt, encrypted_metadata,
kDeviceName);
std::move(callback).Run(std::move(advertisement));
}));
std::string intro = "introduction_frame";
std::vector<uint8_t> bytes(intro.begin(), intro.end());
EXPECT_CALL(mock_decoder, DecodeFrame(testing::Eq(bytes), testing::_))
.WillOnce(testing::Invoke(
[&](const std::vector<uint8_t>& data,
MockNearbySharingDecoder::DecodeFrameCallback callback) {
sharing::mojom::V1FramePtr mojo_v1frame =
sharing::mojom::V1Frame::New();
mojo_v1frame->set_introduction(
sharing::mojom::IntroductionFrame::New());
sharing::mojom::FramePtr mojo_frame = sharing::mojom::Frame::New();
mojo_frame->set_v1(std::move(mojo_v1frame));
std::move(callback).Run(std::move(mojo_frame));
}));
FakeNearbyConnection connection;
connection.AppendReadableData(bytes);
ui::ScopedSetIdleState unlocked(ui::IDLE_STATE_IDLE); ui::ScopedSetIdleState unlocked(ui::IDLE_STATE_IDLE);
SetConnectionType(net::NetworkChangeNotifier::CONNECTION_WIFI); SetConnectionType(net::NetworkChangeNotifier::CONNECTION_WIFI);
NiceMock<MockTransferUpdateCallback> callback; NiceMock<MockTransferUpdateCallback> callback;
...@@ -1371,15 +1310,15 @@ TEST_F(NearbySharingServiceImplTest, ...@@ -1371,15 +1310,15 @@ TEST_F(NearbySharingServiceImplTest,
run_loop.Quit(); run_loop.Quit();
})); }));
SetUpCertificateManager(encrypted_metadata, salt, SetUpCertificateManager(/*return_empty_certificate=*/false);
/*return_empty_certificate=*/false);
SetUpReceiveSurface(callback); SetUpReceiveSurface(callback);
service_->OnIncomingConnection(kEndpointId, v1EndpointInfo, &connection); service_->OnIncomingConnection(kEndpointId, kValidV1EndpointInfo,
&connection_);
run_loop.Run(); run_loop.Run();
// Check data written to connection. // Check data written to connection_.
std::vector<uint8_t> data = connection.GetWrittenData(); std::vector<uint8_t> data = connection_.GetWrittenData();
sharing::nearby::Frame frame; sharing::nearby::Frame frame;
frame.ParseFromArray(data.data(), data.size()); frame.ParseFromArray(data.data(), data.size());
...@@ -1395,39 +1334,10 @@ TEST_F(NearbySharingServiceImplTest, ...@@ -1395,39 +1334,10 @@ TEST_F(NearbySharingServiceImplTest,
TEST_F(NearbySharingServiceImplTest, TEST_F(NearbySharingServiceImplTest,
IncomingConnection_ValidIntroductionFrame_InvalidCertificate) { IncomingConnection_ValidIntroductionFrame_InvalidCertificate) {
NiceMock<MockNearbySharingDecoder> mock_decoder; SetUpAdvertisementDecoder(kValidV1EndpointInfo,
EXPECT_CALL(mock_nearby_process_manager(), /*return_empty_advertisement=*/false);
GetOrStartNearbySharingDecoder(testing::_)) SetUpIntroductionFrameDecoder(/*return_empty_introduction_frame=*/false);
.WillRepeatedly(testing::Return(&mock_decoder));
std::vector<uint8_t> encrypted_metadata =
GetNearbyShareTestEncryptedMetadata();
std::vector<uint8_t> salt = GetNearbyShareTestSalt();
std::vector<uint8_t> v1EndpointInfo = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 10, 100, 101, 118, 105, 99, 101, 78, 97, 109, 101};
EXPECT_CALL(mock_decoder,
DecodeAdvertisement(testing::Eq(v1EndpointInfo), testing::_))
.WillOnce(testing::Invoke(
[&](const std::vector<uint8_t>& data,
MockNearbySharingDecoder::DecodeAdvertisementCallback callback) {
sharing::mojom::AdvertisementPtr advertisement =
sharing::mojom::Advertisement::New(salt, encrypted_metadata,
kDeviceName);
std::move(callback).Run(std::move(advertisement));
}));
std::string intro = "introduction_frame";
std::vector<uint8_t> bytes(intro.begin(), intro.end());
EXPECT_CALL(mock_decoder, DecodeFrame(testing::Eq(bytes), testing::_))
.WillOnce(testing::Invoke(
[&](const std::vector<uint8_t>& data,
MockNearbySharingDecoder::DecodeFrameCallback callback) {
std::move(callback).Run(GetValidIntroductionFrame());
}));
FakeNearbyConnection connection;
connection.AppendReadableData(bytes);
ui::ScopedSetIdleState unlocked(ui::IDLE_STATE_IDLE); ui::ScopedSetIdleState unlocked(ui::IDLE_STATE_IDLE);
SetConnectionType(net::NetworkChangeNotifier::CONNECTION_WIFI); SetConnectionType(net::NetworkChangeNotifier::CONNECTION_WIFI);
NiceMock<MockTransferUpdateCallback> callback; NiceMock<MockTransferUpdateCallback> callback;
...@@ -1452,11 +1362,11 @@ TEST_F(NearbySharingServiceImplTest, ...@@ -1452,11 +1362,11 @@ TEST_F(NearbySharingServiceImplTest,
run_loop.Quit(); run_loop.Quit();
})); }));
SetUpCertificateManager(encrypted_metadata, salt, SetUpCertificateManager(/*return_empty_certificate=*/true);
/*return_empty_certificate=*/true);
SetUpReceiveSurface(callback); SetUpReceiveSurface(callback);
service_->OnIncomingConnection(kEndpointId, v1EndpointInfo, &connection); service_->OnIncomingConnection(kEndpointId, kValidV1EndpointInfo,
&connection_);
run_loop.Run(); run_loop.Run();
// To avoid UAF in OnIncomingTransferUpdate(). // To avoid UAF in OnIncomingTransferUpdate().
...@@ -1465,9 +1375,8 @@ TEST_F(NearbySharingServiceImplTest, ...@@ -1465,9 +1375,8 @@ TEST_F(NearbySharingServiceImplTest,
TEST_F(NearbySharingServiceImplTest, TEST_F(NearbySharingServiceImplTest,
IncomingConnection_ClosedWaitingLocalConfirmation) { IncomingConnection_ClosedWaitingLocalConfirmation) {
FakeNearbyConnection connection;
NiceMock<MockTransferUpdateCallback> callback; NiceMock<MockTransferUpdateCallback> callback;
ShareTarget share_target = SetUpIncomingConnection(connection, callback); ShareTarget share_target = SetUpIncomingConnection(callback);
base::RunLoop run_loop_2; base::RunLoop run_loop_2;
EXPECT_CALL(callback, OnTransferUpdate(testing::_, testing::_)) EXPECT_CALL(callback, OnTransferUpdate(testing::_, testing::_))
...@@ -1477,7 +1386,7 @@ TEST_F(NearbySharingServiceImplTest, ...@@ -1477,7 +1386,7 @@ TEST_F(NearbySharingServiceImplTest,
run_loop_2.Quit(); run_loop_2.Quit();
})); }));
connection.Close(); connection_.Close();
run_loop_2.Run(); run_loop_2.Run();
// To avoid UAF in OnIncomingTransferUpdate(). // To avoid UAF in OnIncomingTransferUpdate().
...@@ -1486,39 +1395,10 @@ TEST_F(NearbySharingServiceImplTest, ...@@ -1486,39 +1395,10 @@ TEST_F(NearbySharingServiceImplTest,
TEST_F(NearbySharingServiceImplTest, TEST_F(NearbySharingServiceImplTest,
IncomingConnection_ValidIntroductionFrame_ValidCertificate) { IncomingConnection_ValidIntroductionFrame_ValidCertificate) {
NiceMock<MockNearbySharingDecoder> mock_decoder; SetUpAdvertisementDecoder(kValidV1EndpointInfo,
EXPECT_CALL(mock_nearby_process_manager(), /*return_empty_advertisement=*/false);
GetOrStartNearbySharingDecoder(testing::_)) SetUpIntroductionFrameDecoder(/*return_empty_introduction_frame=*/false);
.WillRepeatedly(testing::Return(&mock_decoder));
std::vector<uint8_t> encrypted_metadata =
GetNearbyShareTestEncryptedMetadata();
std::vector<uint8_t> salt = GetNearbyShareTestSalt();
std::vector<uint8_t> v1EndpointInfo = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 10, 100, 101, 118, 105, 99, 101, 78, 97, 109, 101};
EXPECT_CALL(mock_decoder,
DecodeAdvertisement(testing::Eq(v1EndpointInfo), testing::_))
.WillOnce(testing::Invoke(
[&](const std::vector<uint8_t>& data,
MockNearbySharingDecoder::DecodeAdvertisementCallback callback) {
sharing::mojom::AdvertisementPtr advertisement =
sharing::mojom::Advertisement::New(salt, encrypted_metadata,
kDeviceName);
std::move(callback).Run(std::move(advertisement));
}));
std::string intro = "introduction_frame";
std::vector<uint8_t> bytes(intro.begin(), intro.end());
EXPECT_CALL(mock_decoder, DecodeFrame(testing::Eq(bytes), testing::_))
.WillOnce(testing::Invoke(
[&](const std::vector<uint8_t>& data,
MockNearbySharingDecoder::DecodeFrameCallback callback) {
std::move(callback).Run(GetValidIntroductionFrame());
}));
FakeNearbyConnection connection;
connection.AppendReadableData(bytes);
ui::ScopedSetIdleState unlocked(ui::IDLE_STATE_IDLE); ui::ScopedSetIdleState unlocked(ui::IDLE_STATE_IDLE);
SetConnectionType(net::NetworkChangeNotifier::CONNECTION_WIFI); SetConnectionType(net::NetworkChangeNotifier::CONNECTION_WIFI);
NiceMock<MockTransferUpdateCallback> callback; NiceMock<MockTransferUpdateCallback> callback;
...@@ -1544,11 +1424,11 @@ TEST_F(NearbySharingServiceImplTest, ...@@ -1544,11 +1424,11 @@ TEST_F(NearbySharingServiceImplTest,
run_loop.Quit(); run_loop.Quit();
})); }));
SetUpCertificateManager(encrypted_metadata, salt, SetUpCertificateManager(/*return_empty_certificate=*/false);
/*return_empty_certificate=*/false);
SetUpReceiveSurface(callback); SetUpReceiveSurface(callback);
service_->OnIncomingConnection(kEndpointId, v1EndpointInfo, &connection); service_->OnIncomingConnection(kEndpointId, kValidV1EndpointInfo,
&connection_);
run_loop.Run(); run_loop.Run();
// To avoid UAF in OnIncomingTransferUpdate(). // To avoid UAF in OnIncomingTransferUpdate().
...@@ -1571,9 +1451,8 @@ TEST_F(NearbySharingServiceImplTest, AcceptInvalidShareTarget) { ...@@ -1571,9 +1451,8 @@ TEST_F(NearbySharingServiceImplTest, AcceptInvalidShareTarget) {
} }
TEST_F(NearbySharingServiceImplTest, AcceptValidShareTarget) { TEST_F(NearbySharingServiceImplTest, AcceptValidShareTarget) {
FakeNearbyConnection connection;
NiceMock<MockTransferUpdateCallback> callback; NiceMock<MockTransferUpdateCallback> callback;
ShareTarget share_target = SetUpIncomingConnection(connection, callback); ShareTarget share_target = SetUpIncomingConnection(callback);
base::RunLoop run_loop_accept; base::RunLoop run_loop_accept;
EXPECT_CALL(callback, OnTransferUpdate(testing::_, testing::_)) EXPECT_CALL(callback, OnTransferUpdate(testing::_, testing::_))
...@@ -1595,8 +1474,8 @@ TEST_F(NearbySharingServiceImplTest, AcceptValidShareTarget) { ...@@ -1595,8 +1474,8 @@ TEST_F(NearbySharingServiceImplTest, AcceptValidShareTarget) {
EXPECT_TRUE( EXPECT_TRUE(
fake_nearby_connections_manager_->DidUpgradeBandwidth(kEndpointId)); fake_nearby_connections_manager_->DidUpgradeBandwidth(kEndpointId));
// Check data written to connection. // Check data written to connection_.
std::vector<uint8_t> data = connection.GetWrittenData(); std::vector<uint8_t> data = connection_.GetWrittenData();
sharing::nearby::Frame frame; sharing::nearby::Frame frame;
frame.ParseFromArray(data.data(), data.size()); frame.ParseFromArray(data.data(), data.size());
...@@ -1625,9 +1504,8 @@ TEST_F(NearbySharingServiceImplTest, RejectInvalidShareTarget) { ...@@ -1625,9 +1504,8 @@ TEST_F(NearbySharingServiceImplTest, RejectInvalidShareTarget) {
} }
TEST_F(NearbySharingServiceImplTest, RejectValidShareTarget) { TEST_F(NearbySharingServiceImplTest, RejectValidShareTarget) {
FakeNearbyConnection connection;
NiceMock<MockTransferUpdateCallback> callback; NiceMock<MockTransferUpdateCallback> callback;
ShareTarget share_target = SetUpIncomingConnection(connection, callback); ShareTarget share_target = SetUpIncomingConnection(callback);
base::RunLoop run_loop_reject; base::RunLoop run_loop_reject;
EXPECT_CALL(callback, OnTransferUpdate(testing::_, testing::_)) EXPECT_CALL(callback, OnTransferUpdate(testing::_, testing::_))
...@@ -1646,8 +1524,8 @@ TEST_F(NearbySharingServiceImplTest, RejectValidShareTarget) { ...@@ -1646,8 +1524,8 @@ TEST_F(NearbySharingServiceImplTest, RejectValidShareTarget) {
run_loop_reject.Run(); run_loop_reject.Run();
// Check data written to connection. // Check data written to connection_.
std::vector<uint8_t> data = connection.GetWrittenData(); std::vector<uint8_t> data = connection_.GetWrittenData();
sharing::nearby::Frame frame; sharing::nearby::Frame frame;
frame.ParseFromArray(data.data(), data.size()); frame.ParseFromArray(data.data(), data.size());
......
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