/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */
#include "test-lib.h"
#include "buffer.h"
#include "str.h"
#include "strfuncs.h"
#include "str-sanitize.h"
#include "istream.h"
#include "ostream.h"
#include "test-common.h"
#include "http-transfer.h"
#include <time.h>
struct http_transfer_chunked_input_test {
const char *in;
const char *out;
};
/* Valid transfer_chunked input tests */
static struct http_transfer_chunked_input_test
{ .in = "1E\r\n"
"This is a simple test payload."
"\r\n"
"0\r\n"
"\r\n",
.out =
"This is a simple test payload."
},
{ .in = "20\r\n"
"This is a longer test payload..."
"\r\n"
"23\r\n"
"...spread over two separate chunks."
"\r\n"
"0\r\n"
"\r\n",
.out =
"This is a longer test payload..."
"...spread over two separate chunks."
},
{ .in = "26\r\n"
"This is an even longer test payload..."
"\r\n"
"27\r\n"
"...spread over three separate chunks..."
"\r\n"
"1F\r\n"
"...and also includes a trailer."
"\r\n"
"0\r\n"
"Checksum: adgfef3fdaf3daf3dfaf3ff3fdag\r\n"
"X-Dovecot: Whatever\r\n"
"\r\n",
.out =
"This is an even longer test payload..."
"...spread over three separate chunks..."
"...and also includes a trailer."
},
{ .in = "26\n"
"This is an even longer test payload..."
"\n"
"27\n"
"...spread over three separate chunks..."
"\n"
"1F\n"
"...and also includes a trailer."
"\n"
"0\n"
"Checksum: adgfef3fdaf3daf3dfaf3ff3fdag\n"
"X-Dovecot: Whatever\n"
"\n",
.out =
"This is an even longer test payload..."
"...spread over three separate chunks..."
"...and also includes a trailer."
}
};
static unsigned int valid_transfer_chunked_input_test_count =
static void test_http_transfer_chunked_input_valid(void)
{
unsigned int i;
for (i = 0; i < valid_transfer_chunked_input_test_count; i++) T_BEGIN {
test_out("payload read", o_stream_send_istream(output, chunked) == OSTREAM_SEND_ISTREAM_RESULT_FINISHED
&& chunked->stream_errno == 0);
test_end();
} T_END;
}
/* Invalid transfer_chunked input tests */
static const char *
// invalid size
"1X\r\n"
"This is a simple test payload."
"\r\n"
"0\r\n"
"\r\n",
// invalid end
"1E\r\n"
"This is a simple test payload."
"\r\n"
"0\r\n"
"ah\r\n",
// invalid size
"20\r\n"
"This is a longer test payload..."
"\r\n"
"2q\r\n"
"...spread over two separate chunks."
"\r\n"
"0\r\n"
"\r\n",
// invalid end
"20\r\n"
"This is a longer test payload..."
"\r\n"
"23\r\n"
"...spread over two separate chunks."
"\r\n"
"0\r\n",
// invalid last chunk
"20\r\n"
"This is a longer test payload..."
"\r\n"
"23\r\n"
"...spread over two separate chunks."
"\r\n"
"4\r\n"
"\r\n",
// invalid trailer
"26\r\n"
"This is an even longer test payload..."
"\r\n"
"27\r\n"
"...spread over three separate chunks..."
"\r\n"
"1F\r\n"
"...and also includes a trailer."
"\r\n"
"0\r\n"
"Checksum adgfef3fdaf3daf3dfaf3ff3fdag\r\n"
"\r\n"
};
static unsigned int invalid_transfer_chunked_input_test_count =
static void test_http_transfer_chunked_input_invalid(void)
{
unsigned int i;
for (i = 0; i < invalid_transfer_chunked_input_test_count; i++) T_BEGIN {
const char *in;
test_end();
} T_END;
}
/* Valid transfer_chunked output tests */
static const char *valid_transfer_chunked_output_tests[] = {
/* The maximum chunk size is set to 16. These tests are tuned to some border
cases
*/
"A small payload", // 15 bytes
"A longer payload", // 16 bytes
"A lengthy payload", // 17 bytes
/* Others */
"This is a test payload with lots of nonsense.",
"Yet another payload.",
"This a very long repetitive payload. This a very long repetitive payload. "
"This a very long repetitive payload. This a very long repetitive payload. "
"This a very long repetitive payload. This a very long repetitive payload. "
"This a very long repetitive payload. This a very long repetitive payload. "
"This a very long repetitive payload. This a very long repetitive payload. "
"This a very long repetitive payload. This a very long repetitive payload. "
"This a very long repetitive payload. This a very long repetitive payload. "
"This a very long repetitive payload. This a very long repetitive payload. "
"This a very long repetitive payload. This a very long repetitive payload. "
"This a very long repetitive payload. This a very long repetitive payload. "
"This a very long repetitive payload. This a very long repetitive payload. "
"This a very long repetitive payload. This a very long repetitive payload. "
"This a very long repetitive payload. This a very long repetitive payload. "
"This a very long repetitive payload. This a very long repetitive payload. "
"This a very long repetitive payload. This a very long repetitive payload. "
"This a very long repetitive payload. This a very long repetitive payload. "
"This a very long repetitive payload. This a very long repetitive payload. "
"This a very long repetitive payload. This a very long repetitive payload. "
"This a very long repetitive payload. This a very long repetitive payload. "
};
static unsigned int valid_transfer_chunked_output_test_count =
static void test_http_transfer_chunked_output_valid(void)
{
unsigned int i;
for (i = 0; i < valid_transfer_chunked_output_test_count; i++) T_BEGIN {
const unsigned char *rdata;
/* create input stream */
/* create buffer output stream */
/* create chunked output stream */
/* send input through chunked stream; chunk size is limited */
for (;;) {
if (ret < 0) {
ret = 1;
break;
}
if (rsize == 0)
break;
if (rsize > 16)
rsize = 16;
if (ret < 0)
break;
ret = -1;
break;
}
}
/* cleanup streams */
/* create chunked input stream */
/* read back chunk */
test_out("payload unchunk",
&& ichunked->stream_errno == 0);
/* test output */
test_end();
} T_END;
}
int main(void)
{
static void (*const test_functions[])(void) = {
};
return test_run(test_functions);
}