xref: /freebsd/contrib/libarchive/libarchive/test/test_write_format_gnutar.c (revision b9128a37faafede823eb456aa65a11ac69997284)
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 
97 DEFINE_TEST(test_write_format_gnutar)
98 {
99 	size_t buffsize = 1000000;
100 	char *buff;
101 	struct archive_entry *ae;
102 	struct archive *a;
103 	size_t used;
104 
105 	buff = malloc(buffsize); /* million bytes of work area */
106 	assert(buff != NULL);
107 
108 	/* Create a new archive in memory. */
109 	assert((a = archive_write_new()) != NULL);
110 	assertA(0 == archive_write_set_format_gnutar(a));
111 	assertA(0 == archive_write_add_filter_none(a));
112 	assertA(0 == archive_write_open_memory(a, buff, buffsize, &used));
113 
114 	/*
115 	 * "file" has a bunch of attributes and 8 bytes of data.
116 	 */
117 	assert((ae = archive_entry_new()) != NULL);
118 	archive_entry_set_atime(ae, 2, 20);
119 	archive_entry_set_birthtime(ae, 3, 30);
120 	archive_entry_set_ctime(ae, 4, 40);
121 	archive_entry_set_mtime(ae, 5, 50);
122 	archive_entry_copy_pathname(ae, "file");
123 	archive_entry_set_mode(ae, S_IFREG | 0755);
124 	archive_entry_set_size(ae, 8);
125 	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
126 	archive_entry_free(ae);
127 	assertEqualIntA(a, 8, archive_write_data(a, "12345678", 9));
128 
129 	/*
130 	 * A file with a very long name
131 	 */
132 	assert((ae = archive_entry_new()) != NULL);
133 	archive_entry_copy_pathname(ae, longfilename);
134 	archive_entry_set_mode(ae, S_IFREG | 0755);
135 	archive_entry_set_size(ae, 8);
136 	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
137 	archive_entry_free(ae);
138 	assertEqualIntA(a, 8, archive_write_data(a, "abcdefgh", 9));
139 
140 	/*
141 	 * A hardlink to the above file.
142 	 */
143 	assert((ae = archive_entry_new()) != NULL);
144 	archive_entry_copy_pathname(ae, longhardlinkname);
145 	archive_entry_copy_hardlink(ae, longfilename);
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 
151 	/*
152 	 * A symlink to the above file.
153 	 */
154 	assert((ae = archive_entry_new()) != NULL);
155 	archive_entry_copy_pathname(ae, longlinkname);
156 	archive_entry_copy_symlink(ae, longfilename);
157 	archive_entry_set_mode(ae, AE_IFLNK | 0755);
158 	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
159 	archive_entry_free(ae);
160 
161 	/*
162 	 * A file with large UID/GID that overflow octal encoding.
163 	 */
164 	assert((ae = archive_entry_new()) != NULL);
165 	archive_entry_copy_pathname(ae, "large_uid_gid");
166 	archive_entry_set_mode(ae, S_IFREG | 0755);
167 	archive_entry_set_size(ae, 8);
168 	archive_entry_set_uid(ae, 123456789);
169 	archive_entry_set_gid(ae, 987654321);
170 	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
171 	archive_entry_free(ae);
172 	assertEqualIntA(a, 8, archive_write_data(a, "abcdefgh", 9));
173 
174 	/* TODO: support GNU tar sparse format and test it here. */
175 	/* See test_write_format_pax for an example of testing sparse files. */
176 
177 	/* Close out the archive. */
178 	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
179 	assertEqualIntA(a, ARCHIVE_OK, archive_write_free(a));
180 
181 	/*
182 	 * Some basic verification of the low-level format.
183 	 */
184 
185 	/* Verify GNU tar magic/version fields */
186 	assertEqualMem(buff + 257, "ustar  \0", 8);
187 
188 	assertEqualInt(15360, used);
189 
190 	/*
191 	 *
192 	 * Now, read the data back.
193 	 *
194 	 */
195 	assert((a = archive_read_new()) != NULL);
196 	assertEqualIntA(a, 0, archive_read_support_format_all(a));
197 	assertEqualIntA(a, 0, archive_read_support_filter_all(a));
198 	assertEqualIntA(a, 0, archive_read_open_memory(a, buff, used));
199 
200 	/*
201 	 * Read "file"
202 	 */
203 	assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
204 	assert(!archive_entry_atime_is_set(ae));
205 	assert(!archive_entry_birthtime_is_set(ae));
206 	assert(!archive_entry_ctime_is_set(ae));
207 	assertEqualInt(5, archive_entry_mtime(ae));
208 	assertEqualInt(0, archive_entry_mtime_nsec(ae));
209 	assertEqualString("file", archive_entry_pathname(ae));
210 	assertEqualInt(S_IFREG | 0755, archive_entry_mode(ae));
211 	assertEqualInt(8, archive_entry_size(ae));
212 	assertEqualIntA(a, 8, archive_read_data(a, buff2, 10));
213 	assertEqualMem(buff2, "12345678", 8);
214 
215 	/*
216 	 * Read file with very long name.
217 	 */
218 	assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
219 	assertEqualString(longfilename, archive_entry_pathname(ae));
220 	assertEqualInt(S_IFREG | 0755, archive_entry_mode(ae));
221 	assertEqualInt(8, archive_entry_size(ae));
222 	assertEqualIntA(a, 8, archive_read_data(a, buff2, 10));
223 	assertEqualMem(buff2, "abcdefgh", 8);
224 
225 	/*
226 	 * Read hardlink.
227 	 */
228 	assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
229 	assertEqualString(longhardlinkname, archive_entry_pathname(ae));
230 	assertEqualString(longfilename, archive_entry_hardlink(ae));
231 
232 	/*
233 	 * Read symlink.
234 	 */
235 	assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
236 	assertEqualString(longlinkname, archive_entry_pathname(ae));
237 	assertEqualString(longfilename, archive_entry_symlink(ae));
238 	assertEqualInt(AE_IFLNK | 0755, archive_entry_mode(ae));
239 
240 	/*
241 	 * Read file with large UID/GID.
242 	 */
243 	assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
244 	assertEqualInt(123456789, archive_entry_uid(ae));
245 	assertEqualInt(987654321, archive_entry_gid(ae));
246 	assertEqualString("large_uid_gid", archive_entry_pathname(ae));
247 	assertEqualInt(S_IFREG | 0755, archive_entry_mode(ae));
248 
249 	/*
250 	 * Verify the end of the archive.
251 	 */
252 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
253 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
254 	assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
255 
256 	free(buff);
257 }
258