kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/cxx/common/kzip_writer_c_api_test.cc (about)

     1  /*
     2   * Copyright 2018 The Kythe Authors. All rights reserved.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *   http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  #include "kythe/cxx/common/kzip_writer_c_api.h"
    18  
    19  #include <zip.h>
    20  
    21  #include <cstdint>
    22  #include <cstdlib>
    23  #include <functional>
    24  #include <memory>
    25  #include <string>
    26  #include <vector>
    27  
    28  #include "absl/strings/str_cat.h"
    29  #include "absl/strings/str_replace.h"
    30  #include "absl/strings/string_view.h"
    31  #include "absl/strings/strip.h"
    32  #include "gmock/gmock.h"
    33  #include "gtest/gtest.h"
    34  #include "kythe/cxx/common/libzip/error.h"
    35  #include "kythe/proto/analysis.pb.h"
    36  
    37  namespace kythe {
    38  namespace {
    39  using ::testing::ElementsAre;
    40  using ::testing::Values;
    41  
    42  absl::string_view TestTmpdir() {
    43    return absl::StripSuffix(std::getenv("TEST_TMPDIR"), "/");
    44  }
    45  
    46  std::string TestOutputFile(absl::string_view basename) {
    47    const auto* test_info = testing::UnitTest::GetInstance()->current_test_info();
    48    const auto filename =
    49        absl::StrReplaceAll(absl::StrCat(test_info->test_case_name(), "_",
    50                                         test_info->name(), "_", basename),
    51                            {{"/", "-"}});
    52    return absl::StrCat(TestTmpdir(), "/", filename);
    53  }
    54  
    55  TEST(KzipWriterCApiTest, CApiTest) {
    56    std::string dummy_file = TestOutputFile("dummy.kzip");
    57    int32_t status;
    58    std::unique_ptr<::KzipWriter, std::function<void(::KzipWriter*)>> w(
    59        KzipWriter_Create(dummy_file.c_str(), dummy_file.length(),
    60                          KZIP_WRITER_ENCODING_PROTO, &status),
    61        [](::KzipWriter* w) {
    62          if (w != nullptr) {
    63            KzipWriter_Delete(w);
    64          }
    65        });
    66    ASSERT_EQ(0, status);
    67    {
    68      std::string contents("contents");
    69      char digest[200];
    70      size_t length;
    71      int32_t status =
    72          KzipWriter_WriteFile(w.get(), contents.c_str(), contents.length(),
    73                               digest, sizeof(digest), &length);
    74      ASSERT_EQ(0, status);
    75    }
    76    {
    77      std::string proto_bytes;
    78      kythe::proto::IndexedCompilation unit;
    79      ASSERT_TRUE(unit.SerializeToString(&proto_bytes));
    80      char digest[800];
    81      size_t length;
    82      int32_t status =
    83          KzipWriter_WriteUnit(w.get(), proto_bytes.c_str(), proto_bytes.length(),
    84                               digest, sizeof(digest), &length);
    85      ASSERT_EQ(0, status);
    86    }
    87    {
    88      int32_t status = KzipWriter_Close(w.get());
    89      ASSERT_EQ(0, status);
    90    }
    91  
    92    std::vector<std::string> contents;
    93    {
    94      auto* archive = zip_open(dummy_file.c_str(), ZIP_RDONLY, nullptr);
    95      ASSERT_NE(archive, nullptr);
    96      struct Closer {
    97        ~Closer() { zip_discard(a); }
    98        zip_t* a;
    99      } closer{archive};
   100      for (int i = 0; i < zip_get_num_entries(archive, 0); ++i) {
   101        const char* name = zip_get_name(archive, i, 0);
   102        ASSERT_NE(archive, nullptr) << libzip::ToStatus(zip_get_error(archive));
   103        contents.push_back(name);
   104      }
   105    }
   106    EXPECT_THAT(
   107        contents,
   108        // Order matters here as "root/" must come first.
   109        // We don't really care about the rest of the entries, but it's easy
   110        // enough to fix the order of the subdirectories and minimally harmful.
   111        ElementsAre(
   112            "root/", "root/files/", "root/pbunits/",
   113            "root/files/"
   114            "d1b2a59fbea7e20077af9f91b27e95e865061b270be03ff539ab3b73587882e8",
   115            "root/pbunits/"
   116            "44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a"));
   117  }
   118  
   119  }  // namespace
   120  }  // namespace kythe