Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp1107635rwd; Thu, 18 May 2023 08:04:29 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7YIP0x0ZLzdvelQJQIARN8T8uSv8AMajG8D/80uJETPX2hnRgbfJFBk2aomQoeA36JCBG1 X-Received: by 2002:a05:6a20:9585:b0:101:5f22:b46d with SMTP id iu5-20020a056a20958500b001015f22b46dmr2718808pzb.29.1684422268891; Thu, 18 May 2023 08:04:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684422268; cv=none; d=google.com; s=arc-20160816; b=EXAT1hSu+O/osTWd92AgRaztrpvdApjGTeXkpLadCxe4f8xixTF2jFx4+MGEWZMmz4 sn6/fLFpRW8LNG5jJA7nq9YoqKjAKGu7HNPFgezJHZl5z1xfXQBHo1utby2Rbm1ri1xS 6+vI+NxaRgAvIz7kAIfMIfJV0BjYPNSkS0dFXzX1ScXvSnHZ1lVkR2UH4pgTLsyGztDa 4IBWY0OXxNO3ChMtJBxjHKeoVSvlppGWQet09XmaqrdrVyI6MmebObjGo9X7m4thbHMe YiGavjhkEGw0bOlKWXXDbHcfrnE2ZJYldyBPEiRPdD1D4gX0wrN2OPBXlmgIpinRVYLS SjtQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=MjOYl2zEtcL5/OiYzzz0OaFOmscGAYNCUZZLv9wU5J4=; b=0IKbfLWug91OfVDakYe9sfiYqfY55gtvTUBbrZOzxZ+5qWR/uggpGjLYjXSG47PEKe 5JGC4eei0FQ5Op/PyWZ4t8CsP8EgH7ad9CXgPUOVNuF9k7HXuOhFY8rLaTnaHavxITrw 7BuGo2FOqkXd+y/EtCBZRHYhXDMan2L3aOH61KnN+eVmdWLa/DTkR3fKXaP0KTK/WDSM 01HGoJAEJJIaU2Exyd9Rvs11/XtEBVC325lm5pW6e76xwM3ZVjc+7RypiE0CNea0h4Uz UAjxDD4x/SFAVrpWJBsIZu8oADqzASaagt02jOyOW5nvfXDljBbRpBuVhk+rf7MnWs8I +eLQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=exJk6fUV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l186-20020a6391c3000000b00534842ba603si227021pge.820.2023.05.18.08.04.09; Thu, 18 May 2023 08:04:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=exJk6fUV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231804AbjEROzt (ORCPT + 99 others); Thu, 18 May 2023 10:55:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35958 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231750AbjEROza (ORCPT ); Thu, 18 May 2023 10:55:30 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EC91B12E for ; Thu, 18 May 2023 07:55:27 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 593BE64FE7 for ; Thu, 18 May 2023 14:55:25 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B6A0BC433A1; Thu, 18 May 2023 14:55:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1684421723; bh=gpgpcEtunmdu9Bwe6EgvPNpLf5lDxIDNN1gL/ShrEz4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=exJk6fUVzzcyLzaPPr1taRHEZh0enXf1X54640PYulffd2Un8/j2KA8Feo1md9LgI 1CuSNHS5L0wTj1uTNGuX87n0GgdB2jVRR7ETNCPskPSGWU9mbpgOJ9b0ytdnugd46U tZtjuDYRpME60oJpLRivbDIFJ2HG7TC/yBd2j9V5tgkrboOQBHHIq8CoNwqfh8kJIG XAVFNmTb+BL/8DZSqHamFBEkbEcGYfJjCvzV84YQ/OGRE1AvzrOB0XKK9189rI3WhY s9eO+V5bXAN6IReEILoVlzcxi2nay734ud7jeinMmh7s/tzSgMTENPUaXXefackw0M X/mx0OoI4Vp0w== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 21BAECE1431; Thu, 18 May 2023 07:55:23 -0700 (PDT) From: "Paul E. McKenney" To: linux-kernel@vger.kernel.org Cc: gwml@vger.gnuweeb.org, kernel-team@meta.com, w@lwt.eu, =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Willy Tarreau , "Paul E . McKenney" Subject: [PATCH v2 nolibc 07/15] tools/nolibc: add testcases for vfprintf Date: Thu, 18 May 2023 07:55:13 -0700 Message-Id: <20230518145521.3806117-7-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <4817a4dc-69e6-4937-84d5-f2f630ff646c@paulmck-laptop> References: <4817a4dc-69e6-4937-84d5-f2f630ff646c@paulmck-laptop> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Thomas Weißschuh vfprintf() is complex and so far did not have proper tests. Signed-off-by: Thomas Weißschuh Signed-off-by: Willy Tarreau Signed-off-by: Paul E. McKenney --- tools/testing/selftests/nolibc/nolibc-test.c | 86 ++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c index 1bafbd8da6af..888da60eb5ba 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -669,6 +670,90 @@ int run_stdlib(int min, int max) return ret; } +#define EXPECT_VFPRINTF(c, expected, fmt, ...) \ + ret += expect_vfprintf(llen, c, expected, fmt, ##__VA_ARGS__) + +static int expect_vfprintf(int llen, size_t c, const char *expected, const char *fmt, ...) +{ + int ret, fd, w, r; + char buf[100]; + FILE *memfile; + va_list args; + + fd = memfd_create("vfprintf", 0); + if (fd == -1) { + pad_spc(llen, 64, "[FAIL]\n"); + return 1; + } + + memfile = fdopen(fd, "w+"); + if (!memfile) { + pad_spc(llen, 64, "[FAIL]\n"); + return 1; + } + + va_start(args, fmt); + w = vfprintf(memfile, fmt, args); + va_end(args); + + if (w != c) { + llen += printf(" written(%d) != %d", w, (int) c); + pad_spc(llen, 64, "[FAIL]\n"); + return 1; + } + + fflush(memfile); + lseek(fd, 0, SEEK_SET); + + r = read(fd, buf, sizeof(buf) - 1); + buf[r] = '\0'; + + fclose(memfile); + + if (r != w) { + llen += printf(" written(%d) != read(%d)", w, r); + pad_spc(llen, 64, "[FAIL]\n"); + return 1; + } + + llen += printf(" \"%s\" = \"%s\"", expected, buf); + ret = strncmp(expected, buf, c); + + pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n"); + return ret; +} + +static int run_vfprintf(int min, int max) +{ + int test; + int tmp; + int ret = 0; + void *p1, *p2; + + for (test = min; test >= 0 && test <= max; test++) { + int llen = 0; // line length + + /* avoid leaving empty lines below, this will insert holes into + * test numbers. + */ + switch (test + __LINE__ + 1) { + CASE_TEST(empty); EXPECT_VFPRINTF(0, "", ""); break; + CASE_TEST(simple); EXPECT_VFPRINTF(3, "foo", "foo"); break; + CASE_TEST(string); EXPECT_VFPRINTF(3, "foo", "%s", "foo"); break; + CASE_TEST(number); EXPECT_VFPRINTF(4, "1234", "%d", 1234); break; + CASE_TEST(negnumber); EXPECT_VFPRINTF(5, "-1234", "%d", -1234); break; + CASE_TEST(unsigned); EXPECT_VFPRINTF(5, "12345", "%u", 12345); break; + CASE_TEST(char); EXPECT_VFPRINTF(1, "c", "%c", 'c'); break; + CASE_TEST(hex); EXPECT_VFPRINTF(1, "f", "%x", 0xf); break; + CASE_TEST(pointer); EXPECT_VFPRINTF(3, "0x1", "%p", (void *) 0x1); break; + case __LINE__: + return ret; /* must be last */ + /* note: do not set any defaults so as to permit holes above */ + } + } + return ret; +} + static int smash_stack(void) { char buf[100]; @@ -777,6 +862,7 @@ static const struct test test_names[] = { /* add new tests here */ { .name = "syscall", .func = run_syscall }, { .name = "stdlib", .func = run_stdlib }, + { .name = "vfprintf", .func = run_vfprintf }, { .name = "protection", .func = run_protection }, { 0 } }; -- 2.40.1