libkmod: fix return error when opening index
[platform/upstream/kmod.git] / testsuite / test-modprobe.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 "testsuite.h"
27
28 static noreturn int modprobe_show_depends(const struct test *t)
29 {
30         const char *progname = ABS_TOP_BUILDDIR "/tools/modprobe";
31         const char *const args[] = {
32                 progname,
33                 "--show-depends", "mod-loop-a",
34                 NULL,
35         };
36
37         test_spawn_prog(progname, args);
38         exit(EXIT_FAILURE);
39 }
40 DEFINE_TEST(modprobe_show_depends,
41         .description = "check if output for modprobe --show-depends is correct for loaded modules",
42         .config = {
43                 [TC_UNAME_R] = "4.4.4",
44                 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-modprobe/show-depends",
45         },
46         .output = {
47                 .out = TESTSUITE_ROOTFS "test-modprobe/show-depends/correct.txt",
48         });
49
50 static noreturn int modprobe_show_depends2(const struct test *t)
51 {
52         const char *progname = ABS_TOP_BUILDDIR "/tools/modprobe";
53         const char *const args[] = {
54                 progname,
55                 "--show-depends", "mod-simple",
56                 NULL,
57         };
58
59         test_spawn_prog(progname, args);
60         exit(EXIT_FAILURE);
61 }
62 DEFINE_TEST(modprobe_show_depends2,
63         .description = "check if output for modprobe --show-depends is correct",
64         .config = {
65                 [TC_UNAME_R] = "4.4.4",
66                 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-modprobe/show-depends",
67         },
68         .output = {
69                 .out = TESTSUITE_ROOTFS "test-modprobe/show-depends/correct-mod-simple.txt",
70         });
71
72
73 static noreturn int modprobe_show_alias_to_none(const struct test *t)
74 {
75         const char *progname = ABS_TOP_BUILDDIR "/tools/modprobe";
76         const char *const args[] = {
77                 progname,
78                 "--show-depends", "--ignore-install", "--quiet", "mod-simple",
79                 NULL,
80         };
81
82         test_spawn_prog(progname, args);
83         exit(EXIT_FAILURE);
84 }
85 DEFINE_TEST(modprobe_show_alias_to_none,
86         .description = "check if modprobe --show-depends doesn't explode with an alias to nothing",
87         .config = {
88                 [TC_UNAME_R] = "4.4.4",
89                 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-modprobe/alias-to-none",
90         },
91         .output = {
92                 .out = TESTSUITE_ROOTFS "test-modprobe/alias-to-none/correct.txt",
93         },
94         .modules_loaded = "",
95         );
96
97
98 static noreturn int modprobe_show_exports(const struct test *t)
99 {
100         const char *progname = ABS_TOP_BUILDDIR "/tools/modprobe";
101         const char *const args[] = {
102                 progname,
103                 "--show-exports", "--quiet", "/mod-loop-a.ko",
104                 NULL,
105         };
106
107         test_spawn_prog(progname, args);
108         exit(EXIT_FAILURE);
109 }
110 DEFINE_TEST(modprobe_show_exports,
111         .description = "check if modprobe --show-depends doesn't explode with an alias to nothing",
112         .config = {
113                 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-modprobe/show-exports",
114         },
115         .output = {
116                 .out = TESTSUITE_ROOTFS "test-modprobe/show-exports/correct.txt",
117                 .regex = true,
118         });
119
120
121 static noreturn int modprobe_builtin(const struct test *t)
122 {
123         const char *progname = ABS_TOP_BUILDDIR "/tools/modprobe";
124         const char *const args[] = {
125                 progname,
126                 "unix",
127                 NULL,
128         };
129
130         test_spawn_prog(progname, args);
131         exit(EXIT_FAILURE);
132 }
133 DEFINE_TEST(modprobe_builtin,
134         .description = "check if modprobe return 0 for builtin",
135         .config = {
136                 [TC_UNAME_R] = "4.4.4",
137                 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-modprobe/builtin",
138         });
139
140 static noreturn int modprobe_builtin_lookup_only(const struct test *t)
141 {
142         const char *progname = ABS_TOP_BUILDDIR "/tools/modprobe";
143         const char *const args[] = {
144                 progname,
145                 "-R", "unix",
146                 NULL,
147         };
148
149         test_spawn_prog(progname, args);
150         exit(EXIT_FAILURE);
151 }
152 DEFINE_TEST(modprobe_builtin_lookup_only,
153         .description = "check if modprobe -R correctly returns the builtin module",
154         .config = {
155                 [TC_UNAME_R] = "4.4.4",
156                 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-modprobe/builtin",
157         },
158         .output = {
159                 .out = TESTSUITE_ROOTFS "test-modprobe/builtin/correct.txt",
160         });
161
162 static noreturn int modprobe_softdep_loop(const struct test *t)
163 {
164         const char *progname = ABS_TOP_BUILDDIR "/tools/modprobe";
165         const char *const args[] = {
166                 progname,
167                 "mod-loop-b",
168                 NULL,
169         };
170
171         test_spawn_prog(progname, args);
172         exit(EXIT_FAILURE);
173 }
174 DEFINE_TEST(modprobe_softdep_loop,
175         .description = "check if modprobe breaks softdep loop",
176         .config = {
177                 [TC_UNAME_R] = "4.4.4",
178                 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-modprobe/softdep-loop",
179                 [TC_INIT_MODULE_RETCODES] = "",
180         },
181         .modules_loaded = "mod-loop-a,mod-loop-b",
182         );
183
184 static noreturn int modprobe_install_cmd_loop(const struct test *t)
185 {
186         const char *progname = ABS_TOP_BUILDDIR "/tools/modprobe";
187         const char *const args[] = {
188                 progname,
189                 "mod-loop-a",
190                 NULL,
191         };
192
193         test_spawn_prog(progname, args);
194         exit(EXIT_FAILURE);
195 }
196 DEFINE_TEST(modprobe_install_cmd_loop,
197         .description = "check if modprobe breaks install-commands loop",
198         .config = {
199                 [TC_UNAME_R] = "4.4.4",
200                 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-modprobe/install-cmd-loop",
201                 [TC_INIT_MODULE_RETCODES] = "",
202         },
203         .env_vars = (const struct keyval[]) {
204                 { "MODPROBE", ABS_TOP_BUILDDIR "/tools/modprobe" },
205                 { }
206                 },
207         .modules_loaded = "mod-loop-b,mod-loop-a",
208         );
209
210 static noreturn int modprobe_param_kcmdline(const struct test *t)
211 {
212         const char *progname = ABS_TOP_BUILDDIR "/tools/modprobe";
213         const char *const args[] = {
214                 progname,
215                 "--show-depends", "mod-simple",
216                 NULL,
217         };
218
219         test_spawn_prog(progname, args);
220         exit(EXIT_FAILURE);
221 }
222 DEFINE_TEST(modprobe_param_kcmdline,
223         .description = "check if params from kcmdline are passed to (f)init_module call",
224         .config = {
225                 [TC_UNAME_R] = "4.4.4",
226                 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-modprobe/module-param-kcmdline",
227         },
228         .output = {
229                 .out = TESTSUITE_ROOTFS "test-modprobe/module-param-kcmdline/correct.txt",
230         },
231         .modules_loaded = "",
232         );
233
234 static noreturn int modprobe_param_kcmdline2(const struct test *t)
235 {
236         const char *progname = ABS_TOP_BUILDDIR "/tools/modprobe";
237         const char *const args[] = {
238                 progname,
239                 "-c",
240                 NULL,
241         };
242
243         test_spawn_prog(progname, args);
244         exit(EXIT_FAILURE);
245 }
246 DEFINE_TEST(modprobe_param_kcmdline2,
247         .description = "check if params with no value are parsed correctly from kcmdline",
248         .config = {
249                 [TC_UNAME_R] = "4.4.4",
250                 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-modprobe/module-param-kcmdline2",
251         },
252         .output = {
253                 .out = TESTSUITE_ROOTFS "test-modprobe/module-param-kcmdline2/correct.txt",
254         },
255         .modules_loaded = "",
256         );
257
258 static noreturn int modprobe_param_kcmdline3(const struct test *t)
259 {
260         const char *progname = ABS_TOP_BUILDDIR "/tools/modprobe";
261         const char *const args[] = {
262                 progname,
263                 "-c",
264                 NULL,
265         };
266
267         test_spawn_prog(progname, args);
268         exit(EXIT_FAILURE);
269 }
270 DEFINE_TEST(modprobe_param_kcmdline3,
271         .description = "check if unrelated strings in kcmdline are correctly ignored",
272         .config = {
273                 [TC_UNAME_R] = "4.4.4",
274                 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-modprobe/module-param-kcmdline3",
275         },
276         .output = {
277                 .out = TESTSUITE_ROOTFS "test-modprobe/module-param-kcmdline3/correct.txt",
278         },
279         .modules_loaded = "",
280         );
281
282 static noreturn int modprobe_param_kcmdline4(const struct test *t)
283 {
284         const char *progname = ABS_TOP_BUILDDIR "/tools/modprobe";
285         const char *const args[] = {
286                 progname,
287                 "-c",
288                 NULL,
289         };
290
291         test_spawn_prog(progname, args);
292         exit(EXIT_FAILURE);
293 }
294 DEFINE_TEST(modprobe_param_kcmdline4,
295         .description = "check if unrelated strings in kcmdline are correctly ignored",
296         .config = {
297                 [TC_UNAME_R] = "4.4.4",
298                 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-modprobe/module-param-kcmdline4",
299         },
300         .output = {
301                 .out = TESTSUITE_ROOTFS "test-modprobe/module-param-kcmdline4/correct.txt",
302         },
303         .modules_loaded = "",
304         );
305
306 static noreturn int modprobe_param_kcmdline5(const struct test *t)
307 {
308         const char *progname = ABS_TOP_BUILDDIR "/tools/modprobe";
309         const char *const args[] = {
310                 progname,
311                 "-c",
312                 NULL,
313         };
314
315         test_spawn_prog(progname, args);
316         exit(EXIT_FAILURE);
317 }
318 DEFINE_TEST(modprobe_param_kcmdline5,
319         .description = "check if params with spaces are parsed correctly from kcmdline",
320         .config = {
321                 [TC_UNAME_R] = "4.4.4",
322                 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-modprobe/module-param-kcmdline5",
323         },
324         .output = {
325                 .out = TESTSUITE_ROOTFS "test-modprobe/module-param-kcmdline5/correct.txt",
326         },
327         .modules_loaded = "",
328         );
329
330
331 static noreturn int modprobe_param_kcmdline6(const struct test *t)
332 {
333         const char *progname = ABS_TOP_BUILDDIR "/tools/modprobe";
334         const char *const args[] = {
335                 progname,
336                 "-c",
337                 NULL,
338         };
339
340         test_spawn_prog(progname, args);
341         exit(EXIT_FAILURE);
342 }
343 DEFINE_TEST(modprobe_param_kcmdline6,
344         .description = "check if dots on other parts of kcmdline don't confuse our parser",
345         .config = {
346                 [TC_UNAME_R] = "4.4.4",
347                 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-modprobe/module-param-kcmdline6",
348         },
349         .output = {
350                 .out = TESTSUITE_ROOTFS "test-modprobe/module-param-kcmdline6/correct.txt",
351         },
352         .modules_loaded = "",
353         );
354
355
356 static noreturn int modprobe_force(const struct test *t)
357 {
358         const char *progname = ABS_TOP_BUILDDIR "/tools/modprobe";
359         const char *const args[] = {
360                 progname,
361                 "--force", "mod-simple",
362                 NULL,
363         };
364
365         test_spawn_prog(progname, args);
366         exit(EXIT_FAILURE);
367 }
368 DEFINE_TEST(modprobe_force,
369         .description = "check modprobe --force",
370         .config = {
371                 [TC_UNAME_R] = "4.4.4",
372                 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-modprobe/force",
373                 [TC_INIT_MODULE_RETCODES] = "",
374         },
375         .modules_loaded = "mod-simple",
376         );
377
378 static noreturn int modprobe_oldkernel(const struct test *t)
379 {
380         const char *progname = ABS_TOP_BUILDDIR "/tools/modprobe";
381         const char *const args[] = {
382                 progname,
383                 "mod-simple",
384                 NULL,
385         };
386
387         test_spawn_prog(progname, args);
388         exit(EXIT_FAILURE);
389 }
390 DEFINE_TEST(modprobe_oldkernel,
391         .description = "check modprobe with kernel without finit_module()",
392         .config = {
393                 [TC_UNAME_R] = "3.3.3",
394                 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-modprobe/oldkernel",
395                 [TC_INIT_MODULE_RETCODES] = "",
396         },
397         .modules_loaded = "mod-simple",
398         );
399
400 static noreturn int modprobe_oldkernel_force(const struct test *t)
401 {
402         const char *progname = ABS_TOP_BUILDDIR "/tools/modprobe";
403         const char *const args[] = {
404                 progname,
405                 "--force", "mod-simple",
406                 NULL,
407         };
408
409         test_spawn_prog(progname, args);
410         exit(EXIT_FAILURE);
411 }
412 DEFINE_TEST(modprobe_oldkernel_force,
413         .description = "check modprobe --force with kernel without finit_module()",
414         .config = {
415                 [TC_UNAME_R] = "3.3.3",
416                 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-modprobe/oldkernel-force",
417                 [TC_INIT_MODULE_RETCODES] = "",
418         },
419         .modules_loaded = "mod-simple",
420         );
421
422 static noreturn int modprobe_external(const struct test *t)
423 {
424         const char *progname = ABS_TOP_BUILDDIR "/tools/modprobe";
425         const char *const args[] = {
426                 progname,
427                 "mod-simple",
428                 NULL,
429         };
430
431         test_spawn_prog(progname, args);
432         exit(EXIT_FAILURE);
433 }
434 DEFINE_TEST(modprobe_external,
435         .description = "check modprobe able to load external module",
436         .config = {
437                 [TC_UNAME_R] = "4.4.4",
438                 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-modprobe/external",
439                 [TC_INIT_MODULE_RETCODES] = "",
440         },
441         .modules_loaded = "mod-simple",
442         );
443
444 TESTSUITE_MAIN();