Ada 3.3.0
Fast spec-compliant URL parser
Loading...
Searching...
No Matches
url_pattern.cc
Go to the documentation of this file.
1#include <fuzzer/FuzzedDataProvider.h>
2
3#include <memory>
4#include <string>
5
6#include "ada.cpp"
7#include "ada.h"
8
9using regex_provider = ada::url_pattern_regex::std_regex_provider;
10
11void exercise_result(auto result) {
12 (void)result.get_protocol();
13 (void)result.get_username();
14 (void)result.get_password();
15 (void)result.get_hostname();
16 (void)result.get_port();
17 (void)result.get_pathname();
18 (void)result.get_search();
19 (void)result.get_hash();
20 (void)result.ignore_case();
21 (void)result.has_regexp_groups();
22}
23
24extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
25 auto to_ascii = [](const std::string& source) -> std::string {
26 std::string result;
27 result.reserve(source.size());
28 for (char c : source) {
29 result.push_back(static_cast<unsigned char>(c) % 128);
30 }
31 return result;
32 };
33 FuzzedDataProvider fdp(data, size);
34 // We do not want to trigger arbitrary regex matching.
35 std::string source_1 = "/" + to_ascii(fdp.ConsumeRandomLengthString(50)) +
36 "/" + to_ascii(fdp.ConsumeRandomLengthString(50));
37 std::string base_source_1 = "/" +
38 to_ascii(fdp.ConsumeRandomLengthString(50)) +
39 "/" + to_ascii(fdp.ConsumeRandomLengthString(50));
40
41 std::string source_2 = "https://ada-url.com/*";
42 std::string base_source_2 = "https://ada-url.com";
43
44 std::array<std::pair<std::string, std::string>, 2> sources = {{
45 {source_1, base_source_1},
46 {source_2, base_source_2},
47 }};
48
49 for (const auto& [source, base_source] : sources) {
50 // Without base or options
51 auto result =
52 ada::parse_url_pattern<regex_provider>(source, nullptr, nullptr);
53 if (result) exercise_result(*result);
54
55 // Testing with base_url
56 std::string_view base_source_view(base_source.data(), base_source.length());
57 auto result_with_base = ada::parse_url_pattern<regex_provider>(
58 source, &base_source_view, nullptr);
59 if (result_with_base) exercise_result(*result_with_base);
60
61 // Testing with base_url and options
62 ada::url_pattern_options options{.ignore_case = fdp.ConsumeBool()};
63 auto result_with_base_and_options = ada::parse_url_pattern<regex_provider>(
64 source, &base_source_view, &options);
65 if (result_with_base_and_options)
66 exercise_result(*result_with_base_and_options);
67
68 // Testing with url_pattern_init and base url.
69 int field_index = fdp.ConsumeIntegralInRange(0, 7);
70 std::string random_value = to_ascii(fdp.ConsumeRandomLengthString(50));
71 ada::url_pattern_init init{};
72 switch (field_index) {
73 case 0:
74 init.protocol = random_value;
75 break;
76 case 1:
77 init.username = random_value;
78 break;
79 case 2:
80 init.password = random_value;
81 break;
82 case 3:
83 init.hostname = random_value;
84 break;
85 case 4:
86 init.port = random_value;
87 break;
88 case 5:
89 init.pathname = random_value;
90 break;
91 case 6:
92 init.search = random_value;
93 break;
94 case 7:
95 init.hash = random_value;
96 break;
97 }
98 auto result_with_init = ada::parse_url_pattern<regex_provider>(
99 init, &base_source_view, nullptr);
100 if (result_with_init) exercise_result(*result_with_init);
101 }
102
103 return 0;
104}
Includes all definitions for Ada.
void exercise_result(auto result)
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
ada::url_pattern_regex::std_regex_provider regex_provider
Definition url_pattern.cc:9