test_string.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/module.h>
  3. #include <linux/printk.h>
  4. #include <linux/slab.h>
  5. #include <linux/string.h>
  6. static __init int memset16_selftest(void)
  7. {
  8. unsigned i, j, k;
  9. u16 v, *p;
  10. p = kmalloc(256 * 2 * 2, GFP_KERNEL);
  11. if (!p)
  12. return -1;
  13. for (i = 0; i < 256; i++) {
  14. for (j = 0; j < 256; j++) {
  15. memset(p, 0xa1, 256 * 2 * sizeof(v));
  16. memset16(p + i, 0xb1b2, j);
  17. for (k = 0; k < 512; k++) {
  18. v = p[k];
  19. if (k < i) {
  20. if (v != 0xa1a1)
  21. goto fail;
  22. } else if (k < i + j) {
  23. if (v != 0xb1b2)
  24. goto fail;
  25. } else {
  26. if (v != 0xa1a1)
  27. goto fail;
  28. }
  29. }
  30. }
  31. }
  32. fail:
  33. kfree(p);
  34. if (i < 256)
  35. return (i << 24) | (j << 16) | k | 0x8000;
  36. return 0;
  37. }
  38. static __init int memset32_selftest(void)
  39. {
  40. unsigned i, j, k;
  41. u32 v, *p;
  42. p = kmalloc(256 * 2 * 4, GFP_KERNEL);
  43. if (!p)
  44. return -1;
  45. for (i = 0; i < 256; i++) {
  46. for (j = 0; j < 256; j++) {
  47. memset(p, 0xa1, 256 * 2 * sizeof(v));
  48. memset32(p + i, 0xb1b2b3b4, j);
  49. for (k = 0; k < 512; k++) {
  50. v = p[k];
  51. if (k < i) {
  52. if (v != 0xa1a1a1a1)
  53. goto fail;
  54. } else if (k < i + j) {
  55. if (v != 0xb1b2b3b4)
  56. goto fail;
  57. } else {
  58. if (v != 0xa1a1a1a1)
  59. goto fail;
  60. }
  61. }
  62. }
  63. }
  64. fail:
  65. kfree(p);
  66. if (i < 256)
  67. return (i << 24) | (j << 16) | k | 0x8000;
  68. return 0;
  69. }
  70. static __init int memset64_selftest(void)
  71. {
  72. unsigned i, j, k;
  73. u64 v, *p;
  74. p = kmalloc(256 * 2 * 8, GFP_KERNEL);
  75. if (!p)
  76. return -1;
  77. for (i = 0; i < 256; i++) {
  78. for (j = 0; j < 256; j++) {
  79. memset(p, 0xa1, 256 * 2 * sizeof(v));
  80. memset64(p + i, 0xb1b2b3b4b5b6b7b8ULL, j);
  81. for (k = 0; k < 512; k++) {
  82. v = p[k];
  83. if (k < i) {
  84. if (v != 0xa1a1a1a1a1a1a1a1ULL)
  85. goto fail;
  86. } else if (k < i + j) {
  87. if (v != 0xb1b2b3b4b5b6b7b8ULL)
  88. goto fail;
  89. } else {
  90. if (v != 0xa1a1a1a1a1a1a1a1ULL)
  91. goto fail;
  92. }
  93. }
  94. }
  95. }
  96. fail:
  97. kfree(p);
  98. if (i < 256)
  99. return (i << 24) | (j << 16) | k | 0x8000;
  100. return 0;
  101. }
  102. static __init int strchr_selftest(void)
  103. {
  104. const char *test_string = "abcdefghijkl";
  105. const char *empty_string = "";
  106. char *result;
  107. int i;
  108. for (i = 0; i < strlen(test_string) + 1; i++) {
  109. result = strchr(test_string, test_string[i]);
  110. if (result - test_string != i)
  111. return i + 'a';
  112. }
  113. result = strchr(empty_string, '\0');
  114. if (result != empty_string)
  115. return 0x101;
  116. result = strchr(empty_string, 'a');
  117. if (result)
  118. return 0x102;
  119. result = strchr(test_string, 'z');
  120. if (result)
  121. return 0x103;
  122. return 0;
  123. }
  124. static __init int strnchr_selftest(void)
  125. {
  126. const char *test_string = "abcdefghijkl";
  127. const char *empty_string = "";
  128. char *result;
  129. int i, j;
  130. for (i = 0; i < strlen(test_string) + 1; i++) {
  131. for (j = 0; j < strlen(test_string) + 2; j++) {
  132. result = strnchr(test_string, j, test_string[i]);
  133. if (j <= i) {
  134. if (!result)
  135. continue;
  136. return ((i + 'a') << 8) | j;
  137. }
  138. if (result - test_string != i)
  139. return ((i + 'a') << 8) | j;
  140. }
  141. }
  142. result = strnchr(empty_string, 0, '\0');
  143. if (result)
  144. return 0x10001;
  145. result = strnchr(empty_string, 1, '\0');
  146. if (result != empty_string)
  147. return 0x10002;
  148. result = strnchr(empty_string, 1, 'a');
  149. if (result)
  150. return 0x10003;
  151. result = strnchr(NULL, 0, '\0');
  152. if (result)
  153. return 0x10004;
  154. return 0;
  155. }
  156. static __init int string_selftest_init(void)
  157. {
  158. int test, subtest;
  159. test = 1;
  160. subtest = memset16_selftest();
  161. if (subtest)
  162. goto fail;
  163. test = 2;
  164. subtest = memset32_selftest();
  165. if (subtest)
  166. goto fail;
  167. test = 3;
  168. subtest = memset64_selftest();
  169. if (subtest)
  170. goto fail;
  171. test = 4;
  172. subtest = strchr_selftest();
  173. if (subtest)
  174. goto fail;
  175. test = 5;
  176. subtest = strnchr_selftest();
  177. if (subtest)
  178. goto fail;
  179. pr_info("String selftests succeeded\n");
  180. return 0;
  181. fail:
  182. pr_crit("String selftest failure %d.%08x\n", test, subtest);
  183. return 0;
  184. }
  185. module_init(string_selftest_init);
  186. MODULE_LICENSE("GPL v2");