]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/journal/test-compress.c
journal/compress: return early in uncompress_startswith
[thirdparty/systemd.git] / src / journal / test-compress.c
CommitLineData
843fecc0
RC
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
24static void test_compress_uncompress(void) {
25 char text[] = "foofoofoofoo AAAA aaaaaaaaa ghost busters barbarbar FFF"
26 "foofoofoofoo AAAA aaaaaaaaa ghost busters barbarbar FFF";
27 char compressed[512];
28 uint64_t csize = 512;
29 uint64_t usize = 0;
30 _cleanup_free_ char *uncompressed = NULL;
31
32 assert_se(compress_blob(text, sizeof(text), compressed, &csize));
33 assert_se(uncompress_blob(compressed,
34 csize,
35 (void **) &uncompressed,
36 &usize, &csize, 0));
37 assert_se(uncompressed);
38 assert_se(streq(uncompressed, text));
39 assert_se(!uncompress_blob("garbage",
40 7,
41 (void **) &uncompressed,
42 &usize, &csize, 0));
43}
44
45static void test_uncompress_startswith(void) {
46 char text[] = "foofoofoofoo AAAA aaaaaaaaa ghost busters barbarbar FFF"
47 "foofoofoofoo AAAA aaaaaaaaa ghost busters barbarbar FFF";
48 char compressed[512];
49 uint64_t csize = 512;
50 uint64_t usize = 0;
51 _cleanup_free_ char *uncompressed = NULL;
52
53 assert_se(compress_blob(text, sizeof(text), compressed, &csize));
54 assert_se(uncompress_startswith(compressed,
55 csize,
56 (void **) &uncompressed,
57 &usize,
58 "foofoofoofoo", 12, ' '));
59 assert_se(!uncompress_startswith(compressed,
60 csize,
61 (void **) &uncompressed,
62 &usize,
63 "foofoofoofoo", 12, 'w'));
64 assert_se(!uncompress_startswith(compressed,
65 csize,
66 (void **) &uncompressed,
67 &usize,
68 "barbarbar", 9, ' '));
69}
70
355b59e2
ZJS
71static void test_compress_stream(const char *srcfile) {
72 _cleanup_close_ int src = -1, dst = -1, dst2 = -1;
73 char pattern[] = "/tmp/systemd-test.xz.XXXXXX",
74 pattern2[] = "/tmp/systemd-test.xz.XXXXXX";
75 int r;
76 _cleanup_free_ char *cmd, *cmd2;
77 struct stat st = {};
78
79 log_debug("/* create source from %s */", srcfile);
80
81 assert_se((src = open(srcfile, O_RDONLY|O_CLOEXEC)) >= 0);
82
83 log_debug("/* test compression */");
84
85 assert_se((dst = mkostemp_safe(pattern, O_RDWR|O_CLOEXEC)) >= 0);
86
34727273 87 r = compress_stream(src, dst, 1, -1);
355b59e2
ZJS
88 assert(r == 0);
89
90 assert_se(asprintf(&cmd, "xzcat %s | diff %s -", pattern, srcfile) > 0);
91 assert_se(system(cmd) == 0);
92
93 log_debug("/* test decompression */");
94
95 assert_se((dst2 = mkostemp_safe(pattern2, O_RDWR|O_CLOEXEC)) >= 0);
96
97 assert_se(stat(srcfile, &st) == 0);
98
99 assert_se(lseek(dst, 0, SEEK_SET) == 0);
100 r = decompress_stream(dst, dst2, st.st_size);
101 assert(r == 0);
102
103 assert_se(asprintf(&cmd2, "diff %s %s", srcfile, pattern2) > 0);
104 assert_se(system(cmd2) == 0);
105
106 log_debug("/* test faulty decompression */");
107
108 assert_se(lseek(dst, 1, SEEK_SET) == 1);
109 r = decompress_stream(dst, dst2, st.st_size);
110 assert(r == -EBADMSG);
111
112 assert_se(lseek(dst, 0, SEEK_SET) == 0);
113 assert_se(lseek(dst2, 0, SEEK_SET) == 0);
114 r = decompress_stream(dst, dst2, st.st_size - 1);
115 assert(r == -E2BIG);
116
117 assert_se(unlink(pattern) == 0);
118 assert_se(unlink(pattern2) == 0);
119}
120
843fecc0 121int main(int argc, char *argv[]) {
355b59e2
ZJS
122
123 log_set_max_level(LOG_DEBUG);
124
843fecc0
RC
125 test_compress_uncompress();
126 test_uncompress_startswith();
355b59e2 127 test_compress_stream(argv[0]);
843fecc0
RC
128
129 return 0;
130}