dovecot-2.2: liblib: Added unit test for hash methods to make su...

dovecot at dovecot.org dovecot at dovecot.org
Wed May 7 10:03:26 UTC 2014


details:   http://hg.dovecot.org/dovecot-2.2/rev/2e7ac48c6072
changeset: 17330:2e7ac48c6072
user:      Timo Sirainen <tss at iki.fi>
date:      Wed May 07 13:02:29 2014 +0300
description:
liblib: Added unit test for hash methods to make sure they don't do read access beyond buffer.
This currently fails for MD4 and MD5, so they need to be fixed/replaced..

diffstat:

 src/lib/Makefile.am        |   1 +
 src/lib/test-hash-method.c |  43 +++++++++++++++++++++++++++++++++++++++++++
 src/lib/test-lib.c         |   1 +
 src/lib/test-lib.h         |   1 +
 4 files changed, 46 insertions(+), 0 deletions(-)

diffs (80 lines):

diff -r 6b40179a6868 -r 2e7ac48c6072 src/lib/Makefile.am
--- a/src/lib/Makefile.am	Wed May 07 13:01:17 2014 +0300
+++ b/src/lib/Makefile.am	Wed May 07 13:02:29 2014 +0300
@@ -275,6 +275,7 @@
 	test-buffer.c \
 	test-crc32.c \
 	test-hash-format.c \
+	test-hash-method.c \
 	test-hex-binary.c \
 	test-iso8601-date.c \
 	test-istream-base64-decoder.c \
diff -r 6b40179a6868 -r 2e7ac48c6072 src/lib/test-hash-method.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/test-hash-method.c	Wed May 07 13:02:29 2014 +0300
@@ -0,0 +1,43 @@
+/* Copyright (c) 2014 Dovecot authors, see the included COPYING file */
+
+#include "test-lib.h"
+#include "mmap-util.h"
+#include "hash-method.h"
+
+static unsigned char *buf;
+static unsigned int buf_size;
+
+static void test_hash_method_one(const struct hash_method *method)
+{
+	unsigned char *ctx, *digest;
+	unsigned int i;
+
+	test_begin(t_strdup_printf("hash method %s", method->name));
+
+	ctx = i_malloc(method->context_size);
+	digest = i_malloc(method->digest_size);
+	method->init(ctx);
+
+	/* make sure the code doesn't try to access data past boundaries */
+	for (i = 0; i < buf_size; i++)
+		method->loop(ctx, buf + buf_size - i, i);
+	method->result(ctx, digest);
+
+	i_free(ctx);
+	i_free(digest);
+	test_end();
+}
+
+void test_hash_method(void)
+{
+	unsigned int i;
+
+	buf_size = mmap_get_page_size();
+	buf = mmap(NULL, buf_size*2, PROT_READ | PROT_WRITE,
+		   MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
+	mprotect(buf + buf_size, buf_size, PROT_NONE);
+	memset(buf, 0, buf_size);
+
+	for (i = 0; hash_methods[i] != NULL; i++)
+		test_hash_method_one(hash_methods[i]);
+}
diff -r 6b40179a6868 -r 2e7ac48c6072 src/lib/test-lib.c
--- a/src/lib/test-lib.c	Wed May 07 13:01:17 2014 +0300
+++ b/src/lib/test-lib.c	Wed May 07 13:02:29 2014 +0300
@@ -12,6 +12,7 @@
 		test_buffer,
 		test_crc32,
 		test_hash_format,
+		test_hash_method,
 		test_hex_binary,
 		test_iso8601_date,
 		test_istream_base64_decoder,
diff -r 6b40179a6868 -r 2e7ac48c6072 src/lib/test-lib.h
--- a/src/lib/test-lib.h	Wed May 07 13:01:17 2014 +0300
+++ b/src/lib/test-lib.h	Wed May 07 13:02:29 2014 +0300
@@ -11,6 +11,7 @@
 void test_buffer(void);
 void test_crc32(void);
 void test_hash_format(void);
+void test_hash_method(void);
 void test_hex_binary(void);
 void test_iso8601_date(void);
 void test_istream_base64_decoder(void);


More information about the dovecot-cvs mailing list