xref: /src/contrib/libarchive/libarchive/test/test_write_filter_compress.c (revision bd66c1b43e33540205dbc1187c2f2a15c58b57ba)
1 /*-
2  * Copyright (c) 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  *    in this position and unchanged.
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.
14  *
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.
25  */
26 
27 #include "test.h"
28 
29 /*
30  * A basic exercise of compress reading and writing.
31  *
32  * TODO: Add a reference file and make sure we can decompress that.
33  */
34 
DEFINE_TEST(test_write_filter_compress)35 DEFINE_TEST(test_write_filter_compress)
36 {
37 	struct archive_entry *ae;
38 	struct archive* a;
39 	char *buff, *data;
40 	size_t buffsize, datasize;
41 	char path[16];
42 	size_t used;
43 	int i;
44 
45 	buffsize = 1000000;
46 	assert(NULL != (buff = malloc(buffsize)));
47 
48 	datasize = 10000;
49 	assert(NULL != (data = malloc(datasize)));
50 	memset(data, 0, datasize);
51 
52 	assert((a = archive_write_new()) != NULL);
53 	assertEqualIntA(a, ARCHIVE_OK,
54 	    archive_write_set_format_ustar(a));
55 	assertEqualIntA(a, ARCHIVE_OK,
56 	    archive_write_add_filter_compress(a));
57 	assertEqualIntA(a, ARCHIVE_OK,
58 	    archive_write_open_memory(a, buff, buffsize, &used));
59 
60 	for (i = 0; i < 100; i++) {
61 		snprintf(path, sizeof(path), "file%03d", i);
62 		assert((ae = archive_entry_new()) != NULL);
63 		archive_entry_copy_pathname(ae, path);
64 		archive_entry_set_size(ae, datasize);
65 		archive_entry_set_filetype(ae, AE_IFREG);
66 		assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
67 		assertEqualInt(datasize,
68 		    archive_write_data(a, data, datasize));
69 		archive_entry_free(ae);
70 	}
71 
72 	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
73 	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
74 
75 	/*
76 	 * Now, read the data back.
77 	 */
78 	assert((a = archive_read_new()) != NULL);
79 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
80 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
81 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, used));
82 
83 
84 	for (i = 0; i < 100; i++) {
85 		snprintf(path, sizeof(path), "file%03d", i);
86 		if (!assertEqualInt(0, archive_read_next_header(a, &ae)))
87 			break;
88 		assertEqualString(path, archive_entry_pathname(ae));
89 		assertEqualInt((int)datasize, archive_entry_size(ae));
90 	}
91 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
92 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
93 
94 	free(data);
95 	free(buff);
96 }
97