public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Richard Fitzgerald <rf@opensource.cirrus.com>
To: <brendan.higgins@linux.dev>, <davidgow@google.com>, <rmoar@google.com>
Cc: <linux-kselftest@vger.kernel.org>, <kunit-dev@googlegroups.com>,
	<linux-kernel@vger.kernel.org>, <patches@opensource.cirrus.com>,
	Richard Fitzgerald <rf@opensource.cirrus.com>
Subject: [PATCH v3 5/7] kunit: kunit-test: Add test cases for logging very long lines
Date: Wed, 9 Aug 2023 16:54:36 +0100	[thread overview]
Message-ID: <20230809155438.22470-6-rf@opensource.cirrus.com> (raw)
In-Reply-To: <20230809155438.22470-1-rf@opensource.cirrus.com>

Add kunit_log_long_line_test() to test that logging a line longer than
the buffer fragment size doesn't truncate the line.

Add extra tests to kunit_log_newline_test() for lines longer than the
buffer fragment size.

Signed-off-by: Richard Fitzgerald <rf@opensource.cirrus.com>
---
 lib/kunit/kunit-test.c | 84 +++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 83 insertions(+), 1 deletion(-)

diff --git a/lib/kunit/kunit-test.c b/lib/kunit/kunit-test.c
index 9ac81828d018..c079550c3afd 100644
--- a/lib/kunit/kunit-test.c
+++ b/lib/kunit/kunit-test.c
@@ -609,7 +609,7 @@ static void kunit_log_newline_test(struct kunit *test)
 {
 	struct kunit_suite suite;
 	struct kunit_log_frag *frag;
-	char *p;
+	char *p, *line;
 
 	kunit_info(test, "Add newline\n");
 	if (test->log) {
@@ -635,6 +635,33 @@ static void kunit_log_newline_test(struct kunit *test)
 		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);
+		kunit_kfree(test, p);
+
+		/* String that is much longer than a fragment */
+		line = kunit_kzalloc(test, sizeof(frag->buf) * 6, GFP_KERNEL);
+		KUNIT_ASSERT_NOT_ERR_OR_NULL(test, line);
+		memset(line, 'x', (sizeof(frag->buf) * 6) - 1);
+		kunit_log_append(suite.log, "%s", line);
+		p = get_concatenated_log(test, suite.log, NULL);
+		KUNIT_ASSERT_NOT_ERR_OR_NULL(test, p);
+		KUNIT_EXPECT_EQ(test, p[strlen(p) - 1], '\n');
+		KUNIT_EXPECT_NULL(test, strstr(p, "\n\n"));
+		kunit_kfree(test, p);
+
+		kunit_log_append(suite.log, "%s\n", line);
+		p = get_concatenated_log(test, suite.log, NULL);
+		KUNIT_ASSERT_NOT_ERR_OR_NULL(test, p);
+		KUNIT_EXPECT_EQ(test, p[strlen(p) - 1], '\n');
+		KUNIT_EXPECT_NULL(test, strstr(p, "\n\n"));
+		kunit_kfree(test, p);
+
+		line[strlen(line) - 1] = '\n';
+		kunit_log_append(suite.log, "%s", line);
+		p = get_concatenated_log(test, suite.log, NULL);
+		KUNIT_ASSERT_NOT_ERR_OR_NULL(test, p);
+		KUNIT_EXPECT_EQ(test, p[strlen(p) - 1], '\n');
+		KUNIT_EXPECT_NULL(test, strstr(p, "\n\n"));
+		kunit_kfree(test, p);
 	} else {
 		kunit_skip(test, "only useful when debugfs is enabled");
 	}
@@ -799,6 +826,60 @@ static void kunit_log_frag_sized_line_test(struct kunit *test)
 #endif
 }
 
