xref: /aosp_15_r20/external/sandboxed-api/oss-internship-2020/guetzli/guetzli_transaction_test.cc (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1 // Copyright 2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "guetzli_transaction.h"  // NOLINT(build/include)
16 
17 #include <fcntl.h>
18 #include <sys/stat.h>
19 #include <sys/types.h>
20 #include <unistd.h>
21 
22 #include <fstream>
23 #include <memory>
24 #include <sstream>
25 
26 #include "gtest/gtest.h"
27 
28 namespace guetzli::sandbox::tests {
29 
30 namespace {
31 
32 constexpr absl::string_view kInPngFilename = "bees.png";
33 constexpr absl::string_view kInJpegFilename = "nature.jpg";
34 constexpr absl::string_view kOutJpegFilename = "out_jpeg.jpg";
35 constexpr absl::string_view kOutPngFilename = "out_png.png";
36 constexpr absl::string_view kPngReferenceFilename = "bees_reference.jpg";
37 constexpr absl::string_view kJpegReferenceFIlename = "nature_reference.jpg";
38 
39 constexpr int kPngExpectedSize = 38'625;
40 constexpr int kJpegExpectedSize = 10'816;
41 
42 constexpr int kDefaultQualityTarget = 95;
43 constexpr int kDefaultMemlimitMb = 6000;
44 
45 constexpr absl::string_view kRelativePathToTestdata =
46     "/guetzli_sandboxed/testdata/";
47 
GetPathToFile(absl::string_view filename)48 std::string GetPathToFile(absl::string_view filename) {
49   return absl::StrCat(getenv("TEST_SRCDIR"), kRelativePathToTestdata, filename);
50 }
51 
ReadFromFile(const std::string & filename)52 std::string ReadFromFile(const std::string& filename) {
53   std::ifstream stream(filename, std::ios::binary);
54 
55   if (!stream.is_open()) {
56     return "";
57   }
58 
59   std::stringstream result;
60   result << stream.rdbuf();
61   return result.str();
62 }
63 
64 // Helper class to delete file after opening
65 class FileRemover {
66  public:
FileRemover(const char * path)67   explicit FileRemover(const char* path)
68       : path_(path), fd_(open(path, O_RDONLY)) {}
69 
~FileRemover()70   ~FileRemover() {
71     close(fd_);
72     remove(path_);
73   }
74 
get() const75   int get() const { return fd_; }
76 
77  private:
78   const char* path_;
79   int fd_;
80 };
81 
82 }  // namespace
83 
TEST(GuetzliTransactionTest,TestTransactionJpg)84 TEST(GuetzliTransactionTest, TestTransactionJpg) {
85   std::string in_path = GetPathToFile(kInJpegFilename);
86   std::string out_path = GetPathToFile(kOutJpegFilename);
87 
88   TransactionParams params = {in_path.c_str(), out_path.c_str(), 0,
89                               kDefaultQualityTarget, kDefaultMemlimitMb};
90   {
91     GuetzliTransaction transaction(std::move(params));
92     absl::Status result = transaction.Run();
93 
94     ASSERT_TRUE(result.ok()) << result.ToString();
95   }
96   std::string reference_data =
97       ReadFromFile(GetPathToFile(kJpegReferenceFIlename));
98   FileRemover file_remover(out_path.c_str());
99   ASSERT_TRUE(file_remover.get() != -1) << "Error opening output file";
100   off_t output_size = lseek(file_remover.get(), 0, SEEK_END);
101   ASSERT_EQ(reference_data.size(), output_size)
102       << "Different sizes of reference and returned data";
103   ASSERT_EQ(lseek(file_remover.get(), 0, SEEK_SET), 0)
104       << "Error repositioning out file";
105 
106   std::string output;
107   output.resize(output_size);
108   ssize_t status = read(file_remover.get(), output.data(), output_size);
109   ASSERT_EQ(status, output_size) << "Error reading data from temp output file";
110 
111   ASSERT_EQ(output, reference_data) << "Returned data doesn't match reference";
112 }
113 
TEST(GuetzliTransactionTest,TestTransactionPng)114 TEST(GuetzliTransactionTest, TestTransactionPng) {
115   std::string in_path = GetPathToFile(kInPngFilename);
116   std::string out_path = GetPathToFile(kOutPngFilename);
117 
118   TransactionParams params = {in_path.c_str(), out_path.c_str(), 0,
119                               kDefaultQualityTarget, kDefaultMemlimitMb};
120   {
121     GuetzliTransaction transaction(std::move(params));
122     absl::Status result = transaction.Run();
123 
124     ASSERT_TRUE(result.ok()) << result.ToString();
125   }
126   std::string reference_data =
127       ReadFromFile(GetPathToFile(kPngReferenceFilename));
128   FileRemover file_remover(out_path.c_str());
129   ASSERT_TRUE(file_remover.get() != -1) << "Error opening output file";
130   off_t output_size = lseek(file_remover.get(), 0, SEEK_END);
131   ASSERT_EQ(reference_data.size(), output_size)
132       << "Different sizes of reference and returned data";
133   ASSERT_EQ(lseek(file_remover.get(), 0, SEEK_SET), 0)
134       << "Error repositioning out file";
135 
136   std::string output;
137   output.resize(output_size);
138   ssize_t status = read(file_remover.get(), output.data(), output_size);
139   ASSERT_EQ(status, output_size) << "Error reading data from temp output file";
140 
141   ASSERT_EQ(output, reference_data) << "Returned data doesn't match refernce";
142 }
143 
144 }  // namespace guetzli::sandbox::tests
145