]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-mail: Make sure istream-attachment-connector detects wrong mail size.
authorTimo Sirainen <timo.sirainen@dovecot.fi>
Tue, 1 Nov 2016 16:42:31 +0000 (18:42 +0200)
committerGitLab <gitlab@git.dovecot.net>
Wed, 22 Feb 2017 18:10:48 +0000 (20:10 +0200)
src/lib-mail/istream-attachment-connector.c
src/lib-mail/test-istream-attachment.c

index a85e84bcb72168c469f6e74c0d94f138cb0ba019..a4332d2d2445004ce5ffd7680ea89410e9a1764b 100644 (file)
@@ -65,9 +65,10 @@ int istream_attachment_connector_add(struct istream_attachment_connector *conn,
 
        if (base_prefix_size > 0) {
                /* add a part of the base message before the attachment */
-               input = i_stream_create_range(conn->base_input,
-                                             conn->base_input_offset,
-                                             base_prefix_size);
+               input = i_stream_create_min_sized_range(conn->base_input,
+                       conn->base_input_offset, base_prefix_size);
+               i_stream_set_name(input, t_strdup_printf("%s middle",
+                       i_stream_get_name(conn->base_input)));
                array_append(&conn->streams, &input, 1);
                conn->base_input_offset += base_prefix_size;
                conn->encoded_offset += base_prefix_size;
@@ -119,9 +120,11 @@ istream_attachment_connector_finish(struct istream_attachment_connector **_conn)
                i_assert(conn->base_input_offset < conn->msg_size);
 
                trailer_size = conn->msg_size - conn->encoded_offset;
-               input = i_stream_create_range(conn->base_input,
-                                             conn->base_input_offset,
-                                             trailer_size);
+               input = i_stream_create_sized_range(conn->base_input,
+                                                   conn->base_input_offset,
+                                                   trailer_size);
+               i_stream_set_name(input, t_strdup_printf(
+                       "%s trailer", i_stream_get_name(conn->base_input)));
                array_append(&conn->streams, &input, 1);
        }
        array_append_zero(&conn->streams);
index f5763bcd00aa6f20b1ab5e67b1b2344d6a364a73..effa1ad63507535f0cd34a01a779f80b41b4cbbd 100644 (file)
@@ -279,21 +279,36 @@ static int test_input_stream(struct istream *file_input)
        i_stream_unref(&input2);
 
        /* rebuild the original stream and see if the hash matches */
-       input2 = i_stream_create_from_data(base_buf->data, base_buf->used);
-       input = test_build_original_istream(input2, msg_size);
-       i_stream_unref(&input2);
+       {
+               input2 = i_stream_create_from_data(base_buf->data, base_buf->used);
+               input = test_build_original_istream(input2, msg_size);
+               i_stream_unref(&input2);
+
+               sha1_init(&hash);
+               while (i_stream_read_more(input, &data, &size) > 0) {
+                       sha1_loop(&hash, data, size);
+                       i_stream_skip(input, size);
+               }
+               test_assert(input->eof && input->stream_errno == 0);
+               sha1_result(&hash, hash_attached);
+               i_stream_unref(&input);
 
-       sha1_init(&hash);
-       while (i_stream_read_more(input, &data, &size) > 0) {
-               sha1_loop(&hash, data, size);
-               i_stream_skip(input, size);
+               if (memcmp(hash_file, hash_attached, SHA1_RESULTLEN) != 0)
+                       ret = -1;
        }
-       sha1_result(&hash, hash_attached);
-       i_stream_unref(&input);
 
-       ret = memcmp(hash_file, hash_attached, SHA1_RESULTLEN) == 0 ? 0 : -1;
+       /* try with a wrong message size */
+       for (int i = 0; i < 2; i++) {
+               input2 = i_stream_create_from_data(base_buf->data, base_buf->used);
+               input = test_build_original_istream(input2, msg_size +
+                                                   (i == 0 ? 1 : -1));
+               i_stream_unref(&input2);
+               while (i_stream_read_more(input, &data, &size) > 0)
+                       i_stream_skip(input, size);
+               test_assert(input->stream_errno == (i == 0 ? EPIPE : EINVAL));
+               i_stream_unref(&input);
+       }
 
-       i_stream_unref(&file_input);
        buffer_free(&base_buf);
        if (attachment_data != NULL)
                buffer_free(&attachment_data);
@@ -425,6 +440,17 @@ static void test_istream_attachment_extractor_error(void)
        test_end();
 }
 
+static void test_istream_attachment_connector(void)
+{
+       struct istream *input;
+
+       test_begin("istream attachment connector");
+       input = i_stream_create_from_data(mail_input, sizeof(mail_input));
+       test_assert(test_input_stream(input) == 0);
+       i_stream_unref(&input);
+       test_end();
+}
+
 static int test_input_file(const char *path)
 {
        struct istream *file_input;
@@ -450,6 +476,7 @@ int main(int argc, char *argv[])
                test_istream_attachment,
                test_istream_attachment_extractor,
                test_istream_attachment_extractor_error,
+               test_istream_attachment_connector,
                NULL
        };
        if (argc > 1)