libkmod: fix return error when opening index
[platform/upstream/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, 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
32 static noreturn int test_initlib(const struct test *t)
33 {
34         struct kmod_ctx *ctx;
35         const char *null_config = NULL;
36
37         ctx = kmod_new(NULL, &null_config);
38         if (ctx == NULL)
39                 exit(EXIT_FAILURE);
40
41         kmod_unref(ctx);
42
43         exit(EXIT_SUCCESS);
44 }
45 DEFINE_TEST(test_initlib,
46                 .description = "test if libkmod's init function work");
47
48 static noreturn int test_insert(const struct test *t)
49 {
50         struct kmod_ctx *ctx;
51         struct kmod_module *mod;
52         const char *null_config = NULL;
53         int err;
54
55         ctx = kmod_new(NULL, &null_config);
56         if (ctx == NULL)
57                 exit(EXIT_FAILURE);
58
59         err = kmod_module_new_from_path(ctx, "/mod-simple.ko", &mod);
60         if (err != 0) {
61                 ERR("could not create module from path: %m\n");
62                 exit(EXIT_FAILURE);
63         }
64
65         err = kmod_module_insert_module(mod, 0, NULL);
66         if (err != 0) {
67                 ERR("could not insert module: %m\n");
68                 exit(EXIT_FAILURE);
69         }
70         kmod_unref(ctx);
71
72         exit(EXIT_SUCCESS);
73 }
74 DEFINE_TEST(test_insert,
75         .description = "test if libkmod's insert_module returns ok",
76         .config = {
77                 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-init/",
78                 [TC_INIT_MODULE_RETCODES] = "bla:1:20",
79         },
80         .modules_loaded = "mod_simple",
81         .need_spawn = true);
82
83 static noreturn int test_remove(const struct test *t)
84 {
85         struct kmod_ctx *ctx;
86         struct kmod_module *mod_simple, *mod_bla;
87         const char *null_config = NULL;
88         int err;
89
90         ctx = kmod_new(NULL, &null_config);
91         if (ctx == NULL)
92                 exit(EXIT_FAILURE);
93
94         err = kmod_module_new_from_name(ctx, "mod-simple", &mod_simple);
95         if (err != 0) {
96                 ERR("could not create module from name: %s\n", strerror(-err));
97                 exit(EXIT_FAILURE);
98         }
99
100         err = kmod_module_new_from_name(ctx, "bla", &mod_bla);
101         if (err != 0) {
102                 ERR("could not create module from name: %s\n", strerror(-err));
103                 exit(EXIT_FAILURE);
104         }
105
106         err = kmod_module_remove_module(mod_simple, 0);
107         if (err != 0) {
108                 ERR("could not remove module: %s\n", strerror(-err));
109                 exit(EXIT_FAILURE);
110         }
111
112         err = kmod_module_remove_module(mod_bla, 0);
113         if (err != -ENOENT) {
114                 ERR("wrong return code for failure test: %d\n", err);
115                 exit(EXIT_FAILURE);
116         }
117
118         kmod_unref(ctx);
119
120         exit(EXIT_SUCCESS);
121 }
122 DEFINE_TEST(test_remove,
123         .description = "test if libkmod's remove_module returns ok",
124         .config = {
125                 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-remove/",
126                 [TC_DELETE_MODULE_RETCODES] =
127                         "mod-simple:0:0:bla:-1:" STRINGIFY(ENOENT),
128         },
129         .need_spawn = true);
130
131 TESTSUITE_MAIN();