]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
Added unit test for istream-base64-encoder.
authorTimo Sirainen <tss@iki.fi>
Mon, 15 Nov 2010 16:22:08 +0000 (16:22 +0000)
committerTimo Sirainen <tss@iki.fi>
Mon, 15 Nov 2010 16:22:08 +0000 (16:22 +0000)
src/lib/Makefile.am
src/lib/test-istream-base64-encoder.c [new file with mode: 0644]
src/lib/test-lib.c
src/lib/test-lib.h

index 133fe67a4064cdc2e648186c9c866b40fe4f6565..c9afb6546396d8d5702965e0f32127f2f750b74e 100644 (file)
@@ -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 (file)
index 0000000..0aec035
--- /dev/null
@@ -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();
+}
index c76ba712e1fb5235819572f41ad5ce54ff87cf69..76873ddfeee05eaee9a6851e547e34501914cb4a 100644 (file)
@@ -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,
index 068c57c15e7eef2da0b6ee002e63352230942f02..5993939f95162952c1c915d9a775e9fa284cd8f2 100644 (file)
@@ -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);