]> git.ipfire.org Git - thirdparty/kmod.git/blob - testsuite/test-init.c
testsuite: Add assert_return
[thirdparty/kmod.git] / testsuite / test-init.c
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, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <stddef.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <unistd.h>
25 #include <libkmod.h>
26
27 #include "testsuite.h"
28
29 static noreturn int test_initlib(const struct test *t)
30 {
31 struct kmod_ctx *ctx;
32 const char *null_config = NULL;
33
34 ctx = kmod_new(NULL, &null_config);
35 if (ctx == NULL)
36 exit(EXIT_FAILURE);
37
38 kmod_unref(ctx);
39
40 exit(EXIT_SUCCESS);
41 }
42 static DEFINE_TEST(test_initlib,
43 .description = "test if libkmod's init function work");
44
45 static noreturn int test_insert(const struct test *t)
46 {
47 struct kmod_ctx *ctx;
48 struct kmod_module *mod;
49 const char *null_config = NULL;
50 int err;
51
52 ctx = kmod_new(NULL, &null_config);
53 if (ctx == NULL)
54 exit(EXIT_FAILURE);
55
56 err = kmod_module_new_from_path(ctx, "/ext4-x86_64.ko", &mod);
57 if (err != 0) {
58 ERR("could not create module from path: %m\n");
59 exit(EXIT_FAILURE);
60 }
61
62 err = kmod_module_insert_module(mod, 0, NULL);
63 if (err != 0) {
64 ERR("could not insert module: %m\n");
65 exit(EXIT_FAILURE);
66 }
67 kmod_unref(ctx);
68
69 exit(EXIT_SUCCESS);
70 }
71 static DEFINE_TEST(test_insert,
72 .description = "test if libkmod's insert_module returns ok",
73 .config = {
74 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-init/",
75 [TC_INIT_MODULE_RETCODES] = "bla:1:20",
76 },
77 .modules_loaded = "ext4",
78 .need_spawn = true);
79
80 static noreturn int test_remove(const struct test *t)
81 {
82 struct kmod_ctx *ctx;
83 struct kmod_module *mod;
84 const char *null_config = NULL;
85 int err;
86
87 ctx = kmod_new(NULL, &null_config);
88 if (ctx == NULL)
89 exit(EXIT_FAILURE);
90
91 err = kmod_module_new_from_name(ctx, "ext4", &mod);
92 if (err != 0) {
93 ERR("could not create module from name: %m\n");
94 exit(EXIT_FAILURE);
95 }
96
97 err = kmod_module_remove_module(mod, 0);
98 if (err != 0) {
99 ERR("could not remove module: %m\n");
100 exit(EXIT_FAILURE);
101 }
102 kmod_unref(ctx);
103
104 exit(EXIT_SUCCESS);
105 }
106 static DEFINE_TEST(test_remove,
107 .description = "test if libkmod's remove_module returns ok",
108 .config = {
109 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-remove/",
110 [TC_DELETE_MODULE_RETCODES] = "bla:1:20",
111 },
112 .need_spawn = true);
113
114 static const struct test *tests[] = {
115 &stest_initlib,
116 &stest_insert,
117 &stest_remove,
118 NULL,
119 };
120
121 TESTSUITE_MAIN(tests);