lib/kunit/string-stream-test.c

Source file repositories/reference/linux-study-clean/lib/kunit/string-stream-test.c

File Facts

System
Linux kernel
Corpus path
lib/kunit/string-stream-test.c
Extension
.c
Size
17641 bytes
Lines
538
Domain
Kernel Services
Bucket
lib
Inferred role
Kernel Services: implementation source
Status
source implementation candidate

Why This File Exists

Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.

Dependency Surface

Detected Declarations

Annotated Snippet

struct string_stream_test_priv {
	/* For testing resource-managed free. */
	struct string_stream *expected_free_stream;
	bool stream_was_freed;
	bool stream_free_again;
};

/* Avoids a cast warning if kfree() is passed direct to kunit_add_action(). */
KUNIT_DEFINE_ACTION_WRAPPER(kfree_wrapper, kfree, const void *);

/* Avoids a cast warning if string_stream_destroy() is passed direct to kunit_add_action(). */
KUNIT_DEFINE_ACTION_WRAPPER(cleanup_raw_stream, string_stream_destroy, struct string_stream *);

static char *get_concatenated_string(struct kunit *test, struct string_stream *stream)
{
	char *str = string_stream_get_string(stream);

	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, str);
	kunit_add_action(test, kfree_wrapper, (void *)str);

	return str;
}

/* Managed string_stream object is initialized correctly. */
static void string_stream_managed_init_test(struct kunit *test)
{
	struct string_stream *stream;

	/* Resource-managed initialization. */
	stream = kunit_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_TRUE(test, (stream->gfp == GFP_KERNEL));
	KUNIT_EXPECT_FALSE(test, stream->append_newlines);
	KUNIT_EXPECT_TRUE(test, string_stream_is_empty(stream));
}

/* Unmanaged string_stream object is initialized correctly. */
static void string_stream_unmanaged_init_test(struct kunit *test)
{
	struct string_stream *stream;

	stream = alloc_string_stream(GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream);
	kunit_add_action(test, cleanup_raw_stream, stream);

	KUNIT_EXPECT_EQ(test, stream->length, 0);
	KUNIT_EXPECT_TRUE(test, list_empty(&stream->fragments));
	KUNIT_EXPECT_TRUE(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_destroy_stub(struct string_stream *stream)
{
	struct kunit *fake_test = kunit_get_current_test();
	struct string_stream_test_priv *priv = fake_test->priv;

	/* The kunit could own string_streams other than the one we are testing. */
	if (stream == priv->expected_free_stream) {
		if (priv->stream_was_freed)
			priv->stream_free_again = true;
		else
			priv->stream_was_freed = true;
	}

	/*
	 * Calling string_stream_destroy() will only call this function again
	 * because the redirection stub is still active.
	 * Avoid calling deactivate_static_stub() or changing current->kunit_test
	 * during cleanup.
	 */
	string_stream_clear(stream);
	kfree(stream);
}

/* kunit_free_string_stream() calls string_stream_desrtoy() */
static void string_stream_managed_free_test(struct kunit *test)
{
	struct string_stream_test_priv *priv = test->priv;

	priv->expected_free_stream = NULL;
	priv->stream_was_freed = false;
	priv->stream_free_again = false;

	kunit_activate_static_stub(test,
				   string_stream_destroy,

Annotation

Implementation Notes