ktest.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. #ifndef SYS_TESTS_KTEST_H_
  28. #define SYS_TESTS_KTEST_H_
  29. #ifdef _KERNEL
  30. #include <sys/param.h>
  31. #include <sys/kernel.h>
  32. #include <sys/module.h>
  33. #include <sys/syslog.h>
  34. struct nlattr;
  35. struct nl_pstate;
  36. struct nlmsghdr;
  37. struct ktest_test_context {
  38. void *arg;
  39. struct nl_pstate *npt;
  40. struct nlmsghdr *hdr;
  41. char *buf;
  42. size_t bufsize;
  43. };
  44. typedef int (*ktest_run_t)(struct ktest_test_context *ctx);
  45. typedef int (*ktest_parse_t)(struct ktest_test_context *ctx, struct nlattr *container);
  46. struct ktest_test_info {
  47. const char *name;
  48. const char *desc;
  49. ktest_run_t func;
  50. ktest_parse_t parse;
  51. };
  52. struct ktest_module_info {
  53. const char *name;
  54. const struct ktest_test_info *tests;
  55. int num_tests;
  56. void *module_ptr;
  57. };
  58. int ktest_default_modevent(module_t mod, int type, void *arg);
  59. bool ktest_start_msg(struct ktest_test_context *ctx);
  60. void ktest_add_msg_meta(struct ktest_test_context *ctx, const char *func,
  61. const char *fname, int line);
  62. void ktest_add_msg_text(struct ktest_test_context *ctx, int msg_level,
  63. const char *fmt, ...);
  64. void ktest_end_msg(struct ktest_test_context *ctx);
  65. #define KTEST_LOG_LEVEL(_ctx, _l, _fmt, ...) { \
  66. if (ktest_start_msg(_ctx)) { \
  67. ktest_add_msg_meta(_ctx, __func__, __FILE__, __LINE__); \
  68. ktest_add_msg_text(_ctx, _l, _fmt, ## __VA_ARGS__); \
  69. ktest_end_msg(_ctx); \
  70. } \
  71. }
  72. #define KTEST_LOG(_ctx, _fmt, ...) \
  73. KTEST_LOG_LEVEL(_ctx, LOG_DEBUG, _fmt, ## __VA_ARGS__)
  74. #define KTEST_MAX_BUF 512
  75. #define KTEST_MODULE_DECLARE(_n, _t) \
  76. static struct ktest_module_info _module_info = { \
  77. .name = #_n, \
  78. .tests = _t, \
  79. .num_tests = nitems(_t), \
  80. }; \
  81. \
  82. static moduledata_t _module_data = { \
  83. #_n, \
  84. ktest_default_modevent, \
  85. &_module_info, \
  86. }; \
  87. \
  88. DECLARE_MODULE(ktest_##_n, _module_data, SI_SUB_PSEUDO, SI_ORDER_ANY); \
  89. MODULE_VERSION(ktest_##_n, 1); \
  90. MODULE_DEPEND(ktest_##_n, ktestmod, 1, 1, 1); \
  91. MODULE_DEPEND(ktest_##_n, netlink, 1, 1, 1); \
  92. #endif /* _KERNEL */
  93. /* genetlink definitions */
  94. #define KTEST_FAMILY_NAME "ktest"
  95. /* commands */
  96. enum {
  97. KTEST_CMD_UNSPEC = 0,
  98. KTEST_CMD_LIST = 1,
  99. KTEST_CMD_RUN = 2,
  100. KTEST_CMD_NEWTEST = 3,
  101. KTEST_CMD_NEWMESSAGE = 4,
  102. __KTEST_CMD_MAX,
  103. };
  104. #define KTEST_CMD_MAX (__KTEST_CMD_MAX - 1)
  105. enum ktest_attr_type_t {
  106. KTEST_ATTR_UNSPEC,
  107. KTEST_ATTR_MOD_NAME = 1, /* string: test module name */
  108. KTEST_ATTR_TEST_NAME = 2, /* string: test name */
  109. KTEST_ATTR_TEST_DESCR = 3, /* string: test description */
  110. KTEST_ATTR_TEST_META = 4, /* nested: container with test-specific metadata */
  111. };
  112. enum ktest_msg_attr_type_t {
  113. KTEST_MSG_ATTR_UNSPEC,
  114. KTEST_MSG_ATTR_TS = 1, /* struct timespec */
  115. KTEST_MSG_ATTR_FUNC = 2, /* string: function name */
  116. KTEST_MSG_ATTR_FILE = 3, /* string: file name */
  117. KTEST_MSG_ATTR_LINE = 4, /* u32: line in the file */
  118. KTEST_MSG_ATTR_TEXT = 5, /* string: actual message data */
  119. KTEST_MSG_ATTR_LEVEL = 6, /* u8: syslog loglevel */
  120. KTEST_MSG_ATTR_META = 7, /* nested: message metadata */
  121. };
  122. #endif