^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Test cases for memcat_p() in lib/memcat_p.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) struct test_struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) int num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) unsigned int magic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #define MAGIC 0xf00ff00f
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) /* Size of each of the NULL-terminated input arrays */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define INPUT_MAX 128
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) /* Expected number of non-NULL elements in the output array */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #define EXPECT (INPUT_MAX * 2 - 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static int __init test_memcat_p_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct test_struct **in0, **in1, **out, **p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) int err = -ENOMEM, i, r, total = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) in0 = kcalloc(INPUT_MAX, sizeof(*in0), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) if (!in0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) in1 = kcalloc(INPUT_MAX, sizeof(*in1), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) if (!in1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) goto err_free_in0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) for (i = 0, r = 1; i < INPUT_MAX - 1; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) in0[i] = kmalloc(sizeof(**in0), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) if (!in0[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) goto err_free_elements;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) in1[i] = kmalloc(sizeof(**in1), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) if (!in1[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) kfree(in0[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) goto err_free_elements;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) /* lifted from test_sort.c */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) r = (r * 725861) % 6599;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) in0[i]->num = r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) in1[i]->num = -r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) in0[i]->magic = MAGIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) in1[i]->magic = MAGIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) in0[i] = in1[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) out = memcat_p(in0, in1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) if (!out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) goto err_free_all_elements;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) for (i = 0, p = out; *p && (i < INPUT_MAX * 2 - 1); p++, i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) total += (*p)->num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) if ((*p)->magic != MAGIC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) pr_err("test failed: wrong magic at %d: %u\n", i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) (*p)->magic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) goto err_free_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if (total) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) pr_err("test failed: expected zero total, got %d\n", total);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) goto err_free_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (i != EXPECT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) pr_err("test failed: expected output size %d, got %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) EXPECT, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) goto err_free_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) for (i = 0; i < INPUT_MAX - 1; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (out[i] != in0[i] || out[i + INPUT_MAX - 1] != in1[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) pr_err("test failed: wrong element order at %d\n", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) goto err_free_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) pr_info("test passed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) err_free_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) kfree(out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) err_free_all_elements:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) i = INPUT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) err_free_elements:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) for (i--; i >= 0; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) kfree(in1[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) kfree(in0[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) kfree(in1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) err_free_in0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) kfree(in0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static void __exit test_memcat_p_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) module_init(test_memcat_p_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) module_exit(test_memcat_p_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) MODULE_LICENSE("GPL");