added more error/warning messages.

This commit is contained in:
Andrei Medar 2020-08-29 18:54:08 +00:00
parent 0df7894409
commit 298aa457c1
5 changed files with 91 additions and 66 deletions

View File

@ -29,10 +29,10 @@ std::vector<uint8_t> GenerateValues() {
std::string CreateTempDirAtCWD() {
std::string cwd = sandbox2::file_util::fileops::GetCWD();
CHECK(!cwd.empty());
CHECK(!cwd.empty()) << "Could not get current working directory";
cwd.append("/");
sapi::StatusOr<std::string> result = sandbox2::CreateTempDir(cwd);
CHECK(result.ok());
CHECK(result.ok()) << "Could not create temporary directory";
return result.value();
}

View File

@ -22,7 +22,9 @@
#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;
constexpr uint32_t kWidth = 512;
constexpr uint32_t kHeight = 512;
constexpr uint32_t kImgLen = kWidth * kHeight * 4;
// Returns a vector that contains values used for testing.
// This part of code is taken from

View File

@ -30,8 +30,8 @@ void EncodeDecodeOneStep(SapiLodepngSandbox &sandbox, LodepngApi &api) {
sapi::StatusOr<unsigned int> result = api.lodepng_encode32_file(
sapi_filename.PtrBefore(), sapi_image.PtrBefore(), kWidth, kHeight);
CHECK(result.ok());
CHECK(!result.value());
CHECK(result.ok()) << "encode32_file call failed";
CHECK(!result.value()) << "Unexpected result from encode32_file call";
// After the image has been encoded, decode it to check that the
// pixel values are the same.
@ -42,11 +42,11 @@ void EncodeDecodeOneStep(SapiLodepngSandbox &sandbox, LodepngApi &api) {
sapi_image_ptr.PtrBoth(), sapi_width2.PtrBoth(), sapi_height2.PtrBoth(),
sapi_filename.PtrBefore());
CHECK(result.ok());
CHECK(!result.value());
CHECK(result.ok()) << "decode32_file call failes";
CHECK(!result.value()) << "Unexpected result from decode32_file call";
CHECK(sapi_width2.GetValue() == kWidth);
CHECK(sapi_height2.GetValue() == kHeight);
CHECK(sapi_width2.GetValue() == kWidth) << "Widths differ";
CHECK(sapi_height2.GetValue() == kHeight) << "Heights differ";
// The pixels have been allocated inside the sandboxed process
// memory, so we need to transfer them to this process.
@ -59,14 +59,14 @@ void EncodeDecodeOneStep(SapiLodepngSandbox &sandbox, LodepngApi &api) {
sapi::v::Array<uint8_t> sapi_pixels(kImgLen);
sapi_pixels.SetRemote(sapi_image_ptr.GetValue());
CHECK(sandbox.TransferFromSandboxee(&sapi_pixels).ok());
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));
sapi_pixels.GetData() + kImgLen)) << "Values differ";
// Free the memory allocated inside the sandbox.
CHECK(sandbox.GetRpcChannel()->Free(sapi_image_ptr.GetValue()).ok());
CHECK(sandbox.GetRpcChannel()->Free(sapi_image_ptr.GetValue()).ok()) << "Could not free memory inside sandboxed process";
}
void EncodeDecodeTwoSteps(SapiLodepngSandbox &sandbox, LodepngApi &api) {
@ -85,8 +85,8 @@ void EncodeDecodeTwoSteps(SapiLodepngSandbox &sandbox, LodepngApi &api) {
api.lodepng_encode32(sapi_png_ptr.PtrBoth(), sapi_pngsize.PtrBoth(),
sapi_image.PtrBefore(), kWidth, kHeight);
CHECK(result.ok());
CHECK(!result.value());
CHECK(result.ok()) << "encode32 call failed";
CHECK(!result.value()) << "Unexpected result from encode32 call";
// The new array (pointed to by sapi_png_ptr) is allocated
// inside the sandboxed process so we need to transfer it to this
@ -94,15 +94,15 @@ void EncodeDecodeTwoSteps(SapiLodepngSandbox &sandbox, LodepngApi &api) {
sapi::v::Array<uint8_t> sapi_png_array(sapi_pngsize.GetValue());
sapi_png_array.SetRemote(sapi_png_ptr.GetValue());
CHECK(sandbox.TransferFromSandboxee(&sapi_png_array).ok());
CHECK(sandbox.TransferFromSandboxee(&sapi_png_array).ok()) << "Error during transfer from sandboxee";
// Write the image into the file (from memory).
result =
api.lodepng_save_file(sapi_png_array.PtrBefore(), sapi_pngsize.GetValue(),
sapi_filename.PtrBefore());
CHECK(result.ok());
CHECK(!result.value());
CHECK(result.ok()) << "save_file call failed";
CHECK(!result.value()) << "Unexpected result from save_file call";
// Now, decode the image using the 2 steps in order to compare the values.
sapi::v::UInt sapi_width2, sapi_height2;
@ -114,16 +114,16 @@ void EncodeDecodeTwoSteps(SapiLodepngSandbox &sandbox, LodepngApi &api) {
api.lodepng_load_file(sapi_png_ptr2.PtrBoth(), sapi_pngsize2.PtrBoth(),
sapi_filename.PtrBefore());
CHECK(result.ok());
CHECK(!result.value());
CHECK(result.ok()) << "load_file call failed";
CHECK(!result.value()) << "Unexpected result from load_file call";
CHECK(sapi_pngsize.GetValue() == sapi_pngsize2.GetValue());
CHECK(sapi_pngsize.GetValue() == sapi_pngsize2.GetValue()) << "Png sizes differ";
// Transfer the png array.
sapi::v::Array<uint8_t> sapi_png_array2(sapi_pngsize2.GetValue());
sapi_png_array2.SetRemote(sapi_png_ptr2.GetValue());
CHECK(sandbox.TransferFromSandboxee(&sapi_png_array2).ok());
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.
@ -132,42 +132,46 @@ void EncodeDecodeTwoSteps(SapiLodepngSandbox &sandbox, LodepngApi &api) {
sapi_png_ptr3.PtrBoth(), sapi_width2.PtrBoth(), sapi_height2.PtrBoth(),
sapi_png_array2.PtrBefore(), sapi_pngsize2.GetValue());
CHECK(result.ok());
CHECK(!result.value());
CHECK(result.ok()) << "decode32 call failed";
CHECK(!result.value()) << "Unexpected result from decode32 call";
CHECK(sapi_width2.GetValue() == kWidth);
CHECK(sapi_height2.GetValue() == kHeight);
CHECK(sapi_width2.GetValue() == kWidth) << "Widths differ";
CHECK(sapi_height2.GetValue() == kHeight) << "Heights differ";
// Transfer the pixels so they can be used here.
sapi::v::Array<uint8_t> sapi_pixels(kImgLen);
sapi_pixels.SetRemote(sapi_png_ptr3.GetValue());
CHECK(sandbox.TransferFromSandboxee(&sapi_pixels).ok());
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));
sapi_pixels.GetData() + kImgLen)) << "Values differ";
// 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());
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[]) {
google::InitGoogleLogging(argv[0]);
const std::string images_path = CreateTempDirAtCWD();
CHECK(sandbox2::file_util::fileops::Exists(images_path, false))
<< "Temporary directory does not exist";
SapiLodepngSandbox sandbox(images_path);
CHECK(sandbox.Init().ok());
CHECK(sandbox.Init().ok()) << "Error during sandbox initialization";
LodepngApi api(&sandbox);
EncodeDecodeOneStep(sandbox, api);
EncodeDecodeTwoSteps(sandbox, api);
CHECK(sandbox2::file_util::fileops::DeleteRecursively(images_path));
if (sandbox2::file_util::fileops::DeleteRecursively(images_path)) {
LOG(WARNING) << "Temporary folder could not be deleted";
}
return EXIT_SUCCESS;
}

