^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * DAMON Debugfs Interface Unit Tests
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Author: SeongJae Park <sjpark@amazon.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #ifdef CONFIG_DAMON_DBGFS_KUNIT_TEST
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #ifndef _DAMON_DBGFS_TEST_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #define _DAMON_DBGFS_TEST_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <kunit/test.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) static void damon_dbgfs_test_str_to_target_ids(struct kunit *test)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) char *question;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) unsigned long *answers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) unsigned long expected[] = {12, 35, 46};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) ssize_t nr_integers = 0, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) question = "123";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) answers = str_to_target_ids(question, strlen(question),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) &nr_integers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) KUNIT_EXPECT_EQ(test, (ssize_t)1, nr_integers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) KUNIT_EXPECT_EQ(test, 123ul, answers[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) kfree(answers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) question = "123abc";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) answers = str_to_target_ids(question, strlen(question),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) &nr_integers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) KUNIT_EXPECT_EQ(test, (ssize_t)1, nr_integers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) KUNIT_EXPECT_EQ(test, 123ul, answers[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) kfree(answers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) question = "a123";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) answers = str_to_target_ids(question, strlen(question),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) &nr_integers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) KUNIT_EXPECT_EQ(test, (ssize_t)0, nr_integers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) kfree(answers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) question = "12 35";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) answers = str_to_target_ids(question, strlen(question),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) &nr_integers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) KUNIT_EXPECT_EQ(test, (ssize_t)2, nr_integers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) for (i = 0; i < nr_integers; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) KUNIT_EXPECT_EQ(test, expected[i], answers[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) kfree(answers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) question = "12 35 46";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) answers = str_to_target_ids(question, strlen(question),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) &nr_integers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) KUNIT_EXPECT_EQ(test, (ssize_t)3, nr_integers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) for (i = 0; i < nr_integers; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) KUNIT_EXPECT_EQ(test, expected[i], answers[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) kfree(answers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) question = "12 35 abc 46";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) answers = str_to_target_ids(question, strlen(question),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) &nr_integers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) KUNIT_EXPECT_EQ(test, (ssize_t)2, nr_integers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) for (i = 0; i < 2; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) KUNIT_EXPECT_EQ(test, expected[i], answers[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) kfree(answers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) question = "";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) answers = str_to_target_ids(question, strlen(question),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) &nr_integers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) KUNIT_EXPECT_EQ(test, (ssize_t)0, nr_integers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) kfree(answers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) question = "\n";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) answers = str_to_target_ids(question, strlen(question),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) &nr_integers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) KUNIT_EXPECT_EQ(test, (ssize_t)0, nr_integers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) kfree(answers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) static void damon_dbgfs_test_set_targets(struct kunit *test)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) struct damon_ctx *ctx = dbgfs_new_ctx();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) unsigned long ids[] = {1, 2, 3};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) char buf[64];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) /* Make DAMON consider target id as plain number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) ctx->primitive.target_valid = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) ctx->primitive.cleanup = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) damon_set_targets(ctx, ids, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) sprint_target_ids(ctx, buf, 64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) KUNIT_EXPECT_STREQ(test, (char *)buf, "1 2 3\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) damon_set_targets(ctx, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) sprint_target_ids(ctx, buf, 64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) KUNIT_EXPECT_STREQ(test, (char *)buf, "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) damon_set_targets(ctx, (unsigned long []){1, 2}, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) sprint_target_ids(ctx, buf, 64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) KUNIT_EXPECT_STREQ(test, (char *)buf, "1 2\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) damon_set_targets(ctx, (unsigned long []){2}, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) sprint_target_ids(ctx, buf, 64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) KUNIT_EXPECT_STREQ(test, (char *)buf, "2\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) damon_set_targets(ctx, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) sprint_target_ids(ctx, buf, 64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) KUNIT_EXPECT_STREQ(test, (char *)buf, "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) dbgfs_destroy_ctx(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static void damon_dbgfs_test_set_init_regions(struct kunit *test)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) struct damon_ctx *ctx = damon_new_ctx();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) unsigned long ids[] = {1, 2, 3};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) /* Each line represents one region in ``<target id> <start> <end>`` */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) char * const valid_inputs[] = {"2 10 20\n 2 20 30\n2 35 45",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) "2 10 20\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) "2 10 20\n1 39 59\n1 70 134\n 2 20 25\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) ""};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) /* Reading the file again will show sorted, clean output */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) char * const valid_expects[] = {"2 10 20\n2 20 30\n2 35 45\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) "2 10 20\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) "1 39 59\n1 70 134\n2 10 20\n2 20 25\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) ""};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) char * const invalid_inputs[] = {"4 10 20\n", /* target not exists */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) "2 10 20\n 2 14 26\n", /* regions overlap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) "1 10 20\n2 30 40\n 1 5 8"}; /* not sorted by address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) char *input, *expect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) int i, rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) char buf[256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) damon_set_targets(ctx, ids, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) /* Put valid inputs and check the results */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) for (i = 0; i < ARRAY_SIZE(valid_inputs); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) input = valid_inputs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) expect = valid_expects[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) rc = set_init_regions(ctx, input, strnlen(input, 256));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) KUNIT_EXPECT_EQ(test, rc, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) memset(buf, 0, 256);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) sprint_init_regions(ctx, buf, 256);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) KUNIT_EXPECT_STREQ(test, (char *)buf, expect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) /* Put invalid inputs and check the return error code */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) for (i = 0; i < ARRAY_SIZE(invalid_inputs); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) input = invalid_inputs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) pr_info("input: %s\n", input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) rc = set_init_regions(ctx, input, strnlen(input, 256));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) KUNIT_EXPECT_EQ(test, rc, -EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) memset(buf, 0, 256);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) sprint_init_regions(ctx, buf, 256);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) KUNIT_EXPECT_STREQ(test, (char *)buf, "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) damon_set_targets(ctx, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) damon_destroy_ctx(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) static struct kunit_case damon_test_cases[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) KUNIT_CASE(damon_dbgfs_test_str_to_target_ids),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) KUNIT_CASE(damon_dbgfs_test_set_targets),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) KUNIT_CASE(damon_dbgfs_test_set_init_regions),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) {},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) static struct kunit_suite damon_test_suite = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) .name = "damon-dbgfs",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) .test_cases = damon_test_cases,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) kunit_test_suite(damon_test_suite);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) #endif /* _DAMON_TEST_H */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) #endif /* CONFIG_DAMON_KUNIT_TEST */