33 const ada::url_pattern_options& options,
34 std::vector<reference_route>& out) {
35 for (
const std::string& pattern : patterns) {
36 auto compile_options = ada::url_pattern_compile_component_options::PATHNAME;
37 auto part_list = ada::url_pattern_helpers::parse_pattern_string(
38 pattern, compile_options,
39 ada::url_pattern_helpers::canonicalize_pathname);
44 if (compiler::classify_parts(*part_list, route.
info.segments,
45 route.
info.group_names)) {
46 compiler::finalize_route(route.
info);
48 compiler::approximate_kind_sequence(*part_list, route.
info);
50 auto parsed = ada::parse_url_pattern<regex_provider>(
51 ada::url_pattern_init{.pathname = pattern},
nullptr, &options);
55 route.
pattern = std::move(*parsed);
56 out.push_back(std::move(route));
116 const std::vector<reference_route>& reference,
117 const std::vector<std::string>& patterns,
118 const std::string& input) {
123 const std::vector<char> exact(input.begin(), input.end());
124 const auto matched = list.match(std::string_view(exact.data(), exact.size()));
127 const int32_t expected_route =
129 if (matched.route_index != expected_route) {
130 printf(
"list=%d ref=%d\n", matched.route_index, expected_route);
131 fail(
"winner mismatch", input, patterns);
133 if (expected_route < 0) {
137 reference[
static_cast<size_t>(expected_route)];
138 const bool is_regexp =
139 winner.
info.mode == ada::url_pattern_list_detail::route_mode::regexp;
140 if (matched.regexp_route != is_regexp) {
141 fail(
"capture form mismatch", input, patterns);
145 if (matched.regexp_groups != expected_groups) {
146 fail(
"regexp group mismatch", input, patterns);
152 const size_t n_reported =
153 std::min<size_t>(expected_groups.size(),
154 ada::url_pattern_list_limits::max_captures_per_route);
155 if (matched.capture_count != n_reported ||
156 matched.captures_truncated != (expected_groups.size() > n_reported) ||
157 !matched.regexp_groups.empty()) {
158 fail(
"capture count mismatch", input, patterns);
160 for (
size_t k = 0; k < n_reported; k++) {
161 const auto& capture = matched.captures[k];
162 if (capture.offset + capture.length > input.size() ||
163 !expected_groups[k].has_value() ||
164 input.compare(capture.offset, capture.length, *expected_groups[k]) !=
166 fail(
"capture slice mismatch", input, patterns);
172 FuzzedDataProvider fdp(data, size);
173 auto to_ascii = [](
const std::string& source) -> std::string {
175 result.reserve(source.size());
176 for (
char c : source) {
177 result.push_back(
static_cast<unsigned char>(c) % 128);
184 auto token = [&fdp]() -> std::string {
185 static constexpr char alphabet[] =
186 "abcdefghijklmnopqrstuvwxyzABCDEF0123456789-_.~";
187 const size_t length = fdp.ConsumeIntegralInRange<
size_t>(1, 12);
189 result.reserve(length);
190 for (
size_t j = 0; j < length; j++) {
192 alphabet[fdp.ConsumeIntegralInRange<
size_t>(0,
sizeof(alphabet) - 2)];
198 const size_t n_patterns = fdp.ConsumeIntegralInRange<
size_t>(1, 64);
199 std::vector<std::string> patterns;
200 patterns.reserve(n_patterns);
201 for (
size_t i = 0; i < n_patterns; i++) {
202 const int mode = fdp.ConsumeIntegralInRange<
int>(0, 6);
206 patterns.push_back(
"/" + to_ascii(fdp.ConsumeRandomLengthString(40)));
207 }
else if (mode == 1 && !patterns.empty()) {
210 patterns[fdp.ConsumeIntegralInRange<
size_t>(0, patterns.size() - 1)]);
214 const size_t depth = fdp.ConsumeIntegralInRange<
size_t>(1, 20);
216 for (
size_t d = 0; d < depth; d++) {
217 const int kind = fdp.ConsumeIntegralInRange<
int>(0, 11);
218 if (kind == 0 && d + 1 == depth) {
220 }
else if (kind <= 3) {
221 pattern +=
"/:p" + std::to_string(params++);
222 }
else if (kind == 4) {
224 pattern +=
"/aaaaaaaa";
226 static_cast<char>(
'A' + fdp.ConsumeIntegralInRange<
int>(0, 25));
227 pattern +=
"aaaaaaaa";
228 }
else if (kind == 5 && mode == 6) {
230 switch (fdp.ConsumeIntegralInRange<
int>(0, 3)) {
232 pattern +=
"/(\\d+)";
235 pattern +=
"/:q" + std::to_string(params++) +
"?";
238 pattern +=
"/x-(\\w+)";
241 pattern +=
"/pre-:m" + std::to_string(params++);
245 pattern +=
"/" + token();
248 patterns.push_back(std::move(pattern));
251 const ada::url_pattern_options options{.ignore_case = fdp.ConsumeBool()};
252 std::vector<std::string_view> views(patterns.begin(), patterns.end());
254 ada::parse_url_pattern_list<regex_provider>(views,
nullptr, &options);
258 std::vector<reference_route> reference;
259 const bool reference_ok =
build_reference(patterns, options, reference);
260 if (list_result.has_value() != reference_ok) {
261 printf(
"parse/reference disagreement: parse=%d reference=%d\n",
262 list_result.has_value() ? 1 : 0, reference_ok ? 1 : 0);
271 volatile uint64_t sink = list.size();
272 sink += list.ignore_case() ? 1 : 0;
273 bool has_regexp_route =
false;
274 for (
size_t i = 0; i < list.size(); i++) {
275 sink += list.pattern(i).size();
276 for (
const std::string& name : list.group_names(i)) {
279 has_regexp_route |= reference[i].info.mode ==
280 ada::url_pattern_list_detail::route_mode::regexp;
284 const size_t n_inputs = fdp.ConsumeIntegralInRange<
size_t>(1, 8);
285 for (
size_t u = 0; u < n_inputs; u++) {
287 const int strategy = fdp.ConsumeIntegralInRange<
int>(0, 9);
288 if (strategy >= 8 && !has_regexp_route) {
292 if (fdp.ConsumeBool()) {
294 const size_t n_segments = fdp.ConsumeIntegralInRange<
size_t>(23, 27);
295 const std::string segment = token();
296 for (
size_t s = 0; s < n_segments; s++) {
302 const size_t target = fdp.ConsumeIntegralInRange<
size_t>(4080, 4110);
303 input =
"/" + patterns[fdp.ConsumeIntegralInRange<
size_t>(
304 0, patterns.size() - 1)];
305 while (input.size() < target) {
309 input.resize(target);
311 }
else if (strategy == 7) {
313 input = fdp.ConsumeRandomLengthString(64);
318 reference[fdp.ConsumeIntegralInRange<
size_t>(0,
319 reference.size() - 1)];
320 if (base.info.segments.empty()) {
321 input =
"/" + to_ascii(fdp.ConsumeRandomLengthString(40));
323 for (
const compiler::route_segment& segment : base.info.segments) {
324 if (segment.kind == compiler::segment_kind::literal) {
325 input +=
"/" + segment.text;
326 }
else if (segment.kind == compiler::segment_kind::param) {
327 input +=
"/" + token();
329 const size_t tail = fdp.ConsumeIntegralInRange<
size_t>(0, 3);
330 for (
size_t t = 0; t < tail; t++) {
331 input +=
"/" + token();
339 switch (fdp.ConsumeIntegralInRange<
int>(0, 6)) {
341 if (!input.empty()) {
342 input[fdp.ConsumeIntegralInRange<
size_t>(0, input.size() - 1)] =
343 static_cast<char>(
'a' + fdp.ConsumeIntegralInRange<
int>(0, 25));
350 input.insert(fdp.ConsumeIntegralInRange<
size_t>(0, input.size()),
354 input +=
"/" + token();
357 if (
const size_t slash = input.find_last_of(
'/');
358 slash != std::string::npos && slash > 0) {
364 if (!input.empty()) {
366 input[fdp.ConsumeIntegralInRange<
size_t>(0, input.size() - 1)];
367 c =
static_cast<char>(
static_cast<unsigned char>(c) ^ 0x20);
374 if (has_regexp_route && input.size() > 64) {