test.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2010 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/mm.h>
  19. #include <grub/misc.h>
  20. #include <grub/test.h>
  21. struct grub_test_failure
  22. {
  23. /* The next failure. */
  24. struct grub_test_failure *next;
  25. struct grub_test_failure **prev;
  26. /* The test source file name. */
  27. char *file;
  28. /* The test function name. */
  29. char *funp;
  30. /* The test call line number. */
  31. grub_uint32_t line;
  32. /* The test failure message. */
  33. char *message;
  34. };
  35. typedef struct grub_test_failure *grub_test_failure_t;
  36. grub_test_t grub_test_list;
  37. static grub_test_failure_t failure_list;
  38. static grub_test_failure_t
  39. failure_start(const char *file, const char *funp, grub_uint32_t line);
  40. static grub_test_failure_t
  41. failure_start(const char *file, const char *funp, grub_uint32_t line)
  42. {
  43. grub_test_failure_t failure;
  44. failure = (grub_test_failure_t) grub_malloc (sizeof (*failure));
  45. if (!failure)
  46. return NULL;
  47. failure->file = grub_strdup (file ? : "<unknown_file>");
  48. if (!failure->file)
  49. {
  50. grub_free(failure);
  51. return NULL;
  52. }
  53. failure->funp = grub_strdup (funp ? : "<unknown_function>");
  54. if (!failure->funp)
  55. {
  56. grub_free(failure->file);
  57. grub_free(failure);
  58. return NULL;
  59. }
  60. failure->line = line;
  61. failure->message = NULL;
  62. return failure;
  63. }
  64. static void
  65. failure_append_vtext(grub_test_failure_t failure, const char *fmt, va_list args);
  66. static void
  67. failure_append_vtext(grub_test_failure_t failure, const char *fmt, va_list args)
  68. {
  69. char *msg = grub_xvasprintf(fmt, args);
  70. if (failure->message)
  71. {
  72. char *oldmsg = failure->message;
  73. failure->message = grub_xasprintf("%s%s", oldmsg, msg);
  74. grub_free (oldmsg);
  75. grub_free (msg);
  76. }
  77. else
  78. {
  79. failure->message = msg;
  80. }
  81. }
  82. static void
  83. failure_append_text(grub_test_failure_t failure, const char *fmt, ...)
  84. {
  85. va_list args;
  86. va_start(args, fmt);
  87. failure_append_vtext(failure, fmt, args);
  88. va_end(args);
  89. }
  90. static void
  91. add_failure (const char *file,
  92. const char *funp,
  93. grub_uint32_t line, const char *fmt, va_list args)
  94. {
  95. grub_test_failure_t failure = failure_start(file, funp, line);
  96. failure_append_text(failure, fmt, args);
  97. grub_list_push (GRUB_AS_LIST_P (&failure_list), GRUB_AS_LIST (failure));
  98. }
  99. static void
  100. free_failures (void)
  101. {
  102. grub_test_failure_t item;
  103. while (failure_list)
  104. {
  105. item = failure_list;
  106. failure_list = item->next;
  107. if (item->message)
  108. grub_free (item->message);
  109. if (item->funp)
  110. grub_free (item->funp);
  111. if (item->file)
  112. grub_free (item->file);
  113. grub_free (item);
  114. }
  115. failure_list = 0;
  116. }
  117. void
  118. grub_test_nonzero (int cond,
  119. const char *file,
  120. const char *funp, grub_uint32_t line, const char *fmt, ...)
  121. {
  122. va_list ap;
  123. if (cond)
  124. return;
  125. va_start (ap, fmt);
  126. add_failure (file, funp, line, fmt, ap);
  127. va_end (ap);
  128. }
  129. void
  130. grub_test_assert_helper (int cond, const char *file, const char *funp,
  131. grub_uint32_t line, const char *condstr,
  132. const char *fmt, ...)
  133. {
  134. va_list ap;
  135. grub_test_failure_t failure;
  136. if (cond)
  137. return;
  138. failure = failure_start(file, funp, line);
  139. failure_append_text(failure, "assert failed: %s ", condstr);
  140. va_start(ap, fmt);
  141. failure_append_vtext(failure, fmt, ap);
  142. va_end(ap);
  143. grub_list_push (GRUB_AS_LIST_P (&failure_list), GRUB_AS_LIST (failure));
  144. }
  145. void
  146. grub_test_register (const char *name, void (*test_main) (void))
  147. {
  148. grub_test_t test;
  149. test = (grub_test_t) grub_malloc (sizeof (*test));
  150. if (!test)
  151. return;
  152. test->name = grub_strdup (name);
  153. test->main = test_main;
  154. grub_list_push (GRUB_AS_LIST_P (&grub_test_list), GRUB_AS_LIST (test));
  155. }
  156. void
  157. grub_test_unregister (const char *name)
  158. {
  159. grub_test_t test;
  160. test = (grub_test_t) grub_named_list_find
  161. (GRUB_AS_NAMED_LIST (grub_test_list), name);
  162. if (test)
  163. {
  164. grub_list_remove (GRUB_AS_LIST (test));
  165. if (test->name)
  166. grub_free (test->name);
  167. grub_free (test);
  168. }
  169. }
  170. int
  171. grub_test_run (grub_test_t test)
  172. {
  173. grub_test_failure_t failure;
  174. test->main ();
  175. grub_printf ("%s:\n", test->name);
  176. FOR_LIST_ELEMENTS (failure, failure_list)
  177. grub_printf (" %s:%s:%u: %s\n",
  178. (failure->file ? : "<unknown_file>"),
  179. (failure->funp ? : "<unknown_function>"),
  180. failure->line, (failure->message ? : "<no message>"));
  181. if (!failure_list)
  182. {
  183. grub_printf ("%s: PASS\n", test->name);
  184. return GRUB_ERR_NONE;
  185. }
  186. else
  187. {
  188. grub_printf ("%s: FAIL\n", test->name);
  189. free_failures ();
  190. return GRUB_ERR_TEST_FAILURE;
  191. }
  192. }