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 */ ht = mail_html2text_init(0); mail_html2text_more(ht, (const void *)test_blockquote_input, strlen(test_blockquote_input), str); test_assert(strcmp(str_c(str), test_blockquote_output) == 0); mail_html2text_deinit(&ht); test_end(); }
int message_snippet_generate(struct istream *input, unsigned int max_snippet_chars, string_t *snippet) { struct message_parser_ctx *parser; struct message_part *parts; struct message_decoder_context *decoder; struct message_block raw_block, block; struct snippet_context ctx; pool_t pool; int ret; memset(&ctx, 0, sizeof(ctx)); pool = pool_alloconly_create("message snippet", 1024); ctx.snippet = snippet; ctx.chars_left = max_snippet_chars; parser = message_parser_init(pool_datastack_create(), input, 0, 0); decoder = message_decoder_init(NULL, 0); while ((ret = message_parser_parse_next_block(parser, &raw_block)) > 0) { if (!message_decoder_decode_next_block(decoder, &raw_block, &block)) continue; if (block.size == 0) { const char *ct; if (block.hdr != NULL) continue; /* end of headers - verify that we can use this Content-Type. we get here only once, because we always handle only one non-multipart MIME part. */ ct = message_decoder_current_content_type(decoder); if (ct == NULL) /* text/plain */ ; else if (mail_html2text_content_type_match(ct)) { ctx.html2text = mail_html2text_init(MAIL_HTML2TEXT_FLAG_SKIP_QUOTED); ctx.plain_output = buffer_create_dynamic(pool, 1024); } else if (strncasecmp(ct, "text/", 5) != 0) break; continue; } if (!snippet_generate(&ctx, block.data, block.size)) break; } i_assert(ret != 0); message_decoder_deinit(&decoder); message_parser_deinit(&parser, &parts); if (ctx.html2text != NULL) mail_html2text_deinit(&ctx.html2text); pool_unref(&pool); return input->stream_errno == 0 ? 0 : -1; }
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[rand() % N_ELEMENTS(valid_chars)]; s[1] = valid_chars[rand() % N_ELEMENTS(valid_chars)]; mail_html2text_more(ht, s, rand()%2+1, str); } mail_html2text_deinit(&ht); str_truncate(str, 0); } test_end(); }