From patchwork Fri Mar 1 16:00:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ivan Orlov X-Patchwork-Id: 1906761 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=e27ywbsT; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=Q0LVfUta; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=patchwork.ozlabs.org) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TmXrj2Cf4z23q3 for ; Sat, 2 Mar 2024 03:01:25 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=4dAGHirzr/tAtE6JTXAn5dphJHPdw6TDvS9EeOB6qhU=; b=e27ywbsTW9kxiR gIx5ADv8It2Nkyr0wECbVCkOij5GUXUMOYzgw19LM2yTW7o8V63Yg8KZvQX+nGxaQcQRj+CvgdOa6 yFN02H6Tfz7/D1rv0U0N+sTHg1apQfQfYz6JKKd/1HP5lc7iuBamsV9glSxvkF/d/ZQz4pYRKkc5T O+Th0yHSTpohExgN919r8um3wER/0iiGxTHDintVltPwE25ssY2FrdFG5nsS+I+2vtC4o/U51hfKb SVgw6sKKVGVQyg74+mOSUA9MCwh1Zi+3C+yP+iz3WD+ghik0LxksgTqj75yszirCnyR/2VS3kBa+6 nYA+WfBsQrIRuhHMZ49A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rg5K3-00000001055-00oA; Fri, 01 Mar 2024 16:01:19 +0000 Received: from mail-wm1-x335.google.com ([2a00:1450:4864:20::335]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rg5K0-0000000101a-2BHy for opensbi@lists.infradead.org; Fri, 01 Mar 2024 16:01:17 +0000 Received: by mail-wm1-x335.google.com with SMTP id 5b1f17b1804b1-412bf22b6a4so971415e9.1 for ; Fri, 01 Mar 2024 08:01:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709308872; x=1709913672; darn=lists.infradead.org; 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=UWIG+nE0HJLUz2fLkYweIVkaLOeKcF5bSxDSoVjZStg=; b=Q0LVfUtaDIWnU1e/TGgl/7SZIgj65+UDsCbbOHxtDtHA4TLUyZatrGT5DQK9AVf1vh vsZMQBXLSiPbLCzZDplPKMo7UOYbYujyUAWnEvPdfK7iOv5LcnHiq0QHBh1Z/uQ303W8 ax7wgbKj4r4029dw9NiwV5gcRILXy6YuQJcTiEsL9UcPKuU2yrZG0jXylkwu9aGS17oq i1pKSt0MStLhDwucU9BmknGc+UzOGW8viLYm87nU9eTOO1OnxuUNFV677XmLgui8gnVw 2hn8XShrIyuLAAqzAK+WchxAh9lXcMV1CZSQRH9dXP+DfdG60tnX0Hop+veBD4HYacgC +0Dg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709308872; x=1709913672; 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=UWIG+nE0HJLUz2fLkYweIVkaLOeKcF5bSxDSoVjZStg=; b=Ev56ujVL5cgdYOW31LjlEhWH2a37OPT9lbJjhcXwAQUf1ZHrbfhzetFokrEzxxK+ac xDF0Rk7BKFqXbY2aumYVHAYD07oSGYox5Ln3zVX8EMu+Ijfhl5lRfgHzkOq+cfYrciIj fZd5PxsCnKexledbhg1TsdD/y0y8I0It8EiaSVS1B2B9nm/UxAj3vtN6OSoegvQkeR6T 8MjbaNLRFA39vkDoUZRWSWWZuGYMH8+LLDQMxzAC9LiNAAuSPIGGLVAeQGVlac5iPQF4 4kFKFl50gF7cP44rGORxpziU6qibsiH3pHs2IVQ99z4whjsi4N26tEnjq/lJMkR4AXFd Wc3Q== X-Forwarded-Encrypted: i=1; AJvYcCV/EhB/jHtNb4C/8sbZXel0/UJ1mW7JKqrE/dC3oBqavEGPZFuQJw5TZq38a5HoF7xKbkB7JAu1TcJlxgSK/+EHftroZ/p1V3+ISC0= X-Gm-Message-State: AOJu0YxqbwolqCyXt80vk+57L0x8KJsBsuOKICzH8LaC91jSKoe33WMq pumoRiuL54iKUO/o6PHPu0J0Y2Dh2tleQgwmdyxDnYs8l+QN4hnM X-Google-Smtp-Source: AGHT+IE9WHnuQxIlbJuFbQGtRqMRNim42VQcxpmDrQ02sWCzVi5whddRDslGQJKNmgdYvxmacbkjeQ== X-Received: by 2002:a05:600c:3541:b0:412:c6c9:f324 with SMTP id i1-20020a05600c354100b00412c6c9f324mr1674128wmq.3.1709308872335; Fri, 01 Mar 2024 08:01:12 -0800 (PST) Received: from ivan-HLYL-WXX9.guest.codethink.co.uk ([167.98.27.226]) by smtp.gmail.com with ESMTPSA id s16-20020a05600c45d000b00412b67388f0sm8133175wmo.6.2024.03.01.08.01.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Mar 2024 08:01:10 -0800 (PST) From: Ivan Orlov To: anup@brainfault.org, ajones@ventanamicro.com Cc: Ivan Orlov , opensbi@lists.infradead.org Subject: [PATCH v3 4/4] lib: tests: Add sbi_console test Date: Fri, 1 Mar 2024 16:00:45 +0000 Message-Id: <20240301160046.267814-5-ivan.orlov0322@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240301160046.267814-1-ivan.orlov0322@gmail.com> References: <20240301160046.267814-1-ivan.orlov0322@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240301_080116_585806_4EBBC29F X-CRM114-Status: GOOD ( 16.08 ) X-Spam-Score: 0.0 (/) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: Add the test suite covering some of the functions from lib/sbi/sbi_console.c: putc, puts and printf. The test covers a variety of format specifiers for printf and different strings and characters for [...] Content analysis details: (0.0 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2a00:1450:4864:20:0:0:0:335 listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [ivan.orlov0322(at)gmail.com] 0.2 FREEMAIL_ENVFROM_END_DIGIT Envelope-from freemail username ends in digit [ivan.orlov0322(at)gmail.com] -0.0 T_SCC_BODY_TEXT_LINE No description available. X-BeenThere: opensbi@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "opensbi" Errors-To: opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org Add the test suite covering some of the functions from lib/sbi/sbi_console.c: putc, puts and printf. The test covers a variety of format specifiers for printf and different strings and characters for putc and puts. In order to do that, the test "mocks" the sbi_console_device structure by setting the 'console_dev' variable to the virtual console. Signed-off-by: Ivan Orlov --- V1 -> V2: - Rewrite using the carray functionality - Replace CONSOLE_DO and CONSOLE_DO_RET macros with two inline functions: one of them "mocks" the default console device, and the second one restores the old console device. - Fix codestyle issues (comments, etc.) - Remove incorrect 'puts' test - Use updated SBIUNIT_ASSERT_STREQ API V2 -> V3: - Remove unused include statement - Rename "new_dev" => "test_console_dev" - Use SBIUNIT_END_CASE macro in the test cases list instead of '{}' lib/sbi/objects.mk | 1 + lib/sbi/sbi_console.c | 4 ++ lib/sbi/sbi_console_test.c | 101 +++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 lib/sbi/sbi_console_test.c diff --git a/lib/sbi/objects.mk b/lib/sbi/objects.mk index b4c273f..9d065fa 100644 --- a/lib/sbi/objects.mk +++ b/lib/sbi/objects.mk @@ -16,6 +16,7 @@ libsbi-objs-$(CONFIG_SBIUNIT) += sbi_unit_tests.o libsbi-objs-$(CONFIG_SBIUNIT) += sbi_bitmap_test.o carray-sbi_unit_tests-$(CONFIG_SBIUNIT) += bitmap_test_suite +carray-sbi_unit_tests-$(CONFIG_SBIUNIT) += console_test_suite libsbi-objs-y += sbi_ecall.o libsbi-objs-y += sbi_ecall_exts.o diff --git a/lib/sbi/sbi_console.c b/lib/sbi/sbi_console.c index ab09a5c..d1229d0 100644 --- a/lib/sbi/sbi_console.c +++ b/lib/sbi/sbi_console.c @@ -488,3 +488,7 @@ int sbi_console_init(struct sbi_scratch *scratch) return rc; } + +#ifdef CONFIG_SBIUNIT +#include "sbi_console_test.c" +#endif diff --git a/lib/sbi/sbi_console_test.c b/lib/sbi/sbi_console_test.c new file mode 100644 index 0000000..734a68c --- /dev/null +++ b/lib/sbi/sbi_console_test.c @@ -0,0 +1,101 @@ +/* + * SPDX-License-Identifier: BSD-2-Clause + * + * Author: Ivan Orlov + */ +#include + +#define TEST_CONSOLE_BUF_LEN 1024 + +static const struct sbi_console_device *old_dev; +static char test_console_buf[TEST_CONSOLE_BUF_LEN]; +static u32 test_console_buf_pos; + +static void test_console_putc(char c) +{ + test_console_buf[test_console_buf_pos] = c; + test_console_buf_pos = (test_console_buf_pos + 1) % TEST_CONSOLE_BUF_LEN; +} + +static void clear_test_console_buf(void) +{ + test_console_buf_pos = 0; + test_console_buf[0] = '\0'; +} + +static const struct sbi_console_device test_console_dev = { + .name = "Test console device", + .console_putc = test_console_putc, +}; + +/* Mock the console device */ +static inline void test_console_begin(const struct sbi_console_device *device) +{ + old_dev = console_dev; + console_dev = device; +} + +static inline void test_console_end(void) +{ + console_dev = old_dev; +} + +static void putc_test(struct sbiunit_test_case *test) +{ + clear_test_console_buf(); + + test_console_begin(&test_console_dev); + sbi_putc('a'); + test_console_end(); + SBIUNIT_ASSERT_EQ(test, test_console_buf[0], 'a'); +} + +#define PUTS_TEST(test, expected, str) do { \ + clear_test_console_buf(); \ + test_console_begin(&test_console_dev); \ + sbi_puts(str); \ + test_console_end(); \ + SBIUNIT_ASSERT_STREQ(test, test_console_buf, expected, \ + sbi_strlen(expected)); \ +} while (0) + +static void puts_test(struct sbiunit_test_case *test) +{ + PUTS_TEST(test, "Hello, OpenSBI!", "Hello, OpenSBI!"); + PUTS_TEST(test, "Hello,\r\nOpenSBI!", "Hello,\nOpenSBI!"); +} + +#define PRINTF_TEST(test, expected, format, ...) do { \ + clear_test_console_buf(); \ + test_console_begin(&test_console_dev); \ + size_t __res = sbi_printf(format, ##__VA_ARGS__); \ + test_console_end(); \ + SBIUNIT_ASSERT_EQ(test, __res, sbi_strlen(expected)); \ + SBIUNIT_ASSERT_STREQ(test, test_console_buf, expected, \ + sbi_strlen(expected)); \ +} while (0) + +static void printf_test(struct sbiunit_test_case *test) +{ + PRINTF_TEST(test, "Hello", "Hello"); + PRINTF_TEST(test, "3 5 7", "%d %d %d", 3, 5, 7); + PRINTF_TEST(test, "Hello", "%s", "Hello"); + PRINTF_TEST(test, "-1", "%d", -1); + PRINTF_TEST(test, "FF", "%X", 255); + PRINTF_TEST(test, "ff", "%x", 255); + PRINTF_TEST(test, "A", "%c", 'A'); + PRINTF_TEST(test, "1fe", "%p", (void *)0x1fe); + PRINTF_TEST(test, "4294967295", "%u", 4294967295U); + PRINTF_TEST(test, "-2147483647", "%ld", -2147483647l); + PRINTF_TEST(test, "-9223372036854775807", "%lld", -9223372036854775807LL); + PRINTF_TEST(test, "18446744073709551615", "%llu", 18446744073709551615ULL); +} + +static struct sbiunit_test_case console_test_cases[] = { + SBIUNIT_TEST_CASE(putc_test), + SBIUNIT_TEST_CASE(puts_test), + SBIUNIT_TEST_CASE(printf_test), + SBIUNIT_END_CASE, +}; + +SBIUNIT_TEST_SUITE(console_test_suite, console_test_cases);