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