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