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 * Write a file using archive_write_data call, read the file
30 * back and verify the contents. The data written includes large
31 * blocks of nulls, so it should exercise the sparsification logic
32 * if ARCHIVE_EXTRACT_SPARSE is enabled.
35 verify_write_data(struct archive *a, int sparse)
37 static const char data[]="abcdefghijklmnopqrstuvwxyz";
39 struct archive_entry *ae;
40 size_t buff_size = 64 * 1024;
42 const char *msg = sparse ? "sparse" : "non-sparse";
45 buff = malloc(buff_size);
48 ae = archive_entry_new();
50 archive_entry_set_size(ae, 8 * buff_size);
51 archive_entry_set_pathname(ae, "test_write_data");
52 archive_entry_set_mode(ae, AE_IFREG | 0755);
53 assertEqualIntA(a, 0, archive_write_header(a, ae));
55 /* Use archive_write_data() to write three relatively sparse blocks. */
57 /* First has non-null data at beginning. */
58 memset(buff, 0, buff_size);
59 memcpy(buff, data, sizeof(data));
61 assertEqualInt(buff_size, archive_write_data(a, buff, buff_size));
63 /* Second has non-null data in the middle. */
64 memset(buff, 0, buff_size);
65 memcpy(buff + buff_size / 2 - 3, data, sizeof(data));
67 assertEqualInt(buff_size, archive_write_data(a, buff, buff_size));
69 /* Third has non-null data at the end. */
70 memset(buff, 0, buff_size);
71 memcpy(buff + buff_size - sizeof(data), data, sizeof(data));
73 assertEqualInt(buff_size, archive_write_data(a, buff, buff_size));
76 assertEqualIntA(a, 0, archive_write_finish_entry(a));
78 /* Test the entry on disk. */
79 assert(0 == stat(archive_entry_pathname(ae), &st));
80 assertEqualInt(st.st_size, 8 * buff_size);
81 f = fopen(archive_entry_pathname(ae), "rb");
82 if (!assert(f != NULL))
85 /* Check first block. */
86 assertEqualInt(buff_size, fread(buff, 1, buff_size, f));
88 assertEqualMem(buff, data, sizeof(data));
89 for (p = buff + sizeof(data); p < buff + buff_size; ++p) {
90 failure("offset: %d, %s", (int)(p - buff), msg);
91 if (!assertEqualInt(0, *p))
95 /* Check second block. */
96 assertEqualInt(buff_size, fread(buff, 1, buff_size, f));
97 for (p = buff; p < buff + buff_size; ++p) {
98 failure("offset: %d, %s", (int)(p - buff), msg);
99 if (p == buff + buff_size / 2 - 3) {
100 assertEqualMem(p, data, sizeof(data));
102 } else if (!assertEqualInt(0, *p))
106 /* Check third block. */
107 assertEqualInt(buff_size, fread(buff, 1, buff_size, f));
108 for (p = buff; p < buff + buff_size - sizeof(data); ++p) {
109 failure("offset: %d, %s", (int)(p - buff), msg);
110 if (!assertEqualInt(0, *p))
114 assertEqualMem(buff + buff_size - sizeof(data), data, sizeof(data));
118 assertEqualInt(0, fclose(f));
119 archive_entry_free(ae);
124 * As above, but using the archive_write_data_block() call.
127 verify_write_data_block(struct archive *a, int sparse)
129 static const char data[]="abcdefghijklmnopqrstuvwxyz";
131 struct archive_entry *ae;
132 size_t buff_size = 64 * 1024;
134 const char *msg = sparse ? "sparse" : "non-sparse";
137 buff = malloc(buff_size);
138 assert(buff != NULL);
140 ae = archive_entry_new();
142 archive_entry_set_size(ae, 8 * buff_size);
143 archive_entry_set_pathname(ae, "test_write_data_block");
144 archive_entry_set_mode(ae, AE_IFREG | 0755);
145 assertEqualIntA(a, 0, archive_write_header(a, ae));
147 /* Use archive_write_data_block() to write three
148 relatively sparse blocks. */
150 /* First has non-null data at beginning. */
151 memset(buff, 0, buff_size);
152 memcpy(buff, data, sizeof(data));
154 assertEqualInt(ARCHIVE_OK,
155 archive_write_data_block(a, buff, buff_size, 100));
157 /* Second has non-null data in the middle. */
158 memset(buff, 0, buff_size);
159 memcpy(buff + buff_size / 2 - 3, data, sizeof(data));
161 assertEqualInt(ARCHIVE_OK,
162 archive_write_data_block(a, buff, buff_size, buff_size + 200));
164 /* Third has non-null data at the end. */
165 memset(buff, 0, buff_size);
166 memcpy(buff + buff_size - sizeof(data), data, sizeof(data));
168 assertEqualInt(ARCHIVE_OK,
169 archive_write_data_block(a, buff, buff_size, buff_size * 2 + 300));
172 assertEqualIntA(a, 0, archive_write_finish_entry(a));
174 /* Test the entry on disk. */
175 assert(0 == stat(archive_entry_pathname(ae), &st));
176 assertEqualInt(st.st_size, 8 * buff_size);
177 f = fopen(archive_entry_pathname(ae), "rb");
178 if (!assert(f != NULL))
181 /* Check 100-byte gap at beginning */
182 assertEqualInt(100, fread(buff, 1, 100, f));
184 for (p = buff; p < buff + 100; ++p) {
185 failure("offset: %d, %s", (int)(p - buff), msg);
186 if (!assertEqualInt(0, *p))
190 /* Check first block. */
191 assertEqualInt(buff_size, fread(buff, 1, buff_size, f));
193 assertEqualMem(buff, data, sizeof(data));
194 for (p = buff + sizeof(data); p < buff + buff_size; ++p) {
195 failure("offset: %d, %s", (int)(p - buff), msg);
196 if (!assertEqualInt(0, *p))
200 /* Check 100-byte gap */
201 assertEqualInt(100, fread(buff, 1, 100, f));
203 for (p = buff; p < buff + 100; ++p) {
204 failure("offset: %d, %s", (int)(p - buff), msg);
205 if (!assertEqualInt(0, *p))
209 /* Check second block. */
210 assertEqualInt(buff_size, fread(buff, 1, buff_size, f));
211 for (p = buff; p < buff + buff_size; ++p) {
212 failure("offset: %d, %s", (int)(p - buff), msg);
213 if (p == buff + buff_size / 2 - 3) {
214 assertEqualMem(p, data, sizeof(data));
216 } else if (!assertEqualInt(0, *p))
220 /* Check 100-byte gap */
221 assertEqualInt(100, fread(buff, 1, 100, f));
223 for (p = buff; p < buff + 100; ++p) {
224 failure("offset: %d, %s", (int)(p - buff), msg);
225 if (!assertEqualInt(0, *p))
229 /* Check third block. */
230 assertEqualInt(buff_size, fread(buff, 1, buff_size, f));
231 for (p = buff; p < buff + buff_size - sizeof(data); ++p) {
232 failure("offset: %d, %s", (int)(p - buff), msg);
233 if (!assertEqualInt(0, *p))
237 assertEqualMem(buff + buff_size - sizeof(data), data, sizeof(data));
239 /* Check another block size beyond last we wrote. */
240 assertEqualInt(buff_size, fread(buff, 1, buff_size, f));
242 for (p = buff; p < buff + buff_size; ++p) {
243 failure("offset: %d, %s", (int)(p - buff), msg);
244 if (!assertEqualInt(0, *p))
251 assertEqualInt(0, fclose(f));
253 archive_entry_free(ae);
256 DEFINE_TEST(test_write_disk_sparse)
262 * The return values, etc, of the write data functions
263 * shouldn't change regardless of whether we've requested
264 * sparsification. (The performance and pattern of actual
265 * write calls to the disk should vary, of course, but the
266 * client program shouldn't see any difference.)
268 assert((ad = archive_write_disk_new()) != NULL);
269 archive_write_disk_set_options(ad, 0);
270 verify_write_data(ad, 0);
271 verify_write_data_block(ad, 0);
272 assertEqualInt(0, archive_write_finish(ad));
274 assert((ad = archive_write_disk_new()) != NULL);
275 archive_write_disk_set_options(ad, ARCHIVE_EXTRACT_SPARSE);
276 verify_write_data(ad, 1);
277 verify_write_data_block(ad, 1);
278 assertEqualInt(0, archive_write_finish(ad));