18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci#include <stdbool.h>
38c2ecf20Sopenharmony_ci#include <stdlib.h>
48c2ecf20Sopenharmony_ci#include <string.h>
58c2ecf20Sopenharmony_ci#include "tests.h"
68c2ecf20Sopenharmony_ci#include "dso.h"
78c2ecf20Sopenharmony_ci#include "debug.h"
88c2ecf20Sopenharmony_ci#include "event.h"
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_cistatic int test(const char *path, bool alloc_name, bool kmod,
118c2ecf20Sopenharmony_ci		int comp, const char *name)
128c2ecf20Sopenharmony_ci{
138c2ecf20Sopenharmony_ci	struct kmod_path m;
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci	memset(&m, 0x0, sizeof(m));
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci	TEST_ASSERT_VAL("kmod_path__parse",
188c2ecf20Sopenharmony_ci			!__kmod_path__parse(&m, path, alloc_name));
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci	pr_debug("%s - alloc name %d, kmod %d, comp %d, name '%s'\n",
218c2ecf20Sopenharmony_ci		 path, alloc_name, m.kmod, m.comp, m.name);
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci	TEST_ASSERT_VAL("wrong kmod", m.kmod == kmod);
248c2ecf20Sopenharmony_ci	TEST_ASSERT_VAL("wrong comp", m.comp == comp);
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci	if (name)
278c2ecf20Sopenharmony_ci		TEST_ASSERT_VAL("wrong name", m.name && !strcmp(name, m.name));
288c2ecf20Sopenharmony_ci	else
298c2ecf20Sopenharmony_ci		TEST_ASSERT_VAL("wrong name", !m.name);
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci	free(m.name);
328c2ecf20Sopenharmony_ci	return 0;
338c2ecf20Sopenharmony_ci}
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cistatic int test_is_kernel_module(const char *path, int cpumode, bool expect)
368c2ecf20Sopenharmony_ci{
378c2ecf20Sopenharmony_ci	TEST_ASSERT_VAL("is_kernel_module",
388c2ecf20Sopenharmony_ci			(!!is_kernel_module(path, cpumode)) == (!!expect));
398c2ecf20Sopenharmony_ci	pr_debug("%s (cpumode: %d) - is_kernel_module: %s\n",
408c2ecf20Sopenharmony_ci			path, cpumode, expect ? "true" : "false");
418c2ecf20Sopenharmony_ci	return 0;
428c2ecf20Sopenharmony_ci}
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci#define T(path, an, k, c, n) \
458c2ecf20Sopenharmony_ci	TEST_ASSERT_VAL("failed", !test(path, an, k, c, n))
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci#define M(path, c, e) \
488c2ecf20Sopenharmony_ci	TEST_ASSERT_VAL("failed", !test_is_kernel_module(path, c, e))
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ciint test__kmod_path__parse(struct test *t __maybe_unused, int subtest __maybe_unused)
518c2ecf20Sopenharmony_ci{
528c2ecf20Sopenharmony_ci	/* path                alloc_name  kmod  comp   name   */
538c2ecf20Sopenharmony_ci	T("/xxxx/xxxx/x-x.ko", true      , true, 0    , "[x_x]");
548c2ecf20Sopenharmony_ci	T("/xxxx/xxxx/x-x.ko", false     , true, 0    , NULL   );
558c2ecf20Sopenharmony_ci	T("/xxxx/xxxx/x-x.ko", true      , true, 0    , "[x_x]");
568c2ecf20Sopenharmony_ci	T("/xxxx/xxxx/x-x.ko", false     , true, 0    , NULL   );
578c2ecf20Sopenharmony_ci	M("/xxxx/xxxx/x-x.ko", PERF_RECORD_MISC_CPUMODE_UNKNOWN, true);
588c2ecf20Sopenharmony_ci	M("/xxxx/xxxx/x-x.ko", PERF_RECORD_MISC_KERNEL, true);
598c2ecf20Sopenharmony_ci	M("/xxxx/xxxx/x-x.ko", PERF_RECORD_MISC_USER, false);
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci#ifdef HAVE_ZLIB_SUPPORT
628c2ecf20Sopenharmony_ci	/* path                alloc_name   kmod  comp  name  */
638c2ecf20Sopenharmony_ci	T("/xxxx/xxxx/x.ko.gz", true     , true, 1   , "[x]");
648c2ecf20Sopenharmony_ci	T("/xxxx/xxxx/x.ko.gz", false    , true, 1   , NULL );
658c2ecf20Sopenharmony_ci	T("/xxxx/xxxx/x.ko.gz", true     , true, 1   , "[x]");
668c2ecf20Sopenharmony_ci	T("/xxxx/xxxx/x.ko.gz", false    , true, 1   , NULL );
678c2ecf20Sopenharmony_ci	M("/xxxx/xxxx/x.ko.gz", PERF_RECORD_MISC_CPUMODE_UNKNOWN, true);
688c2ecf20Sopenharmony_ci	M("/xxxx/xxxx/x.ko.gz", PERF_RECORD_MISC_KERNEL, true);
698c2ecf20Sopenharmony_ci	M("/xxxx/xxxx/x.ko.gz", PERF_RECORD_MISC_USER, false);
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	/* path              alloc_name  kmod   comp  name  */
728c2ecf20Sopenharmony_ci	T("/xxxx/xxxx/x.gz", true      , false, 1   , "x.gz");
738c2ecf20Sopenharmony_ci	T("/xxxx/xxxx/x.gz", false     , false, 1   , NULL  );
748c2ecf20Sopenharmony_ci	T("/xxxx/xxxx/x.gz", true      , false, 1   , "x.gz");
758c2ecf20Sopenharmony_ci	T("/xxxx/xxxx/x.gz", false     , false, 1   , NULL  );
768c2ecf20Sopenharmony_ci	M("/xxxx/xxxx/x.gz", PERF_RECORD_MISC_CPUMODE_UNKNOWN, false);
778c2ecf20Sopenharmony_ci	M("/xxxx/xxxx/x.gz", PERF_RECORD_MISC_KERNEL, false);
788c2ecf20Sopenharmony_ci	M("/xxxx/xxxx/x.gz", PERF_RECORD_MISC_USER, false);
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	/* path   alloc_name  kmod   comp  name   */
818c2ecf20Sopenharmony_ci	T("x.gz", true      , false, 1   , "x.gz");
828c2ecf20Sopenharmony_ci	T("x.gz", false     , false, 1   , NULL  );
838c2ecf20Sopenharmony_ci	T("x.gz", true      , false, 1   , "x.gz");
848c2ecf20Sopenharmony_ci	T("x.gz", false     , false, 1   , NULL  );
858c2ecf20Sopenharmony_ci	M("x.gz", PERF_RECORD_MISC_CPUMODE_UNKNOWN, false);
868c2ecf20Sopenharmony_ci	M("x.gz", PERF_RECORD_MISC_KERNEL, false);
878c2ecf20Sopenharmony_ci	M("x.gz", PERF_RECORD_MISC_USER, false);
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	/* path      alloc_name  kmod  comp  name  */
908c2ecf20Sopenharmony_ci	T("x.ko.gz", true      , true, 1   , "[x]");
918c2ecf20Sopenharmony_ci	T("x.ko.gz", false     , true, 1   , NULL );
928c2ecf20Sopenharmony_ci	T("x.ko.gz", true      , true, 1   , "[x]");
938c2ecf20Sopenharmony_ci	T("x.ko.gz", false     , true, 1   , NULL );
948c2ecf20Sopenharmony_ci	M("x.ko.gz", PERF_RECORD_MISC_CPUMODE_UNKNOWN, true);
958c2ecf20Sopenharmony_ci	M("x.ko.gz", PERF_RECORD_MISC_KERNEL, true);
968c2ecf20Sopenharmony_ci	M("x.ko.gz", PERF_RECORD_MISC_USER, false);
978c2ecf20Sopenharmony_ci#endif
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	/* path            alloc_name  kmod  comp   name           */
1008c2ecf20Sopenharmony_ci	T("[test_module]", true      , true, false, "[test_module]");
1018c2ecf20Sopenharmony_ci	T("[test_module]", false     , true, false, NULL           );
1028c2ecf20Sopenharmony_ci	T("[test_module]", true      , true, false, "[test_module]");
1038c2ecf20Sopenharmony_ci	T("[test_module]", false     , true, false, NULL           );
1048c2ecf20Sopenharmony_ci	M("[test_module]", PERF_RECORD_MISC_CPUMODE_UNKNOWN, true);
1058c2ecf20Sopenharmony_ci	M("[test_module]", PERF_RECORD_MISC_KERNEL, true);
1068c2ecf20Sopenharmony_ci	M("[test_module]", PERF_RECORD_MISC_USER, false);
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	/* path            alloc_name  kmod  comp   name           */
1098c2ecf20Sopenharmony_ci	T("[test.module]", true      , true, false, "[test.module]");
1108c2ecf20Sopenharmony_ci	T("[test.module]", false     , true, false, NULL           );
1118c2ecf20Sopenharmony_ci	T("[test.module]", true      , true, false, "[test.module]");
1128c2ecf20Sopenharmony_ci	T("[test.module]", false     , true, false, NULL           );
1138c2ecf20Sopenharmony_ci	M("[test.module]", PERF_RECORD_MISC_CPUMODE_UNKNOWN, true);
1148c2ecf20Sopenharmony_ci	M("[test.module]", PERF_RECORD_MISC_KERNEL, true);
1158c2ecf20Sopenharmony_ci	M("[test.module]", PERF_RECORD_MISC_USER, false);
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	/* path     alloc_name  kmod   comp   name    */
1188c2ecf20Sopenharmony_ci	T("[vdso]", true      , false, false, "[vdso]");
1198c2ecf20Sopenharmony_ci	T("[vdso]", false     , false, false, NULL    );
1208c2ecf20Sopenharmony_ci	T("[vdso]", true      , false, false, "[vdso]");
1218c2ecf20Sopenharmony_ci	T("[vdso]", false     , false, false, NULL    );
1228c2ecf20Sopenharmony_ci	M("[vdso]", PERF_RECORD_MISC_CPUMODE_UNKNOWN, false);
1238c2ecf20Sopenharmony_ci	M("[vdso]", PERF_RECORD_MISC_KERNEL, false);
1248c2ecf20Sopenharmony_ci	M("[vdso]", PERF_RECORD_MISC_USER, false);
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	T("[vdso32]", true      , false, false, "[vdso32]");
1278c2ecf20Sopenharmony_ci	T("[vdso32]", false     , false, false, NULL      );
1288c2ecf20Sopenharmony_ci	T("[vdso32]", true      , false, false, "[vdso32]");
1298c2ecf20Sopenharmony_ci	T("[vdso32]", false     , false, false, NULL      );
1308c2ecf20Sopenharmony_ci	M("[vdso32]", PERF_RECORD_MISC_CPUMODE_UNKNOWN, false);
1318c2ecf20Sopenharmony_ci	M("[vdso32]", PERF_RECORD_MISC_KERNEL, false);
1328c2ecf20Sopenharmony_ci	M("[vdso32]", PERF_RECORD_MISC_USER, false);
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	T("[vdsox32]", true      , false, false, "[vdsox32]");
1358c2ecf20Sopenharmony_ci	T("[vdsox32]", false     , false, false, NULL       );
1368c2ecf20Sopenharmony_ci	T("[vdsox32]", true      , false, false, "[vdsox32]");
1378c2ecf20Sopenharmony_ci	T("[vdsox32]", false     , false, false, NULL       );
1388c2ecf20Sopenharmony_ci	M("[vdsox32]", PERF_RECORD_MISC_CPUMODE_UNKNOWN, false);
1398c2ecf20Sopenharmony_ci	M("[vdsox32]", PERF_RECORD_MISC_KERNEL, false);
1408c2ecf20Sopenharmony_ci	M("[vdsox32]", PERF_RECORD_MISC_USER, false);
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	/* path         alloc_name  kmod   comp   name        */
1438c2ecf20Sopenharmony_ci	T("[vsyscall]", true      , false, false, "[vsyscall]");
1448c2ecf20Sopenharmony_ci	T("[vsyscall]", false     , false, false, NULL        );
1458c2ecf20Sopenharmony_ci	T("[vsyscall]", true      , false, false, "[vsyscall]");
1468c2ecf20Sopenharmony_ci	T("[vsyscall]", false     , false, false, NULL        );
1478c2ecf20Sopenharmony_ci	M("[vsyscall]", PERF_RECORD_MISC_CPUMODE_UNKNOWN, false);
1488c2ecf20Sopenharmony_ci	M("[vsyscall]", PERF_RECORD_MISC_KERNEL, false);
1498c2ecf20Sopenharmony_ci	M("[vsyscall]", PERF_RECORD_MISC_USER, false);
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	/* path                alloc_name  kmod   comp   name      */
1528c2ecf20Sopenharmony_ci	T("[kernel.kallsyms]", true      , false, false, "[kernel.kallsyms]");
1538c2ecf20Sopenharmony_ci	T("[kernel.kallsyms]", false     , false, false, NULL               );
1548c2ecf20Sopenharmony_ci	T("[kernel.kallsyms]", true      , false, false, "[kernel.kallsyms]");
1558c2ecf20Sopenharmony_ci	T("[kernel.kallsyms]", false     , false, false, NULL               );
1568c2ecf20Sopenharmony_ci	M("[kernel.kallsyms]", PERF_RECORD_MISC_CPUMODE_UNKNOWN, false);
1578c2ecf20Sopenharmony_ci	M("[kernel.kallsyms]", PERF_RECORD_MISC_KERNEL, false);
1588c2ecf20Sopenharmony_ci	M("[kernel.kallsyms]", PERF_RECORD_MISC_USER, false);
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	return 0;
1618c2ecf20Sopenharmony_ci}
162