1 /*
2 * Copyright (C) 2012-2013 ProFUSION embedded systems
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <errno.h>
19 #include <inttypes.h>
20 #include <stddef.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25
26 #include <shared/macro.h>
27
28 #include <libkmod/libkmod.h>
29
30 #include "testsuite.h"
31
test_load_resources(const struct test * t)32 static noreturn int test_load_resources(const struct test *t)
33 {
34 struct kmod_ctx *ctx;
35 const char *null_config = NULL;
36 int err;
37
38 ctx = kmod_new(NULL, &null_config);
39 if (ctx == NULL)
40 exit(EXIT_FAILURE);
41
42 kmod_set_log_priority(ctx, 7);
43
44 err = kmod_load_resources(ctx);
45 if (err != 0) {
46 ERR("could not load libkmod resources: %s\n", strerror(-err));
47 exit(EXIT_FAILURE);
48 }
49
50 kmod_unref(ctx);
51
52 exit(EXIT_SUCCESS);
53 }
54 DEFINE_TEST(test_load_resources,
55 .description = "test if kmod_load_resources works (recent modprobe on kernel without modules.builtin.modinfo)",
56 .config = {
57 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-init-load-resources/",
58 [TC_UNAME_R] = "5.6.0",
59 },
60 .need_spawn = true);
61
62 DEFINE_TEST(test_load_resources,
63 .description = "test if kmod_load_resources works with empty modules.builtin.aliases.bin (recent depmod on kernel without modules.builtin.modinfo)",
64 .config = {
65 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-init-load-resources-empty-builtin-aliases-bin/",
66 [TC_UNAME_R] = "5.6.0",
67 },
68 .need_spawn = true);
69
test_initlib(const struct test * t)70 static noreturn int test_initlib(const struct test *t)
71 {
72 struct kmod_ctx *ctx;
73 const char *null_config = NULL;
74
75 ctx = kmod_new(NULL, &null_config);
76 if (ctx == NULL)
77 exit(EXIT_FAILURE);
78
79 kmod_unref(ctx);
80
81 exit(EXIT_SUCCESS);
82 }
83 DEFINE_TEST(test_initlib,
84 .description = "test if libkmod's init function work");
85
test_insert(const struct test * t)86 static noreturn int test_insert(const struct test *t)
87 {
88 struct kmod_ctx *ctx;
89 struct kmod_module *mod;
90 const char *null_config = NULL;
91 int err;
92
93 ctx = kmod_new(NULL, &null_config);
94 if (ctx == NULL)
95 exit(EXIT_FAILURE);
96
97 err = kmod_module_new_from_path(ctx, "/mod-simple.ko", &mod);
98 if (err != 0) {
99 ERR("could not create module from path: %m\n");
100 exit(EXIT_FAILURE);
101 }
102
103 err = kmod_module_insert_module(mod, 0, NULL);
104 if (err != 0) {
105 ERR("could not insert module: %m\n");
106 exit(EXIT_FAILURE);
107 }
108 kmod_unref(ctx);
109
110 exit(EXIT_SUCCESS);
111 }
112 DEFINE_TEST(test_insert,
113 .description = "test if libkmod's insert_module returns ok",
114 .config = {
115 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-init/",
116 [TC_INIT_MODULE_RETCODES] = "bla:1:20",
117 },
118 .modules_loaded = "mod_simple",
119 .need_spawn = true);
120
test_remove(const struct test * t)121 static noreturn int test_remove(const struct test *t)
122 {
123 struct kmod_ctx *ctx;
124 struct kmod_module *mod_simple, *mod_bla;
125 const char *null_config = NULL;
126 int err;
127
128 ctx = kmod_new(NULL, &null_config);
129 if (ctx == NULL)
130 exit(EXIT_FAILURE);
131
132 err = kmod_module_new_from_name(ctx, "mod-simple", &mod_simple);
133 if (err != 0) {
134 ERR("could not create module from name: %s\n", strerror(-err));
135 exit(EXIT_FAILURE);
136 }
137
138 err = kmod_module_new_from_name(ctx, "bla", &mod_bla);
139 if (err != 0) {
140 ERR("could not create module from name: %s\n", strerror(-err));
141 exit(EXIT_FAILURE);
142 }
143
144 err = kmod_module_remove_module(mod_simple, 0);
145 if (err != 0) {
146 ERR("could not remove module: %s\n", strerror(-err));
147 exit(EXIT_FAILURE);
148 }
149
150 err = kmod_module_remove_module(mod_bla, 0);
151 if (err != -ENOENT) {
152 ERR("wrong return code for failure test: %d\n", err);
153 exit(EXIT_FAILURE);
154 }
155
156 kmod_unref(ctx);
157
158 exit(EXIT_SUCCESS);
159 }
160 DEFINE_TEST(test_remove,
161 .description = "test if libkmod's remove_module returns ok",
162 .config = {
163 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-remove/",
164 [TC_DELETE_MODULE_RETCODES] =
165 "mod-simple:0:0:bla:-1:" STRINGIFY(ENOENT),
166 },
167 .need_spawn = true);
168
169 TESTSUITE_MAIN();
170