diff --git a/oss-internship-2020/libtiff/example/CMakeLists.txt b/oss-internship-2020/libtiff/example/CMakeLists.txt index 045e0a9..f3dff36 100644 --- a/oss-internship-2020/libtiff/example/CMakeLists.txt +++ b/oss-internship-2020/libtiff/example/CMakeLists.txt @@ -15,6 +15,9 @@ add_executable(sandboxed main_sandboxed.cc ../sandboxed.h + ../test/data.h + ../test/helper.h + ../test/helper.cc ) target_link_libraries(sandboxed PRIVATE diff --git a/oss-internship-2020/libtiff/example/main_sandboxed.cc b/oss-internship-2020/libtiff/example/main_sandboxed.cc index ec29762..b9582bf 100644 --- a/oss-internship-2020/libtiff/example/main_sandboxed.cc +++ b/oss-internship-2020/libtiff/example/main_sandboxed.cc @@ -12,12 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include -#include #include -#include -#include "../sandboxed.h" // NOLINT(build/include) +#include "../sandboxed.h" // NOLINT(build/include) +#include "../test/data.h" // NOLINT(build/include) +#include "../test/helper.h" // NOLINT(build/include) #include "absl/algorithm/container.h" #include "absl/strings/str_join.h" #include "sandboxed_api/sandbox2/util/fileops.h" @@ -27,35 +26,6 @@ namespace { -struct ChannelLimits { - uint8_t min_red; - uint8_t max_red; - uint8_t min_green; - uint8_t max_green; - uint8_t min_blue; - uint8_t max_blue; - uint8_t min_alpha; - uint8_t max_alpha; -}; - -constexpr uint32_t kRawTileNumber = 9; -constexpr uint32_t kClusterSize = 6; -constexpr uint32_t kChannelsInPixel = 3; -constexpr uint32_t kTestCount = 3; -constexpr uint32_t kImageSize = 128 * 128; -constexpr uint32_t kClusterImageSize = 64 * 64; -using ClusterData = std::array; - -constexpr std::array, kTestCount> kClusters = { - {{0, {0, 0, 2, 0, 138, 139}}, - {64, {0, 0, 9, 6, 134, 119}}, - {128, {44, 40, 63, 59, 230, 95}}}}; - -constexpr std::array, kTestCount> kLimits = { - {{0, {15, 18, 0, 0, 18, 41, 255, 255}}, - {64, {0, 0, 0, 0, 0, 2, 255, 255}}, - {512, {5, 6, 34, 36, 182, 196, 255, 255}}}}; - constexpr absl::string_view kClusterErrorFormatStr = "Cluster %d did not match expected results.\n" "Expect:\t%s\n" @@ -141,30 +111,6 @@ absl::Status CheckRgbaPixel(uint32_t pixel, const ChannelLimits& limits, } // namespace -std::string GetFilePath(const absl::string_view dir, - const absl::string_view filename) { - return sandbox2::file::JoinPath(dir, "test", "images", filename); -} - -std::string GetFilePath(const absl::string_view filename) { - std::string cwd = sandbox2::file_util::fileops::GetCWD(); - auto find = cwd.rfind("build"); - - std::string project_path; - if (find == std::string::npos) { - LOG(ERROR) - << "Something went wrong: CWD don't contain build dir. " - << "Please run tests from build dir or send project dir as a " - << "parameter: ./sandboxed /absolute/path/to/project/dir .\n" - << "Falling back to using current working directory as root dir.\n"; - project_path = cwd; - } else { - project_path = cwd.substr(0, find); - } - - return sandbox2::file::JoinPath(project_path, "test", "images", filename); -} - absl::Status LibTIFFMain(const std::string& srcfile) { // to use dir and file inside sapi-libtiff, use // sandbox(file) – file only -- or diff --git a/oss-internship-2020/libtiff/test/CMakeLists.txt b/oss-internship-2020/libtiff/test/CMakeLists.txt index ffa4213..75d60c2 100644 --- a/oss-internship-2020/libtiff/test/CMakeLists.txt +++ b/oss-internship-2020/libtiff/test/CMakeLists.txt @@ -15,6 +15,7 @@ include(GoogleTest) add_executable(tests + ../test/data.h check_tag.h check_tag.cc defer_strile_writing.cc diff --git a/oss-internship-2020/libtiff/test/data.h b/oss-internship-2020/libtiff/test/data.h new file mode 100644 index 0000000..bd102f1 --- /dev/null +++ b/oss-internship-2020/libtiff/test/data.h @@ -0,0 +1,31 @@ +#include + +constexpr uint32_t kRawTileNumber = 9; +constexpr uint32_t kClusterSize = 6; +constexpr uint32_t kChannelsInPixel = 3; +constexpr uint32_t kTestCount = 3; +constexpr uint32_t kImageSize = 128 * 128; +constexpr uint32_t kClusterImageSize = 64 * 64; + +using ClusterData = std::array; + +struct ChannelLimits { + uint8_t min_red; + uint8_t max_red; + uint8_t min_green; + uint8_t max_green; + uint8_t min_blue; + uint8_t max_blue; + uint8_t min_alpha; + uint8_t max_alpha; +}; + +constexpr std::array, kTestCount> kClusters = { + {{0, {0, 0, 2, 0, 138, 139}}, + {64, {0, 0, 9, 6, 134, 119}}, + {128, {44, 40, 63, 59, 230, 95}}}}; + +constexpr std::array, kTestCount> kLimits = { + {{0, {15, 18, 0, 0, 18, 41, 255, 255}}, + {64, {0, 0, 0, 0, 0, 2, 255, 255}}, + {512, {5, 6, 34, 36, 182, 196, 255, 255}}}}; diff --git a/oss-internship-2020/libtiff/test/helper.cc b/oss-internship-2020/libtiff/test/helper.cc index 869cfa2..55e29d2 100644 --- a/oss-internship-2020/libtiff/test/helper.cc +++ b/oss-internship-2020/libtiff/test/helper.cc @@ -18,20 +18,24 @@ #include "sandboxed_api/sandbox2/util/fileops.h" #include "sandboxed_api/sandbox2/util/path.h" -std::string GetImagesFolder() { +std::string GetFilePath(const absl::string_view filename) { std::string cwd = sandbox2::file_util::fileops::GetCWD(); auto find = cwd.rfind("/build"); if (find == std::string::npos) { - LOG(ERROR) << "Something went wrong: CWD don't contain build dir. " - << "Please run tests from build dir, path might be incorrect\n"; + LOG(ERROR) + << "Something went wrong: CWD don't contain build dir. Please run " + "tests from build dir. To run example send project dir as a " + "parameter: ./sandboxed /absolute/path/to/project/dir .\n" + "Falling back to using current working directory as root dir.\n"; return sandbox2::file::JoinPath(cwd, "test", "images"); } - return sandbox2::file::JoinPath(cwd.substr(0, find), "test", "images"); + return sandbox2::file::JoinPath(cwd.substr(0, find), "test", "images", + filename); } -std::string GetFilePath(const std::string& filename) { - std::string images_folder_path = GetImagesFolder(); - return sandbox2::file::JoinPath(images_folder_path, filename); +std::string GetFilePath(const absl::string_view dir, + const absl::string_view filename) { + return sandbox2::file::JoinPath(dir, "test", "images", filename); } diff --git a/oss-internship-2020/libtiff/test/helper.h b/oss-internship-2020/libtiff/test/helper.h index b5e5afd..217a848 100644 --- a/oss-internship-2020/libtiff/test/helper.h +++ b/oss-internship-2020/libtiff/test/helper.h @@ -14,4 +14,8 @@ #include -std::string GetFilePath(const std::string& filename); +#include "absl/strings/str_join.h" + +std::string GetFilePath(const absl::string_view filename); +std::string GetFilePath(const absl::string_view dir, + const absl::string_view filename); diff --git a/oss-internship-2020/libtiff/test/raw_decode.cc b/oss-internship-2020/libtiff/test/raw_decode.cc index 09aee53..6388519 100644 --- a/oss-internship-2020/libtiff/test/raw_decode.cc +++ b/oss-internship-2020/libtiff/test/raw_decode.cc @@ -12,10 +12,10 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include #include #include "../sandboxed.h" // NOLINT(build/include) +#include "../test/data.h" // NOLINT(build/include) #include "absl/algorithm/container.h" #include "absl/strings/str_join.h" #include "gtest/gtest.h" @@ -35,35 +35,6 @@ using ::testing::IsTrue; using ::testing::Le; using ::testing::NotNull; -struct ChannelLimits { - uint8_t min_red; - uint8_t max_red; - uint8_t min_green; - uint8_t max_green; - uint8_t min_blue; - uint8_t max_blue; - uint8_t min_alpha; - uint8_t max_alpha; -}; - -constexpr uint32_t kRawTileNumber = 9; -constexpr uint32_t kClusterSize = 6; -constexpr uint32_t kChannelsInPixel = 3; -constexpr uint32_t kTestCount = 3; -constexpr uint32_t kImageSize = 128 * 128; -constexpr uint32_t kClusterImageSize = 64 * 64; -using ClusterData = std::array; - -constexpr std::array, kTestCount> kClusters = { - {{0, {0, 0, 2, 0, 138, 139}}, - {64, {0, 0, 9, 6, 134, 119}}, - {128, {44, 40, 63, 59, 230, 95}}}}; - -constexpr std::array, kTestCount> kLimits = { - {{0, {15, 18, 0, 0, 18, 41, 255, 255}}, - {64, {0, 0, 0, 0, 0, 2, 255, 255}}, - {512, {5, 6, 34, 36, 182, 196, 255, 255}}}}; - void CheckCluster(uint32_t cluster, const sapi::v::Array& buffer, const ClusterData& expected_cluster) { ASSERT_THAT(buffer.GetSize(), Ge((cluster + 1) * kClusterSize)) << "Overrun";