2 * Copyright (c) 2011-2012 Michihiro NAKAJIMA
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
28 __FBSDID("$FreeBSD$");
31 * Test writing an empty archive.
33 DEFINE_TEST(test_write_format_7zip_empty_archive)
36 size_t buffsize = 1000;
40 buff = malloc(buffsize);
42 /* Create a new archive in memory. */
43 assert((a = archive_write_new()) != NULL);
44 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_7zip(a));
45 assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a));
46 assertEqualIntA(a, ARCHIVE_OK,
47 archive_write_open_memory(a, buff, buffsize, &used));
49 /* Close out the archive. */
50 assertEqualInt(ARCHIVE_OK, archive_write_close(a));
51 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
53 /* Verify the archive file size. */
54 assertEqualInt(32, used);
56 /* Verify the initial header. */
58 "\x37\x7a\xbc\xaf\x27\x1c\x00\x03"
59 "\x8d\x9b\xd5\x0f\x00\x00\x00\x00"
60 "\x00\x00\x00\x00\x00\x00\x00\x00"
61 "\x00\x00\x00\x00\x00\x00\x00\x00", 32);
67 * Test writing an empty file.
70 test_only_empty_file(void)
73 struct archive_entry *ae;
74 size_t buffsize = 1000;
78 buff = malloc(buffsize);
80 /* Create a new archive in memory. */
81 assert((a = archive_write_new()) != NULL);
82 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_7zip(a));
83 assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a));
84 assertEqualIntA(a, ARCHIVE_OK,
85 archive_write_open_memory(a, buff, buffsize, &used));
88 * Write an empty file to it.
90 assert((ae = archive_entry_new()) != NULL);
91 archive_entry_set_mtime(ae, 1, 10);
92 assertEqualInt(1, archive_entry_mtime(ae));
93 assertEqualInt(10, archive_entry_mtime_nsec(ae));
94 archive_entry_set_atime(ae, 2, 20);
95 assertEqualInt(2, archive_entry_atime(ae));
96 assertEqualInt(20, archive_entry_atime_nsec(ae));
97 archive_entry_set_ctime(ae, 0, 100);
98 assertEqualInt(0, archive_entry_ctime(ae));
99 assertEqualInt(100, archive_entry_ctime_nsec(ae));
100 archive_entry_copy_pathname(ae, "empty");
101 assertEqualString("empty", archive_entry_pathname(ae));
102 archive_entry_set_mode(ae, AE_IFREG | 0755);
103 assertEqualInt((S_IFREG | 0755), archive_entry_mode(ae));
105 assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae));
106 archive_entry_free(ae);
108 /* Close out the archive. */
109 assertEqualInt(ARCHIVE_OK, archive_write_close(a));
110 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
112 /* Verify the archive file size. */
113 assertEqualInt(102, used);
115 /* Verify the initial header. */
117 "\x37\x7a\xbc\xaf\x27\x1c\x00\x03"
118 "\x00\x5b\x58\x25\x00\x00\x00\x00"
119 "\x00\x00\x00\x00\x46\x00\x00\x00"
120 "\x00\x00\x00\x00\x8f\xce\x1d\xf3", 32);
123 * Now, read the data back.
125 /* With the test memory reader -- seeking mode. */
126 assert((a = archive_read_new()) != NULL);
127 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
128 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
129 assertEqualIntA(a, ARCHIVE_OK, read_open_memory_seek(a, buff, used, 7));
132 * Read and verify an empty file.
134 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
135 assertEqualInt(1, archive_entry_mtime(ae));
136 assertEqualInt(0, archive_entry_mtime_nsec(ae));
137 assertEqualInt(2, archive_entry_atime(ae));
138 assertEqualInt(0, archive_entry_atime_nsec(ae));
139 assertEqualInt(0, archive_entry_ctime(ae));
140 assertEqualInt(100, archive_entry_ctime_nsec(ae));
141 assertEqualString("empty", archive_entry_pathname(ae));
142 assertEqualInt(AE_IFREG | 0755, archive_entry_mode(ae));
143 assertEqualInt(0, archive_entry_size(ae));
145 /* Verify the end of the archive. */
146 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
148 /* Verify archive format. */
149 assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
150 assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
152 assertEqualInt(ARCHIVE_OK, archive_read_close(a));
153 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
159 test_only_empty_files(void)
162 struct archive_entry *ae;
163 size_t buffsize = 1000;
167 buff = malloc(buffsize);
169 /* Create a new archive in memory. */
170 assert((a = archive_write_new()) != NULL);
171 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_7zip(a));
172 assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a));
173 assertEqualIntA(a, ARCHIVE_OK,
174 archive_write_open_memory(a, buff, buffsize, &used));
177 * Write an empty file to it.
179 assert((ae = archive_entry_new()) != NULL);
180 archive_entry_set_mtime(ae, 1, 10);
181 assertEqualInt(1, archive_entry_mtime(ae));
182 assertEqualInt(10, archive_entry_mtime_nsec(ae));
183 archive_entry_set_atime(ae, 2, 20);
184 assertEqualInt(2, archive_entry_atime(ae));
185 assertEqualInt(20, archive_entry_atime_nsec(ae));
186 archive_entry_copy_pathname(ae, "empty");
187 assertEqualString("empty", archive_entry_pathname(ae));
188 archive_entry_set_mode(ae, AE_IFREG | 0755);
189 assertEqualInt((AE_IFREG | 0755), archive_entry_mode(ae));
191 assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae));
192 archive_entry_free(ae);
195 * Write second empty file to it.
197 assert((ae = archive_entry_new()) != NULL);
198 archive_entry_set_mtime(ae, 2, 10);
199 assertEqualInt(2, archive_entry_mtime(ae));
200 assertEqualInt(10, archive_entry_mtime_nsec(ae));
201 archive_entry_set_ctime(ae, 2, 10);
202 assertEqualInt(2, archive_entry_ctime(ae));
203 assertEqualInt(10, archive_entry_ctime_nsec(ae));
204 archive_entry_copy_pathname(ae, "empty2");
205 assertEqualString("empty2", archive_entry_pathname(ae));
206 archive_entry_set_mode(ae, AE_IFREG | 0644);
207 assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
209 assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae));
210 archive_entry_free(ae);
213 * Write third empty file to it.
215 assert((ae = archive_entry_new()) != NULL);
216 archive_entry_set_mtime(ae, 3, 10);
217 assertEqualInt(3, archive_entry_mtime(ae));
218 assertEqualInt(10, archive_entry_mtime_nsec(ae));
219 archive_entry_copy_pathname(ae, "empty3");
220 assertEqualString("empty3", archive_entry_pathname(ae));
221 archive_entry_set_mode(ae, AE_IFREG | 0644);
222 assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
224 assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae));
225 archive_entry_free(ae);
227 /* Close out the archive. */
228 assertEqualInt(ARCHIVE_OK, archive_write_close(a));
229 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
231 /* Verify the initial header. */
232 assertEqualMem(buff, "\x37\x7a\xbc\xaf\x27\x1c\x00\x03", 8);
235 * Now, read the data back.
237 /* With the test memory reader -- seeking mode. */
238 assert((a = archive_read_new()) != NULL);
239 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
240 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
241 assertEqualIntA(a, ARCHIVE_OK, read_open_memory_seek(a, buff, used, 7));
244 * Read and verify an empty file.
246 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
247 assertEqualInt(1, archive_entry_mtime(ae));
248 assertEqualInt(0, archive_entry_mtime_nsec(ae));
249 assertEqualInt(2, archive_entry_atime(ae));
250 assertEqualInt(0, archive_entry_atime_nsec(ae));
251 assertEqualInt(0, archive_entry_ctime(ae));
252 assertEqualString("empty", archive_entry_pathname(ae));
253 assertEqualInt(AE_IFREG | 0755, archive_entry_mode(ae));
254 assertEqualInt(0, archive_entry_size(ae));
257 * Read and verify second empty file.
259 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
260 assertEqualInt(2, archive_entry_mtime(ae));
261 assertEqualInt(0, archive_entry_mtime_nsec(ae));
262 assertEqualInt(0, archive_entry_atime(ae));
263 assertEqualInt(2, archive_entry_ctime(ae));
264 assertEqualInt(0, archive_entry_ctime_nsec(ae));
265 assertEqualString("empty2", archive_entry_pathname(ae));
266 assertEqualInt(AE_IFREG | 0644, archive_entry_mode(ae));
267 assertEqualInt(0, archive_entry_size(ae));
270 * Read and verify third empty file.
272 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
273 assertEqualInt(3, archive_entry_mtime(ae));
274 assertEqualInt(0, archive_entry_mtime_nsec(ae));
275 assertEqualInt(0, archive_entry_atime(ae));
276 assertEqualInt(0, archive_entry_ctime(ae));
277 assertEqualString("empty3", archive_entry_pathname(ae));
278 assertEqualInt(AE_IFREG | 0644, archive_entry_mode(ae));
279 assertEqualInt(0, archive_entry_size(ae));
281 /* Verify the end of the archive. */
282 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
284 /* Verify archive format. */
285 assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
286 assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
288 assertEqualInt(ARCHIVE_OK, archive_read_close(a));
289 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
294 DEFINE_TEST(test_write_format_7zip_empty_files)
296 /* Test that write an empty file. */
297 test_only_empty_file();
298 test_only_empty_files();