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 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_ustar(a));
41 assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a));
42 assertEqualIntA(a, ARCHIVE_OK, 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 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
54 archive_entry_free(ae);
55 assertEqualIntA(a, sizeof(buff2), archive_write_data(a, buff2, sizeof(buff2)));
57 /* Close out the archive. */
58 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
59 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
61 /* Now, read back a truncated version of the archive and
62 * verify that we get an appropriate error. */
63 for (i = 1; i < used + 100; i += 100) {
64 assert((a = archive_read_new()) != NULL);
65 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
66 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
68 assertEqualIntA(a, ARCHIVE_FATAL, archive_read_open_memory(a, buff, i));
71 assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, i));
73 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
75 if (i < 512 + sizeof(buff2)) {
76 assertEqualIntA(a, ARCHIVE_FATAL, archive_read_data(a, buff2, sizeof(buff2)));
79 assertEqualIntA(a, sizeof(buff2), archive_read_data(a, buff2, sizeof(buff2)));
82 /* Verify the end of the archive. */
83 /* Archive must be long enough to capture a 512-byte
84 * block of zeroes after the entry. (POSIX requires a
85 * second block of zeros to be written but libarchive
86 * does not return an error if it can't consume
88 if (i < 512 + 512*((sizeof(buff2) + 511)/512) + 512) {
89 assertEqualIntA(a, ARCHIVE_FATAL, archive_read_next_header(a, &ae));
91 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
94 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
95 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
100 /* Same as above, except skip the body instead of reading it. */
101 for (i = 1; i < used + 100; i += 100) {
102 assert((a = archive_read_new()) != NULL);
103 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
104 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
106 assertEqualIntA(a, ARCHIVE_FATAL, archive_read_open_memory(a, buff, i));
109 assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, i));
111 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
113 if (i < 512 + 512*((sizeof(buff2)+511)/512)) {
114 assertEqualIntA(a, ARCHIVE_FATAL, archive_read_data_skip(a));
117 assertEqualIntA(a, ARCHIVE_OK, archive_read_data_skip(a));
120 /* Verify the end of the archive. */
121 /* Archive must be long enough to capture a 512-byte
122 * block of zeroes after the entry. (POSIX requires a
123 * second block of zeros to be written but libarchive
124 * does not return an error if it can't consume
126 if (i < 512 + 512*((sizeof(buff2) + 511)/512) + 512) {
127 assertEqualIntA(a, ARCHIVE_FATAL, archive_read_next_header(a, &ae));
129 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
132 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
133 assertEqualInt(ARCHIVE_OK, archive_read_free(a));