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$");
31 DEFINE_TEST(test_read_truncated)
33 struct archive_entry *ae;
38 /* Create a new archive in memory. */
39 assert((a = archive_write_new()) != NULL);
40 assertA(0 == archive_write_set_format_ustar(a));
41 assertA(0 == archive_write_set_compression_none(a));
42 assertA(0 == archive_write_open_memory(a, buff, sizeof(buff), &used));
47 assert((ae = archive_entry_new()) != NULL);
48 archive_entry_copy_pathname(ae, "file");
49 archive_entry_set_mode(ae, S_IFREG | 0755);
50 for (i = 0; i < sizeof(buff2); i++)
51 buff2[i] = (unsigned char)rand();
52 archive_entry_set_size(ae, sizeof(buff2));
53 assertA(0 == archive_write_header(a, ae));
54 archive_entry_free(ae);
55 assertA(sizeof(buff2) == archive_write_data(a, buff2, sizeof(buff2)));
57 /* Close out the archive. */
58 assertA(0 == archive_write_close(a));
59 #if ARCHIVE_VERSION_NUMBER < 2000000
60 archive_write_finish(a);
62 assertA(0 == archive_write_finish(a));
65 /* Now, read back a truncated version of the archive and
66 * verify that we get an appropriate error. */
67 for (i = 1; i < used + 100; i += 100) {
68 assert((a = archive_read_new()) != NULL);
69 assertA(0 == archive_read_support_format_all(a));
70 assertA(0 == archive_read_support_compression_all(a));
71 assertA(0 == archive_read_open_memory(a, buff, i));
74 assertA(ARCHIVE_FATAL == archive_read_next_header(a, &ae));
77 assertA(0 == archive_read_next_header(a, &ae));
80 if (i < 512 + sizeof(buff2)) {
81 assertA(ARCHIVE_FATAL == archive_read_data(a, buff2, sizeof(buff2)));
84 assertA(sizeof(buff2) == archive_read_data(a, buff2, sizeof(buff2)));
87 /* Verify the end of the archive. */
88 /* Archive must be long enough to capture a 512-byte
89 * block of zeroes after the entry. (POSIX requires a
90 * second block of zeros to be written but libarchive
91 * does not return an error if it can't consume
93 if (i < 512 + 512*((sizeof(buff2) + 511)/512) + 512) {
94 assertA(ARCHIVE_FATAL == archive_read_next_header(a, &ae));
96 assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
99 assert(0 == archive_read_close(a));
100 #if ARCHIVE_VERSION_NUMBER < 2000000
101 archive_read_finish(a);
103 assert(0 == archive_read_finish(a));
109 /* Same as above, except skip the body instead of reading it. */
110 for (i = 1; i < used + 100; i += 100) {
111 assert((a = archive_read_new()) != NULL);
112 assertA(0 == archive_read_support_format_all(a));
113 assertA(0 == archive_read_support_compression_all(a));
114 assertA(0 == archive_read_open_memory(a, buff, i));
117 assertA(ARCHIVE_FATAL == archive_read_next_header(a, &ae));
120 assertA(0 == archive_read_next_header(a, &ae));
123 if (i < 512 + 512*((sizeof(buff2)+511)/512)) {
124 assertA(ARCHIVE_FATAL == archive_read_data_skip(a));
127 assertA(ARCHIVE_OK == archive_read_data_skip(a));
130 /* Verify the end of the archive. */
131 /* Archive must be long enough to capture a 512-byte
132 * block of zeroes after the entry. (POSIX requires a
133 * second block of zeros to be written but libarchive
134 * does not return an error if it can't consume
136 if (i < 512 + 512*((sizeof(buff2) + 511)/512) + 512) {
137 assertA(ARCHIVE_FATAL == archive_read_next_header(a, &ae));
139 assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
142 assert(0 == archive_read_close(a));
143 #if ARCHIVE_VERSION_NUMBER < 2000000
144 archive_read_finish(a);
146 assert(0 == archive_read_finish(a));