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 <libkmod/libkmod.h>
27 
28 #include "testsuite.h"
29 
from_name(const struct test * t)30 static int from_name(const struct test *t)
31 {
32 	static const char *modnames[] = {
33 		"ext4",
34 		"balbalbalbbalbalbalbalbalbalbal",
35 		"snd-hda-intel",
36 		"snd-timer",
37 		"iTCO_wdt",
38 		NULL,
39 	};
40 	const char **p;
41 	struct kmod_ctx *ctx;
42 	struct kmod_module *mod;
43 	const char *null_config = NULL;
44 	int err;
45 
46 	ctx = kmod_new(NULL, &null_config);
47 	if (ctx == NULL)
48 		exit(EXIT_FAILURE);
49 
50 	for (p = modnames; p != NULL; p++) {
51 		err = kmod_module_new_from_name(ctx, *p, &mod);
52 		if (err < 0)
53 			exit(EXIT_SUCCESS);
54 
55 		printf("modname: %s\n", kmod_module_get_name(mod));
56 		kmod_module_unref(mod);
57 	}
58 
59 	kmod_unref(ctx);
60 
61 	return EXIT_SUCCESS;
62 }
63 DEFINE_TEST(from_name,
64 	.description = "check if module names are parsed correctly",
65 	.config = {
66 		[TC_ROOTFS] = TESTSUITE_ROOTFS "test-new-module/from_name/",
67 	},
68 	.need_spawn = true,
69 	.output = {
70 		.out = TESTSUITE_ROOTFS "test-new-module/from_name/correct.txt",
71 	});
72 
from_alias(const struct test * t)73 static int from_alias(const struct test *t)
74 {
75 	static const char *modnames[] = {
76 		"ext4.*",
77 		NULL,
78 	};
79 	const char **p;
80 	struct kmod_ctx *ctx;
81 	int err;
82 
83 	ctx = kmod_new(NULL, NULL);
84 	if (ctx == NULL)
85 		exit(EXIT_FAILURE);
86 
87 	for (p = modnames; p != NULL; p++) {
88 		struct kmod_list *l, *list = NULL;
89 
90 		err = kmod_module_new_from_lookup(ctx, *p, &list);
91 		if (err < 0)
92 			exit(EXIT_SUCCESS);
93 
94 		kmod_list_foreach(l, list) {
95 			struct kmod_module *m;
96 			m = kmod_module_get_module(l);
97 
98 			printf("modname: %s\n", kmod_module_get_name(m));
99 			kmod_module_unref(m);
100 		}
101 		kmod_module_unref_list(list);
102 	}
103 
104 	kmod_unref(ctx);
105 
106 	return EXIT_SUCCESS;
107 }
108 DEFINE_TEST(from_alias,
109 	.description = "check if aliases are parsed correctly",
110 	.config = {
111 		[TC_ROOTFS] = TESTSUITE_ROOTFS "test-new-module/from_alias/",
112 	},
113 	.need_spawn = true,
114 	.output = {
115 		.out = TESTSUITE_ROOTFS "test-new-module/from_alias/correct.txt",
116 	});
117 
118 TESTSUITE_MAIN();
119