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/>.
27 # define XZ_OK -EPROTONOSUPPORT
33 # define LZ4_OK -EPROTONOSUPPORT
36 typedef int (compress_blob_t)(const void *src, uint64_t src_size,
37 void *dst, uint64_t *dst_size);
38 typedef int (decompress_blob_t)(const void *src, uint64_t src_size,
39 void **dst, uint64_t *dst_alloc_size,
40 uint64_t* dst_size, uint64_t dst_max);
42 typedef int (decompress_sw_t)(const void *src, uint64_t src_size,
43 void **buffer, uint64_t *buffer_size,
44 const void *prefix, uint64_t prefix_len,
47 typedef int (compress_stream_t)(int fdf, int fdt, off_t max_bytes);
48 typedef int (decompress_stream_t)(int fdf, int fdt, off_t max_size);
50 static void test_compress_decompress(int compression,
51 compress_blob_t compress,
52 decompress_blob_t decompress) {
53 char text[] = "foofoofoofoo AAAA aaaaaaaaa ghost busters barbarbar FFF"
54 "foofoofoofoo AAAA aaaaaaaaa ghost busters barbarbar FFF";
58 _cleanup_free_ char *decompressed = NULL;
61 log_info("/* testing %s blob compression/decompression */",
62 object_compressed_to_string(compression));
64 r = compress(text, sizeof(text), compressed, &csize);
66 r = decompress(compressed, csize,
67 (void **) &decompressed, &usize, &csize, 0);
69 assert_se(decompressed);
70 assert_se(streq(decompressed, text));
72 r = decompress("garbage", 7,
73 (void **) &decompressed, &usize, &csize, 0);
76 /* make sure to have the minimal lz4 compressed size */
77 r = decompress("00000000\1g", 9,
78 (void **) &decompressed, &usize, &csize, 0);
81 r = decompress("\100000000g", 9,
82 (void **) &decompressed, &usize, &csize, 0);
85 memzero(decompressed, usize);
88 static void test_decompress_startswith(int compression,
89 compress_blob_t compress,
90 decompress_sw_t decompress_sw) {
92 char text[] = "foofoofoofoo AAAA aaaaaaaaa ghost busters barbarbar FFF"
93 "foofoofoofoo AAAA aaaaaaaaa ghost busters barbarbar FFF";
97 _cleanup_free_ char *decompressed = NULL;
99 log_info("/* testing decompress_startswith with %s */",
100 object_compressed_to_string(compression));
102 assert_se(compress(text, sizeof(text), compressed, &csize) == 0);
103 assert_se(decompress_sw(compressed,
105 (void **) &decompressed,
107 "foofoofoofoo", 12, ' ') > 0);
108 assert_se(decompress_sw(compressed,
110 (void **) &decompressed,
112 "foofoofoofoo", 12, 'w') == 0);
113 assert_se(decompress_sw(compressed,
115 (void **) &decompressed,
117 "barbarbar", 9, ' ') == 0);
118 assert_se(decompress_sw(compressed,
120 (void **) &decompressed,
122 "foofoofoofoo", 12, ' ') > 0);
125 static void test_compress_stream(int compression,
127 compress_stream_t compress,
128 decompress_stream_t decompress,
129 const char *srcfile) {
131 _cleanup_close_ int src = -1, dst = -1, dst2 = -1;
132 char pattern[] = "/tmp/systemd-test.xz.XXXXXX",
133 pattern2[] = "/tmp/systemd-test.xz.XXXXXX";
135 _cleanup_free_ char *cmd = NULL, *cmd2;
138 log_debug("/* testing %s compression */",
139 object_compressed_to_string(compression));
141 log_debug("/* create source from %s */", srcfile);
143 assert_se((src = open(srcfile, O_RDONLY|O_CLOEXEC)) >= 0);
145 log_debug("/* test compression */");
147 assert_se((dst = mkostemp_safe(pattern, O_RDWR|O_CLOEXEC)) >= 0);
149 assert(compress(src, dst, -1) == 0);
152 assert_se(asprintf(&cmd, "%s %s | diff %s -", cat, pattern, srcfile) > 0);
153 assert(system(cmd) == 0);
156 log_debug("/* test decompression */");
158 assert_se((dst2 = mkostemp_safe(pattern2, O_RDWR|O_CLOEXEC)) >= 0);
160 assert_se(stat(srcfile, &st) == 0);
162 assert_se(lseek(dst, 0, SEEK_SET) == 0);
163 r = decompress(dst, dst2, st.st_size);
166 assert_se(asprintf(&cmd2, "diff %s %s", srcfile, pattern2) > 0);
167 assert_se(system(cmd2) == 0);
169 log_debug("/* test faulty decompression */");
171 assert_se(lseek(dst, 1, SEEK_SET) == 1);
172 r = decompress(dst, dst2, st.st_size);
173 assert(r == -EBADMSG);
175 assert_se(lseek(dst, 0, SEEK_SET) == 0);
176 assert_se(lseek(dst2, 0, SEEK_SET) == 0);
177 r = decompress(dst, dst2, st.st_size - 1);
180 assert_se(unlink(pattern) == 0);
181 assert_se(unlink(pattern2) == 0);
184 int main(int argc, char *argv[]) {
186 log_set_max_level(LOG_DEBUG);
189 test_compress_decompress(OBJECT_COMPRESSED_XZ, compress_blob_xz, decompress_blob_xz);
190 test_decompress_startswith(OBJECT_COMPRESSED_XZ, compress_blob_xz, decompress_startswith_xz);
191 test_compress_stream(OBJECT_COMPRESSED_XZ, "xzcat",
192 compress_stream_xz, decompress_stream_xz, argv[0]);
194 log_info("/* XZ test skipped */");
197 test_compress_decompress(OBJECT_COMPRESSED_LZ4, compress_blob_lz4, decompress_blob_lz4);
198 test_decompress_startswith(OBJECT_COMPRESSED_LZ4, compress_blob_lz4, decompress_startswith_lz4);
200 /* Produced stream is not compatible with lz4 binary, skip lz4cat check. */
201 test_compress_stream(OBJECT_COMPRESSED_LZ4, NULL,
202 compress_stream_lz4, decompress_stream_lz4, argv[0]);
204 log_info("/* LZ4 test skipped */");