Pedro Pedruzzi | 760b896 | 2012-01-28 03:22:47 -0200 | [diff] [blame] | 1 | /* |
Lucas De Marchi | e6b0e49 | 2013-01-16 11:27:21 -0200 | [diff] [blame] | 2 | * Copyright (C) 2012-2013 ProFUSION embedded systems |
Pedro Pedruzzi | 760b896 | 2012-01-28 03:22:47 -0200 | [diff] [blame] | 3 | * Copyright (C) 2012 Pedro Pedruzzi |
| 4 | * |
Lucas De Marchi | e1b1ab2 | 2012-07-10 09:42:24 -0300 | [diff] [blame] | 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. |
Pedro Pedruzzi | 760b896 | 2012-01-28 03:22:47 -0200 | [diff] [blame] | 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 |
Lucas De Marchi | e1b1ab2 | 2012-07-10 09:42:24 -0300 | [diff] [blame] | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
Pedro Pedruzzi | 760b896 | 2012-01-28 03:22:47 -0200 | [diff] [blame] | 14 | * |
Lucas De Marchi | e1b1ab2 | 2012-07-10 09:42:24 -0300 | [diff] [blame] | 15 | * You should have received a copy of the GNU Lesser General Public |
Lucas De Marchi | dea2dfe | 2014-12-25 23:32:03 -0200 | [diff] [blame] | 16 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
Pedro Pedruzzi | 760b896 | 2012-01-28 03:22:47 -0200 | [diff] [blame] | 17 | */ |
| 18 | |
Lucas De Marchi | 9c6084d | 2015-01-14 17:25:45 -0200 | [diff] [blame] | 19 | #include <fcntl.h> |
| 20 | #include <stdbool.h> |
Pedro Pedruzzi | 760b896 | 2012-01-28 03:22:47 -0200 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
Lucas De Marchi | 9c6084d | 2015-01-14 17:25:45 -0200 | [diff] [blame] | 24 | #include <unistd.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <sys/types.h> |
Pedro Pedruzzi | 760b896 | 2012-01-28 03:22:47 -0200 | [diff] [blame] | 27 | |
Lucas De Marchi | 96573a0 | 2014-10-03 00:01:35 -0300 | [diff] [blame] | 28 | #include <shared/util.h> |
| 29 | |
Pedro Pedruzzi | 760b896 | 2012-01-28 03:22:47 -0200 | [diff] [blame] | 30 | #include "testsuite.h" |
| 31 | |
| 32 | static 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 | |
Pedro Pedruzzi | 760b896 | 2012-01-28 03:22:47 -0200 | [diff] [blame] | 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 | } |
Lucas De Marchi | f1155c1 | 2014-10-09 13:00:30 -0300 | [diff] [blame] | 64 | DEFINE_TEST(alias_1, |
Pedro Pedruzzi | 760b896 | 2012-01-28 03:22:47 -0200 | [diff] [blame] | 65 | .description = "check if alias_normalize does the right thing", |
Pedro Pedruzzi | 760b896 | 2012-01-28 03:22:47 -0200 | [diff] [blame] | 66 | .config = { |
Lucas De Marchi | 807c601 | 2013-11-18 04:32:45 -0200 | [diff] [blame] | 67 | [TC_ROOTFS] = TESTSUITE_ROOTFS "test-util/", |
Pedro Pedruzzi | 760b896 | 2012-01-28 03:22:47 -0200 | [diff] [blame] | 68 | }, |
| 69 | .need_spawn = true, |
| 70 | .output = { |
Lucas De Marchi | 807c601 | 2013-11-18 04:32:45 -0200 | [diff] [blame] | 71 | .out = TESTSUITE_ROOTFS "test-util/alias-correct.txt", |
Lucas De Marchi | c5d8198 | 2012-02-07 10:46:46 -0200 | [diff] [blame] | 72 | }); |
Pedro Pedruzzi | 760b896 | 2012-01-28 03:22:47 -0200 | [diff] [blame] | 73 | |
Lucas De Marchi | aafd383 | 2014-10-03 03:25:06 -0300 | [diff] [blame] | 74 | static int test_freadline_wrapped(const struct test *t) |
Lucas De Marchi | 1dda626 | 2013-11-18 05:13:59 -0200 | [diff] [blame] | 75 | { |
Lucas De Marchi | aafd383 | 2014-10-03 03:25:06 -0300 | [diff] [blame] | 76 | FILE *fp = fopen("/freadline_wrapped-input.txt", "re"); |
Lucas De Marchi | 1dda626 | 2013-11-18 05:13:59 -0200 | [diff] [blame] | 77 | |
| 78 | if (!fp) |
| 79 | return EXIT_FAILURE; |
| 80 | |
| 81 | while (!feof(fp) && !ferror(fp)) { |
| 82 | unsigned int num = 0; |
Lucas De Marchi | aafd383 | 2014-10-03 03:25:06 -0300 | [diff] [blame] | 83 | char *s = freadline_wrapped(fp, &num); |
Lucas De Marchi | 1dda626 | 2013-11-18 05:13:59 -0200 | [diff] [blame] | 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 | } |
Lucas De Marchi | f1155c1 | 2014-10-09 13:00:30 -0300 | [diff] [blame] | 94 | DEFINE_TEST(test_freadline_wrapped, |
Lucas De Marchi | aafd383 | 2014-10-03 03:25:06 -0300 | [diff] [blame] | 95 | .description = "check if freadline_wrapped() does the right thing", |
Lucas De Marchi | 1dda626 | 2013-11-18 05:13:59 -0200 | [diff] [blame] | 96 | .config = { |
| 97 | [TC_ROOTFS] = TESTSUITE_ROOTFS "test-util/", |
| 98 | }, |
| 99 | .need_spawn = true, |
| 100 | .output = { |
Lucas De Marchi | aafd383 | 2014-10-03 03:25:06 -0300 | [diff] [blame] | 101 | .out = TESTSUITE_ROOTFS "test-util/freadline_wrapped-correct.txt", |
Lucas De Marchi | 1dda626 | 2013-11-18 05:13:59 -0200 | [diff] [blame] | 102 | }); |
| 103 | |
Lucas De Marchi | 9c6084d | 2015-01-14 17:25:45 -0200 | [diff] [blame] | 104 | static 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 | } |
| 114 | DEFINE_TEST(test_strchr_replace, |
| 115 | .description = "check implementation of strchr_replace()", |
| 116 | .need_spawn = false, |
| 117 | ); |
| 118 | |
| 119 | static 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 | } |
| 142 | DEFINE_TEST(test_underscores, |
| 143 | .description = "check implementation of underscores()", |
| 144 | .need_spawn = false, |
| 145 | ); |
| 146 | |
| 147 | static 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 | } |
| 175 | DEFINE_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 |
| 182 | static 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 | } |
| 195 | DEFINE_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 | |
Lucas De Marchi | aac5f45 | 2015-02-10 10:41:47 -0200 | [diff] [blame] | 209 | static 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 | } |
| 223 | DEFINE_TEST(test_addu64_overflow, |
| 224 | .description = "check implementation of addu4_overflow()", |
| 225 | .need_spawn = false, |
| 226 | ); |
| 227 | |
Lucas De Marchi | 9c6084d | 2015-01-14 17:25:45 -0200 | [diff] [blame] | 228 | |
Lucas De Marchi | 4328982 | 2014-10-09 14:29:04 -0300 | [diff] [blame] | 229 | TESTSUITE_MAIN(); |