Coverage Report

Created: 2026-12-01 06:19

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ada-url/fuzz/parse.cc
Line
Count
Source
1
#include <fuzzer/FuzzedDataProvider.h>
2
3
#include <cassert>
4
#include <cstdio>
5
#include <memory>
6
#include <string>
7
8
#include "ada.cpp"
9
#include "ada.h"
10
11
59.3k
bool is_valid_utf8_string(const char* buf, size_t len) {
12
59.3k
  const uint8_t* data = reinterpret_cast<const uint8_t*>(buf);
13
59.3k
  uint64_t pos = 0;
14
59.3k
  uint32_t code_point = 0;
15
115k
  while (pos < len) {
16
91.6k
    uint64_t next_pos = pos + 16;
17
91.6k
    if (next_pos <= len) {  // if it is safe to read 16 more bytes, check that
18
                            // they are ascii
19
50.1k
      uint64_t v1;
20
50.1k
      std::memcpy(&v1, data + pos, sizeof(uint64_t));
21
50.1k
      uint64_t v2;
22
50.1k
      std::memcpy(&v2, data + pos + sizeof(uint64_t), sizeof(uint64_t));
23
50.1k
      uint64_t v{v1 | v2};
24
50.1k
      if ((v & 0x8080808080808080) == 0) {
25
25.4k
        pos = next_pos;
26
25.4k
        continue;
27
25.4k
      }
28
50.1k
    }
29
66.2k
    unsigned char byte = data[pos];
30
279k
    while (byte < 0b10000000) {
31
240k
      if (++pos == len) {
32
26.9k
        return true;
33
26.9k
      }
34
213k
      byte = data[pos];
35
213k
    }
36
37
39.2k
    if ((byte & 0b11100000) == 0b11000000) {
38
8.45k
      next_pos = pos + 2;
39
8.45k
      if (next_pos > len) {
40
510
        return false;
41
510
      }
42
7.94k
      if ((data[pos + 1] & 0b11000000) != 0b10000000) {
43
1.21k
        return false;
44
1.21k
      }
45
6.72k
      code_point = (byte & 0b00011111) << 6 | (data[pos + 1] & 0b00111111);
46
6.72k
      if ((code_point < 0x80) || (0x7ff < code_point)) {
47
54
        return false;
48
54
      }
49
30.8k
    } else if ((byte & 0b11110000) == 0b11100000) {
50
23.5k
      next_pos = pos + 3;
51
23.5k
      if (next_pos > len) {
52
211
        return false;
53
211
      }
54
23.2k
      if ((data[pos + 1] & 0b11000000) != 0b10000000) {
55
413
        return false;
56
413
      }
57
22.8k
      if ((data[pos + 2] & 0b11000000) != 0b10000000) {
58
77
        return false;
59
77
      }
60
22.8k
      code_point = (byte & 0b00001111) << 12 |
61
22.8k
                   (data[pos + 1] & 0b00111111) << 6 |
62
22.8k
                   (data[pos + 2] & 0b00111111);
63
22.8k
      if ((code_point < 0x800) || (0xffff < code_point) ||
64
22.7k
          (0xd7ff < code_point && code_point < 0xe000)) {
65
50
        return false;
66
50
      }
67
22.8k
    } else if ((byte & 0b11111000) == 0b11110000) {  // 0b11110000
68
1.63k
      next_pos = pos + 4;
69
1.63k
      if (next_pos > len) {
70
224
        return false;
71
224
      }
72
1.41k
      if ((data[pos + 1] & 0b11000000) != 0b10000000) {
73
276
        return false;
74
276
      }
75
1.13k
      if ((data[pos + 2] & 0b11000000) != 0b10000000) {
76
68
        return false;
77
68
      }
78
1.06k
      if ((data[pos + 3] & 0b11000000) != 0b10000000) {
79
53
        return false;
80
53
      }
81
1.01k
      code_point =
82
1.01k
          (byte & 0b00000111) << 18 | (data[pos + 1] & 0b00111111) << 12 |
83
1.01k
          (data[pos + 2] & 0b00111111) << 6 | (data[pos + 3] & 0b00111111);
84
1.01k
      if (code_point <= 0xffff || 0x10ffff < code_point) {
85
44
        return false;
86
44
      }
87
5.68k
    } else {
88
5.68k
      return false;
89
5.68k
    }
90
30.3k
    pos = next_pos;
91
30.3k
  }
92
23.4k
  return true;
93
59.3k
}
94
95
// Exercise all getters and boolean predicates on ada::url
96
13.0k
static void exercise_url_predicates(const ada::url& u) {
97
13.0k
  volatile size_t length = 0;
98
13.0k
  length += u.get_href().size();
99
13.0k
  length += u.get_origin().size();
100
13.0k
  length += u.get_protocol().size();
101
13.0k
  length += u.get_username().size();
102
13.0k
  length += u.get_password().size();
103
13.0k
  length += u.get_host().size();
104
13.0k
  length += u.get_hostname().size();
105
13.0k
  length += u.get_pathname().size();
106
13.0k
  length += u.get_search().size();
107
13.0k
  length += u.get_hash().size();
108
13.0k
  length += u.get_port().size();
109
13.0k
  length += u.to_string().size();
110
13.0k
  length += u.get_pathname_length();
111
13.0k
  (void)u.has_valid_domain();
112
13.0k
  (void)u.has_credentials();
113
13.0k
  (void)u.has_empty_hostname();
114
13.0k
  (void)u.has_hostname();
115
13.0k
  (void)u.has_port();
116
13.0k
  (void)u.has_hash();
117
13.0k
  (void)u.has_search();
118
13.0k
  (void)u.get_components();
119
13.0k
}
120
121
// Exercise all getters and boolean predicates on ada::url_aggregator
122
32.6k
static void exercise_aggregator_predicates(const ada::url_aggregator& u) {
123
32.6k
  volatile size_t length = 0;
124
32.6k
  length += u.get_href().size();
125
32.6k
  length += u.get_origin().size();
126
32.6k
  length += u.get_protocol().size();
127
32.6k
  length += u.get_username().size();
128
32.6k
  length += u.get_password().size();
129
32.6k
  length += u.get_host().size();
130
32.6k
  length += u.get_hostname().size();
131
32.6k
  length += u.get_pathname().size();
132
32.6k
  length += u.get_search().size();
133
32.6k
  length += u.get_hash().size();
134
32.6k
  length += u.get_port().size();
135
32.6k
  length += u.to_string().size();
136
32.6k
  length += u.get_pathname_length();
137
32.6k
  (void)u.has_valid_domain();
138
32.6k
  (void)u.has_credentials();
139
32.6k
  (void)u.has_empty_hostname();
140
32.6k
  (void)u.has_hostname();
141
32.6k
  (void)u.has_non_empty_username();
142
32.6k
  (void)u.has_non_empty_password();
143
32.6k
  (void)u.has_password();
144
32.6k
  (void)u.has_port();
145
32.6k
  (void)u.has_hash();
146
32.6k
  (void)u.has_search();
147
32.6k
  (void)u.get_components();
148
32.6k
  volatile bool is_valid = u.validate();
149
32.6k
  (void)is_valid;
150
32.6k
  (void)u.to_diagram();
151
32.6k
}
152
153
21.1k
static std::string make_simple_absolute_candidate(FuzzedDataProvider& fdp) {
154
21.1k
  static constexpr const char* kSchemes[] = {"http://",  "https://", "HTTP://",
155
21.1k
                                             "Https://", "http:",    "https:"};
156
21.1k
  static constexpr const char* kHosts[] = {
157
21.1k
      "example.com",
158
21.1k
      "www.example.com",
159
21.1k
      "WWW.Example.COM",
160
21.1k
      "a",
161
21.1k
      "a.b.c",
162
21.1k
      "192.168.0.1",
163
21.1k
      "0x7f.1",
164
21.1k
      "127.1",
165
21.1k
      "xn--nxasmq6b.com",
166
21.1k
      "xn--a",
167
21.1k
      "user@example.com",
168
21.1k
      "user:pass@example.com",
169
21.1k
      "example.com:8080",
170
21.1k
      "example.com:0",
171
21.1k
      "",
172
21.1k
  };
173
21.1k
  static constexpr const char* kPaths[] = {
174
21.1k
      "",
175
21.1k
      "/",
176
21.1k
      "/path",
177
21.1k
      "/path/file.js",
178
21.1k
      "/a/./b/../c",
179
21.1k
      "/foo/%2e",
180
21.1k
      "/foo/%2e%2e",
181
21.1k
      "/path with space",
182
21.1k
      "/path\twith\ttab",
183
21.1k
      "\\path",
184
21.1k
      "/continue=https%3A%2F%2Fexample.com%2F",
185
21.1k
  };
186
21.1k
  static constexpr const char* kQueries[] = {
187
21.1k
      "",
188
21.1k
      "?",
189
21.1k
      "?q=1",
190
21.1k
      "?hl=en&tab=wi",
191
21.1k
      "?continue=https%3A%2F%2Fexample.com%2F",
192
21.1k
  };
193
21.1k
  static constexpr const char* kFrags[] = {"", "#", "#frag", "#x%20y"};
194
195
21.1k
  std::string out;
196
21.1k
  out += kSchemes[fdp.ConsumeIntegralInRange<size_t>(
197
21.1k
      0, sizeof(kSchemes) / sizeof(kSchemes[0]) - 1)];
198
21.1k
  out += kHosts[fdp.ConsumeIntegralInRange<size_t>(
199
21.1k
      0, sizeof(kHosts) / sizeof(kHosts[0]) - 1)];
200
21.1k
  out += kPaths[fdp.ConsumeIntegralInRange<size_t>(
201
21.1k
      0, sizeof(kPaths) / sizeof(kPaths[0]) - 1)];
202
21.1k
  out += kQueries[fdp.ConsumeIntegralInRange<size_t>(
203
21.1k
      0, sizeof(kQueries) / sizeof(kQueries[0]) - 1)];
204
21.1k
  out += kFrags[fdp.ConsumeIntegralInRange<size_t>(
205
21.1k
      0, sizeof(kFrags) / sizeof(kFrags[0]) - 1)];
206
207
21.1k
  if (fdp.ConsumeBool() && !out.empty()) {
208
1.01k
    std::string mid = fdp.ConsumeRandomLengthString(16);
209
1.01k
    size_t pos = fdp.ConsumeIntegralInRange<size_t>(0, out.size());
210
1.01k
    out.insert(pos, mid);
211
1.01k
  }
212
21.1k
  return out;
213
21.1k
}
214
215
28.7k
static void check_href_size_invariant(const ada::url& u) {
216
28.7k
  if (u.get_href_size() != u.get_href().size()) {
217
0
    printf("get_href_size mismatch (url): size=%zu href.size=%zu href=%s\n",
218
0
           u.get_href_size(), u.get_href().size(), u.get_href().c_str());
219
0
    abort();
220
0
  }
221
28.7k
}
222
223
28.7k
static void check_href_size_invariant(const ada::url_aggregator& u) {
224
28.7k
  if (u.get_href_size() != u.get_href().size()) {
225
0
    printf(
226
0
        "get_href_size mismatch (aggregator): size=%zu href.size=%zu href=%s\n",
227
0
        u.get_href_size(), u.get_href().size(),
228
0
        std::string(u.get_href()).c_str());
229
0
    abort();
230
0
  }
231
28.7k
}
232
233
21.1k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
234
21.1k
  FuzzedDataProvider fdp(data, size);
