Skip to content

Commit

Permalink
Tests: test_filter_str: Test *error_pos more thoroughly
Browse files Browse the repository at this point in the history
  • Loading branch information
Larhzu committed Apr 22, 2024
1 parent 70d12dd commit b0366df
Showing 1 changed file with 76 additions and 1 deletion.
77 changes: 76 additions & 1 deletion tests/test_filter_str.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,50 +19,72 @@ test_lzma_str_to_filters(void)
int error_pos;

// Test with NULL string.
error_pos = -1;
assert_true(lzma_str_to_filters(NULL, &error_pos, filters, 0,
NULL) != NULL);
assert_int_eq(error_pos, 0);

// Test with NULL filter array.
error_pos = -1;
assert_true(lzma_str_to_filters("lzma2", &error_pos, NULL, 0,
NULL) != NULL);
assert_int_eq(error_pos, 0);

// Test with unsupported flags.
error_pos = -1;
assert_true(lzma_str_to_filters("lzma2", &error_pos, filters,
UINT32_MAX, NULL) != NULL);
assert_int_eq(error_pos, 0);

error_pos = -1;
assert_true(lzma_str_to_filters("lzma2", &error_pos, filters,
LZMA_STR_NO_SPACES << 1, NULL) != NULL);
assert_int_eq(error_pos, 0);

error_pos = -1;
assert_true(lzma_str_to_filters("lzma2", &error_pos, filters,
LZMA_STR_NO_SPACES, NULL) != NULL);
assert_int_eq(error_pos, 0);

// Test with empty string.
error_pos = -1;
assert_true(lzma_str_to_filters("", &error_pos,
filters, 0, NULL) != NULL);
assert_int_eq(error_pos, 0);

// Test with invalid filter name and missing filter name.
error_pos = -1;
assert_true(lzma_str_to_filters("abcd", &error_pos,
filters, 0, NULL) != NULL);
assert_int_eq(error_pos, 0);

error_pos = -1;
assert_true(lzma_str_to_filters("lzma2 abcd", &error_pos,
filters, 0, NULL) != NULL);
assert_int_eq(error_pos, 6);

error_pos = -1;
assert_true(lzma_str_to_filters("lzma2--abcd", &error_pos,
filters, 0, NULL) != NULL);
assert_int_eq(error_pos, 7);

error_pos = -1;
assert_true(lzma_str_to_filters("lzma2--", &error_pos,
filters, 0, NULL) != NULL);
assert_int_eq(error_pos, 7);

// Test LZMA_STR_ALL_FILTERS flag (should work with LZMA1 if built).
#if defined(HAVE_ENCODER_LZMA1) || defined(HAVE_DECODER_LZMA1)
// Using LZMA1 as a Filter should fail without LZMA_STR_ALL_FILTERS.
error_pos = -1;
assert_true(lzma_str_to_filters("lzma1", &error_pos, filters,
0, NULL) != NULL);
assert_int_eq(error_pos, 0);

error_pos = -1;
assert_true(lzma_str_to_filters("lzma1", &error_pos, filters,
LZMA_STR_ALL_FILTERS, NULL) == NULL);
assert_int_eq(error_pos, 5);

// Verify Filters array IDs are correct. The array should contain
// only two elements:
Expand All @@ -78,11 +100,15 @@ test_lzma_str_to_filters(void)
// same Filter multiple times in the chain and having a non-last
// Filter like lzma2 appear before another Filter.
// Without the flag, "lzma2 lzma2" must fail.
error_pos = -1;
assert_true(lzma_str_to_filters("lzma2 lzma2", &error_pos, filters,
0, NULL) != NULL);
assert_int_eq(error_pos, 11);

error_pos = -1;
assert_true(lzma_str_to_filters("lzma2 lzma2", &error_pos, filters,
LZMA_STR_NO_VALIDATION, NULL) == NULL);
assert_int_eq(error_pos, 11);

assert_uint_eq(filters[0].id, LZMA_FILTER_LZMA2);
assert_uint_eq(filters[1].id, LZMA_FILTER_LZMA2);
Expand All @@ -91,48 +117,65 @@ test_lzma_str_to_filters(void)
lzma_filters_free(filters, NULL);

// Should fail with invalid Filter options (lc + lp must be <= 4).
error_pos = -1;
assert_true(lzma_str_to_filters("lzma2:lc=3,lp=3", &error_pos, filters,
LZMA_STR_NO_VALIDATION, NULL) != NULL);
assert_int_eq(error_pos, 15);

