blob: 901dd906ca78ae1062d2f64e7646f800506459df [file] [log] [blame]
Rui Ueyamae9d17542017-01-09 22:55:00 +00001//===- llvm/unittest/Support/TarWriterTest.cpp ----------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Chandler Carruth9a67b072017-06-06 11:06:56 +000010#include "llvm/Support/TarWriter.h"
Rui Ueyamae9d17542017-01-09 22:55:00 +000011#include "llvm/Support/FileSystem.h"
12#include "llvm/Support/MemoryBuffer.h"
Rui Ueyamae9d17542017-01-09 22:55:00 +000013#include "gtest/gtest.h"
Rui Ueyama283f56a2017-09-27 21:38:02 +000014#include <vector>
Rui Ueyamae9d17542017-01-09 22:55:00 +000015
16using namespace llvm;
17namespace {
18
19struct UstarHeader {
20 char Name[100];
21 char Mode[8];
22 char Uid[8];
23 char Gid[8];
24 char Size[12];
25 char Mtime[12];
26 char Checksum[8];
27 char TypeFlag;
28 char Linkname[100];
29 char Magic[6];
30 char Version[2];
31 char Uname[32];
32 char Gname[32];
33 char DevMajor[8];
34 char DevMinor[8];
35 char Prefix[155];
36 char Pad[12];
37};
38
39class TarWriterTest : public ::testing::Test {};
40
Rui Ueyama283f56a2017-09-27 21:38:02 +000041static std::vector<uint8_t> createTar(StringRef Base, StringRef Filename) {
Rui Ueyamae9d17542017-01-09 22:55:00 +000042 // Create a temporary file.
43 SmallString<128> Path;
44 std::error_code EC =
45 sys::fs::createTemporaryFile("TarWriterTest", "tar", Path);
46 EXPECT_FALSE((bool)EC);
47
48 // Create a tar file.
49 Expected<std::unique_ptr<TarWriter>> TarOrErr = TarWriter::create(Path, Base);
50 EXPECT_TRUE((bool)TarOrErr);
51 std::unique_ptr<TarWriter> Tar = std::move(*TarOrErr);
52 Tar->append(Filename, "contents");
Rui Ueyama6a8520f2017-01-10 20:07:58 +000053 Tar.reset();
Rui Ueyamae9d17542017-01-09 22:55:00 +000054
55 // Read the tar file.
56 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path);
57 EXPECT_TRUE((bool)MBOrErr);
58 std::unique_ptr<MemoryBuffer> MB = std::move(*MBOrErr);
Rui Ueyama283f56a2017-09-27 21:38:02 +000059 std::vector<uint8_t> Buf((const uint8_t *)MB->getBufferStart(),
60 (const uint8_t *)MB->getBufferEnd());
61
62 // Windows does not allow us to remove a mmap'ed files, so
63 // unmap first and then remove the temporary file.
64 MB = nullptr;
Rui Ueyamae9d17542017-01-09 22:55:00 +000065 sys::fs::remove(Path);
Rui Ueyama283f56a2017-09-27 21:38:02 +000066
67 return Buf;
68}
69
70static UstarHeader createUstar(StringRef Base, StringRef Filename) {
71 std::vector<uint8_t> Buf = createTar(Base, Filename);
72 EXPECT_TRUE(Buf.size() >= sizeof(UstarHeader));
73 return *reinterpret_cast<const UstarHeader *>(Buf.data());
Rui Ueyamae9d17542017-01-09 22:55:00 +000074}
75
76TEST_F(TarWriterTest, Basics) {
Rui Ueyama283f56a2017-09-27 21:38:02 +000077 UstarHeader Hdr = createUstar("base", "file");
Rui Ueyamada710362017-01-11 22:02:51 +000078 EXPECT_EQ("ustar", StringRef(Hdr.Magic));
79 EXPECT_EQ("00", StringRef(Hdr.Version, 2));
80 EXPECT_EQ("base/file", StringRef(Hdr.Name));
81 EXPECT_EQ("00000000010", StringRef(Hdr.Size));
Rui Ueyamae9d17542017-01-09 22:55:00 +000082}
83
84TEST_F(TarWriterTest, LongFilename) {
Rui Ueyama283f56a2017-09-27 21:38:02 +000085 std::string x154(154, 'x');
86 std::string x155(155, 'x');
87 std::string y99(99, 'y');
88 std::string y100(100, 'y');
Rui Ueyamae9d17542017-01-09 22:55:00 +000089
Rui Ueyama283f56a2017-09-27 21:38:02 +000090 UstarHeader Hdr1 = createUstar("", x154 + "/" + y99);
91 EXPECT_EQ("/" + x154, StringRef(Hdr1.Prefix));
92 EXPECT_EQ(y99, StringRef(Hdr1.Name));
Rui Ueyamae9d17542017-01-09 22:55:00 +000093
Rui Ueyama283f56a2017-09-27 21:38:02 +000094 UstarHeader Hdr2 = createUstar("", x155 + "/" + y99);
95 EXPECT_EQ("", StringRef(Hdr2.Prefix));
96 EXPECT_EQ("", StringRef(Hdr2.Name));
97
98 UstarHeader Hdr3 = createUstar("", x154 + "/" + y100);
99 EXPECT_EQ("", StringRef(Hdr3.Prefix));
100 EXPECT_EQ("", StringRef(Hdr3.Name));
101
102 UstarHeader Hdr4 = createUstar("", x155 + "/" + y100);
103 EXPECT_EQ("", StringRef(Hdr4.Prefix));
104 EXPECT_EQ("", StringRef(Hdr4.Name));
105
106 std::string yz = "yyyyyyyyyyyyyyyyyyyy/zzzzzzzzzzzzzzzzzzzz";
107 UstarHeader Hdr5 = createUstar("", x154 + "/" + yz);
108 EXPECT_EQ("/" + x154, StringRef(Hdr5.Prefix));
109 EXPECT_EQ(yz, StringRef(Hdr5.Name));
110}
111
112TEST_F(TarWriterTest, Pax) {
113 std::vector<uint8_t> Buf = createTar("", std::string(200, 'x'));
114 EXPECT_TRUE(Buf.size() >= 1024);
115
116 auto *Hdr = reinterpret_cast<const UstarHeader *>(Buf.data());
117 EXPECT_EQ("", StringRef(Hdr->Prefix));
118 EXPECT_EQ("", StringRef(Hdr->Name));
119
120 StringRef Pax = StringRef((char *)(Buf.data() + 512), 512);
121 EXPECT_TRUE(Pax.startswith("211 path=/" + std::string(200, 'x')));
Rui Ueyamae9d17542017-01-09 22:55:00 +0000122}
George Rimarf91f0b02017-12-05 10:09:59 +0000123
124TEST_F(TarWriterTest, SingleFile) {
125 SmallString<128> Path;
126 std::error_code EC =
127 sys::fs::createTemporaryFile("TarWriterTest", "tar", Path);
128 EXPECT_FALSE((bool)EC);
129
130 Expected<std::unique_ptr<TarWriter>> TarOrErr = TarWriter::create(Path, "");
131 EXPECT_TRUE((bool)TarOrErr);
132 std::unique_ptr<TarWriter> Tar = std::move(*TarOrErr);
133 Tar->append("FooPath", "foo");
134 Tar.reset();
135
136 uint64_t TarSize;
137 EC = sys::fs::file_size(Path, TarSize);
138 EXPECT_FALSE((bool)EC);
George Rimar08f59862017-12-05 10:35:11 +0000139 EXPECT_EQ(TarSize, 2048ULL);
Rui Ueyamae9d17542017-01-09 22:55:00 +0000140}
George Rimarf91f0b02017-12-05 10:09:59 +0000141
142TEST_F(TarWriterTest, NoDuplicate) {
143 SmallString<128> Path;
144 std::error_code EC =
145 sys::fs::createTemporaryFile("TarWriterTest", "tar", Path);
146 EXPECT_FALSE((bool)EC);
147
148 Expected<std::unique_ptr<TarWriter>> TarOrErr = TarWriter::create(Path, "");
149 EXPECT_TRUE((bool)TarOrErr);
150 std::unique_ptr<TarWriter> Tar = std::move(*TarOrErr);
151 Tar->append("FooPath", "foo");
152 Tar->append("BarPath", "bar");
153 Tar.reset();
154
155 uint64_t TarSize;
156 EC = sys::fs::file_size(Path, TarSize);
157 EXPECT_FALSE((bool)EC);
George Rimar08f59862017-12-05 10:35:11 +0000158 EXPECT_EQ(TarSize, 3072ULL);
George Rimarf91f0b02017-12-05 10:09:59 +0000159}
160
161TEST_F(TarWriterTest, Duplicate) {
162 SmallString<128> Path;
163 std::error_code EC =
164 sys::fs::createTemporaryFile("TarWriterTest", "tar", Path);
165 EXPECT_FALSE((bool)EC);
166
167 Expected<std::unique_ptr<TarWriter>> TarOrErr = TarWriter::create(Path, "");
168 EXPECT_TRUE((bool)TarOrErr);
169 std::unique_ptr<TarWriter> Tar = std::move(*TarOrErr);
170 Tar->append("FooPath", "foo");
171 Tar->append("FooPath", "bar");
172 Tar.reset();
173
174 uint64_t TarSize;
175 EC = sys::fs::file_size(Path, TarSize);
176 EXPECT_FALSE((bool)EC);
George Rimar08f59862017-12-05 10:35:11 +0000177 EXPECT_EQ(TarSize, 2048ULL);
George Rimarf91f0b02017-12-05 10:09:59 +0000178}
179} // namespace