mirror of
https://github.com/systemd/systemd.git
synced 2025-03-31 14:50:15 +03:00
tests: move escape related tests to test-escape.c
This commit is contained in:
parent
ebde5cb261
commit
45e0b1f68c
1
.gitignore
vendored
1
.gitignore
vendored
@ -181,6 +181,7 @@
|
||||
/test-ellipsize
|
||||
/test-engine
|
||||
/test-env-replace
|
||||
/test-escape
|
||||
/test-event
|
||||
/test-execute
|
||||
/test-extract-word
|
||||
|
@ -1427,6 +1427,7 @@ tests += \
|
||||
test-ellipsize \
|
||||
test-util \
|
||||
test-hexdecoct \
|
||||
test-escape \
|
||||
test-string-util \
|
||||
test-extract-word \
|
||||
test-parse-util \
|
||||
@ -1762,6 +1763,12 @@ test_hexdecoct_SOURCES = \
|
||||
test_hexdecoct_LDADD = \
|
||||
libbasic.la
|
||||
|
||||
test_escape_SOURCES = \
|
||||
src/test/test-escape.c
|
||||
|
||||
test_escape_LDADD = \
|
||||
libbasic.la
|
||||
|
||||
test_string_util_SOURCES = \
|
||||
src/test/test-string-util.c
|
||||
|
||||
|
114
src/test/test-escape.c
Normal file
114
src/test/test-escape.c
Normal file
@ -0,0 +1,114 @@
|
||||
/***
|
||||
This file is part of systemd.
|
||||
|
||||
Copyright 2010 Lennart Poettering
|
||||
|
||||
systemd is free software; you can redistribute it and/or modify it
|
||||
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 "alloc-util.h"
|
||||
#include "escape.h"
|
||||
#include "macro.h"
|
||||
|
||||
static void test_cescape(void) {
|
||||
_cleanup_free_ char *escaped;
|
||||
|
||||
assert_se(escaped = cescape("abc\\\"\b\f\n\r\t\v\a\003\177\234\313"));
|
||||
assert_se(streq(escaped, "abc\\\\\\\"\\b\\f\\n\\r\\t\\v\\a\\003\\177\\234\\313"));
|
||||
}
|
||||
|
||||
static void test_cunescape(void) {
|
||||
_cleanup_free_ char *unescaped;
|
||||
|
||||
assert_se(cunescape("abc\\\\\\\"\\b\\f\\a\\n\\r\\t\\v\\003\\177\\234\\313\\000\\x00", 0, &unescaped) < 0);
|
||||
assert_se(cunescape("abc\\\\\\\"\\b\\f\\a\\n\\r\\t\\v\\003\\177\\234\\313\\000\\x00", UNESCAPE_RELAX, &unescaped) >= 0);
|
||||
assert_se(streq_ptr(unescaped, "abc\\\"\b\f\a\n\r\t\v\003\177\234\313\\000\\x00"));
|
||||
unescaped = mfree(unescaped);
|
||||
|
||||
/* incomplete sequences */
|
||||
assert_se(cunescape("\\x0", 0, &unescaped) < 0);
|
||||
assert_se(cunescape("\\x0", UNESCAPE_RELAX, &unescaped) >= 0);
|
||||
assert_se(streq_ptr(unescaped, "\\x0"));
|
||||
unescaped = mfree(unescaped);
|
||||
|
||||
assert_se(cunescape("\\x", 0, &unescaped) < 0);
|
||||
assert_se(cunescape("\\x", UNESCAPE_RELAX, &unescaped) >= 0);
|
||||
assert_se(streq_ptr(unescaped, "\\x"));
|
||||
unescaped = mfree(unescaped);
|
||||
|
||||
assert_se(cunescape("\\", 0, &unescaped) < 0);
|
||||
assert_se(cunescape("\\", UNESCAPE_RELAX, &unescaped) >= 0);
|
||||
assert_se(streq_ptr(unescaped, "\\"));
|
||||
unescaped = mfree(unescaped);
|
||||
|
||||
assert_se(cunescape("\\11", 0, &unescaped) < 0);
|
||||
assert_se(cunescape("\\11", UNESCAPE_RELAX, &unescaped) >= 0);
|
||||
assert_se(streq_ptr(unescaped, "\\11"));
|
||||
unescaped = mfree(unescaped);
|
||||
|
||||
assert_se(cunescape("\\1", 0, &unescaped) < 0);
|
||||
assert_se(cunescape("\\1", UNESCAPE_RELAX, &unescaped) >= 0);
|
||||
assert_se(streq_ptr(unescaped, "\\1"));
|
||||
unescaped = mfree(unescaped);
|
||||
|
||||
assert_se(cunescape("\\u0000", 0, &unescaped) < 0);
|
||||
assert_se(cunescape("\\u00DF\\U000000df\\u03a0\\U00000041", UNESCAPE_RELAX, &unescaped) >= 0);
|
||||
assert_se(streq_ptr(unescaped, "ßßΠA"));
|
||||
unescaped = mfree(unescaped);
|
||||
|
||||
assert_se(cunescape("\\073", 0, &unescaped) >= 0);
|
||||
assert_se(streq_ptr(unescaped, ";"));
|
||||
}
|
||||
|
||||
static void test_shell_escape_one(const char *s, const char *bad, const char *expected) {
|
||||
_cleanup_free_ char *r;
|
||||
|
||||
assert_se(r = shell_escape(s, bad));
|
||||
assert_se(streq_ptr(r, expected));
|
||||
}
|
||||
|
||||
static void test_shell_escape(void) {
|
||||
test_shell_escape_one("", "", "");
|
||||
test_shell_escape_one("\\", "", "\\\\");
|
||||
test_shell_escape_one("foobar", "", "foobar");
|
||||
test_shell_escape_one("foobar", "o", "f\\o\\obar");
|
||||
test_shell_escape_one("foo:bar,baz", ",:", "foo\\:bar\\,baz");
|
||||
}
|
||||
|
||||
static void test_shell_maybe_quote_one(const char *s, const char *expected) {
|
||||
_cleanup_free_ char *r;
|
||||
|
||||
assert_se(r = shell_maybe_quote(s));
|
||||
assert_se(streq(r, expected));
|
||||
}
|
||||
|
||||
static void test_shell_maybe_quote(void) {
|
||||
|
||||
test_shell_maybe_quote_one("", "");
|
||||
test_shell_maybe_quote_one("\\", "\"\\\\\"");
|
||||
test_shell_maybe_quote_one("\"", "\"\\\"\"");
|
||||
test_shell_maybe_quote_one("foobar", "foobar");
|
||||
test_shell_maybe_quote_one("foo bar", "\"foo bar\"");
|
||||
test_shell_maybe_quote_one("foo \"bar\" waldo", "\"foo \\\"bar\\\" waldo\"");
|
||||
test_shell_maybe_quote_one("foo$bar", "\"foo\\$bar\"");
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
test_cescape();
|
||||
test_cunescape();
|
||||
test_shell_escape();
|
||||
test_shell_maybe_quote();
|
||||
|
||||
return 0;
|
||||
}
|
@ -31,7 +31,6 @@
|
||||
#include "conf-parser.h"
|
||||
#include "cpu-set-util.h"
|
||||
#include "def.h"
|
||||
#include "escape.h"
|
||||
#include "fd-util.h"
|
||||
#include "fileio.h"
|
||||
#include "fs-util.h"
|
||||
@ -229,56 +228,6 @@ static void test_parse_uid(void) {
|
||||
assert_se(r == -EINVAL);
|
||||
}
|
||||
|
||||
static void test_cescape(void) {
|
||||
_cleanup_free_ char *escaped;
|
||||
|
||||
assert_se(escaped = cescape("abc\\\"\b\f\n\r\t\v\a\003\177\234\313"));
|
||||
assert_se(streq(escaped, "abc\\\\\\\"\\b\\f\\n\\r\\t\\v\\a\\003\\177\\234\\313"));
|
||||
}
|
||||
|
||||
static void test_cunescape(void) {
|
||||
_cleanup_free_ char *unescaped;
|
||||
|
||||
assert_se(cunescape("abc\\\\\\\"\\b\\f\\a\\n\\r\\t\\v\\003\\177\\234\\313\\000\\x00", 0, &unescaped) < 0);
|
||||
assert_se(cunescape("abc\\\\\\\"\\b\\f\\a\\n\\r\\t\\v\\003\\177\\234\\313\\000\\x00", UNESCAPE_RELAX, &unescaped) >= 0);
|
||||
assert_se(streq_ptr(unescaped, "abc\\\"\b\f\a\n\r\t\v\003\177\234\313\\000\\x00"));
|
||||
unescaped = mfree(unescaped);
|
||||
|
||||
/* incomplete sequences */
|
||||
assert_se(cunescape("\\x0", 0, &unescaped) < 0);
|
||||
assert_se(cunescape("\\x0", UNESCAPE_RELAX, &unescaped) >= 0);
|
||||
assert_se(streq_ptr(unescaped, "\\x0"));
|
||||
unescaped = mfree(unescaped);
|
||||
|
||||
assert_se(cunescape("\\x", 0, &unescaped) < 0);
|
||||
assert_se(cunescape("\\x", UNESCAPE_RELAX, &unescaped) >= 0);
|
||||
assert_se(streq_ptr(unescaped, "\\x"));
|
||||
unescaped = mfree(unescaped);
|
||||
|
||||
assert_se(cunescape("\\", 0, &unescaped) < 0);
|
||||
assert_se(cunescape("\\", UNESCAPE_RELAX, &unescaped) >= 0);
|
||||
assert_se(streq_ptr(unescaped, "\\"));
|
||||
unescaped = mfree(unescaped);
|
||||
|
||||
assert_se(cunescape("\\11", 0, &unescaped) < 0);
|
||||
assert_se(cunescape("\\11", UNESCAPE_RELAX, &unescaped) >= 0);
|
||||
assert_se(streq_ptr(unescaped, "\\11"));
|
||||
unescaped = mfree(unescaped);
|
||||
|
||||
assert_se(cunescape("\\1", 0, &unescaped) < 0);
|
||||
assert_se(cunescape("\\1", UNESCAPE_RELAX, &unescaped) >= 0);
|
||||
assert_se(streq_ptr(unescaped, "\\1"));
|
||||
unescaped = mfree(unescaped);
|
||||
|
||||
assert_se(cunescape("\\u0000", 0, &unescaped) < 0);
|
||||
assert_se(cunescape("\\u00DF\\U000000df\\u03a0\\U00000041", UNESCAPE_RELAX, &unescaped) >= 0);
|
||||
assert_se(streq_ptr(unescaped, "ßßΠA"));
|
||||
unescaped = mfree(unescaped);
|
||||
|
||||
assert_se(cunescape("\\073", 0, &unescaped) >= 0);
|
||||
assert_se(streq_ptr(unescaped, ";"));
|
||||
}
|
||||
|
||||
static void test_memdup_multiply(void) {
|
||||
int org[] = {1, 2, 3};
|
||||
int *dup;
|
||||
@ -924,39 +873,6 @@ static void test_sparse_write(void) {
|
||||
test_sparse_write_one(fd, test_e, sizeof(test_e));
|
||||
}
|
||||
|
||||
static void test_shell_escape_one(const char *s, const char *bad, const char *expected) {
|
||||
_cleanup_free_ char *r;
|
||||
|
||||
assert_se(r = shell_escape(s, bad));
|
||||
assert_se(streq_ptr(r, expected));
|
||||
}
|
||||
|
||||
static void test_shell_escape(void) {
|
||||
test_shell_escape_one("", "", "");
|
||||
test_shell_escape_one("\\", "", "\\\\");
|
||||
test_shell_escape_one("foobar", "", "foobar");
|
||||
test_shell_escape_one("foobar", "o", "f\\o\\obar");
|
||||
test_shell_escape_one("foo:bar,baz", ",:", "foo\\:bar\\,baz");
|
||||
}
|
||||
|
||||
static void test_shell_maybe_quote_one(const char *s, const char *expected) {
|
||||
_cleanup_free_ char *r;
|
||||
|
||||
assert_se(r = shell_maybe_quote(s));
|
||||
assert_se(streq(r, expected));
|
||||
}
|
||||
|
||||
static void test_shell_maybe_quote(void) {
|
||||
|
||||
test_shell_maybe_quote_one("", "");
|
||||
test_shell_maybe_quote_one("\\", "\"\\\\\"");
|
||||
test_shell_maybe_quote_one("\"", "\"\\\"\"");
|
||||
test_shell_maybe_quote_one("foobar", "foobar");
|
||||
test_shell_maybe_quote_one("foo bar", "\"foo bar\"");
|
||||
test_shell_maybe_quote_one("foo \"bar\" waldo", "\"foo \\\"bar\\\" waldo\"");
|
||||
test_shell_maybe_quote_one("foo$bar", "\"foo\\$bar\"");
|
||||
}
|
||||
|
||||
static void test_tempfn(void) {
|
||||
char *ret = NULL, *p;
|
||||
|
||||
@ -1042,8 +958,6 @@ int main(int argc, char *argv[]) {
|
||||
test_div_round_up();
|
||||
test_close_many();
|
||||
test_parse_uid();
|
||||
test_cescape();
|
||||
test_cunescape();
|
||||
test_memdup_multiply();
|
||||
test_u64log2();
|
||||
test_protect_errno();
|
||||
@ -1072,8 +986,6 @@ int main(int argc, char *argv[]) {
|
||||
test_same_fd();
|
||||
test_uid_ptr();
|
||||
test_sparse_write();
|
||||
test_shell_escape();
|
||||
test_shell_maybe_quote();
|
||||
test_tempfn();
|
||||
test_fgetxattrat_fake();
|
||||
test_runlevel_to_target();
|
||||
|
Loading…
x
Reference in New Issue
Block a user