created a helpers file, removed asserts, removed filsystem header usage, changed constants+functions+tests naming

This commit is contained in:
Andrei Medar 2020-08-27 19:53:06 +00:00
parent 47b519dba4
commit 2db661d655
7 changed files with 242 additions and 231 deletions

View File

@ -15,21 +15,29 @@
# Build the unsandboxed main # Build the unsandboxed main
add_executable(lodepng_unsandboxed add_executable(lodepng_unsandboxed
main_unsandboxed.cc main_unsandboxed.cc
helpers.cc
) )
target_link_libraries(lodepng_unsandboxed PRIVATE target_link_libraries(lodepng_unsandboxed PRIVATE
lodepng lodepng
sapi::sapi
sandbox2::temp_file
sandbox2::fileops
glog::glog
) )
# Build the sandboxed main # Build the sandboxed main
add_executable(lodepng_sandboxed add_executable(lodepng_sandboxed
main_sandboxed.cc main_sandboxed.cc
sandbox.h sandbox.h
helpers.cc
) )
target_link_libraries(lodepng_sandboxed PRIVATE target_link_libraries(lodepng_sandboxed PRIVATE
lodepng_sapi lodepng_sapi
sapi::sapi sapi::sapi
sandbox2::temp_file
sandbox2::fileops
sapi::vars sapi::vars
sapi::status sapi::status
glog::glog glog::glog
@ -41,6 +49,7 @@ enable_testing()
add_executable(main_unit_test add_executable(main_unit_test
main_unit_test.cc main_unit_test.cc
helpers.cc
) )
target_link_libraries(main_unit_test PRIVATE target_link_libraries(main_unit_test PRIVATE
@ -51,8 +60,11 @@ target_link_libraries(main_unit_test PRIVATE
glog::glog glog::glog
sapi::flags sapi::flags
sapi::sapi sapi::sapi
sandbox2::temp_file
sandbox2::fileops
sapi::status sapi::status
sapi::test_main sapi::test_main
sapi::vars sapi::vars
) )
gtest_discover_tests(main_unit_test) gtest_discover_tests(main_unit_test)

View File

@ -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<uint8_t> GenerateValues() {
std::vector<uint8_t> 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<std::string> result = sandbox2::CreateTempDir(cwd);
CHECK(result.ok());
return result.value();
}

View File

@ -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 <glog/logging.h>
#include <cstdint>
#include <vector>
#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<uint8_t> GenerateValues();
std::string CreateTempDirAtCWD();

View File

@ -12,56 +12,41 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include <cassert> #include <glog/logging.h>
#include <filesystem> #include <filesystem>
#include <iostream> #include <iostream>
#include "helpers.h"
#include "sandbox.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<unsigned char> 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 // encode the image
sapi::v::Array<unsigned char> sapi_image(image.data(), img_len); std::vector<uint8_t> image(GenerateValues());
sapi::v::UInt sapi_width(width), sapi_height(height);
sapi::v::Array<uint8_t> sapi_image(image.data(), kImgLen);
sapi::v::ConstCStr sapi_filename("/output/out_generated1.png"); sapi::v::ConstCStr sapi_filename("/output/out_generated1.png");
sapi::StatusOr<unsigned int> result = api.lodepng_encode32_file( sapi::StatusOr<unsigned int> result = api.lodepng_encode32_file(
sapi_filename.PtrBefore(), sapi_image.PtrBefore(), sapi_width.GetValue(), sapi_filename.PtrBefore(), sapi_image.PtrBefore(), kWidth, kHeight);
sapi_height.GetValue());
assert(result.ok()); CHECK(result.ok());
assert(!result.value()); CHECK(!result.value());
// after the image has been encoded, decode it to check that the // after the image has been encoded, decode it to check that the
// pixel values are the same // pixel values are the same
sapi::v::UInt sapi_width2, sapi_height2; sapi::v::UInt sapi_width2, sapi_height2;
sapi::v::IntBase<unsigned char *> sapi_image_ptr(0); sapi::v::IntBase<uint8_t *> sapi_image_ptr(0);
result = api.lodepng_decode32_file( result = api.lodepng_decode32_file(
sapi_image_ptr.PtrBoth(), sapi_width2.PtrBoth(), sapi_height2.PtrBoth(), sapi_image_ptr.PtrBoth(), sapi_width2.PtrBoth(), sapi_height2.PtrBoth(),
sapi_filename.PtrBefore()); sapi_filename.PtrBefore());
assert(result.ok()); CHECK(result.ok());
assert(!result.value()); CHECK(!result.value());
assert(sapi_width2.GetValue() == width); CHECK(sapi_width2.GetValue() == kWidth);
assert(sapi_height2.GetValue() == height); 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. // 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 // that will be transferred is located
// 4) transfer the memory to this process (this step is why we need // 4) transfer the memory to this process (this step is why we need
// the pointer and the length) // the pointer and the length)
sapi::v::RemotePtr sapi_remote_out_ptr(sapi_image_ptr.GetValue()); sapi::v::Array<uint8_t> sapi_pixels(kImgLen);
sapi::v::Array<unsigned char> sapi_pixels(img_len); sapi_pixels.SetRemote(sapi_image_ptr.GetValue());
sapi_pixels.SetRemote(sapi_remote_out_ptr.GetValue());
assert(sandbox.TransferFromSandboxee(&sapi_pixels).ok()); CHECK(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();
// now, we can compare the values // now, we can compare the values
for (size_t i = 0; i < img_len; ++i) { CHECK(std::equal(image.begin(), image.end(), sapi_pixels.GetData()));
assert(pixels_ptr[i] == image[i]); 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 // generate the values
constexpr unsigned int width = 512, height = 512, std::vector<uint8_t> image(GenerateValues());
img_len = width * height * 4;
std::vector<unsigned char> 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;
}
}
// encode the image into memory first // encode the image into memory first
sapi::v::Array<unsigned char> sapi_image(image.data(), img_len); sapi::v::Array<uint8_t> sapi_image(image.data(), kImgLen);
sapi::v::UInt sapi_width(width), sapi_height(height);
sapi::v::ConstCStr sapi_filename("/output/out_generated2.png"); sapi::v::ConstCStr sapi_filename("/output/out_generated2.png");
sapi::v::ULLong sapi_pngsize; sapi::v::ULLong sapi_pngsize;
sapi::v::IntBase<unsigned char *> sapi_png_ptr(0); sapi::v::IntBase<uint8_t *> sapi_png_ptr(0);
// encode it into memory // encode it into memory
sapi::StatusOr<unsigned int> result = api.lodepng_encode32( sapi::StatusOr<unsigned int> result =
sapi_png_ptr.PtrBoth(), sapi_pngsize.PtrBoth(), sapi_image.PtrBefore(), api.lodepng_encode32(sapi_png_ptr.PtrBoth(), sapi_pngsize.PtrBoth(),
sapi_width.GetValue(), sapi_height.GetValue()); sapi_image.PtrBefore(), kWidth, kHeight);
assert(result.ok()); CHECK(result.ok());
assert(!result.value()); 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 // inside the sandboxed process so we need to transfer it to this
// process // process
sapi::v::RemotePtr sapi_remote_out_ptr(sapi_png_ptr.GetValue()); sapi::v::Array<uint8_t> sapi_png_array(sapi_pngsize.GetValue());
sapi::v::Array<unsigned char> sapi_png_array(sapi_pngsize.GetValue()); sapi_png_array.SetRemote(sapi_png_ptr.GetValue());
sapi_png_array.SetRemote(sapi_remote_out_ptr.GetValue()); CHECK(sandbox.TransferFromSandboxee(&sapi_png_array).ok());
assert(sandbox.TransferFromSandboxee(&sapi_png_array).ok());
// write the image into the file (from memory) // write the image into the file (from memory)
result = result =
api.lodepng_save_file(sapi_png_array.PtrBefore(), sapi_pngsize.GetValue(), api.lodepng_save_file(sapi_png_array.PtrBefore(), sapi_pngsize.GetValue(),
sapi_filename.PtrBefore()); sapi_filename.PtrBefore());
assert(result.ok()); CHECK(result.ok());
assert(!result.value()); 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::UInt sapi_width2, sapi_height2;
sapi::v::IntBase<unsigned char *> sapi_png_ptr2(0); sapi::v::IntBase<uint8_t *> sapi_png_ptr2(0);
sapi::v::ULLong sapi_pngsize2; sapi::v::ULLong sapi_pngsize2;
// load the file in memory // 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(), api.lodepng_load_file(sapi_png_ptr2.PtrBoth(), sapi_pngsize2.PtrBoth(),
sapi_filename.PtrBefore()); sapi_filename.PtrBefore());
assert(result.ok()); CHECK(result.ok());
assert(!result.value()); CHECK(!result.value());
assert(sapi_pngsize.GetValue() == sapi_pngsize2.GetValue()); CHECK(sapi_pngsize.GetValue() == sapi_pngsize2.GetValue());
// transfer the png array // transfer the png array
sapi::v::RemotePtr sapi_remote_out_ptr2(sapi_png_ptr2.GetValue()); sapi::v::Array<uint8_t> sapi_png_array2(sapi_pngsize2.GetValue());
sapi::v::Array<unsigned char> sapi_png_array2(sapi_pngsize2.GetValue()); sapi_png_array2.SetRemote(sapi_png_ptr2.GetValue());
sapi_png_array2.SetRemote(sapi_remote_out_ptr2.GetValue()); CHECK(sandbox.TransferFromSandboxee(&sapi_png_array2).ok());
assert(sandbox.TransferFromSandboxee(&sapi_png_array2).ok());
// after the file is loaded, decode it so we have access to the values // after the file is loaded, decode it so we have access to the values
// directly // directly
sapi::v::IntBase<unsigned char *> sapi_png_ptr3(0); sapi::v::IntBase<uint8_t *> sapi_png_ptr3(0);
result = api.lodepng_decode32( result = api.lodepng_decode32(
sapi_png_ptr3.PtrBoth(), sapi_width2.PtrBoth(), sapi_height2.PtrBoth(), sapi_png_ptr3.PtrBoth(), sapi_width2.PtrBoth(), sapi_height2.PtrBoth(),
sapi_png_array2.PtrBefore(), sapi_pngsize2.GetValue()); sapi_png_array2.PtrBefore(), sapi_pngsize2.GetValue());
assert(result.ok()); CHECK(result.ok());
assert(!result.value()); CHECK(!result.value());
assert(sapi_width2.GetValue() == width); CHECK(sapi_width2.GetValue() == kWidth);
assert(sapi_height2.GetValue() == height); CHECK(sapi_height2.GetValue() == kHeight);
// transfer the pixels so they can be used // transfer the pixels so they can be used
sapi::v::RemotePtr sapi_remote_out_ptr3(sapi_png_ptr3.GetValue()); sapi::v::Array<uint8_t> sapi_pixels(kImgLen);
sapi::v::Array<unsigned char> sapi_pixels(img_len); sapi_pixels.SetRemote(sapi_png_ptr3.GetValue());
sapi_pixels.SetRemote(sapi_remote_out_ptr3.GetValue()); CHECK(sandbox.TransferFromSandboxee(&sapi_pixels).ok());
assert(sandbox.TransferFromSandboxee(&sapi_pixels).ok());
unsigned char *pixels_ptr = sapi_pixels.GetData();
// compare values // compare values
for (size_t i = 0; i < img_len; ++i) { CHECK(std::equal(image.begin(), image.end(), sapi_pixels.GetData()));
assert(pixels_ptr[i] == image[i]);
} 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[]) { int main(int argc, char *argv[]) {
gflags::ParseCommandLineFlags(&argc, &argv, true); google::InitGoogleLogging(argv[0]);
SapiLodepngSandbox sandbox(absl::GetFlag(FLAGS_images_path)); const std::string images_path = CreateTempDirAtCWD();
assert(sandbox.Init().ok());
SapiLodepngSandbox sandbox(images_path);
CHECK(sandbox.Init().ok());
LodepngApi api(&sandbox); LodepngApi api(&sandbox);
generate_one_step(sandbox, api); EncodeDecodeOneStep(sandbox, api);
generate_two_steps(sandbox, api); EncodeDecodeTwoSteps(sandbox, api);
CHECK(sandbox2::file_util::fileops::DeleteRecursively(images_path));
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -12,64 +12,66 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include <filesystem>
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "helpers.h"
#include "sandbox.h" #include "sandbox.h"
#include "sandboxed_api/util/status_matchers.h" #include "sandboxed_api/util/status_matchers.h"
using sapi::IsOk; using ::sapi::IsOk;
using testing::Eq; using ::testing::Eq;
using testing::NotNull; using ::testing::NotNull;
using ::testing::IsTrue;
namespace { namespace {
// use the current path TEST(HelpersTest, CreateTempDirAtCWD) {
const std::string images_path = std::filesystem::current_path().string(); 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); SapiLodepngSandbox sandbox(images_path);
ASSERT_THAT(sandbox.Init(), IsOk()) << "Error during sandbox init"; 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 // generate an image, encode it, decode it and compare the pixels with the
// initial values // initial values
TEST(generate_image, encode_decode_compare_one_step) { TEST(LodePngTest, EncodeDecodeOneStep) {
const std::string images_path = CreateTempDirAtCWD();
SapiLodepngSandbox sandbox(images_path); SapiLodepngSandbox sandbox(images_path);
ASSERT_THAT(sandbox.Init(), IsOk()) << "Error during sandbox init"; ASSERT_THAT(sandbox.Init(), IsOk()) << "Error during sandbox init";
LodepngApi api(&sandbox); LodepngApi api(&sandbox);
// generate the values // generate the values
constexpr unsigned int width = 512, height = 512, std::vector<uint8_t> image(GenerateValues());
img_len = width * height * 4;
std::vector<unsigned char> 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;
}
}
// encode the image // encode the image
sapi::v::Array<unsigned char> sapi_image(image.data(), img_len); sapi::v::Array<uint8_t> sapi_image(image.data(), kImgLen);
sapi::v::UInt sapi_width(width), sapi_height(height);
sapi::v::ConstCStr sapi_filename("/output/out_generated1.png"); sapi::v::ConstCStr sapi_filename("/output/out_generated1.png");
SAPI_ASSERT_OK_AND_ASSIGN( SAPI_ASSERT_OK_AND_ASSIGN(
unsigned int result, unsigned int result,
api.lodepng_encode32_file(sapi_filename.PtrBefore(), api.lodepng_encode32_file(sapi_filename.PtrBefore(),
sapi_image.PtrBefore(), sapi_width.GetValue(), sapi_image.PtrBefore(), kWidth, kHeight));
sapi_height.GetValue()));
ASSERT_THAT(result, Eq(0)) << "Result from encode32_file not 0"; ASSERT_THAT(result, Eq(0)) << "Result from encode32_file not 0";
// after the image has been encoded, decode it to check that the // after the image has been encoded, decode it to check that the
// pixel values are the same // pixel values are the same
sapi::v::UInt sapi_width2, sapi_height2; sapi::v::UInt sapi_width2, sapi_height2;
sapi::v::IntBase<unsigned char *> sapi_image_ptr(0); sapi::v::IntBase<uint8_t *> sapi_image_ptr(0);
SAPI_ASSERT_OK_AND_ASSIGN( SAPI_ASSERT_OK_AND_ASSIGN(
result, api.lodepng_decode32_file( 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"; ASSERT_THAT(result, Eq(0)) << "Result from decode32_file not 0";
EXPECT_THAT(sapi_width2.GetValue(), Eq(width)) << "Widths differ"; EXPECT_THAT(sapi_width2.GetValue(), Eq(kWidth)) << "Widths differ";
EXPECT_THAT(sapi_height2.GetValue(), Eq(height)) << "Heights differ"; EXPECT_THAT(sapi_height2.GetValue(), Eq(kHeight)) << "Heights differ";
// 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. // 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 // that will be transferred is located
// 4) transfer the memory to this process (this step is why we need // 4) transfer the memory to this process (this step is why we need
// the pointer and the length) // the pointer and the length)
sapi::v::RemotePtr sapi_remote_out_ptr(sapi_image_ptr.GetValue()); sapi::v::Array<uint8_t> sapi_pixels(kImgLen);
sapi::v::Array<unsigned char> sapi_pixels(img_len); sapi_pixels.SetRemote(sapi_image_ptr.GetValue());
sapi_pixels.SetRemote(sapi_remote_out_ptr.GetValue());
ASSERT_THAT(sandbox.TransferFromSandboxee(&sapi_pixels), IsOk()) ASSERT_THAT(sandbox.TransferFromSandboxee(&sapi_pixels), IsOk())
<< "Error during transfer from sandboxee"; << "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 // now, we can compare the values
for (size_t i = 0; i < img_len; ++i) { EXPECT_THAT(std::equal(image.begin(), image.end(), sapi_pixels.GetData()),
EXPECT_THAT(pixels_ptr[i], Eq(image[i])) << "Pixels values differ"; 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 // 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 writing it to the file and decoding by first decoding in
// memory and then getting the actual pixel values. // 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); SapiLodepngSandbox sandbox(images_path);
ASSERT_THAT(sandbox.Init(), IsOk()) << "Error during sandbox init"; ASSERT_THAT(sandbox.Init(), IsOk()) << "Error during sandbox init";
LodepngApi api(&sandbox); LodepngApi api(&sandbox);
// generate the values // generate the values
constexpr unsigned int width = 512, height = 512, std::vector<uint8_t> image(GenerateValues());
img_len = width * height * 4;
std::vector<unsigned char> 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;
}
}
// encode the image into memory first // encode the image into memory first
sapi::v::Array<unsigned char> sapi_image(image.data(), img_len); sapi::v::Array<uint8_t> sapi_image(image.data(), kImgLen);
sapi::v::UInt sapi_width(width), sapi_height(height);
sapi::v::ConstCStr sapi_filename("/output/out_generated2.png"); sapi::v::ConstCStr sapi_filename("/output/out_generated2.png");
sapi::v::ULLong sapi_pngsize; sapi::v::ULLong sapi_pngsize;
sapi::v::IntBase<unsigned char *> sapi_png_ptr(0); sapi::v::IntBase<uint8_t *> sapi_png_ptr(0);
// encode it into memory // encode it into memory
SAPI_ASSERT_OK_AND_ASSIGN( SAPI_ASSERT_OK_AND_ASSIGN(
unsigned int result, unsigned int result,
api.lodepng_encode32(sapi_png_ptr.PtrBoth(), sapi_pngsize.PtrBoth(), api.lodepng_encode32(sapi_png_ptr.PtrBoth(), sapi_pngsize.PtrBoth(),
sapi_image.PtrBefore(), sapi_width.GetValue(), sapi_image.PtrBefore(), kWidth, kHeight));
sapi_height.GetValue()));
ASSERT_THAT(result, Eq(0)) << "Result from encode32 call not 0"; ASSERT_THAT(result, Eq(0)) << "Result from encode32 call not 0";
// 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 // inside the sandboxed process so we need to transfer it to this
// process // process
sapi::v::RemotePtr sapi_remote_out_ptr(sapi_png_ptr.GetValue()); sapi::v::Array<uint8_t> sapi_png_array(sapi_pngsize.GetValue());
sapi::v::Array<unsigned char> sapi_png_array(sapi_pngsize.GetValue()); sapi_png_array.SetRemote(sapi_png_ptr.GetValue());
sapi_png_array.SetRemote(sapi_remote_out_ptr.GetValue());
ASSERT_THAT(sandbox.TransferFromSandboxee(&sapi_png_array), IsOk()) ASSERT_THAT(sandbox.TransferFromSandboxee(&sapi_png_array), IsOk())
<< "Error during transfer from sandboxee"; << "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 // now, decode the image using the 2 steps in order to compare the values
sapi::v::UInt sapi_width2, sapi_height2; sapi::v::UInt sapi_width2, sapi_height2;
sapi::v::IntBase<unsigned char *> sapi_png_ptr2(0); sapi::v::IntBase<uint8_t *> sapi_png_ptr2(0);
sapi::v::ULLong sapi_pngsize2; sapi::v::ULLong sapi_pngsize2;
// load the file in memory // load the file in memory
@ -183,17 +170,15 @@ TEST(generate_image, encode_decode_compare_two_steps) {
<< "Png sizes differ"; << "Png sizes differ";
// transfer the png array // transfer the png array
sapi::v::RemotePtr sapi_remote_out_ptr2(sapi_png_ptr2.GetValue()); sapi::v::Array<uint8_t> sapi_png_array2(sapi_pngsize2.GetValue());
sapi::v::Array<unsigned char> sapi_png_array2(sapi_pngsize2.GetValue()); sapi_png_array2.SetRemote(sapi_png_ptr2.GetValue());
sapi_png_array2.SetRemote(sapi_remote_out_ptr2.GetValue());
ASSERT_THAT(sandbox.TransferFromSandboxee(&sapi_png_array2), IsOk()) ASSERT_THAT(sandbox.TransferFromSandboxee(&sapi_png_array2), IsOk())
<< "Error during transfer from sandboxee"; << "Error during transfer from sandboxee";
// after the file is loaded, decode it so we have access to the values // after the file is loaded, decode it so we have access to the values
// directly // directly
sapi::v::IntBase<unsigned char *> sapi_png_ptr3(0); sapi::v::IntBase<uint8_t *> sapi_png_ptr3(0);
SAPI_ASSERT_OK_AND_ASSIGN( SAPI_ASSERT_OK_AND_ASSIGN(
result, result,
api.lodepng_decode32(sapi_png_ptr3.PtrBoth(), sapi_width2.PtrBoth(), 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"; ASSERT_THAT(result, Eq(0)) << "Result from decode32 call not 0";
EXPECT_THAT(sapi_width2.GetValue(), Eq(width)) << "Widths differ"; EXPECT_THAT(sapi_width2.GetValue(), Eq(kWidth)) << "Widths differ";
EXPECT_THAT(sapi_height2.GetValue(), Eq(height)) << "Heights differ"; EXPECT_THAT(sapi_height2.GetValue(), Eq(kHeight)) << "Heights differ";
// transfer the pixels so they can be used // transfer the pixels so they can be used
sapi::v::RemotePtr sapi_remote_out_ptr3(sapi_png_ptr3.GetValue()); sapi::v::Array<uint8_t> sapi_pixels(kImgLen);
sapi::v::Array<unsigned char> sapi_pixels(img_len); sapi_pixels.SetRemote(sapi_png_ptr3.GetValue());
sapi_pixels.SetRemote(sapi_remote_out_ptr3.GetValue());
ASSERT_THAT(sandbox.TransferFromSandboxee(&sapi_pixels), IsOk()) ASSERT_THAT(sandbox.TransferFromSandboxee(&sapi_pixels), IsOk())
<< "Error during transfer from sandboxee"; << "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 ASSERT_THAT(sandbox2::file_util::fileops::DeleteRecursively(images_path),
for (size_t i = 0; i < img_len; ++i) { IsTrue());
EXPECT_THAT(pixels_ptr[i], Eq(image[i])) << "Pixel values differ";
}
} }
} // namespace } // namespace

View File

@ -12,111 +12,96 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include <cassert> #include <glog/logging.h>
#include <filesystem>
#include <iostream> #include <iostream>
#include "../lodepng/lodepng.h" #include "../lodepng/lodepng.h"
#include "helpers.h"
#include "sandboxed_api/sandbox2/util/fileops.h"
void generate_one_step(const std::string &images_path) { void EncodeDecodeOneStep(const std::string &images_path) {
constexpr unsigned int width = 512, height = 512, // generate the values
img_len = width * height * 4; std::vector<uint8_t> image(GenerateValues());
std::vector<unsigned char> 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;
}
}
// encode the image // encode the image
const std::string filename = images_path + "/out_generated1.png"; const std::string filename = images_path + "/out_generated1.png";
unsigned int result = 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 // after the image has been encoded, decode it to check that the
// pixel values are the same // pixel values are the same
unsigned int width2, height2; unsigned int width2, height2;
unsigned char *image2 = 0; uint8_t *image2 = 0;
result = lodepng_decode32_file(&image2, &width2, &height2, filename.c_str()); result = lodepng_decode32_file(&image2, &width2, &height2, filename.c_str());
assert(!result); CHECK(!result);
assert(width2 == width); CHECK(width2 == kWidth);
assert(height2 == height); CHECK(height2 == kHeight);
// now, we can compare the values // now, we can compare the values
for (size_t i = 0; i < img_len; ++i) { CHECK(std::equal(image.begin(), image.end(), image2));
assert(image2[i] == image[i]); free(image2);
}
} }
void generate_two_steps(const std::string &images_path) { void EncodeDecodeTwoSteps(const std::string &images_path) {
// generate the values // generate the values
constexpr unsigned int width = 512, height = 512, std::vector<uint8_t> image(GenerateValues());
img_len = width * height * 4;
std::vector<unsigned char> 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;
}
}
// encode the image into memory first // encode the image into memory first
const std::string filename = images_path + "/out_generated2.png"; const std::string filename = images_path + "/out_generated2.png";
unsigned char *png; uint8_t *png;
size_t pngsize; size_t pngsize;
unsigned int result = 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) // write the image into the file (from memory)
result = lodepng_save_file(png, pngsize, filename.c_str()); 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 // now, decode the image using the 2 steps in order to compare the values
unsigned int width2, height2; unsigned int width2, height2;
unsigned char *png2; uint8_t *png2;
size_t pngsize2; size_t pngsize2;
// load the file in memory // load the file in memory
result = lodepng_load_file(&png2, &pngsize2, filename.c_str()); result = lodepng_load_file(&png2, &pngsize2, filename.c_str());
assert(!result); CHECK(!result);
assert(pngsize == pngsize2); CHECK(pngsize == pngsize2);
unsigned char *image2; uint8_t *image2;
result = lodepng_decode32(&image2, &width2, &height2, png2, pngsize2); result = lodepng_decode32(&image2, &width2, &height2, png2, pngsize2);
assert(!result); CHECK(!result);
assert(width2 == width); CHECK(width2 == kWidth);
assert(height2 == height); CHECK(height2 == kHeight);
// compare values // compare values
for (size_t i = 0; i < img_len; ++i) { CHECK(std::equal(image.begin(), image.end(), image2));
assert(image2[i] == image[i]); free(png);
} free(png2);
free(image2);
} }
int main(int argc, char *argv[]) { 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); const std::string images_path = CreateTempDirAtCWD();
generate_two_steps(images_path);
EncodeDecodeOneStep(images_path);
EncodeDecodeTwoSteps(images_path);
CHECK(sandbox2::file_util::fileops::DeleteRecursively(images_path));
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -24,6 +24,7 @@ class SapiLodepngSandbox : public LodepngSandbox {
SapiLodepngSandbox(const std::string &images_path) SapiLodepngSandbox(const std::string &images_path)
: images_path_(images_path) {} : images_path_(images_path) {}
private:
std::unique_ptr<sandbox2::Policy> ModifyPolicy( std::unique_ptr<sandbox2::Policy> ModifyPolicy(
sandbox2::PolicyBuilder *) override { sandbox2::PolicyBuilder *) override {
return sandbox2::PolicyBuilder() return sandbox2::PolicyBuilder()
@ -42,7 +43,6 @@ class SapiLodepngSandbox : public LodepngSandbox {
.BuildOrDie(); .BuildOrDie();
} }
private:
const std::string images_path_; const std::string images_path_;
}; };