xref: /freebsd/sys/tests/ktest_example.c (revision e1c4c8dd8d2d10b6104f06856a77bd5b4813a801)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2023 Alexander V. Chernikov
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <tests/ktest.h>
29 #include <sys/cdefs.h>
30 #include <sys/systm.h>
31 
32 
33 static int
34 test_something(struct ktest_test_context *ctx)
35 {
36 	KTEST_LOG(ctx, "I'm here, [%s]", __func__);
37 
38 	pause("sleeping...", hz / 10);
39 
40 	KTEST_LOG(ctx, "done");
41 
42 	return (0);
43 }
44 
45 static int
46 test_something_else(struct ktest_test_context *ctx)
47 {
48 	return (0);
49 }
50 
51 static int
52 test_failed(struct ktest_test_context *ctx)
53 {
54 	return (EBUSY);
55 }
56 
57 static int
58 test_failed2(struct ktest_test_context *ctx)
59 {
60 	KTEST_LOG(ctx, "failed because it always fails");
61 	return (EBUSY);
62 }
63 
64 #include <sys/malloc.h>
65 #include <netlink/netlink.h>
66 #include <netlink/netlink_ctl.h>
67 
68 struct test1_attrs {
69 	uint32_t	arg1;
70 	uint32_t	arg2;
71 	char		*text;
72 };
73 
74 #define	_OUT(_field)	offsetof(struct test1_attrs, _field)
75 static const struct nlattr_parser nla_p_test1[] = {
76 	{ .type = 1, .off = _OUT(arg1), .cb = nlattr_get_uint32 },
77 	{ .type = 2, .off = _OUT(arg2), .cb = nlattr_get_uint32 },
78 	{ .type = 3, .off = _OUT(text), .cb = nlattr_get_string },
79 };
80 #undef _OUT
81 NL_DECLARE_ATTR_PARSER(test1_parser, nla_p_test1);
82 
83 static int
84 test_with_params_parser(struct ktest_test_context *ctx, struct nlattr *nla)
85 {
86 	struct test1_attrs *attrs = npt_alloc(ctx->npt, sizeof(*attrs));
87 
88 	ctx->arg = attrs;
89 	if (attrs != NULL)
90 		return (nl_parse_nested(nla, &test1_parser, ctx->npt, attrs));
91 	return (ENOMEM);
92 }
93 
94 static int
95 test_with_params(struct ktest_test_context *ctx)
96 {
97 	struct test1_attrs *attrs = ctx->arg;
98 
99 	if (attrs->text != NULL)
100 		KTEST_LOG(ctx, "Get '%s'", attrs->text);
101 	KTEST_LOG(ctx, "%u + %u = %u", attrs->arg1, attrs->arg2,
102 	    attrs->arg1 + attrs->arg2);
103 	return (0);
104 }
105 
106 static const struct ktest_test_info tests[] = {
107 	{
108 		.name = "test_something",
109 		.desc = "example description",
110 		.func = &test_something,
111 	},
112 	{
113 		.name = "test_something_else",
114 		.desc = "example description 2",
115 		.func = &test_something_else,
116 	},
117 	{
118 		.name = "test_failed",
119 		.desc = "always failing test",
120 		.func = &test_failed,
121 	},
122 	{
123 		.name = "test_failed2",
124 		.desc = "always failing test",
125 		.func = &test_failed2,
126 	},
127 	{
128 		.name = "test_with_params",
129 		.desc = "test summing integers",
130 		.func = &test_with_params,
131 		.parse = &test_with_params_parser,
132 	},
133 };
134 KTEST_MODULE_DECLARE(ktest_example, tests);
135