From patchwork Mon Aug 14 13:23:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Fitzgerald X-Patchwork-Id: 135446 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b824:0:b0:3f2:4152:657d with SMTP id z4csp2761777vqi; Mon, 14 Aug 2023 06:58:19 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF+rh/0ZGvL7U4mKdMcizaN0rnh+tQ4CFG+Q7xtVotflYHzXmghRJPnom7NMIwNsImTsFxd X-Received: by 2002:a17:90a:bb88:b0:269:524f:2a19 with SMTP id v8-20020a17090abb8800b00269524f2a19mr8390704pjr.26.1692021499497; Mon, 14 Aug 2023 06:58:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692021499; cv=none; d=google.com; s=arc-20160816; b=srxhgGJvpG5C8nByofns7NJQ8cN4hzEZkcuVrNWjnOZdtnbgFnPalyD02bc5Dytyok VSLbO2QyThEQFmGkLBW+HQjdNPCYIZ8XMtJI0Q6et+j9OZioMCqR/AyRP57eoa9kmEZ5 1O5YnGs6z6Q7v2umbbHx8ojwFaJBvFhcvkUlVR2rzbzWezNfRVGt6iTd8Al1O6F4waqR pp9jd6cr2vBj6g0u9OU72BWzta6kvYkOSMFXU9wsLSGXYpQTlBkp5QiFBImFZSLZjwK0 OJ1Gj9W00qbVDoNowXO/BGSBB/FVr9cWXs/6tlARQYdGhcLmiWTkQIzhaWFd86wiaI/m sIhA== 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=FU/pJaIEhjt7vz5e+0+86R8xFO1BN13daUDaz8Lh1zE=; fh=bGF4ESczbIrmrNxFOdEMfzJNgTVkRsqDRt+LRN0CeSg=; b=sl31cncE2xIiZB94DI1kes2y/9jKBVwSPTQDs+gVMq5MuQH1lc0ACnhfIWDmJ0WusH 6rbMm0kfNqy15qZSG0yepiiI6/JwNR8I1/4I5CmIFM+WfwcfrKmcJeEn2eex1aLcEn31 ZrFdBlXZNJ/SPSPJotvuJHyogAK03DMdtZK5qBY7JMUqu5nL3+D4nDUH3gAsfo8Q/wIf Jlc5EN7yD6Yw6cR6hYK/G7roxZuooIG7kWtgW2DAugfIJGrCUiX3k5tqQh4A8/2nvTa+ UCTcB4eM7k4mXBJMefUA8BnPxruMLqd+bU1mpDsjHAAnFHwj4Le893WD2kEmfeqA2DRH o5oQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cirrus.com header.s=PODMain02222019 header.b=lg+BauF3; 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=REJECT sp=REJECT dis=NONE) header.from=cirrus.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id nv11-20020a17090b1b4b00b00269584b6a10si8490985pjb.15.2023.08.14.06.58.05; Mon, 14 Aug 2023 06:58:19 -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=@cirrus.com header.s=PODMain02222019 header.b=lg+BauF3; 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=REJECT sp=REJECT dis=NONE) header.from=cirrus.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230500AbjHNNX4 (ORCPT + 99 others); Mon, 14 Aug 2023 09:23:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41904 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230461AbjHNNXg (ORCPT ); Mon, 14 Aug 2023 09:23:36 -0400 Received: from mx0b-001ae601.pphosted.com (mx0a-001ae601.pphosted.com [67.231.149.25]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 02AF110E5; Mon, 14 Aug 2023 06:23:33 -0700 (PDT) Received: from pps.filterd (m0077473.ppops.net [127.0.0.1]) by mx0a-001ae601.pphosted.com (8.17.1.22/8.17.1.22) with ESMTP id 37E5t3EG003856; Mon, 14 Aug 2023 08:23:14 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cirrus.com; h= from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding:content-type; s= PODMain02222019; bh=FU/pJaIEhjt7vz5e+0+86R8xFO1BN13daUDaz8Lh1zE=; b= lg+BauF3d1EPVommxd8WM530yR8/B7dUEHbKdW1MmFG43csH29HthV/hkm5z/Xvl Ee+wcHRSzkNdcrO/2mcAscjH/JPx0+Y+RI6BNHiJamf/TCa5M27GNNDDOlsAKnxs t3+5o//UfaQZU2FJYhbh0CM4KtHhU2sG4zsxogIwBSpahmcCVeIqdg8MQJOFcH7E HypXCiarqGFK2c35gV61kapPiepKAJJMY7zUag0ml/3t/zMz7jqX6o8vXoq9mxMK yklpldLGsgvpm5QUOsRbWIMimMGBuhfD42Zv07pA8JPg//ilYVt91hk96yiWXSot GlohbR2EWsOyKVVUFih7FQ== Received: from ediex02.ad.cirrus.com ([84.19.233.68]) by mx0a-001ae601.pphosted.com (PPS) with ESMTPS id 3se8kqt6x8-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 14 Aug 2023 08:23:14 -0500 (CDT) Received: from ediex02.ad.cirrus.com (198.61.84.81) by ediex02.ad.cirrus.com (198.61.84.81) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.30; Mon, 14 Aug 2023 14:23:12 +0100 Received: from ediswmail.ad.cirrus.com (198.61.86.93) by anon-ediex02.ad.cirrus.com (198.61.84.81) with Microsoft SMTP Server id 15.2.1118.30 via Frontend Transport; Mon, 14 Aug 2023 14:23:12 +0100 Received: from EDIN4L06LR3.ad.cirrus.com (EDIN4L06LR3.ad.cirrus.com [198.61.65.68]) by ediswmail.ad.cirrus.com (Postfix) with ESMTP id E14893563; Mon, 14 Aug 2023 13:23:11 +0000 (UTC) From: Richard Fitzgerald To: , , CC: , , , , Richard Fitzgerald Subject: [PATCH v4 01/10] kunit: string-stream: Improve testing of string_stream Date: Mon, 14 Aug 2023 14:23:00 +0100 Message-ID: <20230814132309.32641-2-rf@opensource.cirrus.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230814132309.32641-1-rf@opensource.cirrus.com> References: <20230814132309.32641-1-rf@opensource.cirrus.com> MIME-Version: 1.0 X-Proofpoint-ORIG-GUID: bQ7xVCNwGc8QkOm5-z7g4U3kIXvT1tBT X-Proofpoint-GUID: bQ7xVCNwGc8QkOm5-z7g4U3kIXvT1tBT X-Proofpoint-Spam-Reason: safe X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1774213135830613334 X-GMAIL-MSGID: 1774213135830613334 Replace the minimal tests with more-thorough testing. string_stream_init_test() tests that struct string_stream is initialized correctly. string_stream_line_add_test() adds a series of numbered lines and checks that the resulting string contains all the lines. string_stream_variable_length_line_test() adds a large number of lines of varying length to create many fragments, then tests that all lines are present. string_stream_append_test() tests various cases of using string_stream_append() to append the content of one stream to another. Signed-off-by: Richard Fitzgerald --- lib/kunit/string-stream-test.c | 200 ++++++++++++++++++++++++++++++--- 1 file changed, 184 insertions(+), 16 deletions(-) diff --git a/lib/kunit/string-stream-test.c b/lib/kunit/string-stream-test.c index 110f3a993250..1d46d5f06d2a 100644 --- a/lib/kunit/string-stream-test.c +++ b/lib/kunit/string-stream-test.c @@ -11,38 +11,206 @@ #include "string-stream.h" -static void string_stream_test_empty_on_creation(struct kunit *test) +/* string_stream object is initialized correctly. */ +static void string_stream_init_test(struct kunit *test) { - struct string_stream *stream = alloc_string_stream(test, GFP_KERNEL); + struct string_stream *stream; + + stream = alloc_string_stream(test, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); + + KUNIT_EXPECT_EQ(test, stream->length, 0); + KUNIT_EXPECT_TRUE(test, list_empty(&stream->fragments)); + KUNIT_EXPECT_PTR_EQ(test, stream->test, test); + KUNIT_EXPECT_EQ(test, stream->gfp, GFP_KERNEL); KUNIT_EXPECT_TRUE(test, string_stream_is_empty(stream)); } -static void string_stream_test_not_empty_after_add(struct kunit *test) +/* + * Add a series of lines to a string_stream. Check that all lines + * appear in the correct order and no characters are dropped. + */ +static void string_stream_line_add_test(struct kunit *test) { - struct string_stream *stream = alloc_string_stream(test, GFP_KERNEL); + struct string_stream *stream; + char line[60]; + char *concat_string, *pos, *string_end; + size_t len, total_len; + int num_lines, i; - string_stream_add(stream, "Foo"); + stream = alloc_string_stream(test, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); - KUNIT_EXPECT_FALSE(test, string_stream_is_empty(stream)); + /* Add series of sequence numbered lines */ + total_len = 0; + for (i = 0; i < 100; ++i) { + len = snprintf(line, sizeof(line), + "The quick brown fox jumps over the lazy penguin %d\n", i); + + /* Sanity-check that our test string isn't truncated */ + KUNIT_ASSERT_LT(test, len, sizeof(line)); + + string_stream_add(stream, line); + total_len += len; + } + num_lines = i; + + concat_string = string_stream_get_string(stream); + KUNIT_EXPECT_NOT_ERR_OR_NULL(test, concat_string); + KUNIT_EXPECT_EQ(test, strlen(concat_string), total_len); + + /* + * Split the concatenated string at the newlines and check that + * all the original added strings are present. + */ + pos = concat_string; + for (i = 0; i < num_lines; ++i) { + string_end = strchr(pos, '\n'); + KUNIT_EXPECT_NOT_NULL(test, string_end); + + /* Convert to NULL-terminated string */ + *string_end = '\0'; + + snprintf(line, sizeof(line), + "The quick brown fox jumps over the lazy penguin %d", i); + KUNIT_EXPECT_STREQ(test, pos, line); + + pos = string_end + 1; + } + + /* There shouldn't be any more data after this */ + KUNIT_EXPECT_EQ(test, strlen(pos), 0); } -static void string_stream_test_get_string(struct kunit *test) +/* Add a series of lines of variable length to a string_stream. */ +static void string_stream_variable_length_line_test(struct kunit *test) { - struct string_stream *stream = alloc_string_stream(test, GFP_KERNEL); - char *output; + static const char line[] = + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + " 0123456789!$%^&*()_-+={}[]:;@'~#<>,.?/|"; + struct string_stream *stream; + struct rnd_state rnd; + char *concat_string, *pos, *string_end; + size_t offset, total_len; + int num_lines, i; - string_stream_add(stream, "Foo"); - string_stream_add(stream, " %s", "bar"); + stream = alloc_string_stream(test, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); - output = string_stream_get_string(stream); - KUNIT_ASSERT_STREQ(test, output, "Foo bar"); + /* + * Log many lines of varying lengths until we have created + * many fragments. + * The "randomness" must be repeatable. + */ + prandom_seed_state(&rnd, 3141592653589793238ULL); + total_len = 0; + for (i = 0; i < 100; ++i) { + offset = prandom_u32_state(&rnd) % (sizeof(line) - 1); + string_stream_add(stream, "%s\n", &line[offset]); + total_len += sizeof(line) - offset; + } + num_lines = i; + + concat_string = string_stream_get_string(stream); + KUNIT_EXPECT_NOT_ERR_OR_NULL(test, concat_string); + KUNIT_EXPECT_EQ(test, strlen(concat_string), total_len); + + /* + * Split the concatenated string at the newlines and check that + * all the original added strings are present. + */ + prandom_seed_state(&rnd, 3141592653589793238ULL); + pos = concat_string; + for (i = 0; i < num_lines; ++i) { + string_end = strchr(pos, '\n'); + KUNIT_EXPECT_NOT_NULL(test, string_end); + + /* Convert to NULL-terminated string */ + *string_end = '\0'; + + offset = prandom_u32_state(&rnd) % (sizeof(line) - 1); + KUNIT_EXPECT_STREQ(test, pos, &line[offset]); + + pos = string_end + 1; + } + + /* There shouldn't be any more data after this */ + KUNIT_EXPECT_EQ(test, strlen(pos), 0); +} + +/* Appending the content of one string stream to another. */ +static void string_stream_append_test(struct kunit *test) +{ + static const char * const strings_1[] = { + "one", "two", "three", "four", "five", "six", + "seven", "eight", "nine", "ten", + }; + static const char * const strings_2[] = { + "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIZE", + "SEVEN", "EIGHT", "NINE", "TEN", + }; + struct string_stream *stream_1, *stream_2; + const char *original_content, *stream_2_content; + char *combined_content; + size_t combined_length; + int i; + + stream_1 = alloc_string_stream(test, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream_1); + + stream_2 = alloc_string_stream(test, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream_2); + + /* Append content of empty stream to empty stream */ + string_stream_append(stream_1, stream_2); + KUNIT_EXPECT_EQ(test, strlen(string_stream_get_string(stream_1)), 0); + + /* Add some data to stream_1 */ + for (i = 0; i < ARRAY_SIZE(strings_1); ++i) + string_stream_add(stream_1, "%s\n", strings_1[i]); + + original_content = string_stream_get_string(stream_1); + + /* Append content of empty stream to non-empty stream */ + string_stream_append(stream_1, stream_2); + KUNIT_EXPECT_STREQ(test, string_stream_get_string(stream_1), original_content); + + /* Add some data to stream_2 */ + for (i = 0; i < ARRAY_SIZE(strings_2); ++i) + string_stream_add(stream_2, "%s\n", strings_2[i]); + + /* Append content of non-empty stream to non-empty stream */ + string_stream_append(stream_1, stream_2); + + /* + * End result should be the original content of stream_1 plus + * the content of stream_2. + */ + stream_2_content = string_stream_get_string(stream_2); + combined_length = strlen(original_content) + strlen(stream_2_content); + combined_length++; /* for terminating \0 */ + combined_content = kunit_kmalloc(test, combined_length, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, combined_content); + snprintf(combined_content, combined_length, "%s%s", original_content, stream_2_content); + + KUNIT_EXPECT_STREQ(test, string_stream_get_string(stream_1), combined_content); + + /* Append content of non-empty stream to empty stream */ + string_stream_destroy(stream_1); + + stream_1 = alloc_string_stream(test, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream_1); + + string_stream_append(stream_1, stream_2); + KUNIT_EXPECT_STREQ(test, string_stream_get_string(stream_1), stream_2_content); } static struct kunit_case string_stream_test_cases[] = { - KUNIT_CASE(string_stream_test_empty_on_creation), - KUNIT_CASE(string_stream_test_not_empty_after_add), - KUNIT_CASE(string_stream_test_get_string), + KUNIT_CASE(string_stream_init_test), + KUNIT_CASE(string_stream_line_add_test), + KUNIT_CASE(string_stream_variable_length_line_test), + KUNIT_CASE(string_stream_append_test), {} }; From patchwork Mon Aug 14 13:23:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Fitzgerald X-Patchwork-Id: 135443 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b824:0:b0:3f2:4152:657d with SMTP id z4csp2759856vqi; Mon, 14 Aug 2023 06:54:08 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF8nSglPsQEThp5luEDX7YNPyDhEWRQO/V3M+V9PrV30mS9V/H29p4DuuICcMl7qGiwB2Pe X-Received: by 2002:a17:903:41cb:b0:1b3:b84b:9008 with SMTP id u11-20020a17090341cb00b001b3b84b9008mr12184364ple.5.1692021247655; Mon, 14 Aug 2023 06:54:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692021247; cv=none; d=google.com; s=arc-20160816; b=BsIHjZnKVmPBll3YIEwMA/TGNPpfEIM5xiRZApHQBdHHVK7jRxNYvOHLI9FcH2a8sW sgCgQ6s20+U+o5PErPIs+baVjGgQzQ87eppNYzQekVMxPBtadH9WukCSvgsbVLYoBKma FggRPH00TRJBMNhinLouGLTHkHvM4CtvljCfLzJcc43/0unAtj+eukz2cw9uDu/mkopy Eh7s4DUvUeHqdlgfZSRg+x6WzzxRXsX4s83flUjmwZMvI4IUPs9tNdt6z1SX/1Vtft+a u6dnTfSBpsjooZo4TZR5EtIf57uM4a5/TIrm2R6VVg6aMpVDZgRPgzB6XhdBL27Vr/6U vjbg== 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=Tu5hYj0cENcnReD71Hf/Ug24TIdVaaEFqB1yCIa77wM=; fh=bGF4ESczbIrmrNxFOdEMfzJNgTVkRsqDRt+LRN0CeSg=; b=Z+QIhQx8/bQFK7mVDsm7PkQiaq5AgTfQMakqsamXVl5iQqR1MVPdT4TO4FXlyT1S1e eIGldnBZ4lKhaAh9bwktkgAVeZ9T02jYsIxo/K3rGYq6MB/uvMKr88jDu2wk8W6Z8n/p naD6BbusDTi2JFiano9mcMTgIO1UxnAYn6QmIqrda1mvoTCd7UTb5hymkpIv9fAmpi4b yKVRXS6kLISQLOfQJqYXL5x/ZZtFTXPG2sHGllWNrMEk2Jimy4aNQYkK9/8/f0l7D+I6 J7yZ6LxsbVLo/yrU8lzjYvAMef1q6PEf3XSv9uSNNWulIJS7FwHAth6kcVGsoexuMiE7 VymQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cirrus.com header.s=PODMain02222019 header.b=PzkquUQP; 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=REJECT sp=REJECT dis=NONE) header.from=cirrus.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q18-20020a170902dad200b001b7fd6f95f6si8376510plx.515.2023.08.14.06.53.27; Mon, 14 Aug 2023 06:54:07 -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=@cirrus.com header.s=PODMain02222019 header.b=PzkquUQP; 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=REJECT sp=REJECT dis=NONE) header.from=cirrus.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230390AbjHNNXx (ORCPT + 99 others); Mon, 14 Aug 2023 09:23:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41866 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230450AbjHNNXf (ORCPT ); Mon, 14 Aug 2023 09:23:35 -0400 Received: from mx0b-001ae601.pphosted.com (mx0a-001ae601.pphosted.com [67.231.149.25]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DA20810E4; Mon, 14 Aug 2023 06:23:33 -0700 (PDT) Received: from pps.filterd (m0077473.ppops.net [127.0.0.1]) by mx0a-001ae601.pphosted.com (8.17.1.22/8.17.1.22) with ESMTP id 37E5t3EI003856; Mon, 14 Aug 2023 08:23:15 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cirrus.com; h= from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding:content-type; s= PODMain02222019; bh=Tu5hYj0cENcnReD71Hf/Ug24TIdVaaEFqB1yCIa77wM=; b= PzkquUQPH3Be0u83loyK39r6rppogO3iGnV+BlckNjHR0L2gaIKJwpMcLtaesb2u XRue+MEB6BFUFRceCI+ME7tkGYz078i73BA6DY5ZYwkPVK6caRt/P0x+L6k8mgt4 5kGaYHr30877M3kqDqDKSd+l1fl1NU62oapXNcDtzBaNwcCDaLMtLP7M+4onbI2A PA1gqOHlL4+ilrFunXdFt9W3BB2cKCU1XmNNKQ+pEpUpYsbeHAvOb/Da29psqv1r j0HeCrepdNcRrpibF5fUDyF035WqbF1uHZhtCZwQlr7aqTtFi02Ze1J2ifzdxdwD qmrpQofOePddkwJb4J04xg== Received: from ediex02.ad.cirrus.com ([84.19.233.68]) by mx0a-001ae601.pphosted.com (PPS) with ESMTPS id 3se8kqt6x8-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 14 Aug 2023 08:23:15 -0500 (CDT) Received: from ediex02.ad.cirrus.com (198.61.84.81) by ediex02.ad.cirrus.com (198.61.84.81) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.30; Mon, 14 Aug 2023 14:23:12 +0100 Received: from ediswmail.ad.cirrus.com (198.61.86.93) by anon-ediex02.ad.cirrus.com (198.61.84.81) with Microsoft SMTP Server id 15.2.1118.30 via Frontend Transport; Mon, 14 Aug 2023 14:23:12 +0100 Received: from EDIN4L06LR3.ad.cirrus.com (EDIN4L06LR3.ad.cirrus.com [198.61.65.68]) by ediswmail.ad.cirrus.com (Postfix) with ESMTP id 5AF99458; Mon, 14 Aug 2023 13:23:12 +0000 (UTC) From: Richard Fitzgerald To: , , CC: , , , , Richard Fitzgerald Subject: [PATCH v4 02/10] kunit: string-stream: Don't create a fragment for empty strings Date: Mon, 14 Aug 2023 14:23:01 +0100 Message-ID: <20230814132309.32641-3-rf@opensource.cirrus.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230814132309.32641-1-rf@opensource.cirrus.com> References: <20230814132309.32641-1-rf@opensource.cirrus.com> MIME-Version: 1.0 X-Proofpoint-ORIG-GUID: D4ds0hHj5j_C3xMvSDa2VQ9hXlvwwMec X-Proofpoint-GUID: D4ds0hHj5j_C3xMvSDa2VQ9hXlvwwMec X-Proofpoint-Spam-Reason: safe X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1774212871921751090 X-GMAIL-MSGID: 1774212871921751090 If the result of the formatted string is an empty string just return instead of creating an empty fragment. Signed-off-by: Richard Fitzgerald Reviewed-by: David Gow --- lib/kunit/string-stream.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/kunit/string-stream.c b/lib/kunit/string-stream.c index cc32743c1171..ed24d86af9f5 100644 --- a/lib/kunit/string-stream.c +++ b/lib/kunit/string-stream.c @@ -50,11 +50,17 @@ int string_stream_vadd(struct string_stream *stream, /* Make a copy because `vsnprintf` could change it */ va_copy(args_for_counting, args); - /* Need space for null byte. */ - len = vsnprintf(NULL, 0, fmt, args_for_counting) + 1; + /* Evaluate length of formatted string */ + len = vsnprintf(NULL, 0, fmt, args_for_counting); va_end(args_for_counting); + if (len == 0) + return 0; + + /* Need space for null byte. */ + len++; + frag_container = alloc_string_stream_fragment(stream->test, len, stream->gfp); From patchwork Mon Aug 14 13:23:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Fitzgerald X-Patchwork-Id: 135455 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b824:0:b0:3f2:4152:657d with SMTP id z4csp2768049vqi; Mon, 14 Aug 2023 07:06:41 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHwg96jfFgZUNIJRJv6FriTXsRR2TH7EC/LQyOGd9FYMy1TAeu6uKLQWxIu4ZgLQTUMa+Oj X-Received: by 2002:a05:6512:39cb:b0:4fb:99d9:6ba2 with SMTP id k11-20020a05651239cb00b004fb99d96ba2mr7693927lfu.24.1692022000821; Mon, 14 Aug 2023 07:06:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692022000; cv=none; d=google.com; s=arc-20160816; b=MWT63Sc+4h/fzw+Za2ym7eEQdi1ohxq96JY09YXCdm7lePQO0wp/QhdoXwA1GVmwIQ sMIBi6QuMSzfY85ylowLkIBUM10CmO88X0D1noWvEeGev93I3iK1JjC8Q9AZmkAP3Ybl RyVpo8PmIUAhS+CH8f6AADNhC10m8JIL+4cIvIagy3k6G7EVss8uz1zl//lEnzxzS2F1 nNcxeJOSwMGNDnWGZPq6Z9FL7J8N0hCz06YXfW0UgtFPdYW0qM8gEKcm8Zs5trqbZsUW sXBIhvzXt+Vr+cOxeOXgaR3+EfDgHcdtWLWe1ZoVOVjkop+0syr9ZrQ/xPcqBeq2GhMa VVNw== 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=Y+sd3a/o1GUUs4yo0lq87GbdUfS162oRTEWPoq4kVAc=; fh=bGF4ESczbIrmrNxFOdEMfzJNgTVkRsqDRt+LRN0CeSg=; b=VGPobrpZA0vXA+WLjU1ZQWKBEsQtwZNrDhhYiCLV/KHzCyoY2HkxLyPvqTYXD02/6v CbDSGXKYJxlrST5gdtJ1oZAYuNeSxk8ww3oHByXpB3wkNoUmp/zNvQvGHlP1wd+IVvWF LE3T9KmGcScTttozr+qF6cDED7s6xTDWOhotSuLbBjbSGs1MmGgxkNva0PpoP4dqicOw jZD+1rgFWhGIhPPHAjh77Q6imLf8MykWlXzCH8JrPTKZYe0m/GcVwb5TAA8S7RIUdRs9 ENUT+G4Xmty+6uUFi1xQznoDvqcU0ZtXSHAUjFgU+EOSSGHeklvhha+H66jQ3+ft4z/W b3uQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cirrus.com header.s=PODMain02222019 header.b=ZVtEI+5e; 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=REJECT sp=REJECT dis=NONE) header.from=cirrus.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i3-20020aa7dd03000000b0052318dbe3b0si3531055edv.345.2023.08.14.07.05.52; Mon, 14 Aug 2023 07:06:40 -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=@cirrus.com header.s=PODMain02222019 header.b=ZVtEI+5e; 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=REJECT sp=REJECT dis=NONE) header.from=cirrus.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230347AbjHNNXv (ORCPT + 99 others); Mon, 14 Aug 2023 09:23:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49714 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230396AbjHNNX2 (ORCPT ); Mon, 14 Aug 2023 09:23:28 -0400 Received: from mx0b-001ae601.pphosted.com (mx0b-001ae601.pphosted.com [67.231.152.168]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3AFE210CE; Mon, 14 Aug 2023 06:23:27 -0700 (PDT) Received: from pps.filterd (m0077474.ppops.net [127.0.0.1]) by mx0b-001ae601.pphosted.com (8.17.1.22/8.17.1.22) with ESMTP id 37EBX0QX010808; Mon, 14 Aug 2023 08:23:14 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cirrus.com; h= from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding:content-type; s= PODMain02222019; bh=Y+sd3a/o1GUUs4yo0lq87GbdUfS162oRTEWPoq4kVAc=; b= ZVtEI+5eCgVZlzSbl5qarpI5rhxxPbLJ0trhaZ2kDUK1ZYBCOu9EHStPMPbNmAaj ETReIuRpaqUkNYWVzkTTHkD3AETAmGNncYmPqj6NifJij+9Bzyb/YavL/1cANvRb yZHCiUs5iNvVKBjDMLFUO+N8yqLFbp6phjZYlSMmvFh3S84nKrrSLKRJdPJyg0k6 3WNztbafRj/9EDDdPIjJMX82T3l5plinL0nShGX6Pjh17rtasnmLDoyDgjEmTL67 Hp2ZHYRdoSCz5GCv8vYIhSzLCTEYxpKgv9L3t/GKQiZfisG4Bvq6XP36Hi1cUD3M LoGhQoKIj2RWzo/wsqDxSQ== Received: from ediex01.ad.cirrus.com ([84.19.233.68]) by mx0b-001ae601.pphosted.com (PPS) with ESMTPS id 3se6uhhvdp-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 14 Aug 2023 08:23:13 -0500 (CDT) Received: from ediex02.ad.cirrus.com (198.61.84.81) by ediex01.ad.cirrus.com (198.61.84.80) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.30; Mon, 14 Aug 2023 14:23:13 +0100 Received: from ediswmail.ad.cirrus.com (198.61.86.93) by anon-ediex02.ad.cirrus.com (198.61.84.81) with Microsoft SMTP Server id 15.2.1118.30 via Frontend Transport; Mon, 14 Aug 2023 14:23:13 +0100 Received: from EDIN4L06LR3.ad.cirrus.com (EDIN4L06LR3.ad.cirrus.com [198.61.65.68]) by ediswmail.ad.cirrus.com (Postfix) with ESMTP id DE07C15B4; Mon, 14 Aug 2023 13:23:12 +0000 (UTC) From: Richard Fitzgerald To: , , CC: , , , , Richard Fitzgerald Subject: [PATCH v4 03/10] kunit: string-stream: Add cases for adding empty strings to a string_stream Date: Mon, 14 Aug 2023 14:23:02 +0100 Message-ID: <20230814132309.32641-4-rf@opensource.cirrus.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230814132309.32641-1-rf@opensource.cirrus.com> References: <20230814132309.32641-1-rf@opensource.cirrus.com> MIME-Version: 1.0 X-Proofpoint-GUID: Cfk9gbhP_noQrxClgFSo0sxdLnZ6YV0t X-Proofpoint-ORIG-GUID: Cfk9gbhP_noQrxClgFSo0sxdLnZ6YV0t X-Proofpoint-Spam-Reason: safe X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1774213661820909278 X-GMAIL-MSGID: 1774213661820909278 Adds string_stream_append_empty_string_test() to test that adding an empty string to a string_stream doesn't create a new empty fragment. Signed-off-by: Richard Fitzgerald --- lib/kunit/string-stream-test.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/lib/kunit/string-stream-test.c b/lib/kunit/string-stream-test.c index 1d46d5f06d2a..efe13e3322b5 100644 --- a/lib/kunit/string-stream-test.c +++ b/lib/kunit/string-stream-test.c @@ -206,11 +206,32 @@ static void string_stream_append_test(struct kunit *test) KUNIT_EXPECT_STREQ(test, string_stream_get_string(stream_1), stream_2_content); } +/* Adding an empty string should not create a fragment. */ +static void string_stream_append_empty_string_test(struct kunit *test) +{ + struct string_stream *stream; + + stream = alloc_string_stream(test, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); + + /* Formatted empty string */ + string_stream_add(stream, "%s", ""); + KUNIT_EXPECT_TRUE(test, string_stream_is_empty(stream)); + KUNIT_EXPECT_TRUE(test, list_empty(&stream->fragments)); + + /* Adding an empty string to a non-empty stream */ + string_stream_add(stream, "Add this line"); + string_stream_add(stream, "%s", ""); + KUNIT_EXPECT_EQ(test, list_count_nodes(&stream->fragments), 1); + KUNIT_EXPECT_STREQ(test, string_stream_get_string(stream), "Add this line"); +} + static struct kunit_case string_stream_test_cases[] = { KUNIT_CASE(string_stream_init_test), KUNIT_CASE(string_stream_line_add_test), KUNIT_CASE(string_stream_variable_length_line_test), KUNIT_CASE(string_stream_append_test), + KUNIT_CASE(string_stream_append_empty_string_test), {} }; From patchwork Mon Aug 14 13:23:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Fitzgerald X-Patchwork-Id: 135551 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b824:0:b0:3f2:4152:657d with SMTP id z4csp2874348vqi; Mon, 14 Aug 2023 09:57:06 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHOKDsmiU6l3SQ9YlWh9t14UJBVNDqF/nJHtKYT/dcnJoMbEiYiIXtDpxiNHz4DRJudPuQR X-Received: by 2002:ac2:51a4:0:b0:4f6:1779:b1c1 with SMTP id f4-20020ac251a4000000b004f61779b1c1mr5864800lfk.48.1692032225805; Mon, 14 Aug 2023 09:57:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692032225; cv=none; d=google.com; s=arc-20160816; b=dRQzqoH0mS4bJKgR8jhY0LHdtXf9k/7SNzdb5Mwo789dOST5SFev3x6IzxT8Q3PubQ zg6DRati9GRftXlKWgrHvS35BOeB4bcDhd72dTHP3Sk6xCdAVUlTZfPYuGB9Utfzkgl6 gy6aWUJ8RPkETNWifc6a3KhVLI8DJysKbs0nWcAWSK55gvGiFkPYheWVEsDjiXZbiRRu lh1sZJ913uPSN95FCQ6o7C398voejQtYQLWhbqGaFBlc6P86xEtLsMqZFe6kczy1UoOc vU5ghpJlcD+t8gf4Umcwj3ytgzVXrDnBJemBryFE6qA72VGQiMRU3YA/wTKrSjwL92Iq iyEQ== 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=efYVUT2HRXdKFPb+RuDy82t8juU/ZyaPoA+cVZJirQY=; fh=bGF4ESczbIrmrNxFOdEMfzJNgTVkRsqDRt+LRN0CeSg=; b=w6kW2guxI1vPVv/XXXSPcCUT0eW97i1NvD03MZdlA0PiJaJoq4629S+k31lC/AlIxm Or8lbf5QwpB8FdzDd0KVDpTDKoHMnAXuQ6ltZiRvrbyAHczPCQFnhyR/gYXcyRNulUq9 Q+ky8uFNzDME1w7Yp1EWZPQvhelo8cg3WPngyI1ttQJ0Egei53G8iQhFr4IxtIHsVKZC ERA6EeiCCIHD314Adypx4rpx9cfq5s7mrUhOrdc177q6g0e1vCDMD0/oJpVLwtzaV6Hy Pvsr8N02ImeLq6FFgidHUQQlgNUgRi9icV08XwCiFObB/YP+TtY+ITHAxTtZwMdzJuIn AWVA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cirrus.com header.s=PODMain02222019 header.b=iyjmkYjK; 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=REJECT sp=REJECT dis=NONE) header.from=cirrus.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v15-20020aa7cd4f000000b00522de11e513si7888287edw.500.2023.08.14.09.56.42; Mon, 14 Aug 2023 09:57:05 -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=@cirrus.com header.s=PODMain02222019 header.b=iyjmkYjK; 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=REJECT sp=REJECT dis=NONE) header.from=cirrus.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230446AbjHNNXz (ORCPT + 99 others); Mon, 14 Aug 2023 09:23:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41914 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230462AbjHNNXg (ORCPT ); Mon, 14 Aug 2023 09:23:36 -0400 Received: from mx0b-001ae601.pphosted.com (mx0a-001ae601.pphosted.com [67.231.149.25]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2194010F0; Mon, 14 Aug 2023 06:23:34 -0700 (PDT) Received: from pps.filterd (m0077473.ppops.net [127.0.0.1]) by mx0a-001ae601.pphosted.com (8.17.1.22/8.17.1.22) with ESMTP id 37E5t3EJ003856; Mon, 14 Aug 2023 08:23:16 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cirrus.com; h= from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding:content-type; s= PODMain02222019; bh=efYVUT2HRXdKFPb+RuDy82t8juU/ZyaPoA+cVZJirQY=; b= iyjmkYjKtZubnjuVvtXPsaH7TGKV4twFOld+BcwZphiFtPEZDNOznUuShbMhtmRu NJKSh4zhqX+R3fX3McVLs0uxCXgu++bTEQpUIxOSYjdCLdxrn+wbXUZOGGql+C/W yDcNAK9+IsAuH5BChtzA+eB/U0TJm5R7f0K3UFy2KOZWGz+dUWHurMzEBx6RyRiS l+utZpbi6BnVkEp6crdMdskUqPpqFf96wuKsHn9AJdH/sJ/RQQ+1UOoK+YIbmsBL OGBH3dx4cR5gUCC+yyy5l5EUL4NhVPM7a+WtHNrTIRsdqDVj9OZ9Ng7RvmrdU0Jk 7uK8mSlQE6eSvpXovCSdSg== Received: from ediex02.ad.cirrus.com ([84.19.233.68]) by mx0a-001ae601.pphosted.com (PPS) with ESMTPS id 3se8kqt6x8-3 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 14 Aug 2023 08:23:16 -0500 (CDT) Received: from ediex02.ad.cirrus.com (198.61.84.81) by ediex02.ad.cirrus.com (198.61.84.81) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.30; Mon, 14 Aug 2023 14:23:13 +0100 Received: from ediswmail.ad.cirrus.com (198.61.86.93) by anon-ediex02.ad.cirrus.com (198.61.84.81) with Microsoft SMTP Server id 15.2.1118.30 via Frontend Transport; Mon, 14 Aug 2023 14:23:13 +0100 Received: from EDIN4L06LR3.ad.cirrus.com (EDIN4L06LR3.ad.cirrus.com [198.61.65.68]) by ediswmail.ad.cirrus.com (Postfix) with ESMTP id 63F7F3578; Mon, 14 Aug 2023 13:23:13 +0000 (UTC) From: Richard Fitzgerald To: , , CC: , , , , Richard Fitzgerald Subject: [PATCH v4 04/10] kunit: string-stream: Add option to make all lines end with newline Date: Mon, 14 Aug 2023 14:23:03 +0100 Message-ID: <20230814132309.32641-5-rf@opensource.cirrus.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230814132309.32641-1-rf@opensource.cirrus.com> References: <20230814132309.32641-1-rf@opensource.cirrus.com> MIME-Version: 1.0 X-Proofpoint-ORIG-GUID: 3yrs5C4y32Wiig1n8H3bAvNM9M6zSIS5 X-Proofpoint-GUID: 3yrs5C4y32Wiig1n8H3bAvNM9M6zSIS5 X-Proofpoint-Spam-Reason: safe X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1774224383040902468 X-GMAIL-MSGID: 1774224383040902468 Add an optional feature to string_stream that will append a newline to any added string that does not already end with a newline. The purpose of this is so that string_stream can be used to collect log lines. This is enabled/disabled by calling string_stream_set_append_newlines(). Signed-off-by: Richard Fitzgerald --- lib/kunit/string-stream.c | 28 +++++++++++++++++++++------- lib/kunit/string-stream.h | 7 +++++++ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/lib/kunit/string-stream.c b/lib/kunit/string-stream.c index ed24d86af9f5..1dcf6513b692 100644 --- a/lib/kunit/string-stream.c +++ b/lib/kunit/string-stream.c @@ -44,32 +44,46 @@ int string_stream_vadd(struct string_stream *stream, va_list args) { struct string_stream_fragment *frag_container; - int len; + int buf_len, result_len; va_list args_for_counting; /* Make a copy because `vsnprintf` could change it */ va_copy(args_for_counting, args); /* Evaluate length of formatted string */ - len = vsnprintf(NULL, 0, fmt, args_for_counting); + buf_len = vsnprintf(NULL, 0, fmt, args_for_counting); va_end(args_for_counting); - if (len == 0) + if (buf_len == 0) return 0; + /* Reserve one extra for possible appended newline. */ + if (stream->append_newlines) + buf_len++; + /* Need space for null byte. */ - len++; + buf_len++; frag_container = alloc_string_stream_fragment(stream->test, - len, + buf_len, stream->gfp); if (IS_ERR(frag_container)) return PTR_ERR(frag_container); - len = vsnprintf(frag_container->fragment, len, fmt, args); + if (stream->append_newlines) { + /* Don't include reserved newline byte in writeable length. */ + result_len = vsnprintf(frag_container->fragment, buf_len - 1, fmt, args); + + /* Append newline if necessary. */ + if (frag_container->fragment[result_len - 1] != '\n') + result_len = strlcat(frag_container->fragment, "\n", buf_len); + } else { + result_len = vsnprintf(frag_container->fragment, buf_len, fmt, args); + } + spin_lock(&stream->lock); - stream->length += len; + stream->length += result_len; list_add_tail(&frag_container->node, &stream->fragments); spin_unlock(&stream->lock); diff --git a/lib/kunit/string-stream.h b/lib/kunit/string-stream.h index b669f9a75a94..048930bf97f0 100644 --- a/lib/kunit/string-stream.h +++ b/lib/kunit/string-stream.h @@ -25,6 +25,7 @@ struct string_stream { spinlock_t lock; struct kunit *test; gfp_t gfp; + bool append_newlines; }; struct kunit; @@ -47,4 +48,10 @@ bool string_stream_is_empty(struct string_stream *stream); void string_stream_destroy(struct string_stream *stream); +static inline void string_stream_set_append_newlines(struct string_stream *stream, + bool append_newlines) +{ + stream->append_newlines = append_newlines; +} + #endif /* _KUNIT_STRING_STREAM_H */ From patchwork Mon Aug 14 13:23:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Fitzgerald X-Patchwork-Id: 135456 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b824:0:b0:3f2:4152:657d with SMTP id z4csp2769389vqi; Mon, 14 Aug 2023 07:08:25 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFJWVTQ4mXUMHZ2Sh7zVfPiNysF/ZFVIJeIkvmiTpM0x225MktbSAY6eWLfGZ/sNH5liq5j X-Received: by 2002:a17:906:8a4a:b0:997:c5c3:32cc with SMTP id gx10-20020a1709068a4a00b00997c5c332ccmr8316136ejc.66.1692022105115; Mon, 14 Aug 2023 07:08:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692022105; cv=none; d=google.com; s=arc-20160816; b=EPcMPX57DvEjuVPKjaCSs298jBGfvuTPrrHAkg2AL+GBVhfeyIWSp0ASpZ7PuMsH4v fcdQrF83kLSfmtVka5SA3MeB97JZUPKfbTULgGhB+z5UAjDYY7n1sUJ2PUWKHPFBZQqT zQj93m8ifs0Nsb6VszwXlMbCyG3rc/lxSWhMDxkRx6j4jV8Hxq0S51xPgtLCthaXv6KK zsvrFuV/XxLZ94j+WDbwRoK69j/yb49sZYiu0GMw/+CawvKryVeDRZ2vOZR/BsC+bg8y c9M7t5HQo0zU/inhRBI5t0JPargZQmDJ0w/IeEZLn9HpJjINExh5NadaKFCmVi9kgiJ7 Mrhw== 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=F1ndINnqWJNhTR4PfySXa0GVhkrYpQz+Jc3VDxBD5+o=; fh=bGF4ESczbIrmrNxFOdEMfzJNgTVkRsqDRt+LRN0CeSg=; b=0dMmL+OaM6PxcY48y/mo6ckZJ5PuGoCmvNceudjh0yHkDaxbPTI1PptkL4Z++vq5ir ialFTxX24nDVYYKXosRiB0FAMcUHmVb6etT4+GUtFNM4SKW61SqiQtwyh++SIUx4yfl7 Hwh5jfhbOe2MSsWMv38wQRQGmxJI8s8PUBLQiqKSpQzog9X8q36CSMtmzOxCMaLTiinl DkGvKPOTQeg5wwktp1LAY007iv0fotNEOWWpZ1C+dhEv1S0KEmMBrMW5YxrbaTLbxuX7 b5XtATUOGQW+rsfQUH+snQAyExXLbjj7y4FYEkMyKZR6RFL72c+AGQENLTa4X/5YNfsw LHnw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cirrus.com header.s=PODMain02222019 header.b=BqWkpUEN; 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=REJECT sp=REJECT dis=NONE) header.from=cirrus.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id va30-20020a17090711de00b0098871664f55si7870311ejb.800.2023.08.14.07.08.00; Mon, 14 Aug 2023 07:08:25 -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=@cirrus.com header.s=PODMain02222019 header.b=BqWkpUEN; 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=REJECT sp=REJECT dis=NONE) header.from=cirrus.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231259AbjHNNYF (ORCPT + 99 others); Mon, 14 Aug 2023 09:24:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41970 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230479AbjHNNXi (ORCPT ); Mon, 14 Aug 2023 09:23:38 -0400 Received: from mx0b-001ae601.pphosted.com (mx0a-001ae601.pphosted.com [67.231.149.25]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B5D84E7D; Mon, 14 Aug 2023 06:23:37 -0700 (PDT) Received: from pps.filterd (m0077473.ppops.net [127.0.0.1]) by mx0a-001ae601.pphosted.com (8.17.1.22/8.17.1.22) with ESMTP id 37E5t3EK003856; Mon, 14 Aug 2023 08:23:17 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cirrus.com; h= from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding:content-type; s= PODMain02222019; bh=F1ndINnqWJNhTR4PfySXa0GVhkrYpQz+Jc3VDxBD5+o=; b= BqWkpUENNp3km6myrhPYoRR4IheJ5nk2iv2Ca3a8nN4NBXCNIMB0g2hqGWiIHiJC enqNnKvjMMsfdP5oaOo0GyZbA8bnvlqVgPr5B8T4f9ZhZWs8ZIqrnM67tzBWAYzb 0s708uoKAoDJZeP1/wgH2V/hWvJHjC15nj32A5pPAVL/n6gYFwunxafZmQXR0UZf ztIxW5Ol9Kg/f+qC03wrilVspM46CsufOL5aNU+lIsx+dN2/8OKS5hl+ESzwaVwy MlFK95Kp4POYwUn7YnKYSscZYeb0tmf02gT/svfadyY8X7U+DzBDZvwnzxDWCY2S 4XcHgL5VUUv56qOF8cFjfQ== Received: from ediex02.ad.cirrus.com ([84.19.233.68]) by mx0a-001ae601.pphosted.com (PPS) with ESMTPS id 3se8kqt6x8-4 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 14 Aug 2023 08:23:17 -0500 (CDT) Received: from ediex02.ad.cirrus.com (198.61.84.81) by ediex02.ad.cirrus.com (198.61.84.81) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.30; Mon, 14 Aug 2023 14:23:14 +0100 Received: from ediswmail.ad.cirrus.com (198.61.86.93) by anon-ediex02.ad.cirrus.com (198.61.84.81) with Microsoft SMTP Server id 15.2.1118.30 via Frontend Transport; Mon, 14 Aug 2023 14:23:14 +0100 Received: from EDIN4L06LR3.ad.cirrus.com (EDIN4L06LR3.ad.cirrus.com [198.61.65.68]) by ediswmail.ad.cirrus.com (Postfix) with ESMTP id CBAE015B4; Mon, 14 Aug 2023 13:23:13 +0000 (UTC) From: Richard Fitzgerald To: , , CC: , , , , Richard Fitzgerald Subject: [PATCH v4 05/10] kunit: string-stream: Add cases for string_stream newline appending Date: Mon, 14 Aug 2023 14:23:04 +0100 Message-ID: <20230814132309.32641-6-rf@opensource.cirrus.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230814132309.32641-1-rf@opensource.cirrus.com> References: <20230814132309.32641-1-rf@opensource.cirrus.com> MIME-Version: 1.0 X-Proofpoint-ORIG-GUID: ovUZCUgveNTCqVnlqSXBVGMQLRW0mkFD X-Proofpoint-GUID: ovUZCUgveNTCqVnlqSXBVGMQLRW0mkFD X-Proofpoint-Spam-Reason: safe X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1774213771351790669 X-GMAIL-MSGID: 1774213771351790669 Add test cases for testing the string_stream feature that appends a newline to strings that do not already end with a newline. string_stream_no_auto_newline_test() tests with this feature disabled. Newlines should not be added or dropped. string_stream_auto_newline_test() tests with this feature enabled. Newlines should be added to lines that do not end with a newline. Signed-off-by: Richard Fitzgerald --- lib/kunit/string-stream-test.c | 51 ++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/lib/kunit/string-stream-test.c b/lib/kunit/string-stream-test.c index efe13e3322b5..46c2ac162fe8 100644 --- a/lib/kunit/string-stream-test.c +++ b/lib/kunit/string-stream-test.c @@ -23,6 +23,7 @@ static void string_stream_init_test(struct kunit *test) KUNIT_EXPECT_TRUE(test, list_empty(&stream->fragments)); KUNIT_EXPECT_PTR_EQ(test, stream->test, test); KUNIT_EXPECT_EQ(test, stream->gfp, GFP_KERNEL); + KUNIT_EXPECT_FALSE(test, stream->append_newlines); KUNIT_EXPECT_TRUE(test, string_stream_is_empty(stream)); } @@ -226,12 +227,62 @@ static void string_stream_append_empty_string_test(struct kunit *test) KUNIT_EXPECT_STREQ(test, string_stream_get_string(stream), "Add this line"); } +/* Adding strings without automatic newline appending */ +static void string_stream_no_auto_newline_test(struct kunit *test) +{ + struct string_stream *stream; + + stream = alloc_string_stream(test, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); + + /* + * Add some strings with and without newlines. All formatted + * newlines should be preserved. No extra newlines should be + * added. + */ + string_stream_add(stream, "One"); + string_stream_add(stream, "Two\n"); + string_stream_add(stream, "%s\n", "Three"); + string_stream_add(stream, "Four"); + KUNIT_EXPECT_STREQ(test, string_stream_get_string(stream), + "OneTwo\nThree\nFour"); +} + +/* Adding strings with automatic newline appending */ +static void string_stream_auto_newline_test(struct kunit *test) +{ + struct string_stream *stream; + + stream = alloc_string_stream(test, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); + + string_stream_set_append_newlines(stream, true); + KUNIT_EXPECT_TRUE(test, stream->append_newlines); + + /* + * Add some strings with and without newlines. Newlines should + * be appended to lines that do not end with \n, but newlines + * resulting from the formatting should not be changed. + */ + string_stream_add(stream, "One"); + string_stream_add(stream, "Two\n"); + string_stream_add(stream, "%s\n", "Three"); + string_stream_add(stream, "%s", "Four\n"); + string_stream_add(stream, "Five\n%s", "Six"); + string_stream_add(stream, "Seven\n\n"); + string_stream_add(stream, "Eight"); + KUNIT_EXPECT_STREQ(test, string_stream_get_string(stream), + "One\nTwo\nThree\nFour\nFive\nSix\nSeven\n\nEight\n"); +} + static struct kunit_case string_stream_test_cases[] = { KUNIT_CASE(string_stream_init_test), KUNIT_CASE(string_stream_line_add_test), KUNIT_CASE(string_stream_variable_length_line_test), KUNIT_CASE(string_stream_append_test), KUNIT_CASE(string_stream_append_empty_string_test), + KUNIT_CASE(string_stream_no_auto_newline_test), + KUNIT_CASE(string_stream_auto_newline_test), {} }; From patchwork Mon Aug 14 13:23:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Fitzgerald X-Patchwork-Id: 135502 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b824:0:b0:3f2:4152:657d with SMTP id z4csp2820890vqi; Mon, 14 Aug 2023 08:30:34 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFrrB690khZ+7pgZI7MYYHdweR/rbhS1TlAuzvfHMbKdNzCGNmArfEY/h9pBivwJ0DnlGqR X-Received: by 2002:aa7:c049:0:b0:523:3e47:2657 with SMTP id k9-20020aa7c049000000b005233e472657mr7427680edo.11.1692027033575; Mon, 14 Aug 2023 08:30:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692027033; cv=none; d=google.com; s=arc-20160816; b=TSmCNbQsUVhw3/wguVJzb9LYwOR0wIc05olBXv5p3Rcg8F8aJU7YGappHvKs7ydocW qzLNZUAfSIVFp/621X4dlnR3R2Wd3vPY2LMz+9/GFQrDscZmqjktmaQyfpnllzdig4y1 pMUO1Uoy50tVb2CiTRlO5xwOwAx2q9scvYP2zgX6ULkkMN7s0i5fWNp857CpzWHxpgyA tmF+GAEdMCLmEK/mBfqndQl67klrcDVM3RE40Oa079mfi4PVh/HOj011UvzgtV/rixtL EApft67EiZHLipl+QCXNFs5EI8GP7FkC16ImBgJayM5NG5sFWepolB1P3pVJWAcXG4Jq oaGA== 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=frY2tHP5jPWkeecF6fp4ApId5IiopIw1YuzZvmkax+w=; fh=bGF4ESczbIrmrNxFOdEMfzJNgTVkRsqDRt+LRN0CeSg=; b=kgbejCcwTJzewjml7FZarc/p6aNZ0ItzT6FS60r+vRpZdFGawlCLNTu2tU/8joDCGa 4eF/KSNAUEejHkyGho3vFQW1GZvz1NXJ+SV1YwyvJLPV+mbBEKyE16hUc9LP+q7Tohbu rU9Mkcs+jgW36kZSBekfxrPguNRuJPFS1CC3vsV8DrPpyN4HrcuXmvTo6PKud4fmgXwX AroXFj2hBbcskS7105KiOwtFheDu3BCiC/fG3yw5D8J+7HnWIup+O5SravL7H6vgsn1F lwqkON/ioC9ttfCm6499ES0Yf96brcdX5im6S9HRh0EcKoT6sbhHmLKkz5bhLNVx7yGK Xtnw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cirrus.com header.s=PODMain02222019 header.b=h5MUDuab; 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=REJECT sp=REJECT dis=NONE) header.from=cirrus.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f2-20020a05640214c200b005233ef72dbfsi4090601edx.27.2023.08.14.08.30.07; Mon, 14 Aug 2023 08:30:33 -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=@cirrus.com header.s=PODMain02222019 header.b=h5MUDuab; 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=REJECT sp=REJECT dis=NONE) header.from=cirrus.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230423AbjHNNXy (ORCPT + 99 others); Mon, 14 Aug 2023 09:23:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41906 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230465AbjHNNXg (ORCPT ); Mon, 14 Aug 2023 09:23:36 -0400 Received: from mx0b-001ae601.pphosted.com (mx0a-001ae601.pphosted.com [67.231.149.25]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1324F10C8; Mon, 14 Aug 2023 06:23:34 -0700 (PDT) Received: from pps.filterd (m0077473.ppops.net [127.0.0.1]) by mx0a-001ae601.pphosted.com (8.17.1.22/8.17.1.22) with ESMTP id 37E6GnC9000669; Mon, 14 Aug 2023 08:23:17 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cirrus.com; h= from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding:content-type; s= PODMain02222019; bh=frY2tHP5jPWkeecF6fp4ApId5IiopIw1YuzZvmkax+w=; b= h5MUDuabgjazR/dTBJRqxWN1fGJM7vCCs4AtFhqNBKembGI2AGkGFI1IHvTI+Shi nvhye5KRwoUPmWnujvRnwcK9pXFGQxqoeFTiZChch9VcQx3kpNxTLQke6TQq2Oxk rydufnoLMx/QNslCpmInlvEpY8psrl+8PjbVD2ixAX6XYtYy7ur9nk2q/guX59Ss KlSZK+7HgHsTbjkU/y7Mt2zqtH8d+Eh4ySI0+vjcweSN16TCUoQ71yEPMgk+D9P5 Oi2aRi7mVsMPUtf/OsTfTshxBmgOnE7dkkX1E9cic2eN8rk2Zl+UuOWKadH/cgvH yRsgum83G9tme2TGjEaLcw== Received: from ediex01.ad.cirrus.com ([84.19.233.68]) by mx0a-001ae601.pphosted.com (PPS) with ESMTPS id 3se8kqt6xb-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 14 Aug 2023 08:23:16 -0500 (CDT) Received: from ediex02.ad.cirrus.com (198.61.84.81) by ediex01.ad.cirrus.com (198.61.84.80) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.30; Mon, 14 Aug 2023 14:23:14 +0100 Received: from ediswmail.ad.cirrus.com (198.61.86.93) by anon-ediex02.ad.cirrus.com (198.61.84.81) with Microsoft SMTP Server id 15.2.1118.30 via Frontend Transport; Mon, 14 Aug 2023 14:23:14 +0100 Received: from EDIN4L06LR3.ad.cirrus.com (EDIN4L06LR3.ad.cirrus.com [198.61.65.68]) by ediswmail.ad.cirrus.com (Postfix) with ESMTP id 3E2B9458; Mon, 14 Aug 2023 13:23:14 +0000 (UTC) From: Richard Fitzgerald To: , , CC: , , , , Richard Fitzgerald Subject: [PATCH v4 06/10] kunit: string-stream: Pass struct kunit to string_stream_get_string() Date: Mon, 14 Aug 2023 14:23:05 +0100 Message-ID: <20230814132309.32641-7-rf@opensource.cirrus.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230814132309.32641-1-rf@opensource.cirrus.com> References: <20230814132309.32641-1-rf@opensource.cirrus.com> MIME-Version: 1.0 X-Proofpoint-ORIG-GUID: RR3FX80xx5g7cOMUOdquvm7NZfTJYnA4 X-Proofpoint-GUID: RR3FX80xx5g7cOMUOdquvm7NZfTJYnA4 X-Proofpoint-Spam-Reason: safe X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1774218938412166734 X-GMAIL-MSGID: 1774218938412166734 Pass a struct kunit* and gfp_t to string_stream_get_string(). Allocate the returned buffer using these instead of using the stream->test and stream->gfp. This is preparation for removing the dependence of string_stream on struct kunit, so that string_stream can be used for the debugfs log. Signed-off-by: Richard Fitzgerald --- lib/kunit/string-stream-test.c | 26 +++++++++++++++----------- lib/kunit/string-stream.c | 8 ++++---- lib/kunit/string-stream.h | 3 ++- lib/kunit/test.c | 2 +- 4 files changed, 22 insertions(+), 17 deletions(-) diff --git a/lib/kunit/string-stream-test.c b/lib/kunit/string-stream-test.c index 46c2ac162fe8..8a30bb7d5fb7 100644 --- a/lib/kunit/string-stream-test.c +++ b/lib/kunit/string-stream-test.c @@ -57,7 +57,7 @@ static void string_stream_line_add_test(struct kunit *test) } num_lines = i; - concat_string = string_stream_get_string(stream); + concat_string = string_stream_get_string(test, stream, GFP_KERNEL); KUNIT_EXPECT_NOT_ERR_OR_NULL(test, concat_string); KUNIT_EXPECT_EQ(test, strlen(concat_string), total_len); @@ -113,7 +113,7 @@ static void string_stream_variable_length_line_test(struct kunit *test) } num_lines = i; - concat_string = string_stream_get_string(stream); + concat_string = string_stream_get_string(test, stream, GFP_KERNEL); KUNIT_EXPECT_NOT_ERR_OR_NULL(test, concat_string); KUNIT_EXPECT_EQ(test, strlen(concat_string), total_len); @@ -165,17 +165,18 @@ static void string_stream_append_test(struct kunit *test) /* Append content of empty stream to empty stream */ string_stream_append(stream_1, stream_2); - KUNIT_EXPECT_EQ(test, strlen(string_stream_get_string(stream_1)), 0); + KUNIT_EXPECT_EQ(test, strlen(string_stream_get_string(test, stream_1, GFP_KERNEL)), 0); /* Add some data to stream_1 */ for (i = 0; i < ARRAY_SIZE(strings_1); ++i) string_stream_add(stream_1, "%s\n", strings_1[i]); - original_content = string_stream_get_string(stream_1); + original_content = string_stream_get_string(test, stream_1, GFP_KERNEL); /* Append content of empty stream to non-empty stream */ string_stream_append(stream_1, stream_2); - KUNIT_EXPECT_STREQ(test, string_stream_get_string(stream_1), original_content); + KUNIT_EXPECT_STREQ(test, string_stream_get_string(test, stream_1, GFP_KERNEL), + original_content); /* Add some data to stream_2 */ for (i = 0; i < ARRAY_SIZE(strings_2); ++i) @@ -188,14 +189,15 @@ static void string_stream_append_test(struct kunit *test) * End result should be the original content of stream_1 plus * the content of stream_2. */ - stream_2_content = string_stream_get_string(stream_2); + stream_2_content = string_stream_get_string(test, stream_2, GFP_KERNEL); combined_length = strlen(original_content) + strlen(stream_2_content); combined_length++; /* for terminating \0 */ combined_content = kunit_kmalloc(test, combined_length, GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, combined_content); snprintf(combined_content, combined_length, "%s%s", original_content, stream_2_content); - KUNIT_EXPECT_STREQ(test, string_stream_get_string(stream_1), combined_content); + KUNIT_EXPECT_STREQ(test, string_stream_get_string(test, stream_1, GFP_KERNEL), + combined_content); /* Append content of non-empty stream to empty stream */ string_stream_destroy(stream_1); @@ -204,7 +206,8 @@ static void string_stream_append_test(struct kunit *test) KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream_1); string_stream_append(stream_1, stream_2); - KUNIT_EXPECT_STREQ(test, string_stream_get_string(stream_1), stream_2_content); + KUNIT_EXPECT_STREQ(test, string_stream_get_string(test, stream_1, GFP_KERNEL), + stream_2_content); } /* Adding an empty string should not create a fragment. */ @@ -224,7 +227,8 @@ static void string_stream_append_empty_string_test(struct kunit *test) string_stream_add(stream, "Add this line"); string_stream_add(stream, "%s", ""); KUNIT_EXPECT_EQ(test, list_count_nodes(&stream->fragments), 1); - KUNIT_EXPECT_STREQ(test, string_stream_get_string(stream), "Add this line"); + KUNIT_EXPECT_STREQ(test, string_stream_get_string(test, stream, GFP_KERNEL), + "Add this line"); } /* Adding strings without automatic newline appending */ @@ -244,7 +248,7 @@ static void string_stream_no_auto_newline_test(struct kunit *test) string_stream_add(stream, "Two\n"); string_stream_add(stream, "%s\n", "Three"); string_stream_add(stream, "Four"); - KUNIT_EXPECT_STREQ(test, string_stream_get_string(stream), + KUNIT_EXPECT_STREQ(test, string_stream_get_string(test, stream, GFP_KERNEL), "OneTwo\nThree\nFour"); } @@ -271,7 +275,7 @@ static void string_stream_auto_newline_test(struct kunit *test) string_stream_add(stream, "Five\n%s", "Six"); string_stream_add(stream, "Seven\n\n"); string_stream_add(stream, "Eight"); - KUNIT_EXPECT_STREQ(test, string_stream_get_string(stream), + KUNIT_EXPECT_STREQ(test, string_stream_get_string(test, stream, GFP_KERNEL), "One\nTwo\nThree\nFour\nFive\nSix\nSeven\n\nEight\n"); } diff --git a/lib/kunit/string-stream.c b/lib/kunit/string-stream.c index 1dcf6513b692..eb673d3ea3bd 100644 --- a/lib/kunit/string-stream.c +++ b/lib/kunit/string-stream.c @@ -117,13 +117,14 @@ static void string_stream_clear(struct string_stream *stream) spin_unlock(&stream->lock); } -char *string_stream_get_string(struct string_stream *stream) +char *string_stream_get_string(struct kunit *test, struct string_stream *stream, + gfp_t gfp) { struct string_stream_fragment *frag_container; size_t buf_len = stream->length + 1; /* +1 for null byte. */ char *buf; - buf = kunit_kzalloc(stream->test, buf_len, stream->gfp); + buf = kunit_kzalloc(test, buf_len, gfp); if (!buf) return NULL; @@ -140,8 +141,7 @@ int string_stream_append(struct string_stream *stream, { const char *other_content; - other_content = string_stream_get_string(other); - + other_content = string_stream_get_string(other->test, other, other->gfp); if (!other_content) return -ENOMEM; diff --git a/lib/kunit/string-stream.h b/lib/kunit/string-stream.h index 048930bf97f0..6b4a747881c5 100644 --- a/lib/kunit/string-stream.h +++ b/lib/kunit/string-stream.h @@ -39,7 +39,8 @@ int __printf(2, 0) string_stream_vadd(struct string_stream *stream, const char *fmt, va_list args); -char *string_stream_get_string(struct string_stream *stream); +char *string_stream_get_string(struct kunit *test, struct string_stream *stream, + gfp_t gfp); int string_stream_append(struct string_stream *stream, struct string_stream *other); diff --git a/lib/kunit/test.c b/lib/kunit/test.c index 49698a168437..520e15f49d0d 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -286,7 +286,7 @@ static void kunit_print_string_stream(struct kunit *test, if (string_stream_is_empty(stream)) return; - buf = string_stream_get_string(stream); + buf = string_stream_get_string(test, stream, GFP_KERNEL); if (!buf) { kunit_err(test, "Could not allocate buffer, dumping stream:\n"); From patchwork Mon Aug 14 13:23:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Fitzgerald X-Patchwork-Id: 135488 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b824:0:b0:3f2:4152:657d with SMTP id z4csp2796033vqi; Mon, 14 Aug 2023 07:52:49 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG+BMcwAPCsAsVD9JA+jVyZJSAbcLMgOeFmohbUdG9hvwVdvDKzrrxaz31JzFXj+mEg1LAE X-Received: by 2002:a17:906:5a57:b0:99a:e756:57bf with SMTP id my23-20020a1709065a5700b0099ae75657bfmr8281371ejc.7.1692024769307; Mon, 14 Aug 2023 07:52:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692024769; cv=none; d=google.com; s=arc-20160816; b=X5Dpbq0RL/V9JB51KnBSAWejy/z7LkBNpGVis4phlA17t3HBQAQjp9df0piWo5cGaT NPmjPy9T0Eqg4ro6ctanLb7mMA2e61LwDoQcuoIvODC8MrcYz5mZtxA/2v9fFWoGOZQW OUqQXWmJkGume6rGE5z2AMB62zCbyk018/hlAE0BEGi4eOLbCluy7MZzpt44WHImxk4X /CoOZWfHzsoYDJgkKlEtS8SH9USzYmbFBSd6k8XPADcE81PZsO7IF9hytA5dIeY9caSj P0+t2Ft35NH2NQNFJPpb7xqqF1JoRe4mvrifzAGYv6QWSWPt2jMHkDC6ATKvPxSkh1FA Q20A== 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=al1pLuyWTCVVGzK5+zU3cBUCCTyiFzzFqmI5+hXtnNw=; fh=bGF4ESczbIrmrNxFOdEMfzJNgTVkRsqDRt+LRN0CeSg=; b=DYL7yEZ9tRt5IKiZDY6jlDQpAiJr5sYxa1j4yS4biiZ2yG9Pd7o8P2DkT2+X9yoxfh bvXA8D9V8BEt75i40j8e+Oqhy/ssSEHojVxrRPU7gRD2219sdpqGZf28lIPDDMsIBG9F JiV/ssljFmdbqXQyL5kBvs/0AIsL3tvh/idLnBPgJINWG0oOGVkPwEaDd1ZkqJGOIQbo TzjrdEGK2DV4L2cD0ulL1w40uIU2bC0R+/+cDJI1PPf925/+Uy1mqKZaHxAV4xzuPuMC U1sssjE2h+QbOvbLnTNkjQRhWEzZGVAsxJ/zEzoYQTEzRx4rHQdFsGFW77/6xQ/M0oMH 7nQQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cirrus.com header.s=PODMain02222019 header.b=jq6RE8GP; 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=REJECT sp=REJECT dis=NONE) header.from=cirrus.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w4-20020a170906d20400b00993398bcae2si7809977ejz.122.2023.08.14.07.52.22; Mon, 14 Aug 2023 07:52:49 -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=@cirrus.com header.s=PODMain02222019 header.b=jq6RE8GP; 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=REJECT sp=REJECT dis=NONE) header.from=cirrus.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231190AbjHNNX7 (ORCPT + 99 others); Mon, 14 Aug 2023 09:23:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41920 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230467AbjHNNXg (ORCPT ); Mon, 14 Aug 2023 09:23:36 -0400 Received: from mx0b-001ae601.pphosted.com (mx0a-001ae601.pphosted.com [67.231.149.25]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 059ED10D1; Mon, 14 Aug 2023 06:23:35 -0700 (PDT) Received: from pps.filterd (m0077473.ppops.net [127.0.0.1]) by mx0a-001ae601.pphosted.com (8.17.1.22/8.17.1.22) with ESMTP id 37E5t3EL003856; Mon, 14 Aug 2023 08:23:18 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cirrus.com; h= from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding:content-type; s= PODMain02222019; bh=al1pLuyWTCVVGzK5+zU3cBUCCTyiFzzFqmI5+hXtnNw=; b= jq6RE8GPt/gZFwo2FszmwZcwujQDej08hJbxiNS2vbPHwV9CPANAxGbMXLPyL6ny 4BUe9fNyVVd6sGeoI34LnMw2UvWWfMjMGW9Qe6cRHCnV7dJTHV3thSNCEDe2IL2d pYbkl4U95QCFGrpHNmIIQMPnMk65pF5G+3Iu3ZvTqf1qaNy/4WeenEy/aE9l+NPy eIdbLp0htU99K9KOsXuj9pwZuMnXVkwy0cShPjlWYRL8ZAlu22SF94C6xWw9sn3f Dos9wwjrb/q2rz9UBMUCupagLwCwikI/XdFnF16WsG424wV5JHcLhjnwUSnF3q8D 7cJsRB6YravXV3YbquzsQg== Received: from ediex02.ad.cirrus.com ([84.19.233.68]) by mx0a-001ae601.pphosted.com (PPS) with ESMTPS id 3se8kqt6x8-5 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 14 Aug 2023 08:23:18 -0500 (CDT) Received: from ediex02.ad.cirrus.com (198.61.84.81) by ediex02.ad.cirrus.com (198.61.84.81) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.30; Mon, 14 Aug 2023 14:23:15 +0100 Received: from ediswmail.ad.cirrus.com (198.61.86.93) by anon-ediex02.ad.cirrus.com (198.61.84.81) with Microsoft SMTP Server id 15.2.1118.30 via Frontend Transport; Mon, 14 Aug 2023 14:23:15 +0100 Received: from EDIN4L06LR3.ad.cirrus.com (EDIN4L06LR3.ad.cirrus.com [198.61.65.68]) by ediswmail.ad.cirrus.com (Postfix) with ESMTP id A4A2F15B4; Mon, 14 Aug 2023 13:23:14 +0000 (UTC) From: Richard Fitzgerald To: , , CC: , , , , Richard Fitzgerald Subject: [PATCH v4 07/10] kunit: string-stream: Decouple string_stream from kunit Date: Mon, 14 Aug 2023 14:23:06 +0100 Message-ID: <20230814132309.32641-8-rf@opensource.cirrus.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230814132309.32641-1-rf@opensource.cirrus.com> References: <20230814132309.32641-1-rf@opensource.cirrus.com> MIME-Version: 1.0 X-Proofpoint-ORIG-GUID: fVaEWIB1XCtvu2ogE4F64OV8yyuRQERN X-Proofpoint-GUID: fVaEWIB1XCtvu2ogE4F64OV8yyuRQERN X-Proofpoint-Spam-Reason: safe X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1774216564299032930 X-GMAIL-MSGID: 1774216564299032930 Re-work string_stream so that it is not tied to a struct kunit. This is to allow using it for the log of struct kunit_suite. Instead of resource-managing individual allocations the whole string_stream object can be resource-managed as a single object: alloc_string_stream() API is unchanged and takes a pointer to a struct kunit but it now registers the returned string_stream object to be resource-managed. raw_alloc_string_stream() is a new function that allocates a bare string_stream without any association to a struct kunit. free_string_stream() is a new function that frees a resource-managed string_stream allocated by alloc_string_stream(). raw_free_string_stream() is a new function that frees a non-managed string_stream allocated by raw_alloc_string_stream(). The confusing function string_stream_destroy() has been removed. This only called string_stream_clear() but didn't free the struct string_stream. Instead string_stream_clear() has been exported, and the new functions use the more conventional naming of "free" as the opposite of "alloc". Signed-off-by: Richard Fitzgerald --- lib/kunit/string-stream-test.c | 2 +- lib/kunit/string-stream.c | 92 +++++++++++++++++++++++----------- lib/kunit/string-stream.h | 12 ++++- lib/kunit/test.c | 2 +- 4 files changed, 77 insertions(+), 31 deletions(-) diff --git a/lib/kunit/string-stream-test.c b/lib/kunit/string-stream-test.c index 8a30bb7d5fb7..437aa4b3179d 100644 --- a/lib/kunit/string-stream-test.c +++ b/lib/kunit/string-stream-test.c @@ -200,7 +200,7 @@ static void string_stream_append_test(struct kunit *test) combined_content); /* Append content of non-empty stream to empty stream */ - string_stream_destroy(stream_1); + string_stream_clear(stream_1); stream_1 = alloc_string_stream(test, GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream_1); diff --git a/lib/kunit/string-stream.c b/lib/kunit/string-stream.c index eb673d3ea3bd..06104a729b45 100644 --- a/lib/kunit/string-stream.c +++ b/lib/kunit/string-stream.c @@ -13,30 +13,28 @@ #include "string-stream.h" -static struct string_stream_fragment *alloc_string_stream_fragment( - struct kunit *test, int len, gfp_t gfp) +static struct string_stream_fragment *alloc_string_stream_fragment(int len, gfp_t gfp) { struct string_stream_fragment *frag; - frag = kunit_kzalloc(test, sizeof(*frag), gfp); + frag = kzalloc(sizeof(*frag), gfp); if (!frag) return ERR_PTR(-ENOMEM); - frag->fragment = kunit_kmalloc(test, len, gfp); + frag->fragment = kmalloc(len, gfp); if (!frag->fragment) { - kunit_kfree(test, frag); + kfree(frag); return ERR_PTR(-ENOMEM); } return frag; } -static void string_stream_fragment_destroy(struct kunit *test, - struct string_stream_fragment *frag) +static void string_stream_fragment_destroy(struct string_stream_fragment *frag) { list_del(&frag->node); - kunit_kfree(test, frag->fragment); - kunit_kfree(test, frag); + kfree(frag->fragment); + kfree(frag); } int string_stream_vadd(struct string_stream *stream, @@ -65,9 +63,7 @@ int string_stream_vadd(struct string_stream *stream, /* Need space for null byte. */ buf_len++; - frag_container = alloc_string_stream_fragment(stream->test, - buf_len, - stream->gfp); + frag_container = alloc_string_stream_fragment(buf_len, stream->gfp); if (IS_ERR(frag_container)) return PTR_ERR(frag_container); @@ -102,7 +98,7 @@ int string_stream_add(struct string_stream *stream, const char *fmt, ...) return result; } -static void string_stream_clear(struct string_stream *stream) +void string_stream_clear(struct string_stream *stream) { struct string_stream_fragment *frag_container, *frag_container_safe; @@ -111,16 +107,28 @@ static void string_stream_clear(struct string_stream *stream) frag_container_safe, &stream->fragments, node) { - string_stream_fragment_destroy(stream->test, frag_container); + string_stream_fragment_destroy(frag_container); } stream->length = 0; spin_unlock(&stream->lock); } +static void _string_stream_concatenate_to_buf(struct string_stream *stream, + char *buf, size_t buf_len) +{ + struct string_stream_fragment *frag_container; + + buf[0] = '\0'; + + spin_lock(&stream->lock); + list_for_each_entry(frag_container, &stream->fragments, node) + strlcat(buf, frag_container->fragment, buf_len); + spin_unlock(&stream->lock); +} + char *string_stream_get_string(struct kunit *test, struct string_stream *stream, gfp_t gfp) { - struct string_stream_fragment *frag_container; size_t buf_len = stream->length + 1; /* +1 for null byte. */ char *buf; @@ -128,10 +136,7 @@ char *string_stream_get_string(struct kunit *test, struct string_stream *stream, if (!buf) return NULL; - spin_lock(&stream->lock); - list_for_each_entry(frag_container, &stream->fragments, node) - strlcat(buf, frag_container->fragment, buf_len); - spin_unlock(&stream->lock); + _string_stream_concatenate_to_buf(stream, buf, buf_len); return buf; } @@ -139,13 +144,20 @@ char *string_stream_get_string(struct kunit *test, struct string_stream *stream, int string_stream_append(struct string_stream *stream, struct string_stream *other) { - const char *other_content; + size_t other_buf_len = other->length + 1; /* +1 for null byte. */ + char *other_buf; + int ret; - other_content = string_stream_get_string(other->test, other, other->gfp); - if (!other_content) + other_buf = kmalloc(other_buf_len, GFP_KERNEL); + if (!other_buf) return -ENOMEM; - return string_stream_add(stream, other_content); + _string_stream_concatenate_to_buf(other, other_buf, other_buf_len); + + ret = string_stream_add(stream, other_buf); + kfree(other_buf); + + return ret; } bool string_stream_is_empty(struct string_stream *stream) @@ -153,23 +165,47 @@ bool string_stream_is_empty(struct string_stream *stream) return list_empty(&stream->fragments); } -struct string_stream *alloc_string_stream(struct kunit *test, gfp_t gfp) +void raw_free_string_stream(struct string_stream *stream) +{ + string_stream_clear(stream); + kfree(stream); +} + +struct string_stream *raw_alloc_string_stream(gfp_t gfp) { struct string_stream *stream; - stream = kunit_kzalloc(test, sizeof(*stream), gfp); + stream = kzalloc(sizeof(*stream), gfp); if (!stream) return ERR_PTR(-ENOMEM); stream->gfp = gfp; - stream->test = test; INIT_LIST_HEAD(&stream->fragments); spin_lock_init(&stream->lock); return stream; } -void string_stream_destroy(struct string_stream *stream) +struct string_stream *alloc_string_stream(struct kunit *test, gfp_t gfp) { - string_stream_clear(stream); + struct string_stream *stream; + + stream = raw_alloc_string_stream(gfp); + if (IS_ERR(stream)) + return stream; + + stream->test = test; + + if (kunit_add_action_or_reset(test, (kunit_action_t *)raw_free_string_stream, stream) != 0) + return ERR_PTR(-ENOMEM); + + return stream; +} + +void free_string_stream(struct kunit *test, struct string_stream *stream) +{ + if (!stream) + return; + + kunit_release_action(test, (kunit_action_t *)raw_free_string_stream, (void *)stream); } diff --git a/lib/kunit/string-stream.h b/lib/kunit/string-stream.h index 6b4a747881c5..fbeda486382a 100644 --- a/lib/kunit/string-stream.h +++ b/lib/kunit/string-stream.h @@ -23,14 +23,24 @@ struct string_stream { struct list_head fragments; /* length and fragments are protected by this lock */ spinlock_t lock; + + /* + * Pointer to kunit this stream is associated with, or NULL if + * not associated with a kunit. + */ struct kunit *test; + gfp_t gfp; bool append_newlines; }; struct kunit; +struct string_stream *raw_alloc_string_stream(gfp_t gfp); +void raw_free_string_stream(struct string_stream *stream); + struct string_stream *alloc_string_stream(struct kunit *test, gfp_t gfp); +void free_string_stream(struct kunit *test, struct string_stream *stream); int __printf(2, 3) string_stream_add(struct string_stream *stream, const char *fmt, ...); @@ -47,7 +57,7 @@ int string_stream_append(struct string_stream *stream, bool string_stream_is_empty(struct string_stream *stream); -void string_stream_destroy(struct string_stream *stream); +void string_stream_clear(struct string_stream *stream); static inline void string_stream_set_append_newlines(struct string_stream *stream, bool append_newlines) diff --git a/lib/kunit/test.c b/lib/kunit/test.c index 520e15f49d0d..4b69d12dfc96 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -322,7 +322,7 @@ static void kunit_fail(struct kunit *test, const struct kunit_loc *loc, kunit_print_string_stream(test, stream); - string_stream_destroy(stream); + free_string_stream(test, stream); } void __noreturn __kunit_abort(struct kunit *test) From patchwork Mon Aug 14 13:23:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Fitzgerald X-Patchwork-Id: 135444 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b824:0:b0:3f2:4152:657d with SMTP id z4csp2761766vqi; Mon, 14 Aug 2023 06:58:18 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGa35UBg39QlgyaDf3eG9AiCwFpKZvAVQ530LAq7l36BptljvF6wuGEq/YsNZAEzuvGpC2T X-Received: by 2002:a17:902:a40a:b0:1b8:e41:f43f with SMTP id p10-20020a170902a40a00b001b80e41f43fmr10751903plq.27.1692021498513; Mon, 14 Aug 2023 06:58:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692021498; cv=none; d=google.com; s=arc-20160816; b=Iymp13Mr0gyU+RENWTEaLwcEMr1wAbJgCSf13yxNlqS84kKjcDXgTQML3EMqR82MLn vAB30WzMj3ZW6czh0ypuo+fchwzsyfLYCJCZBsKiqHyokukvih2jqOx3YXNCx0dFiRrs ymgDft+9NZUPQNWDXhurYshv8zXKK2DnAwhOhFlFf6ADPACajdJhNmJz2Hs2/yNwIsV+ K5z9+n2gbnRgtQP25cMVNyW4miCQESdhiVG3FNUhijdVNxuKZ01OfRbWniRGwHEXMqIn uXTFO1JodLuMNtEp8KZoGTCH5/s6EQFkQ91n34oR/OdXdcOdXvnaV6oioswCivV+lOAv 5ElQ== 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=Q2vvCFoXKdnxSXdtqWfmyzZJnVFwCZ6PAHSC4clhH3I=; fh=bGF4ESczbIrmrNxFOdEMfzJNgTVkRsqDRt+LRN0CeSg=; b=IrKL30iAG0mucvO59eDZELrqF1sdcW3mPSVCajw0iJl+zz9nZ7FTz1F6ObfSEijTcW Vpv8MkKnlJ+NEJ7gyXfJJ9gfZ8tQVddDgHkNBR1u8qP/PSlz59iUyXjNh6S+4lkxW9+s LhCkMzyCmC9zCp5uyDAPgtFu5/wy7HuHc+Ky0DMnpD5Qomx8XT6k5bCPptSyyc+Tgqzg L04n/MNFrjbAZG0Rlt+y4N1c5Uwi4nIr1zbPM9JI6ASCe14NQD1wqrKDiBNeBCjuWK0x hGYBRX7Giy0BE29XM46nidXx32ZNNJRmqhA294+GQIh8MSgoezYNdtHH5wJ5X4qdO4TS 8fzg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cirrus.com header.s=PODMain02222019 header.b="l499AT/B"; 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=REJECT sp=REJECT dis=NONE) header.from=cirrus.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u1-20020a17090341c100b001b8805f98e9si8363816ple.452.2023.08.14.06.58.05; Mon, 14 Aug 2023 06:58:18 -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=@cirrus.com header.s=PODMain02222019 header.b="l499AT/B"; 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=REJECT sp=REJECT dis=NONE) header.from=cirrus.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231133AbjHNNX6 (ORCPT + 99 others); Mon, 14 Aug 2023 09:23:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41926 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230468AbjHNNXh (ORCPT ); Mon, 14 Aug 2023 09:23:37 -0400 Received: from mx0b-001ae601.pphosted.com (mx0a-001ae601.pphosted.com [67.231.149.25]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3E0F1E7D; Mon, 14 Aug 2023 06:23:36 -0700 (PDT) Received: from pps.filterd (m0077473.ppops.net [127.0.0.1]) by mx0a-001ae601.pphosted.com (8.17.1.22/8.17.1.22) with ESMTP id 37E5t3EM003856; Mon, 14 Aug 2023 08:23:19 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cirrus.com; h= from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding:content-type; s= PODMain02222019; bh=Q2vvCFoXKdnxSXdtqWfmyzZJnVFwCZ6PAHSC4clhH3I=; b= l499AT/BL3ENUPuVaTvH+4gf2GHrbI+Luvmj90g/26fRm0GIv3Oewue09TkEiGgJ qkh8DEMjYLzqPB333JOsXPukn5JlL/K7FQ7T+vxwtZQayVrocXWNi1ZnPDS8n8qD G/y25dqtn6UCfWnRsxXmJxzz0CevqSJSxYquuW/p3ND9sGRDVG8YV1oGCH8uiaUO qU1G7W9BZeE4OOsvJk3p2p9zdccfIBSrj36i8xfBLJlag+J70vS6AfN01/ESvsQx 51fht8AXEfchgoTUIASuaxhUd7PbK7DXKaDX1uBw2QbBnmfgzZxzM5a/mGzIrUWv k7DYdrIazPHYMYYEw0DGOw== Received: from ediex02.ad.cirrus.com ([84.19.233.68]) by mx0a-001ae601.pphosted.com (PPS) with ESMTPS id 3se8kqt6x8-6 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 14 Aug 2023 08:23:19 -0500 (CDT) Received: from ediex01.ad.cirrus.com (198.61.84.80) by ediex02.ad.cirrus.com (198.61.84.81) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.30; Mon, 14 Aug 2023 14:23:15 +0100 Received: from ediswmail.ad.cirrus.com (198.61.86.93) by ediex01.ad.cirrus.com (198.61.84.80) with Microsoft SMTP Server id 15.2.1118.30 via Frontend Transport; Mon, 14 Aug 2023 14:23:15 +0100 Received: from EDIN4L06LR3.ad.cirrus.com (EDIN4L06LR3.ad.cirrus.com [198.61.65.68]) by ediswmail.ad.cirrus.com (Postfix) with ESMTP id 1DA44458; Mon, 14 Aug 2023 13:23:15 +0000 (UTC) From: Richard Fitzgerald To: , , CC: , , , , Richard Fitzgerald Subject: [PATCH v4 08/10] kunit: string-stream: Add test for freeing resource-managed string_stream Date: Mon, 14 Aug 2023 14:23:07 +0100 Message-ID: <20230814132309.32641-9-rf@opensource.cirrus.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230814132309.32641-1-rf@opensource.cirrus.com> References: <20230814132309.32641-1-rf@opensource.cirrus.com> MIME-Version: 1.0 X-Proofpoint-ORIG-GUID: c335VR0-Lq_QzjDpmR87PUvY8W8MBqpW X-Proofpoint-GUID: c335VR0-Lq_QzjDpmR87PUvY8W8MBqpW X-Proofpoint-Spam-Reason: safe X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1774213134718945256 X-GMAIL-MSGID: 1774213134718945256 string_stream_resource_free_test() allocates a resource-managed string_stream and tests that raw_free_string_stream() is called when the test resources are cleaned up. string_stream_init_test() is extended to test allocating a string_stream that is not resource-managed. Signed-off-by: Richard Fitzgerald --- lib/kunit/string-stream-test.c | 117 ++++++++++++++++++++++++++++++++- lib/kunit/string-stream.c | 3 + 2 files changed, 119 insertions(+), 1 deletion(-) diff --git a/lib/kunit/string-stream-test.c b/lib/kunit/string-stream-test.c index 437aa4b3179d..05bfade2bd8a 100644 --- a/lib/kunit/string-stream-test.c +++ b/lib/kunit/string-stream-test.c @@ -6,16 +6,27 @@ * Author: Brendan Higgins */ +#include #include #include #include "string-stream.h" +struct string_stream_test_priv { + struct string_stream *raw_stream; + + /* For testing resource-managed free */ + struct string_stream *freed_stream; + bool stream_free_again; +}; + /* string_stream object is initialized correctly. */ static void string_stream_init_test(struct kunit *test) { + struct string_stream_test_priv *priv = test->priv; struct string_stream *stream; + /* Resource-managed initialization */ stream = alloc_string_stream(test, GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); @@ -26,6 +37,86 @@ static void string_stream_init_test(struct kunit *test) KUNIT_EXPECT_FALSE(test, stream->append_newlines); KUNIT_EXPECT_TRUE(test, string_stream_is_empty(stream)); + + free_string_stream(test, stream); + + /* + * Raw initialization. This allocation is not resource-managed + * so store it in priv->raw_stream to be cleaned up by the + * exit function. + */ + priv->raw_stream = raw_alloc_string_stream(GFP_KERNEL); + stream = priv->raw_stream; + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); + + KUNIT_EXPECT_EQ(test, stream->length, 0); + KUNIT_EXPECT_TRUE(test, list_empty(&stream->fragments)); + KUNIT_EXPECT_PTR_EQ(test, stream->test, NULL); + KUNIT_EXPECT_EQ(test, stream->gfp, GFP_KERNEL); + KUNIT_EXPECT_FALSE(test, stream->append_newlines); + + KUNIT_EXPECT_TRUE(test, string_stream_is_empty(stream)); +} + +static void string_stream_raw_free_string_stream_stub(struct string_stream *stream) +{ + struct kunit *fake_test = kunit_get_current_test(); + struct string_stream_test_priv *priv = fake_test->priv; + + if (priv->freed_stream) + priv->stream_free_again = true; + + priv->freed_stream = stream; + + /* + * Avoid calling deactivate_static_stub() or changing + * current->kunit_test during cleanup. Leave the stream to + * be freed during the test exit. + */ +} + +/* string_stream object is freed when test is cleaned up. */ +static void string_stream_resource_free_test(struct kunit *test) +{ + struct string_stream_test_priv *priv = test->priv; + struct kunit *fake_test; + struct string_stream *stream; + + fake_test = kunit_kzalloc(test, sizeof(*fake_test), GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fake_test); + + kunit_init_test(fake_test, "string_stream_fake_test", NULL); + fake_test->priv = priv; + + /* + * Activate stub before creating string_stream so the + * string_stream will be cleaned up first. + */ + priv->freed_stream = NULL; + priv->stream_free_again = false; + kunit_activate_static_stub(fake_test, + raw_free_string_stream, + string_stream_raw_free_string_stream_stub); + + stream = alloc_string_stream(fake_test, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); + + /* + * Ensure the stream is freed when this test terminates. + */ + priv->raw_stream = stream; + + /* Set current->kunit_test to fake_test so the static stub will be called. */ + current->kunit_test = fake_test; + + /* Cleanup test - the stub function should be called */ + kunit_cleanup(fake_test); + + /* Set current->kunit_test back to current test. */ + current->kunit_test = test; + + KUNIT_EXPECT_PTR_EQ(test, priv->freed_stream, stream); + KUNIT_EXPECT_FALSE(test, priv->stream_free_again); } /* @@ -279,8 +370,30 @@ static void string_stream_auto_newline_test(struct kunit *test) "One\nTwo\nThree\nFour\nFive\nSix\nSeven\n\nEight\n"); } +static int string_stream_test_init(struct kunit *test) +{ + struct string_stream_test_priv *priv; + + priv = kunit_kzalloc(test, sizeof(*priv), GFP_KERNEL); + if (!priv) + return -ENOMEM; + + test->priv = priv; + + return 0; +} + +static void string_stream_test_exit(struct kunit *test) +{ + struct string_stream_test_priv *priv = test->priv; + + if (priv && priv->raw_stream) + raw_free_string_stream(priv->raw_stream); +} + static struct kunit_case string_stream_test_cases[] = { KUNIT_CASE(string_stream_init_test), + KUNIT_CASE(string_stream_resource_free_test), KUNIT_CASE(string_stream_line_add_test), KUNIT_CASE(string_stream_variable_length_line_test), KUNIT_CASE(string_stream_append_test), @@ -292,6 +405,8 @@ static struct kunit_case string_stream_test_cases[] = { static struct kunit_suite string_stream_test_suite = { .name = "string-stream-test", - .test_cases = string_stream_test_cases + .test_cases = string_stream_test_cases, + .init = string_stream_test_init, + .exit = string_stream_test_exit, }; kunit_test_suites(&string_stream_test_suite); diff --git a/lib/kunit/string-stream.c b/lib/kunit/string-stream.c index 06104a729b45..1b55ac1be2e5 100644 --- a/lib/kunit/string-stream.c +++ b/lib/kunit/string-stream.c @@ -6,6 +6,7 @@ * Author: Brendan Higgins */ +#include #include #include #include @@ -167,6 +168,8 @@ bool string_stream_is_empty(struct string_stream *stream) void raw_free_string_stream(struct string_stream *stream) { + KUNIT_STATIC_STUB_REDIRECT(raw_free_string_stream, stream); + string_stream_clear(stream); kfree(stream); } From patchwork Mon Aug 14 13:23:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Fitzgerald X-Patchwork-Id: 135453 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b824:0:b0:3f2:4152:657d with SMTP id z4csp2766046vqi; Mon, 14 Aug 2023 07:04:04 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEWA1LJtxS3khPFAs+RU3jtbaOakJqUveEeUnJLB47qzCOv4L9uYjBIwFDTCFkLheixw2Eg X-Received: by 2002:a05:6a21:7891:b0:140:730b:4b3f with SMTP id bf17-20020a056a21789100b00140730b4b3fmr11905204pzc.1.1692021844205; Mon, 14 Aug 2023 07:04:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692021844; cv=none; d=google.com; s=arc-20160816; b=nazskegVULXLeausZW5DQkUiSYb+0pparx1hbEzMbeBi1fJV7PwaXG897U1NDFUy+r ZDP6RpFmjhmk8MmAOzIh7imwbHIeFo1nF2ajhVzXWjMKRPv3cmidsMxmxCScgN4MM+lk cbt1LzMCpJ/Laxzh+vZWZycSNpz0CxlLLxNkqxyNf5jgyxce0OiroxSHO39K0mnel9n+ OjwzBT2bS0TuAlMMcaB99t4SRn9gYoXYyIxIKJkV0hHdBm0WBB4eyJerzCcANY11n5q3 veKH6DHNUP1BuQ5H3IdEDGBURfYCpH+q9IVdV5P+Gue2geU0/P+fS+TKtTebkf5ZnbXj ut+Q== 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=+BBXdxK8Pf+uSHNbvIHSNZw2FpnfU50yVfAT5GHEHYw=; fh=bGF4ESczbIrmrNxFOdEMfzJNgTVkRsqDRt+LRN0CeSg=; b=yNl1RtUEYJ7ug64BAA/wENCLD6fvVPhaShv2dyqPnhxv1JNR/VYrVHvvDvW3PSraqW WndytG0UloxEo8LKWVT2/zxaq3Qcjn2nfr/Iy1xFoo+4nJBweWtd7FjuFLp3op3/FQN/ 59Zp2FdeJOOe/ZSH6fUdAxWOxK0YsWCrsh98REo6qYuprIuQCBSodwkNjGcJnMwzAHET Tw13Eio2Q3RTLnM30CZFQ3eJ7/9lr5qZpZ+gXi3rBGgzMpmhY7klzBraMd+BokCj8j5v V13ao9Dxm1F5WTIA/YSzkghW7i2gtcGUfBjTLMCM8YYTFXomXgL6y7lSiGPA+hOm0kFL Fsfw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cirrus.com header.s=PODMain02222019 header.b=npPWybSD; 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=REJECT sp=REJECT dis=NONE) header.from=cirrus.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ck14-20020a056a00328e00b00687546abaf6si8004916pfb.195.2023.08.14.07.03.44; Mon, 14 Aug 2023 07:04:04 -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=@cirrus.com header.s=PODMain02222019 header.b=npPWybSD; 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=REJECT sp=REJECT dis=NONE) header.from=cirrus.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231207AbjHNNYC (ORCPT + 99 others); Mon, 14 Aug 2023 09:24:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41992 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230497AbjHNNXj (ORCPT ); Mon, 14 Aug 2023 09:23:39 -0400 Received: from mx0b-001ae601.pphosted.com (mx0a-001ae601.pphosted.com [67.231.149.25]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0596C10C6; Mon, 14 Aug 2023 06:23:38 -0700 (PDT) Received: from pps.filterd (m0077473.ppops.net [127.0.0.1]) by mx0a-001ae601.pphosted.com (8.17.1.22/8.17.1.22) with ESMTP id 37E5t3EN003856; Mon, 14 Aug 2023 08:23:20 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cirrus.com; h= from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding:content-type; s= PODMain02222019; bh=+BBXdxK8Pf+uSHNbvIHSNZw2FpnfU50yVfAT5GHEHYw=; b= npPWybSDyR3lARAVejRhn7FXbLDU6w64fSAjmnKlXANaWXCgVsXVd7f+iqn0pqph jQ57EaxbiTF6yrSnm969fyrb0DAE5g01vaMzXFVUJis/QExLCsMsSiO6GTbfk5Bl eCQ6xsjofegOAoko7xvPKr025ZtsOYuVcA+vgEX8egsBmRCTAeRuOirQvTCllB34 Y1FBmKMh2cs/C4uTD/tbky2rC57qPgID6sQ1wXAhQ3HyOjMYl1ZnWGs2/u2GET7s F0nsWfBpaEQ0AD0zifafrrpSj9cy/x67ZohQVzuAZc2SxHr419FGdaXpp4EhmMVe tByZNloonmngYCtH7CwDXw== Received: from ediex02.ad.cirrus.com ([84.19.233.68]) by mx0a-001ae601.pphosted.com (PPS) with ESMTPS id 3se8kqt6x8-7 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 14 Aug 2023 08:23:19 -0500 (CDT) Received: from ediex01.ad.cirrus.com (198.61.84.80) by ediex02.ad.cirrus.com (198.61.84.81) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.30; Mon, 14 Aug 2023 14:23:16 +0100 Received: from ediswmail.ad.cirrus.com (198.61.86.93) by ediex01.ad.cirrus.com (198.61.84.80) with Microsoft SMTP Server id 15.2.1118.30 via Frontend Transport; Mon, 14 Aug 2023 14:23:16 +0100 Received: from EDIN4L06LR3.ad.cirrus.com (EDIN4L06LR3.ad.cirrus.com [198.61.65.68]) by ediswmail.ad.cirrus.com (Postfix) with ESMTP id 9AA4F15B4; Mon, 14 Aug 2023 13:23:15 +0000 (UTC) From: Richard Fitzgerald To: , , CC: , , , , Richard Fitzgerald Subject: [PATCH v4 09/10] kunit: Use string_stream for test log Date: Mon, 14 Aug 2023 14:23:08 +0100 Message-ID: <20230814132309.32641-10-rf@opensource.cirrus.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230814132309.32641-1-rf@opensource.cirrus.com> References: <20230814132309.32641-1-rf@opensource.cirrus.com> MIME-Version: 1.0 X-Proofpoint-ORIG-GUID: 7De8gvqUaM7YifO5SdZIjnbEJxphFxX5 X-Proofpoint-GUID: 7De8gvqUaM7YifO5SdZIjnbEJxphFxX5 X-Proofpoint-Spam-Reason: safe X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1774213497200272887 X-GMAIL-MSGID: 1774213497200272887 Replace the fixed-size log buffer with a string_stream so that the log can grow as lines are added. The existing kunit log tests have been updated for using a string_stream as the log. As they now depend on string_stream functions they cannot build when kunit-test is a module. They have been moved to a separate source file to be built only if kunit-test is built-in. Signed-off-by: Richard Fitzgerald --- include/kunit/test.h | 14 ++++---- lib/kunit/Makefile | 5 +-- lib/kunit/debugfs.c | 36 +++++++++++++-------- lib/kunit/kunit-test.c | 52 +----------------------------- lib/kunit/log-test.c | 72 ++++++++++++++++++++++++++++++++++++++++++ lib/kunit/test.c | 44 +++----------------------- 6 files changed, 110 insertions(+), 113 deletions(-) create mode 100644 lib/kunit/log-test.c diff --git a/include/kunit/test.h b/include/kunit/test.h index d33114097d0d..b915a0fe93c0 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -32,9 +32,7 @@ DECLARE_STATIC_KEY_FALSE(kunit_running); struct kunit; - -/* Size of log associated with test. */ -#define KUNIT_LOG_SIZE 2048 +struct string_stream; /* Maximum size of parameter description string. */ #define KUNIT_PARAM_DESC_SIZE 128 @@ -132,7 +130,7 @@ struct kunit_case { /* private: internal use only. */ enum kunit_status status; char *module_name; - char *log; + struct string_stream *log; }; static inline char *kunit_status_to_ok_not_ok(enum kunit_status status) @@ -252,7 +250,7 @@ struct kunit_suite { /* private: internal use only */ char status_comment[KUNIT_STATUS_COMMENT_SIZE]; struct dentry *debugfs; - char *log; + struct string_stream *log; int suite_init_err; }; @@ -278,7 +276,7 @@ struct kunit { /* private: internal use only. */ const char *name; /* Read only after initialization! */ - char *log; /* Points at case log after initialization */ + struct string_stream *log; /* Points at case log after initialization */ struct kunit_try_catch try_catch; /* param_value is the current parameter value for a test case. */ const void *param_value; @@ -314,7 +312,7 @@ const char *kunit_filter_glob(void); char *kunit_filter(void); char *kunit_filter_action(void); -void kunit_init_test(struct kunit *test, const char *name, char *log); +void kunit_init_test(struct kunit *test, const char *name, struct string_stream *log); int kunit_run_tests(struct kunit_suite *suite); @@ -472,7 +470,7 @@ static inline void *kunit_kcalloc(struct kunit *test, size_t n, size_t size, gfp void kunit_cleanup(struct kunit *test); -void __printf(2, 3) kunit_log_append(char *log, const char *fmt, ...); +void __printf(2, 3) kunit_log_append(struct string_stream *log, const char *fmt, ...); /** * kunit_mark_skipped() - Marks @test_or_suite as skipped diff --git a/lib/kunit/Makefile b/lib/kunit/Makefile index 46f75f23dfe4..65735c2e1d14 100644 --- a/lib/kunit/Makefile +++ b/lib/kunit/Makefile @@ -18,9 +18,10 @@ obj-y += hooks.o obj-$(CONFIG_KUNIT_TEST) += kunit-test.o -# string-stream-test compiles built-in only. +# string-stream-test and log-test compiles built-in only. ifeq ($(CONFIG_KUNIT_TEST),y) -obj-$(CONFIG_KUNIT_TEST) += string-stream-test.o +obj-$(CONFIG_KUNIT_TEST) += string-stream-test.o \ + log-test.o endif obj-$(CONFIG_KUNIT_EXAMPLE_TEST) += kunit-example-test.o diff --git a/lib/kunit/debugfs.c b/lib/kunit/debugfs.c index 22c5c496a68f..ab53a7e5c898 100644 --- a/lib/kunit/debugfs.c +++ b/lib/kunit/debugfs.c @@ -37,14 +37,21 @@ void kunit_debugfs_init(void) debugfs_rootdir = debugfs_create_dir(KUNIT_DEBUGFS_ROOT, NULL); } -static void debugfs_print_result(struct seq_file *seq, - struct kunit_suite *suite, - struct kunit_case *test_case) +static void debugfs_print_result(struct seq_file *seq, struct string_stream *log) { - if (!test_case || !test_case->log) + struct string_stream_fragment *frag_container; + + if (!log) return; - seq_printf(seq, "%s", test_case->log); + /* + * Walk the fragments so we don't need to allocate a temporary + * buffer to hold the entire string. + */ + spin_lock(&log->lock); + list_for_each_entry(frag_container, &log->fragments, node) + seq_printf(seq, "%s", frag_container->fragment); + spin_unlock(&log->lock); } /* @@ -69,10 +76,9 @@ static int debugfs_print_results(struct seq_file *seq, void *v) seq_printf(seq, KUNIT_SUBTEST_INDENT "1..%zd\n", kunit_suite_num_test_cases(suite)); kunit_suite_for_each_test_case(suite, test_case) - debugfs_print_result(seq, suite, test_case); + debugfs_print_result(seq, test_case->log); - if (suite->log) - seq_printf(seq, "%s", suite->log); + debugfs_print_result(seq, suite->log); seq_printf(seq, "%s %d %s\n", kunit_status_to_ok_not_ok(success), 1, suite->name); @@ -105,9 +111,13 @@ void kunit_debugfs_create_suite(struct kunit_suite *suite) struct kunit_case *test_case; /* Allocate logs before creating debugfs representation. */ - suite->log = kzalloc(KUNIT_LOG_SIZE, GFP_KERNEL); - kunit_suite_for_each_test_case(suite, test_case) - test_case->log = kzalloc(KUNIT_LOG_SIZE, GFP_KERNEL); + suite->log = raw_alloc_string_stream(GFP_KERNEL); + string_stream_set_append_newlines(suite->log, true); + + kunit_suite_for_each_test_case(suite, test_case) { + test_case->log = raw_alloc_string_stream(GFP_KERNEL); + string_stream_set_append_newlines(test_case->log, true); + } suite->debugfs = debugfs_create_dir(suite->name, debugfs_rootdir); @@ -121,7 +131,7 @@ void kunit_debugfs_destroy_suite(struct kunit_suite *suite) struct kunit_case *test_case; debugfs_remove_recursive(suite->debugfs); - kfree(suite->log); + raw_free_string_stream(suite->log); kunit_suite_for_each_test_case(suite, test_case) - kfree(test_case->log); + raw_free_string_stream(test_case->log); } diff --git a/lib/kunit/kunit-test.c b/lib/kunit/kunit-test.c index 83d8e90ca7a2..ecc39d5f7411 100644 --- a/lib/kunit/kunit-test.c +++ b/lib/kunit/kunit-test.c @@ -530,55 +530,6 @@ static struct kunit_suite kunit_resource_test_suite = { .test_cases = kunit_resource_test_cases, }; -static void kunit_log_test(struct kunit *test) -{ - struct kunit_suite suite; - - suite.log = kunit_kzalloc(test, KUNIT_LOG_SIZE, GFP_KERNEL); - KUNIT_ASSERT_NOT_ERR_OR_NULL(test, suite.log); - - kunit_log(KERN_INFO, test, "put this in log."); - kunit_log(KERN_INFO, test, "this too."); - kunit_log(KERN_INFO, &suite, "add to suite log."); - kunit_log(KERN_INFO, &suite, "along with this."); - -#ifdef CONFIG_KUNIT_DEBUGFS - KUNIT_EXPECT_NOT_ERR_OR_NULL(test, - strstr(test->log, "put this in log.")); - KUNIT_EXPECT_NOT_ERR_OR_NULL(test, - strstr(test->log, "this too.")); - KUNIT_EXPECT_NOT_ERR_OR_NULL(test, - strstr(suite.log, "add to suite log.")); - KUNIT_EXPECT_NOT_ERR_OR_NULL(test, - strstr(suite.log, "along with this.")); -#else - KUNIT_EXPECT_NULL(test, test->log); -#endif -} - -static void kunit_log_newline_test(struct kunit *test) -{ - kunit_info(test, "Add newline\n"); - if (test->log) { - KUNIT_ASSERT_NOT_NULL_MSG(test, strstr(test->log, "Add newline\n"), - "Missing log line, full log:\n%s", test->log); - KUNIT_EXPECT_NULL(test, strstr(test->log, "Add newline\n\n")); - } else { - kunit_skip(test, "only useful when debugfs is enabled"); - } -} - -static struct kunit_case kunit_log_test_cases[] = { - KUNIT_CASE(kunit_log_test), - KUNIT_CASE(kunit_log_newline_test), - {} -}; - -static struct kunit_suite kunit_log_test_suite = { - .name = "kunit-log-test", - .test_cases = kunit_log_test_cases, -}; - static void kunit_status_set_failure_test(struct kunit *test) { struct kunit fake; @@ -658,7 +609,6 @@ static struct kunit_suite kunit_current_test_suite = { }; kunit_test_suites(&kunit_try_catch_test_suite, &kunit_resource_test_suite, - &kunit_log_test_suite, &kunit_status_test_suite, - &kunit_current_test_suite); + &kunit_status_test_suite, &kunit_current_test_suite); MODULE_LICENSE("GPL v2"); diff --git a/lib/kunit/log-test.c b/lib/kunit/log-test.c new file mode 100644 index 000000000000..a93d87112fea --- /dev/null +++ b/lib/kunit/log-test.c @@ -0,0 +1,72 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KUnit test for logging. + * + * Based on code: + * Copyright (C) 2019, Google LLC. + * Author: Brendan Higgins + */ +#include + +#include "string-stream.h" + +static void kunit_log_test(struct kunit *test) +{ + struct kunit_suite suite; + char *full_log; + + suite.log = alloc_string_stream(test, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, suite.log); + string_stream_set_append_newlines(suite.log, true); + + kunit_log(KERN_INFO, test, "put this in log."); + kunit_log(KERN_INFO, test, "this too."); + kunit_log(KERN_INFO, &suite, "add to suite log."); + kunit_log(KERN_INFO, &suite, "along with this."); + +#ifdef CONFIG_KUNIT_DEBUGFS + KUNIT_EXPECT_TRUE(test, test->log->append_newlines); + + full_log = string_stream_get_string(test, test->log, GFP_KERNEL); + KUNIT_EXPECT_NOT_ERR_OR_NULL(test, + strstr(full_log, "put this in log.")); + KUNIT_EXPECT_NOT_ERR_OR_NULL(test, + strstr(full_log, "this too.")); + + full_log = string_stream_get_string(test, suite.log, GFP_KERNEL); + KUNIT_EXPECT_NOT_ERR_OR_NULL(test, + strstr(full_log, "add to suite log.")); + KUNIT_EXPECT_NOT_ERR_OR_NULL(test, + strstr(full_log, "along with this.")); +#else + KUNIT_EXPECT_NULL(test, test->log); +#endif +} + +static void kunit_log_newline_test(struct kunit *test) +{ + char *full_log; + + kunit_info(test, "Add newline\n"); + if (test->log) { + full_log = string_stream_get_string(test, test->log, GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL_MSG(test, strstr(full_log, "Add newline\n"), + "Missing log line, full log:\n%s", full_log); + KUNIT_EXPECT_NULL(test, strstr(full_log, "Add newline\n\n")); + } else { + kunit_skip(test, "only useful when debugfs is enabled"); + } +} + +static struct kunit_case kunit_log_test_cases[] = { + KUNIT_CASE(kunit_log_test), + KUNIT_CASE(kunit_log_newline_test), + {} +}; + +static struct kunit_suite kunit_log_test_suite = { + .name = "kunit-log-test", + .test_cases = kunit_log_test_cases, +}; + +kunit_test_suites(&kunit_log_test_suite); diff --git a/lib/kunit/test.c b/lib/kunit/test.c index 4b69d12dfc96..14e889e80129 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -109,51 +109,17 @@ static void kunit_print_test_stats(struct kunit *test, stats.total); } -/** - * kunit_log_newline() - Add newline to the end of log if one is not - * already present. - * @log: The log to add the newline to. - */ -static void kunit_log_newline(char *log) -{ - int log_len, len_left; - - log_len = strlen(log); - len_left = KUNIT_LOG_SIZE - log_len - 1; - - if (log_len > 0 && log[log_len - 1] != '\n') - strncat(log, "\n", len_left); -} - -/* - * Append formatted message to log, size of which is limited to - * KUNIT_LOG_SIZE bytes (including null terminating byte). - */ -void kunit_log_append(char *log, const char *fmt, ...) +/* Append formatted message to log. */ +void kunit_log_append(struct string_stream *log, const char *fmt, ...) { va_list args; - int len, log_len, len_left; if (!log) return; - log_len = strlen(log); - len_left = KUNIT_LOG_SIZE - log_len - 1; - if (len_left <= 0) - return; - - /* Evaluate length of line to add to log */ va_start(args, fmt); - len = vsnprintf(NULL, 0, fmt, args) + 1; + string_stream_vadd(log, fmt, args); va_end(args); - - /* Print formatted line to the log */ - va_start(args, fmt); - vsnprintf(log + log_len, min(len, len_left), fmt, args); - va_end(args); - - /* Add newline to end of log if not already present. */ - kunit_log_newline(log); } EXPORT_SYMBOL_GPL(kunit_log_append); @@ -359,14 +325,14 @@ void __kunit_do_failed_assertion(struct kunit *test, } EXPORT_SYMBOL_GPL(__kunit_do_failed_assertion); -void kunit_init_test(struct kunit *test, const char *name, char *log) +void kunit_init_test(struct kunit *test, const char *name, struct string_stream *log) { spin_lock_init(&test->lock); INIT_LIST_HEAD(&test->resources); test->name = name; test->log = log; if (test->log) - test->log[0] = '\0'; + string_stream_clear(log); test->status = KUNIT_SUCCESS; test->status_comment[0] = '\0'; } From patchwork Mon Aug 14 13:23:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Fitzgerald X-Patchwork-Id: 135432 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b824:0:b0:3f2:4152:657d with SMTP id z4csp2754462vqi; Mon, 14 Aug 2023 06:43:47 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHz4Y97bBBt+CNORr1lLDWnOvdFJpn5Fqlwj4VsjeDvWbKOq6estuST6CRZkgabE32LJ0v1 X-Received: by 2002:a05:6808:6396:b0:3a7:9837:7148 with SMTP id ec22-20020a056808639600b003a798377148mr8677915oib.58.1692020627226; Mon, 14 Aug 2023 06:43:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692020627; cv=none; d=google.com; s=arc-20160816; b=zMjoPTn7il7WI+10VfZbnkLyxWhkYtaBbLXSqPQTP5n7NTZLWUiA6DirAKfK85Ewpf 9cszXLI0ihr4fwtcALjvwL5hmYZaSnpO/yg0pM4eUoafA1MUdGLZoFlvk1JeHyPzdMVa mbX6aCVKNlgkAylBux/fP8IRpJZcfh9XJIKQlVLEaaIq5U/IApylKKVleppt5SM3tkB7 768U6uS+cnI4Mg/7BlwPIBjwxuNe7pvnEIqsihmp0jnCb8Axly8bbX7eSChHggDLFk+o ZmGjteQfd/WHYN7J4c/CxAPCaeEeAVN2jMM39Yon6nkGy6alMLp/sPrGlgUlCwBp3r8A r9iQ== 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=qUcroq0sql/GRbImJk7ytIZcccdHxxZwy9OPYA42Fng=; fh=bGF4ESczbIrmrNxFOdEMfzJNgTVkRsqDRt+LRN0CeSg=; b=YRHP4fkLYKVkwm3/A4r2fvwJUNKg/oDrPDyDQCHQkJjHEwlTfZv7ixlmyteFfjecp4 A/q1sTd17redGkZgyGL4lt+ymfRwEq58rx1c+fXtGVTrKX8GHVUvxyQk+9WJLjRHBNZJ 40dj1KF3Q9brLojotv4WiofNNT7vutpHDOANvy3SGzo6SPOoxtDXvLcWyWGdsUSYxCKI 8ulwCZXlOVjU7gbpuiiw1V6olKg9NJIKPFl7AfEm/Ib+mQHIwgP6yJRYWfw5d+XqVDKS KzrYRCFyxMoyFDcefk/tnV/2I/GKKxRN4LjDfM1GtSAEjuxLmZNRLgcegmAjHFsg30Mg Dffw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cirrus.com header.s=PODMain02222019 header.b=d3VmG24j; 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=REJECT sp=REJECT dis=NONE) header.from=cirrus.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ca37-20020a056a0206a500b00563e15edb8csi8806181pgb.360.2023.08.14.06.43.32; Mon, 14 Aug 2023 06:43:47 -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=@cirrus.com header.s=PODMain02222019 header.b=d3VmG24j; 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=REJECT sp=REJECT dis=NONE) header.from=cirrus.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231239AbjHNNYE (ORCPT + 99 others); Mon, 14 Aug 2023 09:24:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41978 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230494AbjHNNXj (ORCPT ); Mon, 14 Aug 2023 09:23:39 -0400 Received: from mx0b-001ae601.pphosted.com (mx0a-001ae601.pphosted.com [67.231.149.25]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 622F610C0; Mon, 14 Aug 2023 06:23:38 -0700 (PDT) Received: from pps.filterd (m0077473.ppops.net [127.0.0.1]) by mx0a-001ae601.pphosted.com (8.17.1.22/8.17.1.22) with ESMTP id 37E5t3EO003856; Mon, 14 Aug 2023 08:23:21 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cirrus.com; h= from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding:content-type; s= PODMain02222019; bh=qUcroq0sql/GRbImJk7ytIZcccdHxxZwy9OPYA42Fng=; b= d3VmG24jOMJaqUrRC6qp4dUbommpGhtT4+kdxtXyfercPkap+HeuRe/xVlbckRT6 Ilk/Nb/HAK1MMPewLyzrFVuJ9GpXagBoKAvhM2BRvn082JIezCtLEAJQlClXU7PY SQofO2zimheoZKLobnEY4ldDB+eeC1Z3xsQHivWd2nQeGtu0fN1+7tnwxiiE90Ck yRImeHC5PZy0qx6eqyFINY15iOSxOxytoHUWlnFgo0Ekl3b8DIKq/Wq17M058Wc+ KKr89YAxRc1/oSUc9Ozk16SfuYkm7iKgVDHJxwcpDBhf5DPEPexpqXORbFnP9Jcy OCiSQwoG5pjxqjKWm5ppyw== Received: from ediex02.ad.cirrus.com ([84.19.233.68]) by mx0a-001ae601.pphosted.com (PPS) with ESMTPS id 3se8kqt6x8-8 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 14 Aug 2023 08:23:20 -0500 (CDT) Received: from ediex01.ad.cirrus.com (198.61.84.80) by ediex02.ad.cirrus.com (198.61.84.81) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.30; Mon, 14 Aug 2023 14:23:16 +0100 Received: from ediswmail.ad.cirrus.com (198.61.86.93) by ediex01.ad.cirrus.com (198.61.84.80) with Microsoft SMTP Server id 15.2.1118.30 via Frontend Transport; Mon, 14 Aug 2023 14:23:16 +0100 Received: from EDIN4L06LR3.ad.cirrus.com (EDIN4L06LR3.ad.cirrus.com [198.61.65.68]) by ediswmail.ad.cirrus.com (Postfix) with ESMTP id 2258B3578; Mon, 14 Aug 2023 13:23:16 +0000 (UTC) From: Richard Fitzgerald To: , , CC: , , , , Richard Fitzgerald Subject: [PATCH v4 10/10] kunit: string-stream: Test performance of string_stream Date: Mon, 14 Aug 2023 14:23:09 +0100 Message-ID: <20230814132309.32641-11-rf@opensource.cirrus.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230814132309.32641-1-rf@opensource.cirrus.com> References: <20230814132309.32641-1-rf@opensource.cirrus.com> MIME-Version: 1.0 X-Proofpoint-ORIG-GUID: gF-U2VPMNOOpEKszEfhbqrqrTpbnDKz2 X-Proofpoint-GUID: gF-U2VPMNOOpEKszEfhbqrqrTpbnDKz2 X-Proofpoint-Spam-Reason: safe X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1774212220937659072 X-GMAIL-MSGID: 1774212220937659072 Add a test of the speed and memory use of string_stream. string_stream_performance_test() doesn't actually "test" anything (it cannot fail unless the system has run out of allocatable memory) but it measures the speed and memory consumption of the string_stream and reports the result. This allows changes in the string_stream implementation to be compared. Signed-off-by: Richard Fitzgerald --- lib/kunit/string-stream-test.c | 54 ++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/lib/kunit/string-stream-test.c b/lib/kunit/string-stream-test.c index 05bfade2bd8a..b55cc14f43fb 100644 --- a/lib/kunit/string-stream-test.c +++ b/lib/kunit/string-stream-test.c @@ -8,7 +8,9 @@ #include #include +#include #include +#include #include "string-stream.h" @@ -370,6 +372,57 @@ static void string_stream_auto_newline_test(struct kunit *test) "One\nTwo\nThree\nFour\nFive\nSix\nSeven\n\nEight\n"); } +/* + * This doesn't actually "test" anything. It reports time taken + * and memory used for logging a large number of lines. + */ +static void string_stream_performance_test(struct kunit *test) +{ + struct string_stream_fragment *frag_container; + struct string_stream *stream; + char test_line[101]; + ktime_t start_time, end_time; + size_t len, bytes_requested, actual_bytes_used, total_string_length; + int offset, i; + + stream = alloc_string_stream(test, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); + + memset(test_line, 'x', sizeof(test_line) - 1); + test_line[sizeof(test_line) - 1] = '\0'; + + start_time = ktime_get(); + for (i = 0; i < 10000; i++) { + offset = i % (sizeof(test_line) - 1); + string_stream_add(stream, "%s: %d\n", &test_line[offset], i); + } + end_time = ktime_get(); + + /* + * Calculate memory used. This doesn't include invisible + * overhead due to kernel allocator fragment size rounding. + */ + bytes_requested = sizeof(*stream); + actual_bytes_used = ksize(stream); + total_string_length = 0; + + list_for_each_entry(frag_container, &stream->fragments, node) { + bytes_requested += sizeof(*frag_container); + actual_bytes_used += ksize(frag_container); + + len = strlen(frag_container->fragment); + total_string_length += len; + bytes_requested += len + 1; /* +1 for '\0' */ + actual_bytes_used += ksize(frag_container->fragment); + } + + kunit_info(test, "Time elapsed: %lld us\n", + ktime_us_delta(end_time, start_time)); + kunit_info(test, "Total string length: %zu\n", total_string_length); + kunit_info(test, "Bytes requested: %zu\n", bytes_requested); + kunit_info(test, "Actual bytes allocated: %zu\n", actual_bytes_used); +} + static int string_stream_test_init(struct kunit *test) { struct string_stream_test_priv *priv; @@ -400,6 +453,7 @@ static struct kunit_case string_stream_test_cases[] = { KUNIT_CASE(string_stream_append_empty_string_test), KUNIT_CASE(string_stream_no_auto_newline_test), KUNIT_CASE(string_stream_auto_newline_test), + KUNIT_CASE(string_stream_performance_test), {} };