/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
Copyright 2013 Thomas H.P. Andersen
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <stdlib.h>
#include <string.h>
#include "extract-word.h"
#include "log.h"
#include "string-util.h"
static void test_extract_first_word(void) {
const char *p, *original;
char *t;
p = original = "foobar waldo";
free(t);
free(t);
assert_se(!t);
p = original = "\"foobar\" \'waldo\'";
free(t);
free(t);
assert_se(!t);
p = original = "\"foobar\" \'waldo\'";
free(t);
free(t);
assert_se(!t);
p = original = "\"";
free(t);
p = original = "\"";
p = original = "\'";
free(t);
p = original = "\'";
p = original = "\'fooo";
free(t);
p = original = "\'fooo";
p = original = "\'fooo";
free(t);
p = original = "\"fooo";
free(t);
p = original = "yay\'foo\'bar";
free(t);
p = original = "yay\'foo\'bar";
free(t);
p = original = " foobar ";
free(t);
p = original = " foo\\ba\\x6ar ";
free(t);
p = original = " foo\\ba\\x6ar ";
free(t);
p = original = " f\\u00f6o \"pi\\U0001F4A9le\" ";
free(t);
free(t);
p = original = "fooo\\";
free(t);
p = original = "fooo\\";
free(t);
p = original = "fooo\\";
free(t);
p = original = "fooo\\";
free(t);
p = original = "\"foo\\";
p = original = "\"foo\\";
free(t);
p = original = "foo::bar";
free(t);
free(t);
assert_se(!t);
p = original = "foo\\:bar::waldo";
free(t);
free(t);
assert_se(!t);
p = original = "\"foo\\";
p = original = "\"foo\\";
assert_se(extract_first_word(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_CUNESCAPE_RELAX|EXTRACT_RELAX) > 0);
free(t);
p = original = "\"foo\\";
assert_se(extract_first_word(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_CUNESCAPE|EXTRACT_CUNESCAPE_RELAX|EXTRACT_RELAX) > 0);
free(t);
p = original = "fooo\\ bar quux";
free(t);
p = original = "fooo\\ bar quux";
free(t);
p = original = "fooo\\ bar quux";
free(t);
p = original = "fooo\\ bar quux";
p = original = "fooo\\ bar quux";
free(t);
p = original = "\\w+@\\K[\\d.]+";
p = original = "\\w+@\\K[\\d.]+";
free(t);
p = original = "\\w+\\b";
free(t);
p = original = "-N ''";
free(t);
free(t);
p = original = ":foo\\:bar::waldo:";
assert_se(t);
free(t);
free(t);
assert_se(t);
free(t);
free(t);
free(t);
assert_se(!t);
assert_se(!p);
p = "foo\\xbar";
free(t);
p = "foo\\xbar";
free(t);
}
static void test_extract_first_word_and_warn(void) {
const char *p, *original;
char *t;
p = original = "foobar waldo";
free(t);
free(t);
assert_se(!t);
p = original = "\"foobar\" \'waldo\'";
assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES, NULL, "fake", 1, original) > 0);
free(t);
assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES, NULL, "fake", 1, original) > 0);
free(t);
assert_se(!t);
p = original = "\"";
assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES, NULL, "fake", 1, original) == -EINVAL);
p = original = "\'";
assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES, NULL, "fake", 1, original) == -EINVAL);
p = original = "\'fooo";
assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES, NULL, "fake", 1, original) == -EINVAL);
p = original = "\'fooo";
assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_RELAX, NULL, "fake", 1, original) > 0);
free(t);
p = original = " foo\\ba\\x6ar ";
assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
free(t);
p = original = " foo\\ba\\x6ar ";
free(t);
p = original = " f\\u00f6o \"pi\\U0001F4A9le\" ";
assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
free(t);
assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
free(t);
p = original = "fooo\\";
free(t);
p = original = "fooo\\";
free(t);
p = original = "fooo\\";
assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
free(t);
p = original = "\"foo\\";
assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES, NULL, "fake", 1, original) == -EINVAL);
p = original = "\"foo\\";
assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_RELAX, NULL, "fake", 1, original) > 0);
free(t);
p = original = "\"foo\\";
assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_CUNESCAPE, NULL, "fake", 1, original) == -EINVAL);
p = original = "\"foo\\";
assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_CUNESCAPE|EXTRACT_RELAX, NULL, "fake", 1, original) > 0);
free(t);
p = original = "fooo\\ bar quux";
free(t);
p = original = "fooo\\ bar quux";
free(t);
p = original = "fooo\\ bar quux";
assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
free(t);
p = original = "\\w+@\\K[\\d.]+";
assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
free(t);
p = original = "\\w+\\b";
assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
free(t);
}
static void test_extract_many_words(void) {
const char *p, *original;
char *a, *b, *c;
p = original = "foobar waldi piep";
free(a);
free(b);
free(c);
p = original = "'foobar' wa\"ld\"i ";
free(a);
free(b);
p = original = "'foobar' wa\"ld\"i ";
free(a);
free(b);
p = original = "";
p = original = " ";
p = original = "foobar";
p = original = "foobar waldi";
free(a);
p = original = " foobar ";
free(a);
}
log_open();
return 0;
}