summaryrefslogtreecommitdiffstats
path: root/src/tests/md5_test.c
diff options
context:
space:
mode:
authorMichael Brown2012-03-05 01:31:07 +0100
committerMichael Brown2012-03-05 01:31:07 +0100
commitc15e73f28361a9c6d435b6f64bc1791f4fab8345 (patch)
tree5ac17f2f8d647d0a5d02747d73bedee1c56bbc9b /src/tests/md5_test.c
parent[test] Add self-tests for SHA-256 algorithm (diff)
downloadipxe-c15e73f28361a9c6d435b6f64bc1791f4fab8345.tar.gz
ipxe-c15e73f28361a9c6d435b6f64bc1791f4fab8345.tar.xz
ipxe-c15e73f28361a9c6d435b6f64bc1791f4fab8345.zip
[test] Add self-tests for MD5 algorithm
Signed-off-by: Michael Brown <mcb30@ipxe.org>
Diffstat (limited to 'src/tests/md5_test.c')
-rw-r--r--src/tests/md5_test.c93
1 files changed, 93 insertions, 0 deletions
diff --git a/src/tests/md5_test.c b/src/tests/md5_test.c
new file mode 100644
index 00000000..17972daa
--- /dev/null
+++ b/src/tests/md5_test.c
@@ -0,0 +1,93 @@
+/*
+ * Copyright (C) 2012 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
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+FILE_LICENCE ( GPL2_OR_LATER );
+
+/** @file
+ *
+ * MD5 tests
+ *
+ */
+
+#include <stdint.h>
+#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];
+};
+
+/** 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 } },
+};
+
+/** 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 } },
+};
+
+/**
+ * Perform MD5 self-test
+ *
+ */
+static void md5_test_exec ( void ) {
+ struct digest_algorithm *digest = &md5_algorithm;
+ struct md5_test_vector *test;
+ unsigned int i;
+ unsigned int j;
+
+ 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 );
+ }
+ }
+}
+
+/** MD5 self-test */
+struct self_test md5_test __self_test = {
+ .name = "md5",
+ .exec = md5_test_exec,
+};