string_kunit.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Test cases for string functions.
  4. */
  5. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  6. #include <kunit/test.h>
  7. #include <linux/module.h>
  8. #include <linux/printk.h>
  9. #include <linux/slab.h>
  10. #include <linux/string.h>
  11. #define STRCMP_LARGE_BUF_LEN 2048
  12. #define STRCMP_CHANGE_POINT 1337
  13. #define STRCMP_TEST_EXPECT_EQUAL(test, fn, ...) KUNIT_EXPECT_EQ(test, fn(__VA_ARGS__), 0)
  14. #define STRCMP_TEST_EXPECT_LOWER(test, fn, ...) KUNIT_EXPECT_LT(test, fn(__VA_ARGS__), 0)
  15. #define STRCMP_TEST_EXPECT_GREATER(test, fn, ...) KUNIT_EXPECT_GT(test, fn(__VA_ARGS__), 0)
  16. static void string_test_memset16(struct kunit *test)
  17. {
  18. unsigned i, j, k;
  19. u16 v, *p;
  20. p = kunit_kzalloc(test, 256 * 2 * 2, GFP_KERNEL);
  21. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, p);
  22. for (i = 0; i < 256; i++) {
  23. for (j = 0; j < 256; j++) {
  24. memset(p, 0xa1, 256 * 2 * sizeof(v));
  25. memset16(p + i, 0xb1b2, j);
  26. for (k = 0; k < 512; k++) {
  27. v = p[k];
  28. if (k < i) {
  29. KUNIT_ASSERT_EQ_MSG(test, v, 0xa1a1,
  30. "i:%d j:%d k:%d", i, j, k);
  31. } else if (k < i + j) {
  32. KUNIT_ASSERT_EQ_MSG(test, v, 0xb1b2,
  33. "i:%d j:%d k:%d", i, j, k);
  34. } else {
  35. KUNIT_ASSERT_EQ_MSG(test, v, 0xa1a1,
  36. "i:%d j:%d k:%d", i, j, k);
  37. }
  38. }
  39. }
  40. }
  41. }
  42. static void string_test_memset32(struct kunit *test)
  43. {
  44. unsigned i, j, k;
  45. u32 v, *p;
  46. p = kunit_kzalloc(test, 256 * 2 * 4, GFP_KERNEL);
  47. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, p);
  48. for (i = 0; i < 256; i++) {
  49. for (j = 0; j < 256; j++) {
  50. memset(p, 0xa1, 256 * 2 * sizeof(v));
  51. memset32(p + i, 0xb1b2b3b4, j);
  52. for (k = 0; k < 512; k++) {
  53. v = p[k];
  54. if (k < i) {
  55. KUNIT_ASSERT_EQ_MSG(test, v, 0xa1a1a1a1,
  56. "i:%d j:%d k:%d", i, j, k);
  57. } else if (k < i + j) {
  58. KUNIT_ASSERT_EQ_MSG(test, v, 0xb1b2b3b4,
  59. "i:%d j:%d k:%d", i, j, k);
  60. } else {
  61. KUNIT_ASSERT_EQ_MSG(test, v, 0xa1a1a1a1,
  62. "i:%d j:%d k:%d", i, j, k);
  63. }
  64. }
  65. }
  66. }
  67. }
  68. static void string_test_memset64(struct kunit *test)
  69. {
  70. unsigned i, j, k;
  71. u64 v, *p;
  72. p = kunit_kzalloc(test, 256 * 2 * 8, GFP_KERNEL);
  73. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, p);
  74. for (i = 0; i < 256; i++) {
  75. for (j = 0; j < 256; j++) {
  76. memset(p, 0xa1, 256 * 2 * sizeof(v));
  77. memset64(p + i, 0xb1b2b3b4b5b6b7b8ULL, j);
  78. for (k = 0; k < 512; k++) {
  79. v = p[k];
  80. if (k < i) {
  81. KUNIT_ASSERT_EQ_MSG(test, v, 0xa1a1a1a1a1a1a1a1ULL,
  82. "i:%d j:%d k:%d", i, j, k);
  83. } else if (k < i + j) {
  84. KUNIT_ASSERT_EQ_MSG(test, v, 0xb1b2b3b4b5b6b7b8ULL,
  85. "i:%d j:%d k:%d", i, j, k);
  86. } else {
  87. KUNIT_ASSERT_EQ_MSG(test, v, 0xa1a1a1a1a1a1a1a1ULL,
  88. "i:%d j:%d k:%d", i, j, k);
  89. }
  90. }
  91. }
  92. }
  93. }
  94. static void string_test_strchr(struct kunit *test)
  95. {
  96. const char *test_string = "abcdefghijkl";
  97. const char *empty_string = "";
  98. char *result;
  99. int i;
  100. for (i = 0; i < strlen(test_string) + 1; i++) {
  101. result = strchr(test_string, test_string[i]);
  102. KUNIT_ASSERT_EQ_MSG(test, result - test_string, i,
  103. "char:%c", 'a' + i);
  104. }
  105. result = strchr(empty_string, '\0');
  106. KUNIT_ASSERT_PTR_EQ(test, result, empty_string);
  107. result = strchr(empty_string, 'a');
  108. KUNIT_ASSERT_NULL(test, result);
  109. result = strchr(test_string, 'z');
  110. KUNIT_ASSERT_NULL(test, result);
  111. }
  112. static void string_test_strnchr(struct kunit *test)
  113. {
  114. const char *test_string = "abcdefghijkl";
  115. const char *empty_string = "";
  116. char *result;
  117. int i, j;
  118. for (i = 0; i < strlen(test_string) + 1; i++) {
  119. for (j = 0; j < strlen(test_string) + 2; j++) {
  120. result = strnchr(test_string, j, test_string[i]);
  121. if (j <= i) {
  122. KUNIT_ASSERT_NULL_MSG(test, result,
  123. "char:%c i:%d j:%d", 'a' + i, i, j);
  124. } else {
  125. KUNIT_ASSERT_EQ_MSG(test, result - test_string, i,
  126. "char:%c i:%d j:%d", 'a' + i, i, j);
  127. }
  128. }
  129. }
  130. result = strnchr(empty_string, 0, '\0');
  131. KUNIT_ASSERT_NULL(test, result);
  132. result = strnchr(empty_string, 1, '\0');
  133. KUNIT_ASSERT_PTR_EQ(test, result, empty_string);
  134. result = strnchr(empty_string, 1, 'a');
  135. KUNIT_ASSERT_NULL(test, result);
  136. result = strnchr(NULL, 0, '\0');
  137. KUNIT_ASSERT_NULL(test, result);
  138. }
  139. static void string_test_strspn(struct kunit *test)
  140. {
  141. static const struct strspn_test {
  142. const char str[16];
  143. const char accept[16];
  144. const char reject[16];
  145. unsigned a;
  146. unsigned r;
  147. } tests[] = {
  148. { "foobar", "", "", 0, 6 },
  149. { "abba", "abc", "ABBA", 4, 4 },
  150. { "abba", "a", "b", 1, 1 },
  151. { "", "abc", "abc", 0, 0},
  152. };
  153. const struct strspn_test *s = tests;
  154. size_t i;
  155. for (i = 0; i < ARRAY_SIZE(tests); ++i, ++s) {
  156. KUNIT_ASSERT_EQ_MSG(test, s->a, strspn(s->str, s->accept),
  157. "i:%zu", i);
  158. KUNIT_ASSERT_EQ_MSG(test, s->r, strcspn(s->str, s->reject),
  159. "i:%zu", i);
  160. }
  161. }
  162. static char strcmp_buffer1[STRCMP_LARGE_BUF_LEN];
  163. static char strcmp_buffer2[STRCMP_LARGE_BUF_LEN];
  164. static void strcmp_fill_buffers(char fill1, char fill2)
  165. {
  166. memset(strcmp_buffer1, fill1, STRCMP_LARGE_BUF_LEN);
  167. memset(strcmp_buffer2, fill2, STRCMP_LARGE_BUF_LEN);
  168. strcmp_buffer1[STRCMP_LARGE_BUF_LEN - 1] = 0;
  169. strcmp_buffer2[STRCMP_LARGE_BUF_LEN - 1] = 0;
  170. }
  171. static void string_test_strcmp(struct kunit *test)
  172. {
  173. /* Equal strings */
  174. STRCMP_TEST_EXPECT_EQUAL(test, strcmp, "Hello, Kernel!", "Hello, Kernel!");
  175. /* First string is lexicographically less than the second */
  176. STRCMP_TEST_EXPECT_LOWER(test, strcmp, "Hello, KUnit!", "Hello, Kernel!");
  177. /* First string is lexicographically larger than the second */
  178. STRCMP_TEST_EXPECT_GREATER(test, strcmp, "Hello, Kernel!", "Hello, KUnit!");
  179. /* Empty string is always lexicographically less than any non-empty string */
  180. STRCMP_TEST_EXPECT_LOWER(test, strcmp, "", "Non-empty string");
  181. /* Two empty strings should be equal */
  182. STRCMP_TEST_EXPECT_EQUAL(test, strcmp, "", "");
  183. /* Compare two strings which have only one char difference */
  184. STRCMP_TEST_EXPECT_LOWER(test, strcmp, "Abacaba", "Abadaba");
  185. /* Compare two strings which have the same prefix*/
  186. STRCMP_TEST_EXPECT_LOWER(test, strcmp, "Just a string", "Just a string and something else");
  187. }
  188. static void string_test_strcmp_long_strings(struct kunit *test)
  189. {
  190. strcmp_fill_buffers('B', 'B');
  191. STRCMP_TEST_EXPECT_EQUAL(test, strcmp, strcmp_buffer1, strcmp_buffer2);
  192. strcmp_buffer1[STRCMP_CHANGE_POINT] = 'A';
  193. STRCMP_TEST_EXPECT_LOWER(test, strcmp, strcmp_buffer1, strcmp_buffer2);
  194. strcmp_buffer1[STRCMP_CHANGE_POINT] = 'C';
  195. STRCMP_TEST_EXPECT_GREATER(test, strcmp, strcmp_buffer1, strcmp_buffer2);
  196. }
  197. static void string_test_strncmp(struct kunit *test)
  198. {
  199. /* Equal strings */
  200. STRCMP_TEST_EXPECT_EQUAL(test, strncmp, "Hello, KUnit!", "Hello, KUnit!", 13);
  201. /* First string is lexicographically less than the second */
  202. STRCMP_TEST_EXPECT_LOWER(test, strncmp, "Hello, KUnit!", "Hello, Kernel!", 13);
  203. /* Result is always 'equal' when count = 0 */
  204. STRCMP_TEST_EXPECT_EQUAL(test, strncmp, "Hello, Kernel!", "Hello, KUnit!", 0);
  205. /* Strings with common prefix are equal if count = length of prefix */
  206. STRCMP_TEST_EXPECT_EQUAL(test, strncmp, "Abacaba", "Abadaba", 3);
  207. /* Strings with common prefix are not equal when count = length of prefix + 1 */
  208. STRCMP_TEST_EXPECT_LOWER(test, strncmp, "Abacaba", "Abadaba", 4);
  209. /* If one string is a prefix of another, the shorter string is lexicographically smaller */
  210. STRCMP_TEST_EXPECT_LOWER(test, strncmp, "Just a string", "Just a string and something else",
  211. strlen("Just a string and something else"));
  212. /*
  213. * If one string is a prefix of another, and we check first length
  214. * of prefix chars, the result is 'equal'
  215. */
  216. STRCMP_TEST_EXPECT_EQUAL(test, strncmp, "Just a string", "Just a string and something else",
  217. strlen("Just a string"));
  218. }
  219. static void string_test_strncmp_long_strings(struct kunit *test)
  220. {
  221. strcmp_fill_buffers('B', 'B');
  222. STRCMP_TEST_EXPECT_EQUAL(test, strncmp, strcmp_buffer1,
  223. strcmp_buffer2, STRCMP_LARGE_BUF_LEN);
  224. strcmp_buffer1[STRCMP_CHANGE_POINT] = 'A';
  225. STRCMP_TEST_EXPECT_LOWER(test, strncmp, strcmp_buffer1,
  226. strcmp_buffer2, STRCMP_LARGE_BUF_LEN);
  227. strcmp_buffer1[STRCMP_CHANGE_POINT] = 'C';
  228. STRCMP_TEST_EXPECT_GREATER(test, strncmp, strcmp_buffer1,
  229. strcmp_buffer2, STRCMP_LARGE_BUF_LEN);
  230. /* the strings are equal up to STRCMP_CHANGE_POINT */
  231. STRCMP_TEST_EXPECT_EQUAL(test, strncmp, strcmp_buffer1,
  232. strcmp_buffer2, STRCMP_CHANGE_POINT);
  233. STRCMP_TEST_EXPECT_GREATER(test, strncmp, strcmp_buffer1,
  234. strcmp_buffer2, STRCMP_CHANGE_POINT + 1);
  235. }
  236. static void string_test_strcasecmp(struct kunit *test)
  237. {
  238. /* Same strings in different case should be equal */
  239. STRCMP_TEST_EXPECT_EQUAL(test, strcasecmp, "Hello, Kernel!", "HeLLO, KErNeL!");
  240. /* Empty strings should be equal */
  241. STRCMP_TEST_EXPECT_EQUAL(test, strcasecmp, "", "");
  242. /* Despite ascii code for 'a' is larger than ascii code for 'B', 'a' < 'B' */
  243. STRCMP_TEST_EXPECT_LOWER(test, strcasecmp, "a", "B");
  244. STRCMP_TEST_EXPECT_GREATER(test, strcasecmp, "B", "a");
  245. /* Special symbols and numbers should be processed correctly */
  246. STRCMP_TEST_EXPECT_EQUAL(test, strcasecmp, "-+**.1230ghTTT~^", "-+**.1230Ghttt~^");
  247. }
  248. static void string_test_strcasecmp_long_strings(struct kunit *test)
  249. {
  250. strcmp_fill_buffers('b', 'B');
  251. STRCMP_TEST_EXPECT_EQUAL(test, strcasecmp, strcmp_buffer1, strcmp_buffer2);
  252. strcmp_buffer1[STRCMP_CHANGE_POINT] = 'a';
  253. STRCMP_TEST_EXPECT_LOWER(test, strcasecmp, strcmp_buffer1, strcmp_buffer2);
  254. strcmp_buffer1[STRCMP_CHANGE_POINT] = 'C';
  255. STRCMP_TEST_EXPECT_GREATER(test, strcasecmp, strcmp_buffer1, strcmp_buffer2);
  256. }
  257. static void string_test_strncasecmp(struct kunit *test)
  258. {
  259. /* Same strings in different case should be equal */
  260. STRCMP_TEST_EXPECT_EQUAL(test, strncasecmp, "AbAcAbA", "Abacaba", strlen("Abacaba"));
  261. /* strncasecmp should check 'count' chars only */
  262. STRCMP_TEST_EXPECT_EQUAL(test, strncasecmp, "AbaCaBa", "abaCaDa", 5);
  263. STRCMP_TEST_EXPECT_LOWER(test, strncasecmp, "a", "B", 1);
  264. STRCMP_TEST_EXPECT_GREATER(test, strncasecmp, "B", "a", 1);
  265. /* Result is always 'equal' when count = 0 */
  266. STRCMP_TEST_EXPECT_EQUAL(test, strncasecmp, "Abacaba", "Not abacaba", 0);
  267. }
  268. static void string_test_strncasecmp_long_strings(struct kunit *test)
  269. {
  270. strcmp_fill_buffers('b', 'B');
  271. STRCMP_TEST_EXPECT_EQUAL(test, strncasecmp, strcmp_buffer1,
  272. strcmp_buffer2, STRCMP_LARGE_BUF_LEN);
  273. strcmp_buffer1[STRCMP_CHANGE_POINT] = 'a';
  274. STRCMP_TEST_EXPECT_LOWER(test, strncasecmp, strcmp_buffer1,
  275. strcmp_buffer2, STRCMP_LARGE_BUF_LEN);
  276. strcmp_buffer1[STRCMP_CHANGE_POINT] = 'C';
  277. STRCMP_TEST_EXPECT_GREATER(test, strncasecmp, strcmp_buffer1,
  278. strcmp_buffer2, STRCMP_LARGE_BUF_LEN);
  279. STRCMP_TEST_EXPECT_EQUAL(test, strncasecmp, strcmp_buffer1,
  280. strcmp_buffer2, STRCMP_CHANGE_POINT);
  281. STRCMP_TEST_EXPECT_GREATER(test, strncasecmp, strcmp_buffer1,
  282. strcmp_buffer2, STRCMP_CHANGE_POINT + 1);
  283. }
  284. /**
  285. * strscpy_check() - Run a specific test case.
  286. * @test: KUnit test context pointer
  287. * @src: Source string, argument to strscpy_pad()
  288. * @count: Size of destination buffer, argument to strscpy_pad()
  289. * @expected: Expected return value from call to strscpy_pad()
  290. * @chars: Number of characters from the src string expected to be
  291. * written to the dst buffer.
  292. * @terminator: 1 if there should be a terminating null byte 0 otherwise.
  293. * @pad: Number of pad characters expected (in the tail of dst buffer).
  294. * (@pad does not include the null terminator byte.)
  295. *
  296. * Calls strscpy_pad() and verifies the return value and state of the
  297. * destination buffer after the call returns.
  298. */
  299. static void strscpy_check(struct kunit *test, char *src, int count,
  300. int expected, int chars, int terminator, int pad)
  301. {
  302. int nr_bytes_poison;
  303. int max_expected;
  304. int max_count;
  305. int written;
  306. char buf[6];
  307. int index, i;
  308. const char POISON = 'z';
  309. KUNIT_ASSERT_TRUE_MSG(test, src != NULL,
  310. "null source string not supported");
  311. memset(buf, POISON, sizeof(buf));
  312. /* Future proofing test suite, validate args */
  313. max_count = sizeof(buf) - 2; /* Space for null and to verify overflow */
  314. max_expected = count - 1; /* Space for the null */
  315. KUNIT_ASSERT_LE_MSG(test, count, max_count,
  316. "count (%d) is too big (%d) ... aborting", count, max_count);
  317. KUNIT_EXPECT_LE_MSG(test, expected, max_expected,
  318. "expected (%d) is bigger than can possibly be returned (%d)",
  319. expected, max_expected);
  320. written = strscpy_pad(buf, src, count);
  321. KUNIT_ASSERT_EQ(test, written, expected);
  322. if (count && written == -E2BIG) {
  323. KUNIT_ASSERT_EQ_MSG(test, 0, strncmp(buf, src, count - 1),
  324. "buffer state invalid for -E2BIG");
  325. KUNIT_ASSERT_EQ_MSG(test, buf[count - 1], '\0',
  326. "too big string is not null terminated correctly");
  327. }
  328. for (i = 0; i < chars; i++)
  329. KUNIT_ASSERT_EQ_MSG(test, buf[i], src[i],
  330. "buf[i]==%c != src[i]==%c", buf[i], src[i]);
  331. if (terminator)
  332. KUNIT_ASSERT_EQ_MSG(test, buf[count - 1], '\0',
  333. "string is not null terminated correctly");
  334. for (i = 0; i < pad; i++) {
  335. index = chars + terminator + i;
  336. KUNIT_ASSERT_EQ_MSG(test, buf[index], '\0',
  337. "padding missing at index: %d", i);
  338. }
  339. nr_bytes_poison = sizeof(buf) - chars - terminator - pad;
  340. for (i = 0; i < nr_bytes_poison; i++) {
  341. index = sizeof(buf) - 1 - i; /* Check from the end back */
  342. KUNIT_ASSERT_EQ_MSG(test, buf[index], POISON,
  343. "poison value missing at index: %d", i);
  344. }
  345. }
  346. static void string_test_strscpy(struct kunit *test)
  347. {
  348. char dest[8];
  349. /*
  350. * strscpy_check() uses a destination buffer of size 6 and needs at
  351. * least 2 characters spare (one for null and one to check for
  352. * overflow). This means we should only call tc() with
  353. * strings up to a maximum of 4 characters long and 'count'
  354. * should not exceed 4. To test with longer strings increase
  355. * the buffer size in tc().
  356. */
  357. /* strscpy_check(test, src, count, expected, chars, terminator, pad) */
  358. strscpy_check(test, "a", 0, -E2BIG, 0, 0, 0);
  359. strscpy_check(test, "", 0, -E2BIG, 0, 0, 0);
  360. strscpy_check(test, "a", 1, -E2BIG, 0, 1, 0);
  361. strscpy_check(test, "", 1, 0, 0, 1, 0);
  362. strscpy_check(test, "ab", 2, -E2BIG, 1, 1, 0);
  363. strscpy_check(test, "a", 2, 1, 1, 1, 0);
  364. strscpy_check(test, "", 2, 0, 0, 1, 1);
  365. strscpy_check(test, "abc", 3, -E2BIG, 2, 1, 0);
  366. strscpy_check(test, "ab", 3, 2, 2, 1, 0);
  367. strscpy_check(test, "a", 3, 1, 1, 1, 1);
  368. strscpy_check(test, "", 3, 0, 0, 1, 2);
  369. strscpy_check(test, "abcd", 4, -E2BIG, 3, 1, 0);
  370. strscpy_check(test, "abc", 4, 3, 3, 1, 0);
  371. strscpy_check(test, "ab", 4, 2, 2, 1, 1);
  372. strscpy_check(test, "a", 4, 1, 1, 1, 2);
  373. strscpy_check(test, "", 4, 0, 0, 1, 3);
  374. /* Compile-time-known source strings. */
  375. KUNIT_EXPECT_EQ(test, strscpy(dest, "", ARRAY_SIZE(dest)), 0);
  376. KUNIT_EXPECT_EQ(test, strscpy(dest, "", 3), 0);
  377. KUNIT_EXPECT_EQ(test, strscpy(dest, "", 1), 0);
  378. KUNIT_EXPECT_EQ(test, strscpy(dest, "", 0), -E2BIG);
  379. KUNIT_EXPECT_EQ(test, strscpy(dest, "Fixed", ARRAY_SIZE(dest)), 5);
  380. KUNIT_EXPECT_EQ(test, strscpy(dest, "Fixed", 3), -E2BIG);
  381. KUNIT_EXPECT_EQ(test, strscpy(dest, "Fixed", 1), -E2BIG);
  382. KUNIT_EXPECT_EQ(test, strscpy(dest, "Fixed", 0), -E2BIG);
  383. KUNIT_EXPECT_EQ(test, strscpy(dest, "This is too long", ARRAY_SIZE(dest)), -E2BIG);
  384. }
  385. static volatile int unconst;
  386. static void string_test_strcat(struct kunit *test)
  387. {
  388. char dest[8];
  389. /* Destination is terminated. */
  390. memset(dest, 0, sizeof(dest));
  391. KUNIT_EXPECT_EQ(test, strlen(dest), 0);
  392. /* Empty copy does nothing. */
  393. KUNIT_EXPECT_TRUE(test, strcat(dest, "") == dest);
  394. KUNIT_EXPECT_STREQ(test, dest, "");
  395. /* 4 characters copied in, stops at %NUL. */
  396. KUNIT_EXPECT_TRUE(test, strcat(dest, "four\000123") == dest);
  397. KUNIT_EXPECT_STREQ(test, dest, "four");
  398. KUNIT_EXPECT_EQ(test, dest[5], '\0');
  399. /* 2 more characters copied in okay. */
  400. KUNIT_EXPECT_TRUE(test, strcat(dest, "AB") == dest);
  401. KUNIT_EXPECT_STREQ(test, dest, "fourAB");
  402. }
  403. static void string_test_strncat(struct kunit *test)
  404. {
  405. char dest[8];
  406. /* Destination is terminated. */
  407. memset(dest, 0, sizeof(dest));
  408. KUNIT_EXPECT_EQ(test, strlen(dest), 0);
  409. /* Empty copy of size 0 does nothing. */
  410. KUNIT_EXPECT_TRUE(test, strncat(dest, "", 0 + unconst) == dest);
  411. KUNIT_EXPECT_STREQ(test, dest, "");
  412. /* Empty copy of size 1 does nothing too. */
  413. KUNIT_EXPECT_TRUE(test, strncat(dest, "", 1 + unconst) == dest);
  414. KUNIT_EXPECT_STREQ(test, dest, "");
  415. /* Copy of max 0 characters should do nothing. */
  416. KUNIT_EXPECT_TRUE(test, strncat(dest, "asdf", 0 + unconst) == dest);
  417. KUNIT_EXPECT_STREQ(test, dest, "");
  418. /* 4 characters copied in, even if max is 8. */
  419. KUNIT_EXPECT_TRUE(test, strncat(dest, "four\000123", 8 + unconst) == dest);
  420. KUNIT_EXPECT_STREQ(test, dest, "four");
  421. KUNIT_EXPECT_EQ(test, dest[5], '\0');
  422. KUNIT_EXPECT_EQ(test, dest[6], '\0');
  423. /* 2 characters copied in okay, 2 ignored. */
  424. KUNIT_EXPECT_TRUE(test, strncat(dest, "ABCD", 2 + unconst) == dest);
  425. KUNIT_EXPECT_STREQ(test, dest, "fourAB");
  426. }
  427. static void string_test_strlcat(struct kunit *test)
  428. {
  429. char dest[8] = "";
  430. int len = sizeof(dest) + unconst;
  431. /* Destination is terminated. */
  432. KUNIT_EXPECT_EQ(test, strlen(dest), 0);
  433. /* Empty copy is size 0. */
  434. KUNIT_EXPECT_EQ(test, strlcat(dest, "", len), 0);
  435. KUNIT_EXPECT_STREQ(test, dest, "");
  436. /* Size 1 should keep buffer terminated, report size of source only. */
  437. KUNIT_EXPECT_EQ(test, strlcat(dest, "four", 1 + unconst), 4);
  438. KUNIT_EXPECT_STREQ(test, dest, "");
  439. /* 4 characters copied in. */
  440. KUNIT_EXPECT_EQ(test, strlcat(dest, "four", len), 4);
  441. KUNIT_EXPECT_STREQ(test, dest, "four");
  442. /* 2 characters copied in okay, gets to 6 total. */
  443. KUNIT_EXPECT_EQ(test, strlcat(dest, "AB", len), 6);
  444. KUNIT_EXPECT_STREQ(test, dest, "fourAB");
  445. /* 2 characters ignored if max size (7) reached. */
  446. KUNIT_EXPECT_EQ(test, strlcat(dest, "CD", 7 + unconst), 8);
  447. KUNIT_EXPECT_STREQ(test, dest, "fourAB");
  448. /* 1 of 2 characters skipped, now at true max size. */
  449. KUNIT_EXPECT_EQ(test, strlcat(dest, "EFG", len), 9);
  450. KUNIT_EXPECT_STREQ(test, dest, "fourABE");
  451. /* Everything else ignored, now at full size. */
  452. KUNIT_EXPECT_EQ(test, strlcat(dest, "1234", len), 11);
  453. KUNIT_EXPECT_STREQ(test, dest, "fourABE");
  454. }
  455. static void string_test_strtomem(struct kunit *test)
  456. {
  457. static const char input[sizeof(unsigned long)] = "hi";
  458. static const char truncate[] = "this is too long";
  459. struct {
  460. unsigned long canary1;
  461. unsigned char output[sizeof(unsigned long)] __nonstring;
  462. unsigned long canary2;
  463. } wrap;
  464. memset(&wrap, 0xFF, sizeof(wrap));
  465. KUNIT_EXPECT_EQ_MSG(test, wrap.canary1, ULONG_MAX,
  466. "bad initial canary value");
  467. KUNIT_EXPECT_EQ_MSG(test, wrap.canary2, ULONG_MAX,
  468. "bad initial canary value");
  469. /* Check unpadded copy leaves surroundings untouched. */
  470. strtomem(wrap.output, input);
  471. KUNIT_EXPECT_EQ(test, wrap.canary1, ULONG_MAX);
  472. KUNIT_EXPECT_EQ(test, wrap.output[0], input[0]);
  473. KUNIT_EXPECT_EQ(test, wrap.output[1], input[1]);
  474. for (size_t i = 2; i < sizeof(wrap.output); i++)
  475. KUNIT_EXPECT_EQ(test, wrap.output[i], 0xFF);
  476. KUNIT_EXPECT_EQ(test, wrap.canary2, ULONG_MAX);
  477. /* Check truncated copy leaves surroundings untouched. */
  478. memset(&wrap, 0xFF, sizeof(wrap));
  479. strtomem(wrap.output, truncate);
  480. KUNIT_EXPECT_EQ(test, wrap.canary1, ULONG_MAX);
  481. for (size_t i = 0; i < sizeof(wrap.output); i++)
  482. KUNIT_EXPECT_EQ(test, wrap.output[i], truncate[i]);
  483. KUNIT_EXPECT_EQ(test, wrap.canary2, ULONG_MAX);
  484. /* Check padded copy leaves only string padded. */
  485. memset(&wrap, 0xFF, sizeof(wrap));
  486. strtomem_pad(wrap.output, input, 0xAA);
  487. KUNIT_EXPECT_EQ(test, wrap.canary1, ULONG_MAX);
  488. KUNIT_EXPECT_EQ(test, wrap.output[0], input[0]);
  489. KUNIT_EXPECT_EQ(test, wrap.output[1], input[1]);
  490. for (size_t i = 2; i < sizeof(wrap.output); i++)
  491. KUNIT_EXPECT_EQ(test, wrap.output[i], 0xAA);
  492. KUNIT_EXPECT_EQ(test, wrap.canary2, ULONG_MAX);
  493. /* Check truncated padded copy has no padding. */
  494. memset(&wrap, 0xFF, sizeof(wrap));
  495. strtomem(wrap.output, truncate);
  496. KUNIT_EXPECT_EQ(test, wrap.canary1, ULONG_MAX);
  497. for (size_t i = 0; i < sizeof(wrap.output); i++)
  498. KUNIT_EXPECT_EQ(test, wrap.output[i], truncate[i]);
  499. KUNIT_EXPECT_EQ(test, wrap.canary2, ULONG_MAX);
  500. }
  501. static void string_test_memtostr(struct kunit *test)
  502. {
  503. char nonstring[7] __nonstring = { 'a', 'b', 'c', 'd', 'e', 'f', 'g' };
  504. char nonstring_small[3] __nonstring = { 'a', 'b', 'c' };
  505. char dest[sizeof(nonstring) + 1];
  506. /* Copy in a non-NUL-terminated string into exactly right-sized dest. */
  507. KUNIT_EXPECT_EQ(test, sizeof(dest), sizeof(nonstring) + 1);
  508. memset(dest, 'X', sizeof(dest));
  509. memtostr(dest, nonstring);
  510. KUNIT_EXPECT_STREQ(test, dest, "abcdefg");
  511. memset(dest, 'X', sizeof(dest));
  512. memtostr(dest, nonstring_small);
  513. KUNIT_EXPECT_STREQ(test, dest, "abc");
  514. KUNIT_EXPECT_EQ(test, dest[7], 'X');
  515. memset(dest, 'X', sizeof(dest));
  516. memtostr_pad(dest, nonstring);
  517. KUNIT_EXPECT_STREQ(test, dest, "abcdefg");
  518. memset(dest, 'X', sizeof(dest));
  519. memtostr_pad(dest, nonstring_small);
  520. KUNIT_EXPECT_STREQ(test, dest, "abc");
  521. KUNIT_EXPECT_EQ(test, dest[7], '\0');
  522. }
  523. static void string_test_strends(struct kunit *test)
  524. {
  525. KUNIT_EXPECT_TRUE(test, strends("foo-bar", "bar"));
  526. KUNIT_EXPECT_TRUE(test, strends("foo-bar", "-bar"));
  527. KUNIT_EXPECT_TRUE(test, strends("foobar", "foobar"));
  528. KUNIT_EXPECT_TRUE(test, strends("foobar", ""));
  529. KUNIT_EXPECT_FALSE(test, strends("bar", "foobar"));
  530. KUNIT_EXPECT_FALSE(test, strends("", "foo"));
  531. KUNIT_EXPECT_FALSE(test, strends("foobar", "ba"));
  532. KUNIT_EXPECT_TRUE(test, strends("", ""));
  533. }
  534. static struct kunit_case string_test_cases[] = {
  535. KUNIT_CASE(string_test_memset16),
  536. KUNIT_CASE(string_test_memset32),
  537. KUNIT_CASE(string_test_memset64),
  538. KUNIT_CASE(string_test_strchr),
  539. KUNIT_CASE(string_test_strnchr),
  540. KUNIT_CASE(string_test_strspn),
  541. KUNIT_CASE(string_test_strcmp),
  542. KUNIT_CASE(string_test_strcmp_long_strings),
  543. KUNIT_CASE(string_test_strncmp),
  544. KUNIT_CASE(string_test_strncmp_long_strings),
  545. KUNIT_CASE(string_test_strcasecmp),
  546. KUNIT_CASE(string_test_strcasecmp_long_strings),
  547. KUNIT_CASE(string_test_strncasecmp),
  548. KUNIT_CASE(string_test_strncasecmp_long_strings),
  549. KUNIT_CASE(string_test_strscpy),
  550. KUNIT_CASE(string_test_strcat),
  551. KUNIT_CASE(string_test_strncat),
  552. KUNIT_CASE(string_test_strlcat),
  553. KUNIT_CASE(string_test_strtomem),
  554. KUNIT_CASE(string_test_memtostr),
  555. KUNIT_CASE(string_test_strends),
  556. {}
  557. };
  558. static struct kunit_suite string_test_suite = {
  559. .name = "string",
  560. .test_cases = string_test_cases,
  561. };
  562. kunit_test_suites(&string_test_suite);
  563. MODULE_DESCRIPTION("Test cases for string functions");
  564. MODULE_LICENSE("GPL v2");