xref: /OK3568_Linux_fs/kernel/lib/test_strscpy.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun 
3*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
4*4882a593Smuzhiyun 
5*4882a593Smuzhiyun #include <linux/string.h>
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #include "../tools/testing/selftests/kselftest_module.h"
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun /*
10*4882a593Smuzhiyun  * Kernel module for testing 'strscpy' family of functions.
11*4882a593Smuzhiyun  */
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun KSTM_MODULE_GLOBALS();
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun /*
16*4882a593Smuzhiyun  * tc() - Run a specific test case.
17*4882a593Smuzhiyun  * @src: Source string, argument to strscpy_pad()
18*4882a593Smuzhiyun  * @count: Size of destination buffer, argument to strscpy_pad()
19*4882a593Smuzhiyun  * @expected: Expected return value from call to strscpy_pad()
20*4882a593Smuzhiyun  * @terminator: 1 if there should be a terminating null byte 0 otherwise.
21*4882a593Smuzhiyun  * @chars: Number of characters from the src string expected to be
22*4882a593Smuzhiyun  *         written to the dst buffer.
23*4882a593Smuzhiyun  * @pad: Number of pad characters expected (in the tail of dst buffer).
24*4882a593Smuzhiyun  *       (@pad does not include the null terminator byte.)
25*4882a593Smuzhiyun  *
26*4882a593Smuzhiyun  * Calls strscpy_pad() and verifies the return value and state of the
27*4882a593Smuzhiyun  * destination buffer after the call returns.
28*4882a593Smuzhiyun  */
tc(char * src,int count,int expected,int chars,int terminator,int pad)29*4882a593Smuzhiyun static int __init tc(char *src, int count, int expected,
30*4882a593Smuzhiyun 		     int chars, int terminator, int pad)
31*4882a593Smuzhiyun {
32*4882a593Smuzhiyun 	int nr_bytes_poison;
33*4882a593Smuzhiyun 	int max_expected;
34*4882a593Smuzhiyun 	int max_count;
35*4882a593Smuzhiyun 	int written;
36*4882a593Smuzhiyun 	char buf[6];
37*4882a593Smuzhiyun 	int index, i;
38*4882a593Smuzhiyun 	const char POISON = 'z';
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun 	total_tests++;
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun 	if (!src) {
43*4882a593Smuzhiyun 		pr_err("null source string not supported\n");
44*4882a593Smuzhiyun 		return -1;
45*4882a593Smuzhiyun 	}
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun 	memset(buf, POISON, sizeof(buf));
48*4882a593Smuzhiyun 	/* Future proofing test suite, validate args */
49*4882a593Smuzhiyun 	max_count = sizeof(buf) - 2; /* Space for null and to verify overflow */
50*4882a593Smuzhiyun 	max_expected = count - 1;    /* Space for the null */
51*4882a593Smuzhiyun 	if (count > max_count) {
52*4882a593Smuzhiyun 		pr_err("count (%d) is too big (%d) ... aborting", count, max_count);
53*4882a593Smuzhiyun 		return -1;
54*4882a593Smuzhiyun 	}
55*4882a593Smuzhiyun 	if (expected > max_expected) {
56*4882a593Smuzhiyun 		pr_warn("expected (%d) is bigger than can possibly be returned (%d)",
57*4882a593Smuzhiyun 			expected, max_expected);
58*4882a593Smuzhiyun 	}
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 	written = strscpy_pad(buf, src, count);
61*4882a593Smuzhiyun 	if ((written) != (expected)) {
62*4882a593Smuzhiyun 		pr_err("%d != %d (written, expected)\n", written, expected);
63*4882a593Smuzhiyun 		goto fail;
64*4882a593Smuzhiyun 	}
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	if (count && written == -E2BIG) {
67*4882a593Smuzhiyun 		if (strncmp(buf, src, count - 1) != 0) {
68*4882a593Smuzhiyun 			pr_err("buffer state invalid for -E2BIG\n");
69*4882a593Smuzhiyun 			goto fail;
70*4882a593Smuzhiyun 		}
71*4882a593Smuzhiyun 		if (buf[count - 1] != '\0') {
72*4882a593Smuzhiyun 			pr_err("too big string is not null terminated correctly\n");
73*4882a593Smuzhiyun 			goto fail;
74*4882a593Smuzhiyun 		}
75*4882a593Smuzhiyun 	}
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun 	for (i = 0; i < chars; i++) {
78*4882a593Smuzhiyun 		if (buf[i] != src[i]) {
79*4882a593Smuzhiyun 			pr_err("buf[i]==%c != src[i]==%c\n", buf[i], src[i]);
80*4882a593Smuzhiyun 			goto fail;
81*4882a593Smuzhiyun 		}
82*4882a593Smuzhiyun 	}
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun 	if (terminator) {
85*4882a593Smuzhiyun 		if (buf[count - 1] != '\0') {
86*4882a593Smuzhiyun 			pr_err("string is not null terminated correctly\n");
87*4882a593Smuzhiyun 			goto fail;
88*4882a593Smuzhiyun 		}
89*4882a593Smuzhiyun 	}
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 	for (i = 0; i < pad; i++) {
92*4882a593Smuzhiyun 		index = chars + terminator + i;
93*4882a593Smuzhiyun 		if (buf[index] != '\0') {
94*4882a593Smuzhiyun 			pr_err("padding missing at index: %d\n", i);
95*4882a593Smuzhiyun 			goto fail;
96*4882a593Smuzhiyun 		}
97*4882a593Smuzhiyun 	}
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 	nr_bytes_poison = sizeof(buf) - chars - terminator - pad;
100*4882a593Smuzhiyun 	for (i = 0; i < nr_bytes_poison; i++) {
101*4882a593Smuzhiyun 		index = sizeof(buf) - 1 - i; /* Check from the end back */
102*4882a593Smuzhiyun 		if (buf[index] != POISON) {
103*4882a593Smuzhiyun 			pr_err("poison value missing at index: %d\n", i);
104*4882a593Smuzhiyun 			goto fail;
105*4882a593Smuzhiyun 		}
106*4882a593Smuzhiyun 	}
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 	return 0;
109*4882a593Smuzhiyun fail:
110*4882a593Smuzhiyun 	failed_tests++;
111*4882a593Smuzhiyun 	return -1;
112*4882a593Smuzhiyun }
113*4882a593Smuzhiyun 
selftest(void)114*4882a593Smuzhiyun static void __init selftest(void)
115*4882a593Smuzhiyun {
116*4882a593Smuzhiyun 	/*
117*4882a593Smuzhiyun 	 * tc() uses a destination buffer of size 6 and needs at
118*4882a593Smuzhiyun 	 * least 2 characters spare (one for null and one to check for
119*4882a593Smuzhiyun 	 * overflow).  This means we should only call tc() with
120*4882a593Smuzhiyun 	 * strings up to a maximum of 4 characters long and 'count'
121*4882a593Smuzhiyun 	 * should not exceed 4.  To test with longer strings increase
122*4882a593Smuzhiyun 	 * the buffer size in tc().
123*4882a593Smuzhiyun 	 */
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun 	/* tc(src, count, expected, chars, terminator, pad) */
126*4882a593Smuzhiyun 	KSTM_CHECK_ZERO(tc("a", 0, -E2BIG, 0, 0, 0));
127*4882a593Smuzhiyun 	KSTM_CHECK_ZERO(tc("", 0, -E2BIG, 0, 0, 0));
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun 	KSTM_CHECK_ZERO(tc("a", 1, -E2BIG, 0, 1, 0));
130*4882a593Smuzhiyun 	KSTM_CHECK_ZERO(tc("", 1, 0, 0, 1, 0));
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 	KSTM_CHECK_ZERO(tc("ab", 2, -E2BIG, 1, 1, 0));
133*4882a593Smuzhiyun 	KSTM_CHECK_ZERO(tc("a", 2, 1, 1, 1, 0));
134*4882a593Smuzhiyun 	KSTM_CHECK_ZERO(tc("", 2, 0, 0, 1, 1));
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 	KSTM_CHECK_ZERO(tc("abc", 3, -E2BIG, 2, 1, 0));
137*4882a593Smuzhiyun 	KSTM_CHECK_ZERO(tc("ab", 3, 2, 2, 1, 0));
138*4882a593Smuzhiyun 	KSTM_CHECK_ZERO(tc("a", 3, 1, 1, 1, 1));
139*4882a593Smuzhiyun 	KSTM_CHECK_ZERO(tc("", 3, 0, 0, 1, 2));
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 	KSTM_CHECK_ZERO(tc("abcd", 4, -E2BIG, 3, 1, 0));
142*4882a593Smuzhiyun 	KSTM_CHECK_ZERO(tc("abc", 4, 3, 3, 1, 0));
143*4882a593Smuzhiyun 	KSTM_CHECK_ZERO(tc("ab", 4, 2, 2, 1, 1));
144*4882a593Smuzhiyun 	KSTM_CHECK_ZERO(tc("a", 4, 1, 1, 1, 2));
145*4882a593Smuzhiyun 	KSTM_CHECK_ZERO(tc("", 4, 0, 0, 1, 3));
146*4882a593Smuzhiyun }
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun KSTM_MODULE_LOADERS(test_strscpy);
149*4882a593Smuzhiyun MODULE_AUTHOR("Tobin C. Harding <tobin@kernel.org>");
150*4882a593Smuzhiyun MODULE_LICENSE("GPL");
151