"Das U-Boot" Source Tree
at jcs/rk3128 109 lines 3.2 kB view raw
1// SPDX-License-Identifier: GPL-2.0+ 2/* 3 * Copyright (c) 2012, The Chromium Authors 4 */ 5 6#define DEBUG 7 8#include <command.h> 9#include <env.h> 10#include <log.h> 11#include <string.h> 12#include <linux/errno.h> 13#include <test/cmd.h> 14#include <test/ut.h> 15 16static const char test_cmd[] = "setenv list 1\n setenv list ${list}2; " 17 "setenv list ${list}3\0" 18 "setenv list ${list}4"; 19 20static int command_test(struct unit_test_state *uts) 21{ 22 char long_str[CONFIG_SYS_CBSIZE + 42]; 23 24 printf("%s: Testing commands\n", __func__); 25 run_command("env default -f -a", 0); 26 27 /* commands separated by \n */ 28 run_command_list("setenv list 1\n setenv list ${list}1", -1, 0); 29 ut_assert(!strcmp("11", env_get("list"))); 30 31 /* command followed by \n and nothing else */ 32 run_command_list("setenv list 1${list}\n", -1, 0); 33 ut_assert(!strcmp("111", env_get("list"))); 34 35 /* a command string with \0 in it. Stuff after \0 should be ignored */ 36 run_command("setenv list", 0); 37 run_command_list(test_cmd, sizeof(test_cmd), 0); 38 ut_assert(!strcmp("123", env_get("list"))); 39 40 /* 41 * a command list where we limit execution to only the first command 42 * using the length parameter. 43 */ 44 run_command_list("setenv list 1\n setenv list ${list}2; " 45 "setenv list ${list}3", strlen("setenv list 1"), 0); 46 ut_assert(!strcmp("1", env_get("list"))); 47 48 ut_assertok(run_command("echo", 0)); 49 ut_assertok(run_command_list("echo", -1, 0)); 50 51 if (IS_ENABLED(CONFIG_HUSH_PARSER)) { 52 ut_asserteq(1, run_command("false", 0)); 53 ut_asserteq(1, run_command_list("false", -1, 0)); 54 run_command("setenv foo 'setenv black 1\nsetenv adder 2'", 0); 55 run_command("run foo", 0); 56 ut_assertnonnull(env_get("black")); 57 ut_asserteq(0, strcmp("1", env_get("black"))); 58 ut_assertnonnull(env_get("adder")); 59 ut_asserteq(0, strcmp("2", env_get("adder"))); 60 ut_assertok(run_command("", 0)); 61 ut_assertok(run_command(" ", 0)); 62 } 63 64 ut_asserteq(1, run_command("'", 0)); 65 66 /* Variadic function test-cases */ 67 if (IS_ENABLED(CONFIG_HUSH_PARSER)) { 68#pragma GCC diagnostic push 69#pragma GCC diagnostic ignored "-Wformat-zero-length" 70 ut_assertok(run_commandf("")); 71#pragma GCC diagnostic pop 72 ut_assertok(run_commandf(" ")); 73 } 74 ut_asserteq(1, run_commandf("'")); 75 76 ut_assertok(run_commandf("env %s %s", "delete -f", "list")); 77 /* 78 * Expected: "## Error: "list" not defined" 79 * (disabled to avoid pytest bailing out) 80 * 81 * ut_asserteq(1, run_commandf("printenv list")); 82 */ 83 84 memset(long_str, 'x', sizeof(long_str)); 85 ut_asserteq(-ENOSPC, run_commandf("Truncation case: %s", long_str)); 86 87 if (IS_ENABLED(CONFIG_HUSH_PARSER)) { 88 ut_assertok(run_commandf("env %s %s %s %s", "delete -f", 89 "adder", "black", "foo")); 90 ut_assertok(run_commandf( 91 "setenv foo 'setenv %s 1\nsetenv %s 2'", 92 "black", "adder")); 93 ut_assertok(run_command("run foo", 0)); 94 ut_assertnonnull(env_get("black")); 95 ut_asserteq(0, strcmp("1", env_get("black"))); 96 ut_assertnonnull(env_get("adder")); 97 ut_asserteq(0, strcmp("2", env_get("adder"))); 98 } 99 100 /* Clean up before exit */ 101 ut_assertok(run_command("env default -f -a", 0)); 102 103 /* put back the FDT environment */ 104 ut_assertok(env_set("from_fdt", "yes")); 105 106 printf("%s: Everything went swimmingly\n", __func__); 107 return 0; 108} 109CMD_TEST(command_test, 0);