chiark / gitweb /
test-compress: make sure asserts with side effects use assert_se()
[elogind.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         char text[] = "foofoofoofoo AAAA aaaaaaaaa ghost busters barbarbar FFF"
53                       "foofoofoofoo AAAA aaaaaaaaa ghost busters barbarbar FFF";
54         char compressed[512];
55         size_t csize = 512;
56         size_t usize = 0;
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);
85 }
86
87 static void test_decompress_startswith(int compression,
88                                        compress_blob_t compress,
89                                        decompress_sw_t decompress_sw) {
90
91         char text[] = "foofoofoofoo AAAA aaaaaaaaa ghost busters barbarbar FFF"
92                       "foofoofoofoo AAAA aaaaaaaaa ghost busters barbarbar FFF";
93         char compressed[512];
94         size_t csize = 512;
95         size_t usize = 0;
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);
122 }
123
124 static 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
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;
134         _cleanup_free_ char *cmd = NULL, *cmd2;
135         struct stat st = {};
136
137         log_debug("/* testing %s compression */",
138                   object_compressed_to_string(compression));
139
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
148         assert_se(compress(src, dst, -1) == 0);
149
150         if (cat) {
151                 assert_se(asprintf(&cmd, "%s %s | diff %s -", cat, pattern, srcfile) > 0);
152                 assert_se(system(cmd) == 0);
153         }
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);
162         r = decompress(dst, dst2, st.st_size);
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);
171         r = decompress(dst, dst2, st.st_size);
172         assert(r == -EBADMSG);
173
174         assert_se(lseek(dst, 0, SEEK_SET) == 0);
175         assert_se(lseek(dst2, 0, SEEK_SET) == 0);
176         r = decompress(dst, dst2, st.st_size - 1);
177         assert(r == -EFBIG);
178
179         assert_se(unlink(pattern) == 0);
180         assert_se(unlink(pattern2) == 0);
181 }
182
183 int main(int argc, char *argv[]) {
184
185         log_set_max_level(LOG_DEBUG);
186
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
205
206         return 0;
207 }