reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
//===- llvm/unittest/Support/TarWriterTest.cpp ----------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "llvm/Support/TarWriter.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h"
#include "gtest/gtest.h"
#include <vector>

using namespace llvm;
namespace {

struct UstarHeader {
  char Name[100];
  char Mode[8];
  char Uid[8];
  char Gid[8];
  char Size[12];
  char Mtime[12];
  char Checksum[8];
  char TypeFlag;
  char Linkname[100];
  char Magic[6];
  char Version[2];
  char Uname[32];
  char Gname[32];
  char DevMajor[8];
  char DevMinor[8];
  char Prefix[155];
  char Pad[12];
};

class TarWriterTest : public ::testing::Test {};

static std::vector<uint8_t> createTar(StringRef Base, StringRef Filename) {
  // Create a temporary file.
  SmallString<128> Path;
  std::error_code EC =
      sys::fs::createTemporaryFile("TarWriterTest", "tar", Path);
  EXPECT_FALSE((bool)EC);

  // Create a tar file.
  Expected<std::unique_ptr<TarWriter>> TarOrErr = TarWriter::create(Path, Base);
  EXPECT_TRUE((bool)TarOrErr);
  std::unique_ptr<TarWriter> Tar = std::move(*TarOrErr);
  Tar->append(Filename, "contents");
  Tar.reset();

  // Read the tar file.
  ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path);
  EXPECT_TRUE((bool)MBOrErr);
  std::unique_ptr<MemoryBuffer> MB = std::move(*MBOrErr);
  std::vector<uint8_t> Buf((const uint8_t *)MB->getBufferStart(),
                           (const uint8_t *)MB->getBufferEnd());

  // Windows does not allow us to remove a mmap'ed files, so
  // unmap first and then remove the temporary file.
  MB = nullptr;
  sys::fs::remove(Path);

  return Buf;
}

static UstarHeader createUstar(StringRef Base, StringRef Filename) {
  std::vector<uint8_t> Buf = createTar(Base, Filename);
  EXPECT_TRUE(Buf.size() >= sizeof(UstarHeader));
  return *reinterpret_cast<const UstarHeader *>(Buf.data());
}

TEST_F(TarWriterTest, Basics) {
  UstarHeader Hdr = createUstar("base", "file");
  EXPECT_EQ("ustar", StringRef(Hdr.Magic));
  EXPECT_EQ("00", StringRef(Hdr.Version, 2));
  EXPECT_EQ("base/file", StringRef(Hdr.Name));
  EXPECT_EQ("00000000010", StringRef(Hdr.Size));
}

TEST_F(TarWriterTest, LongFilename) {
  std::string x154(154, 'x');
  std::string x155(155, 'x');
  std::string y99(99, 'y');
  std::string y100(100, 'y');

  UstarHeader Hdr1 = createUstar("", x154 + "/" + y99);
  EXPECT_EQ("/" + x154, StringRef(Hdr1.Prefix));
  EXPECT_EQ(y99, StringRef(Hdr1.Name));

  UstarHeader Hdr2 = createUstar("", x155 + "/" + y99);
  EXPECT_EQ("", StringRef(Hdr2.Prefix));
  EXPECT_EQ("", StringRef(Hdr2.Name));

  UstarHeader Hdr3 = createUstar("", x154 + "/" + y100);
  EXPECT_EQ("", StringRef(Hdr3.Prefix));
  EXPECT_EQ("", StringRef(Hdr3.Name));

  UstarHeader Hdr4 = createUstar("", x155 + "/" + y100);
  EXPECT_EQ("", StringRef(Hdr4.Prefix));
  EXPECT_EQ("", StringRef(Hdr4.Name));

  std::string yz = "yyyyyyyyyyyyyyyyyyyy/zzzzzzzzzzzzzzzzzzzz";
  UstarHeader Hdr5 = createUstar("", x154 + "/" + yz);
  EXPECT_EQ("/" + x154, StringRef(Hdr5.Prefix));
  EXPECT_EQ(yz, StringRef(Hdr5.Name));
}

TEST_F(TarWriterTest, Pax) {
  std::vector<uint8_t> Buf = createTar("", std::string(200, 'x'));
  EXPECT_TRUE(Buf.size() >= 1024);

  auto *Hdr = reinterpret_cast<const UstarHeader *>(Buf.data());
  EXPECT_EQ("", StringRef(Hdr->Prefix));
  EXPECT_EQ("", StringRef(Hdr->Name));

  StringRef Pax = StringRef((char *)(Buf.data() + 512), 512);
  EXPECT_TRUE(Pax.startswith("211 path=/" + std::string(200, 'x')));
}

TEST_F(TarWriterTest, SingleFile) {
  SmallString<128> Path;
  std::error_code EC =
      sys::fs::createTemporaryFile("TarWriterTest", "tar", Path);
  EXPECT_FALSE((bool)EC);

  Expected<std::unique_ptr<TarWriter>> TarOrErr = TarWriter::create(Path, "");
  EXPECT_TRUE((bool)TarOrErr);
  std::unique_ptr<TarWriter> Tar = std::move(*TarOrErr);
  Tar->append("FooPath", "foo");
  Tar.reset();

  uint64_t TarSize;
  EC = sys::fs::file_size(Path, TarSize);
  EXPECT_FALSE((bool)EC);
  EXPECT_EQ(TarSize, 2048ULL);
}

TEST_F(TarWriterTest, NoDuplicate) {
  SmallString<128> Path;
  std::error_code EC =
      sys::fs::createTemporaryFile("TarWriterTest", "tar", Path);
  EXPECT_FALSE((bool)EC);

  Expected<std::unique_ptr<TarWriter>> TarOrErr = TarWriter::create(Path, "");
  EXPECT_TRUE((bool)TarOrErr);
  std::unique_ptr<TarWriter> Tar = std::move(*TarOrErr);
  Tar->append("FooPath", "foo");
  Tar->append("BarPath", "bar");
  Tar.reset();

  uint64_t TarSize;
  EC = sys::fs::file_size(Path, TarSize);
  EXPECT_FALSE((bool)EC);
  EXPECT_EQ(TarSize, 3072ULL);
}

TEST_F(TarWriterTest, Duplicate) {
  SmallString<128> Path;
  std::error_code EC =
      sys::fs::createTemporaryFile("TarWriterTest", "tar", Path);
  EXPECT_FALSE((bool)EC);

  Expected<std::unique_ptr<TarWriter>> TarOrErr = TarWriter::create(Path, "");
  EXPECT_TRUE((bool)TarOrErr);
  std::unique_ptr<TarWriter> Tar = std::move(*TarOrErr);
  Tar->append("FooPath", "foo");
  Tar->append("FooPath", "bar");
  Tar.reset();

  uint64_t TarSize;
  EC = sys::fs::file_size(Path, TarSize);
  EXPECT_FALSE((bool)EC);
  EXPECT_EQ(TarSize, 2048ULL);
}
} // namespace