From 25291a1ab70f258cf28473000db2ca5292d53c1c Mon Sep 17 00:00:00 2001 From: Andrei Medar Date: Fri, 28 Aug 2020 13:29:12 +0000 Subject: [PATCH] modified comments. changed std::equal to absl::equal --- .../sapi_lodepng/examples/helpers.cc | 2 +- .../sapi_lodepng/examples/helpers.h | 14 ++++- .../sapi_lodepng/examples/main_sandboxed.cc | 49 ++++++++-------- .../sapi_lodepng/examples/main_unit_test.cc | 56 ++++++------------- .../sapi_lodepng/examples/main_unsandboxed.cc | 29 +++++----- .../sapi_lodepng/examples/sandbox.h | 6 +- 6 files changed, 76 insertions(+), 80 deletions(-) diff --git a/oss-internship-2020/sapi_lodepng/examples/helpers.cc b/oss-internship-2020/sapi_lodepng/examples/helpers.cc index 36aebe5..6a2c46b 100644 --- a/oss-internship-2020/sapi_lodepng/examples/helpers.cc +++ b/oss-internship-2020/sapi_lodepng/examples/helpers.cc @@ -35,4 +35,4 @@ std::string CreateTempDirAtCWD() { 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 index 7e7207d..34ebe57 100644 --- a/oss-internship-2020/sapi_lodepng/examples/helpers.h +++ b/oss-internship-2020/sapi_lodepng/examples/helpers.h @@ -12,16 +12,26 @@ // See the License for the specific language governing permissions and // limitations under the License. +#ifndef SAPI_LODEPNG_HELPERS_H +#define SAPI_LODEPNG_HELPERS_H + #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; +// Returns a vector that contains values used for testing. +// This part of code is taken from +// https://github.com/lvandeve/lodepng/blob/master/examples/example_encode.c#L96-L104. +// The generated image contains square fractals. std::vector GenerateValues(); -std::string CreateTempDirAtCWD(); \ No newline at end of file +// Creates a temporary directory in the current working directory and returns +// the path. +std::string CreateTempDirAtCWD(); + +#endif // SAPI_LODEPNG_HELPERS_H diff --git a/oss-internship-2020/sapi_lodepng/examples/main_sandboxed.cc b/oss-internship-2020/sapi_lodepng/examples/main_sandboxed.cc index 6ec680f..21ef200 100644 --- a/oss-internship-2020/sapi_lodepng/examples/main_sandboxed.cc +++ b/oss-internship-2020/sapi_lodepng/examples/main_sandboxed.cc @@ -14,16 +14,16 @@ #include -#include #include #include "helpers.h" #include "sandbox.h" void EncodeDecodeOneStep(SapiLodepngSandbox &sandbox, LodepngApi &api) { - // encode the image + // Generate the values. std::vector image(GenerateValues()); + // Encode the image sapi::v::Array sapi_image(image.data(), kImgLen); sapi::v::ConstCStr sapi_filename("/output/out_generated1.png"); @@ -33,22 +33,22 @@ void EncodeDecodeOneStep(SapiLodepngSandbox &sandbox, LodepngApi &api) { CHECK(result.ok()); CHECK(!result.value()); - // after the image has been encoded, decode it to check that the - // pixel values are the same - + // 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); result = api.lodepng_decode32_file( sapi_image_ptr.PtrBoth(), sapi_width2.PtrBoth(), sapi_height2.PtrBoth(), sapi_filename.PtrBefore()); + CHECK(result.ok()); CHECK(!result.value()); CHECK(sapi_width2.GetValue() == kWidth); CHECK(sapi_height2.GetValue() == kHeight); - // the pixels have been allocated inside the sandboxed process + // The pixels have been allocated inside the sandboxed process // memory, so we need to transfer them to this process. // Transferring the memory has the following steps: // 1) define a RemotePtr variable that holds the memory location from @@ -63,23 +63,26 @@ void EncodeDecodeOneStep(SapiLodepngSandbox &sandbox, LodepngApi &api) { CHECK(sandbox.TransferFromSandboxee(&sapi_pixels).ok()); - // now, we can compare the values - CHECK(std::equal(image.begin(), image.end(), sapi_pixels.GetData())); + // Now, we can compare the values. + CHECK(absl::equal(image.begin(), image.end(), sapi_pixels.GetData(), + sapi_pixels.GetData() + kImgLen)); + + // Free the memory allocated inside the sandbox. CHECK(sandbox.GetRpcChannel()->Free(sapi_image_ptr.GetValue()).ok()); } void EncodeDecodeTwoSteps(SapiLodepngSandbox &sandbox, LodepngApi &api) { - // generate the values + // Generate the values. std::vector image(GenerateValues()); - // encode the image into memory first + // Encode the image into memory first. 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); - // encode it into memory + // Encode it into memory. sapi::StatusOr result = api.lodepng_encode32(sapi_png_ptr.PtrBoth(), sapi_pngsize.PtrBoth(), sapi_image.PtrBefore(), kWidth, kHeight); @@ -87,15 +90,15 @@ void EncodeDecodeTwoSteps(SapiLodepngSandbox &sandbox, LodepngApi &api) { CHECK(result.ok()); CHECK(!result.value()); - // the new array (pointed to by sapi_png_ptr) is allocated + // The new array (pointed to by sapi_png_ptr) is allocated // inside the sandboxed process so we need to transfer it to this - // process + // process. sapi::v::Array sapi_png_array(sapi_pngsize.GetValue()); sapi_png_array.SetRemote(sapi_png_ptr.GetValue()); CHECK(sandbox.TransferFromSandboxee(&sapi_png_array).ok()); - // write the image into the file (from memory) + // Write the image into the file (from memory). result = api.lodepng_save_file(sapi_png_array.PtrBefore(), sapi_pngsize.GetValue(), sapi_filename.PtrBefore()); @@ -103,12 +106,12 @@ void EncodeDecodeTwoSteps(SapiLodepngSandbox &sandbox, LodepngApi &api) { CHECK(result.ok()); CHECK(!result.value()); - // now, decode the image using the 2 steps in order to compare the values + // 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::ULLong sapi_pngsize2; - // load the file in memory + // Load the file in memory. result = api.lodepng_load_file(sapi_png_ptr2.PtrBoth(), sapi_pngsize2.PtrBoth(), sapi_filename.PtrBefore()); @@ -118,14 +121,14 @@ void EncodeDecodeTwoSteps(SapiLodepngSandbox &sandbox, LodepngApi &api) { CHECK(sapi_pngsize.GetValue() == sapi_pngsize2.GetValue()); - // transfer the png array + // 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()); - // after the file is loaded, decode it so we have access to the values - // directly + // After the file is loaded, decode it so we have access to the values + // directly. sapi::v::IntBase sapi_png_ptr3(0); result = api.lodepng_decode32( sapi_png_ptr3.PtrBoth(), sapi_width2.PtrBoth(), sapi_height2.PtrBoth(), @@ -137,15 +140,17 @@ void EncodeDecodeTwoSteps(SapiLodepngSandbox &sandbox, LodepngApi &api) { CHECK(sapi_width2.GetValue() == kWidth); CHECK(sapi_height2.GetValue() == kHeight); - // transfer the pixels so they can be used + // Transfer the pixels so they can be used here. sapi::v::Array sapi_pixels(kImgLen); sapi_pixels.SetRemote(sapi_png_ptr3.GetValue()); CHECK(sandbox.TransferFromSandboxee(&sapi_pixels).ok()); - // compare values - CHECK(std::equal(image.begin(), image.end(), sapi_pixels.GetData())); + // Compare the values. + CHECK(absl::equal(image.begin(), image.end(), sapi_pixels.GetData(), + sapi_pixels.GetData() + kImgLen)); + // Free the memory allocated inside the sandbox. 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()); 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 7e788aa..dd7feed 100644 --- a/oss-internship-2020/sapi_lodepng/examples/main_unit_test.cc +++ b/oss-internship-2020/sapi_lodepng/examples/main_unit_test.cc @@ -19,21 +19,22 @@ using ::sapi::IsOk; using ::testing::Eq; -using ::testing::NotNull; using ::testing::IsTrue; +using ::testing::NotNull; namespace { TEST(HelpersTest, CreateTempDirAtCWD) { const std::string images_path = CreateTempDirAtCWD(); - EXPECT_THAT(sandbox2::file_util::fileops::Exists(images_path, false), IsTrue()); + EXPECT_THAT(sandbox2::file_util::fileops::Exists(images_path, false), + IsTrue()); ASSERT_THAT(sandbox2::file_util::fileops::DeleteRecursively(images_path), IsTrue()); } TEST(HelpersTest, GenerateValues) { - EXPECT_THAT(GenerateValues().size(), Eq(kImgLen)); + EXPECT_THAT(GenerateValues().size(), Eq(kImgLen)); } TEST(LodePngTest, Init) { @@ -45,8 +46,8 @@ TEST(LodePngTest, Init) { IsTrue()); } -// generate an image, encode it, decode it and compare the pixels with the -// initial values +// Generate an image, encode it, decode it and compare the pixels with the +// initial values. TEST(LodePngTest, EncodeDecodeOneStep) { const std::string images_path = CreateTempDirAtCWD(); @@ -54,10 +55,8 @@ TEST(LodePngTest, EncodeDecodeOneStep) { ASSERT_THAT(sandbox.Init(), IsOk()) << "Error during sandbox init"; LodepngApi api(&sandbox); - // generate the values std::vector image(GenerateValues()); - // encode the image sapi::v::Array sapi_image(image.data(), kImgLen); sapi::v::ConstCStr sapi_filename("/output/out_generated1.png"); @@ -68,8 +67,6 @@ TEST(LodePngTest, EncodeDecodeOneStep) { 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); @@ -83,32 +80,24 @@ TEST(LodePngTest, EncodeDecodeOneStep) { 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. - // Transferring the memory has the following steps: - // 1) define a RemotePtr variable that holds the memory location from - // the sandboxed process - // 2) define an array with the required length - // 3) set the remote pointer for the array to specify where the memory - // 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::Array sapi_pixels(kImgLen); sapi_pixels.SetRemote(sapi_image_ptr.GetValue()); ASSERT_THAT(sandbox.TransferFromSandboxee(&sapi_pixels), IsOk()) << "Error during transfer from sandboxee"; - // now, we can compare the values - EXPECT_THAT(std::equal(image.begin(), image.end(), sapi_pixels.GetData()), + EXPECT_THAT(absl::equal(image.begin(), image.end(), sapi_pixels.GetData(), + sapi_pixels.GetData() + kImgLen), IsTrue()) << "values differ"; + EXPECT_THAT(sandbox.GetRpcChannel()->Free(sapi_image_ptr.GetValue()), IsOk()); + 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 +// 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(LodePngTest, EncodeDecodeTwoSteps) { @@ -118,17 +107,14 @@ TEST(LodePngTest, EncodeDecodeTwoSteps) { ASSERT_THAT(sandbox.Init(), IsOk()) << "Error during sandbox init"; LodepngApi api(&sandbox); - // generate the values std::vector image(GenerateValues()); - // encode the image into memory first 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); - // encode it into memory SAPI_ASSERT_OK_AND_ASSIGN( unsigned int result, api.lodepng_encode32(sapi_png_ptr.PtrBoth(), sapi_pngsize.PtrBoth(), @@ -136,16 +122,12 @@ TEST(LodePngTest, EncodeDecodeTwoSteps) { 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::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"; - // write the image into the file (from memory) SAPI_ASSERT_OK_AND_ASSIGN( result, api.lodepng_save_file(sapi_png_array.PtrBefore(), sapi_pngsize.GetValue(), @@ -153,12 +135,10 @@ TEST(LodePngTest, EncodeDecodeTwoSteps) { ASSERT_THAT(result, Eq(0)) << "Result from save_file call not 0"; - // 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::ULLong sapi_pngsize2; - // load the file in memory SAPI_ASSERT_OK_AND_ASSIGN( result, api.lodepng_load_file(sapi_png_ptr2.PtrBoth(), sapi_pngsize2.PtrBoth(), @@ -169,15 +149,12 @@ TEST(LodePngTest, EncodeDecodeTwoSteps) { EXPECT_THAT(sapi_pngsize.GetValue(), Eq(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()); 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_ASSERT_OK_AND_ASSIGN( result, @@ -190,20 +167,23 @@ TEST(LodePngTest, EncodeDecodeTwoSteps) { 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::Array sapi_pixels(kImgLen); sapi_pixels.SetRemote(sapi_png_ptr3.GetValue()); ASSERT_THAT(sandbox.TransferFromSandboxee(&sapi_pixels), IsOk()) << "Error during transfer from sandboxee"; - // now we can compare values - EXPECT_THAT(std::equal(image.begin(), image.end(), sapi_pixels.GetData()), + EXPECT_THAT(absl::equal(image.begin(), image.end(), sapi_pixels.GetData(), + sapi_pixels.GetData() + kImgLen), IsTrue()) << "values differ"; + EXPECT_THAT(sandbox.GetRpcChannel()->Free(sapi_png_ptr.GetValue()), IsOk()); + EXPECT_THAT(sandbox.GetRpcChannel()->Free(sapi_png_ptr2.GetValue()), IsOk()); + EXPECT_THAT(sandbox.GetRpcChannel()->Free(sapi_png_ptr3.GetValue()), IsOk()); + ASSERT_THAT(sandbox2::file_util::fileops::DeleteRecursively(images_path), IsTrue()); } -} // namespace \ No newline at end of file +} // namespace diff --git a/oss-internship-2020/sapi_lodepng/examples/main_unsandboxed.cc b/oss-internship-2020/sapi_lodepng/examples/main_unsandboxed.cc index feb1f2a..bc69c0e 100644 --- a/oss-internship-2020/sapi_lodepng/examples/main_unsandboxed.cc +++ b/oss-internship-2020/sapi_lodepng/examples/main_unsandboxed.cc @@ -21,19 +21,18 @@ #include "sandboxed_api/sandbox2/util/fileops.h" void EncodeDecodeOneStep(const std::string &images_path) { - // generate the values + // Generate the values. std::vector image(GenerateValues()); - // encode the image + // Encode the image. const std::string filename = images_path + "/out_generated1.png"; unsigned int result = lodepng_encode32_file(filename.c_str(), image.data(), kWidth, kHeight); CHECK(!result); - // after the image has been encoded, decode it to check that the - // pixel values are the same - + // After the image has been encoded, decode it to check that the + // pixel values are the same. unsigned int width2, height2; uint8_t *image2 = 0; @@ -44,16 +43,17 @@ void EncodeDecodeOneStep(const std::string &images_path) { CHECK(width2 == kWidth); CHECK(height2 == kHeight); - // now, we can compare the values - CHECK(std::equal(image.begin(), image.end(), image2)); + // Now, we can compare the values. + CHECK(absl::equal(image.begin(), image.end(), image2, image2 + kImgLen)); + free(image2); } void EncodeDecodeTwoSteps(const std::string &images_path) { - // generate the values + // Generate the values. std::vector image(GenerateValues()); - // encode the image into memory first + // Encode the image into memory first. const std::string filename = images_path + "/out_generated2.png"; uint8_t *png; size_t pngsize; @@ -63,17 +63,17 @@ void EncodeDecodeTwoSteps(const std::string &images_path) { CHECK(!result); - // write the image into the file (from memory) + // Write the image into the file (from memory). result = lodepng_save_file(png, pngsize, filename.c_str()); CHECK(!result); - // now, decode the image using the 2 steps in order to compare the values + // Now, decode the image using the 2 steps in order to compare the values. unsigned int width2, height2; uint8_t *png2; size_t pngsize2; - // load the file in memory + // Load the file in memory. result = lodepng_load_file(&png2, &pngsize2, filename.c_str()); CHECK(!result); @@ -86,8 +86,9 @@ void EncodeDecodeTwoSteps(const std::string &images_path) { CHECK(width2 == kWidth); CHECK(height2 == kHeight); - // compare values - CHECK(std::equal(image.begin(), image.end(), image2)); + // Compare the values. + CHECK(absl::equal(image.begin(), image.end(), image2, image2 + kImgLen)); + free(png); free(png2); free(image2); diff --git a/oss-internship-2020/sapi_lodepng/examples/sandbox.h b/oss-internship-2020/sapi_lodepng/examples/sandbox.h index 25918e5..e0ab41b 100644 --- a/oss-internship-2020/sapi_lodepng/examples/sandbox.h +++ b/oss-internship-2020/sapi_lodepng/examples/sandbox.h @@ -21,12 +21,12 @@ class SapiLodepngSandbox : public LodepngSandbox { public: - SapiLodepngSandbox(const std::string &images_path) + explicit SapiLodepngSandbox(const std::string& images_path) : images_path_(images_path) {} private: std::unique_ptr ModifyPolicy( - sandbox2::PolicyBuilder *) override { + sandbox2::PolicyBuilder*) override { return sandbox2::PolicyBuilder() .AllowRead() .AllowWrite() @@ -46,4 +46,4 @@ class SapiLodepngSandbox : public LodepngSandbox { const std::string images_path_; }; -#endif // SAPI_LODEPNG_SANDBOX_H_ \ No newline at end of file +#endif // SAPI_LODEPNG_SANDBOX_H_