bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2007-2018 Dovecot authors, see the included COPYING file */
d51bddedf8c02326124935e1ffbee5da2275f8d4Timo Sirainen#define BINARY_TEXT_LONG "we have\ra lot \nof \0binary stuff in here\n" \
d51bddedf8c02326124935e1ffbee5da2275f8d4Timo Sirainen"b adjig sadjg jasidgjiaehga3wht8a3w8ghxjc dsgad hasdghsd gasd ds" \
d51bddedf8c02326124935e1ffbee5da2275f8d4Timo Sirainen"jdsoga sjdga0w3tjhawjgsertniq3n5oqerjqw2r89q23h awhrqh835r8a"
d51bddedf8c02326124935e1ffbee5da2275f8d4Timo Sirainen"d2UgaGF2ZQ1hIGxvdCAKb2YgAGJpbmFyeSBzdHVmZiBpbiBoZXJlCmIgYWRqaWcgc2FkamcgamFz\r\n" \
d51bddedf8c02326124935e1ffbee5da2275f8d4Timo Sirainen"aWRnamlhZWhnYTN3aHQ4YTN3OGdoeGpjIGRzZ2FkIGhhc2RnaHNkIGdhc2QgZHNqZHNvZ2Egc2pk\r\n" \
d51bddedf8c02326124935e1ffbee5da2275f8d4Timo Sirainen"Z2EwdzN0amhhd2pnc2VydG5pcTNuNW9xZXJqcXcycjg5cTIzaCBhd2hycWg4MzVyOGE="
d51bddedf8c02326124935e1ffbee5da2275f8d4Timo Sirainen"MIME-Version: 1.0\r\n"
d51bddedf8c02326124935e1ffbee5da2275f8d4Timo Sirainen"Content-Type: multipart/alternative;\r\n boundary=\"bound\"\r\n"
d51bddedf8c02326124935e1ffbee5da2275f8d4Timo Sirainen"mime header\r\n"
d51bddedf8c02326124935e1ffbee5da2275f8d4Timo Sirainen"\r\n--bound\r\n"
d51bddedf8c02326124935e1ffbee5da2275f8d4Timo Sirainen"Content-Transfer-Encoding: binary\r\n"
2c26b19c543706fff723c3f52b401817507a0ee0Timo Sirainen"Content-Type: text/plain\r\n"
d51bddedf8c02326124935e1ffbee5da2275f8d4Timo Sirainen"\r\n--bound\r\n"
d51bddedf8c02326124935e1ffbee5da2275f8d4Timo Sirainen"Content-Type: text/plain\r\n"
d51bddedf8c02326124935e1ffbee5da2275f8d4Timo Sirainen"Content-Transfer-Encoding: binary\r\n"
1e6170b4c4bc8ed15a6ca47d8dbc056a1e8c9db4Timo Sirainen"\n--bound\r\n"
d51bddedf8c02326124935e1ffbee5da2275f8d4Timo Sirainen"Content-Type: text/plain\r\n"
d51bddedf8c02326124935e1ffbee5da2275f8d4Timo Sirainen"hello world\r\n"
d51bddedf8c02326124935e1ffbee5da2275f8d4Timo Sirainen"\r\n--bound--\r\n";
d51bddedf8c02326124935e1ffbee5da2275f8d4Timo Sirainen"MIME-Version: 1.0\r\n"
d51bddedf8c02326124935e1ffbee5da2275f8d4Timo Sirainen"Content-Type: multipart/alternative;\r\n boundary=\"bound\"\r\n"
d51bddedf8c02326124935e1ffbee5da2275f8d4Timo Sirainen"mime header\r\n"
d51bddedf8c02326124935e1ffbee5da2275f8d4Timo Sirainen"\r\n--bound\r\n"
d51bddedf8c02326124935e1ffbee5da2275f8d4Timo Sirainen"Content-Transfer-Encoding: base64\r\n"
2c26b19c543706fff723c3f52b401817507a0ee0Timo Sirainen"Content-Type: text/plain\r\n"
d51bddedf8c02326124935e1ffbee5da2275f8d4Timo Sirainen"\r\n--bound\r\n"
d51bddedf8c02326124935e1ffbee5da2275f8d4Timo Sirainen"Content-Type: text/plain\r\n"
d51bddedf8c02326124935e1ffbee5da2275f8d4Timo Sirainen"Content-Transfer-Encoding: base64\r\n"
1e6170b4c4bc8ed15a6ca47d8dbc056a1e8c9db4Timo Sirainen"\n--bound\r\n"
d51bddedf8c02326124935e1ffbee5da2275f8d4Timo Sirainen"Content-Type: text/plain\r\n"
d51bddedf8c02326124935e1ffbee5da2275f8d4Timo Sirainen"hello world\r\n"
d51bddedf8c02326124935e1ffbee5da2275f8d4Timo Sirainen"\r\n--bound--\r\n";
1e6170b4c4bc8ed15a6ca47d8dbc056a1e8c9db4Timo Sirainen"MIME-Version: 1.0\r\n"
1e6170b4c4bc8ed15a6ca47d8dbc056a1e8c9db4Timo Sirainen"Content-Transfer-Encoding: binary\r\n"
1e6170b4c4bc8ed15a6ca47d8dbc056a1e8c9db4Timo Sirainen"Content-Type: text/plain\r\n"
1e6170b4c4bc8ed15a6ca47d8dbc056a1e8c9db4Timo Sirainen"MIME-Version: 1.0\r\n"
1e6170b4c4bc8ed15a6ca47d8dbc056a1e8c9db4Timo Sirainen"Content-Transfer-Encoding: base64\r\n"
1e6170b4c4bc8ed15a6ca47d8dbc056a1e8c9db4Timo Sirainen"Content-Type: text/plain\r\n"
1e6170b4c4bc8ed15a6ca47d8dbc056a1e8c9db4Timo Sirainen"MIME-Version: 1.0\r\n"
1e6170b4c4bc8ed15a6ca47d8dbc056a1e8c9db4Timo Sirainen"Content-Type: text/plain\r\n"
1e6170b4c4bc8ed15a6ca47d8dbc056a1e8c9db4Timo Sirainentest_istream_binary_converter_test(const char *mail_input, unsigned int mail_input_len,
1e6170b4c4bc8ed15a6ca47d8dbc056a1e8c9db4Timo Sirainen const char *mail_output, unsigned int mail_output_len,
1e6170b4c4bc8ed15a6ca47d8dbc056a1e8c9db4Timo Sirainen unsigned int idx)
1e6170b4c4bc8ed15a6ca47d8dbc056a1e8c9db4Timo Sirainen datainput = test_istream_create_data(mail_input, mail_input_len);
d51bddedf8c02326124935e1ffbee5da2275f8d4Timo Sirainen input = i_stream_create_binary_converter(datainput);
1e6170b4c4bc8ed15a6ca47d8dbc056a1e8c9db4Timo Sirainenstatic void test_istream_binary_converter_mime(void)
1e6170b4c4bc8ed15a6ca47d8dbc056a1e8c9db4Timo Sirainen test_begin("istream binary converter in mime parts");
1e6170b4c4bc8ed15a6ca47d8dbc056a1e8c9db4Timo Sirainen test_istream_binary_converter_test(mail_input_mime, sizeof(mail_input_mime)-1,
1e6170b4c4bc8ed15a6ca47d8dbc056a1e8c9db4Timo Sirainen mail_output_mime, sizeof(mail_output_mime)-1, 0);
1e6170b4c4bc8ed15a6ca47d8dbc056a1e8c9db4Timo Sirainenstatic void test_istream_binary_converter_root(void)
1e6170b4c4bc8ed15a6ca47d8dbc056a1e8c9db4Timo Sirainen const char *const suffixes[] = { "\n", "\r\n", "\n\r\n\n\n" };
1e6170b4c4bc8ed15a6ca47d8dbc056a1e8c9db4Timo Sirainen unsigned int i;
1e6170b4c4bc8ed15a6ca47d8dbc056a1e8c9db4Timo Sirainen unsigned int input_hdr_len = sizeof(mail_input_root_hdr)-1;
1e6170b4c4bc8ed15a6ca47d8dbc056a1e8c9db4Timo Sirainen test_begin("istream binary converter in root");
1e6170b4c4bc8ed15a6ca47d8dbc056a1e8c9db4Timo Sirainen buffer_append(inbuf, mail_input_root_hdr, input_hdr_len);
1e6170b4c4bc8ed15a6ca47d8dbc056a1e8c9db4Timo Sirainen buffer_append(outbuf, mail_output_root_hdr, sizeof(mail_output_root_hdr)-1);
1e6170b4c4bc8ed15a6ca47d8dbc056a1e8c9db4Timo Sirainen buffer_set_used_size(outbuf, sizeof(mail_output_root_hdr)-1);
1e6170b4c4bc8ed15a6ca47d8dbc056a1e8c9db4Timo Sirainen buffer_append(inbuf, BINARY_TEXT_SHORT, sizeof(BINARY_TEXT_SHORT)-1);
1e6170b4c4bc8ed15a6ca47d8dbc056a1e8c9db4Timo Sirainen buffer_append(inbuf, suffixes[i], strlen(suffixes[i]));
1e6170b4c4bc8ed15a6ca47d8dbc056a1e8c9db4Timo Sirainen base64_encode(CONST_PTR_OFFSET(inbuf->data, input_hdr_len),
1e6170b4c4bc8ed15a6ca47d8dbc056a1e8c9db4Timo Sirainen test_istream_binary_converter_test(inbuf->data, inbuf->used,
1e6170b4c4bc8ed15a6ca47d8dbc056a1e8c9db4Timo Sirainenstatic void test_istream_binary_converter_root_nonbinary(void)
1e6170b4c4bc8ed15a6ca47d8dbc056a1e8c9db4Timo Sirainen test_begin("istream binary converter in root having non-binary");
1e6170b4c4bc8ed15a6ca47d8dbc056a1e8c9db4Timo Sirainen test_istream_binary_converter_test(mail_root_nonbinary, sizeof(mail_root_nonbinary)-1,
1e6170b4c4bc8ed15a6ca47d8dbc056a1e8c9db4Timo Sirainen mail_root_nonbinary, sizeof(mail_root_nonbinary)-1, 0);
d51bddedf8c02326124935e1ffbee5da2275f8d4Timo Sirainen unsigned char hash_file[SHA1_RESULTLEN], hash_converter[SHA1_RESULTLEN];
d51bddedf8c02326124935e1ffbee5da2275f8d4Timo Sirainen /* get hash when directly reading input */
3858a7a5da361c35f1e6e50c8e3214dc0cf379d6Phil Carmody while (i_stream_read_more(input, &data, &size) > 0) {
d51bddedf8c02326124935e1ffbee5da2275f8d4Timo Sirainen /* get hash when going through converter */
d51bddedf8c02326124935e1ffbee5da2275f8d4Timo Sirainen input2 = i_stream_create_binary_converter(input);
3858a7a5da361c35f1e6e50c8e3214dc0cf379d6Phil Carmody while (i_stream_read_more(input2, &data, &size) > 0) {
d51bddedf8c02326124935e1ffbee5da2275f8d4Timo Sirainen if (memcmp(hash_file, hash_converter, SHA1_RESULTLEN) != 0) {
d51bddedf8c02326124935e1ffbee5da2275f8d4Timo Sirainen fprintf(stderr, "istream-binary-converter: mismatch on file %s\n",
baf3e87e186453fda13bd21f7cbcb2efc8492e8bTimo Sirainen static void (*const test_functions[])(void) = {