diff options
| author | 2018-04-06 14:26:03 +0000 | |
|---|---|---|
| committer | 2018-04-06 14:26:03 +0000 | |
| commit | bdabc2f19ffb9e20600dad6e8a300842a7bda50e (patch) | |
| tree | c50e7b2e5449b074651bb82a58517a8ebc4a8cf7 /gnu/llvm/unittests/Support/TarWriterTest.cpp | |
| parent | Print a 'p' flag for file descriptors that were opened after pledge(2). (diff) | |
| download | wireguard-openbsd-bdabc2f19ffb9e20600dad6e8a300842a7bda50e.tar.xz wireguard-openbsd-bdabc2f19ffb9e20600dad6e8a300842a7bda50e.zip | |
Import LLVM 6.0.1 release including clang, lld and lldb.
"where is the kaboom?" deraadt@
Diffstat (limited to 'gnu/llvm/unittests/Support/TarWriterTest.cpp')
| -rw-r--r-- | gnu/llvm/unittests/Support/TarWriterTest.cpp | 129 |
1 files changed, 110 insertions, 19 deletions
diff --git a/gnu/llvm/unittests/Support/TarWriterTest.cpp b/gnu/llvm/unittests/Support/TarWriterTest.cpp index 927c8ed9be1..901dd906ca7 100644 --- a/gnu/llvm/unittests/Support/TarWriterTest.cpp +++ b/gnu/llvm/unittests/Support/TarWriterTest.cpp @@ -11,6 +11,7 @@ #include "llvm/Support/FileSystem.h" #include "llvm/Support/MemoryBuffer.h" #include "gtest/gtest.h" +#include <vector> using namespace llvm; namespace { @@ -37,7 +38,7 @@ struct UstarHeader { class TarWriterTest : public ::testing::Test {}; -static UstarHeader create(StringRef Base, StringRef Filename) { +static std::vector<uint8_t> createTar(StringRef Base, StringRef Filename) { // Create a temporary file. SmallString<128> Path; std::error_code EC = @@ -55,12 +56,25 @@ static UstarHeader create(StringRef Base, StringRef Filename) { 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 *reinterpret_cast<const UstarHeader *>(MB->getBufferStart()); + + 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 = create("base", "file"); + 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)); @@ -68,21 +82,98 @@ TEST_F(TarWriterTest, Basics) { } TEST_F(TarWriterTest, LongFilename) { - UstarHeader Hdr1 = create( - "012345678", std::string(99, 'x') + "/" + std::string(44, 'x') + "/foo"); - EXPECT_EQ("foo", StringRef(Hdr1.Name)); - EXPECT_EQ("012345678/" + std::string(99, 'x') + "/" + std::string(44, 'x'), - StringRef(Hdr1.Prefix)); - - UstarHeader Hdr2 = create( - "012345678", std::string(99, 'x') + "/" + std::string(45, 'x') + "/foo"); - EXPECT_EQ("foo", StringRef(Hdr2.Name)); - EXPECT_EQ("012345678/" + std::string(99, 'x') + "/" + std::string(45, 'x'), - StringRef(Hdr2.Prefix)); - - UstarHeader Hdr3 = create( - "012345678", std::string(99, 'x') + "/" + std::string(46, 'x') + "/foo"); - EXPECT_EQ(std::string(46, 'x') + "/foo", StringRef(Hdr3.Name)); - EXPECT_EQ("012345678/" + std::string(99, 'x'), StringRef(Hdr3.Prefix)); + 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 |
