added some comments in main_unit_test.cc and .gitignore

This commit is contained in:
Andrei Medar 2020-08-12 11:20:02 +00:00
parent b979486ff1
commit 3d015be05c
2 changed files with 25 additions and 13 deletions

View File

@ -0,0 +1 @@
build/

View File

@ -47,7 +47,8 @@ TEST(generate_image, encode_decode_compare_one_step) {
SapiLodepngSandbox sandbox(images_path); SapiLodepngSandbox sandbox(images_path);
ASSERT_TRUE(sandbox.Init().ok()); ASSERT_TRUE(sandbox.Init().ok());
LodepngApi api(&sandbox); LodepngApi api(&sandbox);
// std::cout << "path = " << images_path << std::endl;
// generate the values
unsigned int width = 512, height = 512; unsigned int width = 512, height = 512;
unsigned char *image = (unsigned char *)malloc(width * height * 4); unsigned char *image = (unsigned char *)malloc(width * height * 4);
@ -60,14 +61,12 @@ TEST(generate_image, encode_decode_compare_one_step) {
} }
} }
// encode the image
sapi::v::Array<unsigned char> sapi_image(image, width * height * 4); sapi::v::Array<unsigned char> sapi_image(image, width * height * 4);
sapi::v::UInt sapi_width(width), sapi_height(height); sapi::v::UInt sapi_width(width), sapi_height(height);
std::string filename = images_path + "/out_generated1.png"; std::string filename = images_path + "/out_generated1.png";
sapi::v::ConstCStr sapi_filename(filename.c_str()); sapi::v::ConstCStr sapi_filename(filename.c_str());
// ASSERT_TRUE(sandbox.Allocate(&image_).ok());
// ASSERT_TRUE(sandbox.TransferToSandboxee(&image_).ok());
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(), sapi_width.GetValue(),
sapi_height.GetValue()); sapi_height.GetValue());
@ -75,6 +74,9 @@ TEST(generate_image, encode_decode_compare_one_step) {
ASSERT_TRUE(result.ok()); ASSERT_TRUE(result.ok());
ASSERT_EQ(result.value(), 0); ASSERT_EQ(result.value(), 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::UInt sapi_width2, sapi_height2;
sapi::v::IntBase<unsigned char *> sapi_image_ptr(0); sapi::v::IntBase<unsigned char *> sapi_image_ptr(0);
@ -88,6 +90,10 @@ TEST(generate_image, encode_decode_compare_one_step) {
ASSERT_EQ(sapi_width2.GetValue(), width); ASSERT_EQ(sapi_width2.GetValue(), width);
ASSERT_EQ(sapi_height2.GetValue(), height); ASSERT_EQ(sapi_height2.GetValue(), height);
// 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
sapi::v::RemotePtr sapi_remote_out_ptr( sapi::v::RemotePtr sapi_remote_out_ptr(
reinterpret_cast<void *>(sapi_image_ptr.GetValue())); reinterpret_cast<void *>(sapi_image_ptr.GetValue()));
sapi::v::Array<unsigned char> sapi_pixels(sapi_width2.GetValue() * sapi::v::Array<unsigned char> sapi_pixels(sapi_width2.GetValue() *
@ -96,8 +102,11 @@ TEST(generate_image, encode_decode_compare_one_step) {
ASSERT_TRUE(sandbox.TransferFromSandboxee(&sapi_pixels).ok()); ASSERT_TRUE(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(); unsigned char *pixels_ptr = sapi_pixels.GetData();
// now, we can compare the values
for (size_t i = 0; i < width * height * 4; ++i) { for (size_t i = 0; i < width * height * 4; ++i) {
ASSERT_EQ(pixels_ptr[i], image[i]); ASSERT_EQ(pixels_ptr[i], image[i]);
} }
@ -107,13 +116,13 @@ TEST(generate_image, encode_decode_compare_one_step) {
// 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 pixels. // memory and then getting the actual pixel values.
TEST(generate_image, encode_decode_compare_two_step) { TEST(generate_image, encode_decode_compare_two_step) {
SapiLodepngSandbox sandbox(images_path); SapiLodepngSandbox sandbox(images_path);
ASSERT_TRUE(sandbox.Init().ok()); ASSERT_TRUE(sandbox.Init().ok());
LodepngApi api(&sandbox); LodepngApi api(&sandbox);
// generate the image // generate the values
unsigned int width = 512, height = 512; unsigned int width = 512, height = 512;
unsigned char *image = (unsigned char *)malloc(width * height * 4); unsigned char *image = (unsigned char *)malloc(width * height * 4);
@ -126,6 +135,7 @@ TEST(generate_image, encode_decode_compare_two_step) {
} }
} }
// encode the image into memory first
sapi::v::Array<unsigned char> sapi_image(image, width * height * 4); sapi::v::Array<unsigned char> sapi_image(image, width * height * 4);
sapi::v::UInt sapi_width(width), sapi_height(height); sapi::v::UInt sapi_width(width), sapi_height(height);
std::string filename = images_path + "/out_generated2.png"; std::string filename = images_path + "/out_generated2.png";
@ -145,7 +155,9 @@ TEST(generate_image, encode_decode_compare_two_step) {
std::cout << "sapi_pngsize = " << sapi_pngsize.GetValue() << std::endl; std::cout << "sapi_pngsize = " << sapi_pngsize.GetValue() << std::endl;
// transfer the array from the sandboxed process // 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::v::RemotePtr sapi_remote_out_ptr(
reinterpret_cast<void *>(sapi_png_ptr.GetValue())); reinterpret_cast<void *>(sapi_png_ptr.GetValue()));
@ -163,12 +175,13 @@ TEST(generate_image, encode_decode_compare_two_step) {
ASSERT_TRUE(result.ok()); ASSERT_TRUE(result.ok());
ASSERT_EQ(result.value(), 0); ASSERT_EQ(result.value(), 0);
// now, decode the image using the 2 steps // 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<unsigned char *> sapi_png_ptr2(0);
sapi::v::ULLong sapi_pngsize2; sapi::v::ULLong sapi_pngsize2;
// load the file in memory
result = result =
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());
@ -178,6 +191,7 @@ TEST(generate_image, encode_decode_compare_two_step) {
ASSERT_EQ(sapi_pngsize.GetValue(), sapi_pngsize2.GetValue()); ASSERT_EQ(sapi_pngsize.GetValue(), sapi_pngsize2.GetValue());
// transfer the png array
sapi::v::RemotePtr sapi_remote_out_ptr2( sapi::v::RemotePtr sapi_remote_out_ptr2(
reinterpret_cast<void *>(sapi_png_ptr2.GetValue())); reinterpret_cast<void *>(sapi_png_ptr2.GetValue()));
sapi::v::Array<unsigned char> sapi_png_array2(sapi_pngsize2.GetValue()); sapi::v::Array<unsigned char> sapi_png_array2(sapi_pngsize2.GetValue());
@ -186,9 +200,9 @@ TEST(generate_image, encode_decode_compare_two_step) {
ASSERT_TRUE(sandbox.TransferFromSandboxee(&sapi_png_array2).ok()); ASSERT_TRUE(sandbox.TransferFromSandboxee(&sapi_png_array2).ok());
// after the file is loaded, decode it // after the file is loaded, decode it so we have access to the values
// directly
sapi::v::IntBase<unsigned char *> sapi_png_ptr3(0); sapi::v::IntBase<unsigned char *> sapi_png_ptr3(0);
// sapi::v::UInt sapi_width2, sapi_height2;
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());
@ -196,9 +210,6 @@ TEST(generate_image, encode_decode_compare_two_step) {
ASSERT_TRUE(result.ok()); ASSERT_TRUE(result.ok());
ASSERT_EQ(result.value(), 0); ASSERT_EQ(result.value(), 0);
std::cout << "w2 = " << sapi_width2.GetValue()
<< " h2 = " << sapi_height2.GetValue() << std::endl;
ASSERT_EQ(sapi_width2.GetValue(), width); ASSERT_EQ(sapi_width2.GetValue(), width);
ASSERT_EQ(sapi_height2.GetValue(), height); ASSERT_EQ(sapi_height2.GetValue(), height);