2 * Copyright (c) 2003-2007 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
26 __FBSDID("$FreeBSD$");
28 #if ARCHIVE_VERSION_STAMP >= 1009000
32 static void create(struct archive_entry *ae, const char *msg)
37 /* Write the entry to disk. */
38 assert((ad = archive_write_disk_new()) != NULL);
40 assertEqualIntA(ad, 0, archive_write_header(ad, ae));
41 assertEqualIntA(ad, 0, archive_write_finish_entry(ad));
42 #if ARCHIVE_API_VERSION > 1
43 assertEqualInt(0, archive_write_finish(ad));
45 archive_write_finish(ad);
47 /* Test the entries on disk. */
48 assert(0 == stat(archive_entry_pathname(ae), &st));
49 failure("st.st_mode=%o archive_entry_mode(ae)=%o",
50 st.st_mode, archive_entry_mode(ae));
51 /* When verifying a dir, ignore the S_ISGID bit, as some systems set
52 * that automatically. */
53 if (archive_entry_filetype(ae) == AE_IFDIR)
54 st.st_mode &= ~S_ISGID;
55 assertEqualInt(st.st_mode, archive_entry_mode(ae) & ~UMASK);
58 static void create_reg_file(struct archive_entry *ae, const char *msg)
60 static const char data[]="abcdefghijklmnopqrstuvwxyz";
65 /* Write the entry to disk. */
66 assert((ad = archive_write_disk_new()) != NULL);
67 archive_write_disk_set_options(ad, ARCHIVE_EXTRACT_TIME);
70 * A touchy API design issue: archive_write_data() does (as of
71 * 2.4.12) enforce the entry size as a limit on the data
72 * written to the file. This was not enforced prior to
73 * 2.4.12. The change was prompted by the refined
74 * hardlink-restore semantics introduced at that time. In
75 * short, libarchive needs to know whether a "hardlink entry"
76 * is going to overwrite the contents so that it can know
77 * whether or not to open the file for writing. This implies
78 * that there is a fundamental semantic difference between an
79 * entry with a zero size and one with a non-zero size in the
80 * case of hardlinks and treating the hardlink case
81 * differently from the regular file case is just asking for
82 * trouble. So, a zero size must always mean that no data
83 * will be accepted, which is consistent with the file size in
84 * the entry being a maximum size.
86 archive_entry_set_size(ae, sizeof(data));
87 archive_entry_set_mtime(ae, 123456789, 0);
88 assertEqualIntA(ad, 0, archive_write_header(ad, ae));
89 assertEqualInt(sizeof(data), archive_write_data(ad, data, sizeof(data)));
90 assertEqualIntA(ad, 0, archive_write_finish_entry(ad));
91 #if ARCHIVE_API_VERSION > 1
92 assertEqualInt(0, archive_write_finish(ad));
94 archive_write_finish(ad);
96 /* Test the entries on disk. */
97 assert(0 == stat(archive_entry_pathname(ae), &st));
98 failure("st.st_mode=%o archive_entry_mode(ae)=%o",
99 st.st_mode, archive_entry_mode(ae));
100 assertEqualInt(st.st_mode, (archive_entry_mode(ae) & ~UMASK));
101 assertEqualInt(st.st_size, sizeof(data));
102 assertEqualInt(st.st_mtime, 123456789);
103 failure("No atime was specified, so atime should get set to current time");
105 assert(st.st_atime <= now && st.st_atime > now - 5);
108 static void create_reg_file2(struct archive_entry *ae, const char *msg)
110 const int datasize = 100000;
117 data = malloc(datasize);
118 for (i = 0; i < datasize; i++)
119 data[i] = (char)(i % 256);
121 /* Write the entry to disk. */
122 assert((ad = archive_write_disk_new()) != NULL);
125 * See above for an explanation why this next call
128 archive_entry_set_size(ae, datasize);
129 assertEqualIntA(ad, 0, archive_write_header(ad, ae));
130 for (i = 0; i < datasize - 999; i += 1000) {
131 assertEqualIntA(ad, ARCHIVE_OK,
132 archive_write_data_block(ad, data + i, 1000, i));
134 assertEqualIntA(ad, 0, archive_write_finish_entry(ad));
135 #if ARCHIVE_API_VERSION > 1
136 assertEqualInt(0, archive_write_finish(ad));
138 archive_write_finish(ad);
140 /* Test the entries on disk. */
141 assert(0 == stat(archive_entry_pathname(ae), &st));
142 failure("st.st_mode=%o archive_entry_mode(ae)=%o",
143 st.st_mode, archive_entry_mode(ae));
144 assertEqualInt(st.st_mode, (archive_entry_mode(ae) & ~UMASK));
145 assertEqualInt(st.st_size, i);
147 compare = malloc(datasize);
148 fd = open(archive_entry_pathname(ae), O_RDONLY);
149 assertEqualInt(datasize, read(fd, compare, datasize));
151 assert(memcmp(compare, data, datasize) == 0);
156 static void create_reg_file3(struct archive_entry *ae, const char *msg)
158 static const char data[]="abcdefghijklmnopqrstuvwxyz";
162 /* Write the entry to disk. */
163 assert((ad = archive_write_disk_new()) != NULL);
165 /* Set the size smaller than the data and verify the truncation. */
166 archive_entry_set_size(ae, 5);
167 assertEqualIntA(ad, 0, archive_write_header(ad, ae));
168 assertEqualInt(5, archive_write_data(ad, data, sizeof(data)));
169 assertEqualIntA(ad, 0, archive_write_finish_entry(ad));
170 #if ARCHIVE_VERSION_NUMBER < 2000000
171 archive_write_finish(ad);
173 assertEqualInt(0, archive_write_finish(ad));
175 /* Test the entry on disk. */
176 assert(0 == stat(archive_entry_pathname(ae), &st));
177 failure("st.st_mode=%o archive_entry_mode(ae)=%o",
178 st.st_mode, archive_entry_mode(ae));
179 assertEqualInt(st.st_mode, (archive_entry_mode(ae) & ~UMASK));
180 assertEqualInt(st.st_size, 5);
184 static void create_reg_file4(struct archive_entry *ae, const char *msg)
186 static const char data[]="abcdefghijklmnopqrstuvwxyz";
190 /* Write the entry to disk. */
191 assert((ad = archive_write_disk_new()) != NULL);
192 /* Leave the size unset. The data should not be truncated. */
193 assertEqualIntA(ad, 0, archive_write_header(ad, ae));
194 assertEqualInt(ARCHIVE_OK,
195 archive_write_data_block(ad, data, sizeof(data), 0));
196 assertEqualIntA(ad, 0, archive_write_finish_entry(ad));
197 #if ARCHIVE_VERSION_NUMBER < 2000000
198 archive_write_finish(ad);
200 assertEqualInt(0, archive_write_finish(ad));
202 /* Test the entry on disk. */
203 assert(0 == stat(archive_entry_pathname(ae), &st));
204 failure("st.st_mode=%o archive_entry_mode(ae)=%o",
205 st.st_mode, archive_entry_mode(ae));
206 assertEqualInt(st.st_mode, (archive_entry_mode(ae) & ~UMASK));
208 assertEqualInt(st.st_size, sizeof(data));
212 DEFINE_TEST(test_write_disk)
214 #if ARCHIVE_VERSION_STAMP < 1009000
215 skipping("archive_write_disk interface");
217 struct archive_entry *ae;
219 /* Force the umask to something predictable. */
222 /* A regular file. */
223 assert((ae = archive_entry_new()) != NULL);
224 archive_entry_copy_pathname(ae, "file");
225 archive_entry_set_mode(ae, S_IFREG | 0755);
226 create_reg_file(ae, "Test creating a regular file");
227 archive_entry_free(ae);
229 /* Another regular file. */
230 assert((ae = archive_entry_new()) != NULL);
231 archive_entry_copy_pathname(ae, "file2");
232 archive_entry_set_mode(ae, S_IFREG | 0755);
233 create_reg_file2(ae, "Test creating another regular file");
234 archive_entry_free(ae);
236 /* A regular file with a size restriction */
237 assert((ae = archive_entry_new()) != NULL);
238 archive_entry_copy_pathname(ae, "file3");
239 archive_entry_set_mode(ae, S_IFREG | 0755);
240 create_reg_file3(ae, "Regular file with size restriction");
241 archive_entry_free(ae);
243 /* A regular file with an unspecified size */
244 assert((ae = archive_entry_new()) != NULL);
245 archive_entry_copy_pathname(ae, "file3");
246 archive_entry_set_mode(ae, S_IFREG | 0755);
247 create_reg_file4(ae, "Regular file with unspecified size");
248 archive_entry_free(ae);
250 /* A regular file over an existing file */
251 assert((ae = archive_entry_new()) != NULL);
252 archive_entry_copy_pathname(ae, "file");
253 archive_entry_set_mode(ae, S_IFREG | 0724);
254 create(ae, "Test creating a file over an existing file.");
255 archive_entry_free(ae);
258 assert((ae = archive_entry_new()) != NULL);
259 archive_entry_copy_pathname(ae, "dir");
260 archive_entry_set_mode(ae, S_IFDIR | 0555);
261 create(ae, "Test creating a regular dir.");
262 archive_entry_free(ae);
264 /* A directory over an existing file. */
265 assert((ae = archive_entry_new()) != NULL);
266 archive_entry_copy_pathname(ae, "file");
267 archive_entry_set_mode(ae, S_IFDIR | 0742);
268 create(ae, "Test creating a dir over an existing file.");
269 archive_entry_free(ae);
271 /* A file over an existing dir. */
272 assert((ae = archive_entry_new()) != NULL);
273 archive_entry_copy_pathname(ae, "file");
274 archive_entry_set_mode(ae, S_IFREG | 0744);
275 create(ae, "Test creating a file over an existing dir.");
276 archive_entry_free(ae);