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