From mboxrd@z Thu Jan 1 00:00:00 1970 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 D1295C7619A for ; Thu, 30 Mar 2023 19:48:50 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id B5FF085F66; Thu, 30 Mar 2023 21:48:48 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (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="ZglGuh0B"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 04DF485714; Thu, 30 Mar 2023 21:48:23 +0200 (CEST) Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) (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 4E72A85F01 for ; Thu, 30 Mar 2023 21:48:03 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (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-x32f.google.com with SMTP id j36-20020a05600c1c2400b003f04057c152so399987wms.5 for ; Thu, 30 Mar 2023 12:48:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amarulasolutions.com; s=google; t=1680205682; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=nrfte2WexrrvhM+sgbybEs71Wg9j1mqgTt8v70KJJ6o=; b=ZglGuh0BAiEu+r0pEkplMG9XCJMuKD80/QdwA1YVjb8Z61n06fSJsyBmZPNEDI1KRY 8PKjS3c+Lo7zhizODaPuA30eYuSyJaIkq5BJuYiFrHuCbnDbTkecEEODaSG5m7ag6YEh 46kRRcvw6kHGPHFXdtHSI3+YEGLlif5Pm9kRk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680205682; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=nrfte2WexrrvhM+sgbybEs71Wg9j1mqgTt8v70KJJ6o=; b=ZfDaXmS357rbMAE+AKpWbn/dfAxMNA1+fMU0yhQHpfU8V68ATox0slXRWL4LiEHSOW aOm5ciZpMn3A2lVkYR3rQxyi6WhGKDMrbFp2tUFDwouOnpC4XyH5IUfvfc5jMZi1pHPI yIJH3hFj/sM0rS3CHlmL3lMdNPvDrkYiH4theodd0jV5i683imzNJpFC5E1R6AjDVyF8 T/T24m0nPnMXBkd+qjzTTcmRqRLg4tZQlcudVecPcF5RlVO/+DRW2qIEVK0P5MzHteYb +WMGNeJYwNUAAW5h2T63kuK7sVDPbr6wVviJCctAjzvllXZpK6BJm4S+ykejmihUX25n vaNA== X-Gm-Message-State: AAQBX9cOSyCPfOsZhcEK7UjCbl9H9zFuNBJS9A931V2rD4dRrihhmyoj Ub6QLCKbIc/yAS+eBRyJYi8igHzd5UPp0Qe3IJRCncnk X-Google-Smtp-Source: AKy350bZvQdKm6YO6kufH5z19smd4nxdCLWzdDtze6Qf7rx2j3BExRLBdfx1fsNQGreAaRTrsI9RzQ== X-Received: by 2002:a05:600c:2242:b0:3eb:3998:8c05 with SMTP id a2-20020a05600c224200b003eb39988c05mr5422640wmm.6.1680205682661; Thu, 30 Mar 2023 12:48:02 -0700 (PDT) Received: from pwmachine.numericable.fr ([78.30.11.60]) by smtp.gmail.com with ESMTPSA id l24-20020a1c7918000000b003e203681b26sm203762wme.29.2023.03.30.12.48.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Mar 2023 12:48:02 -0700 (PDT) From: Francis Laniel To: u-boot@lists.denx.de Cc: Michael Nazzareno Trimarchi , Tom Rini , Simon Glass , Harald Seiler , Francis Laniel , "Albert ARIBAUD (3ADEV)" , Tony Dinh , Sergei Antonov , Heinrich Schuchardt , Ilias Apalodimas , Masahisa Kojima , =?UTF-8?q?Pali=20Roh=C3=A1r?= , Stefan Roese , John Keeping , Neil Armstrong , Roger Knecht , Robert Marko , Alexey Romanov , Patrice Chotard , Fabrice Gasnier , Marek Vasut , Patrick Delaunay , Hector Palacios , Rasmus Villemoes , AKASHI Takahiro , Linus Walleij , Rui Miguel Silva , Andrew Scull Subject: [RFC PATCH v7 02/23] test: hush: Test hush if/else Date: Thu, 30 Mar 2023 20:46:50 +0100 Message-Id: <20230330194716.23623-3-francis.laniel@amarulasolutions.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230330194716.23623-1-francis.laniel@amarulasolutions.com> References: <20230330194716.23623-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.8 at phobos.denx.de X-Virus-Status: Clean As asked in commit 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); -- 2.34.1