bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2015-2018 Dovecot authors, see the included COPYING file */
9366765479f32a4df248d015c595d0f46cbf83b7Timo Sirainenstatic void test_charset_utf8_common(const char *input_charset)
b7324e421e2132cbbf753e6fdbe675bbaecdf929Timo Sirainen static const struct {
f3779c0540f0dc8700fd2aef922d69dcc6b0194eTimo Sirainen { "p\xC3\xA4\xC3", "p\xC3\xA4", CHARSET_RET_INCOMPLETE_INPUT },
f3779c0540f0dc8700fd2aef922d69dcc6b0194eTimo Sirainen { "p\xC3\xA4\xC3""a", "p\xC3\xA4"UNICODE_REPLACEMENT_CHAR_UTF8"a", CHARSET_RET_INVALID_INPUT }
9366765479f32a4df248d015c595d0f46cbf83b7Timo Sirainen unsigned int i;
9366765479f32a4df248d015c595d0f46cbf83b7Timo Sirainen test_assert_idx(charset_to_utf8_str(input_charset, NULL,
9366765479f32a4df248d015c595d0f46cbf83b7Timo Sirainen test_assert_idx(strcmp(tests[i].output, str_c(str)) == 0, i);
9366765479f32a4df248d015c595d0f46cbf83b7Timo Sirainen test_assert_idx(result == tests[i].result, i);
f9291653e3d42d630b9212ceb9290c974e51597aTimo Sirainen /* check that E2BIG handling works. We assume that iconv() is called
f9291653e3d42d630b9212ceb9290c974e51597aTimo Sirainen with 8192 byte buffer (tmpbuf[8192]) */
f9291653e3d42d630b9212ceb9290c974e51597aTimo Sirainen for (i = 0; i < 8190; i++)
f9291653e3d42d630b9212ceb9290c974e51597aTimo Sirainen for (i = 0; i < 256; i++) {
f9291653e3d42d630b9212ceb9290c974e51597aTimo Sirainen test_assert_idx(charset_to_utf8_str(input_charset, NULL,
b7324e421e2132cbbf753e6fdbe675bbaecdf929Timo Sirainen static const struct {
19ed8f08b23d6ed204e6b27e5d1c0c6fe6bb11ddPhil Carmody { "ISO-8859-1", "p\xE4\xE4", "p\xC3\xA4\xC3\xA4", CHARSET_RET_OK },
5f7fcc523deee2259146846d5fc9fa61f0299d85Timo Sirainen { "UTF-7", "+AOQA5AD2AOQA9gDkAPYA5AD2AOQA9gDkAPYA5AD2AOQA9gDkAPYA5AD2AOQA9gDkAPYA5AD2AOQA9gDkAPYA5AD2AOQA9gDk",
19ed8f08b23d6ed204e6b27e5d1c0c6fe6bb11ddPhil Carmody "\xC3\xA4\xC3\xA4\xC3\xB6\xC3\xA4\xC3\xB6\xC3\xA4\xC3\xB6\xC3\xA4"
19ed8f08b23d6ed204e6b27e5d1c0c6fe6bb11ddPhil Carmody "\xC3\xB6\xC3\xA4\xC3\xB6\xC3\xA4\xC3\xB6\xC3\xA4\xC3\xB6\xC3\xA4"
19ed8f08b23d6ed204e6b27e5d1c0c6fe6bb11ddPhil Carmody "\xC3\xB6\xC3\xA4\xC3\xB6\xC3\xA4\xC3\xB6\xC3\xA4\xC3\xB6\xC3\xA4"
19ed8f08b23d6ed204e6b27e5d1c0c6fe6bb11ddPhil Carmody "\xC3\xB6\xC3\xA4\xC3\xB6\xC3\xA4\xC3\xB6\xC3\xA4\xC3\xB6\xC3\xA4"
19ed8f08b23d6ed204e6b27e5d1c0c6fe6bb11ddPhil Carmody "\xC3\xB6\xC3\xA4\xC3\xB6\xC3\xA4", CHARSET_RET_OK }
9366765479f32a4df248d015c595d0f46cbf83b7Timo Sirainen unsigned int i;
9366765479f32a4df248d015c595d0f46cbf83b7Timo Sirainen test_assert_idx(charset_to_utf8_str(tests[i].charset, NULL,
9366765479f32a4df248d015c595d0f46cbf83b7Timo Sirainen test_assert_idx(strcmp(tests[i].output, str_c(str)) == 0, i);
9366765479f32a4df248d015c595d0f46cbf83b7Timo Sirainen test_assert_idx(result == tests[i].result, i);
e9257b0d30aa68dc968b6347d9f3f5ac4c8b5c00Timo Sirainen test_assert_idx(charset_to_utf8_begin(tests[i].charset, NULL, &trans) == 0, i);
e9257b0d30aa68dc968b6347d9f3f5ac4c8b5c00Timo Sirainen for (pos = 0, limit = 1; limit <= len; pos += left, limit++) {
e9257b0d30aa68dc968b6347d9f3f5ac4c8b5c00Timo Sirainen result = charset_to_utf8(trans, (const void *)(tests[i].input + pos),
e9257b0d30aa68dc968b6347d9f3f5ac4c8b5c00Timo Sirainen test_assert_idx(strcmp(tests[i].output, str_c(str)) == 0, i);
e9257b0d30aa68dc968b6347d9f3f5ac4c8b5c00Timo Sirainen test_assert_idx(result == tests[i].result, i);
9366765479f32a4df248d015c595d0f46cbf83b7Timo Sirainen /* Use //IGNORE just to force handling to be done by iconv
9366765479f32a4df248d015c595d0f46cbf83b7Timo Sirainen instead of our own UTF-8 routines. */
b7324e421e2132cbbf753e6fdbe675bbaecdf929Timo Sirainen static const struct {
64d895bcca177ee840268180ca7a4e3841295613Timo Sirainen unsigned int i;
64d895bcca177ee840268180ca7a4e3841295613Timo Sirainen /* we don't care about checking the result. we only want to
64d895bcca177ee840268180ca7a4e3841295613Timo Sirainen verify that there's no crash. */
64d895bcca177ee840268180ca7a4e3841295613Timo Sirainen (void)charset_to_utf8_str(tests[i].charset, NULL,
8a0ef83121a9ae375448efb2e1f08e136bf04e22Timo Sirainenstatic void test_charset_iconv_utf7_state(void)
8a0ef83121a9ae375448efb2e1f08e136bf04e22Timo Sirainen unsigned char nextbuf[5+CHARSET_MAX_PENDING_BUF_SIZE+1];
8a0ef83121a9ae375448efb2e1f08e136bf04e22Timo Sirainen test_assert(charset_to_utf8_begin("UTF-7", NULL, &trans) == 0);
8a0ef83121a9ae375448efb2e1f08e136bf04e22Timo Sirainen test_assert(charset_to_utf8(trans, (const void *)"a+", &size, str) == CHARSET_RET_INCOMPLETE_INPUT);
8a0ef83121a9ae375448efb2e1f08e136bf04e22Timo Sirainen test_assert(charset_to_utf8(trans, nextbuf, &size, str) == CHARSET_RET_OK);
8a0ef83121a9ae375448efb2e1f08e136bf04e22Timo Sirainen test_assert(strcmp(str_c(str), "a\xC3\xA4???????????") == 0);
baf3e87e186453fda13bd21f7cbcb2efc8492e8bTimo Sirainen static void (*const test_functions[])(void) = {