test-istream-header-filter.c revision 270c7cd669b1ad4b69edb366dc5f22262948bfed
45312f52ff3a3d4c137447be4c7556500c2f8bf2Timo Sirainen/* Copyright (c) 2007-2016 Dovecot authors, see the included COPYING file */
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainentest_istream_run(struct istream *test_istream, struct istream *filter,
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen const unsigned char *data;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen test_istream_set_size(test_istream, input_len);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen test_assert(size == output_len && memcmp(data, output, size) == 0);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen /* run again to make sure it's still correct the second time */
f7539a17ea306191b53b8f5e752e228937df9ec3Timo Sirainen test_assert(size == output_len && memcmp(data, output, size) == 0);
f7539a17ea306191b53b8f5e752e228937df9ec3Timo Sirainenfilter_callback(struct header_filter_istream *input ATTR_UNUSED,
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen /* drop 1) first header, 2) X-Drop header */
f7539a17ea306191b53b8f5e752e228937df9ec3Timo Sirainenstatic void test_istream_filter(void)
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen static const char *exclude_headers[] = { "Subject", "To" };
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen const char *input = "From: foo\nFrom: abc\nTo: bar\nSubject: plop\nX-Drop: 1\n\nhello world\n";
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen const char *output = "From: abc\n\nhello world\n";
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen const unsigned char *data;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen test_begin("i_stream_create_header_filter(exclude)");
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen filter = i_stream_create_header_filter(istream,
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen filter2 = i_stream_create_header_filter(filter,
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen test_assert(size == output_len && memcmp(data, output, size) == 0);
ab8cf8fb93579981a687942e53bdb361b3b61bafTimo Sirainen test_assert(size == output_len && memcmp(data, output, size) == 0);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainenstatic void add_random_text(string_t *dest, unsigned int count)
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen unsigned int i;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen for (i = 0; i < count; i++)
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen str_append_c(dest, rand() % ('z'-'a'+1) + 'a');
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainenfilter2_callback(struct header_filter_istream *input ATTR_UNUSED,
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen if (hdr != NULL && strcmp(hdr->name, "To") == 0)
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainenstatic void test_istream_filter_large_buffer(void)
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen const unsigned char *data;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen const char *p;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen unsigned int i;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen test_begin("i_stream_create_header_filter(large buffer)");
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainen istream = test_istream_create_data(str_data(input), str_len(input));
b42697a5749b85659a24316d97f1c208d469e4e8Timo Sirainen test_istream_set_max_buffer_size(istream, 8192);
9e9f0cf3cc3ce546e8a433990c92dd9be6665df6Timo Sirainen filter = i_stream_create_header_filter(istream,
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen for (i = 0; i < 2; i++) {
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen test_assert(strncmp(str_c(input), str_c(output), prefix_len) == 0);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen test_assert(strcmp(p+1, str_c(output) + prefix_len) == 0);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen /* seek back and retry once with caching and different
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen buffer size */
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen test_istream_set_max_buffer_size(istream, 4096);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainenfilter3_callback(struct header_filter_istream *input ATTR_UNUSED,
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen unsigned int i;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen test_begin("i_stream_create_header_filter(callbacks)");
71c4ac143fefd57aeec6298489c4559b6f13c67bTimo Sirainen str_append(input, "\nTo: first line\n\tsecond line\n\t");
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen istream = test_istream_create_data(str_data(input), str_len(input));
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen test_istream_set_max_buffer_size(istream, 8192);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen filter = i_stream_create_header_filter(istream,
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen /* callback should be called exactly once for all the header input */
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen for (i = 0; i < 2; i++) {
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainen i_stream_skip(filter, i_stream_get_data_size(filter));
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen test_assert(strcmp(str_c(output), str_c(input)) == 0);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainenedit_callback(struct header_filter_istream *input,
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen if (hdr != NULL && strcasecmp(hdr->name, "To") == 0) {
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen /* modify To header */
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen i_stream_header_filter_add(input, new_to, strlen(new_to));
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainenstatic void test_istream_edit(void)
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainen const char *input = "From: foo\nTo: bar\n\nhello world\n";
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainen const char *output = "From: foo\nTo: 123\n\nhello world\n";
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen test_begin("i_stream_create_header_filter(edit)");
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen filter = i_stream_create_header_filter(istream,
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen test_istream_run(istream, filter, strlen(input), output);
cc0bb7692b8a47abca3279abbb49b073f7a27f21Timo Sirainenstatic void test_istream_end_body_with_lf(void)
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen const char *input = "From: foo\n\nhello world";
7230352f1177a8ed2b924c6992e751fd2ab9bc27Timo Sirainen const char *output = "From: foo\n\nhello world\n";
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen const unsigned char *data;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen test_begin("i_stream_create_header_filter(end_body_with_lf)");
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen filter = i_stream_create_header_filter(istream,
34f284752605dafdc7aefc343bb5b6cd2d2184b5Timo Sirainen test_assert(size == output_len && memcmp(data, output, size) == 0);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainenstrip_eoh_callback(struct header_filter_istream *input ATTR_UNUSED,
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen const char *input = "From: foo\nTo: bar\n\nhello world\n";
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen const char *output = "From: foo\nTo: bar\nhello world\n";
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen test_begin("i_stream_create_header_filter(edit)");
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen filter = i_stream_create_header_filter(istream,
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen HEADER_FILTER_EXCLUDE | HEADER_FILTER_NO_CR, NULL, 0,
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen test_istream_run(istream, filter, strlen(input), output);
a2a08c191a33bfaa758f9c68cafec2521b9a4472Timo Sirainen static void (*test_functions[])(void) = {