]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/libarchive/libarchive/test/test_read_format_zip_high_compression.c
MFC r368207,368607:
[FreeBSD/stable/10.git] / contrib / libarchive / libarchive / test / test_read_format_zip_high_compression.c
1 /*-
2  * Copyright (c) 2016 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  * 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 #include "test.h"
26 __FBSDID("$FreeBSD");
27
28 #include <locale.h>
29
30
31 /*
32  * Github Issue 748 reported problems with end-of-entry handling
33  * with highly-compressible data.  This resulted in the end of the
34  * data being truncated (extracted as zero bytes).
35  */
36
37 /*
38  * Extract the specific test archive that was used to diagnose
39  * Issue 748:
40  */
41 DEFINE_TEST(test_read_format_zip_high_compression)
42 {
43         const char *refname = "test_read_format_zip_high_compression.zip";
44         char *p;
45         size_t archive_size;
46         struct archive *a;
47         struct archive_entry *entry;
48
49         const void *pv;
50         size_t s;
51         int64_t o;
52
53         if (archive_zlib_version() == NULL) {
54                 skipping("Zip compression test requires zlib");
55                 return;
56         }
57
58         extract_reference_file(refname);
59         p = slurpfile(&archive_size, "%s", refname);
60
61         assert((a = archive_read_new()) != NULL);
62         assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_zip(a));
63         assertEqualIntA(a, ARCHIVE_OK, read_open_memory_seek(a, p, archive_size, 16 * 1024));
64         assertEqualInt(ARCHIVE_OK, archive_read_next_header(a, &entry));
65
66         assertEqualInt(ARCHIVE_OK, archive_read_data_block(a, &pv, &s, &o));
67         assertEqualInt(262144, s);
68         assertEqualInt(0, o);
69
70         assertEqualInt(ARCHIVE_OK, archive_read_data_block(a, &pv, &s, &o));
71         assertEqualInt(160, s);
72         assertEqualInt(262144, o);
73
74         assertEqualInt(ARCHIVE_EOF, archive_read_data_block(a, &pv, &s, &o));
75
76         assertEqualInt(ARCHIVE_OK, archive_free(a));
77         free(p);
78 }
79
80 /*
81  * Synthesize a lot of varying inputs that are highly compressible.
82  */
83 DEFINE_TEST(test_read_format_zip_high_compression2)
84 {
85         const size_t body_size = 1024 * 1024;
86         const size_t buff_size = 2 * 1024 * 1024;
87         char *body, *body_read, *buff;
88         int n;
89
90         if (archive_zlib_version() == NULL) {
91                 skipping("Zip compression test requires zlib");
92                 return;
93         }
94
95         assert((body = malloc(body_size)) != NULL);
96         assert((body_read = malloc(body_size)) != NULL);
97         assert((buff = malloc(buff_size)) != NULL);
98
99         /* Highly-compressible data: all bytes 255, except for a
100          * single 1 byte.
101          * The body is always 256k + 6 bytes long (the internal deflation
102          * buffer is exactly 256k).
103          */
104
105         for(n = 1024; n < (int)body_size; n += 1024) {
106                 struct archive *a;
107                 struct archive_entry *entry;
108                 size_t used = 0;
109                 const void *pv;
110                 size_t s;
111                 int64_t o;
112
113                 memset(body, 255, body_size);
114                 body[n] = 1;
115
116                 /* Write an archive with a single entry of n bytes. */
117                 assert((a = archive_write_new()) != NULL);
118                 assertEqualInt(ARCHIVE_OK, archive_write_set_format_zip(a));
119                 assertEqualInt(ARCHIVE_OK, archive_write_open_memory(a, buff, buff_size, &used));
120
121                 entry = archive_entry_new2(a);
122                 archive_entry_set_pathname(entry, "test");
123                 archive_entry_set_filetype(entry, AE_IFREG);
124                 archive_entry_set_size(entry, 262150);
125                 assertEqualInt(ARCHIVE_OK, archive_write_header(a, entry));
126                 archive_entry_free(entry);
127                 assertEqualInt(262150, archive_write_data(a, body, 262150));
128                 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
129
130                 /* Read back the entry and verify the contents. */
131                 assert((a = archive_read_new()) != NULL);
132                 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
133                 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
134                 assertEqualIntA(a, ARCHIVE_OK, read_open_memory(a, buff, used, 17));
135                 assertEqualInt(ARCHIVE_OK, archive_read_next_header(a, &entry));
136
137                 assertEqualInt(ARCHIVE_OK, archive_read_data_block(a, &pv, &s, &o));
138                 assertEqualInt(262144, s);
139                 assertEqualInt(0, o);
140
141                 assertEqualInt(ARCHIVE_OK, archive_read_data_block(a, &pv, &s, &o));
142                 assertEqualInt(6, s);
143                 assertEqualInt(262144, o);
144
145                 assertEqualInt(ARCHIVE_EOF, archive_read_data_block(a, &pv, &s, &o));
146
147                 assertEqualInt(ARCHIVE_OK, archive_free(a));
148         }
149
150         free(body);
151         free(body_read);
152         free(buff);
153 }