From nobody Fri Sep 08 21:23:29 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4Rj8H56jP9z4t8F9; Fri, 8 Sep 2023 21:23:29 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Rj8H55YJlz3Cgw; Fri, 8 Sep 2023 21:23:29 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1694208209; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=/sMElcyTjfmuyE6/rI234JPNaCMw/0V3NG4Cg4UfYrM=; b=lO581briQ0a6/9cyKeklgv9m5emDmgQoLv0ObBDbKhqMUbvmsCGFt04LLmeGTAWtVR4dW2 kVmS7MhuEex/7ABt9+avvkiClh1BxDQOtzlLiSrQF/+19Tv9GCXtfSGzTJbWyHPL07TPYE mP9Nk8UVhQ3OCWkUh7Ux73L+58KoRwOFunH1zZayF0+x2wEDZuYkvuXKGyqVTLtbOfD3Jq 50aiPUxci0p2xiLOnSE2Cwhg8RsP7IlJqTKq1eRAUgA2pWjhFu2oVRdRhbmZY59vwh+NNJ af7anXzTSM2y0HyUQn0JzFVsdOvx27JtuVFZvq0bbjYzyflRQRIsEpsDxbJmag== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1694208209; a=rsa-sha256; cv=none; b=aJCWWi3lfxANyXRD6BUzd+eTUWB37VmJgnSyIteadHgUpr95w0wyHKe9pgS44lltVePuC8 4NBTL+tqH9Nu1FgSkKC6U7t+suoo6HEwZzKRa6NBOQkCtwGf+vtBLNItBRyEjPKBvP/jPD SdTsEorxYVEem4acFjb5D1VvzO3HC8tKhDwMm2k/gnGya7a9Iq5xR1sb3g+pQd9dXexdzd rcG2YCextM3KA6iZ1PYsi5TLk5K1uyINwJWWbxQ6h0a2tonjmyDU1jczxSbG+/8hYT/zH0 3mdcHOrObIc7qVsJu1C8c70WuGHKXmGgICvcXA9fB5xSI0TbxF9cPiJeiIViHg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1694208209; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=/sMElcyTjfmuyE6/rI234JPNaCMw/0V3NG4Cg4UfYrM=; b=kc2/rvayN/c2iWDjRt47Xt99UdzYU2KWrJog+x32bbZvb48T5SCI3UQmjER6J5mPgCivxx s7f49tc+tkyxsT5abymlaAfy4t2NitNouM0hTy7f1lAYacQ/713xdT5YGcqZFFHImYQHSy bjMdo2kNETpGmJQ//JKFtJY/rheeW+wZm2A7HqSpibgUfbkLxqXNn8hEpH6vK+suKGbdU3 mJxXAEBvqveRDUrO9m9spM50E/v6P6++FtX6IVSZvAuNG5hSMbE9TW9/SRebqp52mdV41r LN9fb+8WcOnPxJrj1JRfggi9j0RAq/PBIBVkmX2PMsw66u1KTtAiiL3+pdi6Xg== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4Rj8H54Qpyzy5H; Fri, 8 Sep 2023 21:23:29 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 388LNTFd057355; Fri, 8 Sep 2023 21:23:29 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 388LNT3j057352; Fri, 8 Sep 2023 21:23:29 GMT (envelope-from git) Date: Fri, 8 Sep 2023 21:23:29 GMT Message-Id: <202309082123.388LNT3j057352@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Robert Clausecker Subject: git: 35a5359406fe - main - lib/libc/tests/string: add unit tests for strcspn(3) List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: fuz X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 35a5359406fe779186d1fd7131c95927fefe20be Auto-Submitted: auto-generated The branch main has been updated by fuz: URL: https://cgit.FreeBSD.org/src/commit/?id=35a5359406fe779186d1fd7131c95927fefe20be commit 35a5359406fe779186d1fd7131c95927fefe20be Author: Robert Clausecker AuthorDate: 2023-08-20 22:48:41 +0000 Commit: Robert Clausecker CommitDate: 2023-09-08 21:21:31 +0000 lib/libc/tests/string: add unit tests for strcspn(3) We currently use the NetBSD test suite to cover strcspn(3). It only contains a very rudimentary test of this function. This all new set of unit tests for the FreeBSD test suite should cover many more edge cases relating to alignment issues. Sponsored by: The FreeBSD Foundation Approved by: mjg MFC after: 1 week MFC to: stable/14 Differential Revision: https://reviews.freebsd.org/D41557 --- lib/libc/tests/string/Makefile | 2 +- lib/libc/tests/string/strcspn_test.c | 137 +++++++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+), 1 deletion(-) diff --git a/lib/libc/tests/string/Makefile b/lib/libc/tests/string/Makefile index a7545b75e0e9..639b7f2d2d2d 100644 --- a/lib/libc/tests/string/Makefile +++ b/lib/libc/tests/string/Makefile @@ -11,6 +11,7 @@ ATF_TESTS_C+= flsll_test ATF_TESTS_C+= memcmp_test ATF_TESTS_C+= memset_s_test ATF_TESTS_C+= stpncpy_test +ATF_TESTS_C+= strcspn_test ATF_TESTS_C+= strerror2_test ATF_TESTS_C+= strverscmp_test ATF_TESTS_C+= strxfrm_test @@ -29,7 +30,6 @@ NETBSD_ATF_TESTS_C+= strchr_test NETBSD_ATF_TESTS_C+= strchrnul_test NETBSD_ATF_TESTS_C+= strcmp_test NETBSD_ATF_TESTS_C+= strcpy_test -NETBSD_ATF_TESTS_C+= strcspn_test NETBSD_ATF_TESTS_C+= strerror_test NETBSD_ATF_TESTS_C+= strlen_test NETBSD_ATF_TESTS_C+= strpbrk_test diff --git a/lib/libc/tests/string/strcspn_test.c b/lib/libc/tests/string/strcspn_test.c new file mode 100644 index 000000000000..59da497c16c6 --- /dev/null +++ b/lib/libc/tests/string/strcspn_test.c @@ -0,0 +1,137 @@ +/*- + * Copyright (c) 2023 The FreeBSD Foundation + * + * This software was developed by Robert Clausecker + * under sponsorship from the FreeBSD Foundation. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ''AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE + */ + +#include + +#include +#include +#include +#include +#include +#include + +enum { + MAXALIGN = 16, /* test all offsets from this alignment */ + MAXBUF = 64, /* test up to this buffer length */ +}; + +enum { NOMATCH, MATCH }; + +static void +testcase(char *buf, size_t buflen, char *set, size_t setlen, int want_match) +{ + size_t i, outcome, expected; + + assert(setlen < UCHAR_MAX - 2); + + for (i = 0; i < buflen; i++) + buf[i] = 1 + i % (UCHAR_MAX - setlen - 1); + + buf[i] = '\0'; + + for (i = 0; i < setlen; i++) + set[i] = UCHAR_MAX - i; + + set[i] = '\0'; + + if (want_match == MATCH && buflen > 0 && setlen > 0) { + buf[buflen - 1] = UCHAR_MAX; + expected = buflen - 1; + } else + expected = buflen; + + outcome = strcspn(buf, set); + ATF_CHECK_EQ_MSG(expected, outcome, "strcspn(%p[%zu], %p[%zu]) = %zu != %zu", + buf, buflen, set, setlen, outcome, expected); +} + +/* test set with all alignments and lengths of buf */ +static void +test_buf_alignments(char *set, size_t setlen, int want_match) +{ + char buf[MAXALIGN + MAXBUF + 1]; + size_t i, j; + + for (i = 0; i < MAXALIGN; i++) + for (j = 0; j <= MAXBUF; j++) + testcase(buf + i, j, set, setlen, want_match); +} + +/* test buf with all alignments and lengths of set */ +static void +test_set_alignments(char *buf, size_t buflen, int want_match) +{ + char set[MAXALIGN + MAXBUF + 1]; + size_t i, j; + + for (i = 0; i < MAXALIGN; i++) + for (j = 0; j <= MAXBUF; j++) + testcase(buf, buflen, set + i, j, want_match); +} + +ATF_TC_WITHOUT_HEAD(buf_alignments); +ATF_TC_BODY(buf_alignments, tc) +{ + char set[41]; + + test_buf_alignments(set, 0, MATCH); + test_buf_alignments(set, 1, MATCH); + test_buf_alignments(set, 5, MATCH); + test_buf_alignments(set, 20, MATCH); + test_buf_alignments(set, 40, MATCH); + + test_buf_alignments(set, 0, NOMATCH); + test_buf_alignments(set, 1, NOMATCH); + test_buf_alignments(set, 5, NOMATCH); + test_buf_alignments(set, 20, NOMATCH); + test_buf_alignments(set, 40, NOMATCH); +} + +ATF_TC_WITHOUT_HEAD(set_alignments); +ATF_TC_BODY(set_alignments, tc) +{ + char buf[31]; + + test_set_alignments(buf, 0, MATCH); + test_set_alignments(buf, 10, MATCH); + test_set_alignments(buf, 20, MATCH); + test_set_alignments(buf, 30, MATCH); + + test_set_alignments(buf, 0, NOMATCH); + test_set_alignments(buf, 10, NOMATCH); + test_set_alignments(buf, 20, NOMATCH); + test_set_alignments(buf, 30, NOMATCH); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, buf_alignments); + ATF_TP_ADD_TC(tp, set_alignments); + + return (atf_no_error()); +}