1 #include <stdbool.h>
2 #include <stdlib.h>
3 #include "tests.h"
4 #include "dso.h"
5 #include "debug.h"
6
test(const char * path,bool alloc_name,bool alloc_ext,bool kmod,bool comp,const char * name,const char * ext)7 static int test(const char *path, bool alloc_name, bool alloc_ext,
8 bool kmod, bool comp, const char *name, const char *ext)
9 {
10 struct kmod_path m;
11
12 memset(&m, 0x0, sizeof(m));
13
14 TEST_ASSERT_VAL("kmod_path__parse",
15 !__kmod_path__parse(&m, path, alloc_name, alloc_ext));
16
17 pr_debug("%s - alloc name %d, alloc ext %d, kmod %d, comp %d, name '%s', ext '%s'\n",
18 path, alloc_name, alloc_ext, m.kmod, m.comp, m.name, m.ext);
19
20 TEST_ASSERT_VAL("wrong kmod", m.kmod == kmod);
21 TEST_ASSERT_VAL("wrong comp", m.comp == comp);
22
23 if (ext)
24 TEST_ASSERT_VAL("wrong ext", m.ext && !strcmp(ext, m.ext));
25 else
26 TEST_ASSERT_VAL("wrong ext", !m.ext);
27
28 if (name)
29 TEST_ASSERT_VAL("wrong name", m.name && !strcmp(name, m.name));
30 else
31 TEST_ASSERT_VAL("wrong name", !m.name);
32
33 free(m.name);
34 free(m.ext);
35 return 0;
36 }
37
test_is_kernel_module(const char * path,int cpumode,bool expect)38 static int test_is_kernel_module(const char *path, int cpumode, bool expect)
39 {
40 TEST_ASSERT_VAL("is_kernel_module",
41 (!!is_kernel_module(path, cpumode)) == (!!expect));
42 pr_debug("%s (cpumode: %d) - is_kernel_module: %s\n",
43 path, cpumode, expect ? "true" : "false");
44 return 0;
45 }
46
47 #define T(path, an, ae, k, c, n, e) \
48 TEST_ASSERT_VAL("failed", !test(path, an, ae, k, c, n, e))
49
50 #define M(path, c, e) \
51 TEST_ASSERT_VAL("failed", !test_is_kernel_module(path, c, e))
52
test__kmod_path__parse(int subtest __maybe_unused)53 int test__kmod_path__parse(int subtest __maybe_unused)
54 {
55 /* path alloc_name alloc_ext kmod comp name ext */
56 T("/xxxx/xxxx/x-x.ko", true , true , true, false, "[x_x]", NULL);
57 T("/xxxx/xxxx/x-x.ko", false , true , true, false, NULL , NULL);
58 T("/xxxx/xxxx/x-x.ko", true , false , true, false, "[x_x]", NULL);
59 T("/xxxx/xxxx/x-x.ko", false , false , true, false, NULL , NULL);
60 M("/xxxx/xxxx/x-x.ko", PERF_RECORD_MISC_CPUMODE_UNKNOWN, true);
61 M("/xxxx/xxxx/x-x.ko", PERF_RECORD_MISC_KERNEL, true);
62 M("/xxxx/xxxx/x-x.ko", PERF_RECORD_MISC_USER, false);
63
64 #ifdef HAVE_ZLIB_SUPPORT
65 /* path alloc_name alloc_ext kmod comp name ext */
66 T("/xxxx/xxxx/x.ko.gz", true , true , true, true, "[x]", "gz");
67 T("/xxxx/xxxx/x.ko.gz", false , true , true, true, NULL , "gz");
68 T("/xxxx/xxxx/x.ko.gz", true , false , true, true, "[x]", NULL);
69 T("/xxxx/xxxx/x.ko.gz", false , false , true, true, NULL , NULL);
70 M("/xxxx/xxxx/x.ko.gz", PERF_RECORD_MISC_CPUMODE_UNKNOWN, true);
71 M("/xxxx/xxxx/x.ko.gz", PERF_RECORD_MISC_KERNEL, true);
72 M("/xxxx/xxxx/x.ko.gz", PERF_RECORD_MISC_USER, false);
73
74 /* path alloc_name alloc_ext kmod comp name ext */
75 T("/xxxx/xxxx/x.gz", true , true , false, true, "x.gz" ,"gz");
76 T("/xxxx/xxxx/x.gz", false , true , false, true, NULL ,"gz");
77 T("/xxxx/xxxx/x.gz", true , false , false, true, "x.gz" , NULL);
78 T("/xxxx/xxxx/x.gz", false , false , false, true, NULL , NULL);
79 M("/xxxx/xxxx/x.gz", PERF_RECORD_MISC_CPUMODE_UNKNOWN, false);
80 M("/xxxx/xxxx/x.gz", PERF_RECORD_MISC_KERNEL, false);
81 M("/xxxx/xxxx/x.gz", PERF_RECORD_MISC_USER, false);
82
83 /* path alloc_name alloc_ext kmod comp name ext */
84 T("x.gz", true , true , false, true, "x.gz", "gz");
85 T("x.gz", false , true , false, true, NULL , "gz");
86 T("x.gz", true , false , false, true, "x.gz", NULL);
87 T("x.gz", false , false , false, true, NULL , NULL);
88 M("x.gz", PERF_RECORD_MISC_CPUMODE_UNKNOWN, false);
89 M("x.gz", PERF_RECORD_MISC_KERNEL, false);
90 M("x.gz", PERF_RECORD_MISC_USER, false);
91
92 /* path alloc_name alloc_ext kmod comp name ext */
93 T("x.ko.gz", true , true , true, true, "[x]", "gz");
94 T("x.ko.gz", false , true , true, true, NULL , "gz");
95 T("x.ko.gz", true , false , true, true, "[x]", NULL);
96 T("x.ko.gz", false , false , true, true, NULL , NULL);
97 M("x.ko.gz", PERF_RECORD_MISC_CPUMODE_UNKNOWN, true);
98 M("x.ko.gz", PERF_RECORD_MISC_KERNEL, true);
99 M("x.ko.gz", PERF_RECORD_MISC_USER, false);
100 #endif
101
102 /* path alloc_name alloc_ext kmod comp name ext */
103 T("[test_module]", true , true , true, false, "[test_module]", NULL);
104 T("[test_module]", false , true , true, false, NULL , NULL);
105 T("[test_module]", true , false , true, false, "[test_module]", NULL);
106 T("[test_module]", false , false , true, false, NULL , NULL);
107 M("[test_module]", PERF_RECORD_MISC_CPUMODE_UNKNOWN, true);
108 M("[test_module]", PERF_RECORD_MISC_KERNEL, true);
109 M("[test_module]", PERF_RECORD_MISC_USER, false);
110
111 /* path alloc_name alloc_ext kmod comp name ext */
112 T("[test.module]", true , true , true, false, "[test.module]", NULL);
113 T("[test.module]", false , true , true, false, NULL , NULL);
114 T("[test.module]", true , false , true, false, "[test.module]", NULL);
115 T("[test.module]", false , false , true, false, NULL , NULL);
116 M("[test.module]", PERF_RECORD_MISC_CPUMODE_UNKNOWN, true);
117 M("[test.module]", PERF_RECORD_MISC_KERNEL, true);
118 M("[test.module]", PERF_RECORD_MISC_USER, false);
119
120 /* path alloc_name alloc_ext kmod comp name ext */
121 T("[vdso]", true , true , false, false, "[vdso]", NULL);
122 T("[vdso]", false , true , false, false, NULL , NULL);
123 T("[vdso]", true , false , false, false, "[vdso]", NULL);
124 T("[vdso]", false , false , false, false, NULL , NULL);
125 M("[vdso]", PERF_RECORD_MISC_CPUMODE_UNKNOWN, false);
126 M("[vdso]", PERF_RECORD_MISC_KERNEL, false);
127 M("[vdso]", PERF_RECORD_MISC_USER, false);
128
129 /* path alloc_name alloc_ext kmod comp name ext */
130 T("[vsyscall]", true , true , false, false, "[vsyscall]", NULL);
131 T("[vsyscall]", false , true , false, false, NULL , NULL);
132 T("[vsyscall]", true , false , false, false, "[vsyscall]", NULL);
133 T("[vsyscall]", false , false , false, false, NULL , NULL);
134 M("[vsyscall]", PERF_RECORD_MISC_CPUMODE_UNKNOWN, false);
135 M("[vsyscall]", PERF_RECORD_MISC_KERNEL, false);
136 M("[vsyscall]", PERF_RECORD_MISC_USER, false);
137
138 /* path alloc_name alloc_ext kmod comp name ext */
139 T("[kernel.kallsyms]", true , true , false, false, "[kernel.kallsyms]", NULL);
140 T("[kernel.kallsyms]", false , true , false, false, NULL , NULL);
141 T("[kernel.kallsyms]", true , false , false, false, "[kernel.kallsyms]", NULL);
142 T("[kernel.kallsyms]", false , false , false, false, NULL , NULL);
143 M("[kernel.kallsyms]", PERF_RECORD_MISC_CPUMODE_UNKNOWN, false);
144 M("[kernel.kallsyms]", PERF_RECORD_MISC_KERNEL, false);
145 M("[kernel.kallsyms]", PERF_RECORD_MISC_USER, false);
146
147 return 0;
148 }
149