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$");
29 test_format(int (*set_format)(struct archive *))
32 struct archive_entry *ae;
36 size_t buffsize = 1000000;
40 buff = malloc(buffsize);
42 /* Create a new archive in memory. */
43 assert((a = archive_write_new()) != NULL);
44 assertA(0 == (*set_format)(a));
45 assertA(0 == archive_write_add_filter_none(a));
46 assertA(0 == archive_write_open_memory(a, buff, buffsize, &used));
51 assert((ae = archive_entry_new()) != NULL);
52 archive_entry_set_mtime(ae, 1, 10);
53 assert(1 == archive_entry_mtime(ae));
54 assert(10 == archive_entry_mtime_nsec(ae));
56 archive_entry_copy_pathname(ae, p);
59 assertEqualString("file", archive_entry_pathname(ae));
60 archive_entry_set_mode(ae, S_IFREG | 0755);
61 assert((S_IFREG | 0755) == archive_entry_mode(ae));
62 archive_entry_set_size(ae, 8);
64 assertA(0 == archive_write_header(a, ae));
65 archive_entry_free(ae);
66 assertA(8 == archive_write_data(a, "12345678", 9));
69 * Write another file to it.
71 assert((ae = archive_entry_new()) != NULL);
72 archive_entry_set_mtime(ae, 1, 10);
73 assert(1 == archive_entry_mtime(ae));
74 assert(10 == archive_entry_mtime_nsec(ae));
76 archive_entry_copy_pathname(ae, p);
79 assertEqualString("file2", archive_entry_pathname(ae));
80 archive_entry_set_mode(ae, S_IFREG | 0755);
81 assert((S_IFREG | 0755) == archive_entry_mode(ae));
82 archive_entry_set_size(ae, 4);
84 assertA(0 == archive_write_header(a, ae));
85 archive_entry_free(ae);
86 assertA(4 == archive_write_data(a, "1234", 5));
89 * Write a file with a name, filetype, and size.
91 assert((ae = archive_entry_new()) != NULL);
92 archive_entry_copy_pathname(ae, "name");
93 archive_entry_set_size(ae, 0);
94 archive_entry_set_filetype(ae, AE_IFREG);
95 assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae));
96 assert(archive_error_string(a) == NULL);
97 archive_entry_free(ae);
100 * Write a file with a name and filetype but no size.
102 assert((ae = archive_entry_new()) != NULL);
103 archive_entry_copy_pathname(ae, "name");
104 archive_entry_unset_size(ae);
105 archive_entry_set_filetype(ae, AE_IFREG);
106 assertEqualInt(ARCHIVE_FAILED, archive_write_header(a, ae));
107 assert(archive_error_string(a) != NULL);
108 archive_entry_free(ae);
111 * Write a file with a name and size but no filetype.
113 assert((ae = archive_entry_new()) != NULL);
114 archive_entry_copy_pathname(ae, "name");
115 archive_entry_set_size(ae, 0);
116 assertEqualInt(ARCHIVE_FAILED, archive_write_header(a, ae));
117 assert(archive_error_string(a) != NULL);
118 archive_entry_free(ae);
121 * Write a file with a size and filetype but no name.
123 assert((ae = archive_entry_new()) != NULL);
124 archive_entry_set_size(ae, 0);
125 archive_entry_set_filetype(ae, AE_IFREG);
126 assertEqualInt(ARCHIVE_FAILED, archive_write_header(a, ae));
127 assert(archive_error_string(a) != NULL);
128 archive_entry_free(ae);
131 * Write a directory to it.
133 assert((ae = archive_entry_new()) != NULL);
134 archive_entry_set_mtime(ae, 11, 110);
135 archive_entry_copy_pathname(ae, "dir");
136 archive_entry_set_mode(ae, S_IFDIR | 0755);
137 archive_entry_set_size(ae, 512);
139 assertA(0 == archive_write_header(a, ae));
140 assertEqualInt(0, archive_entry_size(ae));
141 archive_entry_free(ae);
142 assertEqualIntA(a, 0, archive_write_data(a, "12345678", 9));
145 /* Close out the archive. */
146 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
147 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
150 * Damage the second entry to test the search-ahead recovery.
151 * TODO: Move the damage-recovery checking to a separate test;
152 * it doesn't really belong in this write test.
156 for (i = 80; i < 150; i++) {
157 if (memcmp(buff + i, "07070", 5) == 0) {
164 failure("Unable to locate the second header for damage-recovery test.");
165 assert(damaged == 1);
168 * Now, read the data back.
170 assert((a = archive_read_new()) != NULL);
171 assertA(0 == archive_read_support_format_all(a));
172 assertA(0 == archive_read_support_filter_all(a));
173 assertA(0 == archive_read_open_memory(a, buff, used));
175 if (!assertEqualIntA(a, 0, archive_read_next_header(a, &ae))) {
176 archive_read_free(a);
180 assertEqualInt(1, archive_entry_mtime(ae));
181 /* Not the same as above: cpio doesn't store hi-res times. */
182 assert(0 == archive_entry_mtime_nsec(ae));
183 assert(0 == archive_entry_atime(ae));
184 assert(0 == archive_entry_ctime(ae));
185 assertEqualString("file", archive_entry_pathname(ae));
186 assertEqualInt((S_IFREG | 0755), archive_entry_mode(ae));
187 assertEqualInt(8, archive_entry_size(ae));
188 assertA(8 == archive_read_data(a, filedata, 10));
189 assertEqualMem(filedata, "12345678", 8);
192 * The second file can't be read because we damaged its header.
196 * Read the third file back.
197 * ARCHIVE_WARN here because the damaged entry was skipped.
199 assertEqualIntA(a, ARCHIVE_WARN, archive_read_next_header(a, &ae));
200 assertEqualString("name", archive_entry_pathname(ae));
203 * Read the dir entry back.
205 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
206 assertEqualInt(11, archive_entry_mtime(ae));
207 assert(0 == archive_entry_mtime_nsec(ae));
208 assert(0 == archive_entry_atime(ae));
209 assert(0 == archive_entry_ctime(ae));
210 assertEqualString("dir", archive_entry_pathname(ae));
211 assertEqualInt((S_IFDIR | 0755), archive_entry_mode(ae));
212 assertEqualInt(0, archive_entry_size(ae));
213 assertEqualIntA(a, 0, archive_read_data(a, filedata, 10));
215 /* Verify the end of the archive. */
216 assertEqualIntA(a, 1, archive_read_next_header(a, &ae));
217 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
218 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
224 test_big_entries(int (*set_format)(struct archive *), int64_t size, int expected)
226 struct archive_entry *ae;
228 size_t buffsize = 1000000;
232 buff = malloc(buffsize);
234 /* Create a new archive in memory. */
235 assert((a = archive_write_new()) != NULL);
236 assertA(0 == (*set_format)(a));
237 assertA(0 == archive_write_add_filter_none(a));
238 assertA(0 == archive_write_open_memory(a, buff, buffsize, &used));
240 assert((ae = archive_entry_new()) != NULL);
241 archive_entry_copy_pathname(ae, "file");
242 archive_entry_set_size(ae, size);
243 archive_entry_set_filetype(ae, AE_IFREG);
244 assertEqualInt(expected, archive_write_header(a, ae));
245 if (expected != ARCHIVE_OK)
246 assert(archive_error_string(a) != NULL);
248 archive_entry_free(ae);
249 archive_write_free(a);
254 DEFINE_TEST(test_write_format_cpio)
256 int64_t size_4g = ((int64_t)1) << 32;
257 int64_t size_8g = ((int64_t)1) << 33;
259 test_format(archive_write_set_format_cpio);
260 test_format(archive_write_set_format_cpio_newc);
262 test_big_entries(archive_write_set_format_cpio,
263 size_8g - 1, ARCHIVE_OK);
264 test_big_entries(archive_write_set_format_cpio,
265 size_8g, ARCHIVE_FAILED);
266 test_big_entries(archive_write_set_format_cpio_newc,
267 size_4g - 1, ARCHIVE_OK);
268 test_big_entries(archive_write_set_format_cpio_newc,
269 size_4g, ARCHIVE_FAILED);