From 2db661d655e3468c5515026be03c175d49ba8c62 Mon Sep 17 00:00:00 2001 From: Andrei Medar Date: Thu, 27 Aug 2020 19:53:06 +0000 Subject: [PATCH] created a helpers file, removed asserts, removed filsystem header usage, changed constants+functions+tests naming --- .../sapi_lodepng/examples/CMakeLists.txt | 12 ++ .../sapi_lodepng/examples/helpers.cc | 38 +++++ .../sapi_lodepng/examples/helpers.h | 27 +++ .../sapi_lodepng/examples/main_sandboxed.cc | 161 +++++++----------- .../sapi_lodepng/examples/main_unit_test.cc | 138 +++++++-------- .../sapi_lodepng/examples/main_unsandboxed.cc | 95 +++++------ .../sapi_lodepng/examples/sandbox.h | 2 +- 7 files changed, 242 insertions(+), 231 deletions(-) create mode 100644 oss-internship-2020/sapi_lodepng/examples/helpers.cc create mode 100644 oss-internship-2020/sapi_lodepng/examples/helpers.h diff --git a/oss-internship-2020/sapi_lodepng/examples/CMakeLists.txt b/oss-internship-2020/sapi_lodepng/examples/CMakeLists.txt index 5df00f8..acb6f5f 100644 --- a/oss-internship-2020/sapi_lodepng/examples/CMakeLists.txt +++ b/oss-internship-2020/sapi_lodepng/examples/CMakeLists.txt @@ -15,21 +15,29 @@ # Build the unsandboxed main add_executable(lodepng_unsandboxed main_unsandboxed.cc + helpers.cc ) target_link_libraries(lodepng_unsandboxed PRIVATE lodepng + sapi::sapi + sandbox2::temp_file + sandbox2::fileops + glog::glog ) # Build the sandboxed main add_executable(lodepng_sandboxed main_sandboxed.cc sandbox.h + helpers.cc ) target_link_libraries(lodepng_sandboxed PRIVATE lodepng_sapi sapi::sapi + sandbox2::temp_file + sandbox2::fileops sapi::vars sapi::status glog::glog @@ -41,6 +49,7 @@ enable_testing() add_executable(main_unit_test main_unit_test.cc + helpers.cc ) target_link_libraries(main_unit_test PRIVATE @@ -51,8 +60,11 @@ target_link_libraries(main_unit_test PRIVATE glog::glog sapi::flags sapi::sapi + sandbox2::temp_file + sandbox2::fileops sapi::status sapi::test_main sapi::vars ) gtest_discover_tests(main_unit_test) + diff --git a/oss-internship-2020/sapi_lodepng/examples/helpers.cc b/oss-internship-2020/sapi_lodepng/examples/helpers.cc new file mode 100644 index 0000000..36aebe5 --- /dev/null +++ b/oss-internship-2020/sapi_lodepng/examples/helpers.cc @@ -0,0 +1,38 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "helpers.h" + +std::vector GenerateValues() { + std::vector image(kImgLen); + for (int y = 0; y < kHeight; ++y) { + for (int x = 0; x < kWidth; ++x) { + image[4 * kWidth * y + 4 * x + 0] = 255 * !(x & y); + image[4 * kWidth * y + 4 * x + 1] = x ^ y; + image[4 * kWidth * y + 4 * x + 2] = x | y; + image[4 * kWidth * y + 4 * x + 3] = 255; + } + } + return image; +} + +std::string CreateTempDirAtCWD() { + std::string cwd = sandbox2::file_util::fileops::GetCWD(); + CHECK(!cwd.empty()); + cwd.append("/"); + + sapi::StatusOr result = sandbox2::CreateTempDir(cwd); + CHECK(result.ok()); + return result.value(); +} \ No newline at end of file diff --git a/oss-internship-2020/sapi_lodepng/examples/helpers.h b/oss-internship-2020/sapi_lodepng/examples/helpers.h new file mode 100644 index 0000000..7e7207d --- /dev/null +++ b/oss-internship-2020/sapi_lodepng/examples/helpers.h @@ -0,0 +1,27 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include +#include + +#include "sandboxed_api/sandbox2/util/fileops.h" +#include "sandboxed_api/sandbox2/util/temp_file.h" + +constexpr uint32_t kWidth = 512, kHeight = 512, kImgLen = kWidth * kHeight * 4; + +std::vector GenerateValues(); + +std::string CreateTempDirAtCWD(); \ No newline at end of file diff --git a/oss-internship-2020/sapi_lodepng/examples/main_sandboxed.cc b/oss-internship-2020/sapi_lodepng/examples/main_sandboxed.cc index 3b9413f..6ec680f 100644 --- a/oss-internship-2020/sapi_lodepng/examples/main_sandboxed.cc +++ b/oss-internship-2020/sapi_lodepng/examples/main_sandboxed.cc @@ -12,56 +12,41 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include +#include + #include #include +#include "helpers.h" #include "sandbox.h" -#include "sandboxed_api/util/flag.h" - -ABSL_FLAG(string, images_path, std::filesystem::current_path().string(), - "path to the folder containing test images"); - -void generate_one_step(SapiLodepngSandbox &sandbox, LodepngApi &api) { - constexpr unsigned int width = 512, height = 512, - img_len = width * height * 4; - std::vector image(img_len); - - for (int y = 0; y < height; ++y) { - for (int x = 0; x < width; ++x) { - image[4 * width * y + 4 * x + 0] = 255 * !(x & y); - image[4 * width * y + 4 * x + 1] = x ^ y; - image[4 * width * y + 4 * x + 2] = x | y; - image[4 * width * y + 4 * x + 3] = 255; - } - } +void EncodeDecodeOneStep(SapiLodepngSandbox &sandbox, LodepngApi &api) { // encode the image - sapi::v::Array sapi_image(image.data(), img_len); - sapi::v::UInt sapi_width(width), sapi_height(height); + std::vector image(GenerateValues()); + + sapi::v::Array sapi_image(image.data(), kImgLen); sapi::v::ConstCStr sapi_filename("/output/out_generated1.png"); sapi::StatusOr result = api.lodepng_encode32_file( - sapi_filename.PtrBefore(), sapi_image.PtrBefore(), sapi_width.GetValue(), - sapi_height.GetValue()); + sapi_filename.PtrBefore(), sapi_image.PtrBefore(), kWidth, kHeight); - assert(result.ok()); - assert(!result.value()); + CHECK(result.ok()); + CHECK(!result.value()); // after the image has been encoded, decode it to check that the // pixel values are the same sapi::v::UInt sapi_width2, sapi_height2; - sapi::v::IntBase sapi_image_ptr(0); + sapi::v::IntBase sapi_image_ptr(0); result = api.lodepng_decode32_file( sapi_image_ptr.PtrBoth(), sapi_width2.PtrBoth(), sapi_height2.PtrBoth(), sapi_filename.PtrBefore()); - assert(result.ok()); - assert(!result.value()); + CHECK(result.ok()); + CHECK(!result.value()); - assert(sapi_width2.GetValue() == width); - assert(sapi_height2.GetValue() == height); + CHECK(sapi_width2.GetValue() == kWidth); + CHECK(sapi_height2.GetValue() == kHeight); // the pixels have been allocated inside the sandboxed process // memory, so we need to transfer them to this process. @@ -73,74 +58,54 @@ void generate_one_step(SapiLodepngSandbox &sandbox, LodepngApi &api) { // that will be transferred is located // 4) transfer the memory to this process (this step is why we need // the pointer and the length) - sapi::v::RemotePtr sapi_remote_out_ptr(sapi_image_ptr.GetValue()); - sapi::v::Array sapi_pixels(img_len); - sapi_pixels.SetRemote(sapi_remote_out_ptr.GetValue()); + sapi::v::Array sapi_pixels(kImgLen); + sapi_pixels.SetRemote(sapi_image_ptr.GetValue()); - assert(sandbox.TransferFromSandboxee(&sapi_pixels).ok()); - - // after the memory has been transferred, we can access it - // using the GetData function - unsigned char *pixels_ptr = sapi_pixels.GetData(); + CHECK(sandbox.TransferFromSandboxee(&sapi_pixels).ok()); // now, we can compare the values - for (size_t i = 0; i < img_len; ++i) { - assert(pixels_ptr[i] == image[i]); - } + CHECK(std::equal(image.begin(), image.end(), sapi_pixels.GetData())); + CHECK(sandbox.GetRpcChannel()->Free(sapi_image_ptr.GetValue()).ok()); } -void generate_two_steps(SapiLodepngSandbox &sandbox, LodepngApi &api) { +void EncodeDecodeTwoSteps(SapiLodepngSandbox &sandbox, LodepngApi &api) { // generate the values - constexpr unsigned int width = 512, height = 512, - img_len = width * height * 4; - std::vector image(img_len); - - for (int y = 0; y < height; ++y) { - for (int x = 0; x < width; ++x) { - image[4 * width * y + 4 * x + 0] = 255 * !(x & y); - image[4 * width * y + 4 * x + 1] = x ^ y; - image[4 * width * y + 4 * x + 2] = x | y; - image[4 * width * y + 4 * x + 3] = 255; - } - } + std::vector image(GenerateValues()); // encode the image into memory first - sapi::v::Array sapi_image(image.data(), img_len); - sapi::v::UInt sapi_width(width), sapi_height(height); + sapi::v::Array sapi_image(image.data(), kImgLen); sapi::v::ConstCStr sapi_filename("/output/out_generated2.png"); sapi::v::ULLong sapi_pngsize; - sapi::v::IntBase sapi_png_ptr(0); + sapi::v::IntBase sapi_png_ptr(0); // encode it into memory - sapi::StatusOr result = api.lodepng_encode32( - sapi_png_ptr.PtrBoth(), sapi_pngsize.PtrBoth(), sapi_image.PtrBefore(), - sapi_width.GetValue(), sapi_height.GetValue()); + sapi::StatusOr result = + api.lodepng_encode32(sapi_png_ptr.PtrBoth(), sapi_pngsize.PtrBoth(), + sapi_image.PtrBefore(), kWidth, kHeight); - assert(result.ok()); - assert(!result.value()); + CHECK(result.ok()); + CHECK(!result.value()); // the new array (pointed to by sapi_png_ptr) is allocated // inside the sandboxed process so we need to transfer it to this // process - sapi::v::RemotePtr sapi_remote_out_ptr(sapi_png_ptr.GetValue()); - sapi::v::Array sapi_png_array(sapi_pngsize.GetValue()); + sapi::v::Array sapi_png_array(sapi_pngsize.GetValue()); + sapi_png_array.SetRemote(sapi_png_ptr.GetValue()); - sapi_png_array.SetRemote(sapi_remote_out_ptr.GetValue()); - - assert(sandbox.TransferFromSandboxee(&sapi_png_array).ok()); + CHECK(sandbox.TransferFromSandboxee(&sapi_png_array).ok()); // write the image into the file (from memory) result = api.lodepng_save_file(sapi_png_array.PtrBefore(), sapi_pngsize.GetValue(), sapi_filename.PtrBefore()); - assert(result.ok()); - assert(!result.value()); + CHECK(result.ok()); + CHECK(!result.value()); // now, decode the image using the 2 steps in order to compare the values sapi::v::UInt sapi_width2, sapi_height2; - sapi::v::IntBase sapi_png_ptr2(0); + sapi::v::IntBase sapi_png_ptr2(0); sapi::v::ULLong sapi_pngsize2; // load the file in memory @@ -148,58 +113,58 @@ void generate_two_steps(SapiLodepngSandbox &sandbox, LodepngApi &api) { api.lodepng_load_file(sapi_png_ptr2.PtrBoth(), sapi_pngsize2.PtrBoth(), sapi_filename.PtrBefore()); - assert(result.ok()); - assert(!result.value()); + CHECK(result.ok()); + CHECK(!result.value()); - assert(sapi_pngsize.GetValue() == sapi_pngsize2.GetValue()); + CHECK(sapi_pngsize.GetValue() == sapi_pngsize2.GetValue()); // transfer the png array - sapi::v::RemotePtr sapi_remote_out_ptr2(sapi_png_ptr2.GetValue()); - sapi::v::Array sapi_png_array2(sapi_pngsize2.GetValue()); + sapi::v::Array sapi_png_array2(sapi_pngsize2.GetValue()); + sapi_png_array2.SetRemote(sapi_png_ptr2.GetValue()); - sapi_png_array2.SetRemote(sapi_remote_out_ptr2.GetValue()); - - assert(sandbox.TransferFromSandboxee(&sapi_png_array2).ok()); + CHECK(sandbox.TransferFromSandboxee(&sapi_png_array2).ok()); // after the file is loaded, decode it so we have access to the values // directly - sapi::v::IntBase sapi_png_ptr3(0); + sapi::v::IntBase sapi_png_ptr3(0); result = api.lodepng_decode32( sapi_png_ptr3.PtrBoth(), sapi_width2.PtrBoth(), sapi_height2.PtrBoth(), sapi_png_array2.PtrBefore(), sapi_pngsize2.GetValue()); - assert(result.ok()); - assert(!result.value()); + CHECK(result.ok()); + CHECK(!result.value()); - assert(sapi_width2.GetValue() == width); - assert(sapi_height2.GetValue() == height); + CHECK(sapi_width2.GetValue() == kWidth); + CHECK(sapi_height2.GetValue() == kHeight); // transfer the pixels so they can be used - sapi::v::RemotePtr sapi_remote_out_ptr3(sapi_png_ptr3.GetValue()); - sapi::v::Array sapi_pixels(img_len); + sapi::v::Array sapi_pixels(kImgLen); + sapi_pixels.SetRemote(sapi_png_ptr3.GetValue()); - sapi_pixels.SetRemote(sapi_remote_out_ptr3.GetValue()); - - assert(sandbox.TransferFromSandboxee(&sapi_pixels).ok()); - - unsigned char *pixels_ptr = sapi_pixels.GetData(); + CHECK(sandbox.TransferFromSandboxee(&sapi_pixels).ok()); // compare values - for (size_t i = 0; i < img_len; ++i) { - assert(pixels_ptr[i] == image[i]); - } + CHECK(std::equal(image.begin(), image.end(), sapi_pixels.GetData())); + + CHECK(sandbox.GetRpcChannel()->Free(sapi_png_ptr.GetValue()).ok()); + CHECK(sandbox.GetRpcChannel()->Free(sapi_png_ptr2.GetValue()).ok()); + CHECK(sandbox.GetRpcChannel()->Free(sapi_png_ptr3.GetValue()).ok()); } int main(int argc, char *argv[]) { - gflags::ParseCommandLineFlags(&argc, &argv, true); + google::InitGoogleLogging(argv[0]); - SapiLodepngSandbox sandbox(absl::GetFlag(FLAGS_images_path)); - assert(sandbox.Init().ok()); + const std::string images_path = CreateTempDirAtCWD(); + + SapiLodepngSandbox sandbox(images_path); + CHECK(sandbox.Init().ok()); LodepngApi api(&sandbox); - generate_one_step(sandbox, api); - generate_two_steps(sandbox, api); + EncodeDecodeOneStep(sandbox, api); + EncodeDecodeTwoSteps(sandbox, api); + + CHECK(sandbox2::file_util::fileops::DeleteRecursively(images_path)); return EXIT_SUCCESS; } 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 bd71525..7e788aa 100644 --- a/oss-internship-2020/sapi_lodepng/examples/main_unit_test.cc +++ b/oss-internship-2020/sapi_lodepng/examples/main_unit_test.cc @@ -12,64 +12,66 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include "gtest/gtest.h" +#include "helpers.h" #include "sandbox.h" #include "sandboxed_api/util/status_matchers.h" -using sapi::IsOk; -using testing::Eq; -using testing::NotNull; +using ::sapi::IsOk; +using ::testing::Eq; +using ::testing::NotNull; +using ::testing::IsTrue; namespace { -// use the current path -const std::string images_path = std::filesystem::current_path().string(); +TEST(HelpersTest, CreateTempDirAtCWD) { + const std::string images_path = CreateTempDirAtCWD(); + EXPECT_THAT(sandbox2::file_util::fileops::Exists(images_path, false), IsTrue()); -TEST(initSandbox, basic) { + ASSERT_THAT(sandbox2::file_util::fileops::DeleteRecursively(images_path), + IsTrue()); +} + +TEST(HelpersTest, GenerateValues) { + EXPECT_THAT(GenerateValues().size(), Eq(kImgLen)); +} + +TEST(LodePngTest, Init) { + const std::string images_path = CreateTempDirAtCWD(); SapiLodepngSandbox sandbox(images_path); ASSERT_THAT(sandbox.Init(), IsOk()) << "Error during sandbox init"; + + ASSERT_THAT(sandbox2::file_util::fileops::DeleteRecursively(images_path), + IsTrue()); } // generate an image, encode it, decode it and compare the pixels with the // initial values -TEST(generate_image, encode_decode_compare_one_step) { +TEST(LodePngTest, EncodeDecodeOneStep) { + const std::string images_path = CreateTempDirAtCWD(); + SapiLodepngSandbox sandbox(images_path); ASSERT_THAT(sandbox.Init(), IsOk()) << "Error during sandbox init"; LodepngApi api(&sandbox); // generate the values - constexpr unsigned int width = 512, height = 512, - img_len = width * height * 4; - std::vector image(img_len); - - for (int y = 0; y < height; ++y) { - for (int x = 0; x < width; ++x) { - image[4 * width * y + 4 * x + 0] = 255 * !(x & y); - image[4 * width * y + 4 * x + 1] = x ^ y; - image[4 * width * y + 4 * x + 2] = x | y; - image[4 * width * y + 4 * x + 3] = 255; - } - } + std::vector image(GenerateValues()); // encode the image - sapi::v::Array sapi_image(image.data(), img_len); - sapi::v::UInt sapi_width(width), sapi_height(height); + sapi::v::Array sapi_image(image.data(), kImgLen); sapi::v::ConstCStr sapi_filename("/output/out_generated1.png"); SAPI_ASSERT_OK_AND_ASSIGN( unsigned int result, api.lodepng_encode32_file(sapi_filename.PtrBefore(), - sapi_image.PtrBefore(), sapi_width.GetValue(), - sapi_height.GetValue())); + sapi_image.PtrBefore(), kWidth, kHeight)); ASSERT_THAT(result, Eq(0)) << "Result from encode32_file not 0"; // after the image has been encoded, decode it to check that the // pixel values are the same sapi::v::UInt sapi_width2, sapi_height2; - sapi::v::IntBase sapi_image_ptr(0); + sapi::v::IntBase sapi_image_ptr(0); SAPI_ASSERT_OK_AND_ASSIGN( result, api.lodepng_decode32_file( @@ -78,8 +80,8 @@ TEST(generate_image, encode_decode_compare_one_step) { ASSERT_THAT(result, Eq(0)) << "Result from decode32_file not 0"; - EXPECT_THAT(sapi_width2.GetValue(), Eq(width)) << "Widths differ"; - EXPECT_THAT(sapi_height2.GetValue(), Eq(height)) << "Heights differ"; + EXPECT_THAT(sapi_width2.GetValue(), Eq(kWidth)) << "Widths differ"; + EXPECT_THAT(sapi_height2.GetValue(), Eq(kHeight)) << "Heights differ"; // the pixels have been allocated inside the sandboxed process // memory, so we need to transfer them to this process. @@ -91,69 +93,54 @@ TEST(generate_image, encode_decode_compare_one_step) { // that will be transferred is located // 4) transfer the memory to this process (this step is why we need // the pointer and the length) - sapi::v::RemotePtr sapi_remote_out_ptr(sapi_image_ptr.GetValue()); - sapi::v::Array sapi_pixels(img_len); - sapi_pixels.SetRemote(sapi_remote_out_ptr.GetValue()); + sapi::v::Array sapi_pixels(kImgLen); + sapi_pixels.SetRemote(sapi_image_ptr.GetValue()); ASSERT_THAT(sandbox.TransferFromSandboxee(&sapi_pixels), IsOk()) << "Error during transfer from sandboxee"; - // after the memory has been transferred, we can access it - // using the GetData function - unsigned char *pixels_ptr = sapi_pixels.GetData(); - // now, we can compare the values - for (size_t i = 0; i < img_len; ++i) { - EXPECT_THAT(pixels_ptr[i], Eq(image[i])) << "Pixels values differ"; - } + EXPECT_THAT(std::equal(image.begin(), image.end(), sapi_pixels.GetData()), + IsTrue()) + << "values differ"; + + ASSERT_THAT(sandbox2::file_util::fileops::DeleteRecursively(images_path), + IsTrue()); } // similar to the previous test, only that we use encoding by saving the data in // memory and then writing it to the file and decoding by first decoding in // memory and then getting the actual pixel values. -TEST(generate_image, encode_decode_compare_two_steps) { +TEST(LodePngTest, EncodeDecodeTwoSteps) { + const std::string images_path = CreateTempDirAtCWD(); + SapiLodepngSandbox sandbox(images_path); ASSERT_THAT(sandbox.Init(), IsOk()) << "Error during sandbox init"; LodepngApi api(&sandbox); // generate the values - constexpr unsigned int width = 512, height = 512, - img_len = width * height * 4; - std::vector image(img_len); - - for (int y = 0; y < height; ++y) { - for (int x = 0; x < width; ++x) { - image[4 * width * y + 4 * x + 0] = 255 * !(x & y); - image[4 * width * y + 4 * x + 1] = x ^ y; - image[4 * width * y + 4 * x + 2] = x | y; - image[4 * width * y + 4 * x + 3] = 255; - } - } + std::vector image(GenerateValues()); // encode the image into memory first - sapi::v::Array sapi_image(image.data(), img_len); - sapi::v::UInt sapi_width(width), sapi_height(height); + sapi::v::Array sapi_image(image.data(), kImgLen); sapi::v::ConstCStr sapi_filename("/output/out_generated2.png"); sapi::v::ULLong sapi_pngsize; - sapi::v::IntBase sapi_png_ptr(0); + sapi::v::IntBase sapi_png_ptr(0); // encode it into memory SAPI_ASSERT_OK_AND_ASSIGN( unsigned int result, api.lodepng_encode32(sapi_png_ptr.PtrBoth(), sapi_pngsize.PtrBoth(), - sapi_image.PtrBefore(), sapi_width.GetValue(), - sapi_height.GetValue())); + sapi_image.PtrBefore(), kWidth, kHeight)); ASSERT_THAT(result, Eq(0)) << "Result from encode32 call not 0"; // the new array (pointed to by sapi_png_ptr) is allocated // inside the sandboxed process so we need to transfer it to this // process - sapi::v::RemotePtr sapi_remote_out_ptr(sapi_png_ptr.GetValue()); - sapi::v::Array sapi_png_array(sapi_pngsize.GetValue()); - - sapi_png_array.SetRemote(sapi_remote_out_ptr.GetValue()); + sapi::v::Array sapi_png_array(sapi_pngsize.GetValue()); + sapi_png_array.SetRemote(sapi_png_ptr.GetValue()); ASSERT_THAT(sandbox.TransferFromSandboxee(&sapi_png_array), IsOk()) << "Error during transfer from sandboxee"; @@ -168,7 +155,7 @@ TEST(generate_image, encode_decode_compare_two_steps) { // now, decode the image using the 2 steps in order to compare the values sapi::v::UInt sapi_width2, sapi_height2; - sapi::v::IntBase sapi_png_ptr2(0); + sapi::v::IntBase sapi_png_ptr2(0); sapi::v::ULLong sapi_pngsize2; // load the file in memory @@ -183,17 +170,15 @@ TEST(generate_image, encode_decode_compare_two_steps) { << "Png sizes differ"; // transfer the png array - sapi::v::RemotePtr sapi_remote_out_ptr2(sapi_png_ptr2.GetValue()); - sapi::v::Array sapi_png_array2(sapi_pngsize2.GetValue()); - - sapi_png_array2.SetRemote(sapi_remote_out_ptr2.GetValue()); + sapi::v::Array sapi_png_array2(sapi_pngsize2.GetValue()); + sapi_png_array2.SetRemote(sapi_png_ptr2.GetValue()); ASSERT_THAT(sandbox.TransferFromSandboxee(&sapi_png_array2), IsOk()) << "Error during transfer from sandboxee"; // after the file is loaded, decode it so we have access to the values // directly - sapi::v::IntBase sapi_png_ptr3(0); + sapi::v::IntBase sapi_png_ptr3(0); SAPI_ASSERT_OK_AND_ASSIGN( result, api.lodepng_decode32(sapi_png_ptr3.PtrBoth(), sapi_width2.PtrBoth(), @@ -202,24 +187,23 @@ TEST(generate_image, encode_decode_compare_two_steps) { ASSERT_THAT(result, Eq(0)) << "Result from decode32 call not 0"; - EXPECT_THAT(sapi_width2.GetValue(), Eq(width)) << "Widths differ"; - EXPECT_THAT(sapi_height2.GetValue(), Eq(height)) << "Heights differ"; + EXPECT_THAT(sapi_width2.GetValue(), Eq(kWidth)) << "Widths differ"; + EXPECT_THAT(sapi_height2.GetValue(), Eq(kHeight)) << "Heights differ"; // transfer the pixels so they can be used - sapi::v::RemotePtr sapi_remote_out_ptr3(sapi_png_ptr3.GetValue()); - sapi::v::Array sapi_pixels(img_len); - - sapi_pixels.SetRemote(sapi_remote_out_ptr3.GetValue()); + sapi::v::Array sapi_pixels(kImgLen); + sapi_pixels.SetRemote(sapi_png_ptr3.GetValue()); ASSERT_THAT(sandbox.TransferFromSandboxee(&sapi_pixels), IsOk()) << "Error during transfer from sandboxee"; - unsigned char *pixels_ptr = sapi_pixels.GetData(); + // now we can compare values + EXPECT_THAT(std::equal(image.begin(), image.end(), sapi_pixels.GetData()), + IsTrue()) + << "values differ"; - // compare values - for (size_t i = 0; i < img_len; ++i) { - EXPECT_THAT(pixels_ptr[i], Eq(image[i])) << "Pixel values differ"; - } + ASSERT_THAT(sandbox2::file_util::fileops::DeleteRecursively(images_path), + IsTrue()); } } // namespace \ No newline at end of file diff --git a/oss-internship-2020/sapi_lodepng/examples/main_unsandboxed.cc b/oss-internship-2020/sapi_lodepng/examples/main_unsandboxed.cc index 9d0aa64..feb1f2a 100644 --- a/oss-internship-2020/sapi_lodepng/examples/main_unsandboxed.cc +++ b/oss-internship-2020/sapi_lodepng/examples/main_unsandboxed.cc @@ -12,111 +12,96 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include -#include +#include + #include #include "../lodepng/lodepng.h" +#include "helpers.h" +#include "sandboxed_api/sandbox2/util/fileops.h" -void generate_one_step(const std::string &images_path) { - constexpr unsigned int width = 512, height = 512, - img_len = width * height * 4; - std::vector image(img_len); - - for (int y = 0; y < height; ++y) { - for (int x = 0; x < width; ++x) { - image[4 * width * y + 4 * x + 0] = 255 * !(x & y); - image[4 * width * y + 4 * x + 1] = x ^ y; - image[4 * width * y + 4 * x + 2] = x | y; - image[4 * width * y + 4 * x + 3] = 255; - } - } +void EncodeDecodeOneStep(const std::string &images_path) { + // generate the values + std::vector image(GenerateValues()); // encode the image const std::string filename = images_path + "/out_generated1.png"; unsigned int result = - lodepng_encode32_file(filename.c_str(), image.data(), width, height); + lodepng_encode32_file(filename.c_str(), image.data(), kWidth, kHeight); - assert(!result); + CHECK(!result); // after the image has been encoded, decode it to check that the // pixel values are the same unsigned int width2, height2; - unsigned char *image2 = 0; + uint8_t *image2 = 0; result = lodepng_decode32_file(&image2, &width2, &height2, filename.c_str()); - assert(!result); + CHECK(!result); - assert(width2 == width); - assert(height2 == height); + CHECK(width2 == kWidth); + CHECK(height2 == kHeight); // now, we can compare the values - for (size_t i = 0; i < img_len; ++i) { - assert(image2[i] == image[i]); - } + CHECK(std::equal(image.begin(), image.end(), image2)); + free(image2); } -void generate_two_steps(const std::string &images_path) { +void EncodeDecodeTwoSteps(const std::string &images_path) { // generate the values - constexpr unsigned int width = 512, height = 512, - img_len = width * height * 4; - std::vector image(img_len); - - for (int y = 0; y < height; ++y) { - for (int x = 0; x < width; ++x) { - image[4 * width * y + 4 * x + 0] = 255 * !(x & y); - image[4 * width * y + 4 * x + 1] = x ^ y; - image[4 * width * y + 4 * x + 2] = x | y; - image[4 * width * y + 4 * x + 3] = 255; - } - } + std::vector image(GenerateValues()); // encode the image into memory first const std::string filename = images_path + "/out_generated2.png"; - unsigned char *png; + uint8_t *png; size_t pngsize; unsigned int result = - lodepng_encode32(&png, &pngsize, image.data(), width, height); + lodepng_encode32(&png, &pngsize, image.data(), kWidth, kHeight); - assert(!result); + CHECK(!result); // write the image into the file (from memory) result = lodepng_save_file(png, pngsize, filename.c_str()); - assert(!result); + CHECK(!result); // now, decode the image using the 2 steps in order to compare the values unsigned int width2, height2; - unsigned char *png2; + uint8_t *png2; size_t pngsize2; // load the file in memory result = lodepng_load_file(&png2, &pngsize2, filename.c_str()); - assert(!result); - assert(pngsize == pngsize2); + CHECK(!result); + CHECK(pngsize == pngsize2); - unsigned char *image2; + uint8_t *image2; result = lodepng_decode32(&image2, &width2, &height2, png2, pngsize2); - assert(!result); - assert(width2 == width); - assert(height2 == height); + CHECK(!result); + CHECK(width2 == kWidth); + CHECK(height2 == kHeight); // compare values - for (size_t i = 0; i < img_len; ++i) { - assert(image2[i] == image[i]); - } + CHECK(std::equal(image.begin(), image.end(), image2)); + free(png); + free(png2); + free(image2); } int main(int argc, char *argv[]) { - const std::string images_path = std::filesystem::current_path().string(); + google::InitGoogleLogging(argv[0]); - generate_one_step(images_path); - generate_two_steps(images_path); + const std::string images_path = CreateTempDirAtCWD(); + + EncodeDecodeOneStep(images_path); + EncodeDecodeTwoSteps(images_path); + + CHECK(sandbox2::file_util::fileops::DeleteRecursively(images_path)); return EXIT_SUCCESS; -} \ No newline at end of file +} diff --git a/oss-internship-2020/sapi_lodepng/examples/sandbox.h b/oss-internship-2020/sapi_lodepng/examples/sandbox.h index a3658cc..25918e5 100644 --- a/oss-internship-2020/sapi_lodepng/examples/sandbox.h +++ b/oss-internship-2020/sapi_lodepng/examples/sandbox.h @@ -24,6 +24,7 @@ class SapiLodepngSandbox : public LodepngSandbox { SapiLodepngSandbox(const std::string &images_path) : images_path_(images_path) {} + private: std::unique_ptr ModifyPolicy( sandbox2::PolicyBuilder *) override { return sandbox2::PolicyBuilder() @@ -42,7 +43,6 @@ class SapiLodepngSandbox : public LodepngSandbox { .BuildOrDie(); } - private: const std::string images_path_; };