dbgfs-test.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * DAMON Debugfs Interface Unit Tests
  4. *
  5. * Author: SeongJae Park <[email protected]>
  6. */
  7. #ifdef CONFIG_DAMON_DBGFS_KUNIT_TEST
  8. #ifndef _DAMON_DBGFS_TEST_H
  9. #define _DAMON_DBGFS_TEST_H
  10. #include <kunit/test.h>
  11. static void damon_dbgfs_test_str_to_ints(struct kunit *test)
  12. {
  13. char *question;
  14. int *answers;
  15. int expected[] = {12, 35, 46};
  16. ssize_t nr_integers = 0, i;
  17. question = "123";
  18. answers = str_to_ints(question, strlen(question), &nr_integers);
  19. KUNIT_EXPECT_EQ(test, (ssize_t)1, nr_integers);
  20. KUNIT_EXPECT_EQ(test, 123, answers[0]);
  21. kfree(answers);
  22. question = "123abc";
  23. answers = str_to_ints(question, strlen(question), &nr_integers);
  24. KUNIT_EXPECT_EQ(test, (ssize_t)1, nr_integers);
  25. KUNIT_EXPECT_EQ(test, 123, answers[0]);
  26. kfree(answers);
  27. question = "a123";
  28. answers = str_to_ints(question, strlen(question), &nr_integers);
  29. KUNIT_EXPECT_EQ(test, (ssize_t)0, nr_integers);
  30. kfree(answers);
  31. question = "12 35";
  32. answers = str_to_ints(question, strlen(question), &nr_integers);
  33. KUNIT_EXPECT_EQ(test, (ssize_t)2, nr_integers);
  34. for (i = 0; i < nr_integers; i++)
  35. KUNIT_EXPECT_EQ(test, expected[i], answers[i]);
  36. kfree(answers);
  37. question = "12 35 46";
  38. answers = str_to_ints(question, strlen(question), &nr_integers);
  39. KUNIT_EXPECT_EQ(test, (ssize_t)3, nr_integers);
  40. for (i = 0; i < nr_integers; i++)
  41. KUNIT_EXPECT_EQ(test, expected[i], answers[i]);
  42. kfree(answers);
  43. question = "12 35 abc 46";
  44. answers = str_to_ints(question, strlen(question), &nr_integers);
  45. KUNIT_EXPECT_EQ(test, (ssize_t)2, nr_integers);
  46. for (i = 0; i < 2; i++)
  47. KUNIT_EXPECT_EQ(test, expected[i], answers[i]);
  48. kfree(answers);
  49. question = "";
  50. answers = str_to_ints(question, strlen(question), &nr_integers);
  51. KUNIT_EXPECT_EQ(test, (ssize_t)0, nr_integers);
  52. kfree(answers);
  53. question = "\n";
  54. answers = str_to_ints(question, strlen(question), &nr_integers);
  55. KUNIT_EXPECT_EQ(test, (ssize_t)0, nr_integers);
  56. kfree(answers);
  57. }
  58. static void damon_dbgfs_test_set_targets(struct kunit *test)
  59. {
  60. struct damon_ctx *ctx = dbgfs_new_ctx();
  61. char buf[64];
  62. /* Make DAMON consider target has no pid */
  63. damon_select_ops(ctx, DAMON_OPS_PADDR);
  64. dbgfs_set_targets(ctx, 0, NULL);
  65. sprint_target_ids(ctx, buf, 64);
  66. KUNIT_EXPECT_STREQ(test, (char *)buf, "\n");
  67. dbgfs_set_targets(ctx, 1, NULL);
  68. sprint_target_ids(ctx, buf, 64);
  69. KUNIT_EXPECT_STREQ(test, (char *)buf, "42\n");
  70. dbgfs_set_targets(ctx, 0, NULL);
  71. sprint_target_ids(ctx, buf, 64);
  72. KUNIT_EXPECT_STREQ(test, (char *)buf, "\n");
  73. dbgfs_destroy_ctx(ctx);
  74. }
  75. static void damon_dbgfs_test_set_init_regions(struct kunit *test)
  76. {
  77. struct damon_ctx *ctx = damon_new_ctx();
  78. /* Each line represents one region in ``<target idx> <start> <end>`` */
  79. char * const valid_inputs[] = {"1 10 20\n 1 20 30\n1 35 45",
  80. "1 10 20\n",
  81. "1 10 20\n0 39 59\n0 70 134\n 1 20 25\n",
  82. ""};
  83. /* Reading the file again will show sorted, clean output */
  84. char * const valid_expects[] = {"1 10 20\n1 20 30\n1 35 45\n",
  85. "1 10 20\n",
  86. "0 39 59\n0 70 134\n1 10 20\n1 20 25\n",
  87. ""};
  88. char * const invalid_inputs[] = {"3 10 20\n", /* target not exists */
  89. "1 10 20\n 1 14 26\n", /* regions overlap */
  90. "0 10 20\n1 30 40\n 0 5 8"}; /* not sorted by address */
  91. char *input, *expect;
  92. int i, rc;
  93. char buf[256];
  94. damon_select_ops(ctx, DAMON_OPS_PADDR);
  95. dbgfs_set_targets(ctx, 3, NULL);
  96. /* Put valid inputs and check the results */
  97. for (i = 0; i < ARRAY_SIZE(valid_inputs); i++) {
  98. input = valid_inputs[i];
  99. expect = valid_expects[i];
  100. rc = set_init_regions(ctx, input, strnlen(input, 256));
  101. KUNIT_EXPECT_EQ(test, rc, 0);
  102. memset(buf, 0, 256);
  103. sprint_init_regions(ctx, buf, 256);
  104. KUNIT_EXPECT_STREQ(test, (char *)buf, expect);
  105. }
  106. /* Put invalid inputs and check the return error code */
  107. for (i = 0; i < ARRAY_SIZE(invalid_inputs); i++) {
  108. input = invalid_inputs[i];
  109. pr_info("input: %s\n", input);
  110. rc = set_init_regions(ctx, input, strnlen(input, 256));
  111. KUNIT_EXPECT_EQ(test, rc, -EINVAL);
  112. memset(buf, 0, 256);
  113. sprint_init_regions(ctx, buf, 256);
  114. KUNIT_EXPECT_STREQ(test, (char *)buf, "");
  115. }
  116. dbgfs_set_targets(ctx, 0, NULL);
  117. damon_destroy_ctx(ctx);
  118. }
  119. static struct kunit_case damon_test_cases[] = {
  120. KUNIT_CASE(damon_dbgfs_test_str_to_ints),
  121. KUNIT_CASE(damon_dbgfs_test_set_targets),
  122. KUNIT_CASE(damon_dbgfs_test_set_init_regions),
  123. {},
  124. };
  125. static struct kunit_suite damon_test_suite = {
  126. .name = "damon-dbgfs",
  127. .test_cases = damon_test_cases,
  128. };
  129. kunit_test_suite(damon_test_suite);
  130. #endif /* _DAMON_TEST_H */
  131. #endif /* CONFIG_DAMON_KUNIT_TEST */