xref: /freebsd/contrib/libarchive/libarchive/test/test_compat_lzma.c (revision b9128a37faafede823eb456aa65a11ac69997284)
1 /*-
2  * Copyright (c) 2009 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_lzma.c | /bin/sh
31 
32 # Use lzma command of XZ Utils.
33 name=test_compat_lzma_1
34 zcmd=lzma
35 zsuffix=lzma
36 ztar_suffix=tlz
37 dir="$name`date +%Y%m%d%H%M%S`.$USER"
38 mktarfile()
39 {
40 mkdir $dir
41 echo "f1" > $dir/f1
42 echo "f2" > $dir/f2
43 echo "f3" > $dir/f3
44 mkdir $dir/d1
45 echo "f1" > $dir/d1/f1
46 echo "f2" > $dir/d1/f2
47 echo "f3" > $dir/d1/f3
48 (cd $dir; tar cf ../$name.tar f1 f2 f3 d1/f1 d1/f2 d1/f3)
49 rm -r $dir
50 }
51 mktarfile
52 $zcmd $name.tar
53 mv $name.tar.$zsuffix $name.$ztar_suffix
54 echo "This is unrelated junk data at the end of the file" >> $name.$ztar_suffix
55 uuencode $name.$ztar_suffix $name.$ztar_suffix > $name.$ztar_suffix.uu
56 rm -f $name.$ztar_suffix
57 #
58 # Use option -e
59 #
60 name=test_compat_lzma_2
61 dir="$name`date +%Y%m%d%H%M%S`.$USER"
62 mktarfile
63 $zcmd -e $name.tar
64 mv $name.tar.$zsuffix $name.$ztar_suffix
65 uuencode $name.$ztar_suffix $name.$ztar_suffix > $name.$ztar_suffix.uu
66 rm -f $name.$ztar_suffix
67 #
68 # Use lzma command of LZMA SDK with option -d12.
69 #
70 name=test_compat_lzma_3
71 zcmd=lzmasdk	# Change this path to use lzma of LZMA SDK.
72 dir="$name`date +%Y%m%d%H%M%S`.$USER"
73 mktarfile
74 $zcmd e -d12 $name.tar $name.$ztar_suffix
75 rm -f $name.tar
76 uuencode $name.$ztar_suffix $name.$ztar_suffix > $name.$ztar_suffix.uu
77 rm -f $name.$ztar_suffix
78 
79 exit 0
80 */
81 
82 /*
83  * Verify our ability to read sample files compatibly with unlzma.
84  *
85  * In particular:
86  *  * unlzma will read multiple lzma streams, concatenating the output
87  *  * unlzma will read lzma streams which is made by lzma with option -e,
88  *    concatenating the output
89  *
90  * Verify our ability to read sample files compatibly with lzma of
91  * LZMA SDK.
92  *  * lzma will read lzma streams which is made by lzma with option -d12,
93  *    concatenating the output
94  */
95 
96 /*
97  * All of the sample files have the same contents; they're just
98  * compressed in different ways.
99  */
100 static void
101 compat_lzma(const char *name)
102 {
103 	const char *n[7] = { "f1", "f2", "f3", "d1/f1", "d1/f2", "d1/f3", NULL };
104 	struct archive_entry *ae;
105 	struct archive *a;
106 	int i, r;
107 
108 	assert((a = archive_read_new()) != NULL);
109 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
110 	r = archive_read_support_filter_lzma(a);
111 	if (r == ARCHIVE_WARN) {
112 		skipping("lzma reading not fully supported on this platform");
113 		assertEqualInt(ARCHIVE_OK, archive_read_free(a));
114 		return;
115 	}
116 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
117 	extract_reference_file(name);
118 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, name, 2));
119 
120 	/* Read entries, match up names with list above. */
121 	for (i = 0; i < 6; ++i) {
122 		failure("Could not read file %d (%s) from %s", i, n[i], name);
123 		assertEqualIntA(a, ARCHIVE_OK,
124 		    archive_read_next_header(a, &ae));
125 		assertEqualString(n[i], archive_entry_pathname(ae));
126 	}
127 
128 	/* Verify the end-of-archive. */
129 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
130 
131 	/* Verify that the format detection worked. */
132 	assertEqualInt(archive_filter_code(a, 0), ARCHIVE_FILTER_LZMA);
133 	assertEqualString(archive_filter_name(a, 0), "lzma");
134 	assertEqualInt(archive_format(a), ARCHIVE_FORMAT_TAR_USTAR);
135 
136 	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
137 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
138 }
139 
140 
141 DEFINE_TEST(test_compat_lzma)
142 {
143 	/* This sample has been added junk data to its tail. */
144 	compat_lzma("test_compat_lzma_1.tlz");
145 	/* This sample has been made by lzma with option -e,
146 	 * the first byte of which is 0x5e.
147 	 * Not supported in libarchive 2.7.* and earlier */
148 	compat_lzma("test_compat_lzma_2.tlz");
149 	/* This sample has been made by lzma of LZMA SDK with
150 	 * option -d12, second byte and third byte of which is
151 	 * not zero.
152 	 * Not supported in libarchive 2.7.* and earlier */
153 	compat_lzma("test_compat_lzma_3.tlz");
154 }
155