bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2007-2018 Dovecot authors, see the included COPYING file */
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen#define BINARY_TEXT_LONG "we have\ra lot \nof \0binary stuff in here\n" \
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen"b adjig sadjg jasidgjiaehga3wht8a3w8ghxjc dsgad hasdghsd gasd ds" \
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen"jdsoga sjdga0w3tjhawjgsertniq3n5oqerjqw2r89q23h awhrqh835r8a"
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen"d2UgaGF2ZQ1hIGxvdCAKb2YgAGJpbmFyeSBzdHVmZiBpbiBoZXJlCmIgYWRqaWcgc2FkamcgamFz\r\n" \
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen"aWRnamlhZWhnYTN3aHQ4YTN3OGdoeGpjIGRzZ2FkIGhhc2RnaHNkIGdhc2QgZHNqZHNvZ2Egc2pk\r\n" \
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen"Z2EwdzN0amhhd2pnc2VydG5pcTNuNW9xZXJqcXcycjg5cTIzaCBhd2hycWg4MzVyOGE="
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen"MIME-Version: 1.0\r\n"
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen"Content-Type: multipart/alternative;\r\n boundary=\"bound\"\r\n"
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen"mime header\r\n"
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen"\r\n--bound\r\n"
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen"Content-Transfer-Encoding: base64\r\n"
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen"Content-Type: text/plain\r\n"
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen"\r\n--bound\r\n"
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen"Content-Type: text/plain\r\n"
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen"Content-Transfer-Encoding: base64\r\n"
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen"\r\n--bound--\r\n";
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen"MIME-Version: 1.0\r\n"
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen"Content-Type: multipart/alternative;\r\n boundary=\"bound\"\r\n"
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen"mime header\r\n"
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen"\r\n--bound\r\n"
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen"Content-Transfer-Encoding: base64\r\n"
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen"Content-Type: text/plain\r\n"
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen"\r\n--bound\r\n"
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen"Content-Type: text/plain\r\n"
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen"Content-Transfer-Encoding: base64\r\n"
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen"\r\n--bound--\r\n";
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainenstatic const char *mail_broken_input_body_prefix =
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainen"MIME-Version: 1.0\r\n"
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainen"Content-Type: multipart/alternative;\r\n boundary=\"bound\"\r\n"
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainen"Content-Transfer-Encoding: base64\r\n"
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainen"Content-Type: text/plain\r\n"
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainenstatic const char *mail_broken_input_bodies[] = {
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainen /* broken base64 input */
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainen /* extra whitespace */
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainen "Zm9v\n Zm9v\n",
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainen "Zm9v \nZm9v\n",
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainen /* mixed LF vs CRLFs */
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainen "Zm9vYmFy\r\nZm9vYmFy\n",
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainen "Zm9vYmFy\nZm9vYmFy\r\n",
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainen /* line length increases */
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainen "Zm9v\nZm9vYmFy\n",
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainen "Zm9v\nZm9vCg==",
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainen "Zm9v\nZm9vYgo="
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainenstatic const char *mail_nonbroken_input_bodies[] = {
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainen /* suffixes with explicit '=' end */
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainen "Zm9vCg==\n",
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainen "Zm9vCg==\r\n",
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainen "Zm9vCg==\nfoo\n",
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainen "Zm9vCg==\r\nfoo\n",
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainen "Zm9vCg== \t\t\n\n",
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainen /* suffixes with shorter line length */
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainen "Zm9vYmFy\nZm9v\n",
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainen "Zm9vYmFy\r\nZm9v\r\n",
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainen "Zm9vYmFy\nZm9v\nfoo\n",
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainen "Zm9vYmFy\r\nZm9v\r\nfoo\n",
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainen "Zm9vYmFy\nZm9v\n \t\t\n\n",
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainen /* suffixes with empty line */
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainen "Zm9v\r\n\r\n",
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainen "Zm9v\n\nfoo\n"
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainen "Zm9v\r\n\nfoo\n"
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainen "Zm9v\r\n\r\nfoo\n"
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainen /* the whitespace here could be handled as suffixes, but for now
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainen they're not: */
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainenstatic int test_open_temp_fd(void *context ATTR_UNUSED)
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen fd = safe_mkstemp(str, 0600, (uid_t)-1, (gid_t)-1);
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen i_fatal("safe_mkstemp(%s) failed: %m", str_c(str));
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainenstatic int test_open_attachment_ostream(struct istream_attachment_info *info,
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen attachment_data = buffer_create_dynamic(default_pool, 1024);
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen a->base64_blocks_per_line = info->base64_blocks_per_line;
e869616aca1c2469c436e1e99dad93f7e4b2ae8aTimo Sirainen test_assert(strlen(info->hash) == 160/8*2); /* sha1 size */
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen *output_r = o_stream_create_buffer(attachment_data);
af5fc8ca268d69dd3ebbe0416ec6270f94121e39Timo Sirainen if (o_stream_seek(*output_r, a->buffer_offset) < 0)
9d0be57bcc352f776a327d7e2a6c6232137f4a04Timo Sirainentest_open_attachment_ostream_error(struct istream_attachment_info *info ATTR_UNUSED,
9d0be57bcc352f776a327d7e2a6c6232137f4a04Timo Sirainen const char **error_r,
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainenstatic int test_close_attachment_ostream(struct ostream *output, bool success,
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen a = array_idx_modifiable(&attachments, array_count(&attachments)-1);
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen a->decoded_size = output->offset - a->buffer_offset;
aab5704d21173f9f825c016812c8099d16ea5b9fTimo Sirainentest_close_attachment_ostream_error(struct ostream *output,
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainenstatic struct istream *
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainentest_build_original_istream(struct istream *base_input, uoff_t msg_size)
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen const unsigned char *data = attachment_data->data;
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen const struct attachment *a;
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen conn = istream_attachment_connector_begin(base_input, msg_size);
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen input = i_stream_create_from_data(data, a->decoded_size);
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen if (istream_attachment_connector_add(conn, input,
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen return istream_attachment_connector_finish(&conn);
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainenget_istream_attachment_settings(struct istream_attachment_settings *set_r)
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen set_r->open_attachment_ostream = test_open_attachment_ostream;
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen set_r->close_attachment_ostream= test_close_attachment_ostream;
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen if (hash_format_init("%{sha1}", &set_r->hash_format, &error) < 0)
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainenstatic int test_input_stream(struct istream *file_input)
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen unsigned char hash_file[SHA1_RESULTLEN], hash_attached[SHA1_RESULTLEN];
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen /* get hash when directly reading input */
3858a7a5da361c35f1e6e50c8e3214dc0cf379d6Phil Carmody while (i_stream_read_more(input, &data, &size) > 0) {
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen /* read through attachment extractor */
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen input2 = i_stream_create_attachment_extractor(input, &set, NULL);
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen base_buf = buffer_create_dynamic(default_pool, 1024);
3858a7a5da361c35f1e6e50c8e3214dc0cf379d6Phil Carmody while (i_stream_read_more(input2, &data, &size) > 0) {
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen /* rebuild the original stream and see if the hash matches */
075081e25ef07989be10f7c9cf85f833f90be46fTimo Sirainen for (unsigned int i = 0; i < 2; i++) {
24d6fd2b8e2c327ae2a20a0c4be8af2ef7b3b468Timo Sirainen input2 = i_stream_create_from_data(base_buf->data, base_buf->used);
24d6fd2b8e2c327ae2a20a0c4be8af2ef7b3b468Timo Sirainen input = test_build_original_istream(input2, msg_size);
24d6fd2b8e2c327ae2a20a0c4be8af2ef7b3b468Timo Sirainen while (i_stream_read_more(input, &data, &size) > 0) {
075081e25ef07989be10f7c9cf85f833f90be46fTimo Sirainen test_assert_idx(input->eof && input->stream_errno == 0, i);
24d6fd2b8e2c327ae2a20a0c4be8af2ef7b3b468Timo Sirainen if (memcmp(hash_file, hash_attached, SHA1_RESULTLEN) != 0)
075081e25ef07989be10f7c9cf85f833f90be46fTimo Sirainen /* try again without knowing the message's size */
24d6fd2b8e2c327ae2a20a0c4be8af2ef7b3b468Timo Sirainen /* try with a wrong message size */
24d6fd2b8e2c327ae2a20a0c4be8af2ef7b3b468Timo Sirainen for (int i = 0; i < 2; i++) {
24d6fd2b8e2c327ae2a20a0c4be8af2ef7b3b468Timo Sirainen input2 = i_stream_create_from_data(base_buf->data, base_buf->used);
075081e25ef07989be10f7c9cf85f833f90be46fTimo Sirainen input = test_build_original_istream(input2, orig_msg_size +
24d6fd2b8e2c327ae2a20a0c4be8af2ef7b3b468Timo Sirainen while (i_stream_read_more(input, &data, &size) > 0)
24d6fd2b8e2c327ae2a20a0c4be8af2ef7b3b468Timo Sirainen test_assert(input->stream_errno == (i == 0 ? EPIPE : EINVAL));
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen datainput = test_istream_create_data(mail_input, sizeof(mail_input));
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen input = i_stream_create_attachment_extractor(datainput, &set, NULL);
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen sizeof(BINARY_TEXT_LONG)-1 + strlen(BINARY_TEXT_SHORT));
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen test_assert(memcmp(data, BINARY_TEXT_LONG, sizeof(BINARY_TEXT_LONG)-1) == 0);
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen test_assert(memcmp(data + sizeof(BINARY_TEXT_LONG)-1,
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen BINARY_TEXT_SHORT, strlen(BINARY_TEXT_SHORT)) == 0);
9d0be57bcc352f776a327d7e2a6c6232137f4a04Timo Sirainenstatic bool test_istream_attachment_extractor_one(const char *body, int err_type)
2ac5f36aa7c2e7a07ba8815d43a6d7483f62e74cTimo Sirainen const size_t prefix_len = strlen(mail_broken_input_body_prefix);
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainen mail_text = i_strconcat(mail_broken_input_body_prefix, body, NULL);
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainen datainput = test_istream_create_data(mail_text, strlen(mail_text));
9d0be57bcc352f776a327d7e2a6c6232137f4a04Timo Sirainen set.open_attachment_ostream = test_open_attachment_ostream_error;
9d0be57bcc352f776a327d7e2a6c6232137f4a04Timo Sirainen set.close_attachment_ostream = test_close_attachment_ostream_error;
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainen input = i_stream_create_attachment_extractor(datainput, &set, NULL);
9d0be57bcc352f776a327d7e2a6c6232137f4a04Timo Sirainen test_assert(ret == -1 && input->stream_errno == EIO);
9d0be57bcc352f776a327d7e2a6c6232137f4a04Timo Sirainen test_assert(ret == -1 && input->stream_errno == 0);
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainen memcmp(data, mail_broken_input_body_prefix, prefix_len) == 0);
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainen unchanged = attachment_data->used <= strlen(body) &&
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainen memcmp(attachment_data->data, body, attachment_data->used) == 0 &&
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainen strlen(body) - attachment_data->used == size &&
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainen memcmp(data, body + attachment_data->used, size) == 0;
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainenstatic void test_istream_attachment_extractor(void)
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainen unsigned int i;
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainen for (i = 0; i < N_ELEMENTS(mail_broken_input_bodies); i++)
9d0be57bcc352f776a327d7e2a6c6232137f4a04Timo Sirainen test_assert(test_istream_attachment_extractor_one(mail_broken_input_bodies[i], 0));
108340ffacae3b7d09fa51b566d77aa51dd6483bTimo Sirainen for (i = 0; i < N_ELEMENTS(mail_nonbroken_input_bodies); i++)
9d0be57bcc352f776a327d7e2a6c6232137f4a04Timo Sirainen test_assert(!test_istream_attachment_extractor_one(mail_nonbroken_input_bodies[i], 0));
9d0be57bcc352f776a327d7e2a6c6232137f4a04Timo Sirainenstatic void test_istream_attachment_extractor_error(void)
9d0be57bcc352f776a327d7e2a6c6232137f4a04Timo Sirainen unsigned int i;
9d0be57bcc352f776a327d7e2a6c6232137f4a04Timo Sirainen test_begin("istream attachment extractor error");
9d0be57bcc352f776a327d7e2a6c6232137f4a04Timo Sirainen for (int err_type = 1; err_type <= 2; err_type++) {
9d0be57bcc352f776a327d7e2a6c6232137f4a04Timo Sirainen for (i = 0; i < N_ELEMENTS(mail_broken_input_bodies); i++)
9d0be57bcc352f776a327d7e2a6c6232137f4a04Timo Sirainen test_istream_attachment_extractor_one(mail_broken_input_bodies[i], err_type);
9d0be57bcc352f776a327d7e2a6c6232137f4a04Timo Sirainen for (i = 0; i < N_ELEMENTS(mail_nonbroken_input_bodies); i++)
9d0be57bcc352f776a327d7e2a6c6232137f4a04Timo Sirainen test_istream_attachment_extractor_one(mail_nonbroken_input_bodies[i], err_type);
24d6fd2b8e2c327ae2a20a0c4be8af2ef7b3b468Timo Sirainenstatic void test_istream_attachment_connector(void)
24d6fd2b8e2c327ae2a20a0c4be8af2ef7b3b468Timo Sirainen input = i_stream_create_from_data(mail_input, sizeof(mail_input));
8aeae03f9f447c8a792b215c9fb954468053c556Timo Sirainen fprintf(stderr, "istream-attachment-extractor: mismatch on file %s\n",
baf3e87e186453fda13bd21f7cbcb2efc8492e8bTimo Sirainen static void (*const test_functions[])(void) = {