235
21.1k
  std::string source = fdp.ConsumeRandomLengthString(256);
236
21.1k
  std::string base = fdp.ConsumeRandomLengthString(256);
237
238
  // volatile forces the compiler to store the results without undue
239
  // optimizations
240
21.1k
  volatile size_t length = 0;
241
242
21.1k
  std::string simple_abs = make_simple_absolute_candidate(fdp);
243
21.1k
  {
244
21.1k
    auto su = ada::parse<ada::url>(simple_abs);
245
21.1k
    auto sa = ada::parse<ada::url_aggregator>(simple_abs);
246
21.1k
    if (su.has_value() ^ sa.has_value()) {
247
0
      printf("simple_abs parse agreement fail: %s\n", simple_abs.c_str());
248
0
      abort();
249
0
    }
250
21.1k
    if (su) {
251
20.9k
      if (su->get_href() != std::string(sa->get_href())) {
252
0
        printf("simple_abs href mismatch:\n  in=%s\n  url=%s\n  agg=%s\n",
253
0
               simple_abs.c_str(), su->get_href().c_str(),
254
0
               std::string(sa->get_href()).c_str());
255
0
        abort();
256
0
      }
257
20.9k
      check_href_size_invariant(*su);
258
20.9k
      check_href_size_invariant(*sa);
259
20.9k
      length += su->get_href().size();
260
20.9k
    }
261
21.1k
  }
