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__FBSDID("$FreeBSD$"); 27 28static unsigned char nulls[1000]; 29static unsigned char tmp[1000]; 30static unsigned char buff[10000]; 31size_t data_sizes[] = {0, 5, 511, 512, 513}; 32 33static void verify_read_positions(struct archive *a); 34 35static void 36verify_read_positions(struct archive *a) 37{ 38 struct archive_entry *ae; 39 intmax_t read_position = 0; 40 size_t j; 41 42 /* Initial header position is zero. */ 43 assert(read_position == (intmax_t)archive_read_header_position(a)); 44 for (j = 0; j < sizeof(data_sizes)/sizeof(data_sizes[0]); ++j) { 45 assertA(0 == archive_read_next_header(a, &ae)); 46 assertEqualInt(read_position, 47 (intmax_t)archive_read_header_position(a)); 48 /* Every other entry: read, then skip */ 49 if (j & 1) 50 assertEqualInt(1, 51 archive_read_data(a, tmp, 1)); 52 assertA(0 == archive_read_data_skip(a)); 53 /* read_data_skip() doesn't change header_position */ 54 assertEqualInt(read_position, 55 (intmax_t)archive_read_header_position(a)); 56 57 read_position += 512; /* Size of header. */ 58 read_position += (data_sizes[j] + 511) & ~511; 59 } 60 61 assertA(1 == archive_read_next_header(a, &ae)); 62 assertEqualInt(read_position, (intmax_t)archive_read_header_position(a)); 63 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); 64 assertEqualInt(read_position, (intmax_t)archive_read_header_position(a)); 65} 66 67/* Check that header_position tracks correctly on read. */ 68DEFINE_TEST(test_read_position) 69{ 70 struct archive *a; 71 struct archive_entry *ae; 72 size_t write_pos; 73 size_t i; 74 75 /* Sanity test */ 76 assert(sizeof(nulls) + 512 + 1024 <= sizeof(buff)); 77 78 /* Create an archive. */ 79 assert(NULL != (a = archive_write_new())); 80 assertA(0 == archive_write_set_format_pax_restricted(a)); 81 assertA(0 == archive_write_set_bytes_per_block(a, 512)); 82 assertA(0 == archive_write_open_memory(a, buff, sizeof(buff), &write_pos)); 83 84 for (i = 0; i < sizeof(data_sizes)/sizeof(data_sizes[0]); ++i) { 85 /* Create a simple archive_entry. */ 86 assert((ae = archive_entry_new()) != NULL); 87 archive_entry_set_pathname(ae, "testfile"); 88 archive_entry_set_mode(ae, S_IFREG); 89 archive_entry_set_size(ae, data_sizes[i]); 90 assertA(0 == archive_write_header(a, ae)); 91 archive_entry_free(ae); 92 assertA(data_sizes[i] 93 == (size_t)archive_write_data(a, nulls, sizeof(nulls))); 94 } 95 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a)); 96 assertEqualInt(ARCHIVE_OK, archive_write_free(a)); 97 98 /* Read the archive back with a skip function. */ 99 assert(NULL != (a = archive_read_new())); 100 assertA(0 == archive_read_support_format_tar(a)); 101 assertA(0 == read_open_memory(a, buff, sizeof(buff), 512)); 102 verify_read_positions(a); 103 archive_read_free(a); 104 105 /* Read the archive back without a skip function. */ 106 assert(NULL != (a = archive_read_new())); 107 assertA(0 == archive_read_support_format_tar(a)); 108 assertA(0 == read_open_memory_minimal(a, buff, sizeof(buff), 512)); 109 verify_read_positions(a); 110 archive_read_free(a); 111 112} 113