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, size_t *dst_size);
38 typedef int (decompress_blob_t)(const void *src, uint64_t src_size,
39 void **dst, size_t *dst_alloc_size,
40 size_t* dst_size, size_t dst_max);
41 typedef int (decompress_sw_t)(const void *src, uint64_t src_size,
42 void **buffer, size_t *buffer_size,
43 const void *prefix, size_t prefix_len,
46 typedef int (compress_stream_t)(int fdf, int fdt, off_t max_bytes);
47 typedef int (decompress_stream_t)(int fdf, int fdt, off_t max_size);
49 static void test_compress_decompress(int compression,
50 compress_blob_t compress,
51 decompress_blob_t decompress) {
52 char text[] = "foofoofoofoo AAAA aaaaaaaaa ghost busters barbarbar FFF"
53 "foofoofoofoo AAAA aaaaaaaaa ghost busters barbarbar FFF";
57 _cleanup_free_ char *decompressed = NULL;
60 log_info("/* testing %s blob compression/decompression */",
61 object_compressed_to_string(compression));
63 r = compress(text, sizeof(text), compressed, &csize);
65 r = decompress(compressed, csize,
66 (void **) &decompressed, &usize, &csize, 0);
68 assert_se(decompressed);
69 assert_se(streq(decompressed, text));
71 r = decompress("garbage", 7,
72 (void **) &decompressed, &usize, &csize, 0);
75 /* make sure to have the minimal lz4 compressed size */
76 r = decompress("00000000\1g", 9,
77 (void **) &decompressed, &usize, &csize, 0);
80 r = decompress("\100000000g", 9,
81 (void **) &decompressed, &usize, &csize, 0);
84 memzero(decompressed, usize);
87 static void test_decompress_startswith(int compression,
88 compress_blob_t compress,
89 decompress_sw_t decompress_sw) {
91 char text[] = "foofoofoofoo AAAA aaaaaaaaa ghost busters barbarbar FFF"
92 "foofoofoofoo AAAA aaaaaaaaa ghost busters barbarbar FFF";
96 _cleanup_free_ char *decompressed = NULL;
98 log_info("/* testing decompress_startswith with %s */",
99 object_compressed_to_string(compression));
101 assert_se(compress(text, sizeof(text), compressed, &csize) == 0);
102 assert_se(decompress_sw(compressed,
104 (void **) &decompressed,
106 "foofoofoofoo", 12, ' ') > 0);
107 assert_se(decompress_sw(compressed,
109 (void **) &decompressed,
111 "foofoofoofoo", 12, 'w') == 0);
112 assert_se(decompress_sw(compressed,
114 (void **) &decompressed,
116 "barbarbar", 9, ' ') == 0);
117 assert_se(decompress_sw(compressed,
119 (void **) &decompressed,
121 "foofoofoofoo", 12, ' ') > 0);
124 static void test_compress_stream(int compression,
126 compress_stream_t compress,
127 decompress_stream_t decompress,
128 const char *srcfile) {
130 _cleanup_close_ int src = -1, dst = -1, dst2 = -1;
131 char pattern[] = "/tmp/systemd-test.xz.XXXXXX",
132 pattern2[] = "/tmp/systemd-test.xz.XXXXXX";
134 _cleanup_free_ char *cmd = NULL, *cmd2;
137 log_debug("/* testing %s compression */",
138 object_compressed_to_string(compression));
140 log_debug("/* create source from %s */", srcfile);
142 assert_se((src = open(srcfile, O_RDONLY|O_CLOEXEC)) >= 0);
144 log_debug("/* test compression */");
146 assert_se((dst = mkostemp_safe(pattern, O_RDWR|O_CLOEXEC)) >= 0);
148 assert(compress(src, dst, -1) == 0);
151 assert_se(asprintf(&cmd, "%s %s | diff %s -", cat, pattern, srcfile) > 0);
152 assert(system(cmd) == 0);
155 log_debug("/* test decompression */");
157 assert_se((dst2 = mkostemp_safe(pattern2, O_RDWR|O_CLOEXEC)) >= 0);
159 assert_se(stat(srcfile, &st) == 0);
161 assert_se(lseek(dst, 0, SEEK_SET) == 0);
162 r = decompress(dst, dst2, st.st_size);
165 assert_se(asprintf(&cmd2, "diff %s %s", srcfile, pattern2) > 0);
166 assert_se(system(cmd2) == 0);
168 log_debug("/* test faulty decompression */");
170 assert_se(lseek(dst, 1, SEEK_SET) == 1);
171 r = decompress(dst, dst2, st.st_size);
172 assert(r == -EBADMSG);
174 assert_se(lseek(dst, 0, SEEK_SET) == 0);
175 assert_se(lseek(dst2, 0, SEEK_SET) == 0);
176 r = decompress(dst, dst2, st.st_size - 1);
179 assert_se(unlink(pattern) == 0);
180 assert_se(unlink(pattern2) == 0);
183 int main(int argc, char *argv[]) {
185 log_set_max_level(LOG_DEBUG);
188 test_compress_decompress(OBJECT_COMPRESSED_XZ, compress_blob_xz, decompress_blob_xz);
189 test_decompress_startswith(OBJECT_COMPRESSED_XZ, compress_blob_xz, decompress_startswith_xz);
190 test_compress_stream(OBJECT_COMPRESSED_XZ, "xzcat",
191 compress_stream_xz, decompress_stream_xz, argv[0]);
193 log_info("/* XZ test skipped */");
196 test_compress_decompress(OBJECT_COMPRESSED_LZ4, compress_blob_lz4, decompress_blob_lz4);
197 test_decompress_startswith(OBJECT_COMPRESSED_LZ4, compress_blob_lz4, decompress_startswith_lz4);
199 /* Produced stream is not compatible with lz4 binary, skip lz4cat check. */
200 test_compress_stream(OBJECT_COMPRESSED_LZ4, NULL,
201 compress_stream_lz4, decompress_stream_lz4, argv[0]);
203 log_info("/* LZ4 test skipped */");