117ccae8bSSeongJae Park /* SPDX-License-Identifier: GPL-2.0 */
217ccae8bSSeongJae Park /*
317ccae8bSSeongJae Park * DAMON Debugfs Interface Unit Tests
417ccae8bSSeongJae Park *
517ccae8bSSeongJae Park * Author: SeongJae Park <sjpark@amazon.de>
617ccae8bSSeongJae Park */
717ccae8bSSeongJae Park
817ccae8bSSeongJae Park #ifdef CONFIG_DAMON_DBGFS_KUNIT_TEST
917ccae8bSSeongJae Park
1017ccae8bSSeongJae Park #ifndef _DAMON_DBGFS_TEST_H
1117ccae8bSSeongJae Park #define _DAMON_DBGFS_TEST_H
1217ccae8bSSeongJae Park
1317ccae8bSSeongJae Park #include <kunit/test.h>
1417ccae8bSSeongJae Park
damon_dbgfs_test_str_to_ints(struct kunit * test)151971bd63SSeongJae Park static void damon_dbgfs_test_str_to_ints(struct kunit *test)
1617ccae8bSSeongJae Park {
1717ccae8bSSeongJae Park char *question;
181971bd63SSeongJae Park int *answers;
191971bd63SSeongJae Park int expected[] = {12, 35, 46};
2017ccae8bSSeongJae Park ssize_t nr_integers = 0, i;
2117ccae8bSSeongJae Park
2217ccae8bSSeongJae Park question = "123";
231971bd63SSeongJae Park answers = str_to_ints(question, strlen(question), &nr_integers);
2417ccae8bSSeongJae Park KUNIT_EXPECT_EQ(test, (ssize_t)1, nr_integers);
251971bd63SSeongJae Park KUNIT_EXPECT_EQ(test, 123, answers[0]);
2617ccae8bSSeongJae Park kfree(answers);
2717ccae8bSSeongJae Park
2817ccae8bSSeongJae Park question = "123abc";
291971bd63SSeongJae Park answers = str_to_ints(question, strlen(question), &nr_integers);
3017ccae8bSSeongJae Park KUNIT_EXPECT_EQ(test, (ssize_t)1, nr_integers);
311971bd63SSeongJae Park KUNIT_EXPECT_EQ(test, 123, answers[0]);
3217ccae8bSSeongJae Park kfree(answers);
3317ccae8bSSeongJae Park
3417ccae8bSSeongJae Park question = "a123";
351971bd63SSeongJae Park answers = str_to_ints(question, strlen(question), &nr_integers);
3617ccae8bSSeongJae Park KUNIT_EXPECT_EQ(test, (ssize_t)0, nr_integers);
3717ccae8bSSeongJae Park kfree(answers);
3817ccae8bSSeongJae Park
3917ccae8bSSeongJae Park question = "12 35";
401971bd63SSeongJae Park answers = str_to_ints(question, strlen(question), &nr_integers);
4117ccae8bSSeongJae Park KUNIT_EXPECT_EQ(test, (ssize_t)2, nr_integers);
4217ccae8bSSeongJae Park for (i = 0; i < nr_integers; i++)
4317ccae8bSSeongJae Park KUNIT_EXPECT_EQ(test, expected[i], answers[i]);
4417ccae8bSSeongJae Park kfree(answers);
4517ccae8bSSeongJae Park
4617ccae8bSSeongJae Park question = "12 35 46";
471971bd63SSeongJae Park answers = str_to_ints(question, strlen(question), &nr_integers);
4817ccae8bSSeongJae Park KUNIT_EXPECT_EQ(test, (ssize_t)3, nr_integers);
4917ccae8bSSeongJae Park for (i = 0; i < nr_integers; i++)
5017ccae8bSSeongJae Park KUNIT_EXPECT_EQ(test, expected[i], answers[i]);
5117ccae8bSSeongJae Park kfree(answers);
5217ccae8bSSeongJae Park
5317ccae8bSSeongJae Park question = "12 35 abc 46";
541971bd63SSeongJae Park answers = str_to_ints(question, strlen(question), &nr_integers);
5517ccae8bSSeongJae Park KUNIT_EXPECT_EQ(test, (ssize_t)2, nr_integers);
5617ccae8bSSeongJae Park for (i = 0; i < 2; i++)
5717ccae8bSSeongJae Park KUNIT_EXPECT_EQ(test, expected[i], answers[i]);
5817ccae8bSSeongJae Park kfree(answers);
5917ccae8bSSeongJae Park
6017ccae8bSSeongJae Park question = "";
611971bd63SSeongJae Park answers = str_to_ints(question, strlen(question), &nr_integers);
6217ccae8bSSeongJae Park KUNIT_EXPECT_EQ(test, (ssize_t)0, nr_integers);
6317ccae8bSSeongJae Park kfree(answers);
6417ccae8bSSeongJae Park
6517ccae8bSSeongJae Park question = "\n";
661971bd63SSeongJae Park answers = str_to_ints(question, strlen(question), &nr_integers);
6717ccae8bSSeongJae Park KUNIT_EXPECT_EQ(test, (ssize_t)0, nr_integers);
6817ccae8bSSeongJae Park kfree(answers);
6917ccae8bSSeongJae Park }
7017ccae8bSSeongJae Park
damon_dbgfs_test_set_targets(struct kunit * test)7117ccae8bSSeongJae Park static void damon_dbgfs_test_set_targets(struct kunit *test)
7217ccae8bSSeongJae Park {
7317ccae8bSSeongJae Park struct damon_ctx *ctx = dbgfs_new_ctx();
7417ccae8bSSeongJae Park char buf[64];
7517ccae8bSSeongJae Park
761971bd63SSeongJae Park /* Make DAMON consider target has no pid */
77*999b9467SSeongJae Park damon_select_ops(ctx, DAMON_OPS_PADDR);
7817ccae8bSSeongJae Park
791971bd63SSeongJae Park dbgfs_set_targets(ctx, 0, NULL);
8017ccae8bSSeongJae Park sprint_target_ids(ctx, buf, 64);
8117ccae8bSSeongJae Park KUNIT_EXPECT_STREQ(test, (char *)buf, "\n");
8217ccae8bSSeongJae Park
831971bd63SSeongJae Park dbgfs_set_targets(ctx, 1, NULL);
8417ccae8bSSeongJae Park sprint_target_ids(ctx, buf, 64);
851971bd63SSeongJae Park KUNIT_EXPECT_STREQ(test, (char *)buf, "42\n");
8617ccae8bSSeongJae Park
871971bd63SSeongJae Park dbgfs_set_targets(ctx, 0, NULL);
8817ccae8bSSeongJae Park sprint_target_ids(ctx, buf, 64);
8917ccae8bSSeongJae Park KUNIT_EXPECT_STREQ(test, (char *)buf, "\n");
9017ccae8bSSeongJae Park
9117ccae8bSSeongJae Park dbgfs_destroy_ctx(ctx);
9217ccae8bSSeongJae Park }
9317ccae8bSSeongJae Park
damon_dbgfs_test_set_init_regions(struct kunit * test)941c2e11bfSSeongJae Park static void damon_dbgfs_test_set_init_regions(struct kunit *test)
951c2e11bfSSeongJae Park {
961c2e11bfSSeongJae Park struct damon_ctx *ctx = damon_new_ctx();
97144760f8SSeongJae Park /* Each line represents one region in ``<target idx> <start> <end>`` */
98144760f8SSeongJae Park char * const valid_inputs[] = {"1 10 20\n 1 20 30\n1 35 45",
99144760f8SSeongJae Park "1 10 20\n",
100144760f8SSeongJae Park "1 10 20\n0 39 59\n0 70 134\n 1 20 25\n",
1011c2e11bfSSeongJae Park ""};
1021c2e11bfSSeongJae Park /* Reading the file again will show sorted, clean output */
103144760f8SSeongJae Park char * const valid_expects[] = {"1 10 20\n1 20 30\n1 35 45\n",
104144760f8SSeongJae Park "1 10 20\n",
105144760f8SSeongJae Park "0 39 59\n0 70 134\n1 10 20\n1 20 25\n",
1061c2e11bfSSeongJae Park ""};
107144760f8SSeongJae Park char * const invalid_inputs[] = {"3 10 20\n", /* target not exists */
108144760f8SSeongJae Park "1 10 20\n 1 14 26\n", /* regions overlap */
109144760f8SSeongJae Park "0 10 20\n1 30 40\n 0 5 8"}; /* not sorted by address */
1101c2e11bfSSeongJae Park char *input, *expect;
1111c2e11bfSSeongJae Park int i, rc;
1121c2e11bfSSeongJae Park char buf[256];
1131c2e11bfSSeongJae Park
114*999b9467SSeongJae Park damon_select_ops(ctx, DAMON_OPS_PADDR);
115*999b9467SSeongJae Park
1161971bd63SSeongJae Park dbgfs_set_targets(ctx, 3, NULL);
1171c2e11bfSSeongJae Park
1181c2e11bfSSeongJae Park /* Put valid inputs and check the results */
1191c2e11bfSSeongJae Park for (i = 0; i < ARRAY_SIZE(valid_inputs); i++) {
1201c2e11bfSSeongJae Park input = valid_inputs[i];
1211c2e11bfSSeongJae Park expect = valid_expects[i];
1221c2e11bfSSeongJae Park
1231c2e11bfSSeongJae Park rc = set_init_regions(ctx, input, strnlen(input, 256));
1241c2e11bfSSeongJae Park KUNIT_EXPECT_EQ(test, rc, 0);
1251c2e11bfSSeongJae Park
1261c2e11bfSSeongJae Park memset(buf, 0, 256);
1271c2e11bfSSeongJae Park sprint_init_regions(ctx, buf, 256);
1281c2e11bfSSeongJae Park
1291c2e11bfSSeongJae Park KUNIT_EXPECT_STREQ(test, (char *)buf, expect);
1301c2e11bfSSeongJae Park }
13101078655SColin Ian King /* Put invalid inputs and check the return error code */
1321c2e11bfSSeongJae Park for (i = 0; i < ARRAY_SIZE(invalid_inputs); i++) {
1331c2e11bfSSeongJae Park input = invalid_inputs[i];
1341c2e11bfSSeongJae Park pr_info("input: %s\n", input);
1351c2e11bfSSeongJae Park rc = set_init_regions(ctx, input, strnlen(input, 256));
1361c2e11bfSSeongJae Park KUNIT_EXPECT_EQ(test, rc, -EINVAL);
1371c2e11bfSSeongJae Park
1381c2e11bfSSeongJae Park memset(buf, 0, 256);
1391c2e11bfSSeongJae Park sprint_init_regions(ctx, buf, 256);
1401c2e11bfSSeongJae Park
1411c2e11bfSSeongJae Park KUNIT_EXPECT_STREQ(test, (char *)buf, "");
1421c2e11bfSSeongJae Park }
1431c2e11bfSSeongJae Park
1441971bd63SSeongJae Park dbgfs_set_targets(ctx, 0, NULL);
1451c2e11bfSSeongJae Park damon_destroy_ctx(ctx);
1461c2e11bfSSeongJae Park }
1471c2e11bfSSeongJae Park
14817ccae8bSSeongJae Park static struct kunit_case damon_test_cases[] = {
1491971bd63SSeongJae Park KUNIT_CASE(damon_dbgfs_test_str_to_ints),
15017ccae8bSSeongJae Park KUNIT_CASE(damon_dbgfs_test_set_targets),
1511c2e11bfSSeongJae Park KUNIT_CASE(damon_dbgfs_test_set_init_regions),
15217ccae8bSSeongJae Park {},
15317ccae8bSSeongJae Park };
15417ccae8bSSeongJae Park
15517ccae8bSSeongJae Park static struct kunit_suite damon_test_suite = {
15617ccae8bSSeongJae Park .name = "damon-dbgfs",
15717ccae8bSSeongJae Park .test_cases = damon_test_cases,
15817ccae8bSSeongJae Park };
15917ccae8bSSeongJae Park kunit_test_suite(damon_test_suite);
16017ccae8bSSeongJae Park
16117ccae8bSSeongJae Park #endif /* _DAMON_TEST_H */
16217ccae8bSSeongJae Park
16317ccae8bSSeongJae Park #endif /* CONFIG_DAMON_KUNIT_TEST */
164