// Test invalid option name.
error_pos = -1;
assert_true(lzma_str_to_filters("lzma2:foo=1,bar=2", &error_pos,
filters, 0, NULL) != NULL);
assert_int_eq(error_pos, 6);

error_pos = -1;
assert_true(lzma_str_to_filters("lzma2:pb=1,bar=2", &error_pos,
filters, 0, NULL) != NULL);
assert_int_eq(error_pos, 11);

// Test missing option value.
error_pos = -1;
assert_true(lzma_str_to_filters("lzma2:lc=", &error_pos,
filters, 0, NULL) != NULL);
assert_int_eq(error_pos, 9);

error_pos = -1;
assert_true(lzma_str_to_filters("lzma2:=,pb=1", &error_pos,
filters, 0, NULL) != NULL);
assert_int_eq(error_pos, 6);

// Test unsupported preset value.
error_pos = -1;
assert_true(lzma_str_to_filters("-10", &error_pos,
filters, 0, NULL) != NULL);
assert_int_eq(error_pos, 2);

error_pos = -1;
assert_true(lzma_str_to_filters("-5f", &error_pos,
filters, 0, NULL) != NULL);
assert_int_eq(error_pos, 2);

// Test filter chain too long.
error_pos = -1;
assert_true(lzma_str_to_filters("lzma2 lzma2 lzma2 lzma2 lzma2",
&error_pos, filters, LZMA_STR_NO_VALIDATION,
NULL) != NULL);
assert_int_eq(error_pos, 24);
assert_int_eq(error_pos, 24); // The fifth is too many.

#if defined(HAVE_ENCODER_LZMA1) || defined(HAVE_DECODER_LZMA1)
// Should fail with a Filter not supported in the .xz format (lzma1).
error_pos = -1;
assert_true(lzma_str_to_filters("lzma1", &error_pos, filters,
LZMA_STR_NO_VALIDATION, NULL) != NULL);
assert_int_eq(error_pos, 0);
#endif

// Test setting options with the "=" format.
error_pos = -1;
assert_true(lzma_str_to_filters("lzma2=dict=4096,lc=2,lp=2,pb=1,"
"mode=fast,nice=3,mf=hc3,depth=10", &error_pos,
filters, 0, NULL) == NULL);
assert_int_eq(error_pos, 63);
assert_uint_eq(filters[0].id, LZMA_FILTER_LZMA2);
assert_uint_eq(filters[1].id, LZMA_VLI_UNKNOWN);

Expand All @@ -150,8 +193,10 @@ test_lzma_str_to_filters(void)

#if defined(HAVE_ENCODER_X86) || defined(HAVE_DECODER_X86)
// Test BCJ Filter options.
error_pos = -1;
assert_true(lzma_str_to_filters("x86:start=16", &error_pos, filters,
LZMA_STR_NO_VALIDATION, NULL) == NULL);
assert_int_eq(error_pos, 12);

assert_uint_eq(filters[0].id, LZMA_FILTER_X86);
assert_uint_eq(filters[1].id, LZMA_VLI_UNKNOWN);
Expand All @@ -164,8 +209,10 @@ test_lzma_str_to_filters(void)

#if defined(HAVE_ENCODER_DELTA) || defined(HAVE_DECODER_DELTA)
// Test Delta Filter options.
error_pos = -1;
assert_true(lzma_str_to_filters("delta:dist=20", &error_pos, filters,
LZMA_STR_NO_VALIDATION, NULL) == NULL);
assert_int_eq(error_pos, 13);

assert_uint_eq(filters[0].id, LZMA_FILTER_DELTA);
assert_uint_eq(filters[1].id, LZMA_VLI_UNKNOWN);
Expand All @@ -177,26 +224,32 @@ test_lzma_str_to_filters(void)
#endif

// Test skipping leading spaces.
error_pos = -1;
assert_true(lzma_str_to_filters(" lzma2", &error_pos, filters,
0, NULL) == NULL);
assert_int_eq(error_pos, 9);

assert_uint_eq(filters[0].id, LZMA_FILTER_LZMA2);
assert_uint_eq(filters[1].id, LZMA_VLI_UNKNOWN);

lzma_filters_free(filters, NULL);

// Test skipping trailing spaces.
error_pos = -1;
assert_true(lzma_str_to_filters("lzma2 ", &error_pos, filters,
0, NULL) == NULL);
assert_int_eq(error_pos, 9);

assert_uint_eq(filters[0].id, LZMA_FILTER_LZMA2);
assert_uint_eq(filters[1].id, LZMA_VLI_UNKNOWN);

lzma_filters_free(filters, NULL);

