2 This file is part of systemd
4 Copyright 2014 Ronny Chevalier
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 #include "random-util.h"
30 # define XZ_OK -EPROTONOSUPPORT
36 # define LZ4_OK -EPROTONOSUPPORT
39 typedef int (compress_blob_t
)(const void *src
, uint64_t src_size
,
40 void *dst
, size_t *dst_size
);
41 typedef int (decompress_blob_t
)(const void *src
, uint64_t src_size
,
42 void **dst
, size_t *dst_alloc_size
,
43 size_t* dst_size
, size_t dst_max
);
44 typedef int (decompress_sw_t
)(const void *src
, uint64_t src_size
,
45 void **buffer
, size_t *buffer_size
,
46 const void *prefix
, size_t prefix_len
,
49 typedef int (compress_stream_t
)(int fdf
, int fdt
, uint64_t max_bytes
);
50 typedef int (decompress_stream_t
)(int fdf
, int fdt
, uint64_t max_size
);
52 static void test_compress_decompress(int compression
,
53 compress_blob_t compress
,
54 decompress_blob_t decompress
,
61 _cleanup_free_
char *decompressed
= NULL
;
64 log_info("/* testing %s %s blob compression/decompression */",
65 object_compressed_to_string(compression
), data
);
67 r
= compress(data
, data_len
, compressed
, &csize
);
69 log_info_errno(r
, "compression failed: %m");
73 r
= decompress(compressed
, csize
,
74 (void **) &decompressed
, &usize
, &csize
, 0);
76 assert_se(decompressed
);
77 assert_se(memcmp(decompressed
, data
, data_len
) == 0);
80 r
= decompress("garbage", 7,
81 (void **) &decompressed
, &usize
, &csize
, 0);
84 /* make sure to have the minimal lz4 compressed size */
85 r
= decompress("00000000\1g", 9,
86 (void **) &decompressed
, &usize
, &csize
, 0);
89 r
= decompress("\100000000g", 9,
90 (void **) &decompressed
, &usize
, &csize
, 0);
93 memzero(decompressed
, usize
);
96 static void test_decompress_startswith(int compression
,
97 compress_blob_t compress
,
98 decompress_sw_t decompress_sw
,
103 char compressed
[512];
106 _cleanup_free_
char *decompressed
= NULL
;
109 log_info("/* testing decompress_startswith with %s on %s text*/",
110 object_compressed_to_string(compression
), data
);
112 r
= compress(data
, data_len
, compressed
, &csize
);
114 log_info_errno(r
, "compression failed: %m");
120 assert_se(decompress_sw(compressed
,
122 (void **) &decompressed
,
124 data
, strlen(data
), '\0') > 0);
125 assert_se(decompress_sw(compressed
,
127 (void **) &decompressed
,
129 data
, strlen(data
), 'w') == 0);
130 assert_se(decompress_sw(compressed
,
132 (void **) &decompressed
,
134 "barbarbar", 9, ' ') == 0);
135 assert_se(decompress_sw(compressed
,
137 (void **) &decompressed
,
139 data
, strlen(data
), '\0') > 0);
142 static void test_compress_stream(int compression
,
144 compress_stream_t compress
,
145 decompress_stream_t decompress
,
146 const char *srcfile
) {
148 _cleanup_close_
int src
= -1, dst
= -1, dst2
= -1;
149 char pattern
[] = "/tmp/systemd-test.compressed.XXXXXX",
150 pattern2
[] = "/tmp/systemd-test.compressed.XXXXXX";
152 _cleanup_free_
char *cmd
= NULL
, *cmd2
;
155 log_debug("/* testing %s compression */",
156 object_compressed_to_string(compression
));
158 log_debug("/* create source from %s */", srcfile
);
160 assert_se((src
= open(srcfile
, O_RDONLY
|O_CLOEXEC
)) >= 0);
162 log_debug("/* test compression */");
164 assert_se((dst
= mkostemp_safe(pattern
, O_RDWR
|O_CLOEXEC
)) >= 0);
166 assert_se(compress(src
, dst
, -1) == 0);
169 assert_se(asprintf(&cmd
, "%s %s | diff %s -", cat
, pattern
, srcfile
) > 0);
170 assert_se(system(cmd
) == 0);
173 log_debug("/* test decompression */");
175 assert_se((dst2
= mkostemp_safe(pattern2
, O_RDWR
|O_CLOEXEC
)) >= 0);
177 assert_se(stat(srcfile
, &st
) == 0);
179 assert_se(lseek(dst
, 0, SEEK_SET
) == 0);
180 r
= decompress(dst
, dst2
, st
.st_size
);
183 assert_se(asprintf(&cmd2
, "diff %s %s", srcfile
, pattern2
) > 0);
184 assert_se(system(cmd2
) == 0);
186 log_debug("/* test faulty decompression */");
188 assert_se(lseek(dst
, 1, SEEK_SET
) == 1);
189 r
= decompress(dst
, dst2
, st
.st_size
);
190 assert_se(r
== -EBADMSG
|| r
== 0);
192 assert_se(lseek(dst
, 0, SEEK_SET
) == 0);
193 assert_se(lseek(dst2
, 0, SEEK_SET
) == 0);
194 r
= decompress(dst
, dst2
, st
.st_size
- 1);
195 assert_se(r
== -EFBIG
);
197 assert_se(unlink(pattern
) == 0);
198 assert_se(unlink(pattern2
) == 0);
201 int main(int argc
, char *argv
[]) {
203 "text\0foofoofoofoo AAAA aaaaaaaaa ghost busters barbarbar FFF"
204 "foofoofoofoo AAAA aaaaaaaaa ghost busters barbarbar FFF";
206 char data
[512] = "random\0";
208 log_set_max_level(LOG_DEBUG
);
210 random_bytes(data
+ 7, sizeof(data
) - 7);
213 test_compress_decompress(OBJECT_COMPRESSED_XZ
, compress_blob_xz
, decompress_blob_xz
,
214 text
, sizeof(text
), false);
215 test_compress_decompress(OBJECT_COMPRESSED_XZ
, compress_blob_xz
, decompress_blob_xz
,
216 data
, sizeof(data
), true);
217 test_decompress_startswith(OBJECT_COMPRESSED_XZ
,
218 compress_blob_xz
, decompress_startswith_xz
,
219 text
, sizeof(text
), false);
220 test_decompress_startswith(OBJECT_COMPRESSED_XZ
,
221 compress_blob_xz
, decompress_startswith_xz
,
222 data
, sizeof(data
), true);
223 test_compress_stream(OBJECT_COMPRESSED_XZ
, "xzcat",
224 compress_stream_xz
, decompress_stream_xz
, argv
[0]);
226 log_info("/* XZ test skipped */");
230 test_compress_decompress(OBJECT_COMPRESSED_LZ4
, compress_blob_lz4
, decompress_blob_lz4
,
231 text
, sizeof(text
), false);
232 test_compress_decompress(OBJECT_COMPRESSED_LZ4
, compress_blob_lz4
, decompress_blob_lz4
,
233 data
, sizeof(data
), true);
234 test_decompress_startswith(OBJECT_COMPRESSED_LZ4
,
235 compress_blob_lz4
, decompress_startswith_lz4
,
236 text
, sizeof(text
), false);
237 test_decompress_startswith(OBJECT_COMPRESSED_LZ4
,
238 compress_blob_lz4
, decompress_startswith_lz4
,
239 data
, sizeof(data
), true);
241 test_compress_stream(OBJECT_COMPRESSED_LZ4
, "lz4cat",
242 compress_stream_lz4
, decompress_stream_lz4
, argv
[0]);
244 log_info("/* LZ4 test skipped */");