]> git.ipfire.org Git - thirdparty/kmod.git/blame - testsuite/test-util.c
testsuite: depmod: check netsted loops reporting
[thirdparty/kmod.git] / testsuite / test-util.c
CommitLineData
760b8968 1/*
e6b0e49b 2 * Copyright (C) 2012-2013 ProFUSION embedded systems
760b8968
PP
3 * Copyright (C) 2012 Pedro Pedruzzi
4 *
e1b1ab24
LDM
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
760b8968
PP
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
e1b1ab24
LDM
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
760b8968 14 *
e1b1ab24 15 * You should have received a copy of the GNU Lesser General Public
dea2dfee 16 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
760b8968
PP
17 */
18
9c6084d9
LDM
19#include <fcntl.h>
20#include <stdbool.h>
760b8968
PP
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
9c6084d9
LDM
24#include <unistd.h>
25#include <sys/stat.h>
26#include <sys/types.h>
760b8968 27
96573a02
LDM
28#include <shared/util.h>
29
760b8968
PP
30#include "testsuite.h"
31
32static int alias_1(const struct test *t)
33{
34 static const char *input[] = {
35 "test1234",
36 "test[abcfoobar]2211",
37 "bar[aaa][bbbb]sss",
38 "kmod[p.b]lib",
39 "[az]1234[AZ]",
40 NULL,
41 };
42
43 char buf[PATH_MAX];
44 size_t len;
45 const char **alias;
46
47 for (alias = input; *alias != NULL; alias++) {
48 int ret;
49
760b8968
PP
50 ret = alias_normalize(*alias, buf, &len);
51 printf("input %s\n", *alias);
52 printf("return %d\n", ret);
53
54 if (ret == 0) {
55 printf("len %zu\n", len);
56 printf("output %s\n", buf);
57 }
58
59 printf("\n");
60 }
61
62 return EXIT_SUCCESS;
63}
f1155c15 64DEFINE_TEST(alias_1,
760b8968 65 .description = "check if alias_normalize does the right thing",
760b8968 66 .config = {
807c601d 67 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-util/",
760b8968
PP
68 },
69 .need_spawn = true,
70 .output = {
807c601d 71 .out = TESTSUITE_ROOTFS "test-util/alias-correct.txt",
c5d81989 72 });
760b8968 73
aafd3835 74static int test_freadline_wrapped(const struct test *t)
1dda626f 75{
aafd3835 76 FILE *fp = fopen("/freadline_wrapped-input.txt", "re");
1dda626f
LDM
77
78 if (!fp)
79 return EXIT_FAILURE;
80
81 while (!feof(fp) && !ferror(fp)) {
82 unsigned int num = 0;
aafd3835 83 char *s = freadline_wrapped(fp, &num);
1dda626f
LDM
84 if (!s)
85 break;
86 puts(s);
87 free(s);
88 printf("%u\n", num);
89 }
90
91 fclose(fp);
92 return EXIT_SUCCESS;
93}
f1155c15 94DEFINE_TEST(test_freadline_wrapped,
aafd3835 95 .description = "check if freadline_wrapped() does the right thing",
1dda626f
LDM
96 .config = {
97 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-util/",
98 },
99 .need_spawn = true,
100 .output = {
aafd3835 101 .out = TESTSUITE_ROOTFS "test-util/freadline_wrapped-correct.txt",
1dda626f
LDM
102 });
103
9c6084d9
LDM
104static int test_strchr_replace(const struct test *t)
105{
106 _cleanup_free_ char *s = strdup("this is a test string");
107 const char *res = "thiC iC a teCt Ctring";
108
109 strchr_replace(s, 's', 'C');
110 assert_return(streq(s, res), EXIT_FAILURE);
111
112 return EXIT_SUCCESS;
113}
114DEFINE_TEST(test_strchr_replace,
115 .description = "check implementation of strchr_replace()",
116 .need_spawn = false,
117 );
118
119static int test_underscores(const struct test *t)
120{
121 struct teststr {
122 char *val;
123 const char *res;
124 } teststr[] = {
125 { strdup("aa-bb-cc_"), "aa_bb_cc_" },
126 { strdup("-aa-bb-cc-"), "_aa_bb_cc_" },
127 { strdup("-aa[-bb-]cc-"), "_aa[-bb-]cc_" },
128 { strdup("-aa-[bb]-cc-"), "_aa_[bb]_cc_" },
129 { strdup("-aa-[b-b]-cc-"), "_aa_[b-b]_cc_" },
130 { strdup("-aa-b[-]b-cc"), "_aa_b[-]b_cc" },
131 { }
132 }, *iter;
133
134 for (iter = &teststr[0]; iter->val != NULL; iter++) {
135 _cleanup_free_ char *val = iter->val;
136 underscores(val);
137 assert_return(streq(val, iter->res), EXIT_FAILURE);
138 }
139
140 return EXIT_SUCCESS;
141}
142DEFINE_TEST(test_underscores,
143 .description = "check implementation of underscores()",
144 .need_spawn = false,
145 );
146
147static int test_path_ends_with_kmod_ext(const struct test *t)
148{
149 struct teststr {
150 const char *val;
151 bool res;
152 } teststr[] = {
153 { "/bla.ko", true },
154#ifdef ENABLE_ZLIB
155 { "/bla.ko.gz", true },
156#endif
157#ifdef ENABLE_XZ
158 { "/bla.ko.xz", true },
159#endif
160 { "/bla.ko.x", false },
161 { "/bla.ko.", false },
162 { "/bla.koz", false },
163 { "/b", false },
164 { }
165 }, *iter;
166
167 for (iter = &teststr[0]; iter->val != NULL; iter++) {
168 assert_return(path_ends_with_kmod_ext(iter->val,
169 strlen(iter->val)) == iter->res,
170 EXIT_FAILURE);
171 }
172
173 return EXIT_SUCCESS;
174}
175DEFINE_TEST(test_path_ends_with_kmod_ext,
176 .description = "check implementation of path_ends_with_kmod_ext()",
177 .need_spawn = false,
178 );
179
180#define TEST_WRITE_STR_SAFE_FILE "/write-str-safe"
181#define TEST_WRITE_STR_SAFE_PATH TESTSUITE_ROOTFS "test-util2/" TEST_WRITE_STR_SAFE_FILE
182static int test_write_str_safe(const struct test *t)
183{
184 const char *s = "test";
185 int fd;
186
187 fd = open(TEST_WRITE_STR_SAFE_FILE ".txt", O_CREAT|O_TRUNC|O_WRONLY, 0644);
188 assert_return(fd >= 0, EXIT_FAILURE);
189
190 write_str_safe(fd, s, strlen(s));
191 close(fd);
192
193 return EXIT_SUCCESS;
194}
195DEFINE_TEST(test_write_str_safe,
196 .description = "check implementation of write_str_safe()",
197 .config = {
198 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-util2/",
199 },
200 .need_spawn = true,
201 .output = {
202 .files = (const struct keyval[]) {
203 { TEST_WRITE_STR_SAFE_PATH ".txt",
204 TEST_WRITE_STR_SAFE_PATH "-correct.txt" },
205 { }
206 },
207 });
208
aac5f451
LDM
209static int test_addu64_overflow(const struct test *t)
210{
211 uint64_t res;
212 bool overflow;
213
214 overflow = addu64_overflow(UINT64_MAX - 1, 1, &res);
215 assert_return(!overflow, EXIT_FAILURE);
216 assert_return(res == UINT64_MAX, EXIT_FAILURE);
217
218 overflow = addu64_overflow(UINT64_MAX, 1, &res);
219 assert_return(overflow, EXIT_FAILURE);
220
221 return EXIT_SUCCESS;
222}
223DEFINE_TEST(test_addu64_overflow,
224 .description = "check implementation of addu4_overflow()",
225 .need_spawn = false,
226 );
227
9c6084d9 228
43289820 229TESTSUITE_MAIN();