// Test with "--" instead of space separating.
error_pos = -1;
assert_true(lzma_str_to_filters("lzma2--lzma2", &error_pos, filters,
LZMA_STR_NO_VALIDATION, NULL) == NULL);
assert_int_eq(error_pos, 12);

assert_uint_eq(filters[0].id, LZMA_FILTER_LZMA2);
assert_uint_eq(filters[1].id, LZMA_FILTER_LZMA2);
Expand All @@ -205,57 +258,75 @@ test_lzma_str_to_filters(void)
lzma_filters_free(filters, NULL);

// Test preset with and without leading "-", and with "e".
error_pos = -1;
assert_true(lzma_str_to_filters("-3", &error_pos, filters,
0, NULL) == NULL);
assert_int_eq(error_pos, 2);

assert_uint_eq(filters[0].id, LZMA_FILTER_LZMA2);
assert_uint_eq(filters[1].id, LZMA_VLI_UNKNOWN);

lzma_filters_free(filters, NULL);

error_pos = -1;
assert_true(lzma_str_to_filters("4", &error_pos, filters,
0, NULL) == NULL);
assert_int_eq(error_pos, 1);

assert_uint_eq(filters[0].id, LZMA_FILTER_LZMA2);
assert_uint_eq(filters[1].id, LZMA_VLI_UNKNOWN);

lzma_filters_free(filters, NULL);

error_pos = -1;
assert_true(lzma_str_to_filters("9e", &error_pos, filters,
0, NULL) == NULL);
assert_int_eq(error_pos, 2);

assert_uint_eq(filters[0].id, LZMA_FILTER_LZMA2);
assert_uint_eq(filters[1].id, LZMA_VLI_UNKNOWN);

lzma_filters_free(filters, NULL);

// Test using a preset as an lzma2 option.
error_pos = -1;
assert_true(lzma_str_to_filters("lzma2:preset=9e", &error_pos, filters,
0, NULL) == NULL);
assert_int_eq(error_pos, 15);

assert_uint_eq(filters[0].id, LZMA_FILTER_LZMA2);
assert_uint_eq(filters[1].id, LZMA_VLI_UNKNOWN);

lzma_filters_free(filters, NULL);

// Test setting dictionary size with invalid modifier suffix.
error_pos = -1;
assert_true(lzma_str_to_filters("lzma2:dict=4096ZiB", &error_pos, filters,
0, NULL) != NULL);
assert_int_eq(error_pos, 15);

error_pos = -1;
assert_true(lzma_str_to_filters("lzma2:dict=4096KiBs", &error_pos, filters,
0, NULL) != NULL);
assert_int_eq(error_pos, 15);

// Test option that cannot have multiplier modifier.
error_pos = -1;
assert_true(lzma_str_to_filters("lzma2:pb=1k", &error_pos, filters,
0, NULL) != NULL);
assert_int_eq(error_pos, 10);

// Test option value too large.
error_pos = -1;
assert_true(lzma_str_to_filters("lzma2:dict=4096GiB", &error_pos, filters,
0, NULL) != NULL);
assert_int_eq(error_pos, 11);

// Test valid uses of multiplier modifiers (k,m,g).
error_pos = -1;
assert_true(lzma_str_to_filters("lzma2:dict=4096KiB", &error_pos, filters,
0, NULL) == NULL);
assert_int_eq(error_pos, 18);

assert_uint_eq(filters[0].id, LZMA_FILTER_LZMA2);
assert_uint_eq(filters[1].id, LZMA_VLI_UNKNOWN);
Expand All @@ -265,8 +336,10 @@ test_lzma_str_to_filters(void)

lzma_filters_free(filters, NULL);

error_pos = -1;
assert_true(lzma_str_to_filters("lzma2:dict=40Mi", &error_pos, filters,
0, NULL) == NULL);
assert_int_eq(error_pos, 15);

assert_uint_eq(filters[0].id, LZMA_FILTER_LZMA2);
assert_uint_eq(filters[1].id, LZMA_VLI_UNKNOWN);
Expand All @@ -276,8 +349,10 @@ test_lzma_str_to_filters(void)

lzma_filters_free(filters, NULL);

error_pos = -1;
assert_true(lzma_str_to_filters("lzma2:dict=1g", &error_pos, filters,
0, NULL) == NULL);
assert_int_eq(error_pos, 13);

assert_uint_eq(filters[0].id, LZMA_FILTER_LZMA2);
assert_uint_eq(filters[1].id, LZMA_VLI_UNKNOWN);
Expand Down

0 comments on commit b0366df

Please sign in to comment.