2 * Copyright (c) 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$");
30 #define LARGE_SIZE (16*1024*1024)
32 test_large(const char *compression_type)
34 struct archive_entry *ae;
37 size_t buffsize = LARGE_SIZE + 1024 * 256;
38 size_t datasize = LARGE_SIZE;
39 char *buff, *filedata, *filedata2;
42 assert((buff = malloc(buffsize)) != NULL);
43 assert((filedata = malloc(datasize)) != NULL);
44 assert((filedata2 = malloc(datasize)) != NULL);
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);
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",
61 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
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));
72 * Write a large file to it.
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);
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);
90 for (i = 0; i < datasize; i++)
91 filedata[i] = (char)rand();
93 assertEqualInt(datasize, archive_write_data(a, filedata, datasize));
95 /* Close out the archive. */
96 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
97 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
99 /* Verify the initial header. */
100 assertEqualMem(buff, "\x37\x7a\xbc\xaf\x27\x1c\x00\x03", 8);
103 * Now, read the data back.
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));
112 * Read and verify a large file.
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);
125 /* Verify the end of the archive. */
126 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
128 /* Verify archive format. */
129 assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
130 assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
132 assertEqualInt(ARCHIVE_OK, archive_read_close(a));
133 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
140 DEFINE_TEST(test_write_format_7zip_large_bzip2)
142 /* Test that making a 7-Zip archive file with bzip2 compression. */
146 DEFINE_TEST(test_write_format_7zip_large_copy)
148 /* Test that making a 7-Zip archive file without compression. */
152 DEFINE_TEST(test_write_format_7zip_large_deflate)
154 /* Test that making a 7-Zip archive file with deflate compression. */
155 test_large("deflate");
158 DEFINE_TEST(test_write_format_7zip_large_lzma1)
160 /* Test that making a 7-Zip archive file with lzma1 compression. */
164 DEFINE_TEST(test_write_format_7zip_large_lzma2)
166 /* Test that making a 7-Zip archive file with lzma2 compression. */
170 DEFINE_TEST(test_write_format_7zip_large_ppmd)
172 /* Test that making a 7-Zip archive file with PPMd compression. */