]> git.ipfire.org Git - thirdparty/kmod.git/blame - testsuite/test-init.c
testsuite: simplify syscall number handling
[thirdparty/kmod.git] / testsuite / test-init.c
CommitLineData
b5a2cd07 1// SPDX-License-Identifier: LGPL-2.1-or-later
e701e381 2/*
e6b0e49b 3 * Copyright (C) 2012-2013 ProFUSION embedded systems
e701e381
LDM
4 */
5
c2e4286b
LDM
6#include <errno.h>
7#include <inttypes.h>
8#include <stddef.h>
eebca81e
LDM
9#include <stdio.h>
10#include <stdlib.h>
eebca81e 11#include <string.h>
eebca81e 12#include <unistd.h>
c2e4286b 13
9b51e13b
LDM
14#include <shared/macro.h>
15
f357866d 16#include <libkmod/libkmod.h>
eebca81e
LDM
17
18#include "testsuite.h"
19
6d0d80ac 20static int test_load_resources(void)
f5434cf5
LDM
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)
6d0d80ac 28 return EXIT_FAILURE;
f5434cf5
LDM
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));
6d0d80ac 35 return EXIT_FAILURE;
f5434cf5
LDM
36 }
37
38 kmod_unref(ctx);
39
6d0d80ac 40 return EXIT_SUCCESS;
f5434cf5 41}
c5c36b61
EV
42DEFINE_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 = {
f5434cf5
LDM
47 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-init-load-resources/",
48 [TC_UNAME_R] = "5.6.0",
c5c36b61 49 });
f5434cf5 50
c5c36b61
EV
51DEFINE_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/",
30c9c4d9 58 [TC_UNAME_R] = "5.6.0",
c5c36b61 59 });
f5434cf5 60
6d0d80ac 61static int test_initlib(void)
eebca81e
LDM
62{
63 struct kmod_ctx *ctx;
64 const char *null_config = NULL;
65
66 ctx = kmod_new(NULL, &null_config);
67 if (ctx == NULL)
6d0d80ac 68 return EXIT_FAILURE;
eebca81e
LDM
69
70 kmod_unref(ctx);
71
6d0d80ac 72 return EXIT_SUCCESS;
eebca81e 73}
4c760f7e 74DEFINE_TEST(test_initlib, .description = "test if libkmod's init function work");
53646fc5 75
6d0d80ac 76static int test_insert(void)
53646fc5
LDM
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)
6d0d80ac 85 return EXIT_FAILURE;
53646fc5 86
1669be85 87 err = kmod_module_new_from_path(ctx, "/mod-simple.ko", &mod);
53646fc5 88 if (err != 0) {
e8e41223 89 ERR("could not create module from path: %s\n", strerror(-err));
6d0d80ac 90 return EXIT_FAILURE;
53646fc5
LDM
91 }
92
93 err = kmod_module_insert_module(mod, 0, NULL);
94 if (err != 0) {
e8e41223 95 ERR("could not insert module: %s\n", strerror(-err));
6d0d80ac 96 return EXIT_FAILURE;
53646fc5 97 }
5611bf6e 98 kmod_module_unref(mod);
53646fc5
LDM
99 kmod_unref(ctx);
100
6d0d80ac 101 return EXIT_SUCCESS;
53646fc5 102}
f1155c15 103DEFINE_TEST(test_insert,
53646fc5 104 .description = "test if libkmod's insert_module returns ok",
53646fc5 105 .config = {
ada97199 106 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-init/",
53646fc5
LDM
107 [TC_INIT_MODULE_RETCODES] = "bla:1:20",
108 },
c5c36b61 109 .modules_loaded = "mod_simple");
eebca81e 110
6d0d80ac 111static int test_remove(void)
f6ef5d6b
LDM
112{
113 struct kmod_ctx *ctx;
1669be85 114 struct kmod_module *mod_simple, *mod_bla;
f6ef5d6b
LDM
115 const char *null_config = NULL;
116 int err;
117
118 ctx = kmod_new(NULL, &null_config);
119 if (ctx == NULL)
6d0d80ac 120 return EXIT_FAILURE;
f6ef5d6b 121
1669be85 122 err = kmod_module_new_from_name(ctx, "mod-simple", &mod_simple);
9b51e13b
LDM
123 if (err != 0) {
124 ERR("could not create module from name: %s\n", strerror(-err));
6d0d80ac 125 return EXIT_FAILURE;
9b51e13b
LDM
126 }
127
128 err = kmod_module_new_from_name(ctx, "bla", &mod_bla);
f6ef5d6b 129 if (err != 0) {
9b51e13b 130 ERR("could not create module from name: %s\n", strerror(-err));
6d0d80ac 131 return EXIT_FAILURE;
f6ef5d6b
LDM
132 }
133
1669be85 134 err = kmod_module_remove_module(mod_simple, 0);
f6ef5d6b 135 if (err != 0) {
9b51e13b 136 ERR("could not remove module: %s\n", strerror(-err));
6d0d80ac 137 return EXIT_FAILURE;
f6ef5d6b 138 }
9b51e13b
LDM
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);
6d0d80ac 143 return EXIT_FAILURE;
9b51e13b
LDM
144 }
145
5611bf6e
EV
146 kmod_module_unref(mod_bla);
147 kmod_module_unref(mod_simple);
f6ef5d6b
LDM
148 kmod_unref(ctx);
149
6d0d80ac 150 return EXIT_SUCCESS;
f6ef5d6b 151}
c5c36b61
EV
152DEFINE_TEST(
153 test_remove, .description = "test if libkmod's remove_module returns ok",
f6ef5d6b 154 .config = {
ada97199 155 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-remove/",
c5c36b61
EV
156 [TC_DELETE_MODULE_RETCODES] = "mod-simple:0:0:bla:-1:" STRINGIFY(ENOENT),
157 });
f6ef5d6b 158
43289820 159TESTSUITE_MAIN();