262
263
21.1k
  auto parse_url = ada::parse<ada::url>(source);
264
21.1k
  auto parse_url_aggregator = ada::parse<ada::url_aggregator>(source);
265
266
21.1k
  if (is_valid_utf8_string(source.data(), source.length())) {
267
17.0k
    if (parse_url.has_value() ^ parse_url_aggregator.has_value()) {
268
0
      printf("Source used to parse: %s", source.c_str());
269
0
      abort();
270
0
    }
271
17.0k
  }
272
273
21.1k
  if (parse_url) {
274
7.84k
    length += parse_url->get_href().size();
275
7.84k
    length += parse_url->get_origin().size();
276
7.84k
    check_href_size_invariant(*parse_url);
277
7.84k
  }
278
279
21.1k
  if (parse_url_aggregator) {
280
7.84k
    length += parse_url_aggregator->get_href().size();
281
7.84k
    length += parse_url_aggregator->get_origin().size();
282
7.84k
    check_href_size_invariant(*parse_url_aggregator);
283
284
7.84k
    volatile bool is_parse_url_aggregator_output_valid = false;
285
7.84k
    is_parse_url_aggregator_output_valid = parse_url_aggregator->validate();
286
287
7.84k
    assert(parse_url->get_protocol() == parse_url_aggregator->get_protocol());
288
7.84k
    assert(parse_url->get_href() == parse_url_aggregator->get_href());
289
7.84k
    assert(std::string(parse_url->get_hostname()) ==
290
7.84k
           std::string(parse_url_aggregator->get_hostname()));
291
7.84k
    assert(std::string(parse_url->get_pathname()) ==
292
7.84k
           std::string(parse_url_aggregator->get_pathname()));
293
7.84k
    assert(std::string(parse_url->get_search()) ==
294
7.84k
           std::string(parse_url_aggregator->get_search()));
295
7.84k
    assert(std::string(parse_url->get_hash()) ==
296
7.84k
           std::string(parse_url_aggregator->get_hash()));
297
7.84k
    assert(std::string(parse_url->get_port()) ==
298
7.84k
           std::string(parse_url_aggregator->get_port()));
299
7.84k
    assert(parse_url->get_username() ==
300
7.84k
           std::string(parse_url_aggregator->get_username()));
301
7.84k
    assert(parse_url->get_password() ==
302
7.84k
           std::string(parse_url_aggregator->get_password()));
303
7.84k
    assert(std::string(parse_url->get_host()) ==
304
7.84k
           std::string(parse_url_aggregator->get_host()));
305
306
    // Exercise all predicates on both types
307
7.84k
    exercise_url_predicates(*parse_url);
308
7.84k
    exercise_aggregator_predicates(*parse_url_aggregator);
309
310
    // Test set_href consistency
311
7.84k
    parse_url->set_href(source);
312
7.84k
    parse_url_aggregator->set_href(source);
313
7.84k
    assert(parse_url->get_href() == parse_url_aggregator->get_href());
314
7.84k
  }
