Path: blob/main/sys/netlink/ktest_netlink_message_writer.c
39475 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2023 Alexander V. Chernikov4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/2627#include <tests/ktest.h>28#include <sys/cdefs.h>29#include <sys/systm.h>30#include <sys/malloc.h>31#include <netlink/netlink.h>32#include <netlink/netlink_ctl.h>33#include <netlink/netlink_var.h>34#include <netlink/netlink_message_writer.h>3536#define KTEST_CALLER37#include <netlink/ktest_netlink_message_writer.h>3839#ifdef INVARIANTS4041struct test_nlbuf_attrs {42uint32_t size;43uint32_t expected_avail;44int waitok;45};4647#define _OUT(_field) offsetof(struct test_nlbuf_attrs, _field)48static const struct nlattr_parser nla_p_nlbuf_w[] = {49{ .type = 1, .off = _OUT(size), .cb = nlattr_get_uint32 },50{ .type = 2, .off = _OUT(expected_avail), .cb = nlattr_get_uint32 },51{ .type = 3, .off = _OUT(waitok), .cb = nlattr_get_uint32 },52};53#undef _OUT54NL_DECLARE_ATTR_PARSER(nlbuf_w_parser, nla_p_nlbuf_w);5556static int57test_nlbuf_parser(struct ktest_test_context *ctx, struct nlattr *nla)58{59struct test_nlbuf_attrs *attrs = npt_alloc(ctx->npt, sizeof(*attrs));6061ctx->arg = attrs;62if (attrs != NULL)63return (nl_parse_nested(nla, &nlbuf_w_parser, ctx->npt, attrs));64return (ENOMEM);65}6667static int68test_nlbuf_writer_allocation(struct ktest_test_context *ctx)69{70struct test_nlbuf_attrs *attrs = ctx->arg;71struct nl_writer nw = {};72u_int alloc_len;73bool ret;7475ret = nlmsg_get_buf_wrapper(&nw, attrs->size, attrs->waitok);76if (!ret)77return (EINVAL);7879alloc_len = nw.buf->buflen;80KTEST_LOG(ctx, "requested %u, allocated %d", attrs->size, alloc_len);8182/* Mark enomem to avoid reallocation */83nw.enomem = true;8485if (nlmsg_reserve_data(&nw, alloc_len, void *) == NULL) {86KTEST_LOG(ctx, "unable to get %d bytes from the writer", alloc_len);87return (EINVAL);88}8990nl_buf_free(nw.buf);9192if (alloc_len < attrs->expected_avail) {93KTEST_LOG(ctx, "alloc_len %d, expected %u",94alloc_len, attrs->expected_avail);95return (EINVAL);96}9798return (0);99}100#endif101102static const struct ktest_test_info tests[] = {103#ifdef INVARIANTS104{105.name = "test_nlbuf_writer_allocation",106.desc = "test different buffer sizes in the netlink writer",107.func = &test_nlbuf_writer_allocation,108.parse = &test_nlbuf_parser,109},110#endif111};112KTEST_MODULE_DECLARE(ktest_netlink_message_writer, tests);113114115