Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1FE8BC4320A for ; Fri, 20 Aug 2021 15:50:03 +0000 (UTC) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 9D16C61152 for ; Fri, 20 Aug 2021 15:50:02 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 9D16C61152 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=amarulasolutions.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=lists.denx.de Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 6274C82E3F; Fri, 20 Aug 2021 17:49:37 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=amarulasolutions.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=amarulasolutions.com header.i=@amarulasolutions.com header.b="ehHVc9ep"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 17E1382999; Fri, 20 Aug 2021 17:49:17 +0200 (CEST) Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id A0A5282AC8 for ; Fri, 20 Aug 2021 17:49:09 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=amarulasolutions.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=francis.laniel@amarulasolutions.com Received: by mail-wr1-x42b.google.com with SMTP id x12so14861768wrr.11 for ; Fri, 20 Aug 2021 08:49:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amarulasolutions.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=6I+viyVgv5EBLH2pA83SXqxMv8TptRH3/+wZVX5H0mc=; b=ehHVc9epyzCupsr6CuO6Nu/E/mevFe96ijXA/SgXlTloKKqWZToEbFtP0phy3SMa+B K0rgRJChWCVv7CYGTO6D63cvMFmg8GvUQSWxutO4gVhQZl8xeF4wqMtTvQ2z2Q9dSUUl yGnXYjpBagKdaRcCZ2I0A6s++hvxeYuX3ik3I= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=6I+viyVgv5EBLH2pA83SXqxMv8TptRH3/+wZVX5H0mc=; b=rPaE4hHR/+dGGCte+Lm0A3h3wcSFS9C5KxD3scq6maKtKaNAmjkRkHe6HeQR+dtT5g KQe/PLILZd9No2LQpumIys3VLRwKlh9HtGa0a6bSH29CQlodhu1+ko5Ec38krOthYSj4 RGoKfQIwMjBj8PpIqi93441pDBH2Xl6iAdq1vCQcHtMPRV/94vHcsvafvpKpftSUPv0k 1bhsOpIOoOXdlBGUQJZUyACQ+O9/gPU1+MZ16W+UNcFtaC8lh0e0kokXzMLuS6Sa2S0P NUuZfhvKnqkIUQPRyRS9Cx+upzReog4F/5Yl++KDzYI1/IIf+Y0Hzl8nbs1UX/rP+3k4 DNJg== X-Gm-Message-State: AOAM530D+F1PNRNspdS9xLZqDxx5Z8oahDcqVoxMsg2qX1QsXblFCfIF 9YEk22u4SuaEA7K8vEJxJwIENp2h7O/Ybw== X-Google-Smtp-Source: ABdhPJz2XyiCATa+/IY52LzRA3Nd3mYS2hpuRO2xHhIpixlK+VOgkH7d8d/O2JsUCh/8lsgbbpuBbg== X-Received: by 2002:adf:eb4c:: with SMTP id u12mr11375542wrn.111.1629474549185; Fri, 20 Aug 2021 08:49:09 -0700 (PDT) Received: from pwmachine.home ([2a01:cb14:983:1f00:4e0c:e96d:5c36:fa68]) by smtp.gmail.com with ESMTPSA id c13sm2108846wru.73.2021.08.20.08.49.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Aug 2021 08:49:08 -0700 (PDT) From: Francis Laniel To: u-boot@lists.denx.de Cc: michael@amarulasolutions.com, Francis Laniel Subject: [RFC PATCH 4/6] test: hush: Test hush variable expansion. Date: Fri, 20 Aug 2021 17:48:37 +0200 Message-Id: <20210820154839.101107-5-francis.laniel@amarulasolutions.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210820154839.101107-1-francis.laniel@amarulasolutions.com> References: <20210820154839.101107-1-francis.laniel@amarulasolutions.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean This commit ensures shell variables are replaced by their values. Signed-off-by: Francis Laniel --- test/hush/Makefile | 1 + test/hush/dollar.c | 188 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 189 insertions(+) create mode 100644 test/hush/dollar.c diff --git a/test/hush/Makefile b/test/hush/Makefile index a3c9ae5106..feb4f71956 100644 --- a/test/hush/Makefile +++ b/test/hush/Makefile @@ -5,3 +5,4 @@ obj-y += cmd_ut_hush.o obj-y += if.o +obj-y += dollar.o diff --git a/test/hush/dollar.c b/test/hush/dollar.c new file mode 100644 index 0000000000..4f153958ae --- /dev/null +++ b/test/hush/dollar.c @@ -0,0 +1,188 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * (C) Copyright 2021 + * Francis Laniel, Amarula Solutions, francis.laniel@amarulasolutions.com + */ + +#include +#include +#include +#include +#include + +static int hush_test_simple_dollar(struct unit_test_state *uts) +{ + console_record_reset_enable(); + ut_assertok(run_command("echo $dollar_foo", 0)); +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wformat-zero-length" + /* + * Next line is empty as $dollar_foo was not set before, but compiler + * complains about the format being empty, so we disable this warning + * only for this line. + */ + ut_assert_nextline(""); +#pragma GCC diagnostic pop + ut_assert_console_end(); + + ut_assertok(run_command("echo ${dollar_foo}", 0)); +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wformat-zero-length" + /* + * Next line is empty as $dollar_foo was not set before, but compiler + * complains about the format being empty, so we disable this warning + * only for this line. + */ + ut_assert_nextline(""); +#pragma GCC diagnostic pop + ut_assert_console_end(); + + ut_assertok(run_command("dollar_foo=bar", 0)); + + ut_assertok(run_command("echo $dollar_foo", 0)); + ut_assert_nextline("bar"); + ut_assert_console_end(); + + ut_assertok(run_command("echo ${dollar_foo}", 0)); + ut_assert_nextline("bar"); + ut_assert_console_end(); + + /* + * R is way of giving raw string literals in C++. + * It means a "\n" is a R string literal will not be interpreted as line + * feed but printed as "\n". + * GCC provides it for C as an extension. + */ + ut_assertok(run_command(R"(dollar_foo=\$bar)", 0)); + + ut_assertok(run_command("echo $dollar_foo", 0)); + ut_assert_nextline("$bar"); + ut_assert_console_end(); + + ut_assertok(run_command("dollar_foo='$bar'", 0)); + + ut_assertok(run_command("echo $dollar_foo", 0)); + ut_assert_nextline("$bar"); + ut_assert_console_end(); + + ut_asserteq(1, run_command("dollar_foo=bar quux", 0)); + /* Next line contains error message. */ + ut_assert_skipline(); + ut_assert_console_end(); + + ut_asserteq(1, run_command("dollar_foo='bar quux", 0)); + /* Next line contains error message. */ + ut_assert_skipline(); + ut_assert_console_end(); + + ut_asserteq(1, run_command(R"(dollar_foo=bar quux")", 0)); + /* Two next lines contain error message. */ + ut_assert_skipline(); + ut_assert_skipline(); + ut_assert_console_end(); + + ut_assertok(run_command(R"(dollar_foo='bar "quux')", 0)); + + ut_assertok(run_command("echo $dollar_foo", 0)); + /* + * This one is buggy. + * ut_assert_nextline(R"(bar "quux)"); + * ut_assert_console_end(); + */ + + ut_asserteq(1, run_command(R"(dollar_foo="bar 'quux")", 0)); + /* Next line contains error message. */ + ut_assert_skipline(); + ut_assert_console_end(); + + ut_assertok(run_command("dollar_foo='bar quux'", 0)); + ut_assertok(run_command("echo $dollar_foo", 0)); + ut_assert_nextline("bar quux"); + ut_assert_console_end(); + + puts("Beware: this test set local variable dollar_foo and it cannot be unset!"); + + return 0; +} +HUSH_TEST(hush_test_simple_dollar, 0); + +static int hush_test_env_dollar(struct unit_test_state *uts) +{ + env_set("env_foo", "bar"); + console_record_reset_enable(); + + ut_assertok(run_command("echo $env_foo", 0)); + ut_assert_nextline("bar"); + ut_assert_console_end(); + + ut_assertok(run_command("echo ${env_foo}", 0)); + ut_assert_nextline("bar"); + ut_assert_console_end(); + + /* Environment variables have precedence over local variable. */ + ut_assertok(run_command("env_foo=quux", 0)); + ut_assertok(run_command("echo ${env_foo}", 0)); + ut_assert_nextline("bar"); + ut_assert_console_end(); + + /* Clean up setting the variable. */ + env_set("env_foo", NULL); + + puts("Beware: this test set local variable env_foo and it cannot be unset!"); + + return 0; +} +HUSH_TEST(hush_test_env_dollar, 0); + +static int hush_test_command_dollar(struct unit_test_state *uts) +{ + console_record_reset_enable(); + + ut_assertok(run_command(R"(dollar_bar="echo bar")", 0)); + + ut_assertok(run_command("$dollar_bar", 0)); + ut_assert_nextline("bar"); + ut_assert_console_end(); + + ut_assertok(run_command("${dollar_bar}", 0)); + ut_assert_nextline("bar"); + ut_assert_console_end(); + + ut_assertok(run_command(R"(dollar_bar="echo + bar")", 0)); + + ut_assertok(run_command("$dollar_bar", 0)); + ut_assert_nextline("bar"); + ut_assert_console_end(); + + ut_assertok(run_command(R"(dollar_bar='echo bar + ')", 0)); + + ut_assertok(run_command("$dollar_bar", 0)); + ut_assert_nextline("bar"); + ut_assert_console_end(); + + ut_assertok(run_command(R"(dollar_bar='echo bar\n')", 0)); + + ut_assertok(run_command("$dollar_bar", 0)); + ut_assert_nextline("barn"); + ut_assert_console_end(); + + ut_assertok(run_command("dollar_bar='echo $bar'", 0)); + + ut_assertok(run_command("$dollar_bar", 0)); + ut_assert_nextline("$bar"); + ut_assert_console_end(); + + ut_assertok(run_command("dollar_quux=quux", 0)); + ut_assertok(run_command(R"(dollar_bar="echo $dollar_quux")", 0)); + + ut_assertok(run_command("$dollar_bar", 0)); + ut_assert_nextline("quux"); + ut_assert_console_end(); + + puts("Beware: this test sets local variable dollar_bar and dollar_quux and they cannot be unset!"); + + return 0; +} +HUSH_TEST(hush_test_command_dollar, 0); -- 2.25.1