From: Timo Sirainen Date: Mon, 15 Nov 2010 16:22:08 +0000 (+0000) Subject: Added unit test for istream-base64-encoder. X-Git-Tag: 2.0.8~91 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=6a3a0723319ea31343ea6f03f40672c4b533b21d;p=thirdparty%2Fdovecot%2Fcore.git Added unit test for istream-base64-encoder. --- diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am index 133fe67a40..c9afb65463 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am @@ -237,6 +237,7 @@ test_lib_SOURCES = \ 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 \ diff --git a/src/lib/test-istream-base64-encoder.c b/src/lib/test-istream-base64-encoder.c new file mode 100644 index 0000000000..0aec035982 --- /dev/null +++ b/src/lib/test-istream-base64-encoder.c @@ -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(); +} diff --git a/src/lib/test-lib.c b/src/lib/test-lib.c index c76ba712e1..76873ddfee 100644 --- a/src/lib/test-lib.c +++ b/src/lib/test-lib.c @@ -13,6 +13,7 @@ int main(void) test_crc32, test_hash_format, test_hex_binary, + test_istream_base64_encoder, test_istream_concat, test_istream_crlf, test_istream_seekable, diff --git a/src/lib/test-lib.h b/src/lib/test-lib.h index 068c57c15e..5993939f95 100644 --- a/src/lib/test-lib.h +++ b/src/lib/test-lib.h @@ -12,6 +12,7 @@ void test_buffer(void); 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);