1 /*- 2 * Copyright (c) 2003-2007 Tim Kientzle 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 #include "test.h" 26 27 static unsigned char nulls[1000]; 28 static unsigned char tmp[1000]; 29 static unsigned char buff[10000]; 30 size_t data_sizes[] = {0, 5, 511, 512, 513}; 31 32 static void verify_read_positions(struct archive *a); 33 34 static void 35 verify_read_positions(struct archive *a) 36 { 37 struct archive_entry *ae; 38 intmax_t read_position = 0; 39 size_t j; 40 41 /* Initial header position is zero. */ 42 assert(read_position == (intmax_t)archive_read_header_position(a)); 43 for (j = 0; j < sizeof(data_sizes)/sizeof(data_sizes[0]); ++j) { 44 assertA(0 == archive_read_next_header(a, &ae)); 45 assertEqualInt(read_position, 46 (intmax_t)archive_read_header_position(a)); 47 /* Every other entry: read, then skip */ 48 if (j & 1) 49 assertEqualInt(1, 50 archive_read_data(a, tmp, 1)); 51 assertA(0 == archive_read_data_skip(a)); 52 /* read_data_skip() doesn't change header_position */ 53 assertEqualInt(read_position, 54 (intmax_t)archive_read_header_position(a)); 55 56 read_position += 512; /* Size of header. */ 57 read_position += (data_sizes[j] + 511) & ~511; 58 } 59 60 assertA(1 == archive_read_next_header(a, &ae)); 61 assertEqualInt(read_position, (intmax_t)archive_read_header_position(a)); 62 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); 63 assertEqualInt(read_position, (intmax_t)archive_read_header_position(a)); 64 } 65 66 /* Check that header_position tracks correctly on read. */ 67 DEFINE_TEST(test_read_position) 68 { 69 struct archive *a; 70 struct archive_entry *ae; 71 size_t write_pos; 72 size_t i; 73 74 /* Sanity test */ 75 assert(sizeof(nulls) + 512 + 1024 <= sizeof(buff)); 76 77 /* Create an archive. */ 78 assert(NULL != (a = archive_write_new())); 79 assertA(0 == archive_write_set_format_pax_restricted(a)); 80 assertA(0 == archive_write_set_bytes_per_block(a, 512)); 81 assertA(0 == archive_write_open_memory(a, buff, sizeof(buff), &write_pos)); 82 83 for (i = 0; i < sizeof(data_sizes)/sizeof(data_sizes[0]); ++i) { 84 /* Create a simple archive_entry. */ 85 assert((ae = archive_entry_new()) != NULL); 86 archive_entry_set_pathname(ae, "testfile"); 87 archive_entry_set_mode(ae, S_IFREG); 88 archive_entry_set_size(ae, data_sizes[i]); 89 assertA(0 == archive_write_header(a, ae)); 90 archive_entry_free(ae); 91 assertA(data_sizes[i] 92 == (size_t)archive_write_data(a, nulls, sizeof(nulls))); 93 } 94 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a)); 95 assertEqualInt(ARCHIVE_OK, archive_write_free(a)); 96 97 /* Read the archive back with a skip function. */ 98 assert(NULL != (a = archive_read_new())); 99 assertA(0 == archive_read_support_format_tar(a)); 100 assertA(0 == read_open_memory(a, buff, sizeof(buff), 512)); 101 verify_read_positions(a); 102 archive_read_free(a); 103 104 /* Read the archive back without a skip function. */ 105 assert(NULL != (a = archive_read_new())); 106 assertA(0 == archive_read_support_format_tar(a)); 107 assertA(0 == read_open_memory_minimal(a, buff, sizeof(buff), 512)); 108 verify_read_positions(a); 109 archive_read_free(a); 110 111 } 112