Skip to content

Commit d668eda

Browse files
Sandboxed API Teamcopybara-github
authored andcommitted
Add a status version of RecvProtoBuf
This should give more detail to callers of the function when it fails. PiperOrigin-RevId: 788906324 Change-Id: Id8e9cdec51639fe7a73dbcef23463a6f0576891d
1 parent 5dc2366 commit d668eda

3 files changed

Lines changed: 20 additions & 5 deletions

File tree

sandboxed_api/sandbox2/comms.cc

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -448,20 +448,31 @@ bool Comms::SendFD(int fd) {
448448
}
449449

450450
bool Comms::RecvProtoBuf(google::protobuf::MessageLite* message) {
451+
return RecvProtoBufWithStatus(message).ok();
452+
}
453+
454+
absl::Status Comms::RecvProtoBufWithStatus(google::protobuf::MessageLite* message) {
451455
uint32_t tag;
452456
std::vector<uint8_t> bytes;
453457
if (!RecvTLV(&tag, &bytes)) {
454458
if (IsConnected()) {
455459
SAPI_RAW_LOG(ERROR, "RecvProtoBuf failed for (%s)", name_.c_str());
460+
return absl::InternalError(
461+
absl::StrFormat("RecvTLV failed for (%s)", name_));
456462
}
457-
return false;
463+
return absl::InternalError("RecvTLV failed");
458464
}
459465

460466
if (tag != kTagProto2) {
461467
SAPI_RAW_LOG(ERROR, "Expected tag: 0x%x, got: 0x%u", kTagProto2, tag);
462-
return false;
468+
return absl::InternalError(
469+
absl::StrFormat("expected tag: 0x%x, got: 0x%u", kTagProto2, tag));
470+
}
471+
bool ok = message->ParseFromArray(bytes.data(), bytes.size());
472+
if (!ok) {
473+
return absl::InternalError("failed to parse proto from received bytes");
463474
}
464-
return message->ParseFromArray(bytes.data(), bytes.size());
475+
return absl::OkStatus();
465476
}
466477

467478
bool Comms::SendProtoBuf(const google::protobuf::MessageLite& message) {

sandboxed_api/sandbox2/comms.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ class Comms {
189189

190190
// Receives/sends protobufs.
191191
bool RecvProtoBuf(google::protobuf::MessageLite* message);
192+
absl::Status RecvProtoBufWithStatus(google::protobuf::MessageLite* message);
192193
bool SendProtoBuf(const google::protobuf::MessageLite& message);
193194

194195
// Receives/sends Status objects.

sandboxed_api/sandbox2/comms_test.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,8 @@ TEST(CommsTest, SendRecvFailsAfterTerminate) {
414414
int fd;
415415
EXPECT_THAT(comms->RecvFD(&fd), IsFalse());
416416
CommsTestMsg msg;
417-
EXPECT_THAT(comms->RecvProtoBuf(&msg), IsFalse());
417+
EXPECT_THAT(comms->RecvProtoBufWithStatus(&msg),
418+
StatusIs(absl::StatusCode::kInternal, "RecvTLV failed"));
418419
};
419420
auto b = [](Comms* comms) {};
420421
HandleCommunication(a, b);
@@ -457,7 +458,9 @@ TEST(CommsTest, RecvFDFailsOnTagMismatch) {
457458
TEST(CommsTest, RecvProtoBufFailsOnTagMismatch) {
458459
auto a = [](Comms* comms) {
459460
CommsTestMsg msg;
460-
EXPECT_THAT(comms->RecvProtoBuf(&msg), IsFalse());
461+
EXPECT_THAT(comms->RecvProtoBufWithStatus(&msg),
462+
StatusIs(absl::StatusCode::kInternal,
463+
"expected tag: 0x80000102, got: 0x2147483904"));
461464
};
462465
auto b = [](Comms* comms) {
463466
ASSERT_THAT(comms->SendString("hello"), IsTrue());

0 commit comments

Comments
 (0)