xref: /freebsd/contrib/libarchive/libarchive/test/test_open_failure.c (revision 0b3105a37d7adcadcb720112fed4dc4e8040be99)
1 /*-
2  * Copyright (c) 2003-2010 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 #define MAGIC 123456789
29 struct my_data {
30 	int magic;
31 	int read_return;
32 	int read_called;
33 	int write_return;
34 	int write_called;
35 	int open_return;
36 	int open_called;
37 	int close_return;
38 	int close_called;
39 };
40 
41 static ssize_t
42 my_read(struct archive *a, void *_private, const void **buff)
43 {
44 	struct my_data *private = (struct my_data *)_private;
45 	(void)a; /* UNUSED */
46 	(void)buff; /* UNUSED */
47 	assertEqualInt(MAGIC, private->magic);
48 	++private->read_called;
49 	return (private->read_return);
50 }
51 
52 static ssize_t
53 my_write(struct archive *a, void *_private, const void *buff, size_t s)
54 {
55 	struct my_data *private = (struct my_data *)_private;
56 	(void)a; /* UNUSED */
57 	(void)buff; /* UNUSED */
58 	(void)s; /* UNUSED */
59 	assertEqualInt(MAGIC, private->magic);
60 	++private->write_called;
61 	return (private->write_return);
62 }
63 
64 static int
65 my_open(struct archive *a, void *_private)
66 {
67 	struct my_data *private = (struct my_data *)_private;
68 	(void)a; /* UNUSED */
69 	assertEqualInt(MAGIC, private->magic);
70 	++private->open_called;
71 	return (private->open_return);
72 }
73 
74 static int
75 my_close(struct archive *a, void *_private)
76 {
77 	struct my_data *private = (struct my_data *)_private;
78 	(void)a; /* UNUSED */
79 	assertEqualInt(MAGIC, private->magic);
80 	++private->close_called;
81 	return (private->close_return);
82 }
83 
84 
85 DEFINE_TEST(test_open_failure)
86 {
87 	struct archive *a;
88 	struct my_data private;
89 
90 	memset(&private, 0, sizeof(private));
91 	private.magic = MAGIC;
92 	private.open_return = ARCHIVE_FATAL;
93 	a = archive_read_new();
94 	assert(a != NULL);
95 	assertEqualInt(ARCHIVE_FATAL,
96 	    archive_read_open(a, &private, my_open, my_read, my_close));
97 	assertEqualInt(1, private.open_called);
98 	assertEqualInt(0, private.read_called);
99 	assertEqualInt(1, private.close_called);
100 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
101 	assertEqualInt(1, private.open_called);
102 	assertEqualInt(0, private.read_called);
103 	assertEqualInt(1, private.close_called);
104 
105 	memset(&private, 0, sizeof(private));
106 	private.magic = MAGIC;
107 	private.open_return = ARCHIVE_FAILED;
108 	a = archive_read_new();
109 	assert(a != NULL);
110 	assertEqualInt(ARCHIVE_FAILED,
111 	    archive_read_open(a, &private, my_open, my_read, my_close));
112 	assertEqualInt(1, private.open_called);
113 	assertEqualInt(0, private.read_called);
114 	assertEqualInt(1, private.close_called);
115 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
116 	assertEqualInt(1, private.open_called);
117 	assertEqualInt(0, private.read_called);
118 	assertEqualInt(1, private.close_called);
119 
120 	memset(&private, 0, sizeof(private));
121 	private.magic = MAGIC;
122 	private.open_return = ARCHIVE_WARN;
123 	a = archive_read_new();
124 	assert(a != NULL);
125 	assertEqualInt(ARCHIVE_WARN,
126 	    archive_read_open(a, &private, my_open, my_read, my_close));
127 	assertEqualInt(1, private.open_called);
128 	assertEqualInt(0, private.read_called);
129 	assertEqualInt(1, private.close_called);
130 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
131 	assertEqualInt(1, private.open_called);
132 	assertEqualInt(0, private.read_called);
133 	assertEqualInt(1, private.close_called);
134 
135 	memset(&private, 0, sizeof(private));
136 	private.magic = MAGIC;
137 	private.open_return = ARCHIVE_OK;
138 	private.read_return = ARCHIVE_FATAL;
139 	a = archive_read_new();
140 	assert(a != NULL);
141 	assertEqualInt(ARCHIVE_OK,
142 	    archive_read_support_filter_compress(a));
143 	assertEqualInt(ARCHIVE_OK, archive_read_support_format_tar(a));
144 	assertEqualInt(ARCHIVE_FATAL,
145 	    archive_read_open(a, &private, my_open, my_read, my_close));
146 	assertEqualInt(1, private.open_called);
147 	assertEqualInt(1, private.read_called);
148 	assertEqualInt(1, private.close_called);
149 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
150 	assertEqualInt(1, private.open_called);
151 	assertEqualInt(1, private.read_called);
152 	assertEqualInt(1, private.close_called);
153 
154 	memset(&private, 0, sizeof(private));
155 	private.magic = MAGIC;
156 	private.open_return = ARCHIVE_FATAL;
157 	a = archive_write_new();
158 	assert(a != NULL);
159 	assertEqualInt(ARCHIVE_FATAL,
160 	    archive_write_open(a, &private, my_open, my_write, my_close));
161 	assertEqualInt(1, private.open_called);
162 	assertEqualInt(0, private.write_called);
163 	assertEqualInt(1, private.close_called);
164 	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
165 	assertEqualInt(1, private.open_called);
166 	assertEqualInt(0, private.write_called);
167 	assertEqualInt(1, private.close_called);
168 
169 	memset(&private, 0, sizeof(private));
170 	private.magic = MAGIC;
171 	private.open_return = ARCHIVE_FATAL;
172 	a = archive_write_new();
173 	assert(a != NULL);
174 	archive_write_add_filter_compress(a);
175 	archive_write_set_format_ustar(a);
176 	assertEqualInt(ARCHIVE_FATAL,
177 	    archive_write_open(a, &private, my_open, my_write, my_close));
178 	assertEqualInt(1, private.open_called);
179 	assertEqualInt(0, private.write_called);
180 	assertEqualInt(1, private.close_called);
181 	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
182 	assertEqualInt(1, private.open_called);
183 	assertEqualInt(0, private.write_called);
184 	assertEqualInt(1, private.close_called);
185 
186 	memset(&private, 0, sizeof(private));
187 	private.magic = MAGIC;
188 	private.open_return = ARCHIVE_FATAL;
189 	a = archive_write_new();
190 	assert(a != NULL);
191 	archive_write_set_format_zip(a);
192 	assertEqualInt(ARCHIVE_FATAL,
193 	    archive_write_open(a, &private, my_open, my_write, my_close));
194 	assertEqualInt(1, private.open_called);
195 	assertEqualInt(0, private.write_called);
196 	assertEqualInt(1, private.close_called);
197 	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
198 	assertEqualInt(1, private.open_called);
199 	assertEqualInt(0, private.write_called);
200 	assertEqualInt(1, private.close_called);
201 
202 	memset(&private, 0, sizeof(private));
203 	private.magic = MAGIC;
204 	private.open_return = ARCHIVE_FATAL;
205 	a = archive_write_new();
206 	assert(a != NULL);
207 	archive_write_add_filter_gzip(a);
208 	assertEqualInt(ARCHIVE_FATAL,
209 	    archive_write_open(a, &private, my_open, my_write, my_close));
210 	assertEqualInt(1, private.open_called);
211 	assertEqualInt(0, private.write_called);
212 	assertEqualInt(1, private.close_called);
213 	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
214 	assertEqualInt(1, private.open_called);
215 	assertEqualInt(0, private.write_called);
216 	assertEqualInt(1, private.close_called);
217 
218 }
219