2011-10-13 00:50:14 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 Michael Brown <mbrown@fensystems.co.uk>.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of the
|
|
|
|
* License, or any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2012-07-20 20:55:45 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
|
|
* 02110-1301, USA.
|
2011-10-13 00:50:14 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
FILE_LICENCE ( GPL2_OR_LATER );
|
|
|
|
|
|
|
|
/** @file
|
|
|
|
*
|
|
|
|
* Self-test infrastructure
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Forcibly enable assertions */
|
|
|
|
#undef NDEBUG
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
2012-03-18 03:42:00 +01:00
|
|
|
#include <errno.h>
|
2011-10-13 00:50:14 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <ipxe/test.h>
|
|
|
|
#include <ipxe/init.h>
|
2012-03-18 03:42:00 +01:00
|
|
|
#include <ipxe/image.h>
|
2014-05-02 16:20:12 +02:00
|
|
|
#include <usr/profstat.h>
|
2011-10-13 00:50:14 +02:00
|
|
|
|
|
|
|
/** Current self-test set */
|
|
|
|
static struct self_test *current_tests;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Report test result
|
|
|
|
*
|
|
|
|
* @v success Test succeeded
|
|
|
|
* @v file Test code file
|
|
|
|
* @v line Test code line
|
2013-11-27 04:03:16 +01:00
|
|
|
* @v test Test code
|
2011-10-13 00:50:14 +02:00
|
|
|
*/
|
2013-11-27 04:03:16 +01:00
|
|
|
void test_ok ( int success, const char *file, unsigned int line,
|
|
|
|
const char *test ) {
|
2011-10-13 00:50:14 +02:00
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
assert ( current_tests != NULL );
|
|
|
|
|
|
|
|
/* Increment test counter */
|
|
|
|
current_tests->total++;
|
|
|
|
|
|
|
|
/* Report failure if applicable */
|
|
|
|
if ( ! success ) {
|
|
|
|
current_tests->failures++;
|
2013-11-27 04:03:16 +01:00
|
|
|
printf ( "FAILURE: \"%s\" test failed at %s line %d: ( %s )\n",
|
|
|
|
current_tests->name, file, line, test );
|
2011-10-13 00:50:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run self-test set
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static void run_tests ( struct self_test *tests ) {
|
|
|
|
unsigned int old_assertion_failures = assertion_failures;
|
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
assert ( current_tests == NULL );
|
|
|
|
|
|
|
|
/* Record current test set */
|
|
|
|
current_tests = tests;
|
|
|
|
|
|
|
|
/* Run tests */
|
|
|
|
tests->exec();
|
|
|
|
|
|
|
|
/* Clear current test set */
|
|
|
|
current_tests = NULL;
|
|
|
|
|
|
|
|
/* Record number of assertion failures */
|
|
|
|
tests->assertion_failures =
|
|
|
|
( assertion_failures - old_assertion_failures );
|
|
|
|
|
|
|
|
/* Print test set summary */
|
|
|
|
if ( tests->failures || tests->assertion_failures ) {
|
|
|
|
printf ( "FAILURE: \"%s\" %d of %d tests failed",
|
|
|
|
tests->name, tests->failures, tests->total );
|
|
|
|
if ( tests->assertion_failures ) {
|
|
|
|
printf ( " with %d assertion failures",
|
|
|
|
tests->assertion_failures );
|
|
|
|
}
|
|
|
|
printf ( "\n" );
|
|
|
|
} else {
|
|
|
|
printf ( "OK: \"%s\" %d tests passed\n",
|
|
|
|
tests->name, tests->total );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run all self-tests
|
|
|
|
*
|
2012-03-18 03:42:00 +01:00
|
|
|
* @ret rc Return status code
|
2011-10-13 00:50:14 +02:00
|
|
|
*/
|
2012-03-18 03:42:00 +01:00
|
|
|
static int run_all_tests ( void ) {
|
2011-10-13 00:50:14 +02:00
|
|
|
struct self_test *tests;
|
|
|
|
unsigned int failures = 0;
|
|
|
|
unsigned int assertions = 0;
|
|
|
|
unsigned int total = 0;
|
|
|
|
|
|
|
|
/* Run all compiled-in self-tests */
|
|
|
|
printf ( "Starting self-tests\n" );
|
|
|
|
for_each_table_entry ( tests, SELF_TESTS )
|
|
|
|
run_tests ( tests );
|
|
|
|
|
|
|
|
/* Print overall summary */
|
|
|
|
for_each_table_entry ( tests, SELF_TESTS ) {
|
|
|
|
total += tests->total;
|
|
|
|
failures += tests->failures;
|
|
|
|
assertions += tests->assertion_failures;
|
|
|
|
}
|
|
|
|
if ( failures || assertions ) {
|
|
|
|
printf ( "FAILURE: %d of %d tests failed",
|
|
|
|
failures, total );
|
|
|
|
if ( assertions ) {
|
|
|
|
printf ( " with %d assertion failures", assertions );
|
|
|
|
}
|
|
|
|
printf ( "\n" );
|
2012-03-18 03:42:00 +01:00
|
|
|
return -EINPROGRESS;
|
2011-10-13 00:50:14 +02:00
|
|
|
} else {
|
|
|
|
printf ( "OK: all %d tests passed\n", total );
|
2014-05-02 16:20:12 +02:00
|
|
|
profstat();
|
2012-03-18 03:42:00 +01:00
|
|
|
return 0;
|
2011-10-13 00:50:14 +02:00
|
|
|
}
|
2012-03-18 03:42:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int test_image_probe ( struct image *image __unused ) {
|
|
|
|
return -ENOTTY;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int test_image_exec ( struct image *image __unused ) {
|
|
|
|
return run_all_tests();
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct image_type test_image_type = {
|
|
|
|
.name = "self-tests",
|
|
|
|
.probe = test_image_probe,
|
|
|
|
.exec = test_image_exec,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct image test_image = {
|
2012-03-24 02:16:37 +01:00
|
|
|
.refcnt = REF_INIT ( ref_no_free ),
|
2012-03-18 03:42:00 +01:00
|
|
|
.name = "<TESTS>",
|
|
|
|
.type = &test_image_type,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void test_init ( void ) {
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* Register self-tests image */
|
|
|
|
if ( ( rc = register_image ( &test_image ) ) != 0 ) {
|
|
|
|
DBG ( "Could not register self-test image: %s\n",
|
|
|
|
strerror ( rc ) );
|
|
|
|
/* No way to report failure */
|
|
|
|
return;
|
|
|
|
}
|
2011-10-13 00:50:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Self-test initialisation function */
|
2012-03-18 03:42:00 +01:00
|
|
|
struct init_fn test_init_fn __init_fn ( INIT_EARLY ) = {
|
2011-10-13 00:50:14 +02:00
|
|
|
.initialise = test_init,
|
|
|
|
};
|