fs/hfsplus/unicode_test.c

Source file repositories/reference/linux-study-clean/fs/hfsplus/unicode_test.c

File Facts

System
Linux kernel
Corpus path
fs/hfsplus/unicode_test.c
Extension
.c
Size
47696 bytes
Lines
1597
Domain
Core OS
Bucket
VFS And Filesystem Core
Inferred role
Core OS: implementation source
Status
source implementation candidate

Why This File Exists

Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.

Dependency Surface

Detected Declarations

Annotated Snippet

struct test_mock_string_env {
	struct hfsplus_unistr str1;
	struct hfsplus_unistr str2;
	char *buf;
	u32 buf_size;
};

static struct test_mock_string_env *setup_mock_str_env(u32 buf_size)
{
	struct test_mock_string_env *env;

	env = kzalloc_obj(struct test_mock_string_env);
	if (!env)
		return NULL;

	env->buf = kzalloc(buf_size, GFP_KERNEL);
	if (!env->buf) {
		kfree(env);
		return NULL;
	}

	env->buf_size = buf_size;

	return env;
}

static void free_mock_str_env(struct test_mock_string_env *env)
{
	if (env->buf)
		kfree(env->buf);
	kfree(env);
}

/* Helper function to create hfsplus_unistr */
static void create_unistr(struct hfsplus_unistr *ustr, const char *ascii_str)
{
	int len = strlen(ascii_str);
	int i;

	memset(ustr->unicode, 0, sizeof(ustr->unicode));

	ustr->length = cpu_to_be16(len);
	for (i = 0; i < len && i < HFSPLUS_MAX_STRLEN; i++)
		ustr->unicode[i] = cpu_to_be16((u16)ascii_str[i]);
}

static void corrupt_unistr(struct hfsplus_unistr *ustr)
{
	ustr->length = cpu_to_be16(U16_MAX);
}

/* Test hfsplus_strcasecmp function */
static void hfsplus_strcasecmp_test(struct kunit *test)
{
	struct test_mock_string_env *mock_env;

	mock_env = setup_mock_str_env(HFSPLUS_MAX_STRLEN + 1);
	KUNIT_ASSERT_NOT_NULL(test, mock_env);

	/* Test identical strings */
	create_unistr(&mock_env->str1, "hello");
	create_unistr(&mock_env->str2, "hello");
	KUNIT_EXPECT_EQ(test, 0, hfsplus_strcasecmp(&mock_env->str1,
						    &mock_env->str2));

	/* Test case insensitive comparison */
	create_unistr(&mock_env->str1, "Hello");
	create_unistr(&mock_env->str2, "hello");
	KUNIT_EXPECT_EQ(test, 0, hfsplus_strcasecmp(&mock_env->str1,
						    &mock_env->str2));

	create_unistr(&mock_env->str1, "HELLO");
	create_unistr(&mock_env->str2, "hello");
	KUNIT_EXPECT_EQ(test, 0, hfsplus_strcasecmp(&mock_env->str1,
						    &mock_env->str2));

	/* Test different strings */
	create_unistr(&mock_env->str1, "apple");
	create_unistr(&mock_env->str2, "banana");
	KUNIT_EXPECT_LT(test, hfsplus_strcasecmp(&mock_env->str1,
						 &mock_env->str2), 0);

	create_unistr(&mock_env->str1, "zebra");
	create_unistr(&mock_env->str2, "apple");
	KUNIT_EXPECT_GT(test, hfsplus_strcasecmp(&mock_env->str1,
						 &mock_env->str2), 0);

	/* Test different lengths */
	create_unistr(&mock_env->str1, "test");
	create_unistr(&mock_env->str2, "testing");

Annotation

Implementation Notes