bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch#include "test-lib.h"
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch#include "buffer.h"
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch#include "str.h"
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch#include "strfuncs.h"
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch#include "str-sanitize.h"
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch#include "istream.h"
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch#include "ostream.h"
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch#include "test-common.h"
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch#include "http-transfer.h"
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch#include <time.h>
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Boschstruct http_transfer_chunked_input_test {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch const char *in;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch const char *out;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch};
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch/* Valid transfer_chunked input tests */
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Boschstatic struct http_transfer_chunked_input_test
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Boschvalid_transfer_chunked_input_tests[] = {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch { .in = "1E\r\n"
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "This is a simple test payload."
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "\r\n"
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "0\r\n"
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "\r\n",
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch .out =
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "This is a simple test payload."
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch },
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch { .in = "20\r\n"
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "This is a longer test payload..."
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "\r\n"
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "23\r\n"
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "...spread over two separate chunks."
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "\r\n"
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "0\r\n"
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "\r\n",
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch .out =
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "This is a longer test payload..."
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "...spread over two separate chunks."
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch },
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch { .in = "26\r\n"
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "This is an even longer test payload..."
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "\r\n"
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "27\r\n"
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "...spread over three separate chunks..."
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "\r\n"
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "1F\r\n"
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "...and also includes a trailer."
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "\r\n"
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "0\r\n"
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "Checksum: adgfef3fdaf3daf3dfaf3ff3fdag\r\n"
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "X-Dovecot: Whatever\r\n"
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "\r\n",
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch .out =
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "This is an even longer test payload..."
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "...spread over three separate chunks..."
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "...and also includes a trailer."
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch },
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch { .in = "26\n"
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "This is an even longer test payload..."
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "\n"
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "27\n"
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "...spread over three separate chunks..."
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "\n"
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "1F\n"
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "...and also includes a trailer."
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "\n"
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "0\n"
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "Checksum: adgfef3fdaf3daf3dfaf3ff3fdag\n"
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "X-Dovecot: Whatever\n"
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "\n",
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch .out =
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "This is an even longer test payload..."
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "...spread over three separate chunks..."
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "...and also includes a trailer."
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch }
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch};
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Boschstatic unsigned int valid_transfer_chunked_input_test_count =
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch N_ELEMENTS(valid_transfer_chunked_input_tests);
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Boschstatic void test_http_transfer_chunked_input_valid(void)
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch{
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch struct istream *input, *chunked;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch struct ostream *output;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch buffer_t *payload_buffer;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch unsigned int i;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch payload_buffer = buffer_create_dynamic(default_pool, 1024);
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch for (i = 0; i < valid_transfer_chunked_input_test_count; i++) T_BEGIN {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch const char *in, *out, *stream_out;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch in = valid_transfer_chunked_input_tests[i].in;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch out = valid_transfer_chunked_input_tests[i].out;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch test_begin(t_strdup_printf("http transfer_chunked input valid [%d]", i));
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch input = i_stream_create_from_data(in, strlen(in));
1175415b88ff168e367c77df23901eada13225b9Stephan Bosch chunked = http_transfer_chunked_istream_create(input, 0);
5968fa8151eecd191b1973b44dd8bec9b75810a6Phil Carmody i_stream_unref(&input);
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch buffer_set_used_size(payload_buffer, 0);
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch output = o_stream_create_buffer(payload_buffer);
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen test_out("payload read", o_stream_send_istream(output, chunked) == OSTREAM_SEND_ISTREAM_RESULT_FINISHED
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch && chunked->stream_errno == 0);
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch o_stream_destroy(&output);
5968fa8151eecd191b1973b44dd8bec9b75810a6Phil Carmody i_stream_unref(&chunked);
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch stream_out = str_c(payload_buffer);
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch test_out(t_strdup_printf("response->payload = %s",
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch str_sanitize(stream_out, 80)),
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch strcmp(stream_out, out) == 0);
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch test_end();
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch } T_END;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch buffer_free(&payload_buffer);
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch}
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch/* Invalid transfer_chunked input tests */
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Boschstatic const char *
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Boschinvalid_transfer_chunked_input_tests[] = {
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch // invalid size
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "1X\r\n"
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "This is a simple test payload."
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "\r\n"
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "0\r\n"
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "\r\n",
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch // invalid end
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "1E\r\n"
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "This is a simple test payload."
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "\r\n"
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "0\r\n"
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "ah\r\n",
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch // invalid size
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "20\r\n"
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "This is a longer test payload..."
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "\r\n"
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "2q\r\n"
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "...spread over two separate chunks."
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "\r\n"
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "0\r\n"
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "\r\n",
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch // invalid end
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "20\r\n"
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "This is a longer test payload..."
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "\r\n"
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "23\r\n"
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "...spread over two separate chunks."
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "\r\n"
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "0\r\n",
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch // invalid last chunk
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "20\r\n"
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "This is a longer test payload..."
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "\r\n"
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "23\r\n"
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "...spread over two separate chunks."
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "\r\n"
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "4\r\n"
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "\r\n",
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch // invalid trailer
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "26\r\n"
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "This is an even longer test payload..."
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "\r\n"
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "27\r\n"
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "...spread over three separate chunks..."
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "\r\n"
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "1F\r\n"
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "...and also includes a trailer."
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "\r\n"
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "0\r\n"
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "Checksum adgfef3fdaf3daf3dfaf3ff3fdag\r\n"
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "\r\n"
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch};
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Boschstatic unsigned int invalid_transfer_chunked_input_test_count =
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch N_ELEMENTS(invalid_transfer_chunked_input_tests);
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Boschstatic void test_http_transfer_chunked_input_invalid(void)
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch{
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch struct istream *input, *chunked;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch struct ostream *output;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch buffer_t *payload_buffer;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch unsigned int i;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch payload_buffer = buffer_create_dynamic(default_pool, 1024);
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch for (i = 0; i < invalid_transfer_chunked_input_test_count; i++) T_BEGIN {
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch const char *in;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch in = invalid_transfer_chunked_input_tests[i];
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch test_begin(t_strdup_printf("http transfer_chunked input invalid [%d]", i));
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch input = i_stream_create_from_data(in, strlen(in));
1175415b88ff168e367c77df23901eada13225b9Stephan Bosch chunked = http_transfer_chunked_istream_create(input, 0);
5968fa8151eecd191b1973b44dd8bec9b75810a6Phil Carmody i_stream_unref(&input);
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch buffer_set_used_size(payload_buffer, 0);
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch output = o_stream_create_buffer(payload_buffer);
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch (void)o_stream_send_istream(output, chunked);
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch test_out("payload read failure", chunked->stream_errno != 0);
5968fa8151eecd191b1973b44dd8bec9b75810a6Phil Carmody i_stream_unref(&chunked);
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch o_stream_destroy(&output);
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch test_end();
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch } T_END;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch buffer_free(&payload_buffer);
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch}
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch/* Valid transfer_chunked output tests */
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Boschstatic const char *valid_transfer_chunked_output_tests[] = {
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch /* The maximum chunk size is set to 16. These tests are tuned to some border
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch cases
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch */
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "A small payload", // 15 bytes
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "A longer payload", // 16 bytes
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "A lengthy payload", // 17 bytes
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch /* Others */
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "This is a test payload with lots of nonsense.",
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "Yet another payload.",
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "This a very long repetitive payload. This a very long repetitive payload. "
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "This a very long repetitive payload. This a very long repetitive payload. "
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "This a very long repetitive payload. This a very long repetitive payload. "
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "This a very long repetitive payload. This a very long repetitive payload. "
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "This a very long repetitive payload. This a very long repetitive payload. "
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "This a very long repetitive payload. This a very long repetitive payload. "
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "This a very long repetitive payload. This a very long repetitive payload. "
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "This a very long repetitive payload. This a very long repetitive payload. "
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "This a very long repetitive payload. This a very long repetitive payload. "
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "This a very long repetitive payload. This a very long repetitive payload. "
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "This a very long repetitive payload. This a very long repetitive payload. "
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "This a very long repetitive payload. This a very long repetitive payload. "
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "This a very long repetitive payload. This a very long repetitive payload. "
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "This a very long repetitive payload. This a very long repetitive payload. "
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "This a very long repetitive payload. This a very long repetitive payload. "
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "This a very long repetitive payload. This a very long repetitive payload. "
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "This a very long repetitive payload. This a very long repetitive payload. "
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "This a very long repetitive payload. This a very long repetitive payload. "
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch "This a very long repetitive payload. This a very long repetitive payload. "
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch};
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Boschstatic unsigned int valid_transfer_chunked_output_test_count =
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch N_ELEMENTS(valid_transfer_chunked_output_tests);
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Boschstatic void test_http_transfer_chunked_output_valid(void)
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch{
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch struct istream *input, *ichunked;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch struct ostream *output, *ochunked;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch buffer_t *chunked_buffer, *plain_buffer;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch unsigned int i;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch chunked_buffer = buffer_create_dynamic(default_pool, 1024);
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch plain_buffer = buffer_create_dynamic(default_pool, 1024);
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch for (i = 0; i < valid_transfer_chunked_output_test_count; i++) T_BEGIN {
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch const char *data, *stream_out;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch const unsigned char *rdata;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch size_t rsize;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch ssize_t ret;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch data = valid_transfer_chunked_output_tests[i];
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch test_begin(t_strdup_printf("http transfer_chunked output valid [%d]", i));
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch /* create input stream */
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch input = i_stream_create_from_data(data, strlen(data));
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch /* create buffer output stream */
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch buffer_set_used_size(chunked_buffer, 0);
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch output = o_stream_create_buffer(chunked_buffer);
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch /* create chunked output stream */
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch ochunked = http_transfer_chunked_ostream_create(output);
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch /* send input through chunked stream; chunk size is limited */
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch for (;;) {
3858a7a5da361c35f1e6e50c8e3214dc0cf379d6Phil Carmody ret = i_stream_read_more(input, &rdata, &rsize);
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch if (ret < 0) {
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch if (input->eof)
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch ret = 1;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch break;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch }
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch if (rsize == 0)
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch break;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch if (rsize > 16)
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch rsize = 16;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch ret = o_stream_send(ochunked, rdata, rsize);
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch if (ret < 0)
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch break;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch if ((size_t)ret != rsize) {
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch ret = -1;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch break;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch }
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch i_stream_skip(input, ret);
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch }
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch /* cleanup streams */
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch test_out("payload chunk", ret > 0);
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch o_stream_destroy(&ochunked);
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch o_stream_destroy(&output);
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch i_stream_destroy(&input);
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch /* create chunked input stream */
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch input = i_stream_create_from_data
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch (chunked_buffer->data, chunked_buffer->used);
1175415b88ff168e367c77df23901eada13225b9Stephan Bosch ichunked = http_transfer_chunked_istream_create(input, 0);
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch /* read back chunk */
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch buffer_set_used_size(plain_buffer, 0);
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch output = o_stream_create_buffer(plain_buffer);
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen test_out("payload unchunk",
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen o_stream_send_istream(output, ichunked) == OSTREAM_SEND_ISTREAM_RESULT_FINISHED
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch && ichunked->stream_errno == 0);
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch o_stream_destroy(&output);
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch i_stream_destroy(&ichunked);
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch i_stream_destroy(&input);
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch /* test output */
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch stream_out = str_c(plain_buffer);
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch test_out(t_strdup_printf("response->payload = %s",
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch str_sanitize(stream_out, 80)),
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch strcmp(stream_out, data) == 0);
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch test_end();
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch } T_END;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch buffer_free(&chunked_buffer);
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch buffer_free(&plain_buffer);
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch}
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Boschint main(void)
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch{
baf3e87e186453fda13bd21f7cbcb2efc8492e8bTimo Sirainen static void (*const test_functions[])(void) = {
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch test_http_transfer_chunked_input_valid,
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch test_http_transfer_chunked_input_invalid,
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch test_http_transfer_chunked_output_valid,
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch NULL
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch };
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return test_run(test_functions);
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch}