lib/tests/base64_kunit.c

Source file repositories/reference/linux-study-clean/lib/tests/base64_kunit.c

File Facts

System
Linux kernel
Corpus path
lib/tests/base64_kunit.c
Extension
.c
Size
10616 bytes
Lines
295
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

// SPDX-License-Identifier: GPL-2.0
/*
 * base64_kunit_test.c - KUnit tests for base64 encoding and decoding functions
 *
 * Copyright (c) 2025, Guan-Chun Wu <409411716@gms.tku.edu.tw>
 */

#include <kunit/test.h>
#include <linux/base64.h>

/* ---------- Benchmark helpers ---------- */
static u64 bench_encode_ns(const u8 *data, int len, char *dst, int reps,
			   enum base64_variant variant)
{
	u64 t0, t1;

	t0 = ktime_get_ns();
	for (int i = 0; i < reps; i++)
		base64_encode(data, len, dst, true, variant);
	t1 = ktime_get_ns();

	return div64_u64(t1 - t0, (u64)reps);
}

static u64 bench_decode_ns(const char *data, int len, u8 *dst, int reps,
			   enum base64_variant variant)
{
	u64 t0, t1;

	t0 = ktime_get_ns();
	for (int i = 0; i < reps; i++)
		base64_decode(data, len, dst, true, variant);
	t1 = ktime_get_ns();

	return div64_u64(t1 - t0, (u64)reps);
}

static void run_perf_and_check(struct kunit *test, const char *label, int size,
			       enum base64_variant variant)
{
	const int reps = 1000;
	size_t outlen = DIV_ROUND_UP(size, 3) * 4;
	u8 *in = kmalloc(size, GFP_KERNEL);
	char *enc = kmalloc(outlen, GFP_KERNEL);
	u8 *decoded = kmalloc(size, GFP_KERNEL);

	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, in);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, enc);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, decoded);

	get_random_bytes(in, size);
	int enc_len = base64_encode(in, size, enc, true, variant);
	int dec_len = base64_decode(enc, enc_len, decoded, true, variant);

	/* correctness sanity check */
	KUNIT_EXPECT_EQ(test, dec_len, size);
	KUNIT_EXPECT_MEMEQ(test, decoded, in, size);

	/* benchmark encode */

	u64 t1 = bench_encode_ns(in, size, enc, reps, variant);

	kunit_info(test, "[%s] encode run : %lluns", label, t1);

	u64 t2 = bench_decode_ns(enc, enc_len, decoded, reps, variant);

	kunit_info(test, "[%s] decode run : %lluns", label, t2);

	kfree(in);
	kfree(enc);
	kfree(decoded);
}

static void base64_performance_tests(struct kunit *test)
{
	/* run on STD variant only */
	run_perf_and_check(test, "64B", 64, BASE64_STD);
	run_perf_and_check(test, "1KB", 1024, BASE64_STD);
}

/* ---------- Helpers for encode ---------- */
static void expect_encode_ok(struct kunit *test, const u8 *src, int srclen,
			     const char *expected, bool padding,
			     enum base64_variant variant)
{
	char buf[128];
	int encoded_len = base64_encode(src, srclen, buf, padding, variant);

	buf[encoded_len] = '\0';

Annotation

Implementation Notes