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
|
|
|
|
2020-09-17 23:36:07 +08:00
|
|
|
#include "guetzli_transaction.h" // NOLINT(build/include)
|
|
|
|
|
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"
|
|
|
|
|
2020-08-31 17:19:00 +08:00
|
|
|
namespace guetzli::sandbox::tests {
|
2020-08-12 18:45:08 +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 kOutJpegFilename = "out_jpeg.jpg";
|
|
|
|
constexpr absl::string_view kOutPngFilename = "out_png.png";
|
|
|
|
constexpr absl::string_view kPngReferenceFilename = "bees_reference.jpg";
|
|
|
|
constexpr absl::string_view 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-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 18:45:08 +08:00
|
|
|
|
2020-08-31 17:19:00 +08:00
|
|
|
std::string GetPathToFile(absl::string_view filename) {
|
2020-08-18 04:29:07 +08:00
|
|
|
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:
|
2020-09-17 23:36:07 +08:00
|
|
|
explicit FileRemover(const char* path)
|
|
|
|
: path_(path), fd_(open(path, O_RDONLY)) {}
|
2020-08-18 04:29:07 +08:00
|
|
|
|
|
|
|
~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-09-17 23:36:07 +08:00
|
|
|
TransactionParams params = {in_path.c_str(), out_path.c_str(), 0,
|
|
|
|
kDefaultQualityTarget, kDefaultMemlimitMb};
|
2020-08-12 18:45:08 +08:00
|
|
|
{
|
|
|
|
GuetzliTransaction transaction(std::move(params));
|
2020-08-31 17:19:00 +08:00
|
|
|
absl::Status result = transaction.Run();
|
2020-08-12 18:45:08 +08:00
|
|
|
|
|
|
|
ASSERT_TRUE(result.ok()) << result.ToString();
|
|
|
|
}
|
2020-09-17 23:36:07 +08:00
|
|
|
std::string reference_data =
|
|
|
|
ReadFromFile(GetPathToFile(kJpegReferenceFIlename));
|
2020-08-18 04:29:07 +08:00
|
|
|
FileRemover file_remover(out_path.c_str());
|
|
|
|
ASSERT_TRUE(file_remover.get() != -1) << "Error opening output file";
|
2020-09-17 23:36:07 +08:00
|
|
|
off_t output_size = lseek(file_remover.get(), 0, SEEK_END);
|
|
|
|
ASSERT_EQ(reference_data.size(), output_size)
|
|
|
|
<< "Different sizes of reference and returned data";
|
|
|
|
ASSERT_EQ(lseek(file_remover.get(), 0, SEEK_SET), 0)
|
|
|
|
<< "Error repositioning out file";
|
|
|
|
|
2020-08-31 17:19:00 +08:00
|
|
|
std::string output;
|
|
|
|
output.resize(output_size);
|
|
|
|
ssize_t status = read(file_remover.get(), output.data(), output_size);
|
2020-08-12 18:45:08 +08:00
|
|
|
ASSERT_EQ(status, output_size) << "Error reading data from temp output file";
|
|
|
|
|
2020-08-31 17:19:00 +08:00
|
|
|
ASSERT_EQ(output, reference_data) << "Returned data doesn't match reference";
|
2020-08-12 18:45:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(GuetzliTransactionTest, TestTransactionPng) {
|
2020-08-18 04:29:07 +08:00
|
|
|
std::string in_path = GetPathToFile(kInPngFilename);
|
|
|
|
std::string out_path = GetPathToFile(kOutPngFilename);
|
|
|
|
|
2020-09-17 23:36:07 +08:00
|
|
|
TransactionParams params = {in_path.c_str(), out_path.c_str(), 0,
|
|
|
|
kDefaultQualityTarget, kDefaultMemlimitMb};
|
2020-08-12 18:45:08 +08:00
|
|
|
{
|
|
|
|
GuetzliTransaction transaction(std::move(params));
|
2020-08-31 17:19:00 +08:00
|
|
|
absl::Status result = transaction.Run();
|
2020-08-12 18:45:08 +08:00
|
|
|
|
|
|
|
ASSERT_TRUE(result.ok()) << result.ToString();
|
|
|
|
}
|
2020-09-17 23:36:07 +08:00
|
|
|
std::string reference_data =
|
|
|
|
ReadFromFile(GetPathToFile(kPngReferenceFilename));
|
2020-08-18 04:29:07 +08:00
|
|
|
FileRemover file_remover(out_path.c_str());
|
|
|
|
ASSERT_TRUE(file_remover.get() != -1) << "Error opening output file";
|
2020-08-31 17:19:00 +08:00
|
|
|
off_t output_size = lseek(file_remover.get(), 0, SEEK_END);
|
2020-09-17 23:36:07 +08:00
|
|
|
ASSERT_EQ(reference_data.size(), output_size)
|
|
|
|
<< "Different sizes of reference and returned data";
|
|
|
|
ASSERT_EQ(lseek(file_remover.get(), 0, SEEK_SET), 0)
|
|
|
|
<< "Error repositioning out file";
|
2020-08-31 17:19:00 +08:00
|
|
|
|
|
|
|
std::string output;
|
|
|
|
output.resize(output_size);
|
|
|
|
ssize_t status = read(file_remover.get(), output.data(), output_size);
|
2020-08-12 18:45:08 +08:00
|
|
|
ASSERT_EQ(status, output_size) << "Error reading data from temp output file";
|
|
|
|
|
2020-08-31 17:19:00 +08:00
|
|
|
ASSERT_EQ(output, reference_data) << "Returned data doesn't match refernce";
|
2020-08-12 18:45:08 +08:00
|
|
|
}
|
|
|
|
|
2020-08-31 17:19:00 +08:00
|
|
|
} // namespace guetzli::sandbox::tests
|