1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd
5 Copyright 2014 Zbigniew Jędrzejewski-Szmek
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 #include "alloc-util.h"
25 #include "parse-util.h"
26 #include "random-util.h"
27 #include "string-util.h"
30 typedef int (compress_t
)(const void *src
, uint64_t src_size
, void *dst
,
31 size_t dst_alloc_size
, size_t *dst_size
);
32 typedef int (decompress_t
)(const void *src
, uint64_t src_size
,
33 void **dst
, size_t *dst_alloc_size
, size_t* dst_size
, size_t dst_max
);
35 #if HAVE_XZ || HAVE_LZ4
37 static usec_t arg_duration
;
38 static size_t arg_start
;
40 #define MAX_SIZE (1024*1024LU)
41 #define PRIME 1048571 /* A prime close enough to one megabyte that mod 4 == 3 */
43 static size_t _permute(size_t x
) {
49 residue
= x
*x
% PRIME
;
53 return PRIME
- residue
;
56 static size_t permute(size_t x
) {
57 return _permute((_permute(x
) + arg_start
) % MAX_SIZE
^ 0xFF345);
60 static char* make_buf(size_t count
, const char *type
) {
67 if (streq(type
, "zeros"))
69 else if (streq(type
, "simple"))
70 for (i
= 0; i
< count
; i
++)
71 buf
[i
] = 'a' + i
% ('z' - 'a' + 1);
72 else if (streq(type
, "random")) {
73 size_t step
= count
/ 10;
75 random_bytes(buf
, step
);
76 memzero(buf
+ 1*step
, step
);
77 random_bytes(buf
+ 2*step
, step
);
78 memzero(buf
+ 3*step
, step
);
79 random_bytes(buf
+ 4*step
, step
);
80 memzero(buf
+ 5*step
, step
);
81 random_bytes(buf
+ 6*step
, step
);
82 memzero(buf
+ 7*step
, step
);
83 random_bytes(buf
+ 8*step
, step
);
84 memzero(buf
+ 9*step
, step
);
86 assert_not_reached("here");
91 static void test_compress_decompress(const char* label
, const char* type
,
92 compress_t compress
, decompress_t decompress
) {
96 _cleanup_free_
char *text
, *buf
;
97 _cleanup_free_
void *buf2
= NULL
;
98 size_t buf2_allocated
= 0;
99 size_t skipped
= 0, compressed
= 0, total
= 0;
101 text
= make_buf(MAX_SIZE
, type
);
102 buf
= calloc(MAX_SIZE
+ 1, 1);
103 assert_se(text
&& buf
);
105 n
= now(CLOCK_MONOTONIC
);
107 for (size_t i
= 0; i
<= MAX_SIZE
; i
++) {
108 size_t j
= 0, k
= 0, size
;
115 log_debug("%s %zu %zu", type
, i
, size
);
117 memzero(buf
, MIN(size
+ 1000, MAX_SIZE
));
119 r
= compress(text
, size
, buf
, size
, &j
);
120 /* assume compression must be successful except for small or random inputs */
121 assert_se(r
== 0 || (size
< 2048 && r
== -ENOBUFS
) || streq(type
, "random"));
123 /* check for overwrites */
124 assert_se(buf
[size
] == 0);
132 log_error("%s \"compressed\" %zu -> %zu", label
, size
, j
);
134 r
= decompress(buf
, j
, &buf2
, &buf2_allocated
, &k
, 0);
136 assert_se(buf2_allocated
>= k
);
137 assert_se(k
== size
);
139 assert_se(memcmp(text
, buf2
, size
) == 0);
144 n2
= now(CLOCK_MONOTONIC
);
145 if (n2
- n
> arg_duration
)
151 log_info("%s/%s: compressed & decompressed %zu bytes in %.2fs (%.2fMiB/s), "
152 "mean compresion %.2f%%, skipped %zu bytes",
153 label
, type
, total
, dt
,
154 total
/ 1024. / 1024 / dt
,
155 100 - compressed
* 100. / total
,
160 int main(int argc
, char *argv
[]) {
161 #if HAVE_XZ || HAVE_LZ4
165 log_set_max_level(LOG_INFO
);
170 assert_se(safe_atou(argv
[1], &x
) >= 0);
171 arg_duration
= x
* USEC_PER_SEC
;
175 r
= getenv_bool("SYSTEMD_SLOW_TESTS");
176 slow
= r
>= 0 ? r
: SYSTEMD_SLOW_TESTS_DEFAULT
;
178 arg_duration
= slow
? 2 * USEC_PER_SEC
: USEC_PER_SEC
/ 50;
182 (void) safe_atozu(argv
[2], &arg_start
);
184 arg_start
= getpid_cached();
186 NULSTR_FOREACH(i
, "zeros\0simple\0random\0") {
188 test_compress_decompress("XZ", i
, compress_blob_xz
, decompress_blob_xz
);
191 test_compress_decompress("LZ4", i
, compress_blob_lz4
, decompress_blob_lz4
);
196 return EXIT_TEST_SKIP
;