]> git.ipfire.org Git - thirdparty/kmod.git/blame - testsuite/test-init.c
Install kmod.pc in ${datadir}/pkgconfig
[thirdparty/kmod.git] / testsuite / test-init.c
CommitLineData
e701e381 1/*
e6b0e49b 2 * Copyright (C) 2012-2013 ProFUSION embedded systems
e701e381 3 *
e1b1ab24
LDM
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.
e701e381
LDM
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
e1b1ab24
LDM
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
e701e381 13 *
e1b1ab24 14 * You should have received a copy of the GNU Lesser General Public
dea2dfee 15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
e701e381
LDM
16 */
17
c2e4286b
LDM
18#include <errno.h>
19#include <inttypes.h>
20#include <stddef.h>
eebca81e
LDM
21#include <stdio.h>
22#include <stdlib.h>
eebca81e 23#include <string.h>
eebca81e 24#include <unistd.h>
c2e4286b 25
9b51e13b
LDM
26#include <shared/macro.h>
27
f357866d 28#include <libkmod/libkmod.h>
eebca81e
LDM
29
30#include "testsuite.h"
31
f5434cf5
LDM
32static 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}
54DEFINE_TEST(test_load_resources,
30c9c4d9 55 .description = "test if kmod_load_resources works (recent modprobe on kernel without modules.builtin.modinfo)",
f5434cf5
LDM
56 .config = {
57 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-init-load-resources/",
58 [TC_UNAME_R] = "5.6.0",
59 },
60 .need_spawn = true);
61
30c9c4d9
LDM
62DEFINE_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);
f5434cf5 69
d96ca9c4 70static noreturn int test_initlib(const struct test *t)
eebca81e
LDM
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}
f1155c15 83DEFINE_TEST(test_initlib,
c5d81989 84 .description = "test if libkmod's init function work");
53646fc5 85
d96ca9c4 86static noreturn int test_insert(const struct test *t)
53646fc5
LDM
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
1669be85 97 err = kmod_module_new_from_path(ctx, "/mod-simple.ko", &mod);
53646fc5
LDM
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}
f1155c15 112DEFINE_TEST(test_insert,
53646fc5 113 .description = "test if libkmod's insert_module returns ok",
53646fc5 114 .config = {
ada97199 115 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-init/",
53646fc5
LDM
116 [TC_INIT_MODULE_RETCODES] = "bla:1:20",
117 },
1669be85 118 .modules_loaded = "mod_simple",
c5d81989 119 .need_spawn = true);
eebca81e 120
d96ca9c4 121static noreturn int test_remove(const struct test *t)
f6ef5d6b
LDM
122{
123 struct kmod_ctx *ctx;
1669be85 124 struct kmod_module *mod_simple, *mod_bla;
f6ef5d6b
LDM
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
1669be85 132 err = kmod_module_new_from_name(ctx, "mod-simple", &mod_simple);
9b51e13b
LDM
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);
f6ef5d6b 139 if (err != 0) {
9b51e13b 140 ERR("could not create module from name: %s\n", strerror(-err));
f6ef5d6b
LDM
141 exit(EXIT_FAILURE);
142 }
143
1669be85 144 err = kmod_module_remove_module(mod_simple, 0);
f6ef5d6b 145 if (err != 0) {
9b51e13b 146 ERR("could not remove module: %s\n", strerror(-err));
f6ef5d6b
LDM
147 exit(EXIT_FAILURE);
148 }
9b51e13b
LDM
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
f6ef5d6b
LDM
156 kmod_unref(ctx);
157
158 exit(EXIT_SUCCESS);
159}
f1155c15 160DEFINE_TEST(test_remove,
f6ef5d6b 161 .description = "test if libkmod's remove_module returns ok",
f6ef5d6b 162 .config = {
ada97199 163 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-remove/",
9b51e13b 164 [TC_DELETE_MODULE_RETCODES] =
1669be85 165 "mod-simple:0:0:bla:-1:" STRINGIFY(ENOENT),
f6ef5d6b 166 },
c5d81989 167 .need_spawn = true);
f6ef5d6b 168
43289820 169TESTSUITE_MAIN();