main fixes

This commit is contained in:
Alexandra Latysheva 2020-09-25 21:21:07 +00:00
parent 309b476a3d
commit 71e43cd99c
13 changed files with 365 additions and 311 deletions

View File

@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
cmake_minimum_required(VERSION 3.10) cmake_minimum_required(VERSION 3.12)
project(sandboxed_libtiff CXX) project(sandboxed_libtiff CXX)
@ -120,10 +120,10 @@ target_include_directories(tiff_sapi INTERFACE
"${PROJECT_BINARY_DIR}" # To find the generated SAPI header "${PROJECT_BINARY_DIR}" # To find the generated SAPI header
) )
if (TIFF_SAPI_ENABLE_EXAMPLES) #if (TIFF_SAPI_ENABLE_EXAMPLES)
add_subdirectory(example) add_subdirectory(example)
endif() #endif()
if (TIFF_SAPI_ENABLE_TESTS) #if (TIFF_SAPI_ENABLE_TESTS)
add_subdirectory(test) add_subdirectory(test)
endif() #endif()

View File

@ -19,4 +19,4 @@ You should make sure the libtiff submodule is cloned.
#### to run tests: #### to run tests:
`./test/tests` `./test/tests`
you also can use sandbox flags `sandbox2_danger_danger_permit_all` and `sandbox2_danger_danger_permit_all_and_log` You also can use sandbox flags `sandbox2_danger_danger_permit_all` and `sandbox2_danger_danger_permit_all_and_log`.

View File

