Received: by 2002:a05:7412:cfc7:b0:fc:a2b0:25d7 with SMTP id by7csp789688rdb; Sun, 18 Feb 2024 11:52:34 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWGMrGMtYC8NuWp/cYc8Hd8HDNX8U7LqHo7tqmDq4ZfIhLbqxbdkfRPWlCjBCo982gI5Z+E20qmbXeuklqd/C1Ab7O/d0fhz6/QuqjFcQ== X-Google-Smtp-Source: AGHT+IELTEXJA2GT7JxBu9VU7fGNozAh8GlMP3BGm8UpUwf+CVKDv20uYLb4zoRctw4go5vpTW2J X-Received: by 2002:a05:6a00:c84:b0:6e3:8c19:2045 with SMTP id a4-20020a056a000c8400b006e38c192045mr3932998pfv.2.1708285954452; Sun, 18 Feb 2024 11:52:34 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708285954; cv=pass; d=google.com; s=arc-20160816; b=qa0kvltKDkHtbiUkWpCBiPxoITLajhy6BD0Ks7qKpeWun7gks5B6nhLgRC6wpGto5f i1HbEboe7NfDSoC/opMvU+B/hyHNXxyqgKYUXY3bJYPlKhSQE+Ew5vUcuLW9f3NycDTq /DqVvFwjGXs6UH6fvejlBjiMNGEys7mlKwKkUnNSNWqQ+tdPwfDEb2LJVNF4eNgS+Z/Z xPkhZSGoDRvTmj+A0HDpSm7mb1falMcG9xA1XuxMv7uLHZYKaUMh9J5wHmC4aAxNESvl St2tcf3jnIBKBsqvGAxdSWpYQHnunZxRM30WsYNwyvOJocL6el5iHkjsvb65sBFu2W8N 3TdA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=a23UA1u2pf2eSJPG9lDzi7mpsnwH/OTJWisWXIBouwo=; fh=1pO/fnqlbA1DlSshLp2Kp23nBXobkeZ558bQEuNU3Z8=; b=0XnbpmtTnwaAq3e8nzfybROIxQVmd1A1Vy1wR9uNhbjpHLGZb/KBMlxXely3uDXcrU BwGfG68EU/A/ZYmuH0WCgXqa/bEp/CXPtCFK/wFToDjwJMie1uguFiUSemYtdLpT8Fpx ce1aXb3LAe3B1mOvdFLAhSQAsSsyXggMQGZkX/tE/1kj4uzptqU434bfgw9ENJ25jGqs M8ATeN0kUaWgesNSFZga2u6FlOqJfGVnE4wabwiCVNmoS7HJQkyH0zp7Bx2/eoD1Ev6N ZRwzicGhxi1IGktJC88O+AFlGoXsAQrKGDzLuXLK2i0DZvBzs+ixW9KP6PXJGjyI+6Uo 1gZg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=sdfg.com.ar); spf=pass (google.com: domain of linux-kernel+bounces-70561-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-70561-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id x2-20020a636302000000b005dc9bea610esi3287490pgb.789.2024.02.18.11.52.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Feb 2024 11:52:34 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-70561-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=sdfg.com.ar); spf=pass (google.com: domain of linux-kernel+bounces-70561-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-70561-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 292AD281CA2 for ; Sun, 18 Feb 2024 19:52:34 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0174D745D9; Sun, 18 Feb 2024 19:51:41 +0000 (UTC) Received: from alerce.blitiri.com.ar (alerce.blitiri.com.ar [49.12.208.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B1E9D7319A for ; Sun, 18 Feb 2024 19:51:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=49.12.208.134 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708285900; cv=none; b=dLvai4KIONbkN59Nk3zgFL2bQEuzeIAJr94bxDDZf7ZXnfFo+EQ3Noi62PKAiN/DNoJ1SGDU2v6vs92szg+omCJ/EqTH7PhkZ9mbYooD0kzofuBwtomvE3vRw3BFzHPr98jf7INK+YmPVV0hx3AO4wi5itouSDdpnEh32UgHJxM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708285900; c=relaxed/simple; bh=9RVmOlD4OgrvSuTTroVq8lj5KRTmT9fr3g11wmsxIZQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=BdUqKgOBMwh+cHWGnGIIwF/WUwcXtSdTSqjGK/t/tLzQakldjxD4ysSXdqQ4hzEo7IVvSGh6g02c/8dM9KP0uwjPm1BctI/VHn26vEpIlw4ZVWEMtOpXCpXZ4e4DcgKUZ1re/fDE0GHEyuFySLGZE5rN/rz/+dTmrbJD78p+aKE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=sdfg.com.ar; spf=pass smtp.mailfrom=sdfg.com.ar; arc=none smtp.client-ip=49.12.208.134 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=sdfg.com.ar Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=sdfg.com.ar Received: from lindsay.fibertel.com.ar by sdfg.com.ar (chasquid) with ESMTPSA tls TLS_AES_128_GCM_SHA256 (over submission, TLS-1.3, envelope from "rodrigo@sdfg.com.ar") ; Sun, 18 Feb 2024 19:51:36 +0000 From: Rodrigo Campos To: Willy Tarreau , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= Cc: linux-kernel@vger.kernel.org, Rodrigo Campos Subject: [PATCH v3 4/4] selftests/nolibc: Add tests for strlcat() and strlcpy() Date: Sun, 18 Feb 2024 16:51:06 -0300 Message-ID: <20240218195110.1386840-5-rodrigo@sdfg.com.ar> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240218195110.1386840-1-rodrigo@sdfg.com.ar> References: <20240218195110.1386840-1-rodrigo@sdfg.com.ar> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit I've verified that the tests matches libbsd's strlcat()/strlcpy() implementation. Please note that as strlcat()/strlcpy() are not part of the libc, the tests are only compiled when using nolibc. Signed-off-by: Rodrigo Campos --- tools/testing/selftests/nolibc/nolibc-test.c | 40 ++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c index 6ba4f8275ac4..d373fc14706c 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -600,6 +600,25 @@ int expect_strne(const char *expr, int llen, const char *cmp) return ret; } +#define EXPECT_STRBUFEQ(cond, expr, buf, val, cmp) \ + do { if (!(cond)) result(llen, SKIPPED); else ret += expect_str_buf_eq(expr, buf, val, llen, cmp); } while (0) + +static __attribute__((unused)) +int expect_str_buf_eq(size_t expr, const char *buf, size_t val, int llen, const char *cmp) +{ + llen += printf(" = %lu <%s> ", expr, buf); + if (strcmp(buf, cmp) != 0) { + result(llen, FAIL); + return 1; + } + if (expr != val) { + result(llen, FAIL); + return 1; + } + + result(llen, OK); + return 0; +} /* declare tests based on line numbers. There must be exactly one test per line. */ #define CASE_TEST(name) \ @@ -991,6 +1010,14 @@ int run_stdlib(int min, int max) for (test = min; test >= 0 && test <= max; test++) { int llen = 0; /* line length */ + /* For functions that take a long buffer, like strlcat() + * Add some more chars after the \0, to test functions that overwrite the buffer set + * the \0 at the exact right position. + */ + char buf[10] = "test123456"; + buf[4] = '\0'; + + /* avoid leaving empty lines below, this will insert holes into * test numbers. */ @@ -1007,6 +1034,19 @@ int run_stdlib(int min, int max) CASE_TEST(strchr_foobar_z); EXPECT_STRZR(1, strchr("foobar", 'z')); break; CASE_TEST(strrchr_foobar_o); EXPECT_STREQ(1, strrchr("foobar", 'o'), "obar"); break; CASE_TEST(strrchr_foobar_z); EXPECT_STRZR(1, strrchr("foobar", 'z')); break; +#ifdef NOLIBC + CASE_TEST(strlcat_0); EXPECT_STRBUFEQ(1, strlcat(buf, "bar", 0), buf, 3, "test"); break; + CASE_TEST(strlcat_1); EXPECT_STRBUFEQ(1, strlcat(buf, "bar", 1), buf, 4, "test"); break; + CASE_TEST(strlcat_5); EXPECT_STRBUFEQ(1, strlcat(buf, "bar", 5), buf, 7, "test"); break; + CASE_TEST(strlcat_6); EXPECT_STRBUFEQ(1, strlcat(buf, "bar", 6), buf, 7, "testb"); break; + CASE_TEST(strlcat_7); EXPECT_STRBUFEQ(1, strlcat(buf, "bar", 7), buf, 7, "testba"); break; + CASE_TEST(strlcat_8); EXPECT_STRBUFEQ(1, strlcat(buf, "bar", 8), buf, 7, "testbar"); break; + CASE_TEST(strlcpy_0); EXPECT_STRBUFEQ(1, strlcpy(buf, "bar", 0), buf, 3, "test"); break; + CASE_TEST(strlcpy_1); EXPECT_STRBUFEQ(1, strlcpy(buf, "bar", 1), buf, 3, ""); break; + CASE_TEST(strlcpy_2); EXPECT_STRBUFEQ(1, strlcpy(buf, "bar", 2), buf, 3, "b"); break; + CASE_TEST(strlcpy_3); EXPECT_STRBUFEQ(1, strlcpy(buf, "bar", 3), buf, 3, "ba"); break; + CASE_TEST(strlcpy_4); EXPECT_STRBUFEQ(1, strlcpy(buf, "bar", 4), buf, 3, "bar"); break; +#endif CASE_TEST(memcmp_20_20); EXPECT_EQ(1, memcmp("aaa\x20", "aaa\x20", 4), 0); break; CASE_TEST(memcmp_20_60); EXPECT_LT(1, memcmp("aaa\x20", "aaa\x60", 4), 0); break; CASE_TEST(memcmp_60_20); EXPECT_GT(1, memcmp("aaa\x60", "aaa\x20", 4), 0); break; -- 2.43.0