From patchwork Sun Feb 6 18:36:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Francis Laniel X-Patchwork-Id: 1589016 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=amarulasolutions.com header.i=@amarulasolutions.com header.a=rsa-sha256 header.s=google header.b=d+y8fo9K; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4JsJ251Dt0z9s1l for ; Mon, 7 Feb 2022 05:38:37 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 23FB983BD0; Sun, 6 Feb 2022 19:37:52 +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="d+y8fo9K"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id C647F83BC8; Sun, 6 Feb 2022 19:37:46 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.2 Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [IPv6:2a00:1450:4864:20::32e]) (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 11EE683BAD for ; Sun, 6 Feb 2022 19:37:38 +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-x32e.google.com with SMTP id l123-20020a1c2581000000b0037b9d960079so4262000wml.0 for ; Sun, 06 Feb 2022 10:37:38 -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=qcmpFzkkVSPLhKl2sSb3SMM790zDAzNNRjA9cv+P13w=; b=d+y8fo9KzUjoadc9hVGghakh74eOuGDlwDnTMSPVWtH28/u+gX7USF4qKjgJZ8lOd/ vSrfIYvIWanSINGU3uexKiOw+6xdixOP0A6R222p5YHjUBc8e5nDpBuLynutOSyZE987 CvZMoUEop9tfOhHErY0Qt92ORKjxlechXqias= 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=qcmpFzkkVSPLhKl2sSb3SMM790zDAzNNRjA9cv+P13w=; b=nJVJjjBt8XQfslQBkZDfIcZz1JuvOdgOgo5BEHvRbsa4C16M2hErLFB+YWW162XRyl LO6+rb/p+iNaov9DTqNUwackxbV/WIGjroKqK3klstlYocyscbR4bHayfW8Ptzfv2zMy 0L4HrPVsv4gLlTcKwGBFCih+LP5cZ4xut5An5QbDQAXaSnBgFLsfpE7NojHqv07APIPu FBSAW3wPi81L6RD/JhsqSFhc6vgQMq7lYQuUhk0XFYZkU57cf3MBsegjM5QFUgDE0dhO oyR3vMdoSRRb7NzL0F3OISJ5F0i6cjm9uwCDqFNVCwQh4FYyrtDqzOsN/kGCbZ6GXRzJ 2YHA== X-Gm-Message-State: AOAM530G3wF0F0NgtyQGPUQQAYHohl9GwuFYcg3DRY0TJXkrdAbNdT1v pBGRlKYfRTKA6Mlh0Nwu58BL9EpRfdLgIw== X-Google-Smtp-Source: ABdhPJyCtCfZHxqWJ5UX2jVp08aKPSycCXgkU0JzSVltg8XUadI4etmUMjvlKOtuAuJlD+NtWO4Q3Q== X-Received: by 2002:a1c:f702:: with SMTP id v2mr11097979wmh.155.1644172657684; Sun, 06 Feb 2022 10:37:37 -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.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 06 Feb 2022 10:37:37 -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 02/21] test: hush: Test hush if/else Date: Sun, 6 Feb 2022 19:36:58 +0100 Message-Id: <20220206183717.57446-3-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 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 As asked in 9c6bf1715f6a ("test/py: hush_if_test: Add tests to cover octal/hex values"), this commit translates test_hush_if_test.py to a C test. Signed-off-by: Francis Laniel Reviewed-by: Simon Glass --- test/hush/Makefile | 1 + test/hush/if.c | 308 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 309 insertions(+) create mode 100644 test/hush/if.c diff --git a/test/hush/Makefile b/test/hush/Makefile index dfa2a92615..a3c9ae5106 100644 --- a/test/hush/Makefile +++ b/test/hush/Makefile @@ -4,3 +4,4 @@ # Francis Laniel, Amarula Solutions, francis.laniel@amarulasolutions.com obj-y += cmd_ut_hush.o +obj-y += if.o diff --git a/test/hush/if.c b/test/hush/if.c new file mode 100644 index 0000000000..a57d88850f --- /dev/null +++ b/test/hush/if.c @@ -0,0 +1,308 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * (C) Copyright 2021 + * Francis Laniel, Amarula Solutions, francis.laniel@amarulasolutions.com + */ + +#include +#include +#include +#include +#include + +/* + * All tests will execute the following: + * if condition_to_test; then + * true + * else + * false + * fi + * If condition is true, command returns 1, 0 otherwise. + */ +const char *if_format = "if %s; then true; else false; fi"; + +static int hush_test_if_base(struct unit_test_state *uts) +{ + char if_formatted[128]; + + sprintf(if_formatted, if_format, "true"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "false"); + ut_asserteq(1, run_command(if_formatted, 0)); + + return 0; +} +HUSH_TEST(hush_test_if_base, 0); + +static int hush_test_if_basic_operators(struct unit_test_state *uts) +{ + char if_formatted[128]; + + sprintf(if_formatted, if_format, "test aaa = aaa"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test aaa = bbb"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test aaa != bbb"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test aaa != aaa"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test aaa < bbb"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test bbb < aaa"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test bbb > aaa"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test aaa > bbb"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 123 -eq 123"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 123 -eq 456"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 123 -ne 456"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 123 -ne 123"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 123 -lt 456"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 123 -lt 123"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 456 -lt 123"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 123 -le 456"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 123 -le 123"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 456 -le 123"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 456 -gt 123"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 123 -gt 123"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 123 -gt 456"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 456 -ge 123"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 123 -ge 123"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 123 -ge 456"); + ut_asserteq(1, run_command(if_formatted, 0)); + + return 0; +} +HUSH_TEST(hush_test_if_basic_operators, 0); + +static int hush_test_if_octal(struct unit_test_state *uts) +{ + char if_formatted[128]; + + sprintf(if_formatted, if_format, "test 010 -eq 010"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 010 -eq 011"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 010 -ne 011"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 010 -ne 010"); + ut_asserteq(1, run_command(if_formatted, 0)); + + return 0; +} +HUSH_TEST(hush_test_if_octal, 0); + +static int hush_test_if_hexadecimal(struct unit_test_state *uts) +{ + char if_formatted[128]; + + sprintf(if_formatted, if_format, "test 0x2000000 -gt 0x2000001"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 0x2000000 -gt 0x2000000"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 0x2000000 -gt 0x1ffffff"); + ut_assertok(run_command(if_formatted, 0)); + + return 0; +} +HUSH_TEST(hush_test_if_hexadecimal, 0); + +static int hush_test_if_mixed(struct unit_test_state *uts) +{ + char if_formatted[128]; + + sprintf(if_formatted, if_format, "test 010 -eq 10"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 010 -ne 10"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 0xa -eq 10"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 0xa -eq 012"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 2000000 -gt 0x1ffffff"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 0x2000000 -gt 1ffffff"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 0x2000000 -lt 1ffffff"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 0x2000000 -eq 2000000"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test 0x2000000 -ne 2000000"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test -z \"\""); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test -z \"aaa\""); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test -n \"aaa\""); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test -n \"\""); + ut_asserteq(1, run_command(if_formatted, 0)); + + return 0; +} +HUSH_TEST(hush_test_if_mixed, 0); + +static int hush_test_if_inverted(struct unit_test_state *uts) +{ + char if_formatted[128]; + + sprintf(if_formatted, if_format, "test ! aaa = aaa"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test ! aaa = bbb"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test ! ! aaa = aaa"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test ! ! aaa = bbb"); + ut_asserteq(1, run_command(if_formatted, 0)); + + return 0; +} +HUSH_TEST(hush_test_if_inverted, 0); + +static int hush_test_if_binary(struct unit_test_state *uts) +{ + char if_formatted[128]; + + sprintf(if_formatted, if_format, "test aaa != aaa -o bbb != bbb"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test aaa != aaa -o bbb = bbb"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test aaa = aaa -o bbb != bbb"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test aaa = aaa -o bbb = bbb"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test aaa != aaa -a bbb != bbb"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test aaa != aaa -a bbb = bbb"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test aaa = aaa -a bbb != bbb"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test aaa = aaa -a bbb = bbb"); + ut_assertok(run_command(if_formatted, 0)); + + return 0; +} +HUSH_TEST(hush_test_if_binary, 0); + +static int hush_test_if_inverted_binary(struct unit_test_state *uts) +{ + char if_formatted[128]; + + sprintf(if_formatted, if_format, "test ! aaa != aaa -o ! bbb != bbb"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test ! aaa != aaa -o ! bbb = bbb"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test ! aaa = aaa -o ! bbb != bbb"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test ! aaa = aaa -o ! bbb = bbb"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, + "test ! ! aaa != aaa -o ! ! bbb != bbb"); + ut_asserteq(1, run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, + "test ! ! aaa != aaa -o ! ! bbb = bbb"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, + "test ! ! aaa = aaa -o ! ! bbb != bbb"); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test ! ! aaa = aaa -o ! ! bbb = bbb"); + ut_assertok(run_command(if_formatted, 0)); + + return 0; +} +HUSH_TEST(hush_test_if_inverted_binary, 0); + +static int hush_test_if_z_operator(struct unit_test_state *uts) +{ + char if_formatted[128]; + + /* Deal with environment variable used during test. */ + env_set("ut_var_nonexistent", NULL); + env_set("ut_var_exists", "1"); + + sprintf(if_formatted, if_format, "test -z \"$ut_var_nonexistent\""); + ut_assertok(run_command(if_formatted, 0)); + + sprintf(if_formatted, if_format, "test -z \"$ut_var_exists\""); + ut_asserteq(1, run_command(if_formatted, 0)); + + /* Clear the set environment variable. */ + env_set("ut_var_exists", NULL); + + return 0; +} +HUSH_TEST(hush_test_if_z_operator, 0);