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_strnchr(struct kunit *test) 194 { 195 const char *test_string = "abcdefghijkl"; 196 const char *empty_string = ""; 197 char *result; 198 int i, j; 199 200 for (i = 0; i < strlen(test_string) + 1; i++) { 201 for (j = 0; j < strlen(test_string) + 2; j++) { 202 result = strnchr(test_string, j, test_string[i]); 203 if (j <= i) { 204 KUNIT_ASSERT_NULL_MSG(test, result, 205 "char:%c i:%d j:%d", 'a' + i, i, j); 206 } else { 207 KUNIT_ASSERT_EQ_MSG(test, result - test_string, i, 208 "char:%c i:%d j:%d", 'a' + i, i, j); 209 } 210 } 211 } 212 213 result = strnchr(empty_string, 0, '\0'); 214 KUNIT_ASSERT_NULL(test, result); 215 216 result = strnchr(empty_string, 1, '\0'); 217 KUNIT_ASSERT_PTR_EQ(test, result, empty_string); 218 219 result = strnchr(empty_string, 1, 'a'); 220 KUNIT_ASSERT_NULL(test, result); 221 222 result = strnchr(NULL, 0, '\0'); 223 KUNIT_ASSERT_NULL(test, result); 224 } 225 226 static void string_test_strspn(struct kunit *test) 227 { 228 static const struct strspn_test { 229 const char str[16]; 230 const char accept[16]; 231 const char reject[16]; 232 unsigned a; 233 unsigned r; 234 } tests[] = { 235 { "foobar", "", "", 0, 6 }, 236 { "abba", "abc", "ABBA", 4, 4 }, 237 { "abba", "a", "b", 1, 1 }, 238 { "", "abc", "abc", 0, 0}, 239 }; 240 const struct strspn_test *s = tests; 241 size_t i; 242 243 for (i = 0; i < ARRAY_SIZE(tests); ++i, ++s) { 244 KUNIT_ASSERT_EQ_MSG(test, s->a, strspn(s->str, s->accept), 245 "i:%zu", i); 246 KUNIT_ASSERT_EQ_MSG(test, s->r, strcspn(s->str, s->reject), 247 "i:%zu", i); 248 } 249 } 250 251 static char strcmp_buffer1[STRCMP_LARGE_BUF_LEN]; 252 static char strcmp_buffer2[STRCMP_LARGE_BUF_LEN]; 253 254 static void strcmp_fill_buffers(char fill1, char fill2) 255 { 256 memset(strcmp_buffer1, fill1, STRCMP_LARGE_BUF_LEN); 257 memset(strcmp_buffer2, fill2, STRCMP_LARGE_BUF_LEN); 258 strcmp_buffer1[STRCMP_LARGE_BUF_LEN - 1] = 0; 259 strcmp_buffer2[STRCMP_LARGE_BUF_LEN - 1] = 0; 260 } 261 262 static void string_test_strcmp(struct kunit *test) 263 { 264 /* Equal strings */ 265 STRCMP_TEST_EXPECT_EQUAL(test, strcmp, "Hello, Kernel!", "Hello, Kernel!"); 266 /* First string is lexicographically less than the second */ 267 STRCMP_TEST_EXPECT_LOWER(test, strcmp, "Hello, KUnit!", "Hello, Kernel!"); 268 /* First string is lexicographically larger than the second */ 269 STRCMP_TEST_EXPECT_GREATER(test, strcmp, "Hello, Kernel!", "Hello, KUnit!"); 270 /* Empty string is always lexicographically less than any non-empty string */ 271 STRCMP_TEST_EXPECT_LOWER(test, strcmp, "", "Non-empty string"); 272 /* Two empty strings should be equal */ 273 STRCMP_TEST_EXPECT_EQUAL(test, strcmp, "", ""); 274 /* Compare two strings which have only one char difference */ 275 STRCMP_TEST_EXPECT_LOWER(test, strcmp, "Abacaba", "Abadaba"); 276 /* Compare two strings which have the same prefix*/ 277 STRCMP_TEST_EXPECT_LOWER(test, strcmp, "Just a string", "Just a string and something else"); 278 } 279 280 static void string_test_strcmp_long_strings(struct kunit *test) 281 { 282 strcmp_fill_buffers('B', 'B'); 283 STRCMP_TEST_EXPECT_EQUAL(test, strcmp, strcmp_buffer1, strcmp_buffer2); 284 285 strcmp_buffer1[STRCMP_CHANGE_POINT] = 'A'; 286 STRCMP_TEST_EXPECT_LOWER(test, strcmp, strcmp_buffer1, strcmp_buffer2); 287 288 strcmp_buffer1[STRCMP_CHANGE_POINT] = 'C'; 289 STRCMP_TEST_EXPECT_GREATER(test, strcmp, strcmp_buffer1, strcmp_buffer2); 290 } 291 292 static void string_test_strncmp(struct kunit *test) 293 { 294 /* Equal strings */ 295 STRCMP_TEST_EXPECT_EQUAL(test, strncmp, "Hello, KUnit!", "Hello, KUnit!", 13); 296 /* First string is lexicographically less than the second */ 297 STRCMP_TEST_EXPECT_LOWER(test, strncmp, "Hello, KUnit!", "Hello, Kernel!", 13); 298 /* Result is always 'equal' when count = 0 */ 299 STRCMP_TEST_EXPECT_EQUAL(test, strncmp, "Hello, Kernel!", "Hello, KUnit!", 0); 300 /* Strings with common prefix are equal if count = length of prefix */ 301 STRCMP_TEST_EXPECT_EQUAL(test, strncmp, "Abacaba", "Abadaba", 3); 302 /* Strings with common prefix are not equal when count = length of prefix + 1 */ 303 STRCMP_TEST_EXPECT_LOWER(test, strncmp, "Abacaba", "Abadaba", 4); 304 /* If one string is a prefix of another, the shorter string is lexicographically smaller */ 305 STRCMP_TEST_EXPECT_LOWER(test, strncmp, "Just a string", "Just a string and something else", 306 strlen("Just a string and something else")); 307 /* 308 * If one string is a prefix of another, and we check first length 309 * of prefix chars, the result is 'equal' 310 */ 311 STRCMP_TEST_EXPECT_EQUAL(test, strncmp, "Just a string", "Just a string and something else", 312 strlen("Just a string")); 313 } 314 315 static void string_test_strncmp_long_strings(struct kunit *test) 316 { 317 strcmp_fill_buffers('B', 'B'); 318 STRCMP_TEST_EXPECT_EQUAL(test, strncmp, strcmp_buffer1, 319 strcmp_buffer2, STRCMP_LARGE_BUF_LEN); 320 321 strcmp_buffer1[STRCMP_CHANGE_POINT] = 'A'; 322 STRCMP_TEST_EXPECT_LOWER(test, strncmp, strcmp_buffer1, 323 strcmp_buffer2, STRCMP_LARGE_BUF_LEN); 324 325 strcmp_buffer1[STRCMP_CHANGE_POINT] = 'C'; 326 STRCMP_TEST_EXPECT_GREATER(test, strncmp, strcmp_buffer1, 327 strcmp_buffer2, STRCMP_LARGE_BUF_LEN); 328 /* the strings are equal up to STRCMP_CHANGE_POINT */ 329 STRCMP_TEST_EXPECT_EQUAL(test, strncmp, strcmp_buffer1, 330 strcmp_buffer2, STRCMP_CHANGE_POINT); 331 STRCMP_TEST_EXPECT_GREATER(test, strncmp, strcmp_buffer1, 332 strcmp_buffer2, STRCMP_CHANGE_POINT + 1); 333 } 334 335 static void string_test_strcasecmp(struct kunit *test) 336 { 337 /* Same strings in different case should be equal */ 338 STRCMP_TEST_EXPECT_EQUAL(test, strcasecmp, "Hello, Kernel!", "HeLLO, KErNeL!"); 339 /* Empty strings should be equal */ 340 STRCMP_TEST_EXPECT_EQUAL(test, strcasecmp, "", ""); 341 /* Despite ascii code for 'a' is larger than ascii code for 'B', 'a' < 'B' */ 342 STRCMP_TEST_EXPECT_LOWER(test, strcasecmp, "a", "B"); 343 STRCMP_TEST_EXPECT_GREATER(test, strcasecmp, "B", "a"); 344 /* Special symbols and numbers should be processed correctly */ 345 STRCMP_TEST_EXPECT_EQUAL(test, strcasecmp, "-+**.1230ghTTT~^", "-+**.1230Ghttt~^"); 346 } 347 348 static void string_test_strcasecmp_long_strings(struct kunit *test) 349 { 350 strcmp_fill_buffers('b', 'B'); 351 STRCMP_TEST_EXPECT_EQUAL(test, strcasecmp, strcmp_buffer1, strcmp_buffer2); 352 353 strcmp_buffer1[STRCMP_CHANGE_POINT] = 'a'; 354 STRCMP_TEST_EXPECT_LOWER(test, strcasecmp, strcmp_buffer1, strcmp_buffer2); 355 356 strcmp_buffer1[STRCMP_CHANGE_POINT] = 'C'; 357 STRCMP_TEST_EXPECT_GREATER(test, strcasecmp, strcmp_buffer1, strcmp_buffer2); 358 } 359 360 static void string_test_strncasecmp(struct kunit *test) 361 { 362 /* Same strings in different case should be equal */ 363 STRCMP_TEST_EXPECT_EQUAL(test, strncasecmp, "AbAcAbA", "Abacaba", strlen("Abacaba")); 364 /* strncasecmp should check 'count' chars only */ 365 STRCMP_TEST_EXPECT_EQUAL(test, strncasecmp, "AbaCaBa", "abaCaDa", 5); 366 STRCMP_TEST_EXPECT_LOWER(test, strncasecmp, "a", "B", 1); 367 STRCMP_TEST_EXPECT_GREATER(test, strncasecmp, "B", "a", 1); 368 /* Result is always 'equal' when count = 0 */ 369 STRCMP_TEST_EXPECT_EQUAL(test, strncasecmp, "Abacaba", "Not abacaba", 0); 370 } 371 372 static void string_test_strncasecmp_long_strings(struct kunit *test) 373 { 374 strcmp_fill_buffers('b', 'B'); 375 STRCMP_TEST_EXPECT_EQUAL(test, strncasecmp, strcmp_buffer1, 376 strcmp_buffer2, STRCMP_LARGE_BUF_LEN); 377 378 strcmp_buffer1[STRCMP_CHANGE_POINT] = 'a'; 379 STRCMP_TEST_EXPECT_LOWER(test, strncasecmp, strcmp_buffer1, 380 strcmp_buffer2, STRCMP_LARGE_BUF_LEN); 381 382 strcmp_buffer1[STRCMP_CHANGE_POINT] = 'C'; 383 STRCMP_TEST_EXPECT_GREATER(test, strncasecmp, strcmp_buffer1, 384 strcmp_buffer2, STRCMP_LARGE_BUF_LEN); 385 386 STRCMP_TEST_EXPECT_EQUAL(test, strncasecmp, strcmp_buffer1, 387 strcmp_buffer2, STRCMP_CHANGE_POINT); 388 STRCMP_TEST_EXPECT_GREATER(test, strncasecmp, strcmp_buffer1, 389 strcmp_buffer2, STRCMP_CHANGE_POINT + 1); 390 } 391 392 /** 393 * strscpy_check() - Run a specific test case. 394 * @test: KUnit test context pointer 395 * @src: Source string, argument to strscpy_pad() 396 * @count: Size of destination buffer, argument to strscpy_pad() 397 * @expected: Expected return value from call to strscpy_pad() 398 * @chars: Number of characters from the src string expected to be 399 * written to the dst buffer. 400 * @terminator: 1 if there should be a terminating null byte 0 otherwise. 401 * @pad: Number of pad characters expected (in the tail of dst buffer). 402 * (@pad does not include the null terminator byte.) 403 * 404 * Calls strscpy_pad() and verifies the return value and state of the 405 * destination buffer after the call returns. 406 */ 407 static void strscpy_check(struct kunit *test, char *src, int count, 408 int expected, int chars, int terminator, int pad) 409 { 410 int nr_bytes_poison; 411 int max_expected; 412 int max_count; 413 int written; 414 char buf[6]; 415 int index, i; 416 const char POISON = 'z'; 417 418 KUNIT_ASSERT_TRUE_MSG(test, src != NULL, 419 "null source string not supported"); 420 421 memset(buf, POISON, sizeof(buf)); 422 /* Future proofing test suite, validate args */ 423 max_count = sizeof(buf) - 2; /* Space for null and to verify overflow */ 424 max_expected = count - 1; /* Space for the null */ 425 426 KUNIT_ASSERT_LE_MSG(test, count, max_count, 427 "count (%d) is too big (%d) ... aborting", count, max_count); 428 KUNIT_EXPECT_LE_MSG(test, expected, max_expected, 429 "expected (%d) is bigger than can possibly be returned (%d)", 430 expected, max_expected); 431 432 written = strscpy_pad(buf, src, count); 433 KUNIT_ASSERT_EQ(test, written, expected); 434 435 if (count && written == -E2BIG) { 436 KUNIT_ASSERT_EQ_MSG(test, 0, strncmp(buf, src, count - 1), 437 "buffer state invalid for -E2BIG"); 438 KUNIT_ASSERT_EQ_MSG(test, buf[count - 1], '\0', 439 "too big string is not null terminated correctly"); 440 } 441 442 for (i = 0; i < chars; i++) 443 KUNIT_ASSERT_EQ_MSG(test, buf[i], src[i], 444 "buf[i]==%c != src[i]==%c", buf[i], src[i]); 445 446 if (terminator) 447 KUNIT_ASSERT_EQ_MSG(test, buf[count - 1], '\0', 448 "string is not null terminated correctly"); 449 450 for (i = 0; i < pad; i++) { 451 index = chars + terminator + i; 452 KUNIT_ASSERT_EQ_MSG(test, buf[index], '\0', 453 "padding missing at index: %d", i); 454 } 455 456 nr_bytes_poison = sizeof(buf) - chars - terminator - pad; 457 for (i = 0; i < nr_bytes_poison; i++) { 458 index = sizeof(buf) - 1 - i; /* Check from the end back */ 459 KUNIT_ASSERT_EQ_MSG(test, buf[index], POISON, 460 "poison value missing at index: %d", i); 461 } 462 } 463 464 static void string_test_strscpy(struct kunit *test) 465 { 466 char dest[8]; 467 468 /* 469 * strscpy_check() uses a destination buffer of size 6 and needs at 470 * least 2 characters spare (one for null and one to check for 471 * overflow). This means we should only call tc() with 472 * strings up to a maximum of 4 characters long and 'count' 473 * should not exceed 4. To test with longer strings increase 474 * the buffer size in tc(). 475 */ 476 477 /* strscpy_check(test, src, count, expected, chars, terminator, pad) */ 478 strscpy_check(test, "a", 0, -E2BIG, 0, 0, 0); 479 strscpy_check(test, "", 0, -E2BIG, 0, 0, 0); 480 481 strscpy_check(test, "a", 1, -E2BIG, 0, 1, 0); 482 strscpy_check(test, "", 1, 0, 0, 1, 0); 483 484 strscpy_check(test, "ab", 2, -E2BIG, 1, 1, 0); 485 strscpy_check(test, "a", 2, 1, 1, 1, 0); 486 strscpy_check(test, "", 2, 0, 0, 1, 1); 487 488 strscpy_check(test, "abc", 3, -E2BIG, 2, 1, 0); 489 strscpy_check(test, "ab", 3, 2, 2, 1, 0); 490 strscpy_check(test, "a", 3, 1, 1, 1, 1); 491 strscpy_check(test, "", 3, 0, 0, 1, 2); 492 493 strscpy_check(test, "abcd", 4, -E2BIG, 3, 1, 0); 494 strscpy_check(test, "abc", 4, 3, 3, 1, 0); 495 strscpy_check(test, "ab", 4, 2, 2, 1, 1); 496 strscpy_check(test, "a", 4, 1, 1, 1, 2); 497 strscpy_check(test, "", 4, 0, 0, 1, 3); 498 499 /* Compile-time-known source strings. */ 500 KUNIT_EXPECT_EQ(test, strscpy(dest, "", ARRAY_SIZE(dest)), 0); 501 KUNIT_EXPECT_EQ(test, strscpy(dest, "", 3), 0); 502 KUNIT_EXPECT_EQ(test, strscpy(dest, "", 1), 0); 503 KUNIT_EXPECT_EQ(test, strscpy(dest, "", 0), -E2BIG); 504 KUNIT_EXPECT_EQ(test, strscpy(dest, "Fixed", ARRAY_SIZE(dest)), 5); 505 KUNIT_EXPECT_EQ(test, strscpy(dest, "Fixed", 3), -E2BIG); 506 KUNIT_EXPECT_EQ(test, strscpy(dest, "Fixed", 1), -E2BIG); 507 KUNIT_EXPECT_EQ(test, strscpy(dest, "Fixed", 0), -E2BIG); 508 KUNIT_EXPECT_EQ(test, strscpy(dest, "This is too long", ARRAY_SIZE(dest)), -E2BIG); 509 } 510 511 static volatile int unconst; 512 513 static void string_test_strcat(struct kunit *test) 514 { 515 char dest[8]; 516 517 /* Destination is terminated. */ 518 memset(dest, 0, sizeof(dest)); 519 KUNIT_EXPECT_EQ(test, strlen(dest), 0); 520 /* Empty copy does nothing. */ 521 KUNIT_EXPECT_TRUE(test, strcat(dest, "") == dest); 522 KUNIT_EXPECT_STREQ(test, dest, ""); 523 /* 4 characters copied in, stops at %NUL. */ 524 KUNIT_EXPECT_TRUE(test, strcat(dest, "four\000123") == dest); 525 KUNIT_EXPECT_STREQ(test, dest, "four"); 526 KUNIT_EXPECT_EQ(test, dest[5], '\0'); 527 /* 2 more characters copied in okay. */ 528 KUNIT_EXPECT_TRUE(test, strcat(dest, "AB") == dest); 529 KUNIT_EXPECT_STREQ(test, dest, "fourAB"); 530 } 531 532 static void string_test_strncat(struct kunit *test) 533 { 534 char dest[8]; 535 536 /* Destination is terminated. */ 537 memset(dest, 0, sizeof(dest)); 538 KUNIT_EXPECT_EQ(test, strlen(dest), 0); 539 /* Empty copy of size 0 does nothing. */ 540 KUNIT_EXPECT_TRUE(test, strncat(dest, "", 0 + unconst) == dest); 541 KUNIT_EXPECT_STREQ(test, dest, ""); 542 /* Empty copy of size 1 does nothing too. */ 543 KUNIT_EXPECT_TRUE(test, strncat(dest, "", 1 + unconst) == dest); 544 KUNIT_EXPECT_STREQ(test, dest, ""); 545 /* Copy of max 0 characters should do nothing. */ 546 KUNIT_EXPECT_TRUE(test, strncat(dest, "asdf", 0 + unconst) == dest); 547 KUNIT_EXPECT_STREQ(test, dest, ""); 548 549 /* 4 characters copied in, even if max is 8. */ 550 KUNIT_EXPECT_TRUE(test, strncat(dest, "four\000123", 8 + unconst) == dest); 551 KUNIT_EXPECT_STREQ(test, dest, "four"); 552 KUNIT_EXPECT_EQ(test, dest[5], '\0'); 553 KUNIT_EXPECT_EQ(test, dest[6], '\0'); 554 /* 2 characters copied in okay, 2 ignored. */ 555 KUNIT_EXPECT_TRUE(test, strncat(dest, "ABCD", 2 + unconst) == dest); 556 KUNIT_EXPECT_STREQ(test, dest, "fourAB"); 557 } 558 559 static void string_test_strlcat(struct kunit *test) 560 { 561 char dest[8] = ""; 562 int len = sizeof(dest) + unconst; 563 564 /* Destination is terminated. */ 565 KUNIT_EXPECT_EQ(test, strlen(dest), 0); 566 /* Empty copy is size 0. */ 567 KUNIT_EXPECT_EQ(test, strlcat(dest, "", len), 0); 568 KUNIT_EXPECT_STREQ(test, dest, ""); 569 /* Size 1 should keep buffer terminated, report size of source only. */ 570 KUNIT_EXPECT_EQ(test, strlcat(dest, "four", 1 + unconst), 4); 571 KUNIT_EXPECT_STREQ(test, dest, ""); 572 573 /* 4 characters copied in. */ 574 KUNIT_EXPECT_EQ(test, strlcat(dest, "four", len), 4); 575 KUNIT_EXPECT_STREQ(test, dest, "four"); 576 /* 2 characters copied in okay, gets to 6 total. */ 577 KUNIT_EXPECT_EQ(test, strlcat(dest, "AB", len), 6); 578 KUNIT_EXPECT_STREQ(test, dest, "fourAB"); 579 /* 2 characters ignored if max size (7) reached. */ 580 KUNIT_EXPECT_EQ(test, strlcat(dest, "CD", 7 + unconst), 8); 581 KUNIT_EXPECT_STREQ(test, dest, "fourAB"); 582 /* 1 of 2 characters skipped, now at true max size. */ 583 KUNIT_EXPECT_EQ(test, strlcat(dest, "EFG", len), 9); 584 KUNIT_EXPECT_STREQ(test, dest, "fourABE"); 585 /* Everything else ignored, now at full size. */ 586 KUNIT_EXPECT_EQ(test, strlcat(dest, "1234", len), 11); 587 KUNIT_EXPECT_STREQ(test, dest, "fourABE"); 588 } 589 590 static void string_test_strtomem(struct kunit *test) 591 { 592 static const char input[sizeof(unsigned long)] = "hi"; 593 static const char truncate[] = "this is too long"; 594 struct { 595 unsigned long canary1; 596 unsigned char output[sizeof(unsigned long)] __nonstring; 597 unsigned long canary2; 598 } wrap; 599 600 memset(&wrap, 0xFF, sizeof(wrap)); 601 KUNIT_EXPECT_EQ_MSG(test, wrap.canary1, ULONG_MAX, 602 "bad initial canary value"); 603 KUNIT_EXPECT_EQ_MSG(test, wrap.canary2, ULONG_MAX, 604 "bad initial canary value"); 605 606 /* Check unpadded copy leaves surroundings untouched. */ 607 strtomem(wrap.output, input); 608 KUNIT_EXPECT_EQ(test, wrap.canary1, ULONG_MAX); 609 KUNIT_EXPECT_EQ(test, wrap.output[0], input[0]); 610 KUNIT_EXPECT_EQ(test, wrap.output[1], input[1]); 611 for (size_t i = 2; i < sizeof(wrap.output); i++) 612 KUNIT_EXPECT_EQ(test, wrap.output[i], 0xFF); 613 KUNIT_EXPECT_EQ(test, wrap.canary2, ULONG_MAX); 614 615 /* Check truncated copy leaves surroundings untouched. */ 616 memset(&wrap, 0xFF, sizeof(wrap)); 617 strtomem(wrap.output, truncate); 618 KUNIT_EXPECT_EQ(test, wrap.canary1, ULONG_MAX); 619 for (size_t i = 0; i < sizeof(wrap.output); i++) 620 KUNIT_EXPECT_EQ(test, wrap.output[i], truncate[i]); 621 KUNIT_EXPECT_EQ(test, wrap.canary2, ULONG_MAX); 622 623 /* Check padded copy leaves only string padded. */ 624 memset(&wrap, 0xFF, sizeof(wrap)); 625 strtomem_pad(wrap.output, input, 0xAA); 626 KUNIT_EXPECT_EQ(test, wrap.canary1, ULONG_MAX); 627 KUNIT_EXPECT_EQ(test, wrap.output[0], input[0]); 628 KUNIT_EXPECT_EQ(test, wrap.output[1], input[1]); 629 for (size_t i = 2; i < sizeof(wrap.output); i++) 630 KUNIT_EXPECT_EQ(test, wrap.output[i], 0xAA); 631 KUNIT_EXPECT_EQ(test, wrap.canary2, ULONG_MAX); 632 633 /* Check truncated padded copy has no padding. */ 634 memset(&wrap, 0xFF, sizeof(wrap)); 635 strtomem(wrap.output, truncate); 636 KUNIT_EXPECT_EQ(test, wrap.canary1, ULONG_MAX); 637 for (size_t i = 0; i < sizeof(wrap.output); i++) 638 KUNIT_EXPECT_EQ(test, wrap.output[i], truncate[i]); 639 KUNIT_EXPECT_EQ(test, wrap.canary2, ULONG_MAX); 640 } 641 642 643 static void string_test_memtostr(struct kunit *test) 644 { 645 char nonstring[7] __nonstring = { 'a', 'b', 'c', 'd', 'e', 'f', 'g' }; 646 char nonstring_small[3] __nonstring = { 'a', 'b', 'c' }; 647 char dest[sizeof(nonstring) + 1]; 648 649 /* Copy in a non-NUL-terminated string into exactly right-sized dest. */ 650 KUNIT_EXPECT_EQ(test, sizeof(dest), sizeof(nonstring) + 1); 651 memset(dest, 'X', sizeof(dest)); 652 memtostr(dest, nonstring); 653 KUNIT_EXPECT_STREQ(test, dest, "abcdefg"); 654 memset(dest, 'X', sizeof(dest)); 655 memtostr(dest, nonstring_small); 656 KUNIT_EXPECT_STREQ(test, dest, "abc"); 657 KUNIT_EXPECT_EQ(test, dest[7], 'X'); 658 659 memset(dest, 'X', sizeof(dest)); 660 memtostr_pad(dest, nonstring); 661 KUNIT_EXPECT_STREQ(test, dest, "abcdefg"); 662 memset(dest, 'X', sizeof(dest)); 663 memtostr_pad(dest, nonstring_small); 664 KUNIT_EXPECT_STREQ(test, dest, "abc"); 665 KUNIT_EXPECT_EQ(test, dest[7], '\0'); 666 } 667 668 static void string_test_strends(struct kunit *test) 669 { 670 KUNIT_EXPECT_TRUE(test, strends("foo-bar", "bar")); 671 KUNIT_EXPECT_TRUE(test, strends("foo-bar", "-bar")); 672 KUNIT_EXPECT_TRUE(test, strends("foobar", "foobar")); 673 KUNIT_EXPECT_TRUE(test, strends("foobar", "")); 674 KUNIT_EXPECT_FALSE(test, strends("bar", "foobar")); 675 KUNIT_EXPECT_FALSE(test, strends("", "foo")); 676 KUNIT_EXPECT_FALSE(test, strends("foobar", "ba")); 677 KUNIT_EXPECT_TRUE(test, strends("", "")); 678 } 679 680 static struct kunit_case string_test_cases[] = { 681 KUNIT_CASE(string_test_memset16), 682 KUNIT_CASE(string_test_memset32), 683 KUNIT_CASE(string_test_memset64), 684 KUNIT_CASE(string_test_strlen), 685 KUNIT_CASE(string_test_strnlen), 686 KUNIT_CASE(string_test_strchr), 687 KUNIT_CASE(string_test_strnchr), 688 KUNIT_CASE(string_test_strspn), 689 KUNIT_CASE(string_test_strcmp), 690 KUNIT_CASE(string_test_strcmp_long_strings), 691 KUNIT_CASE(string_test_strncmp), 692 KUNIT_CASE(string_test_strncmp_long_strings), 693 KUNIT_CASE(string_test_strcasecmp), 694 KUNIT_CASE(string_test_strcasecmp_long_strings), 695 KUNIT_CASE(string_test_strncasecmp), 696 KUNIT_CASE(string_test_strncasecmp_long_strings), 697 KUNIT_CASE(string_test_strscpy), 698 KUNIT_CASE(string_test_strcat), 699 KUNIT_CASE(string_test_strncat), 700 KUNIT_CASE(string_test_strlcat), 701 KUNIT_CASE(string_test_strtomem), 702 KUNIT_CASE(string_test_memtostr), 703 KUNIT_CASE(string_test_strends), 704 {} 705 }; 706 707 static struct kunit_suite string_test_suite = { 708 .name = "string", 709 .test_cases = string_test_cases, 710 }; 711 712 kunit_test_suites(&string_test_suite); 713 714 MODULE_DESCRIPTION("Test cases for string functions"); 715 MODULE_LICENSE("GPL v2"); 716