1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2020 Viresh Kumar <viresh.kumar@linaro.org>
4f08c3bdfSopenharmony_ci */
5f08c3bdfSopenharmony_ci
6f08c3bdfSopenharmony_ci/*\
7f08c3bdfSopenharmony_ci * [Description]
8f08c3bdfSopenharmony_ci *
9f08c3bdfSopenharmony_ci * Basic finit_module() tests.
10f08c3bdfSopenharmony_ci *
11f08c3bdfSopenharmony_ci * [Algorithm]
12f08c3bdfSopenharmony_ci *
13f08c3bdfSopenharmony_ci * Inserts a simple module after opening and mmaping the module file.
14f08c3bdfSopenharmony_ci */
15f08c3bdfSopenharmony_ci
16f08c3bdfSopenharmony_ci#include <errno.h>
17f08c3bdfSopenharmony_ci#include "lapi/init_module.h"
18f08c3bdfSopenharmony_ci#include "tst_module.h"
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_ci#define MODULE_NAME	"finit_module.ko"
21f08c3bdfSopenharmony_ci
22f08c3bdfSopenharmony_cistatic int fd;
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_cistatic char *mod_path;
25f08c3bdfSopenharmony_ci
26f08c3bdfSopenharmony_cistatic void setup(void)
27f08c3bdfSopenharmony_ci{
28f08c3bdfSopenharmony_ci	tst_module_exists(MODULE_NAME, &mod_path);
29f08c3bdfSopenharmony_ci
30f08c3bdfSopenharmony_ci	fd = SAFE_OPEN(mod_path, O_RDONLY|O_CLOEXEC);
31f08c3bdfSopenharmony_ci}
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_cistatic void run(void)
34f08c3bdfSopenharmony_ci{
35f08c3bdfSopenharmony_ci	TST_EXP_PASS(finit_module(fd, "status=valid", 0));
36f08c3bdfSopenharmony_ci	if (!TST_PASS)
37f08c3bdfSopenharmony_ci		return;
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_ci	tst_module_unload(MODULE_NAME);
40f08c3bdfSopenharmony_ci}
41f08c3bdfSopenharmony_ci
42f08c3bdfSopenharmony_cistatic void cleanup(void)
43f08c3bdfSopenharmony_ci{
44f08c3bdfSopenharmony_ci	SAFE_CLOSE(fd);
45f08c3bdfSopenharmony_ci}
46f08c3bdfSopenharmony_ci
47f08c3bdfSopenharmony_cistatic struct tst_test test = {
48f08c3bdfSopenharmony_ci	.test_all = run,
49f08c3bdfSopenharmony_ci	.setup = setup,
50f08c3bdfSopenharmony_ci	.cleanup = cleanup,
51f08c3bdfSopenharmony_ci	.needs_root = 1,
52f08c3bdfSopenharmony_ci	/* lockdown and SecureBoot requires signed modules */
53f08c3bdfSopenharmony_ci	.skip_in_lockdown = 1,
54f08c3bdfSopenharmony_ci	.skip_in_secureboot = 1,
55f08c3bdfSopenharmony_ci};
56