2 * Copyright (C) 2012-2013 ProFUSION embedded systems
3 * Copyright (C) 2012 Pedro Pedruzzi
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.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
26 #include <sys/types.h>
28 #include <shared/util.h>
30 #include "testsuite.h"
32 static int alias_1(const struct test
*t
)
34 static const char *input
[] = {
36 "test[abcfoobar]2211",
47 for (alias
= input
; *alias
!= NULL
; alias
++) {
50 ret
= alias_normalize(*alias
, buf
, &len
);
51 printf("input %s\n", *alias
);
52 printf("return %d\n", ret
);
55 printf("len %zu\n", len
);
56 printf("output %s\n", buf
);
65 .description
= "check if alias_normalize does the right thing",
67 [TC_ROOTFS
] = TESTSUITE_ROOTFS
"test-util/",
71 .out
= TESTSUITE_ROOTFS
"test-util/alias-correct.txt",
74 static int test_freadline_wrapped(const struct test
*t
)
76 FILE *fp
= fopen("/freadline_wrapped-input.txt", "re");
81 while (!feof(fp
) && !ferror(fp
)) {
83 char *s
= freadline_wrapped(fp
, &num
);
94 DEFINE_TEST(test_freadline_wrapped
,
95 .description
= "check if freadline_wrapped() does the right thing",
97 [TC_ROOTFS
] = TESTSUITE_ROOTFS
"test-util/",
101 .out
= TESTSUITE_ROOTFS
"test-util/freadline_wrapped-correct.txt",
104 static int test_strchr_replace(const struct test
*t
)
106 _cleanup_free_
char *s
= strdup("this is a test string");
107 const char *res
= "thiC iC a teCt Ctring";
109 strchr_replace(s
, 's', 'C');
110 assert_return(streq(s
, res
), EXIT_FAILURE
);
114 DEFINE_TEST(test_strchr_replace
,
115 .description
= "check implementation of strchr_replace()",
119 static int test_underscores(const struct test
*t
)
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" },
134 for (iter
= &teststr
[0]; iter
->val
!= NULL
; iter
++) {
135 _cleanup_free_
char *val
= iter
->val
;
137 assert_return(streq(val
, iter
->res
), EXIT_FAILURE
);
142 DEFINE_TEST(test_underscores
,
143 .description
= "check implementation of underscores()",
147 static int test_path_ends_with_kmod_ext(const struct test
*t
)
155 { "/bla.ko.gz", true },
158 { "/bla.ko.xz", true },
160 { "/bla.ko.x", false },
161 { "/bla.ko.", false },
162 { "/bla.koz", false },
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
,
175 DEFINE_TEST(test_path_ends_with_kmod_ext
,
176 .description
= "check implementation of path_ends_with_kmod_ext()",
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
182 static int test_write_str_safe(const struct test
*t
)
184 const char *s
= "test";
187 fd
= open(TEST_WRITE_STR_SAFE_FILE
".txt", O_CREAT
|O_TRUNC
|O_WRONLY
, 0644);
188 assert_return(fd
>= 0, EXIT_FAILURE
);
190 write_str_safe(fd
, s
, strlen(s
));
195 DEFINE_TEST(test_write_str_safe
,
196 .description
= "check implementation of write_str_safe()",
198 [TC_ROOTFS
] = TESTSUITE_ROOTFS
"test-util2/",
202 .files
= (const struct keyval
[]) {
203 { TEST_WRITE_STR_SAFE_PATH
".txt",
204 TEST_WRITE_STR_SAFE_PATH
"-correct.txt" },
209 static int test_addu64_overflow(const struct test
*t
)
214 overflow
= addu64_overflow(UINT64_MAX
- 1, 1, &res
);
215 assert_return(!overflow
, EXIT_FAILURE
);
216 assert_return(res
== UINT64_MAX
, EXIT_FAILURE
);
218 overflow
= addu64_overflow(UINT64_MAX
, 1, &res
);
219 assert_return(overflow
, EXIT_FAILURE
);
223 DEFINE_TEST(test_addu64_overflow
,
224 .description
= "check implementation of addu4_overflow()",