test-compress.c revision 843fecc076c8a58cda13e51362d145f7452db0e9
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering/***
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering This file is part of systemd
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering Copyright 2014 Ronny Chevalier
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering systemd is free software; you can redistribute it and/or modify it
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering under the terms of the GNU Lesser General Public License as published by
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering (at your option) any later version.
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering systemd is distributed in the hope that it will be useful, but
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering Lesser General Public License for more details.
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering You should have received a copy of the GNU Lesser General Public License
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering***/
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#include "compress.h"
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#include "util.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "macro.h"
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poetteringstatic void test_compress_uncompress(void) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering char text[] = "foofoofoofoo AAAA aaaaaaaaa ghost busters barbarbar FFF"
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering "foofoofoofoo AAAA aaaaaaaaa ghost busters barbarbar FFF";
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering char compressed[512];
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering uint64_t csize = 512;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering uint64_t usize = 0;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering _cleanup_free_ char *uncompressed = NULL;
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering assert_se(compress_blob(text, sizeof(text), compressed, &csize));
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert_se(uncompress_blob(compressed,
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering csize,
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering (void **) &uncompressed,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering &usize, &csize, 0));
9bf3b53533cdc9b95c921b71da755401f223f765Lennart Poettering assert_se(uncompressed);
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering assert_se(streq(uncompressed, text));
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering assert_se(!uncompress_blob("garbage",
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering 7,
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering (void **) &uncompressed,
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering &usize, &csize, 0));
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering}
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringstatic void test_uncompress_startswith(void) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering char text[] = "foofoofoofoo AAAA aaaaaaaaa ghost busters barbarbar FFF"
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering "foofoofoofoo AAAA aaaaaaaaa ghost busters barbarbar FFF";
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering char compressed[512];
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering uint64_t csize = 512;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering uint64_t usize = 0;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering _cleanup_free_ char *uncompressed = NULL;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert_se(compress_blob(text, sizeof(text), compressed, &csize));
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert_se(uncompress_startswith(compressed,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering csize,
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poettering (void **) &uncompressed,
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poettering &usize,
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poettering "foofoofoofoo", 12, ' '));
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert_se(!uncompress_startswith(compressed,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering csize,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering (void **) &uncompressed,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering &usize,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering "foofoofoofoo", 12, 'w'));
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poettering assert_se(!uncompress_startswith(compressed,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering csize,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering (void **) &uncompressed,
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen &usize,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering "barbarbar", 9, ' '));
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering}
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersenint main(int argc, char *argv[]) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering test_compress_uncompress();
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering test_uncompress_startswith();
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return 0;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering}
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering