]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/test-compress.c
util-lib: split out globbing related calls into glob-util.[ch]
[thirdparty/systemd.git] / src / journal / test-compress.c
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 "fd-util.h"
22 #include "fileio.h"
23 #include "macro.h"
24 #include "random-util.h"
25 #include "util.h"
26
27 #ifdef HAVE_XZ
28 # define XZ_OK 0
29 #else
30 # define XZ_OK -EPROTONOSUPPORT
31 #endif
32
33 #ifdef HAVE_LZ4
34 # define LZ4_OK 0
35 #else
36 # define LZ4_OK -EPROTONOSUPPORT
37 #endif
38
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,
47 uint8_t extra);
48
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);
51
52 static void test_compress_decompress(int compression,
53 compress_blob_t compress,
54 decompress_blob_t decompress,
55 const char *data,
56 size_t data_len,
57 bool may_fail) {
58 char compressed[512];
59 size_t csize = 512;
60 size_t usize = 0;
61 _cleanup_free_ char *decompressed = NULL;
62 int r;
63
64 log_info("/* testing %s %s blob compression/decompression */",
65 object_compressed_to_string(compression), data);
66
67 r = compress(data, data_len, compressed, &csize);
68 if (r == -ENOBUFS) {
69 log_info_errno(r, "compression failed: %m");
70 assert_se(may_fail);
71 } else {
72 assert_se(r == 0);
73 r = decompress(compressed, csize,
74 (void **) &decompressed, &usize, &csize, 0);
75 assert_se(r == 0);
76 assert_se(decompressed);
77 assert_se(memcmp(decompressed, data, data_len) == 0);
78 }
79
80 r = decompress("garbage", 7,
81 (void **) &decompressed, &usize, &csize, 0);
82 assert_se(r < 0);
83
84 /* make sure to have the minimal lz4 compressed size */
85 r = decompress("00000000\1g", 9,
86 (void **) &decompressed, &usize, &csize, 0);
87 assert_se(r < 0);
88
89 r = decompress("\100000000g", 9,
90 (void **) &decompressed, &usize, &csize, 0);
91 assert_se(r < 0);
92
93 memzero(decompressed, usize);
94 }
95
96 static void test_decompress_startswith(int compression,
97 compress_blob_t compress,
98 decompress_sw_t decompress_sw,
99 const char *data,
100 size_t data_len,
101 bool may_fail) {
102
103 char compressed[512];
104 size_t csize = 512;
105 size_t usize = 0;
106 _cleanup_free_ char *decompressed = NULL;
107 int r;
108
109 log_info("/* testing decompress_startswith with %s on %s text*/",
110 object_compressed_to_string(compression), data);
111
112 r = compress(data, data_len, compressed, &csize);
113 if (r == -ENOBUFS) {
114 log_info_errno(r, "compression failed: %m");
115 assert_se(may_fail);
116 return;
117 }
118 assert_se(r == 0);
119
120 assert_se(decompress_sw(compressed,
121 csize,
122 (void **) &decompressed,
123 &usize,
124 data, strlen(data), '\0') > 0);
125 assert_se(decompress_sw(compressed,
126 csize,
127 (void **) &decompressed,
128 &usize,
129 data, strlen(data), 'w') == 0);
130 assert_se(decompress_sw(compressed,
131 csize,
132 (void **) &decompressed,
133 &usize,
134 "barbarbar", 9, ' ') == 0);
135 assert_se(decompress_sw(compressed,
136 csize,
137 (void **) &decompressed,
138 &usize,
139 data, strlen(data), '\0') > 0);
140 }
141
142 static void test_compress_stream(int compression,
143 const char* cat,
144 compress_stream_t compress,
145 decompress_stream_t decompress,
146 const char *srcfile) {
147
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";
151 int r;
152 _cleanup_free_ char *cmd = NULL, *cmd2;
153 struct stat st = {};
154
155 log_debug("/* testing %s compression */",
156 object_compressed_to_string(compression));
157
158 log_debug("/* create source from %s */", srcfile);
159
160 assert_se((src = open(srcfile, O_RDONLY|O_CLOEXEC)) >= 0);
161
162 log_debug("/* test compression */");
163
164 assert_se((dst = mkostemp_safe(pattern, O_RDWR|O_CLOEXEC)) >= 0);
165
166 assert_se(compress(src, dst, -1) == 0);
167
168 if (cat) {
169 assert_se(asprintf(&cmd, "%s %s | diff %s -", cat, pattern, srcfile) > 0);
170 assert_se(system(cmd) == 0);
171 }
172
173 log_debug("/* test decompression */");
174
175 assert_se((dst2 = mkostemp_safe(pattern2, O_RDWR|O_CLOEXEC)) >= 0);
176
177 assert_se(stat(srcfile, &st) == 0);
178
179 assert_se(lseek(dst, 0, SEEK_SET) == 0);
180 r = decompress(dst, dst2, st.st_size);
181 assert_se(r == 0);
182
183 assert_se(asprintf(&cmd2, "diff %s %s", srcfile, pattern2) > 0);
184 assert_se(system(cmd2) == 0);
185
186 log_debug("/* test faulty decompression */");
187
188 assert_se(lseek(dst, 1, SEEK_SET) == 1);
189 r = decompress(dst, dst2, st.st_size);
190 assert_se(r == -EBADMSG || r == 0);
191
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);
196
197 assert_se(unlink(pattern) == 0);
198 assert_se(unlink(pattern2) == 0);
199 }
200
201 int main(int argc, char *argv[]) {
202 const char text[] =
203 "text\0foofoofoofoo AAAA aaaaaaaaa ghost busters barbarbar FFF"
204 "foofoofoofoo AAAA aaaaaaaaa ghost busters barbarbar FFF";
205
206 char data[512] = "random\0";
207
208 log_set_max_level(LOG_DEBUG);
209
210 random_bytes(data + 7, sizeof(data) - 7);
211
212 #ifdef HAVE_XZ
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]);
225 #else
226 log_info("/* XZ test skipped */");
227 #endif
228
229 #ifdef HAVE_LZ4
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);
240
241 test_compress_stream(OBJECT_COMPRESSED_LZ4, "lz4cat",
242 compress_stream_lz4, decompress_stream_lz4, argv[0]);
243 #else
244 log_info("/* LZ4 test skipped */");
245 #endif
246
247 return 0;
248 }