test.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. GRUB_EXPORT(grub_test_register);
  22. GRUB_EXPORT(grub_test_unregister);
  23. GRUB_EXPORT(grub_test_nonzero);
  24. struct grub_test_failure
  25. {
  26. /* The next failure. */
  27. struct grub_test_failure *next;
  28. /* The test source file name. */
  29. char *file;
  30. /* The test function name. */
  31. char *funp;
  32. /* The test call line number. */
  33. grub_uint32_t line;
  34. /* The test failure message. */
  35. char *message;
  36. };
  37. typedef struct grub_test_failure *grub_test_failure_t;
  38. grub_test_t grub_test_list;
  39. static grub_test_failure_t failure_list;
  40. static void
  41. add_failure (const char *file,
  42. const char *funp,
  43. grub_uint32_t line, const char *fmt, va_list args)
  44. {
  45. grub_test_failure_t failure;
  46. failure = (grub_test_failure_t) grub_malloc (sizeof (*failure));
  47. if (!failure)
  48. return;
  49. failure->file = grub_strdup (file ? : "<unknown_file>");
  50. failure->funp = grub_strdup (funp ? : "<unknown_function>");
  51. failure->line = line;
  52. failure->message = grub_xvasprintf (fmt, args);
  53. grub_list_push (GRUB_AS_LIST_P (&failure_list), GRUB_AS_LIST (failure));
  54. }
  55. static void
  56. free_failures (void)
  57. {
  58. grub_test_failure_t item;
  59. while ((item = grub_list_pop (GRUB_AS_LIST_P (&failure_list))) != 0)
  60. {
  61. if (item->message)
  62. grub_free (item->message);
  63. if (item->funp)
  64. grub_free (item->funp);
  65. if (item->file)
  66. grub_free (item->file);
  67. grub_free (item);
  68. }
  69. failure_list = 0;
  70. }
  71. void
  72. grub_test_nonzero (int cond,
  73. const char *file,
  74. const char *funp, grub_uint32_t line, const char *fmt, ...)
  75. {
  76. va_list ap;
  77. if (cond)
  78. return;
  79. va_start (ap, fmt);
  80. add_failure (file, funp, line, fmt, ap);
  81. va_end (ap);
  82. }
  83. void
  84. grub_test_register (const char *name, void (*test_main) (void))
  85. {
  86. grub_test_t test;
  87. test = (grub_test_t) grub_malloc (sizeof (*test));
  88. if (!test)
  89. return;
  90. test->name = grub_strdup (name);
  91. test->main = test_main;
  92. grub_list_push (GRUB_AS_LIST_P (&grub_test_list), GRUB_AS_LIST (test));
  93. }
  94. void
  95. grub_test_unregister (const char *name)
  96. {
  97. grub_test_t test;
  98. test = (grub_test_t) grub_named_list_find
  99. (GRUB_AS_NAMED_LIST (grub_test_list), name);
  100. if (test)
  101. {
  102. grub_list_remove (GRUB_AS_LIST_P (&grub_test_list), GRUB_AS_LIST (test));
  103. if (test->name)
  104. grub_free (test->name);
  105. grub_free (test);
  106. }
  107. }
  108. static int
  109. print_failure (grub_test_failure_t item)
  110. {
  111. grub_test_failure_t failure = (grub_test_failure_t) item;
  112. grub_printf (" %s:%s:%u: %s\n",
  113. (failure->file ? : "<unknown_file>"),
  114. (failure->funp ? : "<unknown_function>"),
  115. failure->line, (failure->message ? : "<no message>"));
  116. return 0;
  117. }
  118. int
  119. grub_test_run (grub_test_t test)
  120. {
  121. test->main ();
  122. grub_printf ("%s:\n", test->name);
  123. grub_list_iterate (GRUB_AS_LIST (failure_list),
  124. (grub_list_hook_t) print_failure, 0);
  125. if (!failure_list)
  126. grub_printf ("%s: PASS\n", test->name);
  127. else
  128. grub_printf ("%s: FAIL\n", test->name);
  129. free_failures ();
  130. return GRUB_ERR_NONE;
  131. }