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: src/lib/libarchive/test/test_read_truncated.c,v 1.4 2008/09/01 05:38:33 kientzle Exp $");
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 	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)));
56 
57 	/* Close out the archive. */
58 	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
59 	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
60 
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));
67 		if (i < 512) {
68 			assertEqualIntA(a, ARCHIVE_FATAL, archive_read_open_memory(a, buff, i));
69 			goto wrap_up;
70 		} else {
71 			assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, i));
72 		}
73 		assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
74 
75 		if (i < 512 + sizeof(buff2)) {
76 			assertEqualIntA(a, ARCHIVE_FATAL, archive_read_data(a, buff2, sizeof(buff2)));
77 			goto wrap_up;
78 		} else {
79 			assertEqualIntA(a, sizeof(buff2), archive_read_data(a, buff2, sizeof(buff2)));
80 		}
81 
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
87 		 * it.) */
88 		if (i < 512 + 512*((sizeof(buff2) + 511)/512) + 512) {
89 			assertEqualIntA(a, ARCHIVE_FATAL, archive_read_next_header(a, &ae));
90 		} else {
91 			assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
92 		}
93 	wrap_up:
94 		assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
95 		assertEqualInt(ARCHIVE_OK, archive_read_free(a));
96 	}
97 
98 
99 
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));
105 		if (i < 512) {
106 			assertEqualIntA(a, ARCHIVE_FATAL, archive_read_open_memory(a, buff, i));
107 			goto wrap_up2;
108 		} else {
109 			assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, i));
110 		}
111 		assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
112 
113 		if (i < 512 + 512*((sizeof(buff2)+511)/512)) {
114 			assertEqualIntA(a, ARCHIVE_FATAL, archive_read_data_skip(a));
115 			goto wrap_up2;
116 		} else {
117 			assertEqualIntA(a, ARCHIVE_OK, archive_read_data_skip(a));
118 		}
119 
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
125 		 * it.) */
126 		if (i < 512 + 512*((sizeof(buff2) + 511)/512) + 512) {
127 			assertEqualIntA(a, ARCHIVE_FATAL, archive_read_next_header(a, &ae));
128 		} else {
129 			assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
130 		}
131 	wrap_up2:
132 		assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
133 		assertEqualInt(ARCHIVE_OK, archive_read_free(a));
134 	}
135 }
136