23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger This file is part of systemd.
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger Copyright 2010 Lennart Poettering
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger Copyright 2013 Thomas H.P. Andersen
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger systemd is free software; you can redistribute it and/or modify it
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger under the terms of the GNU Lesser General Public License as published by
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger the Free Software Foundation; either version 2.1 of the License, or
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger (at your option) any later version.
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger systemd is distributed in the hope that it will be useful, but
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger WITHOUT ANY WARRANTY; without even the implied warranty of
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger Lesser General Public License for more details.
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger You should have received a copy of the GNU Lesser General Public License
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger along with systemd; If not, see <http://www.gnu.org/licenses/>.
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburgerstatic void test_extract_first_word(void) {
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, NULL, 0) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, NULL, 0) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, NULL, 0) == 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, NULL, 0) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, NULL, 0) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, NULL, 0) == 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, NULL, EXTRACT_QUOTES) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, NULL, EXTRACT_QUOTES) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, NULL, 0) == 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, NULL, 0) == 1);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, NULL, EXTRACT_QUOTES) == -EINVAL);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, NULL, 0) == 1);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, NULL, EXTRACT_QUOTES) == -EINVAL);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, NULL, 0) == 1);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, NULL, EXTRACT_QUOTES) == -EINVAL);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_RELAX) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_RELAX) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, NULL, 0) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, NULL, EXTRACT_QUOTES) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, NULL, 0) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, NULL, 0) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger p = original = " f\\u00f6o \"pi\\U0001F4A9le\" ";
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_CUNESCAPE) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(streq(t, "pi\360\237\222\251le"));
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, NULL, EXTRACT_RELAX) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE_RELAX) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE_RELAX|EXTRACT_RELAX) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE|EXTRACT_CUNESCAPE_RELAX) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, NULL, 0) == -EINVAL);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_RELAX) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, ":", 0) == 1);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, ":", 0) == 1);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, ":", 0) == 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, ":", 0) == 1);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, ":", 0) == 1);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, ":", 0) == 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_CUNESCAPE_RELAX) == -EINVAL);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_CUNESCAPE_RELAX|EXTRACT_RELAX) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_CUNESCAPE|EXTRACT_CUNESCAPE_RELAX|EXTRACT_RELAX) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, NULL, EXTRACT_RELAX) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE_RELAX) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE_RELAX|EXTRACT_RELAX) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE) == -EINVAL);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE|EXTRACT_CUNESCAPE_RELAX) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE) == -EINVAL);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE|EXTRACT_CUNESCAPE_RELAX) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(streq(t, "\\w+@\\K[\\d.]+"));
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE|EXTRACT_CUNESCAPE_RELAX) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, NULL, EXTRACT_QUOTES) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, NULL, EXTRACT_QUOTES) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, ":", EXTRACT_DONT_COALESCE_SEPARATORS) == 1);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, ":", EXTRACT_DONT_COALESCE_SEPARATORS) == 1);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, ":", EXTRACT_DONT_COALESCE_SEPARATORS) == 1);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, ":", EXTRACT_DONT_COALESCE_SEPARATORS) == 1);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, ":", EXTRACT_DONT_COALESCE_SEPARATORS) == 1);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word(&p, &t, ":", EXTRACT_DONT_COALESCE_SEPARATORS) == 0);
c89f52ac6938374972253d8752ed65f3af0b3ef4Lennart Poettering p = "foo\\xbar";
c89f52ac6938374972253d8752ed65f3af0b3ef4Lennart Poettering assert_se(extract_first_word(&p, &t, NULL, 0) > 0);
c89f52ac6938374972253d8752ed65f3af0b3ef4Lennart Poettering p = "foo\\xbar";
c89f52ac6938374972253d8752ed65f3af0b3ef4Lennart Poettering assert_se(extract_first_word(&p, &t, NULL, EXTRACT_RETAIN_ESCAPE) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburgerstatic void test_extract_first_word_and_warn(void) {
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word_and_warn(&p, &t, NULL, 0, NULL, "fake", 1, original) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word_and_warn(&p, &t, NULL, 0, NULL, "fake", 1, original) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word_and_warn(&p, &t, NULL, 0, NULL, "fake", 1, original) == 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES, NULL, "fake", 1, original) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES, NULL, "fake", 1, original) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word_and_warn(&p, &t, NULL, 0, NULL, "fake", 1, original) == 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES, NULL, "fake", 1, original) == -EINVAL);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES, NULL, "fake", 1, original) == -EINVAL);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES, NULL, "fake", 1, original) == -EINVAL);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_RELAX, NULL, "fake", 1, original) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word_and_warn(&p, &t, NULL, 0, NULL, "fake", 1, original) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger p = original = " f\\u00f6o \"pi\\U0001F4A9le\" ";
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(streq(t, "pi\360\237\222\251le"));
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_RELAX, NULL, "fake", 1, original) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word_and_warn(&p, &t, NULL, 0, NULL, "fake", 1, original) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES, NULL, "fake", 1, original) == -EINVAL);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_RELAX, NULL, "fake", 1, original) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_CUNESCAPE, NULL, "fake", 1, original) == -EINVAL);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_CUNESCAPE|EXTRACT_RELAX, NULL, "fake", 1, original) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_RELAX, NULL, "fake", 1, original) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word_and_warn(&p, &t, NULL, 0, NULL, "fake", 1, original) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(streq(t, "\\w+@\\K[\\d.]+"));
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburgerstatic void test_extract_many_words(void) {
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger char *a, *b, *c;
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_many_words(&p, NULL, 0, &a, &b, &c, NULL) == 3);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_many_words(&p, NULL, 0, &a, &b, &c, NULL) == 2);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_many_words(&p, NULL, EXTRACT_QUOTES, &a, &b, &c, NULL) == 2);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_many_words(&p, NULL, 0, &a, &b, &c, NULL) == 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_many_words(&p, NULL, 0, &a, &b, &c, NULL) == 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_many_words(&p, NULL, 0, NULL) == 0);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_many_words(&p, NULL, 0, &a, NULL) == 1);
23a3c916179873d755b863d14b14c6e7f4d0f2deFilipe Brandenburger assert_se(extract_many_words(&p, NULL, 0, &a, NULL) == 1);