/* Copyright (c) 2015-2018 Dovecot authors, see the included COPYING file */ #include "lib.h" #include "str.h" #include "istream.h" #include "mail-html2text.h" #include "test-common.h" static const struct { const char *input; const char *output; } tests[] = { { "&&aaaaaaaaaa", "" }, { "a&<♣>b", "a&<\xE2\x99\xA3>b" }, { "&", "" }, { "&", "" }, { "ab", "a b" }, { "abc", "a b c" }, { "ab", "ab" }, { "ab", "a b" }, { "abc", "a b c" }, { "a
second level
ignored
b", "a b" }, { "a]] >b]]>c", "ac" }, { "afoob
c
d
ef", "a&
b&
&
&c
d&
&e
f&", NULL }; static const char *test_blockquote_output[] = { "a\n> b\n> \n> c\n> d\n> e\nf", "a&\n> b&\n> &\n> &c\n> d&\n> &e\nf&", NULL }; static void test_mail_html2text(void) { string_t *str = t_str_new(128); struct mail_html2text *ht; unsigned int i, j; test_begin("mail_html2text()"); for (i = 0; i < N_ELEMENTS(tests); i++) { ht = mail_html2text_init(MAIL_HTML2TEXT_FLAG_SKIP_QUOTED); for (j = 0; tests[i].input[j] != '\0'; j++) { unsigned char c = tests[i].input[j]; mail_html2text_more(ht, &c, 1, str); } test_assert_idx(strcmp(str_c(str), tests[i].output) == 0, i); mail_html2text_deinit(&ht); str_truncate(str, 0); } /* test without skipping quoted */ for (unsigned int i = 0; test_blockquote_input[i] != NULL; i++) { str_truncate(str, 0); ht = mail_html2text_init(0); mail_html2text_more(ht, (const void *)test_blockquote_input[i], strlen(test_blockquote_input[i]), str); test_assert_idx(strcmp(str_c(str), test_blockquote_output[i]) == 0, i); mail_html2text_deinit(&ht); } test_end(); } static void test_mail_html2text_random(void) { string_t *str = t_str_new(128); struct mail_html2text *ht; test_begin("mail_html2text() random"); for (unsigned int i = 0; i < 1000; i++) { char valid_chars[] = { '0', 'a', '<', '>', '&', ';', '\\', '\'', '"', '/' }; unsigned char s[2]; ht = mail_html2text_init(0); for (unsigned int i = 0; i < 100; i++) { s[0] = valid_chars[i_rand_limit(N_ELEMENTS(valid_chars))]; s[1] = valid_chars[i_rand_limit(N_ELEMENTS(valid_chars))]; mail_html2text_more(ht, s, i_rand_minmax(1, 2), str); } mail_html2text_deinit(&ht); str_truncate(str, 0); } test_end(); } int main(void) { static void (*const test_functions[])(void) = { test_mail_html2text, test_mail_html2text_random, NULL }; return test_run(test_functions); }