]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/test-compress.c
2 This file is part of systemd
4 Copyright 2014 Ronny Chevalier
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.
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.
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/>.
24 static void test_compress_uncompress(void) {
25 char text
[] = "foofoofoofoo AAAA aaaaaaaaa ghost busters barbarbar FFF"
26 "foofoofoofoo AAAA aaaaaaaaa ghost busters barbarbar FFF";
30 _cleanup_free_
char *uncompressed
= NULL
;
32 assert_se(compress_blob(text
, sizeof(text
), compressed
, &csize
));
33 assert_se(uncompress_blob(compressed
,
35 (void **) &uncompressed
,
37 assert_se(uncompressed
);
38 assert_se(streq(uncompressed
, text
));
39 assert_se(!uncompress_blob("garbage",
41 (void **) &uncompressed
,
45 static void test_uncompress_startswith(void) {
46 char text
[] = "foofoofoofoo AAAA aaaaaaaaa ghost busters barbarbar FFF"
47 "foofoofoofoo AAAA aaaaaaaaa ghost busters barbarbar FFF";
51 _cleanup_free_
char *uncompressed
= NULL
;
53 assert_se(compress_blob(text
, sizeof(text
), compressed
, &csize
));
54 assert_se(uncompress_startswith(compressed
,
56 (void **) &uncompressed
,
58 "foofoofoofoo", 12, ' '));
59 assert_se(!uncompress_startswith(compressed
,
61 (void **) &uncompressed
,
63 "foofoofoofoo", 12, 'w'));
64 assert_se(!uncompress_startswith(compressed
,
66 (void **) &uncompressed
,
68 "barbarbar", 9, ' '));
71 static 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";
76 _cleanup_free_
char *cmd
, *cmd2
;
79 log_debug("/* create source from %s */", srcfile
);
81 assert_se((src
= open(srcfile
, O_RDONLY
|O_CLOEXEC
)) >= 0);
83 log_debug("/* test compression */");
85 assert_se((dst
= mkostemp_safe(pattern
, O_RDWR
|O_CLOEXEC
)) >= 0);
87 r
= compress_stream(src
, dst
, 1, -1);
90 assert_se(asprintf(&cmd
, "xzcat %s | diff %s -", pattern
, srcfile
) > 0);
91 assert_se(system(cmd
) == 0);
93 log_debug("/* test decompression */");
95 assert_se((dst2
= mkostemp_safe(pattern2
, O_RDWR
|O_CLOEXEC
)) >= 0);
97 assert_se(stat(srcfile
, &st
) == 0);
99 assert_se(lseek(dst
, 0, SEEK_SET
) == 0);
100 r
= decompress_stream(dst
, dst2
, st
.st_size
);
103 assert_se(asprintf(&cmd2
, "diff %s %s", srcfile
, pattern2
) > 0);
104 assert_se(system(cmd2
) == 0);
106 log_debug("/* test faulty decompression */");
108 assert_se(lseek(dst
, 1, SEEK_SET
) == 1);
109 r
= decompress_stream(dst
, dst2
, st
.st_size
);
110 assert(r
== -EBADMSG
);
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);
117 assert_se(unlink(pattern
) == 0);
118 assert_se(unlink(pattern2
) == 0);
121 int main(int argc
, char *argv
[]) {
123 log_set_max_level(LOG_DEBUG
);
125 test_compress_uncompress();
126 test_uncompress_startswith();
127 test_compress_stream(argv
[0]);