View File

@ -26,11 +26,13 @@ namespace {
TEST(HelpersTest, CreateTempDirAtCWD) {
const std::string images_path = CreateTempDirAtCWD();
EXPECT_THAT(sandbox2::file_util::fileops::Exists(images_path, false),
IsTrue());
ASSERT_THAT(sandbox2::file_util::fileops::Exists(images_path, false),
IsTrue())
<< "Temporary directory does not exist";
ASSERT_THAT(sandbox2::file_util::fileops::DeleteRecursively(images_path),
IsTrue());
EXPECT_THAT(sandbox2::file_util::fileops::DeleteRecursively(images_path),
IsTrue())
<< "Temporary directory could not be deleted";
}
TEST(HelpersTest, GenerateValues) {
@ -39,20 +41,28 @@ TEST(HelpersTest, GenerateValues) {
TEST(LodePngTest, Init) {
const std::string images_path = CreateTempDirAtCWD();
ASSERT_THAT(sandbox2::file_util::fileops::Exists(images_path, false),
IsTrue())
<< "Temporary directory does not exist";
SapiLodepngSandbox sandbox(images_path);
ASSERT_THAT(sandbox.Init(), IsOk()) << "Error during sandbox init";
ASSERT_THAT(sandbox2::file_util::fileops::DeleteRecursively(images_path),
IsTrue());
EXPECT_THAT(sandbox2::file_util::fileops::DeleteRecursively(images_path),
IsTrue())
<< "Temporary directory could not be deleted";
}
// Generate an image, encode it, decode it and compare the pixels with the
// initial values.
TEST(LodePngTest, EncodeDecodeOneStep) {
const std::string images_path = CreateTempDirAtCWD();
ASSERT_THAT(sandbox2::file_util::fileops::Exists(images_path, false),
IsTrue())
<< "Temporary directory does not exist";
SapiLodepngSandbox sandbox(images_path);
ASSERT_THAT(sandbox.Init(), IsOk()) << "Error during sandbox init";
ASSERT_THAT(sandbox.Init(), IsOk()) << "Error during sandbox initialization";
LodepngApi api(&sandbox);
std::vector<uint8_t> image(GenerateValues());
@ -65,7 +75,7 @@ TEST(LodePngTest, EncodeDecodeOneStep) {
api.lodepng_encode32_file(sapi_filename.PtrBefore(),
sapi_image.PtrBefore(), kWidth, kHeight));
ASSERT_THAT(result, Eq(0)) << "Result from encode32_file not 0";
ASSERT_THAT(result, Eq(0)) << "Unexpected result from encode32_file call";
sapi::v::UInt sapi_width2, sapi_height2;
sapi::v::IntBase<uint8_t *> sapi_image_ptr(0);
@ -75,7 +85,7 @@ TEST(LodePngTest, EncodeDecodeOneStep) {
sapi_image_ptr.PtrBoth(), sapi_width2.PtrBoth(),
sapi_height2.PtrBoth(), sapi_filename.PtrBefore()));
ASSERT_THAT(result, Eq(0)) << "Result from decode32_file not 0";
ASSERT_THAT(result, Eq(0)) << "Unexpected result from decode32_file call";
EXPECT_THAT(sapi_width2.GetValue(), Eq(kWidth)) << "Widths differ";
EXPECT_THAT(sapi_height2.GetValue(), Eq(kHeight)) << "Heights differ";
@ -89,12 +99,13 @@ TEST(LodePngTest, EncodeDecodeOneStep) {
EXPECT_THAT(absl::equal(image.begin(), image.end(), sapi_pixels.GetData(),
sapi_pixels.GetData() + kImgLen),
IsTrue())
<< "values differ";
<< "Values differ";
EXPECT_THAT(sandbox.GetRpcChannel()->Free(sapi_image_ptr.GetValue()), IsOk());
EXPECT_THAT(sandbox.GetRpcChannel()->Free(sapi_image_ptr.GetValue()), IsOk()) << "Could not free memory inside sandboxed process";
ASSERT_THAT(sandbox2::file_util::fileops::DeleteRecursively(images_path),
IsTrue());
EXPECT_THAT(sandbox2::file_util::fileops::DeleteRecursively(images_path),
IsTrue())
<< "Temporary directory could not be deleted";
}
// Similar to the previous test, only that we use encoding by saving the data in
@ -102,6 +113,9 @@ TEST(LodePngTest, EncodeDecodeOneStep) {
// memory and then getting the actual pixel values.
TEST(LodePngTest, EncodeDecodeTwoSteps) {
const std::string images_path = CreateTempDirAtCWD();
ASSERT_THAT(sandbox2::file_util::fileops::Exists(images_path, false),
IsTrue())
<< "Temporary directory does not exist";
SapiLodepngSandbox sandbox(images_path);
ASSERT_THAT(sandbox.Init(), IsOk()) << "Error during sandbox init";
@ -120,7 +134,7 @@ TEST(LodePngTest, EncodeDecodeTwoSteps) {
api.lodepng_encode32(sapi_png_ptr.PtrBoth(), sapi_pngsize.PtrBoth(),
sapi_image.PtrBefore(), kWidth, kHeight));
ASSERT_THAT(result, Eq(0)) << "Result from encode32 call not 0";
ASSERT_THAT(result, Eq(0)) << "Unexpected result from encode32 call";
sapi::v::Array<uint8_t> sapi_png_array(sapi_pngsize.GetValue());
sapi_png_array.SetRemote(sapi_png_ptr.GetValue());
@ -133,7 +147,7 @@ TEST(LodePngTest, EncodeDecodeTwoSteps) {
api.lodepng_save_file(sapi_png_array.PtrBefore(), sapi_pngsize.GetValue(),
sapi_filename.PtrBefore()));
ASSERT_THAT(result, Eq(0)) << "Result from save_file call not 0";
ASSERT_THAT(result, Eq(0)) << "Unexpected result from save_file call";
sapi::v::UInt sapi_width2, sapi_height2;
sapi::v::IntBase<uint8_t *> sapi_png_ptr2(0);
@ -144,7 +158,7 @@ TEST(LodePngTest, EncodeDecodeTwoSteps) {
api.lodepng_load_file(sapi_png_ptr2.PtrBoth(), sapi_pngsize2.PtrBoth(),
sapi_filename.PtrBefore()));
ASSERT_THAT(result, Eq(0)) << "Result from load_file call not 0";
ASSERT_THAT(result, Eq(0)) << "Unexpected result from load_file call";
EXPECT_THAT(sapi_pngsize.GetValue(), Eq(sapi_pngsize2.GetValue()))
<< "Png sizes differ";
@ -162,7 +176,7 @@ TEST(LodePngTest, EncodeDecodeTwoSteps) {
sapi_height2.PtrBoth(), sapi_png_array2.PtrBefore(),
sapi_pngsize2.GetValue()));
ASSERT_THAT(result, Eq(0)) << "Result from decode32 call not 0";
ASSERT_THAT(result, Eq(0)) << "Unexpected result from decode32 call";
EXPECT_THAT(sapi_width2.GetValue(), Eq(kWidth)) << "Widths differ";
EXPECT_THAT(sapi_height2.GetValue(), Eq(kHeight)) << "Heights differ";
@ -176,14 +190,15 @@ TEST(LodePngTest, EncodeDecodeTwoSteps) {
EXPECT_THAT(absl::equal(image.begin(), image.end(), sapi_pixels.GetData(),
sapi_pixels.GetData() + kImgLen),
IsTrue())
<< "values differ";
<< "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());
EXPECT_THAT(sandbox2::file_util::fileops::DeleteRecursively(images_path),
IsTrue())
<< "Temporary directory could not be deleted";
}
} // namespace

View File

@ -29,7 +29,7 @@ void EncodeDecodeOneStep(const std::string &images_path) {
unsigned int result =
lodepng_encode32_file(filename.c_str(), image.data(), kWidth, kHeight);
CHECK(!result);
CHECK(!result) << "Unexpected result from encode32_file call";
// After the image has been encoded, decode it to check that the
// pixel values are the same.
@ -38,13 +38,13 @@ void EncodeDecodeOneStep(const std::string &images_path) {
result = lodepng_decode32_file(&image2, &width2, &height2, filename.c_str());
CHECK(!result);
CHECK(!result) << "Unexpected result from decode32_file call";
CHECK(width2 == kWidth);
CHECK(height2 == kHeight);
CHECK(width2 == kWidth) << "Widths differ";
CHECK(height2 == kHeight) << "Heights differ";
// Now, we can compare the values.
CHECK(absl::equal(image.begin(), image.end(), image2, image2 + kImgLen));
CHECK(absl::equal(image.begin(), image.end(), image2, image2 + kImgLen)) << "Values differ";
free(image2);
}
@ -61,12 +61,12 @@ void EncodeDecodeTwoSteps(const std::string &images_path) {
unsigned int result =
lodepng_encode32(&png, &pngsize, image.data(), kWidth, kHeight);
CHECK(!result);
CHECK(!result) << "Unexpected result from encode32 call";
// Write the image into the file (from memory).
result = lodepng_save_file(png, pngsize, filename.c_str());
CHECK(!result);
CHECK(!result) << "Unexpected result from save_file call";
// Now, decode the image using the 2 steps in order to compare the values.
unsigned int width2, height2;
@ -76,18 +76,18 @@ void EncodeDecodeTwoSteps(const std::string &images_path) {
// Load the file in memory.
result = lodepng_load_file(&png2, &pngsize2, filename.c_str());
CHECK(!result);
CHECK(pngsize == pngsize2);
CHECK(!result) << "Unexpected result from load_file call";
CHECK(pngsize == pngsize2) << "Png sizes differ";
uint8_t *image2;
result = lodepng_decode32(&image2, &width2, &height2, png2, pngsize2);
CHECK(!result);
CHECK(width2 == kWidth);
CHECK(height2 == kHeight);
CHECK(!result) << "Unexpected result from decode32 call";
CHECK(width2 == kWidth) << "Widths differ";
CHECK(height2 == kHeight) << "Heights differ";
// Compare the values.
CHECK(absl::equal(image.begin(), image.end(), image2, image2 + kImgLen));
CHECK(absl::equal(image.begin(), image.end(), image2, image2 + kImgLen)) << "Values differ";
free(png);
free(png2);
@ -98,11 +98,15 @@ int main(int argc, char *argv[]) {
google::InitGoogleLogging(argv[0]);
const std::string images_path = CreateTempDirAtCWD();
CHECK(sandbox2::file_util::fileops::Exists(images_path, false))
<< "Temporary directory does not exist";
EncodeDecodeOneStep(images_path);
EncodeDecodeTwoSteps(images_path);
CHECK(sandbox2::file_util::fileops::DeleteRecursively(images_path));
if (sandbox2::file_util::fileops::DeleteRecursively(images_path)) {
LOG(WARNING) << "Temporary folder could not be deleted";
}
return EXIT_SUCCESS;
}