Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/netlink/ktest_netlink_message_writer.c
39475 views
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
#include <sys/malloc.h>
32
#include <netlink/netlink.h>
33
#include <netlink/netlink_ctl.h>
34
#include <netlink/netlink_var.h>
35
#include <netlink/netlink_message_writer.h>
36
37
#define KTEST_CALLER
38
#include <netlink/ktest_netlink_message_writer.h>
39
40
#ifdef INVARIANTS
41
42
struct test_nlbuf_attrs {
43
uint32_t size;
44
uint32_t expected_avail;
45
int waitok;
46
};
47
48
#define _OUT(_field) offsetof(struct test_nlbuf_attrs, _field)
49
static const struct nlattr_parser nla_p_nlbuf_w[] = {
50
{ .type = 1, .off = _OUT(size), .cb = nlattr_get_uint32 },
51
{ .type = 2, .off = _OUT(expected_avail), .cb = nlattr_get_uint32 },
52
{ .type = 3, .off = _OUT(waitok), .cb = nlattr_get_uint32 },
53
};
54
#undef _OUT
55
NL_DECLARE_ATTR_PARSER(nlbuf_w_parser, nla_p_nlbuf_w);
56
57
static int
58
test_nlbuf_parser(struct ktest_test_context *ctx, struct nlattr *nla)
59
{
60
struct test_nlbuf_attrs *attrs = npt_alloc(ctx->npt, sizeof(*attrs));
61
62
ctx->arg = attrs;
63
if (attrs != NULL)
64
return (nl_parse_nested(nla, &nlbuf_w_parser, ctx->npt, attrs));
65
return (ENOMEM);
66
}
67
68
static int
69
test_nlbuf_writer_allocation(struct ktest_test_context *ctx)
70
{
71
struct test_nlbuf_attrs *attrs = ctx->arg;
72
struct nl_writer nw = {};
73
u_int alloc_len;
74
bool ret;
75
76
ret = nlmsg_get_buf_wrapper(&nw, attrs->size, attrs->waitok);
77
if (!ret)
78
return (EINVAL);
79
80
alloc_len = nw.buf->buflen;
81
KTEST_LOG(ctx, "requested %u, allocated %d", attrs->size, alloc_len);
82
83
/* Mark enomem to avoid reallocation */
84
nw.enomem = true;
85
86
if (nlmsg_reserve_data(&nw, alloc_len, void *) == NULL) {
87
KTEST_LOG(ctx, "unable to get %d bytes from the writer", alloc_len);
88
return (EINVAL);
89
}
90
91
nl_buf_free(nw.buf);
92
93
if (alloc_len < attrs->expected_avail) {
94
KTEST_LOG(ctx, "alloc_len %d, expected %u",
95
alloc_len, attrs->expected_avail);
96
return (EINVAL);
97
}
98
99
return (0);
100
}
101
#endif
102
103
static const struct ktest_test_info tests[] = {
104
#ifdef INVARIANTS
105
{
106
.name = "test_nlbuf_writer_allocation",
107
.desc = "test different buffer sizes in the netlink writer",
108
.func = &test_nlbuf_writer_allocation,
109
.parse = &test_nlbuf_parser,
110
},
111
#endif
112
};
113
KTEST_MODULE_DECLARE(ktest_netlink_message_writer, tests);
114
115