HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/test/test-string-util.c
Git blob: 732517541f11ba5a849662dba64b36c015946275
Lines 1–1000 of 1,840
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <sys/utsname.h>
#include <unistd.h>
#include "alloc-util.h"
#include "extract-word.h"
#include "locale-util.h"
#include "string-util.h"
#include "strv.h"
#include "tests.h"
#include "utf8.h"
#include "version.h"
TEST(ellipsize_mem_ansi_short) {
_cleanup_free_ char *a = ellipsize_mem("X\x1b[m", 4, 1, 50);
assert_se(a);
_cleanup_free_ char *b = ellipsize_mem(" \x1b[A", 4, 1, 0);
assert_se(b);
_cleanup_free_ char *c = ellipsize_mem("\x1b[m", 3, 1, 50);
assert_se(c);
}
TEST(ellipsize_mem_tab) {
/* unichar_console_width() counts a tab as eight character cells. Callers such as
* format-table.c measure a field with utf8_console_width() and then ask ellipsize() to fit it
* into that many columns, so ellipsize() must not count a tab as a single cell: the result
* would still overflow the column that the measurement had just asked us to fit. */
unsigned percent;
FOREACH_STRING(x,
"\t",
"a\tb",
"ab\tcd",
"\t\t\t",
"a\tb\tc\td\te",
"a\t你\tb") /* tabs mixed with a wide character */
for (size_t w = 1; w <= 24; w++)
/* Sweep the ellipsis placement too: it decides how much of the string the
* forward loop takes and how much is left to the backward one, and the two
* meet at quite different places for an eight cell tab than for a single cell
* character. */
FOREACH_ARGUMENT(percent, 0u, 30u, 50u, 90u, 100u) {
_cleanup_free_ char *e = ASSERT_NOT_NULL(ellipsize(x, w, percent));
ASSERT_LE(utf8_console_width(e), w);
/* And don't truncate what already fits. */
if (utf8_console_width(x) <= w)
ASSERT_STREQ(e, x);
}
/* Pin a couple of concrete results, so that a change to the assumed tab width or to where the
* ellipsis lands shows up in the diff. Note that these are in terms of the flat eight cells
* unichar_console_width() charges a tab, which is what our callers measure with; a terminal
* instead advances to the next multiple of eight, and hence needs one column less for "a\tb"
* at the start of a line. */
_cleanup_free_ char *a = ASSERT_NOT_NULL(ellipsize("a\tb", 10, 50));
ASSERT_STREQ(a, "a\tb");
_cleanup_free_ char *b = ASSERT_NOT_NULL(ellipsize("a\tb", 9, 50));
ASSERT_STREQ(b, "a…b");
}
TEST(xsprintf) {
char buf[5];
xsprintf(buf, "asdf");
xsprintf(buf, "%4s", "a");
xsprintf(buf, "%-4s", "a");
xsprintf(buf, "%04d", 1);
ASSERT_SIGNAL(xsprintf(buf, "asdfe"), SIGABRT);
ASSERT_SIGNAL(xsprintf(buf, "asdfefghdhdhdhdhd"), SIGABRT);
ASSERT_SIGNAL(xsprintf(buf, "%5s", "a"), SIGABRT);
}
TEST(string_erase) {
char *x;
x = strdupa_safe("");
ASSERT_STREQ(string_erase(x), "");
x = strdupa_safe("1");
ASSERT_STREQ(string_erase(x), "");
x = strdupa_safe("123456789");
ASSERT_STREQ(string_erase(x), "");
assert_se(x[1] == '\0');
assert_se(x[2] == '\0');
assert_se(x[3] == '\0');
assert_se(x[4] == '\0');
assert_se(x[5] == '\0');
assert_se(x[6] == '\0');
assert_se(x[7] == '\0');
assert_se(x[8] == '\0');
assert_se(x[9] == '\0');
}
static void test_free_and_strndup_one(char **t, const char *src, size_t l, const char *expected, bool change) {
log_debug("%s: \"%s\", \"%s\", %zu (expect \"%s\", %s)",
__func__, strnull(*t), strnull(src), l, strnull(expected), yes_no(change));
int r = free_and_strndup(t, src, l);
ASSERT_STREQ(*t, expected);
assert_se(r == change); /* check that change occurs only when necessary */
}
TEST(free_and_strndup) {
static const struct test_case {
const char *src;
size_t len;
const char *expected;
} cases[] = {
{"abc", 0, ""},
{"abc", 0, ""},
{"abc", 1, "a"},
{"abc", 2, "ab"},
{"abc", 3, "abc"},
{"abc", 4, "abc"},
{"abc", 5, "abc"},
{"abc", 5, "abc"},
{"abc", 4, "abc"},
{"abc", 3, "abc"},
{"abc", 2, "ab"},
{"abc", 1, "a"},
{"abc", 0, ""},
{"", 0, ""},
{"", 1, ""},
{"", 2, ""},
{"", 0, ""},
{"", 1, ""},
{"", 2, ""},
{"", 2, ""},
{"", 1, ""},
{"", 0, ""},
{NULL, 0, NULL},
{"foo", 3, "foo"},
{"foobar", 6, "foobar"},
};
_cleanup_free_ char *t = NULL;
const char *prev_expected = t;
FOREACH_ELEMENT(c, cases) {
test_free_and_strndup_one(&t,
c->src, c->len, c->expected,
!streq_ptr(c->expected, prev_expected));
prev_expected = t;
}
}
TEST(strdup_to_full) {
_cleanup_free_ char *dst;
assert_se(strdup_to_full(NULL, NULL) == 0);
assert_se(strdup_to_full(&dst, NULL) == 0);
assert_se(strdup_to_full(NULL, "") == 1);
assert_se(strdup_to_full(&dst, "") == 1);
ASSERT_STREQ(dst, "");
dst = mfree(dst);
assert_se(strdup_to_full(NULL, "x") == 1);
assert_se(strdup_to_full(&dst, "x") == 1);
ASSERT_STREQ(dst, "x");
}
TEST(strdup_to) {
_cleanup_free_ char *dst;
assert_se(strdup_to(&dst, NULL) == 0);
assert_se(strdup_to(&dst, "") == 0);
ASSERT_STREQ(dst, "");
dst = mfree(dst);
assert_se(strdup_to(&dst, "x") == 0);
ASSERT_STREQ(dst, "x");
}
TEST(ascii_strcasecmp_n) {
assert_se(ascii_strcasecmp_n("", "", 0) == 0);
assert_se(ascii_strcasecmp_n("", "", 1) == 0);
assert_se(ascii_strcasecmp_n("", "a", 1) < 0);
assert_se(ascii_strcasecmp_n("", "a", 2) < 0);
assert_se(ascii_strcasecmp_n("a", "", 1) > 0);
assert_se(ascii_strcasecmp_n("a", "", 2) > 0);
assert_se(ascii_strcasecmp_n("a", "a", 1) == 0);
assert_se(ascii_strcasecmp_n("a", "a", 2) == 0);
assert_se(ascii_strcasecmp_n("a", "b", 1) < 0);
assert_se(ascii_strcasecmp_n("a", "b", 2) < 0);
assert_se(ascii_strcasecmp_n("b", "a", 1) > 0);
assert_se(ascii_strcasecmp_n("b", "a", 2) > 0);
assert_se(ascii_strcasecmp_n("xxxxyxxxx", "xxxxYxxxx", 9) == 0);
assert_se(ascii_strcasecmp_n("xxxxxxxxx", "xxxxyxxxx", 9) < 0);
assert_se(ascii_strcasecmp_n("xxxxXxxxx", "xxxxyxxxx", 9) < 0);
assert_se(ascii_strcasecmp_n("xxxxxxxxx", "xxxxYxxxx", 9) < 0);
assert_se(ascii_strcasecmp_n("xxxxXxxxx", "xxxxYxxxx", 9) < 0);
assert_se(ascii_strcasecmp_n("xxxxYxxxx", "xxxxYxxxx", 9) == 0);
assert_se(ascii_strcasecmp_n("xxxxyxxxx", "xxxxxxxxx", 9) > 0);
assert_se(ascii_strcasecmp_n("xxxxyxxxx", "xxxxXxxxx", 9) > 0);
assert_se(ascii_strcasecmp_n("xxxxYxxxx", "xxxxxxxxx", 9) > 0);
assert_se(ascii_strcasecmp_n("xxxxYxxxx", "xxxxXxxxx", 9) > 0);
}
TEST(ascii_strcasecmp_nn) {
assert_se(ascii_strcasecmp_nn("", 0, "", 0) == 0);
assert_se(ascii_strcasecmp_nn("", 0, "", 1) < 0);
assert_se(ascii_strcasecmp_nn("", 1, "", 0) > 0);
assert_se(ascii_strcasecmp_nn("", 1, "", 1) == 0);
assert_se(ascii_strcasecmp_nn("aaaa", 4, "aaAa", 4) == 0);
assert_se(ascii_strcasecmp_nn("aaa", 3, "aaAa", 4) < 0);
assert_se(ascii_strcasecmp_nn("aaa", 4, "aaAa", 4) < 0);
assert_se(ascii_strcasecmp_nn("aaaa", 4, "aaA", 3) > 0);
assert_se(ascii_strcasecmp_nn("aaaa", 4, "AAA", 4) > 0);
assert_se(ascii_strcasecmp_nn("aaaa", 4, "bbbb", 4) < 0);
assert_se(ascii_strcasecmp_nn("aaAA", 4, "BBbb", 4) < 0);
assert_se(ascii_strcasecmp_nn("BBbb", 4, "aaaa", 4) > 0);
}
TEST(cellescape) {
char buf[40];
ASSERT_STREQ(cellescape(buf, 1, ""), "");
ASSERT_STREQ(cellescape(buf, 1, "1"), "");
ASSERT_STREQ(cellescape(buf, 1, "12"), "");
ASSERT_STREQ(cellescape(buf, 2, ""), "");
ASSERT_STREQ(cellescape(buf, 2, "1"), "1");
ASSERT_STREQ(cellescape(buf, 2, "12"), ".");
ASSERT_STREQ(cellescape(buf, 2, "123"), ".");
ASSERT_STREQ(cellescape(buf, 3, ""), "");
ASSERT_STREQ(cellescape(buf, 3, "1"), "1");
ASSERT_STREQ(cellescape(buf, 3, "12"), "12");
ASSERT_STREQ(cellescape(buf, 3, "123"), "..");
ASSERT_STREQ(cellescape(buf, 3, "1234"), "..");
ASSERT_STREQ(cellescape(buf, 4, ""), "");
ASSERT_STREQ(cellescape(buf, 4, "1"), "1");
ASSERT_STREQ(cellescape(buf, 4, "12"), "12");
ASSERT_STREQ(cellescape(buf, 4, "123"), "123");
ASSERT_STREQ(cellescape(buf, 4, "1234"), is_locale_utf8() ? "…" : "...");
ASSERT_STREQ(cellescape(buf, 4, "12345"), is_locale_utf8() ? "…" : "...");
ASSERT_STREQ(cellescape(buf, 5, ""), "");
ASSERT_STREQ(cellescape(buf, 5, "1"), "1");
ASSERT_STREQ(cellescape(buf, 5, "12"), "12");
ASSERT_STREQ(cellescape(buf, 5, "123"), "123");
ASSERT_STREQ(cellescape(buf, 5, "1234"), "1234");
ASSERT_STREQ(cellescape(buf, 5, "12345"), is_locale_utf8() ? "1…" : "1...");
ASSERT_STREQ(cellescape(buf, 5, "123456"), is_locale_utf8() ? "1…" : "1...");
ASSERT_STREQ(cellescape(buf, 1, "\020"), "");
ASSERT_STREQ(cellescape(buf, 2, "\020"), ".");
ASSERT_STREQ(cellescape(buf, 3, "\020"), "..");
ASSERT_STREQ(cellescape(buf, 4, "\020"), is_locale_utf8() ? "…" : "...");
ASSERT_STREQ(cellescape(buf, 5, "\020"), "\\020");
ASSERT_STREQ(cellescape(buf, 5, "1234\020"), is_locale_utf8() ? "1…" : "1...");
ASSERT_STREQ(cellescape(buf, 6, "1234\020"), is_locale_utf8() ? "12…" : "12...");
ASSERT_STREQ(cellescape(buf, 7, "1234\020"), is_locale_utf8() ? "123…" : "123...");
ASSERT_STREQ(cellescape(buf, 8, "1234\020"), is_locale_utf8() ? "1234…" : "1234...");
ASSERT_STREQ(cellescape(buf, 9, "1234\020"), "1234\\020");
ASSERT_STREQ(cellescape(buf, 1, "\t\n"), "");
ASSERT_STREQ(cellescape(buf, 2, "\t\n"), ".");
ASSERT_STREQ(cellescape(buf, 3, "\t\n"), "..");
ASSERT_STREQ(cellescape(buf, 4, "\t\n"), is_locale_utf8() ? "…" : "...");
ASSERT_STREQ(cellescape(buf, 5, "\t\n"), "\\t\\n");
ASSERT_STREQ(cellescape(buf, 5, "1234\t\n"), is_locale_utf8() ? "1…" : "1...");
ASSERT_STREQ(cellescape(buf, 6, "1234\t\n"), is_locale_utf8() ? "12…" : "12...");
ASSERT_STREQ(cellescape(buf, 7, "1234\t\n"), is_locale_utf8() ? "123…" : "123...");
ASSERT_STREQ(cellescape(buf, 8, "1234\t\n"), is_locale_utf8() ? "1234…" : "1234...");
ASSERT_STREQ(cellescape(buf, 9, "1234\t\n"), "1234\\t\\n");
ASSERT_STREQ(cellescape(buf, 4, "x\t\020\n"), is_locale_utf8() ? "…" : "...");
ASSERT_STREQ(cellescape(buf, 5, "x\t\020\n"), is_locale_utf8() ? "x…" : "x...");
ASSERT_STREQ(cellescape(buf, 6, "x\t\020\n"), is_locale_utf8() ? "x…" : "x...");
ASSERT_STREQ(cellescape(buf, 7, "x\t\020\n"), is_locale_utf8() ? "x\\t…" : "x\\t...");
ASSERT_STREQ(cellescape(buf, 8, "x\t\020\n"), is_locale_utf8() ? "x\\t…" : "x\\t...");
ASSERT_STREQ(cellescape(buf, 9, "x\t\020\n"), is_locale_utf8() ? "x\\t…" : "x\\t...");
ASSERT_STREQ(cellescape(buf, 10, "x\t\020\n"), "x\\t\\020\\n");
ASSERT_STREQ(cellescape(buf, 6, "1\011"), "1\\t");
ASSERT_STREQ(cellescape(buf, 6, "1\020"), "1\\020");
ASSERT_STREQ(cellescape(buf, 6, "1\020x"), is_locale_utf8() ? "1…" : "1...");
ASSERT_STREQ(cellescape(buf, 40, "1\020"), "1\\020");
ASSERT_STREQ(cellescape(buf, 40, "1\020x"), "1\\020x");
ASSERT_STREQ(cellescape(buf, 40, "\a\b\f\n\r\t\v\\\"'"), "\\a\\b\\f\\n\\r\\t\\v\\\\\\\"\\'");
ASSERT_STREQ(cellescape(buf, 6, "\a\b\f\n\r\t\v\\\"'"), is_locale_utf8() ? "\\a…" : "\\a...");
ASSERT_STREQ(cellescape(buf, 7, "\a\b\f\n\r\t\v\\\"'"), is_locale_utf8() ? "\\a…" : "\\a...");
ASSERT_STREQ(cellescape(buf, 8, "\a\b\f\n\r\t\v\\\"'"), is_locale_utf8() ? "\\a\\b…" : "\\a\\b...");
ASSERT_STREQ(cellescape(buf, sizeof buf, "1\020"), "1\\020");
ASSERT_STREQ(cellescape(buf, sizeof buf, "1\020x"), "1\\020x");
}
TEST(streq_ptr) {
assert_se(streq_ptr(NULL, NULL));
assert_se(!streq_ptr("abc", "cdef"));
}
TEST(strstrip) {
char *ret, input[] = " hello, waldo. ";
ret = strstrip(input);
ASSERT_STREQ(ret, "hello, waldo.");
}
TEST(strextend) {
_cleanup_free_ char *str = NULL;
assert_se(strextend(&str, NULL));
ASSERT_STREQ(str, "");
assert_se(strextend(&str, "", "0", "", "", "123"));
ASSERT_STREQ(str, "0123");
assert_se(strextend(&str, "456", "78", "9"));
ASSERT_STREQ(str, "0123456789");
assert_se(strextend(&str, "more", NULL, "huch"));
ASSERT_STREQ(str, "0123456789more");
assert_se(strextend(&str, "MORE", POINTER_MAX, "HUCH"));
ASSERT_STREQ(str, "0123456789moreMOREHUCH");
}
TEST(strextend_with_separator) {
_cleanup_free_ char *str = NULL;
assert_se(strextend_with_separator(&str, NULL, NULL));
ASSERT_STREQ(str, "");
str = mfree(str);
assert_se(strextend_with_separator(&str, "...", NULL));
ASSERT_STREQ(str, "");
assert_se(strextend_with_separator(&str, "...", NULL));
ASSERT_STREQ(str, "");
str = mfree(str);
assert_se(strextend_with_separator(&str, "xyz", "a", "bb", "ccc"));
ASSERT_STREQ(str, "axyzbbxyzccc");
str = mfree(str);
assert_se(strextend_with_separator(&str, ",", "start", "", "1", "234"));
ASSERT_STREQ(str, "start,,1,234");
assert_se(strextend_with_separator(&str, ";", "more", "5", "678"));
ASSERT_STREQ(str, "start,,1,234;more;5;678");
assert_se(strextend_with_separator(&str, ";", "xxxx", POINTER_MAX, "yyy"));
ASSERT_STREQ(str, "start,,1,234;more;5;678;xxxx;yyy");
}
TEST(strrep) {
_cleanup_free_ char *one = NULL, *three = NULL, *zero = NULL;
char *onea, *threea;
one = strrep("waldo", 1);
three = strrep("waldo", 3);
zero = strrep("waldo", 0);
ASSERT_STREQ(one, "waldo");
ASSERT_STREQ(three, "waldowaldowaldo");
ASSERT_STREQ(zero, "");
onea = strrepa("waldo", 1);
threea = strrepa("waldo", 3);
ASSERT_STREQ(onea, "waldo");
ASSERT_STREQ(threea, "waldowaldowaldo");
ASSERT_NULL(strrep("waldo", SIZE_MAX - 1));
}
TEST(string_has_cc) {
assert_se(string_has_cc("abc\1", NULL));
assert_se(string_has_cc("abc\x7f", NULL));
assert_se(string_has_cc("abc\x7f", NULL));
assert_se(string_has_cc("abc\t\x7f", "\t"));
assert_se(string_has_cc("abc\t\x7f", "\t"));
assert_se(string_has_cc("\x7f", "\t"));
assert_se(string_has_cc("\x7f", "\t\a"));
assert_se(!string_has_cc("abc\t\t", "\t"));
assert_se(!string_has_cc("abc\t\t\a", "\t\a"));
assert_se(!string_has_cc("a\ab\tc", "\t\a"));
}
TEST(ascii_strlower) {
char a[] = "AabBcC Jk Ii Od LKJJJ kkd LK";
ASSERT_STREQ(ascii_strlower(a), "aabbcc jk ii od lkjjj kkd lk");
}
TEST(strshorten) {
char s[] = "foobar";
assert_se(strlen(strshorten(s, SIZE_MAX)) == 6);
assert_se(strlen(strshorten(s, SIZE_MAX-1)) == 6);
assert_se(strlen(strshorten(s, SIZE_MAX-2)) == 6);
assert_se(strlen(strshorten(s, 6)) == 6);
assert_se(strlen(strshorten(s, 7)) == 6);
assert_se(strlen(strshorten(s, 12)) == 6);
assert_se(strlen(strshorten(s, 5)) == 5);
assert_se(strlen(strshorten(s, 2)) == 2);
assert_se(strlen(strshorten(s, 0)) == 0);
}
TEST(strjoina) {
char *actual;
actual = strjoina("", "foo", "bar");
ASSERT_STREQ(actual, "foobar");
actual = strjoina("foo", "bar", "baz");
ASSERT_STREQ(actual, "foobarbaz");
actual = strjoina("foo", "", "bar", "baz");
ASSERT_STREQ(actual, "foobarbaz");
actual = strjoina("foo");
ASSERT_STREQ(actual, "foo");
actual = strjoina(NULL);
ASSERT_STREQ(actual, "");
actual = strjoina(NULL, "foo");
ASSERT_STREQ(actual, "");
actual = strjoina("foo", NULL, "bar");
ASSERT_STREQ(actual, "foo");
actual = strjoina("/sys/fs/cgroup/", "dn", "/a/b/c", "/cgroup.procs");
ASSERT_STREQ(actual, "/sys/fs/cgroup/dn/a/b/c/cgroup.procs");
actual = strjoina("/sys/fs/cgroup/", "dn", NULL, NULL);
ASSERT_STREQ(actual, "/sys/fs/cgroup/dn");
}
TEST(strjoin) {
char *actual;
actual = strjoin("", "foo", "bar");
ASSERT_STREQ(actual, "foobar");
free(actual);
actual = strjoin("foo", "bar", "baz");
ASSERT_STREQ(actual, "foobarbaz");
free(actual);
actual = strjoin("foo", "", "bar", "baz");
ASSERT_STREQ(actual, "foobarbaz");
free(actual);
actual = strjoin("foo", NULL);
ASSERT_STREQ(actual, "foo");
free(actual);
actual = strjoin(NULL, NULL);
ASSERT_STREQ(actual, "");
free(actual);
actual = strjoin(NULL, "foo");
ASSERT_STREQ(actual, "");
free(actual);
actual = strjoin("foo", NULL, "bar");
ASSERT_STREQ(actual, "foo");
free(actual);
actual = strjoin("foo", POINTER_MAX, "bar");
ASSERT_STREQ(actual, "foobar");
free(actual);
}
TEST(strcmp_ptr) {
assert_se(strcmp_ptr(NULL, NULL) == 0);
assert_se(strcmp_ptr("", NULL) > 0);
assert_se(strcmp_ptr("foo", NULL) > 0);
assert_se(strcmp_ptr(NULL, "") < 0);
assert_se(strcmp_ptr(NULL, "bar") < 0);
assert_se(strcmp_ptr("foo", "bar") > 0);
assert_se(strcmp_ptr("bar", "baz") < 0);
assert_se(strcmp_ptr("foo", "foo") == 0);
assert_se(strcmp_ptr("", "") == 0);
}
TEST(foreach_word) {
const char *test = "test abc d\te f ";
const char * const expected[] = {
"test",
"abc",
"d",
"e",
"f",
};
size_t i = 0;
int r;
for (const char *p = test;;) {
_cleanup_free_ char *word = NULL;
r = extract_first_word(&p, &word, NULL, 0);
if (r == 0) {
assert_se(i == ELEMENTSOF(expected));
break;
}
assert_se(r > 0);
ASSERT_STREQ(expected[i++], word);
}
}
static void check(const char *test, char** expected, bool trailing) {
size_t i = 0;
int r;
printf("<<<%s>>>\n", test);
for (;;) {
_cleanup_free_ char *word = NULL;
r = extract_first_word(&test, &word, NULL, EXTRACT_UNQUOTE);
if (r == 0) {
assert_se(!trailing);
break;
} else if (r < 0) {
assert_se(trailing);
break;
}
ASSERT_STREQ(word, expected[i++]);
printf("<%s>\n", word);
}
ASSERT_NULL(expected[i]);
}
TEST(foreach_word_quoted) {
check("test a b c 'd' e '' '' hhh '' '' \"a b c\"",
STRV_MAKE("test",
"a",
"b",
"c",
"d",
"e",
"",
"",
"hhh",
"",
"",
"a b c"),
false);
check("test \"xxx",
STRV_MAKE("test"),
true);
check("test\\",
STRV_EMPTY,
true);
}
TEST(endswith) {
assert_se(endswith("foobar", "bar"));
assert_se(endswith("foobar", ""));
assert_se(endswith("foobar", "foobar"));
assert_se(endswith("", ""));
assert_se(!endswith("foobar", "foo"));
assert_se(!endswith("foobar", "foobarfoofoo"));
}
TEST(endswith_no_case) {
assert_se(endswith_no_case("fooBAR", "bar"));
assert_se(endswith_no_case("foobar", ""));
assert_se(endswith_no_case("foobar", "FOOBAR"));
assert_se(endswith_no_case("", ""));
assert_se(!endswith_no_case("foobar", "FOO"));
assert_se(!endswith_no_case("foobar", "FOOBARFOOFOO"));
}
TEST(strrstr_no_case) {
ASSERT_STREQ(strrstr_no_case("fooBARfoobar", "bar"), "bar");
ASSERT_STREQ(strrstr_no_case("fooBARfoobar", "BAR"), "bar");
ASSERT_STREQ(strrstr_no_case("fooBARfoobar", "bAR"), "bar");
ASSERT_STREQ(strrstr_no_case("fooBARfoobar", "FOO"), "foobar");
ASSERT_STREQ(strrstr_no_case("fooBARfoobar", "foo"), "foobar");
ASSERT_STREQ(strrstr_no_case("fooBARfoobar", "FoO"), "foobar");
ASSERT_STREQ(strrstr_no_case("aXaXa", "x"), "Xa");
ASSERT_STREQ(strrstr_no_case("aXaXa", "X"), "Xa");
ASSERT_STREQ(strrstr_no_case("xHello", "hello"), "Hello");
ASSERT_STREQ(strrstr_no_case("Hello", "l"), "lo");
ASSERT_STREQ(strrstr_no_case("Hello", ""), "");
ASSERT_STREQ(strrstr_no_case("", ""), "");
ASSERT_STREQ(strrstr_no_case("FOO", "foo"), "FOO");
ASSERT_STREQ(strrstr_no_case("hello", "hello"), "hello");
ASSERT_STREQ(strrstr_no_case("X", "x"), "X");
ASSERT_NULL(strrstr_no_case("hello", "xyz"));
ASSERT_NULL(strrstr_no_case("", "x"));
ASSERT_NULL(strrstr_no_case(NULL, "x"));
ASSERT_NULL(strrstr_no_case("x", NULL));
}
TEST(delete_chars) {
char *s, input[] = " hello, waldo. abc";
s = delete_chars(input, WHITESPACE);
ASSERT_STREQ(s, "hello,waldo.abc");
assert_se(s == input);
}
TEST(delete_trailing_chars) {
char *s,
input1[] = " \n \r k \n \r ",
input2[] = "kkkkthiskkkiskkkaktestkkk",
input3[] = "abcdef";
s = delete_trailing_chars(input1, WHITESPACE);
ASSERT_STREQ(s, " \n \r k");
assert_se(s == input1);
s = delete_trailing_chars(input2, "kt");
ASSERT_STREQ(s, "kkkkthiskkkiskkkaktes");
assert_se(s == input2);
s = delete_trailing_chars(input3, WHITESPACE);
ASSERT_STREQ(s, "abcdef");
assert_se(s == input3);
s = delete_trailing_chars(input3, "fe");
ASSERT_STREQ(s, "abcd");
assert_se(s == input3);
}
TEST(delete_trailing_slashes) {
char s1[] = "foobar//",
s2[] = "foobar/",
s3[] = "foobar",
s4[] = "";
ASSERT_STREQ(delete_trailing_chars(s1, "_"), "foobar//");
ASSERT_STREQ(delete_trailing_chars(s1, "/"), "foobar");
ASSERT_STREQ(delete_trailing_chars(s2, "/"), "foobar");
ASSERT_STREQ(delete_trailing_chars(s3, "/"), "foobar");
ASSERT_STREQ(delete_trailing_chars(s4, "/"), "");
}
TEST(skip_leading_chars) {
char input1[] = " \n \r k \n \r ",
input2[] = "kkkkthiskkkiskkkaktestkkk",
input3[] = "abcdef";
ASSERT_STREQ(skip_leading_chars(input1, WHITESPACE), "k \n \r ");
ASSERT_STREQ(skip_leading_chars(input2, "k"), "thiskkkiskkkaktestkkk");
ASSERT_STREQ(skip_leading_chars(input2, "tk"), "hiskkkiskkkaktestkkk");
ASSERT_STREQ(skip_leading_chars(input3, WHITESPACE), "abcdef");
ASSERT_STREQ(skip_leading_chars(input3, "bcaef"), "def");
}
TEST(in_charset) {
assert_se(in_charset("dddaaabbbcccc", "abcd"));
assert_se(!in_charset("dddaaabbbcccc", "abc f"));
}
TEST(split_pair) {
_cleanup_free_ char *a = NULL, *b = NULL;
ASSERT_SIGNAL(split_pair("", NULL, &a, &b), SIGABRT);
ASSERT_SIGNAL(split_pair("", "", &a, &b), SIGABRT);
ASSERT_SIGNAL(split_pair("foo=bar", "", &a, &b), SIGABRT);
ASSERT_SIGNAL(split_pair(NULL, "=", &a, &b), SIGABRT);
ASSERT_ERROR(split_pair("", "=", &a, &b), EINVAL);
ASSERT_OK(split_pair("foo=bar", "=", &a, &b));
ASSERT_STREQ(a, "foo");
ASSERT_STREQ(b, "bar");
a = mfree(a);
b = mfree(b);
ASSERT_OK(split_pair("==", "==", &a, &b));
ASSERT_STREQ(a, "");
ASSERT_STREQ(b, "");
a = mfree(a);
b = mfree(b);
ASSERT_OK(split_pair("===", "==", &a, &b));
ASSERT_STREQ(a, "");
ASSERT_STREQ(b, "=");
a = mfree(a);
b = mfree(b);
/* The output parameters are optional */
ASSERT_OK(split_pair("foo=bar", "=", NULL, &b));
ASSERT_NULL(a);
ASSERT_STREQ(b, "bar");
b = mfree(b);
ASSERT_OK(split_pair("foo=bar", "=", &a, NULL));
ASSERT_STREQ(a, "foo");
ASSERT_NULL(b);
a = mfree(a);
ASSERT_OK(split_pair("foo=bar", "=", NULL, NULL));
ASSERT_NULL(a);
ASSERT_NULL(b);
/* ... but the separator must still be present */
ASSERT_ERROR(split_pair("foo", "=", NULL, NULL), EINVAL);
}
TEST(empty_to_null) {
const char *s = "asdf", *n = NULL, *e = "";
char *t = (char*) "asdf";
const char p[] = "asdf";
char q[] = "asdf";
/* empty_to_null cannot be used with constant strings, e.g.
* empty_to_null("") fails with 'error: cast specifies array type'. */
ASSERT_NULL(empty_to_null(NULL));
ASSERT_NULL(empty_to_null(n));
ASSERT_NULL(empty_to_null(e));
ASSERT_STREQ(empty_to_null(s), "asdf");
ASSERT_NULL(empty_to_null(s + 4));
ASSERT_STREQ(empty_to_null(t), "asdf");
ASSERT_NULL(empty_to_null(t + 4));
ASSERT_STREQ(empty_to_null(&p[0]), "asdf");
ASSERT_NULL(empty_to_null(&p[0] + 4));
ASSERT_STREQ(empty_to_null(&q[0]), "asdf");
ASSERT_NULL(empty_to_null(&q[0] + 4));
}
TEST(first_word) {
assert_se(first_word("Hello", ""));
assert_se(first_word("Hello", "Hello"));
assert_se(first_word("Hello world", "Hello"));
assert_se(first_word("Hello\tworld", "Hello"));
assert_se(first_word("Hello\nworld", "Hello"));
assert_se(first_word("Hello\rworld", "Hello"));
assert_se(first_word("Hello ", "Hello"));
assert_se(!first_word("Hello", "Hellooo"));
assert_se(!first_word("Hello", "xxxxx"));
assert_se(!first_word("Hellooo", "Hello"));
}
TEST(strlen_ptr) {
assert_se(strlen_ptr("foo") == 3);
assert_se(strlen_ptr("") == 0);
assert_se(strlen_ptr(NULL) == 0);
}
TEST(memory_startswith) {
ASSERT_STREQ(memory_startswith("", 0, ""), "");
ASSERT_STREQ(memory_startswith("", 1, ""), "");
ASSERT_STREQ(memory_startswith("x", 2, ""), "x");
assert_se(!memory_startswith("", 1, "x"));
assert_se(!memory_startswith("", 1, "xxxxxxxx"));
ASSERT_STREQ(memory_startswith("xxx", 4, "x"), "xx");
ASSERT_STREQ(memory_startswith("xxx", 4, "xx"), "x");
ASSERT_STREQ(memory_startswith("xxx", 4, "xxx"), "");
assert_se(!memory_startswith("xxx", 4, "xxxx"));
}
TEST(memory_startswith_no_case) {
ASSERT_STREQ(memory_startswith_no_case("", 0, ""), "");
ASSERT_STREQ(memory_startswith_no_case("", 1, ""), "");
ASSERT_STREQ(memory_startswith_no_case("x", 2, ""), "x");
ASSERT_STREQ(memory_startswith_no_case("X", 2, ""), "X");
assert_se(!memory_startswith_no_case("", 1, "X"));
assert_se(!memory_startswith_no_case("", 1, "xxxxXXXX"));
ASSERT_STREQ(memory_startswith_no_case("xxx", 4, "X"), "xx");
ASSERT_STREQ(memory_startswith_no_case("XXX", 4, "x"), "XX");
ASSERT_STREQ(memory_startswith_no_case("XXX", 4, "X"), "XX");
ASSERT_STREQ(memory_startswith_no_case("xxx", 4, "XX"), "x");
ASSERT_STREQ(memory_startswith_no_case("XXX", 4, "xx"), "X");
ASSERT_STREQ(memory_startswith_no_case("XXX", 4, "XX"), "X");
ASSERT_STREQ(memory_startswith_no_case("xxx", 4, "XXX"), "");
ASSERT_STREQ(memory_startswith_no_case("XXX", 4, "xxx"), "");
ASSERT_STREQ(memory_startswith_no_case("XXX", 4, "XXX"), "");
assert_se(memory_startswith_no_case((char[2]){'x', 'x'}, 2, "xx"));
assert_se(memory_startswith_no_case((char[2]){'x', 'X'}, 2, "xX"));
assert_se(memory_startswith_no_case((char[2]){'X', 'x'}, 2, "Xx"));
assert_se(memory_startswith_no_case((char[2]){'X', 'X'}, 2, "XX"));
}
static void test_string_truncate_lines_one(const char *input, size_t n_lines, const char *output, bool truncation) {
_cleanup_free_ char *b = NULL;
int k;
assert_se((k = string_truncate_lines(input, n_lines, &b)) >= 0);
ASSERT_STREQ(b, output);
assert_se(!!k == truncation);
}
TEST(string_truncate_lines) {
test_string_truncate_lines_one("", 0, "", false);
test_string_truncate_lines_one("", 1, "", false);
test_string_truncate_lines_one("", 2, "", false);
test_string_truncate_lines_one("", 3, "", false);
test_string_truncate_lines_one("x", 0, "", true);
test_string_truncate_lines_one("x", 1, "x", false);
test_string_truncate_lines_one("x", 2, "x", false);
test_string_truncate_lines_one("x", 3, "x", false);
test_string_truncate_lines_one("x\n", 0, "", true);
test_string_truncate_lines_one("x\n", 1, "x", false);
test_string_truncate_lines_one("x\n", 2, "x", false);
test_string_truncate_lines_one("x\n", 3, "x", false);
test_string_truncate_lines_one("x\ny", 0, "", true);
test_string_truncate_lines_one("x\ny", 1, "x", true);
test_string_truncate_lines_one("x\ny", 2, "x\ny", false);
test_string_truncate_lines_one("x\ny", 3, "x\ny", false);
test_string_truncate_lines_one("x\ny\n", 0, "", true);
test_string_truncate_lines_one("x\ny\n", 1, "x", true);
test_string_truncate_lines_one("x\ny\n", 2, "x\ny", false);
test_string_truncate_lines_one("x\ny\n", 3, "x\ny", false);
test_string_truncate_lines_one("x\ny\nz", 0, "", true);
test_string_truncate_lines_one("x\ny\nz", 1, "x", true);
test_string_truncate_lines_one("x\ny\nz", 2, "x\ny", true);
test_string_truncate_lines_one("x\ny\nz", 3, "x\ny\nz", false);
test_string_truncate_lines_one("x\ny\nz\n", 0, "", true);
test_string_truncate_lines_one("x\ny\nz\n", 1, "x", true);
test_string_truncate_lines_one("x\ny\nz\n", 2, "x\ny", true);
test_string_truncate_lines_one("x\ny\nz\n", 3, "x\ny\nz", false);
test_string_truncate_lines_one("\n", 0, "", false);
test_string_truncate_lines_one("\n", 1, "", false);
test_string_truncate_lines_one("\n", 2, "", false);
test_string_truncate_lines_one("\n", 3, "", false);
test_string_truncate_lines_one("\n\n", 0, "", false);
test_string_truncate_lines_one("\n\n", 1, "", false);
test_string_truncate_lines_one("\n\n", 2, "", false);
test_string_truncate_lines_one("\n\n", 3, "", false);
test_string_truncate_lines_one("\n\n\n", 0, "", false);
test_string_truncate_lines_one("\n\n\n", 1, "", false);
test_string_truncate_lines_one("\n\n\n", 2, "", false);
test_string_truncate_lines_one("\n\n\n", 3, "", false);
test_string_truncate_lines_one("\nx\n\n", 0, "", true);
test_string_truncate_lines_one("\nx\n\n", 1, "", true);
test_string_truncate_lines_one("\nx\n\n", 2, "\nx", false);
test_string_truncate_lines_one("\nx\n\n", 3, "\nx", false);
test_string_truncate_lines_one("\n\nx\n", 0, "", true);
test_string_truncate_lines_one("\n\nx\n", 1, "", true);
test_string_truncate_lines_one("\n\nx\n", 2, "", true);
test_string_truncate_lines_one("\n\nx\n", 3, "\n\nx", false);
}
static void test_string_extract_lines_one(const char *input, size_t i, const char *output, bool more) {
_cleanup_free_ char *b = NULL;
int k;
assert_se((k = string_extract_line(input, i, &b)) >= 0);
ASSERT_STREQ(b ?: input, output);
assert_se(!!k == more);
}
TEST(string_extract_line) {
test_string_extract_lines_one("", 0, "", false);
test_string_extract_lines_one("", 1, "", false);
test_string_extract_lines_one("", 2, "", false);
test_string_extract_lines_one("", 3, "", false);
test_string_extract_lines_one("x", 0, "x", false);
test_string_extract_lines_one("x", 1, "", false);
test_string_extract_lines_one("x", 2, "", false);
test_string_extract_lines_one("x", 3, "", false);
test_string_extract_lines_one("x\n", 0, "x", false);
test_string_extract_lines_one("x\n", 1, "", false);
test_string_extract_lines_one("x\n", 2, "", false);
test_string_extract_lines_one("x\n", 3, "", false);
test_string_extract_lines_one("x\ny", 0, "x", true);
test_string_extract_lines_one("x\ny", 1, "y", false);
test_string_extract_lines_one("x\ny", 2, "", false);
test_string_extract_lines_one("x\ny", 3, "", false);
test_string_extract_lines_one("x\ny\n", 0, "x", true);
test_string_extract_lines_one("x\ny\n", 1, "y", false);
test_string_extract_lines_one("x\ny\n", 2, "", false);
test_string_extract_lines_one("x\ny\n", 3, "", false);
test_string_extract_lines_one("x\ny\nz", 0, "x", true);
test_string_extract_lines_one("x\ny\nz", 1, "y", true);
test_string_extract_lines_one("x\ny\nz", 2, "z", false);
test_string_extract_lines_one("x\ny\nz", 3, "", false);
test_string_extract_lines_one("\n", 0, "", false);
test_string_extract_lines_one("\n", 1, "", false);
test_string_extract_lines_one("\n", 2, "", false);
test_string_extract_lines_one("\n", 3, "", false);
test_string_extract_lines_one("\n\n", 0, "", true);
test_string_extract_lines_one("\n\n", 1, "", false);
test_string_extract_lines_one("\n\n", 2, "", false);
test_string_extract_lines_one("\n\n", 3, "", false);
test_string_extract_lines_one("\n\n\n", 0, "", true);
test_string_extract_lines_one("\n\n\n", 1, "", true);
test_string_extract_lines_one("\n\n\n", 2, "", false);
test_string_extract_lines_one("\n\n\n", 3, "", false);
test_string_extract_lines_one("\n\n\n\n", 0, "", true);
test_string_extract_lines_one("\n\n\n\n", 1, "", true);
test_string_extract_lines_one("\n\n\n\n", 2, "", true);
test_string_extract_lines_one("\n\n\n\n", 3, "", false);
test_string_extract_lines_one("\nx\n\n\n", 0, "", true);
test_string_extract_lines_one("\nx\n\n\n", 1, "x", true);
test_string_extract_lines_one("\nx\n\n\n", 2, "", true);
test_string_extract_lines_one("\nx\n\n\n", 3, "", false);
test_string_extract_lines_one("\n\nx\n\n", 0, "", true);
test_string_extract_lines_one("\n\nx\n\n", 1, "", true);
test_string_extract_lines_one("\n\nx\n\n", 2, "x", true);
test_string_extract_lines_one("\n\nx\n\n", 3, "", false);
test_string_extract_lines_one("\n\n\nx\n", 0, "", true);
test_string_extract_lines_one("\n\n\nx\n", 1, "", true);
test_string_extract_lines_one("\n\n\nx\n", 2, "", true);
test_string_extract_lines_one("\n\n\nx\n", 3, "x", false);
}
TEST(string_contains_word_strv) {
const char *w;
assert_se(string_contains_word_strv("a b cc", NULL, STRV_MAKE("a", "b"), NULL));
assert_se(string_contains_word_strv("a b cc", NULL, STRV_MAKE("a", "b"), &w));
ASSERT_STREQ(w, "a");
assert_se(!string_contains_word_strv("a b cc", NULL, STRV_MAKE("d"), &w));
ASSERT_NULL(w);
assert_se(string_contains_word_strv("a b cc", NULL, STRV_MAKE("b", "a"), &w));
ASSERT_STREQ(w, "a");
assert_se(string_contains_word_strv("b a b cc", NULL, STRV_MAKE("b", "a", "b"), &w));
ASSERT_STREQ(w, "b");
assert_se(string_contains_word_strv("a b cc", NULL, STRV_MAKE("b", ""), &w));
ASSERT_STREQ(w, "b");
assert_se(!string_contains_word_strv("a b cc", NULL, STRV_MAKE(""), &w));
ASSERT_NULL(w);
assert_se(string_contains_word_strv("a b cc", " ", STRV_MAKE(""), &w));
ASSERT_STREQ(w, "");
}
TEST(string_contains_word) {
assert_se( string_contains_word("a b cc", NULL, "a"));
assert_se( string_contains_word("a b cc", NULL, "b"));
assert_se(!string_contains_word("a b cc", NULL, "c"));
assert_se( string_contains_word("a b cc", NULL, "cc"));
assert_se(!string_contains_word("a b cc", NULL, "d"));
assert_se(!string_contains_word("a b cc", NULL, "a b"));
assert_se(!string_contains_word("a b cc", NULL, "a b c"));
assert_se(!string_contains_word("a b cc", NULL, "b c"));
assert_se(!string_contains_word("a b cc", NULL, "b cc"));
assert_se(!string_contains_word("a b cc", NULL, "a "));
assert_se(!string_contains_word("a b cc", NULL, " b "));
assert_se(!string_contains_word("a b cc", NULL, " cc"));
assert_se( string_contains_word(" a b\t\tcc", NULL, "a"));
assert_se( string_contains_word(" a b\t\tcc", NULL, "b"));
assert_se(!string_contains_word(" a b\t\tcc", NULL, "c"));
assert_se( string_contains_word(" a b\t\tcc", NULL, "cc"));
assert_se(!string_contains_word(" a b\t\tcc", NULL, "d"));
assert_se(!string_contains_word(" a b\t\tcc", NULL, "a b"));
assert_se(!string_contains_word(" a b\t\tcc", NULL, "a b\t\tc"));
assert_se(!string_contains_word(" a b\t\tcc", NULL, "b\t\tc"));
assert_se(!string_contains_word(" a b\t\tcc", NULL, "b\t\tcc"));
assert_se(!string_contains_word(" a b\t\tcc", NULL, "a "));
assert_se(!string_contains_word(" a b\t\tcc", NULL, " b "));
assert_se(!string_contains_word(" a b\t\tcc", NULL, " cc"));
assert_se(!string_contains_word(" a b\t\tcc", NULL, ""));
assert_se(!string_contains_word(" a b\t\tcc", NULL, " "));
assert_se(!string_contains_word(" a b\t\tcc", NULL, " "));
assert_se( string_contains_word(" a b\t\tcc", " ", ""));
assert_se( string_contains_word(" a b\t\tcc", "\t", ""));