18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+ 28c2ecf20Sopenharmony_ci 38c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 48c2ecf20Sopenharmony_ci 58c2ecf20Sopenharmony_ci#include <linux/string.h> 68c2ecf20Sopenharmony_ci 78c2ecf20Sopenharmony_ci#include "../tools/testing/selftests/kselftest_module.h" 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ci/* 108c2ecf20Sopenharmony_ci * Kernel module for testing 'strscpy' family of functions. 118c2ecf20Sopenharmony_ci */ 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ciKSTM_MODULE_GLOBALS(); 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci/* 168c2ecf20Sopenharmony_ci * tc() - Run a specific test case. 178c2ecf20Sopenharmony_ci * @src: Source string, argument to strscpy_pad() 188c2ecf20Sopenharmony_ci * @count: Size of destination buffer, argument to strscpy_pad() 198c2ecf20Sopenharmony_ci * @expected: Expected return value from call to strscpy_pad() 208c2ecf20Sopenharmony_ci * @terminator: 1 if there should be a terminating null byte 0 otherwise. 218c2ecf20Sopenharmony_ci * @chars: Number of characters from the src string expected to be 228c2ecf20Sopenharmony_ci * written to the dst buffer. 238c2ecf20Sopenharmony_ci * @pad: Number of pad characters expected (in the tail of dst buffer). 248c2ecf20Sopenharmony_ci * (@pad does not include the null terminator byte.) 258c2ecf20Sopenharmony_ci * 268c2ecf20Sopenharmony_ci * Calls strscpy_pad() and verifies the return value and state of the 278c2ecf20Sopenharmony_ci * destination buffer after the call returns. 288c2ecf20Sopenharmony_ci */ 298c2ecf20Sopenharmony_cistatic int __init tc(char *src, int count, int expected, 308c2ecf20Sopenharmony_ci int chars, int terminator, int pad) 318c2ecf20Sopenharmony_ci{ 328c2ecf20Sopenharmony_ci int nr_bytes_poison; 338c2ecf20Sopenharmony_ci int max_expected; 348c2ecf20Sopenharmony_ci int max_count; 358c2ecf20Sopenharmony_ci int written; 368c2ecf20Sopenharmony_ci char buf[6]; 378c2ecf20Sopenharmony_ci int index, i; 388c2ecf20Sopenharmony_ci const char POISON = 'z'; 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ci total_tests++; 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci if (!src) { 438c2ecf20Sopenharmony_ci pr_err("null source string not supported\n"); 448c2ecf20Sopenharmony_ci return -1; 458c2ecf20Sopenharmony_ci } 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci memset(buf, POISON, sizeof(buf)); 488c2ecf20Sopenharmony_ci /* Future proofing test suite, validate args */ 498c2ecf20Sopenharmony_ci max_count = sizeof(buf) - 2; /* Space for null and to verify overflow */ 508c2ecf20Sopenharmony_ci max_expected = count - 1; /* Space for the null */ 518c2ecf20Sopenharmony_ci if (count > max_count) { 528c2ecf20Sopenharmony_ci pr_err("count (%d) is too big (%d) ... aborting", count, max_count); 538c2ecf20Sopenharmony_ci return -1; 548c2ecf20Sopenharmony_ci } 558c2ecf20Sopenharmony_ci if (expected > max_expected) { 568c2ecf20Sopenharmony_ci pr_warn("expected (%d) is bigger than can possibly be returned (%d)", 578c2ecf20Sopenharmony_ci expected, max_expected); 588c2ecf20Sopenharmony_ci } 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci written = strscpy_pad(buf, src, count); 618c2ecf20Sopenharmony_ci if ((written) != (expected)) { 628c2ecf20Sopenharmony_ci pr_err("%d != %d (written, expected)\n", written, expected); 638c2ecf20Sopenharmony_ci goto fail; 648c2ecf20Sopenharmony_ci } 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci if (count && written == -E2BIG) { 678c2ecf20Sopenharmony_ci if (strncmp(buf, src, count - 1) != 0) { 688c2ecf20Sopenharmony_ci pr_err("buffer state invalid for -E2BIG\n"); 698c2ecf20Sopenharmony_ci goto fail; 708c2ecf20Sopenharmony_ci } 718c2ecf20Sopenharmony_ci if (buf[count - 1] != '\0') { 728c2ecf20Sopenharmony_ci pr_err("too big string is not null terminated correctly\n"); 738c2ecf20Sopenharmony_ci goto fail; 748c2ecf20Sopenharmony_ci } 758c2ecf20Sopenharmony_ci } 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci for (i = 0; i < chars; i++) { 788c2ecf20Sopenharmony_ci if (buf[i] != src[i]) { 798c2ecf20Sopenharmony_ci pr_err("buf[i]==%c != src[i]==%c\n", buf[i], src[i]); 808c2ecf20Sopenharmony_ci goto fail; 818c2ecf20Sopenharmony_ci } 828c2ecf20Sopenharmony_ci } 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci if (terminator) { 858c2ecf20Sopenharmony_ci if (buf[count - 1] != '\0') { 868c2ecf20Sopenharmony_ci pr_err("string is not null terminated correctly\n"); 878c2ecf20Sopenharmony_ci goto fail; 888c2ecf20Sopenharmony_ci } 898c2ecf20Sopenharmony_ci } 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci for (i = 0; i < pad; i++) { 928c2ecf20Sopenharmony_ci index = chars + terminator + i; 938c2ecf20Sopenharmony_ci if (buf[index] != '\0') { 948c2ecf20Sopenharmony_ci pr_err("padding missing at index: %d\n", i); 958c2ecf20Sopenharmony_ci goto fail; 968c2ecf20Sopenharmony_ci } 978c2ecf20Sopenharmony_ci } 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci nr_bytes_poison = sizeof(buf) - chars - terminator - pad; 1008c2ecf20Sopenharmony_ci for (i = 0; i < nr_bytes_poison; i++) { 1018c2ecf20Sopenharmony_ci index = sizeof(buf) - 1 - i; /* Check from the end back */ 1028c2ecf20Sopenharmony_ci if (buf[index] != POISON) { 1038c2ecf20Sopenharmony_ci pr_err("poison value missing at index: %d\n", i); 1048c2ecf20Sopenharmony_ci goto fail; 1058c2ecf20Sopenharmony_ci } 1068c2ecf20Sopenharmony_ci } 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci return 0; 1098c2ecf20Sopenharmony_cifail: 1108c2ecf20Sopenharmony_ci failed_tests++; 1118c2ecf20Sopenharmony_ci return -1; 1128c2ecf20Sopenharmony_ci} 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_cistatic void __init selftest(void) 1158c2ecf20Sopenharmony_ci{ 1168c2ecf20Sopenharmony_ci /* 1178c2ecf20Sopenharmony_ci * tc() uses a destination buffer of size 6 and needs at 1188c2ecf20Sopenharmony_ci * least 2 characters spare (one for null and one to check for 1198c2ecf20Sopenharmony_ci * overflow). This means we should only call tc() with 1208c2ecf20Sopenharmony_ci * strings up to a maximum of 4 characters long and 'count' 1218c2ecf20Sopenharmony_ci * should not exceed 4. To test with longer strings increase 1228c2ecf20Sopenharmony_ci * the buffer size in tc(). 1238c2ecf20Sopenharmony_ci */ 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_ci /* tc(src, count, expected, chars, terminator, pad) */ 1268c2ecf20Sopenharmony_ci KSTM_CHECK_ZERO(tc("a", 0, -E2BIG, 0, 0, 0)); 1278c2ecf20Sopenharmony_ci KSTM_CHECK_ZERO(tc("", 0, -E2BIG, 0, 0, 0)); 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci KSTM_CHECK_ZERO(tc("a", 1, -E2BIG, 0, 1, 0)); 1308c2ecf20Sopenharmony_ci KSTM_CHECK_ZERO(tc("", 1, 0, 0, 1, 0)); 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci KSTM_CHECK_ZERO(tc("ab", 2, -E2BIG, 1, 1, 0)); 1338c2ecf20Sopenharmony_ci KSTM_CHECK_ZERO(tc("a", 2, 1, 1, 1, 0)); 1348c2ecf20Sopenharmony_ci KSTM_CHECK_ZERO(tc("", 2, 0, 0, 1, 1)); 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ci KSTM_CHECK_ZERO(tc("abc", 3, -E2BIG, 2, 1, 0)); 1378c2ecf20Sopenharmony_ci KSTM_CHECK_ZERO(tc("ab", 3, 2, 2, 1, 0)); 1388c2ecf20Sopenharmony_ci KSTM_CHECK_ZERO(tc("a", 3, 1, 1, 1, 1)); 1398c2ecf20Sopenharmony_ci KSTM_CHECK_ZERO(tc("", 3, 0, 0, 1, 2)); 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci KSTM_CHECK_ZERO(tc("abcd", 4, -E2BIG, 3, 1, 0)); 1428c2ecf20Sopenharmony_ci KSTM_CHECK_ZERO(tc("abc", 4, 3, 3, 1, 0)); 1438c2ecf20Sopenharmony_ci KSTM_CHECK_ZERO(tc("ab", 4, 2, 2, 1, 1)); 1448c2ecf20Sopenharmony_ci KSTM_CHECK_ZERO(tc("a", 4, 1, 1, 1, 2)); 1458c2ecf20Sopenharmony_ci KSTM_CHECK_ZERO(tc("", 4, 0, 0, 1, 3)); 1468c2ecf20Sopenharmony_ci} 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ciKSTM_MODULE_LOADERS(test_strscpy); 1498c2ecf20Sopenharmony_ciMODULE_AUTHOR("Tobin C. Harding <tobin@kernel.org>"); 1508c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 151