2 * Copyright (c) 2011 Tim Kientzle
3 * Copyright (c) 2011-2012 Andres Mejia
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 __FBSDID("$FreeBSD$");
30 #if defined(_WIN32) && !defined(__CYGWIN__)
34 #if !defined(__BORLANDC__)
38 #define lseek(f, o, w) _lseek(f, (long)(o), (int)(w))
43 test_splitted_file(void)
46 static const char *reffiles[] =
48 "test_read_splitted_rar_aa",
49 "test_read_splitted_rar_ab",
50 "test_read_splitted_rar_ac",
51 "test_read_splitted_rar_ad",
54 const char test_txt[] = "test text document\r\n";
55 int size = sizeof(test_txt)-1;
56 struct archive_entry *ae;
59 extract_reference_files(reffiles);
60 assert((a = archive_read_new()) != NULL);
61 assertA(0 == archive_read_support_filter_all(a));
62 assertA(0 == archive_read_support_format_all(a));
63 assertA(0 == archive_read_open_filenames(a, reffiles, 10240));
66 assertA(0 == archive_read_next_header(a, &ae));
67 assertEqualString("test.txt", archive_entry_pathname(ae));
68 assertA((int)archive_entry_mtime(ae));
69 assertA((int)archive_entry_ctime(ae));
70 assertA((int)archive_entry_atime(ae));
71 assertEqualInt(20, archive_entry_size(ae));
72 assertEqualInt(33188, archive_entry_mode(ae));
73 assertA(size == archive_read_data(a, buff, size));
74 assertEqualMem(buff, test_txt, size);
77 assertA(0 == archive_read_next_header(a, &ae));
78 assertEqualString("testlink", archive_entry_pathname(ae));
79 assertA((int)archive_entry_mtime(ae));
80 assertA((int)archive_entry_ctime(ae));
81 assertA((int)archive_entry_atime(ae));
82 assertEqualInt(0, archive_entry_size(ae));
83 assertEqualInt(41471, archive_entry_mode(ae));
84 assertEqualString("test.txt", archive_entry_symlink(ae));
85 assertEqualIntA(a, 0, archive_read_data(a, buff, sizeof(buff)));
88 assertA(0 == archive_read_next_header(a, &ae));
89 assertEqualString("testdir/test.txt", archive_entry_pathname(ae));
90 assertA((int)archive_entry_mtime(ae));
91 assertA((int)archive_entry_ctime(ae));
92 assertA((int)archive_entry_atime(ae));
93 assertEqualInt(20, archive_entry_size(ae));
94 assertEqualInt(33188, archive_entry_mode(ae));
95 assertA(size == archive_read_data(a, buff, size));
96 assertEqualMem(buff, test_txt, size);
99 assertA(0 == archive_read_next_header(a, &ae));
100 assertEqualString("testdir", archive_entry_pathname(ae));
101 assertA((int)archive_entry_mtime(ae));
102 assertA((int)archive_entry_ctime(ae));
103 assertA((int)archive_entry_atime(ae));
104 assertEqualInt(0, archive_entry_size(ae));
105 assertEqualInt(16877, archive_entry_mode(ae));
108 assertA(0 == archive_read_next_header(a, &ae));
109 assertEqualString("testemptydir", archive_entry_pathname(ae));
110 assertA((int)archive_entry_mtime(ae));
111 assertA((int)archive_entry_ctime(ae));
112 assertA((int)archive_entry_atime(ae));
113 assertEqualInt(0, archive_entry_size(ae));
114 assertEqualInt(16877, archive_entry_mode(ae));
117 assertA(1 == archive_read_next_header(a, &ae));
118 assertEqualInt(5, archive_file_count(a));
119 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
120 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
124 test_large_splitted_file(void)
126 static const char *reffiles[] =
128 "test_read_large_splitted_rar_aa",
129 "test_read_large_splitted_rar_ab",
130 "test_read_large_splitted_rar_ac",
131 "test_read_large_splitted_rar_ad",
132 "test_read_large_splitted_rar_ae",
135 const char test_txt[] = "gin-bottom: 0in\"><BR>\n</P>\n</BODY>\n</HTML>";
136 int size = 241647978, offset = 0;
138 struct archive_entry *ae;
141 extract_reference_files(reffiles);
142 assert((a = archive_read_new()) != NULL);
143 assertA(0 == archive_read_support_filter_all(a));
144 assertA(0 == archive_read_support_format_all(a));
145 assertA(0 == archive_read_open_filenames(a, reffiles, 10240));
148 assertA(0 == archive_read_next_header(a, &ae));
149 assertEqualString("ppmd_lzss_conversion_test.txt",
150 archive_entry_pathname(ae));
151 assertA((int)archive_entry_mtime(ae));
152 assertA((int)archive_entry_ctime(ae));
153 assertA((int)archive_entry_atime(ae));
154 assertEqualInt(size, archive_entry_size(ae));
155 assertEqualInt(33188, archive_entry_mode(ae));
156 while (offset + (int)sizeof(buff) < size)
158 assertA(sizeof(buff) == archive_read_data(a, buff, sizeof(buff)));
159 offset += sizeof(buff);
161 assertA(size - offset == archive_read_data(a, buff, size - offset));
162 assertEqualMem(buff, test_txt, size - offset);
165 assertA(1 == archive_read_next_header(a, &ae));
166 assertEqualInt(1, archive_file_count(a));
167 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
168 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
171 #define BLOCK_SIZE 10240
179 file_open(struct archive *a, void *data)
181 struct mydata *mydata = (struct mydata *)data;
185 mydata->fd = open(mydata->filename, O_RDONLY | O_BINARY);
188 if ((mydata->buffer = (void*)calloc(1, BLOCK_SIZE)) == NULL)
189 return (ARCHIVE_FAILED);
195 file_read(struct archive *a, void *data, const void **buff)
197 struct mydata *mydata = (struct mydata *)data;
199 *buff = mydata->buffer;
200 return read(mydata->fd, mydata->buffer, BLOCK_SIZE);
203 file_skip(struct archive *a, void *data, int64_t request)
205 struct mydata *mydata = (struct mydata *)data;
206 int64_t result = lseek(mydata->fd, SEEK_CUR, request);
209 archive_set_error(a, errno, "Error seeking in '%s'", mydata->filename);
213 file_switch(struct archive *a, void *data1, void *data2)
215 struct mydata *mydata1 = (struct mydata *)data1;
216 struct mydata *mydata2 = (struct mydata *)data2;
217 int r = (ARCHIVE_OK);
220 if (mydata1 && mydata1->fd >= 0)
223 free(mydata1->buffer);
224 mydata1->buffer = NULL;
229 r = file_open(a, mydata2);
234 file_close(struct archive *a, void *data)
236 struct mydata *mydata = (struct mydata *)data;
238 return (ARCHIVE_FATAL);
239 file_switch(a, mydata, NULL);
240 free(mydata->filename);
245 file_seek(struct archive *a, void *data, int64_t request, int whence)
247 struct mydata *mine = (struct mydata *)data;
251 r = lseek(mine->fd, request, whence);
254 return (ARCHIVE_FATAL);
258 test_customized_multiple_data_objects(void)
261 static const char *reffiles[] =
263 "test_read_splitted_rar_aa",
264 "test_read_splitted_rar_ab",
265 "test_read_splitted_rar_ac",
266 "test_read_splitted_rar_ad",
269 const char test_txt[] = "test text document\r\n";
270 int size = sizeof(test_txt)-1;
271 struct archive_entry *ae;
273 struct mydata *mydata;
274 const char *filename = *reffiles;
277 extract_reference_files(reffiles);
278 assert((a = archive_read_new()) != NULL);
279 assertA(0 == archive_read_support_filter_all(a));
280 assertA(0 == archive_read_support_format_all(a));
282 for (i = 0; filename != NULL;)
284 assert((mydata = (struct mydata *)calloc(1, sizeof(*mydata))) != NULL);
285 if (mydata == NULL) {
286 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
289 assert((mydata->filename =
290 (char *)calloc(1, strlen(filename) + 1)) != NULL);
291 if (mydata->filename == NULL) {
293 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
296 strcpy(mydata->filename, filename);
298 filename = reffiles[++i];
299 assertA(0 == archive_read_append_callback_data(a, mydata));
301 assertA(0 == archive_read_set_open_callback(a, file_open));
302 assertA(0 == archive_read_set_read_callback(a, file_read));
303 assertA(0 == archive_read_set_skip_callback(a, file_skip));
304 assertA(0 == archive_read_set_close_callback(a, file_close));
305 assertA(0 == archive_read_set_switch_callback(a, file_switch));
306 assertA(0 == archive_read_set_seek_callback(a, file_seek));
307 assertA(0 == archive_read_open1(a));
310 assertA(0 == archive_read_next_header(a, &ae));
311 assertEqualString("test.txt", archive_entry_pathname(ae));
312 assertA((int)archive_entry_mtime(ae));
313 assertA((int)archive_entry_ctime(ae));
314 assertA((int)archive_entry_atime(ae));
315 assertEqualInt(20, archive_entry_size(ae));
316 assertEqualInt(33188, archive_entry_mode(ae));
317 assertA(size == archive_read_data(a, buff, size));
318 assertEqualMem(buff, test_txt, size);
321 assertA(0 == archive_read_next_header(a, &ae));
322 assertEqualString("testlink", archive_entry_pathname(ae));
323 assertA((int)archive_entry_mtime(ae));
324 assertA((int)archive_entry_ctime(ae));
325 assertA((int)archive_entry_atime(ae));
326 assertEqualInt(0, archive_entry_size(ae));
327 assertEqualInt(41471, archive_entry_mode(ae));
328 assertEqualString("test.txt", archive_entry_symlink(ae));
329 assertEqualIntA(a, 0, archive_read_data(a, buff, sizeof(buff)));
332 assertA(0 == archive_read_next_header(a, &ae));
333 assertEqualString("testdir/test.txt", archive_entry_pathname(ae));
334 assertA((int)archive_entry_mtime(ae));
335 assertA((int)archive_entry_ctime(ae));
336 assertA((int)archive_entry_atime(ae));
337 assertEqualInt(20, archive_entry_size(ae));
338 assertEqualInt(33188, archive_entry_mode(ae));
339 assertA(size == archive_read_data(a, buff, size));
340 assertEqualMem(buff, test_txt, size);
343 assertA(0 == archive_read_next_header(a, &ae));
344 assertEqualString("testdir", archive_entry_pathname(ae));
345 assertA((int)archive_entry_mtime(ae));
346 assertA((int)archive_entry_ctime(ae));
347 assertA((int)archive_entry_atime(ae));
348 assertEqualInt(0, archive_entry_size(ae));
349 assertEqualInt(16877, archive_entry_mode(ae));
352 assertA(0 == archive_read_next_header(a, &ae));
353 assertEqualString("testemptydir", archive_entry_pathname(ae));
354 assertA((int)archive_entry_mtime(ae));
355 assertA((int)archive_entry_ctime(ae));
356 assertA((int)archive_entry_atime(ae));
357 assertEqualInt(0, archive_entry_size(ae));
358 assertEqualInt(16877, archive_entry_mode(ae));
361 assertA(1 == archive_read_next_header(a, &ae));
362 assertEqualInt(5, archive_file_count(a));
363 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
364 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
367 DEFINE_TEST(test_archive_read_multiple_data_objects)
369 test_splitted_file();
370 test_large_splitted_file();
371 test_customized_multiple_data_objects();