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[10000];
29 static unsigned char buff[10000000];
31 /* Check that header_position tracks correctly on read. */
32 DEFINE_TEST(test_read_position)
35 struct archive_entry *ae;
37 intmax_t read_position;
39 size_t data_sizes[] = {0, 5, 511, 512, 513};
42 assert(sizeof(nulls) + 512 + 1024 <= sizeof(buff));
44 /* Create an archive. */
45 assert(NULL != (a = archive_write_new()));
46 assertA(0 == archive_write_set_format_pax_restricted(a));
47 assertA(0 == archive_write_set_bytes_per_block(a, 512));
48 assertA(0 == archive_write_open_memory(a, buff, sizeof(buff), &write_pos));
50 for (i = 0; i < sizeof(data_sizes)/sizeof(data_sizes[0]); ++i) {
51 /* Create a simple archive_entry. */
52 assert((ae = archive_entry_new()) != NULL);
53 archive_entry_set_pathname(ae, "testfile");
54 archive_entry_set_mode(ae, S_IFREG);
55 archive_entry_set_size(ae, data_sizes[i]);
56 assertA(0 == archive_write_header(a, ae));
57 archive_entry_free(ae);
59 == (size_t)archive_write_data(a, nulls, sizeof(nulls)));
61 assertA(0 == archive_write_close(a));
62 assertA(0 == archive_write_finish(a));
64 /* Read the archive back. */
65 assert(NULL != (a = archive_read_new()));
66 assertA(0 == archive_read_support_format_tar(a));
67 assertA(0 == archive_read_open_memory2(a, buff, sizeof(buff), 512));
70 /* Initial header position is zero. */
71 assert(read_position == (intmax_t)archive_read_header_position(a));
72 for (j = 0; j < i; ++j) {
73 assertA(0 == archive_read_next_header(a, &ae));
75 == (intmax_t)archive_read_header_position(a));
76 /* Every other entry: read, then skip */
78 assertEqualInt(ARCHIVE_OK,
79 archive_read_data_into_buffer(a, buff, 1));
80 assertA(0 == archive_read_data_skip(a));
81 /* read_data_skip() doesn't change header_position */
83 == (intmax_t)archive_read_header_position(a));
85 read_position += 512; /* Size of header. */
86 read_position += (data_sizes[j] + 511) & ~511;
89 assertA(1 == archive_read_next_header(a, &ae));
90 assert(read_position == (intmax_t)archive_read_header_position(a));
91 assertA(0 == archive_read_close(a));
92 assert(read_position == (intmax_t)archive_read_header_position(a));
93 archive_read_finish(a);