xref: /aosp_15_r20/external/sandboxed-api/contrib/libxls/test/libxls_test.cc (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1*ec63e07aSXin Li // Copyright 2022 Google LLC
2*ec63e07aSXin Li //
3*ec63e07aSXin Li // Licensed under the Apache License, Version 2.0 (the "License");
4*ec63e07aSXin Li // you may not use this file except in compliance with the License.
5*ec63e07aSXin Li // You may obtain a copy of the License at
6*ec63e07aSXin Li //
7*ec63e07aSXin Li //     https://www.apache.org/licenses/LICENSE-2.0
8*ec63e07aSXin Li //
9*ec63e07aSXin Li // Unless required by applicable law or agreed to in writing, software
10*ec63e07aSXin Li // distributed under the License is distributed on an "AS IS" BASIS,
11*ec63e07aSXin Li // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*ec63e07aSXin Li // See the License for the specific language governing permissions and
13*ec63e07aSXin Li // limitations under the License.
14*ec63e07aSXin Li 
15*ec63e07aSXin Li #include "contrib/libxls/sandboxed.h"
16*ec63e07aSXin Li #include "contrib/libxls/utils/utils_libxls.h"
17*ec63e07aSXin Li #undef FILE  // TODO(cblichmann): Artifact from generated header
18*ec63e07aSXin Li 
19*ec63e07aSXin Li #include "sandboxed_api/util/path.h"
20*ec63e07aSXin Li #include "sandboxed_api/util/status_matchers.h"
21*ec63e07aSXin Li 
22*ec63e07aSXin Li namespace {
23*ec63e07aSXin Li 
24*ec63e07aSXin Li using ::sapi::IsOk;
25*ec63e07aSXin Li using ::testing::Not;
26*ec63e07aSXin Li 
27*ec63e07aSXin Li struct Sheet {
28*ec63e07aSXin Li   int count_row;
29*ec63e07aSXin Li   int count_col;
30*ec63e07aSXin Li   double values[4][4];
31*ec63e07aSXin Li };
32*ec63e07aSXin Li 
33*ec63e07aSXin Li const struct TestCase {
34*ec63e07aSXin Li   std::string filename;
35*ec63e07aSXin Li   size_t sheet_count;
36*ec63e07aSXin Li   struct Sheet sheet[2];
37*ec63e07aSXin Li } kTestData[] = {
38*ec63e07aSXin Li     {
39*ec63e07aSXin Li         .filename = "t1.xls",
40*ec63e07aSXin Li         .sheet_count = 1,
41*ec63e07aSXin Li         .sheet =
42*ec63e07aSXin Li             {
43*ec63e07aSXin Li                 {.count_row = 4,
44*ec63e07aSXin Li                  .count_col = 2,
45*ec63e07aSXin Li                  .values =
46*ec63e07aSXin Li                      {
47*ec63e07aSXin Li                          {1, 2, 0, 0},
48*ec63e07aSXin Li                          {3, 4, 0, 0},
49*ec63e07aSXin Li                          {5, 6, 0, 0},
50*ec63e07aSXin Li                          {7, 8, 0, 0},
51*ec63e07aSXin Li                      }},
52*ec63e07aSXin Li             },
53*ec63e07aSXin Li     },
54*ec63e07aSXin Li     {
55*ec63e07aSXin Li         .filename = "t2.xls",
56*ec63e07aSXin Li         .sheet_count = 2,
57*ec63e07aSXin Li         .sheet =
58*ec63e07aSXin Li             {
59*ec63e07aSXin Li                 {.count_row = 2,
60*ec63e07aSXin Li                  .count_col = 3,
61*ec63e07aSXin Li                  .values =
62*ec63e07aSXin Li                      {
63*ec63e07aSXin Li                          {1, 2, 3, 0},
64*ec63e07aSXin Li                          {4, 5, 6, 0},
65*ec63e07aSXin Li                          {0, 0, 0, 0},
66*ec63e07aSXin Li                          {0, 0, 0, 0},
67*ec63e07aSXin Li                      }},
68*ec63e07aSXin Li                 {.count_row = 2,
69*ec63e07aSXin Li                  .count_col = 2,
70*ec63e07aSXin Li                  .values =
71*ec63e07aSXin Li                      {
72*ec63e07aSXin Li                          {9, 8, 0, 0},
73*ec63e07aSXin Li                          {7, 6, 0, 0},
74*ec63e07aSXin Li                          {0, 0, 0, 0},
75*ec63e07aSXin Li                          {0, 0, 0, 0},
76*ec63e07aSXin Li                      }},
77*ec63e07aSXin Li             },
78*ec63e07aSXin Li     },
79*ec63e07aSXin Li };
80*ec63e07aSXin Li 
81*ec63e07aSXin Li class LibXlsBase : public testing::Test {
82*ec63e07aSXin Li  protected:
GetTestFilePath(const std::string & filename)83*ec63e07aSXin Li   std::string GetTestFilePath(const std::string& filename) {
84*ec63e07aSXin Li     return sapi::file::JoinPath(test_dir_, filename);
85*ec63e07aSXin Li   }
86*ec63e07aSXin Li 
87*ec63e07aSXin Li   void SetUp() override;
88*ec63e07aSXin Li 
89*ec63e07aSXin Li   const char* test_dir_;
90*ec63e07aSXin Li };
91*ec63e07aSXin Li 
92*ec63e07aSXin Li class LibXlsTestFiles : public LibXlsBase,
93*ec63e07aSXin Li                         public testing::WithParamInterface<TestCase> {};
94*ec63e07aSXin Li 
SetUp()95*ec63e07aSXin Li void LibXlsBase::SetUp() {
96*ec63e07aSXin Li   test_dir_ = getenv("TEST_FILES_DIR");
97*ec63e07aSXin Li   ASSERT_NE(test_dir_, nullptr);
98*ec63e07aSXin Li }
99*ec63e07aSXin Li 
TEST_P(LibXlsTestFiles,TestValues)100*ec63e07aSXin Li TEST_P(LibXlsTestFiles, TestValues) {
101*ec63e07aSXin Li   const TestCase& tv = GetParam();
102*ec63e07aSXin Li   std::string test_file_path = GetTestFilePath(tv.filename);
103*ec63e07aSXin Li 
104*ec63e07aSXin Li   LibxlsSapiSandbox sandbox(test_file_path);
105*ec63e07aSXin Li   SAPI_ASSERT_OK(sandbox.Init());
106*ec63e07aSXin Li 
107*ec63e07aSXin Li   SAPI_ASSERT_OK_AND_ASSIGN(LibXlsWorkbook wb,
108*ec63e07aSXin Li                             LibXlsWorkbook::Open(&sandbox, test_file_path));
109*ec63e07aSXin Li   ASSERT_EQ(wb.GetSheetCount(), tv.sheet_count);
110*ec63e07aSXin Li 
111*ec63e07aSXin Li   for (int i = 0; i < tv.sheet_count; ++i) {
112*ec63e07aSXin Li     SAPI_ASSERT_OK_AND_ASSIGN(LibXlsSheet sheet, wb.OpenSheet(i));
113*ec63e07aSXin Li     ASSERT_EQ(sheet.GetRowCount(), tv.sheet[i].count_row);
114*ec63e07aSXin Li     ASSERT_EQ(sheet.GetColCount(), tv.sheet[i].count_col);
115*ec63e07aSXin Li     for (size_t row = 0; row < sheet.GetRowCount(); ++row) {
116*ec63e07aSXin Li       for (size_t col = 0; col < sheet.GetColCount(); ++col) {
117*ec63e07aSXin Li         SAPI_ASSERT_OK_AND_ASSIGN(LibXlsCell cell, sheet.GetCell(row, col));
118*ec63e07aSXin Li         ASSERT_EQ(cell.type, XLS_RECORD_NUMBER);
119*ec63e07aSXin Li         ASSERT_EQ(std::get<double>(cell.value), tv.sheet[i].values[row][col]);
120*ec63e07aSXin Li       }
121*ec63e07aSXin Li     }
122*ec63e07aSXin Li   }
123*ec63e07aSXin Li }
124*ec63e07aSXin Li 
125*ec63e07aSXin Li INSTANTIATE_TEST_SUITE_P(LibXlsBase, LibXlsTestFiles,
126*ec63e07aSXin Li                          testing::ValuesIn(kTestData));
127*ec63e07aSXin Li 
TEST_F(LibXlsBase,TestFormula)128*ec63e07aSXin Li TEST_F(LibXlsBase, TestFormula) {
129*ec63e07aSXin Li   std::string test_file_path = GetTestFilePath("t3.xls");
130*ec63e07aSXin Li 
131*ec63e07aSXin Li   LibxlsSapiSandbox sandbox(test_file_path);
132*ec63e07aSXin Li   SAPI_ASSERT_OK(sandbox.Init());
133*ec63e07aSXin Li 
134*ec63e07aSXin Li   SAPI_ASSERT_OK_AND_ASSIGN(LibXlsWorkbook wb,
135*ec63e07aSXin Li                             LibXlsWorkbook::Open(&sandbox, test_file_path));
136*ec63e07aSXin Li 
137*ec63e07aSXin Li   SAPI_ASSERT_OK_AND_ASSIGN(LibXlsSheet sheet, wb.OpenSheet(0));
138*ec63e07aSXin Li   SAPI_ASSERT_OK_AND_ASSIGN(LibXlsCell cell, sheet.GetCell(0, 0));
139*ec63e07aSXin Li   ASSERT_EQ(cell.type, XLS_RECORD_STRING);
140*ec63e07aSXin Li   ASSERT_EQ(std::get<std::string>(cell.value), "10.000000");
141*ec63e07aSXin Li }
142*ec63e07aSXin Li 
143*ec63e07aSXin Li }  // namespace
144