string-stream-test.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * KUnit test for struct string_stream.
  4. *
  5. * Copyright (C) 2019, Google LLC.
  6. * Author: Brendan Higgins <brendanhiggins@google.com>
  7. */
  8. #include <kunit/static_stub.h>
  9. #include <kunit/test.h>
  10. #include <linux/ktime.h>
  11. #include <linux/prandom.h>
  12. #include <linux/slab.h>
  13. #include <linux/timekeeping.h>
  14. #include "string-stream.h"
  15. struct string_stream_test_priv {
  16. /* For testing resource-managed free. */
  17. struct string_stream *expected_free_stream;
  18. bool stream_was_freed;
  19. bool stream_free_again;
  20. };
  21. /* Avoids a cast warning if kfree() is passed direct to kunit_add_action(). */
  22. KUNIT_DEFINE_ACTION_WRAPPER(kfree_wrapper, kfree, const void *);
  23. /* Avoids a cast warning if string_stream_destroy() is passed direct to kunit_add_action(). */
  24. KUNIT_DEFINE_ACTION_WRAPPER(cleanup_raw_stream, string_stream_destroy, struct string_stream *);
  25. static char *get_concatenated_string(struct kunit *test, struct string_stream *stream)
  26. {
  27. char *str = string_stream_get_string(stream);
  28. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, str);
  29. kunit_add_action(test, kfree_wrapper, (void *)str);
  30. return str;
  31. }
  32. /* Managed string_stream object is initialized correctly. */
  33. static void string_stream_managed_init_test(struct kunit *test)
  34. {
  35. struct string_stream *stream;
  36. /* Resource-managed initialization. */
  37. stream = kunit_alloc_string_stream(test, GFP_KERNEL);
  38. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream);
  39. KUNIT_EXPECT_EQ(test, stream->length, 0);
  40. KUNIT_EXPECT_TRUE(test, list_empty(&stream->fragments));
  41. KUNIT_EXPECT_TRUE(test, (stream->gfp == GFP_KERNEL));
  42. KUNIT_EXPECT_FALSE(test, stream->append_newlines);
  43. KUNIT_EXPECT_TRUE(test, string_stream_is_empty(stream));
  44. }
  45. /* Unmanaged string_stream object is initialized correctly. */
  46. static void string_stream_unmanaged_init_test(struct kunit *test)
  47. {
  48. struct string_stream *stream;
  49. stream = alloc_string_stream(GFP_KERNEL);
  50. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream);
  51. kunit_add_action(test, cleanup_raw_stream, stream);
  52. KUNIT_EXPECT_EQ(test, stream->length, 0);
  53. KUNIT_EXPECT_TRUE(test, list_empty(&stream->fragments));
  54. KUNIT_EXPECT_TRUE(test, (stream->gfp == GFP_KERNEL));
  55. KUNIT_EXPECT_FALSE(test, stream->append_newlines);
  56. KUNIT_EXPECT_TRUE(test, string_stream_is_empty(stream));
  57. }
  58. static void string_stream_destroy_stub(struct string_stream *stream)
  59. {
  60. struct kunit *fake_test = kunit_get_current_test();
  61. struct string_stream_test_priv *priv = fake_test->priv;
  62. /* The kunit could own string_streams other than the one we are testing. */
  63. if (stream == priv->expected_free_stream) {
  64. if (priv->stream_was_freed)
  65. priv->stream_free_again = true;
  66. else
  67. priv->stream_was_freed = true;
  68. }
  69. /*
  70. * Calling string_stream_destroy() will only call this function again
  71. * because the redirection stub is still active.
  72. * Avoid calling deactivate_static_stub() or changing current->kunit_test
  73. * during cleanup.
  74. */
  75. string_stream_clear(stream);
  76. kfree(stream);
  77. }
  78. /* kunit_free_string_stream() calls string_stream_desrtoy() */
  79. static void string_stream_managed_free_test(struct kunit *test)
  80. {
  81. struct string_stream_test_priv *priv = test->priv;
  82. priv->expected_free_stream = NULL;
  83. priv->stream_was_freed = false;
  84. priv->stream_free_again = false;
  85. kunit_activate_static_stub(test,
  86. string_stream_destroy,
  87. string_stream_destroy_stub);
  88. priv->expected_free_stream = kunit_alloc_string_stream(test, GFP_KERNEL);
  89. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, priv->expected_free_stream);
  90. /* This should call the stub function. */
  91. kunit_free_string_stream(test, priv->expected_free_stream);
  92. KUNIT_EXPECT_TRUE(test, priv->stream_was_freed);
  93. KUNIT_EXPECT_FALSE(test, priv->stream_free_again);
  94. }
  95. /* string_stream object is freed when test is cleaned up. */
  96. static void string_stream_resource_free_test(struct kunit *test)
  97. {
  98. struct string_stream_test_priv *priv = test->priv;
  99. struct kunit *fake_test;
  100. fake_test = kunit_kzalloc(test, sizeof(*fake_test), GFP_KERNEL);
  101. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fake_test);
  102. kunit_init_test(fake_test, "string_stream_fake_test", NULL);
  103. fake_test->priv = priv;
  104. /*
  105. * Activate stub before creating string_stream so the
  106. * string_stream will be cleaned up first.
  107. */
  108. priv->expected_free_stream = NULL;
  109. priv->stream_was_freed = false;
  110. priv->stream_free_again = false;
  111. kunit_activate_static_stub(fake_test,
  112. string_stream_destroy,
  113. string_stream_destroy_stub);
  114. priv->expected_free_stream = kunit_alloc_string_stream(fake_test, GFP_KERNEL);
  115. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, priv->expected_free_stream);
  116. /* Set current->kunit_test to fake_test so the static stub will be called. */
  117. current->kunit_test = fake_test;
  118. /* Cleanup test - the stub function should be called */
  119. kunit_cleanup(fake_test);
  120. /* Set current->kunit_test back to current test. */
  121. current->kunit_test = test;
  122. KUNIT_EXPECT_TRUE(test, priv->stream_was_freed);
  123. KUNIT_EXPECT_FALSE(test, priv->stream_free_again);
  124. }
  125. /*
  126. * Add a series of lines to a string_stream. Check that all lines
  127. * appear in the correct order and no characters are dropped.
  128. */
  129. static void string_stream_line_add_test(struct kunit *test)
  130. {
  131. struct string_stream *stream;
  132. char line[60];
  133. char *concat_string, *pos, *string_end;
  134. size_t len, total_len;
  135. int num_lines, i;
  136. stream = kunit_alloc_string_stream(test, GFP_KERNEL);
  137. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream);
  138. /* Add series of sequence numbered lines */
  139. total_len = 0;
  140. for (i = 0; i < 100; ++i) {
  141. len = snprintf(line, sizeof(line),
  142. "The quick brown fox jumps over the lazy penguin %d\n", i);
  143. /* Sanity-check that our test string isn't truncated */
  144. KUNIT_ASSERT_LT(test, len, sizeof(line));
  145. string_stream_add(stream, line);
  146. total_len += len;
  147. }
  148. num_lines = i;
  149. concat_string = get_concatenated_string(test, stream);
  150. KUNIT_EXPECT_NOT_ERR_OR_NULL(test, concat_string);
  151. KUNIT_EXPECT_EQ(test, strlen(concat_string), total_len);
  152. /*
  153. * Split the concatenated string at the newlines and check that
  154. * all the original added strings are present.
  155. */
  156. pos = concat_string;
  157. for (i = 0; i < num_lines; ++i) {
  158. string_end = strchr(pos, '\n');
  159. KUNIT_EXPECT_NOT_NULL(test, string_end);
  160. /* Convert to NULL-terminated string */
  161. *string_end = '\0';
  162. snprintf(line, sizeof(line),
  163. "The quick brown fox jumps over the lazy penguin %d", i);
  164. KUNIT_EXPECT_STREQ(test, pos, line);
  165. pos = string_end + 1;
  166. }
  167. /* There shouldn't be any more data after this */
  168. KUNIT_EXPECT_EQ(test, strlen(pos), 0);
  169. }
  170. /* Add a series of lines of variable length to a string_stream. */
  171. static void string_stream_variable_length_line_test(struct kunit *test)
  172. {
  173. static const char line[] =
  174. "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  175. " 0123456789!$%^&*()_-+={}[]:;@'~#<>,.?/|";
  176. struct string_stream *stream;
  177. struct rnd_state rnd;
  178. char *concat_string, *pos, *string_end;
  179. size_t offset, total_len;
  180. int num_lines, i;
  181. stream = kunit_alloc_string_stream(test, GFP_KERNEL);
  182. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream);
  183. /*
  184. * Log many lines of varying lengths until we have created
  185. * many fragments.
  186. * The "randomness" must be repeatable.
  187. */
  188. prandom_seed_state(&rnd, 3141592653589793238ULL);
  189. total_len = 0;
  190. for (i = 0; i < 100; ++i) {
  191. offset = prandom_u32_state(&rnd) % (sizeof(line) - 1);
  192. string_stream_add(stream, "%s\n", &line[offset]);
  193. total_len += sizeof(line) - offset;
  194. }
  195. num_lines = i;
  196. concat_string = get_concatenated_string(test, stream);
  197. KUNIT_EXPECT_NOT_ERR_OR_NULL(test, concat_string);
  198. KUNIT_EXPECT_EQ(test, strlen(concat_string), total_len);
  199. /*
  200. * Split the concatenated string at the newlines and check that
  201. * all the original added strings are present.
  202. */
  203. prandom_seed_state(&rnd, 3141592653589793238ULL);
  204. pos = concat_string;
  205. for (i = 0; i < num_lines; ++i) {
  206. string_end = strchr(pos, '\n');
  207. KUNIT_EXPECT_NOT_NULL(test, string_end);
  208. /* Convert to NULL-terminated string */
  209. *string_end = '\0';
  210. offset = prandom_u32_state(&rnd) % (sizeof(line) - 1);
  211. KUNIT_EXPECT_STREQ(test, pos, &line[offset]);
  212. pos = string_end + 1;
  213. }
  214. /* There shouldn't be any more data after this */
  215. KUNIT_EXPECT_EQ(test, strlen(pos), 0);
  216. }
  217. /* Appending the content of one string stream to another. */
  218. static void string_stream_append_test(struct kunit *test)
  219. {
  220. static const char * const strings_1[] = {
  221. "one", "two", "three", "four", "five", "six",
  222. "seven", "eight", "nine", "ten",
  223. };
  224. static const char * const strings_2[] = {
  225. "Apple", "Pear", "Orange", "Banana", "Grape", "Apricot",
  226. };
  227. struct string_stream *stream_1, *stream_2;
  228. const char *stream1_content_before_append, *stream_2_content;
  229. char *combined_content;
  230. size_t combined_length;
  231. int i;
  232. stream_1 = kunit_alloc_string_stream(test, GFP_KERNEL);
  233. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream_1);
  234. stream_2 = kunit_alloc_string_stream(test, GFP_KERNEL);
  235. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream_2);
  236. /* Append content of empty stream to empty stream */
  237. string_stream_append(stream_1, stream_2);
  238. KUNIT_EXPECT_EQ(test, strlen(get_concatenated_string(test, stream_1)), 0);
  239. /* Add some data to stream_1 */
  240. for (i = 0; i < ARRAY_SIZE(strings_1); ++i)
  241. string_stream_add(stream_1, "%s\n", strings_1[i]);
  242. stream1_content_before_append = get_concatenated_string(test, stream_1);
  243. /* Append content of empty stream to non-empty stream */
  244. string_stream_append(stream_1, stream_2);
  245. KUNIT_EXPECT_STREQ(test, get_concatenated_string(test, stream_1),
  246. stream1_content_before_append);
  247. /* Add some data to stream_2 */
  248. for (i = 0; i < ARRAY_SIZE(strings_2); ++i)
  249. string_stream_add(stream_2, "%s\n", strings_2[i]);
  250. /* Append content of non-empty stream to non-empty stream */
  251. string_stream_append(stream_1, stream_2);
  252. /*
  253. * End result should be the original content of stream_1 plus
  254. * the content of stream_2.
  255. */
  256. stream_2_content = get_concatenated_string(test, stream_2);
  257. combined_length = strlen(stream1_content_before_append) + strlen(stream_2_content);
  258. combined_length++; /* for terminating \0 */
  259. combined_content = kunit_kmalloc(test, combined_length, GFP_KERNEL);
  260. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, combined_content);
  261. snprintf(combined_content, combined_length, "%s%s",
  262. stream1_content_before_append, stream_2_content);
  263. KUNIT_EXPECT_STREQ(test, get_concatenated_string(test, stream_1), combined_content);
  264. /* Append content of non-empty stream to empty stream */
  265. kunit_free_string_stream(test, stream_1);
  266. stream_1 = kunit_alloc_string_stream(test, GFP_KERNEL);
  267. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream_1);
  268. string_stream_append(stream_1, stream_2);
  269. KUNIT_EXPECT_STREQ(test, get_concatenated_string(test, stream_1), stream_2_content);
  270. }
  271. /* Appending the content of one string stream to one with auto-newlining. */
  272. static void string_stream_append_auto_newline_test(struct kunit *test)
  273. {
  274. struct string_stream *stream_1, *stream_2;
  275. /* Stream 1 has newline appending enabled */
  276. stream_1 = kunit_alloc_string_stream(test, GFP_KERNEL);
  277. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream_1);
  278. string_stream_set_append_newlines(stream_1, true);
  279. KUNIT_EXPECT_TRUE(test, stream_1->append_newlines);
  280. /* Stream 2 does not append newlines */
  281. stream_2 = kunit_alloc_string_stream(test, GFP_KERNEL);
  282. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream_2);
  283. /* Appending a stream with a newline should not add another newline */
  284. string_stream_add(stream_1, "Original string\n");
  285. string_stream_add(stream_2, "Appended content\n");
  286. string_stream_add(stream_2, "More stuff\n");
  287. string_stream_append(stream_1, stream_2);
  288. KUNIT_EXPECT_STREQ(test, get_concatenated_string(test, stream_1),
  289. "Original string\nAppended content\nMore stuff\n");
  290. kunit_free_string_stream(test, stream_2);
  291. stream_2 = kunit_alloc_string_stream(test, GFP_KERNEL);
  292. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream_2);
  293. /*
  294. * Appending a stream without newline should add a final newline.
  295. * The appended string_stream is treated as a single string so newlines
  296. * should not be inserted between fragments.
  297. */
  298. string_stream_add(stream_2, "Another");
  299. string_stream_add(stream_2, "And again");
  300. string_stream_append(stream_1, stream_2);
  301. KUNIT_EXPECT_STREQ(test, get_concatenated_string(test, stream_1),
  302. "Original string\nAppended content\nMore stuff\nAnotherAnd again\n");
  303. }
  304. /* Adding an empty string should not create a fragment. */
  305. static void string_stream_append_empty_string_test(struct kunit *test)
  306. {
  307. struct string_stream *stream;
  308. int original_frag_count;
  309. stream = kunit_alloc_string_stream(test, GFP_KERNEL);
  310. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream);
  311. /* Formatted empty string */
  312. string_stream_add(stream, "%s", "");
  313. KUNIT_EXPECT_TRUE(test, string_stream_is_empty(stream));
  314. KUNIT_EXPECT_TRUE(test, list_empty(&stream->fragments));
  315. /* Adding an empty string to a non-empty stream */
  316. string_stream_add(stream, "Add this line");
  317. original_frag_count = list_count_nodes(&stream->fragments);
  318. string_stream_add(stream, "%s", "");
  319. KUNIT_EXPECT_EQ(test, list_count_nodes(&stream->fragments), original_frag_count);
  320. KUNIT_EXPECT_STREQ(test, get_concatenated_string(test, stream), "Add this line");
  321. }
  322. /* Adding strings without automatic newline appending */
  323. static void string_stream_no_auto_newline_test(struct kunit *test)
  324. {
  325. struct string_stream *stream;
  326. stream = kunit_alloc_string_stream(test, GFP_KERNEL);
  327. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream);
  328. /*
  329. * Add some strings with and without newlines. All formatted newlines
  330. * should be preserved. It should not add any extra newlines.
  331. */
  332. string_stream_add(stream, "One");
  333. string_stream_add(stream, "Two\n");
  334. string_stream_add(stream, "%s\n", "Three");
  335. string_stream_add(stream, "%s", "Four\n");
  336. string_stream_add(stream, "Five\n%s", "Six");
  337. string_stream_add(stream, "Seven\n\n");
  338. string_stream_add(stream, "Eight");
  339. KUNIT_EXPECT_STREQ(test, get_concatenated_string(test, stream),
  340. "OneTwo\nThree\nFour\nFive\nSixSeven\n\nEight");
  341. }
  342. /* Adding strings with automatic newline appending */
  343. static void string_stream_auto_newline_test(struct kunit *test)
  344. {
  345. struct string_stream *stream;
  346. stream = kunit_alloc_string_stream(test, GFP_KERNEL);
  347. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream);
  348. string_stream_set_append_newlines(stream, true);
  349. KUNIT_EXPECT_TRUE(test, stream->append_newlines);
  350. /*
  351. * Add some strings with and without newlines. Newlines should
  352. * be appended to lines that do not end with \n, but newlines
  353. * resulting from the formatting should not be changed.
  354. */
  355. string_stream_add(stream, "One");
  356. string_stream_add(stream, "Two\n");
  357. string_stream_add(stream, "%s\n", "Three");
  358. string_stream_add(stream, "%s", "Four\n");
  359. string_stream_add(stream, "Five\n%s", "Six");
  360. string_stream_add(stream, "Seven\n\n");
  361. string_stream_add(stream, "Eight");
  362. KUNIT_EXPECT_STREQ(test, get_concatenated_string(test, stream),
  363. "One\nTwo\nThree\nFour\nFive\nSix\nSeven\n\nEight\n");
  364. }
  365. /*
  366. * This doesn't actually "test" anything. It reports time taken
  367. * and memory used for logging a large number of lines.
  368. */
  369. static void string_stream_performance_test(struct kunit *test)
  370. {
  371. struct string_stream_fragment *frag_container;
  372. struct string_stream *stream;
  373. char test_line[101];
  374. ktime_t start_time, end_time;
  375. size_t len, bytes_requested, actual_bytes_used, total_string_length;
  376. int offset, i;
  377. stream = kunit_alloc_string_stream(test, GFP_KERNEL);
  378. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream);
  379. memset(test_line, 'x', sizeof(test_line) - 1);
  380. test_line[sizeof(test_line) - 1] = '\0';
  381. start_time = ktime_get();
  382. for (i = 0; i < 10000; i++) {
  383. offset = i % (sizeof(test_line) - 1);
  384. string_stream_add(stream, "%s: %d\n", &test_line[offset], i);
  385. }
  386. end_time = ktime_get();
  387. /*
  388. * Calculate memory used. This doesn't include invisible
  389. * overhead due to kernel allocator fragment size rounding.
  390. */
  391. bytes_requested = sizeof(*stream);
  392. actual_bytes_used = ksize(stream);
  393. total_string_length = 0;
  394. list_for_each_entry(frag_container, &stream->fragments, node) {
  395. bytes_requested += sizeof(*frag_container);
  396. actual_bytes_used += ksize(frag_container);
  397. len = strlen(frag_container->fragment);
  398. total_string_length += len;
  399. bytes_requested += len + 1; /* +1 for '\0' */
  400. actual_bytes_used += ksize(frag_container->fragment);
  401. }
  402. kunit_info(test, "Time elapsed: %lld us\n",
  403. ktime_us_delta(end_time, start_time));
  404. kunit_info(test, "Total string length: %zu\n", total_string_length);
  405. kunit_info(test, "Bytes requested: %zu\n", bytes_requested);
  406. kunit_info(test, "Actual bytes allocated: %zu\n", actual_bytes_used);
  407. }
  408. static int string_stream_test_init(struct kunit *test)
  409. {
  410. struct string_stream_test_priv *priv;
  411. priv = kunit_kzalloc(test, sizeof(*priv), GFP_KERNEL);
  412. if (!priv)
  413. return -ENOMEM;
  414. test->priv = priv;
  415. return 0;
  416. }
  417. static struct kunit_case string_stream_test_cases[] = {
  418. KUNIT_CASE(string_stream_managed_init_test),
  419. KUNIT_CASE(string_stream_unmanaged_init_test),
  420. KUNIT_CASE(string_stream_managed_free_test),
  421. KUNIT_CASE(string_stream_resource_free_test),
  422. KUNIT_CASE(string_stream_line_add_test),
  423. KUNIT_CASE(string_stream_variable_length_line_test),
  424. KUNIT_CASE(string_stream_append_test),
  425. KUNIT_CASE(string_stream_append_auto_newline_test),
  426. KUNIT_CASE(string_stream_append_empty_string_test),
  427. KUNIT_CASE(string_stream_no_auto_newline_test),
  428. KUNIT_CASE(string_stream_auto_newline_test),
  429. KUNIT_CASE(string_stream_performance_test),
  430. {}
  431. };
  432. static struct kunit_suite string_stream_test_suite = {
  433. .name = "string-stream-test",
  434. .test_cases = string_stream_test_cases,
  435. .init = string_stream_test_init,
  436. };
  437. kunit_test_suites(&string_stream_test_suite);