]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/libarchive/libarchive/test/test_read_position.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / libarchive / libarchive / test / test_read_position.c
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
28 static unsigned char nulls[10000];
29 static unsigned char  buff[10000000];
30
31 /* Check that header_position tracks correctly on read. */
32 DEFINE_TEST(test_read_position)
33 {
34         struct archive *a;
35         struct archive_entry *ae;
36         size_t write_pos;
37         intmax_t read_position;
38         size_t i, j;
39         size_t data_sizes[] = {0, 5, 511, 512, 513};
40
41         /* Sanity test */
42         assert(sizeof(nulls) + 512 + 1024 <= sizeof(buff));
43
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));
49
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);
58                 assertA(data_sizes[i]
59                     == (size_t)archive_write_data(a, nulls, sizeof(nulls)));
60         }
61         assertA(0 == archive_write_close(a));
62         assertA(0 == archive_write_finish(a));
63
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));
68
69         read_position = 0;
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));
74                 assert(read_position
75                     == (intmax_t)archive_read_header_position(a));
76                 /* Every other entry: read, then skip */
77                 if (j & 1)
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 */
82                 assert(read_position
83                     == (intmax_t)archive_read_header_position(a));
84
85                 read_position += 512; /* Size of header. */
86                 read_position += (data_sizes[j] + 511) & ~511;
87         }
88
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);
94 }