Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 
0003 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0004 
0005 #include <linux/string.h>
0006 
0007 #include "../tools/testing/selftests/kselftest_module.h"
0008 
0009 /*
0010  * Kernel module for testing 'strscpy' family of functions.
0011  */
0012 
0013 KSTM_MODULE_GLOBALS();
0014 
0015 /*
0016  * tc() - Run a specific test case.
0017  * @src: Source string, argument to strscpy_pad()
0018  * @count: Size of destination buffer, argument to strscpy_pad()
0019  * @expected: Expected return value from call to strscpy_pad()
0020  * @terminator: 1 if there should be a terminating null byte 0 otherwise.
0021  * @chars: Number of characters from the src string expected to be
0022  *         written to the dst buffer.
0023  * @pad: Number of pad characters expected (in the tail of dst buffer).
0024  *       (@pad does not include the null terminator byte.)
0025  *
0026  * Calls strscpy_pad() and verifies the return value and state of the
0027  * destination buffer after the call returns.
0028  */
0029 static int __init tc(char *src, int count, int expected,
0030              int chars, int terminator, int pad)
0031 {
0032     int nr_bytes_poison;
0033     int max_expected;
0034     int max_count;
0035     int written;
0036     char buf[6];
0037     int index, i;
0038     const char POISON = 'z';
0039 
0040     total_tests++;
0041 
0042     if (!src) {
0043         pr_err("null source string not supported\n");
0044         return -1;
0045     }
0046 
0047     memset(buf, POISON, sizeof(buf));
0048     /* Future proofing test suite, validate args */
0049     max_count = sizeof(buf) - 2; /* Space for null and to verify overflow */
0050     max_expected = count - 1;    /* Space for the null */
0051     if (count > max_count) {
0052         pr_err("count (%d) is too big (%d) ... aborting", count, max_count);
0053         return -1;
0054     }
0055     if (expected > max_expected) {
0056         pr_warn("expected (%d) is bigger than can possibly be returned (%d)",
0057             expected, max_expected);
0058     }
0059 
0060     written = strscpy_pad(buf, src, count);
0061     if ((written) != (expected)) {
0062         pr_err("%d != %d (written, expected)\n", written, expected);
0063         goto fail;
0064     }
0065 
0066     if (count && written == -E2BIG) {
0067         if (strncmp(buf, src, count - 1) != 0) {
0068             pr_err("buffer state invalid for -E2BIG\n");
0069             goto fail;
0070         }
0071         if (buf[count - 1] != '\0') {
0072             pr_err("too big string is not null terminated correctly\n");
0073             goto fail;
0074         }
0075     }
0076 
0077     for (i = 0; i < chars; i++) {
0078         if (buf[i] != src[i]) {
0079             pr_err("buf[i]==%c != src[i]==%c\n", buf[i], src[i]);
0080             goto fail;
0081         }
0082     }
0083 
0084     if (terminator) {
0085         if (buf[count - 1] != '\0') {
0086             pr_err("string is not null terminated correctly\n");
0087             goto fail;
0088         }
0089     }
0090 
0091     for (i = 0; i < pad; i++) {
0092         index = chars + terminator + i;
0093         if (buf[index] != '\0') {
0094             pr_err("padding missing at index: %d\n", i);
0095             goto fail;
0096         }
0097     }
0098 
0099     nr_bytes_poison = sizeof(buf) - chars - terminator - pad;
0100     for (i = 0; i < nr_bytes_poison; i++) {
0101         index = sizeof(buf) - 1 - i; /* Check from the end back */
0102         if (buf[index] != POISON) {
0103             pr_err("poison value missing at index: %d\n", i);
0104             goto fail;
0105         }
0106     }
0107 
0108     return 0;
0109 fail:
0110     failed_tests++;
0111     return -1;
0112 }
0113 
0114 static void __init selftest(void)
0115 {
0116     /*
0117      * tc() uses a destination buffer of size 6 and needs at
0118      * least 2 characters spare (one for null and one to check for
0119      * overflow).  This means we should only call tc() with
0120      * strings up to a maximum of 4 characters long and 'count'
0121      * should not exceed 4.  To test with longer strings increase
0122      * the buffer size in tc().
0123      */
0124 
0125     /* tc(src, count, expected, chars, terminator, pad) */
0126     KSTM_CHECK_ZERO(tc("a", 0, -E2BIG, 0, 0, 0));
0127     KSTM_CHECK_ZERO(tc("", 0, -E2BIG, 0, 0, 0));
0128 
0129     KSTM_CHECK_ZERO(tc("a", 1, -E2BIG, 0, 1, 0));
0130     KSTM_CHECK_ZERO(tc("", 1, 0, 0, 1, 0));
0131 
0132     KSTM_CHECK_ZERO(tc("ab", 2, -E2BIG, 1, 1, 0));
0133     KSTM_CHECK_ZERO(tc("a", 2, 1, 1, 1, 0));
0134     KSTM_CHECK_ZERO(tc("", 2, 0, 0, 1, 1));
0135 
0136     KSTM_CHECK_ZERO(tc("abc", 3, -E2BIG, 2, 1, 0));
0137     KSTM_CHECK_ZERO(tc("ab", 3, 2, 2, 1, 0));
0138     KSTM_CHECK_ZERO(tc("a", 3, 1, 1, 1, 1));
0139     KSTM_CHECK_ZERO(tc("", 3, 0, 0, 1, 2));
0140 
0141     KSTM_CHECK_ZERO(tc("abcd", 4, -E2BIG, 3, 1, 0));
0142     KSTM_CHECK_ZERO(tc("abc", 4, 3, 3, 1, 0));
0143     KSTM_CHECK_ZERO(tc("ab", 4, 2, 2, 1, 1));
0144     KSTM_CHECK_ZERO(tc("a", 4, 1, 1, 1, 2));
0145     KSTM_CHECK_ZERO(tc("", 4, 0, 0, 1, 3));
0146 }
0147 
0148 KSTM_MODULE_LOADERS(test_strscpy);
0149 MODULE_AUTHOR("Tobin C. Harding <tobin@kernel.org>");
0150 MODULE_LICENSE("GPL");