315
316
  /**
317
   * Test copy and move semantics
318
   */
319
21.1k
  if (parse_url) {
320
    // Copy constructor
321
7.84k
    ada::url copied_url = *parse_url;
322
7.84k
    assert(copied_url.get_href() == parse_url->get_href());
323
324
    // Copy assignment
325
7.84k
    ada::url assigned_url;
326
7.84k
    assigned_url = *parse_url;
327
7.84k
    assert(assigned_url.get_href() == parse_url->get_href());
328
329
    // Move constructor
330
7.84k
    ada::url moved_url = std::move(copied_url);
331
7.84k
    assert(moved_url.get_href() == parse_url->get_href());
332
7.84k
  }
333
334
21.1k
  if (parse_url_aggregator) {
335
    // Copy constructor
336
7.84k
    ada::url_aggregator copied_agg = *parse_url_aggregator;
337
7.84k
    assert(std::string(copied_agg.get_href()) ==
338
7.84k
           std::string(parse_url_aggregator->get_href()));
339
340
    // Copy assignment
341
7.84k
    ada::url_aggregator assigned_agg;
342
7.84k
    assigned_agg = *parse_url_aggregator;
343
7.84k
    assert(std::string(assigned_agg.get_href()) ==
344
7.84k
           std::string(parse_url_aggregator->get_href()));
345
346
    // Move constructor
347
7.84k
    ada::url_aggregator moved_agg = std::move(copied_agg);
348
7.84k
    assert(std::string(moved_agg.get_href()) ==
349
7.84k
           std::string(parse_url_aggregator->get_href()));
350
351
    // Move assignment
352
7.84k
    ada::url_aggregator move_assigned_agg;
353
7.84k
    move_assigned_agg = std::move(assigned_agg);
354
7.84k
    assert(std::string(move_assigned_agg.get_href()) ==
355
7.84k
           std::string(parse_url_aggregator->get_href()));
356
7.84k
  }
