From patchwork Mon Mar 4 21:45:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ivan Orlov X-Patchwork-Id: 1907901 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=G0OdL/D9; 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=jUyhDE+o; 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 4TpXMM0XNQz23qq for ; Tue, 5 Mar 2024 08:46:23 +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=3vm9tc4KUvO9+NAbiO9DqBOZbkB87UKBO13siX9kPlY=; b=G0OdL/D9oDu2n2 vHJFdS6OH149eFNNu5cnkAh2oNsaNSlGJPkbnxi7PXu/RINXc2adJU04iyqsnxl3wsCsmWEVGW9eg lMeRSyH8QN+9gSQHBfYLo0C+lCApdcLhblHjWa+djouyY81kJ3qPK7ut2hqpD0hZBKS98Qz4Oavpt VVOkb0c5PFW06ZZ086q0027yioaTm6tPZ1U7Ij3HFlbep5HROnqsGKchTwsxMlooSKEnfjA3lLruk z221KQ23NEJwT9soFRzlckOSAE56no3Gf6IMqQzmI+n+rtQ6AgZGzxQz5IDjqpa57LRy+cBg21jsg SWSC7XaYLXKUlma83M5w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rhG8V-0000000Apxy-1BCM; Mon, 04 Mar 2024 21:46:15 +0000 Received: from mail-wr1-x434.google.com ([2a00:1450:4864:20::434]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rhG8J-0000000ApnY-1JyO for opensbi@lists.infradead.org; Mon, 04 Mar 2024 21:46:10 +0000 Received: by mail-wr1-x434.google.com with SMTP id ffacd0b85a97d-33d855caf7dso812542f8f.0 for ; Mon, 04 Mar 2024 13:45:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709588756; x=1710193556; 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=NGDmBVC5K62hVCAdlhFEvB3HQB89IrviF00eYqf8nRo=; b=jUyhDE+otz12jQg0ckK+o0hauz7iHVdQTT+lEWCxE7YTpNIYha9KW19b4cXxetLGem ICuDic0sicZDlX91w/hgTI43MmZK0+XIdpuW7okKBvNr+ySXoFU2omoH0QgTeIdxNuF7 Wo1m9FXCQZD88UJO9InmpOeCkvLsju88WceYFa/9A2fbm7ulAKbEKMmq46Jh5Ls6SG/d GGj92PxsYcwJWi7Ni2ka02xsuFRH2Ooi2QWaK5M/urxZC0QS9VeouL5+KkPR38mz7C1z /n/viOV0eSgHQ/AS4DVf2SrpRfebi2fZUwcPz8hQ6nhxX1l6cqpWj7U4C/Y6WuFuMWwS nbTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709588756; x=1710193556; 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=NGDmBVC5K62hVCAdlhFEvB3HQB89IrviF00eYqf8nRo=; b=wtxY3Ml5GvXW7g4Aqr0sGORVPJePJz6W+5RQ1agFu55xYte/u9cOMnFhJ1J7DjN0fI n9NBKa2XVLkP3dWv27AW3kSZab6Oe5ckR3rMETj3RHwKa6fyugO7UXk1oYd2EeSuCk6L 4m5Zg1Mz4QVgAQKj/iz4+k+UgW4grMhwCA0rjy08F21HkP+b+qab6frLLJwC2WH/bBf1 dwob3tbETJQULYZUMfuHj0ViS9hTwZcE7EWbAFFPOPEqWI5WBagWI6IHOpsQqQt2Oc/q yhLo7bzYI7LAekGMPh/CX60UueUAb3FNYOLWjibwsluBHzvKv/jmm6PvEDVzQRovH4xW 7Lug== X-Forwarded-Encrypted: i=1; AJvYcCU+LG/lr9u5Cqh6xlm5lK8a4fN+K6IHtULzGuqslxjKvsSG/huprUm/Ogh47KdCiQFEE3CZREuqfcy2i5x6xgC1WwOz5M9C0IaC1Aw= X-Gm-Message-State: AOJu0Yy5BKlzb1me79ua6R8NV8Yy0orodpeJG13gQnvmw8C7EwAy9mD3 SUi7H+1JbSu8WWaEXRNXTCaGKcBiiYw+BoulqiKUZ+TQbj6g6SqI X-Google-Smtp-Source: AGHT+IFOpWtvvbAi5bvzh4SZJRCwxx37FWjFzzEYKHV6OUoiFnbU/gHIMd48U996z04Rf60S6eSfwg== X-Received: by 2002:a05:600c:3b25:b0:412:b2af:2632 with SMTP id m37-20020a05600c3b2500b00412b2af2632mr8598924wms.0.1709588755981; Mon, 04 Mar 2024 13:45:55 -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.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Mar 2024 13:45:54 -0800 (PST) From: Ivan Orlov To: anup@brainfault.org, ajones@ventanamicro.com Cc: Ivan Orlov , opensbi@lists.infradead.org Subject: [PATCH v4 2/4] lib: Add SBIUnit testing macros and functions Date: Mon, 4 Mar 2024 21:45:49 +0000 Message-Id: <20240304214551.365057-3-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_134606_277291_E6C5C5DC X-CRM114-Status: GOOD ( 27.99 ) 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: This patch introduces all of the SBIUnit macros and functions which can be used during the test development process. Also, it defines the 'run_all_tests' function, which is being called during the 'in [...] 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:434 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 This patch introduces all of the SBIUnit macros and functions which can be used during the test development process. Also, it defines the 'run_all_tests' function, which is being called during the 'init_coldboot' right after printing the boot hart information. Also, add the CONFIG_SBIUNIT Kconfig entry in order to be able to turn the tests on and off. When the CONFIG_SBIUNIT is disabled, the tests and all related code is excluded completely on the compilation stage. Signed-off-by: Ivan Orlov Reviewed-by: Andrew Jones --- V1 -> V2: - Rename the sources to have 'test' in the names: 'sbi_unit.c' => 'sbi_unit_test.c', 'sbi_unit.h' => 'sbi_unit_test.h'. - Rewrite using the carray functionality instead of placing the pointers to the test suites into the source explicitly. - Add 'ifdef' into the sbi_unit_test header file so we could avoid writing a lot of 'ifdef' in other files. - Get rid of unused symbols and macros - Change the behaviour of SBIUNIT_ASSERT_* functions: now they trigger sbi_panic if the assertion fails. - Add a 'SBIUnit' prefix to the SBIUNIT_INFO macro message - Rename the sbiunit_test_case structure fields, and use true/false instead of 1/0 - Change the logic of the test fail detection: now we have the 'failed' sbiunit_test_case structure field instead of the 'result'. 'failed' field gets set to 'true' if an assertion or an expectaion fails. - Fix codestyle issues - Add 'len' parameter to SBIUNIT_*_STREQ V2 -> V3: - Remove redundant 'onerr' field from 'sbiunit_test_case' struct - Remove setting of the 'failed' field for the test case struct in case of assert (because panic() is called after that) - Add 'SBIUNIT_END_CASE' macro, which should be placed at the end of a test cases list - Remove redundant 'enabled' field of the test case, as currently we can't enable/disable tests separately; Now we iterate the test cases until we face test_func == NULL V3 -> V4: - No changes include/sbi/sbi_unit_test.h | 71 +++++++++++++++++++++++++++++++++++ lib/sbi/Kconfig | 4 ++ lib/sbi/objects.mk | 2 + lib/sbi/sbi_init.c | 3 ++ lib/sbi/sbi_unit_test.c | 43 +++++++++++++++++++++ lib/sbi/sbi_unit_tests.carray | 3 ++ 6 files changed, 126 insertions(+) create mode 100644 include/sbi/sbi_unit_test.h create mode 100644 lib/sbi/sbi_unit_test.c create mode 100644 lib/sbi/sbi_unit_tests.carray diff --git a/include/sbi/sbi_unit_test.h b/include/sbi/sbi_unit_test.h new file mode 100644 index 0000000..c63d900 --- /dev/null +++ b/include/sbi/sbi_unit_test.h @@ -0,0 +1,71 @@ +/* + * SPDX-License-Identifier: BSD-2-Clause + * + * Author: Ivan Orlov + */ +#ifdef CONFIG_SBIUNIT +#ifndef __SBI_UNIT_H__ +#define __SBI_UNIT_H__ + +#include +#include +#include + +struct sbiunit_test_case { + const char *name; + bool failed; + void (*test_func)(struct sbiunit_test_case *test); +}; + +struct sbiunit_test_suite { + const char *name; + struct sbiunit_test_case *cases; +}; + +#define SBIUNIT_TEST_CASE(func) \ + { \ + .name = #func, \ + .failed = false, \ + .test_func = (func) \ + } + +#define SBIUNIT_END_CASE { } + +#define SBIUNIT_TEST_SUITE(suite_name, cases_arr) \ + struct sbiunit_test_suite suite_name = { \ + .name = #suite_name, \ + .cases = cases_arr \ + } + +#define _sbiunit_msg(test, msg) "[SBIUnit] [%s:%d]: %s: %s", __FILE__, \ + __LINE__, test->name, msg + +#define SBIUNIT_INFO(test, msg) sbi_printf(_sbiunit_msg(test, msg)) +#define SBIUNIT_PANIC(test, msg) sbi_panic(_sbiunit_msg(test, msg)) + +#define SBIUNIT_EXPECT(test, cond) do { \ + if (!(cond)) { \ + test->failed = true; \ + SBIUNIT_INFO(test, "Condition \"" #cond "\" expected to be true!\n"); \ + } \ +} while (0) + +#define SBIUNIT_ASSERT(test, cond) do { \ + if (!(cond)) \ + SBIUNIT_PANIC(test, "Condition \"" #cond "\" must be true!\n"); \ +} while (0) + +#define SBIUNIT_EXPECT_EQ(test, a, b) SBIUNIT_EXPECT(test, (a) == (b)) +#define SBIUNIT_ASSERT_EQ(test, a, b) SBIUNIT_ASSERT(test, (a) == (b)) +#define SBIUNIT_EXPECT_NE(test, a, b) SBIUNIT_EXPECT(test, (a) != (b)) +#define SBIUNIT_ASSERT_NE(test, a, b) SBIUNIT_ASSERT(test, (a) != (b)) +#define SBIUNIT_EXPECT_MEMEQ(test, a, b, len) SBIUNIT_EXPECT(test, !sbi_memcmp(a, b, len)) +#define SBIUNIT_ASSERT_MEMEQ(test, a, b, len) SBIUNIT_ASSERT(test, !sbi_memcmp(a, b, len)) +#define SBIUNIT_EXPECT_STREQ(test, a, b, len) SBIUNIT_EXPECT(test, !sbi_strncmp(a, b, len)) +#define SBIUNIT_ASSERT_STREQ(test, a, b, len) SBIUNIT_ASSERT(test, !sbi_strncmp(a, b, len)) + +void run_all_tests(void); +#endif +#else +#define run_all_tests() +#endif diff --git a/lib/sbi/Kconfig b/lib/sbi/Kconfig index 81dd2db..e3038ee 100644 --- a/lib/sbi/Kconfig +++ b/lib/sbi/Kconfig @@ -50,4 +50,8 @@ config SBI_ECALL_DBTR bool "Debug Trigger Extension" default y +config SBIUNIT + bool "Enable SBIUNIT tests" + default n + endmenu diff --git a/lib/sbi/objects.mk b/lib/sbi/objects.mk index 0a50e95..08959f1 100644 --- a/lib/sbi/objects.mk +++ b/lib/sbi/objects.mk @@ -11,6 +11,8 @@ libsbi-objs-y += riscv_asm.o libsbi-objs-y += riscv_atomic.o libsbi-objs-y += riscv_hardfp.o libsbi-objs-y += riscv_locks.o +libsbi-objs-$(CONFIG_SBIUNIT) += sbi_unit_test.o +libsbi-objs-$(CONFIG_SBIUNIT) += sbi_unit_tests.o libsbi-objs-y += sbi_ecall.o libsbi-objs-y += sbi_ecall_exts.o diff --git a/lib/sbi/sbi_init.c b/lib/sbi/sbi_init.c index 804b01c..796cccc 100644 --- a/lib/sbi/sbi_init.c +++ b/lib/sbi/sbi_init.c @@ -29,6 +29,7 @@ #include #include #include +#include #define BANNER \ " ____ _____ ____ _____\n" \ @@ -398,6 +399,8 @@ static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid) sbi_boot_print_hart(scratch, hartid); + run_all_tests(); + /* * Configure PMP at last because if SMEPMP is detected, * M-mode access to the S/U space will be rescinded. diff --git a/lib/sbi/sbi_unit_test.c b/lib/sbi/sbi_unit_test.c new file mode 100644 index 0000000..1987838 --- /dev/null +++ b/lib/sbi/sbi_unit_test.c @@ -0,0 +1,43 @@ +/* + * SPDX-License-Identifier: BSD-2-Clause + * + * Author: Ivan Orlov + */ +#include +#include +#include + +extern struct sbiunit_test_suite *sbi_unit_tests[]; +extern unsigned long sbi_unit_tests_size; + +static void run_test_suite(struct sbiunit_test_suite *suite) +{ + struct sbiunit_test_case *s_case; + u32 count_pass = 0, count_fail = 0; + + sbi_printf("## Running test suite: %s\n", suite->name); + + s_case = suite->cases; + while (s_case->test_func) { + s_case->test_func(s_case); + if (s_case->failed) + count_fail++; + else + count_pass++; + sbi_printf("[%s] %s\n", s_case->failed ? "FAILED" : "PASSED", + s_case->name); + s_case++; + } + sbi_printf("%u PASSED / %u FAILED / %u TOTAL\n", count_pass, count_fail, + count_pass + count_fail); +} + +void run_all_tests(void) +{ + u32 i; + + sbi_printf("\n# Running SBIUNIT tests #\n"); + + for (i = 0; i < sbi_unit_tests_size; i++) + run_test_suite(sbi_unit_tests[i]); +} diff --git a/lib/sbi/sbi_unit_tests.carray b/lib/sbi/sbi_unit_tests.carray new file mode 100644 index 0000000..8d6069b --- /dev/null +++ b/lib/sbi/sbi_unit_tests.carray @@ -0,0 +1,3 @@ +HEADER: sbi/sbi_unit_test.h +TYPE: struct sbiunit_test_suite +NAME: sbi_unit_tests