From: Wen Yang <[email protected]>

Add a test case to the shared rv_monitors_test.c suite covering
the uprobe-line parser in the tlob monitor.

Cases cover valid inputs, malformed paths and offsets (including
negative values), out-of-range thresholds, and valid and invalid
remove lines.

Suggested-by: Gabriele Monaco <[email protected]>
Signed-off-by: Wen Yang <[email protected]>
---
 kernel/trace/rv/monitors/tlob/tlob.c       |  11 ++
 kernel/trace/rv/monitors/tlob/tlob_kunit.c | 151 +++++++++++++++++++++
 kernel/trace/rv/monitors/tlob/tlob_kunit.h |  18 +++
 kernel/trace/rv/rv_monitors_test.c         |   2 +
 4 files changed, 182 insertions(+)
 create mode 100644 kernel/trace/rv/monitors/tlob/tlob_kunit.c
 create mode 100644 kernel/trace/rv/monitors/tlob/tlob_kunit.h

diff --git a/kernel/trace/rv/monitors/tlob/tlob.c 
b/kernel/trace/rv/monitors/tlob/tlob.c
index 066a2f0663d5..159fc9a2c49e 100644
--- a/kernel/trace/rv/monitors/tlob/tlob.c
+++ b/kernel/trace/rv/monitors/tlob/tlob.c
@@ -863,6 +863,17 @@ static int tlob_parse_remove_line(char *buf, char 
**name_out)
        return 0;
 }
 
