modified tests to use EXPECT_THAT checks

This commit is contained in:
Andrei Medar 2020-08-13 14:02:05 +00:00
parent c556f4358f
commit ce5b7a3789
2 changed files with 36 additions and 41 deletions

View File

@ -258,8 +258,7 @@ 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(
reinterpret_cast<void *>(sapi_image_ptr.GetValue()));
sapi::v::RemotePtr sapi_remote_out_ptr(sapi_image_ptr.GetValue());
sapi::v::Array<unsigned char> sapi_pixels(sapi_width2.GetValue() *
sapi_height2.GetValue() * 4);
sapi_pixels.SetRemote(sapi_remote_out_ptr.GetValue());
@ -315,8 +314,7 @@ void generate_two_steps(SapiLodepngSandbox &sandbox, LodepngApi &api) {
// inside the sandboxed process so we need to transfer it to this
// process
sapi::v::RemotePtr sapi_remote_out_ptr(
reinterpret_cast<void *>(sapi_png_ptr.GetValue()));
sapi::v::RemotePtr sapi_remote_out_ptr(sapi_png_ptr.GetValue());
sapi::v::Array<unsigned char> sapi_png_array(sapi_pngsize.GetValue());
sapi_png_array.SetRemote(sapi_remote_out_ptr.GetValue());
@ -347,8 +345,7 @@ void generate_two_steps(SapiLodepngSandbox &sandbox, LodepngApi &api) {
assert(sapi_pngsize.GetValue() == sapi_pngsize2.GetValue());
// transfer the png array
sapi::v::RemotePtr sapi_remote_out_ptr2(
reinterpret_cast<void *>(sapi_png_ptr2.GetValue()));
sapi::v::RemotePtr sapi_remote_out_ptr2(sapi_png_ptr2.GetValue());
sapi::v::Array<unsigned char> sapi_png_array2(sapi_pngsize2.GetValue());
sapi_png_array2.SetRemote(sapi_remote_out_ptr2.GetValue());
@ -369,8 +366,7 @@ void generate_two_steps(SapiLodepngSandbox &sandbox, LodepngApi &api) {
assert(sapi_height2.GetValue() == height);
// transfer the pixels so they can be used
sapi::v::RemotePtr sapi_remote_out_ptr3(
reinterpret_cast<void *>(sapi_png_ptr3.GetValue()));
sapi::v::RemotePtr sapi_remote_out_ptr3(sapi_png_ptr3.GetValue());
sapi::v::Array<unsigned char> sapi_pixels(sapi_width2.GetValue() *
sapi_height2.GetValue() * 4);

View File

@ -23,6 +23,12 @@
#include "sandboxed_api/util/flag.h"
#include "sandboxed_api/util/status_matchers.h"
using testing::NotNull;
using testing::Eq;
using sapi::IsOk;
namespace {
// use the current path
@ -30,21 +36,21 @@ std::string images_path = std::filesystem::current_path().string();
TEST(initSandbox, basic) {
SapiLodepngSandbox sandbox(images_path);
ASSERT_THAT(sandbox.Init(), sapi::IsOk());
ASSERT_THAT(sandbox.Init(), IsOk());
}
// generate an image, encode it, decode it and compare the pixels with the
// initial values
TEST(generate_image, encode_decode_compare_one_step) {
SapiLodepngSandbox sandbox(images_path);
ASSERT_THAT(sandbox.Init(), sapi::IsOk());
ASSERT_THAT(sandbox.Init(), IsOk());
LodepngApi api(&sandbox);
// generate the values
unsigned int width = 512, height = 512;
unsigned char *image = (unsigned char *)malloc(width * height * 4);
ASSERT_TRUE(image);
ASSERT_THAT(image, NotNull());
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
@ -67,7 +73,7 @@ TEST(generate_image, encode_decode_compare_one_step) {
sapi_image.PtrBefore(), sapi_width.GetValue(),
sapi_height.GetValue()));
ASSERT_THAT(result, testing::Eq(0));
ASSERT_THAT(result, Eq(0));
// after the image has been encoded, decode it to check that the
// pixel values are the same
@ -79,10 +85,10 @@ TEST(generate_image, encode_decode_compare_one_step) {
sapi_image_ptr.PtrBoth(), sapi_width2.PtrBoth(),
sapi_height2.PtrBoth(), sapi_filename.PtrBefore()));
ASSERT_THAT(result, testing::Eq(0));
ASSERT_THAT(result, Eq(0));
ASSERT_THAT(sapi_width2.GetValue(), testing::Eq(width));
ASSERT_THAT(sapi_height2.GetValue(), testing::Eq(height));
EXPECT_THAT(sapi_width2.GetValue(), Eq(width));
EXPECT_THAT(sapi_height2.GetValue(), Eq(height));
// the pixels have been allocated inside the sandboxed process
// memory, so we need to transfer them to this process.
@ -94,13 +100,12 @@ 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(
reinterpret_cast<void *>(sapi_image_ptr.GetValue()));
sapi::v::RemotePtr sapi_remote_out_ptr(sapi_image_ptr.GetValue());
sapi::v::Array<unsigned char> sapi_pixels(sapi_width2.GetValue() *
sapi_height2.GetValue() * 4);
sapi_pixels.SetRemote(sapi_remote_out_ptr.GetValue());
ASSERT_THAT(sandbox.TransferFromSandboxee(&sapi_pixels), sapi::IsOk());
ASSERT_THAT(sandbox.TransferFromSandboxee(&sapi_pixels), IsOk());
// after the memory has been transferred, we can access it
// using the GetData function
@ -108,7 +113,7 @@ TEST(generate_image, encode_decode_compare_one_step) {
// now, we can compare the values
for (size_t i = 0; i < width * height * 4; ++i) {
ASSERT_THAT(pixels_ptr[i], testing::Eq(image[i]));
EXPECT_THAT(pixels_ptr[i], Eq(image[i]));
}
free(image);
@ -119,14 +124,14 @@ TEST(generate_image, encode_decode_compare_one_step) {
// memory and then getting the actual pixel values.
TEST(generate_image, encode_decode_compare_two_steps) {
SapiLodepngSandbox sandbox(images_path);
ASSERT_THAT(sandbox.Init(), sapi::IsOk());
ASSERT_THAT(sandbox.Init(), IsOk());
LodepngApi api(&sandbox);
// generate the values
unsigned int width = 512, height = 512;
unsigned char *image = (unsigned char *)malloc(width * height * 4);
ASSERT_THAT(image, testing::NotNull());
ASSERT_THAT(image, NotNull());
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
@ -153,22 +158,18 @@ TEST(generate_image, encode_decode_compare_two_steps) {
sapi_image.PtrBefore(), sapi_width.GetValue(),
sapi_height.GetValue()));
ASSERT_THAT(result, testing::Eq(0));
// ASSERT_TRUE(result.ok());
// ASSERT_EQ(result.value(), 0);
ASSERT_THAT(result, Eq(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(
reinterpret_cast<void *>(sapi_png_ptr.GetValue()));
sapi::v::RemotePtr sapi_remote_out_ptr(sapi_png_ptr.GetValue());
sapi::v::Array<unsigned char> sapi_png_array(sapi_pngsize.GetValue());
sapi_png_array.SetRemote(sapi_remote_out_ptr.GetValue());
ASSERT_THAT(sandbox.TransferFromSandboxee(&sapi_png_array), sapi::IsOk());
ASSERT_THAT(sandbox.TransferFromSandboxee(&sapi_png_array), IsOk());
// write the image into the file (from memory)
SAPI_ASSERT_OK_AND_ASSIGN(
@ -176,7 +177,7 @@ TEST(generate_image, encode_decode_compare_two_steps) {
api.lodepng_save_file(sapi_png_array.PtrBefore(), sapi_pngsize.GetValue(),
sapi_filename.PtrBefore()));
ASSERT_THAT(result, testing::Eq(0));
ASSERT_THAT(result, Eq(0));
// now, decode the image using the 2 steps in order to compare the values
sapi::v::UInt sapi_width2, sapi_height2;
@ -189,18 +190,17 @@ TEST(generate_image, encode_decode_compare_two_steps) {
api.lodepng_load_file(sapi_png_ptr2.PtrBoth(), sapi_pngsize2.PtrBoth(),
sapi_filename.PtrBefore()));
ASSERT_THAT(result, testing::Eq(0));
ASSERT_THAT(result, Eq(0));
ASSERT_THAT(sapi_pngsize.GetValue(), testing::Eq(sapi_pngsize2.GetValue()));
EXPECT_THAT(sapi_pngsize.GetValue(), Eq(sapi_pngsize2.GetValue()));
// transfer the png array
sapi::v::RemotePtr sapi_remote_out_ptr2(
reinterpret_cast<void *>(sapi_png_ptr2.GetValue()));
sapi::v::RemotePtr sapi_remote_out_ptr2(sapi_png_ptr2.GetValue());
sapi::v::Array<unsigned char> sapi_png_array2(sapi_pngsize2.GetValue());
sapi_png_array2.SetRemote(sapi_remote_out_ptr2.GetValue());
ASSERT_THAT(sandbox.TransferFromSandboxee(&sapi_png_array2), sapi::IsOk());
ASSERT_THAT(sandbox.TransferFromSandboxee(&sapi_png_array2), IsOk());
// after the file is loaded, decode it so we have access to the values
// directly
@ -211,26 +211,25 @@ TEST(generate_image, encode_decode_compare_two_steps) {
sapi_height2.PtrBoth(), sapi_png_array2.PtrBefore(),
sapi_pngsize2.GetValue()));
ASSERT_THAT(result, testing::Eq(0));
ASSERT_THAT(result, Eq(0));
ASSERT_THAT(sapi_width2.GetValue(), testing::Eq(width));
ASSERT_THAT(sapi_height2.GetValue(), testing::Eq(height));
EXPECT_THAT(sapi_width2.GetValue(), Eq(width));
EXPECT_THAT(sapi_height2.GetValue(), Eq(height));
// transfer the pixels so they can be used
sapi::v::RemotePtr sapi_remote_out_ptr3(
reinterpret_cast<void *>(sapi_png_ptr3.GetValue()));
sapi::v::RemotePtr sapi_remote_out_ptr3(sapi_png_ptr3.GetValue());
sapi::v::Array<unsigned char> sapi_pixels(sapi_width2.GetValue() *
sapi_height2.GetValue() * 4);
sapi_pixels.SetRemote(sapi_remote_out_ptr3.GetValue());
ASSERT_THAT(sandbox.TransferFromSandboxee(&sapi_pixels), sapi::IsOk());
ASSERT_THAT(sandbox.TransferFromSandboxee(&sapi_pixels), IsOk());
unsigned char *pixels_ptr = sapi_pixels.GetData();
// compare values
for (size_t i = 0; i < width * height * 4; ++i) {
ASSERT_THAT(pixels_ptr[i], testing::Eq(image[i]));
EXPECT_THAT(pixels_ptr[i], Eq(image[i]));
}
free(image);