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.
115 * TODO: Move the damage-recovery checking to a separate test;
116 * it doesn't really belong in this write test.
120 for (i = 80; i < 150; i++) {
121 if (memcmp(buff + i, "07070", 5) == 0) {
128 failure("Unable to locate the second header for damage-recovery test.");
129 assert(damaged == 1);
132 * Now, read the data back.
134 assert((a = archive_read_new()) != NULL);
135 assertA(0 == archive_read_support_format_all(a));
136 assertA(0 == archive_read_support_compression_all(a));
137 assertA(0 == archive_read_open_memory(a, buff, used));
139 if (!assertEqualIntA(a, 0, archive_read_next_header(a, &ae))) {
140 archive_read_finish(a);
144 assertEqualInt(1, archive_entry_mtime(ae));
145 /* Not the same as above: cpio doesn't store hi-res times. */
146 assert(0 == archive_entry_mtime_nsec(ae));
147 assert(0 == archive_entry_atime(ae));
148 assert(0 == archive_entry_ctime(ae));
149 assertEqualString("file", archive_entry_pathname(ae));
150 assertEqualInt((S_IFREG | 0755), archive_entry_mode(ae));
151 assertEqualInt(8, archive_entry_size(ae));
152 assertA(8 == archive_read_data(a, filedata, 10));
153 assert(0 == memcmp(filedata, "12345678", 8));
156 * The second file can't be read because we damaged its header.
160 * Read the dir entry back.
161 * ARCHIVE_WARN here because the damaged entry was skipped.
163 assertEqualIntA(a, ARCHIVE_WARN, archive_read_next_header(a, &ae));
164 assertEqualInt(11, archive_entry_mtime(ae));
165 assert(0 == archive_entry_mtime_nsec(ae));
166 assert(0 == archive_entry_atime(ae));
167 assert(0 == archive_entry_ctime(ae));
168 assertEqualString("dir", archive_entry_pathname(ae));
169 assertEqualInt((S_IFDIR | 0755), archive_entry_mode(ae));
170 assertEqualInt(0, archive_entry_size(ae));
171 assertEqualIntA(a, 0, archive_read_data(a, filedata, 10));
173 /* Verify the end of the archive. */
174 assertEqualIntA(a, 1, archive_read_next_header(a, &ae));
175 assert(0 == archive_read_close(a));
176 #if ARCHIVE_VERSION_NUMBER < 2000000
177 archive_read_finish(a);
179 assert(0 == archive_read_finish(a));
186 DEFINE_TEST(test_write_format_cpio)
188 #if ARCHIVE_VERSION_NUMBER >= 1009000
189 test_format(archive_write_set_format_cpio);
190 test_format(archive_write_set_format_cpio_newc);
192 skipping("cpio write support");