test-message-header-decode.c revision baf3e87e186453fda13bd21f7cbcb2efc8492e8b
2e37d45867d081db150ab78dad303b9077aea24fTimo Sirainen/* Copyright (c) 2009-2016 Dovecot authors, see the included COPYING file */
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen#include "lib.h"
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen#include "buffer.h"
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen#include "str.h"
472369cba85d9f7c995dda60e7cd01d78b4a960aTimo Sirainen#include "charset-utf8.h"
e28fa207d1a097fa6e4a867f74ee0761472ef1ceTimo Sirainen#include "message-header-encode.h"
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainen#include "message-header-decode.h"
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen#include "test-common.h"
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainen
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainen
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainenbool charset_is_utf8(const char *charset ATTR_UNUSED) { return TRUE; }
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainen
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainenint charset_to_utf8_begin(const char *charset ATTR_UNUSED,
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen normalizer_func_t *normalizer ATTR_UNUSED,
94d8e51119003d2bc5a100c663f90141f297385dTimo Sirainen struct charset_translation **t_r ATTR_UNUSED) { return 0; }
ef50336eefcb9ba99f73c6af37420eaf8857a39bTimo Sirainenvoid charset_to_utf8_end(struct charset_translation **t ATTR_UNUSED) {}
e28fa207d1a097fa6e4a867f74ee0761472ef1ceTimo Sirainen
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainenenum charset_result
ef50336eefcb9ba99f73c6af37420eaf8857a39bTimo Sirainencharset_to_utf8(struct charset_translation *t ATTR_UNUSED,
91b203fd2132510a47a4b34252c0ae0efd688a19Timo Sirainen const unsigned char *src, size_t *src_size, buffer_t *dest)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen{
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen buffer_append(dest, src, *src_size);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen return CHARSET_RET_OK;
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen}
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenstatic void test_message_header_decode(void)
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen{
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen static const char *data[] = {
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen " \t=?utf-8?q?=c3=a4?= =?utf-8?q?=c3=a4?= b \t\r\n ", "\xC3\xA4\xC3\xA4 b \t\r\n ",
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen "a =?utf-8?q?=c3=a4?= b", "a \xC3\xA4 b",
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen "a =?utf-8?q?=c3=a4?= b", "a \xC3\xA4 b",
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen "a =?utf-8?q?=c3=a4?=\t\t\r\n =?utf-8?q?=c3=a4?= b", "a \xC3\xA4\xC3\xA4 b",
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen "a =?utf-8?q?=c3=a4?= x =?utf-8?q?=c3=a4?= b", "a \xC3\xA4 x \xC3\xA4 b",
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen "a =?utf-8?b?w6TDpCDDpA==?= b", "a \xC3\xA4\xC3\xA4 \xC3\xA4 b",
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen "=?utf-8?b?w6Qgw6Q=?=", "\xC3\xA4 \xC3\xA4",
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen };
de486b59018016977015ef42e6071155b60e82e1Timo Sirainen string_t *dest;
de486b59018016977015ef42e6071155b60e82e1Timo Sirainen unsigned int i;
de486b59018016977015ef42e6071155b60e82e1Timo Sirainen
de486b59018016977015ef42e6071155b60e82e1Timo Sirainen test_begin("message header decode");
de486b59018016977015ef42e6071155b60e82e1Timo Sirainen
de4d9f77d6378a416c9963963eac5ac18b75ec0bTimo Sirainen dest = t_str_new(256);
de4d9f77d6378a416c9963963eac5ac18b75ec0bTimo Sirainen for (i = 0; i < N_ELEMENTS(data); i += 2) {
de486b59018016977015ef42e6071155b60e82e1Timo Sirainen str_truncate(dest, 0);
de486b59018016977015ef42e6071155b60e82e1Timo Sirainen message_header_decode_utf8((const unsigned char *)data[i],
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen strlen(data[i]), dest, NULL);
220e21750948941dc6e33b8f11b552fa21d7f81eTimo Sirainen test_assert(strcmp(str_c(dest), data[i+1]) == 0);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen }
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen test_end();
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen}
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainenstatic void test_message_header_decode_encode_random(void)
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen{
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen string_t *encoded, *decoded;
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen unsigned char buf[1024];
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen unsigned int i, j, buflen;
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen test_begin("message header encode & decode randomly");
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen encoded = t_str_new(256);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen decoded = t_str_new(256);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen for (i = 0; i < 1000; i++) {
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen /* fill only with 7bit data so we don't have to worry about
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen the data being valid UTF-8 */
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen for (j = 0; j < sizeof(buf); j++)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen buf[j] = rand() % 128;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen buflen = rand() % sizeof(buf);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen str_truncate(encoded, 0);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen str_truncate(decoded, 0);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen /* test Q */
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen message_header_encode_q(buf, buflen, encoded, 0);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen message_header_decode_utf8(encoded->data, encoded->used,
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen decoded, NULL);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen test_assert(decoded->used == buflen &&
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen memcmp(decoded->data, buf, buflen) == 0);
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen /* test B */
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen str_truncate(encoded, 0);
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen str_truncate(decoded, 0);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen message_header_encode_b(buf, buflen, encoded, 0);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen message_header_decode_utf8(encoded->data, encoded->used,
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen decoded, NULL);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen test_assert(decoded->used == buflen &&
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen memcmp(decoded->data, buf, buflen) == 0);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen }
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen test_end();
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen}
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenint main(void)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen{
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen static void (*const test_functions[])(void) = {
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen test_message_header_decode,
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen test_message_header_decode_encode_random,
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen NULL
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen };
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen return test_run(test_functions);
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen}
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen