ktest_netlink_message_writer.c 3.5 KB

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