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 18:45:08 +08:00
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2020-08-13 05:09:40 +08:00
|
|
|
|
2020-08-12 18:45:08 +08:00
|
|
|
#include <fstream>
|
|
|
|
#include <memory>
|
2020-08-13 05:09:40 +08:00
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
#include "sandboxed_api/sandbox2/util/fileops.h"
|
|
|
|
|
|
|
|
#include "guetzli_transaction.h"
|
2020-08-12 18:45:08 +08:00
|
|
|
|
|
|
|
namespace guetzli {
|
|
|
|
namespace sandbox {
|
|
|
|
namespace tests {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2020-08-13 05:09:40 +08:00
|
|
|
constexpr const char* kInPngFilename = "bees.png";
|
|
|
|
constexpr const char* kInJpegFilename = "nature.jpg";
|
2020-08-18 04:29:07 +08:00
|
|
|
constexpr const char* kOutJpegFilename = "out_jpeg.jpg";
|
|
|
|
constexpr const char* kOutPngFilename = "out_png.png";
|
2020-08-13 05:09:40 +08:00
|
|
|
constexpr const char* kPngReferenceFilename = "bees_reference.jpg";
|
|
|
|
constexpr const char* kJpegReferenceFIlename = "nature_reference.jpg";
|
2020-08-12 18:45:08 +08:00
|
|
|
|
2020-08-13 05:09:40 +08:00
|
|
|
constexpr int kPngExpectedSize = 38'625;
|
|
|
|
constexpr int kJpegExpectedSize = 10'816;
|
2020-08-12 18:45:08 +08:00
|
|
|
|
2020-08-13 05:09:40 +08:00
|
|
|
constexpr int kDefaultQualityTarget = 95;
|
|
|
|
constexpr int kDefaultMemlimitMb = 6000;
|
2020-08-12 18:45:08 +08:00
|
|
|
|
2020-08-13 05:09:40 +08:00
|
|
|
constexpr const char* kRelativePathToTestdata =
|
2020-08-12 18:45:08 +08:00
|
|
|
"/guetzli_sandboxed/tests/testdata/";
|
|
|
|
|
2020-08-18 04:29:07 +08:00
|
|
|
std::string GetPathToFile(const char* filename) {
|
|
|
|
return absl::StrCat(getenv("TEST_SRCDIR"), kRelativePathToTestdata, filename);
|
2020-08-12 18:45:08 +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-08-18 04:29:07 +08:00
|
|
|
// Helper class to delete file after opening
|
|
|
|
class FileRemover {
|
|
|
|
public:
|
|
|
|
explicit FileRemover(const char* path)
|
|
|
|
: path_(path)
|
|
|
|
, fd_(open(path, O_RDONLY))
|
|
|
|
{}
|
|
|
|
|
|
|
|
~FileRemover() {
|
|
|
|
close(fd_);
|
|
|
|
remove(path_);
|
|
|
|
}
|
|
|
|
|
|
|
|
int get() const { return fd_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
const char* path_;
|
|
|
|
int fd_;
|
|
|
|
};
|
|
|
|
|
2020-08-13 05:09:40 +08:00
|
|
|
} // namespace
|
2020-08-12 18:45:08 +08:00
|
|
|
|
|
|
|
TEST(GuetzliTransactionTest, TestTransactionJpg) {
|
2020-08-18 04:29:07 +08:00
|
|
|
std::string in_path = GetPathToFile(kInJpegFilename);
|
|
|
|
std::string out_path = GetPathToFile(kOutJpegFilename);
|
|
|
|
|
2020-08-12 18:45:08 +08:00
|
|
|
TransactionParams params = {
|
2020-08-18 04:29:07 +08:00
|
|
|
in_path.c_str(),
|
|
|
|
out_path.c_str(),
|
2020-08-12 18:45:08 +08:00
|
|
|
0,
|
2020-08-13 05:09:40 +08:00
|
|
|
kDefaultQualityTarget,
|
|
|
|
kDefaultMemlimitMb
|
2020-08-12 18:45:08 +08:00
|
|
|
};
|
|
|
|
{
|
|
|
|
GuetzliTransaction transaction(std::move(params));
|
|
|
|
auto result = transaction.Run();
|
|
|
|
|
|
|
|
ASSERT_TRUE(result.ok()) << result.ToString();
|
|
|
|
}
|
2020-08-18 04:29:07 +08:00
|
|
|
auto reference_data = ReadFromFile(GetPathToFile(kJpegReferenceFIlename));
|
|
|
|
FileRemover file_remover(out_path.c_str());
|
|
|
|
ASSERT_TRUE(file_remover.get() != -1) << "Error opening output file";
|
|
|
|
auto output_size = lseek(file_remover.get(), 0, SEEK_END);
|
2020-08-12 18:45:08 +08:00
|
|
|
ASSERT_EQ(reference_data.size(), output_size)
|
|
|
|
<< "Different sizes of reference and returned data";
|
2020-08-18 04:29:07 +08:00
|
|
|
ASSERT_EQ(lseek(file_remover.get(), 0, SEEK_SET), 0)
|
2020-08-12 18:45:08 +08:00
|
|
|
<< "Error repositioning out file";
|
|
|
|
|
|
|
|
std::unique_ptr<char[]> buf(new char[output_size]);
|
2020-08-18 04:29:07 +08:00
|
|
|
auto status = read(file_remover.get(), buf.get(), output_size);
|
2020-08-12 18:45:08 +08:00
|
|
|
ASSERT_EQ(status, output_size) << "Error reading data from temp output file";
|
|
|
|
|
|
|
|
ASSERT_TRUE(
|
|
|
|
std::equal(buf.get(), buf.get() + output_size, reference_data.begin()))
|
|
|
|
<< "Returned data doesn't match reference";
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(GuetzliTransactionTest, TestTransactionPng) {
|
2020-08-18 04:29:07 +08:00
|
|
|
std::string in_path = GetPathToFile(kInPngFilename);
|
|
|
|
std::string out_path = GetPathToFile(kOutPngFilename);
|
|
|
|
|
2020-08-12 18:45:08 +08:00
|
|
|
TransactionParams params = {
|
2020-08-18 04:29:07 +08:00
|
|
|
in_path.c_str(),
|
|
|
|
out_path.c_str(),
|
|
|
|
0,
|
2020-08-13 05:09:40 +08:00
|
|
|
kDefaultQualityTarget,
|
|
|
|
kDefaultMemlimitMb
|
2020-08-12 18:45:08 +08:00
|
|
|
};
|
|
|
|
{
|
|
|
|
GuetzliTransaction transaction(std::move(params));
|
|
|
|
auto result = transaction.Run();
|
|
|
|
|
|
|
|
ASSERT_TRUE(result.ok()) << result.ToString();
|
|
|
|
}
|
2020-08-18 04:29:07 +08:00
|
|
|
auto reference_data = ReadFromFile(GetPathToFile(kPngReferenceFilename));
|
|
|
|
FileRemover file_remover(out_path.c_str());
|
|
|
|
ASSERT_TRUE(file_remover.get() != -1) << "Error opening output file";
|
|
|
|
auto output_size = lseek(file_remover.get(), 0, SEEK_END);
|
2020-08-12 18:45:08 +08:00
|
|
|
ASSERT_EQ(reference_data.size(), output_size)
|
|
|
|
<< "Different sizes of reference and returned data";
|
2020-08-18 04:29:07 +08:00
|
|
|
ASSERT_EQ(lseek(file_remover.get(), 0, SEEK_SET), 0)
|
2020-08-12 18:45:08 +08:00
|
|
|
<< "Error repositioning out file";
|
|
|
|
|
|
|
|
std::unique_ptr<char[]> buf(new char[output_size]);
|
2020-08-18 04:29:07 +08:00
|
|
|
auto status = read(file_remover.get(), buf.get(), output_size);
|
2020-08-12 18:45:08 +08:00
|
|
|
ASSERT_EQ(status, output_size) << "Error reading data from temp output file";
|
|
|
|
|
|
|
|
ASSERT_TRUE(
|
|
|
|
std::equal(buf.get(), buf.get() + output_size, reference_data.begin()))
|
|
|
|
<< "Returned data doesn't match refernce";
|
|
|
|
}
|
|
|
|
|
2020-08-13 05:09:40 +08:00
|
|
|
} // namespace tests
|
|
|
|
} // namespace sandbox
|
|
|
|
} // namespace guetzli
|