]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/journal/test-compress.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / journal / test-compress.c
index 68c9a4d76c0a76a289a29ffd1f622b5b7b2187e7..1b9b186be37ad61223d0cf527642304973dbb1ef 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd
 
@@ -17,7 +18,7 @@
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
-#ifdef HAVE_LZ4
+#if HAVE_LZ4
 #include <lz4.h>
 #endif
 
 #include "random-util.h"
 #include "util.h"
 
-#ifdef HAVE_XZ
+#if HAVE_XZ
 # define XZ_OK 0
 #else
 # define XZ_OK -EPROTONOSUPPORT
 #endif
 
-#ifdef HAVE_LZ4
+#if HAVE_LZ4
 # define LZ4_OK 0
 #else
 # define LZ4_OK -EPROTONOSUPPORT
@@ -54,6 +55,7 @@ typedef int (decompress_sw_t)(const void *src, uint64_t src_size,
 typedef int (compress_stream_t)(int fdf, int fdt, uint64_t max_bytes);
 typedef int (decompress_stream_t)(int fdf, int fdt, uint64_t max_size);
 
+#if HAVE_XZ || HAVE_LZ4
 static void test_compress_decompress(int compression,
                                      compress_blob_t compress,
                                      decompress_blob_t decompress,
@@ -109,7 +111,7 @@ static void test_decompress_startswith(int compression,
         size_t csize, usize = 0, len;
         int r;
 
-        log_info("/* testing decompress_startswith with %s on %.20s text*/",
+        log_info("/* testing decompress_startswith with %s on %.20s text */",
                  object_compressed_to_string(compression), data);
 
 #define BUFSIZE_1 512
@@ -167,7 +169,7 @@ static void test_compress_stream(int compression,
 
         log_debug("/* test compression */");
 
-        assert_se((dst = mkostemp_safe(pattern, O_RDWR|O_CLOEXEC)) >= 0);
+        assert_se((dst = mkostemp_safe(pattern)) >= 0);
 
         assert_se(compress(src, dst, -1) == 0);
 
@@ -178,7 +180,7 @@ static void test_compress_stream(int compression,
 
         log_debug("/* test decompression */");
 
-        assert_se((dst2 = mkostemp_safe(pattern2, O_RDWR|O_CLOEXEC)) >= 0);
+        assert_se((dst2 = mkostemp_safe(pattern2)) >= 0);
 
         assert_se(stat(srcfile, &st) == 0);
 
@@ -193,7 +195,7 @@ static void test_compress_stream(int compression,
 
         assert_se(lseek(dst, 1, SEEK_SET) == 1);
         r = decompress(dst, dst2, st.st_size);
-        assert_se(r == -EBADMSG || r == 0);
+        assert_se(IN_SET(r, 0, -EBADMSG));
 
         assert_se(lseek(dst, 0, SEEK_SET) == 0);
         assert_se(lseek(dst2, 0, SEEK_SET) == 0);
@@ -203,8 +205,9 @@ static void test_compress_stream(int compression,
         assert_se(unlink(pattern) == 0);
         assert_se(unlink(pattern2) == 0);
 }
+#endif
 
-#ifdef HAVE_LZ4
+#if HAVE_LZ4
 static void test_lz4_decompress_partial(void) {
         char buf[20000];
         size_t buf_size = sizeof(buf), compressed;
@@ -216,7 +219,11 @@ static void test_lz4_decompress_partial(void) {
         memset(huge, 'x', HUGE_SIZE);
         memcpy(huge, "HUGE=", 5);
 
+#if LZ4_VERSION_NUMBER >= 10700
+        r = LZ4_compress_default(huge, buf, HUGE_SIZE, buf_size);
+#else
         r = LZ4_compress_limitedOutput(huge, buf, HUGE_SIZE, buf_size);
+#endif
         assert_se(r >= 0);
         compressed = r;
         log_info("Compressed %i → %zu", HUGE_SIZE, compressed);
@@ -243,10 +250,14 @@ static void test_lz4_decompress_partial(void) {
 #endif
 
 int main(int argc, char *argv[]) {
+#if HAVE_XZ || HAVE_LZ4
         const char text[] =
                 "text\0foofoofoofoo AAAA aaaaaaaaa ghost busters barbarbar FFF"
                 "foofoofoofoo AAAA aaaaaaaaa ghost busters barbarbar FFF";
 
+        /* The file to test compression on can be specified as the first argument */
+        const char *srcfile = argc > 1 ? argv[1] : argv[0];
+
         char data[512] = "random\0";
 
         char huge[4096*1024];
@@ -258,7 +269,7 @@ int main(int argc, char *argv[]) {
 
         random_bytes(data + 7, sizeof(data) - 7);
 
-#ifdef HAVE_XZ
+#if HAVE_XZ
         test_compress_decompress(OBJECT_COMPRESSED_XZ, compress_blob_xz, decompress_blob_xz,
                                  text, sizeof(text), false);
         test_compress_decompress(OBJECT_COMPRESSED_XZ, compress_blob_xz, decompress_blob_xz,
@@ -275,12 +286,12 @@ int main(int argc, char *argv[]) {
                                    huge, sizeof(huge), true);
 
         test_compress_stream(OBJECT_COMPRESSED_XZ, "xzcat",
-                             compress_stream_xz, decompress_stream_xz, argv[0]);
+                             compress_stream_xz, decompress_stream_xz, srcfile);
 #else
         log_info("/* XZ test skipped */");
 #endif
 
-#ifdef HAVE_LZ4
+#if HAVE_LZ4
         test_compress_decompress(OBJECT_COMPRESSED_LZ4, compress_blob_lz4, decompress_blob_lz4,
                                  text, sizeof(text), false);
         test_compress_decompress(OBJECT_COMPRESSED_LZ4, compress_blob_lz4, decompress_blob_lz4,
@@ -297,7 +308,7 @@ int main(int argc, char *argv[]) {
                                    huge, sizeof(huge), true);
 
         test_compress_stream(OBJECT_COMPRESSED_LZ4, "lz4cat",
-                             compress_stream_lz4, decompress_stream_lz4, argv[0]);
+                             compress_stream_lz4, decompress_stream_lz4, srcfile);
 
         test_lz4_decompress_partial();
 #else
@@ -305,4 +316,7 @@ int main(int argc, char *argv[]) {
 #endif
 
         return 0;
+#else
+        return EXIT_TEST_SKIP;
+#endif
 }