2020-08-13 05:09:40 +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.
|
2020-08-12 06:44:13 +08:00
|
|
|
|
2020-08-13 05:09:40 +08:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <syscall.h>
|
|
|
|
|
|
|
|
#include <algorithm>
|
2020-08-12 06:44:13 +08:00
|
|
|
#include <fstream>
|
|
|
|
#include <memory>
|
|
|
|
#include <sstream>
|
2020-08-13 05:09:40 +08:00
|
|
|
|
2020-09-17 23:36:07 +08:00
|
|
|
#include "guetzli_sandbox.h" // NOLINT(build/include)
|
2020-08-13 05:09:40 +08:00
|
|
|
#include "gtest/gtest.h"
|
2020-10-27 00:08:06 +08:00
|
|
|
#include "absl/status/statusor.h"
|
2020-08-13 05:09:40 +08:00
|
|
|
#include "sandboxed_api/vars.h"
|
|
|
|
|
2020-08-31 17:19:00 +08:00
|
|
|
namespace guetzli::sandbox::tests {
|
2020-08-12 06:44:13 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2020-08-31 17:19:00 +08:00
|
|
|
constexpr absl::string_view kInPngFilename = "bees.png";
|
|
|
|
constexpr absl::string_view kInJpegFilename = "nature.jpg";
|
|
|
|
constexpr absl::string_view kPngReferenceFilename = "bees_reference.jpg";
|
|
|
|
constexpr absl::string_view kJpegReferenceFIlename = "nature_reference.jpg";
|
2020-08-12 06:44:13 +08:00
|
|
|
|
2020-08-13 05:09:40 +08:00
|
|
|
constexpr int kDefaultQualityTarget = 95;
|
|
|
|
constexpr int kDefaultMemlimitMb = 6000;
|
2020-08-12 06:44:13 +08:00
|
|
|
|
2020-08-31 17:19:00 +08:00
|
|
|
constexpr absl::string_view kRelativePathToTestdata =
|
2020-09-17 23:36:07 +08:00
|
|
|
"/guetzli_sandboxed/testdata/";
|
2020-08-12 06:44:13 +08:00
|
|
|
|
2020-08-31 17:19:00 +08:00
|
|
|
std::string GetPathToInputFile(absl::string_view filename) {
|
2020-08-18 04:29:07 +08:00
|
|
|
return absl::StrCat(getenv("TEST_SRCDIR"), kRelativePathToTestdata, filename);
|
2020-08-12 06:44:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string ReadFromFile(const std::string& filename) {
|
|
|
|
std::ifstream stream(filename, std::ios::binary);
|
|
|
|
|
|
|
|
if (!stream.is_open()) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::stringstream result;
|
|
|
|
result << stream.rdbuf();
|
|
|
|
return result.str();
|
|
|
|
}
|
|
|
|
|
2020-09-17 23:36:07 +08:00
|
|
|
} // namespace
|
2020-08-12 06:44:13 +08:00
|
|
|
|
|
|
|
class GuetzliSapiTest : public ::testing::Test {
|
2020-09-17 23:36:07 +08:00
|
|
|
protected:
|
2020-08-12 06:44:13 +08:00
|
|
|
void SetUp() override {
|
|
|
|
sandbox_ = std::make_unique<GuetzliSapiSandbox>();
|
2020-08-31 17:19:00 +08:00
|
|
|
ASSERT_EQ(sandbox_->Init(), absl::OkStatus());
|
2020-08-12 06:44:13 +08:00
|
|
|
api_ = std::make_unique<GuetzliApi>(sandbox_.get());
|
|
|
|
}
|
2020-09-17 23:36:07 +08:00
|
|
|
|
2020-08-12 06:44:13 +08:00
|
|
|
std::unique_ptr<GuetzliSapiSandbox> sandbox_;
|
|
|
|
std::unique_ptr<GuetzliApi> api_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// This test can take up to few minutes depending on your hardware
|
|
|
|
TEST_F(GuetzliSapiTest, ProcessRGB) {
|
2020-09-17 23:36:07 +08:00
|
|
|
sapi::v::Fd in_fd(open(GetPathToInputFile(kInPngFilename).c_str(), O_RDONLY));
|
2020-08-12 18:45:08 +08:00
|
|
|
ASSERT_TRUE(in_fd.GetValue() != -1) << "Error opening input file";
|
|
|
|
ASSERT_EQ(api_->sandbox()->TransferToSandboxee(&in_fd), absl::OkStatus())
|
2020-09-17 23:36:07 +08:00
|
|
|
<< "Error transfering fd to sandbox";
|
2020-08-12 18:45:08 +08:00
|
|
|
ASSERT_TRUE(in_fd.GetRemoteFd() != -1) << "Error opening remote fd";
|
|
|
|
sapi::v::Struct<ProcessingParams> processing_params;
|
2020-09-17 23:36:07 +08:00
|
|
|
*processing_params.mutable_data() = {
|
|
|
|
in_fd.GetRemoteFd(), 0, kDefaultQualityTarget, kDefaultMemlimitMb};
|
2020-08-12 18:45:08 +08:00
|
|
|
sapi::v::LenVal output(0);
|
2020-09-25 22:43:06 +08:00
|
|
|
absl::StatusOr<bool> processing_result =
|
2020-09-17 23:36:07 +08:00
|
|
|
api_->ProcessRgb(processing_params.PtrBefore(), output.PtrBoth());
|
2020-08-12 18:45:08 +08:00
|
|
|
ASSERT_TRUE(processing_result.value_or(false)) << "Error processing rgb data";
|
2020-09-17 23:36:07 +08:00
|
|
|
std::string reference_data =
|
|
|
|
ReadFromFile(GetPathToInputFile(kPngReferenceFilename));
|
|
|
|
ASSERT_EQ(output.GetDataSize(), reference_data.size())
|
|
|
|
<< "Incorrect result data size";
|
|
|
|
ASSERT_EQ(
|
|
|
|
std::string(output.GetData(), output.GetData() + output.GetDataSize()),
|
|
|
|
reference_data)
|
|
|
|
<< "Processed data doesn't match reference output";
|
2020-08-12 06:44:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// This test can take up to few minutes depending on your hardware
|
|
|
|
TEST_F(GuetzliSapiTest, ProcessJpeg) {
|
2020-09-17 23:36:07 +08:00
|
|
|
sapi::v::Fd in_fd(
|
|
|
|
open(GetPathToInputFile(kInJpegFilename).c_str(), O_RDONLY));
|
2020-08-12 18:45:08 +08:00
|
|
|
ASSERT_TRUE(in_fd.GetValue() != -1) << "Error opening input file";
|
|
|
|
ASSERT_EQ(api_->sandbox()->TransferToSandboxee(&in_fd), absl::OkStatus())
|
2020-09-17 23:36:07 +08:00
|
|
|
<< "Error transfering fd to sandbox";
|
2020-08-12 18:45:08 +08:00
|
|
|
ASSERT_TRUE(in_fd.GetRemoteFd() != -1) << "Error opening remote fd";
|
|
|
|
sapi::v::Struct<ProcessingParams> processing_params;
|
2020-09-17 23:36:07 +08:00
|
|
|
*processing_params.mutable_data() = {
|
|
|
|
in_fd.GetRemoteFd(), 0, kDefaultQualityTarget, kDefaultMemlimitMb};
|
2020-08-12 18:45:08 +08:00
|
|
|
sapi::v::LenVal output(0);
|
2020-09-25 22:43:06 +08:00
|
|
|
absl::StatusOr<bool> processing_result =
|
2020-09-17 23:36:07 +08:00
|
|
|
api_->ProcessJpeg(processing_params.PtrBefore(), output.PtrBoth());
|
2020-08-12 18:45:08 +08:00
|
|
|
ASSERT_TRUE(processing_result.value_or(false)) << "Error processing jpg data";
|
2020-09-17 23:36:07 +08:00
|
|
|
std::string reference_data =
|
|
|
|
ReadFromFile(GetPathToInputFile(kJpegReferenceFIlename));
|
|
|
|
ASSERT_EQ(output.GetDataSize(), reference_data.size())
|
|
|
|
<< "Incorrect result data size";
|
|
|
|
ASSERT_EQ(
|
|
|
|
std::string(output.GetData(), output.GetData() + output.GetDataSize()),
|
|
|
|
reference_data)
|
|
|
|
<< "Processed data doesn't match reference output";
|
2020-08-12 06:44:13 +08:00
|
|
|
}
|
|
|
|
|
2020-08-31 17:19:00 +08:00
|
|
|
} // namespace guetzli::sandbox::tests
|