+#if IS_ENABLED(CONFIG_RV_MONITORS_KUNIT_TEST)
+#include <kunit/visibility.h>
+#include "tlob_kunit.h"
+
+const struct rv_tlob_kunit_ops rv_tlob_kunit_ops = {
+       .parse_uprobe_line = tlob_parse_uprobe_line,
+       .parse_remove_line = tlob_parse_remove_line,
+};
+EXPORT_SYMBOL_IF_KUNIT(rv_tlob_kunit_ops);
+#endif
+
 static int tlob_create_or_delete_uprobe(char *buf)
 {
        loff_t offset_start, offset_stop;
diff --git a/kernel/trace/rv/monitors/tlob/tlob_kunit.c 
b/kernel/trace/rv/monitors/tlob/tlob_kunit.c
new file mode 100644
index 000000000000..06b212d1103b
--- /dev/null
+++ b/kernel/trace/rv/monitors/tlob/tlob_kunit.c
@@ -0,0 +1,151 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <linux/kernel.h>
+#include <linux/string.h>
+#include "tlob_kunit.h"
+
+#if IS_REACHABLE(CONFIG_RV_MON_TLOB)
+
+/* Valid "p:NAME PATH:START STOP threshold=NS" lines and what they decode to. 
*/
+static const struct {
+       const char      *line;
+       const char      *name;
+       const char      *path;
+       loff_t          start;
+       loff_t          stop;
+       u64             thr;
+} tlob_parse_valid[] = {
+       { "p:region /usr/bin/myapp:4768 4848 threshold=5000000",
+         "region", "/usr/bin/myapp", 4768, 4848, 5000000 },
+       /* Base 0: hex offsets must not decode as decimal. */
+       { "p:my_region_2 /usr/bin/myapp:0x12a0 0x12f0 threshold=10000000",
+         "my_region_2", "/usr/bin/myapp", 0x12a0, 0x12f0, 10000000 },
+       /* The last ':' separates the offset, so a ':' in PATH is fine. */
+       { "p:_r3 /opt/my:app/bin:0x100 0x200 threshold=1000000",
+         "_r3", "/opt/my:app/bin", 0x100, 0x200, 1000000 },
+       /* TLOB_MAX_NAME - 1 characters: the longest name accepted. */
+       { "p:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa /bin/a:0x100 0x200 threshold=5000",
+         "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "/bin/a", 0x100, 0x200, 5000 },
+       /* The accepted threshold bounds themselves. */
+       { "p:lo /bin/a:0x100 0x200 threshold=1000",
+         "lo", "/bin/a", 0x100, 0x200, 1000 },
+       { "p:hi /bin/a:0x100 0x200 threshold=3600000000000",
+         "hi", "/bin/a", 0x100, 0x200, 3600000000000ULL },
+};
+
+/* Malformed "p:..." lines that must be rejected with -EINVAL. */
+static const char * const tlob_parse_invalid[] = {
+       "p /usr/bin/myapp:0x100 0x200 threshold=5000",          /* no name */
+       "p:",                                                   /* empty name */
+       "p: /usr/bin/myapp:0x100 0x200 threshold=5000",         /* name is a 
path */
+       "p:r /bin/a:0x100junk 0x200 threshold=5000",            /* trailing 
junk */
+       "p:1region /usr/bin/myapp:0x100 0x200 threshold=5000",  /* digit lead */
+       "p:my-win /usr/bin/myapp:0x100 0x200 threshold=5000",   /* bad char */
+       "p:my.win /usr/bin/myapp:0x100 0x200 threshold=5000",   /* bad char */
+       /* TLOB_MAX_NAME characters: one over the limit. */
+       "p:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa /bin/a:0x100 0x200 threshold=5000",
+       "p:region :0x100 0x200 threshold=5000",                 /* empty path */
+       "p:region /usr/bin/myapp:0x100 threshold=5000",         /* missing stop 
*/
+       "p:region /usr/bin/myapp:-1 0x200 threshold=5000",              /* 
negative start */
+       "p:region /usr/bin/myapp:0x100 -1 threshold=5000000",   /* negative 
stop */
+       "p:region /usr/bin/myapp:0x100 0x200",                  /* missing 
threshold */
+       "p:region /usr/bin/myapp:0x100 0x100 threshold=5000",   /* start == 
stop */
+       /* Duplicate threshold=, like a duplicate offset, is an error. */
+       "p:region /bin/a:0x100 0x200 threshold=5000 threshold=9000",
+};
+
+/* threshold_ns out of valid range => -ERANGE. */
+static const char * const tlob_parse_out_of_range[] = {
+       "p:region /usr/bin/myapp:0x100 0x200 threshold=0",
+       "p:region /usr/bin/myapp:0x100 0x200 threshold=999",
+       "p:region /usr/bin/myapp:0x100 0x200 threshold=3600000000001",
+};
+
+/* Valid "-:NAME" remove lines. */
+static const char * const tlob_remove_valid[] = {
+       "-:region",
+       "-:my_region",
+};
+
+/* Malformed remove lines that must be rejected with -EINVAL. */
+static const char * const tlob_remove_invalid[] = {
+       "-",    /* no name */
+       "-:",   /* empty name */
+       "-region",      /* missing ':' */
+       "-:my region",  /* bad char */
+       "-:1region",    /* digit lead */
+       "-:my-region",  /* bad char */
+       "-:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",   /* TLOB_MAX_NAME, one too many 
*/
+};
+
+static void rv_test_tlob(struct kunit *test)
+{
+       u64 thr;
+       char *name, *path;
+       loff_t start, stop;
+       char buf[128];
+       int i, ret;
+
+       /* NAME and PATH come back NUL-terminated inside buf. */
+       strscpy(buf, "p:region /usr/bin/myapp:0x100 0x200 threshold=5000",
+               sizeof(buf));
+       ret = rv_tlob_kunit_ops.parse_uprobe_line(buf, &name, &path,
+                                                 &start, &stop, &thr);
+       KUNIT_EXPECT_EQ(test, ret, 0);
+       KUNIT_EXPECT_STREQ(test, name, "region");
+       KUNIT_EXPECT_STREQ(test, path, "/usr/bin/myapp");
+
+       strscpy(buf, "-:region", sizeof(buf));
+       ret = rv_tlob_kunit_ops.parse_remove_line(buf, &name);
+       KUNIT_EXPECT_EQ(test, ret, 0);
+       KUNIT_EXPECT_STREQ(test, name, "region");
+
+       for (i = 0; i < ARRAY_SIZE(tlob_parse_valid); i++) {
+               strscpy(buf, tlob_parse_valid[i].line, sizeof(buf));
+               ret = rv_tlob_kunit_ops.parse_uprobe_line(buf, &name, &path,
+                                                         &start, &stop,
+                                                         &thr);
+               KUNIT_EXPECT_EQ(test, ret, 0);
+               if (ret)
+                       continue;
+               KUNIT_EXPECT_STREQ(test, name, tlob_parse_valid[i].name);
+               KUNIT_EXPECT_STREQ(test, path, tlob_parse_valid[i].path);
+               KUNIT_EXPECT_EQ(test, start, tlob_parse_valid[i].start);
+               KUNIT_EXPECT_EQ(test, stop, tlob_parse_valid[i].stop);
+               KUNIT_EXPECT_EQ(test, thr, tlob_parse_valid[i].thr);
+       }
+
+       for (i = 0; i < ARRAY_SIZE(tlob_parse_invalid); i++) {
+               strscpy(buf, tlob_parse_invalid[i], sizeof(buf));
+               ret = rv_tlob_kunit_ops.parse_uprobe_line(buf, &name, &path,
+                                                         &start, &stop,
+                                                         &thr);
+               KUNIT_EXPECT_EQ(test, ret, -EINVAL);
+       }
+
+       for (i = 0; i < ARRAY_SIZE(tlob_parse_out_of_range); i++) {
+               strscpy(buf, tlob_parse_out_of_range[i], sizeof(buf));
+               ret = rv_tlob_kunit_ops.parse_uprobe_line(buf, &name, &path,
+                                                         &start, &stop,
+                                                         &thr);
+               KUNIT_EXPECT_EQ(test, ret, -ERANGE);
+       }
+
+       for (i = 0; i < ARRAY_SIZE(tlob_remove_valid); i++) {
+               strscpy(buf, tlob_remove_valid[i], sizeof(buf));
+               ret = rv_tlob_kunit_ops.parse_remove_line(buf, &name);
+               KUNIT_EXPECT_EQ(test, ret, 0);
+               if (!ret)
+                       KUNIT_EXPECT_STREQ(test, name,
+                                          tlob_remove_valid[i] + 2);
+       }
+
+       for (i = 0; i < ARRAY_SIZE(tlob_remove_invalid); i++) {
+               strscpy(buf, tlob_remove_invalid[i], sizeof(buf));
+               ret = rv_tlob_kunit_ops.parse_remove_line(buf, &name);
+               KUNIT_EXPECT_EQ(test, ret, -EINVAL);
+       }
+}
+
+#else
+#define rv_test_tlob rv_test_stub
+#endif
diff --git a/kernel/trace/rv/monitors/tlob/tlob_kunit.h 
b/kernel/trace/rv/monitors/tlob/tlob_kunit.h
new file mode 100644
index 000000000000..c0c80c34f17b
--- /dev/null
+++ b/kernel/trace/rv/monitors/tlob/tlob_kunit.h
@@ -0,0 +1,18 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef __TLOB_KUNIT_H
+#define __TLOB_KUNIT_H
+
+#if IS_ENABLED(CONFIG_RV_MONITORS_KUNIT_TEST)
+
+#include <linux/types.h>
+
+extern const struct rv_tlob_kunit_ops {
+       int (*parse_uprobe_line)(char *buf, char **name_out, char **path_out,
+                                loff_t *start_out, loff_t *stop_out,
+                                u64 *thr_out);
+       int (*parse_remove_line)(char *buf, char **name_out);
+} rv_tlob_kunit_ops;
+
+#endif
+
+#endif /* __TLOB_KUNIT_H */
diff --git a/kernel/trace/rv/rv_monitors_test.c 
b/kernel/trace/rv/rv_monitors_test.c
index b30e49419e34..ee9fde63b291 100644
--- a/kernel/trace/rv/rv_monitors_test.c
+++ b/kernel/trace/rv/rv_monitors_test.c
@@ -309,6 +309,7 @@ static void rv_test_rvtest_arm_after_destroy(struct kunit 
*test)
 #include "monitors/sts/sts_kunit.c"
 #include "monitors/opid/opid_kunit.c"
 #include "monitors/nomiss/nomiss_kunit.c"
+#include "monitors/tlob/tlob_kunit.c"
 #include "monitors/pagefault/pagefault_kunit.c"
 #include "monitors/sleep/sleep_kunit.c"
 
@@ -323,6 +324,7 @@ static struct kunit_case rv_mon_test_cases[] = {
        KUNIT_CASE(rv_test_sleep),
        KUNIT_CASE(rv_test_rvtest_underflow),
        KUNIT_CASE(rv_test_rvtest_arm_after_destroy),
+       KUNIT_CASE(rv_test_tlob),
        {}
 };
 
-- 
2.25.1


Reply via email to