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
|
|
|
|
//
|
|
|
|
// 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 <filesystem>
|
|
|
|
|
|
|
|
#include "gmock/gmock.h"
|
2020-08-07 19:01:41 +08:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
#include "lodepng_sapi.sapi.h"
|
|
|
|
#include "sandbox.h"
|
2020-08-07 21:50:16 +08:00
|
|
|
#include "sandboxed_api/util/flag.h"
|
2020-08-10 19:05:52 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
2020-08-07 21:50:16 +08:00
|
|
|
|
|
|
|
// defining the flag does not work as intended (always has the default value)
|
|
|
|
// ignore for now
|
2020-08-11 15:15:53 +08:00
|
|
|
// ABSL_FLAG(string, images_path, std::filesystem::current_path().string(),
|
|
|
|
// "path to the folder containing test images");
|
2020-08-07 19:01:41 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2020-08-11 15:15:53 +08:00
|
|
|
// TODO change this into pwd/something else
|
2020-08-10 19:05:52 +08:00
|
|
|
std::string images_path = "/usr/local/google/home/amedar/internship/sandboxed-api/oss-internship-2020/sapi_lodepng/test_images";
|
2020-08-07 19:01:41 +08:00
|
|
|
|
2020-08-10 19:05:52 +08:00
|
|
|
TEST(initSandbox, basic) {
|
|
|
|
SapiLodepngSandbox sandbox(images_path);
|
|
|
|
ASSERT_TRUE(sandbox.Init().ok());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(encode32, generate_and_encode_one_step) {
|
|
|
|
// randomly generate pixels of an image and encode it into a file
|
|
|
|
SapiLodepngSandbox sandbox(images_path);
|
|
|
|
ASSERT_TRUE(sandbox.Init().ok());
|
|
|
|
LodepngApi api(&sandbox);
|
|
|
|
|
|
|
|
|
|
|
|
srand(time(NULL)); // maybe use something else
|
2020-08-11 15:15:53 +08:00
|
|
|
unsigned int width = 512, height = 512;
|
|
|
|
unsigned char *image = (unsigned char*)malloc(width * height * 4);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2020-08-10 19:05:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
sapi::v::Array<unsigned char> image_(image, width * height);
|
|
|
|
sapi::v::UInt width_(width), height_(height);
|
2020-08-11 15:15:53 +08:00
|
|
|
std::string filename = images_path + "/out/generate_and_encode_one_step1.png";
|
2020-08-10 19:05:52 +08:00
|
|
|
sapi::v::ConstCStr filename_(filename.c_str());
|
|
|
|
|
2020-08-11 15:15:53 +08:00
|
|
|
ASSERT_TRUE(sandbox.Allocate(&image_).ok());
|
|
|
|
ASSERT_TRUE(sandbox.TransferToSandboxee(&image_).ok());
|
|
|
|
|
|
|
|
auto res = api.lodepng_encode32_file(filename_.PtrBefore(), image_.PtrBefore(), width_.GetValue(), height_.GetValue()).value();
|
|
|
|
free(image);
|
2020-08-10 19:05:52 +08:00
|
|
|
}
|
|
|
|
|
2020-08-07 21:50:16 +08:00
|
|
|
} // namespace
|