From f7548d6d28c313cf80e6f3ef89aed16a19815df1 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 28 Apr 2024 11:51:24 +0200 Subject: Adding upstream version 1:2.3.19.1+dfsg1. Signed-off-by: Daniel Baumann --- src/lib-mail/test-mail-html2text.c | 120 +++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 src/lib-mail/test-mail-html2text.c (limited to 'src/lib-mail/test-mail-html2text.c') diff --git a/src/lib-mail/test-mail-html2text.c b/src/lib-mail/test-mail-html2text.c new file mode 100644 index 0000000..73e93f7 --- /dev/null +++ b/src/lib-mail/test-mail-html2text.c @@ -0,0 +1,120 @@ +/* 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); +} -- cgit v1.2.3