libkmod: fix return error when opening index
[platform/upstream/kmod.git] / testsuite / test-initstate.c
1 /*
2  * Copyright (C) 2015  Intel Corporation. All rights reserved.
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 <stdarg.h>
21 #include <stddef.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <syslog.h>
26 #include <unistd.h>
27
28 #include <libkmod/libkmod.h>
29
30 #include <shared/macro.h>
31
32 #include "testsuite.h"
33
34
35 static noreturn int test_initstate_from_lookup(const struct test *t)
36 {
37         struct kmod_ctx *ctx;
38         struct kmod_list *list = NULL;
39         struct kmod_module *mod;
40         const char *null_config = NULL;
41         int err, r;
42
43         ctx = kmod_new(NULL, &null_config);
44         if (ctx == NULL)
45                 exit(EXIT_FAILURE);
46
47         err = kmod_module_new_from_lookup(ctx, "fake-builtin", &list);
48         if (err != 0) {
49                 ERR("could not create module from lookup: %s\n", strerror(-err));
50                 exit(EXIT_FAILURE);
51         }
52
53         if (!list) {
54                 ERR("could not create module from lookup: module not found: fake-builtin\n");
55                 exit(EXIT_FAILURE);
56         }
57
58         mod = kmod_module_get_module(list);
59
60         r = kmod_module_get_initstate(mod);
61         if (r != KMOD_MODULE_BUILTIN) {
62                 ERR("module should have builtin state but is: %s\n",
63                     kmod_module_initstate_str(r));
64                 exit(EXIT_FAILURE);
65         }
66
67         kmod_module_unref(mod);
68         kmod_module_unref_list(list);
69         kmod_unref(ctx);
70
71         exit(EXIT_SUCCESS);
72 }
73 DEFINE_TEST(test_initstate_from_lookup,
74         .description = "test if libkmod return correct initstate for builtin module from lookup",
75         .config = {
76                 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-initstate",
77                 [TC_UNAME_R] = "4.4.4",
78         },
79         .need_spawn = true);
80
81 static noreturn int test_initstate_from_name(const struct test *t)
82 {
83         struct kmod_ctx *ctx;
84         struct kmod_module *mod = NULL;
85         const char *null_config = NULL;
86         int err, r;
87
88         ctx = kmod_new(NULL, &null_config);
89         if (ctx == NULL)
90                 exit(EXIT_FAILURE);
91
92         err = kmod_module_new_from_name(ctx, "fake-builtin", &mod);
93         if (err != 0) {
94                 ERR("could not create module from lookup: %s\n", strerror(-err));
95                 exit(EXIT_FAILURE);
96         }
97
98         if (!mod) {
99                 ERR("could not create module from lookup: module not found: fake-builtin\n");
100                 exit(EXIT_FAILURE);
101         }
102
103         r = kmod_module_get_initstate(mod);
104         if (r != KMOD_MODULE_BUILTIN) {
105                 ERR("module should have builtin state but is: %s\n",
106                     kmod_module_initstate_str(r));
107                 exit(EXIT_FAILURE);
108         }
109
110         kmod_module_unref(mod);
111         kmod_unref(ctx);
112
113         exit(EXIT_SUCCESS);
114 }
115 DEFINE_TEST(test_initstate_from_name,
116         .description = "test if libkmod return correct initstate for builtin module from name",
117         .config = {
118                 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-initstate",
119                 [TC_UNAME_R] = "4.4.4",
120         },
121         .need_spawn = true);
122
123
124
125
126 TESTSUITE_MAIN();