]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/journal/test-compress.c
journal/compress: use LZ4_compress_continue()
[thirdparty/systemd.git] / src / journal / test-compress.c
CommitLineData
843fecc0
RC
1/***
2 This file is part of systemd
3
4 Copyright 2014 Ronny Chevalier
5
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.
10
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.
15
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/>.
18***/
19
20#include "compress.h"
21#include "util.h"
22#include "macro.h"
23
d89c8fdf
ZJS
24#ifdef HAVE_XZ
25# define XZ_OK 0
26#else
27# define XZ_OK -EPROTONOSUPPORT
28#endif
29
92261977 30#ifdef HAVE_LZ4
d89c8fdf
ZJS
31# define LZ4_OK 0
32#else
33# define LZ4_OK -EPROTONOSUPPORT
34#endif
35
36typedef int (compress_blob_t)(const void *src, uint64_t src_size,
fa1c4b51 37 void *dst, size_t *dst_size);
d89c8fdf 38typedef int (decompress_blob_t)(const void *src, uint64_t src_size,
fa1c4b51
ZJS
39 void **dst, size_t *dst_alloc_size,
40 size_t* dst_size, size_t dst_max);
d89c8fdf 41typedef int (decompress_sw_t)(const void *src, uint64_t src_size,
fa1c4b51
ZJS
42 void **buffer, size_t *buffer_size,
43 const void *prefix, size_t prefix_len,
d89c8fdf
ZJS
44 uint8_t extra);
45
46typedef int (compress_stream_t)(int fdf, int fdt, off_t max_bytes);
47typedef int (decompress_stream_t)(int fdf, int fdt, off_t max_size);
48
49static void test_compress_decompress(int compression,
50 compress_blob_t compress,
51 decompress_blob_t decompress) {
843fecc0
RC
52 char text[] = "foofoofoofoo AAAA aaaaaaaaa ghost busters barbarbar FFF"
53 "foofoofoofoo AAAA aaaaaaaaa ghost busters barbarbar FFF";
54 char compressed[512];
fa1c4b51
ZJS
55 size_t csize = 512;
56 size_t usize = 0;
d89c8fdf
ZJS
57 _cleanup_free_ char *decompressed = NULL;
58 int r;
59
60 log_info("/* testing %s blob compression/decompression */",
61 object_compressed_to_string(compression));
62
63 r = compress(text, sizeof(text), compressed, &csize);
64 assert(r == 0);
65 r = decompress(compressed, csize,
66 (void **) &decompressed, &usize, &csize, 0);
67 assert(r == 0);
68 assert_se(decompressed);
69 assert_se(streq(decompressed, text));
70
71 r = decompress("garbage", 7,
72 (void **) &decompressed, &usize, &csize, 0);
73 assert(r < 0);
74
75 /* make sure to have the minimal lz4 compressed size */
76 r = decompress("00000000\1g", 9,
77 (void **) &decompressed, &usize, &csize, 0);
78 assert(r < 0);
79
80 r = decompress("\100000000g", 9,
81 (void **) &decompressed, &usize, &csize, 0);
82 assert(r < 0);
83
84 memzero(decompressed, usize);
843fecc0
RC
85}
86
d89c8fdf
ZJS
87static void test_decompress_startswith(int compression,
88 compress_blob_t compress,
89 decompress_sw_t decompress_sw) {
90
843fecc0
RC
91 char text[] = "foofoofoofoo AAAA aaaaaaaaa ghost busters barbarbar FFF"
92 "foofoofoofoo AAAA aaaaaaaaa ghost busters barbarbar FFF";
93 char compressed[512];
fa1c4b51
ZJS
94 size_t csize = 512;
95 size_t usize = 0;
d89c8fdf
ZJS
96 _cleanup_free_ char *decompressed = NULL;
97
98 log_info("/* testing decompress_startswith with %s */",
99 object_compressed_to_string(compression));
100
101 assert_se(compress(text, sizeof(text), compressed, &csize) == 0);
102 assert_se(decompress_sw(compressed,
103 csize,
104 (void **) &decompressed,
105 &usize,
106 "foofoofoofoo", 12, ' ') > 0);
107 assert_se(decompress_sw(compressed,
108 csize,
109 (void **) &decompressed,
110 &usize,
111 "foofoofoofoo", 12, 'w') == 0);
112 assert_se(decompress_sw(compressed,
113 csize,
114 (void **) &decompressed,
115 &usize,
116 "barbarbar", 9, ' ') == 0);
117 assert_se(decompress_sw(compressed,
118 csize,
119 (void **) &decompressed,
120 &usize,
121 "foofoofoofoo", 12, ' ') > 0);
843fecc0
RC
122}
123
d89c8fdf
ZJS
124static void test_compress_stream(int compression,
125 const char* cat,
126 compress_stream_t compress,
127 decompress_stream_t decompress,
128 const char *srcfile) {
129
355b59e2
ZJS
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";
133 int r;
d89c8fdf 134 _cleanup_free_ char *cmd = NULL, *cmd2;
355b59e2
ZJS
135 struct stat st = {};
136
d89c8fdf
ZJS
137 log_debug("/* testing %s compression */",
138 object_compressed_to_string(compression));
139
355b59e2
ZJS
140 log_debug("/* create source from %s */", srcfile);
141
142 assert_se((src = open(srcfile, O_RDONLY|O_CLOEXEC)) >= 0);
143
144 log_debug("/* test compression */");
145
146 assert_se((dst = mkostemp_safe(pattern, O_RDWR|O_CLOEXEC)) >= 0);
147
52754725 148 assert_se(compress(src, dst, -1) == 0);
355b59e2 149
d89c8fdf
ZJS
150 if (cat) {
151 assert_se(asprintf(&cmd, "%s %s | diff %s -", cat, pattern, srcfile) > 0);
52754725 152 assert_se(system(cmd) == 0);
d89c8fdf 153 }
355b59e2
ZJS
154
155 log_debug("/* test decompression */");
156
157 assert_se((dst2 = mkostemp_safe(pattern2, O_RDWR|O_CLOEXEC)) >= 0);
158
159 assert_se(stat(srcfile, &st) == 0);
160
161 assert_se(lseek(dst, 0, SEEK_SET) == 0);
d89c8fdf 162 r = decompress(dst, dst2, st.st_size);
355b59e2
ZJS
163 assert(r == 0);
164
165 assert_se(asprintf(&cmd2, "diff %s %s", srcfile, pattern2) > 0);
166 assert_se(system(cmd2) == 0);
167
168 log_debug("/* test faulty decompression */");
169
170 assert_se(lseek(dst, 1, SEEK_SET) == 1);
d89c8fdf 171 r = decompress(dst, dst2, st.st_size);
355b59e2
ZJS
172 assert(r == -EBADMSG);
173
174 assert_se(lseek(dst, 0, SEEK_SET) == 0);
175 assert_se(lseek(dst2, 0, SEEK_SET) == 0);
d89c8fdf
ZJS
176 r = decompress(dst, dst2, st.st_size - 1);
177 assert(r == -EFBIG);
355b59e2
ZJS
178
179 assert_se(unlink(pattern) == 0);
180 assert_se(unlink(pattern2) == 0);
181}
182
843fecc0 183int main(int argc, char *argv[]) {
355b59e2
ZJS
184
185 log_set_max_level(LOG_DEBUG);
186
d89c8fdf
ZJS
187#ifdef HAVE_XZ
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]);
192#else
193 log_info("/* XZ test skipped */");
194#endif
195#ifdef HAVE_LZ4
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);
198
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]);
202#else
203 log_info("/* LZ4 test skipped */");
204#endif
843fecc0
RC
205
206 return 0;
207}