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 /* 30 Execute 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. 34 name=test_compat_lzma_1 35 zcmd=lzma 36 zsuffix=lzma 37 ztar_suffix=tlz 38 dir="$name`date +%Y%m%d%H%M%S`.$USER" 39 mktarfile() 40 { 41 mkdir $dir 42 echo "f1" > $dir/f1 43 echo "f2" > $dir/f2 44 echo "f3" > $dir/f3 45 mkdir $dir/d1 46 echo "f1" > $dir/d1/f1 47 echo "f2" > $dir/d1/f2 48 echo "f3" > $dir/d1/f3 49 (cd $dir; tar cf ../$name.tar f1 f2 f3 d1/f1 d1/f2 d1/f3) 50 rm -r $dir 51 } 52 mktarfile 53 $zcmd $name.tar 54 mv $name.tar.$zsuffix $name.$ztar_suffix 55 echo "This is unrelated junk data at the end of the file" >> $name.$ztar_suffix 56 uuencode $name.$ztar_suffix $name.$ztar_suffix > $name.$ztar_suffix.uu 57 rm -f $name.$ztar_suffix 58 # 59 # Use option -e 60 # 61 name=test_compat_lzma_2 62 dir="$name`date +%Y%m%d%H%M%S`.$USER" 63 mktarfile 64 $zcmd -e $name.tar 65 mv $name.tar.$zsuffix $name.$ztar_suffix 66 uuencode $name.$ztar_suffix $name.$ztar_suffix > $name.$ztar_suffix.uu 67 rm -f $name.$ztar_suffix 68 # 69 # Use lzma command of LZMA SDK with option -d12. 70 # 71 name=test_compat_lzma_3 72 zcmd=lzmasdk # Change this path to use lzma of LZMA SDK. 73 dir="$name`date +%Y%m%d%H%M%S`.$USER" 74 mktarfile 75 $zcmd e -d12 $name.tar $name.$ztar_suffix 76 rm -f $name.tar 77 uuencode $name.$ztar_suffix $name.$ztar_suffix > $name.$ztar_suffix.uu 78 rm -f $name.$ztar_suffix 79 80 exit 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 */ 101 static void 102 compat_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_filter_all(a)); 111 r = archive_read_support_filter_lzma(a); 112 if (r == ARCHIVE_WARN) { 113 skipping("lzma reading not fully supported on this platform"); 114 assertEqualInt(ARCHIVE_OK, archive_read_free(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_filter_code(a, 0), ARCHIVE_FILTER_LZMA); 134 assertEqualString(archive_filter_name(a, 0), "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_free(a)); 139 } 140 141 142 DEFINE_TEST(test_compat_lzma) 143 { 144 /* This sample has been added junk data 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