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