2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2023 Alexander V. Chernikov
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <tests/ktest.h>
29 #include <sys/cdefs.h>
30 #include <sys/systm.h>
34 test_something(struct ktest_test_context *ctx)
36 KTEST_LOG(ctx, "I'm here, [%s]", __func__);
38 pause("sleeping...", hz / 10);
40 KTEST_LOG(ctx, "done");
46 test_something_else(struct ktest_test_context *ctx)
52 test_failed(struct ktest_test_context *ctx)
58 test_failed2(struct ktest_test_context *ctx)
60 KTEST_LOG(ctx, "failed because it always fails");
64 #include <sys/malloc.h>
65 #include <netlink/netlink.h>
66 #include <netlink/netlink_ctl.h>
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 },
81 NL_DECLARE_ATTR_PARSER(test1_parser, nla_p_test1);
84 test_with_params_parser(struct ktest_test_context *ctx, struct nlattr *nla)
86 struct test1_attrs *attrs = npt_alloc(ctx->npt, sizeof(*attrs));
90 return (nl_parse_nested(nla, &test1_parser, ctx->npt, attrs));
95 test_with_params(struct ktest_test_context *ctx)
97 struct test1_attrs *attrs = ctx->arg;
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);
106 static const struct ktest_test_info tests[] = {
108 .name = "test_something",
109 .desc = "example description",
110 .func = &test_something,
113 .name = "test_something_else",
114 .desc = "example description 2",
115 .func = &test_something_else,
118 .name = "test_failed",
119 .desc = "always failing test",
120 .func = &test_failed,
123 .name = "test_failed2",
124 .desc = "always failing test",
125 .func = &test_failed2,
128 .name = "test_with_params",
129 .desc = "test summing integers",
130 .func = &test_with_params,
131 .parse = &test_with_params_parser,
134 KTEST_MODULE_DECLARE(ktest_example, tests);