test-istream-header-filter.c revision d4488f314d0c799c5f5b8a7890e17eef868f7a2c
2454dfa32c93c20a8522c6ed42fe057baaac9f9aStephan Bosch/* Copyright (c) 2007-2016 Dovecot authors, see the included COPYING file */
bd63b5b860658b01b1f46f26d406e1e4a9dc019aTimo Sirainenstatic void run_callback(struct header_filter_istream *input,
a443e5aaf632257bfd1e7aa9b3c42c09512bbe43Timo Sirainen struct run_ctx run_ctx = { .callback = callback, .null_hdr_seen = FALSE };
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen const unsigned char *data;
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen filter = i_stream_create_header_filter(test_istream, flags, NULL, 0,
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen test_istream_set_size(test_istream, input_len);
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen test_assert(size == output_len && memcmp(data, output, size) == 0);
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen /* run again to make sure it's still correct the second time */
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen test_assert(run_ctx.null_hdr_seen == run_ctx.callback_called);
bffaca7da2825c2a77aa46022358e2f8400a0f94Timo Sirainen test_assert(size == output_len && memcmp(data, output, size) == 0);
f162f350e1eacce12288a565657e769d7976dd7fTimo Sirainen test_assert(i_stream_stat(filter, TRUE, &st) == 0 &&
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainenfilter_callback(struct header_filter_istream *input ATTR_UNUSED,
a443e5aaf632257bfd1e7aa9b3c42c09512bbe43Timo Sirainen /* drop 1) first header, 2) X-Drop header */
5a37e34b1b5acf453372cd112c70bb4e46b4bee2Timo Sirainenstatic void test_istream_filter(void)
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen static const char *exclude_headers[] = { "Subject", "To" };
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen const char *input = "From: foo\nFrom: abc\nTo: bar\nSubject: plop\nX-Drop: 1\n\nhello world\n";
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen const char *output = "From: abc\n\nhello world\n";
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen const unsigned char *data;
ef2b2ef2e6a6eb5e4667f2e63faae8a3b646e8baTimo Sirainen test_begin("i_stream_create_header_filter: exclude");
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen filter = i_stream_create_header_filter(istream,
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen filter2 = i_stream_create_header_filter(filter,
f4e66312c54f8f21df984e3b17c0cc752e019ec5Timo Sirainen test_assert(size == output_len && memcmp(data, output, size) == 0);
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen test_assert(i_stream_stat(filter, TRUE, &st) == 0 &&
d337f4291d9ea9cd1adee0f468bc0c3baeabb4feTimo Sirainen test_assert(size == output_len && memcmp(data, output, size) == 0);
d337f4291d9ea9cd1adee0f468bc0c3baeabb4feTimo Sirainen test_assert(i_stream_stat(filter, TRUE, &st) == 0 &&
e5dca7edde333c2759b0e6b1d0d00b94ea303322Timo Sirainenstatic void add_random_text(string_t *dest, unsigned int count)
e5dca7edde333c2759b0e6b1d0d00b94ea303322Timo Sirainen unsigned int i;
e5dca7edde333c2759b0e6b1d0d00b94ea303322Timo Sirainen for (i = 0; i < count; i++)
e5dca7edde333c2759b0e6b1d0d00b94ea303322Timo Sirainen str_append_c(dest, rand() % ('z'-'a'+1) + 'a');
e5dca7edde333c2759b0e6b1d0d00b94ea303322Timo Sirainenfilter2_callback(struct header_filter_istream *input ATTR_UNUSED,
e5dca7edde333c2759b0e6b1d0d00b94ea303322Timo Sirainenstatic void test_istream_filter_large_buffer(void)
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen const unsigned char *data;
5a37e34b1b5acf453372cd112c70bb4e46b4bee2Timo Sirainen const char *p;
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen unsigned int i;
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen test_begin("i_stream_create_header_filter: large buffer");
f4e66312c54f8f21df984e3b17c0cc752e019ec5Timo Sirainen istream = test_istream_create_data(str_data(input), str_len(input));
f4e66312c54f8f21df984e3b17c0cc752e019ec5Timo Sirainen test_istream_set_max_buffer_size(istream, 8192);
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen filter = i_stream_create_header_filter(istream,
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen for (i = 0; i < 2; i++) {
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen /* callbacks are called only once */
2521fd0986302cdabc8b0711eef63ac188f32cd6Timo Sirainen test_assert(strncmp(str_c(input), str_c(output), prefix_len) == 0);
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen test_assert(strcmp(p+1, str_c(output) + prefix_len) == 0);
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen test_assert(i_stream_stat(filter, TRUE, &st) == 0 &&
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen (uoff_t)st->st_size == filter->v_offset + size);
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen /* seek back and retry once with caching and different
bf9ea5404a0094a8fb8199b677d81f803512c44eTimo Sirainen buffer size */
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen test_istream_set_max_buffer_size(istream, 4096);
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainenstatic void test_istream_filter_large_buffer2(void)
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen static const char *wanted_headers[] = { "References" };
66a872b4569c048e804f0731680d89c6042d8890Timo Sirainen const unsigned char *data;
66a872b4569c048e804f0731680d89c6042d8890Timo Sirainen unsigned int i;
ac0fed903142d28ae3a1d5d00d2097fdf161b138Timo Sirainen test_begin("i_stream_create_header_filter: large buffer2");
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen istream = test_istream_create_data(str_data(input), str_len(input));
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen test_istream_set_max_buffer_size(istream, 8192);
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen filter = i_stream_create_header_filter(istream,
23fdad6c7e2581921f511e24cd9371c9eaebcef9Timo Sirainen HEADER_FILTER_INCLUDE | HEADER_FILTER_HIDE_BODY,
test_end();
static void test_istream_callbacks(void)
NULL, 0,
output);
test_end();
static void test_istream_edit(void)
test_end();
static void test_istream_end_body_with_lf(void)
const unsigned char *data;
NULL, 0,
(void *)NULL);
if (size > 0)
test_end();
static void test_istream_add_missing_eoh(void)
const char *input;
const char *output;
unsigned int extra;
} tests[] = {
test_end();
static void test_istream_hide_body(void)
const char *input;
const char *output;
int extra;
} tests[] = {
test_end();
static void test_istream_strip_eoh(void)
test_end();
static void test_istream_missing_eoh_callback(void)
test_end();
static void test_istream_empty_missing_eoh_callback(void)
test_end();
int main(void)
static void (*test_functions[])(void) = {