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/>.
20 #include "alloc-util.h"
25 #include "random-util.h"
31 # define XZ_OK -EPROTONOSUPPORT
37 # define LZ4_OK -EPROTONOSUPPORT
40 typedef int (compress_blob_t
)(const void *src
, uint64_t src_size
,
41 void *dst
, size_t *dst_size
);
42 typedef int (decompress_blob_t
)(const void *src
, uint64_t src_size
,
43 void **dst
, size_t *dst_alloc_size
,
44 size_t* dst_size
, size_t dst_max
);
45 typedef int (decompress_sw_t
)(const void *src
, uint64_t src_size
,
46 void **buffer
, size_t *buffer_size
,
47 const void *prefix
, size_t prefix_len
,
50 typedef int (compress_stream_t
)(int fdf
, int fdt
, uint64_t max_bytes
);
51 typedef int (decompress_stream_t
)(int fdf
, int fdt
, uint64_t max_size
);
53 static void test_compress_decompress(int compression
,
54 compress_blob_t compress
,
55 decompress_blob_t decompress
,
62 _cleanup_free_
char *decompressed
= NULL
;
65 log_info("/* testing %s %s blob compression/decompression */",
66 object_compressed_to_string(compression
), data
);
68 r
= compress(data
, data_len
, compressed
, &csize
);
70 log_info_errno(r
, "compression failed: %m");
74 r
= decompress(compressed
, csize
,
75 (void **) &decompressed
, &usize
, &csize
, 0);
77 assert_se(decompressed
);
78 assert_se(memcmp(decompressed
, data
, data_len
) == 0);
81 r
= decompress("garbage", 7,
82 (void **) &decompressed
, &usize
, &csize
, 0);
85 /* make sure to have the minimal lz4 compressed size */
86 r
= decompress("00000000\1g", 9,
87 (void **) &decompressed
, &usize
, &csize
, 0);
90 r
= decompress("\100000000g", 9,
91 (void **) &decompressed
, &usize
, &csize
, 0);
94 memzero(decompressed
, usize
);
97 static void test_decompress_startswith(int compression
,
98 compress_blob_t compress
,
99 decompress_sw_t decompress_sw
,
104 char compressed
[512];
107 _cleanup_free_
char *decompressed
= NULL
;
110 log_info("/* testing decompress_startswith with %s on %s text*/",
111 object_compressed_to_string(compression
), data
);
113 r
= compress(data
, data_len
, compressed
, &csize
);
115 log_info_errno(r
, "compression failed: %m");
121 assert_se(decompress_sw(compressed
,
123 (void **) &decompressed
,
125 data
, strlen(data
), '\0') > 0);
126 assert_se(decompress_sw(compressed
,
128 (void **) &decompressed
,
130 data
, strlen(data
), 'w') == 0);
131 assert_se(decompress_sw(compressed
,
133 (void **) &decompressed
,
135 "barbarbar", 9, ' ') == 0);
136 assert_se(decompress_sw(compressed
,
138 (void **) &decompressed
,
140 data
, strlen(data
), '\0') > 0);
143 static void test_compress_stream(int compression
,
145 compress_stream_t compress
,
146 decompress_stream_t decompress
,
147 const char *srcfile
) {
149 _cleanup_close_
int src
= -1, dst
= -1, dst2
= -1;
150 char pattern
[] = "/tmp/systemd-test.compressed.XXXXXX",
151 pattern2
[] = "/tmp/systemd-test.compressed.XXXXXX";
153 _cleanup_free_
char *cmd
= NULL
, *cmd2
;
156 log_debug("/* testing %s compression */",
157 object_compressed_to_string(compression
));
159 log_debug("/* create source from %s */", srcfile
);
161 assert_se((src
= open(srcfile
, O_RDONLY
|O_CLOEXEC
)) >= 0);
163 log_debug("/* test compression */");
165 assert_se((dst
= mkostemp_safe(pattern
, O_RDWR
|O_CLOEXEC
)) >= 0);
167 assert_se(compress(src
, dst
, -1) == 0);
170 assert_se(asprintf(&cmd
, "%s %s | diff %s -", cat
, pattern
, srcfile
) > 0);
171 assert_se(system(cmd
) == 0);
174 log_debug("/* test decompression */");
176 assert_se((dst2
= mkostemp_safe(pattern2
, O_RDWR
|O_CLOEXEC
)) >= 0);
178 assert_se(stat(srcfile
, &st
) == 0);
180 assert_se(lseek(dst
, 0, SEEK_SET
) == 0);
181 r
= decompress(dst
, dst2
, st
.st_size
);
184 assert_se(asprintf(&cmd2
, "diff %s %s", srcfile
, pattern2
) > 0);
185 assert_se(system(cmd2
) == 0);
187 log_debug("/* test faulty decompression */");
189 assert_se(lseek(dst
, 1, SEEK_SET
) == 1);
190 r
= decompress(dst
, dst2
, st
.st_size
);
191 assert_se(r
== -EBADMSG
|| r
== 0);
193 assert_se(lseek(dst
, 0, SEEK_SET
) == 0);
194 assert_se(lseek(dst2
, 0, SEEK_SET
) == 0);
195 r
= decompress(dst
, dst2
, st
.st_size
- 1);
196 assert_se(r
== -EFBIG
);
198 assert_se(unlink(pattern
) == 0);
199 assert_se(unlink(pattern2
) == 0);
202 int main(int argc
, char *argv
[]) {
204 "text\0foofoofoofoo AAAA aaaaaaaaa ghost busters barbarbar FFF"
205 "foofoofoofoo AAAA aaaaaaaaa ghost busters barbarbar FFF";
207 char data
[512] = "random\0";
209 log_set_max_level(LOG_DEBUG
);
211 random_bytes(data
+ 7, sizeof(data
) - 7);
214 test_compress_decompress(OBJECT_COMPRESSED_XZ
, compress_blob_xz
, decompress_blob_xz
,
215 text
, sizeof(text
), false);
216 test_compress_decompress(OBJECT_COMPRESSED_XZ
, compress_blob_xz
, decompress_blob_xz
,
217 data
, sizeof(data
), true);
218 test_decompress_startswith(OBJECT_COMPRESSED_XZ
,
219 compress_blob_xz
, decompress_startswith_xz
,
220 text
, sizeof(text
), false);
221 test_decompress_startswith(OBJECT_COMPRESSED_XZ
,
222 compress_blob_xz
, decompress_startswith_xz
,
223 data
, sizeof(data
), true);
224 test_compress_stream(OBJECT_COMPRESSED_XZ
, "xzcat",
225 compress_stream_xz
, decompress_stream_xz
, argv
[0]);
227 log_info("/* XZ test skipped */");
231 test_compress_decompress(OBJECT_COMPRESSED_LZ4
, compress_blob_lz4
, decompress_blob_lz4
,
232 text
, sizeof(text
), false);
233 test_compress_decompress(OBJECT_COMPRESSED_LZ4
, compress_blob_lz4
, decompress_blob_lz4
,
234 data
, sizeof(data
), true);
235 test_decompress_startswith(OBJECT_COMPRESSED_LZ4
,
236 compress_blob_lz4
, decompress_startswith_lz4
,
237 text
, sizeof(text
), false);
238 test_decompress_startswith(OBJECT_COMPRESSED_LZ4
,
239 compress_blob_lz4
, decompress_startswith_lz4
,
240 data
, sizeof(data
), true);
242 test_compress_stream(OBJECT_COMPRESSED_LZ4
, "lz4cat",
243 compress_stream_lz4
, decompress_stream_lz4
, argv
[0]);
245 log_info("/* LZ4 test skipped */");