Received: by 2002:a05:6359:6284:b0:131:369:b2a3 with SMTP id se4csp4842613rwb; Tue, 8 Aug 2023 15:04:11 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEp1SWNHhJ756Xav6cyhiaNQd1LGotK47bHl9LdYZ5887E6MFmzElMLOIFJ8fx4q7EZ6iJG X-Received: by 2002:a17:902:c950:b0:1bc:6c8:cded with SMTP id i16-20020a170902c95000b001bc06c8cdedmr1146600pla.67.1691532251379; Tue, 08 Aug 2023 15:04:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691532251; cv=none; d=google.com; s=arc-20160816; b=ZuVwP/84foDyUgzIY4U2XiwokyCxM++anfwqE9/b4DCkJAKoYgbSRWXRrqDHWflCCf v6LmFSRDJxByM0DQ7ILtou6APb2G2s/oTHXMtLafUxNL5KNWPtOdyDjepPa05F0a+bWI ojS4yirQ3FI/LVUySLsKPgndNDu0zRBy6jqPTcJwmF1JyjjsQ5J/mRkIEuZcA0yyRsAm FzG7m74Ivtii3YYdWsm5V6XGGoHxbYEE4etXaiw3a11Ggifvzc2R5NMsqiKMskwJTPlH 8SBvqFn+evu9VobPHRcCkCq8F3ztaY2Hykv0DkrDOdD6wtiRTi7QauzBfTKtayd6T1Po 4XYw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=GBdvRieiB8dFFT8uavZWsfJqKc+2lRPExOCUtNLgaBw=; fh=8UDfi8xBJ790SZyvmkQ8i9XlHyHv9fuRECSK+7qBwQM=; b=lrTKfPTykmpODoGiFh/3dEgTmE7yZYhmGyqgsk/h2eqkz+c3PwxjmEzAG1vlAZI3w9 ERmSborrUjP/wuk2AbmoTF6n2f8T/l+HdH+xernj0r9qPZx0jITqT0pwWqyWOjbBfIxc tDmUH8fOONMZEFoSrUQdo6qHW36pkAVrEPj5mhjIk9Dkk5kOIbM4+6dd8mfRrJnUSs+n P4r0b8PgV+3Qv31ERYUPdUApm7ohAoxnAH9OF0m6fqhAa0l/vhAe1bPPHKYcwlmy2JHy 41Fg6fY40KGdpyLKpfOG7XYK3U4YTz/GHMWbezQodaRbIsN19LmWE/m3GC8TBy75hg1H SxBA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=Mgpa25MN; 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=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id kx11-20020a170902f94b00b001b9e82a6beesi7870211plb.548.2023.08.08.15.03.58; Tue, 08 Aug 2023 15:04:11 -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=@google.com header.s=20221208 header.b=Mgpa25MN; 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=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236617AbjHHVSx (ORCPT + 99 others); Tue, 8 Aug 2023 17:18:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48918 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236190AbjHHVSo (ORCPT ); Tue, 8 Aug 2023 17:18:44 -0400 Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0F7BE272B for ; Tue, 8 Aug 2023 14:16:24 -0700 (PDT) Received: by mail-wm1-x329.google.com with SMTP id 5b1f17b1804b1-3fe2d620d17so10425e9.0 for ; Tue, 08 Aug 2023 14:16:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1691529382; x=1692134182; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=GBdvRieiB8dFFT8uavZWsfJqKc+2lRPExOCUtNLgaBw=; b=Mgpa25MNfI0MQEyzTW2SGw5hCallSXKKOBEgRFwKQ9K8BpRfTQjMGiH4zgKV1UzotL pes26JYXGS3kBm7+gHLNEaErV1uM5T6vke19Y0a6mCKBzSUw0tE5sVqIVf9LOtYxd5Fd QaVT02QLqic9i+hEVSL5R01EBVpru9BqWdJKuZciKx9Z4cI7VElKR7Lxv2q3ZWqtHNGk NrXxZcRT78Mi/lkQxYd+iFcWeAgbh1h8mJZFiazrLUhO+Al1aWZNkW7MT45H5IPrQRE2 upNoWAHDgWiP1C6iAMdUW+ijN7Cmj+0F6+SZaeLn+D8NBkoSyglo3pqKGAeQLgbvkJG5 dJWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691529382; x=1692134182; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=GBdvRieiB8dFFT8uavZWsfJqKc+2lRPExOCUtNLgaBw=; b=g5qxI8XusDh8umiOeMAQ4lleFyeYFbnxzm1fVE1RV/WHCOke9nI+jitufAs9h9l9TM q1gzMpSMs5bFc0g2b+Hc3tjMf0U/BWSowSR+q+aEu9aZZ9TZw5P/PedlMVeeAZljPaQh vu3C+Lmqh/NG48cj6uIOb8DOc9XQHMmaZUbNa6pWMlH46kdNxnv875/ckEmcLAawIb4p a2GWlTaMDtde7TjppkqGDhaWap2YKbRG2kXKXEviq4m4zu3abAZPt0itvIqtgvDCcU/f 3bUzkw0aocmA41URvoh0qv3MW3qvsh0Lr2mDjKgDaaI+/HCiDtxzB79tfvamQsUhbrME 1OaA== X-Gm-Message-State: AOJu0YxBLribIpxWcknxCyIb10xZPWAC2KcOVqigbWtTIx+FNbkJ9pOF VJgUjXrmI5OsR4cUMiR3htEosaxQb+o+MK4nxfefnw== X-Received: by 2002:a05:600c:1c1f:b0:3f4:fb7:48d4 with SMTP id j31-20020a05600c1c1f00b003f40fb748d4mr2180wms.3.1691529382353; Tue, 08 Aug 2023 14:16:22 -0700 (PDT) MIME-Version: 1.0 References: <20230808123529.4725-1-rf@opensource.cirrus.com> <20230808123529.4725-3-rf@opensource.cirrus.com> In-Reply-To: <20230808123529.4725-3-rf@opensource.cirrus.com> From: Rae Moar Date: Tue, 8 Aug 2023 17:16:11 -0400 Message-ID: Subject: Re: [PATCH v2 2/6] kunit: kunit-test: Add test cases for extending log buffer To: Richard Fitzgerald Cc: brendan.higgins@linux.dev, davidgow@google.com, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, patches@opensource.cirrus.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL 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 On Tue, Aug 8, 2023 at 8:35=E2=80=AFAM Richard Fitzgerald wrote: > > Add test cases for the dynamically-extending log buffer. > > kunit_log_extend_test_1() logs a series of numbered lines then tests > that the resulting log contains all the lines. > > kunit_log_extend_test_2() logs a large number of lines of varying length > to create many fragments, then tests that all lines are present. > > kunit_log_frag_sized_line_test() logs a line that exactly fills a > fragment. This should not cause an extension of the log or truncation > of the line. > > kunit_log_newline_test() has a new test to append a line that is exactly > the length of the available space in the current fragment and check that > the resulting log has a trailing '\n'. > > Signed-off-by: Richard Fitzgerald Hello! I am happy to see so many tests in this patch series. I've been working with these patches and the debugfs logs seem to be working well. However, when I ran the new kunit-log-test tests three of the tests failed: kunit_log_extend_test_1(), kunit_log_extend_test_2(), and kunit_log_newline_test(). The diagnostic info for kunit_log_extend_test_1() reports: [20:55:27] # kunit_log_extend_test_1: EXPECTATION FAILED at lib/kunit/kunit-test.c:705 [20:55:27] Expected p =3D=3D line, but [20:55:27] p =3D=3D "xxxxxx=E2=80=A6xxxx12345678" [20:55:27] line =3D=3D "The quick brown fox jumps over the lazy penguin= 0" =E2=80=A6 [20:55:27] # kunit_log_extend_test_1: EXPECTATION FAILED at lib/kunit/kunit-test.c:705 [20:55:27] Expected p =3D=3D line, but [20:55:27] p =3D=3D "The quick brown fox jumps over the lazy penguin 1" [20:55:27] line =3D=3D "The quick brown fox jumps over the lazy penguin= 4" [20:55:27] # kunit_log_extend_test_1: EXPECTATION FAILED at lib/kunit/kunit-test.c:705 [20:55:27] Expected p =3D=3D line, but [20:55:27] p =3D=3D "The quick brown fox jumps over the lazy penguin 2" [20:55:27] line =3D=3D "The quick brown fox jumps over the lazy penguin= 5" =E2=80=A6 [20:55:27] # kunit_log_extend_test_1: EXPECTATION FAILED at lib/kunit/kunit-test.c:709 [20:55:27] Expected i =3D=3D num_lines, but [20:55:27] i =3D=3D 64 (0x40) [20:55:27] num_lines =3D=3D 141 (0x8d) So it looks like the log contains a different number of lines than expected which is causing the difference of 3 between expected and what was obtained. Potentially the log is not getting cleared/freed properly in between test cases? The diagnostic info for kunit_log_extend_test_2() reports: [20:55:27] # kunit_log_extend_test_2: EXPECTATION FAILED at lib/kunit/kunit-test.c:776 [20:55:27] Expected p =3D=3D &line[i], but [20:55:27] p =3D=3D "xxxxx...xxxxx123456780123456789abcdef101112131415161718191a1b1c1d1e1f20212= 2232425262728292a2b2c2d2e2f30313233343536373839" [20:55:27] &line[i] =3D=3D "0123456789abcdef101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c= 2d2e2f30313233343536373839" [20:55:27] # kunit_log_extend_test_2: EXPECTATION FAILED at lib/kunit/kunit-test.c:781 [20:55:27] Expected n =3D=3D num_lines, but [20:55:27] n =3D=3D 147 (0x93) [20:55:27] num_lines =3D=3D 155 (0x9b) [20:55:27] Not enough lines. Similar difference in the number of lines here. The diagnostic info for kunit_log_newline_test() reports that the test fails on this line: KUNIT_EXPECT_EQ(test, p[strlen(p) - 1], '\n'); Let me know if you are seeing similar errors. I can post the full log if that would be helpful. -Rae > --- > lib/kunit/kunit-test.c | 204 +++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 204 insertions(+) > > diff --git a/lib/kunit/kunit-test.c b/lib/kunit/kunit-test.c > index 54dc011c8980..48967d12e053 100644 > --- a/lib/kunit/kunit-test.c > +++ b/lib/kunit/kunit-test.c > @@ -7,6 +7,7 @@ > */ > #include > #include > +#include > > #include "try-catch-impl.h" > > @@ -530,6 +531,31 @@ static struct kunit_suite kunit_resource_test_suite = =3D { > .test_cases =3D kunit_resource_test_cases, > }; > > +static char *get_concatenated_log(struct kunit *test, const struct list_= head *log, > + int *num_frags) > +{ > + struct kunit_log_frag *frag; > + size_t len =3D 0; > + int frag_count =3D 0; > + char *p; > + > + list_for_each_entry(frag, log, list) > + len +=3D strlen(frag->buf); > + > + len++; /* for terminating '\0' */ > + p =3D kunit_kmalloc(test, len, GFP_KERNEL); > + > + list_for_each_entry(frag, log, list) { > + strlcat(p, frag->buf, len); > + ++frag_count; > + } > + > + if (num_frags) > + *num_frags =3D frag_count; > + > + return p; > +} > + > static void kunit_log_test(struct kunit *test) > { > struct kunit_suite suite; > @@ -568,7 +594,9 @@ static void kunit_log_test(struct kunit *test) > > static void kunit_log_newline_test(struct kunit *test) > { > + struct kunit_suite suite; > struct kunit_log_frag *frag; > + char *p; > > kunit_info(test, "Add newline\n"); > if (test->log) { > @@ -576,14 +604,190 @@ static void kunit_log_newline_test(struct kunit *t= est) > KUNIT_ASSERT_NOT_NULL_MSG(test, strstr(frag->buf, "Add ne= wline\n"), > "Missing log line, full log:\n%s", frag->buf); > KUNIT_EXPECT_NULL(test, strstr(frag->buf, "Add newline\n\= n")); > + > + suite.log =3D kunit_kzalloc(test, sizeof(*suite.log), GFP= _KERNEL); > + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, suite.log); > + INIT_LIST_HEAD(suite.log); > + frag =3D kunit_kmalloc(test, sizeof(*frag), GFP_KERNEL); > + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, frag); > + kunit_init_log_frag(frag); > + list_add_tail(&frag->list, suite.log); > + > + /* String that exactly fills fragment leaving no room for= \n */ > + memset(frag->buf, 0, sizeof(frag->buf)); > + memset(frag->buf, 'x', sizeof(frag->buf) - 9); > + kunit_log_append(suite.log, "12345678"); > + p =3D get_concatenated_log(test, suite.log, NULL); > + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, p); > + KUNIT_EXPECT_NOT_NULL_MSG(test, strstr(p, "x12345678\n"), > + "Newline not appended when fragment is full. Log = is:\n'%s'", p); > } else { > kunit_skip(test, "only useful when debugfs is enabled"); > } > } > > +static void kunit_log_extend_test_1(struct kunit *test) > +{ > +#ifdef CONFIG_KUNIT_DEBUGFS > + struct kunit_suite suite; > + struct kunit_log_frag *frag; > + char line[60]; > + char *p, *pn; > + size_t len, n; > + int num_lines, num_frags, i; > + > + suite.log =3D kunit_kzalloc(test, sizeof(*suite.log), GFP_KERNEL)= ; > + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, suite.log); > + INIT_LIST_HEAD(suite.log); > + frag =3D kunit_kmalloc(test, sizeof(*frag), GFP_KERNEL); > + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, frag); > + kunit_init_log_frag(frag); > + KUNIT_EXPECT_EQ(test, frag->buf[0], '\0'); > + list_add_tail(&frag->list, suite.log); > + > + i =3D 0; > + len =3D 0; > + do { > + n =3D snprintf(line, sizeof(line), > + "The quick brown fox jumps over the lazy pen= guin %d\n", i); > + KUNIT_ASSERT_LT(test, n, sizeof(line)); > + kunit_log_append(suite.log, line); > + ++i; > + len +=3D n; > + } while (len < (sizeof(frag->buf) * 30)); > + num_lines =3D i; > + > + p =3D get_concatenated_log(test, suite.log, &num_frags); > + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, p); > + KUNIT_EXPECT_GT(test, num_frags, 1); > + > + kunit_info(test, "num lines:%d num_frags:%d total len:%zu\n", > + num_lines, num_frags, strlen(p)); > + > + i =3D 0; > + while ((pn =3D strchr(p, '\n')) !=3D NULL) { > + *pn =3D '\0'; > + snprintf(line, sizeof(line), > + "The quick brown fox jumps over the lazy penguin= %d", i); > + KUNIT_EXPECT_STREQ(test, p, line); > + p =3D pn + 1; > + ++i; > + } > + KUNIT_EXPECT_EQ(test, i, num_lines); > +#else > + kunit_skip(test, "only useful when debugfs is enabled"); > +#endif > +} > + > +static void kunit_log_extend_test_2(struct kunit *test) > +{ > +#ifdef CONFIG_KUNIT_DEBUGFS > + struct kunit_suite suite; > + struct kunit_log_frag *frag; > + struct rnd_state rnd; > + char line[101]; > + char *p, *pn; > + size_t len; > + int num_lines, num_frags, n, i; > + > + suite.log =3D kunit_kzalloc(test, sizeof(*suite.log), GFP_KERNEL)= ; > + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, suite.log); > + INIT_LIST_HEAD(suite.log); > + frag =3D kunit_kmalloc(test, sizeof(*frag), GFP_KERNEL); > + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, frag); > + kunit_init_log_frag(frag); > + KUNIT_EXPECT_EQ(test, frag->buf[0], '\0'); > + list_add_tail(&frag->list, suite.log); > + > + /* Build log line of varying content */ > + line[0] =3D '\0'; > + i =3D 0; > + do { > + char tmp[9]; > + > + snprintf(tmp, sizeof(tmp), "%x", i++); > + len =3D strlcat(line, tmp, sizeof(line)); > + } while (len < sizeof(line) - 1); > + > + /* > + * Log lines of different lengths until we have created > + * many fragments. > + * The "randomness" must be repeatable. > + */ > + prandom_seed_state(&rnd, 3141592653589793238ULL); > + i =3D 0; > + len =3D 0; > + num_lines =3D 0; > + do { > + kunit_log_append(suite.log, "%s\n", &line[i]); > + len +=3D sizeof(line) - i; > + num_lines++; > + i =3D prandom_u32_state(&rnd) % (sizeof(line) - 1); > + } while (len < (sizeof(frag->buf) * 30)); > + > + /* There must be more than one buffer fragment now */ > + KUNIT_EXPECT_FALSE(test, list_is_singular(suite.log)); > + > + p =3D get_concatenated_log(test, suite.log, &num_frags); > + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, p); > + KUNIT_EXPECT_GT(test, num_frags, 1); > + > + kunit_info(test, "num lines:%d num_frags:%d total len:%zu\n", > + num_lines, num_frags, strlen(p)); > + > + prandom_seed_state(&rnd, 3141592653589793238ULL); > + i =3D 0; > + n =3D 0; > + while ((pn =3D strchr(p, '\n')) !=3D NULL) { > + *pn =3D '\0'; > + KUNIT_EXPECT_STREQ(test, p, &line[i]); > + p =3D pn + 1; > + n++; > + i =3D prandom_u32_state(&rnd) % (sizeof(line) - 1); > + } > + KUNIT_EXPECT_EQ_MSG(test, n, num_lines, "Not enough lines."); > +#else > + kunit_skip(test, "only useful when debugfs is enabled"); > +#endif > +} > + > +static void kunit_log_frag_sized_line_test(struct kunit *test) > +{ > +#ifdef CONFIG_KUNIT_DEBUGFS > + struct kunit_suite suite; > + struct kunit_log_frag *frag, *src; > + > + suite.log =3D kunit_kzalloc(test, sizeof(*suite.log), GFP_KERNEL)= ; > + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, suite.log); > + INIT_LIST_HEAD(suite.log); > + frag =3D kunit_kmalloc(test, sizeof(*frag), GFP_KERNEL); > + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, frag); > + kunit_init_log_frag(frag); > + KUNIT_EXPECT_EQ(test, frag->buf[0], '\0'); > + list_add_tail(&frag->list, suite.log); > + > + src =3D kunit_kzalloc(test, sizeof(*src), GFP_KERNEL); > + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, src); > + memset(src->buf, 'x', sizeof(src->buf) - 2); > + KUNIT_ASSERT_EQ(test, strlen(src->buf), sizeof(src->buf) - 2); > + > + /* Log a string that exactly fills the fragment */ > + kunit_log_append(suite.log, "%s\n", src->buf); > + KUNIT_EXPECT_TRUE(test, list_is_singular(suite.log)); > + KUNIT_EXPECT_EQ(test, strlen(frag->buf), sizeof(frag->buf) - 1); > + strlcat(src->buf, "\n", sizeof(src->buf)); > + KUNIT_EXPECT_STREQ(test, frag->buf, src->buf); > +#else > + kunit_skip(test, "only useful when debugfs is enabled"); > +#endif > +} > + > static struct kunit_case kunit_log_test_cases[] =3D { > KUNIT_CASE(kunit_log_test), > KUNIT_CASE(kunit_log_newline_test), > + KUNIT_CASE(kunit_log_extend_test_1), > + KUNIT_CASE(kunit_log_extend_test_2), > + KUNIT_CASE(kunit_log_frag_sized_line_test), > {} > }; > > -- > 2.30.2 >