Ada 3.2.1
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
11std::string bytesToAlphanumeric(const std::string& source) {
12 static const char alphanumeric[] =
13 "abcdefghijklmnopqrstuvwxyz"
14 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
15 "0123456789";
16
17 std::string result;
18 result.reserve(source.size());
19
20 for (char byte : source) {
21 int index = static_cast<unsigned char>(byte) % (sizeof(alphanumeric) - 1);
22 result.push_back(alphanumeric[index]);
23 }
24
25 return result;
26}
27
28void 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
41extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
42 FuzzedDataProvider fdp(data, size);
43 // We do not want to trigger arbitrary regex matching.
44 std::string source_1 =
45 "/" + bytesToAlphanumeric(fdp.ConsumeRandomLengthString(50)) + "/" +
46 bytesToAlphanumeric(fdp.ConsumeRandomLengthString(50));
47 std::string base_source_1 =
48 "/" + bytesToAlphanumeric(fdp.ConsumeRandomLengthString(50)) + "/" +
49 bytesToAlphanumeric(fdp.ConsumeRandomLengthString(50));
50
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 }
91
92 return 0;
93}
Includes all definitions for Ada.
void exercise_result(auto result)
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
std::string bytesToAlphanumeric(const std::string &source)
ada::url_pattern_regex::std_regex_provider regex_provider
Definition url_pattern.cc:9