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 "../sandboxed.h" // NOLINT(build/include)
16 #include "helper.h" // NOLINT(build/include)
17 #include "libpng.h" // NOLINT(build/include)
18 #include "gtest/gtest.h"
19 #include "sandboxed_api/util/fileops.h"
20 #include "sandboxed_api/util/path.h"
21 #include "sandboxed_api/util/status_matchers.h"
22 #include "sandboxed_api/util/temp_file.h"
23
24 namespace {
25
26 using ::sapi::IsOk;
27 using ::testing::Eq;
28 using ::testing::IsTrue;
29
TEST(SandboxTest,ReadWrite)30 TEST(SandboxTest, ReadWrite) {
31 std::string infile = GetFilePath("pngtest.png");
32
33 absl::StatusOr<std::string> status_or_path =
34 sapi::CreateNamedTempFileAndClose("output.png");
35 ASSERT_THAT(status_or_path, IsOk()) << "Could not create temp output file";
36
37 std::string outfile = sapi::file::JoinPath(sapi::file_util::fileops::GetCWD(),
38 status_or_path.value());
39
40 LibPNGSapiSandbox sandbox;
41 sandbox.AddFile(infile);
42 sandbox.AddFile(outfile);
43
44 ASSERT_THAT(sandbox.Init(), IsOk()) << "Couldn't initialize Sandboxed API";
45
46 LibPNGApi api(&sandbox);
47
48 sapi::v::Struct<png_image> image;
49 sapi::v::ConstCStr infile_var(infile.c_str());
50 sapi::v::ConstCStr outfile_var(outfile.c_str());
51
52 image.mutable_data()->version = PNG_IMAGE_VERSION;
53
54 absl::StatusOr<int> status_or_int = api.png_image_begin_read_from_file(
55 image.PtrBoth(), infile_var.PtrBefore());
56 ASSERT_THAT(status_or_int, IsOk())
57 << "fatal error when invoking png_image_begin_read_from_file";
58 ASSERT_THAT(status_or_int.value(), IsTrue())
59 << "png_image_begin_read_from_file failed: "
60 << image.mutable_data()->message;
61
62 image.mutable_data()->format = PNG_FORMAT_RGBA;
63 ASSERT_THAT(image.mutable_data()->version, Eq(PNG_IMAGE_VERSION))
64 << "image version changed";
65
66 sapi::v::Array<uint8_t> buffer(PNG_IMAGE_SIZE(*image.mutable_data()));
67 sapi::v::NullPtr null = sapi::v::NullPtr();
68 status_or_int = api.png_image_finish_read(image.PtrBoth(), &null,
69 buffer.PtrBoth(), 0, &null);
70 ASSERT_THAT(status_or_int, IsOk())
71 << "fatal error when invoking png_image_finish_read";
72 ASSERT_THAT(status_or_int.value(), IsTrue())
73 << "png_image_finish_read failed: " << image.mutable_data()->message;
74 ASSERT_THAT(image.mutable_data()->version, Eq(PNG_IMAGE_VERSION))
75 << "image version changed";
76 ASSERT_THAT(image.mutable_data()->format, Eq(PNG_FORMAT_RGBA))
77 << "image format changed";
78
79 status_or_int = api.png_image_write_to_file(
80 image.PtrBoth(), outfile_var.PtrBefore(), 0, buffer.PtrBoth(), 0, &null);
81 ASSERT_THAT(status_or_int, IsOk())
82 << "fatal error when invoking png_image_write_to_file";
83 ASSERT_THAT(status_or_int.value(), IsTrue())
84 << "png_image_finish_read failed: " << image.mutable_data()->message;
85 ASSERT_THAT(image.mutable_data()->version, Eq(PNG_IMAGE_VERSION))
86 << "image version changed";
87 ASSERT_THAT(image.mutable_data()->format, Eq(PNG_FORMAT_RGBA))
88 << "image format changed";
89 }
90
91 } // namespace
92