2020-08-07 21:50:16 +08:00
|
|
|
// 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
|
|
|
|
//
|
2022-01-28 17:38:27 +08:00
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
2020-08-07 21:50:16 +08:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2020-09-30 17:56:32 +08:00
|
|
|
#include "helpers.h" // NOLINT(build/include)
|
|
|
|
#include "sandbox.h" // NOLINT(build/include)
|
2020-08-07 19:01:41 +08:00
|
|
|
#include "gtest/gtest.h"
|
2020-08-13 20:30:06 +08:00
|
|
|
#include "sandboxed_api/util/status_matchers.h"
|
2020-08-11 22:53:24 +08:00
|
|
|
|
2020-08-28 03:53:06 +08:00
|
|
|
using ::sapi::IsOk;
|
|
|
|
using ::testing::Eq;
|
|
|
|
using ::testing::IsTrue;
|
2020-08-28 21:29:12 +08:00
|
|
|
using ::testing::NotNull;
|
2020-08-13 22:02:05 +08:00
|
|
|
|
2020-08-07 19:01:41 +08:00
|
|
|
namespace {
|
|
|
|
|
2020-08-28 03:53:06 +08:00
|
|
|
TEST(HelpersTest, CreateTempDirAtCWD) {
|
|
|
|
const std::string images_path = CreateTempDirAtCWD();
|
2022-06-10 19:23:39 +08:00
|
|
|
ASSERT_THAT(sapi::file_util::fileops::Exists(images_path, false), IsTrue())
|
2020-08-30 02:54:08 +08:00
|
|
|
<< "Temporary directory does not exist";
|
2020-08-28 03:53:06 +08:00
|
|
|
|
2022-06-10 19:23:39 +08:00
|
|
|
EXPECT_THAT(sapi::file_util::fileops::DeleteRecursively(images_path),
|
2020-08-30 02:54:08 +08:00
|
|
|
IsTrue())
|
|
|
|
<< "Temporary directory could not be deleted";
|
2020-08-28 03:53:06 +08:00
|
|
|
}
|
2020-08-07 19:01:41 +08:00
|
|
|
|
2020-08-28 03:53:06 +08:00
|
|
|
TEST(HelpersTest, GenerateValues) {
|
2020-08-28 21:29:12 +08:00
|
|
|
EXPECT_THAT(GenerateValues().size(), Eq(kImgLen));
|
2020-08-28 03:53:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(LodePngTest, Init) {
|
|
|
|
const std::string images_path = CreateTempDirAtCWD();
|
2022-06-10 19:23:39 +08:00
|
|
|
ASSERT_THAT(sapi::file_util::fileops::Exists(images_path, false), IsTrue())
|
2020-08-30 02:54:08 +08:00
|
|
|
<< "Temporary directory does not exist";
|
|
|
|
|
2020-08-11 22:53:24 +08:00
|
|
|
SapiLodepngSandbox sandbox(images_path);
|
2020-08-13 23:58:47 +08:00
|
|
|
ASSERT_THAT(sandbox.Init(), IsOk()) << "Error during sandbox init";
|
2020-08-28 03:53:06 +08:00
|
|
|
|
2022-06-10 19:23:39 +08:00
|
|
|
EXPECT_THAT(sapi::file_util::fileops::DeleteRecursively(images_path),
|
2020-08-30 02:54:08 +08:00
|
|
|
IsTrue())
|
|
|
|
<< "Temporary directory could not be deleted";
|
2020-08-10 19:05:52 +08:00
|
|
|
}
|
|
|
|
|
2020-08-28 21:29:12 +08:00
|
|
|
// Generate an image, encode it, decode it and compare the pixels with the
|
|
|
|
// initial values.
|
2020-08-28 03:53:06 +08:00
|
|
|
TEST(LodePngTest, EncodeDecodeOneStep) {
|
|
|
|
const std::string images_path = CreateTempDirAtCWD();
|
2022-06-10 19:23:39 +08:00
|
|
|
ASSERT_THAT(sapi::file_util::fileops::Exists(images_path, false), IsTrue())
|
2020-08-30 02:54:08 +08:00
|
|
|
<< "Temporary directory does not exist";
|
2020-08-28 03:53:06 +08:00
|
|
|
|
2020-08-11 22:53:24 +08:00
|
|
|
SapiLodepngSandbox sandbox(images_path);
|
2020-08-30 02:54:08 +08:00
|
|
|
ASSERT_THAT(sandbox.Init(), IsOk()) << "Error during sandbox initialization";
|
2020-08-11 22:53:24 +08:00
|
|
|
LodepngApi api(&sandbox);
|
2020-08-12 19:20:02 +08:00
|
|
|
|
2020-09-14 23:23:29 +08:00
|
|
|
std::vector<uint8_t> image = GenerateValues();
|
2020-08-11 22:53:24 +08:00
|
|
|
|
2020-09-08 22:21:57 +08:00
|
|
|
sapi::v::Array<uint8_t> sapi_image(kImgLen);
|
|
|
|
EXPECT_THAT(std::copy(image.begin(), image.end(), sapi_image.GetData()),
|
|
|
|
IsTrue())
|
2020-09-30 06:58:35 +08:00
|
|
|
<< "Could not copy values";
|
2020-09-08 22:21:57 +08:00
|
|
|
|
2020-08-21 00:37:45 +08:00
|
|
|
sapi::v::ConstCStr sapi_filename("/output/out_generated1.png");
|
2020-08-11 22:53:24 +08:00
|
|
|
|
2020-08-13 20:30:06 +08:00
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(
|
|
|
|
unsigned int result,
|
|
|
|
api.lodepng_encode32_file(sapi_filename.PtrBefore(),
|
2020-08-28 03:53:06 +08:00
|
|
|
sapi_image.PtrBefore(), kWidth, kHeight));
|
2020-08-11 22:53:24 +08:00
|
|
|
|
2020-08-30 02:54:08 +08:00
|
|
|
ASSERT_THAT(result, Eq(0)) << "Unexpected result from encode32_file call";
|
2020-08-13 22:59:30 +08:00
|
|
|
|
2020-09-14 23:23:29 +08:00
|
|
|
sapi::v::UInt sapi_width, sapi_height;
|
2020-09-24 01:12:38 +08:00
|
|
|
sapi::v::IntBase<uint8_t*> sapi_image_ptr(0);
|
2020-08-11 22:53:24 +08:00
|
|
|
|
2021-01-22 22:01:05 +08:00
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(
|
|
|
|
result, api.lodepng_decode32_file(
|
|
|
|
sapi_image_ptr.PtrBoth(), sapi_width.PtrBoth(),
|
|
|
|
sapi_height.PtrBoth(), sapi_filename.PtrBefore()));
|
2020-08-11 22:53:24 +08:00
|
|
|
|
2020-08-30 02:54:08 +08:00
|
|
|
ASSERT_THAT(result, Eq(0)) << "Unexpected result from decode32_file call";
|
2020-08-10 19:05:52 +08:00
|
|
|
|
2020-09-14 23:23:29 +08:00
|
|
|
EXPECT_THAT(sapi_width.GetValue(), Eq(kWidth)) << "Widths differ";
|
|
|
|
EXPECT_THAT(sapi_height.GetValue(), Eq(kHeight)) << "Heights differ";
|
2020-08-10 19:05:52 +08:00
|
|
|
|
2020-08-28 03:53:06 +08:00
|
|
|
sapi::v::Array<uint8_t> sapi_pixels(kImgLen);
|
|
|
|
sapi_pixels.SetRemote(sapi_image_ptr.GetValue());
|
2020-08-11 15:15:53 +08:00
|
|
|
|
2020-08-13 23:58:47 +08:00
|
|
|
ASSERT_THAT(sandbox.TransferFromSandboxee(&sapi_pixels), IsOk())
|
|
|
|
<< "Error during transfer from sandboxee";
|
2020-08-11 22:53:24 +08:00
|
|
|
|
2020-08-28 21:29:12 +08:00
|
|
|
EXPECT_THAT(absl::equal(image.begin(), image.end(), sapi_pixels.GetData(),
|
|
|
|
sapi_pixels.GetData() + kImgLen),
|
2020-08-28 03:53:06 +08:00
|
|
|
IsTrue())
|
2020-08-30 02:54:08 +08:00
|
|
|
<< "Values differ";
|
2020-08-28 03:53:06 +08:00
|
|
|
|
2022-06-10 19:23:39 +08:00
|
|
|
EXPECT_THAT(sandbox.rpc_channel()->Free(sapi_image_ptr.GetValue()), IsOk())
|
2020-09-08 22:21:57 +08:00
|
|
|
<< "Could not free memory inside sandboxed process";
|
2020-08-28 21:29:12 +08:00
|
|
|
|
2022-06-10 19:23:39 +08:00
|
|
|
EXPECT_THAT(sapi::file_util::fileops::DeleteRecursively(images_path),
|
2020-08-30 02:54:08 +08:00
|
|
|
IsTrue())
|
|
|
|
<< "Temporary directory could not be deleted";
|
2020-08-11 22:53:24 +08:00
|
|
|
}
|
|
|
|
|
2020-08-28 21:29:12 +08:00
|
|
|
// Similar to the previous test, only that we use encoding by saving the data in
|
2020-08-11 22:53:24 +08:00
|
|
|
// memory and then writing it to the file and decoding by first decoding in
|
2020-08-12 19:20:02 +08:00
|
|
|
// memory and then getting the actual pixel values.
|
2020-08-28 03:53:06 +08:00
|
|
|
TEST(LodePngTest, EncodeDecodeTwoSteps) {
|
|
|
|
const std::string images_path = CreateTempDirAtCWD();
|
2022-06-10 19:23:39 +08:00
|
|
|
ASSERT_THAT(sapi::file_util::fileops::Exists(images_path, false), IsTrue())
|
2020-08-30 02:54:08 +08:00
|
|
|
<< "Temporary directory does not exist";
|
2020-08-28 03:53:06 +08:00
|
|
|
|
2020-08-11 22:53:24 +08:00
|
|
|
SapiLodepngSandbox sandbox(images_path);
|
2020-08-13 23:58:47 +08:00
|
|
|
ASSERT_THAT(sandbox.Init(), IsOk()) << "Error during sandbox init";
|
2020-08-11 22:53:24 +08:00
|
|
|
LodepngApi api(&sandbox);
|
|
|
|
|
2020-09-14 23:23:29 +08:00
|
|
|
std::vector<uint8_t> image = GenerateValues();
|
2020-08-11 22:53:24 +08:00
|
|
|
|
2020-09-08 22:21:57 +08:00
|
|
|
sapi::v::Array<uint8_t> sapi_image(kImgLen);
|
|
|
|
EXPECT_THAT(std::copy(image.begin(), image.end(), sapi_image.GetData()),
|
|
|
|
IsTrue())
|
2020-09-30 06:58:35 +08:00
|
|
|
<< "Could not copy values";
|
2020-09-08 22:21:57 +08:00
|
|
|
|
2020-08-21 00:37:45 +08:00
|
|
|
sapi::v::ConstCStr sapi_filename("/output/out_generated2.png");
|
2020-08-11 22:53:24 +08:00
|
|
|
|
|
|
|
sapi::v::ULLong sapi_pngsize;
|
2020-09-24 01:12:38 +08:00
|
|
|
sapi::v::IntBase<uint8_t*> sapi_png_ptr(0);
|
2020-08-11 22:53:24 +08:00
|
|
|
|
2020-08-13 20:30:06 +08:00
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(
|
|
|
|
unsigned int result,
|
|
|
|
api.lodepng_encode32(sapi_png_ptr.PtrBoth(), sapi_pngsize.PtrBoth(),
|
2020-08-28 03:53:06 +08:00
|
|
|
sapi_image.PtrBefore(), kWidth, kHeight));
|
2020-08-13 20:30:06 +08:00
|
|
|
|
2020-08-30 02:54:08 +08:00
|
|
|
ASSERT_THAT(result, Eq(0)) << "Unexpected result from encode32 call";
|
2020-08-10 19:05:52 +08:00
|
|
|
|
2020-08-28 03:53:06 +08:00
|
|
|
sapi::v::Array<uint8_t> sapi_png_array(sapi_pngsize.GetValue());
|
|
|
|
sapi_png_array.SetRemote(sapi_png_ptr.GetValue());
|
2020-08-11 22:53:24 +08:00
|
|
|
|
2020-08-13 23:58:47 +08:00
|
|
|
ASSERT_THAT(sandbox.TransferFromSandboxee(&sapi_png_array), IsOk())
|
|
|
|
<< "Error during transfer from sandboxee";
|
2020-08-11 22:53:24 +08:00
|
|
|
|
2020-08-13 20:30:06 +08:00
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(
|
|
|
|
result,
|
2020-08-11 22:53:24 +08:00
|
|
|
api.lodepng_save_file(sapi_png_array.PtrBefore(), sapi_pngsize.GetValue(),
|
2020-08-13 20:30:06 +08:00
|
|
|
sapi_filename.PtrBefore()));
|
2020-08-11 22:53:24 +08:00
|
|
|
|
2020-08-30 02:54:08 +08:00
|
|
|
ASSERT_THAT(result, Eq(0)) << "Unexpected result from save_file call";
|
2020-08-11 22:53:24 +08:00
|
|
|
|
2020-09-14 23:23:29 +08:00
|
|
|
sapi::v::UInt sapi_width, sapi_height;
|
2020-09-24 01:12:38 +08:00
|
|
|
sapi::v::IntBase<uint8_t*> sapi_png_ptr2(0);
|
2020-08-11 22:53:24 +08:00
|
|
|
sapi::v::ULLong sapi_pngsize2;
|
|
|
|
|
2020-08-13 20:30:06 +08:00
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(
|
|
|
|
result,
|
2020-08-11 22:53:24 +08:00
|
|
|
api.lodepng_load_file(sapi_png_ptr2.PtrBoth(), sapi_pngsize2.PtrBoth(),
|
2020-08-13 20:30:06 +08:00
|
|
|
sapi_filename.PtrBefore()));
|
2020-08-11 22:53:24 +08:00
|
|
|
|
2020-08-30 02:54:08 +08:00
|
|
|
ASSERT_THAT(result, Eq(0)) << "Unexpected result from load_file call";
|
2020-08-11 22:53:24 +08:00
|
|
|
|
2020-08-13 23:58:47 +08:00
|
|
|
EXPECT_THAT(sapi_pngsize.GetValue(), Eq(sapi_pngsize2.GetValue()))
|
|
|
|
<< "Png sizes differ";
|
2020-08-11 22:53:24 +08:00
|
|
|
|
2020-08-28 03:53:06 +08:00
|
|
|
sapi::v::Array<uint8_t> sapi_png_array2(sapi_pngsize2.GetValue());
|
|
|
|
sapi_png_array2.SetRemote(sapi_png_ptr2.GetValue());
|
2020-08-11 22:53:24 +08:00
|
|
|
|
2020-08-13 23:58:47 +08:00
|
|
|
ASSERT_THAT(sandbox.TransferFromSandboxee(&sapi_png_array2), IsOk())
|
|
|
|
<< "Error during transfer from sandboxee";
|
2020-08-11 22:53:24 +08:00
|
|
|
|
2020-09-24 01:12:38 +08:00
|
|
|
sapi::v::IntBase<uint8_t*> sapi_png_ptr3(0);
|
2020-08-13 20:30:06 +08:00
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(
|
|
|
|
result,
|
2020-09-14 23:23:29 +08:00
|
|
|
api.lodepng_decode32(sapi_png_ptr3.PtrBoth(), sapi_width.PtrBoth(),
|
|
|
|
sapi_height.PtrBoth(), sapi_png_array2.PtrBefore(),
|
2020-08-13 20:30:06 +08:00
|
|
|
sapi_pngsize2.GetValue()));
|
2020-08-11 22:53:24 +08:00
|
|
|
|
2020-08-30 02:54:08 +08:00
|
|
|
ASSERT_THAT(result, Eq(0)) << "Unexpected result from decode32 call";
|
2020-08-11 22:53:24 +08:00
|
|
|
|
2020-09-14 23:23:29 +08:00
|
|
|
EXPECT_THAT(sapi_width.GetValue(), Eq(kWidth)) << "Widths differ";
|
|
|
|
EXPECT_THAT(sapi_height.GetValue(), Eq(kHeight)) << "Heights differ";
|
2020-08-11 22:53:24 +08:00
|
|
|
|
2020-08-28 03:53:06 +08:00
|
|
|
sapi::v::Array<uint8_t> sapi_pixels(kImgLen);
|
|
|
|
sapi_pixels.SetRemote(sapi_png_ptr3.GetValue());
|
2020-08-11 22:53:24 +08:00
|
|
|
|
2020-08-13 23:58:47 +08:00
|
|
|
ASSERT_THAT(sandbox.TransferFromSandboxee(&sapi_pixels), IsOk())
|
|
|
|
<< "Error during transfer from sandboxee";
|
2020-08-11 22:53:24 +08:00
|
|
|
|
2020-08-28 21:29:12 +08:00
|
|
|
EXPECT_THAT(absl::equal(image.begin(), image.end(), sapi_pixels.GetData(),
|
|
|
|
sapi_pixels.GetData() + kImgLen),
|
2020-08-28 03:53:06 +08:00
|
|
|
IsTrue())
|
2020-08-30 02:54:08 +08:00
|
|
|
<< "Values differ";
|
2020-08-11 22:53:24 +08:00
|
|
|
|
2022-06-10 19:23:39 +08:00
|
|
|
EXPECT_THAT(sandbox.rpc_channel()->Free(sapi_png_ptr.GetValue()), IsOk());
|
|
|
|
EXPECT_THAT(sandbox.rpc_channel()->Free(sapi_png_ptr2.GetValue()), IsOk());
|
|
|
|
EXPECT_THAT(sandbox.rpc_channel()->Free(sapi_png_ptr3.GetValue()), IsOk());
|
2020-08-28 21:29:12 +08:00
|
|
|
|
2022-06-10 19:23:39 +08:00
|
|
|
EXPECT_THAT(sapi::file_util::fileops::DeleteRecursively(images_path),
|
2020-08-30 02:54:08 +08:00
|
|
|
IsTrue())
|
|
|
|
<< "Temporary directory could not be deleted";
|
2020-08-10 19:05:52 +08:00
|
|
|
}
|
|
|
|
|
2020-08-28 21:29:12 +08:00
|
|
|
} // namespace
|