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
104882 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
struct test_nlbuf_attrs {
41
uint32_t size;
42
uint32_t expected_avail;
43
int waitok;
44
};
45
46
#define _OUT(_field) offsetof(struct test_nlbuf_attrs, _field)
47
static const struct nlattr_parser nla_p_nlbuf_w[] = {
48
{ .type = 1, .off = _OUT(size), .cb = nlattr_get_uint32 },
49
{ .type = 2, .off = _OUT(expected_avail), .cb = nlattr_get_uint32 },
50
{ .type = 3, .off = _OUT(waitok), .cb = nlattr_get_uint32 },
51
};
52
#undef _OUT
53
NL_DECLARE_ATTR_PARSER(nlbuf_w_parser, nla_p_nlbuf_w);
54
55
static int
56
test_nlbuf_parser(struct ktest_test_context *ctx, struct nlattr *nla)
57
{
58
struct test_nlbuf_attrs *attrs = npt_alloc(ctx->npt, sizeof(*attrs));
59
60
ctx->arg = attrs;
61
if (attrs != NULL)
62
return (nl_parse_nested(nla, &nlbuf_w_parser, ctx->npt, attrs));
63
return (ENOMEM);
64
}
65
66
static int
67
test_nlbuf_writer_allocation(struct ktest_test_context *ctx)
68
{
69
struct test_nlbuf_attrs *attrs = ctx->arg;
70
struct nl_writer nw = {};
71
u_int alloc_len;
72
bool ret;
73
74
ret = nlmsg_get_buf_wrapper(&nw, attrs->size, attrs->waitok);
75
if (!ret)
76
return (EINVAL);
77
78
alloc_len = nw.buf->buflen;
79
KTEST_LOG(ctx, "requested %u, allocated %d", attrs->size, alloc_len);
80
81
/* Mark enomem to avoid reallocation */
82
nw.enomem = true;
83
84
if (nlmsg_reserve_data(&nw, alloc_len, void *) == NULL) {
85
KTEST_LOG(ctx, "unable to get %d bytes from the writer", alloc_len);
86
return (EINVAL);
87
}
88
89
nl_buf_free(nw.buf);
90
91
if (alloc_len < attrs->expected_avail) {
92
KTEST_LOG(ctx, "alloc_len %d, expected %u",
93
alloc_len, attrs->expected_avail);
94
return (EINVAL);
95
}
96
97
return (0);
98
}
99
100
static const struct ktest_test_info tests[] = {
101
{
102
.name = "test_nlbuf_writer_allocation",
103
.desc = "test different buffer sizes in the netlink writer",
104
.func = &test_nlbuf_writer_allocation,
105
.parse = &test_nlbuf_parser,
106
},
107
};
108
KTEST_MODULE_DECLARE(ktest_netlink_message_writer, tests);
109
110