]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - lib/libarchive/test/test_read_position.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / lib / 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[10000000];
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         const size_t data_size = 1000000;
38
39         /* Create a simple archive_entry. */
40         assert((ae = archive_entry_new()) != NULL);
41         archive_entry_set_pathname(ae, "testfile");
42         archive_entry_set_mode(ae, S_IFREG);
43         archive_entry_set_size(ae, data_size);
44
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         assertA(0 == archive_write_header(a, ae));
50         archive_entry_free(ae);
51         assertA(data_size == (size_t)archive_write_data(a, nulls, sizeof(nulls)));
52 #if ARCHIVE_API_VERSION > 1
53         assertA(0 == archive_write_finish(a));
54 #else
55         assertA(0 == archive_write_close(a));
56         archive_write_finish(a);
57 #endif
58         /* 512-byte header + data_size (rounded up) + 1024 end-of-archive */
59         assert(write_pos == ((512 + data_size + 1024 + 511)/512)*512);
60
61         /* Read the archive back. */
62         assert(NULL != (a = archive_read_new()));
63         assertA(0 == archive_read_support_format_tar(a));
64         assertA(0 == archive_read_open_memory2(a, buff, sizeof(buff), 512));
65         assert((intmax_t)0 == (intmax_t)archive_read_header_position(a));
66         assertA(0 == archive_read_next_header(a, &ae));
67         assert((intmax_t)0 == (intmax_t)archive_read_header_position(a));
68         assertA(0 == archive_read_data_skip(a));
69         assert((intmax_t)0 == (intmax_t)archive_read_header_position(a));
70         assertA(1 == archive_read_next_header(a, &ae));
71         assert((intmax_t)((data_size + 511 + 512)/512)*512 == (intmax_t)archive_read_header_position(a));
72         assertA(0 == archive_read_close(a));
73         assert((intmax_t)((data_size + 511 + 512)/512)*512 == (intmax_t)archive_read_header_position(a));
74         archive_read_finish(a);
75 }