From patchwork Mon Mar 4 21:45:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ivan Orlov X-Patchwork-Id: 1907902 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=apqnNA/6; 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=DdjzhT+x; 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 4TpXMN1Bttz1yX3 for ; Tue, 5 Mar 2024 08:46:24 +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=Tyeq4O2MyfuaihgI4AFws4ugPWVX8qn46iUwX+kmHC0=; b=apqnNA/622b8Bd 3KH2X2VCZAvdT5pd+Z/5JV7kziuZ7peXmt9YIwsk3LjxxhVDhtmQgaTDbly39/7yPC98A6g6kBnrP r9EexdlXpW/Q/sSDMUvTeLsNCGbHwMUoWWucNg0nZjNzCdr3AHgz7kZSmL7/Pfx/ErZc/cRdUzDOG kHDqdmQelCy2cZ473PbyUmwdHfqR4kWgyGCBz3dpCybnj8cEuSRQEu6qa6n0c9HfYTE9mzSApn9fa Et/qnFqcsTZNOn46iRIArtWYIVD8lvCdnPnWbCBrLPymrn7v+ZTbzHZuYMsL6P5kCUgr41TRDcGi8 ko1RBPo9Hu25it79plNQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rhG8W-0000000ApyS-0w0H; Mon, 04 Mar 2024 21:46:16 +0000 Received: from mail-wm1-x32a.google.com ([2a00:1450:4864:20::32a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rhG8O-0000000Apnr-1naw for opensbi@lists.infradead.org; Mon, 04 Mar 2024 21:46:13 +0000 Received: by mail-wm1-x32a.google.com with SMTP id 5b1f17b1804b1-412e445aa25so518215e9.0 for ; Mon, 04 Mar 2024 13:46:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709588759; x=1710193559; 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=hVdNhcxf1fqUWsv1B1eujOsw+t6r/ZP6dDk0+4+E9C8=; b=DdjzhT+xP9SS+mmsnxSpnSZA8RpMRUWompd4PeU8COq3SpzaS4TzjkREOPWRHJJ+un SVpHuHayhjAZZlkCcAhrg2NQOJ5yuOXvkc6NOtFOe+rTJ8EB3QEfk3t+qysndxlj2jQT UXyZfGyKusTCuQnij+RIRqru2AUKLS6zQbfZpEz8o6y2qkiPoj4kPNBYh/Pbh4vuR8ZI nG9kTUwXzNFnLItZFE51rPEQEhvdaIzKhzBCBbQOHJN88jijXbs/DlyGvkR2FxDpG5Og jgdSpmhQDWmz4mNXAsvv5WUim2KPEvzR5tHPZQNK1Gj6jXv25nJkFFNZ+rxF/0GClDLC pNxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709588759; x=1710193559; 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=hVdNhcxf1fqUWsv1B1eujOsw+t6r/ZP6dDk0+4+E9C8=; b=NZK7B0TvNjAyC2Z5/t6/Bc85a1VJCw39LiH6Bj8i3VlWrBbCrb1Pf0+G4MD4gMN8bo UD+sLsnfNvmELTAyLmWeFQ7MM44eZ6Dy9b9NiDLcdiP+VDLy/Zy3fgSZw1tj4k++t1oZ ijfLT4Y3RJenhhq/HmvHu6oShKQILiVil9Kb5H1b6bo20MZq4aLkCEJCr4hwuBo6TAPz HVEWqV+RoYq/sgedi30tIJhCA4TiWpqRinqP35qvJDBioixmFfACjqOI01RKvI6HR1pG VVmAwlkn3w4yTfdPv4qmJPkc9nLxwUzFmBL/Phey+ihP1Zvzqhh8MiKQGpl4uBsn1GAC JZ7Q== X-Forwarded-Encrypted: i=1; AJvYcCV5/5wn8sTonH7/QXgGqquxMQ3Ydy5l7OZXhvDFFl3FHAtt2dDO5jlHeugE0oiqew5g7Tr2/p7JkhlLDSTaIQwv86ZI9haqzF1c2v8= X-Gm-Message-State: AOJu0Yy2iK/u3NTCEv0YURnz4CLiAOE8RxSoIrZFEUlDTnjaGFGyicwj giYzNk/LH+hk+TbOtG/Ss/mrVbnKz/4w7t4efo6SbU2W6uGSEAdk X-Google-Smtp-Source: AGHT+IExYZCSI7QoICxJj2zBPGv7WUhaPcAakr8dl0x08EFE3FzE7/QUYWvddKQTg10QfQ2g29KP7Q== X-Received: by 2002:a05:600c:1c14:b0:412:a2b1:66fa with SMTP id j20-20020a05600c1c1400b00412a2b166famr225242wms.0.1709588758835; Mon, 04 Mar 2024 13:45:58 -0800 (PST) Received: from ivan-HLYL-WXX9.. ([2a01:4b00:d20e:7300:f7e0:efec:4145:922]) by smtp.gmail.com with ESMTPSA id bh6-20020a05600c3d0600b00412e88f7adbsm1899159wmb.17.2024.03.04.13.45.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Mar 2024 13:45:56 -0800 (PST) From: Ivan Orlov To: anup@brainfault.org, ajones@ventanamicro.com Cc: Ivan Orlov , opensbi@lists.infradead.org Subject: [PATCH v4 4/4] lib: tests: Add sbi_console test Date: Mon, 4 Mar 2024 21:45:51 +0000 Message-Id: <20240304214551.365057-5-ivan.orlov0322@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240304214551.365057-1-ivan.orlov0322@gmail.com> References: <20240304214551.365057-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-20240304_134608_767042_45A63D2A X-CRM114-Status: GOOD ( 17.36 ) 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:32a 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.2 FREEMAIL_ENVFROM_END_DIGIT Envelope-from freemail username ends in digit [ivan.orlov0322(at)gmail.com] 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [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 Reviewed-by: Andrew Jones --- 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 '{}' V3 -> V4: - Add 'test_console_lock' spinlock and corresponding locking in the 'PUTS_TEST' and 'PRINTF_TEST' macros. At the moment, there is no chance that tests will run simultaneously on multiple harts, but locking still makes the whole thing more robust and protects from possible mistakes in the future. lib/sbi/objects.mk | 1 + lib/sbi/sbi_console.c | 4 ++ lib/sbi/sbi_console_test.c | 105 +++++++++++++++++++++++++++++++++++++ 3 files changed, 110 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..29f4ac1 --- /dev/null +++ b/lib/sbi/sbi_console_test.c @@ -0,0 +1,105 @@ +/* + * 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 spinlock_t test_console_lock = SPIN_LOCK_INITIALIZER; + +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 { \ + spin_lock(&test_console_lock); \ + 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)); \ + spin_unlock(&test_console_lock); \ +} 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 { \ + spin_lock(&test_console_lock); \ + 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)); \ + spin_unlock(&test_console_lock); \ +} 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);