liblzma: Prevent warning for MSYS2 Windows build.
[xz/debian.git] / tests / test_memlimit.c
blobc45a44b5769ec95faf3dc1650851cbafa4beb3b2
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file test_memlimit.c
4 /// \brief Tests memory usage limit in decoders
5 //
6 // Author: Lasse Collin
7 //
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
11 ///////////////////////////////////////////////////////////////////////////////
13 #include "tests.h"
14 #include "mythread.h"
17 #define MEMLIMIT_TOO_LOW 1234U
18 #define MEMLIMIT_HIGH_ENOUGH (2U << 20)
21 static uint8_t *in;
22 static size_t in_size;
24 #ifdef HAVE_DECODERS
25 static uint8_t out[8192];
26 #endif
29 static void
30 test_memlimit_stream_decoder(void)
32 #ifndef HAVE_DECODERS
33 assert_skip("Decoder support disabled");
34 #else
35 lzma_stream strm = LZMA_STREAM_INIT;
36 assert_lzma_ret(lzma_stream_decoder(&strm, MEMLIMIT_TOO_LOW, 0),
37 LZMA_OK);
39 strm.next_in = in;
40 strm.avail_in = in_size;
41 strm.next_out = out;
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),
48 LZMA_MEMLIMIT_ERROR);
49 assert_lzma_ret(lzma_memlimit_set(&strm, MEMLIMIT_HIGH_ENOUGH),
50 LZMA_OK);
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);
56 lzma_end(&strm);
57 #endif
61 static void
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");
68 #else
69 lzma_stream strm = LZMA_STREAM_INIT;
70 lzma_mt mt = {
71 .flags = 0,
72 .threads = 1,
73 .timeout = 0,
74 .memlimit_threading = 0,
75 .memlimit_stop = MEMLIMIT_TOO_LOW,
78 assert_lzma_ret(lzma_stream_decoder_mt(&strm, &mt), LZMA_OK);
80 strm.next_in = in;
81 strm.avail_in = in_size;
82 strm.next_out = out;
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),
89 LZMA_MEMLIMIT_ERROR);
90 assert_lzma_ret(lzma_memlimit_set(&strm, MEMLIMIT_HIGH_ENOUGH),
91 LZMA_OK);
93 assert_lzma_ret(lzma_code(&strm, LZMA_FINISH), LZMA_STREAM_END);
94 lzma_end(&strm);
95 #endif
99 static void
100 test_memlimit_alone_decoder(void)
102 #ifndef HAVE_DECODERS
103 assert_skip("Decoder support disabled");
104 #else
105 size_t alone_size;
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;
114 strm.next_out = out;
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),
123 LZMA_OK);
125 assert_lzma_ret(lzma_code(&strm, LZMA_FINISH), LZMA_STREAM_END);
126 lzma_end(&strm);
127 #endif
131 static void
132 test_memlimit_auto_decoder(void)
134 #ifndef HAVE_DECODERS
135 assert_skip("Decoder support disabled");
136 #else
137 lzma_stream strm = LZMA_STREAM_INIT;
138 assert_lzma_ret(lzma_auto_decoder(&strm, MEMLIMIT_TOO_LOW, 0),
139 LZMA_OK);
141 strm.next_in = in;
142 strm.avail_in = in_size;
143 strm.next_out = out;
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),
152 LZMA_OK);
154 assert_lzma_ret(lzma_code(&strm, LZMA_FINISH), LZMA_STREAM_END);
155 lzma_end(&strm);
156 #endif
160 extern int
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();