1 /*-
2  * Copyright (c) 2012 Michihiro NAKAJIMA
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 
26 
27 #include "test.h"
28 __FBSDID("$FreeBSD$");
29 
30 #define LARGE_SIZE	(1*1024*1024)
31 static void
32 test_large(const char *compression_type)
33 {
34 	struct archive_entry *ae;
35 	struct archive *a;
36 	size_t used;
37 	size_t buffsize = LARGE_SIZE + 1024 * 256;
38 	size_t datasize = LARGE_SIZE;
39 	char *buff, *filedata, *filedata2;
40 
41 	assert((buff = malloc(buffsize)) != NULL);
42 	assert((filedata = malloc(datasize)) != NULL);
43 	assert((filedata2 = malloc(datasize)) != NULL);
44 
45 	/* Create a new archive in memory. */
46 	assert((a = archive_write_new()) != NULL);
47 	if (a == NULL || buff == NULL || filedata == NULL || filedata2 == NULL) {
48 		archive_write_free(a);
49 		free(buff);
50 		free(filedata);
51 		free(filedata2);
52 		return;
53 	}
54 	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_7zip(a));
55 	if (compression_type != NULL &&
56 	    ARCHIVE_OK != archive_write_set_format_option(a, "7zip",
57 	    "compression", compression_type)) {
58 		skipping("%s writing not fully supported on this platform",
59 		   compression_type);
60 		assertEqualInt(ARCHIVE_OK, archive_write_free(a));
61 		free(buff);
62 		free(filedata);
63 		free(filedata2);
64 		return;
65 	}
66 	assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a));
67 	assertEqualIntA(a, ARCHIVE_OK,
68 	    archive_write_open_memory(a, buff, buffsize, &used));
69 
70 	/*
71 	 * Write a large file to it.
72 	 */
73 	assert((ae = archive_entry_new()) != NULL);
74 	archive_entry_set_mtime(ae, 1, 100);
75 	assertEqualInt(1, archive_entry_mtime(ae));
76 	assertEqualInt(100, archive_entry_mtime_nsec(ae));
77 	archive_entry_copy_pathname(ae, "file");
78 	assertEqualString("file", archive_entry_pathname(ae));
79 	archive_entry_set_mode(ae, AE_IFREG | 0755);
80 	assertEqualInt((AE_IFREG | 0755), archive_entry_mode(ae));
81 	archive_entry_set_size(ae, datasize);
82 
83 	assertEqualInt(0, archive_write_header(a, ae));
84 	archive_entry_free(ae);
85 	if (strcmp(compression_type, "ppmd") == 0) {
86 		/* NOTE: PPMd cannot handle random data correctly.*/
87 		memset(filedata, 'a', datasize);
88 	} else {
89 		fill_with_pseudorandom_data(filedata, datasize);
90 	}
91 	assertEqualInt(datasize, archive_write_data(a, filedata, datasize));
92 
93 	/* Close out the archive. */
94 	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
95 	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
96 
97 	/* Verify the initial header. */
98 	assertEqualMem(buff, "\x37\x7a\xbc\xaf\x27\x1c\x00\x03", 8);
99 
100 	/*
101 	 * Now, read the data back.
102 	 */
103 	/* With the test memory reader -- seeking mode. */
104 	assert((a = archive_read_new()) != NULL);
105 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
106 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
107 	assertEqualIntA(a, ARCHIVE_OK, read_open_memory_seek(a, buff, used, 7));
108 
109 	/*
110 	 * Read and verify a large file.
111 	 */
112 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
113 	assertEqualInt(1, archive_entry_mtime(ae));
114 	assertEqualInt(100, archive_entry_mtime_nsec(ae));
115 	assertEqualInt(0, archive_entry_atime(ae));
116 	assertEqualInt(0, archive_entry_ctime(ae));
117 	assertEqualString("file", archive_entry_pathname(ae));
118 	assertEqualInt(AE_IFREG | 0755, archive_entry_mode(ae));
119 	assertEqualInt(datasize, archive_entry_size(ae));
120 	assertEqualIntA(a, datasize, archive_read_data(a, filedata2, datasize));
121 	assertEqualMem(filedata, filedata2, datasize);
122 
123 	/* Verify the end of the archive. */
124 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
125 
126 	/* Verify archive format. */
127 	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
128 	assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
129 
130 	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
131 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
132 
133 	free(buff);
134 	free(filedata);
135 	free(filedata2);
136 }
137 
138 DEFINE_TEST(test_write_format_7zip_large_bzip2)
139 {
140 	/* Test that making a 7-Zip archive file with bzip2 compression. */
141 	test_large("bzip2");
142 }
143 
144 DEFINE_TEST(test_write_format_7zip_large_copy)
145 {
146 	/* Test that making a 7-Zip archive file without compression. */
147 	test_large("copy");
148 }
149 
150 DEFINE_TEST(test_write_format_7zip_large_deflate)
151 {
152 	/* Test that making a 7-Zip archive file with deflate compression. */
153 	test_large("deflate");
154 }
155 
156 DEFINE_TEST(test_write_format_7zip_large_lzma1)
157 {
158 	/* Test that making a 7-Zip archive file with lzma1 compression. */
159 	test_large("lzma1");
160 }
161 
162 DEFINE_TEST(test_write_format_7zip_large_lzma2)
163 {
164 	/* Test that making a 7-Zip archive file with lzma2 compression. */
165 	test_large("lzma2");
166 }
167 
168 DEFINE_TEST(test_write_format_7zip_large_ppmd)
169 {
170 	/* Test that making a 7-Zip archive file with PPMd compression. */
171 	test_large("ppmd");
172 }
173