Doc: Rename Doxygen HTML doc directory name liblzma => api.
[xz/debian.git] / tests / test_filter_str.c
blob15aee55ab27667cf8b8973fe619a713a8a251089
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file test_filter_str.c
4 /// \brief Tests Filter string functions
5 //
6 // Author: Jia Tan
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"
16 static void
17 test_lzma_str_to_filters(void)
19 lzma_filter filters[LZMA_FILTERS_MAX + 1];
20 int error_pos;
22 // Test with NULL string.
23 assert_true(lzma_str_to_filters(NULL, &error_pos, filters, 0,
24 NULL) != NULL);
26 // Test with NULL filter array.
27 assert_true(lzma_str_to_filters("lzma2", &error_pos, NULL, 0,
28 NULL) != NULL);
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,
62 0, NULL) != NULL);
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
69 // only two elements:
70 // 1. LZMA1 Filter
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);
76 #endif
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,
83 0, NULL) != NULL);
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,
124 NULL) != NULL);
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);
131 #endif
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);
164 #endif
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);
178 #endif
180 // Test skipping leading spaces.
181 assert_true(lzma_str_to_filters(" lzma2", &error_pos, filters,
182 0, NULL) == NULL);
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,
191 0, NULL) == NULL);
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,
210 0, NULL) == NULL);
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,
218 0, NULL) == NULL);
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,
226 0, NULL) == NULL);
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,
235 0, NULL) == NULL);
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,
244 0, NULL) != NULL);
246 assert_true(lzma_str_to_filters("lzma2:dict=4096KiBs", &error_pos, filters,
247 0, NULL) != NULL);
249 // Test option that cannot have multiplier modifier.
250 assert_true(lzma_str_to_filters("lzma2:pb=1k", &error_pos, filters,
251 0, NULL) != NULL);
253 // Test option value too large.
254 assert_true(lzma_str_to_filters("lzma2:dict=4096GiB", &error_pos, filters,
255 0, NULL) != NULL);
257 // Test valid uses of multiplier modifiers (k,m,g).
258 assert_true(lzma_str_to_filters("lzma2:dict=4096KiB", &error_pos, filters,
259 0, NULL) == NULL);
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,
270 0, NULL) == NULL);
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,
281 0, NULL) == NULL);
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);
293 static void
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),
303 LZMA_PROG_ERROR);
305 assert_lzma_ret(lzma_str_from_filters(&output_str, NULL, 0, NULL),
306 LZMA_PROG_ERROR);
308 // Test with empty filters array.
309 assert_lzma_ret(lzma_str_from_filters(&output_str, filters, 0, NULL),
310 LZMA_OPTIONS_ERROR);
312 // Create a simple filter array only containing an LZMA2 Filter.
313 assert_true(lzma_str_to_filters("lzma2", NULL, filters, 0, NULL)
314 == 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),
325 LZMA_OK);
327 assert_str_eq(output_str, "lzma2");
328 free(output_str);
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
335 // expected result.
336 assert_lzma_ret(lzma_str_from_filters(&output_str, filters,
337 LZMA_STR_ENCODER, NULL), LZMA_OK);
338 free(output_str);
340 // Test LZMA_STR_DECODER flag.
341 assert_lzma_ret(lzma_str_from_filters(&output_str, filters,
342 LZMA_STR_DECODER, NULL), LZMA_OK);
343 free(output_str);
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);
348 free(output_str);
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);
357 free(output_str);
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)
363 == NULL);
365 assert_lzma_ret(lzma_str_from_filters(&output_str, filters, 0, NULL),
366 LZMA_OK);
368 assert_str_eq(output_str, "x86 lzma2");
370 free(output_str);
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),
379 LZMA_OK);
381 assert_str_eq(output_str, "x86 lzma2");
383 lzma_filters_free(filters, NULL);
384 free(output_str);
385 #endif
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] = {
419 "lzma2",
420 #ifdef HAVE_ENCODER_X86
421 "x86",
422 #endif
423 #ifdef HAVE_ENCODER_POWERPC
424 "powerpc",
425 #endif
426 #ifdef HAVE_ENCODER_IA64
427 "ia64",
428 #endif
429 #ifdef HAVE_ENCODER_ARM
430 "arm",
431 #endif
432 #ifdef HAVE_ENCODER_ARMTHUMB
433 "armthumb",
434 #endif
435 #ifdef HAVE_ENCODER_SPARC
436 "sparc",
437 #endif
438 #ifdef HAVE_ENCODER_ARM64
439 "arm64",
440 #endif
441 #ifdef HAVE_ENCODER_DELTA
442 "delta",
443 #endif
446 static const char supported_decoders[][9] = {
447 "lzma2",
448 #ifdef HAVE_DECODER_X86
449 "x86",
450 #endif
451 #ifdef HAVE_DECODER_POWERPC
452 "powerpc",
453 #endif
454 #ifdef HAVE_DECODER_IA64
455 "ia64",
456 #endif
457 #ifdef HAVE_DECODER_ARM
458 "arm",
459 #endif
460 #ifdef HAVE_DECODER_ARMTHUMB
461 "armthumb",
462 #endif
463 #ifdef HAVE_DECODER_SPARC
464 "sparc",
465 #endif
466 #ifdef HAVE_DECODER_ARM64
467 "arm64",
468 #endif
469 #ifdef HAVE_DECODER_DELTA
470 "delta",
471 #endif
474 static const char supported_filters[][9] = {
475 "lzma2",
476 #if defined(HAVE_ENCODER_X86) || defined(HAVE_DECODER_X86)
477 "x86",
478 #endif
479 #if defined(HAVE_ENCODER_POWERPC) || defined(HAVE_DECODER_POWERPC)
480 "powerpc",
481 #endif
482 #if defined(HAVE_ENCODER_IA64) || defined(HAVE_DECODER_IA64)
483 "ia64",
484 #endif
485 #if defined(HAVE_ENCODER_ARM) || defined(HAVE_DECODER_ARM)
486 "arm",
487 #endif
488 #if defined(HAVE_ENCODER_ARMTHUMB) || defined(HAVE_DECODER_ARMTHUMB)
489 "armthumb",
490 #endif
491 #if defined(HAVE_ENCODER_SPARC) || defined(HAVE_DECODER_SPARC)
492 "sparc",
493 #endif
494 #if defined(HAVE_ENCODER_ARM64) || defined(HAVE_DECODER_ARM64)
495 "arm64",
496 #endif
497 #if defined(HAVE_ENCODER_DELTA) || defined(HAVE_DECODER_DELTA)
498 "delta",
499 #endif
503 static void
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);
510 char *str = NULL;
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]);
530 free(str);
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]);
539 free(str);
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);
545 free(str);
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");
554 free(str);
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,
558 0, NULL), LZMA_OK);
559 assert_str_eq(str, "lzma1");
560 #endif
561 free(str);
563 // Test with no flags.
564 assert_lzma_ret(lzma_str_list_filters(&str, LZMA_VLI_UNKNOWN,
565 0, NULL), LZMA_OK);
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");
572 free(str);
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");
579 free(str);
583 extern int
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();