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