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 <array>
16 #include <cstdint>
17 #include <cstring>
18 #include <iostream>
19 #include <vector>
20
21 #include "../sandboxed.h" // NOLINT(build/include)
22 #include "absl/flags/parse.h"
23 #include "absl/log/globals.h"
24 #include "absl/log/initialize.h"
25 #include "sandboxed_api/util/fileops.h"
26 #include "sandboxed_api/util/path.h"
27 #include "sandboxed_api/vars.h"
28 #include "tiffio.h" // NOLINT(build/include)
29
30 // sapi functions:
31 // TIFFTileSize
32 // TIFFOpen
33 // TIFFReadEncodedTile
34 // TIFFSetField
35 // TIFFClose
36 // TIFFReadRGBATile
37 // TIFFGetField
38
39 namespace {
40
41 constexpr std::array<uint8_t, 6> kCluster0 = {0, 0, 2, 0, 138, 139};
42 constexpr std::array<uint8_t, 6> kCluster64 = {0, 0, 9, 6, 134, 119};
43 constexpr std::array<uint8_t, 6> kCluster128 = {44, 40, 63, 59, 230, 95};
44
45 template <typename ArrayT>
CheckCluster(int cluster,const sapi::v::Array<uint8_t> & buffer,const ArrayT & expected_cluster)46 int CheckCluster(int cluster, const sapi::v::Array<uint8_t>& buffer,
47 const ArrayT& expected_cluster) {
48 uint8_t* target = buffer.GetData() + cluster * 6;
49
50 if (!std::memcmp(target, expected_cluster.data(), 6)) {
51 return 0;
52 }
53
54 std::cerr << "Cluster " << cluster << " did not match expected results.\n"
55 << "Expect: " << expected_cluster[0] << "\t" << expected_cluster[1]
56 << "\t" << expected_cluster[4] << "\t" << expected_cluster[5]
57 << "\t" << expected_cluster[2] << "\t" << expected_cluster[3]
58 << "\n"
59 << "Got: " << target[0] << "\t" << target[1] << "\t" << target[4]
60 << "\t" << target[5] << "\t" << target[2] << "\t" << target[3]
61 << '\n';
62
63 return 1;
64 }
65
CheckRgbPixel(int pixel,int min_red,int max_red,int min_green,int max_green,int min_blue,int max_blue,const sapi::v::Array<uint8_t> & buffer)66 int CheckRgbPixel(int pixel, int min_red, int max_red, int min_green,
67 int max_green, int min_blue, int max_blue,
68 const sapi::v::Array<uint8_t>& buffer) {
69 uint8_t* rgb = buffer.GetData() + 3 * pixel;
70
71 if (rgb[0] >= min_red && rgb[0] <= max_red && rgb[1] >= min_green &&
72 rgb[1] <= max_green && rgb[2] >= min_blue && rgb[2] <= max_blue) {
73 return 0;
74 }
75
76 std::cerr << "Pixel " << pixel << " did not match expected results.\n"
77 << "Got R=" << rgb[0] << " (expected " << min_red << ".." << max_red
78 << "), G=" << rgb[1] << " (expected " << min_green << ".."
79 << max_green << "), B=" << rgb[2] << " (expected " << min_blue
80 << ".." << max_blue << ")\n";
81 return 1;
82 }
83
CheckRgbaPixel(int pixel,int min_red,int max_red,int min_green,int max_green,int min_blue,int max_blue,int min_alpha,int max_alpha,const sapi::v::Array<uint32_t> & buffer)84 int CheckRgbaPixel(int pixel, int min_red, int max_red, int min_green,
85 int max_green, int min_blue, int max_blue, int min_alpha,
86 int max_alpha, const sapi::v::Array<uint32_t>& buffer) {
87 // RGBA images are upside down - adjust for normal ordering
88 int adjusted_pixel = pixel % 128 + (127 - (pixel / 128)) * 128;
89 uint32_t rgba = buffer[adjusted_pixel];
90
91 if (TIFFGetR(rgba) >= static_cast<uint32_t>(min_red) &&
92 TIFFGetR(rgba) <= static_cast<uint32_t>(max_red) &&
93 TIFFGetG(rgba) >= static_cast<uint32_t>(min_green) &&
94 TIFFGetG(rgba) <= static_cast<uint32_t>(max_green) &&
95 TIFFGetB(rgba) >= static_cast<uint32_t>(min_blue) &&
96 TIFFGetB(rgba) <= static_cast<uint32_t>(max_blue) &&
97 TIFFGetA(rgba) >= static_cast<uint32_t>(min_alpha) &&
98 TIFFGetA(rgba) <= static_cast<uint32_t>(max_alpha)) {
99 return 0;
100 }
101
102 std::cerr << "Pixel " << pixel << " did not match expected results.\n"
103 << "Got R=" << TIFFGetR(rgba) << " (expected " << min_red << ".."
104 << max_red << "), G=" << TIFFGetG(rgba) << " (expected "
105 << min_green << ".." << max_green << "), B=" << TIFFGetB(rgba)
106 << " (expected " << min_blue << ".." << max_blue
107 << "), A=" << TIFFGetA(rgba) << " (expected " << min_alpha << ".."
108 << max_alpha << ")\n";
109 return 1;
110 }
GetFilePath(const std::string & dir,const std::string & filename)111 std::string GetFilePath(const std::string& dir, const std::string& filename) {
112 return sapi::file::JoinPath(dir, "test", "images", filename);
113 }
114
GetCWD()115 std::string GetCWD() {
116 char cwd[PATH_MAX];
117 getcwd(cwd, sizeof(cwd));
118 return cwd;
119 }
120
GetFilePath(const std::string filename)121 std::string GetFilePath(const std::string filename) {
122 std::string cwd = GetCWD();
123 auto find = cwd.rfind("build");
124
125 std::string project_path;
126 if (find == std::string::npos) {
127 std::cerr << "Something went wrong: CWD don't contain build dir. "
128 << "Please run tests from build dir or send project dir as a "
129 << "parameter: ./sandboxed /absolute/path/to/project/dir \n";
130 project_path = cwd;
131 } else {
132 project_path = cwd.substr(0, find);
133 }
134
135 return sapi::file::JoinPath(project_path, "test", "images", filename);
136 }
137
138 } // namespace
139
main(int argc,char * argv[])140 int main(int argc, char* argv[]) {
141 absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
142 absl::ParseCommandLine(argc, argv);
143 absl::InitializeLog();
144
145 std::string srcfile;
146 // "test/images/quad-tile.jpg.tiff"
147 std::string srcfilerel = "quad-tile.jpg.tiff";
148
149 if (argc < 2) {
150 srcfile = GetFilePath(srcfilerel);
151 } else {
152 srcfile = GetFilePath(argv[1], srcfilerel);
153 }
154
155 // without addDir to sandbox. to add dir use
156 // sandbox(absolute_path_to_dir, srcfile) or
157 // sandbox(absolute_path_to_dir). file and dir should be exists.
158 // srcfile must also be absolute_path_to_file
159 TiffSapiSandbox sandbox("", srcfile);
160
161 // initialize sapi vars after constructing TiffSapiSandbox
162 sapi::v::UShort h, v;
163 absl::StatusOr<TIFF*> status_or_tif;
164 absl::StatusOr<int> status_or_int;
165 absl::StatusOr<tmsize_t> status_or_long;
166
167 auto status = sandbox.Init();
168 if (!status.ok()) {
169 fprintf(stderr, "Couldn't initialize Sandboxed API: %s\n", status);
170 }
171
172 TiffApi api(&sandbox);
173 sapi::v::ConstCStr srcfile_var(srcfile.c_str());
174 sapi::v::ConstCStr r_var("r");
175
176 status_or_tif = api.TIFFOpen(srcfile_var.PtrBefore(), r_var.PtrBefore());
177 if (!status_or_tif.ok()) {
178 std::cerr << "Could not open " << srcfile << ", TIFFError\n";
179 return 1;
180 }
181
182 sapi::v::RemotePtr tif(status_or_tif.value());
183 if (!tif.GetValue()) {
184 // tif is NULL
185 std::cerr << "Could not open " << srcfile << "\n";
186 return 1;
187 }
188
189 status_or_int = api.TIFFGetField2(&tif, TIFFTAG_YCBCRSUBSAMPLING, h.PtrBoth(),
190 v.PtrBoth());
191 if (!status_or_int.ok() || status_or_int.value() == 0 || h.GetValue() != 2 ||
192 v.GetValue() != 2) {
193 std::cerr << "Could not retrieve subsampling tag\n";
194 return 1;
195 }
196
197 status_or_long = api.TIFFTileSize(&tif);
198 if (!status_or_int.ok() || status_or_long.value() != 24576) {
199 std::cerr << "tiles are " << status_or_long.value() << " bytes\n";
200 exit(1);
201 }
202 tsize_t sz = status_or_long.value();
203
204 sapi::v::Array<uint8_t> buffer_(sz);
205 status_or_long = api.TIFFReadEncodedTile(&tif, 9, buffer_.PtrBoth(), sz);
206 if (!status_or_long.ok() || status_or_long.value() != sz) {
207 std::cerr << "Did not get expected result code from"
208 << "TIFFReadEncodedTile(): (" << status_or_long.value()
209 << " instead of " << sz << ")\n";
210 return 1;
211 }
212
213 if (CheckCluster(0, buffer_, kCluster0) ||
214 CheckCluster(64, buffer_, kCluster64) ||
215 CheckCluster(128, buffer_, kCluster128)) {
216 return 1;
217 }
218
219 status_or_int =
220 api.TIFFSetFieldU1(&tif, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
221 if (!status_or_int.ok() || !status_or_int.value()) {
222 std::cerr << "TIFFSetFieldU1 not available\n";
223 }
224
225 status_or_long = api.TIFFTileSize(&tif);
226 if (!status_or_long.ok() || status_or_long.value() != 128 * 128 * 3) {
227 std::cerr << "tiles are " << status_or_long.value() << " bytes\n";
228 return 1;
229 }
230 sz = status_or_long.value();
231
232 sapi::v::Array<uint8_t> buffer2_(sz);
233
234 status_or_long = api.TIFFReadEncodedTile(&tif, 9, buffer2_.PtrBoth(), sz);
235 if (!status_or_long.ok() || status_or_long.value() != sz) {
236 std::cerr << "Did not get expected result code from "
237 << "TIFFReadEncodedTile(): (" << status_or_long.value()
238 << " instead of " << sz << ")\n";
239 return 1;
240 }
241
242 uint32_t pixel_status = 0;
243 pixel_status |= CheckRgbPixel(0, 15, 18, 0, 0, 18, 41, buffer2_);
244 pixel_status |= CheckRgbPixel(64, 0, 0, 0, 0, 0, 2, buffer2_);
245 pixel_status |= CheckRgbPixel(512, 5, 6, 34, 36, 182, 196, buffer2_);
246
247 if (!api.TIFFClose(&tif).ok()) {
248 std::cerr << "TIFFClose error\n";
249 }
250
251 status_or_tif = api.TIFFOpen(srcfile_var.PtrBefore(), r_var.PtrBefore());
252 if (!status_or_tif.ok()) {
253 std::cerr << "Could not reopen " << srcfile << "\n";
254 return 1;
255 }
256
257 sapi::v::RemotePtr tif2(status_or_tif.value());
258 if (!tif2.GetValue()) { // tif is NULL
259 std::cerr << "Could not reopen " << srcfile << "\n";
260 return 1;
261 }
262
263 sapi::v::Array<uint32> rgba_buffer_(128 * 128);
264
265 status_or_int =
266 api.TIFFReadRGBATile(&tif2, 1 * 128, 2 * 128, rgba_buffer_.PtrBoth());
267 if (!status_or_int.ok() || !status_or_int.value()) {
268 fprintf(stderr, "TIFFReadRGBATile() returned failure code.\n");
269 return 1;
270 }
271
272 pixel_status |=
273 CheckRgbaPixel(0, 15, 18, 0, 0, 18, 41, 255, 255, rgba_buffer_);
274 pixel_status |= CheckRgbaPixel(64, 0, 0, 0, 0, 0, 2, 255, 255, rgba_buffer_);
275 pixel_status |=
276 CheckRgbaPixel(512, 5, 6, 34, 36, 182, 196, 255, 255, rgba_buffer_);
277
278 if (!api.TIFFClose(&tif2).ok()) {
279 std::cerr << "TIFFClose erro\n";
280 }
281
282 if (pixel_status) {
283 std::cerr << "pixel_status is true, expected false";
284 return 1;
285 }
286
287 return 0;
288 }
289