use api->sandbox() instead of send sandbox as an argument

This commit is contained in:
Alexandra Latysheva 2020-10-30 14:02:34 +00:00
parent 56615d5d57
commit 64d04a80e5
2 changed files with 16 additions and 17 deletions

View File

@ -28,15 +28,15 @@ struct Data {
std::unique_ptr<sapi::v::Array<uint8_t>> row_pointers; std::unique_ptr<sapi::v::Array<uint8_t>> row_pointers;
}; };
absl::StatusOr<Data> ReadPng(LibPNGApi& api, LibPNGSapiSandbox& sandbox, absl::StatusOr<Data> ReadPng(LibPNGApi& api, absl::string_view infile) {
absl::string_view infile) {
sapi::v::Fd fd(open(infile.data(), O_RDONLY)); sapi::v::Fd fd(open(infile.data(), O_RDONLY));
if (fd.GetValue() < 0) { if (fd.GetValue() < 0) {
return absl::InternalError("Error opening input file"); return absl::InternalError("Error opening input file");
} }
SAPI_RETURN_IF_ERROR(sandbox.TransferToSandboxee(&fd)); SAPI_RETURN_IF_ERROR((&api)->sandbox()->TransferToSandboxee(&fd));
if (fd.GetRemoteFd() < 0) { if (fd.GetRemoteFd() < 0) {
return absl::InternalError("Error receiving remote FD"); return absl::InternalError("Error receiving remote FD");
} }
@ -118,14 +118,13 @@ absl::StatusOr<Data> ReadPng(LibPNGApi& api, LibPNGSapiSandbox& sandbox,
return data; return data;
} }
absl::Status WritePng(LibPNGApi& api, LibPNGSapiSandbox& sandbox, absl::Status WritePng(LibPNGApi& api, absl::string_view outfile, Data& data) {
absl::string_view outfile, Data& data) {
sapi::v::Fd fd(open(outfile.data(), O_WRONLY)); sapi::v::Fd fd(open(outfile.data(), O_WRONLY));
if (fd.GetValue() < 0) { if (fd.GetValue() < 0) {
return absl::InternalError("Error opening output file"); return absl::InternalError("Error opening output file");
} }
SAPI_RETURN_IF_ERROR(sandbox.TransferToSandboxee(&fd)); SAPI_RETURN_IF_ERROR((&api)->sandbox()->TransferToSandboxee(&fd));
if (fd.GetRemoteFd() < 0) { if (fd.GetRemoteFd() < 0) {
return absl::InternalError("Error receiving remote FD"); return absl::InternalError("Error receiving remote FD");
} }
@ -191,7 +190,7 @@ absl::Status LibPNGMain(const std::string& infile, const std::string& outfile) {
SAPI_RETURN_IF_ERROR(sandbox.Init()); SAPI_RETURN_IF_ERROR(sandbox.Init());
LibPNGApi api(&sandbox); LibPNGApi api(&sandbox);
SAPI_ASSIGN_OR_RETURN(Data data, ReadPng(api, sandbox, infile)); SAPI_ASSIGN_OR_RETURN(Data data, ReadPng(api, infile));
if (data.color_type != PNG_COLOR_TYPE_RGBA && if (data.color_type != PNG_COLOR_TYPE_RGBA &&
data.color_type != PNG_COLOR_TYPE_RGB) { data.color_type != PNG_COLOR_TYPE_RGB) {
@ -217,7 +216,7 @@ absl::Status LibPNGMain(const std::string& infile, const std::string& outfile) {
} }
} }
SAPI_RETURN_IF_ERROR(WritePng(api, sandbox, outfile, data)); SAPI_RETURN_IF_ERROR(WritePng(api, outfile, data));
return absl::OkStatus(); return absl::OkStatus();
} }

View File

@ -41,12 +41,12 @@ struct Data {
std::unique_ptr<sapi::v::Array<uint8_t>> row_pointers; std::unique_ptr<sapi::v::Array<uint8_t>> row_pointers;
}; };
void ReadPng(LibPNGApi& api, LibPNGSapiSandbox& sandbox, void ReadPng(LibPNGApi& api, absl::string_view infile, Data& data) {
absl::string_view infile, Data& data) {
sapi::v::Fd fd(open(infile.data(), O_RDONLY)); sapi::v::Fd fd(open(infile.data(), O_RDONLY));
ASSERT_THAT(fd.GetValue(), Ge(0)) << "Error opening input file"; ASSERT_THAT(fd.GetValue(), Ge(0)) << "Error opening input file";
ASSERT_THAT(sandbox.TransferToSandboxee(&fd), IsOk()); ASSERT_THAT((&api)->sandbox()->TransferToSandboxee(&fd), IsOk());
ASSERT_THAT(fd.GetRemoteFd(), Ge(0)) << "Error receiving remote FD"; ASSERT_THAT(fd.GetRemoteFd(), Ge(0)) << "Error receiving remote FD";
sapi::v::ConstCStr rb_var("rb"); sapi::v::ConstCStr rb_var("rb");
@ -132,12 +132,12 @@ void ReadPng(LibPNGApi& api, LibPNGSapiSandbox& sandbox,
ASSERT_THAT(api.png_fclose(&file), IsOk()); ASSERT_THAT(api.png_fclose(&file), IsOk());
} }
void WritePng(LibPNGApi& api, LibPNGSapiSandbox& sandbox, void WritePng(LibPNGApi& api, absl::string_view outfile, Data& data) {
absl::string_view outfile, Data& data) {
sapi::v::Fd fd(open(outfile.data(), O_WRONLY)); sapi::v::Fd fd(open(outfile.data(), O_WRONLY));
ASSERT_THAT(fd.GetValue(), Ge(0)) << "Error opening output file"; ASSERT_THAT(fd.GetValue(), Ge(0)) << "Error opening output file";
ASSERT_THAT(sandbox.TransferToSandboxee(&fd), IsOk()); ASSERT_THAT((&api)->sandbox()->TransferToSandboxee(&fd), IsOk());
ASSERT_THAT(fd.GetRemoteFd(), Ge(0)) << "Error receiving remote FD"; ASSERT_THAT(fd.GetRemoteFd(), Ge(0)) << "Error receiving remote FD";
sapi::v::ConstCStr wb_var("wb"); sapi::v::ConstCStr wb_var("wb");
@ -199,7 +199,7 @@ TEST(SandboxTest, ReadModifyWrite) {
LibPNGApi api(&sandbox); LibPNGApi api(&sandbox);
Data data; Data data;
ReadPng(api, sandbox, infile, data); ReadPng(api, infile, data);
ASSERT_THAT(data.color_type == PNG_COLOR_TYPE_RGBA || ASSERT_THAT(data.color_type == PNG_COLOR_TYPE_RGBA ||
data.color_type == PNG_COLOR_TYPE_RGB, data.color_type == PNG_COLOR_TYPE_RGB,
@ -224,10 +224,10 @@ TEST(SandboxTest, ReadModifyWrite) {
} }
} }
WritePng(api, sandbox, outfile, data); WritePng(api, outfile, data);
Data result; Data result;
ReadPng(api, sandbox, outfile, result); ReadPng(api, outfile, result);
EXPECT_THAT(result.height, Eq(data.height)); EXPECT_THAT(result.height, Eq(data.height));
EXPECT_THAT(result.width, Eq(data.width)); EXPECT_THAT(result.width, Eq(data.width));