bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2009-2018 Dovecot authors, see the included COPYING file */
6d7c5ee9298444135f6051f810e6dcc467a2a968Timo Sirainen
6d7c5ee9298444135f6051f810e6dcc467a2a968Timo Sirainen#include "lib.h"
6d7c5ee9298444135f6051f810e6dcc467a2a968Timo Sirainen#include "istream.h"
6d7c5ee9298444135f6051f810e6dcc467a2a968Timo Sirainen#include "imap-parser.h"
6d7c5ee9298444135f6051f810e6dcc467a2a968Timo Sirainen#include "test-common.h"
6d7c5ee9298444135f6051f810e6dcc467a2a968Timo Sirainen
6d7c5ee9298444135f6051f810e6dcc467a2a968Timo Sirainenstatic void test_imap_parser_crlf(void)
6d7c5ee9298444135f6051f810e6dcc467a2a968Timo Sirainen{
6d7c5ee9298444135f6051f810e6dcc467a2a968Timo Sirainen static const char *test_input = "foo\r\nx\ry\n";
6d7c5ee9298444135f6051f810e6dcc467a2a968Timo Sirainen struct istream *input;
6d7c5ee9298444135f6051f810e6dcc467a2a968Timo Sirainen struct imap_parser *parser;
6d7c5ee9298444135f6051f810e6dcc467a2a968Timo Sirainen const struct imap_arg *args;
6d7c5ee9298444135f6051f810e6dcc467a2a968Timo Sirainen unsigned int i;
0adc24c0c534944b55a185795e09dfaea2ca3131Stephan Bosch enum imap_parser_error parse_error;
6d7c5ee9298444135f6051f810e6dcc467a2a968Timo Sirainen
6d7c5ee9298444135f6051f810e6dcc467a2a968Timo Sirainen test_begin("imap parser crlf handling");
6d7c5ee9298444135f6051f810e6dcc467a2a968Timo Sirainen input = test_istream_create(test_input);
6d7c5ee9298444135f6051f810e6dcc467a2a968Timo Sirainen parser = imap_parser_create(input, NULL, 1024);
6d7c5ee9298444135f6051f810e6dcc467a2a968Timo Sirainen
6d7c5ee9298444135f6051f810e6dcc467a2a968Timo Sirainen /* must return -2 until LF is read */
6d7c5ee9298444135f6051f810e6dcc467a2a968Timo Sirainen for (i = 0; test_input[i] != '\n'; i++) {
6d7c5ee9298444135f6051f810e6dcc467a2a968Timo Sirainen test_istream_set_size(input, i+1);
f2fceb99b8bfc3cfccae5f74a9d2eb7d1f55617cTimo Sirainen (void)i_stream_read(input);
6d7c5ee9298444135f6051f810e6dcc467a2a968Timo Sirainen test_assert(imap_parser_read_args(parser, 0, 0, &args) == -2);
6d7c5ee9298444135f6051f810e6dcc467a2a968Timo Sirainen }
6d7c5ee9298444135f6051f810e6dcc467a2a968Timo Sirainen test_istream_set_size(input, i+1);
f2fceb99b8bfc3cfccae5f74a9d2eb7d1f55617cTimo Sirainen (void)i_stream_read(input);
32cec611ae362c98e3a22373fa1d2ded48621b57Timo Sirainen test_assert(imap_parser_read_args(parser, 0, 0, &args) == 1);
6d7c5ee9298444135f6051f810e6dcc467a2a968Timo Sirainen test_assert(args[0].type == IMAP_ARG_ATOM);
6d7c5ee9298444135f6051f810e6dcc467a2a968Timo Sirainen test_assert(args[1].type == IMAP_ARG_EOL);
6d7c5ee9298444135f6051f810e6dcc467a2a968Timo Sirainen
6d7c5ee9298444135f6051f810e6dcc467a2a968Timo Sirainen /* CR without LF should fail with error */
6d7c5ee9298444135f6051f810e6dcc467a2a968Timo Sirainen imap_parser_reset(parser);
6d7c5ee9298444135f6051f810e6dcc467a2a968Timo Sirainen i_stream_seek(input, ++i);
6d7c5ee9298444135f6051f810e6dcc467a2a968Timo Sirainen test_istream_set_size(input, ++i);
f2fceb99b8bfc3cfccae5f74a9d2eb7d1f55617cTimo Sirainen (void)i_stream_read(input);
6d7c5ee9298444135f6051f810e6dcc467a2a968Timo Sirainen test_assert(imap_parser_read_args(parser, 0, 0, &args) == -2);
6d7c5ee9298444135f6051f810e6dcc467a2a968Timo Sirainen test_istream_set_size(input, ++i);
f2fceb99b8bfc3cfccae5f74a9d2eb7d1f55617cTimo Sirainen (void)i_stream_read(input);
6d7c5ee9298444135f6051f810e6dcc467a2a968Timo Sirainen test_assert(imap_parser_read_args(parser, 0, 0, &args) == -2);
6d7c5ee9298444135f6051f810e6dcc467a2a968Timo Sirainen test_istream_set_size(input, ++i);
f2fceb99b8bfc3cfccae5f74a9d2eb7d1f55617cTimo Sirainen (void)i_stream_read(input);
6d7c5ee9298444135f6051f810e6dcc467a2a968Timo Sirainen test_assert(imap_parser_read_args(parser, 0, 0, &args) == -1);
0adc24c0c534944b55a185795e09dfaea2ca3131Stephan Bosch test_assert(strcmp(imap_parser_get_error
0adc24c0c534944b55a185795e09dfaea2ca3131Stephan Bosch (parser, &parse_error), "CR sent without LF") == 0 &&
0adc24c0c534944b55a185795e09dfaea2ca3131Stephan Bosch parse_error == IMAP_PARSE_ERROR_BAD_SYNTAX);
6d7c5ee9298444135f6051f810e6dcc467a2a968Timo Sirainen
428fb4dc39c6e9b2eb36216c396dad6096a65f8fTimo Sirainen imap_parser_unref(&parser);
6d7c5ee9298444135f6051f810e6dcc467a2a968Timo Sirainen i_stream_destroy(&input);
6d7c5ee9298444135f6051f810e6dcc467a2a968Timo Sirainen test_end();
6d7c5ee9298444135f6051f810e6dcc467a2a968Timo Sirainen}
6d7c5ee9298444135f6051f810e6dcc467a2a968Timo Sirainen
6d7c5ee9298444135f6051f810e6dcc467a2a968Timo Sirainenint main(void)
6d7c5ee9298444135f6051f810e6dcc467a2a968Timo Sirainen{
baf3e87e186453fda13bd21f7cbcb2efc8492e8bTimo Sirainen static void (*const test_functions[])(void) = {
6d7c5ee9298444135f6051f810e6dcc467a2a968Timo Sirainen test_imap_parser_crlf,
6d7c5ee9298444135f6051f810e6dcc467a2a968Timo Sirainen NULL
6d7c5ee9298444135f6051f810e6dcc467a2a968Timo Sirainen };
6d7c5ee9298444135f6051f810e6dcc467a2a968Timo Sirainen return test_run(test_functions);
6d7c5ee9298444135f6051f810e6dcc467a2a968Timo Sirainen}