Skip to content

Commit

Permalink
improve fuzzing coverage of urlpattern (#889)
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosEduR authored Feb 24, 2025
1 parent b359743 commit 2bbe8f1
Showing 1 changed file with 55 additions and 31 deletions.
86 changes: 55 additions & 31 deletions fuzz/url_pattern.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,45 +25,69 @@ std::string bytesToAlphanumeric(const std::string& source) {
return result;
}

void exercise_result(auto result) {
(void)result.get_protocol();
(void)result.get_username();
(void)result.get_password();
(void)result.get_hostname();
(void)result.get_port();
(void)result.get_pathname();
(void)result.get_search();
(void)result.get_hash();
(void)result.ignore_case();
(void)result.has_regexp_groups();
}

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
FuzzedDataProvider fdp(data, size);
// We do not want to trigger arbitrary regex matching.
std::string source =
std::string source_1 =
"/" + bytesToAlphanumeric(fdp.ConsumeRandomLengthString(50)) + "/" +
bytesToAlphanumeric(fdp.ConsumeRandomLengthString(50));
std::string base_source =
std::string base_source_1 =
"/" + bytesToAlphanumeric(fdp.ConsumeRandomLengthString(50)) + "/" +
bytesToAlphanumeric(fdp.ConsumeRandomLengthString(50));

// Without base or options
auto result =
ada::parse_url_pattern<regex_provider>(source, nullptr, nullptr);
(void)result;

// Testing with base_url
std::string_view base_source_view(base_source.data(), base_source.length());
auto result_with_base = ada::parse_url_pattern<regex_provider>(
source, &base_source_view, nullptr);
(void)result_with_base;

// Testing with base_url and options
ada::url_pattern_options options{.ignore_case = true};
auto result_with_base_and_options = ada::parse_url_pattern<regex_provider>(
source, &base_source_view, &options);
(void)result_with_base_and_options;

// Testing with url_pattern_init and base url.
ada::url_pattern_init init{.protocol = source,
.username = source,
.password = source,
.hostname = source,
.port = source,
.pathname = source,
.search = source,
.hash = source};
auto result_with_init =
ada::parse_url_pattern<regex_provider>(init, &base_source_view, nullptr);
(void)result_with_init;
std::string source_2 = "https://ada-url.com/*";
std::string base_source_2 = "https://ada-url.com";

std::array<std::pair<std::string, std::string>, 2> sources = {{
{source_1, base_source_1},
{source_2, base_source_2},
}};

for (const auto& [source, base_source] : sources) {
// Without base or options
auto result =
ada::parse_url_pattern<regex_provider>(source, nullptr, nullptr);
if (result) exercise_result(*result);

// Testing with base_url
std::string_view base_source_view(base_source.data(), base_source.length());
auto result_with_base = ada::parse_url_pattern<regex_provider>(
source, &base_source_view, nullptr);
if (result_with_base) exercise_result(*result_with_base);

// Testing with base_url and options
ada::url_pattern_options options{.ignore_case = fdp.ConsumeBool()};
auto result_with_base_and_options = ada::parse_url_pattern<regex_provider>(
source, &base_source_view, &options);
if (result_with_base_and_options)
exercise_result(*result_with_base_and_options);

// Testing with url_pattern_init and base url.
ada::url_pattern_init init{.protocol = source,
.username = source,
.password = source,
.hostname = source,
.port = source,
.pathname = source,
.search = source,
.hash = source};
auto result_with_init = ada::parse_url_pattern<regex_provider>(
init, &base_source_view, nullptr);
if (result_with_init) exercise_result(*result_with_init);
}

return 0;
}

0 comments on commit 2bbe8f1

Please sign in to comment.