test-iostream-temp.c revision 431c0aba57fba2fd6eb398acd6ef4e73b5dc1b86
2e37d45867d081db150ab78dad303b9077aea24fTimo Sirainen/* Copyright (c) 2016 Dovecot authors, see the included COPYING file */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#include "test-lib.h"
bca919b207e27d0d08b431bdb0f2ac099ef8b512Timo Sirainen#include "istream.h"
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#include "ostream.h"
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen#include "iostream-temp.h"
7a7d2aa11e46195e2d92d6c337d7e78052a5ce67Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#include <unistd.h>
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#include <fcntl.h>
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstatic void test_iostream_temp_create_sized_memory(void)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen{
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct ostream *output;
2b9e49e4e65e3e2dca38f56971029a3051ccdb99Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen test_begin("iostream_temp_create_sized() memory");
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen output = iostream_temp_create_sized(".intentional-nonexistent-error/", 0, "test", 4);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen test_assert(o_stream_send(output, "123", 3) == 3);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen test_assert(output->offset == 3);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen test_assert(o_stream_send(output, "4", 1) == 1);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen test_assert(output->offset == 4);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen test_assert(o_stream_get_fd(output) == -1);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
2b9e49e4e65e3e2dca38f56971029a3051ccdb99Timo Sirainen /* now we'll try to switch to writing to a file, but it'll fail */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen test_expect_error_string("safe_mkstemp");
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen test_assert(o_stream_send(output, "5", 1) == 1);
afa201e7e1d2447e8dfa1aff43de0fdad564105fTimo Sirainen test_expect_no_more_errors();
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen test_assert(o_stream_get_fd(output) == -1);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen o_stream_destroy(&output);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen test_end();
e5fd6dfd0a492e4708d4dbb7971d7fc5d7b8fd85Timo Sirainen}
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainenstatic void test_iostream_temp_create_sized_disk(void)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen{
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct ostream *output;
d22301419109ed4a38351715e6760011421dadecTimo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen test_begin("iostream_temp_create_sized() disk");
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen output = iostream_temp_create_sized(".", 0, "test", 4);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen test_assert(o_stream_send(output, "123", 3) == 3);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen test_assert(output->offset == 3);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen test_assert(o_stream_send(output, "4", 1) == 1);
2b9e49e4e65e3e2dca38f56971029a3051ccdb99Timo Sirainen test_assert(output->offset == 4);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen test_assert(o_stream_get_fd(output) == -1);
d22301419109ed4a38351715e6760011421dadecTimo Sirainen test_assert(o_stream_send(output, "5", 1) == 1);
d22301419109ed4a38351715e6760011421dadecTimo Sirainen test_assert(output->offset == 5);
d22301419109ed4a38351715e6760011421dadecTimo Sirainen test_assert(o_stream_get_fd(output) != -1);
d22301419109ed4a38351715e6760011421dadecTimo Sirainen o_stream_destroy(&output);
d22301419109ed4a38351715e6760011421dadecTimo Sirainen test_end();
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen}
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainenstatic void test_iostream_temp_istream(void)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen{
d22301419109ed4a38351715e6760011421dadecTimo Sirainen struct istream *input, *input2, *temp_input;
d22301419109ed4a38351715e6760011421dadecTimo Sirainen struct ostream *output;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen int fd;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen test_begin("iostream_temp istream");
d22301419109ed4a38351715e6760011421dadecTimo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen fd = open(".temp.istream", O_RDWR | O_CREAT | O_TRUNC, 0600);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (fd == -1)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen i_fatal("create(.temp.istream) failed: %m");
4b2a4c8c762e3eaddf7fd2abfe7d4cca6e5e3fd8Timo Sirainen test_assert(write(fd, "foobar", 6) == 6);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen test_assert(lseek(fd, 0, SEEK_SET) == 0);
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen input = i_stream_create_fd_autoclose(&fd, 1024);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* a working fd-dup */
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen output = iostream_temp_create_sized(".nonexistent/",
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen IOSTREAM_TEMP_FLAG_TRY_FD_DUP, "test", 1);
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen test_assert(o_stream_send_istream(output, input) == OSTREAM_SEND_ISTREAM_RESULT_FINISHED);
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen test_assert(output->offset == 6);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen temp_input = iostream_temp_finish(&output, 128);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen test_assert(i_stream_read(temp_input) == 6);
7baab0b0b60df7ce9093d0881cd322dff1e79491Timo Sirainen i_stream_destroy(&temp_input);
4b2a4c8c762e3eaddf7fd2abfe7d4cca6e5e3fd8Timo Sirainen
4b2a4c8c762e3eaddf7fd2abfe7d4cca6e5e3fd8Timo Sirainen /* non-working fd-dup: write data before sending istream */
4b2a4c8c762e3eaddf7fd2abfe7d4cca6e5e3fd8Timo Sirainen i_stream_seek(input, 0);
2b9e49e4e65e3e2dca38f56971029a3051ccdb99Timo Sirainen output = iostream_temp_create_sized(".intentional-nonexistent-error/",
ca98892a6b8a30ffc1fe26fcf02c7d59e3204e7eTimo Sirainen IOSTREAM_TEMP_FLAG_TRY_FD_DUP, "test", 4);
4b2a4c8c762e3eaddf7fd2abfe7d4cca6e5e3fd8Timo Sirainen test_assert(o_stream_send(output, "1234", 4) == 4);
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen test_assert(output->offset == 4);
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen test_expect_error_string("safe_mkstemp");
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen test_assert(o_stream_send_istream(output, input) == OSTREAM_SEND_ISTREAM_RESULT_FINISHED);
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen test_assert(output->offset == 10);
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen test_expect_no_more_errors();
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen o_stream_destroy(&output);
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen /* non-working fd-dup: write data after sending istream */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen i_stream_seek(input, 0);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen output = iostream_temp_create_sized(".intentional-nonexistent-error/",
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen IOSTREAM_TEMP_FLAG_TRY_FD_DUP, "test", 4);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen test_assert(o_stream_send_istream(output, input) == OSTREAM_SEND_ISTREAM_RESULT_FINISHED);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen test_assert(output->offset == 6);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen test_expect_error_string("safe_mkstemp");
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen test_assert(o_stream_send(output, "1", 1) == 1);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen test_assert(output->offset == 7);
4b2a4c8c762e3eaddf7fd2abfe7d4cca6e5e3fd8Timo Sirainen test_expect_no_more_errors();
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen o_stream_destroy(&output);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* non-working fd-dup: send two istreams */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen i_stream_seek(input, 0);
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen input2 = i_stream_create_limit(input, (uoff_t)-1);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen output = iostream_temp_create_sized(".intentional-nonexistent-error/",
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen IOSTREAM_TEMP_FLAG_TRY_FD_DUP, "test", 4);
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen test_assert(o_stream_send_istream(output, input) == OSTREAM_SEND_ISTREAM_RESULT_FINISHED);
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen test_assert(output->offset == 6);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen test_expect_error_string("safe_mkstemp");
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen test_assert(o_stream_send_istream(output, input2) == OSTREAM_SEND_ISTREAM_RESULT_FINISHED);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen test_assert(output->offset == 12);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen test_expect_no_more_errors();
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen o_stream_destroy(&output);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen i_stream_unref(&input2);
7e1b549a1d841e59faad16430cbd1d56317db8afTimo Sirainen
7e1b549a1d841e59faad16430cbd1d56317db8afTimo Sirainen i_stream_destroy(&input);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen i_unlink(".temp.istream");
7baab0b0b60df7ce9093d0881cd322dff1e79491Timo Sirainen test_end();
7baab0b0b60df7ce9093d0881cd322dff1e79491Timo Sirainen}
7baab0b0b60df7ce9093d0881cd322dff1e79491Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenvoid test_iostream_temp(void)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen{
dd4f30895ebbddd77e000472fbadcb3128ae2883Timo Sirainen test_iostream_temp_create_sized_memory();
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen test_iostream_temp_create_sized_disk();
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen test_iostream_temp_istream();
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen}
dd4f30895ebbddd77e000472fbadcb3128ae2883Timo Sirainen