2023-08-09 16:30:32

by Richard Fitzgerald

[permalink] [raw]
Subject: [PATCH v3 4/7] kunit: kunit-test: Test logging a line that exactly fills a fragment

If a log string is the exact length of a log fragment buffer
kunit_log_append() should now exactly fill that fragment without
extending the log.

Signed-off-by: Richard Fitzgerald <[email protected]>
---
lib/kunit/kunit-test.c | 37 +++++++++++++++++++++++++++++++++++++
1 file changed, 37 insertions(+)

diff --git a/lib/kunit/kunit-test.c b/lib/kunit/kunit-test.c
index c0ee33a8031e..9ac81828d018 100644
--- a/lib/kunit/kunit-test.c
+++ b/lib/kunit/kunit-test.c
@@ -763,12 +763,49 @@ static void kunit_log_extend_test_2(struct kunit *test)
#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 = kunit_kzalloc(test, sizeof(*suite.log), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, suite.log);
+ INIT_LIST_HEAD(suite.log);
+ frag = kunit_kzalloc(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);
+
+ src = 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);
+
+ /* Logging another string should extend the log */
+ kunit_log_append(suite.log, "Next\n");
+ KUNIT_EXPECT_EQ(test, list_count_nodes(suite.log), 2);
+ frag = list_last_entry(suite.log, struct kunit_log_frag, list);
+ KUNIT_EXPECT_STREQ(test, frag->buf, "Next\n");
+#else
+ kunit_skip(test, "only useful when debugfs is enabled");
+#endif
+}
+
static struct kunit_case kunit_log_test_cases[] = {
KUNIT_CASE(kunit_log_init_frag_test),
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



2023-08-09 23:11:36

by Rae Moar

[permalink] [raw]
Subject: Re: [PATCH v3 4/7] kunit: kunit-test: Test logging a line that exactly fills a fragment

On Wed, Aug 9, 2023 at 11:54 AM Richard Fitzgerald
<[email protected]> wrote:
>
> If a log string is the exact length of a log fragment buffer
> kunit_log_append() should now exactly fill that fragment without
> extending the log.
>
> Signed-off-by: Richard Fitzgerald <[email protected]>

Hello!

This test looks good to me. I have tested it and it seems to be working well.

I appreciate all of the assert and expect statements. I do have one
comment below.

Although, I would be happy to set this as reviewed by me after that
comment is responded to.

Thanks!
-Rae

> ---
> lib/kunit/kunit-test.c | 37 +++++++++++++++++++++++++++++++++++++
> 1 file changed, 37 insertions(+)
>
> diff --git a/lib/kunit/kunit-test.c b/lib/kunit/kunit-test.c
> index c0ee33a8031e..9ac81828d018 100644
> --- a/lib/kunit/kunit-test.c
> +++ b/lib/kunit/kunit-test.c
> @@ -763,12 +763,49 @@ static void kunit_log_extend_test_2(struct kunit *test)
> #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 = kunit_kzalloc(test, sizeof(*suite.log), GFP_KERNEL);
> + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, suite.log);
> + INIT_LIST_HEAD(suite.log);
> + frag = kunit_kzalloc(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);
> +
> + src = 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);

Should this be an EXPECT instead? It doesn't seem like the test needs
to fail immediately if this fails. Let me know what you think.

> +
> + /* 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);
> +
> + /* Logging another string should extend the log */
> + kunit_log_append(suite.log, "Next\n");
> + KUNIT_EXPECT_EQ(test, list_count_nodes(suite.log), 2);
> + frag = list_last_entry(suite.log, struct kunit_log_frag, list);
> + KUNIT_EXPECT_STREQ(test, frag->buf, "Next\n");
> +#else
> + kunit_skip(test, "only useful when debugfs is enabled");
> +#endif
> +}
> +
> static struct kunit_case kunit_log_test_cases[] = {
> KUNIT_CASE(kunit_log_init_frag_test),
> 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
>

2023-08-10 15:49:58

by Richard Fitzgerald

[permalink] [raw]
Subject: Re: [PATCH v3 4/7] kunit: kunit-test: Test logging a line that exactly fills a fragment

On 9/8/23 22:22, Rae Moar wrote:
> On Wed, Aug 9, 2023 at 11:54 AM Richard Fitzgerald
> <[email protected]> wrote:
>>
>> If a log string is the exact length of a log fragment buffer
>> kunit_log_append() should now exactly fill that fragment without
>> extending the log.
>>
>> Signed-off-by: Richard Fitzgerald <[email protected]>
>
> Hello!
>
> This test looks good to me. I have tested it and it seems to be working well.
>
> I appreciate all of the assert and expect statements. I do have one
> comment below.
>
> Although, I would be happy to set this as reviewed by me after that
> comment is responded to.
>
> Thanks!
> -Rae
>
>> ---
>> lib/kunit/kunit-test.c | 37 +++++++++++++++++++++++++++++++++++++
>> 1 file changed, 37 insertions(+)
>>
>> diff --git a/lib/kunit/kunit-test.c b/lib/kunit/kunit-test.c
>> index c0ee33a8031e..9ac81828d018 100644
>> --- a/lib/kunit/kunit-test.c
>> +++ b/lib/kunit/kunit-test.c
>> @@ -763,12 +763,49 @@ static void kunit_log_extend_test_2(struct kunit *test)
>> #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 = kunit_kzalloc(test, sizeof(*suite.log), GFP_KERNEL);
>> + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, suite.log);
>> + INIT_LIST_HEAD(suite.log);
>> + frag = kunit_kzalloc(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);
>> +
>> + src = 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);
>
> Should this be an EXPECT instead? It doesn't seem like the test needs
> to fail immediately if this fails. Let me know what you think.

I think ASSERT is appropriate here. This isn't testing anything
(unless you don't trust memset). It's ensuring that the test data
I generate is what I expect otherwise the following testing is
invalid.

This is redundant because the first 3 lines must produce the expected
string, but I put it in to prove to myself that I can do math and
decided to leave it in.