Ada 3.4.4
Fast spec-compliant URL parser
Loading...
Searching...
No Matches
url_search_params.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
9extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
10 FuzzedDataProvider fdp(data, size);
11 std::string source = fdp.ConsumeRandomLengthString(256);
12 std::string base_source = fdp.ConsumeRandomLengthString(256);
13 std::string key3 = fdp.ConsumeRandomLengthString(64);
14 std::string value3 = fdp.ConsumeRandomLengthString(64);
15
19 volatile size_t length = 0;
20
21 auto base_source_view =
22 std::string_view(base_source.data(), base_source.length());
23
24 // Test constructor with initial value
25 auto initialized = ada::url_search_params(base_source_view);
26 length += initialized.size();
27 initialized.to_string();
28
29 // Test get() and get_all() on initialized params
30 {
31 auto val = initialized.get(source);
32 if (val.has_value()) {
33 length += val->size();
34 }
35 auto all_vals = initialized.get_all(source);
36 for (const auto &v : all_vals) {
37 length += v.size();
38 }
39 }
40
41 // Test sort() on initialized params
42 initialized.sort();
43
44 // Test C++ range-for iteration
45 for (const auto &pair : initialized) {
46 length += pair.first.size();
47 length += pair.second.size();
48 }
49
50 // Test index-based access
51 if (initialized.size() > 0) {
52 auto front = initialized.front();
53 length += front.first.size();
54 auto back = initialized.back();
55 length += back.first.size();
56 auto first = initialized[0];
57 length += first.first.size();
58 }
59
60 // Test default-constructed params with various mutations
61 auto search_params = ada::url_search_params();
62 search_params.append(source, base_source);
63 search_params.append(key3, value3);
64 search_params.set(source, base_source);
65 search_params.to_string();
66
67 // Test size()
68 length += search_params.size();
69
70 // Test has() and has(key, value) overloads
71 volatile bool has_key = search_params.has(base_source);
72 if (has_key) {
73 search_params.append(base_source, source);
74 }
75
76 // Test get() - returns first matching value
77 {
78 auto val = search_params.get(source);
79 if (val.has_value()) {
80 length += val->size();
81 }
82 }
83
84 // Test get_all() - returns all matching values
85 {
86 auto all_vals = search_params.get_all(source);
87 length += all_vals.size();
88 for (const auto &v : all_vals) {
89 length += v.size();
90 }
91 }
92
93 // Test remove(key) and remove(key, value) overloads
94 search_params.remove(source);
95 search_params.remove(source, base_source);
96
97 // Test has(key, value) after remove
98 if (search_params.has(base_source, source)) {
99 search_params.remove(base_source);
100 search_params.remove(base_source, source);
101 }
102
103 // Append more pairs for iteration testing
104 search_params.append(key3, value3);
105 search_params.append(source, key3);
106 search_params.append(base_source, value3);
107
108 // Test sort()
109 search_params.sort();
110
111 // Test serialization after sort
112 std::string serialized = search_params.to_string();
113 length += serialized.size();
114
115 // Test JavaScript-style iterator: keys
116 auto keys = search_params.get_keys();
117 while (keys.has_next()) {
118 auto k = keys.next();
119 if (k.has_value()) {
120 length += k->size();
121 }
122 }
123
124 // Test JavaScript-style iterator: values
125 auto values = search_params.get_values();
126 while (values.has_next()) {
127 auto v = values.next();
128 if (v.has_value()) {
129 length += v->size();
130 }
131 }
132
133 // Test JavaScript-style iterator: entries
134 auto entries = search_params.get_entries();
135 while (entries.has_next()) {
136 auto e = entries.next();
137 if (e.has_value()) {
138 length += e->first.size();
139 length += e->second.size();
140 }
141 }
142
143 // Test C++ range-for on the mutated params
144 for (const auto &pair : search_params) {
145 length += pair.first.size();
146 length += pair.second.size();
147 }
148
149 // Test reset() - private method used by C API
150 std::string resetted_value = fdp.ConsumeRandomLengthString(256);
151 search_params.reset(resetted_value);
152 length += search_params.size();
153
154 // Test that reset() followed by iteration doesn't crash
155 for (const auto &pair : search_params) {
156 length += pair.first.size();
157 length += pair.second.size();
158 }
159
160 // Test get() after reset
161 {
162 auto val = search_params.get(source);
163 if (val.has_value()) {
164 length += val->size();
165 }
166 }
167
168 // Test copy constructor and copy assignment
169 ada::url_search_params copied = search_params;
170 length += copied.size();
171 ada::url_search_params assigned;
172 assigned = search_params;
173 length += assigned.size();
174
175 // Test move constructor and move assignment
176 ada::url_search_params move_constructed = std::move(copied);
177 length += move_constructed.size();
178 ada::url_search_params move_assigned;
179 move_assigned = std::move(assigned);
180 length += move_assigned.size();
181
182 return 0;
183}
Main header for the Ada URL parser library.
Class for parsing and manipulating URL query strings.
size_t size() const noexcept
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)