You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test.c 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright (C) 2011 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * 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, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. /** @file
  25. *
  26. * Self-test infrastructure
  27. *
  28. */
  29. /* Forcibly enable assertions */
  30. #undef NDEBUG
  31. #include <stddef.h>
  32. #include <stdio.h>
  33. #include <errno.h>
  34. #include <assert.h>
  35. #include <ipxe/test.h>
  36. #include <ipxe/init.h>
  37. #include <ipxe/image.h>
  38. #include <usr/profstat.h>
  39. /** Current self-test set */
  40. static struct self_test *current_tests;
  41. /**
  42. * Report test result
  43. *
  44. * @v success Test succeeded
  45. * @v file Test code file
  46. * @v line Test code line
  47. * @v test Test code
  48. */
  49. void test_ok ( int success, const char *file, unsigned int line,
  50. const char *test ) {
  51. /* Sanity check */
  52. assert ( current_tests != NULL );
  53. /* Increment test counter */
  54. current_tests->total++;
  55. /* Report failure if applicable */
  56. if ( ! success ) {
  57. current_tests->failures++;
  58. printf ( "FAILURE: \"%s\" test failed at %s line %d: ( %s )\n",
  59. current_tests->name, file, line, test );
  60. }
  61. }
  62. /**
  63. * Run self-test set
  64. *
  65. */
  66. static void run_tests ( struct self_test *tests ) {
  67. unsigned int old_assertion_failures = assertion_failures;
  68. /* Sanity check */
  69. assert ( current_tests == NULL );
  70. /* Record current test set */
  71. current_tests = tests;
  72. /* Run tests */
  73. tests->exec();
  74. /* Clear current test set */
  75. current_tests = NULL;
  76. /* Record number of assertion failures */
  77. tests->assertion_failures =
  78. ( assertion_failures - old_assertion_failures );
  79. /* Print test set summary */
  80. if ( tests->failures || tests->assertion_failures ) {
  81. printf ( "FAILURE: \"%s\" %d of %d tests failed",
  82. tests->name, tests->failures, tests->total );
  83. if ( tests->assertion_failures ) {
  84. printf ( " with %d assertion failures",
  85. tests->assertion_failures );
  86. }
  87. printf ( "\n" );
  88. } else {
  89. printf ( "OK: \"%s\" %d tests passed\n",
  90. tests->name, tests->total );
  91. }
  92. }
  93. /**
  94. * Run all self-tests
  95. *
  96. * @ret rc Return status code
  97. */
  98. static int run_all_tests ( void ) {
  99. struct self_test *tests;
  100. unsigned int failures = 0;
  101. unsigned int assertions = 0;
  102. unsigned int total = 0;
  103. /* Run all compiled-in self-tests */
  104. printf ( "Starting self-tests\n" );
  105. for_each_table_entry ( tests, SELF_TESTS )
  106. run_tests ( tests );
  107. /* Print overall summary */
  108. for_each_table_entry ( tests, SELF_TESTS ) {
  109. total += tests->total;
  110. failures += tests->failures;
  111. assertions += tests->assertion_failures;
  112. }
  113. if ( failures || assertions ) {
  114. printf ( "FAILURE: %d of %d tests failed",
  115. failures, total );
  116. if ( assertions ) {
  117. printf ( " with %d assertion failures", assertions );
  118. }
  119. printf ( "\n" );
  120. return -EINPROGRESS;
  121. } else {
  122. printf ( "OK: all %d tests passed\n", total );
  123. profstat();
  124. return 0;
  125. }
  126. }
  127. static int test_image_probe ( struct image *image __unused ) {
  128. return -ENOTTY;
  129. }
  130. static int test_image_exec ( struct image *image __unused ) {
  131. return run_all_tests();
  132. }
  133. static struct image_type test_image_type = {
  134. .name = "self-tests",
  135. .probe = test_image_probe,
  136. .exec = test_image_exec,
  137. };
  138. static struct image test_image = {
  139. .refcnt = REF_INIT ( ref_no_free ),
  140. .name = "<TESTS>",
  141. .type = &test_image_type,
  142. };
  143. static void test_init ( void ) {
  144. int rc;
  145. /* Register self-tests image */
  146. if ( ( rc = register_image ( &test_image ) ) != 0 ) {
  147. DBG ( "Could not register self-test image: %s\n",
  148. strerror ( rc ) );
  149. /* No way to report failure */
  150. return;
  151. }
  152. }
  153. /** Self-test initialisation function */
  154. struct init_fn test_init_fn __init_fn ( INIT_EARLY ) = {
  155. .initialise = test_init,
  156. };