xref: /netbsd-src/external/bsd/libarchive/dist/libarchive/test/test_write_format_tar_sparse.c (revision 97f7d7b603fb686e31ff0696877e479788d51b37)
1 /*-
2  * Copyright (c) 2003-2010 Tim Kientzle
3  * Copyright (c) 2012 Michihiro NAKAJIMA
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 #include "test.h"
27 
28 static char buff[1000000];
29 
30 static void
test_1(void)31 test_1(void)
32 {
33 	struct archive_entry *ae;
34 	struct archive *a;
35 	size_t used;
36 	size_t blocksize;
37 	int64_t offset, length;
38 	char *buff2;
39 	size_t buff2_size = 0x13000;
40 	char buff3[1024];
41 	long i;
42 
43 	assert((buff2 = malloc(buff2_size)) != NULL);
44 	/* Repeat the following for a variety of odd blocksizes. */
45 	for (blocksize = 1; blocksize < 100000; blocksize += blocksize + 3) {
46 		/* Create a new archive in memory. */
47 		assert((a = archive_write_new()) != NULL);
48 		assertEqualIntA(a, ARCHIVE_OK,
49 		    archive_write_set_format_pax(a));
50 		assertEqualIntA(a, ARCHIVE_OK,
51 		    archive_write_add_filter_none(a));
52 		assertEqualIntA(a, ARCHIVE_OK,
53 		    archive_write_set_bytes_per_block(a, (int)blocksize));
54 		assertEqualIntA(a, ARCHIVE_OK,
55 		    archive_write_set_bytes_in_last_block(a, (int)blocksize));
56 		assertEqualInt(blocksize,
57 		    archive_write_get_bytes_in_last_block(a));
58 		assertEqualIntA(a, ARCHIVE_OK,
59 		    archive_write_open_memory(a, buff, sizeof(buff), &used));
60 		assertEqualInt(blocksize,
61 		    archive_write_get_bytes_in_last_block(a));
62 
63 		/*
64 		 * Write a file to it.
65 		 */
66 		assert((ae = archive_entry_new()) != NULL);
67 		archive_entry_set_mtime(ae, 1, 10);
68 		assertEqualInt(1, archive_entry_mtime(ae));
69 		assertEqualInt(10, archive_entry_mtime_nsec(ae));
70 		archive_entry_copy_pathname(ae, "file");
71 		assertEqualString("file", archive_entry_pathname(ae));
72 		archive_entry_set_mode(ae, S_IFREG | 0755);
73 		assertEqualInt(S_IFREG | 0755, archive_entry_mode(ae));
74 		archive_entry_set_size(ae, 0x81000);
75 		archive_entry_sparse_add_entry(ae, 0x10000, 0x1000);
76 		archive_entry_sparse_add_entry(ae, 0x80000, 0x1000);
77 
78 		assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
79 		archive_entry_free(ae);
80 		memset(buff2, 'a', buff2_size);
81 		for (i = 0; i < 0x81000;) {
82 			size_t ws = buff2_size;
83 			if (i + ws > 0x81000)
84 				ws = 0x81000 - i;
85 			assertEqualInt(ws,
86 				archive_write_data(a, buff2, ws));
87 			i += (long)ws;
88 		}
89 
90 		/* Close out the archive. */
91 		assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
92 		assertEqualInt(ARCHIVE_OK, archive_write_free(a));
93 
94 		/* This calculation gives "the smallest multiple of
95 		 * the block size that is at least 11264 bytes". */
96 		failure("blocksize=%zu", blocksize);
97 		assertEqualInt(((11264 - 1)/blocksize+1)*blocksize, used);
98 
99 		/*
100 		 * Now, read the data back.
101 		 */
102 		assert((a = archive_read_new()) != NULL);
103 		assertEqualIntA(a, ARCHIVE_OK,
104 		    archive_read_support_format_all(a));
105 		assertEqualIntA(a, ARCHIVE_OK,
106 		    archive_read_support_filter_all(a));
107 		assertEqualIntA(a, ARCHIVE_OK,
108 		    archive_read_open_memory(a, buff, used));
109 
110 		assertEqualIntA(a, ARCHIVE_OK,
111 		    archive_read_next_header(a, &ae));
112 
113 		assertEqualInt(1, archive_entry_mtime(ae));
114 		assertEqualInt(10, archive_entry_mtime_nsec(ae));
115 		assertEqualInt(0, archive_entry_atime(ae));
116 		assertEqualInt(0, archive_entry_ctime(ae));
117 		assertEqualString("file", archive_entry_pathname(ae));
118 		assertEqualInt(AE_IFREG, archive_entry_filetype(ae));
119 		assertEqualInt(AE_IFREG | 0755, archive_entry_mode(ae));
120 		assertEqualInt(0x81000, archive_entry_size(ae));
121 		/* Verify sparse information. */
122 		assertEqualInt(2, archive_entry_sparse_reset(ae));
123 		assertEqualInt(0,
124 			archive_entry_sparse_next(ae, &offset, &length));
125 		assertEqualInt(0x10000, offset);
126 		assertEqualInt(0x1000, length);
127 		assertEqualInt(0,
128 			archive_entry_sparse_next(ae, &offset, &length));
129 		assertEqualInt(0x80000, offset);
130 		assertEqualInt(0x1000, length);
131 		/* Verify file contents. */
132 		memset(buff3, 0, sizeof(buff3));
133 		for (i = 0; i < 0x10000; i += 1024) {
134 			assertEqualInt(1024, archive_read_data(a, buff2, 1024));
135 			failure("Read data(0x%lx - 0x%lx) should be all zero",
136 			    i, i + 1024);
137 			assertEqualMem(buff2, buff3, 1024);
138 		}
139 		memset(buff3, 'a', sizeof(buff3));
140 		for (i = 0x10000; i < 0x11000; i += 1024) {
141 			assertEqualInt(1024, archive_read_data(a, buff2, 1024));
142 			failure("Read data(0x%lx - 0x%lx) should be all 'a'",
143 			    i, i + 1024);
144 			assertEqualMem(buff2, buff3, 1024);
145 		}
146 		memset(buff3, 0, sizeof(buff3));
147 		for (i = 0x11000; i < 0x80000; i += 1024) {
148 			assertEqualInt(1024, archive_read_data(a, buff2, 1024));
149 			failure("Read data(0x%lx - 0x%lx) should be all zero",
150 			    i, i + 1024);
151 			assertEqualMem(buff2, buff3, 1024);
152 		}
153 		memset(buff3, 'a', sizeof(buff3));
154 		for (i = 0x80000; i < 0x81000; i += 1024) {
155 			assertEqualInt(1024, archive_read_data(a, buff2, 1024));
156 			failure("Read data(0x%lx - 0x%lx) should be all 'a'",
157 			    i, i + 1024);
158 			assertEqualMem(buff2, buff3, 1024);
159 		}
160 
161 		/* Verify the end of the archive. */
162 		assertEqualIntA(a, ARCHIVE_EOF,
163 		    archive_read_next_header(a, &ae));
164 		assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
165 		assertEqualInt(ARCHIVE_OK, archive_read_free(a));
166 	}
167 	free(buff2);
168 }
169 
170 /*
171  * Test for the case the full bytes of sparse file data is not written.
172  */
173 static void
test_2(void)174 test_2(void)
175 {
176 	struct archive_entry *ae;
177 	struct archive *a;
178 	size_t used;
179 	size_t blocksize = 20 * 512;
180 	int64_t offset, length;
181 	char *buff2;
182 	size_t buff2_size = 0x11000;
183 	char buff3[1024];
184 	long i;
185 
186 	assert((buff2 = malloc(buff2_size)) != NULL);
187 	/* Create a new archive in memory. */
188 	assert((a = archive_write_new()) != NULL);
189 	assertEqualIntA(a, ARCHIVE_OK,
190 	    archive_write_set_format_pax(a));
191 	assertEqualIntA(a, ARCHIVE_OK,
192 	    archive_write_add_filter_none(a));
193 	assertEqualIntA(a, ARCHIVE_OK,
194 	    archive_write_set_bytes_per_block(a, (int)blocksize));
195 	assertEqualIntA(a, ARCHIVE_OK,
196 	    archive_write_set_bytes_in_last_block(a, (int)blocksize));
197 	assertEqualInt(blocksize,
198 	    archive_write_get_bytes_in_last_block(a));
199 	assertEqualIntA(a, ARCHIVE_OK,
200 	    archive_write_open_memory(a, buff, sizeof(buff), &used));
201 	assertEqualInt(blocksize,
202 	    archive_write_get_bytes_in_last_block(a));
203 
204 	/*
205 	 * Write a file to it.
206 	 */
207 	assert((ae = archive_entry_new()) != NULL);
208 	archive_entry_set_mtime(ae, 1, 10);
209 	assertEqualInt(1, archive_entry_mtime(ae));
210 	assertEqualInt(10, archive_entry_mtime_nsec(ae));
211 	archive_entry_copy_pathname(ae, "file");
212 	assertEqualString("file", archive_entry_pathname(ae));
213 	archive_entry_set_mode(ae, S_IFREG | 0755);
214 	assertEqualInt(S_IFREG | 0755, archive_entry_mode(ae));
215 	archive_entry_set_size(ae, 0x81000);
216 	archive_entry_sparse_add_entry(ae, 0x10000, 0x1000);
217 	archive_entry_sparse_add_entry(ae, 0x80000, 0x1000);
218 
219 	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
220 	archive_entry_free(ae);
221 	memset(buff2, 'a', buff2_size);
222 	/* Write bytes less than it should be. */
223 	assertEqualInt(buff2_size, archive_write_data(a, buff2, buff2_size));
224 
225 	/* Close out the archive. */
226 	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
227 	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
228 
229 	/* This calculation gives "the smallest multiple of
230 	 * the block size that is at least 11264 bytes". */
231 	failure("blocksize=%zu", blocksize);
232 	assertEqualInt(((11264 - 1)/blocksize+1)*blocksize, used);
233 
234 	/*
235 	 * Now, read the data back.
236 	 */
237 	assert((a = archive_read_new()) != NULL);
238 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
239 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
240 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, used));
241 
242 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
243 
244 	assertEqualInt(1, archive_entry_mtime(ae));
245 	assertEqualInt(10, archive_entry_mtime_nsec(ae));
246 	assertEqualInt(0, archive_entry_atime(ae));
247 	assertEqualInt(0, archive_entry_ctime(ae));
248 	assertEqualString("file", archive_entry_pathname(ae));
249 	assertEqualInt(AE_IFREG, archive_entry_filetype(ae));
250 	assertEqualInt(AE_IFREG | 0755, archive_entry_mode(ae));
251 	assertEqualInt(0x81000, archive_entry_size(ae));
252 	/* Verify sparse information. */
253 	assertEqualInt(2, archive_entry_sparse_reset(ae));
254 	assertEqualInt(0, archive_entry_sparse_next(ae, &offset, &length));
255 	assertEqualInt(0x10000, offset);
256 	assertEqualInt(0x1000, length);
257 	assertEqualInt(0, archive_entry_sparse_next(ae, &offset, &length));
258 	assertEqualInt(0x80000, offset);
259 	assertEqualInt(0x1000, length);
260 	/* Verify file contents. */
261 	memset(buff3, 0, sizeof(buff3));
262 	for (i = 0; i < 0x10000; i += 1024) {
263 		assertEqualInt(1024, archive_read_data(a, buff2, 1024));
264 		failure("Read data(0x%lx - 0x%lx) should be all zero",
265 		    i, i + 1024);
266 		assertEqualMem(buff2, buff3, 1024);
267 	}
268 	memset(buff3, 'a', sizeof(buff3));
269 	for (i = 0x10000; i < 0x11000; i += 1024) {
270 		assertEqualInt(1024, archive_read_data(a, buff2, 1024));
271 		failure("Read data(0x%lx - 0x%lx) should be all 'a'",
272 		    i, i + 1024);
273 		assertEqualMem(buff2, buff3, 1024);
274 	}
275 	memset(buff3, 0, sizeof(buff3));
276 	for (i = 0x11000; i < 0x80000; i += 1024) {
277 		assertEqualInt(1024, archive_read_data(a, buff2, 1024));
278 		failure("Read data(0x%lx - 0x%lx) should be all zero",
279 		    i, i + 1024);
280 		assertEqualMem(buff2, buff3, 1024);
281 	}
282 	memset(buff3, 0, sizeof(buff3));
283 	for (i = 0x80000; i < 0x81000; i += 1024) {
284 		assertEqualInt(1024, archive_read_data(a, buff2, 1024));
285 		failure("Read data(0x%lx - 0x%lx) should be all 'a'",
286 		    i, i + 1024);
287 		assertEqualMem(buff2, buff3, 1024);
288 	}
289 
290 	/* Verify the end of the archive. */
291 	assertEqualIntA(a, ARCHIVE_EOF,
292 	    archive_read_next_header(a, &ae));
293 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
294 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
295 	free(buff2);
296 }
297 
DEFINE_TEST(test_write_format_tar_sparse)298 DEFINE_TEST(test_write_format_tar_sparse)
299 {
300 	/* Test1: archiving sparse files. */
301 	test_1();
302 	/* Test2: incompletely archiving sparse files. */
303 	test_2();
304 }
305