357
358
  /**
359
   * ada::parse<ada::url>
360
   */
361
21.1k
  auto out_url = ada::parse<ada::url>("https://www.ada-url.com");
362
363
21.1k
  if (out_url) {
364
21.1k
    out_url->set_protocol(source);
365
21.1k
    out_url->set_username(source);
366
21.1k
    out_url->set_password(source);
367
21.1k
    out_url->set_hostname(source);
368
21.1k
    out_url->set_host(source);
369
21.1k
    out_url->set_pathname(source);
370
21.1k
    out_url->set_search(source);
371
21.1k
    out_url->set_hash(source);
372
21.1k
    out_url->set_port(source);
373
374
    // getters
375
21.1k
    length += out_url->get_protocol().size();
376
21.1k
    length += out_url->get_username().size();
377
21.1k
    length += out_url->get_password().size();
378
21.1k
    length += out_url->get_hostname().size();
379
21.1k
    length += out_url->get_host().size();
380
21.1k
    length += out_url->get_pathname().size();
381
21.1k
    length += out_url->get_search().size();
382
21.1k
    length += out_url->get_hash().size();
383
21.1k
    length += out_url->get_origin().size();
384
21.1k
    length += out_url->get_port().size();
385
21.1k
    length += out_url->get_pathname_length();
386
387
21.1k
    length += out_url->to_string().size();
388
389
    // boolean predicates after setters
390
21.1k
    (void)out_url->has_valid_domain();
391
21.1k
    (void)out_url->has_credentials();
392
21.1k
    (void)out_url->has_empty_hostname();
393
21.1k
    (void)out_url->has_hostname();
394
21.1k
    (void)out_url->has_port();
395
21.1k
    (void)out_url->has_hash();
396
21.1k
    (void)out_url->has_search();
397
21.1k
    (void)out_url->get_components();
398
21.1k
  }
399
400
  /**
401
   * ada::parse<ada::url_aggregator>
402
   */
403
21.1k
  auto out_aggregator =
404
21.1k
      ada::parse<ada::url_aggregator>("https://www.ada-url.com");
