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 
modprobe_show_depends(const struct test * t)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 
modprobe_show_depends2(const struct test * t)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 
modprobe_show_alias_to_none(const struct test * t)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 
modprobe_show_exports(const struct test * t)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 
modprobe_builtin(const struct test * t)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 
modprobe_builtin_lookup_only(const struct test * t)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 
modprobe_softdep_loop(const struct test * t)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 
modprobe_install_cmd_loop(const struct test * t)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 
modprobe_param_kcmdline(const struct test * t)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 
modprobe_param_kcmdline2(const struct test * t)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 
modprobe_param_kcmdline3(const struct test * t)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 
modprobe_param_kcmdline4(const struct test * t)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 
modprobe_param_kcmdline5(const struct test * t)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 
modprobe_force(const struct test * t)331 static noreturn int modprobe_force(const struct test *t)
332 {
333 	const char *progname = ABS_TOP_BUILDDIR "/tools/modprobe";
334 	const char *const args[] = {
335 		progname,
336 		"--force", "mod-simple",
337 		NULL,
338 	};
339 
340 	test_spawn_prog(progname, args);
341 	exit(EXIT_FAILURE);
342 }
343 DEFINE_TEST(modprobe_force,
344 	.description = "check modprobe --force",
345 	.config = {
346 		[TC_UNAME_R] = "4.4.4",
347 		[TC_ROOTFS] = TESTSUITE_ROOTFS "test-modprobe/force",
348 		[TC_INIT_MODULE_RETCODES] = "",
349 	},
350 	.modules_loaded = "mod-simple",
351 	);
352 
modprobe_oldkernel(const struct test * t)353 static noreturn int modprobe_oldkernel(const struct test *t)
354 {
355 	const char *progname = ABS_TOP_BUILDDIR "/tools/modprobe";
356 	const char *const args[] = {
357 		progname,
358 		"mod-simple",
359 		NULL,
360 	};
361 
362 	test_spawn_prog(progname, args);
363 	exit(EXIT_FAILURE);
364 }
365 DEFINE_TEST(modprobe_oldkernel,
366 	.description = "check modprobe with kernel without finit_module()",
367 	.config = {
368 		[TC_UNAME_R] = "3.3.3",
369 		[TC_ROOTFS] = TESTSUITE_ROOTFS "test-modprobe/oldkernel",
370 		[TC_INIT_MODULE_RETCODES] = "",
371 	},
372 	.modules_loaded = "mod-simple",
373 	);
374 
modprobe_oldkernel_force(const struct test * t)375 static noreturn int modprobe_oldkernel_force(const struct test *t)
376 {
377 	const char *progname = ABS_TOP_BUILDDIR "/tools/modprobe";
378 	const char *const args[] = {
379 		progname,
380 		"--force", "mod-simple",
381 		NULL,
382 	};
383 
384 	test_spawn_prog(progname, args);
385 	exit(EXIT_FAILURE);
386 }
387 DEFINE_TEST(modprobe_oldkernel_force,
388 	.description = "check modprobe --force with kernel without finit_module()",
389 	.config = {
390 		[TC_UNAME_R] = "3.3.3",
391 		[TC_ROOTFS] = TESTSUITE_ROOTFS "test-modprobe/oldkernel-force",
392 		[TC_INIT_MODULE_RETCODES] = "",
393 	},
394 	.modules_loaded = "mod-simple",
395 	);
396 
modprobe_external(const struct test * t)397 static noreturn int modprobe_external(const struct test *t)
398 {
399 	const char *progname = ABS_TOP_BUILDDIR "/tools/modprobe";
400 	const char *const args[] = {
401 		progname,
402 		"mod-simple",
403 		NULL,
404 	};
405 
406 	test_spawn_prog(progname, args);
407 	exit(EXIT_FAILURE);
408 }
409 DEFINE_TEST(modprobe_external,
410 	.description = "check modprobe able to load external module",
411 	.config = {
412 		[TC_UNAME_R] = "4.4.4",
413 		[TC_ROOTFS] = TESTSUITE_ROOTFS "test-modprobe/external",
414 		[TC_INIT_MODULE_RETCODES] = "",
415 	},
416 	.modules_loaded = "mod-simple",
417 	);
418 
419 TESTSUITE_MAIN();
420