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 char buff[1000000];
29 char buff2[100000];
30 
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 	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));
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 	assertA(0 == archive_write_header(a, ae));
54 	archive_entry_free(ae);
55 	assertA((int)sizeof(buff2) == archive_write_data(a, buff2, sizeof(buff2)));
56 
57 	/* Close out the archive. */
58 	assertA(0 == archive_write_close(a));
59 #if ARCHIVE_VERSION_NUMBER < 2000000
60 	archive_write_finish(a);
61 #else
62 	assertA(0 == archive_write_finish(a));
63 #endif
64 
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));
72 
73 		if (i < 512) {
74 			assertA(ARCHIVE_FATAL == archive_read_next_header(a, &ae));
75 			goto wrap_up;
76 		} else {
77 			assertA(0 == archive_read_next_header(a, &ae));
78 		}
79 
80 		if (i < 512 + sizeof(buff2)) {
81 			assertA(ARCHIVE_FATAL == archive_read_data(a, buff2, sizeof(buff2)));
82 			goto wrap_up;
83 		} else {
84 			assertA((int)sizeof(buff2) == archive_read_data(a, buff2, sizeof(buff2)));
85 		}
86 
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
92 		 * it.) */
93 		if (i < 512 + 512*((sizeof(buff2) + 511)/512) + 512) {
94 			assertA(ARCHIVE_FATAL == archive_read_next_header(a, &ae));
95 		} else {
96 			assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
97 		}
98 	wrap_up:
99 		assert(0 == archive_read_close(a));
100 #if ARCHIVE_VERSION_NUMBER < 2000000
101 		archive_read_finish(a);
102 #else
103 		assert(0 == archive_read_finish(a));
104 #endif
105 	}
106 
107 
108 
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));
115 
116 		if (i < 512) {
117 			assertA(ARCHIVE_FATAL == archive_read_next_header(a, &ae));
118 			goto wrap_up2;
119 		} else {
120 			assertA(0 == archive_read_next_header(a, &ae));
121 		}
122 
123 		if (i < 512 + 512*((sizeof(buff2)+511)/512)) {
124 			assertA(ARCHIVE_FATAL == archive_read_data_skip(a));
125 			goto wrap_up2;
126 		} else {
127 			assertA(ARCHIVE_OK == archive_read_data_skip(a));
128 		}
129 
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
135 		 * it.) */
136 		if (i < 512 + 512*((sizeof(buff2) + 511)/512) + 512) {
137 			assertA(ARCHIVE_FATAL == archive_read_next_header(a, &ae));
138 		} else {
139 			assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
140 		}
141 	wrap_up2:
142 		assert(0 == archive_read_close(a));
143 #if ARCHIVE_VERSION_NUMBER < 2000000
144 		archive_read_finish(a);
145 #else
146 		assert(0 == archive_read_finish(a));
147 #endif
148 	}
149 }
150