test-hash-method.c revision 1b6228f23fb87733da13ac1057cf70fe513d30a0
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen/* Copyright (c) 2014 Dovecot authors, see the included COPYING file */
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen#include "test-lib.h"
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen#include "mmap-util.h"
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen#include "hash-method.h"
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen# define MAP_ANONYMOUS MAP_ANON
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen#endif
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainenstatic unsigned char *buf;
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainenstatic unsigned int buf_size;
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainenstatic void test_hash_method_one(const struct hash_method *method)
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen{
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen unsigned char *ctx, *digest;
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen unsigned int i;
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen test_begin(t_strdup_printf("hash method %s", method->name));
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen ctx = i_malloc(method->context_size);
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen digest = i_malloc(method->digest_size);
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen method->init(ctx);
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen /* make sure the code doesn't try to access data past boundaries */
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen for (i = 0; i < buf_size; i++)
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen method->loop(ctx, buf + buf_size - i, i);
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen method->result(ctx, digest);
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen i_free(ctx);
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen i_free(digest);
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen test_end();
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen}
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainenvoid test_hash_method(void)
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen{
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen unsigned int i;
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen buf_size = mmap_get_page_size();
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen#ifdef MAP_ANONYMOUS
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen buf = mmap(NULL, buf_size*2, PROT_READ | PROT_WRITE,
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen mprotect(buf + buf_size, buf_size, PROT_NONE);
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen#else
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen buf = i_malloc(buf_size);
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen#endif
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen memset(buf, 0, buf_size);
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen for (i = 0; hash_methods[i] != NULL; i++)
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen test_hash_method_one(hash_methods[i]);
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen}
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen