test-message-snippet.c revision 7f7be2cbf68f8a202a688d5bc50f82483d461643
2454dfa32c93c20a8522c6ed42fe057baaac9f9aStephan Bosch/* Copyright (c) 2015 Dovecot authors, see the included COPYING file */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "lib.h"
e05a4c4136fec723f019bee8383103080203f127Timo Sirainen#include "str.h"
50c617761ee9653bd44646a95178773a3686d62eTimo Sirainen#include "istream.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "message-snippet.h"
e2d268e9531227ead6a98466ecf3c046c857ef70Timo Sirainen#include "test-common.h"
2b9dbb270ad82e58d5f3581436e6f143176d5819Timo Sirainen
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainenstatic struct {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const char *input;
c04f9a724a7b3cc649485a61b0a540868d25d71bTimo Sirainen unsigned int max_snippet_chars;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const char *output;
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen} tests[] = {
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen { "Content-Type: text/plain\n"
128ea07dab8d67124ea74bcc085a478784b6358aTimo Sirainen "\n"
c04f9a724a7b3cc649485a61b0a540868d25d71bTimo Sirainen "1234567890 234567890",
e2d268e9531227ead6a98466ecf3c046c857ef70Timo Sirainen 12,
e2d268e9531227ead6a98466ecf3c046c857ef70Timo Sirainen "1234567890 2" },
c04f9a724a7b3cc649485a61b0a540868d25d71bTimo Sirainen { "Content-Type: text/plain\n"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen "\n"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen "line1\n>quote2\nline2\n",
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen 100,
eb0816090cf5a549280ad783b9aa6fec199d36baTimo Sirainen "line1 line2" },
eb0816090cf5a549280ad783b9aa6fec199d36baTimo Sirainen { "Content-Type: text/plain\n"
9f19a50d5966643c4d1c5ca06868ac2ad31bc4d5Timo Sirainen "\n"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen "line1\n>quote2\n> quote3\n > line4\n\n \t\t \nline5\n \t ",
c04f9a724a7b3cc649485a61b0a540868d25d71bTimo Sirainen 100,
eb0816090cf5a549280ad783b9aa6fec199d36baTimo Sirainen "line1 > line4 line5" },
e2d268e9531227ead6a98466ecf3c046c857ef70Timo Sirainen { "Content-Type: text/plain; charset=utf-8\n"
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainen "\n"
eb0816090cf5a549280ad783b9aa6fec199d36baTimo Sirainen "hyv\xC3\xA4\xC3\xA4 p\xC3\xA4iv\xC3\xA4\xC3\xA4",
eb0816090cf5a549280ad783b9aa6fec199d36baTimo Sirainen 11,
2b9dbb270ad82e58d5f3581436e6f143176d5819Timo Sirainen "hyv\xC3\xA4\xC3\xA4 p\xC3\xA4iv\xC3\xA4" },
900bb5e316d030cdebff7ee128ce65881dfb27f7Timo Sirainen { "Content-Type: text/plain; charset=utf-8\n"
900bb5e316d030cdebff7ee128ce65881dfb27f7Timo Sirainen "Content-Transfer-Encoding: quoted-printable\n"
900bb5e316d030cdebff7ee128ce65881dfb27f7Timo Sirainen "\n"
50c617761ee9653bd44646a95178773a3686d62eTimo Sirainen "hyv=C3=A4=C3=A4 p=C3=A4iv=C3=A4=C3=A4",
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainen 11,
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainen "hyv\xC3\xA4\xC3\xA4 p\xC3\xA4iv\xC3\xA4" },
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainen
50c617761ee9653bd44646a95178773a3686d62eTimo Sirainen { "Content-Transfer-Encoding: quoted-printable\n"
50c617761ee9653bd44646a95178773a3686d62eTimo Sirainen "Content-Type: text/html;\n"
50c617761ee9653bd44646a95178773a3686d62eTimo Sirainen " charset=utf-8\n"
50c617761ee9653bd44646a95178773a3686d62eTimo Sirainen "\n"
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainen "<html><head><meta http-equiv=3D\"Content-Type\" content=3D\"text/html =\n"
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainen "charset=3Dutf-8\"></head><body style=3D\"word-wrap: break-word; =\n"
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainen "-webkit-nbsp-mode: space; -webkit-line-break: after-white-space;\" =\n"
50c617761ee9653bd44646a95178773a3686d62eTimo Sirainen "class=3D\"\">Hi,<div class=3D\"\"><br class=3D\"\"></div><div class=3D\"\">How =\n"
50c617761ee9653bd44646a95178773a3686d62eTimo Sirainen "is it going? <blockquote>quoted text is ignored</blockquote>\n"
50c617761ee9653bd44646a95178773a3686d62eTimo Sirainen "&gt; -foo\n"
50c617761ee9653bd44646a95178773a3686d62eTimo Sirainen "</div><br =class=3D\"\"></body></html>=\n",
eb0816090cf5a549280ad783b9aa6fec199d36baTimo Sirainen 100,
897368f75a68f97e172ba76540759a10c557f1c3Timo Sirainen "Hi, How is it going? > -foo" },
50c617761ee9653bd44646a95178773a3686d62eTimo Sirainen};
50c617761ee9653bd44646a95178773a3686d62eTimo Sirainen
50c617761ee9653bd44646a95178773a3686d62eTimo Sirainenstatic void test_message_snippet(void)
50c617761ee9653bd44646a95178773a3686d62eTimo Sirainen{
eb0816090cf5a549280ad783b9aa6fec199d36baTimo Sirainen string_t *str = t_str_new(128);
eb0816090cf5a549280ad783b9aa6fec199d36baTimo Sirainen struct istream *input;
900bb5e316d030cdebff7ee128ce65881dfb27f7Timo Sirainen unsigned int i;
900bb5e316d030cdebff7ee128ce65881dfb27f7Timo Sirainen
eb0816090cf5a549280ad783b9aa6fec199d36baTimo Sirainen test_begin("message snippet");
eb0816090cf5a549280ad783b9aa6fec199d36baTimo Sirainen for (i = 0; i < N_ELEMENTS(tests); i++) {
01937f71b3ae0d5b30b813372f44a3e7e86c89dcTimo Sirainen str_truncate(str, 0);
e2d268e9531227ead6a98466ecf3c046c857ef70Timo Sirainen input = i_stream_create_from_data(tests[i].input, strlen(tests[i].input));
e2d268e9531227ead6a98466ecf3c046c857ef70Timo Sirainen test_assert_idx(message_snippet_generate(input, tests[i].max_snippet_chars, str) == 0, i);
e2d268e9531227ead6a98466ecf3c046c857ef70Timo Sirainen test_assert_idx(strcmp(tests[i].output, str_c(str)) == 0, i);
e2d268e9531227ead6a98466ecf3c046c857ef70Timo Sirainen i_stream_destroy(&input);
e2d268e9531227ead6a98466ecf3c046c857ef70Timo Sirainen }
c04f9a724a7b3cc649485a61b0a540868d25d71bTimo Sirainen test_end();
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
c04f9a724a7b3cc649485a61b0a540868d25d71bTimo Sirainen
eb0816090cf5a549280ad783b9aa6fec199d36baTimo Sirainenint main(void)
c04f9a724a7b3cc649485a61b0a540868d25d71bTimo Sirainen{
1f1ee8db68d9ae1604350801cd8dc33ebe29fe8aTimo Sirainen static void (*test_functions[])(void) = {
eb0816090cf5a549280ad783b9aa6fec199d36baTimo Sirainen test_message_snippet,
900bb5e316d030cdebff7ee128ce65881dfb27f7Timo Sirainen NULL
900bb5e316d030cdebff7ee128ce65881dfb27f7Timo Sirainen };
900bb5e316d030cdebff7ee128ce65881dfb27f7Timo Sirainen return test_run(test_functions);
eb0816090cf5a549280ad783b9aa6fec199d36baTimo Sirainen}
eb0816090cf5a549280ad783b9aa6fec199d36baTimo Sirainen