405
406
21.1k
  if (out_aggregator) {
407
21.1k
    out_aggregator->set_protocol(source);
408
21.1k
    out_aggregator->set_username(source);
409
21.1k
    out_aggregator->set_password(source);
410
21.1k
    out_aggregator->set_hostname(source);
411
21.1k
    out_aggregator->set_host(source);
412
21.1k
    out_aggregator->set_pathname(source);
413
21.1k
    out_aggregator->set_search(source);
414
21.1k
    out_aggregator->set_hash(source);
415
21.1k
    out_aggregator->set_port(source);
416
417
    // getters
418
21.1k
    length += out_aggregator->get_protocol().size();
419
21.1k
    length += out_aggregator->get_username().size();
420
21.1k
    length += out_aggregator->get_password().size();
421
21.1k
    length += out_aggregator->get_hostname().size();
422
21.1k
    length += out_aggregator->get_host().size();
423
21.1k
    length += out_aggregator->get_pathname().size();
424
21.1k
    length += out_aggregator->get_search().size();
425
21.1k
    length += out_aggregator->get_hash().size();
426
21.1k
    length += out_aggregator->get_origin().size();
427
21.1k
    length += out_aggregator->get_port().size();
428
21.1k
    length += out_aggregator->get_pathname_length();
429
430
21.1k
    length += out_aggregator->to_string().size();
431
432
21.1k
    volatile bool is_output_valid = false;
433
21.1k
    is_output_valid = out_aggregator->validate();
434
435
21.1k
    (void)out_aggregator->to_diagram();
436
437
    // boolean predicates after setters
438
21.1k
    (void)out_aggregator->has_valid_domain();
439
21.1k
    (void)out_aggregator->has_credentials();
440
21.1k
    (void)out_aggregator->has_empty_hostname();
441
21.1k
    (void)out_aggregator->has_hostname();
442
21.1k
    (void)out_aggregator->has_non_empty_username();
443
21.1k
    (void)out_aggregator->has_non_empty_password();
444
21.1k
    (void)out_aggregator->has_password();
445
21.1k
    (void)out_aggregator->has_port();
446
21.1k
    (void)out_aggregator->has_hash();
447
21.1k
    (void)out_aggregator->has_search();
448
21.1k
    (void)out_aggregator->get_components();
449
450
    // clear methods + postcondition assertions
451
21.1k
    out_aggregator->clear_port();
452
21.1k
    if (out_aggregator->has_port()) {
453
0
      printf("clear_port() did not clear has_port()\n");
454
0
      abort();
455
0
    }
456
21.1k
    if (!out_aggregator->get_port().empty()) {
457
0
      printf("clear_port() left non-empty get_port()\n");
458
0
      abort();
459
0
    }
460
461
21.1k
    out_aggregator->clear_search();
462
21.1k
    if (out_aggregator->has_search()) {
463
0
      printf("clear_search() did not clear has_search()\n");
464
0
      abort();
465
0
    }
466
21.1k
    if (!out_aggregator->get_search().empty()) {
467
0
      printf("clear_search() left non-empty get_search()\n");
468
0
      abort();
469
0
    }
470
471
21.1k
    out_aggregator->clear_hash();
472
21.1k
    if (out_aggregator->has_hash()) {
473
0
      printf("clear_hash() did not clear has_hash()\n");
474
0
      abort();
475
0
    }
476
21.1k
    if (!out_aggregator->get_hash().empty()) {
477
0
      printf("clear_hash() left non-empty get_hash()\n");
478
0
      abort();
479
0
    }
480
21.1k
  }
481
482
  /**
483
   * Relative URL parsing with base (tests the base URL resolution code path)
484
   */
485
21.1k
  auto base_url = ada::parse<ada::url>(base);
486
21.1k
  auto base_agg = ada::parse<ada::url_aggregator>(base);
487
488
21.1k
  if (base_url) {
489
5.72k
    auto result = ada::parse<ada::url>(source, &*base_url);
490
5.72k
    if (result) {
491
5.22k
      length += result->get_href().size();
492
5.22k
      length += result->get_origin().size();
493
5.22k
      exercise_url_predicates(*result);
494
5.22k
    }
495
5.72k
  }
496
497
21.1k
  if (base_agg) {
498
5.72k
    auto result = ada::parse<ada::url_aggregator>(source, &*base_agg);
499
5.72k
    if (result) {
500
5.22k
      length += result->get_href().size();
501
5.22k
      length += result->get_origin().size();
502
5.22k
      exercise_aggregator_predicates(*result);
503
5.22k
    }
504
5.72k
  }
505
506
  // Cross-type consistency: relative URL parsing with a base should agree
507
  // between url and url_aggregator representations for valid UTF-8 inputs.
508
21.1k
  if (is_valid_utf8_string(source.data(), source.length()) &&
509
17.0k
      is_valid_utf8_string(base.data(), base.length()) && base_url &&
510
4.08k
      base_agg) {
511
4.08k
    auto res_url = ada::parse<ada::url>(source, &*base_url);
512
4.08k
    auto res_agg = ada::parse<ada::url_aggregator>(source, &*base_agg);
513
4.08k
    if (res_url.has_value() ^ res_agg.has_value()) {
514
0
      printf("Relative parse inconsistency for source=%s base=%s\n",
515
0
             source.c_str(), base.c_str());
516
0
      abort();
517
0
    }
518
4.08k
    if (res_url && res_agg) {
519
3.61k
      if (res_url->get_href() != std::string(res_agg->get_href())) {
520
0
        printf("Relative parse href mismatch for source=%s base=%s\n",
521
0
               source.c_str(), base.c_str());
522
0
        abort();
523
0
      }
524
3.61k
    }
525
4.08k
  }
