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