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