55 const std::string& test_input,
56 const std::string& test_base) {
57 std::string_view test_view(test_input.data(), test_input.size());
61 auto exec_result = pattern.exec(test_view,
nullptr);
62 auto test_result = pattern.test(test_view,
nullptr);
64 bool exec_matched = exec_result && exec_result->has_value();
65 bool test_matched = test_result && *test_result;
67 if (exec_matched != test_matched) {
69 "exec/test inconsistency on input '%s': exec_matched=%d "
71 test_input.c_str(), exec_matched, test_matched);
75 if (exec_result && exec_result->has_value()) {
80 if (!test_base.empty()) {
81 std::string_view base_view(test_base.data(), test_base.size());
82 auto test_result_with_base = pattern.test(test_view, &base_view);
83 auto exec_with_base = pattern.exec(test_view, &base_view);
85 bool exec_base_matched = exec_with_base && exec_with_base->has_value();
86 bool test_base_matched = test_result_with_base && *test_result_with_base;
88 if (exec_base_matched != test_base_matched) {
90 "exec/test inconsistency with base on input '%s': "
91 "exec_matched=%d test_matched=%d\n",
92 test_input.c_str(), exec_base_matched, test_base_matched);
96 if (exec_with_base && exec_with_base->has_value()) {
102 ada::url_pattern_init init_input{};
103 init_input.pathname = test_input;
104 auto test_with_init = pattern.test(init_input,
nullptr);
105 auto exec_with_init = pattern.exec(init_input,
nullptr);
107 if ((test_with_init && *test_with_init) !=
108 (exec_with_init && exec_with_init->has_value())) {
109 printf(
"exec/test inconsistency on url_pattern_init input\n");
112 if (exec_with_init && exec_with_init->has_value()) {
118 std::string_view sv(test_input.data(), test_input.size());
121 volatile bool tc = pattern.test_components(
122 std::string(parsed->get_protocol()),
123 std::string(parsed->get_username()),
124 std::string(parsed->get_password()),
125 std::string(parsed->get_hostname()), std::string(parsed->get_port()),
126 std::string(parsed->get_pathname()),
127 std::string(parsed->get_search()), std::string(parsed->get_hash()));
133 auto match_result = pattern.match(test_view,
nullptr);
138 auto to_ascii = [](
const std::string& source) -> std::string {
140 result.reserve(source.size());
141 for (
char c : source) {
142 result.push_back(
static_cast<unsigned char>(c) % 128);
146 FuzzedDataProvider fdp(data, size);
148 std::string source_1 =
"/" + to_ascii(fdp.ConsumeRandomLengthString(50)) +
149 "/" + to_ascii(fdp.ConsumeRandomLengthString(50));
150 std::string base_source_1 =
"/" +
151 to_ascii(fdp.ConsumeRandomLengthString(50)) +
152 "/" + to_ascii(fdp.ConsumeRandomLengthString(50));
154 std::string source_2 =
"https://ada-url.com/*";
155 std::string base_source_2 =
"https://ada-url.com";
158 std::string test_input =
"https://" +
159 to_ascii(fdp.ConsumeRandomLengthString(30)) +
"/" +
160 to_ascii(fdp.ConsumeRandomLengthString(20));
161 std::string test_base =
"https://ada-url.com";
163 std::array<std::pair<std::string, std::string>, 2> sources = {{
164 {source_1, base_source_1},
165 {source_2, base_source_2},
168 for (
const auto& [source, base_source] : sources) {
171 ada::parse_url_pattern<regex_provider>(source,
nullptr,
nullptr);
178 std::string_view base_source_view(base_source.data(), base_source.length());
179 auto result_with_base = ada::parse_url_pattern<regex_provider>(
180 source, &base_source_view,
nullptr);
181 if (result_with_base) {
187 ada::url_pattern_options options{.ignore_case = fdp.ConsumeBool()};
188 auto result_with_base_and_options = ada::parse_url_pattern<regex_provider>(
189 source, &base_source_view, &options);
190 if (result_with_base_and_options) {
197 int field_index = fdp.ConsumeIntegralInRange(0, 7);
198 std::string random_value = to_ascii(fdp.ConsumeRandomLengthString(50));
199 ada::url_pattern_init init{};
200 switch (field_index) {
202 init.protocol = random_value;
205 init.username = random_value;
208 init.password = random_value;
211 init.hostname = random_value;
214 init.port = random_value;
217 init.pathname = random_value;
220 init.search = random_value;
223 init.hash = random_value;
226 auto result_with_init = ada::parse_url_pattern<regex_provider>(
227 init, &base_source_view,
nullptr);
228 if (result_with_init) {
234 ada::url_pattern_init init_all{};
235 init_all.protocol = to_ascii(fdp.ConsumeRandomLengthString(10));
236 init_all.username = to_ascii(fdp.ConsumeRandomLengthString(10));
237 init_all.password = to_ascii(fdp.ConsumeRandomLengthString(10));
238 init_all.hostname = to_ascii(fdp.ConsumeRandomLengthString(20));
239 init_all.port = to_ascii(fdp.ConsumeRandomLengthString(5));
240 init_all.pathname =
"/" + to_ascii(fdp.ConsumeRandomLengthString(20));
241 init_all.search = to_ascii(fdp.ConsumeRandomLengthString(10));
242 init_all.hash = to_ascii(fdp.ConsumeRandomLengthString(10));
243 auto result_with_init_all =
244 ada::parse_url_pattern<regex_provider>(init_all,
nullptr,
nullptr);
245 if (result_with_init_all) {
257 ada::url_pattern_init init_base_url{};
258 init_base_url.pathname =
259 "/" + to_ascii(fdp.ConsumeRandomLengthString(20));
260 init_base_url.base_url =
"https://example.com";
261 auto result_base_in_init = ada::parse_url_pattern<regex_provider>(
262 init_base_url,
nullptr,
nullptr);
263 if (result_base_in_init) {
269 ada::url_pattern_init init_fuzz_base{};
270 init_fuzz_base.pathname =
271 "/" + to_ascii(fdp.ConsumeRandomLengthString(15));
272 init_fuzz_base.base_url =
273 "https://" + to_ascii(fdp.ConsumeRandomLengthString(20));
274 auto result_fuzz_base = ada::parse_url_pattern<regex_provider>(
275 init_fuzz_base,
nullptr,
nullptr);
276 if (result_fuzz_base) {
288 uint8_t field_mask = fdp.ConsumeIntegral<uint8_t>();
289 ada::url_pattern_init init_subset{};
290 if (field_mask & 0x01)
291 init_subset.protocol = to_ascii(fdp.ConsumeRandomLengthString(8));
292 if (field_mask & 0x02)
293 init_subset.hostname = to_ascii(fdp.ConsumeRandomLengthString(20));
294 if (field_mask & 0x04)
295 init_subset.port = to_ascii(fdp.ConsumeRandomLengthString(5));
296 if (field_mask & 0x08)
297 init_subset.pathname =
298 "/" + to_ascii(fdp.ConsumeRandomLengthString(20));
299 if (field_mask & 0x10)
300 init_subset.search = to_ascii(fdp.ConsumeRandomLengthString(10));
301 if (field_mask & 0x20)
302 init_subset.hash = to_ascii(fdp.ConsumeRandomLengthString(10));
303 if (field_mask & 0x40)
304 init_subset.username = to_ascii(fdp.ConsumeRandomLengthString(10));
305 if (field_mask & 0x80)
306 init_subset.password = to_ascii(fdp.ConsumeRandomLengthString(10));
308 ada::parse_url_pattern<regex_provider>(init_subset,
nullptr,
nullptr);