526
527
  /**
528
   * Chained relative URL resolution: parse source against base, then use the
529
   * result as the base for a second parse. Exercises multi-level inheritance.
530
   */
531
21.1k
  if (base_agg) {
532
5.72k
    auto level1 = ada::parse<ada::url_aggregator>(source, &*base_agg);
533
5.72k
    if (level1) {
534
5.22k
      std::string input2 = fdp.ConsumeRandomLengthString(128);
535
5.22k
      auto level2 = ada::parse<ada::url_aggregator>(input2, &*level1);
536
5.22k
      if (level2) {
537
4.00k
        length += level2->get_href().size();
538
4.00k
        volatile bool v = level2->validate();
539
4.00k
        (void)v;
540
4.00k
      }
541
5.22k
    }
542
5.72k
  }
543
544
  /**
545
   * Known-good base URL with fuzzed relative input. Using a fixed valid base
546
   * lets the fuzzer focus entropy entirely on the relative-input code paths
547
   * (path resolution, query/fragment inheritance, scheme-relative URLs, etc.)
548
   */
549
21.1k
  {
550
21.1k
    auto known_base =
551
21.1k
        ada::parse<ada::url_aggregator>("https://example.com/a/b/c?query#hash");
552
21.1k
    if (known_base) {
553
21.1k
      auto result = ada::parse<ada::url_aggregator>(source, &*known_base);
554
21.1k
      if (result) {
555
19.5k
        length += result->get_href().size();
556
19.5k
        exercise_aggregator_predicates(*result);
557
19.5k
      }
558
21.1k
    }
559
21.1k
  }
560
561
  /**
562
   * Node.js specific
563
   */
564
21.1k
  length += ada::href_from_file(source).size();
565
566
  /**
567
   * Others
568
   */
569
21.1k
  bool is_valid = ada::checkers::verify_dns_length(source);
570
571
21.1k
  (void)is_valid;
572
573
  /**
574
   * Sequential setter interactions with FDP-controlled ordering.
575
   *
576
   * The existing code calls every setter with the same `source` value in a
577
   * fixed order. Here we let the fuzzer choose an arbitrary sequence of
578
   * setter/value pairs, checking that url and url_aggregator stay in sync
579
   * after every step. This exercises setter-interaction state bugs that
580
   * fixed-order testing would miss.
581
   */
582
21.1k
  {
583
21.1k
    auto url_seq = ada::parse<ada::url>(
584
21.1k
        "https://user:pass@example.com:8080/path?query=1#hash");
585
21.1k
    auto agg_seq = ada::parse<ada::url_aggregator>(
586
21.1k
        "https://user:pass@example.com:8080/path?query=1#hash");
587
21.1k
    if (url_seq && agg_seq) {
588
21.1k
      int steps = fdp.ConsumeIntegralInRange(1, 8);
589
66.8k
      for (int i = 0; i < steps; ++i) {
590
45.6k
        std::string val = fdp.ConsumeRandomLengthString(64);
591
45.6k
        int which = fdp.ConsumeIntegralInRange(0, 8);
592
45.6k
        switch (which) {
593
29.2k
          case 0:
594
29.2k
            url_seq->set_protocol(val);
595
29.2k
            agg_seq->set_protocol(val);
596
29.2k
            break;
597
1.86k
          case 1:
598
1.86k
            url_seq->set_username(val);
599
1.86k
            agg_seq->set_username(val);
600
1.86k
            break;
601
2.07k
          case 2:
602
2.07k
            url_seq->set_password(val);
603
2.07k
            agg_seq->set_password(val);
604
2.07k
            break;
605
3.37k
          case 3:
606
3.37k
            url_seq->set_hostname(val);
607
3.37k
            agg_seq->set_hostname(val);
608
3.37k
            break;
609
4.40k
          case 4:
610
4.40k
            url_seq->set_host(val);
611
4.40k
            agg_seq->set_host(val);
612
4.40k
            break;
613
1.64k
          case 5:
614
1.64k
            url_seq->set_pathname(val);
615
1.64k
            agg_seq->set_pathname(val);
616
1.64k
            break;
617
736
          case 6:
618
736
            url_seq->set_search(val);
619
736
            agg_seq->set_search(val);
620
736
            break;
621
534
          case 7:
622
534
            url_seq->set_hash(val);
623
534
            agg_seq->set_hash(val);
624
534
            break;
625
1.79k
          case 8:
626
1.79k
            url_seq->set_port(val);
627
1.79k
            agg_seq->set_port(val);
628
1.79k
            break;
629
45.6k
        }
630
        // After every setter both representations must agree on href.
631
45.6k
        if (url_seq->get_href() != std::string(agg_seq->get_href())) {
632
0
          printf(
633
0
              "Sequential setter href mismatch after setter=%d val='%s'\n"
634
0
              "  url:  %s\n  agg:  %s\n",
635
0
              which, val.c_str(), url_seq->get_href().c_str(),
636
0
              std::string(agg_seq->get_href()).c_str());
637
0
          abort();
638
0
        }
639
        // url_aggregator internal invariant must still hold.
640
45.6k
        volatile bool v = agg_seq->validate();
641
45.6k
        (void)v;
642
45.6k
      }
643
21.1k
    }
644
21.1k
  }
