summaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2017-11-21 17:52:31 +0100
committerSven Eden <yamakuzure@gmx.net>2017-11-21 17:52:31 +0100
commitbadf96da81314a055c45c6186d1369bb96a7bab1 (patch)
treeb4eafea45ae4e635ba135837d6020819e46c2542 /src/test
parent4df5852ca4d6f901d54583dce3f470264c5256a5 (diff)
specifier: add helper for escaping '%' characters to avoid making them subject for expansion
This is ultimately just a wrapper around strreplace(), but it makes things a bit more self-descriptive.
Diffstat (limited to 'src/test')
-rw-r--r--src/test/meson.build4
-rw-r--r--src/test/test-specifier.c66
2 files changed, 70 insertions, 0 deletions
diff --git a/src/test/meson.build b/src/test/meson.build
index e6865a679..89d742a80 100644
--- a/src/test/meson.build
+++ b/src/test/meson.build
@@ -252,6 +252,10 @@ tests += [
[],
[]],
+ [['src/test/test-specifier.c'],
+ [],
+ []],
+
[['src/test/test-string-util.c'],
[],
[]],
diff --git a/src/test/test-specifier.c b/src/test/test-specifier.c
new file mode 100644
index 000000000..bec74c641
--- /dev/null
+++ b/src/test/test-specifier.c
@@ -0,0 +1,66 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
+/***
+ This file is part of elogind.
+
+ Copyright 2017 Lennart Poettering
+
+ elogind 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.
+
+ elogind 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 elogind; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include "alloc-util.h"
+#include "log.h"
+#include "specifier.h"
+#include "string-util.h"
+#include "strv.h"
+
+static void test_specifier_escape_one(const char *a, const char *b) {
+ _cleanup_free_ char *x = NULL;
+
+ x = specifier_escape(a);
+ assert_se(streq_ptr(x, b));
+}
+
+static void test_specifier_escape(void) {
+ test_specifier_escape_one(NULL, NULL);
+ test_specifier_escape_one("", "");
+ test_specifier_escape_one("%", "%%");
+ test_specifier_escape_one("foo bar", "foo bar");
+ test_specifier_escape_one("foo%bar", "foo%%bar");
+ test_specifier_escape_one("%%%%%", "%%%%%%%%%%");
+}
+
+static void test_specifier_escape_strv_one(char **a, char **b) {
+ _cleanup_strv_free_ char **x = NULL;
+
+ assert_se(specifier_escape_strv(a, &x) >= 0);
+ assert_se(strv_equal(x, b));
+}
+
+static void test_specifier_escape_strv(void) {
+ test_specifier_escape_strv_one(NULL, NULL);
+ test_specifier_escape_strv_one(STRV_MAKE(NULL), STRV_MAKE(NULL));
+ test_specifier_escape_strv_one(STRV_MAKE(""), STRV_MAKE(""));
+ test_specifier_escape_strv_one(STRV_MAKE("foo"), STRV_MAKE("foo"));
+ test_specifier_escape_strv_one(STRV_MAKE("%"), STRV_MAKE("%%"));
+ test_specifier_escape_strv_one(STRV_MAKE("foo", "%", "foo%", "%foo", "foo%foo", "quux", "%%%"), STRV_MAKE("foo", "%%", "foo%%", "%%foo", "foo%%foo", "quux", "%%%%%%"));
+}
+
+int main(int argc, char *argv[]) {
+ log_set_max_level(LOG_DEBUG);
+
+ test_specifier_escape();
+ test_specifier_escape_strv();
+
+ return 0;
+}