+/* SPDX-License-Identifier: LGPL-2.1+ */
/***
This file is part of systemd
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
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,
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
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);
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);
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);
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;
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);
#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];
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,
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,
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
#endif
return 0;
+#else
+ return EXIT_TEST_SKIP;
+#endif
}