645
646
  /**
647
   * Re-parse idempotency.
648
   *
649
   * If parse(source) succeeds, then parse(href) must also succeed and
650
   * produce the same href. Serialization and parsing must be consistent:
651
   * a normalized URL is always its own fixed point.
652
   */
653
21.1k
  if (parse_url_aggregator) {
654
7.84k
    std::string href1 = std::string(parse_url_aggregator->get_href());
655
7.84k
    auto reparsed = ada::parse<ada::url_aggregator>(href1);
656
7.84k
    if (!reparsed) {
657
0
      printf("Re-parse of href failed unexpectedly: '%s'\n", href1.c_str());
658
0
      abort();
659
0
    }
660
7.84k
    std::string href2 = std::string(reparsed->get_href());
661
7.84k
    if (href1 != href2) {
662
0
      printf(
663
0
          "Re-parse idempotency failure!\n"
664
0
          "  href1: %s\n  href2: %s\n",
665
0
          href1.c_str(), href2.c_str());
666
0
      abort();
667
0
    }
668
7.84k
  }
669
670
  /**
671
   * URL search params round-trip via URL integration.
672
   *
673
   * Construct a URL whose query is the fuzz source, extract the search
674
   * component as a url_search_params, mutate it, serialise it back, and
675
   * set it on the URL. Exercises the interaction between URL objects and
676
   * url_search_params and verifies that the combined pipeline doesn't crash.
677
   *
678
   * Also verifies the url_search_params serialisation idempotency property:
679
   *   url_search_params(sp.to_string()).to_string() == sp.to_string()
680
   */
681
21.1k
  {
682
21.1k
    std::string search_url = "https://example.com/?" + source;
683
21.1k
    auto url_with_search = ada::parse<ada::url_aggregator>(search_url);
684
21.1k
    if (url_with_search) {
685
      // Extract the search string (may include leading '?').
686
21.1k
      std::string search_raw = std::string(url_with_search->get_search());
687
21.1k
      std::string_view search_view = search_raw;
688
21.1k
      if (!search_view.empty() && search_view[0] == '?') {
689
17.1k
        search_view = search_view.substr(1);
690
17.1k
      }
691
692
21.1k
      ada::url_search_params sp(search_view);
693
694
      // Mutate with additional entries from the fuzz corpus.
695
21.1k
      sp.append(source, base);
696
697
21.1k
      std::string serialized = sp.to_string();
698
699
      // Idempotency: re-parsing the serialised form must yield the same string.
700
21.1k
      ada::url_search_params sp2(serialized);
701
21.1k
      std::string serialized2 = sp2.to_string();
702
21.1k
      if (serialized2 != serialized) {
703
0
        printf(
704
0
            "url_search_params serialisation not idempotent!\n"
705
0
            "  first:  %s\n  second: %s\n",
706
0
            serialized.c_str(), serialized2.c_str());
707
0
        abort();
708
0
      }
709
710
      // Set the serialised params back on the URL.
711
21.1k
      url_with_search->set_search(serialized);
712
21.1k
      volatile bool v = url_with_search->validate();
713
21.1k
      (void)v;
714
21.1k
    }
715
21.1k
  }
716
717
21.1k
  return 0;
718
21.1k
}  // extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {