diff options
| author | 2017-01-24 08:32:59 +0000 | |
|---|---|---|
| committer | 2017-01-24 08:32:59 +0000 | |
| commit | 53d771aafdbe5b919f264f53cba3788e2c4cffd2 (patch) | |
| tree | 7eca39498be0ff1e3a6daf583cd9ca5886bb2636 /gnu/llvm/unittests/Support/TarWriterTest.cpp | |
| parent | In preparation of compiling our kernels with -ffreestanding, explicitly map (diff) | |
| download | wireguard-openbsd-53d771aafdbe5b919f264f53cba3788e2c4cffd2.tar.xz wireguard-openbsd-53d771aafdbe5b919f264f53cba3788e2c4cffd2.zip | |
Import LLVM 4.0.0 rc1 including clang and lld to help the current
development effort on OpenBSD/arm64.
Diffstat (limited to 'gnu/llvm/unittests/Support/TarWriterTest.cpp')
| -rw-r--r-- | gnu/llvm/unittests/Support/TarWriterTest.cpp | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/gnu/llvm/unittests/Support/TarWriterTest.cpp b/gnu/llvm/unittests/Support/TarWriterTest.cpp new file mode 100644 index 00000000000..84005de56c1 --- /dev/null +++ b/gnu/llvm/unittests/Support/TarWriterTest.cpp @@ -0,0 +1,88 @@ +//===- llvm/unittest/Support/TarWriterTest.cpp ----------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Support/FileSystem.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/TarWriter.h" +#include "gtest/gtest.h" + +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 UstarHeader create(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); + sys::fs::remove(Path); + return *reinterpret_cast<const UstarHeader *>(MB->getBufferStart()); +} + +TEST_F(TarWriterTest, Basics) { + UstarHeader Hdr = create("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) { + 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)); +} +} |
