lib/tests/string_helpers_kunit.c
Source file repositories/reference/linux-study-clean/lib/tests/string_helpers_kunit.c
File Facts
- System
- Linux kernel
- Corpus path
lib/tests/string_helpers_kunit.c- Extension
.c- Size
- 16435 bytes
- Lines
- 641
- 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/test.hlinux/array_size.hlinux/bug.hlinux/limits.hlinux/module.hlinux/random.hlinux/slab.hlinux/sprintf.hlinux/string_helpers.hlinux/types.h
Detected Declarations
struct test_stringstruct test_string_1struct test_string_2function test_string_check_buffunction test_string_unescapefunction test_string_escape_overflowfunction test_string_escapefunction test_string_get_size_checkfunction __strchrcutfunction __test_string_get_size_onefunction __test_string_get_sizefunction test_get_sizefunction test_upper_lowerfunction test_unescapefunction test_escape
Annotated Snippet
struct test_string {
const char *in;
const char *out;
unsigned int flags;
};
static const struct test_string strings[] = {
{
.in = "\\f\\ \\n\\r\\t\\v",
.out = "\f\\ \n\r\t\v",
.flags = UNESCAPE_SPACE,
},
{
.in = "\\40\\1\\387\\0064\\05\\040\\8a\\110\\777",
.out = " \001\00387\0064\005 \\8aH?7",
.flags = UNESCAPE_OCTAL,
},
{
.in = "\\xv\\xa\\x2c\\xD\\x6f2",
.out = "\\xv\n,\ro2",
.flags = UNESCAPE_HEX,
},
{
.in = "\\h\\\\\\\"\\a\\e\\",
.out = "\\h\\\"\a\e\\",
.flags = UNESCAPE_SPECIAL,
},
};
static void test_string_unescape(struct kunit *test,
const char *name, unsigned int flags,
bool inplace)
{
int q_real = 256;
char *in = kunit_kzalloc(test, q_real, GFP_KERNEL);
char *out_test = kunit_kzalloc(test, q_real, GFP_KERNEL);
char *out_real = kunit_kzalloc(test, q_real, GFP_KERNEL);
int i, p = 0, q_test = 0;
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, in);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, out_test);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, out_real);
for (i = 0; i < ARRAY_SIZE(strings); i++) {
const char *s = strings[i].in;
int len = strlen(strings[i].in);
/* Copy string to in buffer */
memcpy(&in[p], s, len);
p += len;
/* Copy expected result for given flags */
if (flags & strings[i].flags) {
s = strings[i].out;
len = strlen(strings[i].out);
}
memcpy(&out_test[q_test], s, len);
q_test += len;
}
in[p++] = '\0';
/* Call string_unescape and compare result */
if (inplace) {
memcpy(out_real, in, p);
if (flags == UNESCAPE_ANY)
q_real = string_unescape_any_inplace(out_real);
else
q_real = string_unescape_inplace(out_real, flags);
} else if (flags == UNESCAPE_ANY) {
q_real = string_unescape_any(in, out_real, q_real);
} else {
q_real = string_unescape(in, out_real, q_real, flags);
}
test_string_check_buf(test, name, flags, in, p - 1, out_real, q_real,
out_test, q_test);
}
struct test_string_1 {
const char *out;
unsigned int flags;
};
#define TEST_STRING_2_MAX_S1 32
struct test_string_2 {
const char *in;
struct test_string_1 s1[TEST_STRING_2_MAX_S1];
};
#define TEST_STRING_2_DICT_0 NULL
Annotation
- Immediate include surface: `kunit/test.h`, `linux/array_size.h`, `linux/bug.h`, `linux/limits.h`, `linux/module.h`, `linux/random.h`, `linux/slab.h`, `linux/sprintf.h`.
- Detected declarations: `struct test_string`, `struct test_string_1`, `struct test_string_2`, `function test_string_check_buf`, `function test_string_unescape`, `function test_string_escape_overflow`, `function test_string_escape`, `function test_string_get_size_check`, `function __strchrcut`, `function __test_string_get_size_one`.
- 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.