Mercurial > dovecot > core-2.2
changeset 12420:bb6fc4e13bc9
Added unit test for istream-base64-encoder.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Mon, 15 Nov 2010 16:22:08 +0000 |
parents | b1d946388d9b |
children | 8a28deb29320 |
files | src/lib/Makefile.am src/lib/test-istream-base64-encoder.c src/lib/test-lib.c src/lib/test-lib.h |
diffstat | 4 files changed, 74 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/src/lib/Makefile.am Mon Nov 15 16:21:45 2010 +0000 +++ b/src/lib/Makefile.am Mon Nov 15 16:22:08 2010 +0000 @@ -237,6 +237,7 @@ test-crc32.c \ test-hash-format.c \ test-hex-binary.c \ + test-istream-base64-encoder.c \ test-istream-concat.c \ test-istream-crlf.c \ test-istream-seekable.c \
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lib/test-istream-base64-encoder.c Mon Nov 15 16:22:08 2010 +0000 @@ -0,0 +1,71 @@ +/* Copyright (c) 2010 Dovecot authors, see the included COPYING file */ + +#include "test-lib.h" +#include "str.h" +#include "istream-internal.h" +#include "istream-base64-encoder.h" + +static const char *hello = "hello world"; + +static const char * +encode(const char *text, unsigned int chars_per_line, bool crlf) +{ + struct istream *input, *input_data; + const char *reply; + const unsigned char *data; + size_t size; + ssize_t ret; + + input_data = i_stream_create_from_data(text, strlen(text)); + input = i_stream_create_base64_encoder(input_data, chars_per_line, crlf); + while ((ret = i_stream_read(input)) > 0) ; + test_assert(ret == -1); + + data = i_stream_get_data(input, &size); + reply = t_strndup(data, size); + + i_stream_unref(&input); + i_stream_unref(&input_data); + return reply; +} + +static void +test_istream_base64_encoder_seek(const char *textin, const char *textout) +{ + unsigned int offset, len = strlen(textout); + struct istream *input, *input_data; + const unsigned char *data; + size_t size; + ssize_t ret; + + input_data = i_stream_create_from_data(textin, strlen(textin)); + input = i_stream_create_base64_encoder(input_data, 4, TRUE); + + while ((ret = i_stream_read(input)) > 0) ; + data = i_stream_get_data(input, &size); + i_stream_skip(input, size); + + for (offset = 0; offset < len; offset++) { + i_stream_seek(input, offset); + while ((ret = i_stream_read(input)) > 0) ; + test_assert(ret == -1); + + data = i_stream_get_data(input, &size); + test_assert(size == len-offset); + test_assert(memcmp(data, textout+offset, size) == 0); + i_stream_skip(input, size); + } + + i_stream_unref(&input); + i_stream_unref(&input_data); +} + +void test_istream_base64_encoder(void) +{ + test_begin("istream base64 encoder"); + test_assert(strcmp(encode(hello, 80, FALSE), "aGVsbG8gd29ybGQ=") == 0); + test_assert(strcmp(encode(hello, 4, FALSE), "aGVs\nbG8g\nd29y\nbGQ=") == 0); + test_assert(strcmp(encode(hello, 4, TRUE), "aGVs\r\nbG8g\r\nd29y\r\nbGQ=") == 0); + test_istream_base64_encoder_seek(hello, "aGVs\r\nbG8g\r\nd29y\r\nbGQ="); + test_end(); +}
--- a/src/lib/test-lib.c Mon Nov 15 16:21:45 2010 +0000 +++ b/src/lib/test-lib.c Mon Nov 15 16:22:08 2010 +0000 @@ -13,6 +13,7 @@ test_crc32, test_hash_format, test_hex_binary, + test_istream_base64_encoder, test_istream_concat, test_istream_crlf, test_istream_seekable,
--- a/src/lib/test-lib.h Mon Nov 15 16:21:45 2010 +0000 +++ b/src/lib/test-lib.h Mon Nov 15 16:22:08 2010 +0000 @@ -12,6 +12,7 @@ void test_crc32(void); void test_hash_format(void); void test_hex_binary(void); +void test_istream_base64_encoder(void); void test_istream_concat(void); void test_istream_crlf(void); void test_istream_seekable(void);