Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitriyMusatkin committed Jan 8, 2024
1 parent 2679066 commit ebfb115
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
16 changes: 15 additions & 1 deletion source/endpoints_regex.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,21 @@ struct aws_endpoint_regex *aws_endpoint_regex_new_from_string(
goto on_error;
}

aws_byte_cursor_advance(&regex_pattern, group.len);
if (regex_pattern.len == 0 || regex_pattern.ptr[0] != ')') {
AWS_LOGF_ERROR(
AWS_LS_SDKUTILS_ENDPOINTS_REGEX, "Invalid regex pattern. Missing closing parenthesis.");
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
goto on_error;
}
aws_byte_cursor_advance(&regex_pattern, 1);

if (group.len == 0) {
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_REGEX, "Invalid regex pattern. Empty group.");
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
goto on_error;
}

/* Verify that group is only used for alternation. */
for (size_t i = 0; i < group.len; ++i) {
if (!aws_isalnum(group.ptr[i]) && group.ptr[i] != '|') {
Expand All @@ -157,7 +172,6 @@ struct aws_endpoint_regex *aws_endpoint_regex_new_from_string(

symbol.type = AWS_ENDPOINTS_REGEX_SYMBOL_ALTERNATION_GROUP;
symbol.info.alternation = aws_string_new_from_cursor(allocator, &group);
aws_byte_cursor_advance(&regex_pattern, group.len + 1);
break;
}

Expand Down
35 changes: 35 additions & 0 deletions tests/endpoint_regex_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,40 @@ static int s_test_misc_regex_validation(struct aws_allocator *allocator, void *c
ASSERT_NULL(regex);
ASSERT_INT_EQUALS(AWS_ERROR_SDKUTILS_ENDPOINTS_UNSUPPORTED_REGEX, aws_last_error());

regex = aws_endpoint_regex_new_from_string(allocator, aws_byte_cursor_from_c_str(""));
ASSERT_NULL(regex);
ASSERT_INT_EQUALS(AWS_ERROR_INVALID_ARGUMENT, aws_last_error());

regex = aws_endpoint_regex_new_from_string(
allocator, aws_byte_cursor_from_c_str("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
ASSERT_NULL(regex);
ASSERT_INT_EQUALS(AWS_ERROR_INVALID_ARGUMENT, aws_last_error());

regex = aws_endpoint_regex_new_from_string(allocator, aws_byte_cursor_from_c_str("aaaaa"));
ASSERT_NULL(regex);
ASSERT_INT_EQUALS(AWS_ERROR_SDKUTILS_ENDPOINTS_UNSUPPORTED_REGEX, aws_last_error());

regex = aws_endpoint_regex_new_from_string(allocator, aws_byte_cursor_from_c_str("^aaa(aa$"));
ASSERT_NULL(regex);
ASSERT_INT_EQUALS(AWS_ERROR_INVALID_ARGUMENT, aws_last_error());

regex = aws_endpoint_regex_new_from_string(allocator, aws_byte_cursor_from_c_str("^aaaaa($"));
ASSERT_NULL(regex);
ASSERT_INT_EQUALS(AWS_ERROR_INVALID_ARGUMENT, aws_last_error());

regex = aws_endpoint_regex_new_from_string(allocator, aws_byte_cursor_from_c_str("^aaa()aa$"));
ASSERT_NULL(regex);
ASSERT_INT_EQUALS(AWS_ERROR_INVALID_ARGUMENT, aws_last_error());

regex = aws_endpoint_regex_new_from_string(allocator, aws_byte_cursor_from_c_str("^aaaaa$"));
ASSERT_NOT_NULL(regex);

ASSERT_ERROR(
AWS_ERROR_INVALID_ARGUMENT,
aws_endpoint_regex_match(
regex, aws_byte_cursor_from_c_str("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")));

aws_endpoint_regex_destroy(regex);

return AWS_OP_SUCCESS;
}

0 comments on commit ebfb115

Please sign in to comment.