(root)/
xz-5.4.5/
tests/
test_memlimit.c
       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.
      10  //
      11  ///////////////////////////////////////////////////////////////////////////////
      12  
      13  #include "tests.h"
      14  #include "mythread.h"
      15  
      16  
      17  #define MEMLIMIT_TOO_LOW 1234U
      18  #define MEMLIMIT_HIGH_ENOUGH (2U << 20)
      19  
      20  
      21  static uint8_t *in;
      22  static size_t in_size;
      23  
      24  #ifdef HAVE_DECODERS
      25  static uint8_t out[8192];
      26  #endif
      27  
      28  
      29  static void
      30  test_memlimit_stream_decoder(void)
      31  {
      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);
      38  
      39  	strm.next_in = in;
      40  	strm.avail_in = in_size;
      41  	strm.next_out = out;
      42  	strm.avail_out = sizeof(out);
      43  
      44  	assert_lzma_ret(lzma_code(&strm, LZMA_FINISH), LZMA_MEMLIMIT_ERROR);
      45  
      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);
      51  
      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);
      55  
      56  	lzma_end(&strm);
      57  #endif
      58  }
      59  
      60  
      61  static void
      62  test_memlimit_stream_decoder_mt(void)
      63  {
      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,
      76  	};
      77  
      78  	assert_lzma_ret(lzma_stream_decoder_mt(&strm, &mt), LZMA_OK);
      79  
      80  	strm.next_in = in;
      81  	strm.avail_in = in_size;
      82  	strm.next_out = out;
      83  	strm.avail_out = sizeof(out);
      84  
      85  	assert_lzma_ret(lzma_code(&strm, LZMA_FINISH), LZMA_MEMLIMIT_ERROR);
      86  
      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);
      92  
      93  	assert_lzma_ret(lzma_code(&strm, LZMA_FINISH), LZMA_STREAM_END);
      94  	lzma_end(&strm);
      95  #endif
      96  }
      97  
      98  
      99  static void
     100  test_memlimit_alone_decoder(void)
     101  {
     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);
     108  
     109  	lzma_stream strm = LZMA_STREAM_INIT;
     110  	assert_lzma_ret(lzma_alone_decoder(&strm, MEMLIMIT_TOO_LOW), LZMA_OK);
     111  
     112  	strm.next_in = alone_buf;
     113  	strm.avail_in = alone_size;
     114  	strm.next_out = out;
     115  	strm.avail_out = sizeof(out);
     116  
     117  	assert_lzma_ret(lzma_code(&strm, LZMA_FINISH), LZMA_MEMLIMIT_ERROR);
     118  
     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);
     124  
     125  	assert_lzma_ret(lzma_code(&strm, LZMA_FINISH), LZMA_STREAM_END);
     126  	lzma_end(&strm);
     127  #endif
     128  }
     129  
     130  
     131  static void
     132  test_memlimit_auto_decoder(void)
     133  {
     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);
     140  
     141  	strm.next_in = in;
     142  	strm.avail_in = in_size;
     143  	strm.next_out = out;
     144  	strm.avail_out = sizeof(out);
     145  
     146  	assert_lzma_ret(lzma_code(&strm, LZMA_FINISH), LZMA_MEMLIMIT_ERROR);
     147  
     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);
     153  
     154  	assert_lzma_ret(lzma_code(&strm, LZMA_FINISH), LZMA_STREAM_END);
     155  	lzma_end(&strm);
     156  #endif
     157  }
     158  
     159  
     160  extern int
     161  main(int argc, char **argv)
     162  {
     163  	tuktest_start(argc, argv);
     164  
     165  	in = tuktest_file_from_srcdir("files/good-1-check-crc32.xz", &in_size);
     166  
     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);
     171  
     172  	return tuktest_end();
     173  }