xref: /freebsd/contrib/libarchive/libarchive/test/test_write_format_gnutar.c (revision 2e113ef82465598b8c26e0ca415fbe90677fbd47)
1 /*-
2  * Copyright (c) 2010 Tim Kientzle
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 #include "test.h"
26 
27 static char buff2[64];
28 
29 /* Some names 1026 characters long */
30 static const char *longfilename = "abcdefghijklmnopqrstuvwxyz"
31     "12345678901234567890123456789012345678901234567890"
32     "12345678901234567890123456789012345678901234567890"
33     "12345678901234567890123456789012345678901234567890"
34     "12345678901234567890123456789012345678901234567890"
35     "12345678901234567890123456789012345678901234567890"
36     "12345678901234567890123456789012345678901234567890"
37     "12345678901234567890123456789012345678901234567890"
38     "12345678901234567890123456789012345678901234567890"
39     "12345678901234567890123456789012345678901234567890"
40     "12345678901234567890123456789012345678901234567890"
41     "12345678901234567890123456789012345678901234567890"
42     "12345678901234567890123456789012345678901234567890"
43     "12345678901234567890123456789012345678901234567890"
44     "12345678901234567890123456789012345678901234567890"
45     "12345678901234567890123456789012345678901234567890"
46     "12345678901234567890123456789012345678901234567890"
47     "12345678901234567890123456789012345678901234567890"
48     "12345678901234567890123456789012345678901234567890"
49     "12345678901234567890123456789012345678901234567890"
50     "12345678901234567890123456789012345678901234567890";
51 
52 static const char *longlinkname = "Xabcdefghijklmnopqrstuvwxyz"
53     "12345678901234567890123456789012345678901234567890"
54     "12345678901234567890123456789012345678901234567890"
55     "12345678901234567890123456789012345678901234567890"
56     "12345678901234567890123456789012345678901234567890"
57     "12345678901234567890123456789012345678901234567890"
58     "12345678901234567890123456789012345678901234567890"
59     "12345678901234567890123456789012345678901234567890"
60     "12345678901234567890123456789012345678901234567890"
61     "12345678901234567890123456789012345678901234567890"
62     "12345678901234567890123456789012345678901234567890"
63     "12345678901234567890123456789012345678901234567890"
64     "12345678901234567890123456789012345678901234567890"
65     "12345678901234567890123456789012345678901234567890"
66     "12345678901234567890123456789012345678901234567890"
67     "12345678901234567890123456789012345678901234567890"
68     "12345678901234567890123456789012345678901234567890"
69     "12345678901234567890123456789012345678901234567890"
70     "12345678901234567890123456789012345678901234567890"
71     "12345678901234567890123456789012345678901234567890"
72     "12345678901234567890123456789012345678901234567890";
73 
74 static const char *longhardlinkname = "Yabcdefghijklmnopqrstuvwxyz"
75     "12345678901234567890123456789012345678901234567890"
76     "12345678901234567890123456789012345678901234567890"
77     "12345678901234567890123456789012345678901234567890"
78     "12345678901234567890123456789012345678901234567890"
79     "12345678901234567890123456789012345678901234567890"
80     "12345678901234567890123456789012345678901234567890"
81     "12345678901234567890123456789012345678901234567890"
82     "12345678901234567890123456789012345678901234567890"
83     "12345678901234567890123456789012345678901234567890"
84     "12345678901234567890123456789012345678901234567890"
85     "12345678901234567890123456789012345678901234567890"
86     "12345678901234567890123456789012345678901234567890"
87     "12345678901234567890123456789012345678901234567890"
88     "12345678901234567890123456789012345678901234567890"
89     "12345678901234567890123456789012345678901234567890"
90     "12345678901234567890123456789012345678901234567890"
91     "12345678901234567890123456789012345678901234567890"
92     "12345678901234567890123456789012345678901234567890"
93     "12345678901234567890123456789012345678901234567890"
94     "12345678901234567890123456789012345678901234567890";
95 
96 static const char *longfilename_largeuid = "large_uid_gid---"
97     "12345678901234567890123456789012345678901234567890"
98     "12345678901234567890123456789012345678901234567890"
99     "12345678901234567890123456789012345678901234567890"
100     "12345678901234567890123456789012345678901234567890"
101     "12345678901234567890123456789012345678901234567890"
102     "12345678901234567890123456789012345678901234567890"
103     "12345678901234567890123456789012345678901234567890"
104     "12345678901234567890123456789012345678901234567890"
105     "12345678901234567890123456789012345678901234567890"
106     "12345678901234567890123456789012345678901234567890"
107     "12345678901234567890123456789012345678901234567890"
108     "12345678901234567890123456789012345678901234567890"
109     "12345678901234567890123456789012345678901234567890"
110     "12345678901234567890123456789012345678901234567890"
111     "12345678901234567890123456789012345678901234567890"
112     "12345678901234567890123456789012345678901234567890"
113     "12345678901234567890123456789012345678901234567890"
114     "12345678901234567890123456789012345678901234567890"
115     "12345678901234567890123456789012345678901234567890"
116     "12345678901234567890123456789012345678901234567890";
117 
118 
119 
DEFINE_TEST(test_write_format_gnutar)120 DEFINE_TEST(test_write_format_gnutar)
121 {
122 	size_t buffsize = 1000000;
123 	char *buff;
124 	struct archive_entry *ae;
125 	struct archive *a;
126 	size_t used;
127 
128 	buff = malloc(buffsize); /* million bytes of work area */
129 	assert(buff != NULL);
130 
131 	/* Create a new archive in memory. */
132 	assert((a = archive_write_new()) != NULL);
133 	assertA(0 == archive_write_set_format_gnutar(a));
134 	assertA(0 == archive_write_add_filter_none(a));
135 	assertA(0 == archive_write_open_memory(a, buff, buffsize, &used));
136 
137 	/*
138 	 * "file" has a bunch of attributes and 8 bytes of data.
139 	 */
140 	assert((ae = archive_entry_new()) != NULL);
141 	archive_entry_set_atime(ae, 2, 20);
142 	archive_entry_set_birthtime(ae, 3, 30);
143 	archive_entry_set_ctime(ae, 4, 40);
144 	archive_entry_set_mtime(ae, 5, 50);
145 	archive_entry_copy_pathname(ae, "file");
146 	archive_entry_set_mode(ae, S_IFREG | 0755);
147 	archive_entry_set_size(ae, 8);
148 	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
149 	archive_entry_free(ae);
150 	assertEqualIntA(a, 8, archive_write_data(a, "12345678", 9));
151 
152 	/*
153 	 * A file with a very long name
154 	 */
155 	assert((ae = archive_entry_new()) != NULL);
156 	archive_entry_copy_pathname(ae, longfilename);
157 	archive_entry_set_mode(ae, S_IFREG | 0755);
158 	archive_entry_set_size(ae, 8);
159 	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
160 	archive_entry_free(ae);
161 	assertEqualIntA(a, 8, archive_write_data(a, "abcdefgh", 9));
162 
163 	/*
164 	 * A hardlink to the above file.
165 	 */
166 	assert((ae = archive_entry_new()) != NULL);
167 	archive_entry_copy_pathname(ae, longhardlinkname);
168 	archive_entry_copy_hardlink(ae, longfilename);
169 	archive_entry_set_mode(ae, S_IFREG | 0755);
170 	archive_entry_set_size(ae, 8);
171 	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
172 	archive_entry_free(ae);
173 
174 	/*
175 	 * A symlink to the above file.
176 	 */
177 	assert((ae = archive_entry_new()) != NULL);
178 	archive_entry_copy_pathname(ae, longlinkname);
179 	archive_entry_copy_symlink(ae, longfilename);
180 	archive_entry_set_mode(ae, AE_IFLNK | 0755);
181 	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
182 	archive_entry_free(ae);
183 
184 	/*
185 	 * A file with large UID/GID that overflow octal encoding.
186 	 */
187 	assert((ae = archive_entry_new()) != NULL);
188 	archive_entry_copy_pathname(ae, longfilename_largeuid);
189 	archive_entry_set_mode(ae, S_IFREG | 0755);
190 	archive_entry_set_size(ae, 8);
191 	archive_entry_set_uid(ae, 123456789);
192 	archive_entry_set_gid(ae, 987654321);
193 	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
194 	archive_entry_free(ae);
195 	assertEqualIntA(a, 8, archive_write_data(a, "abcdefgh", 9));
196 
197 	/* TODO: support GNU tar sparse format and test it here. */
198 	/* See test_write_format_pax for an example of testing sparse files. */
199 
200 	/* Close out the archive. */
201 	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
202 	assertEqualIntA(a, ARCHIVE_OK, archive_write_free(a));
203 
204 	/*
205 	 * Some basic verification of the low-level format.
206 	 */
207 
208 	/* Verify GNU tar magic/version fields */
209 	assertEqualMem(buff + 257, "ustar  \0", 8);
210 
211 	assertEqualInt(16896, used);
212 
213 	/*
214 	 *
215 	 * Now, read the data back.
216 	 *
217 	 */
218 	assert((a = archive_read_new()) != NULL);
219 	assertEqualIntA(a, 0, archive_read_support_format_all(a));
220 	assertEqualIntA(a, 0, archive_read_support_filter_all(a));
221 	assertEqualIntA(a, 0, archive_read_open_memory(a, buff, used));
222 
223 	/*
224 	 * Read "file"
225 	 */
226 	assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
227 	assert(!archive_entry_atime_is_set(ae));
228 	assert(!archive_entry_birthtime_is_set(ae));
229 	assert(!archive_entry_ctime_is_set(ae));
230 	assertEqualInt(5, archive_entry_mtime(ae));
231 	assertEqualInt(0, archive_entry_mtime_nsec(ae));
232 	assertEqualString("file", archive_entry_pathname(ae));
233 	assertEqualInt(S_IFREG | 0755, archive_entry_mode(ae));
234 	assertEqualInt(8, archive_entry_size(ae));
235 	assertEqualIntA(a, 8, archive_read_data(a, buff2, 10));
236 	assertEqualMem(buff2, "12345678", 8);
237 
238 	/*
239 	 * Read file with very long name.
240 	 */
241 	assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
242 	assertEqualString(longfilename, archive_entry_pathname(ae));
243 	assertEqualInt(S_IFREG | 0755, archive_entry_mode(ae));
244 	assertEqualInt(8, archive_entry_size(ae));
245 	assertEqualIntA(a, 8, archive_read_data(a, buff2, 10));
246 	assertEqualMem(buff2, "abcdefgh", 8);
247 
248 	/*
249 	 * Read hardlink.
250 	 */
251 	assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
252 	assertEqualString(longhardlinkname, archive_entry_pathname(ae));
253 	assertEqualString(longfilename, archive_entry_hardlink(ae));
254 
255 	/*
256 	 * Read symlink.
257 	 */
258 	assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
259 	assertEqualString(longlinkname, archive_entry_pathname(ae));
260 	assertEqualString(longfilename, archive_entry_symlink(ae));
261 	assertEqualInt(AE_IFLNK | 0755, archive_entry_mode(ae));
262 
263 	/*
264 	 * Read file with large UID/GID.
265 	 */
266 	assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
267 	assertEqualInt(123456789, archive_entry_uid(ae));
268 	assertEqualInt(987654321, archive_entry_gid(ae));
269 	assertEqualString(longfilename_largeuid, archive_entry_pathname(ae));
270 	assertEqualInt(S_IFREG | 0755, archive_entry_mode(ae));
271 
272 	/*
273 	 * Verify the end of the archive.
274 	 */
275 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
276 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
277 	assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
278 
279 	free(buff);
280 }
281