| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "alloc-util.h" |
| 4 |
#include "nulstr-util.h" |
| 5 |
#include "set.h" |
| 6 |
#include "strv.h" |
| 7 |
#include "tests.h" |
| 8 |
|
| 9 |
TEST(strv_split_nulstr) { |
| 10 |
_cleanup_strv_free_ char **l = NULL; |
| 11 |
const char nulstr[] = "str0\0str1\0str2\0str3\0"; |
| 12 |
|
| 13 |
l = strv_split_nulstr(nulstr); |
| 14 |
assert_se(l); |
| 15 |
|
| 16 |
ASSERT_STREQ(l[0], "str0"); |
| 17 |
ASSERT_STREQ(l[1], "str1"); |
| 18 |
ASSERT_STREQ(l[2], "str2"); |
| 19 |
ASSERT_STREQ(l[3], "str3"); |
| 20 |
} |
| 21 |
|
| 22 |
#define strv_parse_nulstr_full_one(s, n, e0, e1) \ |
| 23 |
({ \ |
| 24 |
_cleanup_strv_free_ char **v0 = NULL, **v1 = NULL; \ |
| 25 |
\ |
| 26 |
assert_se(v0 = strv_parse_nulstr_full(s, n, false)); \ |
| 27 |
assert_se(strv_equal(v0, e0)); \ |
| 28 |
assert_se(v1 = strv_parse_nulstr_full(s, n, true)); \ |
| 29 |
assert_se(strv_equal(v1, e1)); \ |
| 30 |
}) |
| 31 |
|
| 32 |
TEST(strv_parse_nulstr_full) { |
| 33 |
const char nulstr1[] = "hoge\0hoge2\0hoge3\0\0hoge5\0\0xxx"; |
| 34 |
const char nulstr2[] = "hoge\0hoge2\0hoge3\0\0hoge5\0\0xxx\0\0\0"; |
| 35 |
|
| 36 |
strv_parse_nulstr_full_one(nulstr1, sizeof(nulstr1) - 1, |
| 37 |
STRV_MAKE("hoge", "hoge2", "hoge3", "", "hoge5", "", "xxx"), |
| 38 |
STRV_MAKE("hoge", "hoge2", "hoge3", "", "hoge5", "", "xxx")); |
| 39 |
|
| 40 |
strv_parse_nulstr_full_one(nulstr2, sizeof(nulstr2) - 1, |
| 41 |
STRV_MAKE("hoge", "hoge2", "hoge3", "", "hoge5", "", "xxx", "", ""), |
| 42 |
STRV_MAKE("hoge", "hoge2", "hoge3", "", "hoge5", "", "xxx")); |
| 43 |
|
| 44 |
strv_parse_nulstr_full_one(((const char[0]) {}), 0, |
| 45 |
STRV_EMPTY, STRV_EMPTY); |
| 46 |
|
| 47 |
strv_parse_nulstr_full_one(((const char[1]) { 0 }), 1, |
| 48 |
STRV_MAKE(""), STRV_EMPTY); |
| 49 |
|
| 50 |
strv_parse_nulstr_full_one(((const char[1]) { 'x' }), 1, |
| 51 |
STRV_MAKE("x"), STRV_MAKE("x")); |
| 52 |
|
| 53 |
strv_parse_nulstr_full_one(((const char[2]) { 0, 0 }), 2, |
| 54 |
STRV_MAKE("", ""), STRV_EMPTY); |
| 55 |
|
| 56 |
strv_parse_nulstr_full_one(((const char[2]) { 'x', 0 }), 2, |
| 57 |
STRV_MAKE("x"), STRV_MAKE("x")); |
| 58 |
|
| 59 |
strv_parse_nulstr_full_one(((const char[3]) { 0, 0, 0 }), 3, |
| 60 |
STRV_MAKE("", "", ""), STRV_EMPTY); |
| 61 |
|
| 62 |
strv_parse_nulstr_full_one(((const char[3]) { 'x', 0, 0 }), 3, |
| 63 |
STRV_MAKE("x", ""), STRV_MAKE("x")); |
| 64 |
|
| 65 |
strv_parse_nulstr_full_one(((const char[3]) { 0, 'x', 0 }), 3, |
| 66 |
STRV_MAKE("", "x"), STRV_MAKE("", "x")); |
| 67 |
|
| 68 |
strv_parse_nulstr_full_one(((const char[3]) { 0, 0, 'x' }), 3, |
| 69 |
STRV_MAKE("", "", "x"), STRV_MAKE("", "", "x")); |
| 70 |
|
| 71 |
strv_parse_nulstr_full_one(((const char[3]) { 'x', 'x', 0 }), 3, |
| 72 |
STRV_MAKE("xx"), STRV_MAKE("xx")); |
| 73 |
|
| 74 |
strv_parse_nulstr_full_one(((const char[3]) { 0, 'x', 'x' }), 3, |
| 75 |
STRV_MAKE("", "xx"), STRV_MAKE("", "xx")); |
| 76 |
|
| 77 |
strv_parse_nulstr_full_one(((const char[3]) { 'x', 0, 'x' }), 3, |
| 78 |
STRV_MAKE("x", "x"), STRV_MAKE("x", "x")); |
| 79 |
|
| 80 |
strv_parse_nulstr_full_one(((const char[3]) { 'x', 'x', 'x' }), 3, |
| 81 |
STRV_MAKE("xxx"), STRV_MAKE("xxx")); |
| 82 |
} |
| 83 |
|
| 84 |
static void test_strv_make_nulstr_one(char **l) { |
| 85 |
_cleanup_free_ char *b = NULL, *c = NULL; |
| 86 |
_cleanup_strv_free_ char **q = NULL; |
| 87 |
size_t n, m; |
| 88 |
unsigned i = 0; |
| 89 |
|
| 90 |
log_info("/* %s */", __func__); |
| 91 |
|
| 92 |
assert_se(strv_make_nulstr(l, &b, &n) >= 0); |
| 93 |
assert_se(q = strv_parse_nulstr(b, n)); |
| 94 |
assert_se(strv_equal(l, q)); |
| 95 |
|
| 96 |
assert_se(strv_make_nulstr(q, &c, &m) >= 0); |
| 97 |
assert_se(memcmp_nn(b, n, c, m) == 0); |
| 98 |
|
| 99 |
NULSTR_FOREACH(s, b) |
| 100 |
ASSERT_STREQ(s, l[i++]); |
| 101 |
assert_se(i == strv_length(l)); |
| 102 |
} |
| 103 |
|
| 104 |
TEST(strv_make_nulstr) { |
| 105 |
test_strv_make_nulstr_one(NULL); |
| 106 |
test_strv_make_nulstr_one(STRV_MAKE(NULL)); |
| 107 |
test_strv_make_nulstr_one(STRV_MAKE("foo")); |
| 108 |
test_strv_make_nulstr_one(STRV_MAKE("foo", "bar")); |
| 109 |
test_strv_make_nulstr_one(STRV_MAKE("foo", "bar", "quuux")); |
| 110 |
} |
| 111 |
|
| 112 |
TEST(set_make_nulstr) { |
| 113 |
_cleanup_set_free_ Set *set = NULL; |
| 114 |
size_t len = 0; |
| 115 |
int r; |
| 116 |
|
| 117 |
{ |
| 118 |
/* Unallocated and empty set. */ |
| 119 |
static const char expect[] = { 0x00, 0x00 }; |
| 120 |
_cleanup_free_ char *nulstr = NULL; |
| 121 |
|
| 122 |
r = set_make_nulstr(set, &nulstr, &len); |
| 123 |
assert_se(r == 0); |
| 124 |
assert_se(len == 0); |
| 125 |
assert_se(memcmp(expect, nulstr, len + 2) == 0); |
| 126 |
} |
| 127 |
|
| 128 |
{ |
| 129 |
/* Allocated by empty set. */ |
| 130 |
static const char expect[] = { 0x00, 0x00 }; |
| 131 |
_cleanup_free_ char *nulstr = NULL; |
| 132 |
|
| 133 |
set = set_new(&string_hash_ops_free); |
| 134 |
assert_se(set); |
| 135 |
|
| 136 |
r = set_make_nulstr(set, &nulstr, &len); |
| 137 |
assert_se(r == 0); |
| 138 |
assert_se(len == 0); |
| 139 |
assert_se(memcmp(expect, nulstr, len + 2) == 0); |
| 140 |
} |
| 141 |
|
| 142 |
{ |
| 143 |
/* Non-empty set. */ |
| 144 |
static const char expect[] = { 'a', 'a', 'a', 0x00, 0x00 }; |
| 145 |
_cleanup_free_ char *nulstr = NULL; |
| 146 |
|
| 147 |
assert_se(set_put_strdup(&set, "aaa") >= 0); |
| 148 |
|
| 149 |
r = set_make_nulstr(set, &nulstr, &len); |
| 150 |
assert_se(r == 0); |
| 151 |
assert_se(len == 4); |
| 152 |
assert_se(memcmp(expect, nulstr, len + 1) == 0); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
static void test_strv_make_nulstr_binary_one(char **l, const char *b, size_t n) { |
| 157 |
_cleanup_strv_free_ char **z = NULL; |
| 158 |
_cleanup_free_ char *a = NULL; |
| 159 |
size_t m; |
| 160 |
|
| 161 |
assert_se(strv_make_nulstr(l, &a, &m) >= 0); |
| 162 |
assert_se(memcmp_nn(a, m, b, n) == 0); |
| 163 |
assert_se(z = strv_parse_nulstr(a, m)); |
| 164 |
assert_se(strv_equal(l, z)); |
| 165 |
} |
| 166 |
|
| 167 |
TEST(strv_make_nulstr_binary) { |
| 168 |
test_strv_make_nulstr_binary_one(NULL, (const char[0]) {}, 0); |
| 169 |
test_strv_make_nulstr_binary_one(STRV_MAKE(NULL), (const char[0]) {}, 0); |
| 170 |
test_strv_make_nulstr_binary_one(STRV_MAKE(""), (const char[1]) { 0 }, 1); |
| 171 |
test_strv_make_nulstr_binary_one(STRV_MAKE("", ""), (const char[2]) { 0, 0 }, 2); |
| 172 |
test_strv_make_nulstr_binary_one(STRV_MAKE("x", ""), (const char[3]) { 'x', 0, 0 }, 3); |
| 173 |
test_strv_make_nulstr_binary_one(STRV_MAKE("", "x"), (const char[3]) { 0, 'x', 0 }, 3); |
| 174 |
test_strv_make_nulstr_binary_one(STRV_MAKE("", "", ""), (const char[3]) { 0, 0, 0 }, 3); |
| 175 |
test_strv_make_nulstr_binary_one(STRV_MAKE("x", "", ""), (const char[4]) { 'x', 0, 0, 0 }, 4); |
| 176 |
test_strv_make_nulstr_binary_one(STRV_MAKE("", "x", ""), (const char[4]) { 0, 'x', 0, 0 }, 4); |
| 177 |
test_strv_make_nulstr_binary_one(STRV_MAKE("", "", "x"), (const char[4]) { 0, 0, 'x', 0 }, 4); |
| 178 |
test_strv_make_nulstr_binary_one(STRV_MAKE("x", "x", ""), (const char[5]) { 'x', 0, 'x', 0, 0 }, 5); |
| 179 |
test_strv_make_nulstr_binary_one(STRV_MAKE("", "x", "x"), (const char[5]) { 0, 'x', 0, 'x', 0 }, 5); |
| 180 |
test_strv_make_nulstr_binary_one(STRV_MAKE("x", "", "x"), (const char[5]) { 'x', 0, 0, 'x', 0 }, 5); |
| 181 |
test_strv_make_nulstr_binary_one(STRV_MAKE("x", "x", "x"), (const char[6]) { 'x', 0, 'x', 0, 'x', 0 }, 6); |
| 182 |
} |
| 183 |
|
| 184 |
DEFINE_TEST_MAIN(LOG_INFO); |