Added flush() on 'outputStream' + added unit tests for 'charsetFilteredOutputStream' when input contains invalid sequences.
This commit is contained in:
parent
5e41e4bd7e
commit
c241f071d2
@ -162,6 +162,8 @@ void charsetConverter::convert(const string& in, string& out)
|
||||
utility::outputStreamStringAdapter os(out);
|
||||
|
||||
convert(is, os);
|
||||
|
||||
os.flush();
|
||||
}
|
||||
|
||||
|
||||
@ -327,6 +329,60 @@ void charsetFilteredOutputStream::write
|
||||
}
|
||||
|
||||
|
||||
void charsetFilteredOutputStream::flush()
|
||||
{
|
||||
if (m_desc == NULL)
|
||||
throw exceptions::charset_conv_error("Cannot initialize converter.");
|
||||
|
||||
const iconv_t cd = *static_cast <iconv_t*>(m_desc);
|
||||
|
||||
size_t offset = 0;
|
||||
|
||||
// Process unconverted bytes
|
||||
while (m_unconvCount != 0)
|
||||
{
|
||||
// Try a conversion
|
||||
const char* inPtr = m_unconvBuffer + offset;
|
||||
size_t inLength = m_unconvCount;
|
||||
char* outPtr = m_outputBuffer;
|
||||
size_t outLength = sizeof(m_outputBuffer);
|
||||
|
||||
const size_t inLength0 = inLength;
|
||||
|
||||
if (iconv(cd, ICONV_HACK(&inPtr), &inLength, &outPtr, &outLength) == static_cast <size_t>(-1))
|
||||
{
|
||||
const size_t inputConverted = inLength0 - inLength;
|
||||
|
||||
// Skip a "blocking" character
|
||||
if (inputConverted == 0)
|
||||
{
|
||||
m_stream.write("?", 1);
|
||||
|
||||
offset++;
|
||||
m_unconvCount--;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Write successfully converted bytes
|
||||
m_stream.write(m_outputBuffer, sizeof(m_outputBuffer) - outLength);
|
||||
|
||||
offset += inputConverted;
|
||||
m_unconvCount -= inputConverted;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Write successfully converted bytes
|
||||
m_stream.write(m_outputBuffer, sizeof(m_outputBuffer) - outLength);
|
||||
|
||||
m_unconvCount = 0;
|
||||
}
|
||||
}
|
||||
|
||||
m_stream.flush();
|
||||
}
|
||||
|
||||
|
||||
} // utility
|
||||
|
||||
|
||||
|
@ -542,6 +542,8 @@ void SMTPTransport::send(const mailbox& expeditor, const mailboxList& recipients
|
||||
|
||||
utility::bufferedStreamCopy(is, fos, size, progress);
|
||||
|
||||
fos.flush();
|
||||
|
||||
// Send end-of-data delimiter
|
||||
m_socket->sendRaw("\r\n.\r\n", 5);
|
||||
|
||||
|
@ -119,6 +119,11 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
void flush()
|
||||
{
|
||||
::fsync(m_desc);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
const int m_desc;
|
||||
|
@ -119,6 +119,12 @@ void posixFileWriterOutputStream::write(const value_type* const data, const size
|
||||
}
|
||||
|
||||
|
||||
void posixFileWriterOutputStream::flush()
|
||||
{
|
||||
::fsync(m_fd);
|
||||
}
|
||||
|
||||
|
||||
|
||||
//
|
||||
// posixFileReaderInputStream
|
||||
|
@ -516,6 +516,13 @@ void windowsFileWriterOutputStream::write(const value_type* const data, const si
|
||||
windowsFileSystemFactory::reportError(m_path, GetLastError());
|
||||
}
|
||||
|
||||
|
||||
void windowsFileWriterOutputStream::flush()
|
||||
{
|
||||
// TODO
|
||||
}
|
||||
|
||||
|
||||
} // windows
|
||||
} // platforms
|
||||
} // vmime
|
||||
|
@ -160,6 +160,13 @@ void dotFilteredOutputStream::write
|
||||
}
|
||||
|
||||
|
||||
void dotFilteredOutputStream::flush()
|
||||
{
|
||||
// Do nothing
|
||||
m_stream.flush();
|
||||
}
|
||||
|
||||
|
||||
// CRLFToLFFilteredOutputStream
|
||||
|
||||
CRLFToLFFilteredOutputStream::CRLFToLFFilteredOutputStream(outputStream& os)
|
||||
@ -185,8 +192,8 @@ void CRLFToLFFilteredOutputStream::write
|
||||
const value_type* start = data;
|
||||
|
||||
// Warning: if the whole buffer finishes with '\r', this
|
||||
// last character will not be written back...
|
||||
// TODO: add a finalize() method?
|
||||
// last character will not be written back if flush() is
|
||||
// not called
|
||||
if (m_previousChar == '\r')
|
||||
{
|
||||
if (*pos != '\n')
|
||||
@ -228,6 +235,14 @@ void CRLFToLFFilteredOutputStream::write
|
||||
}
|
||||
|
||||
|
||||
void CRLFToLFFilteredOutputStream::flush()
|
||||
{
|
||||
m_stream.flush();
|
||||
|
||||
// TODO
|
||||
}
|
||||
|
||||
|
||||
// stopSequenceFilteredInputStream <1>
|
||||
|
||||
template <>
|
||||
|
@ -100,6 +100,12 @@ void outputStreamAdapter::write
|
||||
}
|
||||
|
||||
|
||||
void outputStreamAdapter::flush()
|
||||
{
|
||||
m_stream.flush();
|
||||
}
|
||||
|
||||
|
||||
|
||||
// outputStreamStringAdapter
|
||||
|
||||
@ -116,6 +122,12 @@ void outputStreamStringAdapter::write(const value_type* const data, const size_t
|
||||
}
|
||||
|
||||
|
||||
void outputStreamStringAdapter::flush()
|
||||
{
|
||||
// Do nothing
|
||||
}
|
||||
|
||||
|
||||
|
||||
// outputStreamByteArrayAdapter
|
||||
|
||||
@ -132,6 +144,12 @@ void outputStreamByteArrayAdapter::write(const value_type* const data, const siz
|
||||
}
|
||||
|
||||
|
||||
void outputStreamByteArrayAdapter::flush()
|
||||
{
|
||||
// Do nothing
|
||||
}
|
||||
|
||||
|
||||
|
||||
// inputStreamAdapter
|
||||
|
||||
@ -421,6 +439,12 @@ void outputStreamSocketAdapter::write
|
||||
}
|
||||
|
||||
|
||||
void outputStreamSocketAdapter::flush()
|
||||
{
|
||||
// Do nothing
|
||||
}
|
||||
|
||||
|
||||
// inputStreamSocketAdapter
|
||||
|
||||
inputStreamSocketAdapter::inputStreamSocketAdapter(net::socket& sok)
|
||||
@ -460,7 +484,5 @@ const stream::size_type inputStreamSocketAdapter::skip
|
||||
#endif // VMIME_HAVE_MESSAGING_FEATURES
|
||||
|
||||
|
||||
|
||||
|
||||
} // utility
|
||||
} // vmime
|
||||
|
@ -101,6 +101,9 @@ VMIME_TEST_SUITE_BEGIN
|
||||
VMIME_TEST(testFilterValid2)
|
||||
VMIME_TEST(testFilterValid3)
|
||||
|
||||
// Test invalid input
|
||||
VMIME_TEST(testFilterInvalid1)
|
||||
|
||||
// TODO: more tests
|
||||
VMIME_TEST_LIST_END
|
||||
|
||||
@ -130,6 +133,8 @@ VMIME_TEST_SUITE_BEGIN
|
||||
vmime::charset::convert
|
||||
(is, os, inputCharset, outputCharset);
|
||||
|
||||
os.flush();
|
||||
|
||||
VASSERT_EQ("1", toHex(expectedOut), toHex(actualOut));
|
||||
}
|
||||
|
||||
@ -148,6 +153,8 @@ VMIME_TEST_SUITE_BEGIN
|
||||
|
||||
vmime::utility::bufferedStreamCopy(is, os);
|
||||
|
||||
os.flush();
|
||||
|
||||
VASSERT_EQ("1", toHex(expectedOut), toHex(actualOut));
|
||||
}
|
||||
|
||||
@ -169,6 +176,8 @@ VMIME_TEST_SUITE_BEGIN
|
||||
for (int i = 0 ; !is.eof() ; ++i)
|
||||
os.write(buffer, is.read(buffer, 1));
|
||||
|
||||
os.flush();
|
||||
|
||||
VASSERT_EQ("1", toHex(expectedOut), toHex(actualOut));
|
||||
}
|
||||
|
||||
@ -190,6 +199,31 @@ VMIME_TEST_SUITE_BEGIN
|
||||
for (int i = 0 ; !is.eof() ; ++i)
|
||||
os.write(buffer, is.read(buffer, (i % 5) + 1));
|
||||
|
||||
os.flush();
|
||||
|
||||
VASSERT_EQ("1", toHex(expectedOut), toHex(actualOut));
|
||||
}
|
||||
|
||||
void testFilterInvalid1()
|
||||
{
|
||||
vmime::string in("foo\xab\xcd\xef bar");
|
||||
vmime::string expectedOut("foo??? bar");
|
||||
|
||||
vmime::string actualOut;
|
||||
vmime::utility::outputStreamStringAdapter osa(actualOut);
|
||||
vmime::utility::charsetFilteredOutputStream os
|
||||
(vmime::charset("utf-8"),
|
||||
vmime::charset("iso-8859-1"), osa);
|
||||
|
||||
vmime::utility::inputStreamStringAdapter is(in);
|
||||
|
||||
vmime::utility::stream::value_type buffer[16];
|
||||
|
||||
for (int i = 0 ; !is.eof() ; ++i)
|
||||
os.write(buffer, is.read(buffer, 1));
|
||||
|
||||
os.flush();
|
||||
|
||||
VASSERT_EQ("1", toHex(expectedOut), toHex(actualOut));
|
||||
}
|
||||
|
||||
|
@ -113,6 +113,7 @@ public:
|
||||
outputStream& getNextOutputStream();
|
||||
|
||||
void write(const value_type* const data, const size_type count);
|
||||
void flush();
|
||||
|
||||
private:
|
||||
|
||||
|
@ -47,6 +47,7 @@ public:
|
||||
~posixFileWriterOutputStream();
|
||||
|
||||
void write(const value_type* const data, const size_type count);
|
||||
void flush();
|
||||
|
||||
private:
|
||||
|
||||
|
@ -192,6 +192,7 @@ public:
|
||||
public:
|
||||
|
||||
void write(const value_type* const data, const size_type count);
|
||||
void flush();
|
||||
|
||||
private:
|
||||
|
||||
|
@ -112,6 +112,7 @@ public:
|
||||
outputStream& getNextOutputStream();
|
||||
|
||||
void write(const value_type* const data, const size_type count);
|
||||
void flush();
|
||||
|
||||
private:
|
||||
|
||||
@ -137,6 +138,7 @@ public:
|
||||
outputStream& getNextOutputStream();
|
||||
|
||||
void write(const value_type* const data, const size_type count);
|
||||
void flush();
|
||||
|
||||
private:
|
||||
|
||||
|
@ -88,6 +88,11 @@ public:
|
||||
* @param count number of bytes to write
|
||||
*/
|
||||
virtual void write(const value_type* const data, const size_type count) = 0;
|
||||
|
||||
/** Flush this output stream and forces any buffered output
|
||||
* bytes to be written out to the stream.
|
||||
*/
|
||||
virtual void flush() = 0;
|
||||
};
|
||||
|
||||
|
||||
@ -205,6 +210,7 @@ public:
|
||||
outputStreamAdapter(std::ostream& os);
|
||||
|
||||
void write(const value_type* const data, const size_type count);
|
||||
void flush();
|
||||
|
||||
private:
|
||||
|
||||
@ -222,6 +228,7 @@ public:
|
||||
outputStreamStringAdapter(string& buffer);
|
||||
|
||||
void write(const value_type* const data, const size_type count);
|
||||
void flush();
|
||||
|
||||
private:
|
||||
|
||||
@ -239,6 +246,7 @@ public:
|
||||
outputStreamByteArrayAdapter(byteArray& array);
|
||||
|
||||
void write(const value_type* const data, const size_type count);
|
||||
void flush();
|
||||
|
||||
private:
|
||||
|
||||
@ -383,6 +391,7 @@ public:
|
||||
outputStreamSocketAdapter(net::socket& sok);
|
||||
|
||||
void write(const value_type* const data, const size_type count);
|
||||
void flush();
|
||||
|
||||
private:
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user