]> git.ipfire.org Git - thirdparty/kmod.git/blob - testsuite/test-init.c
fb4cdce41a2782c9a94b54b501a167297e4c7a8d
[thirdparty/kmod.git] / testsuite / test-init.c
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /*
3 * Copyright (C) 2012-2013 ProFUSION embedded systems
4 */
5
6 #include <errno.h>
7 #include <inttypes.h>
8 #include <stddef.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
13
14 #include <shared/macro.h>
15
16 #include <libkmod/libkmod.h>
17
18 #include "testsuite.h"
19
20 static int test_load_resources(void)
21 {
22 struct kmod_ctx *ctx;
23 const char *null_config = NULL;
24 int err;
25
26 ctx = kmod_new(NULL, &null_config);
27 if (ctx == NULL)
28 return EXIT_FAILURE;
29
30 kmod_set_log_priority(ctx, 7);
31
32 err = kmod_load_resources(ctx);
33 if (err != 0) {
34 ERR("could not load libkmod resources: %s\n", strerror(-err));
35 return EXIT_FAILURE;
36 }
37
38 kmod_unref(ctx);
39
40 return EXIT_SUCCESS;
41 }
42 DEFINE_TEST_WITH_FUNC(
43 test_load_resource1, test_load_resources,
44 .description =
45 "test if kmod_load_resources works (recent modprobe on kernel without modules.builtin.modinfo)",
46 .config = {
47 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-init-load-resources/",
48 [TC_UNAME_R] = "5.6.0",
49 });
50
51 DEFINE_TEST_WITH_FUNC(
52 test_load_resource2, test_load_resources,
53 .description =
54 "test if kmod_load_resources works with empty modules.builtin.aliases.bin (recent depmod on kernel without modules.builtin.modinfo)",
55 .config = {
56 [TC_ROOTFS] = TESTSUITE_ROOTFS
57 "test-init-load-resources-empty-builtin-aliases-bin/",
58 [TC_UNAME_R] = "5.6.0",
59 });
60
61 static int test_initlib(void)
62 {
63 struct kmod_ctx *ctx;
64 const char *null_config = NULL;
65
66 ctx = kmod_new(NULL, &null_config);
67 if (ctx == NULL)
68 return EXIT_FAILURE;
69
70 kmod_unref(ctx);
71
72 return EXIT_SUCCESS;
73 }
74 DEFINE_TEST(test_initlib, .description = "test if libkmod's init function work");
75
76 static int test_insert(void)
77 {
78 struct kmod_ctx *ctx;
79 struct kmod_module *mod;
80 const char *null_config = NULL;
81 int err;
82
83 ctx = kmod_new(NULL, &null_config);
84 if (ctx == NULL)
85 return EXIT_FAILURE;
86
87 err = kmod_module_new_from_path(ctx, "/mod-simple.ko", &mod);
88 if (err != 0) {
89 ERR("could not create module from path: %s\n", strerror(-err));
90 return EXIT_FAILURE;
91 }
92
93 err = kmod_module_insert_module(mod, 0, NULL);
94 if (err != 0) {
95 ERR("could not insert module: %s\n", strerror(-err));
96 return EXIT_FAILURE;
97 }
98 kmod_module_unref(mod);
99 kmod_unref(ctx);
100
101 return EXIT_SUCCESS;
102 }
103 DEFINE_TEST(test_insert,
104 .description = "test if libkmod's insert_module returns ok",
105 .config = {
106 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-init/",
107 [TC_INIT_MODULE_RETCODES] = "bla:1:20",
108 },
109 .modules_loaded = "mod_simple");
110
111 static int test_remove(void)
112 {
113 struct kmod_ctx *ctx;
114 struct kmod_module *mod_simple, *mod_bla;
115 const char *null_config = NULL;
116 int err;
117
118 ctx = kmod_new(NULL, &null_config);
119 if (ctx == NULL)
120 return EXIT_FAILURE;
121
122 err = kmod_module_new_from_name(ctx, "mod-simple", &mod_simple);
123 if (err != 0) {
124 ERR("could not create module from name: %s\n", strerror(-err));
125 return EXIT_FAILURE;
126 }
127
128 err = kmod_module_new_from_name(ctx, "bla", &mod_bla);
129 if (err != 0) {
130 ERR("could not create module from name: %s\n", strerror(-err));
131 return EXIT_FAILURE;
132 }
133
134 err = kmod_module_remove_module(mod_simple, 0);
135 if (err != 0) {
136 ERR("could not remove module: %s\n", strerror(-err));
137 return EXIT_FAILURE;
138 }
139
140 err = kmod_module_remove_module(mod_bla, 0);
141 if (err != -ENOENT) {
142 ERR("wrong return code for failure test: %d\n", err);
143 return EXIT_FAILURE;
144 }
145
146 kmod_module_unref(mod_bla);
147 kmod_module_unref(mod_simple);
148 kmod_unref(ctx);
149
150 return EXIT_SUCCESS;
151 }
152 DEFINE_TEST(
153 test_remove, .description = "test if libkmod's remove_module returns ok",
154 .config = {
155 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-remove/",
156 [TC_DELETE_MODULE_RETCODES] = "mod-simple:0:0:bla:-1:" STRINGIFY(ENOENT),
157 });
158
159 TESTSUITE_MAIN();