test.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (c) 2022 Agustina Arzille.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <assert.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <kern/fmt.h>
  21. #include <kern/log.h>
  22. #include <kern/macros.h>
  23. #include <kern/symbol.h>
  24. #include <kern/task.h>
  25. #include <kern/thread.h>
  26. #include <machine/cpu.h>
  27. #include <machine/pmap.h>
  28. #include <test/test.h>
  29. #define PREFIX_LEN (sizeof (QUOTE (TEST_PREFIX)) - 1)
  30. typedef int (*test_fn_t) (void);
  31. static bool
  32. test_is_inline (const char *name)
  33. {
  34. return (name[PREFIX_LEN] == QUOTE(TEST_INLINE_CHAR)[0]);
  35. }
  36. static const char*
  37. test_exit_status (int ret)
  38. {
  39. switch (ret)
  40. {
  41. case TEST_OK:
  42. return ("OK");
  43. case TEST_SKIPPED:
  44. return ("skipped");
  45. case TEST_RUNNING:
  46. return ("running");
  47. case TEST_FAILED:
  48. return ("failed");
  49. default:
  50. panic ("unsupported test type");
  51. return (NULL);
  52. }
  53. }
  54. static void
  55. test_thread_run (void *arg)
  56. {
  57. const struct symbol *sym = arg;
  58. int ret = ((test_fn_t)sym->addr) ();
  59. const char *name = sym->name + PREFIX_LEN +
  60. (test_is_inline (sym->name) ? 2 : 1);
  61. log_info ("test (%s): %s", name, test_exit_status (ret));
  62. }
  63. void
  64. test_setup (void)
  65. {
  66. struct symbol_iter iter;
  67. for (symbol_iter_init (&iter);
  68. symbol_iter_valid (&iter);
  69. symbol_iter_next (&iter))
  70. {
  71. const struct symbol *sym = iter.symbol;
  72. if ((uintptr_t)sym->name < PMAP_START_KERNEL_ADDRESS ||
  73. (uintptr_t)sym->name > PMAP_END_KERNEL_ADDRESS)
  74. /*
  75. * This can happen for symbols that live in the BOOT section;
  76. * at this stage, that memory is no longer accessible, so we
  77. * simply skip them.
  78. */
  79. continue;
  80. size_t len = strlen (sym->name);
  81. if (len <= PREFIX_LEN ||
  82. memcmp (sym->name, QUOTE (TEST_PREFIX), PREFIX_LEN) != 0)
  83. continue;
  84. else if (test_is_inline (sym->name))
  85. {
  86. test_thread_run ((void *)sym);
  87. continue;
  88. }
  89. char name[THREAD_NAME_SIZE];
  90. snprintf (name, sizeof (name), THREAD_KERNEL_PREFIX "test_%s",
  91. sym->name + PREFIX_LEN + 1);
  92. struct thread_attr attr;
  93. thread_attr_init (&attr, name);
  94. thread_attr_set_detached (&attr);
  95. if (thread_create (NULL, &attr, test_thread_run, (void *)sym) != 0)
  96. log_err ("failed to run test: %s", attr.name);
  97. }
  98. }
  99. int
  100. test_util_create_thr (struct thread **out, void (*fn) (void *),
  101. void *arg, const char *name)
  102. {
  103. struct task *task;
  104. int error = task_create (&task, name);
  105. if (error)
  106. return (error);
  107. char tname[TASK_NAME_SIZE];
  108. sprintf (tname, "%s/0", name);
  109. struct thread_attr attr;
  110. thread_attr_init (&attr, tname);
  111. thread_attr_set_task (&attr, task);
  112. error = thread_create (out, &attr, fn, arg);
  113. if (error)
  114. task_destroy (task);
  115. return (error);
  116. }
  117. void
  118. test_thread_wait_state (struct thread *thr, uint32_t state)
  119. {
  120. while (thread_state (thr) != state)
  121. thread_yield ();
  122. }