Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 87DD6C433FE for ; Sun, 6 Feb 2022 18:39:01 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 781EB83C2C; Sun, 6 Feb 2022 19:37:57 +0100 (CET) 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="XPTKzFYh"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 1BBED83BD8; Sun, 6 Feb 2022 19:37:51 +0100 (CET) Received: from mail-wm1-x331.google.com (mail-wm1-x331.google.com [IPv6:2a00:1450:4864:20::331]) (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 1BD9883BCE for ; Sun, 6 Feb 2022 19:37:40 +0100 (CET) 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-wm1-x331.google.com with SMTP id r7so8412003wmq.5 for ; Sun, 06 Feb 2022 10:37:40 -0800 (PST) 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=Fzxu81StPME8QHWqus+XbT2Ztco/drwY2Xst8GtGsK4=; b=XPTKzFYhX/s8xRv1UzYSAxKUhCU2gP5mDYuVAUheygCZClELZZ9KoPlCAgOEV4sMaM 4BvQrrlboNX1IxYrEihgj5sVa7WdrCDnEVMMSJ3UxXc14YVmJCOaGyPa6jemadWZ9D0W iM3EPIGZaBouIcak5q7AfA7Cmks8cdDQFpTXc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Fzxu81StPME8QHWqus+XbT2Ztco/drwY2Xst8GtGsK4=; b=fN6LKGlbLhtk+ZK8b7tcgAdO3/HBzqPpwOBRXRixccUhq9e9feUsVx+DExYbodc2Yv AI0A6o07GI4XlvFLgRv3c0IPSg6PfmQVndao1voow6jTpufnta3p9wFbLhotSD/S6odn UKQFOAtLuJyorrU5p6XSeGx5HU6CjVaTV0ogIISZm52SIErPeTNQHYxQSSXyLcX3WZcH qfhvRFPd2qVcFNVer665k/bpwe0BiA/oy2xnLlpDUXBGU+VirSoKKn1Qu7i8Um5xYvyg Pq+S6h9B/7ULHv3lWrlBvw+0zWmbB2rswSARd5CbTILzApVNn62l43kg3FP04ydZkiBC HdPQ== X-Gm-Message-State: AOAM533xWi7Or9pSYLh7cJuZcRAMnllL3kBNBHDMJvHl64KObWQQVCJM HElFxskAH6WTVtmqeV/CIDCjYwWJa79isQ== X-Google-Smtp-Source: ABdhPJwzcaCKpeLWHGIxePJJWsAxEO8m54pma41lxST4J3TnDABnxmh2Di6bA6Btlj3bk0tuIs3k/Q== X-Received: by 2002:a05:600c:1c09:: with SMTP id j9mr11367316wms.88.1644172659723; Sun, 06 Feb 2022 10:37:39 -0800 (PST) Received: from pwmachine.home ([2a01:cb14:983:1f00:e5b3:218b:cb92:2549]) by smtp.gmail.com with ESMTPSA id ay29sm7095011wmb.38.2022.02.06.10.37.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 06 Feb 2022 10:37:39 -0800 (PST) From: Francis Laniel To: u-boot@lists.denx.de Cc: Marek Behun , Michael Nazzareno Trimarchi , Simon Glass , Wolfgang Denk , Harald Seiler , Francis Laniel Subject: [RFC PATCH v2 04/21] test: hush: Test hush variable expansion Date: Sun, 6 Feb 2022 19:37:00 +0100 Message-Id: <20220206183717.57446-5-francis.laniel@amarulasolutions.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220206183717.57446-1-francis.laniel@amarulasolutions.com> References: <20220206183717.57446-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.39 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.5 at phobos.denx.de X-Virus-Status: Clean Verifies shell variables are replaced by their values. Signed-off-by: Francis Laniel --- test/hush/Makefile | 1 + test/hush/dollar.c | 175 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 176 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..2614517cd2 --- /dev/null +++ b/test/hush/dollar.c @@ -0,0 +1,175 @@ +// 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)); + ut_assert_nextline("%s", ""); + ut_assert_console_end(); + + ut_assertok(run_command("echo ${dollar_foo}", 0)); + ut_assert_nextline("%s", ""); + 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(); + * + * So, let's reset output: + */ + console_record_reset_enable(); + + 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 priority 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