test_read_set_format.c revision 307798
1/*- 2 * Copyright (c) 2003-2007 Tim Kientzle 3 * Copyright (c) 2012 Andres Mejia 4 * Copyright (c) 2011-2012 Michihiro NAKAJIMA 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27#include "test.h" 28 29DEFINE_TEST(test_read_set_format) 30{ 31 char buff[64]; 32 const char reffile[] = "test_read_format_rar.rar"; 33 const char test_txt[] = "test text document\r\n"; 34 int size = sizeof(test_txt)-1; 35 struct archive_entry *ae; 36 struct archive *a; 37 38 extract_reference_file(reffile); 39 assert((a = archive_read_new()) != NULL); 40 assertA(0 == archive_read_set_format(a, ARCHIVE_FORMAT_RAR)); 41 assertA(0 == archive_read_append_filter(a, ARCHIVE_FILTER_NONE)); 42 assertA(0 == archive_read_open_filename(a, reffile, 10240)); 43 44 /* First header. */ 45 assertA(0 == archive_read_next_header(a, &ae)); 46 assertEqualString("test.txt", archive_entry_pathname(ae)); 47 assertA((int)archive_entry_mtime(ae)); 48 assertA((int)archive_entry_ctime(ae)); 49 assertA((int)archive_entry_atime(ae)); 50 assertEqualInt(20, archive_entry_size(ae)); 51 assertEqualInt(33188, archive_entry_mode(ae)); 52 assertA(size == archive_read_data(a, buff, size)); 53 assertEqualMem(buff, test_txt, size); 54 55 /* Second header. */ 56 assertA(0 == archive_read_next_header(a, &ae)); 57 assertEqualString("testlink", archive_entry_pathname(ae)); 58 assertA((int)archive_entry_mtime(ae)); 59 assertA((int)archive_entry_ctime(ae)); 60 assertA((int)archive_entry_atime(ae)); 61 assertEqualInt(0, archive_entry_size(ae)); 62 assertEqualInt(41471, archive_entry_mode(ae)); 63 assertEqualString("test.txt", archive_entry_symlink(ae)); 64 assertEqualIntA(a, 0, archive_read_data(a, buff, sizeof(buff))); 65 66 /* Third header. */ 67 assertA(0 == archive_read_next_header(a, &ae)); 68 assertEqualString("testdir/test.txt", archive_entry_pathname(ae)); 69 assertA((int)archive_entry_mtime(ae)); 70 assertA((int)archive_entry_ctime(ae)); 71 assertA((int)archive_entry_atime(ae)); 72 assertEqualInt(20, archive_entry_size(ae)); 73 assertEqualInt(33188, archive_entry_mode(ae)); 74 assertA(size == archive_read_data(a, buff, size)); 75 assertEqualMem(buff, test_txt, size); 76 77 /* Fourth header. */ 78 assertA(0 == archive_read_next_header(a, &ae)); 79 assertEqualString("testdir", archive_entry_pathname(ae)); 80 assertA((int)archive_entry_mtime(ae)); 81 assertA((int)archive_entry_ctime(ae)); 82 assertA((int)archive_entry_atime(ae)); 83 assertEqualInt(0, archive_entry_size(ae)); 84 assertEqualInt(16877, archive_entry_mode(ae)); 85 86 /* Fifth header. */ 87 assertA(0 == archive_read_next_header(a, &ae)); 88 assertEqualString("testemptydir", archive_entry_pathname(ae)); 89 assertA((int)archive_entry_mtime(ae)); 90 assertA((int)archive_entry_ctime(ae)); 91 assertA((int)archive_entry_atime(ae)); 92 assertEqualInt(0, archive_entry_size(ae)); 93 assertEqualInt(16877, archive_entry_mode(ae)); 94 95 /* Test EOF */ 96 assertA(1 == archive_read_next_header(a, &ae)); 97 assertEqualInt(5, archive_file_count(a)); 98 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); 99 assertEqualInt(ARCHIVE_OK, archive_read_free(a)); 100} 101 102DEFINE_TEST(test_read_set_wrong_format) 103{ 104 const char reffile[] = "test_read_format_zip.zip"; 105 struct archive_entry *ae; 106 struct archive *a; 107 108 extract_reference_file(reffile); 109 assert((a = archive_read_new()) != NULL); 110 assertA(0 == archive_read_set_format(a, ARCHIVE_FORMAT_RAR)); 111 assertA(0 == archive_read_append_filter(a, ARCHIVE_FILTER_NONE)); 112 assertA(0 == archive_read_open_filename(a, reffile, 10240)); 113 114 /* Check that this actually fails, then close the archive. */ 115 assertA(archive_read_next_header(a, &ae) < (ARCHIVE_WARN)); 116 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); 117 assertEqualInt(ARCHIVE_OK, archive_read_free(a)); 118} 119 120static unsigned char archive[] = { 12131,139,8,0,222,'C','p','C',0,3,211,'c',160,'=','0','0','0','0','7','5','U', 1220,210,134,230,166,6,200,'4',28,'(',24,26,24,27,155,24,152,24,154,27,155,')', 12324,24,26,152,154,25,'2','(',152,210,193,'m',12,165,197,'%',137,'E','@',167, 124148,'d',230,226,'U','G','H',30,234,15,'8','=',10,'F',193,'(',24,5,131,28, 1250,0,29,172,5,240,0,6,0,0}; 126 127DEFINE_TEST(test_read_append_filter) 128{ 129 struct archive_entry *ae; 130 struct archive *a; 131 int r; 132 133 assert((a = archive_read_new()) != NULL); 134 assertA(0 == archive_read_set_format(a, ARCHIVE_FORMAT_TAR)); 135 r = archive_read_append_filter(a, ARCHIVE_FILTER_GZIP); 136 if (r == ARCHIVE_WARN && !canGzip()) { 137 skipping("gzip reading not fully supported on this platform"); 138 assertEqualInt(ARCHIVE_OK, archive_read_free(a)); 139 return; 140 } 141 assertEqualInt(ARCHIVE_OK, 142 archive_read_open_memory(a, archive, sizeof(archive))); 143 assertEqualInt(ARCHIVE_OK, archive_read_next_header(a, &ae)); 144 assertEqualInt(1, archive_file_count(a)); 145 assertEqualInt(archive_filter_code(a, 0), ARCHIVE_COMPRESSION_GZIP); 146 assertEqualInt(archive_format(a), ARCHIVE_FORMAT_TAR_USTAR); 147 assertEqualInt(ARCHIVE_OK, archive_read_close(a)); 148 assertEqualInt(ARCHIVE_OK,archive_read_free(a)); 149} 150 151DEFINE_TEST(test_read_append_wrong_filter) 152{ 153 struct archive_entry *ae; 154 struct archive *a; 155 int r; 156 157 assert((a = archive_read_new()) != NULL); 158 assertA(0 == archive_read_set_format(a, ARCHIVE_FORMAT_TAR)); 159 r = archive_read_append_filter(a, ARCHIVE_FILTER_XZ); 160 if (r == ARCHIVE_WARN && !canXz()) { 161 skipping("xz reading not fully supported on this platform"); 162 assertEqualInt(ARCHIVE_OK, archive_read_free(a)); 163 return; 164 } 165 assertEqualInt(ARCHIVE_OK, 166 archive_read_open_memory(a, archive, sizeof(archive))); 167 assertA(archive_read_next_header(a, &ae) < (ARCHIVE_WARN)); 168 if (r == ARCHIVE_WARN && canXz()) { 169 assertEqualIntA(a, ARCHIVE_WARN, archive_read_close(a)); 170 } else { 171 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); 172 } 173 assertEqualInt(ARCHIVE_OK,archive_read_free(a)); 174} 175 176DEFINE_TEST(test_read_append_filter_program) 177{ 178 struct archive_entry *ae; 179 struct archive *a; 180 181 if (!canGzip()) { 182 skipping("Can't run gzip program on this platform"); 183 return; 184 } 185 assert((a = archive_read_new()) != NULL); 186 assertA(0 == archive_read_set_format(a, ARCHIVE_FORMAT_TAR)); 187 assertEqualIntA(a, ARCHIVE_OK, 188 archive_read_append_filter_program(a, "gzip -d")); 189 assertEqualIntA(a, ARCHIVE_OK, 190 archive_read_open_memory(a, archive, sizeof(archive))); 191 assertEqualIntA(a, ARCHIVE_OK, 192 archive_read_next_header(a, &ae)); 193 assertEqualInt(archive_filter_code(a, 0), ARCHIVE_FILTER_PROGRAM); 194 assertEqualInt(archive_format(a), ARCHIVE_FORMAT_TAR_USTAR); 195 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); 196 assertEqualInt(ARCHIVE_OK, archive_read_free(a)); 197} 198 199DEFINE_TEST(test_read_append_filter_wrong_program) 200{ 201 struct archive_entry *ae; 202 struct archive *a; 203 int fd; 204 fpos_t pos; 205 206 /* 207 * If we have "bunzip2 -q", try using that. 208 */ 209 if (!canRunCommand("bunzip2 -h")) { 210 skipping("Can't run bunzip2 program on this platform"); 211 return; 212 } 213 214 /* bunzip2 will write to stderr, redirect it to a file */ 215 fflush(stderr); 216 fgetpos(stderr, &pos); 217 fd = dup(fileno(stderr)); 218 freopen("stderr1", "w", stderr); 219 220 assert((a = archive_read_new()) != NULL); 221 assertA(0 == archive_read_set_format(a, ARCHIVE_FORMAT_TAR)); 222 assertEqualIntA(a, ARCHIVE_OK, 223 archive_read_append_filter_program(a, "bunzip2 -q")); 224 assertEqualIntA(a, ARCHIVE_OK, 225 archive_read_open_memory(a, archive, sizeof(archive))); 226 assertA(archive_read_next_header(a, &ae) < (ARCHIVE_WARN)); 227 assertEqualIntA(a, ARCHIVE_WARN, archive_read_close(a)); 228 assertEqualInt(ARCHIVE_OK, archive_read_free(a)); 229 230 /* restore stderr */ 231 fflush(stderr); 232 dup2(fd, fileno(stderr)); 233 close(fd); 234 clearerr(stderr); 235 fsetpos(stderr, &pos); 236 237 assertTextFileContents("bunzip2: (stdin) is not a bzip2 file.\n", "stderr1"); 238} 239