@ -12,36 +12,39 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include <cstdlib>
#include <cstring> #include <cstring>
#include <iostream> #include <iostream>
#include <vector> #include <array>
#include <cstdint>
#include "../sandboxed.h" #include "../sandboxed.h"
#include "sandboxed_api/sandbox2/util/fileops.h" #include "sandboxed_api/sandbox2/util/fileops.h"
#include "sandboxed_api/sandbox2/util/path.h" #include "sandboxed_api/sandbox2/util/path.h"
#include "tiffio.h" #include "sandboxed_api/vars.h"
#include "tiffio.h" // NOLINT(build/include)
/* sapi functions: // sapi functions:
* TIFFTileSize // TIFFTileSize
* TIFFOpen // TIFFOpen
* TIFFReadEncodedTile // TIFFReadEncodedTile
* TIFFSetField // TIFFSetField
* TIFFClose // TIFFClose
* TIFFReadRGBATile // TIFFReadRGBATile
* TIFFGetField // TIFFGetField
*/
static const std::vector<unsigned char> cluster_0 = {0, 0, 2, 0, 138, 139}; constexpr std::array<uint8_t, 6> kCluster0 = {0, 0, 2, 0, 138, 139};
static const std::vector<unsigned char> cluster_64 = {0, 0, 9, 6, 134, 119}; constexpr std::array<uint8_t, 6> kCluster64 = {0, 0, 9, 6, 134, 119};
static const std::vector<unsigned char> cluster_128 = {44, 40, 63, 59, 230, 95}; constexpr std::array<uint8_t, 6> kCluster128 = {44, 40, 63, 59, 230, 95};
static int CheckCluster(int cluster, namespace {
const sapi::v::Array<unsigned char> &buffer,
const std::vector<unsigned char> &expected_cluster) { absl::Status CheckCluster(int cluster, const sapi::v::Array<uint8_t>& buffer,
unsigned char* target = buffer.GetData() + cluster * 6; const std::array<uint8_t, 6>& expected_cluster) {
uint8_t* target = buffer.GetData() + cluster * 6;
if (!std::memcmp(target, expected_cluster.data(), 6)) { if (!std::memcmp(target, expected_cluster.data(), 6)) {
return 0; return absl::OkStatus();
} }
std::cerr << "Cluster " << cluster << " did not match expected results.\n" std::cerr << "Cluster " << cluster << " did not match expected results.\n"
@ -53,28 +56,33 @@ static int CheckCluster(int cluster,
<< "\t" << target[5] << "\t" << target[2] << "\t" << target[3] << "\t" << target[5] << "\t" << target[2] << "\t" << target[3]
<< '\n'; << '\n';
return 1; return absl::InternalError(absl::StrCat(
"Cluster ", cluster, " did not match expected results.\n", "Expect: ",
expected_cluster[0], "\t", expected_cluster[1], "\t", expected_cluster[4],
"\t", expected_cluster[5], "\t", expected_cluster[2], "\t",
expected_cluster[3], "\n"
));
} }
static int CheckRgbPixel(int pixel, int min_red, int max_red, int min_green, absl::Status CheckRgbPixel(int pixel, int min_red, int max_red, int min_green,
int max_green, int min_blue, int max_blue, int max_green, int min_blue, int max_blue,
const sapi::v::Array<unsigned char> &buffer) { const sapi::v::Array<uint8_t>& buffer) {
unsigned char* rgb = buffer.GetData() + 3 * pixel; uint8_t* rgb = buffer.GetData() + 3 * pixel;
if (rgb[0] >= min_red && rgb[0] <= max_red && rgb[1] >= min_green && if (rgb[0] >= min_red && rgb[0] <= max_red && rgb[1] >= min_green &&
rgb[1] <= max_green && rgb[2] >= min_blue && rgb[2] <= max_blue) { rgb[1] <= max_green && rgb[2] >= min_blue && rgb[2] <= max_blue) {
return 0; return absl::OkStatus();
} }
std::cerr << "Pixel " << pixel << " did not match expected results.\n" return absl::InternalError(absl::StrCat(
<< "Got R=" << rgb[0] << " (expected " << min_red << ".." << max_red "Pixel ", pixel, " did not match expected results.\n", "Got R=", rgb[0],
<< "), G=" << rgb[1] << " (expected " << min_green << ".." " (expected ", min_red, "..", max_red, "), G=", rgb[1], " (expected ",
<< max_green << "), B=" << rgb[2] << " (expected " << min_blue min_green, "..", max_green, "), B=", rgb[2], " (expected ", min_blue, "..",
<< ".." << max_blue << ")\n"; max_blue, ")\n"
return 1; ));
} }
static int CheckRgbaPixel(int pixel, int min_red, int max_red, int min_green, absl::Status CheckRgbaPixel(int pixel, int min_red, int max_red, int min_green,
int max_green, int min_blue, int max_blue, int max_green, int min_blue, int max_blue,
int min_alpha, int max_alpha, int min_alpha, int max_alpha,
const sapi::v::Array<unsigned>& buffer) { const sapi::v::Array<unsigned>& buffer) {
@ -89,35 +97,31 @@ static int CheckRgbaPixel(int pixel, int min_red, int max_red, int min_green,
TIFFGetB(rgba) <= (uint32)max_blue && TIFFGetB(rgba) <= (uint32)max_blue &&
TIFFGetA(rgba) >= (uint32)min_alpha && TIFFGetA(rgba) >= (uint32)min_alpha &&
TIFFGetA(rgba) <= (uint32)max_alpha) { TIFFGetA(rgba) <= (uint32)max_alpha) {
return 0; return absl::OkStatus();
} }
std::cerr << "Pixel " << pixel << " did not match expected results.\n" return absl::InternalError(absl::StrCat(
<< "Got R=" << TIFFGetR(rgba) << " (expected " << min_red << ".." "Pixel ", pixel, " did not match expected results.\n", "Got R=",
<< max_red << "), G=" << TIFFGetG(rgba) << " (expected " TIFFGetR(rgba), " (expected ", min_red, "..", max_red, "), G=",
<< min_green << ".." << max_green << "), B=" << TIFFGetB(rgba) TIFFGetG(rgba), " (expected ", min_green, "..", max_green, "), B=",
<< " (expected " << min_blue << ".." << max_blue TIFFGetB(rgba), " (expected ", min_blue, "..", max_blue, "), A=",
<< "), A=" << TIFFGetA(rgba) << " (expected " << min_alpha << ".." TIFFGetA(rgba), " (expected ", min_alpha, "..", max_alpha, ")\n"
<< max_alpha << ")\n"; ));
return 1;
} }
} // namespace
std::string GetFilePath(const std::string& dir, const std::string& filename) { std::string GetFilePath(const std::string& dir, const std::string& filename) {
return sandbox2::file::JoinPath(dir, "test", "images", filename); return sandbox2::file::JoinPath(dir, "test", "images", filename);
} }
std::string GetCWD() {
char cwd[PATH_MAX];
getcwd(cwd, sizeof(cwd));
return cwd;
}
std::string GetFilePath(const std::string filename) { std::string GetFilePath(const std::string filename) {
std::string cwd = GetCWD(); std::string cwd = sandbox2::file_util::fileops::GetCWD();
auto find = cwd.rfind("build"); auto find = cwd.rfind("build");
std::string project_path; std::string project_path;
if (find == std::string::npos) { if (find == std::string::npos) {
std::cerr << "Something went wrong: CWD don't contain build dir. " LOG(ERROR) << "Something went wrong: CWD don't contain build dir. "
<< "Please run tests from build dir or send project dir as a " << "Please run tests from build dir or send project dir as a "
<< "parameter: ./sandboxed /absolute/path/to/project/dir \n"; << "parameter: ./sandboxed /absolute/path/to/project/dir \n";
project_path = cwd; project_path = cwd;
@ -128,6 +132,173 @@ std::string GetFilePath(const std::string filename) {
return sandbox2::file::JoinPath(project_path, "test", "images", filename); return sandbox2::file::JoinPath(project_path, "test", "images", filename);
} }
absl::Status LibTIFFMain(const std::string& srcfile) {
// without addDir to sandbox. to add dir use
// sandbox(absolute_path_to_dir, srcfile) or
// sandbox(absolute_path_to_dir). file and dir should be exists.
// srcfile must also be absolute_path_to_file
TiffSapiSandbox sandbox("", srcfile);
// initialize sapi vars after constructing TiffSapiSandbox
sapi::v::UShort h, v;
sapi::StatusOr<TIFF*> status_or_tif;
sapi::StatusOr<int> status_or_int;
sapi::StatusOr<tmsize_t> status_or_long;
absl::Status status;
status = sandbox.Init();
SAPI_RETURN_IF_ERROR(sandbox.Init());
TiffApi api(&sandbox);
sapi::v::ConstCStr srcfile_var(srcfile.c_str());
sapi::v::ConstCStr r_var("r");
SAPI_ASSIGN_OR_RETURN(
status_or_tif, api.TIFFOpen(srcfile_var.PtrBefore(), r_var.PtrBefore()));
sapi::v::RemotePtr tif(status_or_tif.value());
if (!tif.GetValue()) {
// tif is NULL
return absl::InternalError(absl::StrCat("Could not open ", srcfile));
}
SAPI_ASSIGN_OR_RETURN(
status_or_int, api.TIFFGetField2(&tif, TIFFTAG_YCBCRSUBSAMPLING,
h.PtrBoth(), v.PtrBoth()));
if (status_or_int.value() == 0 || h.GetValue() != 2 || v.GetValue() != 2) {
return absl::InternalError("Could not retrieve subsampling tag");
}
SAPI_ASSIGN_OR_RETURN(
status_or_long, api.TIFFTileSize(&tif));
if (status_or_long.value() != 24576) {
return absl::InternalError(absl::StrCat("tiles are ",
status_or_long.value(),
" bytes\n"));
}
tsize_t sz = status_or_long.value();
sapi::v::Array<uint8_t> buffer_(sz);
SAPI_ASSIGN_OR_RETURN(
status_or_long, api.TIFFReadEncodedTile(&tif, 9, buffer_.PtrBoth(), sz));
if (status_or_long.value() != sz) {
return absl::InternalError(absl::StrCat(
"Did not get expected result code from TIFFReadEncodedTile(): ",
status_or_long.value(), " instead of ", sz
));
}
unsigned pixel_status = 1;
if (status = CheckCluster(0, buffer_, kCluster0); !status.ok()) {
LOG(ERROR) << "CheckCluster failed:\n" << status.ToString();
}
pixel_status &= status.ok();
if (status = CheckCluster(64, buffer_, kCluster64); !status.ok()) {
LOG(ERROR) << "CheckCluster failed:\n" << status.ToString();
}
pixel_status &= status.ok();
if (status = CheckCluster(128, buffer_, kCluster128); !status.ok()) {
LOG(ERROR) << "CheckCluster failed:\n" << status.ToString();
}
pixel_status &= status.ok();
if (!pixel_status) {
return absl::InternalError("unexpected pixel_status value");
}
SAPI_ASSIGN_OR_RETURN(
status_or_int, api.TIFFSetFieldU1(&tif, TIFFTAG_JPEGCOLORMODE,
JPEGCOLORMODE_RGB));
if (!status_or_int.value()) {
return absl::InternalError("TIFFSetFieldU1 not available");
}
SAPI_ASSIGN_OR_RETURN(
status_or_long, api.TIFFTileSize(&tif));
if (status_or_long.value() != 128 * 128 * 3) {
return absl::InternalError(absl::StrCat("tiles are ",
status_or_long.value(),
" bytes"));
}
sz = status_or_long.value();
sapi::v::Array<uint8_t> buffer2_(sz);
SAPI_ASSIGN_OR_RETURN(
status_or_long, api.TIFFReadEncodedTile(&tif, 9, buffer2_.PtrBoth(), sz));
if (status_or_long.value() != sz) {
return absl::InternalError(absl::StrCat(
"Did not get expected result code from TIFFReadEncodedTile(): ",
status_or_long.value(), " instead of ", sz
));
}
pixel_status = 1;
if (status = CheckRgbPixel(0, 15, 18, 0, 0, 18, 41, buffer2_);
!status.ok()) {
LOG(ERROR) << "CheckRgbPixel failed:\n" << status.ToString();
}
pixel_status &= status.ok();
if (status = CheckRgbPixel(64, 0, 0, 0, 0, 0, 2, buffer2_); !status.ok()) {
LOG(ERROR) << "CheckRgbPixel failed:\n" << status.ToString();
}
pixel_status &= status.ok();
if (status = CheckRgbPixel(512, 5, 6, 34, 36, 182, 196, buffer2_);
!status.ok()) {
LOG(ERROR) << "CheckRgbPixel failed:\n" << status.ToString();
}
pixel_status &= status.ok();
SAPI_RETURN_IF_ERROR(api.TIFFClose(&tif));
SAPI_ASSIGN_OR_RETURN(
status_or_tif, api.TIFFOpen(srcfile_var.PtrBefore(), r_var.PtrBefore()));
sapi::v::RemotePtr tif2(status_or_tif.value());
if (!tif2.GetValue()) { // tif is NULL
return absl::InternalError(absl::StrCat("Could not reopen ", srcfile));
}
sapi::v::Array<uint32> rgba_buffer_(128 * 128);
SAPI_ASSIGN_OR_RETURN(status_or_int,
api.TIFFReadRGBATile(&tif2, 1 * 128, 2 * 128, rgba_buffer_.PtrBoth()));
if (!status_or_int.value()) {
return absl::InternalError("TIFFReadRGBATile() returned failure code");
}
if (status = CheckRgbaPixel(0, 15, 18, 0, 0, 18, 41, 255, 255, rgba_buffer_);
!status.ok()) {
LOG(ERROR) << "CheckRgbaPixel failed:\n" << status.ToString();
}
pixel_status &= status.ok();
if (status = CheckRgbaPixel(64, 0, 0, 0, 0, 0, 2, 255, 255, rgba_buffer_);
!status.ok()) {
LOG(ERROR) << "CheckRgbaPixel failed:\n" << status.ToString();
}
pixel_status &= status.ok();
if (status = CheckRgbaPixel(512, 5, 6, 34, 36, 182, 196, 255, 255,
rgba_buffer_); !status.ok()) {
LOG(ERROR) << "CheckRgbaPixel failed:\n" << status.ToString();
}
pixel_status &= status.ok();
SAPI_RETURN_IF_ERROR(api.TIFFClose(&tif2));
if (!pixel_status) {
return absl::InternalError("unexpected pixel_status value");
}
return absl::OkStatus();
}
int main(int argc, char** argv) { int main(int argc, char** argv) {
gflags::ParseCommandLineFlags(&argc, &argv, true); gflags::ParseCommandLineFlags(&argc, &argv, true);
@ -141,137 +312,12 @@ int main(int argc, char** argv) {
srcfile = GetFilePath(argv[1], srcfilerel); srcfile = GetFilePath(argv[1], srcfilerel);
} }
// without addDir to sandbox. to add dir use auto status = LibTIFFMain(srcfile);
// sandbox(absolute_path_to_dir, srcfile) or
// sandbox(absolute_path_to_dir). file and dir should be exists.
// srcfile must also be absolute_path_to_file
TiffSapiSandbox sandbox("", srcfile);
// initialize sapi vars after constructing TiffSapiSandbox
sapi::v::UShort h, v;
sapi::StatusOr<TIFF*> status_or_tif;
sapi::StatusOr<int> status_or_int;
sapi::StatusOr<tmsize_t> status_or_long;
auto status = sandbox.Init();
if (!status.ok()) { if (!status.ok()) {
fprintf(stderr, "Couldn't initialize Sandboxed API: %s\n", status); LOG(ERROR)
<< "LibTIFFMain failed with error:\n" << status.ToString() << '\n';
return EXIT_FAILURE;
} }
TiffApi api(&sandbox); return EXIT_SUCCESS;
sapi::v::ConstCStr srcfile_var(srcfile.c_str());
sapi::v::ConstCStr r_var("r");
status_or_tif = api.TIFFOpen(srcfile_var.PtrBefore(), r_var.PtrBefore());
if (!status_or_tif.ok()) {
std::cerr << "Could not open " << srcfile << ", TIFFError\n";
return 1;
}
sapi::v::RemotePtr tif(status_or_tif.value());
if (!tif.GetValue()) {
// tif is NULL
std::cerr << "Could not open " << srcfile << "\n";
return 1;
}
status_or_int = api.TIFFGetField2(&tif, TIFFTAG_YCBCRSUBSAMPLING, h.PtrBoth(),
v.PtrBoth());
if (!status_or_int.ok() || status_or_int.value() == 0 || h.GetValue() != 2 ||
v.GetValue() != 2) {
std::cerr << "Could not retrieve subsampling tag\n";
return 1;
}
status_or_long = api.TIFFTileSize(&tif);
if (!status_or_int.ok() || status_or_long.value() != 24576) {
std::cerr << "tiles are " << status_or_long.value() << " bytes\n";
exit(1);
}
tsize_t sz = status_or_long.value();
sapi::v::Array<unsigned char> buffer_(sz);
status_or_long = api.TIFFReadEncodedTile(&tif, 9, buffer_.PtrBoth(), sz);
if (!status_or_long.ok() || status_or_long.value() != sz) {
std::cerr << "Did not get expected result code from"
<< "TIFFReadEncodedTile(): (" << status_or_long.value()
<< " instead of " << sz << ")\n";
return 1;
}
if (CheckCluster(0, buffer_, cluster_0) ||
CheckCluster(64, buffer_, cluster_64) ||
CheckCluster(128, buffer_, cluster_128)) {
return 1;
}
status_or_int =
api.TIFFSetFieldU1(&tif, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
if (!status_or_int.ok() || !status_or_int.value()) {
std::cerr << "TIFFSetFieldU1 not available\n";
}
status_or_long = api.TIFFTileSize(&tif);
if (!status_or_long.ok() || status_or_long.value() != 128 * 128 * 3) {
std::cerr << "tiles are " << status_or_long.value() << " bytes\n";
return 1;
}
sz = status_or_long.value();
sapi::v::Array<unsigned char> buffer2_(sz);
status_or_long = api.TIFFReadEncodedTile(&tif, 9, buffer2_.PtrBoth(), sz);
if (!status_or_long.ok() || status_or_long.value() != sz) {
std::cerr << "Did not get expected result code from "
<< "TIFFReadEncodedTile(): (" << status_or_long.value()
<< " instead of " << sz << ")\n";
return 1;
}
unsigned pixel_status = 0;
pixel_status |= CheckRgbPixel(0, 15, 18, 0, 0, 18, 41, buffer2_);
pixel_status |= CheckRgbPixel(64, 0, 0, 0, 0, 0, 2, buffer2_);
pixel_status |= CheckRgbPixel(512, 5, 6, 34, 36, 182, 196, buffer2_);
if (!api.TIFFClose(&tif).ok()) {
std::cerr << "TIFFClose error\n";
}
status_or_tif = api.TIFFOpen(srcfile_var.PtrBefore(), r_var.PtrBefore());
if (!status_or_tif.ok()) {
std::cerr << "Could not reopen " << srcfile << "\n";
return 1;
}
sapi::v::RemotePtr tif2(status_or_tif.value());
if (!tif2.GetValue()) { // tif is NULL
std::cerr << "Could not reopen " << srcfile << "\n";
return 1;
}
sapi::v::Array<uint32> rgba_buffer_(128 * 128);
status_or_int =
api.TIFFReadRGBATile(&tif2, 1 * 128, 2 * 128, rgba_buffer_.PtrBoth());
if (!status_or_int.ok() || !status_or_int.value()) {
fprintf(stderr, "TIFFReadRGBATile() returned failure code.\n");
return 1;
}
pixel_status |=
CheckRgbaPixel(0, 15, 18, 0, 0, 18, 41, 255, 255, rgba_buffer_);
pixel_status |= CheckRgbaPixel(64, 0, 0, 0, 0, 0, 2, 255, 255, rgba_buffer_);
pixel_status |=
CheckRgbaPixel(512, 5, 6, 34, 36, 182, 196, 255, 255, rgba_buffer_);
if (!api.TIFFClose(&tif2).ok()) {
std::cerr << "TIFFClose erro\n";
}
if (pixel_status) {
std::cerr << "pixel_status is true, expected false";
return 1;
}
return 0;
} }

View File

@ -41,6 +41,7 @@ class TiffSapiSandbox : public TiffSandbox {
.AllowOpen() .AllowOpen()
.AllowExit() .AllowExit()
.AllowStat() .AllowStat()
.AllowMmap()
.AllowSystemMalloc() .AllowSystemMalloc()
.AllowSyscalls({ .AllowSyscalls({
__NR_futex, __NR_futex,
@ -48,7 +49,6 @@ class TiffSapiSandbox : public TiffSandbox {
__NR_lseek, __NR_lseek,
__NR_gettid, __NR_gettid,
__NR_sysinfo, __NR_sysinfo,
__NR_mmap,
__NR_munmap, __NR_munmap,
}); });
@ -63,7 +63,8 @@ class TiffSapiSandbox : public TiffSandbox {
return builder.get()->BuildOrDie(); return builder.get()->BuildOrDie();
} }
std::string dir_, file_; std::string dir_;
std::string file_;
}; };
} // namespace } // namespace

View File

@ -14,12 +14,11 @@
#include "check_tag.h" #include "check_tag.h"
/* sapi functions: // sapi functions:
* TIFFGetField // TIFFGetField
*/
void CheckShortField(TiffApi& api, sapi::v::RemotePtr& tif, const ttag_t field, void CheckShortField(TiffApi& api, sapi::v::RemotePtr& tif, const ttag_t field,
const unsigned short value) { const uint16_t value) {
sapi::v::UShort tmp(123); sapi::v::UShort tmp(123);
sapi::StatusOr<int> status_or_int; sapi::StatusOr<int> status_or_int;
@ -33,7 +32,7 @@ void CheckShortField(TiffApi& api, sapi::v::RemotePtr& tif, const ttag_t field,
void CheckShortPairedField(TiffApi& api, sapi::v::RemotePtr& tif, void CheckShortPairedField(TiffApi& api, sapi::v::RemotePtr& tif,
const ttag_t field, const ttag_t field,
const std::array<unsigned short, 2>& values) { const std::array<uint16_t, 2>& values) {
sapi::v::UShort tmp0(123); sapi::v::UShort tmp0(123);
sapi::v::UShort tmp1(456); sapi::v::UShort tmp1(456);
sapi::StatusOr<int> status_or_int; sapi::StatusOr<int> status_or_int;

View File

@ -12,13 +12,15 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include <cstring>
#include "helper.h" #include "helper.h"
#include "tiffio.h" #include "tiffio.h"
void CheckShortField(TiffApi&, sapi::v::RemotePtr& tif, const ttag_t field, void CheckShortField(TiffApi&, sapi::v::RemotePtr& tif, const ttag_t field,
const unsigned short value); const uint16_t value);
void CheckShortPairedField(TiffApi& api, sapi::v::RemotePtr& tif, void CheckShortPairedField(TiffApi& api, sapi::v::RemotePtr& tif,
const ttag_t field, const ttag_t field,
const std::array<unsigned short, 2>& values); const std::array<uint16_t, 2>& values);
void CheckLongField(TiffApi&, sapi::v::RemotePtr& tif, const ttag_t field, void CheckLongField(TiffApi&, sapi::v::RemotePtr& tif, const ttag_t field,
const unsigned value); const unsigned value);

View File

@ -12,33 +12,36 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include <cstdint>
#include "helper.h" #include "helper.h"
#include "tiffio.h" #include "tiffio.h"
/* sapi functions: // sapi functions:
* TIFFOpen // TIFFOpen
* TIFFClose // TIFFClose
* TIFFGetField // TIFFGetField
* TIFFSetField // TIFFSetField
* TIFFWriteCheck // TIFFWriteCheck
* TIFFSetDirectory // TIFFSetDirectory
* TIFFFreeDirectory // TIFFFreeDirectory
* TIFFWriteScanline // TIFFWriteScanline
* TIFFWriteDirectory // TIFFWriteDirectory
* TIFFCreateDirectory // TIFFCreateDirectory
* TIFFReadEncodedTile // TIFFReadEncodedTile
* TIFFReadEncodedStrip // TIFFReadEncodedStrip
* TIFFWriteEncodedTile // TIFFWriteEncodedTile
* TIFFWriteEncodedStrip // TIFFWriteEncodedStrip
* TIFFDeferStrileArrayWriting // TIFFDeferStrileArrayWriting
* TIFFForceStrileArrayWriting // TIFFForceStrileArrayWriting
*/
namespace {
#define TBS 256 // kTileBufferSize #define TBS 256 // kTileBufferSize
static const unsigned short kWidth = 1; constexpr uint16_t kWidth = 1;
static const unsigned short kBps = 8; constexpr uint16_t kBps = 8;
static const unsigned short kRowsPerStrip = 1; constexpr uint16_t kRowsPerStrip = 1;
static const unsigned short kSamplePerPixel = 1; constexpr uint16_t kSamplePerPixel = 1;
void TestWriting(const char* mode, int tiled, int height) { void TestWriting(const char* mode, int tiled, int height) {
sapi::StatusOr<std::string> status_or_path = sapi::StatusOr<std::string> status_or_path =
@ -320,3 +323,5 @@ TEST(SandboxTest, DeferStrileWriting) {
TestWriting("wD", tiled, 1); TestWriting("wD", tiled, 1);
} }
} }
} // namespace

View File

@ -16,16 +16,10 @@
#include <iostream> #include <iostream>
std::string inDir; static auto* g_in_dir = new std::string();
std::string GetCWD() {
char cwd[PATH_MAX];
getcwd(cwd, sizeof(cwd));
return cwd;
}
std::string GetImagesDir() { std::string GetImagesDir() {
std::string cwd = GetCWD(); std::string cwd = sandbox2::file_util::fileops::GetCWD();
auto find = cwd.rfind("/build"); auto find = cwd.rfind("/build");
if (find == std::string::npos) { if (find == std::string::npos) {
std::cerr << "Something went wrong: CWD don't contain build dir. " std::cerr << "Something went wrong: CWD don't contain build dir. "
@ -38,8 +32,8 @@ std::string GetImagesDir() {
} }
std::string GetFilePath(const std::string& filename) { std::string GetFilePath(const std::string& filename) {
if (inDir.empty()) { if (g_in_dir->empty()) {
inDir = GetImagesDir(); *g_in_dir = GetImagesDir();
} }
return sandbox2::file::JoinPath(inDir, filename); return sandbox2::file::JoinPath(*g_in_dir, filename);
} }

View File

@ -18,13 +18,14 @@
#include "check_tag.h" #include "check_tag.h"
#include "tiffio.h" #include "tiffio.h"
/* sapi functions: // sapi functions:
* TIFFWriteScanline // TIFFWriteScanline
* TIFFOpen // TIFFOpen
* TIFFClose // TIFFClose
* TIFFGetField (from check_tag.c) // TIFFGetField (from check_tag.c)
* TIFFSetField // TIFFSetField
*/
namespace {
struct LongTag { struct LongTag {
ttag_t tag; ttag_t tag;
@ -32,14 +33,13 @@ struct LongTag {
unsigned value; unsigned value;
}; };
const std::vector<LongTag> long_tags = { constexpr std::array<LongTag, 1> kLongTags = {
{TIFFTAG_SUBFILETYPE, 1, {TIFFTAG_SUBFILETYPE, 1, FILETYPE_REDUCEDIMAGE | FILETYPE_PAGE | FILETYPE_MASK}};
FILETYPE_REDUCEDIMAGE | FILETYPE_PAGE | FILETYPE_MASK}};
#define SPP 3 // kSamplePerPixel #define SPP 3 // kSamplePerPixel
static const unsigned kWidth = 1; constexpr unsigned kWidth = 1;
static const unsigned kLength = 1; constexpr unsigned kLength = 1;
static const unsigned kBps = 8; constexpr unsigned kBps = 8;
static const unsigned kRowsPerStrip = 1; constexpr unsigned kRowsPerStrip = 1;
TEST(SandboxTest, LongTag) { TEST(SandboxTest, LongTag) {
sapi::StatusOr<std::string> status_or_path = sapi::StatusOr<std::string> status_or_path =
@ -52,8 +52,8 @@ TEST(SandboxTest, LongTag) {
TiffSapiSandbox sandbox("", srcfile); TiffSapiSandbox sandbox("", srcfile);
ASSERT_THAT(sandbox.Init(), IsOk()) << "Couldn't initialize Sandboxed API"; ASSERT_THAT(sandbox.Init(), IsOk()) << "Couldn't initialize Sandboxed API";
std::array<unsigned char, SPP> buffer = {0, 127, 255}; std::array<uint8_t, SPP> buffer = {0, 127, 255};
sapi::v::Array<unsigned char> buffer_(buffer.data(), SPP); sapi::v::Array<uint8_t> buffer_(buffer.data(), SPP);
sapi::StatusOr<int> status_or_int; sapi::StatusOr<int> status_or_int;
sapi::StatusOr<TIFF*> status_or_tif; sapi::StatusOr<TIFF*> status_or_tif;
@ -102,7 +102,7 @@ TEST(SandboxTest, LongTag) {
EXPECT_THAT(status_or_int.value(), IsTrue()) EXPECT_THAT(status_or_int.value(), IsTrue())
<< "Can't set PhotometricInterpretation tag"; << "Can't set PhotometricInterpretation tag";
for (auto& tag : long_tags) { for (auto& tag : kLongTags) {
status_or_int = api.TIFFSetFieldU1(&tif, tag.tag, tag.value); status_or_int = api.TIFFSetFieldU1(&tif, tag.tag, tag.value);
ASSERT_THAT(status_or_int, IsOk()) << "TIFFSetFieldUShort1 fatal error"; ASSERT_THAT(status_or_int, IsOk()) << "TIFFSetFieldUShort1 fatal error";
EXPECT_THAT(status_or_int.value(), IsTrue()) << "Can't set tag " << tag.tag; EXPECT_THAT(status_or_int.value(), IsTrue()) << "Can't set tag " << tag.tag;
@ -126,10 +126,12 @@ TEST(SandboxTest, LongTag) {
CheckLongField(api, tif2, TIFFTAG_IMAGELENGTH, kLength); CheckLongField(api, tif2, TIFFTAG_IMAGELENGTH, kLength);
CheckLongField(api, tif2, TIFFTAG_ROWSPERSTRIP, kRowsPerStrip); CheckLongField(api, tif2, TIFFTAG_ROWSPERSTRIP, kRowsPerStrip);
for (auto& tag : long_tags) { for (auto& tag : kLongTags) {
CheckLongField(api, tif2, tag.tag, tag.value); CheckLongField(api, tif2, tag.tag, tag.value);
} }
ASSERT_THAT(api.TIFFClose(&tif2), IsOk()) << "TIFFClose fatal error"; ASSERT_THAT(api.TIFFClose(&tif2), IsOk()) << "TIFFClose fatal error";
unlink(srcfile.c_str()); unlink(srcfile.c_str());
} }
} // namespace

View File

@ -12,30 +12,31 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include <array>
#include <cstring> #include <cstring>
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "helper.h" #include "helper.h"
#include "tiffio.h" #include "tiffio.h"
/* sapi functions: // sapi functions:
* TIFFOpen // TIFFOpen
* TIFFClose // TIFFClose
* TIFFGetField // TIFFGetField
* TIFFSetField // TIFFSetField
* TIFFTileSize // TIFFTileSize
* TIFFReadRGBATile // TIFFReadRGBATile
* TIFFReadEncodedTile // TIFFReadEncodedTile
*/
static const std::vector<unsigned char> cluster_0 = {0, 0, 2, 0, 138, 139}; namespace {
static const std::vector<unsigned char> cluster_64 = {0, 0, 9, 6, 134, 119};
static const std::vector<unsigned char> cluster_128 = {44, 40, 63, 59, 230, 95};
static int CheckCluster(int cluster, constexpr std::array<uint8_t, 6> kCluster0 = {0, 0, 2, 0, 138, 139};
const sapi::v::Array<unsigned char> &buffer, constexpr std::array<uint8_t, 6> kCluster64 = {0, 0, 9, 6, 134, 119};
const std::vector<unsigned char> &expected_cluster) { constexpr std::array<uint8_t, 6> kCluster128 = {44, 40, 63, 59, 230, 95};
unsigned char* target = buffer.GetData() + cluster * 6;
int CheckCluster(int cluster, const sapi::v::Array<uint8_t> &buffer,
const std::array<uint8_t, 6> &expected_cluster) {
uint8_t* target = buffer.GetData() + cluster * 6;
bool comp = !(std::memcmp(target, expected_cluster.data(), 6) == 0); bool comp = !(std::memcmp(target, expected_cluster.data(), 6) == 0);
@ -50,10 +51,10 @@ static int CheckCluster(int cluster,
return comp; return comp;
} }
static int CheckRgbPixel(int pixel, int min_red, int max_red, int min_green, int CheckRgbPixel(int pixel, int min_red, int max_red, int min_green,
int max_green, int min_blue, int max_blue, int max_green, int min_blue, int max_blue,
const sapi::v::Array<unsigned char> &buffer) { const sapi::v::Array<uint8_t> &buffer) {
unsigned char* rgb = buffer.GetData() + 3 * pixel; uint8_t* rgb = buffer.GetData() + 3 * pixel;
bool comp = bool comp =
!(rgb[0] >= min_red && rgb[0] <= max_red && rgb[1] >= min_green && !(rgb[0] >= min_red && rgb[0] <= max_red && rgb[1] >= min_green &&
@ -68,10 +69,9 @@ static int CheckRgbPixel(int pixel, int min_red, int max_red, int min_green,
return comp; return comp;
} }
static int CheckRgbaPixel(int pixel, int min_red, int max_red, int min_green, int CheckRgbaPixel(int pixel, int min_red, int max_red, int min_green,
int max_green, int min_blue, int max_blue, int max_green, int min_blue, int max_blue, int min_alpha,
int min_alpha, int max_alpha, int max_alpha, const sapi::v::Array<unsigned> &buffer) {
const sapi::v::Array<unsigned> &buffer) {
// RGBA images are upside down - adjust for normal ordering // RGBA images are upside down - adjust for normal ordering
int adjusted_pixel = pixel % 128 + (127 - (pixel / 128)) * 128; int adjusted_pixel = pixel % 128 + (127 - (pixel / 128)) * 128;
unsigned rgba = buffer[adjusted_pixel]; unsigned rgba = buffer[adjusted_pixel];
@ -133,16 +133,16 @@ TEST(SandboxTest, RawDecode) {
<< "tiles are " << status_or_long.value() << " bytes"; << "tiles are " << status_or_long.value() << " bytes";
sz = status_or_long.value(); sz = status_or_long.value();
sapi::v::Array<unsigned char> buffer_(sz); sapi::v::Array<uint8_t> buffer_(sz);
status_or_long = api.TIFFReadEncodedTile(&tif, 9, buffer_.PtrBoth(), sz); status_or_long = api.TIFFReadEncodedTile(&tif, 9, buffer_.PtrBoth(), sz);
ASSERT_THAT(status_or_long, IsOk()) << "TIFFReadEncodedTile fatal error"; ASSERT_THAT(status_or_long, IsOk()) << "TIFFReadEncodedTile fatal error";
EXPECT_THAT(status_or_long.value(), Eq(sz)) EXPECT_THAT(status_or_long.value(), Eq(sz))
<< "Did not get expected result code from TIFFReadEncodedTile()(" << "Did not get expected result code from TIFFReadEncodedTile()("
<< (int)status_or_long.value() << " instead of " << (int)sz << ")"; << (int)status_or_long.value() << " instead of " << (int)sz << ")";
ASSERT_FALSE(CheckCluster(0, buffer_, cluster_0) || ASSERT_FALSE(CheckCluster(0, buffer_, kCluster0) ||
CheckCluster(64, buffer_, cluster_64) || CheckCluster(64, buffer_, kCluster64) ||
CheckCluster(128, buffer_, cluster_128)) CheckCluster(128, buffer_, kCluster128))
<< "Clusters did not match expected results"; << "Clusters did not match expected results";
status_or_int = status_or_int =
@ -157,7 +157,7 @@ TEST(SandboxTest, RawDecode) {
<< "tiles are " << status_or_long.value() << " bytes"; << "tiles are " << status_or_long.value() << " bytes";
sz = status_or_long.value(); sz = status_or_long.value();
sapi::v::Array<unsigned char> buffer2_(sz); sapi::v::Array<uint8_t> buffer2_(sz);
status_or_long = api.TIFFReadEncodedTile(&tif, 9, buffer2_.PtrBoth(), sz); status_or_long = api.TIFFReadEncodedTile(&tif, 9, buffer2_.PtrBoth(), sz);
ASSERT_THAT(status_or_long, IsOk()) << "TIFFReadEncodedTile fatal error"; ASSERT_THAT(status_or_long, IsOk()) << "TIFFReadEncodedTile fatal error";
EXPECT_THAT(status_or_long.value(), Eq(sz)) EXPECT_THAT(status_or_long.value(), Eq(sz))
@ -195,3 +195,5 @@ TEST(SandboxTest, RawDecode) {
EXPECT_THAT(pixel_status, IsFalse()) << "wrong encoding"; EXPECT_THAT(pixel_status, IsFalse()) << "wrong encoding";
} }
} // namespace

View File

@ -13,38 +13,40 @@
// limitations under the License. // limitations under the License.
#include <array> #include <array>
#include <cstdint>
#include <vector> #include <vector>
#include "check_tag.h" #include "check_tag.h"
#include "tiffio.h" #include "tiffio.h"
/* sapi functions: // sapi functions:
* TIFFWriteScanline // TIFFWriteScanline
* TIFFOpen // TIFFOpen
* TIFFClose // TIFFClose
* TIFFGetField (from check_tag.c) // TIFFGetField (from check_tag.c)
* TIFFSetField // TIFFSetField
*/
namespace {
#define SPP 3 // kSamplePerPixel #define SPP 3 // kSamplePerPixel
static const unsigned short kWidth = 1; constexpr uint16_t kWidth = 1;
static const unsigned short kLength = 1; constexpr uint16_t kLength = 1;
static const unsigned short kBps = 8; constexpr uint16_t kBps = 8;
static const unsigned short kPhotometric = PHOTOMETRIC_RGB; constexpr uint16_t kPhotometric = PHOTOMETRIC_RGB;
static const unsigned short kRowsPerStrip = 1; constexpr uint16_t kRowsPerStrip = 1;
static const unsigned short kPlanarConfig = PLANARCONFIG_CONTIG; constexpr uint16_t kPlanarConfig = PLANARCONFIG_CONTIG;
struct SingleTag { struct SingleTag {
const ttag_t tag; const ttag_t tag;
const unsigned short value; const uint16_t value;
}; };
struct PairedTag { struct PairedTag {
const ttag_t tag; const ttag_t tag;
const std::array<unsigned short, 2> values; const std::array<uint16_t, 2> values;
}; };
static const std::vector<SingleTag> short_single_tags = { constexpr std::array<SingleTag, 9> kShortSingleTags = {{
{TIFFTAG_COMPRESSION, COMPRESSION_NONE}, {TIFFTAG_COMPRESSION, COMPRESSION_NONE},
{TIFFTAG_FILLORDER, FILLORDER_MSB2LSB}, {TIFFTAG_FILLORDER, FILLORDER_MSB2LSB},
{TIFFTAG_ORIENTATION, ORIENTATION_BOTRIGHT}, {TIFFTAG_ORIENTATION, ORIENTATION_BOTRIGHT},
@ -53,13 +55,13 @@ static const std::vector<SingleTag> short_single_tags = {
{TIFFTAG_MAXSAMPLEVALUE, 241}, {TIFFTAG_MAXSAMPLEVALUE, 241},
{TIFFTAG_INKSET, INKSET_MULTIINK}, {TIFFTAG_INKSET, INKSET_MULTIINK},
{TIFFTAG_NUMBEROFINKS, SPP}, {TIFFTAG_NUMBEROFINKS, SPP},
{TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT}}; {TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT}}};
static const std::vector<PairedTag> short_paired_tags = { constexpr std::array<PairedTag, 4> kShortPairedTags = {{
{TIFFTAG_PAGENUMBER, {1, 1}}, {TIFFTAG_PAGENUMBER, {1, 1}},
{TIFFTAG_HALFTONEHINTS, {0, 255}}, {TIFFTAG_HALFTONEHINTS, {0, 255}},
{TIFFTAG_DOTRANGE, {8, 16}}, {TIFFTAG_DOTRANGE, {8, 16}},
{TIFFTAG_YCBCRSUBSAMPLING, {2, 1}}}; {TIFFTAG_YCBCRSUBSAMPLING, {2, 1}}}};
TEST(SandboxTest, ShortTag) { TEST(SandboxTest, ShortTag) {
sapi::StatusOr<std::string> status_or_path = sapi::StatusOr<std::string> status_or_path =
@ -72,8 +74,8 @@ TEST(SandboxTest, ShortTag) {
TiffSapiSandbox sandbox("", srcfile); TiffSapiSandbox sandbox("", srcfile);
ASSERT_THAT(sandbox.Init(), IsOk()) << "Couldn't initialize Sandboxed API"; ASSERT_THAT(sandbox.Init(), IsOk()) << "Couldn't initialize Sandboxed API";
std::array<unsigned char, SPP> buffer = {0, 127, 255}; std::array<uint8_t, SPP> buffer = {0, 127, 255};
sapi::v::Array<unsigned char> buffer_(buffer.data(), SPP); sapi::v::Array<uint8_t> buffer_(buffer.data(), SPP);
sapi::StatusOr<int> status_or_int; sapi::StatusOr<int> status_or_int;
sapi::StatusOr<TIFF*> status_or_tif; sapi::StatusOr<TIFF*> status_or_tif;
@ -123,13 +125,13 @@ TEST(SandboxTest, ShortTag) {
EXPECT_THAT(status_or_int.value(), IsTrue()) EXPECT_THAT(status_or_int.value(), IsTrue())
<< "Can't set PhotometricInterpretation tag"; << "Can't set PhotometricInterpretation tag";
for (auto& tag : short_single_tags) { for (auto& tag : kShortSingleTags) {
status_or_int = api.TIFFSetFieldUShort1(&tif, tag.tag, tag.value); status_or_int = api.TIFFSetFieldUShort1(&tif, tag.tag, tag.value);
ASSERT_THAT(status_or_int, IsOk()) << "TIFFSetFieldUShort1 fatal error"; ASSERT_THAT(status_or_int, IsOk()) << "TIFFSetFieldUShort1 fatal error";
EXPECT_THAT(status_or_int.value(), IsTrue()) << "Can't set tag " << tag.tag; EXPECT_THAT(status_or_int.value(), IsTrue()) << "Can't set tag " << tag.tag;
} }
for (auto& tag : short_paired_tags) { for (auto& tag : kShortPairedTags) {
status_or_int = status_or_int =
api.TIFFSetFieldUShort2(&tif, tag.tag, tag.values[0], tag.values[1]); api.TIFFSetFieldUShort2(&tif, tag.tag, tag.values[0], tag.values[1]);
ASSERT_THAT(status_or_int, IsOk()) << "TIFFSetFieldUShort2 fatal error"; ASSERT_THAT(status_or_int, IsOk()) << "TIFFSetFieldUShort2 fatal error";
@ -158,13 +160,15 @@ TEST(SandboxTest, ShortTag) {
CheckLongField(api, tif2, TIFFTAG_ROWSPERSTRIP, kRowsPerStrip); CheckLongField(api, tif2, TIFFTAG_ROWSPERSTRIP, kRowsPerStrip);
CheckShortField(api, tif2, TIFFTAG_PLANARCONFIG, kPlanarConfig); CheckShortField(api, tif2, TIFFTAG_PLANARCONFIG, kPlanarConfig);
for (auto& tag : short_single_tags) { for (auto& tag : kShortSingleTags) {
CheckShortField(api, tif2, tag.tag, tag.value); CheckShortField(api, tif2, tag.tag, tag.value);
} }
for (auto& tag : short_paired_tags) { for (auto& tag : kShortPairedTags) {
CheckShortPairedField(api, tif2, tag.tag, tag.values); CheckShortPairedField(api, tif2, tag.tag, tag.values);
} }
ASSERT_THAT(api.TIFFClose(&tif2), IsOk()) << "TIFFClose fatal error"; ASSERT_THAT(api.TIFFClose(&tif2), IsOk()) << "TIFFClose fatal error";
} }
} // namespace

View File

@ -17,10 +17,9 @@
#include "tiffio.h" #include "tiffio.h"
/* s - signed // wrapper for variadic functions TIFFGetField and TIFFSetField
* u - unsigned // s - signed
* wrapper for variadic functions TIFFGetField and TIFFSetField // u - unsigned
*/
extern "C" { extern "C" {
int TIFFGetField1(TIFF* tif, unsigned tag, void* param); int TIFFGetField1(TIFF* tif, unsigned tag, void* param);