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.
- Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
kunit/static_stub.hkunit/test.hlinux/ktime.hlinux/prandom.hlinux/slab.hlinux/timekeeping.hstring-stream.h
Detected Declarations
struct string_stream_test_privfunction string_stream_managed_init_testfunction string_stream_unmanaged_init_testfunction string_stream_destroy_stubfunction string_stream_managed_free_testfunction string_stream_resource_free_testfunction string_stream_line_add_testfunction string_stream_variable_length_line_testfunction string_stream_append_testfunction string_stream_append_auto_newline_testfunction string_stream_append_empty_string_testfunction string_stream_no_auto_newline_testfunction string_stream_auto_newline_testfunction string_stream_performance_testfunction list_for_each_entryfunction string_stream_test_init
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
- Immediate include surface: `kunit/static_stub.h`, `kunit/test.h`, `linux/ktime.h`, `linux/prandom.h`, `linux/slab.h`, `linux/timekeeping.h`, `string-stream.h`.
- Detected declarations: `struct string_stream_test_priv`, `function string_stream_managed_init_test`, `function string_stream_unmanaged_init_test`, `function string_stream_destroy_stub`, `function string_stream_managed_free_test`, `function string_stream_resource_free_test`, `function string_stream_line_add_test`, `function string_stream_variable_length_line_test`, `function string_stream_append_test`, `function string_stream_append_auto_newline_test`.
- Atlas domain: Kernel Services / lib.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.