test_strscpy.c 4.0 KB

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