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