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 <fcntl.h>
16 #include <sys/stat.h>
17 #include <sys/types.h>
18 #include <syscall.h>
19
20 #include <algorithm>
21 #include <fstream>
22 #include <memory>
23 #include <sstream>
24
25 #include "guetzli_sandbox.h" // NOLINT(build/include)
26 #include "gtest/gtest.h"
27 #include "absl/status/statusor.h"
28 #include "sandboxed_api/vars.h"
29
30 namespace guetzli::sandbox::tests {
31
32 namespace {
33
34 constexpr absl::string_view kInPngFilename = "bees.png";
35 constexpr absl::string_view kInJpegFilename = "nature.jpg";
36 constexpr absl::string_view kPngReferenceFilename = "bees_reference.jpg";
37 constexpr absl::string_view kJpegReferenceFIlename = "nature_reference.jpg";
38
39 constexpr int kDefaultQualityTarget = 95;
40 constexpr int kDefaultMemlimitMb = 6000;
41
42 constexpr absl::string_view kRelativePathToTestdata =
43 "/guetzli_sandboxed/testdata/";
44
GetPathToInputFile(absl::string_view filename)45 std::string GetPathToInputFile(absl::string_view filename) {
46 return absl::StrCat(getenv("TEST_SRCDIR"), kRelativePathToTestdata, filename);
47 }
48
ReadFromFile(const std::string & filename)49 std::string ReadFromFile(const std::string& filename) {
50 std::ifstream stream(filename, std::ios::binary);
51
52 if (!stream.is_open()) {
53 return "";
54 }
55
56 std::stringstream result;
57 result << stream.rdbuf();
58 return result.str();
59 }
60
61 } // namespace
62
63 class GuetzliSapiTest : public ::testing::Test {
64 protected:
SetUp()65 void SetUp() override {
66 sandbox_ = std::make_unique<GuetzliSapiSandbox>();
67 ASSERT_EQ(sandbox_->Init(), absl::OkStatus());
68 api_ = std::make_unique<GuetzliApi>(sandbox_.get());
69 }
70
71 std::unique_ptr<GuetzliSapiSandbox> sandbox_;
72 std::unique_ptr<GuetzliApi> api_;
73 };
74
75 // This test can take up to few minutes depending on your hardware
TEST_F(GuetzliSapiTest,ProcessRGB)76 TEST_F(GuetzliSapiTest, ProcessRGB) {
77 sapi::v::Fd in_fd(open(GetPathToInputFile(kInPngFilename).c_str(), O_RDONLY));
78 ASSERT_TRUE(in_fd.GetValue() != -1) << "Error opening input file";
79 ASSERT_EQ(api_->sandbox()->TransferToSandboxee(&in_fd), absl::OkStatus())
80 << "Error transfering fd to sandbox";
81 ASSERT_TRUE(in_fd.GetRemoteFd() != -1) << "Error opening remote fd";
82 sapi::v::Struct<ProcessingParams> processing_params;
83 *processing_params.mutable_data() = {
84 in_fd.GetRemoteFd(), 0, kDefaultQualityTarget, kDefaultMemlimitMb};
85 sapi::v::LenVal output(0);
86 absl::StatusOr<bool> processing_result =
87 api_->ProcessRgb(processing_params.PtrBefore(), output.PtrBoth());
88 ASSERT_TRUE(processing_result.value_or(false)) << "Error processing rgb data";
89 std::string reference_data =
90 ReadFromFile(GetPathToInputFile(kPngReferenceFilename));
91 ASSERT_EQ(output.GetDataSize(), reference_data.size())
92 << "Incorrect result data size";
93 ASSERT_EQ(
94 std::string(output.GetData(), output.GetData() + output.GetDataSize()),
95 reference_data)
96 << "Processed data doesn't match reference output";
97 }
98
99 // This test can take up to few minutes depending on your hardware
TEST_F(GuetzliSapiTest,ProcessJpeg)100 TEST_F(GuetzliSapiTest, ProcessJpeg) {
101 sapi::v::Fd in_fd(
102 open(GetPathToInputFile(kInJpegFilename).c_str(), O_RDONLY));
103 ASSERT_TRUE(in_fd.GetValue() != -1) << "Error opening input file";
104 ASSERT_EQ(api_->sandbox()->TransferToSandboxee(&in_fd), absl::OkStatus())
105 << "Error transfering fd to sandbox";
106 ASSERT_TRUE(in_fd.GetRemoteFd() != -1) << "Error opening remote fd";
107 sapi::v::Struct<ProcessingParams> processing_params;
108 *processing_params.mutable_data() = {
109 in_fd.GetRemoteFd(), 0, kDefaultQualityTarget, kDefaultMemlimitMb};
110 sapi::v::LenVal output(0);
111 absl::StatusOr<bool> processing_result =
112 api_->ProcessJpeg(processing_params.PtrBefore(), output.PtrBoth());
113 ASSERT_TRUE(processing_result.value_or(false)) << "Error processing jpg data";
114 std::string reference_data =
115 ReadFromFile(GetPathToInputFile(kJpegReferenceFIlename));
116 ASSERT_EQ(output.GetDataSize(), reference_data.size())
117 << "Incorrect result data size";
118 ASSERT_EQ(
119 std::string(output.GetData(), output.GetData() + output.GetDataSize()),
120 reference_data)
121 << "Processed data doesn't match reference output";
122 }
123
124 } // namespace guetzli::sandbox::tests
125