0001
0002
0003
0004
0005
0006
0007
0008 #ifdef CONFIG_DAMON_DBGFS_KUNIT_TEST
0009
0010 #ifndef _DAMON_DBGFS_TEST_H
0011 #define _DAMON_DBGFS_TEST_H
0012
0013 #include <kunit/test.h>
0014
0015 static void damon_dbgfs_test_str_to_ints(struct kunit *test)
0016 {
0017 char *question;
0018 int *answers;
0019 int expected[] = {12, 35, 46};
0020 ssize_t nr_integers = 0, i;
0021
0022 question = "123";
0023 answers = str_to_ints(question, strlen(question), &nr_integers);
0024 KUNIT_EXPECT_EQ(test, (ssize_t)1, nr_integers);
0025 KUNIT_EXPECT_EQ(test, 123, answers[0]);
0026 kfree(answers);
0027
0028 question = "123abc";
0029 answers = str_to_ints(question, strlen(question), &nr_integers);
0030 KUNIT_EXPECT_EQ(test, (ssize_t)1, nr_integers);
0031 KUNIT_EXPECT_EQ(test, 123, answers[0]);
0032 kfree(answers);
0033
0034 question = "a123";
0035 answers = str_to_ints(question, strlen(question), &nr_integers);
0036 KUNIT_EXPECT_EQ(test, (ssize_t)0, nr_integers);
0037 kfree(answers);
0038
0039 question = "12 35";
0040 answers = str_to_ints(question, strlen(question), &nr_integers);
0041 KUNIT_EXPECT_EQ(test, (ssize_t)2, nr_integers);
0042 for (i = 0; i < nr_integers; i++)
0043 KUNIT_EXPECT_EQ(test, expected[i], answers[i]);
0044 kfree(answers);
0045
0046 question = "12 35 46";
0047 answers = str_to_ints(question, strlen(question), &nr_integers);
0048 KUNIT_EXPECT_EQ(test, (ssize_t)3, nr_integers);
0049 for (i = 0; i < nr_integers; i++)
0050 KUNIT_EXPECT_EQ(test, expected[i], answers[i]);
0051 kfree(answers);
0052
0053 question = "12 35 abc 46";
0054 answers = str_to_ints(question, strlen(question), &nr_integers);
0055 KUNIT_EXPECT_EQ(test, (ssize_t)2, nr_integers);
0056 for (i = 0; i < 2; i++)
0057 KUNIT_EXPECT_EQ(test, expected[i], answers[i]);
0058 kfree(answers);
0059
0060 question = "";
0061 answers = str_to_ints(question, strlen(question), &nr_integers);
0062 KUNIT_EXPECT_EQ(test, (ssize_t)0, nr_integers);
0063 kfree(answers);
0064
0065 question = "\n";
0066 answers = str_to_ints(question, strlen(question), &nr_integers);
0067 KUNIT_EXPECT_EQ(test, (ssize_t)0, nr_integers);
0068 kfree(answers);
0069 }
0070
0071 static void damon_dbgfs_test_set_targets(struct kunit *test)
0072 {
0073 struct damon_ctx *ctx = dbgfs_new_ctx();
0074 char buf[64];
0075
0076
0077 damon_select_ops(ctx, DAMON_OPS_PADDR);
0078
0079 dbgfs_set_targets(ctx, 0, NULL);
0080 sprint_target_ids(ctx, buf, 64);
0081 KUNIT_EXPECT_STREQ(test, (char *)buf, "\n");
0082
0083 dbgfs_set_targets(ctx, 1, NULL);
0084 sprint_target_ids(ctx, buf, 64);
0085 KUNIT_EXPECT_STREQ(test, (char *)buf, "42\n");
0086
0087 dbgfs_set_targets(ctx, 0, NULL);
0088 sprint_target_ids(ctx, buf, 64);
0089 KUNIT_EXPECT_STREQ(test, (char *)buf, "\n");
0090
0091 dbgfs_destroy_ctx(ctx);
0092 }
0093
0094 static void damon_dbgfs_test_set_init_regions(struct kunit *test)
0095 {
0096 struct damon_ctx *ctx = damon_new_ctx();
0097
0098 char * const valid_inputs[] = {"1 10 20\n 1 20 30\n1 35 45",
0099 "1 10 20\n",
0100 "1 10 20\n0 39 59\n0 70 134\n 1 20 25\n",
0101 ""};
0102
0103 char * const valid_expects[] = {"1 10 20\n1 20 30\n1 35 45\n",
0104 "1 10 20\n",
0105 "0 39 59\n0 70 134\n1 10 20\n1 20 25\n",
0106 ""};
0107 char * const invalid_inputs[] = {"3 10 20\n",
0108 "1 10 20\n 1 14 26\n",
0109 "0 10 20\n1 30 40\n 0 5 8"};
0110 char *input, *expect;
0111 int i, rc;
0112 char buf[256];
0113
0114 damon_select_ops(ctx, DAMON_OPS_PADDR);
0115
0116 dbgfs_set_targets(ctx, 3, NULL);
0117
0118
0119 for (i = 0; i < ARRAY_SIZE(valid_inputs); i++) {
0120 input = valid_inputs[i];
0121 expect = valid_expects[i];
0122
0123 rc = set_init_regions(ctx, input, strnlen(input, 256));
0124 KUNIT_EXPECT_EQ(test, rc, 0);
0125
0126 memset(buf, 0, 256);
0127 sprint_init_regions(ctx, buf, 256);
0128
0129 KUNIT_EXPECT_STREQ(test, (char *)buf, expect);
0130 }
0131
0132 for (i = 0; i < ARRAY_SIZE(invalid_inputs); i++) {
0133 input = invalid_inputs[i];
0134 pr_info("input: %s\n", input);
0135 rc = set_init_regions(ctx, input, strnlen(input, 256));
0136 KUNIT_EXPECT_EQ(test, rc, -EINVAL);
0137
0138 memset(buf, 0, 256);
0139 sprint_init_regions(ctx, buf, 256);
0140
0141 KUNIT_EXPECT_STREQ(test, (char *)buf, "");
0142 }
0143
0144 dbgfs_set_targets(ctx, 0, NULL);
0145 damon_destroy_ctx(ctx);
0146 }
0147
0148 static struct kunit_case damon_test_cases[] = {
0149 KUNIT_CASE(damon_dbgfs_test_str_to_ints),
0150 KUNIT_CASE(damon_dbgfs_test_set_targets),
0151 KUNIT_CASE(damon_dbgfs_test_set_init_regions),
0152 {},
0153 };
0154
0155 static struct kunit_suite damon_test_suite = {
0156 .name = "damon-dbgfs",
0157 .test_cases = damon_test_cases,
0158 };
0159 kunit_test_suite(damon_test_suite);
0160
0161 #endif
0162
0163 #endif