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 /* The version stamp macro was introduced after cpio write support. */
29 #if ARCHIVE_VERSION_NUMBER >= 1009000
31 test_format(int (*set_format)(struct archive *))
34 struct archive_entry *ae;
38 size_t buffsize = 1000000;
42 buff = malloc(buffsize);
44 /* Create a new archive in memory. */
45 assert((a = archive_write_new()) != NULL);
46 assertA(0 == (*set_format)(a));
47 assertA(0 == archive_write_set_compression_none(a));
48 assertA(0 == archive_write_open_memory(a, buff, buffsize, &used));
53 assert((ae = archive_entry_new()) != NULL);
54 archive_entry_set_mtime(ae, 1, 10);
55 assert(1 == archive_entry_mtime(ae));
56 assert(10 == archive_entry_mtime_nsec(ae));
58 archive_entry_copy_pathname(ae, p);
61 assertEqualString("file", archive_entry_pathname(ae));
62 archive_entry_set_mode(ae, S_IFREG | 0755);
63 assert((S_IFREG | 0755) == archive_entry_mode(ae));
64 archive_entry_set_size(ae, 8);
66 assertA(0 == archive_write_header(a, ae));
67 archive_entry_free(ae);
68 assertA(8 == archive_write_data(a, "12345678", 9));
71 * Write another file to it.
73 assert((ae = archive_entry_new()) != NULL);
74 archive_entry_set_mtime(ae, 1, 10);
75 assert(1 == archive_entry_mtime(ae));
76 assert(10 == archive_entry_mtime_nsec(ae));
78 archive_entry_copy_pathname(ae, p);
81 assertEqualString("file2", archive_entry_pathname(ae));
82 archive_entry_set_mode(ae, S_IFREG | 0755);
83 assert((S_IFREG | 0755) == archive_entry_mode(ae));
84 archive_entry_set_size(ae, 4);
86 assertA(0 == archive_write_header(a, ae));
87 archive_entry_free(ae);
88 assertA(4 == archive_write_data(a, "1234", 5));
91 * Write a directory to it.
93 assert((ae = archive_entry_new()) != NULL);
94 archive_entry_set_mtime(ae, 11, 110);
95 archive_entry_copy_pathname(ae, "dir");
96 archive_entry_set_mode(ae, S_IFDIR | 0755);
97 archive_entry_set_size(ae, 512);
99 assertA(0 == archive_write_header(a, ae));
100 assertEqualInt(0, archive_entry_size(ae));
101 archive_entry_free(ae);
102 assertEqualIntA(a, 0, archive_write_data(a, "12345678", 9));
105 /* Close out the archive. */
106 assertA(0 == archive_write_close(a));
107 #if ARCHIVE_VERSION_NUMBER < 2000000
108 archive_write_finish(a);
110 assertA(0 == archive_write_finish(a));
114 * Damage the second entry to test the search-ahead recovery.
118 for (i = 80; i < 150; i++) {
119 if (memcmp(buff + i, "07070", 5) == 0) {
126 failure("Unable to locate the second header for damage-recovery test.");
130 * Now, read the data back.
132 assert((a = archive_read_new()) != NULL);
133 assertA(0 == archive_read_support_format_all(a));
134 assertA(0 == archive_read_support_compression_all(a));
135 assertA(0 == archive_read_open_memory(a, buff, used));
137 if (!assertEqualIntA(a, 0, archive_read_next_header(a, &ae))) {
138 archive_read_finish(a);
142 assertEqualInt(1, archive_entry_mtime(ae));
143 /* Not the same as above: cpio doesn't store hi-res times. */
144 assert(0 == archive_entry_mtime_nsec(ae));
145 assert(0 == archive_entry_atime(ae));
146 assert(0 == archive_entry_ctime(ae));
147 assertEqualString("file", archive_entry_pathname(ae));
148 assertEqualInt((S_IFREG | 0755), archive_entry_mode(ae));
149 assertEqualInt(8, archive_entry_size(ae));
150 assertA(8 == archive_read_data(a, filedata, 10));
151 assert(0 == memcmp(filedata, "12345678", 8));
154 * Read the second file back.
157 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
158 assertEqualInt(1, archive_entry_mtime(ae));
159 /* Not the same as above: cpio doesn't store hi-res times. */
160 assert(0 == archive_entry_mtime_nsec(ae));
161 assert(0 == archive_entry_atime(ae));
162 assert(0 == archive_entry_ctime(ae));
163 assertEqualString("file2", archive_entry_pathname(ae));
164 assert((S_IFREG | 0755) == archive_entry_mode(ae));
165 assertEqualInt(4, archive_entry_size(ae));
166 assertEqualIntA(a, 4, archive_read_data(a, filedata, 10));
167 assert(0 == memcmp(filedata, "1234", 4));
171 * Read the dir entry back.
174 damaged ? ARCHIVE_WARN : ARCHIVE_OK,
175 archive_read_next_header(a, &ae));
176 assertEqualInt(11, archive_entry_mtime(ae));
177 assert(0 == archive_entry_mtime_nsec(ae));
178 assert(0 == archive_entry_atime(ae));
179 assert(0 == archive_entry_ctime(ae));
180 assertEqualString("dir", archive_entry_pathname(ae));
181 assertEqualInt((S_IFDIR | 0755), archive_entry_mode(ae));
182 assertEqualInt(0, archive_entry_size(ae));
183 assertEqualIntA(a, 0, archive_read_data(a, filedata, 10));
185 /* Verify the end of the archive. */
186 assertEqualIntA(a, 1, archive_read_next_header(a, &ae));
187 assert(0 == archive_read_close(a));
188 #if ARCHIVE_VERSION_NUMBER < 2000000
189 archive_read_finish(a);
191 assert(0 == archive_read_finish(a));
198 DEFINE_TEST(test_write_format_cpio)
200 #if ARCHIVE_VERSION_NUMBER >= 1009000
201 test_format(archive_write_set_format_cpio);
202 test_format(archive_write_set_format_cpio_newc);
204 skipping("cpio write support");