]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/test/test-strv.c
tree-wide: drop license boilerplate
[thirdparty/systemd.git] / src / test / test-strv.c
index fc01dcfaf13ce5a834be1db1e0c63e968de974ba..fa658eb0566c9bf8a5526c84dd50ad9ac5fa6660 100644 (file)
@@ -1,26 +1,15 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd.
 
   Copyright 2010 Lennart Poettering
   Copyright 2013 Thomas H.P. Andersen
-
-  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 <string.h>
 
 #include "alloc-util.h"
+#include "escape.h"
 #include "specifier.h"
 #include "string-util.h"
 #include "strv.h"
@@ -54,6 +43,25 @@ static void test_specifier_printf(void) {
         puts(w);
 }
 
+static void test_str_in_set(void) {
+        assert_se(STR_IN_SET("x", "x", "y", "z"));
+        assert_se(!STR_IN_SET("X", "x", "y", "z"));
+        assert_se(!STR_IN_SET("", "x", "y", "z"));
+        assert_se(STR_IN_SET("x", "w", "x"));
+}
+
+static void test_strptr_in_set(void) {
+        assert_se(STRPTR_IN_SET("x", "x", "y", "z"));
+        assert_se(!STRPTR_IN_SET("X", "x", "y", "z"));
+        assert_se(!STRPTR_IN_SET("", "x", "y", "z"));
+        assert_se(STRPTR_IN_SET("x", "w", "x"));
+
+        assert_se(!STRPTR_IN_SET(NULL, "x", "y", "z"));
+        assert_se(!STRPTR_IN_SET(NULL, ""));
+        /* strv cannot contain a null, hence the result below */
+        assert_se(!STRPTR_IN_SET(NULL, NULL));
+}
+
 static const char* const input_table_multiple[] = {
         "one",
         "two",
@@ -82,36 +90,6 @@ static const char* const input_table_one_empty[] = {
 };
 
 
-static const char* const input_table_quotes[] = {
-        "\"",
-        "'",
-        "\"\"",
-        "\\",
-        "\\\\",
-        NULL,
-};
-#define QUOTES_STRING                            \
-        "\"\\\"\" "                              \
-        "\"\\\'\" "                              \
-        "\"\\\"\\\"\" "                          \
-        "\"\\\\\" "                              \
-        "\"\\\\\\\\\""
-
-static const char * const input_table_spaces[] = {
-        " ",
-        "' '",
-        "\" ",
-        " \"",
-        " \\\\ ",
-        NULL,
-};
-#define SPACES_STRING                           \
-        "\" \" "                                \
-        "\"\\' \\'\" "                          \
-        "\"\\\" \" "                            \
-        "\" \\\"\" "                            \
-        "\" \\\\\\\\ \""
-
 static void test_strv_find(void) {
         assert_se(strv_find((char **)input_table_multiple, "three"));
         assert_se(!strv_find((char **)input_table_multiple, "four"));
@@ -173,28 +151,6 @@ static void test_strv_join(void) {
         assert_se(streq(w, ""));
 }
 
-static void test_strv_quote_unquote(const char* const *split, const char *quoted) {
-        _cleanup_free_ char *p;
-        _cleanup_strv_free_ char **s = NULL;
-        char **t;
-        int r;
-
-        p = strv_join_quoted((char **)split);
-        assert_se(p);
-        printf("-%s- --- -%s-\n", p, quoted); /* fprintf deals with NULL, puts does not */
-        assert_se(p);
-        assert_se(streq(p, quoted));
-
-        r = strv_split_extract(&s, quoted, WHITESPACE, EXTRACT_QUOTES);
-        assert_se(r == (int) strv_length(s));
-        assert_se(s);
-        STRV_FOREACH(t, s) {
-                assert_se(*t);
-                assert_se(streq(*t, *split));
-                split++;
-        }
-}
-
 static void test_strv_unquote(const char *quoted, char **list) {
         _cleanup_strv_free_ char **s;
         _cleanup_free_ char *j;
@@ -434,9 +390,14 @@ static void test_strv_foreach_backwards(void) {
 
         assert_se(a);
 
-        STRV_FOREACH_BACKWARDS(check, a) {
+        STRV_FOREACH_BACKWARDS(check, a)
                 assert_se(streq_ptr(*check, input_table_multiple[i--]));
-        }
+
+        STRV_FOREACH_BACKWARDS(check, (char**) NULL)
+                assert_not_reached("Let's see that we check empty strv right, too.");
+
+        STRV_FOREACH_BACKWARDS(check, (char**) { NULL })
+                assert_not_reached("Let's see that we check empty strv right, too.");
 }
 
 static void test_strv_foreach_pair(void) {
@@ -473,6 +434,36 @@ static void test_strv_from_stdarg_alloca(void) {
         test_strv_from_stdarg_alloca_one(STRV_MAKE_EMPTY, NULL);
 }
 
+static void test_strv_insert(void) {
+        _cleanup_strv_free_ char **a = NULL;
+
+        assert_se(strv_insert(&a, 0, strdup("first")) == 0);
+        assert_se(streq(a[0], "first"));
+        assert_se(!a[1]);
+
+        assert_se(strv_insert(&a, 0, NULL) == 0);
+        assert_se(streq(a[0], "first"));
+        assert_se(!a[1]);
+
+        assert_se(strv_insert(&a, 1, strdup("two")) == 0);
+        assert_se(streq(a[0], "first"));
+        assert_se(streq(a[1], "two"));
+        assert_se(!a[2]);
+
+        assert_se(strv_insert(&a, 4, strdup("tri")) == 0);
+        assert_se(streq(a[0], "first"));
+        assert_se(streq(a[1], "two"));
+        assert_se(streq(a[2], "tri"));
+        assert_se(!a[3]);
+
+        assert_se(strv_insert(&a, 1, strdup("duo")) == 0);
+        assert_se(streq(a[0], "first"));
+        assert_se(streq(a[1], "duo"));
+        assert_se(streq(a[2], "two"));
+        assert_se(streq(a[3], "tri"));
+        assert_se(!a[4]);
+}
+
 static void test_strv_push_prepend(void) {
         _cleanup_strv_free_ char **a = NULL;
 
@@ -647,7 +638,9 @@ static void test_strv_extend_n(void) {
 static void test_strv_make_nulstr_one(char **l) {
         _cleanup_free_ char *b = NULL, *c = NULL;
         _cleanup_strv_free_ char **q = NULL;
+        const char *s = NULL;
         size_t n, m;
+        unsigned i = 0;
 
         assert_se(strv_make_nulstr(l, &b, &n) >= 0);
         assert_se(q = strv_parse_nulstr(b, n));
@@ -656,6 +649,10 @@ static void test_strv_make_nulstr_one(char **l) {
         assert_se(strv_make_nulstr(q, &c, &m) >= 0);
         assert_se(m == n);
         assert_se(memcmp(b, c, m) == 0);
+
+        NULSTR_FOREACH(s, b)
+                assert_se(streq(s, l[i++]));
+        assert_se(i == strv_length(l));
 }
 
 static void test_strv_make_nulstr(void) {
@@ -666,6 +663,17 @@ static void test_strv_make_nulstr(void) {
         test_strv_make_nulstr_one(STRV_MAKE("foo", "bar", "quuux"));
 }
 
+static void test_strv_free_free(void) {
+        char ***t;
+
+        assert_se(t = new(char**, 3));
+        assert_se(t[0] = strv_new("a", "b", NULL));
+        assert_se(t[1] = strv_new("c", "d", "e", NULL));
+        t[2] = NULL;
+
+        t = strv_free_free(t);
+}
+
 static void test_foreach_string(void) {
         const char * const t[] = {
                 "foo",
@@ -685,8 +693,20 @@ static void test_foreach_string(void) {
                 assert_se(streq(x, "zzz"));
 }
 
+static void test_strv_fnmatch(void) {
+        _cleanup_strv_free_ char **v = NULL;
+
+        assert_se(!strv_fnmatch(STRV_MAKE_EMPTY, "a", 0));
+
+        v = strv_new("*\\*", NULL);
+        assert_se(!strv_fnmatch(v, "\\", 0));
+        assert_se(strv_fnmatch(v, "\\", FNM_NOESCAPE));
+}
+
 int main(int argc, char *argv[]) {
         test_specifier_printf();
+        test_str_in_set();
+        test_strptr_in_set();
         test_strv_foreach();
         test_strv_foreach_backwards();
         test_strv_foreach_pair();
@@ -695,14 +715,6 @@ int main(int argc, char *argv[]) {
         test_strv_find_startswith();
         test_strv_join();
 
-        test_strv_quote_unquote(input_table_multiple, "\"one\" \"two\" \"three\"");
-        test_strv_quote_unquote(input_table_one, "\"one\"");
-        test_strv_quote_unquote(input_table_none, "");
-        test_strv_quote_unquote(input_table_one_empty, "\"\"");
-        test_strv_quote_unquote(input_table_two_empties, "\"\" \"\"");
-        test_strv_quote_unquote(input_table_quotes, QUOTES_STRING);
-        test_strv_quote_unquote(input_table_spaces, SPACES_STRING);
-
         test_strv_unquote("    foo=bar     \"waldo\"    zzz    ", STRV_MAKE("foo=bar", "waldo", "zzz"));
         test_strv_unquote("", STRV_MAKE_EMPTY);
         test_strv_unquote(" ", STRV_MAKE_EMPTY);
@@ -739,6 +751,7 @@ int main(int argc, char *argv[]) {
         test_strv_extend();
         test_strv_extendf();
         test_strv_from_stdarg_alloca();
+        test_strv_insert();
         test_strv_push_prepend();
         test_strv_push();
         test_strv_equal();
@@ -748,8 +761,10 @@ int main(int argc, char *argv[]) {
         test_strv_skip();
         test_strv_extend_n();
         test_strv_make_nulstr();
+        test_strv_free_free();
 
         test_foreach_string();
+        test_strv_fnmatch();
 
         return 0;
 }