]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-specifier.c
6bf312057a7b3db28abaee148ce9bd642083f704
[thirdparty/systemd.git] / src / test / test-specifier.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2017 Lennart Poettering
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 "alloc-util.h"
22 #include "log.h"
23 #include "specifier.h"
24 #include "string-util.h"
25 #include "strv.h"
26
27 static void test_specifier_escape_one(const char *a, const char *b) {
28 _cleanup_free_ char *x = NULL;
29
30 x = specifier_escape(a);
31 assert_se(streq_ptr(x, b));
32 }
33
34 static void test_specifier_escape(void) {
35 test_specifier_escape_one(NULL, NULL);
36 test_specifier_escape_one("", "");
37 test_specifier_escape_one("%", "%%");
38 test_specifier_escape_one("foo bar", "foo bar");
39 test_specifier_escape_one("foo%bar", "foo%%bar");
40 test_specifier_escape_one("%%%%%", "%%%%%%%%%%");
41 }
42
43 static void test_specifier_escape_strv_one(char **a, char **b) {
44 _cleanup_strv_free_ char **x = NULL;
45
46 assert_se(specifier_escape_strv(a, &x) >= 0);
47 assert_se(strv_equal(x, b));
48 }
49
50 static void test_specifier_escape_strv(void) {
51 test_specifier_escape_strv_one(NULL, NULL);
52 test_specifier_escape_strv_one(STRV_MAKE(NULL), STRV_MAKE(NULL));
53 test_specifier_escape_strv_one(STRV_MAKE(""), STRV_MAKE(""));
54 test_specifier_escape_strv_one(STRV_MAKE("foo"), STRV_MAKE("foo"));
55 test_specifier_escape_strv_one(STRV_MAKE("%"), STRV_MAKE("%%"));
56 test_specifier_escape_strv_one(STRV_MAKE("foo", "%", "foo%", "%foo", "foo%foo", "quux", "%%%"), STRV_MAKE("foo", "%%", "foo%%", "%%foo", "foo%%foo", "quux", "%%%%%%"));
57 }
58
59 int main(int argc, char *argv[]) {
60 log_set_max_level(LOG_DEBUG);
61
62 test_specifier_escape();
63 test_specifier_escape_strv();
64
65 return 0;
66 }