chiark / gitweb /
journal/compress: simplify compress_blob
[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 static 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
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";
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
71 int main(int argc, char *argv[]) {
72         test_compress_uncompress();
73         test_uncompress_startswith();
74
75         return 0;
76 }