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 char buff[1000000];
29 static char buff2[100000];
30
DEFINE_TEST(test_read_truncated)31 DEFINE_TEST(test_read_truncated)
32 {
33 struct archive_entry *ae;
34 struct archive *a;
35 unsigned int i;
36 size_t used;
37
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));
43
44 /*
45 * Write a file to it.
46 */
47 assert((ae = archive_entry_new()) != NULL);
48 archive_entry_copy_pathname(ae, "file");
49 archive_entry_set_mode(ae, S_IFREG | 0755);
50 fill_with_pseudorandom_data(buff2, sizeof(buff2));
51 archive_entry_set_size(ae, sizeof(buff2));
52 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
53 archive_entry_free(ae);
54 assertEqualIntA(a, sizeof(buff2), archive_write_data(a, buff2, sizeof(buff2)));
55
56 /* Close out the archive. */
57 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
58 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
59
60 /* Now, read back a truncated version of the archive and
61 * verify that we get an appropriate error. */
62 for (i = 1; i < used + 100; i += 100) {
63 assert((a = archive_read_new()) != NULL);
64 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
65 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
66 if (i < 512) {
67 assertEqualIntA(a, ARCHIVE_FATAL, archive_read_open_memory(a, buff, i));
68 goto wrap_up;
69 } else {
70 assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, i));
71 }
72 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
73
74 if (i < 512 + sizeof(buff2)) {
75 assertEqualIntA(a, ARCHIVE_FATAL, archive_read_data(a, buff2, sizeof(buff2)));
76 goto wrap_up;
77 } else {
78 assertEqualIntA(a, sizeof(buff2), archive_read_data(a, buff2, sizeof(buff2)));
79 }
80
81 /* Verify the end of the archive. */
82 /* Archive must be long enough to capture a 512-byte
83 * block of zeroes after the entry. (POSIX requires a
84 * second block of zeros to be written but libarchive
85 * does not return an error if it can't consume
86 * it.) */
87 if (i < 512 + 512*((sizeof(buff2) + 511)/512) + 512) {
88 assertEqualIntA(a, ARCHIVE_FATAL, archive_read_next_header(a, &ae));
89 } else {
90 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
91 }
92 wrap_up:
93 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
94 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
95 }
96
97
98
99 /* Same as above, except skip the body instead of reading it. */
100 for (i = 1; i < used + 100; i += 100) {
101 assert((a = archive_read_new()) != NULL);
102 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
103 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
104 if (i < 512) {
105 assertEqualIntA(a, ARCHIVE_FATAL, archive_read_open_memory(a, buff, i));
106 goto wrap_up2;
107 } else {
108 assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, i));
109 }
110 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
111
112 if (i < 512 + 512*((sizeof(buff2)+511)/512)) {
113 assertEqualIntA(a, ARCHIVE_FATAL, archive_read_data_skip(a));
114 goto wrap_up2;
115 } else {
116 assertEqualIntA(a, ARCHIVE_OK, archive_read_data_skip(a));
117 }
118
119 /* Verify the end of the archive. */
120 /* Archive must be long enough to capture a 512-byte
121 * block of zeroes after the entry. (POSIX requires a
122 * second block of zeros to be written but libarchive
123 * does not return an error if it can't consume
124 * it.) */
125 if (i < 512 + 512*((sizeof(buff2) + 511)/512) + 512) {
126 assertEqualIntA(a, ARCHIVE_FATAL, archive_read_next_header(a, &ae));
127 } else {
128 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
129 }
130 wrap_up2:
131 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
132 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
133 }
134 }
135