2 * Copyright (c) 2003-2007 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
26 __FBSDID("$FreeBSD$");
28 static unsigned char nulls[1000];
29 static unsigned char tmp[1000];
30 static unsigned char buff[10000];
31 size_t data_sizes[] = {0, 5, 511, 512, 513};
33 static void verify_read_positions(struct archive *a);
36 verify_read_positions(struct archive *a)
38 struct archive_entry *ae;
39 intmax_t read_position = 0;
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 */
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));
57 read_position += 512; /* Size of header. */
58 read_position += (data_sizes[j] + 511) & ~511;
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));
67 /* Check that header_position tracks correctly on read. */
68 DEFINE_TEST(test_read_position)
71 struct archive_entry *ae;
76 assert(sizeof(nulls) + 512 + 1024 <= sizeof(buff));
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));
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);
93 == (size_t)archive_write_data(a, nulls, sizeof(nulls)));
95 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
96 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
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);
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);