Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add APK support #2921

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sharing/attachment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Attachment::Attachment(Attachment::Family family, int64_t size,

Attachment::Attachment(int64_t id, Attachment::Family family, int64_t size,
int32_t batch_id, SourceType source_type)
: id_(id),
: id_(id == 0 ? CreateRandomId() : id),
family_(family),
size_(size),
batch_id_(batch_id),
Expand Down
38 changes: 38 additions & 0 deletions sharing/incoming_share_session.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ namespace {

using ::location::nearby::proto::sharing::OSType;
using ::location::nearby::proto::sharing::ResponseToIntroduction;
using ::nearby::sharing::service::proto::AppMetadata;
using ::nearby::sharing::service::proto::ConnectionResponseFrame;
using ::nearby::sharing::service::proto::IntroductionFrame;
using ::nearby::sharing::service::proto::V1Frame;
Expand Down Expand Up @@ -111,6 +112,43 @@ IncomingShareSession::ProcessIntroduction(
file_size_sum += file.size();
}

for (const AppMetadata& apk : introduction_frame.app_metadata()) {
if (apk.size() <= 0) {
NL_LOG(WARNING)
<< __func__
<< ": Ignore introduction, due to invalid attachment size";
return TransferMetadata::Status::kUnsupportedAttachmentType;
}

VLOG(1) << __func__ << ": Found app attachment: id=" << apk.id()
<< ", app_name=" << apk.app_name()
<< ", package_name=" << apk.package_name()
<< ", size=" << apk.size();
if (std::numeric_limits<int64_t>::max() - apk.size() < file_size_sum) {
NL_LOG(WARNING) << __func__
<< ": Ignoring introduction, total file size overflowed "
"64 bit integer.";
container.Clear();
return TransferMetadata::Status::kNotEnoughSpace;
}
// Map each apk file to a file attachment.
for (int index = 0; index < apk.file_name_size(); ++index) {
// Locally generate an attachment id for each apk file, and map it to the
// payload id.
FileAttachment apk_file(
/*id=*/0, apk.file_size(index), apk.file_name(index),
/*mime_type=*/"", service::proto::FileMetadata::ANDROID_APP);
int64_t apk_file_id = apk_file.id();
VLOG(1) << __func__ << ": Found app file name: " << apk.file_name(index)
<< ", attachment id=" << apk_file_id
<< ", file size=" << apk.file_size(index)
<< ", payload_id=" << apk.payload_id(index);
container.AddFileAttachment(std::move(apk_file));
SetAttachmentPayloadId(apk_file_id, apk.payload_id(index));
}
file_size_sum += apk.size();
}

for (const auto& text : introduction_frame.text_metadata()) {
if (text.size() <= 0) {
LOG(WARNING) << "Ignore introduction, due to invalid attachment size";
Expand Down
112 changes: 84 additions & 28 deletions sharing/incoming_share_session_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "sharing/incoming_share_session.h"

#include <algorithm>
#include <cstdint>
#include <filesystem> // NOLINT
#include <limits>
Expand Down Expand Up @@ -297,6 +298,65 @@ TEST_F(IncomingShareSessionTest, ProcessIntroductionSuccess) {
Eq(wifimeta2.payload_id()));
}

TEST_F(IncomingShareSessionTest, ProcessIntroductionWithApkSuccess) {
IntroductionFrame introduction_frame;
CHECK(proto2::TextFormat::ParseFromString(R"pb(
app_metadata {
app_name: "MyApp"
size: 300
payload_id: 9876
payload_id: 9877
payload_id: 9878
id: 1234
file_name: "MyApp.apk"
file_name: "MyApp1.apk"
file_name: "MyApp2.apk"
file_size: 100
file_size: 100
file_size: 100
package_name: "com.example.myapp"
}
)pb",
&introduction_frame));
service::proto::AppMetadata app_metadata = introduction_frame.app_metadata(0);
int64_t payload_id1 = app_metadata.payload_id(0);
int64_t payload_id2 = app_metadata.payload_id(1);
int64_t payload_id3 = app_metadata.payload_id(2);
session_.OnConnected(&connection_);

EXPECT_THAT(session_.ProcessIntroduction(introduction_frame),
Eq(std::nullopt));
EXPECT_THAT(session_.attachment_container().HasAttachments(), IsTrue());
// Find generated file attachments ids.
auto it = std::find_if(std::begin(session_.attachment_payload_map()),
std::end(session_.attachment_payload_map()),
[&](auto&& p) { return p.second == payload_id1; });
ASSERT_NE(it, std::end(session_.attachment_payload_map()));
int64_t file_id1 = it->first;
it = std::find_if(std::begin(session_.attachment_payload_map()),
std::end(session_.attachment_payload_map()),
[&](auto&& p) { return p.second == payload_id2; });
ASSERT_NE(it, std::end(session_.attachment_payload_map()));
int64_t file_id2 = it->first;
it = std::find_if(std::begin(session_.attachment_payload_map()),
std::end(session_.attachment_payload_map()),
[&](auto&& p) { return p.second == payload_id3; });
ASSERT_NE(it, std::end(session_.attachment_payload_map()));
int64_t file_id3 = it->first;
FileAttachment file1(
file_id1, app_metadata.file_size(0), app_metadata.file_name(0),
/*mime_type=*/"", service::proto::FileMetadata::ANDROID_APP);
FileAttachment file2(
file_id2, app_metadata.file_size(1), app_metadata.file_name(1),
/*mime_type=*/"", service::proto::FileMetadata::ANDROID_APP);
FileAttachment file3(
file_id3, app_metadata.file_size(2), app_metadata.file_name(2),
/*mime_type=*/"", service::proto::FileMetadata::ANDROID_APP);

EXPECT_THAT(session_.attachment_container().GetFileAttachments(),
UnorderedElementsAre(file1, file2, file3));
}

TEST_F(IncomingShareSessionTest,
PayloadTransferUpdateCompleteWithWrongPayloadType) {
connections_manager_.AcceptConnection(
Expand Down Expand Up @@ -1103,17 +1163,15 @@ TEST_F(IncomingShareSessionTest, ReadyForTransferTimeoutCancelled) {
}

TEST_F(IncomingShareSessionTest, AcceptTransferNotConnected) {
EXPECT_THAT(session_.AcceptTransfer([]() {}),
IsFalse());
EXPECT_THAT(session_.AcceptTransfer([]() {}), IsFalse());
}

TEST_F(IncomingShareSessionTest, AcceptTransferNotReady) {
session_.OnConnected(&connection_);
EXPECT_THAT(session_.ProcessIntroduction(introduction_frame_),
Eq(std::nullopt));

EXPECT_THAT(session_.AcceptTransfer([]() {}),
IsFalse());
EXPECT_THAT(session_.AcceptTransfer([]() {}), IsFalse());
}

TEST_F(IncomingShareSessionTest, AcceptTransferSuccess) {
Expand Down Expand Up @@ -1150,8 +1208,7 @@ TEST_F(IncomingShareSessionTest, AcceptTransferSuccess) {
frames_data.push(std::move(payload->content.bytes_payload.bytes));
});

EXPECT_THAT(session_.AcceptTransfer([]() {}),
IsTrue());
EXPECT_THAT(session_.AcceptTransfer([]() {}), IsTrue());

for (auto it : session_.attachment_payload_map()) {
EXPECT_THAT(
Expand Down Expand Up @@ -1310,28 +1367,27 @@ TEST_F(IncomingShareSessionTest, TryUpgradeBandwidthNotNeeded) {

TEST_F(IncomingShareSessionTest, TryUpgradeBandwidthNeeded) {
IntroductionFrame introduction_frame;
CHECK(
proto2::TextFormat::ParseFromString(R"pb(
file_metadata {
id: 1234
size: 1000000
name: "file_name1"
mime_type: "application/pdf"
type: DOCUMENT
parent_folder: "parent_folder1"
payload_id: 9876
}
file_metadata {
id: 1235
size: 200
name: "file_name2"
mime_type: "image/jpeg"
type: IMAGE
parent_folder: "parent_folder2"
payload_id: 9875
}
)pb",
&introduction_frame));
CHECK(proto2::TextFormat::ParseFromString(R"pb(
file_metadata {
id: 1234
size: 1000000
name: "file_name1"
mime_type: "application/pdf"
type: DOCUMENT
parent_folder: "parent_folder1"
payload_id: 9876
}
file_metadata {
id: 1235
size: 200
name: "file_name2"
mime_type: "image/jpeg"
type: IMAGE
parent_folder: "parent_folder2"
payload_id: 9875
}
)pb",
&introduction_frame));
session_.OnConnected(&connection_);
EXPECT_THAT(session_.ProcessIntroduction(introduction_frame),
Eq(std::nullopt));
Expand Down
Loading