1
linux/lib/test_memcat_p.c

117 lines
2.3 KiB
C
Raw Normal View History

// SPDX-License-Identifier: GPL-2.0
/*
* Test cases for memcat_p() in lib/memcat_p.c
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/string.h>
#include <linux/slab.h>
#include <linux/module.h>
struct test_struct {
int num;
unsigned int magic;
};
#define MAGIC 0xf00ff00f
/* Size of each of the NULL-terminated input arrays */
#define INPUT_MAX 128
/* Expected number of non-NULL elements in the output array */
#define EXPECT (INPUT_MAX * 2 - 2)
static int __init test_memcat_p_init(void)
{
struct test_struct **in0, **in1, **out, **p;
int err = -ENOMEM, i, r, total = 0;
in0 = kcalloc(INPUT_MAX, sizeof(*in0), GFP_KERNEL);
if (!in0)
return err;
in1 = kcalloc(INPUT_MAX, sizeof(*in1), GFP_KERNEL);
if (!in1)
goto err_free_in0;
for (i = 0, r = 1; i < INPUT_MAX - 1; i++) {
in0[i] = kmalloc(sizeof(**in0), GFP_KERNEL);
if (!in0[i])
goto err_free_elements;
in1[i] = kmalloc(sizeof(**in1), GFP_KERNEL);
if (!in1[i]) {
kfree(in0[i]);
goto err_free_elements;
}
/* lifted from test_sort.c */
r = (r * 725861) % 6599;
in0[i]->num = r;
in1[i]->num = -r;
in0[i]->magic = MAGIC;
in1[i]->magic = MAGIC;
}
in0[i] = in1[i] = NULL;
out = memcat_p(in0, in1);
if (!out)
goto err_free_all_elements;
err = -EINVAL;
for (i = 0, p = out; *p && (i < INPUT_MAX * 2 - 1); p++, i++) {
total += (*p)->num;
if ((*p)->magic != MAGIC) {
pr_err("test failed: wrong magic at %d: %u\n", i,
(*p)->magic);
goto err_free_out;
}
}
if (total) {
pr_err("test failed: expected zero total, got %d\n", total);
goto err_free_out;
}
if (i != EXPECT) {
pr_err("test failed: expected output size %d, got %d\n",
EXPECT, i);
goto err_free_out;
}
for (i = 0; i < INPUT_MAX - 1; i++)
if (out[i] != in0[i] || out[i + INPUT_MAX - 1] != in1[i]) {
pr_err("test failed: wrong element order at %d\n", i);
goto err_free_out;
}
err = 0;
pr_info("test passed\n");
err_free_out:
kfree(out);
err_free_all_elements:
i = INPUT_MAX;
err_free_elements:
for (i--; i >= 0; i--) {
kfree(in1[i]);
kfree(in0[i]);
}
kfree(in1);
err_free_in0:
kfree(in0);
return err;
}
static void __exit test_memcat_p_exit(void)
{
}
module_init(test_memcat_p_init);
module_exit(test_memcat_p_exit);
KUnit: add missing MODULE_DESCRIPTION() macros for lib/test_*.ko make allmodconfig && make W=1 C=1 reports for lib/test_*.ko: WARNING: modpost: missing MODULE_DESCRIPTION() in lib/test_hexdump.o WARNING: modpost: missing MODULE_DESCRIPTION() in lib/test_dhry.o WARNING: modpost: missing MODULE_DESCRIPTION() in lib/test_firmware.o WARNING: modpost: missing MODULE_DESCRIPTION() in lib/test_sysctl.o WARNING: modpost: missing MODULE_DESCRIPTION() in lib/test_hash.o WARNING: modpost: missing MODULE_DESCRIPTION() in lib/test_ida.o WARNING: modpost: missing MODULE_DESCRIPTION() in lib/test_list_sort.o WARNING: modpost: missing MODULE_DESCRIPTION() in lib/test_min_heap.o WARNING: modpost: missing MODULE_DESCRIPTION() in lib/test_module.o WARNING: modpost: missing MODULE_DESCRIPTION() in lib/test_sort.o WARNING: modpost: missing MODULE_DESCRIPTION() in lib/test_static_keys.o WARNING: modpost: missing MODULE_DESCRIPTION() in lib/test_static_key_base.o WARNING: modpost: missing MODULE_DESCRIPTION() in lib/test_memcat_p.o WARNING: modpost: missing MODULE_DESCRIPTION() in lib/test_blackhole_dev.o WARNING: modpost: missing MODULE_DESCRIPTION() in lib/test_meminit.o WARNING: modpost: missing MODULE_DESCRIPTION() in lib/test_free_pages.o WARNING: modpost: missing MODULE_DESCRIPTION() in lib/test_kprobes.o WARNING: modpost: missing MODULE_DESCRIPTION() in lib/test_ref_tracker.o WARNING: modpost: missing MODULE_DESCRIPTION() in lib/test_bits.o Add the missing invocations of the MODULE_DESCRIPTION() macro. Link: https://lkml.kernel.org/r/20240619-md-lib-test-v2-1-301e30eeba1e@quicinc.com Signed-off-by: Jeff Johnson <quic_jjohnson@quicinc.com> Reviewed-by: Kees Cook <kees@kernel.org> Reviewed-by: Masami Hiramatsu (Google) <mhiramat@kernel.org> Cc: Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com> Cc: "David S. Miller" <davem@davemloft.net> Cc: Masami Hiramatsu (Google) <mhiramat@kernel.org> Cc: "Naveen N. Rao" <naveen.n.rao@linux.ibm.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2024-06-19 13:59:15 -07:00
MODULE_DESCRIPTION("Test cases for memcat_p() in lib/memcat_p.c");
MODULE_LICENSE("GPL");