Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp3608325rwl; Sun, 2 Apr 2023 11:05:24 -0700 (PDT) X-Google-Smtp-Source: AKy350b1ZTUXjQuNCywSabpciDn22yYH6NP8iO3JqYuYnnp8kPv6cSUCBlbW1P0Pufe7XPiiwQi2 X-Received: by 2002:a17:907:d402:b0:930:ca4d:f2bf with SMTP id vi2-20020a170907d40200b00930ca4df2bfmr37830692ejc.54.1680458724730; Sun, 02 Apr 2023 11:05:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680458724; cv=none; d=google.com; s=arc-20160816; b=IRKJmTNtviFSmiCbLYGSqnDp85CuHXZv9WLIvgF17GBa5Z/FaXTYAVEZUfjMbjh333 yuAm3DInZIk6px3gpXeEJxBcM8+5BptXdFb6n7OMaT32mCBIxK1IIU9rUXf8pu1+89jP /iaZuG1Z+4MO5XFgwd8dV1tCV6j/9t2SndofKehgc7wmkgHXSFE7yVOo1E3KTOTazmSe IZs7gDC50RURGhwz/k2aYTE+8e4GVLPg0d8y7ybACrnEPnOpg3EO6U8dfrBnvACMmVaL WU+Zm1e8UmZdXxP/PXIeB9uF4LFlPTTLv/qJ5eB83wnYWHCMIAhwGj87JJW3/NYGL+cg 3xNA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:dkim-signature :from; bh=boSegzIl0yeeY31ZX1otQJXyEx/cGmJA7NXEg+7FPr4=; b=pu+lSZHi9jCxwxCzLdpP1xAXhL/3I3LjQ+DSfFF/pqmHxHXrW+DF/CB4WDEgz1VtCI DwTqTRIMhIeBi1gr3NUgGO9v6Ax44xbMYcEbpEFcMM/fJWsxhSwUaOMMhKajUp5keiaa TOBI0AmAQodnOhQ7owBu6sGpqx0Jb6YtiTemhv+jIU4Z/QvxAx7w2kw6tVIIgMuYh8Gm sfMSQ1HV3fTM0uAQapf45hS6hlunHaPgIp2sL8dH/1R/+Gi38kv8RyIcZHfSuwHgthPP FQKDgzNiUX//NLQTb3E5zgoo50obeWNc0fVOuYUqnEg2TjTVnYil4gKk5d1rr1Ck58Jx XXvg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@weissschuh.net header.s=mail header.b=hAqtsk1t; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a12-20020a1709062b0c00b0092d6e62eb2bsi4552249ejg.197.2023.04.02.11.05.00; Sun, 02 Apr 2023 11:05:24 -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=@weissschuh.net header.s=mail header.b=hAqtsk1t; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231384AbjDBSEx (ORCPT + 99 others); Sun, 2 Apr 2023 14:04:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35090 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231136AbjDBSEr (ORCPT ); Sun, 2 Apr 2023 14:04:47 -0400 Received: from todd.t-8ch.de (todd.t-8ch.de [IPv6:2a01:4f8:c010:41de::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9F5DA5255; Sun, 2 Apr 2023 11:04:45 -0700 (PDT) From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1680458683; bh=lRlYUuDp5glflddJvEoeapRiiCbk9usZQ1sjBHNJHWA=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=hAqtsk1tMBTOcLmQdfShfuClxGMOfQ87C1yjw1nXaohBeAmIZ/e/XxuFfsdy7yO+p OXqzOIh903ka6x+00ab+Iz2eJSzlOzTipUjoo/vQ0DSbbJ3BJgIGarqD0mQTag+73p CKaeLPdhaEvB9qxGbfTqKeI8hzmdfQSmNYAYoOU0= Date: Sun, 02 Apr 2023 18:04:37 +0000 Subject: [PATCH v3 4/4] tools/nolibc: add testcases for vfprintf MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Message-Id: <20230328-nolibc-printf-test-v3-4-ddc79f92efd5@weissschuh.net> References: <20230328-nolibc-printf-test-v3-0-ddc79f92efd5@weissschuh.net> In-Reply-To: <20230328-nolibc-printf-test-v3-0-ddc79f92efd5@weissschuh.net> To: Willy Tarreau , Shuah Khan Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Mailer: b4 0.12.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1680458682; l=3462; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=lRlYUuDp5glflddJvEoeapRiiCbk9usZQ1sjBHNJHWA=; b=r1gaqwcr5bI+JYJYF6F/e7zU/ZbkMtq+8Nx9jeWHBtiCSsdad9o8hV1RBLVCxN8lFZ0Rv2ta+ gv3a94JEvWNBu2sE9onoc7uR2GvxejWHKmk4N4eDNT6RtNdjesRUa4P X-Developer-Key: i=linux@weissschuh.net; a=ed25519; pk=KcycQgFPX2wGR5azS7RhpBqedglOZVgRPfdFSPB1LNw= X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=unavailable 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 vfprintf() is complex and so far did not have proper tests. Signed-off-by: Thomas Weißschuh --- 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 47013b78972e..de943e028933 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 @@ -667,6 +668,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]; @@ -774,6 +859,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.0