1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file test_memlimit.c
4 /// \brief Tests memory usage limit in decoders
6 // Author: Lasse Collin
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
11 ///////////////////////////////////////////////////////////////////////////////
17 #define MEMLIMIT_TOO_LOW 1234U
18 #define MEMLIMIT_HIGH_ENOUGH (2U << 20)
22 static size_t in_size
;
25 static uint8_t out
[8192];
30 test_memlimit_stream_decoder(void)
33 assert_skip("Decoder support disabled");
35 lzma_stream strm
= LZMA_STREAM_INIT
;
36 assert_lzma_ret(lzma_stream_decoder(&strm
, MEMLIMIT_TOO_LOW
, 0),
40 strm
.avail_in
= in_size
;
42 strm
.avail_out
= sizeof(out
);
44 assert_lzma_ret(lzma_code(&strm
, LZMA_FINISH
), LZMA_MEMLIMIT_ERROR
);
46 assert_uint_eq(lzma_memlimit_get(&strm
), MEMLIMIT_TOO_LOW
);
47 assert_lzma_ret(lzma_memlimit_set(&strm
, MEMLIMIT_TOO_LOW
+ 1),
49 assert_lzma_ret(lzma_memlimit_set(&strm
, MEMLIMIT_HIGH_ENOUGH
),
52 // This fails before commit 660739f99ab211edec4071de98889fb32ed04e98
53 // (liblzma <= 5.2.6, liblzma <= 5.3.3alpha). It was fixed in 5.2.7.
54 assert_lzma_ret(lzma_code(&strm
, LZMA_FINISH
), LZMA_STREAM_END
);
62 test_memlimit_stream_decoder_mt(void)
64 #ifndef MYTHREAD_ENABLED
65 assert_skip("Threading support disabled");
66 #elif !defined(HAVE_DECODERS)
67 assert_skip("Decoder support disabled");
69 lzma_stream strm
= LZMA_STREAM_INIT
;
74 .memlimit_threading
= 0,
75 .memlimit_stop
= MEMLIMIT_TOO_LOW
,
78 assert_lzma_ret(lzma_stream_decoder_mt(&strm
, &mt
), LZMA_OK
);
81 strm
.avail_in
= in_size
;
83 strm
.avail_out
= sizeof(out
);
85 assert_lzma_ret(lzma_code(&strm
, LZMA_FINISH
), LZMA_MEMLIMIT_ERROR
);
87 assert_uint_eq(lzma_memlimit_get(&strm
), MEMLIMIT_TOO_LOW
);
88 assert_lzma_ret(lzma_memlimit_set(&strm
, MEMLIMIT_TOO_LOW
+ 1),
90 assert_lzma_ret(lzma_memlimit_set(&strm
, MEMLIMIT_HIGH_ENOUGH
),
93 assert_lzma_ret(lzma_code(&strm
, LZMA_FINISH
), LZMA_STREAM_END
);
100 test_memlimit_alone_decoder(void)
102 #ifndef HAVE_DECODERS
103 assert_skip("Decoder support disabled");
106 uint8_t *alone_buf
= tuktest_file_from_srcdir(
107 "files/good-unknown_size-with_eopm.lzma", &alone_size
);
109 lzma_stream strm
= LZMA_STREAM_INIT
;
110 assert_lzma_ret(lzma_alone_decoder(&strm
, MEMLIMIT_TOO_LOW
), LZMA_OK
);
112 strm
.next_in
= alone_buf
;
113 strm
.avail_in
= alone_size
;
115 strm
.avail_out
= sizeof(out
);
117 assert_lzma_ret(lzma_code(&strm
, LZMA_FINISH
), LZMA_MEMLIMIT_ERROR
);
119 assert_uint_eq(lzma_memlimit_get(&strm
), MEMLIMIT_TOO_LOW
);
120 assert_lzma_ret(lzma_memlimit_set(&strm
, MEMLIMIT_TOO_LOW
+ 1),
121 LZMA_MEMLIMIT_ERROR
);
122 assert_lzma_ret(lzma_memlimit_set(&strm
, MEMLIMIT_HIGH_ENOUGH
),
125 assert_lzma_ret(lzma_code(&strm
, LZMA_FINISH
), LZMA_STREAM_END
);
132 test_memlimit_auto_decoder(void)
134 #ifndef HAVE_DECODERS
135 assert_skip("Decoder support disabled");
137 lzma_stream strm
= LZMA_STREAM_INIT
;
138 assert_lzma_ret(lzma_auto_decoder(&strm
, MEMLIMIT_TOO_LOW
, 0),
142 strm
.avail_in
= in_size
;
144 strm
.avail_out
= sizeof(out
);
146 assert_lzma_ret(lzma_code(&strm
, LZMA_FINISH
), LZMA_MEMLIMIT_ERROR
);
148 assert_uint_eq(lzma_memlimit_get(&strm
), MEMLIMIT_TOO_LOW
);
149 assert_lzma_ret(lzma_memlimit_set(&strm
, MEMLIMIT_TOO_LOW
+ 1),
150 LZMA_MEMLIMIT_ERROR
);
151 assert_lzma_ret(lzma_memlimit_set(&strm
, MEMLIMIT_HIGH_ENOUGH
),
154 assert_lzma_ret(lzma_code(&strm
, LZMA_FINISH
), LZMA_STREAM_END
);
161 main(int argc
, char **argv
)
163 tuktest_start(argc
, argv
);
165 in
= tuktest_file_from_srcdir("files/good-1-check-crc32.xz", &in_size
);
167 tuktest_run(test_memlimit_stream_decoder
);
168 tuktest_run(test_memlimit_stream_decoder_mt
);
169 tuktest_run(test_memlimit_alone_decoder
);
170 tuktest_run(test_memlimit_auto_decoder
);
172 return tuktest_end();