Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

test.c 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. FILE_LICENCE ( GPL2_OR_LATER );
  20. /** @file
  21. *
  22. * Self-test infrastructure
  23. *
  24. */
  25. /* Forcibly enable assertions */
  26. #undef NDEBUG
  27. #include <stddef.h>
  28. #include <stdio.h>
  29. #include <errno.h>
  30. #include <assert.h>
  31. #include <ipxe/test.h>
  32. #include <ipxe/init.h>
  33. #include <ipxe/image.h>
  34. #include <usr/profstat.h>
  35. /** Current self-test set */
  36. static struct self_test *current_tests;
  37. /**
  38. * Report test result
  39. *
  40. * @v success Test succeeded
  41. * @v file Test code file
  42. * @v line Test code line
  43. * @v test Test code
  44. */
  45. void test_ok ( int success, const char *file, unsigned int line,
  46. const char *test ) {
  47. /* Sanity check */
  48. assert ( current_tests != NULL );
  49. /* Increment test counter */
  50. current_tests->total++;
  51. /* Report failure if applicable */
  52. if ( ! success ) {
  53. current_tests->failures++;
  54. printf ( "FAILURE: \"%s\" test failed at %s line %d: ( %s )\n",
  55. current_tests->name, file, line, test );
  56. }
  57. }
  58. /**
  59. * Run self-test set
  60. *
  61. */
  62. static void run_tests ( struct self_test *tests ) {
  63. unsigned int old_assertion_failures = assertion_failures;
  64. /* Sanity check */
  65. assert ( current_tests == NULL );
  66. /* Record current test set */
  67. current_tests = tests;
  68. /* Run tests */
  69. tests->exec();
  70. /* Clear current test set */
  71. current_tests = NULL;
  72. /* Record number of assertion failures */
  73. tests->assertion_failures =
  74. ( assertion_failures - old_assertion_failures );
  75. /* Print test set summary */
  76. if ( tests->failures || tests->assertion_failures ) {
  77. printf ( "FAILURE: \"%s\" %d of %d tests failed",
  78. tests->name, tests->failures, tests->total );
  79. if ( tests->assertion_failures ) {
  80. printf ( " with %d assertion failures",
  81. tests->assertion_failures );
  82. }
  83. printf ( "\n" );
  84. } else {
  85. printf ( "OK: \"%s\" %d tests passed\n",
  86. tests->name, tests->total );
  87. }
  88. }
  89. /**
  90. * Run all self-tests
  91. *
  92. * @ret rc Return status code
  93. */
  94. static int run_all_tests ( void ) {
  95. struct self_test *tests;
  96. unsigned int failures = 0;
  97. unsigned int assertions = 0;
  98. unsigned int total = 0;
  99. /* Run all compiled-in self-tests */
  100. printf ( "Starting self-tests\n" );
  101. for_each_table_entry ( tests, SELF_TESTS )
  102. run_tests ( tests );
  103. /* Print overall summary */
  104. for_each_table_entry ( tests, SELF_TESTS ) {
  105. total += tests->total;
  106. failures += tests->failures;
  107. assertions += tests->assertion_failures;
  108. }
  109. if ( failures || assertions ) {
  110. printf ( "FAILURE: %d of %d tests failed",
  111. failures, total );
  112. if ( assertions ) {
  113. printf ( " with %d assertion failures", assertions );
  114. }
  115. printf ( "\n" );
  116. return -EINPROGRESS;
  117. } else {
  118. printf ( "OK: all %d tests passed\n", total );
  119. profstat();
  120. return 0;
  121. }
  122. }
  123. static int test_image_probe ( struct image *image __unused ) {
  124. return -ENOTTY;
  125. }
  126. static int test_image_exec ( struct image *image __unused ) {
  127. return run_all_tests();
  128. }
  129. static struct image_type test_image_type = {
  130. .name = "self-tests",
  131. .probe = test_image_probe,
  132. .exec = test_image_exec,
  133. };
  134. static struct image test_image = {
  135. .refcnt = REF_INIT ( ref_no_free ),
  136. .name = "<TESTS>",
  137. .type = &test_image_type,
  138. };
  139. static void test_init ( void ) {
  140. int rc;
  141. /* Register self-tests image */
  142. if ( ( rc = register_image ( &test_image ) ) != 0 ) {
  143. DBG ( "Could not register self-test image: %s\n",
  144. strerror ( rc ) );
  145. /* No way to report failure */
  146. return;
  147. }
  148. }
  149. /** Self-test initialisation function */
  150. struct init_fn test_init_fn __init_fn ( INIT_EARLY ) = {
  151. .initialise = test_init,
  152. };