diff options
Diffstat (limited to 'tests/utility/filteredStreamTest.cpp')
-rw-r--r-- | tests/utility/filteredStreamTest.cpp | 423 |
1 files changed, 203 insertions, 220 deletions
diff --git a/tests/utility/filteredStreamTest.cpp b/tests/utility/filteredStreamTest.cpp index 0552902a..ca974774 100644 --- a/tests/utility/filteredStreamTest.cpp +++ b/tests/utility/filteredStreamTest.cpp @@ -17,274 +17,257 @@ // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. // -#include "../lib/unit++/unit++.h" - -#include <iostream> -#include <ostream> -#include <algorithm> - -#include "vmime/vmime.hpp" -#include "vmime/platforms/posix/posixHandler.hpp" +#include "tests/testUtils.hpp" #include "vmime/utility/filteredStream.hpp" -using namespace unitpp; - -namespace -{ - class filteredStreamTest : public suite - { - class chunkInputStream : public vmime::utility::inputStream - { - private: +#define VMIME_TEST_SUITE filteredStreamTest +#define VMIME_TEST_SUITE_MODULE "Utility" - std::vector <std::string> m_chunks; - std::vector <std::string>::size_type m_index; - public: +VMIME_TEST_SUITE_BEGIN - chunkInputStream() : m_index(0) { } + VMIME_TEST_LIST_BEGIN + VMIME_TEST(testDotFilteredInputStream) + VMIME_TEST(testDotFilteredOutputStream) + VMIME_TEST(testCRLFToLFFilteredOutputStream) + VMIME_TEST(testStopSequenceFilteredInputStream1) + VMIME_TEST(testStopSequenceFilteredInputStreamN_2) + VMIME_TEST(testStopSequenceFilteredInputStreamN_3) + VMIME_TEST_LIST_END - void addChunk(const std::string& chunk) { m_chunks.push_back(chunk); } - const bool eof() const { return (m_index >= m_chunks.size()); } - void reset() { m_index = 0; } + class chunkInputStream : public vmime::utility::inputStream + { + private: - const size_type read(value_type* const data, const size_type /* count */) - { - if (eof()) - return 0; + std::vector <std::string> m_chunks; + std::vector <std::string>::size_type m_index; - const std::string chunk = m_chunks[m_index]; + public: - // Warning: 'count' should be larger than chunk length. - // This is OK for our tests. - std::copy(chunk.begin(), chunk.end(), data); + chunkInputStream() : m_index(0) { } - ++m_index; + void addChunk(const std::string& chunk) { m_chunks.push_back(chunk); } - return chunk.length(); - } + const bool eof() const { return (m_index >= m_chunks.size()); } + void reset() { m_index = 0; } - const size_type skip(const size_type /* count */) - { - // Not supported + const size_type read(value_type* const data, const size_type /* count */) + { + if (eof()) return 0; - } - }; - - const std::string readWhole(vmime::utility::inputStream& is) - { - vmime::utility::stream::value_type buffer[256]; - std::string whole; + const std::string chunk = m_chunks[m_index]; - while (!is.eof()) - { - const vmime::utility::stream::size_type read = - is.read(buffer, sizeof(buffer)); + // Warning: 'count' should be larger than chunk length. + // This is OK for our tests. + std::copy(chunk.begin(), chunk.end(), data); - whole += std::string(buffer, read); - } + ++m_index; - return (whole); + return chunk.length(); } - - // dotFilteredInputStream - - void testDotFilteredInputStreamHelper - (const std::string& number, const std::string& expected, - const std::string& c1, const std::string& c2 = "", - const std::string& c3 = "", const std::string& c4 = "") + const size_type skip(const size_type /* count */) { - chunkInputStream cis; - cis.addChunk(c1); - if (!c2.empty()) cis.addChunk(c2); - if (!c3.empty()) cis.addChunk(c3); - if (!c4.empty()) cis.addChunk(c4); + // Not supported + return 0; + } + }; - vmime::utility::dotFilteredInputStream is(cis); - std::ostringstream oss; - vmime::utility::outputStreamAdapter os(oss); + const std::string readWhole(vmime::utility::inputStream& is) + { + vmime::utility::stream::value_type buffer[256]; + std::string whole; - vmime::utility::bufferedStreamCopy(is, os); + while (!is.eof()) + { + const vmime::utility::stream::size_type read = + is.read(buffer, sizeof(buffer)); - assert_eq(number, expected, oss.str()); + whole += std::string(buffer, read); } - void testDotFilteredInputStream() - { - testDotFilteredInputStreamHelper("1", "foo\n.bar", "foo\n..bar"); - testDotFilteredInputStreamHelper("2", "foo\n.bar", "foo\n", "..bar"); - testDotFilteredInputStreamHelper("3", "foo\n.bar", "foo\n.", ".bar"); - testDotFilteredInputStreamHelper("4", "foo\n.bar", "foo\n..", "bar"); - testDotFilteredInputStreamHelper("5", "foo\n.bar", "foo\n", ".", ".bar"); - testDotFilteredInputStreamHelper("6", "foo\n.bar", "foo\n", ".", ".", "bar"); - } + return (whole); + } - // dotFilteredOutputStream - // CRLFToLFFilteredOutputStream - template <typename FILTER> - void testFilteredOutputStreamHelper - (const std::string& number, const std::string& expected, - const std::string& c1, const std::string& c2 = "", - const std::string& c3 = "", const std::string& c4 = "") - { - std::ostringstream oss; - vmime::utility::outputStreamAdapter os(oss); + // dotFilteredInputStream - FILTER fos(os); + void testDotFilteredInputStreamHelper + (const std::string& number, const std::string& expected, + const std::string& c1, const std::string& c2 = "", + const std::string& c3 = "", const std::string& c4 = "") + { + chunkInputStream cis; + cis.addChunk(c1); + if (!c2.empty()) cis.addChunk(c2); + if (!c3.empty()) cis.addChunk(c3); + if (!c4.empty()) cis.addChunk(c4); - fos.write(c1.data(), c1.length()); - if (!c2.empty()) fos.write(c2.data(), c2.length()); - if (!c3.empty()) fos.write(c3.data(), c3.length()); - if (!c4.empty()) fos.write(c4.data(), c4.length()); + vmime::utility::dotFilteredInputStream is(cis); - assert_eq(number, expected, oss.str()); - } + std::ostringstream oss; + vmime::utility::outputStreamAdapter os(oss); - void testDotFilteredOutputStream() - { - typedef vmime::utility::dotFilteredOutputStream FILTER; + vmime::utility::bufferedStreamCopy(is, os); - testFilteredOutputStreamHelper<FILTER>("1", "foo\n..bar", "foo\n.bar"); - testFilteredOutputStreamHelper<FILTER>("2", "foo\n..bar", "foo\n", ".bar"); - testFilteredOutputStreamHelper<FILTER>("3", "foo\n..bar", "foo", "\n.bar"); - testFilteredOutputStreamHelper<FILTER>("4", "foo\n..bar", "foo", "\n", ".bar"); - testFilteredOutputStreamHelper<FILTER>("5", "foo\n..bar", "foo", "\n", ".", "bar"); - } + VASSERT_EQ(number, expected, oss.str()); + } - void testCRLFToLFFilteredOutputStream() - { - typedef vmime::utility::CRLFToLFFilteredOutputStream FILTER; - - testFilteredOutputStreamHelper<FILTER>("1", "foo\nbar", "foo\r\nbar"); - testFilteredOutputStreamHelper<FILTER>("2", "foo\nbar", "foo\r\n", "bar"); - testFilteredOutputStreamHelper<FILTER>("3", "foo\nbar", "foo\r", "\nbar"); - testFilteredOutputStreamHelper<FILTER>("4", "foo\nbar", "foo", "\r\nbar"); - testFilteredOutputStreamHelper<FILTER>("5", "foo\nbar", "foo", "\r", "\nbar"); - testFilteredOutputStreamHelper<FILTER>("6", "foo\nbar", "foo", "\r", "\n", "bar"); - } + void testDotFilteredInputStream() + { + testDotFilteredInputStreamHelper("1", "foo\n.bar", "foo\n..bar"); + testDotFilteredInputStreamHelper("2", "foo\n.bar", "foo\n", "..bar"); + testDotFilteredInputStreamHelper("3", "foo\n.bar", "foo\n.", ".bar"); + testDotFilteredInputStreamHelper("4", "foo\n.bar", "foo\n..", "bar"); + testDotFilteredInputStreamHelper("5", "foo\n.bar", "foo\n", ".", ".bar"); + testDotFilteredInputStreamHelper("6", "foo\n.bar", "foo\n", ".", ".", "bar"); + } + + // dotFilteredOutputStream + // CRLFToLFFilteredOutputStream + + template <typename FILTER> + void testFilteredOutputStreamHelper + (const std::string& number, const std::string& expected, + const std::string& c1, const std::string& c2 = "", + const std::string& c3 = "", const std::string& c4 = "") + { + std::ostringstream oss; + vmime::utility::outputStreamAdapter os(oss); - // stopSequenceFilteredInputStream + FILTER fos(os); - template <int N> - void testStopSequenceFISHelper - (const std::string& number, const std::string& sequence, - const std::string& expected, const std::string& c1, - const std::string& c2 = "", const std::string& c3 = "", - const std::string& c4 = "", const std::string& c5 = "") - { - chunkInputStream cis; - cis.addChunk(c1); - if (!c2.empty()) cis.addChunk(c2); - if (!c3.empty()) cis.addChunk(c3); - if (!c4.empty()) cis.addChunk(c4); - if (!c5.empty()) cis.addChunk(c5); + fos.write(c1.data(), c1.length()); + if (!c2.empty()) fos.write(c2.data(), c2.length()); + if (!c3.empty()) fos.write(c3.data(), c3.length()); + if (!c4.empty()) fos.write(c4.data(), c4.length()); - vmime::utility::stopSequenceFilteredInputStream <N> is(cis, sequence.data()); + VASSERT_EQ(number, expected, oss.str()); + } - assert_eq(number, expected, readWhole(is)); - } + void testDotFilteredOutputStream() + { + typedef vmime::utility::dotFilteredOutputStream FILTER; - void testStopSequenceFilteredInputStream1() - { - testStopSequenceFISHelper <1>("1", "x", "foo", "fooxbar"); - testStopSequenceFISHelper <1>("2", "x", "foo", "foox", "bar"); - testStopSequenceFISHelper <1>("3", "x", "foo", "foo", "x", "bar"); - testStopSequenceFISHelper <1>("4", "x", "foo", "fo", "o", "x", "bar"); - testStopSequenceFISHelper <1>("5", "x", "foo", "fo", "o", "x", "b", "ar"); - - testStopSequenceFISHelper <1>("6", "x", "foobar", "fo", "o", "b", "ar"); - testStopSequenceFISHelper <1>("7", "x", "foobar", "foo", "bar"); - testStopSequenceFISHelper <1>("8", "x", "foobar", "foo", "b", "ar"); - - testStopSequenceFISHelper <1>("9", "x", "foobar", "foobar"); - testStopSequenceFISHelper <1>("10", "x", "foobar", "foobarx"); - - testStopSequenceFISHelper <1>("11", "x", "", ""); - testStopSequenceFISHelper <1>("12", "x", "", "x"); - testStopSequenceFISHelper <1>("13", "x", "", "", "x"); - } + testFilteredOutputStreamHelper<FILTER>("1", "foo\n..bar", "foo\n.bar"); + testFilteredOutputStreamHelper<FILTER>("2", "foo\n..bar", "foo\n", ".bar"); + testFilteredOutputStreamHelper<FILTER>("3", "foo\n..bar", "foo", "\n.bar"); + testFilteredOutputStreamHelper<FILTER>("4", "foo\n..bar", "foo", "\n", ".bar"); + testFilteredOutputStreamHelper<FILTER>("5", "foo\n..bar", "foo", "\n", ".", "bar"); + } - void testStopSequenceFilteredInputStreamN_2() - { - testStopSequenceFISHelper <2>("1", "xy", "foo", "fooxybar"); - testStopSequenceFISHelper <2>("2", "xy", "foo", "foox", "ybar"); - testStopSequenceFISHelper <2>("3", "xy", "foo", "foox", "y", "bar"); - testStopSequenceFISHelper <2>("4", "xy", "foo", "foo", "x", "ybar"); - testStopSequenceFISHelper <2>("5", "xy", "foo", "foo", "xy", "bar"); - testStopSequenceFISHelper <2>("6", "xy", "foo", "foo", "x", "y", "bar"); - - testStopSequenceFISHelper <2>("7", "xy", "fooxbar", "foox", "bar"); - testStopSequenceFISHelper <2>("8", "xy", "fooxbar", "foo", "xbar"); - testStopSequenceFISHelper <2>("9", "xy", "fooxbar", "foo", "x", "bar"); - testStopSequenceFISHelper <2>("10", "xy", "foobarx", "foo", "barx"); - - testStopSequenceFISHelper <2>("11", "xy", "foobar", "foobarxy"); - testStopSequenceFISHelper <2>("12", "xy", "foobar", "foo", "barxy"); - testStopSequenceFISHelper <2>("13", "xy", "foobar", "foo", "bar", "xy"); - - testStopSequenceFISHelper <2>("14", "xy", "", ""); - testStopSequenceFISHelper <2>("15", "xy", "x", "x"); - testStopSequenceFISHelper <2>("16", "xy", "", "xy"); - testStopSequenceFISHelper <2>("17", "xy", "", "x", "y"); - } + void testCRLFToLFFilteredOutputStream() + { + typedef vmime::utility::CRLFToLFFilteredOutputStream FILTER; + + testFilteredOutputStreamHelper<FILTER>("1", "foo\nbar", "foo\r\nbar"); + testFilteredOutputStreamHelper<FILTER>("2", "foo\nbar", "foo\r\n", "bar"); + testFilteredOutputStreamHelper<FILTER>("3", "foo\nbar", "foo\r", "\nbar"); + testFilteredOutputStreamHelper<FILTER>("4", "foo\nbar", "foo", "\r\nbar"); + testFilteredOutputStreamHelper<FILTER>("5", "foo\nbar", "foo", "\r", "\nbar"); + testFilteredOutputStreamHelper<FILTER>("6", "foo\nbar", "foo", "\r", "\n", "bar"); + } + + // stopSequenceFilteredInputStream + + template <int N> + void testStopSequenceFISHelper + (const std::string& number, const std::string& sequence, + const std::string& expected, const std::string& c1, + const std::string& c2 = "", const std::string& c3 = "", + const std::string& c4 = "", const std::string& c5 = "") + { + chunkInputStream cis; + cis.addChunk(c1); + if (!c2.empty()) cis.addChunk(c2); + if (!c3.empty()) cis.addChunk(c3); + if (!c4.empty()) cis.addChunk(c4); + if (!c5.empty()) cis.addChunk(c5); - void testStopSequenceFilteredInputStreamN_3() - { - testStopSequenceFISHelper <3>("1", "xyz", "foo", "fooxyzbar"); - testStopSequenceFISHelper <3>("2", "xyz", "foo", "foox", "yzbar"); - testStopSequenceFISHelper <3>("3", "xyz", "foo", "foox", "y", "zbar"); - testStopSequenceFISHelper <3>("4", "xyz", "foo", "foox", "yz", "bar"); - testStopSequenceFISHelper <3>("5", "xyz", "foo", "foo", "xyz", "bar"); - testStopSequenceFISHelper <3>("6", "xyz", "foo", "foo", "xy", "zbar"); - testStopSequenceFISHelper <3>("7", "xyz", "foo", "foo", "x", "y", "zbar"); - testStopSequenceFISHelper <3>("8", "xyz", "foo", "foo", "x", "y", "z", "bar"); - testStopSequenceFISHelper <3>("9", "xyz", "foo", "fooxy", "z", "bar"); - - testStopSequenceFISHelper <3>("10", "xyz", "fooxybar", "foox", "y", "bar"); - testStopSequenceFISHelper <3>("11", "xyz", "fooxybar", "fooxy", "bar"); - testStopSequenceFISHelper <3>("12", "xyz", "fooxybar", "fo", "ox", "y", "bar"); - testStopSequenceFISHelper <3>("13", "xyz", "fooxybar", "fo", "o", "x", "y", "bar"); - testStopSequenceFISHelper <3>("14", "xyz", "fooxybar", "foo", "x", "ybar"); - testStopSequenceFISHelper <3>("15", "xyz", "fooxybar", "foo", "xybar"); - - testStopSequenceFISHelper <3>("16", "xyz", "xfoxoxybxar", "xfoxo", "xybxar"); - testStopSequenceFISHelper <3>("17", "xyz", "xfoxoxybxarx", "xfoxo", "xybxarx"); - testStopSequenceFISHelper <3>("18", "xyz", "xfoxoxybxarxy", "xfoxo", "xybxarxy"); - - testStopSequenceFISHelper <3>("19", "xyz", "", ""); - testStopSequenceFISHelper <3>("20", "xyz", "x", "x"); - testStopSequenceFISHelper <3>("21", "xyz", "xy", "xy"); - testStopSequenceFISHelper <3>("22", "xyz", "", "xyz"); - testStopSequenceFISHelper <3>("23", "xyz", "", "x", "yz"); - testStopSequenceFISHelper <3>("24", "xyz", "", "x", "y", "z"); - } + vmime::utility::stopSequenceFilteredInputStream <N> is(cis, sequence.data()); - public: + VASSERT_EQ(number, expected, readWhole(is)); + } - filteredStreamTest() : suite("vmime::utility::filteredStream") - { - // VMime initialization - vmime::platformDependant::setHandler<vmime::platforms::posix::posixHandler>(); + void testStopSequenceFilteredInputStream1() + { + testStopSequenceFISHelper <1>("1", "x", "foo", "fooxbar"); + testStopSequenceFISHelper <1>("2", "x", "foo", "foox", "bar"); + testStopSequenceFISHelper <1>("3", "x", "foo", "foo", "x", "bar"); + testStopSequenceFISHelper <1>("4", "x", "foo", "fo", "o", "x", "bar"); + testStopSequenceFISHelper <1>("5", "x", "foo", "fo", "o", "x", "b", "ar"); - add("dotFilteredInputStream", testcase(this, "dotFilteredInputStream", &filteredStreamTest::testDotFilteredInputStream)); - add("dotFilteredOutputStream", testcase(this, "dotFilteredOutputStream", &filteredStreamTest::testDotFilteredOutputStream)); - add("CRLFToLFFilteredOutputStream", testcase(this, "CRLFToLFFilteredOutputStream", &filteredStreamTest::testCRLFToLFFilteredOutputStream)); - add("stopSequenceFilteredInputStream1", testcase(this, "stopSequenceFilteredInputStream1", &filteredStreamTest::testStopSequenceFilteredInputStream1)); - add("stopSequenceFilteredInputStreamN_2", testcase(this, "stopSequenceFilteredInputStreamN_2", &filteredStreamTest::testStopSequenceFilteredInputStreamN_2)); - add("stopSequenceFilteredInputStreamN_3", testcase(this, "stopSequenceFilteredInputStreamN_3", &filteredStreamTest::testStopSequenceFilteredInputStreamN_3)); + testStopSequenceFISHelper <1>("6", "x", "foobar", "fo", "o", "b", "ar"); + testStopSequenceFISHelper <1>("7", "x", "foobar", "foo", "bar"); + testStopSequenceFISHelper <1>("8", "x", "foobar", "foo", "b", "ar"); - suite::main().add("vmime::utility::filteredStream", this); - } + testStopSequenceFISHelper <1>("9", "x", "foobar", "foobar"); + testStopSequenceFISHelper <1>("10", "x", "foobar", "foobarx"); - }; + testStopSequenceFISHelper <1>("11", "x", "", ""); + testStopSequenceFISHelper <1>("12", "x", "", "x"); + testStopSequenceFISHelper <1>("13", "x", "", "", "x"); + } - filteredStreamTest* theTest = new filteredStreamTest(); -} + void testStopSequenceFilteredInputStreamN_2() + { + testStopSequenceFISHelper <2>("1", "xy", "foo", "fooxybar"); + testStopSequenceFISHelper <2>("2", "xy", "foo", "foox", "ybar"); + testStopSequenceFISHelper <2>("3", "xy", "foo", "foox", "y", "bar"); + testStopSequenceFISHelper <2>("4", "xy", "foo", "foo", "x", "ybar"); + testStopSequenceFISHelper <2>("5", "xy", "foo", "foo", "xy", "bar"); + testStopSequenceFISHelper <2>("6", "xy", "foo", "foo", "x", "y", "bar"); + + testStopSequenceFISHelper <2>("7", "xy", "fooxbar", "foox", "bar"); + testStopSequenceFISHelper <2>("8", "xy", "fooxbar", "foo", "xbar"); + testStopSequenceFISHelper <2>("9", "xy", "fooxbar", "foo", "x", "bar"); + testStopSequenceFISHelper <2>("10", "xy", "foobarx", "foo", "barx"); + + testStopSequenceFISHelper <2>("11", "xy", "foobar", "foobarxy"); + testStopSequenceFISHelper <2>("12", "xy", "foobar", "foo", "barxy"); + testStopSequenceFISHelper <2>("13", "xy", "foobar", "foo", "bar", "xy"); + + testStopSequenceFISHelper <2>("14", "xy", "", ""); + testStopSequenceFISHelper <2>("15", "xy", "x", "x"); + testStopSequenceFISHelper <2>("16", "xy", "", "xy"); + testStopSequenceFISHelper <2>("17", "xy", "", "x", "y"); + } + + void testStopSequenceFilteredInputStreamN_3() + { + testStopSequenceFISHelper <3>("1", "xyz", "foo", "fooxyzbar"); + testStopSequenceFISHelper <3>("2", "xyz", "foo", "foox", "yzbar"); + testStopSequenceFISHelper <3>("3", "xyz", "foo", "foox", "y", "zbar"); + testStopSequenceFISHelper <3>("4", "xyz", "foo", "foox", "yz", "bar"); + testStopSequenceFISHelper <3>("5", "xyz", "foo", "foo", "xyz", "bar"); + testStopSequenceFISHelper <3>("6", "xyz", "foo", "foo", "xy", "zbar"); + testStopSequenceFISHelper <3>("7", "xyz", "foo", "foo", "x", "y", "zbar"); + testStopSequenceFISHelper <3>("8", "xyz", "foo", "foo", "x", "y", "z", "bar"); + testStopSequenceFISHelper <3>("9", "xyz", "foo", "fooxy", "z", "bar"); + + testStopSequenceFISHelper <3>("10", "xyz", "fooxybar", "foox", "y", "bar"); + testStopSequenceFISHelper <3>("11", "xyz", "fooxybar", "fooxy", "bar"); + testStopSequenceFISHelper <3>("12", "xyz", "fooxybar", "fo", "ox", "y", "bar"); + testStopSequenceFISHelper <3>("13", "xyz", "fooxybar", "fo", "o", "x", "y", "bar"); + testStopSequenceFISHelper <3>("14", "xyz", "fooxybar", "foo", "x", "ybar"); + testStopSequenceFISHelper <3>("15", "xyz", "fooxybar", "foo", "xybar"); + + testStopSequenceFISHelper <3>("16", "xyz", "xfoxoxybxar", "xfoxo", "xybxar"); + testStopSequenceFISHelper <3>("17", "xyz", "xfoxoxybxarx", "xfoxo", "xybxarx"); + testStopSequenceFISHelper <3>("18", "xyz", "xfoxoxybxarxy", "xfoxo", "xybxarxy"); + + testStopSequenceFISHelper <3>("19", "xyz", "", ""); + testStopSequenceFISHelper <3>("20", "xyz", "x", "x"); + testStopSequenceFISHelper <3>("21", "xyz", "xy", "xy"); + testStopSequenceFISHelper <3>("22", "xyz", "", "xyz"); + testStopSequenceFISHelper <3>("23", "xyz", "", "x", "yz"); + testStopSequenceFISHelper <3>("24", "xyz", "", "x", "y", "z"); + } + +VMIME_TEST_SUITE_END |