1 /* SPDX-License-Identifier: GPL-2.0 */
3 * DAMON Debugfs Interface Unit Tests
5 * Author: SeongJae Park <sjpark@amazon.de>
8 #ifdef CONFIG_DAMON_DBGFS_KUNIT_TEST
10 #ifndef _DAMON_DBGFS_TEST_H
11 #define _DAMON_DBGFS_TEST_H
13 #include <kunit/test.h>
15 static void damon_dbgfs_test_str_to_ints(struct kunit *test)
19 int expected[] = {12, 35, 46};
20 ssize_t nr_integers = 0, i;
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]);
29 answers = str_to_ints(question, strlen(question), &nr_integers);
30 KUNIT_EXPECT_EQ(test, (ssize_t)1, nr_integers);
31 KUNIT_EXPECT_EQ(test, 123, answers[0]);
35 answers = str_to_ints(question, strlen(question), &nr_integers);
36 KUNIT_EXPECT_EQ(test, (ssize_t)0, nr_integers);
40 answers = str_to_ints(question, strlen(question), &nr_integers);
41 KUNIT_EXPECT_EQ(test, (ssize_t)2, nr_integers);
42 for (i = 0; i < nr_integers; i++)
43 KUNIT_EXPECT_EQ(test, expected[i], answers[i]);
46 question = "12 35 46";
47 answers = str_to_ints(question, strlen(question), &nr_integers);
48 KUNIT_EXPECT_EQ(test, (ssize_t)3, nr_integers);
49 for (i = 0; i < nr_integers; i++)
50 KUNIT_EXPECT_EQ(test, expected[i], answers[i]);
53 question = "12 35 abc 46";
54 answers = str_to_ints(question, strlen(question), &nr_integers);
55 KUNIT_EXPECT_EQ(test, (ssize_t)2, nr_integers);
56 for (i = 0; i < 2; i++)
57 KUNIT_EXPECT_EQ(test, expected[i], answers[i]);
61 answers = str_to_ints(question, strlen(question), &nr_integers);
62 KUNIT_EXPECT_EQ(test, (ssize_t)0, nr_integers);
66 answers = str_to_ints(question, strlen(question), &nr_integers);
67 KUNIT_EXPECT_EQ(test, (ssize_t)0, nr_integers);
71 static void damon_dbgfs_test_set_targets(struct kunit *test)
73 struct damon_ctx *ctx = dbgfs_new_ctx();
76 /* Make DAMON consider target has no pid */
77 damon_select_ops(ctx, DAMON_OPS_PADDR);
79 dbgfs_set_targets(ctx, 0, NULL);
80 sprint_target_ids(ctx, buf, 64);
81 KUNIT_EXPECT_STREQ(test, (char *)buf, "\n");
83 dbgfs_set_targets(ctx, 1, NULL);
84 sprint_target_ids(ctx, buf, 64);
85 KUNIT_EXPECT_STREQ(test, (char *)buf, "42\n");
87 dbgfs_set_targets(ctx, 0, NULL);
88 sprint_target_ids(ctx, buf, 64);
89 KUNIT_EXPECT_STREQ(test, (char *)buf, "\n");
91 dbgfs_destroy_ctx(ctx);
94 static void damon_dbgfs_test_set_init_regions(struct kunit *test)
96 struct damon_ctx *ctx = damon_new_ctx();
97 /* Each line represents one region in ``<target idx> <start> <end>`` */
98 char * const valid_inputs[] = {"1 10 20\n 1 20 30\n1 35 45",
100 "1 10 20\n0 39 59\n0 70 134\n 1 20 25\n",
102 /* Reading the file again will show sorted, clean output */
103 char * const valid_expects[] = {"1 10 20\n1 20 30\n1 35 45\n",
105 "0 39 59\n0 70 134\n1 10 20\n1 20 25\n",
107 char * const invalid_inputs[] = {"3 10 20\n", /* target not exists */
108 "1 10 20\n 1 14 26\n", /* regions overlap */
109 "0 10 20\n1 30 40\n 0 5 8"}; /* not sorted by address */
110 char *input, *expect;
114 damon_select_ops(ctx, DAMON_OPS_PADDR);
116 dbgfs_set_targets(ctx, 3, NULL);
118 /* Put valid inputs and check the results */
119 for (i = 0; i < ARRAY_SIZE(valid_inputs); i++) {
120 input = valid_inputs[i];
121 expect = valid_expects[i];
123 rc = set_init_regions(ctx, input, strnlen(input, 256));
124 KUNIT_EXPECT_EQ(test, rc, 0);
127 sprint_init_regions(ctx, buf, 256);
129 KUNIT_EXPECT_STREQ(test, (char *)buf, expect);
131 /* Put invalid inputs and check the return error code */
132 for (i = 0; i < ARRAY_SIZE(invalid_inputs); i++) {
133 input = invalid_inputs[i];
134 pr_info("input: %s\n", input);
135 rc = set_init_regions(ctx, input, strnlen(input, 256));
136 KUNIT_EXPECT_EQ(test, rc, -EINVAL);
139 sprint_init_regions(ctx, buf, 256);
141 KUNIT_EXPECT_STREQ(test, (char *)buf, "");
144 dbgfs_set_targets(ctx, 0, NULL);
145 damon_destroy_ctx(ctx);
148 static struct kunit_case damon_test_cases[] = {
149 KUNIT_CASE(damon_dbgfs_test_str_to_ints),
150 KUNIT_CASE(damon_dbgfs_test_set_targets),
151 KUNIT_CASE(damon_dbgfs_test_set_init_regions),
155 static struct kunit_suite damon_test_suite = {
156 .name = "damon-dbgfs",
157 .test_cases = damon_test_cases,
159 kunit_test_suite(damon_test_suite);
161 #endif /* _DAMON_TEST_H */
163 #endif /* CONFIG_DAMON_KUNIT_TEST */