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.
- 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/base64.h
Detected Declarations
function Copyrightfunction bench_decode_nsfunction run_perf_and_checkfunction base64_performance_testsfunction expect_encode_okfunction expect_decode_okfunction expect_decode_errfunction base64_std_encode_testsfunction base64_std_decode_testsfunction base64_variant_tests
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
- Immediate include surface: `kunit/test.h`, `linux/base64.h`.
- Detected declarations: `function Copyright`, `function bench_decode_ns`, `function run_perf_and_check`, `function base64_performance_tests`, `function expect_encode_ok`, `function expect_decode_ok`, `function expect_decode_err`, `function base64_std_encode_tests`, `function base64_std_decode_tests`, `function base64_variant_tests`.
- 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.