summaryrefslogtreecommitdiff
path: root/src/journal/test-compress.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/journal/test-compress.c')
-rw-r--r--src/journal/test-compress.c54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/journal/test-compress.c b/src/journal/test-compress.c
index 15b3f9aa00..b098ef93a8 100644
--- a/src/journal/test-compress.c
+++ b/src/journal/test-compress.c
@@ -68,9 +68,63 @@ static void test_uncompress_startswith(void) {
"barbarbar", 9, ' '));
}
+static void test_compress_stream(const char *srcfile) {
+ _cleanup_close_ int src = -1, dst = -1, dst2 = -1;
+ char pattern[] = "/tmp/systemd-test.xz.XXXXXX",
+ pattern2[] = "/tmp/systemd-test.xz.XXXXXX";
+ int r;
+ _cleanup_free_ char *cmd, *cmd2;
+ struct stat st = {};
+
+ log_debug("/* create source from %s */", srcfile);
+
+ assert_se((src = open(srcfile, O_RDONLY|O_CLOEXEC)) >= 0);
+
+ log_debug("/* test compression */");
+
+ assert_se((dst = mkostemp_safe(pattern, O_RDWR|O_CLOEXEC)) >= 0);
+
+ r = compress_stream(src, dst, -1);
+ assert(r == 0);
+
+ assert_se(asprintf(&cmd, "xzcat %s | diff %s -", pattern, srcfile) > 0);
+ assert_se(system(cmd) == 0);
+
+ log_debug("/* test decompression */");
+
+ assert_se((dst2 = mkostemp_safe(pattern2, O_RDWR|O_CLOEXEC)) >= 0);
+
+ assert_se(stat(srcfile, &st) == 0);
+
+ assert_se(lseek(dst, 0, SEEK_SET) == 0);
+ r = decompress_stream(dst, dst2, st.st_size);
+ assert(r == 0);
+
+ assert_se(asprintf(&cmd2, "diff %s %s", srcfile, pattern2) > 0);
+ assert_se(system(cmd2) == 0);
+
+ log_debug("/* test faulty decompression */");
+
+ assert_se(lseek(dst, 1, SEEK_SET) == 1);
+ r = decompress_stream(dst, dst2, st.st_size);
+ assert(r == -EBADMSG);
+
+ assert_se(lseek(dst, 0, SEEK_SET) == 0);
+ assert_se(lseek(dst2, 0, SEEK_SET) == 0);
+ r = decompress_stream(dst, dst2, st.st_size - 1);
+ assert(r == -E2BIG);
+
+ assert_se(unlink(pattern) == 0);
+ assert_se(unlink(pattern2) == 0);
+}
+
int main(int argc, char *argv[]) {
+
+ log_set_max_level(LOG_DEBUG);
+
test_compress_uncompress();
test_uncompress_startswith();
+ test_compress_stream(argv[0]);
return 0;
}