Line data Source code
1 : /* SPDX-License-Identifier: LGPL-2.1+ */
2 :
3 : #include <errno.h>
4 : #include <stdlib.h>
5 : #include <string.h>
6 :
7 : #include "extract-word.h"
8 : #include "log.h"
9 : #include "string-util.h"
10 :
11 1 : static void test_extract_first_word(void) {
12 : const char *p, *original;
13 : char *t;
14 :
15 1 : p = original = "foobar waldo";
16 1 : assert_se(extract_first_word(&p, &t, NULL, 0) > 0);
17 1 : assert_se(streq(t, "foobar"));
18 1 : free(t);
19 1 : assert_se(p == original + 7);
20 :
21 1 : assert_se(extract_first_word(&p, &t, NULL, 0) > 0);
22 1 : assert_se(streq(t, "waldo"));
23 1 : free(t);
24 1 : assert_se(isempty(p));
25 :
26 1 : assert_se(extract_first_word(&p, &t, NULL, 0) == 0);
27 1 : assert_se(!t);
28 1 : assert_se(isempty(p));
29 :
30 1 : p = original = "\"foobar\" \'waldo\'";
31 1 : assert_se(extract_first_word(&p, &t, NULL, 0) > 0);
32 1 : assert_se(streq(t, "\"foobar\""));
33 1 : free(t);
34 1 : assert_se(p == original + 9);
35 :
36 1 : assert_se(extract_first_word(&p, &t, NULL, 0) > 0);
37 1 : assert_se(streq(t, "\'waldo\'"));
38 1 : free(t);
39 1 : assert_se(isempty(p));
40 :
41 1 : assert_se(extract_first_word(&p, &t, NULL, 0) == 0);
42 1 : assert_se(!t);
43 1 : assert_se(isempty(p));
44 :
45 1 : p = original = "\"foobar\" \'waldo\'";
46 1 : assert_se(extract_first_word(&p, &t, NULL, EXTRACT_UNQUOTE) > 0);
47 1 : assert_se(streq(t, "foobar"));
48 1 : free(t);
49 1 : assert_se(p == original + 9);
50 :
51 1 : assert_se(extract_first_word(&p, &t, NULL, EXTRACT_UNQUOTE) > 0);
52 1 : assert_se(streq(t, "waldo"));
53 1 : free(t);
54 1 : assert_se(isempty(p));
55 :
56 1 : assert_se(extract_first_word(&p, &t, NULL, 0) == 0);
57 1 : assert_se(!t);
58 1 : assert_se(isempty(p));
59 :
60 1 : p = original = "\"";
61 1 : assert_se(extract_first_word(&p, &t, NULL, 0) == 1);
62 1 : assert_se(streq(t, "\""));
63 1 : free(t);
64 1 : assert_se(isempty(p));
65 :
66 1 : p = original = "\"";
67 1 : assert_se(extract_first_word(&p, &t, NULL, EXTRACT_UNQUOTE) == -EINVAL);
68 1 : assert_se(p == original + 1);
69 :
70 1 : p = original = "\'";
71 1 : assert_se(extract_first_word(&p, &t, NULL, 0) == 1);
72 1 : assert_se(streq(t, "\'"));
73 1 : free(t);
74 1 : assert_se(isempty(p));
75 :
76 1 : p = original = "\'";
77 1 : assert_se(extract_first_word(&p, &t, NULL, EXTRACT_UNQUOTE) == -EINVAL);
78 1 : assert_se(p == original + 1);
79 :
80 1 : p = original = "\'fooo";
81 1 : assert_se(extract_first_word(&p, &t, NULL, 0) == 1);
82 1 : assert_se(streq(t, "\'fooo"));
83 1 : free(t);
84 1 : assert_se(isempty(p));
85 :
86 1 : p = original = "KEY=val \"KEY2=val with space\" \"KEY3=val with \\\"quotation\\\"\"";
87 1 : assert_se(extract_first_word(&p, &t, NULL, EXTRACT_UNQUOTE) == 1);
88 1 : assert_se(streq(t, "KEY=val"));
89 1 : free(t);
90 1 : assert_se(extract_first_word(&p, &t, NULL, EXTRACT_UNQUOTE) == 1);
91 1 : assert_se(streq(t, "KEY2=val with space"));
92 1 : free(t);
93 1 : assert_se(extract_first_word(&p, &t, NULL, EXTRACT_UNQUOTE) == 1);
94 1 : assert_se(streq(t, "KEY3=val with \"quotation\""));
95 1 : free(t);
96 1 : assert_se(isempty(p));
97 :
98 1 : p = original = "KEY=val \"KEY2=val space\" \"KEY3=val with \\\"quotation\\\"\"";
99 1 : assert_se(extract_first_word(&p, &t, NULL, EXTRACT_RETAIN_ESCAPE) == 1);
100 1 : assert_se(streq(t, "KEY=val"));
101 1 : free(t);
102 1 : assert_se(extract_first_word(&p, &t, NULL, EXTRACT_RETAIN_ESCAPE) == 1);
103 1 : assert_se(streq(t, "\"KEY2=val"));
104 1 : free(t);
105 1 : assert_se(extract_first_word(&p, &t, NULL, EXTRACT_RETAIN_ESCAPE) == 1);
106 1 : assert_se(streq(t, "space\""));
107 1 : free(t);
108 1 : assert_se(startswith(p, "\"KEY3="));
109 :
110 1 : p = original = "\'fooo";
111 1 : assert_se(extract_first_word(&p, &t, NULL, EXTRACT_UNQUOTE) == -EINVAL);
112 1 : assert_se(p == original + 5);
113 :
114 1 : p = original = "\'fooo";
115 1 : assert_se(extract_first_word(&p, &t, NULL, EXTRACT_UNQUOTE|EXTRACT_RELAX) > 0);
116 1 : assert_se(streq(t, "fooo"));
117 1 : free(t);
118 1 : assert_se(isempty(p));
119 :
120 1 : p = original = "\"fooo";
121 1 : assert_se(extract_first_word(&p, &t, NULL, EXTRACT_UNQUOTE|EXTRACT_RELAX) > 0);
122 1 : assert_se(streq(t, "fooo"));
123 1 : free(t);
124 1 : assert_se(isempty(p));
125 :
126 1 : p = original = "yay\'foo\'bar";
127 1 : assert_se(extract_first_word(&p, &t, NULL, 0) > 0);
128 1 : assert_se(streq(t, "yay\'foo\'bar"));
129 1 : free(t);
130 1 : assert_se(isempty(p));
131 :
132 1 : p = original = "yay\'foo\'bar";
133 1 : assert_se(extract_first_word(&p, &t, NULL, EXTRACT_UNQUOTE) > 0);
134 1 : assert_se(streq(t, "yayfoobar"));
135 1 : free(t);
136 1 : assert_se(isempty(p));
137 :
138 1 : p = original = " foobar ";
139 1 : assert_se(extract_first_word(&p, &t, NULL, 0) > 0);
140 1 : assert_se(streq(t, "foobar"));
141 1 : free(t);
142 1 : assert_se(isempty(p));
143 :
144 1 : p = original = " foo\\ba\\x6ar ";
145 1 : assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE) > 0);
146 1 : assert_se(streq(t, "foo\ba\x6ar"));
147 1 : free(t);
148 1 : assert_se(isempty(p));
149 :
150 1 : p = original = " foo\\ba\\x6ar ";
151 1 : assert_se(extract_first_word(&p, &t, NULL, 0) > 0);
152 1 : assert_se(streq(t, "foobax6ar"));
153 1 : free(t);
154 1 : assert_se(isempty(p));
155 :
156 1 : p = original = " f\\u00f6o \"pi\\U0001F4A9le\" ";
157 1 : assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE) > 0);
158 1 : assert_se(streq(t, "föo"));
159 1 : free(t);
160 1 : assert_se(p == original + 13);
161 :
162 1 : assert_se(extract_first_word(&p, &t, NULL, EXTRACT_UNQUOTE|EXTRACT_CUNESCAPE) > 0);
163 1 : assert_se(streq(t, "pi\360\237\222\251le"));
164 1 : free(t);
165 1 : assert_se(isempty(p));
166 :
167 1 : p = original = "fooo\\";
168 1 : assert_se(extract_first_word(&p, &t, NULL, EXTRACT_RELAX) > 0);
169 1 : assert_se(streq(t, "fooo"));
170 1 : free(t);
171 1 : assert_se(isempty(p));
172 :
173 1 : p = original = "fooo\\";
174 1 : assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE_RELAX) > 0);
175 1 : assert_se(streq(t, "fooo\\"));
176 1 : free(t);
177 1 : assert_se(isempty(p));
178 :
179 1 : p = original = "fooo\\";
180 1 : assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE_RELAX|EXTRACT_RELAX) > 0);
181 1 : assert_se(streq(t, "fooo\\"));
182 1 : free(t);
183 1 : assert_se(isempty(p));
184 :
185 1 : p = original = "fooo\\";
186 1 : assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE|EXTRACT_CUNESCAPE_RELAX) > 0);
187 1 : assert_se(streq(t, "fooo\\"));
188 1 : free(t);
189 1 : assert_se(isempty(p));
190 :
191 1 : p = original = "\"foo\\";
192 1 : assert_se(extract_first_word(&p, &t, NULL, 0) == -EINVAL);
193 1 : assert_se(p == original + 5);
194 :
195 1 : p = original = "\"foo\\";
196 1 : assert_se(extract_first_word(&p, &t, NULL, EXTRACT_UNQUOTE|EXTRACT_RELAX) > 0);
197 1 : assert_se(streq(t, "foo"));
198 1 : free(t);
199 1 : assert_se(isempty(p));
200 :
201 1 : p = original = "foo::bar";
202 1 : assert_se(extract_first_word(&p, &t, ":", 0) == 1);
203 1 : assert_se(streq(t, "foo"));
204 1 : free(t);
205 1 : assert_se(p == original + 5);
206 :
207 1 : assert_se(extract_first_word(&p, &t, ":", 0) == 1);
208 1 : assert_se(streq(t, "bar"));
209 1 : free(t);
210 1 : assert_se(isempty(p));
211 :
212 1 : assert_se(extract_first_word(&p, &t, ":", 0) == 0);
213 1 : assert_se(!t);
214 1 : assert_se(isempty(p));
215 :
216 1 : p = original = "foo\\:bar::waldo";
217 1 : assert_se(extract_first_word(&p, &t, ":", 0) == 1);
218 1 : assert_se(streq(t, "foo:bar"));
219 1 : free(t);
220 1 : assert_se(p == original + 10);
221 :
222 1 : assert_se(extract_first_word(&p, &t, ":", 0) == 1);
223 1 : assert_se(streq(t, "waldo"));
224 1 : free(t);
225 1 : assert_se(isempty(p));
226 :
227 1 : assert_se(extract_first_word(&p, &t, ":", 0) == 0);
228 1 : assert_se(!t);
229 1 : assert_se(isempty(p));
230 :
231 1 : p = original = "\"foo\\";
232 1 : assert_se(extract_first_word(&p, &t, NULL, EXTRACT_UNQUOTE|EXTRACT_CUNESCAPE_RELAX) == -EINVAL);
233 1 : assert_se(p == original + 5);
234 :
235 1 : p = original = "\"foo\\";
236 1 : assert_se(extract_first_word(&p, &t, NULL, EXTRACT_UNQUOTE|EXTRACT_CUNESCAPE_RELAX|EXTRACT_RELAX) > 0);
237 1 : assert_se(streq(t, "foo\\"));
238 1 : free(t);
239 1 : assert_se(isempty(p));
240 :
241 1 : p = original = "\"foo\\";
242 1 : assert_se(extract_first_word(&p, &t, NULL, EXTRACT_UNQUOTE|EXTRACT_CUNESCAPE|EXTRACT_CUNESCAPE_RELAX|EXTRACT_RELAX) > 0);
243 1 : assert_se(streq(t, "foo\\"));
244 1 : free(t);
245 1 : assert_se(isempty(p));
246 :
247 1 : p = original = "fooo\\ bar quux";
248 1 : assert_se(extract_first_word(&p, &t, NULL, EXTRACT_RELAX) > 0);
249 1 : assert_se(streq(t, "fooo bar"));
250 1 : free(t);
251 1 : assert_se(p == original + 10);
252 :
253 1 : p = original = "fooo\\ bar quux";
254 1 : assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE_RELAX) > 0);
255 1 : assert_se(streq(t, "fooo bar"));
256 1 : free(t);
257 1 : assert_se(p == original + 10);
258 :
259 1 : p = original = "fooo\\ bar quux";
260 1 : assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE_RELAX|EXTRACT_RELAX) > 0);
261 1 : assert_se(streq(t, "fooo bar"));
262 1 : free(t);
263 1 : assert_se(p == original + 10);
264 :
265 1 : p = original = "fooo\\ bar quux";
266 1 : assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE) == -EINVAL);
267 1 : assert_se(p == original + 5);
268 :
269 1 : p = original = "fooo\\ bar quux";
270 1 : assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE|EXTRACT_CUNESCAPE_RELAX) > 0);
271 1 : assert_se(streq(t, "fooo\\ bar"));
272 1 : free(t);
273 1 : assert_se(p == original + 10);
274 :
275 1 : p = original = "\\w+@\\K[\\d.]+";
276 1 : assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE) == -EINVAL);
277 1 : assert_se(p == original + 1);
278 :
279 1 : p = original = "\\w+@\\K[\\d.]+";
280 1 : assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE|EXTRACT_CUNESCAPE_RELAX) > 0);
281 1 : assert_se(streq(t, "\\w+@\\K[\\d.]+"));
282 1 : free(t);
283 1 : assert_se(isempty(p));
284 :
285 1 : p = original = "\\w+\\b";
286 1 : assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE|EXTRACT_CUNESCAPE_RELAX) > 0);
287 1 : assert_se(streq(t, "\\w+\b"));
288 1 : free(t);
289 1 : assert_se(isempty(p));
290 :
291 1 : p = original = "-N ''";
292 1 : assert_se(extract_first_word(&p, &t, NULL, EXTRACT_UNQUOTE) > 0);
293 1 : assert_se(streq(t, "-N"));
294 1 : free(t);
295 1 : assert_se(p == original + 3);
296 :
297 1 : assert_se(extract_first_word(&p, &t, NULL, EXTRACT_UNQUOTE) > 0);
298 1 : assert_se(streq(t, ""));
299 1 : free(t);
300 1 : assert_se(isempty(p));
301 :
302 1 : p = original = ":foo\\:bar::waldo:";
303 1 : assert_se(extract_first_word(&p, &t, ":", EXTRACT_DONT_COALESCE_SEPARATORS) == 1);
304 1 : assert_se(t);
305 1 : assert_se(streq(t, ""));
306 1 : free(t);
307 1 : assert_se(p == original + 1);
308 :
309 1 : assert_se(extract_first_word(&p, &t, ":", EXTRACT_DONT_COALESCE_SEPARATORS) == 1);
310 1 : assert_se(streq(t, "foo:bar"));
311 1 : free(t);
312 1 : assert_se(p == original + 10);
313 :
314 1 : assert_se(extract_first_word(&p, &t, ":", EXTRACT_DONT_COALESCE_SEPARATORS) == 1);
315 1 : assert_se(t);
316 1 : assert_se(streq(t, ""));
317 1 : free(t);
318 1 : assert_se(p == original + 11);
319 :
320 1 : assert_se(extract_first_word(&p, &t, ":", EXTRACT_DONT_COALESCE_SEPARATORS) == 1);
321 1 : assert_se(streq(t, "waldo"));
322 1 : free(t);
323 1 : assert_se(p == original + 17);
324 :
325 1 : assert_se(extract_first_word(&p, &t, ":", EXTRACT_DONT_COALESCE_SEPARATORS) == 1);
326 1 : assert_se(streq(t, ""));
327 1 : free(t);
328 1 : assert_se(p == NULL);
329 :
330 1 : assert_se(extract_first_word(&p, &t, ":", EXTRACT_DONT_COALESCE_SEPARATORS) == 0);
331 1 : assert_se(!t);
332 1 : assert_se(!p);
333 :
334 1 : p = "foo\\xbar";
335 1 : assert_se(extract_first_word(&p, &t, NULL, 0) > 0);
336 1 : assert_se(streq(t, "fooxbar"));
337 1 : free(t);
338 1 : assert_se(p == NULL);
339 :
340 1 : p = "foo\\xbar";
341 1 : assert_se(extract_first_word(&p, &t, NULL, EXTRACT_RETAIN_ESCAPE) > 0);
342 1 : assert_se(streq(t, "foo\\xbar"));
343 1 : free(t);
344 1 : assert_se(p == NULL);
345 1 : }
346 :
347 1 : static void test_extract_first_word_and_warn(void) {
348 : const char *p, *original;
349 : char *t;
350 :
351 1 : p = original = "foobar waldo";
352 1 : assert_se(extract_first_word_and_warn(&p, &t, NULL, 0, NULL, "fake", 1, original) > 0);
353 1 : assert_se(streq(t, "foobar"));
354 1 : free(t);
355 1 : assert_se(p == original + 7);
356 :
357 1 : assert_se(extract_first_word_and_warn(&p, &t, NULL, 0, NULL, "fake", 1, original) > 0);
358 1 : assert_se(streq(t, "waldo"));
359 1 : free(t);
360 1 : assert_se(isempty(p));
361 :
362 1 : assert_se(extract_first_word_and_warn(&p, &t, NULL, 0, NULL, "fake", 1, original) == 0);
363 1 : assert_se(!t);
364 1 : assert_se(isempty(p));
365 :
366 1 : p = original = "\"foobar\" \'waldo\'";
367 1 : assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_UNQUOTE, NULL, "fake", 1, original) > 0);
368 1 : assert_se(streq(t, "foobar"));
369 1 : free(t);
370 1 : assert_se(p == original + 9);
371 :
372 1 : assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_UNQUOTE, NULL, "fake", 1, original) > 0);
373 1 : assert_se(streq(t, "waldo"));
374 1 : free(t);
375 1 : assert_se(isempty(p));
376 :
377 1 : assert_se(extract_first_word_and_warn(&p, &t, NULL, 0, NULL, "fake", 1, original) == 0);
378 1 : assert_se(!t);
379 1 : assert_se(isempty(p));
380 :
381 1 : p = original = "\"";
382 1 : assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_UNQUOTE, NULL, "fake", 1, original) == -EINVAL);
383 1 : assert_se(p == original + 1);
384 :
385 1 : p = original = "\'";
386 1 : assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_UNQUOTE, NULL, "fake", 1, original) == -EINVAL);
387 1 : assert_se(p == original + 1);
388 :
389 1 : p = original = "\'fooo";
390 1 : assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_UNQUOTE, NULL, "fake", 1, original) == -EINVAL);
391 1 : assert_se(p == original + 5);
392 :
393 1 : p = original = "\'fooo";
394 1 : assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_UNQUOTE|EXTRACT_RELAX, NULL, "fake", 1, original) > 0);
395 1 : assert_se(streq(t, "fooo"));
396 1 : free(t);
397 1 : assert_se(isempty(p));
398 :
399 1 : p = original = " foo\\ba\\x6ar ";
400 1 : assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
401 1 : assert_se(streq(t, "foo\ba\x6ar"));
402 1 : free(t);
403 1 : assert_se(isempty(p));
404 :
405 1 : p = original = " foo\\ba\\x6ar ";
406 1 : assert_se(extract_first_word_and_warn(&p, &t, NULL, 0, NULL, "fake", 1, original) > 0);
407 1 : assert_se(streq(t, "foobax6ar"));
408 1 : free(t);
409 1 : assert_se(isempty(p));
410 :
411 1 : p = original = " f\\u00f6o \"pi\\U0001F4A9le\" ";
412 1 : assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
413 1 : assert_se(streq(t, "föo"));
414 1 : free(t);
415 1 : assert_se(p == original + 13);
416 :
417 1 : assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_UNQUOTE|EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
418 1 : assert_se(streq(t, "pi\360\237\222\251le"));
419 1 : free(t);
420 1 : assert_se(isempty(p));
421 :
422 1 : p = original = "fooo\\";
423 1 : assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_RELAX, NULL, "fake", 1, original) > 0);
424 1 : assert_se(streq(t, "fooo"));
425 1 : free(t);
426 1 : assert_se(isempty(p));
427 :
428 1 : p = original = "fooo\\";
429 1 : assert_se(extract_first_word_and_warn(&p, &t, NULL, 0, NULL, "fake", 1, original) > 0);
430 1 : assert_se(streq(t, "fooo\\"));
431 1 : free(t);
432 1 : assert_se(isempty(p));
433 :
434 1 : p = original = "fooo\\";
435 1 : assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
436 1 : assert_se(streq(t, "fooo\\"));
437 1 : free(t);
438 1 : assert_se(isempty(p));
439 :
440 1 : p = original = "\"foo\\";
441 1 : assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_UNQUOTE, NULL, "fake", 1, original) == -EINVAL);
442 1 : assert_se(p == original + 5);
443 :
444 1 : p = original = "\"foo\\";
445 1 : assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_UNQUOTE|EXTRACT_RELAX, NULL, "fake", 1, original) > 0);
446 1 : assert_se(streq(t, "foo"));
447 1 : free(t);
448 1 : assert_se(isempty(p));
449 :
450 1 : p = original = "\"foo\\";
451 1 : assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_UNQUOTE|EXTRACT_CUNESCAPE, NULL, "fake", 1, original) == -EINVAL);
452 1 : assert_se(p == original + 5);
453 :
454 1 : p = original = "\"foo\\";
455 1 : assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_UNQUOTE|EXTRACT_CUNESCAPE|EXTRACT_RELAX, NULL, "fake", 1, original) > 0);
456 1 : assert_se(streq(t, "foo"));
457 1 : free(t);
458 1 : assert_se(isempty(p));
459 :
460 1 : p = original = "fooo\\ bar quux";
461 1 : assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_RELAX, NULL, "fake", 1, original) > 0);
462 1 : assert_se(streq(t, "fooo bar"));
463 1 : free(t);
464 1 : assert_se(p == original + 10);
465 :
466 1 : p = original = "fooo\\ bar quux";
467 1 : assert_se(extract_first_word_and_warn(&p, &t, NULL, 0, NULL, "fake", 1, original) > 0);
468 1 : assert_se(streq(t, "fooo bar"));
469 1 : free(t);
470 1 : assert_se(p == original + 10);
471 :
472 1 : p = original = "fooo\\ bar quux";
473 1 : assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
474 1 : assert_se(streq(t, "fooo\\ bar"));
475 1 : free(t);
476 1 : assert_se(p == original + 10);
477 :
478 1 : p = original = "\\w+@\\K[\\d.]+";
479 1 : assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
480 1 : assert_se(streq(t, "\\w+@\\K[\\d.]+"));
481 1 : free(t);
482 1 : assert_se(isempty(p));
483 :
484 1 : p = original = "\\w+\\b";
485 1 : assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
486 1 : assert_se(streq(t, "\\w+\b"));
487 1 : free(t);
488 1 : assert_se(isempty(p));
489 1 : }
490 :
491 1 : static void test_extract_many_words(void) {
492 : const char *p, *original;
493 : char *a, *b, *c;
494 :
495 1 : p = original = "foobar waldi piep";
496 1 : assert_se(extract_many_words(&p, NULL, 0, &a, &b, &c, NULL) == 3);
497 1 : assert_se(isempty(p));
498 1 : assert_se(streq_ptr(a, "foobar"));
499 1 : assert_se(streq_ptr(b, "waldi"));
500 1 : assert_se(streq_ptr(c, "piep"));
501 1 : free(a);
502 1 : free(b);
503 1 : free(c);
504 :
505 1 : p = original = "'foobar' wa\"ld\"i ";
506 1 : assert_se(extract_many_words(&p, NULL, 0, &a, &b, &c, NULL) == 2);
507 1 : assert_se(isempty(p));
508 1 : assert_se(streq_ptr(a, "'foobar'"));
509 1 : assert_se(streq_ptr(b, "wa\"ld\"i"));
510 1 : assert_se(streq_ptr(c, NULL));
511 1 : free(a);
512 1 : free(b);
513 :
514 1 : p = original = "'foobar' wa\"ld\"i ";
515 1 : assert_se(extract_many_words(&p, NULL, EXTRACT_UNQUOTE, &a, &b, &c, NULL) == 2);
516 1 : assert_se(isempty(p));
517 1 : assert_se(streq_ptr(a, "foobar"));
518 1 : assert_se(streq_ptr(b, "waldi"));
519 1 : assert_se(streq_ptr(c, NULL));
520 1 : free(a);
521 1 : free(b);
522 :
523 1 : p = original = "";
524 1 : assert_se(extract_many_words(&p, NULL, 0, &a, &b, &c, NULL) == 0);
525 1 : assert_se(isempty(p));
526 1 : assert_se(streq_ptr(a, NULL));
527 1 : assert_se(streq_ptr(b, NULL));
528 1 : assert_se(streq_ptr(c, NULL));
529 :
530 1 : p = original = " ";
531 1 : assert_se(extract_many_words(&p, NULL, 0, &a, &b, &c, NULL) == 0);
532 1 : assert_se(isempty(p));
533 1 : assert_se(streq_ptr(a, NULL));
534 1 : assert_se(streq_ptr(b, NULL));
535 1 : assert_se(streq_ptr(c, NULL));
536 :
537 1 : p = original = "foobar";
538 1 : assert_se(extract_many_words(&p, NULL, 0, NULL) == 0);
539 1 : assert_se(p == original);
540 :
541 1 : p = original = "foobar waldi";
542 1 : assert_se(extract_many_words(&p, NULL, 0, &a, NULL) == 1);
543 1 : assert_se(p == original+7);
544 1 : assert_se(streq_ptr(a, "foobar"));
545 1 : free(a);
546 :
547 1 : p = original = " foobar ";
548 1 : assert_se(extract_many_words(&p, NULL, 0, &a, NULL) == 1);
549 1 : assert_se(isempty(p));
550 1 : assert_se(streq_ptr(a, "foobar"));
551 1 : free(a);
552 1 : }
553 :
554 1 : int main(int argc, char *argv[]) {
555 1 : log_parse_environment();
556 1 : log_open();
557 :
558 1 : test_extract_first_word();
559 1 : test_extract_first_word_and_warn();
560 1 : test_extract_many_words();
561 :
562 1 : return 0;
563 : }
|