bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2015-2018 Dovecot authors, see the included COPYING file */
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen#include "lib.h"
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen#include "str.h"
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen#include "istream.h"
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen#include "message-snippet.h"
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen#include "test-common.h"
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen
b7324e421e2132cbbf753e6fdbe675bbaecdf929Timo Sirainenstatic const struct {
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen const char *input;
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen unsigned int max_snippet_chars;
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen const char *output;
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen} tests[] = {
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen { "Content-Type: text/plain\n"
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen "\n"
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen "1234567890 234567890",
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen 12,
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen "1234567890 2" },
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen { "Content-Type: text/plain\n"
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen "\n"
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen "line1\n>quote2\nline2\n",
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen 100,
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen "line1 line2" },
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen { "Content-Type: text/plain\n"
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen "\n"
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen "line1\n>quote2\n> quote3\n > line4\n\n \t\t \nline5\n \t ",
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen 100,
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen "line1 > line4 line5" },
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen { "Content-Type: text/plain; charset=utf-8\n"
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen "\n"
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen "hyv\xC3\xA4\xC3\xA4 p\xC3\xA4iv\xC3\xA4\xC3\xA4",
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen 11,
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen "hyv\xC3\xA4\xC3\xA4 p\xC3\xA4iv\xC3\xA4" },
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen { "Content-Type: text/plain; charset=utf-8\n"
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen "Content-Transfer-Encoding: quoted-printable\n"
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen "\n"
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen "hyv=C3=A4=C3=A4 p=C3=A4iv=C3=A4=C3=A4",
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen 11,
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen "hyv\xC3\xA4\xC3\xA4 p\xC3\xA4iv\xC3\xA4" },
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen { "Content-Transfer-Encoding: quoted-printable\n"
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen "Content-Type: text/html;\n"
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen " charset=utf-8\n"
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen "\n"
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen "<html><head><meta http-equiv=3D\"Content-Type\" content=3D\"text/html =\n"
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen "charset=3Dutf-8\"></head><body style=3D\"word-wrap: break-word; =\n"
00a50d6dc74e15b5b83afcb8c12c2109a2ca376dTimo Sirainen "-webkit-nbsp-mode: space; -webkit-line-break: after-white-space;\" =\n"
00a50d6dc74e15b5b83afcb8c12c2109a2ca376dTimo Sirainen "class=3D\"\">Hi,<div class=3D\"\"><br class=3D\"\"></div><div class=3D\"\">How =\n"
00a50d6dc74e15b5b83afcb8c12c2109a2ca376dTimo Sirainen "is it going? <blockquote>quoted text is ignored</blockquote>\n"
00a50d6dc74e15b5b83afcb8c12c2109a2ca376dTimo Sirainen "&gt; -foo\n"
00a50d6dc74e15b5b83afcb8c12c2109a2ca376dTimo Sirainen "</div><br =class=3D\"\"></body></html>=\n",
00a50d6dc74e15b5b83afcb8c12c2109a2ca376dTimo Sirainen 100,
00a50d6dc74e15b5b83afcb8c12c2109a2ca376dTimo Sirainen "Hi, How is it going? > -foo" },
00a50d6dc74e15b5b83afcb8c12c2109a2ca376dTimo Sirainen
00a50d6dc74e15b5b83afcb8c12c2109a2ca376dTimo Sirainen { "Content-Transfer-Encoding: quoted-printable\n"
00a50d6dc74e15b5b83afcb8c12c2109a2ca376dTimo Sirainen "Content-Type: application/xhtml+xml;\n"
00a50d6dc74e15b5b83afcb8c12c2109a2ca376dTimo Sirainen " charset=utf-8\n"
00a50d6dc74e15b5b83afcb8c12c2109a2ca376dTimo Sirainen "\n"
00a50d6dc74e15b5b83afcb8c12c2109a2ca376dTimo Sirainen "<html><head><meta http-equiv=3D\"Content-Type\" content=3D\"text/html =\n"
00a50d6dc74e15b5b83afcb8c12c2109a2ca376dTimo Sirainen "charset=3Dutf-8\"></head><body style=3D\"word-wrap: break-word; =\n"
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen "-webkit-nbsp-mode: space; -webkit-line-break: after-white-space;\" =\n"
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen "class=3D\"\">Hi,<div class=3D\"\"><br class=3D\"\"></div><div class=3D\"\">How =\n"
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen "is it going? <blockquote>quoted text is ignored</blockquote>\n"
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen "&gt; -foo\n"
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen "</div><br =class=3D\"\"></body></html>=\n",
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen 100,
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen "Hi, How is it going? > -foo" },
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen};
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainenstatic void test_message_snippet(void)
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen{
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen string_t *str = t_str_new(128);
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen struct istream *input;
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen unsigned int i;
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen test_begin("message snippet");
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen for (i = 0; i < N_ELEMENTS(tests); i++) {
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen str_truncate(str, 0);
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen input = i_stream_create_from_data(tests[i].input, strlen(tests[i].input));
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen test_assert_idx(message_snippet_generate(input, tests[i].max_snippet_chars, str) == 0, i);
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen test_assert_idx(strcmp(tests[i].output, str_c(str)) == 0, i);
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen i_stream_destroy(&input);
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen }
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen test_end();
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen}
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainenint main(void)
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen{
baf3e87e186453fda13bd21f7cbcb2efc8492e8bTimo Sirainen static void (*const test_functions[])(void) = {
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen test_message_snippet,
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen NULL
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen };
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen return test_run(test_functions);
7f7be2cbf68f8a202a688d5bc50f82483d461643Timo Sirainen}