Simon Glass | a72007d | 2012-03-30 21:30:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012, The Chromium Authors |
| 3 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 4 | * SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | a72007d | 2012-03-30 21:30:58 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #define DEBUG |
| 8 | |
| 9 | #include <common.h> |
| 10 | |
| 11 | static const char test_cmd[] = "setenv list 1\n setenv list ${list}2; " |
| 12 | "setenv list ${list}3\0" |
| 13 | "setenv list ${list}4"; |
| 14 | |
| 15 | static int do_ut_cmd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 16 | { |
| 17 | printf("%s: Testing commands\n", __func__); |
Stephen Warren | 6748672 | 2014-02-03 13:24:23 -0700 | [diff] [blame] | 18 | run_command("env default -f -a", 0); |
Simon Glass | a72007d | 2012-03-30 21:30:58 +0000 | [diff] [blame] | 19 | |
| 20 | /* run a single command */ |
| 21 | run_command("setenv single 1", 0); |
| 22 | assert(!strcmp("1", getenv("single"))); |
| 23 | |
| 24 | /* make sure that compound statements work */ |
| 25 | #ifdef CONFIG_SYS_HUSH_PARSER |
| 26 | run_command("if test -n ${single} ; then setenv check 1; fi", 0); |
| 27 | assert(!strcmp("1", getenv("check"))); |
| 28 | run_command("setenv check", 0); |
| 29 | #endif |
| 30 | |
| 31 | /* commands separated by ; */ |
| 32 | run_command_list("setenv list 1; setenv list ${list}1", -1, 0); |
| 33 | assert(!strcmp("11", getenv("list"))); |
| 34 | |
| 35 | /* commands separated by \n */ |
| 36 | run_command_list("setenv list 1\n setenv list ${list}1", -1, 0); |
| 37 | assert(!strcmp("11", getenv("list"))); |
| 38 | |
| 39 | /* command followed by \n and nothing else */ |
| 40 | run_command_list("setenv list 1${list}\n", -1, 0); |
| 41 | assert(!strcmp("111", getenv("list"))); |
| 42 | |
| 43 | /* three commands in a row */ |
| 44 | run_command_list("setenv list 1\n setenv list ${list}2; " |
| 45 | "setenv list ${list}3", -1, 0); |
| 46 | assert(!strcmp("123", getenv("list"))); |
| 47 | |
| 48 | /* a command string with \0 in it. Stuff after \0 should be ignored */ |
| 49 | run_command("setenv list", 0); |
| 50 | run_command_list(test_cmd, sizeof(test_cmd), 0); |
| 51 | assert(!strcmp("123", getenv("list"))); |
| 52 | |
| 53 | /* |
| 54 | * a command list where we limit execution to only the first command |
| 55 | * using the length parameter. |
| 56 | */ |
| 57 | run_command_list("setenv list 1\n setenv list ${list}2; " |
| 58 | "setenv list ${list}3", strlen("setenv list 1"), 0); |
| 59 | assert(!strcmp("1", getenv("list"))); |
| 60 | |
Stephen Warren | f2afe70 | 2014-02-03 13:24:24 -0700 | [diff] [blame] | 61 | #ifdef CONFIG_SYS_HUSH_PARSER |
| 62 | /* Test the 'test' command */ |
| 63 | |
| 64 | #define HUSH_TEST(name, expr, expected_result) \ |
| 65 | run_command("if test " expr " ; then " \ |
| 66 | "setenv " #name "_" #expected_result " y; else " \ |
| 67 | "setenv " #name "_" #expected_result " n; fi", 0); \ |
| 68 | assert(!strcmp(#expected_result, getenv(#name "_" #expected_result))); |
| 69 | |
| 70 | /* Basic operators */ |
| 71 | HUSH_TEST(streq, "aaa = aaa", y); |
| 72 | HUSH_TEST(streq, "aaa = bbb", n); |
| 73 | |
| 74 | HUSH_TEST(strneq, "aaa != bbb", y); |
| 75 | HUSH_TEST(strneq, "aaa != aaa", n); |
| 76 | |
| 77 | HUSH_TEST(strlt, "aaa < bbb", y); |
| 78 | HUSH_TEST(strlt, "bbb < aaa", n); |
| 79 | |
| 80 | HUSH_TEST(strgt, "bbb > aaa", y); |
| 81 | HUSH_TEST(strgt, "aaa > bbb", n); |
| 82 | |
| 83 | HUSH_TEST(eq, "123 -eq 123", y); |
| 84 | HUSH_TEST(eq, "123 -eq 456", n); |
| 85 | |
| 86 | HUSH_TEST(ne, "123 -ne 456", y); |
| 87 | HUSH_TEST(ne, "123 -ne 123", n); |
| 88 | |
| 89 | HUSH_TEST(lt, "123 -lt 456", y); |
| 90 | HUSH_TEST(lt_eq, "123 -lt 123", n); |
| 91 | HUSH_TEST(lt, "456 -lt 123", n); |
| 92 | |
| 93 | HUSH_TEST(le, "123 -le 456", y); |
| 94 | HUSH_TEST(le_eq, "123 -le 123", y); |
| 95 | HUSH_TEST(le, "456 -le 123", n); |
| 96 | |
| 97 | HUSH_TEST(gt, "456 -gt 123", y); |
| 98 | HUSH_TEST(gt_eq, "123 -gt 123", n); |
| 99 | HUSH_TEST(gt, "123 -gt 456", n); |
| 100 | |
| 101 | HUSH_TEST(ge, "456 -ge 123", y); |
| 102 | HUSH_TEST(ge_eq, "123 -ge 123", y); |
| 103 | HUSH_TEST(ge, "123 -ge 456", n); |
| 104 | |
| 105 | HUSH_TEST(z, "-z \"\"", y); |
| 106 | HUSH_TEST(z, "-z \"aaa\"", n); |
| 107 | |
| 108 | HUSH_TEST(n, "-n \"aaa\"", y); |
| 109 | HUSH_TEST(n, "-n \"\"", n); |
| 110 | |
| 111 | /* Inversion of simple tests */ |
| 112 | HUSH_TEST(streq_inv, "! aaa = aaa", n); |
| 113 | HUSH_TEST(streq_inv, "! aaa = bbb", y); |
| 114 | |
| 115 | HUSH_TEST(streq_inv_inv, "! ! aaa = aaa", y); |
| 116 | HUSH_TEST(streq_inv_inv, "! ! aaa = bbb", n); |
| 117 | |
| 118 | /* Binary operators */ |
| 119 | HUSH_TEST(or_0_0, "aaa != aaa -o bbb != bbb", n); |
| 120 | HUSH_TEST(or_0_1, "aaa != aaa -o bbb = bbb", y); |
| 121 | HUSH_TEST(or_1_0, "aaa = aaa -o bbb != bbb", y); |
| 122 | HUSH_TEST(or_1_1, "aaa = aaa -o bbb = bbb", y); |
| 123 | |
| 124 | HUSH_TEST(and_0_0, "aaa != aaa -a bbb != bbb", n); |
| 125 | HUSH_TEST(and_0_1, "aaa != aaa -a bbb = bbb", n); |
| 126 | HUSH_TEST(and_1_0, "aaa = aaa -a bbb != bbb", n); |
| 127 | HUSH_TEST(and_1_1, "aaa = aaa -a bbb = bbb", y); |
| 128 | |
| 129 | /* Inversion within binary operators */ |
| 130 | HUSH_TEST(or_0_0_inv, "! aaa != aaa -o ! bbb != bbb", y); |
| 131 | HUSH_TEST(or_0_1_inv, "! aaa != aaa -o ! bbb = bbb", y); |
| 132 | HUSH_TEST(or_1_0_inv, "! aaa = aaa -o ! bbb != bbb", y); |
| 133 | HUSH_TEST(or_1_1_inv, "! aaa = aaa -o ! bbb = bbb", n); |
| 134 | |
| 135 | HUSH_TEST(or_0_0_inv_inv, "! ! aaa != aaa -o ! ! bbb != bbb", n); |
| 136 | HUSH_TEST(or_0_1_inv_inv, "! ! aaa != aaa -o ! ! bbb = bbb", y); |
| 137 | HUSH_TEST(or_1_0_inv_inv, "! ! aaa = aaa -o ! ! bbb != bbb", y); |
| 138 | HUSH_TEST(or_1_1_inv_inv, "! ! aaa = aaa -o ! ! bbb = bbb", y); |
| 139 | |
| 140 | #ifdef CONFIG_SANDBOX |
| 141 | /* |
| 142 | * File existence |
| 143 | * This assume U-Boot sandbox is run from the U-Boot build directory |
| 144 | */ |
| 145 | HUSH_TEST(e, "-e host - u-boot", y); |
| 146 | HUSH_TEST(e, "-e host - creating_this_file_breaks_uboot_unit_test", n); |
| 147 | #endif |
| 148 | #endif |
| 149 | |
Simon Glass | a72007d | 2012-03-30 21:30:58 +0000 | [diff] [blame] | 150 | printf("%s: Everything went swimmingly\n", __func__); |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | U_BOOT_CMD( |
| 155 | ut_cmd, 5, 1, do_ut_cmd, |
| 156 | "Very basic test of command parsers", |
| 157 | "" |
| 158 | ); |