From ecbd169616a01a711432170ca0cc40c536d01aea Mon Sep 17 00:00:00 2001 From: root Date: Tue, 8 Sep 2020 14:21:57 +0000 Subject: [PATCH] modified usage of sapi::v::Array --- .../sapi_lodepng/examples/main_sandboxed.cc | 43 +++++++++++++------ .../sapi_lodepng/examples/main_unit_test.cc | 15 +++++-- .../sapi_lodepng/examples/main_unsandboxed.cc | 6 ++- 3 files changed, 46 insertions(+), 18 deletions(-) diff --git a/oss-internship-2020/sapi_lodepng/examples/main_sandboxed.cc b/oss-internship-2020/sapi_lodepng/examples/main_sandboxed.cc index 3d82ef7..36a19ba 100644 --- a/oss-internship-2020/sapi_lodepng/examples/main_sandboxed.cc +++ b/oss-internship-2020/sapi_lodepng/examples/main_sandboxed.cc @@ -24,7 +24,10 @@ void EncodeDecodeOneStep(SapiLodepngSandbox &sandbox, LodepngApi &api) { std::vector image(GenerateValues()); // Encode the image. - sapi::v::Array sapi_image(image.data(), kImgLen); + sapi::v::Array sapi_image(kImgLen); + CHECK(std::copy(image.begin(), image.end(), sapi_image.GetData())) + << "could not copy values"; + sapi::v::ConstCStr sapi_filename("/output/out_generated1.png"); sapi::StatusOr result = api.lodepng_encode32_file( @@ -59,14 +62,17 @@ void EncodeDecodeOneStep(SapiLodepngSandbox &sandbox, LodepngApi &api) { sapi::v::Array sapi_pixels(kImgLen); sapi_pixels.SetRemote(sapi_image_ptr.GetValue()); - CHECK(sandbox.TransferFromSandboxee(&sapi_pixels).ok()) << "Error during transfer from sandboxee"; + CHECK(sandbox.TransferFromSandboxee(&sapi_pixels).ok()) + << "Error during transfer from sandboxee"; // Now, we can compare the values. CHECK(absl::equal(image.begin(), image.end(), sapi_pixels.GetData(), - sapi_pixels.GetData() + kImgLen)) << "Values differ"; + sapi_pixels.GetData() + kImgLen)) + << "Values differ"; // Free the memory allocated inside the sandbox. - CHECK(sandbox.GetRpcChannel()->Free(sapi_image_ptr.GetValue()).ok()) << "Could not free memory inside sandboxed process"; + CHECK(sandbox.GetRpcChannel()->Free(sapi_image_ptr.GetValue()).ok()) + << "Could not free memory inside sandboxed process"; } void EncodeDecodeTwoSteps(SapiLodepngSandbox &sandbox, LodepngApi &api) { @@ -74,7 +80,10 @@ void EncodeDecodeTwoSteps(SapiLodepngSandbox &sandbox, LodepngApi &api) { std::vector image(GenerateValues()); // Encode the image into memory first. - sapi::v::Array sapi_image(image.data(), kImgLen); + sapi::v::Array sapi_image(kImgLen); + CHECK(std::copy(image.begin(), image.end(), sapi_image.GetData())) + << "could not copy values"; + sapi::v::ConstCStr sapi_filename("/output/out_generated2.png"); sapi::v::ULLong sapi_pngsize; @@ -94,7 +103,8 @@ void EncodeDecodeTwoSteps(SapiLodepngSandbox &sandbox, LodepngApi &api) { sapi::v::Array sapi_png_array(sapi_pngsize.GetValue()); sapi_png_array.SetRemote(sapi_png_ptr.GetValue()); - CHECK(sandbox.TransferFromSandboxee(&sapi_png_array).ok()) << "Error during transfer from sandboxee"; + CHECK(sandbox.TransferFromSandboxee(&sapi_png_array).ok()) + << "Error during transfer from sandboxee"; // Write the image into the file (from memory). result = @@ -117,13 +127,15 @@ void EncodeDecodeTwoSteps(SapiLodepngSandbox &sandbox, LodepngApi &api) { CHECK(result.ok()) << "load_file call failed"; CHECK(!result.value()) << "Unexpected result from load_file call"; - CHECK(sapi_pngsize.GetValue() == sapi_pngsize2.GetValue()) << "Png sizes differ"; + CHECK(sapi_pngsize.GetValue() == sapi_pngsize2.GetValue()) + << "Png sizes differ"; // Transfer the png array. sapi::v::Array sapi_png_array2(sapi_pngsize2.GetValue()); sapi_png_array2.SetRemote(sapi_png_ptr2.GetValue()); - CHECK(sandbox.TransferFromSandboxee(&sapi_png_array2).ok()) << "Error during transfer from sandboxee"; + CHECK(sandbox.TransferFromSandboxee(&sapi_png_array2).ok()) + << "Error during transfer from sandboxee"; // After the file is loaded, decode it so we have access to the values // directly. @@ -142,16 +154,21 @@ void EncodeDecodeTwoSteps(SapiLodepngSandbox &sandbox, LodepngApi &api) { sapi::v::Array sapi_pixels(kImgLen); sapi_pixels.SetRemote(sapi_png_ptr3.GetValue()); - CHECK(sandbox.TransferFromSandboxee(&sapi_pixels).ok()) << "Error during transfer from sandboxee"; + CHECK(sandbox.TransferFromSandboxee(&sapi_pixels).ok()) + << "Error during transfer from sandboxee"; // Compare the values. CHECK(absl::equal(image.begin(), image.end(), sapi_pixels.GetData(), - sapi_pixels.GetData() + kImgLen)) << "Values differ"; + sapi_pixels.GetData() + kImgLen)) + << "Values differ"; // Free the memory allocated inside the sandbox. - CHECK(sandbox.GetRpcChannel()->Free(sapi_png_ptr.GetValue()).ok()) << "Could not free memory inside sandboxed process"; - CHECK(sandbox.GetRpcChannel()->Free(sapi_png_ptr2.GetValue()).ok()) << "Could not free memory inside sandboxed process"; - CHECK(sandbox.GetRpcChannel()->Free(sapi_png_ptr3.GetValue()).ok()) << "Could not free memory inside sandboxed process"; + CHECK(sandbox.GetRpcChannel()->Free(sapi_png_ptr.GetValue()).ok()) + << "Could not free memory inside sandboxed process"; + CHECK(sandbox.GetRpcChannel()->Free(sapi_png_ptr2.GetValue()).ok()) + << "Could not free memory inside sandboxed process"; + CHECK(sandbox.GetRpcChannel()->Free(sapi_png_ptr3.GetValue()).ok()) + << "Could not free memory inside sandboxed process"; } int main(int argc, char *argv[]) { diff --git a/oss-internship-2020/sapi_lodepng/examples/main_unit_test.cc b/oss-internship-2020/sapi_lodepng/examples/main_unit_test.cc index ac87139..6c4d6d6 100644 --- a/oss-internship-2020/sapi_lodepng/examples/main_unit_test.cc +++ b/oss-internship-2020/sapi_lodepng/examples/main_unit_test.cc @@ -67,7 +67,11 @@ TEST(LodePngTest, EncodeDecodeOneStep) { std::vector image(GenerateValues()); - sapi::v::Array sapi_image(image.data(), kImgLen); + sapi::v::Array sapi_image(kImgLen); + EXPECT_THAT(std::copy(image.begin(), image.end(), sapi_image.GetData()), + IsTrue()) + << "could not copy values"; + sapi::v::ConstCStr sapi_filename("/output/out_generated1.png"); SAPI_ASSERT_OK_AND_ASSIGN( @@ -101,7 +105,8 @@ TEST(LodePngTest, EncodeDecodeOneStep) { IsTrue()) << "Values differ"; - EXPECT_THAT(sandbox.GetRpcChannel()->Free(sapi_image_ptr.GetValue()), IsOk()) << "Could not free memory inside sandboxed process"; + EXPECT_THAT(sandbox.GetRpcChannel()->Free(sapi_image_ptr.GetValue()), IsOk()) + << "Could not free memory inside sandboxed process"; EXPECT_THAT(sandbox2::file_util::fileops::DeleteRecursively(images_path), IsTrue()) @@ -123,7 +128,11 @@ TEST(LodePngTest, EncodeDecodeTwoSteps) { std::vector image(GenerateValues()); - sapi::v::Array sapi_image(image.data(), kImgLen); + sapi::v::Array sapi_image(kImgLen); + EXPECT_THAT(std::copy(image.begin(), image.end(), sapi_image.GetData()), + IsTrue()) + << "could not copy values"; + sapi::v::ConstCStr sapi_filename("/output/out_generated2.png"); sapi::v::ULLong sapi_pngsize; diff --git a/oss-internship-2020/sapi_lodepng/examples/main_unsandboxed.cc b/oss-internship-2020/sapi_lodepng/examples/main_unsandboxed.cc index 2e9a025..8481324 100644 --- a/oss-internship-2020/sapi_lodepng/examples/main_unsandboxed.cc +++ b/oss-internship-2020/sapi_lodepng/examples/main_unsandboxed.cc @@ -44,7 +44,8 @@ void EncodeDecodeOneStep(const std::string &images_path) { CHECK(height2 == kHeight) << "Heights differ"; // Now, we can compare the values. - CHECK(absl::equal(image.begin(), image.end(), image2, image2 + kImgLen)) << "Values differ"; + CHECK(absl::equal(image.begin(), image.end(), image2, image2 + kImgLen)) + << "Values differ"; free(image2); } @@ -87,7 +88,8 @@ void EncodeDecodeTwoSteps(const std::string &images_path) { CHECK(height2 == kHeight) << "Heights differ"; // Compare the values. - CHECK(absl::equal(image.begin(), image.end(), image2, image2 + kImgLen)) << "Values differ"; + CHECK(absl::equal(image.begin(), image.end(), image2, image2 + kImgLen)) + << "Values differ"; free(png); free(png2);