From 3a0c3bbfaf27a338dc5fec36ae91c271fb3af168 Mon Sep 17 00:00:00 2001 From: Timo Sirainen Date: Fri, 23 Jun 2017 09:24:40 +0300 Subject: [PATCH] lib-mail: test-message-header-hash - add more tests --- src/lib-mail/test-message-header-hash.c | 93 +++++++++++++++++-------- 1 file changed, 65 insertions(+), 28 deletions(-) diff --git a/src/lib-mail/test-message-header-hash.c b/src/lib-mail/test-message-header-hash.c index afdd332e20..b90de76b1a 100644 --- a/src/lib-mail/test-message-header-hash.c +++ b/src/lib-mail/test-message-header-hash.c @@ -5,49 +5,86 @@ #include "md5.h" #include "message-header-hash.h" -static const unsigned char test_input[] = +static const char test_input_with_nuls[] = { "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" - "\x20!?x??yz\x7f\x80\x90\xff-plop\xff"; -static const unsigned char test_output[] = - "?\t\n? !?x?yz?-plop?"; + "\x20!?x??yz\x7f\x80\x90\xff-plop\xff" +}; -static void test_dsync_mail_hash_more(void) +static const struct { + const char *input; + unsigned int version; + const char *output; +} tests[] = { + { "???hi???", 1, "???hi???" }, + + { test_input_with_nuls, 2, "?\t\n? !?x?yz?-plop?" }, + { "?hi?", 2, "?hi?" }, + { "\x01hi\x01", 2, "?hi?" }, + { "???hi???", 2, "?hi?" }, + { "\x01?hi??\x01", 2, "?hi?" }, + { "?\t?hi?\t?", 2, "?\t?hi?\t?" }, + { "\n\nhi\n\n", 2, "\n\nhi\n\n" }, +}; + +static void test_message_header_hash_more(void) { struct message_header_hash_context ctx; struct md5_context md5_ctx; unsigned char md5_input[MD5_RESULTLEN], md5_output[MD5_RESULTLEN]; - test_begin("dsync_mail_hash_more v2"); - md5_init(&md5_ctx); - i_zero(&ctx); - message_header_hash_more(&ctx, &hash_method_md5, &md5_ctx, 2, - test_input, sizeof(test_input)-1); - md5_final(&md5_ctx, md5_input); - - md5_init(&md5_ctx); - md5_update(&md5_ctx, test_output, sizeof(test_output)-1); - md5_final(&md5_ctx, md5_output); - - test_assert(memcmp(md5_input, md5_output, MD5_RESULTLEN) == 0); - - /* single byte at a time */ - md5_init(&md5_ctx); - i_zero(&ctx); - for (unsigned int i = 0; i < sizeof(test_input)-1; i++) { - message_header_hash_more(&ctx, &hash_method_md5, &md5_ctx, 2, - test_input + i, 1); - } - md5_final(&md5_ctx, md5_input); - test_assert(memcmp(md5_input, md5_output, MD5_RESULTLEN) == 0); + test_begin("message_header_hash_more"); + for (unsigned int i = 0; i < N_ELEMENTS(tests); i++) { + size_t input_len = tests[i].input == test_input_with_nuls ? + sizeof(test_input_with_nuls)-1 : strlen(tests[i].input); + md5_init(&md5_ctx); + i_zero(&ctx); + message_header_hash_more(&ctx, &hash_method_md5, &md5_ctx, + tests[i].version, + (const unsigned char *)tests[i].input, + input_len); + md5_final(&md5_ctx, md5_input); + + md5_init(&md5_ctx); + md5_update(&md5_ctx, tests[i].output, strlen(tests[i].output)); + md5_final(&md5_ctx, md5_output); + test_assert_idx(memcmp(md5_input, md5_output, MD5_RESULTLEN) == 0, i); + + /* single byte at a time */ + md5_init(&md5_ctx); + i_zero(&ctx); + for (unsigned int j = 0; j < input_len; j++) { + unsigned char chr = tests[i].input[j]; + message_header_hash_more(&ctx, &hash_method_md5, + &md5_ctx, tests[i].version, + &chr, 1); + } + md5_final(&md5_ctx, md5_input); + test_assert_idx(memcmp(md5_input, md5_output, MD5_RESULTLEN) == 0, i); + + /* random number of chars at a time */ + md5_init(&md5_ctx); + i_zero(&ctx); + for (unsigned int j = 0; j < input_len; ) { + const unsigned char *input_part = + (const unsigned char *)tests[i].input + j; + unsigned int len = rand() % (input_len - j) + 1; + message_header_hash_more(&ctx, &hash_method_md5, + &md5_ctx, tests[i].version, + input_part, len); + j += len; + } + md5_final(&md5_ctx, md5_input); + test_assert_idx(memcmp(md5_input, md5_output, MD5_RESULTLEN) == 0, i); + } test_end(); } int main(void) { static void (*test_functions[])(void) = { - test_dsync_mail_hash_more, + test_message_header_hash_more, NULL }; return test_run(test_functions);