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