xref: /freebsd-src/contrib/libarchive/libarchive/test/test_compat_lzip.c (revision bd66c1b43e33540205dbc1187c2f2a15c58b57ba)
1 /*-
2  * Copyright (c) 2010 Michihiro NAKAJIMA
3  * Copyright (c) 2003-2008 Tim Kientzle
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 /*
29 Execute the following to rebuild the data for this program:
30    tail -n +33 test_compat_lzip.c | /bin/sh
31 
32 # Use lzip command.
33 zcmd=lzip
34 zsuffix=lz
35 ztar_suffix=tlz
36 mktarfile()
37 {
38 mkdir $dir
39 echo "f1" > $dir/f1
40 echo "f2" > $dir/f2
41 echo "f3" > $dir/f3
42 mkdir $dir/d1
43 echo "f1" > $dir/d1/f1
44 echo "f2" > $dir/d1/f2
45 echo "f3" > $dir/d1/f3
46 (cd $dir; tar cf ../$name.tar f1 f2 f3 d1/f1 d1/f2 d1/f3)
47 rm -r $dir
48 }
49 #
50 # Make a lzip file from split tar file.
51 #
52 name=test_compat_lzip_1
53 dir="$name`date +%Y%m%d%H%M%S`.$USER"
54 mktarfile
55 split -b 3600 $name.tar $name.tar.
56 rm $name.tar
57 $zcmd $name.tar.*
58 cat $name.tar.*.$zsuffix > $name.$ztar_suffix
59 rm $name.tar.*.$zsuffix
60 uuencode $name.$ztar_suffix $name.$ztar_suffix > $name.$ztar_suffix.uu
61 rm -f $name.$ztar_suffix
62 #
63 # Make a lzip file with junk data at the end of the file.
64 #
65 name=test_compat_lzip_2
66 dir="$name`date +%Y%m%d%H%M%S`.$USER"
67 mktarfile
68 $zcmd $name.tar
69 mv $name.tar.$zsuffix $name.$ztar_suffix
70 echo "This is unrelated junk data at the end of the file" >> $name.$ztar_suffix
71 uuencode $name.$ztar_suffix $name.$ztar_suffix > $name.$ztar_suffix.uu
72 rm -f $name.$ztar_suffix
73 
74 exit 0
75 */
76 
77 /*
78  * Verify our ability to read sample files compatibly with lzip.
79  *
80  * In particular:
81  *  * lzip will read multiple lzip streams, concatenating the output
82  *  * lzip will stop at the end of a stream if the following data
83  *    doesn't start with a gzip signature.
84  *
85  */
86 
87 /*
88  * All of the sample files have the same contents; they're just
89  * compressed in different ways.
90  */
91 static void
92 compat_lzip(const char *name)
93 {
94 	const char *n[7] = { "f1", "f2", "f3", "d1/f1", "d1/f2", "d1/f3", NULL };
95 	struct archive_entry *ae;
96 	struct archive *a;
97 	int i, r;
98 
99 	assert((a = archive_read_new()) != NULL);
100 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
101 	r = archive_read_support_filter_lzip(a);
102 	if (r == ARCHIVE_WARN) {
103 		skipping("lzip reading not fully supported on this platform");
104 		assertEqualInt(ARCHIVE_OK, archive_read_free(a));
105 		return;
106 	}
107 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
108 	extract_reference_file(name);
109 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, name, 2));
110 
111 	/* Read entries, match up names with list above. */
112 	for (i = 0; i < 6; ++i) {
113 		failure("Could not read file %d (%s) from %s", i, n[i], name);
114 		assertEqualIntA(a, ARCHIVE_OK,
115 		    archive_read_next_header(a, &ae));
116 		assertEqualString(n[i], archive_entry_pathname(ae));
117 	}
118 
119 	/* Verify the end-of-archive. */
120 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
121 
122 	/* Verify that the format detection worked. */
123 	assertEqualInt(archive_filter_code(a, 0), ARCHIVE_FILTER_LZIP);
124 	assertEqualString(archive_filter_name(a, 0), "lzip");
125 	assertEqualInt(archive_format(a), ARCHIVE_FORMAT_TAR_USTAR);
126 
127 	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
128 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
129 }
130 
131 
132 static void
133 compat_lzip_3(const char *name)
134 {
135 	struct archive_entry *ae;
136 	struct archive *a;
137 	int r;
138 	const int data_size = 65537;
139 	static uint8_t buff[65537];
140 
141 	assert((a = archive_read_new()) != NULL);
142 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
143 	r = archive_read_support_filter_lzip(a);
144 	if (r == ARCHIVE_WARN) {
145 		skipping("lzip reading not fully supported on this platform");
146 		assertEqualInt(ARCHIVE_OK, archive_read_free(a));
147 		return;
148 	}
149 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_raw(a));
150 	extract_reference_file(name);
151 	assertEqualIntA(a, ARCHIVE_OK,
152 	    archive_read_open_filename(a, name, 64 * 1024));
153 
154 	/* Read an entry. */
155 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
156 	assertEqualString("data", archive_entry_pathname(ae));
157 	/* Verify that whole data could be read. */
158 	assertEqualInt(data_size, archive_read_data(a, buff, data_size));
159 
160 	/* Verify the end-of-archive. */
161 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
162 
163 	/* Verify that the format detection worked. */
164 	assertEqualInt(archive_filter_code(a, 0), ARCHIVE_FILTER_LZIP);
165 	assertEqualString(archive_filter_name(a, 0), "lzip");
166 	assertEqualInt(archive_format(a), ARCHIVE_FORMAT_RAW);
167 
168 	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
169 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
170 }
171 
172 
173 static void
174 compat_lzip_4(const char *name)
175 {
176 	struct archive_entry *ae;
177 	struct archive *a;
178 	int r;
179 
180 	assert((a = archive_read_new()) != NULL);
181 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
182 	r = archive_read_support_filter_lzip(a);
183 	if (r == ARCHIVE_WARN) {
184 		skipping("lzip reading not fully supported on this platform");
185 		assertEqualInt(ARCHIVE_OK, archive_read_free(a));
186 		return;
187 	}
188 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
189 	extract_reference_file(name);
190 	assertEqualIntA(a, ARCHIVE_OK,
191 	    archive_read_open_filename(a, name, 64 * 1024));
192 
193 	/* Read an entry. */
194 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
195 	assertEqualString("test.bin", archive_entry_pathname(ae));
196 
197 	/* Verify the end-of-archive. */
198 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
199 
200 	/* Verify that the format detection worked. */
201 	assertEqualInt(archive_filter_code(a, 0), ARCHIVE_FILTER_LZIP);
202 	assertEqualString(archive_filter_name(a, 0), "lzip");
203 	assertEqualInt(archive_format(a), ARCHIVE_FORMAT_TAR_USTAR);
204 
205 	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
206 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
207 }
208 
209 
210 DEFINE_TEST(test_compat_lzip)
211 {
212         /* This sample has been 'split', each piece compressed separately,
213          * then concatenated.  lzip will emit the concatenated result. */
214         compat_lzip("test_compat_lzip_1.tlz");
215         /* This sample has been compressed as a single stream, but then
216          * some unrelated garbage text has been appended to the end. */
217         compat_lzip("test_compat_lzip_2.tlz");
218 
219         /* These samples have been compressed as multi stream and an eof
220          * of a member is at a read buffer boundary. */
221         compat_lzip_3("test_compat_lzip_3.lz");
222         compat_lzip_4("test_compat_lzip_4.tlz");
223 }
224