Skip to content

Commit 2bbe8f1

Browse files
authored
improve fuzzing coverage of urlpattern (#889)
1 parent b359743 commit 2bbe8f1

File tree

1 file changed

+55
-31
lines changed

1 file changed

+55
-31
lines changed

fuzz/url_pattern.cc

+55-31
Original file line numberDiff line numberDiff line change
@@ -25,45 +25,69 @@ std::string bytesToAlphanumeric(const std::string& source) {
2525
return result;
2626
}
2727

28+
void exercise_result(auto result) {
29+
(void)result.get_protocol();
30+
(void)result.get_username();
31+
(void)result.get_password();
32+
(void)result.get_hostname();
33+
(void)result.get_port();
34+
(void)result.get_pathname();
35+
(void)result.get_search();
36+
(void)result.get_hash();
37+
(void)result.ignore_case();
38+
(void)result.has_regexp_groups();
39+
}
40+
2841
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
2942
FuzzedDataProvider fdp(data, size);
3043
// We do not want to trigger arbitrary regex matching.
31-
std::string source =
44+
std::string source_1 =
3245
"/" + bytesToAlphanumeric(fdp.ConsumeRandomLengthString(50)) + "/" +
3346
bytesToAlphanumeric(fdp.ConsumeRandomLengthString(50));
34-
std::string base_source =
47+
std::string base_source_1 =
3548
"/" + bytesToAlphanumeric(fdp.ConsumeRandomLengthString(50)) + "/" +
3649
bytesToAlphanumeric(fdp.ConsumeRandomLengthString(50));
3750

38-
// Without base or options
39-
auto result =
40-
ada::parse_url_pattern<regex_provider>(source, nullptr, nullptr);
41-
(void)result;
42-
43-
// Testing with base_url
44-
std::string_view base_source_view(base_source.data(), base_source.length());
45-
auto result_with_base = ada::parse_url_pattern<regex_provider>(
46-
source, &base_source_view, nullptr);
47-
(void)result_with_base;
48-
49-
// Testing with base_url and options
50-
ada::url_pattern_options options{.ignore_case = true};
51-
auto result_with_base_and_options = ada::parse_url_pattern<regex_provider>(
52-
source, &base_source_view, &options);
53-
(void)result_with_base_and_options;
54-
55-
// Testing with url_pattern_init and base url.
56-
ada::url_pattern_init init{.protocol = source,
57-
.username = source,
58-
.password = source,
59-
.hostname = source,
60-
.port = source,
61-
.pathname = source,
62-
.search = source,
63-
.hash = source};
64-
auto result_with_init =
65-
ada::parse_url_pattern<regex_provider>(init, &base_source_view, nullptr);
66-
(void)result_with_init;
51+
std::string source_2 = "https://ada-url.com/*";
52+
std::string base_source_2 = "https://ada-url.com";
53+
54+
std::array<std::pair<std::string, std::string>, 2> sources = {{
55+
{source_1, base_source_1},
56+
{source_2, base_source_2},
57+
}};
58+
59+
for (const auto& [source, base_source] : sources) {
60+
// Without base or options
61+
auto result =
62+
ada::parse_url_pattern<regex_provider>(source, nullptr, nullptr);
63+
if (result) exercise_result(*result);
64+
65+
// Testing with base_url
66+
std::string_view base_source_view(base_source.data(), base_source.length());
67+
auto result_with_base = ada::parse_url_pattern<regex_provider>(
68+
source, &base_source_view, nullptr);
69+
if (result_with_base) exercise_result(*result_with_base);
70+
71+
// Testing with base_url and options
72+
ada::url_pattern_options options{.ignore_case = fdp.ConsumeBool()};
73+
auto result_with_base_and_options = ada::parse_url_pattern<regex_provider>(
74+
source, &base_source_view, &options);
75+
if (result_with_base_and_options)
76+
exercise_result(*result_with_base_and_options);
77+
78+
// Testing with url_pattern_init and base url.
79+
ada::url_pattern_init init{.protocol = source,
80+
.username = source,
81+
.password = source,
82+
.hostname = source,
83+
.port = source,
84+
.pathname = source,
85+
.search = source,
86+
.hash = source};
87+
auto result_with_init = ada::parse_url_pattern<regex_provider>(
88+
init, &base_source_view, nullptr);
89+
if (result_with_init) exercise_result(*result_with_init);
90+
}
6791

6892
return 0;
6993
}

0 commit comments

Comments
 (0)