xref: /freebsd/contrib/libarchive/libarchive/test/test_read_data_large.c (revision 185becb1e1bd2657c156f78aeb52edac05ba5fb5)
1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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.
24  */
25 #include "test.h"
26 
27 /*
28  * Test read/write of a 10M block of data in a single operation.
29  * Uses an in-memory archive with a single 10M entry.  Exercises
30  * archive_read_data() to ensure it can handle large blocks like
31  * this and also exercises archive_read_data_into_fd() (which
32  * had a bug relating to this, fixed in Nov 2006).
33  */
34 
35 #if defined(_WIN32) && !defined(__CYGWIN__)
36 #define open _open
37 #define close _close
38 #define ftruncate _chsize
39 #endif
40 
41 static char buff1[11000000];
42 static char buff2[10000000];
43 static char buff3[10000000];
44 
DEFINE_TEST(test_read_data_large)45 DEFINE_TEST(test_read_data_large)
46 {
47 	struct archive_entry *ae;
48 	struct archive *a;
49 	char tmpfilename[] = "largefile";
50 	int tmpfilefd;
51 	FILE *f;
52 	size_t used;
53 
54 	/* Create a new archive in memory. */
55 	assert((a = archive_write_new()) != NULL);
56 	assertA(0 == archive_write_set_format_ustar(a));
57 	assertA(0 == archive_write_add_filter_none(a));
58 	assertA(0 == archive_write_open_memory(a, buff1, sizeof(buff1), &used));
59 
60 	/*
61 	 * Write a file (with random contents) to it.
62 	 */
63 	assert((ae = archive_entry_new()) != NULL);
64 	archive_entry_copy_pathname(ae, "file");
65 	archive_entry_set_mode(ae, S_IFREG | 0755);
66 	fill_with_pseudorandom_data(buff2, sizeof(buff2));
67 	archive_entry_set_size(ae, sizeof(buff2));
68 	assertA(0 == archive_write_header(a, ae));
69 	archive_entry_free(ae);
70 	assertA((int)sizeof(buff2) == archive_write_data(a, buff2, sizeof(buff2)));
71 
72 	/* Close out the archive. */
73 	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
74 	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
75 
76 	/* Check that archive_read_data can handle 10*10^6 at a pop. */
77 	assert((a = archive_read_new()) != NULL);
78 	assertA(0 == archive_read_support_format_all(a));
79 	assertA(0 == archive_read_support_filter_all(a));
80 	assertA(0 == archive_read_open_memory(a, buff1, sizeof(buff1)));
81 	assertA(0 == archive_read_next_header(a, &ae));
82 	failure("Wrote 10MB, but didn't read the same amount");
83 	assertEqualIntA(a, sizeof(buff2),archive_read_data(a, buff3, sizeof(buff3)));
84 	failure("Read expected 10MB, but data read didn't match what was written");
85 	assertEqualMem(buff2, buff3, sizeof(buff3));
86 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
87 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
88 
89 	/* Check archive_read_data_into_fd */
90 	assert((a = archive_read_new()) != NULL);
91 	assertA(0 == archive_read_support_format_all(a));
92 	assertA(0 == archive_read_support_filter_all(a));
93 	assertA(0 == archive_read_open_memory(a, buff1, sizeof(buff1)));
94 	assertA(0 == archive_read_next_header(a, &ae));
95 #if defined(__BORLANDC__)
96 	tmpfilefd = open(tmpfilename, O_WRONLY | O_CREAT | O_BINARY);
97 #else
98 	tmpfilefd = open(tmpfilename, O_WRONLY | O_CREAT | O_BINARY, 0777);
99 #endif
100 	assert(tmpfilefd != 0);
101 	assertEqualIntA(a, 0, archive_read_data_into_fd(a, tmpfilefd));
102 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
103 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
104 	close(tmpfilefd);
105 
106 	f = fopen(tmpfilename, "rb");
107 	assert(f != NULL);
108 	assertEqualInt(sizeof(buff3), fread(buff3, 1, sizeof(buff3), f));
109 	fclose(f);
110 	assertEqualMem(buff2, buff3, sizeof(buff3));
111 }
112 
113 #define	FILE_SIZE	0x90000		/* 576 KiB total logical size */
114 
115 static char sparse_buff[0x10000];	/* Holds the (small) stored archive. */
116 static char sparse_data[FILE_SIZE];
117 static char sparse_expected[FILE_SIZE];
118 
119 /*
120  * Regression test for archive_read_data_into_fd() extracting a sparse
121  * file that ends in a hole. For a sparse entry libarchive seeks the
122  * destination descriptor forward to recreate holes. The trailing hole
123  * is created from the EOF handling after the last data block.
124  */
DEFINE_TEST(test_read_data_into_fd_sparse)125 DEFINE_TEST(test_read_data_into_fd_sparse)
126 {
127 	struct archive *a;
128 	struct archive_entry *ae;
129 	size_t archive_size = 0;
130 	const char *skip_sparse_tests;
131 	int fd;
132 
133 	skip_sparse_tests = getenv("SKIP_TEST_SPARSE");
134 	if (skip_sparse_tests != NULL) {
135 		skipping("Skipping sparse tests due to SKIP_TEST_SPARSE "
136 		"environment variable");
137 		return;
138 	}
139 
140 	/*
141 	 * The logical file is all 'a', and the sparse map marks two data
142 	 * regions, everything else (including the tail) is a hole that
143 	 * must read back as zero. The last data region ends at 0x81000
144 	 * while the file size is 0x90000, so the file ends in a hole.
145 	 */
146 	memset(sparse_data, 'a', sizeof(sparse_data));
147 	memset(sparse_expected, 0, sizeof(sparse_expected));
148 	memset(sparse_expected + 0x10000, 'a', 0x1000);
149 	memset(sparse_expected + 0x80000, 'a', 0x1000);
150 
151 	/* Create a sparse pax archive in memory. */
152 	assert((a = archive_write_new()) != NULL);
153 	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_pax(a));
154 	assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a));
155 	assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, sparse_buff,
156 	    sizeof(sparse_buff), &archive_size));
157 
158 	assert((ae = archive_entry_new()) != NULL);
159 	archive_entry_copy_pathname(ae, "file");
160 	archive_entry_set_mode(ae, S_IFREG | 0644);
161 	archive_entry_set_size(ae, FILE_SIZE);
162 	archive_entry_sparse_add_entry(ae, 0x10000, 0x1000);
163 	archive_entry_sparse_add_entry(ae, 0x80000, 0x1000);
164 	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
165 	archive_entry_free(ae);
166 	assertEqualIntA(a, FILE_SIZE,
167 	    archive_write_data(a, sparse_data, sizeof(sparse_data)));
168 	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
169 	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
170 
171 	/* Read it back and extract it into a real file descriptor. */
172 	assert((a = archive_read_new()) != NULL);
173 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
174 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
175 	assertEqualIntA(a, ARCHIVE_OK,
176 	    archive_read_open_memory(a, sparse_buff, archive_size));
177 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
178 	assertEqualInt(FILE_SIZE, archive_entry_size(ae));
179 
180 	fd = open("file", O_WRONLY | O_CREAT | O_BINARY, 0644);
181 	assert(fd >= 0);
182 
183 	assertEqualIntA(a, ARCHIVE_OK, archive_read_data_into_fd(a, fd));
184 
185 	/*
186 	 * archive_read_data_into_fd() seeks over the trailing hole but, like
187 	 * a regular extraction, leaves the on-disk size short. Truncate up
188 	 * to the full size to realize the final hole, as a consumer does.
189 	 */
190 	assert(ftruncate(fd, FILE_SIZE) != -1);
191 	close(fd);
192 
193 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
194 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
195 
196 	/*
197 	 * The reconstructed file must match byte for byte: data regions
198 	 * are 'a', every hole (including the trailing one) is zero.
199 	 */
200 	assertFileSize("file", FILE_SIZE);
201 	assertFileContents(sparse_expected, FILE_SIZE, "file");
202 }
203