1 /* Copyright (c) 2015-2018 Dovecot authors, see the included COPYING file */
2
3 #include "lib.h"
4 #include "str.h"
5 #include "istream.h"
6 #include "mail-html2text.h"
7 #include "test-common.h"
8
9 static const struct {
10 const char *input;
11 const char *output;
12 } tests[] = {
13 { "&&aaaaaaaaaa", "" },
14
15 { "a&<♣>b",
16 "a&<\xE2\x99\xA3>b" },
17 { "&", "" },
18 { "&", "" },
19
20 { "a<style>stylesheet is ignored</style>b",
21 "a b" },
22 { "a<stylea>b</stylea>c",
23 "a b c" },
24 { "a<!--x <p foo=\"bar\">commented tags ignored also</p> y-->b",
25 "ab" },
26 { "a<script>javascript <p>foo</p> ignored</script>b",
27 "a b" },
28 { "a<scripta>b</scripta>c",
29 "a b c" },
30 { "a<blockquote><blockquote>second level</blockquote>ignored</blockquote>b",
31 "a b" },
32 { "a<![CDATA[<style>]] >b</style>]]>c",
33 "a<style>]] >b</style>c" },
34
35 { "a<foo", "a" },
36 { "a<blockquote", "a" },
37 { "a<blockquote>foo</blockquote", "a " },
38 { "a<", "a" },
39 { "a<![CDATA[b", "ab" },
40 { "a<![CDATA[b]]", "ab" },
41 { "aä", "a\xC3\xA4" },
42 { "aä", "a\xC3\xA4" },
43 { "€", "\xE2\x82\xAC" },
44 { "&#deee;", "" }, // invalid codepoint
45 };
46
47 static const char *test_blockquote_input[] = {
48 "a<blockquote>b<blockquote><blockquote>c</blockquote>d</blockquote>e</blockquote>f",
49 "a&<blockquote>b&<blockquote>&<blockquote>&c</blockquote>d&</blockquote>&e</blockquote>f&",
50 NULL
51 };
52
53 static const char *test_blockquote_output[] = {
54 "a\n> b\n> \n> c\n> d\n> e\nf",
55 "a&\n> b&\n> &\n> &c\n> d&\n> &e\nf&",
56 NULL
57 };
58
test_mail_html2text(void)59 static void test_mail_html2text(void)
60 {
61 string_t *str = t_str_new(128);
62 struct mail_html2text *ht;
63 unsigned int i, j;
64
65 test_begin("mail_html2text()");
66 for (i = 0; i < N_ELEMENTS(tests); i++) {
67 ht = mail_html2text_init(MAIL_HTML2TEXT_FLAG_SKIP_QUOTED);
68 for (j = 0; tests[i].input[j] != '\0'; j++) {
69 unsigned char c = tests[i].input[j];
70 mail_html2text_more(ht, &c, 1, str);
71 }
72 test_assert_idx(strcmp(str_c(str), tests[i].output) == 0, i);
73 mail_html2text_deinit(&ht);
74 str_truncate(str, 0);
75 }
76
77 /* test without skipping quoted */
78 for (unsigned int i = 0; test_blockquote_input[i] != NULL; i++) {
79 str_truncate(str, 0);
80 ht = mail_html2text_init(0);
81 mail_html2text_more(ht, (const void *)test_blockquote_input[i],
82 strlen(test_blockquote_input[i]), str);
83 test_assert_idx(strcmp(str_c(str), test_blockquote_output[i]) == 0, i);
84 mail_html2text_deinit(&ht);
85 }
86
87 test_end();
88 }
89
test_mail_html2text_random(void)90 static void test_mail_html2text_random(void)
91 {
92 string_t *str = t_str_new(128);
93 struct mail_html2text *ht;
94
95 test_begin("mail_html2text() random");
96 for (unsigned int i = 0; i < 1000; i++) {
97 char valid_chars[] = { '0', 'a', '<', '>', '&', ';', '\\', '\'', '"', '/' };
98 unsigned char s[2];
99
100 ht = mail_html2text_init(0);
101 for (unsigned int i = 0; i < 100; i++) {
102 s[0] = valid_chars[i_rand_limit(N_ELEMENTS(valid_chars))];
103 s[1] = valid_chars[i_rand_limit(N_ELEMENTS(valid_chars))];
104 mail_html2text_more(ht, s, i_rand_minmax(1, 2), str);
105 }
106 mail_html2text_deinit(&ht);
107 str_truncate(str, 0);
108 }
109 test_end();
110 }
111
main(void)112 int main(void)
113 {
114 static void (*const test_functions[])(void) = {
115 test_mail_html2text,
116 test_mail_html2text_random,
117 NULL
118 };
119 return test_run(test_functions);
120 }
121