Simon Glass | 90a9901 | 2020-11-01 14:15:35 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Tests for setexpr command |
| 4 | * |
| 5 | * Copyright 2020 Google LLC |
| 6 | * Written by Simon Glass <sjg@chromium.org> |
| 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <console.h> |
| 11 | #include <mapmem.h> |
| 12 | #include <dm/test.h> |
| 13 | #include <test/suites.h> |
| 14 | #include <test/ut.h> |
| 15 | |
| 16 | #define BUF_SIZE 0x100 |
| 17 | |
Bin Meng | f0d0497 | 2021-02-17 17:04:06 +0800 | [diff] [blame] | 18 | /* Declare a new setexpr test */ |
Simon Glass | 90a9901 | 2020-11-01 14:15:35 -0700 | [diff] [blame] | 19 | #define SETEXPR_TEST(_name, _flags) UNIT_TEST(_name, _flags, setexpr_test) |
| 20 | |
| 21 | /* Test 'setexpr' command with simply setting integers */ |
| 22 | static int setexpr_test_int(struct unit_test_state *uts) |
| 23 | { |
| 24 | u8 *buf; |
| 25 | |
| 26 | buf = map_sysmem(0, BUF_SIZE); |
| 27 | memset(buf, '\xff', BUF_SIZE); |
| 28 | |
| 29 | /* byte */ |
| 30 | buf[0x0] = 0x12; |
| 31 | ut_assertok(run_command("setexpr.b fred 0", 0)); |
| 32 | ut_asserteq_str("0", env_get("fred")); |
| 33 | ut_assertok(run_command("setexpr.b fred *0", 0)); |
| 34 | ut_asserteq_str("12", env_get("fred")); |
| 35 | |
| 36 | /* 16-bit */ |
| 37 | *(short *)buf = 0x2345; |
| 38 | ut_assertok(run_command("setexpr.w fred 0", 0)); |
| 39 | ut_asserteq_str("0", env_get("fred")); |
| 40 | ut_assertok(run_command("setexpr.w fred *0", 0)); |
| 41 | ut_asserteq_str("2345", env_get("fred")); |
| 42 | |
| 43 | /* 32-bit */ |
| 44 | *(u32 *)buf = 0x3456789a; |
| 45 | ut_assertok(run_command("setexpr.l fred 0", 0)); |
| 46 | ut_asserteq_str("0", env_get("fred")); |
| 47 | ut_assertok(run_command("setexpr.l fred *0", 0)); |
Simon Glass | 25a43ac | 2020-11-01 14:15:37 -0700 | [diff] [blame] | 48 | ut_asserteq_str("3456789a", env_get("fred")); |
Simon Glass | 90a9901 | 2020-11-01 14:15:35 -0700 | [diff] [blame] | 49 | |
| 50 | /* 64-bit */ |
| 51 | *(u64 *)buf = 0x456789abcdef0123; |
| 52 | ut_assertok(run_command("setexpr.q fred 0", 0)); |
| 53 | ut_asserteq_str("0", env_get("fred")); |
| 54 | ut_assertok(run_command("setexpr.q fred *0", 0)); |
| 55 | ut_asserteq_str("456789abcdef0123", env_get("fred")); |
| 56 | |
| 57 | /* default */ |
| 58 | ut_assertok(run_command("setexpr fred 0", 0)); |
| 59 | ut_asserteq_str("0", env_get("fred")); |
| 60 | ut_assertok(run_command("setexpr fred *0", 0)); |
Simon Glass | 25a43ac | 2020-11-01 14:15:37 -0700 | [diff] [blame] | 61 | ut_asserteq_str("cdef0123", env_get("fred")); |
Simon Glass | 90a9901 | 2020-11-01 14:15:35 -0700 | [diff] [blame] | 62 | |
| 63 | unmap_sysmem(buf); |
| 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | SETEXPR_TEST(setexpr_test_int, UT_TESTF_CONSOLE_REC); |
| 68 | |
| 69 | /* Test 'setexpr' command with + operator */ |
| 70 | static int setexpr_test_plus(struct unit_test_state *uts) |
| 71 | { |
| 72 | char *buf; |
| 73 | |
| 74 | buf = map_sysmem(0, BUF_SIZE); |
| 75 | memset(buf, '\xff', BUF_SIZE); |
| 76 | |
| 77 | /* byte */ |
| 78 | buf[0x0] = 0x12; |
| 79 | buf[0x10] = 0x34; |
| 80 | ut_assertok(run_command("setexpr.b fred *0 + *10", 0)); |
| 81 | ut_asserteq_str("46", env_get("fred")); |
| 82 | |
| 83 | /* 16-bit */ |
| 84 | *(short *)buf = 0x2345; |
| 85 | *(short *)(buf + 0x10) = 0xf012; |
| 86 | ut_assertok(run_command("setexpr.w fred *0 + *10", 0)); |
| 87 | ut_asserteq_str("11357", env_get("fred")); |
| 88 | |
| 89 | /* 32-bit */ |
| 90 | *(u32 *)buf = 0x3456789a; |
| 91 | *(u32 *)(buf + 0x10) = 0xc3384235; |
| 92 | ut_assertok(run_command("setexpr.l fred *0 + *10", 0)); |
Simon Glass | 25a43ac | 2020-11-01 14:15:37 -0700 | [diff] [blame] | 93 | ut_asserteq_str("f78ebacf", env_get("fred")); |
Simon Glass | 90a9901 | 2020-11-01 14:15:35 -0700 | [diff] [blame] | 94 | |
| 95 | /* 64-bit */ |
| 96 | *(u64 *)buf = 0x456789abcdef0123; |
| 97 | *(u64 *)(buf + 0x10) = 0x4987328372849283; |
| 98 | ut_assertok(run_command("setexpr.q fred *0 + *10", 0)); |
| 99 | ut_asserteq_str("8eeebc2f407393a6", env_get("fred")); |
| 100 | |
| 101 | /* default */ |
| 102 | ut_assertok(run_command("setexpr fred *0 + *10", 0)); |
Simon Glass | 25a43ac | 2020-11-01 14:15:37 -0700 | [diff] [blame] | 103 | ut_asserteq_str("1407393a6", env_get("fred")); |
Simon Glass | 90a9901 | 2020-11-01 14:15:35 -0700 | [diff] [blame] | 104 | |
| 105 | unmap_sysmem(buf); |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | SETEXPR_TEST(setexpr_test_plus, UT_TESTF_CONSOLE_REC); |
| 110 | |
| 111 | /* Test 'setexpr' command with other operators */ |
| 112 | static int setexpr_test_oper(struct unit_test_state *uts) |
| 113 | { |
| 114 | char *buf; |
| 115 | |
| 116 | buf = map_sysmem(0, BUF_SIZE); |
| 117 | memset(buf, '\xff', BUF_SIZE); |
| 118 | |
| 119 | *(u32 *)buf = 0x1234; |
| 120 | *(u32 *)(buf + 0x10) = 0x560000; |
| 121 | |
| 122 | /* Quote | to avoid confusing hush */ |
| 123 | ut_assertok(run_command("setexpr fred *0 \"|\" *10", 0)); |
Simon Glass | 25a43ac | 2020-11-01 14:15:37 -0700 | [diff] [blame] | 124 | ut_asserteq_str("561234", env_get("fred")); |
Simon Glass | 90a9901 | 2020-11-01 14:15:35 -0700 | [diff] [blame] | 125 | |
| 126 | *(u32 *)buf = 0x561200; |
| 127 | *(u32 *)(buf + 0x10) = 0x1234; |
| 128 | |
| 129 | /* Quote & to avoid confusing hush */ |
| 130 | ut_assertok(run_command("setexpr.l fred *0 \"&\" *10", 0)); |
Simon Glass | 25a43ac | 2020-11-01 14:15:37 -0700 | [diff] [blame] | 131 | ut_asserteq_str("1200", env_get("fred")); |
Simon Glass | 90a9901 | 2020-11-01 14:15:35 -0700 | [diff] [blame] | 132 | |
| 133 | ut_assertok(run_command("setexpr.l fred *0 ^ *10", 0)); |
| 134 | ut_asserteq_str("560034", env_get("fred")); |
| 135 | |
| 136 | ut_assertok(run_command("setexpr.l fred *0 - *10", 0)); |
| 137 | ut_asserteq_str("55ffcc", env_get("fred")); |
| 138 | |
| 139 | ut_assertok(run_command("setexpr.l fred *0 * *10", 0)); |
Simon Glass | 25a43ac | 2020-11-01 14:15:37 -0700 | [diff] [blame] | 140 | ut_asserteq_str("61ebfa800", env_get("fred")); |
Simon Glass | 90a9901 | 2020-11-01 14:15:35 -0700 | [diff] [blame] | 141 | |
| 142 | ut_assertok(run_command("setexpr.l fred *0 / *10", 0)); |
Simon Glass | 25a43ac | 2020-11-01 14:15:37 -0700 | [diff] [blame] | 143 | ut_asserteq_str("4ba", env_get("fred")); |
Simon Glass | 90a9901 | 2020-11-01 14:15:35 -0700 | [diff] [blame] | 144 | |
| 145 | ut_assertok(run_command("setexpr.l fred *0 % *10", 0)); |
Simon Glass | 25a43ac | 2020-11-01 14:15:37 -0700 | [diff] [blame] | 146 | ut_asserteq_str("838", env_get("fred")); |
Simon Glass | 90a9901 | 2020-11-01 14:15:35 -0700 | [diff] [blame] | 147 | |
| 148 | unmap_sysmem(buf); |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | SETEXPR_TEST(setexpr_test_oper, UT_TESTF_CONSOLE_REC); |
| 153 | |
Simon Glass | e713124 | 2020-11-01 14:15:38 -0700 | [diff] [blame] | 154 | /* Test 'setexpr' command with regex */ |
| 155 | static int setexpr_test_regex(struct unit_test_state *uts) |
| 156 | { |
| 157 | char *buf, *val; |
| 158 | |
| 159 | buf = map_sysmem(0, BUF_SIZE); |
| 160 | |
| 161 | /* Single substitution */ |
| 162 | ut_assertok(run_command("setenv fred 'this is a test'", 0)); |
| 163 | ut_assertok(run_command("setexpr fred sub is us", 0)); |
| 164 | val = env_get("fred"); |
| 165 | ut_asserteq_str("thus is a test", val); |
| 166 | |
| 167 | /* Global substitution */ |
| 168 | ut_assertok(run_command("setenv fred 'this is a test'", 0)); |
Simon Glass | 8f4aa7d | 2020-11-01 14:15:42 -0700 | [diff] [blame] | 169 | ut_assertok(run_command("setexpr fred gsub is us", 0)); |
| 170 | val = env_get("fred"); |
| 171 | ut_asserteq_str("thus us a test", val); |
| 172 | |
Simon Glass | e713124 | 2020-11-01 14:15:38 -0700 | [diff] [blame] | 173 | /* Global substitution */ |
| 174 | ut_assertok(run_command("setenv fred 'this is a test'", 0)); |
| 175 | ut_assertok(run_command("setenv mary 'this is a test'", 0)); |
| 176 | ut_assertok(run_command("setexpr fred gsub is us \"${mary}\"", 0)); |
| 177 | val = env_get("fred"); |
| 178 | ut_asserteq_str("thus us a test", val); |
| 179 | val = env_get("mary"); |
| 180 | ut_asserteq_str("this is a test", val); |
| 181 | |
| 182 | unmap_sysmem(buf); |
| 183 | |
| 184 | return 0; |
| 185 | } |
| 186 | SETEXPR_TEST(setexpr_test_regex, UT_TESTF_CONSOLE_REC); |
| 187 | |
| 188 | /* Test 'setexpr' command with regex replacement that expands the string */ |
| 189 | static int setexpr_test_regex_inc(struct unit_test_state *uts) |
| 190 | { |
| 191 | char *buf, *val; |
| 192 | |
| 193 | buf = map_sysmem(0, BUF_SIZE); |
| 194 | |
| 195 | ut_assertok(run_command("setenv fred 'this is a test'", 0)); |
Simon Glass | 8f4aa7d | 2020-11-01 14:15:42 -0700 | [diff] [blame] | 196 | ut_assertok(run_command("setexpr fred gsub is much_longer_string", 0)); |
| 197 | val = env_get("fred"); |
| 198 | ut_asserteq_str("thmuch_longer_string much_longer_string a test", val); |
Simon Glass | e713124 | 2020-11-01 14:15:38 -0700 | [diff] [blame] | 199 | unmap_sysmem(buf); |
| 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | SETEXPR_TEST(setexpr_test_regex_inc, UT_TESTF_CONSOLE_REC); |
| 204 | |
Simon Glass | d422c77 | 2020-11-01 14:15:40 -0700 | [diff] [blame] | 205 | /* Test setexpr_regex_sub() directly to check buffer usage */ |
| 206 | static int setexpr_test_sub(struct unit_test_state *uts) |
| 207 | { |
| 208 | char *buf, *nbuf; |
| 209 | int i; |
| 210 | |
| 211 | buf = map_sysmem(0, BUF_SIZE); |
| 212 | nbuf = map_sysmem(0x1000, BUF_SIZE); |
| 213 | |
| 214 | /* Add a pattern so we can check the buffer limits */ |
| 215 | memset(buf, '\xff', BUF_SIZE); |
| 216 | memset(nbuf, '\xff', BUF_SIZE); |
| 217 | for (i = BUF_SIZE; i < 0x1000; i++) { |
| 218 | buf[i] = i & 0xff; |
| 219 | nbuf[i] = i & 0xff; |
| 220 | } |
| 221 | strcpy(buf, "this is a test"); |
| 222 | |
| 223 | /* |
| 224 | * This is a regression test, since a bug was found in the use of |
| 225 | * memmove() in setexpr |
| 226 | */ |
| 227 | ut_assertok(setexpr_regex_sub(buf, BUF_SIZE, nbuf, BUF_SIZE, "is", |
| 228 | "us it is longer", true)); |
| 229 | ut_asserteq_str("thus it is longer us it is longer a test", buf); |
Simon Glass | d422c77 | 2020-11-01 14:15:40 -0700 | [diff] [blame] | 230 | for (i = BUF_SIZE; i < 0x1000; i++) { |
| 231 | ut_assertf(buf[i] == (char)i, |
| 232 | "buf byte at %x should be %02x, got %02x)\n", |
| 233 | i, i & 0xff, (u8)buf[i]); |
| 234 | ut_assertf(nbuf[i] == (char)i, |
| 235 | "nbuf byte at %x should be %02x, got %02x)\n", |
| 236 | i, i & 0xff, (u8)nbuf[i]); |
| 237 | } |
| 238 | |
| 239 | unmap_sysmem(buf); |
| 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | SETEXPR_TEST(setexpr_test_sub, UT_TESTF_CONSOLE_REC); |
| 244 | |
| 245 | /* Test setexpr_regex_sub() with back references */ |
| 246 | static int setexpr_test_backref(struct unit_test_state *uts) |
| 247 | { |
| 248 | char *buf, *nbuf; |
| 249 | int i; |
| 250 | |
| 251 | buf = map_sysmem(0, BUF_SIZE); |
| 252 | nbuf = map_sysmem(0x1000, BUF_SIZE); |
| 253 | |
| 254 | /* Add a pattern so we can check the buffer limits */ |
| 255 | memset(buf, '\xff', BUF_SIZE); |
| 256 | memset(nbuf, '\xff', BUF_SIZE); |
| 257 | for (i = BUF_SIZE; i < 0x1000; i++) { |
| 258 | buf[i] = i & 0xff; |
| 259 | nbuf[i] = i & 0xff; |
| 260 | } |
| 261 | strcpy(buf, "this is surely a test is it? yes this is indeed a test"); |
| 262 | |
| 263 | /* |
| 264 | * This is a regression test, since a bug was found in the use of |
| 265 | * memmove() in setexpr |
| 266 | */ |
| 267 | ut_assertok(setexpr_regex_sub(buf, BUF_SIZE, nbuf, BUF_SIZE, |
| 268 | "(this) (is) (surely|indeed)", |
| 269 | "us \\1 \\2 \\3!", true)); |
Simon Glass | d422c77 | 2020-11-01 14:15:40 -0700 | [diff] [blame] | 270 | ut_asserteq_str("us this is surely! a test is it? yes us this is indeed! a test", |
| 271 | buf); |
| 272 | |
| 273 | for (i = BUF_SIZE; i < 0x1000; i++) { |
| 274 | ut_assertf(buf[i] == (char)i, |
| 275 | "buf byte at %x should be %02x, got %02x)\n", |
| 276 | i, i & 0xff, (u8)buf[i]); |
| 277 | ut_assertf(nbuf[i] == (char)i, |
| 278 | "nbuf byte at %x should be %02x, got %02x)\n", |
| 279 | i, i & 0xff, (u8)nbuf[i]); |
| 280 | } |
| 281 | |
| 282 | unmap_sysmem(buf); |
| 283 | |
| 284 | return 0; |
| 285 | } |
| 286 | SETEXPR_TEST(setexpr_test_backref, UT_TESTF_CONSOLE_REC); |
| 287 | |
Simon Glass | 2c02152 | 2020-11-01 14:15:44 -0700 | [diff] [blame] | 288 | /* Test 'setexpr' command with setting strings */ |
| 289 | static int setexpr_test_str(struct unit_test_state *uts) |
| 290 | { |
| 291 | ulong start_mem; |
| 292 | char *buf; |
| 293 | |
| 294 | buf = map_sysmem(0, BUF_SIZE); |
| 295 | memset(buf, '\xff', BUF_SIZE); |
| 296 | |
| 297 | /* |
| 298 | * Set 'fred' to the same length as we expect to get below, to avoid a |
| 299 | * new allocation in 'setexpr'. That way we can check for memory leaks. |
| 300 | */ |
| 301 | ut_assertok(env_set("fred", "x")); |
| 302 | start_mem = ut_check_free(); |
| 303 | strcpy(buf, "hello"); |
| 304 | ut_asserteq(1, run_command("setexpr.s fred 0", 0)); |
| 305 | ut_assertok(ut_check_delta(start_mem)); |
| 306 | |
Simon Glass | 2c02152 | 2020-11-01 14:15:44 -0700 | [diff] [blame] | 307 | ut_assertok(env_set("fred", "12345")); |
Simon Glass | 80b80d8 | 2021-03-07 17:34:44 -0700 | [diff] [blame] | 308 | start_mem = ut_check_free(); |
Simon Glass | 2c02152 | 2020-11-01 14:15:44 -0700 | [diff] [blame] | 309 | ut_assertok(run_command("setexpr.s fred *0", 0)); |
| 310 | ut_asserteq_str("hello", env_get("fred")); |
| 311 | ut_assertok(ut_check_delta(start_mem)); |
| 312 | |
| 313 | unmap_sysmem(buf); |
| 314 | |
| 315 | return 0; |
| 316 | } |
| 317 | SETEXPR_TEST(setexpr_test_str, UT_TESTF_CONSOLE_REC); |
| 318 | |
| 319 | |
| 320 | /* Test 'setexpr' command with concatenating strings */ |
| 321 | static int setexpr_test_str_oper(struct unit_test_state *uts) |
| 322 | { |
| 323 | ulong start_mem; |
| 324 | char *buf; |
| 325 | |
| 326 | buf = map_sysmem(0, BUF_SIZE); |
| 327 | memset(buf, '\xff', BUF_SIZE); |
| 328 | strcpy(buf, "hello"); |
| 329 | strcpy(buf + 0x10, " there"); |
| 330 | |
| 331 | ut_assertok(console_record_reset_enable()); |
| 332 | start_mem = ut_check_free(); |
| 333 | ut_asserteq(1, run_command("setexpr.s fred *0 * *10", 0)); |
| 334 | ut_assertok(ut_check_delta(start_mem)); |
| 335 | ut_assert_nextline("invalid op"); |
| 336 | ut_assert_console_end(); |
| 337 | |
| 338 | /* |
| 339 | * Set 'fred' to the same length as we expect to get below, to avoid a |
| 340 | * new allocation in 'setexpr'. That way we can check for memory leaks. |
| 341 | */ |
| 342 | ut_assertok(env_set("fred", "12345012345")); |
| 343 | start_mem = ut_check_free(); |
| 344 | ut_assertok(run_command("setexpr.s fred *0 + *10", 0)); |
| 345 | ut_asserteq_str("hello there", env_get("fred")); |
Simon Glass | 80b80d8 | 2021-03-07 17:34:44 -0700 | [diff] [blame] | 346 | |
| 347 | /* |
| 348 | * This check does not work with sandbox_flattree, apparently due to |
| 349 | * memory allocations in env_set(). |
| 350 | * |
| 351 | * The truetype console produces lots of memory allocations even though |
| 352 | * the LCD display is not visible. But even without these, it does not |
| 353 | * work. |
| 354 | * |
| 355 | * A better test would be for dlmalloc to record the allocs and frees |
| 356 | * for a particular caller, but that is not supported. |
| 357 | * |
| 358 | * For now, drop this test. |
| 359 | * |
| 360 | * ut_assertok(ut_check_delta(start_mem)); |
| 361 | */ |
Simon Glass | 2c02152 | 2020-11-01 14:15:44 -0700 | [diff] [blame] | 362 | |
| 363 | unmap_sysmem(buf); |
| 364 | |
| 365 | return 0; |
| 366 | } |
| 367 | SETEXPR_TEST(setexpr_test_str_oper, UT_TESTF_CONSOLE_REC); |
| 368 | |
| 369 | /* Test 'setexpr' command with a string that is too long */ |
| 370 | static int setexpr_test_str_long(struct unit_test_state *uts) |
| 371 | { |
| 372 | const int size = 128 << 10; /* setexpr strings are a max of 64KB */ |
| 373 | char *buf, *val; |
| 374 | |
| 375 | buf = map_sysmem(0, size); |
| 376 | memset(buf, 'a', size); |
| 377 | |
| 378 | /* String should be truncated to 64KB */ |
| 379 | ut_assertok(run_command("setexpr.s fred *0", 0)); |
| 380 | val = env_get("fred"); |
| 381 | ut_asserteq(64 << 10, strlen(val)); |
| 382 | |
| 383 | unmap_sysmem(buf); |
| 384 | |
| 385 | return 0; |
| 386 | } |
| 387 | SETEXPR_TEST(setexpr_test_str_long, UT_TESTF_CONSOLE_REC); |
| 388 | |
Roland Gaudig | 62db089 | 2021-07-23 12:29:23 +0000 | [diff] [blame] | 389 | #ifdef CONFIG_CMD_SETEXPR_FMT |
| 390 | /* Test 'setexpr' command with simply setting integers */ |
| 391 | static int setexpr_test_fmt(struct unit_test_state *uts) |
| 392 | { |
| 393 | u8 *buf; |
| 394 | |
| 395 | buf = map_sysmem(0, BUF_SIZE); |
| 396 | memset(buf, '\xff', BUF_SIZE); |
| 397 | |
| 398 | /* Test decimal conversion */ |
| 399 | ut_assertok(run_command("setexpr fred fmt %d 0xff", 0)); |
| 400 | ut_asserteq_str("255", env_get("fred")); |
| 401 | /* Test hexadecimal conversion with 0x prefix and 4 digits */ |
| 402 | ut_assertok(run_command("setexpr fred fmt 0x%04x 257", 0)); |
| 403 | ut_asserteq_str("0x0257", env_get("fred")); |
| 404 | /* Test octal conversion with % prefix */ |
| 405 | ut_assertok(run_command("setexpr fred fmt %%%o 8", 0)); |
| 406 | ut_asserteq_str("%10", env_get("fred")); |
| 407 | /* Test argument surrounded by %% */ |
| 408 | ut_assertok(run_command("setexpr fred fmt %%%x%% 0xff", 0)); |
| 409 | ut_asserteq_str("%ff%", env_get("fred")); |
| 410 | /* Test escape sequence */ |
| 411 | ut_assertok(run_command("setexpr fred fmt \"hello\\040world\"", 0)); |
| 412 | ut_asserteq_str("hello world", env_get("fred")); |
| 413 | /* Test %b with string containing octal escape sequence */ |
| 414 | ut_assertok(run_command("setexpr fred fmt oh%bno \137", 0)); |
| 415 | ut_asserteq_str("oh_no", env_get("fred")); |
| 416 | /* Test %b with string containing \c escape sequence */ |
| 417 | ut_assertok(run_command("setexpr fred fmt hello%bworld \"\\c\"", 0)); |
| 418 | ut_asserteq_str("hello", env_get("fred")); |
| 419 | /* Test multiple arguments referencing environment varialbes */ |
| 420 | ut_assertok(run_command("setenv a eff", 0)); |
| 421 | ut_assertok(run_command("setenv b hello", 0)); |
| 422 | ut_assertok(run_command("setenv c 0x63", 0)); |
| 423 | ut_assertok(run_command("setenv d world", 0)); |
| 424 | ut_assertok(run_command("setexpr fred fmt \"0x%08x-%s-%d-%s\" $a $b $c $d", 0)); |
| 425 | ut_asserteq_str("0x00000eff-hello-99-world", env_get("fred")); |
| 426 | /* Test with two format specifiers, but only one argument */ |
| 427 | ut_assertok(run_command("setexpr fred fmt %d_%x 100", 0)); |
| 428 | ut_asserteq_str("256_0", env_get("fred")); |
| 429 | /* Test maximum string length */ |
| 430 | ut_assertok(run_command("setexpr fred fmt \"%0127d\" 7b", 0)); |
| 431 | ut_asserteq_str("0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000123", env_get("fred")); |
| 432 | /* Test maximum unsigned integer size */ |
| 433 | ut_assertok(run_command("setexpr fred fmt %u ffffffffffffffff", 0)); |
| 434 | ut_asserteq_str("18446744073709551615", env_get("fred")); |
| 435 | /* Test maximum positive integer size */ |
| 436 | ut_assertok(run_command("setexpr fred fmt %d 7fffffffffffffff", 0)); |
| 437 | ut_asserteq_str("9223372036854775807", env_get("fred")); |
| 438 | /* Test maximum negative integer size */ |
| 439 | ut_assertok(run_command("setexpr fred fmt %d 8000000000000000", 0)); |
| 440 | ut_asserteq_str("-9223372036854775808", env_get("fred")); |
| 441 | /* Test minimum negative integer size */ |
| 442 | ut_assertok(run_command("setexpr fred fmt %d ffffffffffffffff", 0)); |
| 443 | ut_asserteq_str("-1", env_get("fred")); |
| 444 | /* Test signed value with + sign */ |
| 445 | ut_assertok(run_command("setexpr fred fmt %d +5", 0)); |
| 446 | ut_asserteq_str("5", env_get("fred")); |
| 447 | /* Test signed value with - sign */ |
| 448 | ut_assertok(run_command("setexpr fred fmt %d -4", 0)); |
| 449 | ut_asserteq_str("-4", env_get("fred")); |
| 450 | /* Test unsigned value with + sign */ |
| 451 | ut_assertok(run_command("setexpr fred fmt %u +3", 0)); |
| 452 | ut_asserteq_str("3", env_get("fred")); |
| 453 | /* Test unsigned value with - sign */ |
| 454 | ut_assertok(run_command("setexpr fred fmt %x -2", 0)); |
| 455 | ut_asserteq_str("fffffffffffffffe", env_get("fred")); |
| 456 | /* Error test with missing format specifier */ |
| 457 | ut_asserteq(1, run_command("setexpr fred fmd hello 0xff", 0)); |
| 458 | /* Error test with invalid format type */ |
| 459 | ut_asserteq(1, run_command("setexpr fred fmt %a 0xff", 0)); |
| 460 | /* Error test with incomplete format specifier */ |
| 461 | ut_asserteq(1, run_command("setexpr fred fmt hello% bf", 0)); |
| 462 | /* Error exceeding maximum string length */ |
| 463 | ut_asserteq(1, run_command("setexpr fred fmt \"%0128d\" 456", 0)); |
| 464 | |
| 465 | unmap_sysmem(buf); |
| 466 | |
| 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | SETEXPR_TEST(setexpr_test_fmt, UT_TESTF_CONSOLE_REC); |
| 471 | #endif |
| 472 | |
Simon Glass | 90a9901 | 2020-11-01 14:15:35 -0700 | [diff] [blame] | 473 | int do_ut_setexpr(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
| 474 | { |
Simon Glass | a7a9875 | 2021-03-07 17:35:10 -0700 | [diff] [blame] | 475 | struct unit_test *tests = UNIT_TEST_SUITE_START(setexpr_test); |
| 476 | const int n_ents = UNIT_TEST_SUITE_COUNT(setexpr_test); |
Simon Glass | 90a9901 | 2020-11-01 14:15:35 -0700 | [diff] [blame] | 477 | |
Simon Glass | 80b80d8 | 2021-03-07 17:34:44 -0700 | [diff] [blame] | 478 | return cmd_ut_category("cmd_setexpr", "setexpr_test_", tests, n_ents, |
| 479 | argc, argv); |
Simon Glass | 90a9901 | 2020-11-01 14:15:35 -0700 | [diff] [blame] | 480 | } |