xref: /aosp_15_r20/external/sandboxed-api/oss-internship-2020/gdal/raster_to_gtiff/tests.cc (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
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 <optional>
16 #include <string>
17 
18 #include "gdal_sandbox.h"     // NOLINT(build/include)
19 #include "get_raster_data.h"  // NOLINT(build/include)
20 #include "gtiff_converter.h"  // NOLINT(build/include)
21 #include "gtest/gtest.h"
22 #include "sandboxed_api/testing.h"
23 #include "sandboxed_api/util/fileops.h"
24 #include "sandboxed_api/util/path.h"
25 #include "utils.h"  // NOLINT(build/include)
26 
27 namespace {
28 
29 inline constexpr absl::string_view kTempFilePrefix = "temp_data";
30 inline constexpr absl::string_view kFirstTestDataPath = "testdata/cea.tif";
31 inline constexpr absl::string_view kSecondTestDataPath =
32     "testdata/SP27GTIF.tif";
33 
34 }  // namespace
35 
36 class TestGTiffProcessor : public testing::TestWithParam<absl::string_view> {
37  public:
TestGTiffProcessor()38   TestGTiffProcessor() : tempfile_(sandbox2::GetTestTempPath()) {}
39 
40  protected:
41   const gdal::sandbox::utils::TempFile tempfile_;
42 };
43 
TEST_P(TestGTiffProcessor,TestProcessorOnGTiffData)44 TEST_P(TestGTiffProcessor, TestProcessorOnGTiffData) {
45   std::string file_path = gdal::sandbox::utils::GetTestDataPath(GetParam());
46 
47   ASSERT_TRUE(sandbox2::file_util::fileops::Exists(file_path, false))
48       << "Error finding input dataset";
49 
50   ASSERT_TRUE(tempfile_.HasValue()) << "Error creating temporary output file";
51 
52   gdal::sandbox::parser::RasterDataset original_bands_data =
53       gdal::sandbox::parser::GetRasterBandsFromFile(file_path);
54 
55   std::optional<std::string> proj_db_path =
56       gdal::sandbox::utils::FindProjDbPath();
57   ASSERT_TRUE(proj_db_path != std::nullopt)
58       << "Specified proj.db does not exist";
59 
60   gdal::sandbox::RasterToGTiffProcessor processor(
61       tempfile_.GetPath(), std::move(proj_db_path.value()),
62       original_bands_data);
63 
64   ASSERT_EQ(processor.Run(), absl::OkStatus())
65       << "Error creating new GTiff dataset inside sandbox";
66 
67   ASSERT_EQ(original_bands_data,
68             gdal::sandbox::parser::GetRasterBandsFromFile(tempfile_.GetPath()))
69       << "New dataset doesn't match the original one";
70 }
71 
72 INSTANTIATE_TEST_CASE_P(GDALTests, TestGTiffProcessor,
73                         ::testing::Values(kFirstTestDataPath,
74                                           kSecondTestDataPath));
75