Ada 4.1.1
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; also verify has(k) and has(k,v) consistency.
45 for (const auto& pair : initialized) {
46 length += pair.first.size();
47 length += pair.second.size();
48
49 // Every key seen in iteration must be reported by has(key).
50 if (!initialized.has(pair.first)) {
51 printf(
52 "url_search_params: iteration yielded key '%s' but has(key) is "
53 "false\n",
54 std::string(pair.first).c_str());
55 abort();
56 }
57
58 // has(key, value) → has(key)
59 if (initialized.has(pair.first, pair.second) &&
60 !initialized.has(pair.first)) {
61 printf(
62 "url_search_params: has(key,value) is true but has(key) is false "
63 "for key '%s'\n",
64 std::string(pair.first).c_str());
65 abort();
66 }
67
68 // get(key) must return a value when the key exists.
69 auto got = initialized.get(pair.first);
70 if (!got.has_value()) {
71 printf(
72 "url_search_params: has(key) is true but get(key) returned nullopt "
73 "for key '%s'\n",
74 std::string(pair.first).c_str());
75 abort();
76 }
77 }
78
79 // Test index-based access
80 if (initialized.size() > 0) {
81 auto front = initialized.front();
82 length += front.first.size();
83 auto back = initialized.back();
84 length += back.first.size();
85 auto first = initialized[0];
86 length += first.first.size();
87 }
88
89 // Test default-constructed params with various mutations
90 auto search_params = ada::url_search_params();
91 search_params.append(source, base_source);
92 search_params.append(key3, value3);
93 search_params.set(source, base_source);
94 search_params.to_string();
95
96 // Test size()
97 length += search_params.size();
98
99 // Test has() and has(key, value) overloads
100 volatile bool has_key = search_params.has(base_source);
101 if (has_key) {
102 search_params.append(base_source, source);
103 }
104
105 // Test get() - returns first matching value
106 {
107 auto val = search_params.get(source);
108 if (val.has_value()) {
109 length += val->size();
110 }
111 }
112
113 // Test get_all() - returns all matching values
114 {
115 auto all_vals = search_params.get_all(source);
116 length += all_vals.size();
117 for (const auto& v : all_vals) {
118 length += v.size();
119 }
120 }
121
122 // Test remove(key) and remove(key, value) overloads
123 search_params.remove(source);
124 search_params.remove(source, base_source);
125
126 // Test has(key, value) after remove
127 if (search_params.has(base_source, source)) {
128 search_params.remove(base_source);
129 search_params.remove(base_source, source);
130 }
131
132 // Append more pairs for iteration testing
133 search_params.append(key3, value3);
134 search_params.append(source, key3);
135 search_params.append(base_source, value3);
136
137 // Test sort()
138 search_params.sort();
139
140 // Test serialization after sort
141 std::string serialized = search_params.to_string();
142 length += serialized.size();
143
144 // Test JavaScript-style iterator: keys
145 auto keys = search_params.get_keys();
146 while (keys.has_next()) {
147 auto k = keys.next();
148 if (k.has_value()) {
149 length += k->size();
150 }
151 }
152
153 // Test JavaScript-style iterator: values
154 auto values = search_params.get_values();
155 while (values.has_next()) {
156 auto v = values.next();
157 if (v.has_value()) {
158 length += v->size();
159 }
160 }
161
162 // Test JavaScript-style iterator: entries
163 auto entries = search_params.get_entries();
164 while (entries.has_next()) {
165 auto e = entries.next();
166 if (e.has_value()) {
167 length += e->first.size();
168 length += e->second.size();
169 }
170 }
171
172 // Test C++ range-for on the mutated params
173 for (const auto& pair : search_params) {
174 length += pair.first.size();
175 length += pair.second.size();
176 }
177
178 // Test reset() - private method used by C API
179 std::string resetted_value = fdp.ConsumeRandomLengthString(256);
180 search_params.reset(resetted_value);
181 length += search_params.size();
182
183 // Test that reset() followed by iteration doesn't crash
184 for (const auto& pair : search_params) {
185 length += pair.first.size();
186 length += pair.second.size();
187 }
188
189 // Test get() after reset
190 {
191 auto val = search_params.get(source);
192 if (val.has_value()) {
193 length += val->size();
194 }
195 }
196
197 // Test copy constructor and copy assignment
198 ada::url_search_params copied = search_params;
199 length += copied.size();
200 ada::url_search_params assigned;
201 assigned = search_params;
202 length += assigned.size();
203
204 // Test move constructor and move assignment
205 ada::url_search_params move_constructed = std::move(copied);
206 length += move_constructed.size();
207 ada::url_search_params move_assigned;
208 move_assigned = std::move(assigned);
209 length += move_assigned.size();
210
222
223 // Initialised params idempotency.
224 {
225 std::string s1 = initialized.to_string();
226 ada::url_search_params reparsed(s1);
227 std::string s2 = reparsed.to_string();
228 if (s1 != s2) {
229 printf(
230 "url_search_params serialisation not idempotent (initialized)!\n"
231 " first: %s\n second: %s\n",
232 s1.c_str(), s2.c_str());
233 abort();
234 }
235 }
236
237 // Mutated params idempotency.
238 {
239 std::string s1 = move_assigned.to_string();
240 ada::url_search_params reparsed(s1);
241 std::string s2 = reparsed.to_string();
242 if (s1 != s2) {
243 printf(
244 "url_search_params serialisation not idempotent (mutated)!\n"
245 " first: %s\n second: %s\n",
246 s1.c_str(), s2.c_str());
247 abort();
248 }
249 }
250
259 {
260 std::string url_str = "https://example.com/?" + source;
261 auto parsed_url = ada::parse<ada::url_aggregator>(url_str);
262 if (parsed_url) {
263 std::string search_raw = std::string(parsed_url->get_search());
264 std::string_view search_view = search_raw;
265 if (!search_view.empty() && search_view[0] == '?') {
266 search_view = search_view.substr(1);
267 }
268
269 ada::url_search_params sp_from_url(search_view);
270
271 // Size and iteration must not crash.
272 length += sp_from_url.size();
273 for (const auto& pair : sp_from_url) {
274 length += pair.first.size();
275 length += pair.second.size();
276 }
277
278 // Idempotency check.
279 std::string t1 = sp_from_url.to_string();
281 std::string t2 = sp2.to_string();
282 if (t1 != t2) {
283 printf(
284 "url_search_params via URL round-trip not idempotent!\n"
285 " first: %s\n second: %s\n",
286 t1.c_str(), t2.c_str());
287 abort();
288 }
289
290 // Set the serialised params back on the URL – must not crash.
291 parsed_url->set_search(t1);
292 volatile bool v = parsed_url->validate();
293 (void)v;
294 }
295 }
296
297 return 0;
298}
Main header for the Ada URL parser library.
ada_warn_unused ada::result< result_type > parse(std::string_view input, const result_type *base_url=nullptr)
Class for parsing and manipulating URL query strings.
std::string to_string() const
size_t size() const noexcept
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)