2017-01-09 23:55:00 +01:00
|
|
|
//===- llvm/unittest/Support/TarWriterTest.cpp ----------------------------===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// 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
|
2017-01-09 23:55:00 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-06-06 13:06:56 +02:00
|
|
|
#include "llvm/Support/TarWriter.h"
|
2017-01-09 23:55:00 +01:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2020-07-06 14:55:49 +02:00
|
|
|
#include "llvm/Testing/Support/SupportHelpers.h"
|
2017-01-09 23:55:00 +01:00
|
|
|
#include "gtest/gtest.h"
|
2017-09-27 23:38:02 +02:00
|
|
|
#include <vector>
|
2017-01-09 23:55:00 +01:00
|
|
|
|
|
|
|
using namespace llvm;
|
2020-07-06 14:55:49 +02:00
|
|
|
using llvm::unittest::TempFile;
|
|
|
|
|
2017-01-09 23:55:00 +01:00
|
|
|
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 {};
|
|
|
|
|
2017-09-27 23:38:02 +02:00
|
|
|
static std::vector<uint8_t> createTar(StringRef Base, StringRef Filename) {
|
2020-07-06 14:55:49 +02:00
|
|
|
TempFile TarWriterTest("TarWriterTest", "tar", "", /*Unique*/ true);
|
2017-01-09 23:55:00 +01:00
|
|
|
|
|
|
|
// Create a tar file.
|
2020-07-06 14:55:49 +02:00
|
|
|
Expected<std::unique_ptr<TarWriter>> TarOrErr =
|
|
|
|
TarWriter::create(TarWriterTest.path(), Base);
|
2017-01-09 23:55:00 +01:00
|
|
|
EXPECT_TRUE((bool)TarOrErr);
|
|
|
|
std::unique_ptr<TarWriter> Tar = std::move(*TarOrErr);
|
|
|
|
Tar->append(Filename, "contents");
|
2017-01-10 21:07:58 +01:00
|
|
|
Tar.reset();
|
2017-01-09 23:55:00 +01:00
|
|
|
|
|
|
|
// Read the tar file.
|
2020-07-06 14:55:49 +02:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
|
|
|
|
MemoryBuffer::getFile(TarWriterTest.path());
|
2017-01-09 23:55:00 +01:00
|
|
|
EXPECT_TRUE((bool)MBOrErr);
|
|
|
|
std::unique_ptr<MemoryBuffer> MB = std::move(*MBOrErr);
|
2017-09-27 23:38:02 +02:00
|
|
|
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;
|
|
|
|
|
|
|
|
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());
|
2017-01-09 23:55:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TarWriterTest, Basics) {
|
2017-09-27 23:38:02 +02:00
|
|
|
UstarHeader Hdr = createUstar("base", "file");
|
2017-01-11 23:02:51 +01:00
|
|
|
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));
|
2017-01-09 23:55:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TarWriterTest, LongFilename) {
|
2020-04-27 19:58:53 +02:00
|
|
|
// The prefix is prefixed by an additional '/' so it's one longer than the
|
|
|
|
// number of x's here.
|
2020-04-27 19:00:23 +02:00
|
|
|
std::string x136(136, 'x');
|
|
|
|
std::string x137(137, 'x');
|
2017-09-27 23:38:02 +02:00
|
|
|
std::string y99(99, 'y');
|
|
|
|
std::string y100(100, 'y');
|
|
|
|
|
2020-04-27 19:00:23 +02:00
|
|
|
UstarHeader Hdr1 = createUstar("", x136 + "/" + y99);
|
|
|
|
EXPECT_EQ("/" + x136, StringRef(Hdr1.Prefix));
|
2017-09-27 23:38:02 +02:00
|
|
|
EXPECT_EQ(y99, StringRef(Hdr1.Name));
|
|
|
|
|
2020-04-27 19:00:23 +02:00
|
|
|
UstarHeader Hdr2 = createUstar("", x137 + "/" + y99);
|
2017-09-27 23:38:02 +02:00
|
|
|
EXPECT_EQ("", StringRef(Hdr2.Prefix));
|
|
|
|
EXPECT_EQ("", StringRef(Hdr2.Name));
|
|
|
|
|
2020-04-27 19:00:23 +02:00
|
|
|
UstarHeader Hdr3 = createUstar("", x136 + "/" + y100);
|
2017-09-27 23:38:02 +02:00
|
|
|
EXPECT_EQ("", StringRef(Hdr3.Prefix));
|
|
|
|
EXPECT_EQ("", StringRef(Hdr3.Name));
|
|
|
|
|
2020-04-27 19:00:23 +02:00
|
|
|
UstarHeader Hdr4 = createUstar("", x137 + "/" + y100);
|
2017-09-27 23:38:02 +02:00
|
|
|
EXPECT_EQ("", StringRef(Hdr4.Prefix));
|
|
|
|
EXPECT_EQ("", StringRef(Hdr4.Name));
|
|
|
|
|
|
|
|
std::string yz = "yyyyyyyyyyyyyyyyyyyy/zzzzzzzzzzzzzzzzzzzz";
|
2020-04-27 19:00:23 +02:00
|
|
|
UstarHeader Hdr5 = createUstar("", x136 + "/" + yz);
|
|
|
|
EXPECT_EQ("/" + x136, StringRef(Hdr5.Prefix));
|
2017-09-27 23:38:02 +02:00
|
|
|
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')));
|
2017-01-09 23:55:00 +01:00
|
|
|
}
|
2017-12-05 11:09:59 +01:00
|
|
|
|
|
|
|
TEST_F(TarWriterTest, SingleFile) {
|
2020-07-06 14:55:49 +02:00
|
|
|
TempFile TarWriterTest("TarWriterTest", "tar", "", /*Unique*/ true);
|
2017-12-05 11:09:59 +01:00
|
|
|
|
2020-07-06 14:55:49 +02:00
|
|
|
Expected<std::unique_ptr<TarWriter>> TarOrErr =
|
|
|
|
TarWriter::create(TarWriterTest.path(), "");
|
2017-12-05 11:09:59 +01:00
|
|
|
EXPECT_TRUE((bool)TarOrErr);
|
|
|
|
std::unique_ptr<TarWriter> Tar = std::move(*TarOrErr);
|
|
|
|
Tar->append("FooPath", "foo");
|
|
|
|
Tar.reset();
|
|
|
|
|
|
|
|
uint64_t TarSize;
|
2020-07-06 14:55:49 +02:00
|
|
|
std::error_code EC = sys::fs::file_size(TarWriterTest.path(), TarSize);
|
2017-12-05 11:09:59 +01:00
|
|
|
EXPECT_FALSE((bool)EC);
|
2017-12-05 11:35:11 +01:00
|
|
|
EXPECT_EQ(TarSize, 2048ULL);
|
2017-12-05 11:09:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TarWriterTest, NoDuplicate) {
|
2020-07-06 14:55:49 +02:00
|
|
|
TempFile TarWriterTest("TarWriterTest", "tar", "", /*Unique*/ true);
|
2017-12-05 11:09:59 +01:00
|
|
|
|
2020-07-06 14:55:49 +02:00
|
|
|
Expected<std::unique_ptr<TarWriter>> TarOrErr =
|
|
|
|
TarWriter::create(TarWriterTest.path(), "");
|
2017-12-05 11:09:59 +01:00
|
|
|
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;
|
2020-07-06 14:55:49 +02:00
|
|
|
std::error_code EC = sys::fs::file_size(TarWriterTest.path(), TarSize);
|
2017-12-05 11:09:59 +01:00
|
|
|
EXPECT_FALSE((bool)EC);
|
2017-12-05 11:35:11 +01:00
|
|
|
EXPECT_EQ(TarSize, 3072ULL);
|
2017-12-05 11:09:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TarWriterTest, Duplicate) {
|
2020-07-06 14:55:49 +02:00
|
|
|
TempFile TarWriterTest("TarWriterTest", "tar", "", /*Unique*/ true);
|
2017-12-05 11:09:59 +01:00
|
|
|
|
2020-07-06 14:55:49 +02:00
|
|
|
Expected<std::unique_ptr<TarWriter>> TarOrErr =
|
|
|
|
TarWriter::create(TarWriterTest.path(), "");
|
2017-12-05 11:09:59 +01:00
|
|
|
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;
|
2020-07-06 14:55:49 +02:00
|
|
|
std::error_code EC = sys::fs::file_size(TarWriterTest.path(), TarSize);
|
2017-12-05 11:09:59 +01:00
|
|
|
EXPECT_FALSE((bool)EC);
|
2017-12-05 11:35:11 +01:00
|
|
|
EXPECT_EQ(TarSize, 2048ULL);
|
2017-01-09 23:55:00 +01:00
|
|
|
}
|
2017-12-05 11:09:59 +01:00
|
|
|
} // namespace
|