iPXE
test.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 /** @file
00027  *
00028  * Self-test infrastructure
00029  *
00030  */
00031 
00032 /* Forcibly enable assertions */
00033 #undef NDEBUG
00034 
00035 #include <stddef.h>
00036 #include <stdio.h>
00037 #include <errno.h>
00038 #include <assert.h>
00039 #include <ipxe/test.h>
00040 #include <ipxe/init.h>
00041 #include <ipxe/image.h>
00042 #include <usr/profstat.h>
00043 
00044 /** Current self-test set */
00045 static struct self_test *current_tests;
00046 
00047 /**
00048  * Report test result
00049  *
00050  * @v success           Test succeeded
00051  * @v file              Test code file
00052  * @v line              Test code line
00053  * @v test              Test code
00054  */
00055 void test_ok ( int success, const char *file, unsigned int line,
00056                const char *test ) {
00057 
00058         /* Sanity check */
00059         assert ( current_tests != NULL );
00060 
00061         /* Increment test counter */
00062         current_tests->total++;
00063 
00064         /* Report failure if applicable */
00065         if ( ! success ) {
00066                 current_tests->failures++;
00067                 printf ( "FAILURE: \"%s\" test failed at %s line %d: ( %s )\n",
00068                          current_tests->name, file, line, test );
00069         }
00070 }
00071 
00072 /**
00073  * Run self-test set
00074  *
00075  */
00076 static void run_tests ( struct self_test *tests ) {
00077         unsigned int old_assertion_failures = assertion_failures;
00078 
00079         /* Sanity check */
00080         assert ( current_tests == NULL );
00081 
00082         /* Record current test set */
00083         current_tests = tests;
00084 
00085         /* Run tests */
00086         tests->exec();
00087 
00088         /* Clear current test set */
00089         current_tests = NULL;
00090 
00091         /* Record number of assertion failures */
00092         tests->assertion_failures =
00093                 ( assertion_failures - old_assertion_failures );
00094 
00095         /* Print test set summary */
00096         if ( tests->failures || tests->assertion_failures ) {
00097                 printf ( "FAILURE: \"%s\" %d of %d tests failed",
00098                          tests->name, tests->failures, tests->total );
00099                 if ( tests->assertion_failures ) {
00100                         printf ( " with %d assertion failures",
00101                                  tests->assertion_failures );
00102                 }
00103                 printf ( "\n" );
00104         } else {
00105                 printf ( "OK: \"%s\" %d tests passed\n",
00106                          tests->name, tests->total );
00107         }
00108 }
00109 
00110 /**
00111  * Run all self-tests
00112  *
00113  * @ret rc              Return status code
00114  */
00115 static int run_all_tests ( void ) {
00116         struct self_test *tests;
00117         unsigned int failures = 0;
00118         unsigned int assertions = 0;
00119         unsigned int total = 0;
00120 
00121         /* Run all compiled-in self-tests */
00122         printf ( "Starting self-tests\n" );
00123         for_each_table_entry ( tests, SELF_TESTS )
00124                 run_tests ( tests );
00125 
00126         /* Print overall summary */
00127         for_each_table_entry ( tests, SELF_TESTS ) {
00128                 total += tests->total;
00129                 failures += tests->failures;
00130                 assertions += tests->assertion_failures;
00131         }
00132         if ( failures || assertions ) {
00133                 printf ( "FAILURE: %d of %d tests failed",
00134                          failures, total );
00135                 if ( assertions ) {
00136                         printf ( " with %d assertion failures", assertions );
00137                 }
00138                 printf ( "\n" );
00139                 return -EINPROGRESS;
00140         } else {
00141                 printf ( "OK: all %d tests passed\n", total );
00142                 profstat();
00143                 return 0;
00144         }
00145 }
00146 
00147 static int test_image_probe ( struct image *image __unused ) {
00148         return -ENOTTY;
00149 }
00150 
00151 static int test_image_exec ( struct image *image __unused ) {
00152         return run_all_tests();
00153 }
00154 
00155 static struct image_type test_image_type = {
00156         .name = "self-tests",
00157         .probe = test_image_probe,
00158         .exec = test_image_exec,
00159 };
00160 
00161 static struct image test_image = {
00162         .refcnt = REF_INIT ( ref_no_free ),
00163         .name = "<TESTS>",
00164         .type = &test_image_type,
00165 };
00166 
00167 static void test_init ( void ) {
00168         int rc;
00169 
00170         /* Register self-tests image */
00171         if ( ( rc = register_image ( &test_image ) ) != 0 ) {
00172                 DBG ( "Could not register self-test image: %s\n",
00173                       strerror ( rc ) );
00174                 /* No way to report failure */
00175                 return;
00176         }
00177 }
00178 
00179 /** Self-test initialisation function */
00180 struct init_fn test_init_fn __init_fn ( INIT_EARLY ) = {
00181         .initialise = test_init,
00182 };