Tests: test_filter_str: Test *error_pos more thoroughly

This commit is contained in:
Lasse Collin 2024-04-22 22:23:32 +03:00
parent 70d12dd069
commit b0366df1d7
1 changed files with 76 additions and 1 deletions

View File

@ -19,37 +19,56 @@ test_lzma_str_to_filters(void)
int error_pos; int error_pos;
// Test with NULL string. // Test with NULL string.
error_pos = -1;
assert_true(lzma_str_to_filters(NULL, &error_pos, filters, 0, assert_true(lzma_str_to_filters(NULL, &error_pos, filters, 0,
NULL) != NULL); NULL) != NULL);
assert_int_eq(error_pos, 0);
// Test with NULL filter array. // Test with NULL filter array.
error_pos = -1;
assert_true(lzma_str_to_filters("lzma2", &error_pos, NULL, 0, assert_true(lzma_str_to_filters("lzma2", &error_pos, NULL, 0,
NULL) != NULL); NULL) != NULL);
assert_int_eq(error_pos, 0);
// Test with unsupported flags. // Test with unsupported flags.
error_pos = -1;
assert_true(lzma_str_to_filters("lzma2", &error_pos, filters, assert_true(lzma_str_to_filters("lzma2", &error_pos, filters,
UINT32_MAX, NULL) != NULL); UINT32_MAX, NULL) != NULL);
assert_int_eq(error_pos, 0);
error_pos = -1;
assert_true(lzma_str_to_filters("lzma2", &error_pos, filters, assert_true(lzma_str_to_filters("lzma2", &error_pos, filters,
LZMA_STR_NO_SPACES << 1, NULL) != NULL); 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, assert_true(lzma_str_to_filters("lzma2", &error_pos, filters,
LZMA_STR_NO_SPACES, NULL) != NULL); LZMA_STR_NO_SPACES, NULL) != NULL);
assert_int_eq(error_pos, 0);
// Test with empty string. // Test with empty string.
error_pos = -1;
assert_true(lzma_str_to_filters("", &error_pos, assert_true(lzma_str_to_filters("", &error_pos,
filters, 0, NULL) != NULL); filters, 0, NULL) != NULL);
assert_int_eq(error_pos, 0); assert_int_eq(error_pos, 0);
// Test with invalid filter name and missing filter name. // 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, assert_true(lzma_str_to_filters("lzma2 abcd", &error_pos,
filters, 0, NULL) != NULL); filters, 0, NULL) != NULL);
assert_int_eq(error_pos, 6); assert_int_eq(error_pos, 6);
error_pos = -1;
assert_true(lzma_str_to_filters("lzma2--abcd", &error_pos, assert_true(lzma_str_to_filters("lzma2--abcd", &error_pos,
filters, 0, NULL) != NULL); filters, 0, NULL) != NULL);
assert_int_eq(error_pos, 7); assert_int_eq(error_pos, 7);
error_pos = -1;
assert_true(lzma_str_to_filters("lzma2--", &error_pos, assert_true(lzma_str_to_filters("lzma2--", &error_pos,
filters, 0, NULL) != NULL); filters, 0, NULL) != NULL);
assert_int_eq(error_pos, 7); assert_int_eq(error_pos, 7);
@ -57,12 +76,15 @@ test_lzma_str_to_filters(void)
// Test LZMA_STR_ALL_FILTERS flag (should work with LZMA1 if built). // Test LZMA_STR_ALL_FILTERS flag (should work with LZMA1 if built).
#if defined(HAVE_ENCODER_LZMA1) || defined(HAVE_DECODER_LZMA1) #if defined(HAVE_ENCODER_LZMA1) || defined(HAVE_DECODER_LZMA1)
// Using LZMA1 as a Filter should fail without LZMA_STR_ALL_FILTERS. // 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, assert_true(lzma_str_to_filters("lzma1", &error_pos, filters,
0, NULL) != NULL); 0, NULL) != NULL);
assert_int_eq(error_pos, 0); assert_int_eq(error_pos, 0);
error_pos = -1;
assert_true(lzma_str_to_filters("lzma1", &error_pos, filters, assert_true(lzma_str_to_filters("lzma1", &error_pos, filters,
LZMA_STR_ALL_FILTERS, NULL) == NULL); LZMA_STR_ALL_FILTERS, NULL) == NULL);
assert_int_eq(error_pos, 5);
// Verify Filters array IDs are correct. The array should contain // Verify Filters array IDs are correct. The array should contain
// only two elements: // only two elements:
@ -78,11 +100,15 @@ test_lzma_str_to_filters(void)
// same Filter multiple times in the chain and having a non-last // same Filter multiple times in the chain and having a non-last
// Filter like lzma2 appear before another Filter. // Filter like lzma2 appear before another Filter.
// Without the flag, "lzma2 lzma2" must fail. // Without the flag, "lzma2 lzma2" must fail.
error_pos = -1;
assert_true(lzma_str_to_filters("lzma2 lzma2", &error_pos, filters, assert_true(lzma_str_to_filters("lzma2 lzma2", &error_pos, filters,
0, NULL) != NULL); 0, NULL) != NULL);
assert_int_eq(error_pos, 11);
error_pos = -1;
assert_true(lzma_str_to_filters("lzma2 lzma2", &error_pos, filters, assert_true(lzma_str_to_filters("lzma2 lzma2", &error_pos, filters,
LZMA_STR_NO_VALIDATION, NULL) == NULL); 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[0].id, LZMA_FILTER_LZMA2);
assert_uint_eq(filters[1].id, LZMA_FILTER_LZMA2); assert_uint_eq(filters[1].id, LZMA_FILTER_LZMA2);
@ -91,48 +117,65 @@ test_lzma_str_to_filters(void)
lzma_filters_free(filters, NULL); lzma_filters_free(filters, NULL);
// Should fail with invalid Filter options (lc + lp must be <= 4). // 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, assert_true(lzma_str_to_filters("lzma2:lc=3,lp=3", &error_pos, filters,
LZMA_STR_NO_VALIDATION, NULL) != NULL); LZMA_STR_NO_VALIDATION, NULL) != NULL);
assert_int_eq(error_pos, 15);
// Test invalid option name. // Test invalid option name.
error_pos = -1;
assert_true(lzma_str_to_filters("lzma2:foo=1,bar=2", &error_pos, assert_true(lzma_str_to_filters("lzma2:foo=1,bar=2", &error_pos,
filters, 0, NULL) != NULL); filters, 0, NULL) != NULL);
assert_int_eq(error_pos, 6); 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. // Test missing option value.
error_pos = -1;
assert_true(lzma_str_to_filters("lzma2:lc=", &error_pos, assert_true(lzma_str_to_filters("lzma2:lc=", &error_pos,
filters, 0, NULL) != NULL); filters, 0, NULL) != NULL);
assert_int_eq(error_pos, 9); assert_int_eq(error_pos, 9);
error_pos = -1;
assert_true(lzma_str_to_filters("lzma2:=,pb=1", &error_pos, assert_true(lzma_str_to_filters("lzma2:=,pb=1", &error_pos,
filters, 0, NULL) != NULL); filters, 0, NULL) != NULL);
assert_int_eq(error_pos, 6); assert_int_eq(error_pos, 6);
// Test unsupported preset value. // Test unsupported preset value.
error_pos = -1;
assert_true(lzma_str_to_filters("-10", &error_pos, assert_true(lzma_str_to_filters("-10", &error_pos,
filters, 0, NULL) != NULL); filters, 0, NULL) != NULL);
assert_int_eq(error_pos, 2); assert_int_eq(error_pos, 2);
error_pos = -1;
assert_true(lzma_str_to_filters("-5f", &error_pos, assert_true(lzma_str_to_filters("-5f", &error_pos,
filters, 0, NULL) != NULL); filters, 0, NULL) != NULL);
assert_int_eq(error_pos, 2); assert_int_eq(error_pos, 2);
// Test filter chain too long. // Test filter chain too long.
error_pos = -1;
assert_true(lzma_str_to_filters("lzma2 lzma2 lzma2 lzma2 lzma2", assert_true(lzma_str_to_filters("lzma2 lzma2 lzma2 lzma2 lzma2",
&error_pos, filters, LZMA_STR_NO_VALIDATION, &error_pos, filters, LZMA_STR_NO_VALIDATION,
NULL) != NULL); 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) #if defined(HAVE_ENCODER_LZMA1) || defined(HAVE_DECODER_LZMA1)
// Should fail with a Filter not supported in the .xz format (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, assert_true(lzma_str_to_filters("lzma1", &error_pos, filters,
LZMA_STR_NO_VALIDATION, NULL) != NULL); LZMA_STR_NO_VALIDATION, NULL) != NULL);
assert_int_eq(error_pos, 0);
#endif #endif
// Test setting options with the "=" format. // Test setting options with the "=" format.
error_pos = -1;
assert_true(lzma_str_to_filters("lzma2=dict=4096,lc=2,lp=2,pb=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, "mode=fast,nice=3,mf=hc3,depth=10", &error_pos,
filters, 0, NULL) == NULL); filters, 0, NULL) == NULL);
assert_int_eq(error_pos, 63);
assert_uint_eq(filters[0].id, LZMA_FILTER_LZMA2); assert_uint_eq(filters[0].id, LZMA_FILTER_LZMA2);
assert_uint_eq(filters[1].id, LZMA_VLI_UNKNOWN); assert_uint_eq(filters[1].id, LZMA_VLI_UNKNOWN);
@ -150,8 +193,10 @@ test_lzma_str_to_filters(void)
#if defined(HAVE_ENCODER_X86) || defined(HAVE_DECODER_X86) #if defined(HAVE_ENCODER_X86) || defined(HAVE_DECODER_X86)
// Test BCJ Filter options. // Test BCJ Filter options.
error_pos = -1;
assert_true(lzma_str_to_filters("x86:start=16", &error_pos, filters, assert_true(lzma_str_to_filters("x86:start=16", &error_pos, filters,
LZMA_STR_NO_VALIDATION, NULL) == NULL); 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[0].id, LZMA_FILTER_X86);
assert_uint_eq(filters[1].id, LZMA_VLI_UNKNOWN); assert_uint_eq(filters[1].id, LZMA_VLI_UNKNOWN);
@ -164,8 +209,10 @@ test_lzma_str_to_filters(void)
#if defined(HAVE_ENCODER_DELTA) || defined(HAVE_DECODER_DELTA) #if defined(HAVE_ENCODER_DELTA) || defined(HAVE_DECODER_DELTA)
// Test Delta Filter options. // Test Delta Filter options.
error_pos = -1;
assert_true(lzma_str_to_filters("delta:dist=20", &error_pos, filters, assert_true(lzma_str_to_filters("delta:dist=20", &error_pos, filters,
LZMA_STR_NO_VALIDATION, NULL) == NULL); 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[0].id, LZMA_FILTER_DELTA);
assert_uint_eq(filters[1].id, LZMA_VLI_UNKNOWN); assert_uint_eq(filters[1].id, LZMA_VLI_UNKNOWN);
@ -177,8 +224,10 @@ test_lzma_str_to_filters(void)
#endif #endif
// Test skipping leading spaces. // Test skipping leading spaces.
error_pos = -1;
assert_true(lzma_str_to_filters(" lzma2", &error_pos, filters, assert_true(lzma_str_to_filters(" lzma2", &error_pos, filters,
0, NULL) == NULL); 0, NULL) == NULL);
assert_int_eq(error_pos, 9);
assert_uint_eq(filters[0].id, LZMA_FILTER_LZMA2); assert_uint_eq(filters[0].id, LZMA_FILTER_LZMA2);
assert_uint_eq(filters[1].id, LZMA_VLI_UNKNOWN); assert_uint_eq(filters[1].id, LZMA_VLI_UNKNOWN);
@ -186,8 +235,10 @@ test_lzma_str_to_filters(void)
lzma_filters_free(filters, NULL); lzma_filters_free(filters, NULL);
// Test skipping trailing spaces. // Test skipping trailing spaces.
error_pos = -1;
assert_true(lzma_str_to_filters("lzma2 ", &error_pos, filters, assert_true(lzma_str_to_filters("lzma2 ", &error_pos, filters,
0, NULL) == NULL); 0, NULL) == NULL);
assert_int_eq(error_pos, 9);
assert_uint_eq(filters[0].id, LZMA_FILTER_LZMA2); assert_uint_eq(filters[0].id, LZMA_FILTER_LZMA2);
assert_uint_eq(filters[1].id, LZMA_VLI_UNKNOWN); assert_uint_eq(filters[1].id, LZMA_VLI_UNKNOWN);
@ -195,8 +246,10 @@ test_lzma_str_to_filters(void)
lzma_filters_free(filters, NULL); lzma_filters_free(filters, NULL);
// Test with "--" instead of space separating. // Test with "--" instead of space separating.
error_pos = -1;
assert_true(lzma_str_to_filters("lzma2--lzma2", &error_pos, filters, assert_true(lzma_str_to_filters("lzma2--lzma2", &error_pos, filters,
LZMA_STR_NO_VALIDATION, NULL) == NULL); 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[0].id, LZMA_FILTER_LZMA2);
assert_uint_eq(filters[1].id, LZMA_FILTER_LZMA2); assert_uint_eq(filters[1].id, LZMA_FILTER_LZMA2);
@ -205,24 +258,30 @@ test_lzma_str_to_filters(void)
lzma_filters_free(filters, NULL); lzma_filters_free(filters, NULL);
// Test preset with and without leading "-", and with "e". // Test preset with and without leading "-", and with "e".
error_pos = -1;
assert_true(lzma_str_to_filters("-3", &error_pos, filters, assert_true(lzma_str_to_filters("-3", &error_pos, filters,
0, NULL) == NULL); 0, NULL) == NULL);
assert_int_eq(error_pos, 2);
assert_uint_eq(filters[0].id, LZMA_FILTER_LZMA2); assert_uint_eq(filters[0].id, LZMA_FILTER_LZMA2);
assert_uint_eq(filters[1].id, LZMA_VLI_UNKNOWN); assert_uint_eq(filters[1].id, LZMA_VLI_UNKNOWN);
lzma_filters_free(filters, NULL); lzma_filters_free(filters, NULL);
error_pos = -1;
assert_true(lzma_str_to_filters("4", &error_pos, filters, assert_true(lzma_str_to_filters("4", &error_pos, filters,
0, NULL) == NULL); 0, NULL) == NULL);
assert_int_eq(error_pos, 1);
assert_uint_eq(filters[0].id, LZMA_FILTER_LZMA2); assert_uint_eq(filters[0].id, LZMA_FILTER_LZMA2);
assert_uint_eq(filters[1].id, LZMA_VLI_UNKNOWN); assert_uint_eq(filters[1].id, LZMA_VLI_UNKNOWN);
lzma_filters_free(filters, NULL); lzma_filters_free(filters, NULL);
error_pos = -1;
assert_true(lzma_str_to_filters("9e", &error_pos, filters, assert_true(lzma_str_to_filters("9e", &error_pos, filters,
0, NULL) == NULL); 0, NULL) == NULL);
assert_int_eq(error_pos, 2);
assert_uint_eq(filters[0].id, LZMA_FILTER_LZMA2); assert_uint_eq(filters[0].id, LZMA_FILTER_LZMA2);
assert_uint_eq(filters[1].id, LZMA_VLI_UNKNOWN); assert_uint_eq(filters[1].id, LZMA_VLI_UNKNOWN);
@ -230,8 +289,10 @@ test_lzma_str_to_filters(void)
lzma_filters_free(filters, NULL); lzma_filters_free(filters, NULL);
// Test using a preset as an lzma2 option. // Test using a preset as an lzma2 option.
error_pos = -1;
assert_true(lzma_str_to_filters("lzma2:preset=9e", &error_pos, filters, assert_true(lzma_str_to_filters("lzma2:preset=9e", &error_pos, filters,
0, NULL) == NULL); 0, NULL) == NULL);
assert_int_eq(error_pos, 15);
assert_uint_eq(filters[0].id, LZMA_FILTER_LZMA2); assert_uint_eq(filters[0].id, LZMA_FILTER_LZMA2);
assert_uint_eq(filters[1].id, LZMA_VLI_UNKNOWN); assert_uint_eq(filters[1].id, LZMA_VLI_UNKNOWN);
@ -239,23 +300,33 @@ test_lzma_str_to_filters(void)
lzma_filters_free(filters, NULL); lzma_filters_free(filters, NULL);
// Test setting dictionary size with invalid modifier suffix. // Test setting dictionary size with invalid modifier suffix.
error_pos = -1;
assert_true(lzma_str_to_filters("lzma2:dict=4096ZiB", &error_pos, filters, assert_true(lzma_str_to_filters("lzma2:dict=4096ZiB", &error_pos, filters,
0, NULL) != NULL); 0, NULL) != NULL);
assert_int_eq(error_pos, 15);
error_pos = -1;
assert_true(lzma_str_to_filters("lzma2:dict=4096KiBs", &error_pos, filters, assert_true(lzma_str_to_filters("lzma2:dict=4096KiBs", &error_pos, filters,
0, NULL) != NULL); 0, NULL) != NULL);
assert_int_eq(error_pos, 15);
// Test option that cannot have multiplier modifier. // Test option that cannot have multiplier modifier.
error_pos = -1;
assert_true(lzma_str_to_filters("lzma2:pb=1k", &error_pos, filters, assert_true(lzma_str_to_filters("lzma2:pb=1k", &error_pos, filters,
0, NULL) != NULL); 0, NULL) != NULL);
assert_int_eq(error_pos, 10);
// Test option value too large. // Test option value too large.
error_pos = -1;
assert_true(lzma_str_to_filters("lzma2:dict=4096GiB", &error_pos, filters, assert_true(lzma_str_to_filters("lzma2:dict=4096GiB", &error_pos, filters,
0, NULL) != NULL); 0, NULL) != NULL);
assert_int_eq(error_pos, 11);
// Test valid uses of multiplier modifiers (k,m,g). // Test valid uses of multiplier modifiers (k,m,g).
error_pos = -1;
assert_true(lzma_str_to_filters("lzma2:dict=4096KiB", &error_pos, filters, assert_true(lzma_str_to_filters("lzma2:dict=4096KiB", &error_pos, filters,
0, NULL) == NULL); 0, NULL) == NULL);
assert_int_eq(error_pos, 18);
assert_uint_eq(filters[0].id, LZMA_FILTER_LZMA2); assert_uint_eq(filters[0].id, LZMA_FILTER_LZMA2);
assert_uint_eq(filters[1].id, LZMA_VLI_UNKNOWN); assert_uint_eq(filters[1].id, LZMA_VLI_UNKNOWN);
@ -265,8 +336,10 @@ test_lzma_str_to_filters(void)
lzma_filters_free(filters, NULL); lzma_filters_free(filters, NULL);
error_pos = -1;
assert_true(lzma_str_to_filters("lzma2:dict=40Mi", &error_pos, filters, assert_true(lzma_str_to_filters("lzma2:dict=40Mi", &error_pos, filters,
0, NULL) == NULL); 0, NULL) == NULL);
assert_int_eq(error_pos, 15);
assert_uint_eq(filters[0].id, LZMA_FILTER_LZMA2); assert_uint_eq(filters[0].id, LZMA_FILTER_LZMA2);
assert_uint_eq(filters[1].id, LZMA_VLI_UNKNOWN); assert_uint_eq(filters[1].id, LZMA_VLI_UNKNOWN);
@ -276,8 +349,10 @@ test_lzma_str_to_filters(void)
lzma_filters_free(filters, NULL); lzma_filters_free(filters, NULL);
error_pos = -1;
assert_true(lzma_str_to_filters("lzma2:dict=1g", &error_pos, filters, assert_true(lzma_str_to_filters("lzma2:dict=1g", &error_pos, filters,
0, NULL) == NULL); 0, NULL) == NULL);
assert_int_eq(error_pos, 13);
assert_uint_eq(filters[0].id, LZMA_FILTER_LZMA2); assert_uint_eq(filters[0].id, LZMA_FILTER_LZMA2);
assert_uint_eq(filters[1].id, LZMA_VLI_UNKNOWN); assert_uint_eq(filters[1].id, LZMA_VLI_UNKNOWN);