test-message-header-decode.c revision 2d310b66c9e541e4a3db7bac89b01534f486bb95
2e37d45867d081db150ab78dad303b9077aea24fTimo Sirainen/* Copyright (c) 2009-2014 Dovecot authors, see the included COPYING file */
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "lib.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "buffer.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "str.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "charset-utf8.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "message-header-encode.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "message-header-decode.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "test-common.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include <stdlib.h>
7a7d2aa11e46195e2d92d6c337d7e78052a5ce67Timo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenbool charset_is_utf8(const char *charset ATTR_UNUSED) { return TRUE; }
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenint charset_to_utf8_begin(const char *charset ATTR_UNUSED,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen normalizer_func_t *normalizer ATTR_UNUSED,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct charset_translation **t_r ATTR_UNUSED) { return 0; }
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenvoid charset_to_utf8_end(struct charset_translation **t ATTR_UNUSED) {}
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenenum charset_result
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainencharset_to_utf8(struct charset_translation *t ATTR_UNUSED,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen const unsigned char *src, size_t *src_size, buffer_t *dest)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen{
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen buffer_append(dest, src, *src_size);
499fec3443374cc89fb8c83b8027c1614097d7a3Timo Sirainen return CHARSET_RET_OK;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen}
f5a7396b31762a1f876517e13ce9065820139f7cTimo Sirainen
f5a7396b31762a1f876517e13ce9065820139f7cTimo Sirainenstatic void test_message_header_decode(void)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen{
132487b9a47c2eb6fc80cfa2b0aaf82c6dc3af56Timo Sirainen static const char *data[] = {
132487b9a47c2eb6fc80cfa2b0aaf82c6dc3af56Timo Sirainen " \t=?utf-8?q?=c3=a4?= =?utf-8?q?=c3=a4?= b \t\r\n ", "ää b \t\r\n ",
132487b9a47c2eb6fc80cfa2b0aaf82c6dc3af56Timo Sirainen "a =?utf-8?q?=c3=a4?= b", "a ä b",
132487b9a47c2eb6fc80cfa2b0aaf82c6dc3af56Timo Sirainen "a =?utf-8?q?=c3=a4?= b", "a ä b",
132487b9a47c2eb6fc80cfa2b0aaf82c6dc3af56Timo Sirainen "a =?utf-8?q?=c3=a4?=\t\t\r\n =?utf-8?q?=c3=a4?= b", "a ää b",
132487b9a47c2eb6fc80cfa2b0aaf82c6dc3af56Timo Sirainen "a =?utf-8?q?=c3=a4?= x =?utf-8?q?=c3=a4?= b", "a ä x ä b",
132487b9a47c2eb6fc80cfa2b0aaf82c6dc3af56Timo Sirainen "a =?utf-8?b?w6TDpCDDpA==?= b", "a ää ä b",
132487b9a47c2eb6fc80cfa2b0aaf82c6dc3af56Timo Sirainen "=?utf-8?b?w6Qgw6Q=?=", "ä ä",
132487b9a47c2eb6fc80cfa2b0aaf82c6dc3af56Timo Sirainen };
132487b9a47c2eb6fc80cfa2b0aaf82c6dc3af56Timo Sirainen string_t *dest;
132487b9a47c2eb6fc80cfa2b0aaf82c6dc3af56Timo Sirainen unsigned int i;
132487b9a47c2eb6fc80cfa2b0aaf82c6dc3af56Timo Sirainen
132487b9a47c2eb6fc80cfa2b0aaf82c6dc3af56Timo Sirainen test_begin("message header decode");
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen dest = t_str_new(256);
132487b9a47c2eb6fc80cfa2b0aaf82c6dc3af56Timo Sirainen for (i = 0; i < N_ELEMENTS(data); i += 2) {
132487b9a47c2eb6fc80cfa2b0aaf82c6dc3af56Timo Sirainen str_truncate(dest, 0);
132487b9a47c2eb6fc80cfa2b0aaf82c6dc3af56Timo Sirainen message_header_decode_utf8((const unsigned char *)data[i],
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen strlen(data[i]), dest, FALSE);
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen test_assert(strcmp(str_c(dest), data[i+1]) == 0);
132487b9a47c2eb6fc80cfa2b0aaf82c6dc3af56Timo Sirainen }
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen test_end();
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen}
132487b9a47c2eb6fc80cfa2b0aaf82c6dc3af56Timo Sirainen
11352dc3e4b29f3d2763c82f8ea4f99e8daf4fa3Timo Sirainenstatic void test_message_header_decode_encode_random(void)
132487b9a47c2eb6fc80cfa2b0aaf82c6dc3af56Timo Sirainen{
132487b9a47c2eb6fc80cfa2b0aaf82c6dc3af56Timo Sirainen string_t *encoded, *decoded;
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen unsigned char buf[1024];
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen unsigned int i, j, buflen;
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen test_begin("message header encode & decode randomly");
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen encoded = t_str_new(256);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen decoded = t_str_new(256);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen for (i = 0; i < 1000; i++) {
cd8507179823de33d6e8242e10dbc15d136245b5Timo Sirainen /* fill only with 7bit data so we don't have to worry about
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen the data being valid UTF-8 */
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen for (j = 0; j < sizeof(buf); j++)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen buf[j] = rand() % 128;
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen buflen = rand() % sizeof(buf);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen str_truncate(encoded, 0);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen str_truncate(decoded, 0);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen /* test Q */
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen message_header_encode_q(buf, buflen, encoded, 0);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen message_header_decode_utf8(encoded->data, encoded->used,
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen decoded, NULL);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen test_assert(decoded->used == buflen &&
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen memcmp(decoded->data, buf, buflen) == 0);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen /* test B */
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen str_truncate(encoded, 0);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen str_truncate(decoded, 0);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen message_header_encode_b(buf, buflen, encoded, 0);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen message_header_decode_utf8(encoded->data, encoded->used,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen decoded, NULL);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen test_assert(decoded->used == buflen &&
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen memcmp(decoded->data, buf, buflen) == 0);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen }
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen test_end();
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen}
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainenint main(void)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen{
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen static void (*test_functions[])(void) = {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen test_message_header_decode,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen test_message_header_decode_encode_random,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen NULL
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen };
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return test_run(test_functions);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen}
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen