| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <unistd.h> |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "extract-word.h" |
| 7 |
#include "locale-util.h" |
| 8 |
#include "string-util.h" |
| 9 |
#include "strv.h" |
| 10 |
#include "tests.h" |
| 11 |
|
| 12 |
TEST(xsprintf) { |
| 13 |
char buf[5]; |
| 14 |
|
| 15 |
xsprintf(buf, "asdf"); |
| 16 |
xsprintf(buf, "%4s", "a"); |
| 17 |
xsprintf(buf, "%-4s", "a"); |
| 18 |
xsprintf(buf, "%04d", 1); |
| 19 |
|
| 20 |
ASSERT_SIGNAL(xsprintf(buf, "asdfe"), SIGABRT); |
| 21 |
ASSERT_SIGNAL(xsprintf(buf, "asdfefghdhdhdhdhd"), SIGABRT); |
| 22 |
ASSERT_SIGNAL(xsprintf(buf, "%5s", "a"), SIGABRT); |
| 23 |
} |
| 24 |
|
| 25 |
TEST(string_erase) { |
| 26 |
char *x; |
| 27 |
x = strdupa_safe(""); |
| 28 |
ASSERT_STREQ(string_erase(x), ""); |
| 29 |
|
| 30 |
x = strdupa_safe("1"); |
| 31 |
ASSERT_STREQ(string_erase(x), ""); |
| 32 |
|
| 33 |
x = strdupa_safe("123456789"); |
| 34 |
ASSERT_STREQ(string_erase(x), ""); |
| 35 |
|
| 36 |
assert_se(x[1] == '\0'); |
| 37 |
assert_se(x[2] == '\0'); |
| 38 |
assert_se(x[3] == '\0'); |
| 39 |
assert_se(x[4] == '\0'); |
| 40 |
assert_se(x[5] == '\0'); |
| 41 |
assert_se(x[6] == '\0'); |
| 42 |
assert_se(x[7] == '\0'); |
| 43 |
assert_se(x[8] == '\0'); |
| 44 |
assert_se(x[9] == '\0'); |
| 45 |
} |
| 46 |
|
| 47 |
static void test_free_and_strndup_one(char **t, const char *src, size_t l, const char *expected, bool change) { |
| 48 |
log_debug("%s: \"%s\", \"%s\", %zu (expect \"%s\", %s)", |
| 49 |
__func__, strnull(*t), strnull(src), l, strnull(expected), yes_no(change)); |
| 50 |
|
| 51 |
int r = free_and_strndup(t, src, l); |
| 52 |
ASSERT_STREQ(*t, expected); |
| 53 |
assert_se(r == change); /* check that change occurs only when necessary */ |
| 54 |
} |
| 55 |
|
| 56 |
TEST(free_and_strndup) { |
| 57 |
static const struct test_case { |
| 58 |
const char *src; |
| 59 |
size_t len; |
| 60 |
const char *expected; |
| 61 |
} cases[] = { |
| 62 |
{"abc", 0, ""}, |
| 63 |
{"abc", 0, ""}, |
| 64 |
{"abc", 1, "a"}, |
| 65 |
{"abc", 2, "ab"}, |
| 66 |
{"abc", 3, "abc"}, |
| 67 |
{"abc", 4, "abc"}, |
| 68 |
{"abc", 5, "abc"}, |
| 69 |
{"abc", 5, "abc"}, |
| 70 |
{"abc", 4, "abc"}, |
| 71 |
{"abc", 3, "abc"}, |
| 72 |
{"abc", 2, "ab"}, |
| 73 |
{"abc", 1, "a"}, |
| 74 |
{"abc", 0, ""}, |
| 75 |
|
| 76 |
{"", 0, ""}, |
| 77 |
{"", 1, ""}, |
| 78 |
{"", 2, ""}, |
| 79 |
{"", 0, ""}, |
| 80 |
{"", 1, ""}, |
| 81 |
{"", 2, ""}, |
| 82 |
{"", 2, ""}, |
| 83 |
{"", 1, ""}, |
| 84 |
{"", 0, ""}, |
| 85 |
|
| 86 |
{NULL, 0, NULL}, |
| 87 |
|
| 88 |
{"foo", 3, "foo"}, |
| 89 |
{"foobar", 6, "foobar"}, |
| 90 |
}; |
| 91 |
|
| 92 |
_cleanup_free_ char *t = NULL; |
| 93 |
const char *prev_expected = t; |
| 94 |
|
| 95 |
FOREACH_ELEMENT(c, cases) { |
| 96 |
test_free_and_strndup_one(&t, |
| 97 |
c->src, c->len, c->expected, |
| 98 |
!streq_ptr(c->expected, prev_expected)); |
| 99 |
prev_expected = t; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
TEST(strdup_to_full) { |
| 104 |
_cleanup_free_ char *dst; |
| 105 |
|
| 106 |
assert_se(strdup_to_full(NULL, NULL) == 0); |
| 107 |
assert_se(strdup_to_full(&dst, NULL) == 0); |
| 108 |
|
| 109 |
assert_se(strdup_to_full(NULL, "") == 1); |
| 110 |
assert_se(strdup_to_full(&dst, "") == 1); |
| 111 |
ASSERT_STREQ(dst, ""); |
| 112 |
dst = mfree(dst); |
| 113 |
|
| 114 |
assert_se(strdup_to_full(NULL, "x") == 1); |
| 115 |
assert_se(strdup_to_full(&dst, "x") == 1); |
| 116 |
ASSERT_STREQ(dst, "x"); |
| 117 |
} |
| 118 |
|
| 119 |
TEST(strdup_to) { |
| 120 |
_cleanup_free_ char *dst; |
| 121 |
|
| 122 |
assert_se(strdup_to(&dst, NULL) == 0); |
| 123 |
|
| 124 |
assert_se(strdup_to(&dst, "") == 0); |
| 125 |
ASSERT_STREQ(dst, ""); |
| 126 |
dst = mfree(dst); |
| 127 |
|
| 128 |
assert_se(strdup_to(&dst, "x") == 0); |
| 129 |
ASSERT_STREQ(dst, "x"); |
| 130 |
} |
| 131 |
|
| 132 |
TEST(ascii_strcasecmp_n) { |
| 133 |
assert_se(ascii_strcasecmp_n("", "", 0) == 0); |
| 134 |
assert_se(ascii_strcasecmp_n("", "", 1) == 0); |
| 135 |
assert_se(ascii_strcasecmp_n("", "a", 1) < 0); |
| 136 |
assert_se(ascii_strcasecmp_n("", "a", 2) < 0); |
| 137 |
assert_se(ascii_strcasecmp_n("a", "", 1) > 0); |
| 138 |
assert_se(ascii_strcasecmp_n("a", "", 2) > 0); |
| 139 |
assert_se(ascii_strcasecmp_n("a", "a", 1) == 0); |
| 140 |
assert_se(ascii_strcasecmp_n("a", "a", 2) == 0); |
| 141 |
assert_se(ascii_strcasecmp_n("a", "b", 1) < 0); |
| 142 |
assert_se(ascii_strcasecmp_n("a", "b", 2) < 0); |
| 143 |
assert_se(ascii_strcasecmp_n("b", "a", 1) > 0); |
| 144 |
assert_se(ascii_strcasecmp_n("b", "a", 2) > 0); |
| 145 |
assert_se(ascii_strcasecmp_n("xxxxyxxxx", "xxxxYxxxx", 9) == 0); |
| 146 |
assert_se(ascii_strcasecmp_n("xxxxxxxxx", "xxxxyxxxx", 9) < 0); |
| 147 |
assert_se(ascii_strcasecmp_n("xxxxXxxxx", "xxxxyxxxx", 9) < 0); |
| 148 |
assert_se(ascii_strcasecmp_n("xxxxxxxxx", "xxxxYxxxx", 9) < 0); |
| 149 |
assert_se(ascii_strcasecmp_n("xxxxXxxxx", "xxxxYxxxx", 9) < 0); |
| 150 |
|
| 151 |
assert_se(ascii_strcasecmp_n("xxxxYxxxx", "xxxxYxxxx", 9) == 0); |
| 152 |
assert_se(ascii_strcasecmp_n("xxxxyxxxx", "xxxxxxxxx", 9) > 0); |
| 153 |
assert_se(ascii_strcasecmp_n("xxxxyxxxx", "xxxxXxxxx", 9) > 0); |
| 154 |
assert_se(ascii_strcasecmp_n("xxxxYxxxx", "xxxxxxxxx", 9) > 0); |
| 155 |
assert_se(ascii_strcasecmp_n("xxxxYxxxx", "xxxxXxxxx", 9) > 0); |
| 156 |
} |
| 157 |
|
| 158 |
TEST(ascii_strcasecmp_nn) { |
| 159 |
assert_se(ascii_strcasecmp_nn("", 0, "", 0) == 0); |
| 160 |
assert_se(ascii_strcasecmp_nn("", 0, "", 1) < 0); |
| 161 |
assert_se(ascii_strcasecmp_nn("", 1, "", 0) > 0); |
| 162 |
assert_se(ascii_strcasecmp_nn("", 1, "", 1) == 0); |
| 163 |
|
| 164 |
assert_se(ascii_strcasecmp_nn("aaaa", 4, "aaAa", 4) == 0); |
| 165 |
assert_se(ascii_strcasecmp_nn("aaa", 3, "aaAa", 4) < 0); |
| 166 |
assert_se(ascii_strcasecmp_nn("aaa", 4, "aaAa", 4) < 0); |
| 167 |
assert_se(ascii_strcasecmp_nn("aaaa", 4, "aaA", 3) > 0); |
| 168 |
assert_se(ascii_strcasecmp_nn("aaaa", 4, "AAA", 4) > 0); |
| 169 |
|
| 170 |
assert_se(ascii_strcasecmp_nn("aaaa", 4, "bbbb", 4) < 0); |
| 171 |
assert_se(ascii_strcasecmp_nn("aaAA", 4, "BBbb", 4) < 0); |
| 172 |
assert_se(ascii_strcasecmp_nn("BBbb", 4, "aaaa", 4) > 0); |
| 173 |
} |
| 174 |
|
| 175 |
TEST(cellescape) { |
| 176 |
char buf[40]; |
| 177 |
|
| 178 |
ASSERT_STREQ(cellescape(buf, 1, ""), ""); |
| 179 |
ASSERT_STREQ(cellescape(buf, 1, "1"), ""); |
| 180 |
ASSERT_STREQ(cellescape(buf, 1, "12"), ""); |
| 181 |
|
| 182 |
ASSERT_STREQ(cellescape(buf, 2, ""), ""); |
| 183 |
ASSERT_STREQ(cellescape(buf, 2, "1"), "1"); |
| 184 |
ASSERT_STREQ(cellescape(buf, 2, "12"), "."); |
| 185 |
ASSERT_STREQ(cellescape(buf, 2, "123"), "."); |
| 186 |
|
| 187 |
ASSERT_STREQ(cellescape(buf, 3, ""), ""); |
| 188 |
ASSERT_STREQ(cellescape(buf, 3, "1"), "1"); |
| 189 |
ASSERT_STREQ(cellescape(buf, 3, "12"), "12"); |
| 190 |
ASSERT_STREQ(cellescape(buf, 3, "123"), ".."); |
| 191 |
ASSERT_STREQ(cellescape(buf, 3, "1234"), ".."); |
| 192 |
|
| 193 |
ASSERT_STREQ(cellescape(buf, 4, ""), ""); |
| 194 |
ASSERT_STREQ(cellescape(buf, 4, "1"), "1"); |
| 195 |
ASSERT_STREQ(cellescape(buf, 4, "12"), "12"); |
| 196 |
ASSERT_STREQ(cellescape(buf, 4, "123"), "123"); |
| 197 |
ASSERT_STREQ(cellescape(buf, 4, "1234"), is_locale_utf8() ? "…" : "..."); |
| 198 |
ASSERT_STREQ(cellescape(buf, 4, "12345"), is_locale_utf8() ? "…" : "..."); |
| 199 |
|
| 200 |
ASSERT_STREQ(cellescape(buf, 5, ""), ""); |
| 201 |
ASSERT_STREQ(cellescape(buf, 5, "1"), "1"); |
| 202 |
ASSERT_STREQ(cellescape(buf, 5, "12"), "12"); |
| 203 |
ASSERT_STREQ(cellescape(buf, 5, "123"), "123"); |
| 204 |
ASSERT_STREQ(cellescape(buf, 5, "1234"), "1234"); |
| 205 |
ASSERT_STREQ(cellescape(buf, 5, "12345"), is_locale_utf8() ? "1…" : "1..."); |
| 206 |
ASSERT_STREQ(cellescape(buf, 5, "123456"), is_locale_utf8() ? "1…" : "1..."); |
| 207 |
|
| 208 |
ASSERT_STREQ(cellescape(buf, 1, "\020"), ""); |
| 209 |
ASSERT_STREQ(cellescape(buf, 2, "\020"), "."); |
| 210 |
ASSERT_STREQ(cellescape(buf, 3, "\020"), ".."); |
| 211 |
ASSERT_STREQ(cellescape(buf, 4, "\020"), is_locale_utf8() ? "…" : "..."); |
| 212 |
ASSERT_STREQ(cellescape(buf, 5, "\020"), "\\020"); |
| 213 |
|
| 214 |
ASSERT_STREQ(cellescape(buf, 5, "1234\020"), is_locale_utf8() ? "1…" : "1..."); |
| 215 |
ASSERT_STREQ(cellescape(buf, 6, "1234\020"), is_locale_utf8() ? "12…" : "12..."); |
| 216 |
ASSERT_STREQ(cellescape(buf, 7, "1234\020"), is_locale_utf8() ? "123…" : "123..."); |
| 217 |
ASSERT_STREQ(cellescape(buf, 8, "1234\020"), is_locale_utf8() ? "1234…" : "1234..."); |
| 218 |
ASSERT_STREQ(cellescape(buf, 9, "1234\020"), "1234\\020"); |
| 219 |
|
| 220 |
ASSERT_STREQ(cellescape(buf, 1, "\t\n"), ""); |
| 221 |
ASSERT_STREQ(cellescape(buf, 2, "\t\n"), "."); |
| 222 |
ASSERT_STREQ(cellescape(buf, 3, "\t\n"), ".."); |
| 223 |
ASSERT_STREQ(cellescape(buf, 4, "\t\n"), is_locale_utf8() ? "…" : "..."); |
| 224 |
ASSERT_STREQ(cellescape(buf, 5, "\t\n"), "\\t\\n"); |
| 225 |
|
| 226 |
ASSERT_STREQ(cellescape(buf, 5, "1234\t\n"), is_locale_utf8() ? "1…" : "1..."); |
| 227 |
ASSERT_STREQ(cellescape(buf, 6, "1234\t\n"), is_locale_utf8() ? "12…" : "12..."); |
| 228 |
ASSERT_STREQ(cellescape(buf, 7, "1234\t\n"), is_locale_utf8() ? "123…" : "123..."); |
| 229 |
ASSERT_STREQ(cellescape(buf, 8, "1234\t\n"), is_locale_utf8() ? "1234…" : "1234..."); |
| 230 |
ASSERT_STREQ(cellescape(buf, 9, "1234\t\n"), "1234\\t\\n"); |
| 231 |
|
| 232 |
ASSERT_STREQ(cellescape(buf, 4, "x\t\020\n"), is_locale_utf8() ? "…" : "..."); |
| 233 |
ASSERT_STREQ(cellescape(buf, 5, "x\t\020\n"), is_locale_utf8() ? "x…" : "x..."); |
| 234 |
ASSERT_STREQ(cellescape(buf, 6, "x\t\020\n"), is_locale_utf8() ? "x…" : "x..."); |
| 235 |
ASSERT_STREQ(cellescape(buf, 7, "x\t\020\n"), is_locale_utf8() ? "x\\t…" : "x\\t..."); |
| 236 |
ASSERT_STREQ(cellescape(buf, 8, "x\t\020\n"), is_locale_utf8() ? "x\\t…" : "x\\t..."); |
| 237 |
ASSERT_STREQ(cellescape(buf, 9, "x\t\020\n"), is_locale_utf8() ? "x\\t…" : "x\\t..."); |
| 238 |
ASSERT_STREQ(cellescape(buf, 10, "x\t\020\n"), "x\\t\\020\\n"); |
| 239 |
|
| 240 |
ASSERT_STREQ(cellescape(buf, 6, "1\011"), "1\\t"); |
| 241 |
ASSERT_STREQ(cellescape(buf, 6, "1\020"), "1\\020"); |
| 242 |
ASSERT_STREQ(cellescape(buf, 6, "1\020x"), is_locale_utf8() ? "1…" : "1..."); |
| 243 |
|
| 244 |
ASSERT_STREQ(cellescape(buf, 40, "1\020"), "1\\020"); |
| 245 |
ASSERT_STREQ(cellescape(buf, 40, "1\020x"), "1\\020x"); |
| 246 |
|
| 247 |
ASSERT_STREQ(cellescape(buf, 40, "\a\b\f\n\r\t\v\\\"'"), "\\a\\b\\f\\n\\r\\t\\v\\\\\\\"\\'"); |
| 248 |
ASSERT_STREQ(cellescape(buf, 6, "\a\b\f\n\r\t\v\\\"'"), is_locale_utf8() ? "\\a…" : "\\a..."); |
| 249 |
ASSERT_STREQ(cellescape(buf, 7, "\a\b\f\n\r\t\v\\\"'"), is_locale_utf8() ? "\\a…" : "\\a..."); |
| 250 |
ASSERT_STREQ(cellescape(buf, 8, "\a\b\f\n\r\t\v\\\"'"), is_locale_utf8() ? "\\a\\b…" : "\\a\\b..."); |
| 251 |
|
| 252 |
ASSERT_STREQ(cellescape(buf, sizeof buf, "1\020"), "1\\020"); |
| 253 |
ASSERT_STREQ(cellescape(buf, sizeof buf, "1\020x"), "1\\020x"); |
| 254 |
} |
| 255 |
|
| 256 |
TEST(streq_ptr) { |
| 257 |
assert_se(streq_ptr(NULL, NULL)); |
| 258 |
assert_se(!streq_ptr("abc", "cdef")); |
| 259 |
} |
| 260 |
|
| 261 |
TEST(strstrip) { |
| 262 |
char *ret, input[] = " hello, waldo. "; |
| 263 |
|
| 264 |
ret = strstrip(input); |
| 265 |
ASSERT_STREQ(ret, "hello, waldo."); |
| 266 |
} |
| 267 |
|
| 268 |
TEST(strextend) { |
| 269 |
_cleanup_free_ char *str = NULL; |
| 270 |
|
| 271 |
assert_se(strextend(&str, NULL)); |
| 272 |
ASSERT_STREQ(str, ""); |
| 273 |
assert_se(strextend(&str, "", "0", "", "", "123")); |
| 274 |
ASSERT_STREQ(str, "0123"); |
| 275 |
assert_se(strextend(&str, "456", "78", "9")); |
| 276 |
ASSERT_STREQ(str, "0123456789"); |
| 277 |
|
| 278 |
assert_se(strextend(&str, "more", NULL, "huch")); |
| 279 |
ASSERT_STREQ(str, "0123456789more"); |
| 280 |
|
| 281 |
assert_se(strextend(&str, "MORE", POINTER_MAX, "HUCH")); |
| 282 |
ASSERT_STREQ(str, "0123456789moreMOREHUCH"); |
| 283 |
} |
| 284 |
|
| 285 |
TEST(strextend_with_separator) { |
| 286 |
_cleanup_free_ char *str = NULL; |
| 287 |
|
| 288 |
assert_se(strextend_with_separator(&str, NULL, NULL)); |
| 289 |
ASSERT_STREQ(str, ""); |
| 290 |
str = mfree(str); |
| 291 |
|
| 292 |
assert_se(strextend_with_separator(&str, "...", NULL)); |
| 293 |
ASSERT_STREQ(str, ""); |
| 294 |
assert_se(strextend_with_separator(&str, "...", NULL)); |
| 295 |
ASSERT_STREQ(str, ""); |
| 296 |
str = mfree(str); |
| 297 |
|
| 298 |
assert_se(strextend_with_separator(&str, "xyz", "a", "bb", "ccc")); |
| 299 |
ASSERT_STREQ(str, "axyzbbxyzccc"); |
| 300 |
str = mfree(str); |
| 301 |
|
| 302 |
assert_se(strextend_with_separator(&str, ",", "start", "", "1", "234")); |
| 303 |
ASSERT_STREQ(str, "start,,1,234"); |
| 304 |
assert_se(strextend_with_separator(&str, ";", "more", "5", "678")); |
| 305 |
ASSERT_STREQ(str, "start,,1,234;more;5;678"); |
| 306 |
|
| 307 |
assert_se(strextend_with_separator(&str, ";", "xxxx", POINTER_MAX, "yyy")); |
| 308 |
ASSERT_STREQ(str, "start,,1,234;more;5;678;xxxx;yyy"); |
| 309 |
} |
| 310 |
|
| 311 |
TEST(strrep) { |
| 312 |
_cleanup_free_ char *one = NULL, *three = NULL, *zero = NULL; |
| 313 |
char *onea, *threea; |
| 314 |
|
| 315 |
one = strrep("waldo", 1); |
| 316 |
three = strrep("waldo", 3); |
| 317 |
zero = strrep("waldo", 0); |
| 318 |
|
| 319 |
ASSERT_STREQ(one, "waldo"); |
| 320 |
ASSERT_STREQ(three, "waldowaldowaldo"); |
| 321 |
ASSERT_STREQ(zero, ""); |
| 322 |
|
| 323 |
onea = strrepa("waldo", 1); |
| 324 |
threea = strrepa("waldo", 3); |
| 325 |
|
| 326 |
ASSERT_STREQ(onea, "waldo"); |
| 327 |
ASSERT_STREQ(threea, "waldowaldowaldo"); |
| 328 |
|
| 329 |
ASSERT_NULL(strrep("waldo", SIZE_MAX - 1)); |
| 330 |
} |
| 331 |
|
| 332 |
TEST(string_has_cc) { |
| 333 |
assert_se(string_has_cc("abc\1", NULL)); |
| 334 |
assert_se(string_has_cc("abc\x7f", NULL)); |
| 335 |
assert_se(string_has_cc("abc\x7f", NULL)); |
| 336 |
assert_se(string_has_cc("abc\t\x7f", "\t")); |
| 337 |
assert_se(string_has_cc("abc\t\x7f", "\t")); |
| 338 |
assert_se(string_has_cc("\x7f", "\t")); |
| 339 |
assert_se(string_has_cc("\x7f", "\t\a")); |
| 340 |
|
| 341 |
assert_se(!string_has_cc("abc\t\t", "\t")); |
| 342 |
assert_se(!string_has_cc("abc\t\t\a", "\t\a")); |
| 343 |
assert_se(!string_has_cc("a\ab\tc", "\t\a")); |
| 344 |
} |
| 345 |
|
| 346 |
TEST(ascii_strlower) { |
| 347 |
char a[] = "AabBcC Jk Ii Od LKJJJ kkd LK"; |
| 348 |
ASSERT_STREQ(ascii_strlower(a), "aabbcc jk ii od lkjjj kkd lk"); |
| 349 |
} |
| 350 |
|
| 351 |
TEST(strshorten) { |
| 352 |
char s[] = "foobar"; |
| 353 |
|
| 354 |
assert_se(strlen(strshorten(s, SIZE_MAX)) == 6); |
| 355 |
assert_se(strlen(strshorten(s, SIZE_MAX-1)) == 6); |
| 356 |
assert_se(strlen(strshorten(s, SIZE_MAX-2)) == 6); |
| 357 |
assert_se(strlen(strshorten(s, 6)) == 6); |
| 358 |
assert_se(strlen(strshorten(s, 7)) == 6); |
| 359 |
assert_se(strlen(strshorten(s, 12)) == 6); |
| 360 |
assert_se(strlen(strshorten(s, 5)) == 5); |
| 361 |
assert_se(strlen(strshorten(s, 2)) == 2); |
| 362 |
assert_se(strlen(strshorten(s, 0)) == 0); |
| 363 |
} |
| 364 |
|
| 365 |
TEST(strjoina) { |
| 366 |
char *actual; |
| 367 |
|
| 368 |
actual = strjoina("", "foo", "bar"); |
| 369 |
ASSERT_STREQ(actual, "foobar"); |
| 370 |
|
| 371 |
actual = strjoina("foo", "bar", "baz"); |
| 372 |
ASSERT_STREQ(actual, "foobarbaz"); |
| 373 |
|
| 374 |
actual = strjoina("foo", "", "bar", "baz"); |
| 375 |
ASSERT_STREQ(actual, "foobarbaz"); |
| 376 |
|
| 377 |
actual = strjoina("foo"); |
| 378 |
ASSERT_STREQ(actual, "foo"); |
| 379 |
|
| 380 |
actual = strjoina(NULL); |
| 381 |
ASSERT_STREQ(actual, ""); |
| 382 |
|
| 383 |
actual = strjoina(NULL, "foo"); |
| 384 |
ASSERT_STREQ(actual, ""); |
| 385 |
|
| 386 |
actual = strjoina("foo", NULL, "bar"); |
| 387 |
ASSERT_STREQ(actual, "foo"); |
| 388 |
|
| 389 |
actual = strjoina("/sys/fs/cgroup/", "dn", "/a/b/c", "/cgroup.procs"); |
| 390 |
ASSERT_STREQ(actual, "/sys/fs/cgroup/dn/a/b/c/cgroup.procs"); |
| 391 |
|
| 392 |
actual = strjoina("/sys/fs/cgroup/", "dn", NULL, NULL); |
| 393 |
ASSERT_STREQ(actual, "/sys/fs/cgroup/dn"); |
| 394 |
} |
| 395 |
|
| 396 |
TEST(strjoin) { |
| 397 |
char *actual; |
| 398 |
|
| 399 |
actual = strjoin("", "foo", "bar"); |
| 400 |
ASSERT_STREQ(actual, "foobar"); |
| 401 |
free(actual); |
| 402 |
|
| 403 |
actual = strjoin("foo", "bar", "baz"); |
| 404 |
ASSERT_STREQ(actual, "foobarbaz"); |
| 405 |
free(actual); |
| 406 |
|
| 407 |
actual = strjoin("foo", "", "bar", "baz"); |
| 408 |
ASSERT_STREQ(actual, "foobarbaz"); |
| 409 |
free(actual); |
| 410 |
|
| 411 |
actual = strjoin("foo", NULL); |
| 412 |
ASSERT_STREQ(actual, "foo"); |
| 413 |
free(actual); |
| 414 |
|
| 415 |
actual = strjoin(NULL, NULL); |
| 416 |
ASSERT_STREQ(actual, ""); |
| 417 |
free(actual); |
| 418 |
|
| 419 |
actual = strjoin(NULL, "foo"); |
| 420 |
ASSERT_STREQ(actual, ""); |
| 421 |
free(actual); |
| 422 |
|
| 423 |
actual = strjoin("foo", NULL, "bar"); |
| 424 |
ASSERT_STREQ(actual, "foo"); |
| 425 |
free(actual); |
| 426 |
|
| 427 |
actual = strjoin("foo", POINTER_MAX, "bar"); |
| 428 |
ASSERT_STREQ(actual, "foobar"); |
| 429 |
free(actual); |
| 430 |
} |
| 431 |
|
| 432 |
TEST(strcmp_ptr) { |
| 433 |
assert_se(strcmp_ptr(NULL, NULL) == 0); |
| 434 |
assert_se(strcmp_ptr("", NULL) > 0); |
| 435 |
assert_se(strcmp_ptr("foo", NULL) > 0); |
| 436 |
assert_se(strcmp_ptr(NULL, "") < 0); |
| 437 |
assert_se(strcmp_ptr(NULL, "bar") < 0); |
| 438 |
assert_se(strcmp_ptr("foo", "bar") > 0); |
| 439 |
assert_se(strcmp_ptr("bar", "baz") < 0); |
| 440 |
assert_se(strcmp_ptr("foo", "foo") == 0); |
| 441 |
assert_se(strcmp_ptr("", "") == 0); |
| 442 |
} |
| 443 |
|
| 444 |
TEST(foreach_word) { |
| 445 |
const char *test = "test abc d\te f "; |
| 446 |
const char * const expected[] = { |
| 447 |
"test", |
| 448 |
"abc", |
| 449 |
"d", |
| 450 |
"e", |
| 451 |
"f", |
| 452 |
}; |
| 453 |
|
| 454 |
size_t i = 0; |
| 455 |
int r; |
| 456 |
for (const char *p = test;;) { |
| 457 |
_cleanup_free_ char *word = NULL; |
| 458 |
|
| 459 |
r = extract_first_word(&p, &word, NULL, 0); |
| 460 |
if (r == 0) { |
| 461 |
assert_se(i == ELEMENTSOF(expected)); |
| 462 |
break; |
| 463 |
} |
| 464 |
assert_se(r > 0); |
| 465 |
|
| 466 |
ASSERT_STREQ(expected[i++], word); |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
static void check(const char *test, char** expected, bool trailing) { |
| 471 |
size_t i = 0; |
| 472 |
int r; |
| 473 |
|
| 474 |
printf("<<<%s>>>\n", test); |
| 475 |
for (;;) { |
| 476 |
_cleanup_free_ char *word = NULL; |
| 477 |
|
| 478 |
r = extract_first_word(&test, &word, NULL, EXTRACT_UNQUOTE); |
| 479 |
if (r == 0) { |
| 480 |
assert_se(!trailing); |
| 481 |
break; |
| 482 |
} else if (r < 0) { |
| 483 |
assert_se(trailing); |
| 484 |
break; |
| 485 |
} |
| 486 |
|
| 487 |
ASSERT_STREQ(word, expected[i++]); |
| 488 |
printf("<%s>\n", word); |
| 489 |
} |
| 490 |
ASSERT_NULL(expected[i]); |
| 491 |
} |
| 492 |
|
| 493 |
TEST(foreach_word_quoted) { |
| 494 |
check("test a b c 'd' e '' '' hhh '' '' \"a b c\"", |
| 495 |
STRV_MAKE("test", |
| 496 |
"a", |
| 497 |
"b", |
| 498 |
"c", |
| 499 |
"d", |
| 500 |
"e", |
| 501 |
"", |
| 502 |
"", |
| 503 |
"hhh", |
| 504 |
"", |
| 505 |
"", |
| 506 |
"a b c"), |
| 507 |
false); |
| 508 |
|
| 509 |
check("test \"xxx", |
| 510 |
STRV_MAKE("test"), |
| 511 |
true); |
| 512 |
|
| 513 |
check("test\\", |
| 514 |
STRV_EMPTY, |
| 515 |
true); |
| 516 |
} |
| 517 |
|
| 518 |
TEST(endswith) { |
| 519 |
assert_se(endswith("foobar", "bar")); |
| 520 |
assert_se(endswith("foobar", "")); |
| 521 |
assert_se(endswith("foobar", "foobar")); |
| 522 |
assert_se(endswith("", "")); |
| 523 |
|
| 524 |
assert_se(!endswith("foobar", "foo")); |
| 525 |
assert_se(!endswith("foobar", "foobarfoofoo")); |
| 526 |
} |
| 527 |
|
| 528 |
TEST(endswith_no_case) { |
| 529 |
assert_se(endswith_no_case("fooBAR", "bar")); |
| 530 |
assert_se(endswith_no_case("foobar", "")); |
| 531 |
assert_se(endswith_no_case("foobar", "FOOBAR")); |
| 532 |
assert_se(endswith_no_case("", "")); |
| 533 |
|
| 534 |
assert_se(!endswith_no_case("foobar", "FOO")); |
| 535 |
assert_se(!endswith_no_case("foobar", "FOOBARFOOFOO")); |
| 536 |
} |
| 537 |
|
| 538 |
TEST(strrstr_no_case) { |
| 539 |
ASSERT_STREQ(strrstr_no_case("fooBARfoobar", "bar"), "bar"); |
| 540 |
ASSERT_STREQ(strrstr_no_case("fooBARfoobar", "BAR"), "bar"); |
| 541 |
ASSERT_STREQ(strrstr_no_case("fooBARfoobar", "bAR"), "bar"); |
| 542 |
ASSERT_STREQ(strrstr_no_case("fooBARfoobar", "FOO"), "foobar"); |
| 543 |
ASSERT_STREQ(strrstr_no_case("fooBARfoobar", "foo"), "foobar"); |
| 544 |
ASSERT_STREQ(strrstr_no_case("fooBARfoobar", "FoO"), "foobar"); |
| 545 |
ASSERT_STREQ(strrstr_no_case("aXaXa", "x"), "Xa"); |
| 546 |
ASSERT_STREQ(strrstr_no_case("aXaXa", "X"), "Xa"); |
| 547 |
ASSERT_STREQ(strrstr_no_case("xHello", "hello"), "Hello"); |
| 548 |
ASSERT_STREQ(strrstr_no_case("Hello", "l"), "lo"); |
| 549 |
ASSERT_STREQ(strrstr_no_case("Hello", ""), ""); |
| 550 |
ASSERT_STREQ(strrstr_no_case("", ""), ""); |
| 551 |
ASSERT_STREQ(strrstr_no_case("FOO", "foo"), "FOO"); |
| 552 |
ASSERT_STREQ(strrstr_no_case("hello", "hello"), "hello"); |
| 553 |
ASSERT_STREQ(strrstr_no_case("X", "x"), "X"); |
| 554 |
|
| 555 |
ASSERT_NULL(strrstr_no_case("hello", "xyz")); |
| 556 |
ASSERT_NULL(strrstr_no_case("", "x")); |
| 557 |
ASSERT_NULL(strrstr_no_case(NULL, "x")); |
| 558 |
ASSERT_NULL(strrstr_no_case("x", NULL)); |
| 559 |
} |
| 560 |
|
| 561 |
TEST(delete_chars) { |
| 562 |
char *s, input[] = " hello, waldo. abc"; |
| 563 |
|
| 564 |
s = delete_chars(input, WHITESPACE); |
| 565 |
ASSERT_STREQ(s, "hello,waldo.abc"); |
| 566 |
assert_se(s == input); |
| 567 |
} |
| 568 |
|
| 569 |
TEST(delete_trailing_chars) { |
| 570 |
char *s, |
| 571 |
input1[] = " \n \r k \n \r ", |
| 572 |
input2[] = "kkkkthiskkkiskkkaktestkkk", |
| 573 |
input3[] = "abcdef"; |
| 574 |
|
| 575 |
s = delete_trailing_chars(input1, WHITESPACE); |
| 576 |
ASSERT_STREQ(s, " \n \r k"); |
| 577 |
assert_se(s == input1); |
| 578 |
|
| 579 |
s = delete_trailing_chars(input2, "kt"); |
| 580 |
ASSERT_STREQ(s, "kkkkthiskkkiskkkaktes"); |
| 581 |
assert_se(s == input2); |
| 582 |
|
| 583 |
s = delete_trailing_chars(input3, WHITESPACE); |
| 584 |
ASSERT_STREQ(s, "abcdef"); |
| 585 |
assert_se(s == input3); |
| 586 |
|
| 587 |
s = delete_trailing_chars(input3, "fe"); |
| 588 |
ASSERT_STREQ(s, "abcd"); |
| 589 |
assert_se(s == input3); |
| 590 |
} |
| 591 |
|
| 592 |
TEST(delete_trailing_slashes) { |
| 593 |
char s1[] = "foobar//", |
| 594 |
s2[] = "foobar/", |
| 595 |
s3[] = "foobar", |
| 596 |
s4[] = ""; |
| 597 |
|
| 598 |
ASSERT_STREQ(delete_trailing_chars(s1, "_"), "foobar//"); |
| 599 |
ASSERT_STREQ(delete_trailing_chars(s1, "/"), "foobar"); |
| 600 |
ASSERT_STREQ(delete_trailing_chars(s2, "/"), "foobar"); |
| 601 |
ASSERT_STREQ(delete_trailing_chars(s3, "/"), "foobar"); |
| 602 |
ASSERT_STREQ(delete_trailing_chars(s4, "/"), ""); |
| 603 |
} |
| 604 |
|
| 605 |
TEST(skip_leading_chars) { |
| 606 |
char input1[] = " \n \r k \n \r ", |
| 607 |
input2[] = "kkkkthiskkkiskkkaktestkkk", |
| 608 |
input3[] = "abcdef"; |
| 609 |
|
| 610 |
ASSERT_STREQ(skip_leading_chars(input1, WHITESPACE), "k \n \r "); |
| 611 |
ASSERT_STREQ(skip_leading_chars(input2, "k"), "thiskkkiskkkaktestkkk"); |
| 612 |
ASSERT_STREQ(skip_leading_chars(input2, "tk"), "hiskkkiskkkaktestkkk"); |
| 613 |
ASSERT_STREQ(skip_leading_chars(input3, WHITESPACE), "abcdef"); |
| 614 |
ASSERT_STREQ(skip_leading_chars(input3, "bcaef"), "def"); |
| 615 |
} |
| 616 |
|
| 617 |
TEST(in_charset) { |
| 618 |
assert_se(in_charset("dddaaabbbcccc", "abcd")); |
| 619 |
assert_se(!in_charset("dddaaabbbcccc", "abc f")); |
| 620 |
} |
| 621 |
|
| 622 |
TEST(split_pair) { |
| 623 |
_cleanup_free_ char *a = NULL, *b = NULL; |
| 624 |
|
| 625 |
ASSERT_SIGNAL(split_pair("", NULL, &a, &b), SIGABRT); |
| 626 |
ASSERT_SIGNAL(split_pair("", "", &a, &b), SIGABRT); |
| 627 |
ASSERT_SIGNAL(split_pair("foo=bar", "", &a, &b), SIGABRT); |
| 628 |
ASSERT_SIGNAL(split_pair(NULL, "=", &a, &b), SIGABRT); |
| 629 |
ASSERT_ERROR(split_pair("", "=", &a, &b), EINVAL); |
| 630 |
ASSERT_OK(split_pair("foo=bar", "=", &a, &b)); |
| 631 |
ASSERT_STREQ(a, "foo"); |
| 632 |
ASSERT_STREQ(b, "bar"); |
| 633 |
a = mfree(a); |
| 634 |
b = mfree(b); |
| 635 |
ASSERT_OK(split_pair("==", "==", &a, &b)); |
| 636 |
ASSERT_STREQ(a, ""); |
| 637 |
ASSERT_STREQ(b, ""); |
| 638 |
a = mfree(a); |
| 639 |
b = mfree(b); |
| 640 |
ASSERT_OK(split_pair("===", "==", &a, &b)); |
| 641 |
ASSERT_STREQ(a, ""); |
| 642 |
ASSERT_STREQ(b, "="); |
| 643 |
} |
| 644 |
|
| 645 |
TEST(empty_to_null) { |
| 646 |
const char *s = "asdf", *n = NULL, *e = ""; |
| 647 |
char *t = (char*) "asdf"; |
| 648 |
const char p[] = "asdf"; |
| 649 |
char q[] = "asdf"; |
| 650 |
|
| 651 |
/* empty_to_null cannot be used with constant strings, e.g. |
| 652 |
* empty_to_null("") fails with 'error: cast specifies array type'. */ |
| 653 |
|
| 654 |
ASSERT_NULL(empty_to_null(NULL)); |
| 655 |
ASSERT_NULL(empty_to_null(n)); |
| 656 |
ASSERT_NULL(empty_to_null(e)); |
| 657 |
ASSERT_STREQ(empty_to_null(s), "asdf"); |
| 658 |
ASSERT_NULL(empty_to_null(s + 4)); |
| 659 |
ASSERT_STREQ(empty_to_null(t), "asdf"); |
| 660 |
ASSERT_NULL(empty_to_null(t + 4)); |
| 661 |
ASSERT_STREQ(empty_to_null(&p[0]), "asdf"); |
| 662 |
ASSERT_NULL(empty_to_null(&p[0] + 4)); |
| 663 |
ASSERT_STREQ(empty_to_null(&q[0]), "asdf"); |
| 664 |
ASSERT_NULL(empty_to_null(&q[0] + 4)); |
| 665 |
} |
| 666 |
|
| 667 |
TEST(first_word) { |
| 668 |
assert_se(first_word("Hello", "")); |
| 669 |
assert_se(first_word("Hello", "Hello")); |
| 670 |
assert_se(first_word("Hello world", "Hello")); |
| 671 |
assert_se(first_word("Hello\tworld", "Hello")); |
| 672 |
assert_se(first_word("Hello\nworld", "Hello")); |
| 673 |
assert_se(first_word("Hello\rworld", "Hello")); |
| 674 |
assert_se(first_word("Hello ", "Hello")); |
| 675 |
|
| 676 |
assert_se(!first_word("Hello", "Hellooo")); |
| 677 |
assert_se(!first_word("Hello", "xxxxx")); |
| 678 |
assert_se(!first_word("Hellooo", "Hello")); |
| 679 |
} |
| 680 |
|
| 681 |
TEST(strlen_ptr) { |
| 682 |
assert_se(strlen_ptr("foo") == 3); |
| 683 |
assert_se(strlen_ptr("") == 0); |
| 684 |
assert_se(strlen_ptr(NULL) == 0); |
| 685 |
} |
| 686 |
|
| 687 |
TEST(memory_startswith) { |
| 688 |
ASSERT_STREQ(memory_startswith("", 0, ""), ""); |
| 689 |
ASSERT_STREQ(memory_startswith("", 1, ""), ""); |
| 690 |
ASSERT_STREQ(memory_startswith("x", 2, ""), "x"); |
| 691 |
assert_se(!memory_startswith("", 1, "x")); |
| 692 |
assert_se(!memory_startswith("", 1, "xxxxxxxx")); |
| 693 |
ASSERT_STREQ(memory_startswith("xxx", 4, "x"), "xx"); |
| 694 |
ASSERT_STREQ(memory_startswith("xxx", 4, "xx"), "x"); |
| 695 |
ASSERT_STREQ(memory_startswith("xxx", 4, "xxx"), ""); |
| 696 |
assert_se(!memory_startswith("xxx", 4, "xxxx")); |
| 697 |
} |
| 698 |
|
| 699 |
TEST(memory_startswith_no_case) { |
| 700 |
ASSERT_STREQ(memory_startswith_no_case("", 0, ""), ""); |
| 701 |
ASSERT_STREQ(memory_startswith_no_case("", 1, ""), ""); |
| 702 |
ASSERT_STREQ(memory_startswith_no_case("x", 2, ""), "x"); |
| 703 |
ASSERT_STREQ(memory_startswith_no_case("X", 2, ""), "X"); |
| 704 |
assert_se(!memory_startswith_no_case("", 1, "X")); |
| 705 |
assert_se(!memory_startswith_no_case("", 1, "xxxxXXXX")); |
| 706 |
ASSERT_STREQ(memory_startswith_no_case("xxx", 4, "X"), "xx"); |
| 707 |
ASSERT_STREQ(memory_startswith_no_case("XXX", 4, "x"), "XX"); |
| 708 |
ASSERT_STREQ(memory_startswith_no_case("XXX", 4, "X"), "XX"); |
| 709 |
ASSERT_STREQ(memory_startswith_no_case("xxx", 4, "XX"), "x"); |
| 710 |
ASSERT_STREQ(memory_startswith_no_case("XXX", 4, "xx"), "X"); |
| 711 |
ASSERT_STREQ(memory_startswith_no_case("XXX", 4, "XX"), "X"); |
| 712 |
ASSERT_STREQ(memory_startswith_no_case("xxx", 4, "XXX"), ""); |
| 713 |
ASSERT_STREQ(memory_startswith_no_case("XXX", 4, "xxx"), ""); |
| 714 |
ASSERT_STREQ(memory_startswith_no_case("XXX", 4, "XXX"), ""); |
| 715 |
|
| 716 |
assert_se(memory_startswith_no_case((char[2]){'x', 'x'}, 2, "xx")); |
| 717 |
assert_se(memory_startswith_no_case((char[2]){'x', 'X'}, 2, "xX")); |
| 718 |
assert_se(memory_startswith_no_case((char[2]){'X', 'x'}, 2, "Xx")); |
| 719 |
assert_se(memory_startswith_no_case((char[2]){'X', 'X'}, 2, "XX")); |
| 720 |
} |
| 721 |
|
| 722 |
static void test_string_truncate_lines_one(const char *input, size_t n_lines, const char *output, bool truncation) { |
| 723 |
_cleanup_free_ char *b = NULL; |
| 724 |
int k; |
| 725 |
|
| 726 |
assert_se((k = string_truncate_lines(input, n_lines, &b)) >= 0); |
| 727 |
ASSERT_STREQ(b, output); |
| 728 |
assert_se(!!k == truncation); |
| 729 |
} |
| 730 |
|
| 731 |
TEST(string_truncate_lines) { |
| 732 |
test_string_truncate_lines_one("", 0, "", false); |
| 733 |
test_string_truncate_lines_one("", 1, "", false); |
| 734 |
test_string_truncate_lines_one("", 2, "", false); |
| 735 |
test_string_truncate_lines_one("", 3, "", false); |
| 736 |
|
| 737 |
test_string_truncate_lines_one("x", 0, "", true); |
| 738 |
test_string_truncate_lines_one("x", 1, "x", false); |
| 739 |
test_string_truncate_lines_one("x", 2, "x", false); |
| 740 |
test_string_truncate_lines_one("x", 3, "x", false); |
| 741 |
|
| 742 |
test_string_truncate_lines_one("x\n", 0, "", true); |
| 743 |
test_string_truncate_lines_one("x\n", 1, "x", false); |
| 744 |
test_string_truncate_lines_one("x\n", 2, "x", false); |
| 745 |
test_string_truncate_lines_one("x\n", 3, "x", false); |
| 746 |
|
| 747 |
test_string_truncate_lines_one("x\ny", 0, "", true); |
| 748 |
test_string_truncate_lines_one("x\ny", 1, "x", true); |
| 749 |
test_string_truncate_lines_one("x\ny", 2, "x\ny", false); |
| 750 |
test_string_truncate_lines_one("x\ny", 3, "x\ny", false); |
| 751 |
|
| 752 |
test_string_truncate_lines_one("x\ny\n", 0, "", true); |
| 753 |
test_string_truncate_lines_one("x\ny\n", 1, "x", true); |
| 754 |
test_string_truncate_lines_one("x\ny\n", 2, "x\ny", false); |
| 755 |
test_string_truncate_lines_one("x\ny\n", 3, "x\ny", false); |
| 756 |
|
| 757 |
test_string_truncate_lines_one("x\ny\nz", 0, "", true); |
| 758 |
test_string_truncate_lines_one("x\ny\nz", 1, "x", true); |
| 759 |
test_string_truncate_lines_one("x\ny\nz", 2, "x\ny", true); |
| 760 |
test_string_truncate_lines_one("x\ny\nz", 3, "x\ny\nz", false); |
| 761 |
|
| 762 |
test_string_truncate_lines_one("x\ny\nz\n", 0, "", true); |
| 763 |
test_string_truncate_lines_one("x\ny\nz\n", 1, "x", true); |
| 764 |
test_string_truncate_lines_one("x\ny\nz\n", 2, "x\ny", true); |
| 765 |
test_string_truncate_lines_one("x\ny\nz\n", 3, "x\ny\nz", false); |
| 766 |
|
| 767 |
test_string_truncate_lines_one("\n", 0, "", false); |
| 768 |
test_string_truncate_lines_one("\n", 1, "", false); |
| 769 |
test_string_truncate_lines_one("\n", 2, "", false); |
| 770 |
test_string_truncate_lines_one("\n", 3, "", false); |
| 771 |
|
| 772 |
test_string_truncate_lines_one("\n\n", 0, "", false); |
| 773 |
test_string_truncate_lines_one("\n\n", 1, "", false); |
| 774 |
test_string_truncate_lines_one("\n\n", 2, "", false); |
| 775 |
test_string_truncate_lines_one("\n\n", 3, "", false); |
| 776 |
|
| 777 |
test_string_truncate_lines_one("\n\n\n", 0, "", false); |
| 778 |
test_string_truncate_lines_one("\n\n\n", 1, "", false); |
| 779 |
test_string_truncate_lines_one("\n\n\n", 2, "", false); |
| 780 |
test_string_truncate_lines_one("\n\n\n", 3, "", false); |
| 781 |
|
| 782 |
test_string_truncate_lines_one("\nx\n\n", 0, "", true); |
| 783 |
test_string_truncate_lines_one("\nx\n\n", 1, "", true); |
| 784 |
test_string_truncate_lines_one("\nx\n\n", 2, "\nx", false); |
| 785 |
test_string_truncate_lines_one("\nx\n\n", 3, "\nx", false); |
| 786 |
|
| 787 |
test_string_truncate_lines_one("\n\nx\n", 0, "", true); |
| 788 |
test_string_truncate_lines_one("\n\nx\n", 1, "", true); |
| 789 |
test_string_truncate_lines_one("\n\nx\n", 2, "", true); |
| 790 |
test_string_truncate_lines_one("\n\nx\n", 3, "\n\nx", false); |
| 791 |
} |
| 792 |
|
| 793 |
static void test_string_extract_lines_one(const char *input, size_t i, const char *output, bool more) { |
| 794 |
_cleanup_free_ char *b = NULL; |
| 795 |
int k; |
| 796 |
|
| 797 |
assert_se((k = string_extract_line(input, i, &b)) >= 0); |
| 798 |
ASSERT_STREQ(b ?: input, output); |
| 799 |
assert_se(!!k == more); |
| 800 |
} |
| 801 |
|
| 802 |
TEST(string_extract_line) { |
| 803 |
test_string_extract_lines_one("", 0, "", false); |
| 804 |
test_string_extract_lines_one("", 1, "", false); |
| 805 |
test_string_extract_lines_one("", 2, "", false); |
| 806 |
test_string_extract_lines_one("", 3, "", false); |
| 807 |
|
| 808 |
test_string_extract_lines_one("x", 0, "x", false); |
| 809 |
test_string_extract_lines_one("x", 1, "", false); |
| 810 |
test_string_extract_lines_one("x", 2, "", false); |
| 811 |
test_string_extract_lines_one("x", 3, "", false); |
| 812 |
|
| 813 |
test_string_extract_lines_one("x\n", 0, "x", false); |
| 814 |
test_string_extract_lines_one("x\n", 1, "", false); |
| 815 |
test_string_extract_lines_one("x\n", 2, "", false); |
| 816 |
test_string_extract_lines_one("x\n", 3, "", false); |
| 817 |
|
| 818 |
test_string_extract_lines_one("x\ny", 0, "x", true); |
| 819 |
test_string_extract_lines_one("x\ny", 1, "y", false); |
| 820 |
test_string_extract_lines_one("x\ny", 2, "", false); |
| 821 |
test_string_extract_lines_one("x\ny", 3, "", false); |
| 822 |
|
| 823 |
test_string_extract_lines_one("x\ny\n", 0, "x", true); |
| 824 |
test_string_extract_lines_one("x\ny\n", 1, "y", false); |
| 825 |
test_string_extract_lines_one("x\ny\n", 2, "", false); |
| 826 |
test_string_extract_lines_one("x\ny\n", 3, "", false); |
| 827 |
|
| 828 |
test_string_extract_lines_one("x\ny\nz", 0, "x", true); |
| 829 |
test_string_extract_lines_one("x\ny\nz", 1, "y", true); |
| 830 |
test_string_extract_lines_one("x\ny\nz", 2, "z", false); |
| 831 |
test_string_extract_lines_one("x\ny\nz", 3, "", false); |
| 832 |
|
| 833 |
test_string_extract_lines_one("\n", 0, "", false); |
| 834 |
test_string_extract_lines_one("\n", 1, "", false); |
| 835 |
test_string_extract_lines_one("\n", 2, "", false); |
| 836 |
test_string_extract_lines_one("\n", 3, "", false); |
| 837 |
|
| 838 |
test_string_extract_lines_one("\n\n", 0, "", true); |
| 839 |
test_string_extract_lines_one("\n\n", 1, "", false); |
| 840 |
test_string_extract_lines_one("\n\n", 2, "", false); |
| 841 |
test_string_extract_lines_one("\n\n", 3, "", false); |
| 842 |
|
| 843 |
test_string_extract_lines_one("\n\n\n", 0, "", true); |
| 844 |
test_string_extract_lines_one("\n\n\n", 1, "", true); |
| 845 |
test_string_extract_lines_one("\n\n\n", 2, "", false); |
| 846 |
test_string_extract_lines_one("\n\n\n", 3, "", false); |
| 847 |
|
| 848 |
test_string_extract_lines_one("\n\n\n\n", 0, "", true); |
| 849 |
test_string_extract_lines_one("\n\n\n\n", 1, "", true); |
| 850 |
test_string_extract_lines_one("\n\n\n\n", 2, "", true); |
| 851 |
test_string_extract_lines_one("\n\n\n\n", 3, "", false); |
| 852 |
|
| 853 |
test_string_extract_lines_one("\nx\n\n\n", 0, "", true); |
| 854 |
test_string_extract_lines_one("\nx\n\n\n", 1, "x", true); |
| 855 |
test_string_extract_lines_one("\nx\n\n\n", 2, "", true); |
| 856 |
test_string_extract_lines_one("\nx\n\n\n", 3, "", false); |
| 857 |
|
| 858 |
test_string_extract_lines_one("\n\nx\n\n", 0, "", true); |
| 859 |
test_string_extract_lines_one("\n\nx\n\n", 1, "", true); |
| 860 |
test_string_extract_lines_one("\n\nx\n\n", 2, "x", true); |
| 861 |
test_string_extract_lines_one("\n\nx\n\n", 3, "", false); |
| 862 |
|
| 863 |
test_string_extract_lines_one("\n\n\nx\n", 0, "", true); |
| 864 |
test_string_extract_lines_one("\n\n\nx\n", 1, "", true); |
| 865 |
test_string_extract_lines_one("\n\n\nx\n", 2, "", true); |
| 866 |
test_string_extract_lines_one("\n\n\nx\n", 3, "x", false); |
| 867 |
} |
| 868 |
|
| 869 |
TEST(string_contains_word_strv) { |
| 870 |
const char *w; |
| 871 |
|
| 872 |
assert_se(string_contains_word_strv("a b cc", NULL, STRV_MAKE("a", "b"), NULL)); |
| 873 |
|
| 874 |
assert_se(string_contains_word_strv("a b cc", NULL, STRV_MAKE("a", "b"), &w)); |
| 875 |
ASSERT_STREQ(w, "a"); |
| 876 |
|
| 877 |
assert_se(!string_contains_word_strv("a b cc", NULL, STRV_MAKE("d"), &w)); |
| 878 |
ASSERT_NULL(w); |
| 879 |
|
| 880 |
assert_se(string_contains_word_strv("a b cc", NULL, STRV_MAKE("b", "a"), &w)); |
| 881 |
ASSERT_STREQ(w, "a"); |
| 882 |
|
| 883 |
assert_se(string_contains_word_strv("b a b cc", NULL, STRV_MAKE("b", "a", "b"), &w)); |
| 884 |
ASSERT_STREQ(w, "b"); |
| 885 |
|
| 886 |
assert_se(string_contains_word_strv("a b cc", NULL, STRV_MAKE("b", ""), &w)); |
| 887 |
ASSERT_STREQ(w, "b"); |
| 888 |
|
| 889 |
assert_se(!string_contains_word_strv("a b cc", NULL, STRV_MAKE(""), &w)); |
| 890 |
ASSERT_NULL(w); |
| 891 |
|
| 892 |
assert_se(string_contains_word_strv("a b cc", " ", STRV_MAKE(""), &w)); |
| 893 |
ASSERT_STREQ(w, ""); |
| 894 |
} |
| 895 |
|
| 896 |
TEST(string_contains_word) { |
| 897 |
assert_se( string_contains_word("a b cc", NULL, "a")); |
| 898 |
assert_se( string_contains_word("a b cc", NULL, "b")); |
| 899 |
assert_se(!string_contains_word("a b cc", NULL, "c")); |
| 900 |
assert_se( string_contains_word("a b cc", NULL, "cc")); |
| 901 |
assert_se(!string_contains_word("a b cc", NULL, "d")); |
| 902 |
assert_se(!string_contains_word("a b cc", NULL, "a b")); |
| 903 |
assert_se(!string_contains_word("a b cc", NULL, "a b c")); |
| 904 |
assert_se(!string_contains_word("a b cc", NULL, "b c")); |
| 905 |
assert_se(!string_contains_word("a b cc", NULL, "b cc")); |
| 906 |
assert_se(!string_contains_word("a b cc", NULL, "a ")); |
| 907 |
assert_se(!string_contains_word("a b cc", NULL, " b ")); |
| 908 |
assert_se(!string_contains_word("a b cc", NULL, " cc")); |
| 909 |
|
| 910 |
assert_se( string_contains_word(" a b\t\tcc", NULL, "a")); |
| 911 |
assert_se( string_contains_word(" a b\t\tcc", NULL, "b")); |
| 912 |
assert_se(!string_contains_word(" a b\t\tcc", NULL, "c")); |
| 913 |
assert_se( string_contains_word(" a b\t\tcc", NULL, "cc")); |
| 914 |
assert_se(!string_contains_word(" a b\t\tcc", NULL, "d")); |
| 915 |
assert_se(!string_contains_word(" a b\t\tcc", NULL, "a b")); |
| 916 |
assert_se(!string_contains_word(" a b\t\tcc", NULL, "a b\t\tc")); |
| 917 |
assert_se(!string_contains_word(" a b\t\tcc", NULL, "b\t\tc")); |
| 918 |
assert_se(!string_contains_word(" a b\t\tcc", NULL, "b\t\tcc")); |
| 919 |
assert_se(!string_contains_word(" a b\t\tcc", NULL, "a ")); |
| 920 |
assert_se(!string_contains_word(" a b\t\tcc", NULL, " b ")); |
| 921 |
assert_se(!string_contains_word(" a b\t\tcc", NULL, " cc")); |
| 922 |
|
| 923 |
assert_se(!string_contains_word(" a b\t\tcc", NULL, "")); |
| 924 |
assert_se(!string_contains_word(" a b\t\tcc", NULL, " ")); |
| 925 |
assert_se(!string_contains_word(" a b\t\tcc", NULL, " ")); |
| 926 |
assert_se( string_contains_word(" a b\t\tcc", " ", "")); |
| 927 |
assert_se( string_contains_word(" a b\t\tcc", "\t", "")); |
| 928 |
assert_se( string_contains_word(" a b\t\tcc", WHITESPACE, "")); |
| 929 |
|
| 930 |
assert_se( string_contains_word("a:b:cc", ":#", "a")); |
| 931 |
assert_se( string_contains_word("a:b:cc", ":#", "b")); |
| 932 |
assert_se(!string_contains_word("a:b:cc", ":#", "c")); |
| 933 |
assert_se( string_contains_word("a:b:cc", ":#", "cc")); |
| 934 |
assert_se(!string_contains_word("a:b:cc", ":#", "d")); |
| 935 |
assert_se(!string_contains_word("a:b:cc", ":#", "a:b")); |
| 936 |
assert_se(!string_contains_word("a:b:cc", ":#", "a:b:c")); |
| 937 |
assert_se(!string_contains_word("a:b:cc", ":#", "b:c")); |
| 938 |
assert_se(!string_contains_word("a#b#cc", ":#", "b:cc")); |
| 939 |
assert_se( string_contains_word("a#b#cc", ":#", "b")); |
| 940 |
assert_se( string_contains_word("a#b#cc", ":#", "cc")); |
| 941 |
assert_se(!string_contains_word("a:b:cc", ":#", "a:")); |
| 942 |
assert_se(!string_contains_word("a:b cc", ":#", "b")); |
| 943 |
assert_se( string_contains_word("a:b cc", ":#", "b cc")); |
| 944 |
assert_se(!string_contains_word("a:b:cc", ":#", ":cc")); |
| 945 |
} |
| 946 |
|
| 947 |
static void test_strverscmp_improved_one(const char* a, const char *b, int expected) { |
| 948 |
int r = strverscmp_improved(a, b); |
| 949 |
|
| 950 |
log_info("'%s' %s '%s'%s", |
| 951 |
strnull(a), |
| 952 |
comparison_operator(r), |
| 953 |
strnull(b), |
| 954 |
r == expected ? "" : " !!!!!!!!!!!!!"); |
| 955 |
assert_se(r == expected); |
| 956 |
} |
| 957 |
|
| 958 |
static void test_strverscmp_improved_newer(const char *older, const char *newer) { |
| 959 |
test_strverscmp_improved_one(older, newer, -1); |
| 960 |
|
| 961 |
assert_se(strverscmp_improved(older, older) == 0); |
| 962 |
assert_se(strverscmp_improved(older, newer) < 0); |
| 963 |
assert_se(strverscmp_improved(newer, older) > 0); |
| 964 |
assert_se(strverscmp_improved(newer, newer) == 0); |
| 965 |
} |
| 966 |
|
| 967 |
TEST(strverscmp_improved) { |
| 968 |
static const char * const versions[] = { |
| 969 |
"~1", |
| 970 |
"", |
| 971 |
"ab", |
| 972 |
"abb", |
| 973 |
"abc", |
| 974 |
"0001", |
| 975 |
"002", |
| 976 |
"12", |
| 977 |
"122", |
| 978 |
"122.9", |
| 979 |
"123~rc1", |
| 980 |
"123", |
| 981 |
"123-a", |
| 982 |
"123-a.1", |
| 983 |
"123-a1", |
| 984 |
"123-a1.1", |
| 985 |
"123-3", |
| 986 |
"123-3.1", |
| 987 |
"123^patch1", |
| 988 |
"123^1", |
| 989 |
"123.a-1", |
| 990 |
"123.1-1", |
| 991 |
"123a-1", |
| 992 |
"124", |
| 993 |
NULL, |
| 994 |
}; |
| 995 |
|
| 996 |
STRV_FOREACH(p, versions) |
| 997 |
STRV_FOREACH(q, p + 1) |
| 998 |
test_strverscmp_improved_newer(*p, *q); |
| 999 |
|
| 1000 |
test_strverscmp_improved_newer("123.45-67.88", "123.45-67.89"); |
| 1001 |
test_strverscmp_improved_newer("123.45-67.89", "123.45-67.89a"); |
| 1002 |
test_strverscmp_improved_newer("123.45-67.ab", "123.45-67.89"); |
| 1003 |
test_strverscmp_improved_newer("123.45-67.9", "123.45-67.89"); |
| 1004 |
test_strverscmp_improved_newer("123.45-67", "123.45-67.89"); |
| 1005 |
test_strverscmp_improved_newer("123.45-66.89", "123.45-67.89"); |
| 1006 |
test_strverscmp_improved_newer("123.45-9.99", "123.45-67.89"); |
| 1007 |
test_strverscmp_improved_newer("123.42-99.99", "123.45-67.89"); |
| 1008 |
test_strverscmp_improved_newer("123-99.99", "123.45-67.89"); |
| 1009 |
|
| 1010 |
/* '~' : pre-releases */ |
| 1011 |
test_strverscmp_improved_newer("123~rc1-99.99", "123.45-67.89"); |
| 1012 |
test_strverscmp_improved_newer("123~rc1-99.99", "123-45.67.89"); |
| 1013 |
test_strverscmp_improved_newer("123~rc1-99.99", "123~rc2-67.89"); |
| 1014 |
test_strverscmp_improved_newer("123~rc1-99.99", "123^aa2-67.89"); |
| 1015 |
test_strverscmp_improved_newer("123~rc1-99.99", "123aa2-67.89"); |
| 1016 |
|
| 1017 |
/* '-' : separator between version and release. */ |
| 1018 |
test_strverscmp_improved_newer("123-99.99", "123.45-67.89"); |
| 1019 |
test_strverscmp_improved_newer("123-99.99", "123^aa2-67.89"); |
| 1020 |
test_strverscmp_improved_newer("123-99.99", "123aa2-67.89"); |
| 1021 |
|
| 1022 |
/* '^' : patch releases */ |
| 1023 |
test_strverscmp_improved_newer("123^45-67.89", "123.45-67.89"); |
| 1024 |
test_strverscmp_improved_newer("123^aa1-99.99", "123^aa2-67.89"); |
| 1025 |
test_strverscmp_improved_newer("123^aa2-67.89", "123aa2-67.89"); |
| 1026 |
|
| 1027 |
/* '.' : point release */ |
| 1028 |
test_strverscmp_improved_newer("123.aa2-67.89", "123aa2-67.89"); |
| 1029 |
test_strverscmp_improved_newer("123.aa2-67.89", "123.ab2-67.89"); |
| 1030 |
|
| 1031 |
/* invalid characters */ |
| 1032 |
assert_se(strverscmp_improved("123_aa2-67.89", "123aa+2-67.89") == 0); |
| 1033 |
|
| 1034 |
/* some corner cases */ |
| 1035 |
assert_se(strverscmp_improved("123.", "123") > 0); /* One more version segment */ |
| 1036 |
assert_se(strverscmp_improved("12_3", "123") < 0); /* 12 < 123 */ |
| 1037 |
assert_se(strverscmp_improved("12_3", "12") > 0); /* 3 > '' */ |
| 1038 |
assert_se(strverscmp_improved("12_3", "12.3") > 0); /* 3 > '' */ |
| 1039 |
assert_se(strverscmp_improved("123.0", "123") > 0); /* 0 > '' */ |
| 1040 |
assert_se(strverscmp_improved("123_0", "123") > 0); /* 0 > '' */ |
| 1041 |
assert_se(strverscmp_improved("123..0", "123.0") < 0); /* '' < 0 */ |
| 1042 |
|
| 1043 |
/* empty strings or strings with ignored characters only */ |
| 1044 |
assert_se(strverscmp_improved("", NULL) == 0); |
| 1045 |
assert_se(strverscmp_improved(NULL, "") == 0); |
| 1046 |
assert_se(strverscmp_improved("0_", "0") == 0); |
| 1047 |
assert_se(strverscmp_improved("_0_", "0") == 0); |
| 1048 |
assert_se(strverscmp_improved("_0", "0") == 0); |
| 1049 |
assert_se(strverscmp_improved("0", "0___") == 0); |
| 1050 |
assert_se(strverscmp_improved("", "_") == 0); |
| 1051 |
assert_se(strverscmp_improved("_", "") == 0); |
| 1052 |
assert_se(strverscmp_improved("_", "_") == 0); |
| 1053 |
assert_se(strverscmp_improved("", "~") > 0); |
| 1054 |
assert_se(strverscmp_improved("~", "") < 0); |
| 1055 |
assert_se(strverscmp_improved("~", "~") == 0); |
| 1056 |
|
| 1057 |
/* non-ASCII digits */ |
| 1058 |
(void) setlocale(LC_NUMERIC, "ar_YE.utf8"); |
| 1059 |
assert_se(strverscmp_improved("1٠١٢٣٤٥٦٧٨٩", "1") == 0); |
| 1060 |
|
| 1061 |
(void) setlocale(LC_NUMERIC, "th_TH.utf8"); |
| 1062 |
assert_se(strverscmp_improved("1๐๑๒๓๔๕๖๗๘๙", "1") == 0); |
| 1063 |
} |
| 1064 |
|
| 1065 |
#define RPMVERCMP(a, b, c) \ |
| 1066 |
test_strverscmp_improved_one(STRINGIFY(a), STRINGIFY(b), (c)) |
| 1067 |
|
| 1068 |
TEST(strverscmp_improved_rpm) { |
| 1069 |
/* Tests copied from rmp's rpmio test suite, under the LGPL license: |
| 1070 |
* https://github.com/rpm-software-management/rpm/blob/master/tests/rpmvercmp.at. |
| 1071 |
* The original form is retained for easy comparisons and updates. |
| 1072 |
*/ |
| 1073 |
|
| 1074 |
RPMVERCMP(1.0, 1.0, 0); |
| 1075 |
RPMVERCMP(1.0, 2.0, -1); |
| 1076 |
RPMVERCMP(2.0, 1.0, 1); |
| 1077 |
|
| 1078 |
RPMVERCMP(2.0.1, 2.0.1, 0); |
| 1079 |
RPMVERCMP(2.0, 2.0.1, -1); |
| 1080 |
RPMVERCMP(2.0.1, 2.0, 1); |
| 1081 |
|
| 1082 |
RPMVERCMP(2.0.1a, 2.0.1a, 0); |
| 1083 |
RPMVERCMP(2.0.1a, 2.0.1, 1); |
| 1084 |
RPMVERCMP(2.0.1, 2.0.1a, -1); |
| 1085 |
|
| 1086 |
RPMVERCMP(5.5p1, 5.5p1, 0); |
| 1087 |
RPMVERCMP(5.5p1, 5.5p2, -1); |
| 1088 |
RPMVERCMP(5.5p2, 5.5p1, 1); |
| 1089 |
|
| 1090 |
RPMVERCMP(5.5p10, 5.5p10, 0); |
| 1091 |
RPMVERCMP(5.5p1, 5.5p10, -1); |
| 1092 |
RPMVERCMP(5.5p10, 5.5p1, 1); |
| 1093 |
|
| 1094 |
RPMVERCMP(10xyz, 10.1xyz, 1); /* Note: this is reversed from rpm's vercmp */ |
| 1095 |
RPMVERCMP(10.1xyz, 10xyz, -1); /* Note: this is reversed from rpm's vercmp */ |
| 1096 |
|
| 1097 |
RPMVERCMP(xyz10, xyz10, 0); |
| 1098 |
RPMVERCMP(xyz10, xyz10.1, -1); |
| 1099 |
RPMVERCMP(xyz10.1, xyz10, 1); |
| 1100 |
|
| 1101 |
RPMVERCMP(xyz.4, xyz.4, 0); |
| 1102 |
RPMVERCMP(xyz.4, 8, -1); |
| 1103 |
RPMVERCMP(8, xyz.4, 1); |
| 1104 |
RPMVERCMP(xyz.4, 2, -1); |
| 1105 |
RPMVERCMP(2, xyz.4, 1); |
| 1106 |
|
| 1107 |
RPMVERCMP(5.5p2, 5.6p1, -1); |
| 1108 |
RPMVERCMP(5.6p1, 5.5p2, 1); |
| 1109 |
|
| 1110 |
RPMVERCMP(5.6p1, 6.5p1, -1); |
| 1111 |
RPMVERCMP(6.5p1, 5.6p1, 1); |
| 1112 |
|
| 1113 |
RPMVERCMP(6.0.rc1, 6.0, 1); |
| 1114 |
RPMVERCMP(6.0, 6.0.rc1, -1); |
| 1115 |
|
| 1116 |
RPMVERCMP(10b2, 10a1, 1); |
| 1117 |
RPMVERCMP(10a2, 10b2, -1); |
| 1118 |
|
| 1119 |
RPMVERCMP(1.0aa, 1.0aa, 0); |
| 1120 |
RPMVERCMP(1.0a, 1.0aa, -1); |
| 1121 |
RPMVERCMP(1.0aa, 1.0a, 1); |
| 1122 |
|
| 1123 |
RPMVERCMP(10.0001, 10.0001, 0); |
| 1124 |
RPMVERCMP(10.0001, 10.1, 0); |
| 1125 |
RPMVERCMP(10.1, 10.0001, 0); |
| 1126 |
RPMVERCMP(10.0001, 10.0039, -1); |
| 1127 |
RPMVERCMP(10.0039, 10.0001, 1); |
| 1128 |
|
| 1129 |
RPMVERCMP(4.999.9, 5.0, -1); |
| 1130 |
RPMVERCMP(5.0, 4.999.9, 1); |
| 1131 |
|
| 1132 |
RPMVERCMP(20101121, 20101121, 0); |
| 1133 |
RPMVERCMP(20101121, 20101122, -1); |
| 1134 |
RPMVERCMP(20101122, 20101121, 1); |
| 1135 |
|
| 1136 |
RPMVERCMP(2_0, 2_0, 0); |
| 1137 |
RPMVERCMP(2.0, 2_0, -1); /* Note: in rpm those compare equal */ |
| 1138 |
RPMVERCMP(2_0, 2.0, 1); /* Note: in rpm those compare equal */ |
| 1139 |
|
| 1140 |
/* RhBug:178798 case */ |
| 1141 |
RPMVERCMP(a, a, 0); |
| 1142 |
RPMVERCMP(a+, a+, 0); |
| 1143 |
RPMVERCMP(a+, a_, 0); |
| 1144 |
RPMVERCMP(a_, a+, 0); |
| 1145 |
RPMVERCMP(+a, +a, 0); |
| 1146 |
RPMVERCMP(+a, _a, 0); |
| 1147 |
RPMVERCMP(_a, +a, 0); |
| 1148 |
RPMVERCMP(+_, +_, 0); |
| 1149 |
RPMVERCMP(_+, +_, 0); |
| 1150 |
RPMVERCMP(_+, _+, 0); |
| 1151 |
RPMVERCMP(+, _, 0); |
| 1152 |
RPMVERCMP(_, +, 0); |
| 1153 |
|
| 1154 |
/* Basic testcases for tilde sorting */ |
| 1155 |
RPMVERCMP(1.0~rc1, 1.0~rc1, 0); |
| 1156 |
RPMVERCMP(1.0~rc1, 1.0, -1); |
| 1157 |
RPMVERCMP(1.0, 1.0~rc1, 1); |
| 1158 |
RPMVERCMP(1.0~rc1, 1.0~rc2, -1); |
| 1159 |
RPMVERCMP(1.0~rc2, 1.0~rc1, 1); |
| 1160 |
RPMVERCMP(1.0~rc1~git123, 1.0~rc1~git123, 0); |
| 1161 |
RPMVERCMP(1.0~rc1~git123, 1.0~rc1, -1); |
| 1162 |
RPMVERCMP(1.0~rc1, 1.0~rc1~git123, 1); |
| 1163 |
|
| 1164 |
/* Basic testcases for caret sorting */ |
| 1165 |
RPMVERCMP(1.0^, 1.0^, 0); |
| 1166 |
RPMVERCMP(1.0^, 1.0, 1); |
| 1167 |
RPMVERCMP(1.0, 1.0^, -1); |
| 1168 |
RPMVERCMP(1.0^git1, 1.0^git1, 0); |
| 1169 |
RPMVERCMP(1.0^git1, 1.0, 1); |
| 1170 |
RPMVERCMP(1.0, 1.0^git1, -1); |
| 1171 |
RPMVERCMP(1.0^git1, 1.0^git2, -1); |
| 1172 |
RPMVERCMP(1.0^git2, 1.0^git1, 1); |
| 1173 |
RPMVERCMP(1.0^git1, 1.01, -1); |
| 1174 |
RPMVERCMP(1.01, 1.0^git1, 1); |
| 1175 |
RPMVERCMP(1.0^20160101, 1.0^20160101, 0); |
| 1176 |
RPMVERCMP(1.0^20160101, 1.0.1, -1); |
| 1177 |
RPMVERCMP(1.0.1, 1.0^20160101, 1); |
| 1178 |
RPMVERCMP(1.0^20160101^git1, 1.0^20160101^git1, 0); |
| 1179 |
RPMVERCMP(1.0^20160102, 1.0^20160101^git1, 1); |
| 1180 |
RPMVERCMP(1.0^20160101^git1, 1.0^20160102, -1); |
| 1181 |
|
| 1182 |
/* Basic testcases for tilde and caret sorting */ |
| 1183 |
RPMVERCMP(1.0~rc1^git1, 1.0~rc1^git1, 0); |
| 1184 |
RPMVERCMP(1.0~rc1^git1, 1.0~rc1, 1); |
| 1185 |
RPMVERCMP(1.0~rc1, 1.0~rc1^git1, -1); |
| 1186 |
RPMVERCMP(1.0^git1~pre, 1.0^git1~pre, 0); |
| 1187 |
RPMVERCMP(1.0^git1, 1.0^git1~pre, 1); |
| 1188 |
RPMVERCMP(1.0^git1~pre, 1.0^git1, -1); |
| 1189 |
|
| 1190 |
/* These are included here to document current, arguably buggy behaviors |
| 1191 |
* for reference purposes and for easy checking against unintended |
| 1192 |
* behavior changes. */ |
| 1193 |
log_info("/* RPM version comparison oddities */"); |
| 1194 |
/* RhBug:811992 case */ |
| 1195 |
RPMVERCMP(1b.fc17, 1b.fc17, 0); |
| 1196 |
RPMVERCMP(1b.fc17, 1.fc17, 1); /* Note: this is reversed from rpm's vercmp, WAT! */ |
| 1197 |
RPMVERCMP(1.fc17, 1b.fc17, -1); |
| 1198 |
RPMVERCMP(1g.fc17, 1g.fc17, 0); |
| 1199 |
RPMVERCMP(1g.fc17, 1.fc17, 1); |
| 1200 |
RPMVERCMP(1.fc17, 1g.fc17, -1); |
| 1201 |
|
| 1202 |
/* Non-ascii characters are considered equal so these are all the same, eh… */ |
| 1203 |
RPMVERCMP(1.1.α, 1.1.α, 0); |
| 1204 |
RPMVERCMP(1.1.α, 1.1.β, 0); |
| 1205 |
RPMVERCMP(1.1.β, 1.1.α, 0); |
| 1206 |
RPMVERCMP(1.1.αα, 1.1.α, 0); |
| 1207 |
RPMVERCMP(1.1.α, 1.1.ββ, 0); |
| 1208 |
RPMVERCMP(1.1.ββ, 1.1.αα, 0); |
| 1209 |
} |
| 1210 |
|
| 1211 |
TEST(strextendf) { |
| 1212 |
_cleanup_free_ char *p = NULL; |
| 1213 |
|
| 1214 |
assert_se(strextendf(&p, "<%i>", 77) >= 0); |
| 1215 |
ASSERT_STREQ(p, "<77>"); |
| 1216 |
|
| 1217 |
assert_se(strextendf(&p, "<%i>", 99) >= 0); |
| 1218 |
ASSERT_STREQ(p, "<77><99>"); |
| 1219 |
|
| 1220 |
assert_se(strextendf(&p, "<%80i>", 88) >= 0); |
| 1221 |
ASSERT_STREQ(p, "<77><99>< 88>"); |
| 1222 |
|
| 1223 |
assert_se(strextendf(&p, "<%08x>", 0x1234u) >= 0); |
| 1224 |
ASSERT_STREQ(p, "<77><99>< 88><00001234>"); |
| 1225 |
|
| 1226 |
p = mfree(p); |
| 1227 |
|
| 1228 |
assert_se(strextendf_with_separator(&p, ",", "<%i>", 77) >= 0); |
| 1229 |
ASSERT_STREQ(p, "<77>"); |
| 1230 |
|
| 1231 |
assert_se(strextendf_with_separator(&p, ",", "<%i>", 99) >= 0); |
| 1232 |
ASSERT_STREQ(p, "<77>,<99>"); |
| 1233 |
|
| 1234 |
assert_se(strextendf_with_separator(&p, ",", "<%80i>", 88) >= 0); |
| 1235 |
ASSERT_STREQ(p, "<77>,<99>,< 88>"); |
| 1236 |
|
| 1237 |
assert_se(strextendf_with_separator(&p, ",", "<%08x>", 0x1234u) >= 0); |
| 1238 |
ASSERT_STREQ(p, "<77>,<99>,< 88>,<00001234>"); |
| 1239 |
} |
| 1240 |
|
| 1241 |
TEST(string_replace_char) { |
| 1242 |
ASSERT_STREQ(string_replace_char(strdupa_safe(""), 'a', 'b'), ""); |
| 1243 |
ASSERT_STREQ(string_replace_char(strdupa_safe("abc"), 'a', 'b'), "bbc"); |
| 1244 |
ASSERT_STREQ(string_replace_char(strdupa_safe("hoge"), 'a', 'b'), "hoge"); |
| 1245 |
ASSERT_STREQ(string_replace_char(strdupa_safe("aaaa"), 'a', 'b'), "bbbb"); |
| 1246 |
ASSERT_STREQ(string_replace_char(strdupa_safe("aaaa"), 'a', '\t'), "\t\t\t\t"); |
| 1247 |
} |
| 1248 |
|
| 1249 |
TEST(strspn_from_end) { |
| 1250 |
assert_se(strspn_from_end(NULL, NULL) == 0); |
| 1251 |
assert_se(strspn_from_end("hoge", NULL) == 0); |
| 1252 |
assert_se(strspn_from_end(NULL, DIGITS) == 0); |
| 1253 |
assert_se(strspn_from_end("", DIGITS) == 0); |
| 1254 |
assert_se(strspn_from_end("hoge", DIGITS) == 0); |
| 1255 |
assert_se(strspn_from_end("1234", DIGITS) == 4); |
| 1256 |
assert_se(strspn_from_end("aaa1234", DIGITS) == 4); |
| 1257 |
assert_se(strspn_from_end("aaa1234aaa", DIGITS) == 0); |
| 1258 |
assert_se(strspn_from_end("aaa12aa34", DIGITS) == 2); |
| 1259 |
} |
| 1260 |
|
| 1261 |
TEST(streq_skip_trailing_chars) { |
| 1262 |
/* NULL is WHITESPACE by default */ |
| 1263 |
assert_se(streq_skip_trailing_chars("foo bar", "foo bar", NULL)); |
| 1264 |
assert_se(streq_skip_trailing_chars("foo", "foo", NULL)); |
| 1265 |
assert_se(streq_skip_trailing_chars("foo bar ", "foo bar", NULL)); |
| 1266 |
assert_se(streq_skip_trailing_chars("foo bar", "foo bar\t\t", NULL)); |
| 1267 |
assert_se(streq_skip_trailing_chars("foo bar ", "foo bar\t\t", NULL)); |
| 1268 |
assert_se(streq_skip_trailing_chars("foo\nbar", "foo\nbar", NULL)); |
| 1269 |
assert_se(streq_skip_trailing_chars("\t\tfoo bar", "\t\tfoo bar", NULL)); |
| 1270 |
assert_se(streq_skip_trailing_chars(" foo bar\t", " foo bar\n", NULL)); |
| 1271 |
|
| 1272 |
assert_se(!streq_skip_trailing_chars("foobar", "foo bar", NULL)); |
| 1273 |
assert_se(!streq_skip_trailing_chars("foo\nbar", "foo\tbar", NULL)); |
| 1274 |
assert_se(!streq_skip_trailing_chars("\t\nfoo bar", "\t foo bar", NULL)); |
| 1275 |
|
| 1276 |
assert_se(streq_skip_trailing_chars("foo bar ", "foo bar", WHITESPACE)); |
| 1277 |
assert_se(!streq_skip_trailing_chars("foo bar ", "foo bar", NEWLINE)); |
| 1278 |
|
| 1279 |
assert_se(streq_skip_trailing_chars(NULL, NULL, NULL)); |
| 1280 |
assert_se(streq_skip_trailing_chars("", "", NULL)); |
| 1281 |
assert_se(!streq_skip_trailing_chars(NULL, "foo bar", NULL)); |
| 1282 |
assert_se(!streq_skip_trailing_chars("foo", NULL, NULL)); |
| 1283 |
assert_se(!streq_skip_trailing_chars("", "f", NULL)); |
| 1284 |
} |
| 1285 |
|
| 1286 |
#define TEST_MAKE_CSTRING_ONE(x, ret, mode, expect) \ |
| 1287 |
do { \ |
| 1288 |
_cleanup_free_ char *b = NULL; \ |
| 1289 |
assert_se(make_cstring((x), ELEMENTSOF(x), (mode), &b) == (ret)); \ |
| 1290 |
ASSERT_STREQ(b, (expect)); \ |
| 1291 |
} while(false) |
| 1292 |
|
| 1293 |
TEST(make_cstring) { |
| 1294 |
static const char test1[] = "this is a test", |
| 1295 |
test2[] = "", |
| 1296 |
test3[] = "a", |
| 1297 |
test4[] = "aa\0aa", |
| 1298 |
test5[] = { 'b', 'b', 0, 'b' , 'b' }, |
| 1299 |
test6[] = {}, |
| 1300 |
test7[] = { 'x' }, |
| 1301 |
test8[] = { 'x', 'y', 'z' }; |
| 1302 |
|
| 1303 |
TEST_MAKE_CSTRING_ONE(test1, -EINVAL, MAKE_CSTRING_REFUSE_TRAILING_NUL, NULL); |
| 1304 |
TEST_MAKE_CSTRING_ONE(test1, 0, MAKE_CSTRING_ALLOW_TRAILING_NUL, "this is a test"); |
| 1305 |
TEST_MAKE_CSTRING_ONE(test1, 0, MAKE_CSTRING_REQUIRE_TRAILING_NUL, "this is a test"); |
| 1306 |
|
| 1307 |
TEST_MAKE_CSTRING_ONE(test2, -EINVAL, MAKE_CSTRING_REFUSE_TRAILING_NUL, NULL); |
| 1308 |
TEST_MAKE_CSTRING_ONE(test2, 0, MAKE_CSTRING_ALLOW_TRAILING_NUL, ""); |
| 1309 |
TEST_MAKE_CSTRING_ONE(test2, 0, MAKE_CSTRING_REQUIRE_TRAILING_NUL, ""); |
| 1310 |
|
| 1311 |
TEST_MAKE_CSTRING_ONE(test3, -EINVAL, MAKE_CSTRING_REFUSE_TRAILING_NUL, NULL); |
| 1312 |
TEST_MAKE_CSTRING_ONE(test3, 0, MAKE_CSTRING_ALLOW_TRAILING_NUL, "a"); |
| 1313 |
TEST_MAKE_CSTRING_ONE(test3, 0, MAKE_CSTRING_REQUIRE_TRAILING_NUL, "a"); |
| 1314 |
|
| 1315 |
TEST_MAKE_CSTRING_ONE(test4, -EINVAL, MAKE_CSTRING_REFUSE_TRAILING_NUL, NULL); |
| 1316 |
TEST_MAKE_CSTRING_ONE(test4, -EINVAL, MAKE_CSTRING_ALLOW_TRAILING_NUL, NULL); |
| 1317 |
TEST_MAKE_CSTRING_ONE(test4, -EINVAL, MAKE_CSTRING_REQUIRE_TRAILING_NUL, NULL); |
| 1318 |
|
| 1319 |
TEST_MAKE_CSTRING_ONE(test5, -EINVAL, MAKE_CSTRING_REFUSE_TRAILING_NUL, NULL); |
| 1320 |
TEST_MAKE_CSTRING_ONE(test5, -EINVAL, MAKE_CSTRING_ALLOW_TRAILING_NUL, NULL); |
| 1321 |
TEST_MAKE_CSTRING_ONE(test5, -EINVAL, MAKE_CSTRING_REQUIRE_TRAILING_NUL, NULL); |
| 1322 |
|
| 1323 |
TEST_MAKE_CSTRING_ONE(test6, 0, MAKE_CSTRING_REFUSE_TRAILING_NUL, ""); |
| 1324 |
TEST_MAKE_CSTRING_ONE(test6, 0, MAKE_CSTRING_ALLOW_TRAILING_NUL, ""); |
| 1325 |
TEST_MAKE_CSTRING_ONE(test6, -EINVAL, MAKE_CSTRING_REQUIRE_TRAILING_NUL, NULL); |
| 1326 |
|
| 1327 |
TEST_MAKE_CSTRING_ONE(test7, 0, MAKE_CSTRING_REFUSE_TRAILING_NUL, "x"); |
| 1328 |
TEST_MAKE_CSTRING_ONE(test7, 0, MAKE_CSTRING_ALLOW_TRAILING_NUL, "x"); |
| 1329 |
TEST_MAKE_CSTRING_ONE(test7, -EINVAL, MAKE_CSTRING_REQUIRE_TRAILING_NUL, NULL); |
| 1330 |
|
| 1331 |
TEST_MAKE_CSTRING_ONE(test8, 0, MAKE_CSTRING_REFUSE_TRAILING_NUL, "xyz"); |
| 1332 |
TEST_MAKE_CSTRING_ONE(test8, 0, MAKE_CSTRING_ALLOW_TRAILING_NUL, "xyz"); |
| 1333 |
TEST_MAKE_CSTRING_ONE(test8, -EINVAL, MAKE_CSTRING_REQUIRE_TRAILING_NUL, NULL); |
| 1334 |
} |
| 1335 |
|
| 1336 |
TEST(find_line_startswith) { |
| 1337 |
static const char text[] = |
| 1338 |
"foobar\n" |
| 1339 |
"this is a test\n" |
| 1340 |
"foobar: waldo\n" |
| 1341 |
"more\n" |
| 1342 |
"\n" |
| 1343 |
"piff\n" |
| 1344 |
"foobarfoobar\n" |
| 1345 |
"iff\n"; |
| 1346 |
static const char emptystring[] = ""; |
| 1347 |
|
| 1348 |
assert_se(find_line_startswith(text, "") == text); |
| 1349 |
assert_se(find_line_startswith(text, "f") == text+1); |
| 1350 |
assert_se(find_line_startswith(text, "foobar") == text+6); |
| 1351 |
assert_se(!find_line_startswith(text, "foobarx")); |
| 1352 |
assert_se(!find_line_startswith(text, "oobar")); |
| 1353 |
assert_se(find_line_startswith(text, "t") == text + 8); |
| 1354 |
assert_se(find_line_startswith(text, "th") == text + 9); |
| 1355 |
assert_se(find_line_startswith(text, "this") == text + 11); |
| 1356 |
assert_se(find_line_startswith(text, "foobarf") == text + 54); |
| 1357 |
assert_se(find_line_startswith(text, "more\n") == text + 41); |
| 1358 |
assert_se(find_line_startswith(text, "\n") == text + 42); |
| 1359 |
assert_se(find_line_startswith(text, "iff") == text + 63); |
| 1360 |
|
| 1361 |
assert_se(find_line_startswith(emptystring, "") == emptystring); |
| 1362 |
assert_se(!find_line_startswith(emptystring, "x")); |
| 1363 |
} |
| 1364 |
|
| 1365 |
TEST(strstrafter) { |
| 1366 |
static const char buffer[] = "abcdefghijklmnopqrstuvwxyz"; |
| 1367 |
|
| 1368 |
assert_se(!strstrafter(NULL, NULL)); |
| 1369 |
assert_se(!strstrafter("", NULL)); |
| 1370 |
assert_se(!strstrafter(NULL, "")); |
| 1371 |
ASSERT_STREQ(strstrafter("", ""), ""); |
| 1372 |
|
| 1373 |
assert_se(strstrafter(buffer, "a") == buffer + 1); |
| 1374 |
assert_se(strstrafter(buffer, "") == buffer); |
| 1375 |
assert_se(strstrafter(buffer, "ab") == buffer + 2); |
| 1376 |
assert_se(strstrafter(buffer, "cde") == buffer + 5); |
| 1377 |
assert_se(strstrafter(buffer, "xyz") == strchr(buffer, 0)); |
| 1378 |
assert_se(strstrafter(buffer, buffer) == strchr(buffer, 0)); |
| 1379 |
assert_se(!strstrafter(buffer, "-")); |
| 1380 |
} |
| 1381 |
|
| 1382 |
TEST(version_is_valid) { |
| 1383 |
assert_se(!version_is_valid(NULL)); |
| 1384 |
assert_se(!version_is_valid("")); |
| 1385 |
assert_se(version_is_valid("0")); |
| 1386 |
assert_se(version_is_valid("5")); |
| 1387 |
assert_se(version_is_valid("999999")); |
| 1388 |
assert_se(version_is_valid("999999.5")); |
| 1389 |
assert_se(version_is_valid("6.2.12-300.fc38.x86_64")); |
| 1390 |
} |
| 1391 |
|
| 1392 |
TEST(strextendn) { |
| 1393 |
_cleanup_free_ char *x = NULL; |
| 1394 |
|
| 1395 |
ASSERT_STREQ(strextendn(&x, NULL, 0), ""); |
| 1396 |
x = mfree(x); |
| 1397 |
|
| 1398 |
ASSERT_STREQ(strextendn(&x, "", 0), ""); |
| 1399 |
x = mfree(x); |
| 1400 |
|
| 1401 |
ASSERT_STREQ(strextendn(&x, "xxx", 3), "xxx"); |
| 1402 |
ASSERT_STREQ(strextendn(&x, "xxx", 3), "xxxxxx"); |
| 1403 |
ASSERT_STREQ(strextendn(&x, "...", 1), "xxxxxx."); |
| 1404 |
ASSERT_STREQ(strextendn(&x, "...", 2), "xxxxxx..."); |
| 1405 |
ASSERT_STREQ(strextendn(&x, "...", 3), "xxxxxx......"); |
| 1406 |
ASSERT_STREQ(strextendn(&x, "...", 4), "xxxxxx........."); |
| 1407 |
x = mfree(x); |
| 1408 |
} |
| 1409 |
|
| 1410 |
TEST(strprepend) { |
| 1411 |
_cleanup_free_ char *x = NULL; |
| 1412 |
|
| 1413 |
ASSERT_STREQ(strprepend(&x, NULL), ""); |
| 1414 |
x = mfree(x); |
| 1415 |
|
| 1416 |
ASSERT_STREQ(strprepend(&x, ""), ""); |
| 1417 |
|
| 1418 |
ASSERT_STREQ(strprepend(&x, "xxx"), "xxx"); |
| 1419 |
ASSERT_STREQ(strprepend(&x, "bar"), "barxxx"); |
| 1420 |
ASSERT_STREQ(strprepend(&x, "foo", "4711"), "foo4711barxxx"); |
| 1421 |
x = mfree(x); |
| 1422 |
|
| 1423 |
ASSERT_STREQ(strprepend_with_separator(&x, "...", NULL), ""); |
| 1424 |
|
| 1425 |
ASSERT_STREQ(strprepend_with_separator(&x, "xyz", "a", "bb", "ccc"), "axyzbbxyzccc"); |
| 1426 |
x = mfree(x); |
| 1427 |
|
| 1428 |
ASSERT_STREQ(strprepend_with_separator(&x, ",", "start", "", "1", "234"), "start,,1,234"); |
| 1429 |
ASSERT_STREQ(strprepend_with_separator(&x, ";", "more", "5", "678"), "more;5;678;start,,1,234"); |
| 1430 |
} |
| 1431 |
|
| 1432 |
TEST(strlevenshtein) { |
| 1433 |
assert_se(strlevenshtein(NULL, NULL) == 0); |
| 1434 |
assert_se(strlevenshtein("", "") == 0); |
| 1435 |
assert_se(strlevenshtein("", NULL) == 0); |
| 1436 |
assert_se(strlevenshtein(NULL, "") == 0); |
| 1437 |
|
| 1438 |
assert_se(strlevenshtein("a", "a") == 0); |
| 1439 |
assert_se(strlevenshtein("a", "b") == 1); |
| 1440 |
assert_se(strlevenshtein("b", "a") == 1); |
| 1441 |
assert_se(strlevenshtein("a", "") == 1); |
| 1442 |
assert_se(strlevenshtein("", "a") == 1); |
| 1443 |
|
| 1444 |
assert_se(strlevenshtein("xxx", "xxx") == 0); |
| 1445 |
assert_se(strlevenshtein("xxx", "yyy") == 3); |
| 1446 |
assert_se(strlevenshtein("yyy", "xxx") == 3); |
| 1447 |
assert_se(strlevenshtein("xx", "xxx") == 1); |
| 1448 |
assert_se(strlevenshtein("xxx", "xx") == 1); |
| 1449 |
assert_se(strlevenshtein("x", "xxx") == 2); |
| 1450 |
assert_se(strlevenshtein("xxx", "x") == 2); |
| 1451 |
|
| 1452 |
assert_se(strlevenshtein("sitting", "kitten") == 3); |
| 1453 |
assert_se(strlevenshtein("sunday", "saturday") == 3); |
| 1454 |
} |
| 1455 |
|
| 1456 |
TEST(strrstr) { |
| 1457 |
assert_se(!strrstr(NULL, NULL)); |
| 1458 |
assert_se(!strrstr("foo", NULL)); |
| 1459 |
assert_se(!strrstr(NULL, "foo")); |
| 1460 |
|
| 1461 |
const char *p = "foo"; |
| 1462 |
assert_se(strrstr(p, "foo") == p); |
| 1463 |
assert_se(strrstr(p, "fo") == p); |
| 1464 |
assert_se(strrstr(p, "f") == p); |
| 1465 |
assert_se(strrstr(p, "oo") == p + 1); |
| 1466 |
assert_se(strrstr(p, "o") == p + 2); |
| 1467 |
assert_se(strrstr(p, "") == p + strlen(p)); |
| 1468 |
assert_se(!strrstr(p, "bar")); |
| 1469 |
|
| 1470 |
p = "xoxoxox"; |
| 1471 |
assert_se(strrstr(p, "") == p + strlen(p)); |
| 1472 |
assert_se(strrstr(p, "x") == p + 6); |
| 1473 |
assert_se(strrstr(p, "ox") == p + 5); |
| 1474 |
assert_se(strrstr(p, "xo") == p + 4); |
| 1475 |
assert_se(strrstr(p, "xox") == p + 4); |
| 1476 |
assert_se(!strrstr(p, "xx")); |
| 1477 |
} |
| 1478 |
|
| 1479 |
TEST(str_common_prefix) { |
| 1480 |
ASSERT_EQ(str_common_prefix("", ""), SIZE_MAX); |
| 1481 |
ASSERT_EQ(str_common_prefix("a", "a"), SIZE_MAX); |
| 1482 |
ASSERT_EQ(str_common_prefix("aa", "aa"), SIZE_MAX); |
| 1483 |
ASSERT_EQ(str_common_prefix("aa", "bb"), 0U); |
| 1484 |
ASSERT_EQ(str_common_prefix("bb", "aa"), 0U); |
| 1485 |
ASSERT_EQ(str_common_prefix("aa", "ab"), 1U); |
| 1486 |
ASSERT_EQ(str_common_prefix("ab", "aa"), 1U); |
| 1487 |
ASSERT_EQ(str_common_prefix("systemd-resolved", "systemd-networkd"), 8U); |
| 1488 |
ASSERT_EQ(str_common_prefix("systemd-", "systemd-networkd"), 8U); |
| 1489 |
ASSERT_EQ(str_common_prefix("systemd-networkd", "systemd-"), 8U); |
| 1490 |
ASSERT_EQ(str_common_prefix("syst", "systemd-networkd"), 4U); |
| 1491 |
ASSERT_EQ(str_common_prefix("systemd-networkd", "syst"), 4U); |
| 1492 |
ASSERT_EQ(str_common_prefix("s", "systemd-networkd"), 1U); |
| 1493 |
ASSERT_EQ(str_common_prefix("systemd-networkd", "s"), 1U); |
| 1494 |
ASSERT_EQ(str_common_prefix("", "systemd-networkd"), 0U); |
| 1495 |
ASSERT_EQ(str_common_prefix("systemd-networkd", ""), 0U); |
| 1496 |
} |
| 1497 |
|
| 1498 |
TEST(string_is_safe) { |
| 1499 |
/* NULL is always rejected, regardless of flags. */ |
| 1500 |
ASSERT_FALSE(string_is_safe(NULL, 0)); |
| 1501 |
ASSERT_FALSE(string_is_safe(NULL, STRING_ALLOW_EMPTY)); |
| 1502 |
ASSERT_FALSE(string_is_safe(NULL, STRING_ASCII)); |
| 1503 |
ASSERT_FALSE(string_is_safe(NULL, STRING_ALLOW_BACKSLASHES)); |
| 1504 |
ASSERT_FALSE(string_is_safe(NULL, STRING_ALLOW_QUOTES)); |
| 1505 |
ASSERT_FALSE(string_is_safe(NULL, STRING_ALLOW_GLOBS)); |
| 1506 |
ASSERT_FALSE(string_is_safe(NULL, STRING_FILENAME)); |
| 1507 |
|
| 1508 |
/* Baseline (flags=0): rejects empty, backslashes, quotes, globs, control chars and invalid UTF-8. |
| 1509 |
* Plain alphanumerics/whitespace and valid UTF-8 accepted. */ |
| 1510 |
ASSERT_TRUE(string_is_safe("hello", 0)); |
| 1511 |
ASSERT_TRUE(string_is_safe("hello world", 0)); |
| 1512 |
ASSERT_TRUE(string_is_safe("über", 0)); /* valid UTF-8 allowed */ |
| 1513 |
ASSERT_TRUE(string_is_safe("ünïcödé", 0)); |
| 1514 |
|
| 1515 |
ASSERT_FALSE(string_is_safe("", 0)); /* empty rejected by default */ |
| 1516 |
ASSERT_FALSE(string_is_safe("a\\b", 0)); /* backslash rejected by default */ |
| 1517 |
ASSERT_FALSE(string_is_safe("\"", 0)); /* double quote rejected by default */ |
| 1518 |
ASSERT_FALSE(string_is_safe("'", 0)); /* single quote rejected by default */ |
| 1519 |
ASSERT_FALSE(string_is_safe("*", 0)); /* glob rejected by default */ |
| 1520 |
ASSERT_FALSE(string_is_safe("?", 0)); /* glob rejected by default */ |
| 1521 |
ASSERT_FALSE(string_is_safe("[", 0)); /* glob rejected by default */ |
| 1522 |
ASSERT_FALSE(string_is_safe("abc\x01", 0)); /* control char */ |
| 1523 |
ASSERT_FALSE(string_is_safe("\t", 0)); |
| 1524 |
ASSERT_FALSE(string_is_safe("\n", 0)); |
| 1525 |
ASSERT_FALSE(string_is_safe("abc\x1f", 0)); |
| 1526 |
ASSERT_FALSE(string_is_safe("abc\x7f", 0)); /* DEL */ |
| 1527 |
ASSERT_FALSE(string_is_safe("ab\xc3\x28", 0)); /* invalid UTF-8 continuation */ |
| 1528 |
ASSERT_FALSE(string_is_safe("\xff", 0)); /* not valid UTF-8 */ |
| 1529 |
|
| 1530 |
/* STRING_ALLOW_EMPTY. */ |
| 1531 |
ASSERT_TRUE(string_is_safe("", STRING_ALLOW_EMPTY)); |
| 1532 |
ASSERT_TRUE(string_is_safe("x", STRING_ALLOW_EMPTY)); |
| 1533 |
ASSERT_TRUE(string_is_safe("hello", STRING_ALLOW_EMPTY)); |
| 1534 |
ASSERT_FALSE(string_is_safe(NULL, STRING_ALLOW_EMPTY)); |
| 1535 |
|
| 1536 |
/* STRING_ASCII: high bytes rejected, low ASCII accepted, control chars still rejected. |
| 1537 |
* Empty is still rejected by default; backslashes/quotes/globs still rejected by default. */ |
| 1538 |
ASSERT_TRUE(string_is_safe("hello", STRING_ASCII)); |
| 1539 |
ASSERT_TRUE(string_is_safe("hello world 123!@#$%^&()", STRING_ASCII)); |
| 1540 |
ASSERT_FALSE(string_is_safe("", STRING_ASCII)); |
| 1541 |
ASSERT_FALSE(string_is_safe("über", STRING_ASCII)); |
| 1542 |
ASSERT_FALSE(string_is_safe("\x80", STRING_ASCII)); |
| 1543 |
ASSERT_FALSE(string_is_safe("\xff", STRING_ASCII)); |
| 1544 |
ASSERT_FALSE(string_is_safe("abc\x01", STRING_ASCII)); |
| 1545 |
ASSERT_FALSE(string_is_safe("abc\x7f", STRING_ASCII)); |
| 1546 |
ASSERT_FALSE(string_is_safe("a\\b", STRING_ASCII)); |
| 1547 |
ASSERT_FALSE(string_is_safe("a\"b", STRING_ASCII)); |
| 1548 |
ASSERT_FALSE(string_is_safe("a*b", STRING_ASCII)); |
| 1549 |
|
| 1550 |
/* STRING_ALLOW_NEWLINES: newlines allowed, quotes/globs still rejected. */ |
| 1551 |
ASSERT_TRUE(string_is_safe("hello", STRING_ALLOW_NEWLINES)); |
| 1552 |
ASSERT_TRUE(string_is_safe("hello world", STRING_ALLOW_NEWLINES)); |
| 1553 |
ASSERT_TRUE(string_is_safe("\n", STRING_ALLOW_NEWLINES)); |
| 1554 |
ASSERT_TRUE(string_is_safe("a\nb", STRING_ALLOW_NEWLINES)); |
| 1555 |
ASSERT_TRUE(string_is_safe("foo\n", STRING_ALLOW_NEWLINES)); |
| 1556 |
ASSERT_TRUE(string_is_safe("\nfoo", STRING_ALLOW_NEWLINES)); |
| 1557 |
ASSERT_TRUE(string_is_safe("foo\nbar", STRING_ALLOW_NEWLINES)); |
| 1558 |
ASSERT_FALSE(string_is_safe("foo\\nbar", STRING_ALLOW_NEWLINES)); /* literal backslash, not newline, rejected */ |
| 1559 |
ASSERT_FALSE(string_is_safe("\"", STRING_ALLOW_NEWLINES)); /* quotes still rejected */ |
| 1560 |
ASSERT_FALSE(string_is_safe("*", STRING_ALLOW_NEWLINES)); /* globs still rejected */ |
| 1561 |
|
| 1562 |
/* STRING_ALLOW_BACKSLASHES: backslashes allowed, quotes/globs still rejected. */ |
| 1563 |
ASSERT_TRUE(string_is_safe("hello", STRING_ALLOW_BACKSLASHES)); |
| 1564 |
ASSERT_TRUE(string_is_safe("hello world", STRING_ALLOW_BACKSLASHES)); |
| 1565 |
ASSERT_TRUE(string_is_safe("\\", STRING_ALLOW_BACKSLASHES)); |
| 1566 |
ASSERT_TRUE(string_is_safe("a\\b", STRING_ALLOW_BACKSLASHES)); |
| 1567 |
ASSERT_TRUE(string_is_safe("foo\\", STRING_ALLOW_BACKSLASHES)); |
| 1568 |
ASSERT_TRUE(string_is_safe("\\foo", STRING_ALLOW_BACKSLASHES)); |
| 1569 |
ASSERT_TRUE(string_is_safe("foo\\nbar", STRING_ALLOW_BACKSLASHES)); /* literal backslash, not newline */ |
| 1570 |
ASSERT_FALSE(string_is_safe("foo\nbar", STRING_ALLOW_BACKSLASHES)); /* newline still rejected */ |
| 1571 |
ASSERT_FALSE(string_is_safe("\"", STRING_ALLOW_BACKSLASHES)); /* quotes still rejected */ |
| 1572 |
ASSERT_FALSE(string_is_safe("*", STRING_ALLOW_BACKSLASHES)); /* globs still rejected */ |
| 1573 |
|
| 1574 |
/* STRING_ALLOW_QUOTES: quotes allowed, backslashes/globs still rejected. */ |
| 1575 |
ASSERT_TRUE(string_is_safe("hello", STRING_ALLOW_QUOTES)); |
| 1576 |
ASSERT_TRUE(string_is_safe("hello world", STRING_ALLOW_QUOTES)); |
| 1577 |
ASSERT_TRUE(string_is_safe("\"", STRING_ALLOW_QUOTES)); |
| 1578 |
ASSERT_TRUE(string_is_safe("'", STRING_ALLOW_QUOTES)); |
| 1579 |
ASSERT_TRUE(string_is_safe("hello\"world", STRING_ALLOW_QUOTES)); |
| 1580 |
ASSERT_TRUE(string_is_safe("it's", STRING_ALLOW_QUOTES)); |
| 1581 |
ASSERT_FALSE(string_is_safe("a\nb", STRING_ALLOW_QUOTES)); /* newline still rejected */ |
| 1582 |
ASSERT_FALSE(string_is_safe("a\\b", STRING_ALLOW_QUOTES)); /* backslashes still rejected */ |
| 1583 |
ASSERT_FALSE(string_is_safe("*", STRING_ALLOW_QUOTES)); /* globs still rejected */ |
| 1584 |
|
| 1585 |
/* STRING_ALLOW_GLOBS: globs allowed, backslashes/quotes still rejected. */ |
| 1586 |
ASSERT_TRUE(string_is_safe("hello", STRING_ALLOW_GLOBS)); |
| 1587 |
ASSERT_TRUE(string_is_safe("ab]c", STRING_ALLOW_GLOBS)); /* ']' is not in GLOB_CHARS anyway */ |
| 1588 |
ASSERT_TRUE(string_is_safe("*", STRING_ALLOW_GLOBS)); |
| 1589 |
ASSERT_TRUE(string_is_safe("?", STRING_ALLOW_GLOBS)); |
| 1590 |
ASSERT_TRUE(string_is_safe("[", STRING_ALLOW_GLOBS)); |
| 1591 |
ASSERT_TRUE(string_is_safe("foo*bar", STRING_ALLOW_GLOBS)); |
| 1592 |
ASSERT_TRUE(string_is_safe("foo?bar", STRING_ALLOW_GLOBS)); |
| 1593 |
ASSERT_TRUE(string_is_safe("foo[bar", STRING_ALLOW_GLOBS)); |
| 1594 |
ASSERT_FALSE(string_is_safe("foo\nbar", STRING_ALLOW_GLOBS)); /* newline still rejected */ |
| 1595 |
ASSERT_FALSE(string_is_safe("\"", STRING_ALLOW_GLOBS)); /* quotes still rejected */ |
| 1596 |
ASSERT_FALSE(string_is_safe("a\\b", STRING_ALLOW_GLOBS)); /* backslashes still rejected */ |
| 1597 |
|
| 1598 |
/* STRING_FILENAME: rejects empty, ".", "..", and strings with '/'. */ |
| 1599 |
ASSERT_TRUE(string_is_safe("hello", STRING_FILENAME)); |
| 1600 |
ASSERT_TRUE(string_is_safe("hello.txt", STRING_FILENAME)); |
| 1601 |
ASSERT_TRUE(string_is_safe("...", STRING_FILENAME)); |
| 1602 |
ASSERT_TRUE(string_is_safe(".hidden", STRING_FILENAME)); |
| 1603 |
ASSERT_FALSE(string_is_safe("", STRING_FILENAME)); |
| 1604 |
ASSERT_FALSE(string_is_safe(".", STRING_FILENAME)); |
| 1605 |
ASSERT_FALSE(string_is_safe("..", STRING_FILENAME)); |
| 1606 |
ASSERT_FALSE(string_is_safe("/", STRING_FILENAME)); |
| 1607 |
ASSERT_FALSE(string_is_safe("/foo", STRING_FILENAME)); |
| 1608 |
ASSERT_FALSE(string_is_safe("foo/bar", STRING_FILENAME)); |
| 1609 |
|
| 1610 |
/* Pairwise combinations. */ |
| 1611 |
ASSERT_TRUE(string_is_safe("", STRING_ALLOW_EMPTY | STRING_ASCII)); |
| 1612 |
ASSERT_FALSE(string_is_safe("über", STRING_ALLOW_EMPTY | STRING_ASCII)); |
| 1613 |
ASSERT_TRUE(string_is_safe("hello", STRING_ALLOW_EMPTY | STRING_ASCII)); |
| 1614 |
|
| 1615 |
ASSERT_TRUE(string_is_safe("ab\"cd", STRING_ALLOW_QUOTES | STRING_ALLOW_GLOBS)); |
| 1616 |
ASSERT_TRUE(string_is_safe("ab*cd", STRING_ALLOW_QUOTES | STRING_ALLOW_GLOBS)); |
| 1617 |
ASSERT_TRUE(string_is_safe("ab'*cd", STRING_ALLOW_QUOTES | STRING_ALLOW_GLOBS)); |
| 1618 |
ASSERT_FALSE(string_is_safe("ab\\cd", STRING_ALLOW_QUOTES | STRING_ALLOW_GLOBS)); /* backslash still rejected */ |
| 1619 |
|
| 1620 |
ASSERT_TRUE(string_is_safe("hello.txt", STRING_FILENAME)); |
| 1621 |
ASSERT_FALSE(string_is_safe("", STRING_FILENAME)); |
| 1622 |
ASSERT_FALSE(string_is_safe("foo/bar", STRING_FILENAME)); |
| 1623 |
|
| 1624 |
ASSERT_TRUE(string_is_safe("foo?bar", STRING_ASCII | STRING_ALLOW_GLOBS)); |
| 1625 |
ASSERT_FALSE(string_is_safe("foo\"bar", STRING_ASCII | STRING_ALLOW_GLOBS)); /* quotes still rejected */ |
| 1626 |
ASSERT_FALSE(string_is_safe("über", STRING_ASCII | STRING_ALLOW_GLOBS)); |
| 1627 |
|
| 1628 |
ASSERT_TRUE(string_is_safe("foo\\bar", STRING_ALLOW_BACKSLASHES)); |
| 1629 |
ASSERT_FALSE(string_is_safe("foo\"bar", STRING_ALLOW_BACKSLASHES)); /* quotes still rejected */ |
| 1630 |
ASSERT_FALSE(string_is_safe("foo*bar", STRING_ALLOW_BACKSLASHES)); /* globs still rejected */ |
| 1631 |
ASSERT_TRUE(string_is_safe("foo\\\"bar", STRING_ALLOW_BACKSLASHES | STRING_ALLOW_QUOTES)); |
| 1632 |
ASSERT_TRUE(string_is_safe("foo\\bar", STRING_ALLOW_BACKSLASHES | STRING_ALLOW_QUOTES)); |
| 1633 |
ASSERT_TRUE(string_is_safe("foo\"bar", STRING_ALLOW_BACKSLASHES | STRING_ALLOW_QUOTES)); |
| 1634 |
|
| 1635 |
/* All allow flags combined: only baseline (control chars, invalid UTF-8) and STRING_FILENAME apply. */ |
| 1636 |
StringSafeFlags all = |
| 1637 |
STRING_ASCII | |
| 1638 |
STRING_ALLOW_EMPTY | |
| 1639 |
STRING_ALLOW_NEWLINES | |
| 1640 |
STRING_ALLOW_BACKSLASHES | |
| 1641 |
STRING_ALLOW_QUOTES | |
| 1642 |
STRING_ALLOW_GLOBS | |
| 1643 |
STRING_FILENAME; |
| 1644 |
ASSERT_TRUE(string_is_safe("hello.txt", all)); |
| 1645 |
ASSERT_TRUE(string_is_safe("foo-bar_baz.conf", all)); |
| 1646 |
ASSERT_TRUE(string_is_safe("a", all)); |
| 1647 |
ASSERT_TRUE(string_is_safe("foo\nbar", all)); /* newline allowed */ |
| 1648 |
ASSERT_TRUE(string_is_safe("foo\\bar", all)); /* backslash allowed */ |
| 1649 |
ASSERT_TRUE(string_is_safe("foo\"bar", all)); /* quote allowed */ |
| 1650 |
ASSERT_TRUE(string_is_safe("foo'bar", all)); /* quote allowed */ |
| 1651 |
ASSERT_TRUE(string_is_safe("foo*bar", all)); /* glob allowed */ |
| 1652 |
ASSERT_TRUE(string_is_safe("foo?bar", all)); /* glob allowed */ |
| 1653 |
ASSERT_TRUE(string_is_safe("foo[bar", all)); /* glob allowed */ |
| 1654 |
ASSERT_FALSE(string_is_safe("", all)); /* fails STRING_FILENAME */ |
| 1655 |
ASSERT_FALSE(string_is_safe("über", all)); /* fails STRING_ASCII */ |
| 1656 |
ASSERT_FALSE(string_is_safe("foo/bar", all)); /* fails STRING_FILENAME */ |
| 1657 |
ASSERT_FALSE(string_is_safe(".", all)); /* fails STRING_FILENAME */ |
| 1658 |
ASSERT_FALSE(string_is_safe("..", all)); /* fails STRING_FILENAME */ |
| 1659 |
ASSERT_FALSE(string_is_safe("foo\x01""bar", all)); /* fails baseline control-char check */ |
| 1660 |
ASSERT_FALSE(string_is_safe(NULL, all)); |
| 1661 |
} |
| 1662 |
|
| 1663 |
DEFINE_TEST_MAIN(LOG_DEBUG); |