[test] Simplify digest algorithm self-tests

Update the digest self-tests to use okx(), and centralise concepts and
data shared between tests for multiple algorithms to reduce duplicated
code.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
Michael Brown
2015-04-12 12:41:08 +01:00
parent b12b1b620f
commit a9da129122
5 changed files with 235 additions and 200 deletions

View File

@@ -27,74 +27,46 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
*
* MD5 tests
*
* Test inputs borrowed from NIST SHA-1 tests, with results calculated
* using md5sum.
*/
#include <stdint.h>
/* Forcibly enable assertions */
#undef NDEBUG
#include <ipxe/md5.h>
#include <ipxe/test.h>
#include "digest_test.h"
/** An MD5 test vector */
struct md5_test_vector {
/** Test data */
void *data;
/** Test data length */
size_t len;
/** Expected digest */
uint8_t digest[MD5_DIGEST_SIZE];
};
/* Empty test vector (digest obtained from "md5sum /dev/null") */
DIGEST_TEST ( md5_empty, &md5_algorithm, DIGEST_EMPTY,
DIGEST ( 0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04, 0xe9,
0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e ) );
/** MD5 test vectors */
static struct md5_test_vector md5_test_vectors[] = {
/* Test inputs borrowed from SHA-1 tests, with results
* calculated using md5sum.
*/
{ NULL, 0,
{ 0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04,
0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e } },
{ "abc", 3,
{ 0x90, 0x01, 0x50, 0x98, 0x3c, 0xd2, 0x4f, 0xb0,
0xd6, 0x96, 0x3f, 0x7d, 0x28, 0xe1, 0x7f, 0x72 } },
{ "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 56,
{ 0x82, 0x15, 0xef, 0x07, 0x96, 0xa2, 0x0b, 0xca,
0xaa, 0xe1, 0x16, 0xd3, 0x87, 0x6c, 0x66, 0x4a } },
};
/* NIST test vector "abc" (digest obtained from "md5sum <data>") */
DIGEST_TEST ( md5_nist_abc, &md5_algorithm, DIGEST_NIST_ABC,
DIGEST ( 0x90, 0x01, 0x50, 0x98, 0x3c, 0xd2, 0x4f, 0xb0, 0xd6,
0x96, 0x3f, 0x7d, 0x28, 0xe1, 0x7f, 0x72 ) );
/** MD5 test fragment lists */
static struct digest_test_fragments md5_test_fragments[] = {
{ { 0, -1UL, } },
{ { 1, 1, 1, 1, 1, 1, 1, 1 } },
{ { 2, 0, 23, 4, 6, 1, 0 } },
};
/* NIST test vector "abc...opq" (digest obtained from "md5sum <data>") */
DIGEST_TEST ( md5_nist_abc_opq, &md5_algorithm, DIGEST_NIST_ABC_OPQ,
DIGEST ( 0x82, 0x15, 0xef, 0x07, 0x96, 0xa2, 0x0b, 0xca, 0xaa,
0xe1, 0x16, 0xd3, 0x87, 0x6c, 0x66, 0x4a ) );
/**
* Perform MD5 self-test
*
*/
static void md5_test_exec ( void ) {
struct digest_algorithm *digest = &md5_algorithm;
struct md5_test_vector *test;
unsigned long cost;
unsigned int i;
unsigned int j;
/* Correctness test */
for ( i = 0 ; i < ( sizeof ( md5_test_vectors ) /
sizeof ( md5_test_vectors[0] ) ) ; i++ ) {
test = &md5_test_vectors[i];
/* Test with a single pass */
digest_ok ( digest, NULL, test->data, test->len, test->digest );
/* Test with fragment lists */
for ( j = 0 ; j < ( sizeof ( md5_test_fragments ) /
sizeof ( md5_test_fragments[0] ) ) ; j++ ){
digest_ok ( digest, &md5_test_fragments[j],
test->data, test->len, test->digest );
}
}
/* Correctness tests */
digest_ok ( &md5_empty );
digest_ok ( &md5_nist_abc );
digest_ok ( &md5_nist_abc_opq );
/* Speed test */
cost = digest_cost ( digest );
DBG ( "MD5 required %ld cycles per byte\n", cost );
/* Speed tests */
DBG ( "MD5 required %ld cycles per byte\n",
digest_cost ( &md5_algorithm ) );
}
/** MD5 self-test */