summaryrefslogtreecommitdiff
path: root/src/test/test-strv.c
diff options
context:
space:
mode:
authorJan Janssen <medhefgo@web.de>2021-11-24 12:00:02 +0100
committerJan Janssen <medhefgo@web.de>2021-11-25 15:03:06 +0100
commit4f7452a8eb7a946efea927fae017d4d661097833 (patch)
tree413778cc0fb08fbbed0890130b5ba66c90581e61 /src/test/test-strv.c
parent4d5ad9d951299c905453ac8e3769dc82b0a09fb4 (diff)
downloadsystemd-4f7452a8eb7a946efea927fae017d4d661097833.tar.gz
test: Use TEST macro
This converts to TEST macro where it is trivial. Some additional notable changes: - simplify HAVE_LIBIDN #ifdef in test-dns-domain.c - use saved_argc/saved_argv in test-copy.c, test-path-util.c, test-tmpfiles.c and test-unit-file.c
Diffstat (limited to 'src/test/test-strv.c')
-rw-r--r--src/test/test-strv.c264
1 files changed, 72 insertions, 192 deletions
diff --git a/src/test/test-strv.c b/src/test/test-strv.c
index 34f438cf9c..a025f34408 100644
--- a/src/test/test-strv.c
+++ b/src/test/test-strv.c
@@ -5,19 +5,16 @@
#include "nulstr-util.h"
#include "string-util.h"
#include "strv.h"
+#include "tests.h"
-static void test_str_in_set(void) {
- log_info("/* %s */", __func__);
-
+TEST(str_in_set) {
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) {
- log_info("/* %s */", __func__);
-
+TEST(strptr_in_set) {
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"));
@@ -29,9 +26,7 @@ static void test_strptr_in_set(void) {
assert_se(!STRPTR_IN_SET(NULL, NULL));
}
-static void test_startswith_set(void) {
- log_info("/* %s */", __func__);
-
+TEST(startswith_set) {
assert_se(!STARTSWITH_SET("foo", "bar", "baz", "waldo"));
assert_se(!STARTSWITH_SET("foo", "bar"));
@@ -99,16 +94,12 @@ static const char* const input_table_retain_escape[] = {
NULL,
};
-static void test_strv_find(void) {
- log_info("/* %s */", __func__);
-
+TEST(strv_find) {
assert_se(strv_find((char **)input_table_multiple, "three"));
assert_se(!strv_find((char **)input_table_multiple, "four"));
}
-static void test_strv_find_prefix(void) {
- log_info("/* %s */", __func__);
-
+TEST(strv_find_prefix) {
assert_se(strv_find_prefix((char **)input_table_multiple, "o"));
assert_se(strv_find_prefix((char **)input_table_multiple, "one"));
assert_se(strv_find_prefix((char **)input_table_multiple, ""));
@@ -116,11 +107,9 @@ static void test_strv_find_prefix(void) {
assert_se(!strv_find_prefix((char **)input_table_multiple, "onee"));
}
-static void test_strv_find_startswith(void) {
+TEST(strv_find_startswith) {
char *r;
- log_info("/* %s */", __func__);
-
r = strv_find_startswith((char **)input_table_multiple, "o");
assert_se(r && streq(r, "ne"));
@@ -134,9 +123,7 @@ static void test_strv_find_startswith(void) {
assert_se(!strv_find_startswith((char **)input_table_multiple, "onee"));
}
-static void test_strv_join(void) {
- log_info("/* %s */", __func__);
-
+TEST(strv_join) {
_cleanup_free_ char *p = strv_join((char **)input_table_multiple, ", ");
assert_se(p);
assert_se(streq(p, "one, two, three"));
@@ -170,9 +157,7 @@ static void test_strv_join(void) {
assert_se(streq(w, ""));
}
-static void test_strv_join_full(void) {
- log_info("/* %s */", __func__);
-
+TEST(strv_join_full) {
_cleanup_free_ char *p = strv_join_full((char **)input_table_multiple, ", ", "foo", false);
assert_se(p);
assert_se(streq(p, "fooone, footwo, foothree"));
@@ -215,7 +200,7 @@ static void test_strv_join_full(void) {
assert_se(streq(y, "foo"));
}
-static void test_strv_unquote(const char *quoted, char **list) {
+static void test_strv_unquote_one(const char *quoted, char **list) {
_cleanup_strv_free_ char **s;
_cleanup_free_ char *j;
unsigned i = 0;
@@ -237,7 +222,25 @@ static void test_strv_unquote(const char *quoted, char **list) {
assert_se(list[i] == NULL);
}
-static void test_invalid_unquote(const char *quoted) {
+TEST(strv_unquote) {
+ test_strv_unquote_one(" foo=bar \"waldo\" zzz ", STRV_MAKE("foo=bar", "waldo", "zzz"));
+ test_strv_unquote_one("", STRV_MAKE_EMPTY);
+ test_strv_unquote_one(" ", STRV_MAKE_EMPTY);
+ test_strv_unquote_one(" ", STRV_MAKE_EMPTY);
+ test_strv_unquote_one(" x", STRV_MAKE("x"));
+ test_strv_unquote_one("x ", STRV_MAKE("x"));
+ test_strv_unquote_one(" x ", STRV_MAKE("x"));
+ test_strv_unquote_one(" \"x\" ", STRV_MAKE("x"));
+ test_strv_unquote_one(" 'x' ", STRV_MAKE("x"));
+ test_strv_unquote_one(" 'x\"' ", STRV_MAKE("x\""));
+ test_strv_unquote_one(" \"x'\" ", STRV_MAKE("x'"));
+ test_strv_unquote_one("a '--b=c \"d e\"'", STRV_MAKE("a", "--b=c \"d e\""));
+
+ /* trailing backslashes */
+ test_strv_unquote_one(" x\\\\", STRV_MAKE("x\\"));
+}
+
+static void test_invalid_unquote_one(const char *quoted) {
char **s = NULL;
int r;
@@ -248,12 +251,20 @@ static void test_invalid_unquote(const char *quoted) {
assert_se(r == -EINVAL);
}
-static void test_strv_split(void) {
+TEST(invalid_unquote) {
+ test_invalid_unquote_one(" x\\");
+ test_invalid_unquote_one("a --b='c \"d e\"''");
+ test_invalid_unquote_one("a --b='c \"d e\" '\"");
+ test_invalid_unquote_one("a --b='c \"d e\"garbage");
+ test_invalid_unquote_one("'");
+ test_invalid_unquote_one("\"");
+ test_invalid_unquote_one("'x'y'g");
+}
+
+TEST(strv_split) {
_cleanup_(strv_free_erasep) char **l = NULL;
const char str[] = "one,two,three";
- log_info("/* %s */", __func__);
-
l = strv_split(str, ",");
assert_se(l);
assert_se(strv_equal(l, (char**) input_table_multiple));
@@ -309,11 +320,9 @@ static void test_strv_split(void) {
assert_se(strv_equal(l, STRV_MAKE("\\")));
}
-static void test_strv_split_empty(void) {
+TEST(strv_split_empty) {
_cleanup_strv_free_ char **l = NULL;
- log_info("/* %s */", __func__);
-
l = strv_split("", WHITESPACE);
assert_se(l);
assert_se(strv_isempty(l));
@@ -373,13 +382,11 @@ static void test_strv_split_empty(void) {
assert_se(strv_isempty(l));
}
-static void test_strv_split_full(void) {
+TEST(strv_split_full) {
_cleanup_strv_free_ char **l = NULL;
const char *str = ":foo\\:bar::waldo:";
int r;
- log_info("/* %s */", __func__);
-
r = strv_split_full(&l, str, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
assert_se(r == (int) strv_length(l));
assert_se(streq_ptr(l[0], ""));
@@ -390,14 +397,12 @@ static void test_strv_split_full(void) {
assert_se(streq_ptr(l[5], NULL));
}
-static void test_strv_split_and_extend_full(void) {
+TEST(strv_split_and_extend_full) {
_cleanup_strv_free_ char **l = NULL;
const char *str1 = ":foo\\:bar:";
const char *str2 = "waldo::::::baz";
int r;
- log_info("/* %s */", __func__);
-
r = strv_split_and_extend(&l, "", ":", false);
assert_se(r == (int) strv_length(l));
r = strv_split_and_extend_full(&l, str1, ":", false, EXTRACT_DONT_COALESCE_SEPARATORS);
@@ -412,14 +417,12 @@ static void test_strv_split_and_extend_full(void) {
assert_se(streq_ptr(l[5], NULL));
}
-static void test_strv_split_colon_pairs(void) {
+TEST(strv_split_colon_pairs) {
_cleanup_strv_free_ char **l = NULL;
const char *str = "one:two three four:five six seven:eight\\:nine ten\\:eleven\\\\",
*str_inval="one:two three:four:five";
int r;
- log_info("/* %s */", __func__);
-
r = strv_split_colon_pairs(&l, str);
assert_se(r == (int) strv_length(l));
assert_se(r == 12);
@@ -441,14 +444,12 @@ static void test_strv_split_colon_pairs(void) {
assert_se(r == -EINVAL);
}
-static void test_strv_split_newlines(void) {
+TEST(strv_split_newlines) {
unsigned i = 0;
char **s;
_cleanup_strv_free_ char **l = NULL;
const char str[] = "one\ntwo\nthree";
- log_info("/* %s */", __func__);
-
l = strv_split_newlines(str);
assert_se(l);
@@ -456,7 +457,7 @@ static void test_strv_split_newlines(void) {
assert_se(streq(*s, input_table_multiple[i++]));
}
-static void test_strv_split_newlines_full(void) {
+TEST(strv_split_newlines_full) {
const char str[] =
"ID_VENDOR=QEMU\n"
"ID_VENDOR_ENC=QEMU\\x20\\x20\\x20\\x20\n"
@@ -464,8 +465,6 @@ static void test_strv_split_newlines_full(void) {
"\n\n\n";
_cleanup_strv_free_ char **l = NULL;
- log_info("/* %s */", __func__);
-
assert_se(strv_split_newlines_full(&l, str, 0) == 3);
assert_se(strv_equal(l, (char**) input_table_unescape));
@@ -475,12 +474,10 @@ static void test_strv_split_newlines_full(void) {
assert_se(strv_equal(l, (char**) input_table_retain_escape));
}
-static void test_strv_split_nulstr(void) {
+TEST(strv_split_nulstr) {
_cleanup_strv_free_ char **l = NULL;
const char nulstr[] = "str0\0str1\0str2\0str3\0";
- log_info("/* %s */", __func__);
-
l = strv_split_nulstr (nulstr);
assert_se(l);
@@ -490,12 +487,10 @@ static void test_strv_split_nulstr(void) {
assert_se(streq(l[3], "str3"));
}
-static void test_strv_parse_nulstr(void) {
+TEST(strv_parse_nulstr) {
_cleanup_strv_free_ char **l = NULL;
const char nulstr[] = "hoge\0hoge2\0hoge3\0\0hoge5\0\0xxx";
- log_info("/* %s */", __func__);
-
l = strv_parse_nulstr(nulstr, sizeof(nulstr)-1);
assert_se(l);
puts("Parse nulstr:");
@@ -510,7 +505,7 @@ static void test_strv_parse_nulstr(void) {
assert_se(streq(l[6], "xxx"));
}
-static void test_strv_overlap(void) {
+TEST(strv_overlap) {
const char * const input_table[] = {
"one",
"two",
@@ -528,13 +523,11 @@ static void test_strv_overlap(void) {
NULL
};
- log_info("/* %s */", __func__);
-
assert_se(strv_overlap((char **)input_table, (char**)input_table_overlap));
assert_se(!strv_overlap((char **)input_table, (char**)input_table_unique));
}
-static void test_strv_sort(void) {
+TEST(strv_sort) {
const char* input_table[] = {
"durian",
"apple",
@@ -544,8 +537,6 @@ static void test_strv_sort(void) {
NULL
};
- log_info("/* %s */", __func__);
-
strv_sort((char **)input_table);
assert_se(streq(input_table[0], "CAPITAL LETTERS FIRST"));
@@ -555,11 +546,9 @@ static void test_strv_sort(void) {
assert_se(streq(input_table[4], "durian"));
}
-static void test_strv_extend_strv_concat(void) {
+TEST(strv_extend_strv_concat) {
_cleanup_strv_free_ char **a = NULL, **b = NULL;
- log_info("/* %s */", __func__);
-
a = strv_new("without", "suffix");
b = strv_new("with", "suffix");
assert_se(a);
@@ -573,11 +562,9 @@ static void test_strv_extend_strv_concat(void) {
assert_se(streq(a[3], "suffix_suffix"));
}
-static void test_strv_extend_strv(void) {
+TEST(strv_extend_strv) {
_cleanup_strv_free_ char **a = NULL, **b = NULL, **n = NULL;
- log_info("/* %s */", __func__);
-
a = strv_new("abc", "def", "ghi");
b = strv_new("jkl", "mno", "abc", "pqr");
assert_se(a);
@@ -601,11 +588,9 @@ static void test_strv_extend_strv(void) {
assert_se(strv_length(n) == 4);
}
-static void test_strv_extend(void) {
+TEST(strv_extend) {
_cleanup_strv_free_ char **a = NULL, **b = NULL;
- log_info("/* %s */", __func__);
-
a = strv_new("test", "test1");
assert_se(a);
assert_se(strv_extend(&a, "test2") >= 0);
@@ -617,11 +602,9 @@ static void test_strv_extend(void) {
assert_se(streq(b[0], "test3"));
}
-static void test_strv_extendf(void) {
+TEST(strv_extendf) {
_cleanup_strv_free_ char **a = NULL, **b = NULL;
- log_info("/* %s */", __func__);
-
a = strv_new("test", "test1");
assert_se(a);
assert_se(strv_extendf(&a, "test2 %s %d %s", "foo", 128, "bar") >= 0);
@@ -633,13 +616,11 @@ static void test_strv_extendf(void) {
assert_se(streq(b[0], "test3 bar foo 128"));
}
-static void test_strv_foreach(void) {
+TEST(strv_foreach) {
_cleanup_strv_free_ char **a;
unsigned i = 0;
char **check;
- log_info("/* %s */", __func__);
-
a = strv_new("one", "two", "three");
assert_se(a);
@@ -647,13 +628,11 @@ static void test_strv_foreach(void) {
assert_se(streq(*check, input_table_multiple[i++]));
}
-static void test_strv_foreach_backwards(void) {
+TEST(strv_foreach_backwards) {
_cleanup_strv_free_ char **a;
unsigned i = 2;
char **check;
- log_info("/* %s */", __func__);
-
a = strv_new("one", "two", "three");
assert_se(a);
@@ -668,12 +647,10 @@ static void test_strv_foreach_backwards(void) {
assert_not_reached();
}
-static void test_strv_foreach_pair(void) {
+TEST(strv_foreach_pair) {
_cleanup_strv_free_ char **a = NULL;
char **x, **y;
- log_info("/* %s */", __func__);
-
a = strv_new("pair_one", "pair_one",
"pair_two", "pair_two",
"pair_three", "pair_three");
@@ -697,19 +674,15 @@ static void test_strv_from_stdarg_alloca_one(char **l, const char *first, ...) {
}
}
-static void test_strv_from_stdarg_alloca(void) {
- log_info("/* %s */", __func__);
-
+TEST(strv_from_stdarg_alloca) {
test_strv_from_stdarg_alloca_one(STRV_MAKE("foo", "bar"), "foo", "bar", NULL);
test_strv_from_stdarg_alloca_one(STRV_MAKE("foo"), "foo", NULL);
test_strv_from_stdarg_alloca_one(STRV_MAKE_EMPTY, NULL);
}
-static void test_strv_insert(void) {
+TEST(strv_insert) {
_cleanup_strv_free_ char **a = NULL;
- log_info("/* %s */", __func__);
-
assert_se(strv_insert(&a, 0, strdup("first")) == 0);
assert_se(streq(a[0], "first"));
assert_se(!a[1]);
@@ -737,11 +710,9 @@ static void test_strv_insert(void) {
assert_se(!a[4]);
}
-static void test_strv_push_prepend(void) {
+TEST(strv_push_prepend) {
_cleanup_strv_free_ char **a = NULL;
- log_info("/* %s */", __func__);
-
assert_se(a = strv_new("foo", "bar", "three"));
assert_se(strv_push_prepend(&a, strdup("first")) >= 0);
@@ -760,12 +731,10 @@ static void test_strv_push_prepend(void) {
assert_se(!a[5]);
}
-static void test_strv_push(void) {
+TEST(strv_push) {
_cleanup_strv_free_ char **a = NULL;
char *i, *j;
- log_info("/* %s */", __func__);
-
assert_se(i = strdup("foo"));
assert_se(strv_push(&a, i) >= 0);
@@ -779,14 +748,12 @@ static void test_strv_push(void) {
assert_se(streq_ptr(a[3], NULL));
}
-static void test_strv_compare(void) {
+TEST(strv_compare) {
_cleanup_strv_free_ char **a = NULL;
_cleanup_strv_free_ char **b = NULL;
_cleanup_strv_free_ char **c = NULL;
_cleanup_strv_free_ char **d = NULL;
- log_info("/* %s */", __func__);
-
a = strv_new("one", "two", "three");
assert_se(a);
b = strv_new("one", "two", "three");
@@ -808,11 +775,9 @@ static void test_strv_compare(void) {
assert_se(strv_compare(b, NULL) == 1);
}
-static void test_strv_is_uniq(void) {
+TEST(strv_is_uniq) {
_cleanup_strv_free_ char **a = NULL, **b = NULL, **c = NULL, **d = NULL;
- log_info("/* %s */", __func__);
-
a = strv_new(NULL);
assert_se(a);
assert_se(strv_is_uniq(a));
@@ -830,11 +795,9 @@ static void test_strv_is_uniq(void) {
assert_se(!strv_is_uniq(d));
}
-static void test_strv_reverse(void) {
+TEST(strv_reverse) {
_cleanup_strv_free_ char **a = NULL, **b = NULL, **c = NULL, **d = NULL;
- log_info("/* %s */", __func__);
-
a = strv_new(NULL);
assert_se(a);
@@ -863,11 +826,9 @@ static void test_strv_reverse(void) {
assert_se(streq_ptr(d[3], NULL));
}
-static void test_strv_shell_escape(void) {
+TEST(strv_shell_escape) {
_cleanup_strv_free_ char **v = NULL;
- log_info("/* %s */", __func__);
-
v = strv_new("foo:bar", "bar,baz", "wal\\do");
assert_se(v);
assert_se(strv_shell_escape(v, ",:"));
@@ -882,9 +843,7 @@ static void test_strv_skip_one(char **a, size_t n, char **b) {
assert_se(strv_equal(a, b));
}
-static void test_strv_skip(void) {
- log_info("/* %s */", __func__);
-
+TEST(strv_skip) {
test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 0, STRV_MAKE("foo", "bar", "baz"));
test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 1, STRV_MAKE("bar", "baz"));
test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 2, STRV_MAKE("baz"));
@@ -901,11 +860,9 @@ static void test_strv_skip(void) {
test_strv_skip_one(STRV_MAKE(NULL), 55, STRV_MAKE(NULL));
}
-static void test_strv_extend_n(void) {
+TEST(strv_extend_n) {
_cleanup_strv_free_ char **v = NULL;
- log_info("/* %s */", __func__);
-
v = strv_new("foo", "bar");
assert_se(v);
@@ -952,9 +909,7 @@ static void test_strv_make_nulstr_one(char **l) {
assert_se(i == strv_length(l));
}
-static void test_strv_make_nulstr(void) {
- log_info("/* %s */", __func__);
-
+TEST(strv_make_nulstr) {
test_strv_make_nulstr_one(NULL);
test_strv_make_nulstr_one(STRV_MAKE(NULL));
test_strv_make_nulstr_one(STRV_MAKE("foo"));
@@ -962,11 +917,9 @@ static void test_strv_make_nulstr(void) {
test_strv_make_nulstr_one(STRV_MAKE("foo", "bar", "quuux"));
}
-static void test_strv_free_free(void) {
+TEST(strv_free_free) {
char ***t;
- log_info("/* %s */", __func__);
-
assert_se(t = new(char**, 3));
assert_se(t[0] = strv_new("a", "b"));
assert_se(t[1] = strv_new("c", "d", "e"));
@@ -975,7 +928,7 @@ static void test_strv_free_free(void) {
t = strv_free_free(t);
}
-static void test_foreach_string(void) {
+TEST(foreach_string) {
const char * const t[] = {
"foo",
"bar",
@@ -985,8 +938,6 @@ static void test_foreach_string(void) {
const char *x;
unsigned i = 0;
- log_info("/* %s */", __func__);
-
FOREACH_STRING(x, "foo", "bar", "waldo")
assert_se(streq_ptr(t[i++], x));
@@ -996,12 +947,10 @@ static void test_foreach_string(void) {
assert_se(streq(x, "zzz"));
}
-static void test_strv_fnmatch(void) {
+TEST(strv_fnmatch) {
_cleanup_strv_free_ char **v = NULL;
size_t pos;
- log_info("/* %s */", __func__);
-
assert_se(!strv_fnmatch(STRV_MAKE_EMPTY, "a"));
v = strv_new("xxx", "*\\*", "yyy");
@@ -1010,73 +959,4 @@ static void test_strv_fnmatch(void) {
assert_se(pos == 1);
}
-int main(int argc, char *argv[]) {
- test_str_in_set();
- test_strptr_in_set();
- test_startswith_set();
- test_strv_foreach();
- test_strv_foreach_backwards();
- test_strv_foreach_pair();
- test_strv_find();
- test_strv_find_prefix();
- test_strv_find_startswith();
- test_strv_join();
- test_strv_join_full();
-
- 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);
- test_strv_unquote(" ", STRV_MAKE_EMPTY);
- test_strv_unquote(" x", STRV_MAKE("x"));
- test_strv_unquote("x ", STRV_MAKE("x"));
- test_strv_unquote(" x ", STRV_MAKE("x"));
- test_strv_unquote(" \"x\" ", STRV_MAKE("x"));
- test_strv_unquote(" 'x' ", STRV_MAKE("x"));
- test_strv_unquote(" 'x\"' ", STRV_MAKE("x\""));
- test_strv_unquote(" \"x'\" ", STRV_MAKE("x'"));
- test_strv_unquote("a '--b=c \"d e\"'", STRV_MAKE("a", "--b=c \"d e\""));
-
- /* trailing backslashes */
- test_strv_unquote(" x\\\\", STRV_MAKE("x\\"));
- test_invalid_unquote(" x\\");
-
- test_invalid_unquote("a --b='c \"d e\"''");
- test_invalid_unquote("a --b='c \"d e\" '\"");
- test_invalid_unquote("a --b='c \"d e\"garbage");
- test_invalid_unquote("'");
- test_invalid_unquote("\"");
- test_invalid_unquote("'x'y'g");
-
- test_strv_split();
- test_strv_split_empty();
- test_strv_split_full();
- test_strv_split_and_extend_full();
- test_strv_split_colon_pairs();
- test_strv_split_newlines();
- test_strv_split_newlines_full();
- test_strv_split_nulstr();
- test_strv_parse_nulstr();
- test_strv_overlap();
- test_strv_sort();
- test_strv_extend_strv();
- test_strv_extend_strv_concat();
- test_strv_extend();
- test_strv_extendf();
- test_strv_from_stdarg_alloca();
- test_strv_insert();
- test_strv_push_prepend();
- test_strv_push();
- test_strv_compare();
- test_strv_is_uniq();
- test_strv_reverse();
- test_strv_shell_escape();
- test_strv_skip();
- test_strv_extend_n();
- test_strv_make_nulstr();
- test_strv_free_free();
-
- test_foreach_string();
- test_strv_fnmatch();
-
- return 0;
-}
+DEFINE_TEST_MAIN(LOG_INFO);