Add three more unit tests for regex_replace()

This commit is contained in:
Thomas Jensen 2021-11-01 21:20:37 +01:00
parent ab28ad8c3a
commit b07af10931
No known key found for this signature in database
GPG Key ID: A4ACEE270D0FB7DB
4 changed files with 74 additions and 4 deletions

View File

@ -67,7 +67,7 @@ uint32_t *regex_replace(pcre2_code *search, char *replace, uint32_t *input, cons
{
PCRE2_SPTR replacement = u32_strconv_from_arg(replace, config_encoding);
if (replacement == NULL) {
fprintf(stderr, "Failed to convert replacement string to UTF-32 - \"%s\"\n", replace);
bx_fprintf(stderr, "Failed to convert replacement string to UTF-32 - \"%s\"\n", replace);
return NULL;
}
@ -112,7 +112,7 @@ uint32_t *regex_replace(pcre2_code *search, char *replace, uint32_t *input, cons
PCRE2_UCHAR buffer[256];
pcre2_get_error_message(pcre2_rc, buffer, sizeof(buffer));
/* buffer will normally contain "invalid replacement string" */
fprintf(stderr, "Error substituting \"%s\": %s\n", replace, u32_strconv_to_output(buffer));
bx_fprintf(stderr, "Error substituting \"%s\": %s\n", replace, u32_strconv_to_output(buffer));
BFREE(output);
return NULL;
}

View File

@ -33,6 +33,15 @@
#include "regulex_test.h"
static int beforeTest(void** state) {
(void) state; /* unused */
collect_reset();
return 0;
}
int main(void)
{
setup_mocks();
@ -45,8 +54,11 @@ int main(void)
};
const struct CMUnitTest regulex_tests[] = {
cmocka_unit_test(test_compile_pattern_error),
cmocka_unit_test(test_compile_pattern_empty)
cmocka_unit_test_setup(test_compile_pattern_error, beforeTest),
cmocka_unit_test_setup(test_compile_pattern_empty, beforeTest),
cmocka_unit_test_setup(test_regex_replace_invalid_utf, beforeTest),
cmocka_unit_test_setup(test_regex_replace_buffer_resize, beforeTest),
cmocka_unit_test_setup(test_regex_replace_error, beforeTest)
};
int num_failed = 0;

View File

@ -27,10 +27,13 @@
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <uniconv.h>
#include <string.h>
#include "global_mock.h"
#include "regulex.h"
void test_compile_pattern_empty(void **state)
{
(void) state; /* unused */
@ -40,6 +43,7 @@ void test_compile_pattern_empty(void **state)
}
void test_compile_pattern_error(void **state)
{
(void) state; /* unused */
@ -57,4 +61,54 @@ void test_compile_pattern_error(void **state)
}
void test_regex_replace_invalid_utf(void **state)
{
(void) state; /* unused */
const char *input = "input";
assert_null(regex_replace(compile_pattern("search"), NULL, /* NULL is an invalid replacement string*/
u32_strconv_from_encoding(input, "ASCII", iconveh_question_mark), strlen(input), 0));
assert_int_equal(1, collect_err_size);
assert_string_equal("Failed to convert replacement string to UTF-32 - \"(null)\"\n", collect_err[0]);
}
void test_regex_replace_buffer_resize(void **state)
{
(void) state; /* unused */
const char *input = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
uint32_t *actual = regex_replace(compile_pattern("x"), "long_replacement_string_",
u32_strconv_from_encoding(input, "ASCII", iconveh_question_mark), strlen(input), 1);
char *actual_str = u32_strconv_to_encoding(actual, "ASCII", iconveh_question_mark);
const char *expected = "long_replacement_string_long_replacement_string_long_replacement_string_"
"long_replacement_string_long_replacement_string_long_replacement_string_long_replacement_string_"
"long_replacement_string_long_replacement_string_long_replacement_string_long_replacement_string_"
"long_replacement_string_long_replacement_string_long_replacement_string_long_replacement_string_"
"long_replacement_string_long_replacement_string_long_replacement_string_long_replacement_string_"
"long_replacement_string_long_replacement_string_long_replacement_string_long_replacement_string_"
"long_replacement_string_long_replacement_string_long_replacement_string_long_replacement_string_"
"long_replacement_string_long_replacement_string_long_replacement_string_long_replacement_string_"
"long_replacement_string_long_replacement_string_long_replacement_string_long_replacement_string_"
"long_replacement_string_long_replacement_string_long_replacement_string_long_replacement_string_";
assert_string_equal(expected, actual_str);
}
void test_regex_replace_error(void **state)
{
(void) state; /* unused */
const char *input = "xxx";
uint32_t *actual = regex_replace(compile_pattern("x"), "INVALID $2",
u32_strconv_from_encoding(input, "ASCII", iconveh_question_mark), strlen(input), 0);
assert_null(actual);
assert_int_equal(1, collect_err_size);
assert_string_equal("Error substituting \"INVALID $2\": unknown substring\n", collect_err[0]);
}
/*EOF*/ /* vim: set cindent sw=4: */

View File

@ -29,6 +29,10 @@
void test_compile_pattern_empty(void **state);
void test_compile_pattern_error(void **state);
void test_regex_replace_invalid_utf(void **state);
void test_regex_replace_buffer_resize(void **state);
void test_regex_replace_error(void **state);
#endif