1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Test cases for string functions. 4 */ 5 6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 7 8 #include <kunit/test.h> 9 #include <linux/mm.h> 10 #include <linux/module.h> 11 #include <linux/printk.h> 12 #include <linux/slab.h> 13 #include <linux/string.h> 14 #include <linux/vmalloc.h> 15 16 #define STRCMP_LARGE_BUF_LEN 2048 17 #define STRCMP_CHANGE_POINT 1337 18 #define STRCMP_TEST_EXPECT_EQUAL(test, fn, ...) KUNIT_EXPECT_EQ(test, fn(__VA_ARGS__), 0) 19 #define STRCMP_TEST_EXPECT_LOWER(test, fn, ...) KUNIT_EXPECT_LT(test, fn(__VA_ARGS__), 0) 20 #define STRCMP_TEST_EXPECT_GREATER(test, fn, ...) KUNIT_EXPECT_GT(test, fn(__VA_ARGS__), 0) 21 22 #define STRING_TEST_MAX_LEN 128 23 #define STRING_TEST_MAX_OFFSET 16 24 25 static void string_test_memset16(struct kunit *test) 26 { 27 unsigned i, j, k; 28 u16 v, *p; 29 30 p = kunit_kzalloc(test, 256 * 2 * 2, GFP_KERNEL); 31 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, p); 32 33 for (i = 0; i < 256; i++) { 34 for (j = 0; j < 256; j++) { 35 memset(p, 0xa1, 256 * 2 * sizeof(v)); 36 memset16(p + i, 0xb1b2, j); 37 for (k = 0; k < 512; k++) { 38 v = p[k]; 39 if (k < i) { 40 KUNIT_ASSERT_EQ_MSG(test, v, 0xa1a1, 41 "i:%d j:%d k:%d", i, j, k); 42 } else if (k < i + j) { 43 KUNIT_ASSERT_EQ_MSG(test, v, 0xb1b2, 44 "i:%d j:%d k:%d", i, j, k); 45 } else { 46 KUNIT_ASSERT_EQ_MSG(test, v, 0xa1a1, 47 "i:%d j:%d k:%d", i, j, k); 48 } 49 } 50 } 51 } 52 } 53 54 static void string_test_memset32(struct kunit *test) 55 { 56 unsigned i, j, k; 57 u32 v, *p; 58 59 p = kunit_kzalloc(test, 256 * 2 * 4, GFP_KERNEL); 60 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, p); 61 62 for (i = 0; i < 256; i++) { 63 for (j = 0; j < 256; j++) { 64 memset(p, 0xa1, 256 * 2 * sizeof(v)); 65 memset32(p + i, 0xb1b2b3b4, j); 66 for (k = 0; k < 512; k++) { 67 v = p[k]; 68 if (k < i) { 69 KUNIT_ASSERT_EQ_MSG(test, v, 0xa1a1a1a1, 70 "i:%d j:%d k:%d", i, j, k); 71 } else if (k < i + j) { 72 KUNIT_ASSERT_EQ_MSG(test, v, 0xb1b2b3b4, 73 "i:%d j:%d k:%d", i, j, k); 74 } else { 75 KUNIT_ASSERT_EQ_MSG(test, v, 0xa1a1a1a1, 76 "i:%d j:%d k:%d", i, j, k); 77 } 78 } 79 } 80 } 81 } 82 83 static void string_test_memset64(struct kunit *test) 84 { 85 unsigned i, j, k; 86 u64 v, *p; 87 88 p = kunit_kzalloc(test, 256 * 2 * 8, GFP_KERNEL); 89 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, p); 90 91 for (i = 0; i < 256; i++) { 92 for (j = 0; j < 256; j++) { 93 memset(p, 0xa1, 256 * 2 * sizeof(v)); 94 memset64(p + i, 0xb1b2b3b4b5b6b7b8ULL, j); 95 for (k = 0; k < 512; k++) { 96 v = p[k]; 97 if (k < i) { 98 KUNIT_ASSERT_EQ_MSG(test, v, 0xa1a1a1a1a1a1a1a1ULL, 99 "i:%d j:%d k:%d", i, j, k); 100 } else if (k < i + j) { 101 KUNIT_ASSERT_EQ_MSG(test, v, 0xb1b2b3b4b5b6b7b8ULL, 102 "i:%d j:%d k:%d", i, j, k); 103 } else { 104 KUNIT_ASSERT_EQ_MSG(test, v, 0xa1a1a1a1a1a1a1a1ULL, 105 "i:%d j:%d k:%d", i, j, k); 106 } 107 } 108 } 109 } 110 } 111 112 static void string_test_strlen(struct kunit *test) 113 { 114 size_t buf_size; 115 char *buf, *s; 116 117 buf_size = PAGE_ALIGN(STRING_TEST_MAX_LEN + STRING_TEST_MAX_OFFSET + 1); 118 buf = vmalloc(buf_size); 119 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buf); 120 121 memset(buf, 'A', buf_size); 122 123 for (size_t offset = 0; offset < STRING_TEST_MAX_OFFSET; offset++) { 124 for (size_t len = 0; len <= STRING_TEST_MAX_LEN; len++) { 125 s = buf + buf_size - 1 - offset - len; 126 s[len] = '\0'; 127 KUNIT_EXPECT_EQ_MSG(test, strlen(s), len, 128 "offset:%zu len:%zu", offset, len); 129 s[len] = 'A'; 130 } 131 } 132 133 vfree(buf); 134 } 135 136 static void string_test_strnlen(struct kunit *test) 137 { 138 size_t buf_size; 139 char *buf, *s; 140 141 buf_size = PAGE_ALIGN(STRING_TEST_MAX_LEN + STRING_TEST_MAX_OFFSET + 1); 142 buf = vmalloc(buf_size); 143 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buf); 144 145 memset(buf, 'A', buf_size); 146 147 for (size_t offset = 0; offset < STRING_TEST_MAX_OFFSET; offset++) { 148 for (size_t len = 0; len <= STRING_TEST_MAX_LEN; len++) { 149 s = buf + buf_size - 1 - offset - len; 150 s[len] = '\0'; 151 152 if (len > 0) 153 KUNIT_EXPECT_EQ(test, strnlen(s, len - 1), len - 1); 154 if (len > 1) 155 KUNIT_EXPECT_EQ(test, strnlen(s, len - 2), len - 2); 156 157 KUNIT_EXPECT_EQ(test, strnlen(s, len), len); 158 159 KUNIT_EXPECT_EQ(test, strnlen(s, len + 1), len); 160 KUNIT_EXPECT_EQ(test, strnlen(s, len + 2), len); 161 KUNIT_EXPECT_EQ(test, strnlen(s, len + 10), len); 162 163 s[len] = 'A'; 164 } 165 } 166 167 vfree(buf); 168 } 169 170 static void string_test_strchr(struct kunit *test) 171 { 172 const char *test_string = "abcdefghijkl"; 173 const char *empty_string = ""; 174 char *result; 175 int i; 176 177 for (i = 0; i < strlen(test_string) + 1; i++) { 178 result = strchr(test_string, test_string[i]); 179 KUNIT_ASSERT_EQ_MSG(test, result - test_string, i, 180 "char:%c", 'a' + i); 181 } 182 183 result = strchr(empty_string, '\0'); 184 KUNIT_ASSERT_PTR_EQ(test, result, empty_string); 185 186 result = strchr(empty_string, 'a'); 187 KUNIT_ASSERT_NULL(test, result); 188 189 result = strchr(test_string, 'z'); 190 KUNIT_ASSERT_NULL(test, result); 191 } 192 193 static void string_test_strrchr(struct kunit *test) 194 { 195 size_t buf_size; 196 char *buf, *s; 197 198 buf_size = PAGE_ALIGN(STRING_TEST_MAX_LEN + STRING_TEST_MAX_OFFSET + 1); 199 buf = vmalloc(buf_size); 200 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buf); 201 202 memset(buf, 'A', buf_size); 203 204 for (size_t offset = 0; offset < STRING_TEST_MAX_OFFSET; offset++) { 205 for (size_t len = 0; len <= STRING_TEST_MAX_LEN; len++) { 206 s = buf + buf_size - 1 - offset - len; 207 s[len] = '\0'; 208 209 KUNIT_EXPECT_PTR_EQ(test, strrchr(s, 'Z'), NULL); 210 211 if (len > 0) 212 KUNIT_EXPECT_PTR_EQ(test, strrchr(s, 'A'), s + len - 1); 213 else 214 KUNIT_EXPECT_PTR_EQ(test, strrchr(s, 'A'), NULL); 215 216 s[len] = 'A'; 217 } 218 } 219 220 vfree(buf); 221 } 222 223 static void string_test_strnchr(struct kunit *test) 224 { 225 const char *test_string = "abcdefghijkl"; 226 const char *empty_string = ""; 227 char *result; 228 int i, j; 229 230 for (i = 0; i < strlen(test_string) + 1; i++) { 231 for (j = 0; j < strlen(test_string) + 2; j++) { 232 result = strnchr(test_string, j, test_string[i]); 233 if (j <= i) { 234 KUNIT_ASSERT_NULL_MSG(test, result, 235 "char:%c i:%d j:%d", 'a' + i, i, j); 236 } else { 237 KUNIT_ASSERT_EQ_MSG(test, result - test_string, i, 238 "char:%c i:%d j:%d", 'a' + i, i, j); 239 } 240 } 241 } 242 243 result = strnchr(empty_string, 0, '\0'); 244 KUNIT_ASSERT_NULL(test, result); 245 246 result = strnchr(empty_string, 1, '\0'); 247 KUNIT_ASSERT_PTR_EQ(test, result, empty_string); 248 249 result = strnchr(empty_string, 1, 'a'); 250 KUNIT_ASSERT_NULL(test, result); 251 252 result = strnchr(NULL, 0, '\0'); 253 KUNIT_ASSERT_NULL(test, result); 254 } 255 256 static void string_test_strspn(struct kunit *test) 257 { 258 static const struct strspn_test { 259 const char str[16]; 260 const char accept[16]; 261 const char reject[16]; 262 unsigned a; 263 unsigned r; 264 } tests[] = { 265 { "foobar", "", "", 0, 6 }, 266 { "abba", "abc", "ABBA", 4, 4 }, 267 { "abba", "a", "b", 1, 1 }, 268 { "", "abc", "abc", 0, 0}, 269 }; 270 const struct strspn_test *s = tests; 271 size_t i; 272 273 for (i = 0; i < ARRAY_SIZE(tests); ++i, ++s) { 274 KUNIT_ASSERT_EQ_MSG(test, s->a, strspn(s->str, s->accept), 275 "i:%zu", i); 276 KUNIT_ASSERT_EQ_MSG(test, s->r, strcspn(s->str, s->reject), 277 "i:%zu", i); 278 } 279 } 280 281 static char strcmp_buffer1[STRCMP_LARGE_BUF_LEN]; 282 static char strcmp_buffer2[STRCMP_LARGE_BUF_LEN]; 283 284 static void strcmp_fill_buffers(char fill1, char fill2) 285 { 286 memset(strcmp_buffer1, fill1, STRCMP_LARGE_BUF_LEN); 287 memset(strcmp_buffer2, fill2, STRCMP_LARGE_BUF_LEN); 288 strcmp_buffer1[STRCMP_LARGE_BUF_LEN - 1] = 0; 289 strcmp_buffer2[STRCMP_LARGE_BUF_LEN - 1] = 0; 290 } 291 292 static void string_test_strcmp(struct kunit *test) 293 { 294 /* Equal strings */ 295 STRCMP_TEST_EXPECT_EQUAL(test, strcmp, "Hello, Kernel!", "Hello, Kernel!"); 296 /* First string is lexicographically less than the second */ 297 STRCMP_TEST_EXPECT_LOWER(test, strcmp, "Hello, KUnit!", "Hello, Kernel!"); 298 /* First string is lexicographically larger than the second */ 299 STRCMP_TEST_EXPECT_GREATER(test, strcmp, "Hello, Kernel!", "Hello, KUnit!"); 300 /* Empty string is always lexicographically less than any non-empty string */ 301 STRCMP_TEST_EXPECT_LOWER(test, strcmp, "", "Non-empty string"); 302 /* Two empty strings should be equal */ 303 STRCMP_TEST_EXPECT_EQUAL(test, strcmp, "", ""); 304 /* Compare two strings which have only one char difference */ 305 STRCMP_TEST_EXPECT_LOWER(test, strcmp, "Abacaba", "Abadaba"); 306 /* Compare two strings which have the same prefix*/ 307 STRCMP_TEST_EXPECT_LOWER(test, strcmp, "Just a string", "Just a string and something else"); 308 } 309 310 static void string_test_strcmp_long_strings(struct kunit *test) 311 { 312 strcmp_fill_buffers('B', 'B'); 313 STRCMP_TEST_EXPECT_EQUAL(test, strcmp, strcmp_buffer1, strcmp_buffer2); 314 315 strcmp_buffer1[STRCMP_CHANGE_POINT] = 'A'; 316 STRCMP_TEST_EXPECT_LOWER(test, strcmp, strcmp_buffer1, strcmp_buffer2); 317 318 strcmp_buffer1[STRCMP_CHANGE_POINT] = 'C'; 319 STRCMP_TEST_EXPECT_GREATER(test, strcmp, strcmp_buffer1, strcmp_buffer2); 320 } 321 322 static void string_test_strncmp(struct kunit *test) 323 { 324 /* Equal strings */ 325 STRCMP_TEST_EXPECT_EQUAL(test, strncmp, "Hello, KUnit!", "Hello, KUnit!", 13); 326 /* First string is lexicographically less than the second */ 327 STRCMP_TEST_EXPECT_LOWER(test, strncmp, "Hello, KUnit!", "Hello, Kernel!", 13); 328 /* Result is always 'equal' when count = 0 */ 329 STRCMP_TEST_EXPECT_EQUAL(test, strncmp, "Hello, Kernel!", "Hello, KUnit!", 0); 330 /* Strings with common prefix are equal if count = length of prefix */ 331 STRCMP_TEST_EXPECT_EQUAL(test, strncmp, "Abacaba", "Abadaba", 3); 332 /* Strings with common prefix are not equal when count = length of prefix + 1 */ 333 STRCMP_TEST_EXPECT_LOWER(test, strncmp, "Abacaba", "Abadaba", 4); 334 /* If one string is a prefix of another, the shorter string is lexicographically smaller */ 335 STRCMP_TEST_EXPECT_LOWER(test, strncmp, "Just a string", "Just a string and something else", 336 strlen("Just a string and something else")); 337 /* 338 * If one string is a prefix of another, and we check first length 339 * of prefix chars, the result is 'equal' 340 */ 341 STRCMP_TEST_EXPECT_EQUAL(test, strncmp, "Just a string", "Just a string and something else", 342 strlen("Just a string")); 343 } 344 345 static void string_test_strncmp_long_strings(struct kunit *test) 346 { 347 strcmp_fill_buffers('B', 'B'); 348 STRCMP_TEST_EXPECT_EQUAL(test, strncmp, strcmp_buffer1, 349 strcmp_buffer2, STRCMP_LARGE_BUF_LEN); 350 351 strcmp_buffer1[STRCMP_CHANGE_POINT] = 'A'; 352 STRCMP_TEST_EXPECT_LOWER(test, strncmp, strcmp_buffer1, 353 strcmp_buffer2, STRCMP_LARGE_BUF_LEN); 354 355 strcmp_buffer1[STRCMP_CHANGE_POINT] = 'C'; 356 STRCMP_TEST_EXPECT_GREATER(test, strncmp, strcmp_buffer1, 357 strcmp_buffer2, STRCMP_LARGE_BUF_LEN); 358 /* the strings are equal up to STRCMP_CHANGE_POINT */ 359 STRCMP_TEST_EXPECT_EQUAL(test, strncmp, strcmp_buffer1, 360 strcmp_buffer2, STRCMP_CHANGE_POINT); 361 STRCMP_TEST_EXPECT_GREATER(test, strncmp, strcmp_buffer1, 362 strcmp_buffer2, STRCMP_CHANGE_POINT + 1); 363 } 364 365 static void string_test_strcasecmp(struct kunit *test) 366 { 367 /* Same strings in different case should be equal */ 368 STRCMP_TEST_EXPECT_EQUAL(test, strcasecmp, "Hello, Kernel!", "HeLLO, KErNeL!"); 369 /* Empty strings should be equal */ 370 STRCMP_TEST_EXPECT_EQUAL(test, strcasecmp, "", ""); 371 /* Despite ascii code for 'a' is larger than ascii code for 'B', 'a' < 'B' */ 372 STRCMP_TEST_EXPECT_LOWER(test, strcasecmp, "a", "B"); 373 STRCMP_TEST_EXPECT_GREATER(test, strcasecmp, "B", "a"); 374 /* Special symbols and numbers should be processed correctly */ 375 STRCMP_TEST_EXPECT_EQUAL(test, strcasecmp, "-+**.1230ghTTT~^", "-+**.1230Ghttt~^"); 376 } 377 378 static void string_test_strcasecmp_long_strings(struct kunit *test) 379 { 380 strcmp_fill_buffers('b', 'B'); 381 STRCMP_TEST_EXPECT_EQUAL(test, strcasecmp, strcmp_buffer1, strcmp_buffer2); 382 383 strcmp_buffer1[STRCMP_CHANGE_POINT] = 'a'; 384 STRCMP_TEST_EXPECT_LOWER(test, strcasecmp, strcmp_buffer1, strcmp_buffer2); 385 386 strcmp_buffer1[STRCMP_CHANGE_POINT] = 'C'; 387 STRCMP_TEST_EXPECT_GREATER(test, strcasecmp, strcmp_buffer1, strcmp_buffer2); 388 } 389 390 static void string_test_strncasecmp(struct kunit *test) 391 { 392 /* Same strings in different case should be equal */ 393 STRCMP_TEST_EXPECT_EQUAL(test, strncasecmp, "AbAcAbA", "Abacaba", strlen("Abacaba")); 394 /* strncasecmp should check 'count' chars only */ 395 STRCMP_TEST_EXPECT_EQUAL(test, strncasecmp, "AbaCaBa", "abaCaDa", 5); 396 STRCMP_TEST_EXPECT_LOWER(test, strncasecmp, "a", "B", 1); 397 STRCMP_TEST_EXPECT_GREATER(test, strncasecmp, "B", "a", 1); 398 /* Result is always 'equal' when count = 0 */ 399 STRCMP_TEST_EXPECT_EQUAL(test, strncasecmp, "Abacaba", "Not abacaba", 0); 400 } 401 402 static void string_test_strncasecmp_long_strings(struct kunit *test) 403 { 404 strcmp_fill_buffers('b', 'B'); 405 STRCMP_TEST_EXPECT_EQUAL(test, strncasecmp, strcmp_buffer1, 406 strcmp_buffer2, STRCMP_LARGE_BUF_LEN); 407 408 strcmp_buffer1[STRCMP_CHANGE_POINT] = 'a'; 409 STRCMP_TEST_EXPECT_LOWER(test, strncasecmp, strcmp_buffer1, 410 strcmp_buffer2, STRCMP_LARGE_BUF_LEN); 411 412 strcmp_buffer1[STRCMP_CHANGE_POINT] = 'C'; 413 STRCMP_TEST_EXPECT_GREATER(test, strncasecmp, strcmp_buffer1, 414 strcmp_buffer2, STRCMP_LARGE_BUF_LEN); 415 416 STRCMP_TEST_EXPECT_EQUAL(test, strncasecmp, strcmp_buffer1, 417 strcmp_buffer2, STRCMP_CHANGE_POINT); 418 STRCMP_TEST_EXPECT_GREATER(test, strncasecmp, strcmp_buffer1, 419 strcmp_buffer2, STRCMP_CHANGE_POINT + 1); 420 } 421 422 /** 423 * strscpy_check() - Run a specific test case. 424 * @test: KUnit test context pointer 425 * @src: Source string, argument to strscpy_pad() 426 * @count: Size of destination buffer, argument to strscpy_pad() 427 * @expected: Expected return value from call to strscpy_pad() 428 * @chars: Number of characters from the src string expected to be 429 * written to the dst buffer. 430 * @terminator: 1 if there should be a terminating null byte 0 otherwise. 431 * @pad: Number of pad characters expected (in the tail of dst buffer). 432 * (@pad does not include the null terminator byte.) 433 * 434 * Calls strscpy_pad() and verifies the return value and state of the 435 * destination buffer after the call returns. 436 */ 437 static void strscpy_check(struct kunit *test, char *src, int count, 438 int expected, int chars, int terminator, int pad) 439 { 440 int nr_bytes_poison; 441 int max_expected; 442 int max_count; 443 int written; 444 char buf[6]; 445 int index, i; 446 const char POISON = 'z'; 447 448 KUNIT_ASSERT_TRUE_MSG(test, src != NULL, 449 "null source string not supported"); 450 451 memset(buf, POISON, sizeof(buf)); 452 /* Future proofing test suite, validate args */ 453 max_count = sizeof(buf) - 2; /* Space for null and to verify overflow */ 454 max_expected = count - 1; /* Space for the null */ 455 456 KUNIT_ASSERT_LE_MSG(test, count, max_count, 457 "count (%d) is too big (%d) ... aborting", count, max_count); 458 KUNIT_EXPECT_LE_MSG(test, expected, max_expected, 459 "expected (%d) is bigger than can possibly be returned (%d)", 460 expected, max_expected); 461 462 written = strscpy_pad(buf, src, count); 463 KUNIT_ASSERT_EQ(test, written, expected); 464 465 if (count && written == -E2BIG) { 466 KUNIT_ASSERT_EQ_MSG(test, 0, strncmp(buf, src, count - 1), 467 "buffer state invalid for -E2BIG"); 468 KUNIT_ASSERT_EQ_MSG(test, buf[count - 1], '\0', 469 "too big string is not null terminated correctly"); 470 } 471 472 for (i = 0; i < chars; i++) 473 KUNIT_ASSERT_EQ_MSG(test, buf[i], src[i], 474 "buf[i]==%c != src[i]==%c", buf[i], src[i]); 475 476 if (terminator) 477 KUNIT_ASSERT_EQ_MSG(test, buf[count - 1], '\0', 478 "string is not null terminated correctly"); 479 480 for (i = 0; i < pad; i++) { 481 index = chars + terminator + i; 482 KUNIT_ASSERT_EQ_MSG(test, buf[index], '\0', 483 "padding missing at index: %d", i); 484 } 485 486 nr_bytes_poison = sizeof(buf) - chars - terminator - pad; 487 for (i = 0; i < nr_bytes_poison; i++) { 488 index = sizeof(buf) - 1 - i; /* Check from the end back */ 489 KUNIT_ASSERT_EQ_MSG(test, buf[index], POISON, 490 "poison value missing at index: %d", i); 491 } 492 } 493 494 static void string_test_strscpy(struct kunit *test) 495 { 496 char dest[8]; 497 498 /* 499 * strscpy_check() uses a destination buffer of size 6 and needs at 500 * least 2 characters spare (one for null and one to check for 501 * overflow). This means we should only call tc() with 502 * strings up to a maximum of 4 characters long and 'count' 503 * should not exceed 4. To test with longer strings increase 504 * the buffer size in tc(). 505 */ 506 507 /* strscpy_check(test, src, count, expected, chars, terminator, pad) */ 508 strscpy_check(test, "a", 0, -E2BIG, 0, 0, 0); 509 strscpy_check(test, "", 0, -E2BIG, 0, 0, 0); 510 511 strscpy_check(test, "a", 1, -E2BIG, 0, 1, 0); 512 strscpy_check(test, "", 1, 0, 0, 1, 0); 513 514 strscpy_check(test, "ab", 2, -E2BIG, 1, 1, 0); 515 strscpy_check(test, "a", 2, 1, 1, 1, 0); 516 strscpy_check(test, "", 2, 0, 0, 1, 1); 517 518 strscpy_check(test, "abc", 3, -E2BIG, 2, 1, 0); 519 strscpy_check(test, "ab", 3, 2, 2, 1, 0); 520 strscpy_check(test, "a", 3, 1, 1, 1, 1); 521 strscpy_check(test, "", 3, 0, 0, 1, 2); 522 523 strscpy_check(test, "abcd", 4, -E2BIG, 3, 1, 0); 524 strscpy_check(test, "abc", 4, 3, 3, 1, 0); 525 strscpy_check(test, "ab", 4, 2, 2, 1, 1); 526 strscpy_check(test, "a", 4, 1, 1, 1, 2); 527 strscpy_check(test, "", 4, 0, 0, 1, 3); 528 529 /* Compile-time-known source strings. */ 530 KUNIT_EXPECT_EQ(test, strscpy(dest, "", ARRAY_SIZE(dest)), 0); 531 KUNIT_EXPECT_EQ(test, strscpy(dest, "", 3), 0); 532 KUNIT_EXPECT_EQ(test, strscpy(dest, "", 1), 0); 533 KUNIT_EXPECT_EQ(test, strscpy(dest, "", 0), -E2BIG); 534 KUNIT_EXPECT_EQ(test, strscpy(dest, "Fixed", ARRAY_SIZE(dest)), 5); 535 KUNIT_EXPECT_EQ(test, strscpy(dest, "Fixed", 3), -E2BIG); 536 KUNIT_EXPECT_EQ(test, strscpy(dest, "Fixed", 1), -E2BIG); 537 KUNIT_EXPECT_EQ(test, strscpy(dest, "Fixed", 0), -E2BIG); 538 KUNIT_EXPECT_EQ(test, strscpy(dest, "This is too long", ARRAY_SIZE(dest)), -E2BIG); 539 } 540 541 static volatile int unconst; 542 543 static void string_test_strcat(struct kunit *test) 544 { 545 char dest[8]; 546 547 /* Destination is terminated. */ 548 memset(dest, 0, sizeof(dest)); 549 KUNIT_EXPECT_EQ(test, strlen(dest), 0); 550 /* Empty copy does nothing. */ 551 KUNIT_EXPECT_TRUE(test, strcat(dest, "") == dest); 552 KUNIT_EXPECT_STREQ(test, dest, ""); 553 /* 4 characters copied in, stops at %NUL. */ 554 KUNIT_EXPECT_TRUE(test, strcat(dest, "four\000123") == dest); 555 KUNIT_EXPECT_STREQ(test, dest, "four"); 556 KUNIT_EXPECT_EQ(test, dest[5], '\0'); 557 /* 2 more characters copied in okay. */ 558 KUNIT_EXPECT_TRUE(test, strcat(dest, "AB") == dest); 559 KUNIT_EXPECT_STREQ(test, dest, "fourAB"); 560 } 561 562 static void string_test_strncat(struct kunit *test) 563 { 564 char dest[8]; 565 566 /* Destination is terminated. */ 567 memset(dest, 0, sizeof(dest)); 568 KUNIT_EXPECT_EQ(test, strlen(dest), 0); 569 /* Empty copy of size 0 does nothing. */ 570 KUNIT_EXPECT_TRUE(test, strncat(dest, "", 0 + unconst) == dest); 571 KUNIT_EXPECT_STREQ(test, dest, ""); 572 /* Empty copy of size 1 does nothing too. */ 573 KUNIT_EXPECT_TRUE(test, strncat(dest, "", 1 + unconst) == dest); 574 KUNIT_EXPECT_STREQ(test, dest, ""); 575 /* Copy of max 0 characters should do nothing. */ 576 KUNIT_EXPECT_TRUE(test, strncat(dest, "asdf", 0 + unconst) == dest); 577 KUNIT_EXPECT_STREQ(test, dest, ""); 578 579 /* 4 characters copied in, even if max is 8. */ 580 KUNIT_EXPECT_TRUE(test, strncat(dest, "four\000123", 8 + unconst) == dest); 581 KUNIT_EXPECT_STREQ(test, dest, "four"); 582 KUNIT_EXPECT_EQ(test, dest[5], '\0'); 583 KUNIT_EXPECT_EQ(test, dest[6], '\0'); 584 /* 2 characters copied in okay, 2 ignored. */ 585 KUNIT_EXPECT_TRUE(test, strncat(dest, "ABCD", 2 + unconst) == dest); 586 KUNIT_EXPECT_STREQ(test, dest, "fourAB"); 587 } 588 589 static void string_test_strlcat(struct kunit *test) 590 { 591 char dest[8] = ""; 592 int len = sizeof(dest) + unconst; 593 594 /* Destination is terminated. */ 595 KUNIT_EXPECT_EQ(test, strlen(dest), 0); 596 /* Empty copy is size 0. */ 597 KUNIT_EXPECT_EQ(test, strlcat(dest, "", len), 0); 598 KUNIT_EXPECT_STREQ(test, dest, ""); 599 /* Size 1 should keep buffer terminated, report size of source only. */ 600 KUNIT_EXPECT_EQ(test, strlcat(dest, "four", 1 + unconst), 4); 601 KUNIT_EXPECT_STREQ(test, dest, ""); 602 603 /* 4 characters copied in. */ 604 KUNIT_EXPECT_EQ(test, strlcat(dest, "four", len), 4); 605 KUNIT_EXPECT_STREQ(test, dest, "four"); 606 /* 2 characters copied in okay, gets to 6 total. */ 607 KUNIT_EXPECT_EQ(test, strlcat(dest, "AB", len), 6); 608 KUNIT_EXPECT_STREQ(test, dest, "fourAB"); 609 /* 2 characters ignored if max size (7) reached. */ 610 KUNIT_EXPECT_EQ(test, strlcat(dest, "CD", 7 + unconst), 8); 611 KUNIT_EXPECT_STREQ(test, dest, "fourAB"); 612 /* 1 of 2 characters skipped, now at true max size. */ 613 KUNIT_EXPECT_EQ(test, strlcat(dest, "EFG", len), 9); 614 KUNIT_EXPECT_STREQ(test, dest, "fourABE"); 615 /* Everything else ignored, now at full size. */ 616 KUNIT_EXPECT_EQ(test, strlcat(dest, "1234", len), 11); 617 KUNIT_EXPECT_STREQ(test, dest, "fourABE"); 618 } 619 620 static void string_test_strtomem(struct kunit *test) 621 { 622 static const char input[sizeof(unsigned long)] = "hi"; 623 static const char truncate[] = "this is too long"; 624 struct { 625 unsigned long canary1; 626 unsigned char output[sizeof(unsigned long)] __nonstring; 627 unsigned long canary2; 628 } wrap; 629 630 memset(&wrap, 0xFF, sizeof(wrap)); 631 KUNIT_EXPECT_EQ_MSG(test, wrap.canary1, ULONG_MAX, 632 "bad initial canary value"); 633 KUNIT_EXPECT_EQ_MSG(test, wrap.canary2, ULONG_MAX, 634 "bad initial canary value"); 635 636 /* Check unpadded copy leaves surroundings untouched. */ 637 strtomem(wrap.output, input); 638 KUNIT_EXPECT_EQ(test, wrap.canary1, ULONG_MAX); 639 KUNIT_EXPECT_EQ(test, wrap.output[0], input[0]); 640 KUNIT_EXPECT_EQ(test, wrap.output[1], input[1]); 641 for (size_t i = 2; i < sizeof(wrap.output); i++) 642 KUNIT_EXPECT_EQ(test, wrap.output[i], 0xFF); 643 KUNIT_EXPECT_EQ(test, wrap.canary2, ULONG_MAX); 644 645 /* Check truncated copy leaves surroundings untouched. */ 646 memset(&wrap, 0xFF, sizeof(wrap)); 647 strtomem(wrap.output, truncate); 648 KUNIT_EXPECT_EQ(test, wrap.canary1, ULONG_MAX); 649 for (size_t i = 0; i < sizeof(wrap.output); i++) 650 KUNIT_EXPECT_EQ(test, wrap.output[i], truncate[i]); 651 KUNIT_EXPECT_EQ(test, wrap.canary2, ULONG_MAX); 652 653 /* Check padded copy leaves only string padded. */ 654 memset(&wrap, 0xFF, sizeof(wrap)); 655 strtomem_pad(wrap.output, input, 0xAA); 656 KUNIT_EXPECT_EQ(test, wrap.canary1, ULONG_MAX); 657 KUNIT_EXPECT_EQ(test, wrap.output[0], input[0]); 658 KUNIT_EXPECT_EQ(test, wrap.output[1], input[1]); 659 for (size_t i = 2; i < sizeof(wrap.output); i++) 660 KUNIT_EXPECT_EQ(test, wrap.output[i], 0xAA); 661 KUNIT_EXPECT_EQ(test, wrap.canary2, ULONG_MAX); 662 663 /* Check truncated padded copy has no padding. */ 664 memset(&wrap, 0xFF, sizeof(wrap)); 665 strtomem(wrap.output, truncate); 666 KUNIT_EXPECT_EQ(test, wrap.canary1, ULONG_MAX); 667 for (size_t i = 0; i < sizeof(wrap.output); i++) 668 KUNIT_EXPECT_EQ(test, wrap.output[i], truncate[i]); 669 KUNIT_EXPECT_EQ(test, wrap.canary2, ULONG_MAX); 670 } 671 672 673 static void string_test_memtostr(struct kunit *test) 674 { 675 char nonstring[7] __nonstring = { 'a', 'b', 'c', 'd', 'e', 'f', 'g' }; 676 char nonstring_small[3] __nonstring = { 'a', 'b', 'c' }; 677 char dest[sizeof(nonstring) + 1]; 678 679 /* Copy in a non-NUL-terminated string into exactly right-sized dest. */ 680 KUNIT_EXPECT_EQ(test, sizeof(dest), sizeof(nonstring) + 1); 681 memset(dest, 'X', sizeof(dest)); 682 memtostr(dest, nonstring); 683 KUNIT_EXPECT_STREQ(test, dest, "abcdefg"); 684 memset(dest, 'X', sizeof(dest)); 685 memtostr(dest, nonstring_small); 686 KUNIT_EXPECT_STREQ(test, dest, "abc"); 687 KUNIT_EXPECT_EQ(test, dest[7], 'X'); 688 689 memset(dest, 'X', sizeof(dest)); 690 memtostr_pad(dest, nonstring); 691 KUNIT_EXPECT_STREQ(test, dest, "abcdefg"); 692 memset(dest, 'X', sizeof(dest)); 693 memtostr_pad(dest, nonstring_small); 694 KUNIT_EXPECT_STREQ(test, dest, "abc"); 695 KUNIT_EXPECT_EQ(test, dest[7], '\0'); 696 } 697 698 static void string_test_strends(struct kunit *test) 699 { 700 KUNIT_EXPECT_TRUE(test, strends("foo-bar", "bar")); 701 KUNIT_EXPECT_TRUE(test, strends("foo-bar", "-bar")); 702 KUNIT_EXPECT_TRUE(test, strends("foobar", "foobar")); 703 KUNIT_EXPECT_TRUE(test, strends("foobar", "")); 704 KUNIT_EXPECT_FALSE(test, strends("bar", "foobar")); 705 KUNIT_EXPECT_FALSE(test, strends("", "foo")); 706 KUNIT_EXPECT_FALSE(test, strends("foobar", "ba")); 707 KUNIT_EXPECT_TRUE(test, strends("", "")); 708 } 709 710 static struct kunit_case string_test_cases[] = { 711 KUNIT_CASE(string_test_memset16), 712 KUNIT_CASE(string_test_memset32), 713 KUNIT_CASE(string_test_memset64), 714 KUNIT_CASE(string_test_strlen), 715 KUNIT_CASE(string_test_strnlen), 716 KUNIT_CASE(string_test_strchr), 717 KUNIT_CASE(string_test_strnchr), 718 KUNIT_CASE(string_test_strrchr), 719 KUNIT_CASE(string_test_strspn), 720 KUNIT_CASE(string_test_strcmp), 721 KUNIT_CASE(string_test_strcmp_long_strings), 722 KUNIT_CASE(string_test_strncmp), 723 KUNIT_CASE(string_test_strncmp_long_strings), 724 KUNIT_CASE(string_test_strcasecmp), 725 KUNIT_CASE(string_test_strcasecmp_long_strings), 726 KUNIT_CASE(string_test_strncasecmp), 727 KUNIT_CASE(string_test_strncasecmp_long_strings), 728 KUNIT_CASE(string_test_strscpy), 729 KUNIT_CASE(string_test_strcat), 730 KUNIT_CASE(string_test_strncat), 731 KUNIT_CASE(string_test_strlcat), 732 KUNIT_CASE(string_test_strtomem), 733 KUNIT_CASE(string_test_memtostr), 734 KUNIT_CASE(string_test_strends), 735 {} 736 }; 737 738 static struct kunit_suite string_test_suite = { 739 .name = "string", 740 .test_cases = string_test_cases, 741 }; 742 743 kunit_test_suites(&string_test_suite); 744 745 MODULE_DESCRIPTION("Test cases for string functions"); 746 MODULE_LICENSE("GPL v2"); 747