1 // SPDX-License-Identifier: 0BSD
3 ///////////////////////////////////////////////////////////////////////////////
5 /// \file test_memlimit.c
6 /// \brief Tests memory usage limit in decoders
8 // Author: Lasse Collin
10 ///////////////////////////////////////////////////////////////////////////////
16 #define MEMLIMIT_TOO_LOW 1234U
17 #define MEMLIMIT_HIGH_ENOUGH (2U << 20)
21 static size_t in_size
;
24 static uint8_t out
[8192];
29 test_memlimit_stream_decoder(void)
32 assert_skip("Decoder support disabled");
34 lzma_stream strm
= LZMA_STREAM_INIT
;
35 assert_lzma_ret(lzma_stream_decoder(&strm
, MEMLIMIT_TOO_LOW
, 0),
39 strm
.avail_in
= in_size
;
41 strm
.avail_out
= sizeof(out
);
43 assert_lzma_ret(lzma_code(&strm
, LZMA_FINISH
), LZMA_MEMLIMIT_ERROR
);
45 assert_uint_eq(lzma_memlimit_get(&strm
), MEMLIMIT_TOO_LOW
);
46 assert_lzma_ret(lzma_memlimit_set(&strm
, MEMLIMIT_TOO_LOW
+ 1),
48 assert_lzma_ret(lzma_memlimit_set(&strm
, MEMLIMIT_HIGH_ENOUGH
),
51 // This fails before commit 660739f99ab211edec4071de98889fb32ed04e98
52 // (liblzma <= 5.2.6, liblzma <= 5.3.3alpha). It was fixed in 5.2.7.
53 assert_lzma_ret(lzma_code(&strm
, LZMA_FINISH
), LZMA_STREAM_END
);
61 test_memlimit_stream_decoder_mt(void)
63 #ifndef MYTHREAD_ENABLED
64 assert_skip("Threading support disabled");
65 #elif !defined(HAVE_DECODERS)
66 assert_skip("Decoder support disabled");
68 lzma_stream strm
= LZMA_STREAM_INIT
;
73 .memlimit_threading
= 0,
74 .memlimit_stop
= MEMLIMIT_TOO_LOW
,
77 assert_lzma_ret(lzma_stream_decoder_mt(&strm
, &mt
), LZMA_OK
);
80 strm
.avail_in
= in_size
;
82 strm
.avail_out
= sizeof(out
);
84 assert_lzma_ret(lzma_code(&strm
, LZMA_FINISH
), LZMA_MEMLIMIT_ERROR
);
86 assert_uint_eq(lzma_memlimit_get(&strm
), MEMLIMIT_TOO_LOW
);
87 assert_lzma_ret(lzma_memlimit_set(&strm
, MEMLIMIT_TOO_LOW
+ 1),
89 assert_lzma_ret(lzma_memlimit_set(&strm
, MEMLIMIT_HIGH_ENOUGH
),
92 assert_lzma_ret(lzma_code(&strm
, LZMA_FINISH
), LZMA_STREAM_END
);
99 test_memlimit_alone_decoder(void)
101 #ifndef HAVE_DECODERS
102 assert_skip("Decoder support disabled");
105 uint8_t *alone_buf
= tuktest_file_from_srcdir(
106 "files/good-unknown_size-with_eopm.lzma", &alone_size
);
108 lzma_stream strm
= LZMA_STREAM_INIT
;
109 assert_lzma_ret(lzma_alone_decoder(&strm
, MEMLIMIT_TOO_LOW
), LZMA_OK
);
111 strm
.next_in
= alone_buf
;
112 strm
.avail_in
= alone_size
;
114 strm
.avail_out
= sizeof(out
);
116 assert_lzma_ret(lzma_code(&strm
, LZMA_FINISH
), LZMA_MEMLIMIT_ERROR
);
118 assert_uint_eq(lzma_memlimit_get(&strm
), MEMLIMIT_TOO_LOW
);
119 assert_lzma_ret(lzma_memlimit_set(&strm
, MEMLIMIT_TOO_LOW
+ 1),
120 LZMA_MEMLIMIT_ERROR
);
121 assert_lzma_ret(lzma_memlimit_set(&strm
, MEMLIMIT_HIGH_ENOUGH
),
124 assert_lzma_ret(lzma_code(&strm
, LZMA_FINISH
), LZMA_STREAM_END
);
131 test_memlimit_auto_decoder(void)
133 #ifndef HAVE_DECODERS
134 assert_skip("Decoder support disabled");
136 lzma_stream strm
= LZMA_STREAM_INIT
;
137 assert_lzma_ret(lzma_auto_decoder(&strm
, MEMLIMIT_TOO_LOW
, 0),
141 strm
.avail_in
= in_size
;
143 strm
.avail_out
= sizeof(out
);
145 assert_lzma_ret(lzma_code(&strm
, LZMA_FINISH
), LZMA_MEMLIMIT_ERROR
);
147 assert_uint_eq(lzma_memlimit_get(&strm
), MEMLIMIT_TOO_LOW
);
148 assert_lzma_ret(lzma_memlimit_set(&strm
, MEMLIMIT_TOO_LOW
+ 1),
149 LZMA_MEMLIMIT_ERROR
);
150 assert_lzma_ret(lzma_memlimit_set(&strm
, MEMLIMIT_HIGH_ENOUGH
),
153 assert_lzma_ret(lzma_code(&strm
, LZMA_FINISH
), LZMA_STREAM_END
);
160 main(int argc
, char **argv
)
162 tuktest_start(argc
, argv
);
164 in
= tuktest_file_from_srcdir("files/good-1-check-crc32.xz", &in_size
);
166 tuktest_run(test_memlimit_stream_decoder
);
167 tuktest_run(test_memlimit_stream_decoder_mt
);
168 tuktest_run(test_memlimit_alone_decoder
);
169 tuktest_run(test_memlimit_auto_decoder
);
171 return tuktest_end();