+static void kunit_log_long_line_test(struct kunit *test)
+{
+#ifdef CONFIG_KUNIT_DEBUGFS
+	struct kunit_suite suite;
+	struct kunit_log_frag *frag;
+	struct rnd_state rnd;
+	char *line, *p, *pn;
+	size_t line_buf_size, len;
+	int num_frags, i;
+
+	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_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);
+
+	/* Create a very long string to be logged */
+	line_buf_size = sizeof(frag->buf) * 6;
+	line = kunit_kmalloc(test, line_buf_size, GFP_KERNEL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, line);
+	line[0] = '\0';
+
+	prandom_seed_state(&rnd, 3141592653589793238ULL);
+	len = 0;
+	do {
+		static const char fill[] =
+			"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
+
+		i = prandom_u32_state(&rnd) % (sizeof(fill) - 1);
+		len = strlcat(line, &fill[i], line_buf_size);
+	} while (len < line_buf_size);
+
+	kunit_log_append(suite.log, "%s\n", line);
+
+	p = 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_frags:%d total len:%zu\n", num_frags, strlen(p));
+
+	/* Don't compare the trailing '\n' */
+	pn = strrchr(p, '\n');
+	KUNIT_EXPECT_NOT_ERR_OR_NULL(test, pn);
+	*pn = '\0';
+	KUNIT_EXPECT_EQ(test, strlen(p), strlen(line));
+	KUNIT_EXPECT_STREQ(test, p, line);
+#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),
@@ -806,6 +887,7 @@ static struct kunit_case kunit_log_test_cases[] = {
 	KUNIT_CASE(kunit_log_extend_test_1),
 	KUNIT_CASE(kunit_log_extend_test_2),
 	KUNIT_CASE(kunit_log_frag_sized_line_test),
+	KUNIT_CASE(kunit_log_long_line_test),
 	{}
 };
 
-- 
2.30.2


  parent reply	other threads:[~2023-08-09 15:55 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-09 15:54 [PATCH v3 0/7] kunit: Add dynamically-extending log Richard Fitzgerald
2023-08-09 15:54 ` [PATCH v3 1/7] kunit: Replace fixed-size log with dynamically-extending buffer Richard Fitzgerald
2023-08-09 21:10   ` Rae Moar
2023-08-10 14:00     ` Richard Fitzgerald
2023-08-09 15:54 ` [PATCH v3 2/7] kunit: kunit-test: Add test cases for extending log buffer Richard Fitzgerald
2023-08-09 21:10   ` Rae Moar
2023-08-10 14:18     ` Richard Fitzgerald
2023-08-09 15:54 ` [PATCH v3 3/7] kunit: Handle logging of lines longer than the fragment buffer size Richard Fitzgerald
2023-08-10 14:38   ` David Gow
2023-08-10 15:09     ` Richard Fitzgerald
2023-08-11  8:27       ` David Gow
2023-08-10 22:41     ` Rae Moar
2023-08-09 15:54 ` [PATCH v3 4/7] kunit: kunit-test: Test logging a line that exactly fills a fragment Richard Fitzgerald
2023-08-09 21:22   ` Rae Moar
2023-08-10 14:24     ` Richard Fitzgerald
2023-08-09 15:54 ` Richard Fitzgerald [this message]
2023-08-10 22:58   ` [PATCH v3 5/7] kunit: kunit-test: Add test cases for logging very long lines Rae Moar
2023-08-09 15:54 ` [PATCH v3 6/7] kunit: kunit-test: Add test of logging only a newline Richard Fitzgerald
2023-08-10 23:02   ` Rae Moar
2023-08-09 15:54 ` [PATCH v3 7/7] kunit: Don't waste first attempt to format string in kunit_log_append() Richard Fitzgerald
2023-08-10 23:53   ` Rae Moar
2023-08-11  8:27   ` David Gow

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230809155438.22470-6-rf@opensource.cirrus.com \
    --to=rf@opensource.cirrus.com \
    --cc=brendan.higgins@linux.dev \
    --cc=davidgow@google.com \
    --cc=kunit-dev@googlegroups.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=patches@opensource.cirrus.com \
    --cc=rmoar@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox