ci: add po4a
[xz/debian.git] / tests / test_memlimit.c
blob7e8f8620874d152fb25e669a74e6e9c19f5052da
1 // SPDX-License-Identifier: 0BSD
3 ///////////////////////////////////////////////////////////////////////////////
4 //
5 /// \file test_memlimit.c
6 /// \brief Tests memory usage limit in decoders
7 //
8 // Author: Lasse Collin
9 //
10 ///////////////////////////////////////////////////////////////////////////////
12 #include "tests.h"
13 #include "mythread.h"
16 #define MEMLIMIT_TOO_LOW 1234U
17 #define MEMLIMIT_HIGH_ENOUGH (2U << 20)
20 static uint8_t *in;
21 static size_t in_size;
23 #ifdef HAVE_DECODERS
24 static uint8_t out[8192];
25 #endif
28 static void
29 test_memlimit_stream_decoder(void)
31 #ifndef HAVE_DECODERS
32 assert_skip("Decoder support disabled");
33 #else
34 lzma_stream strm = LZMA_STREAM_INIT;
35 assert_lzma_ret(lzma_stream_decoder(&strm, MEMLIMIT_TOO_LOW, 0),
36 LZMA_OK);
38 strm.next_in = in;
39 strm.avail_in = in_size;
40 strm.next_out = out;
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),
47 LZMA_MEMLIMIT_ERROR);
48 assert_lzma_ret(lzma_memlimit_set(&strm, MEMLIMIT_HIGH_ENOUGH),
49 LZMA_OK);
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);
55 lzma_end(&strm);
56 #endif
60 static void
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");
67 #else
68 lzma_stream strm = LZMA_STREAM_INIT;
69 lzma_mt mt = {
70 .flags = 0,
71 .threads = 1,
72 .timeout = 0,
73 .memlimit_threading = 0,
74 .memlimit_stop = MEMLIMIT_TOO_LOW,
77 assert_lzma_ret(lzma_stream_decoder_mt(&strm, &mt), LZMA_OK);
79 strm.next_in = in;
80 strm.avail_in = in_size;
81 strm.next_out = out;
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),
88 LZMA_MEMLIMIT_ERROR);
89 assert_lzma_ret(lzma_memlimit_set(&strm, MEMLIMIT_HIGH_ENOUGH),
90 LZMA_OK);
92 assert_lzma_ret(lzma_code(&strm, LZMA_FINISH), LZMA_STREAM_END);
93 lzma_end(&strm);
94 #endif
98 static void
99 test_memlimit_alone_decoder(void)
101 #ifndef HAVE_DECODERS
102 assert_skip("Decoder support disabled");
103 #else
104 size_t alone_size;
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;
113 strm.next_out = out;
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),
122 LZMA_OK);
124 assert_lzma_ret(lzma_code(&strm, LZMA_FINISH), LZMA_STREAM_END);
125 lzma_end(&strm);
126 #endif
130 static void
131 test_memlimit_auto_decoder(void)
133 #ifndef HAVE_DECODERS
134 assert_skip("Decoder support disabled");
135 #else
136 lzma_stream strm = LZMA_STREAM_INIT;
137 assert_lzma_ret(lzma_auto_decoder(&strm, MEMLIMIT_TOO_LOW, 0),
138 LZMA_OK);
140 strm.next_in = in;
141 strm.avail_in = in_size;
142 strm.next_out = out;
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),
151 LZMA_OK);
153 assert_lzma_ret(lzma_code(&strm, LZMA_FINISH), LZMA_STREAM_END);
154 lzma_end(&strm);
155 #endif
159 extern int
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();