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