1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file test_filter_str.c
4 /// \brief Tests Filter string functions
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
11 ///////////////////////////////////////////////////////////////////////////////
17 test_lzma_str_to_filters(void)
19 lzma_filter filters
[LZMA_FILTERS_MAX
+ 1];
22 // Test with NULL string.
23 assert_true(lzma_str_to_filters(NULL
, &error_pos
, filters
, 0,
26 // Test with NULL filter array.
27 assert_true(lzma_str_to_filters("lzma2", &error_pos
, NULL
, 0,
30 // Test with unsupported flags.
31 assert_true(lzma_str_to_filters("lzma2", &error_pos
, filters
,
32 UINT32_MAX
, NULL
) != NULL
);
34 assert_true(lzma_str_to_filters("lzma2", &error_pos
, filters
,
35 LZMA_STR_NO_SPACES
<< 1, NULL
) != NULL
);
37 assert_true(lzma_str_to_filters("lzma2", &error_pos
, filters
,
38 LZMA_STR_NO_SPACES
, NULL
) != NULL
);
40 // Test with empty string.
41 assert_true(lzma_str_to_filters("", &error_pos
,
42 filters
, 0, NULL
) != NULL
);
43 assert_int_eq(error_pos
, 0);
45 // Test with invalid filter name and missing filter name.
46 assert_true(lzma_str_to_filters("lzma2 abcd", &error_pos
,
47 filters
, 0, NULL
) != NULL
);
48 assert_int_eq(error_pos
, 6);
50 assert_true(lzma_str_to_filters("lzma2--abcd", &error_pos
,
51 filters
, 0, NULL
) != NULL
);
52 assert_int_eq(error_pos
, 7);
54 assert_true(lzma_str_to_filters("lzma2--", &error_pos
,
55 filters
, 0, NULL
) != NULL
);
56 assert_int_eq(error_pos
, 7);
58 // Test LZMA_STR_ALL_FILTERS flag (should work with LZMA1 if built).
59 #if defined(HAVE_ENCODER_LZMA1) || defined(HAVE_DECODER_LZMA1)
60 // Using LZMA1 as a Filter should fail without LZMA_STR_ALL_FILTERS.
61 assert_true(lzma_str_to_filters("lzma1", &error_pos
, filters
,
63 assert_int_eq(error_pos
, 0);
65 assert_true(lzma_str_to_filters("lzma1", &error_pos
, filters
,
66 LZMA_STR_ALL_FILTERS
, NULL
) == NULL
);
68 // Verify Filters array IDs are correct. The array should contain
71 // 2. LZMA_VLI_UNKNOWN filter array terminator
72 assert_uint_eq(filters
[0].id
, LZMA_FILTER_LZMA1
);
73 assert_uint_eq(filters
[1].id
, LZMA_VLI_UNKNOWN
);
75 lzma_filters_free(filters
, NULL
);
78 // Test LZMA_STR_NO_VALIDATION flag. This should allow having the
79 // same Filter multiple times in the chain and having a non-last
80 // Filter like lzma2 appear before another Filter.
81 // Without the flag, "lzma2 lzma2" must fail.
82 assert_true(lzma_str_to_filters("lzma2 lzma2", &error_pos
, filters
,
85 assert_true(lzma_str_to_filters("lzma2 lzma2", &error_pos
, filters
,
86 LZMA_STR_NO_VALIDATION
, NULL
) == NULL
);
88 assert_uint_eq(filters
[0].id
, LZMA_FILTER_LZMA2
);
89 assert_uint_eq(filters
[1].id
, LZMA_FILTER_LZMA2
);
90 assert_uint_eq(filters
[2].id
, LZMA_VLI_UNKNOWN
);
92 lzma_filters_free(filters
, NULL
);
94 // Should fail with invalid Filter options (lc + lp must be <= 4).
95 assert_true(lzma_str_to_filters("lzma2:lc=3,lp=3", &error_pos
, filters
,
96 LZMA_STR_NO_VALIDATION
, NULL
) != NULL
);
98 // Test invalid option name.
99 assert_true(lzma_str_to_filters("lzma2:foo=1,bar=2", &error_pos
,
100 filters
, 0, NULL
) != NULL
);
101 assert_int_eq(error_pos
, 6);
103 // Test missing option value.
104 assert_true(lzma_str_to_filters("lzma2:lc=", &error_pos
,
105 filters
, 0, NULL
) != NULL
);
106 assert_int_eq(error_pos
, 9);
108 assert_true(lzma_str_to_filters("lzma2:=,pb=1", &error_pos
,
109 filters
, 0, NULL
) != NULL
);
110 assert_int_eq(error_pos
, 6);
112 // Test unsupported preset value.
113 assert_true(lzma_str_to_filters("-10", &error_pos
,
114 filters
, 0, NULL
) != NULL
);
115 assert_int_eq(error_pos
, 2);
117 assert_true(lzma_str_to_filters("-5f", &error_pos
,
118 filters
, 0, NULL
) != NULL
);
119 assert_int_eq(error_pos
, 2);
121 // Test filter chain too long.
122 assert_true(lzma_str_to_filters("lzma2 lzma2 lzma2 lzma2 lzma2",
123 &error_pos
, filters
, LZMA_STR_NO_VALIDATION
,
125 assert_int_eq(error_pos
, 24);
127 #if defined(HAVE_ENCODER_LZMA1) || defined(HAVE_DECODER_LZMA1)
128 // Should fail with a Filter not supported in the .xz format (lzma1).
129 assert_true(lzma_str_to_filters("lzma1", &error_pos
, filters
,
130 LZMA_STR_NO_VALIDATION
, NULL
) != NULL
);
133 // Test setting options with the "=" format.
134 assert_true(lzma_str_to_filters("lzma2=dict=4096,lc=2,lp=2,pb=1,"
135 "mode=fast,nice=3,mf=hc3,depth=10", &error_pos
,
136 filters
, 0, NULL
) == NULL
);
137 assert_uint_eq(filters
[0].id
, LZMA_FILTER_LZMA2
);
138 assert_uint_eq(filters
[1].id
, LZMA_VLI_UNKNOWN
);
140 lzma_options_lzma
*opts
= filters
[0].options
;
141 assert_uint_eq(opts
->dict_size
, 4096);
142 assert_uint_eq(opts
->lc
, 2);
143 assert_uint_eq(opts
->lp
, 2);
144 assert_uint_eq(opts
->pb
, 1);
145 assert_uint_eq(opts
->mode
, LZMA_MODE_FAST
);
146 assert_uint_eq(opts
->nice_len
, 3);
147 assert_uint_eq(opts
->mf
, LZMA_MF_HC3
);
148 assert_uint_eq(opts
->depth
, 10);
150 lzma_filters_free(filters
, NULL
);
152 #if defined(HAVE_ENCODER_X86) || defined(HAVE_DECODER_X86)
153 // Test BCJ Filter options.
154 assert_true(lzma_str_to_filters("x86:start=16", &error_pos
, filters
,
155 LZMA_STR_NO_VALIDATION
, NULL
) == NULL
);
157 assert_uint_eq(filters
[0].id
, LZMA_FILTER_X86
);
158 assert_uint_eq(filters
[1].id
, LZMA_VLI_UNKNOWN
);
160 lzma_options_bcj
*bcj_opts
= filters
[0].options
;
161 assert_uint_eq(bcj_opts
->start_offset
, 16);
163 lzma_filters_free(filters
, NULL
);
166 #if defined(HAVE_ENCODER_DELTA) || defined(HAVE_DECODER_DELTA)
167 // Test Delta Filter options.
168 assert_true(lzma_str_to_filters("delta:dist=20", &error_pos
, filters
,
169 LZMA_STR_NO_VALIDATION
, NULL
) == NULL
);
171 assert_uint_eq(filters
[0].id
, LZMA_FILTER_DELTA
);
172 assert_uint_eq(filters
[1].id
, LZMA_VLI_UNKNOWN
);
174 lzma_options_delta
*delta_opts
= filters
[0].options
;
175 assert_uint_eq(delta_opts
->dist
, 20);
177 lzma_filters_free(filters
, NULL
);
180 // Test skipping leading spaces.
181 assert_true(lzma_str_to_filters(" lzma2", &error_pos
, filters
,
184 assert_uint_eq(filters
[0].id
, LZMA_FILTER_LZMA2
);
185 assert_uint_eq(filters
[1].id
, LZMA_VLI_UNKNOWN
);
187 lzma_filters_free(filters
, NULL
);
189 // Test skipping trailing spaces.
190 assert_true(lzma_str_to_filters("lzma2 ", &error_pos
, filters
,
193 assert_uint_eq(filters
[0].id
, LZMA_FILTER_LZMA2
);
194 assert_uint_eq(filters
[1].id
, LZMA_VLI_UNKNOWN
);
196 lzma_filters_free(filters
, NULL
);
198 // Test with "--" instead of space separating.
199 assert_true(lzma_str_to_filters("lzma2--lzma2", &error_pos
, filters
,
200 LZMA_STR_NO_VALIDATION
, NULL
) == NULL
);
202 assert_uint_eq(filters
[0].id
, LZMA_FILTER_LZMA2
);
203 assert_uint_eq(filters
[1].id
, LZMA_FILTER_LZMA2
);
204 assert_uint_eq(filters
[2].id
, LZMA_VLI_UNKNOWN
);
206 lzma_filters_free(filters
, NULL
);
208 // Test preset with and without leading "-", and with "e".
209 assert_true(lzma_str_to_filters("-3", &error_pos
, filters
,
212 assert_uint_eq(filters
[0].id
, LZMA_FILTER_LZMA2
);
213 assert_uint_eq(filters
[1].id
, LZMA_VLI_UNKNOWN
);
215 lzma_filters_free(filters
, NULL
);
217 assert_true(lzma_str_to_filters("4", &error_pos
, filters
,
220 assert_uint_eq(filters
[0].id
, LZMA_FILTER_LZMA2
);
221 assert_uint_eq(filters
[1].id
, LZMA_VLI_UNKNOWN
);
223 lzma_filters_free(filters
, NULL
);
225 assert_true(lzma_str_to_filters("9e", &error_pos
, filters
,
228 assert_uint_eq(filters
[0].id
, LZMA_FILTER_LZMA2
);
229 assert_uint_eq(filters
[1].id
, LZMA_VLI_UNKNOWN
);
231 lzma_filters_free(filters
, NULL
);
233 // Test using a preset as an lzma2 option.
234 assert_true(lzma_str_to_filters("lzma2:preset=9e", &error_pos
, filters
,
237 assert_uint_eq(filters
[0].id
, LZMA_FILTER_LZMA2
);
238 assert_uint_eq(filters
[1].id
, LZMA_VLI_UNKNOWN
);
240 lzma_filters_free(filters
, NULL
);
242 // Test setting dictionary size with invalid modifier suffix.
243 assert_true(lzma_str_to_filters("lzma2:dict=4096ZiB", &error_pos
, filters
,
246 assert_true(lzma_str_to_filters("lzma2:dict=4096KiBs", &error_pos
, filters
,
249 // Test option that cannot have multiplier modifier.
250 assert_true(lzma_str_to_filters("lzma2:pb=1k", &error_pos
, filters
,
253 // Test option value too large.
254 assert_true(lzma_str_to_filters("lzma2:dict=4096GiB", &error_pos
, filters
,
257 // Test valid uses of multiplier modifiers (k,m,g).
258 assert_true(lzma_str_to_filters("lzma2:dict=4096KiB", &error_pos
, filters
,
261 assert_uint_eq(filters
[0].id
, LZMA_FILTER_LZMA2
);
262 assert_uint_eq(filters
[1].id
, LZMA_VLI_UNKNOWN
);
264 opts
= filters
[0].options
;
265 assert_uint_eq(opts
->dict_size
, 4096 << 10);
267 lzma_filters_free(filters
, NULL
);
269 assert_true(lzma_str_to_filters("lzma2:dict=40Mi", &error_pos
, filters
,
272 assert_uint_eq(filters
[0].id
, LZMA_FILTER_LZMA2
);
273 assert_uint_eq(filters
[1].id
, LZMA_VLI_UNKNOWN
);
275 opts
= filters
[0].options
;
276 assert_uint_eq(opts
->dict_size
, 40 << 20);
278 lzma_filters_free(filters
, NULL
);
280 assert_true(lzma_str_to_filters("lzma2:dict=1g", &error_pos
, filters
,
283 assert_uint_eq(filters
[0].id
, LZMA_FILTER_LZMA2
);
284 assert_uint_eq(filters
[1].id
, LZMA_VLI_UNKNOWN
);
286 opts
= filters
[0].options
;
287 assert_uint_eq(opts
->dict_size
, 1 << 30);
289 lzma_filters_free(filters
, NULL
);
294 test_lzma_str_from_filters(void)
296 lzma_filter filters
[LZMA_FILTERS_MAX
];
297 filters
[0].id
= LZMA_VLI_UNKNOWN
;
299 char *output_str
= NULL
;
301 // Test basic NULL inputs.
302 assert_lzma_ret(lzma_str_from_filters(NULL
, filters
, 0, NULL
),
305 assert_lzma_ret(lzma_str_from_filters(&output_str
, NULL
, 0, NULL
),
308 // Test with empty filters array.
309 assert_lzma_ret(lzma_str_from_filters(&output_str
, filters
, 0, NULL
),
312 // Create a simple filter array only containing an LZMA2 Filter.
313 assert_true(lzma_str_to_filters("lzma2", NULL
, filters
, 0, NULL
)
316 // Test with bad flags.
317 assert_lzma_ret(lzma_str_from_filters(&output_str
, filters
,
318 LZMA_STR_ALL_FILTERS
, NULL
), LZMA_OPTIONS_ERROR
);
320 assert_lzma_ret(lzma_str_from_filters(&output_str
, filters
,
321 LZMA_STR_NO_VALIDATION
, NULL
), LZMA_OPTIONS_ERROR
);
323 // Test with no flags.
324 assert_lzma_ret(lzma_str_from_filters(&output_str
, filters
, 0, NULL
),
327 assert_str_eq(output_str
, "lzma2");
330 // Test LZMA_STR_ENCODER flag.
331 // Only the the return value is checked since the actual string
332 // may change in the future (even though it is unlikely).
333 // The order of options or the inclusion of new options could
334 // cause a change in output, so we will avoid hardcoding an
336 assert_lzma_ret(lzma_str_from_filters(&output_str
, filters
,
337 LZMA_STR_ENCODER
, NULL
), LZMA_OK
);
340 // Test LZMA_STR_DECODER flag.
341 assert_lzma_ret(lzma_str_from_filters(&output_str
, filters
,
342 LZMA_STR_DECODER
, NULL
), LZMA_OK
);
345 // Test LZMA_STR_GETOPT_LONG flag.
346 assert_lzma_ret(lzma_str_from_filters(&output_str
, filters
,
347 LZMA_STR_GETOPT_LONG
, NULL
), LZMA_OK
);
350 // Test LZMA_STR_NO_SPACES flag.
351 assert_lzma_ret(lzma_str_from_filters(&output_str
, filters
,
352 LZMA_STR_NO_SPACES
, NULL
), LZMA_OK
);
354 // Check to be sure there are no spaces.
355 assert_true(strchr(output_str
, ' ') == NULL
);
359 lzma_filters_free(filters
, NULL
);
361 #if defined(HAVE_ENCODER_X86) || defined(HAVE_DECODER_X86)
362 assert_true(lzma_str_to_filters("x86 lzma2", NULL
, filters
, 0, NULL
)
365 assert_lzma_ret(lzma_str_from_filters(&output_str
, filters
, 0, NULL
),
368 assert_str_eq(output_str
, "x86 lzma2");
372 // Test setting BCJ option to NULL.
373 assert_false(filters
[0].options
== NULL
);
374 free(filters
[0].options
);
376 filters
[0].options
= NULL
;
378 assert_lzma_ret(lzma_str_from_filters(&output_str
, filters
, 0, NULL
),
381 assert_str_eq(output_str
, "x86 lzma2");
383 lzma_filters_free(filters
, NULL
);
387 lzma_options_lzma opts
;
388 assert_false(lzma_lzma_preset(&opts
, LZMA_PRESET_DEFAULT
));
389 // Test with too many Filters (array terminated after 4+ filters).
390 lzma_filter oversized_filters
[LZMA_FILTERS_MAX
+ 2];
392 for (uint32_t i
= 0; i
< ARRAY_SIZE(oversized_filters
) - 1; i
++) {
393 oversized_filters
[i
].id
= LZMA_FILTER_LZMA2
;
394 oversized_filters
[i
].options
= &opts
;
397 oversized_filters
[LZMA_FILTERS_MAX
+ 1].id
= LZMA_VLI_UNKNOWN
;
398 oversized_filters
[LZMA_FILTERS_MAX
+ 1].options
= NULL
;
400 assert_lzma_ret(lzma_str_from_filters(&output_str
, oversized_filters
,
401 0, NULL
), LZMA_OPTIONS_ERROR
);
403 // Test with NULL filter options (when they cannot be NULL).
404 filters
[0].id
= LZMA_FILTER_LZMA2
;
405 filters
[0].options
= NULL
;
406 filters
[1].id
= LZMA_VLI_UNKNOWN
;
408 assert_lzma_ret(lzma_str_from_filters(&output_str
, filters
,
409 LZMA_STR_ENCODER
, NULL
), LZMA_OPTIONS_ERROR
);
411 // Test with bad Filter ID.
412 filters
[0].id
= LZMA_VLI_UNKNOWN
- 1;
413 assert_lzma_ret(lzma_str_from_filters(&output_str
, filters
,
414 LZMA_STR_ENCODER
, NULL
), LZMA_OPTIONS_ERROR
);
418 static const char supported_encoders
[][9] = {
420 #ifdef HAVE_ENCODER_X86
423 #ifdef HAVE_ENCODER_POWERPC
426 #ifdef HAVE_ENCODER_IA64
429 #ifdef HAVE_ENCODER_ARM
432 #ifdef HAVE_ENCODER_ARMTHUMB
435 #ifdef HAVE_ENCODER_SPARC
438 #ifdef HAVE_ENCODER_ARM64
441 #ifdef HAVE_ENCODER_DELTA
446 static const char supported_decoders
[][9] = {
448 #ifdef HAVE_DECODER_X86
451 #ifdef HAVE_DECODER_POWERPC
454 #ifdef HAVE_DECODER_IA64
457 #ifdef HAVE_DECODER_ARM
460 #ifdef HAVE_DECODER_ARMTHUMB
463 #ifdef HAVE_DECODER_SPARC
466 #ifdef HAVE_DECODER_ARM64
469 #ifdef HAVE_DECODER_DELTA
474 static const char supported_filters
[][9] = {
476 #if defined(HAVE_ENCODER_X86) || defined(HAVE_DECODER_X86)
479 #if defined(HAVE_ENCODER_POWERPC) || defined(HAVE_DECODER_POWERPC)
482 #if defined(HAVE_ENCODER_IA64) || defined(HAVE_DECODER_IA64)
485 #if defined(HAVE_ENCODER_ARM) || defined(HAVE_DECODER_ARM)
488 #if defined(HAVE_ENCODER_ARMTHUMB) || defined(HAVE_DECODER_ARMTHUMB)
491 #if defined(HAVE_ENCODER_SPARC) || defined(HAVE_DECODER_SPARC)
494 #if defined(HAVE_ENCODER_ARM64) || defined(HAVE_DECODER_ARM64)
497 #if defined(HAVE_ENCODER_DELTA) || defined(HAVE_DECODER_DELTA)
504 test_lzma_str_list_filters(void)
506 // Test with basic NULL inputs.
507 assert_lzma_ret(lzma_str_list_filters(NULL
, LZMA_VLI_UNKNOWN
, 0,
508 NULL
), LZMA_PROG_ERROR
);
512 // Test with bad flags.
513 assert_lzma_ret(lzma_str_list_filters(&str
, LZMA_VLI_UNKNOWN
,
514 LZMA_STR_NO_VALIDATION
, NULL
), LZMA_OPTIONS_ERROR
);
516 assert_lzma_ret(lzma_str_list_filters(&str
, LZMA_VLI_UNKNOWN
,
517 LZMA_STR_NO_SPACES
, NULL
), LZMA_OPTIONS_ERROR
);
519 // Test with bad Filter ID.
520 assert_lzma_ret(lzma_str_list_filters(&str
, LZMA_VLI_UNKNOWN
- 1,
521 0, NULL
), LZMA_OPTIONS_ERROR
);
523 // Test LZMA_STR_ENCODER flag.
524 assert_lzma_ret(lzma_str_list_filters(&str
, LZMA_VLI_UNKNOWN
,
525 LZMA_STR_ENCODER
, NULL
), LZMA_OK
);
527 for (uint32_t i
= 0; i
< ARRAY_SIZE(supported_encoders
); i
++)
528 assert_str_contains(str
, supported_encoders
[i
]);
532 // Test LZMA_STR_DECODER flag.
533 assert_lzma_ret(lzma_str_list_filters(&str
, LZMA_VLI_UNKNOWN
,
534 LZMA_STR_DECODER
, NULL
), LZMA_OK
);
536 for (uint32_t i
= 0; i
< ARRAY_SIZE(supported_decoders
); i
++)
537 assert_str_contains(str
, supported_decoders
[i
]);
541 // Test LZMA_STR_GETOPT_LONG flag.
542 assert_lzma_ret(lzma_str_list_filters(&str
, LZMA_VLI_UNKNOWN
,
543 LZMA_STR_GETOPT_LONG
, NULL
), LZMA_OK
);
547 // Test LZMA_STR_ALL_FILTERS flag.
548 assert_lzma_ret(lzma_str_list_filters(&str
, LZMA_VLI_UNKNOWN
,
549 LZMA_STR_ALL_FILTERS
, NULL
), LZMA_OK
);
550 #if defined(HAVE_ENCODER_LZMA1) || defined(HAVE_DECODER_LZMA1)
551 // With the flag, the string should contain the LZMA1 Filter.
552 assert_str_contains(str
, "lzma1");
556 // If a non .xz filter is specified, it should still list the Filter.
557 assert_lzma_ret(lzma_str_list_filters(&str
, LZMA_FILTER_LZMA1
,
559 assert_str_eq(str
, "lzma1");
563 // Test with no flags.
564 assert_lzma_ret(lzma_str_list_filters(&str
, LZMA_VLI_UNKNOWN
,
567 for (uint32_t i
= 0; i
< ARRAY_SIZE(supported_filters
); i
++)
568 assert_str_contains(str
, supported_filters
[i
]);
570 assert_str_doesnt_contain(str
, "lzma1");
574 // Test providing a Filter ID.
575 assert_lzma_ret(lzma_str_list_filters(&str
, LZMA_FILTER_LZMA2
,
576 LZMA_STR_ALL_FILTERS
, NULL
), LZMA_OK
);
577 assert_str_eq(str
, "lzma2");
584 main(int argc
, char **argv
)
586 tuktest_start(argc
, argv
);
588 tuktest_run(test_lzma_str_to_filters
);
589 tuktest_run(test_lzma_str_from_filters
);
590 tuktest_run(test_lzma_str_list_filters
);
592 return tuktest_end();