Ada 3.0.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
28extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
29 FuzzedDataProvider fdp(data, size);
30 // We do not want to trigger arbitrary regex matching.
31 std::string source =
32 "/" + bytesToAlphanumeric(fdp.ConsumeRandomLengthString(50)) + "/" +
33 bytesToAlphanumeric(fdp.ConsumeRandomLengthString(50));
34 std::string base_source =
35 "/" + bytesToAlphanumeric(fdp.ConsumeRandomLengthString(50)) + "/" +
36 bytesToAlphanumeric(fdp.ConsumeRandomLengthString(50));
37
38 // Without base or options
39 auto result =
40 ada::parse_url_pattern<regex_provider>(source, nullptr, nullptr);
41 (void)result;
42
43 // Testing with base_url
44 std::string_view base_source_view(base_source.data(), base_source.length());
45 auto result_with_base = ada::parse_url_pattern<regex_provider>(
46 source, &base_source_view, nullptr);
47 (void)result_with_base;
48
49 // Testing with base_url and options
50 ada::url_pattern_options options{.ignore_case = true};
51 auto result_with_base_and_options = ada::parse_url_pattern<regex_provider>(
52 source, &base_source_view, &options);
53 (void)result_with_base_and_options;
54
55 // Testing with url_pattern_init and base url.
56 ada::url_pattern_init init{.protocol = source,
57 .username = source,
58 .password = source,
59 .hostname = source,
60 .port = source,
61 .pathname = source,
62 .search = source,
63 .hash = source};
64 auto result_with_init =
65 ada::parse_url_pattern<regex_provider>(init, &base_source_view, nullptr);
66 (void)result_with_init;
67
68 return 0;
69}
Includes all definitions for Ada.
ada_warn_unused tl::expected< url_pattern< regex_provider >, errors > parse_url_pattern(std::variant< std::string_view, url_pattern_init > input, const std::string_view *base_url, const url_pattern_options *options)
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