From patchwork Wed Jul 29 01:41:10 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 1338093 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.a=rsa-sha256 header.s=google header.b=jOxSwJrm; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (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 ozlabs.org (Postfix) with ESMTPS id 4BGbqn0Y3Dz9sSy for ; Wed, 29 Jul 2020 11:41:41 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 40CE48248B; Wed, 29 Jul 2020 03:41:26 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org 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=chromium.org header.i=@chromium.org header.b="jOxSwJrm"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id E404A82482; Wed, 29 Jul 2020 03:41:22 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,SPF_HELO_NONE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-oi1-x244.google.com (mail-oi1-x244.google.com [IPv6:2607:f8b0:4864:20::244]) (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 E453982482 for ; Wed, 29 Jul 2020 03:41:19 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sjg@chromium.org Received: by mail-oi1-x244.google.com with SMTP id s144so9426962oie.3 for ; Tue, 28 Jul 2020 18:41:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=eCvACyvioLc7XJ/qOiW56YxDe+KzjQCogBPzWj1Pc3s=; b=jOxSwJrmkAwx/Z/NragXLTGQqCBMgj+/hSflm2qUhCs6mSW3fGOH3ElYESgQUWVC2g DiptKXlmj3f4LdruKjSVa9iXybGkep+iwRlNAmXYP8nxO8AI9eyIfopULPBNphJdtIrl MaUXWx4yGvi5jDLK0HmCSzFMcZaYji7XPc+A8= 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=eCvACyvioLc7XJ/qOiW56YxDe+KzjQCogBPzWj1Pc3s=; b=eKg4uPd00sdNXaCPbG7IMDfn85f26hGLUg/XRJ5sXJsxrUHYz0Z9Nhb0QiPZvSLxe/ yB9WnmD1NzfJUqylys3o7sMuCMYu+3kZ9rPbyW2macla975SuS4ByRMtfEiZnwSkMNIc xPiFiKUSXSEwcao89oxy0GqrvlGRY4Da35wfrtGu5yOlM3iHhjqLi2g0MXa/WUx4irPr 008LS8bYt9tB6rd0ZNGvUzD5DxWBp2vEVOejIm1ndzD5hLTZlgBSuCuACucGTh1/84VV 0Hb3RtsBORqTDcH6xYD2YMf+8HpB8/FUmeUclG+YkNKkoFTBPOCXPVr1AGebgaMm2MAd ZcaQ== X-Gm-Message-State: AOAM530C6R6KAEJtQy0II8YLPM7CY0adWgxlmrK3i042dmwrf3eKvqSR ytcS1i312iKfgUYgMBRXfIL0ZFzArRL9og== X-Google-Smtp-Source: ABdhPJxMh2NU3QPst7cc0GHL8M5fumAtetuksr3YF54sPWk9liW77AoSHet8P8M4Sifl7YtdhQyGWQ== X-Received: by 2002:aca:ad4e:: with SMTP id w75mr3638093oie.124.1595986878572; Tue, 28 Jul 2020 18:41:18 -0700 (PDT) Received: from localhost.localdomain (c-73-14-175-90.hsd1.co.comcast.net. [73.14.175.90]) by smtp.gmail.com with ESMTPSA id j35sm88029ota.23.2020.07.28.18.41.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Jul 2020 18:41:18 -0700 (PDT) From: Simon Glass To: U-Boot Mailing List Cc: Tom Rini , Simon Glass Subject: [PATCH v2 1/5] test: Add a way to check part of a console line or skip it Date: Tue, 28 Jul 2020 19:41:10 -0600 Message-Id: <20200728194111.v2.1.Ieff8961f595591ceef66847ff1ee0dd6972ceec9@changeid> X-Mailer: git-send-email 2.28.0.rc0.142.g3c755180ce-goog In-Reply-To: <20200729014114.1960556-1-sjg@chromium.org> References: <20200729014114.1960556-1-sjg@chromium.org> MIME-Version: 1.0 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.102.3 at phobos.denx.de X-Virus-Status: Clean Some lines of the output are not worth testing, or not worth testing in their entirety. For example, when checking a hex dump we know that the hex-dump routine can display ASCII so we only need to check the hex bytes, not the ASCII dump. Add a new test macros which can check only part of a console line. Sometimes it is useful to skip a line altogether, so add a macro for that also. Signed-off-by: Simon Glass --- Changes in v2: - Add new patch to check part of a console line or skip it include/test/ut.h | 59 +++++++++++++++++++++++++++++++++++++++++++++++ test/ut.c | 22 ++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/include/test/ut.h b/include/test/ut.h index 99bbb1230c7..017dd0d8702 100644 --- a/include/test/ut.h +++ b/include/test/ut.h @@ -56,6 +56,31 @@ void ut_failf(struct unit_test_state *uts, const char *fname, int line, int ut_check_console_line(struct unit_test_state *uts, const char *fmt, ...) __attribute__ ((format (__printf__, 2, 3))); +/** + * ut_check_console_linen() - Check part of the next console line + * + * This creates a string and then checks it against the next line of console + * output obtained with console_record_readline(). Only the length of the + * string is checked + * + * After the function returns, uts->expect_str holds the expected string and + * uts->actual_str holds the actual string read from the console. + * + * @uts: Test state + * @fmt: printf() format string for the error, followed by args + * @return 0 if OK, other value on error + */ +int ut_check_console_linen(struct unit_test_state *uts, const char *fmt, ...) + __attribute__ ((format (__printf__, 2, 3))); + +/** + * ut_check_skipline() - Check that the next console line exists and skip it + * + * @uts: Test state + * @return 0 if OK, other value on error + */ +int ut_check_skipline(struct unit_test_state *uts); + /** * ut_check_console_end() - Check there is no more console output * @@ -151,6 +176,23 @@ int ut_check_console_dump(struct unit_test_state *uts, int total_bytes); } \ } +/* + * Assert that two string expressions are equal, up to length of the + * first + */ +#define ut_asserteq_strn(expr1, expr2) { \ + const char *_val1 = (expr1), *_val2 = (expr2); \ + int _len = strlen(_val1); \ + \ + if (memcmp(_val1, _val2, _len)) { \ + ut_failf(uts, __FILE__, __LINE__, __func__, \ + #expr1 " = " #expr2, \ + "Expected \"%.*s\", got \"%.*s\"", \ + _len, _val1, _len, _val2); \ + return CMD_RET_FAILURE; \ + } \ +} + /* Assert that two memory areas are equal */ #define ut_asserteq_mem(expr1, expr2, len) { \ const u8 *_val1 = (u8 *)(expr1), *_val2 = (u8 *)(expr2); \ @@ -230,6 +272,23 @@ int ut_check_console_dump(struct unit_test_state *uts, int total_bytes); return CMD_RET_FAILURE; \ } \ +/* Assert that the next console output line matches up to the length */ +#define ut_assert_nextlinen(fmt, args...) \ + if (ut_check_console_linen(uts, fmt, ##args)) { \ + ut_failf(uts, __FILE__, __LINE__, __func__, \ + "console", "\nExpected '%s',\n got '%s'", \ + uts->expect_str, uts->actual_str); \ + return CMD_RET_FAILURE; \ + } \ + +/* Assert that there is a 'next' console output line, and skip it */ +#define ut_assert_skipline() \ + if (ut_check_skipline(uts)) { \ + ut_failf(uts, __FILE__, __LINE__, __func__, \ + "console", "\nExpected a line, got end"); \ + return CMD_RET_FAILURE; \ + } \ + /* Assert that there is no more console output */ #define ut_assert_console_end() \ if (ut_check_console_end(uts)) { \ diff --git a/test/ut.c b/test/ut.c index c64f0b554d5..95bdd66de6a 100644 --- a/test/ut.c +++ b/test/ut.c @@ -59,6 +59,28 @@ int ut_check_console_line(struct unit_test_state *uts, const char *fmt, ...) return strcmp(uts->expect_str, uts->actual_str); } +int ut_check_console_linen(struct unit_test_state *uts, const char *fmt, ...) +{ + va_list args; + + va_start(args, fmt); + vsnprintf(uts->expect_str, sizeof(uts->expect_str), fmt, args); + va_end(args); + console_record_readline(uts->actual_str, sizeof(uts->actual_str)); + + return strncmp(uts->expect_str, uts->actual_str, + strlen(uts->expect_str)); +} + +int ut_check_skipline(struct unit_test_state *uts) +{ + if (!console_record_avail()) + return -ENFILE; + console_record_readline(uts->actual_str, sizeof(uts->actual_str)); + + return 0; +} + int ut_check_console_end(struct unit_test_state *uts) { if (!console_record_avail())