diff --git a/SConstruct b/SConstruct index bd751271..bc277263 100644 --- a/SConstruct +++ b/SConstruct @@ -373,6 +373,7 @@ libvmimetest_sources = [ 'tests/utility/outputStreamStringAdapterTest.cpp', 'tests/utility/outputStreamSocketAdapterTest.cpp', 'tests/utility/outputStreamByteArrayAdapterTest.cpp', + 'tests/utility/seekableInputStreamRegionAdapterTest.cpp', # =============================== Misc =============================== 'tests/misc/importanceHelperTest.cpp', # ============================= Security ============================= diff --git a/tests/utility/seekableInputStreamRegionAdapterTest.cpp b/tests/utility/seekableInputStreamRegionAdapterTest.cpp new file mode 100644 index 00000000..5cffbb68 --- /dev/null +++ b/tests/utility/seekableInputStreamRegionAdapterTest.cpp @@ -0,0 +1,162 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2013 Vincent Richard +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 3 of +// the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Linking this library statically or dynamically with other modules is making +// a combined work based on this library. Thus, the terms and conditions of +// the GNU General Public License cover the whole combination. +// + +#include "tests/testUtils.hpp" + +#include "vmime/utility/inputStreamStringAdapter.hpp" +#include "vmime/utility/seekableInputStreamRegionAdapter.hpp" + + +#define VMIME_TEST_SUITE seekableInputStreamRegionAdapterTest +#define VMIME_TEST_SUITE_MODULE "Utility" + + +using namespace vmime::utility; + + +VMIME_TEST_SUITE_BEGIN + + VMIME_TEST_LIST_BEGIN + VMIME_TEST(testInitialPosition) + VMIME_TEST(testSeekAndGetPosition) + VMIME_TEST(testRead) + VMIME_TEST(testSkip) + VMIME_TEST(testReset) + VMIME_TEST(testOwnPosition) + VMIME_TEST_LIST_END + + + vmime::ref createStream + (vmime::ref * underlyingStream = NULL) + { + vmime::string buffer("THIS IS A TEST BUFFER"); + + vmime::ref strStream = + vmime::create (buffer); + + vmime::ref rgnStream = + vmime::create (strStream, 10, 11); + + if (underlyingStream) + *underlyingStream = strStream; + + return rgnStream; + } + + void testInitialPosition() + { + vmime::ref stream = createStream(); + + VASSERT_EQ("Pos", 0, stream->getPosition()); + VASSERT_FALSE("EOF", stream->eof()); + } + + void testSeekAndGetPosition() + { + vmime::ref stream = createStream(); + + stream->seek(5); + + VASSERT_EQ("Pos 1", 5, stream->getPosition()); + VASSERT_FALSE("EOF 1", stream->eof()); + + stream->seek(20); + + VASSERT_EQ("Pos 2", 11, stream->getPosition()); + VASSERT_TRUE("EOF 2", stream->eof()); + } + + void testRead() + { + vmime::ref stream = createStream(); + + stream->seek(5); + + stream::value_type buffer[100]; + stream::size_type read = stream->read(buffer, 6); + + VASSERT_EQ("Pos", 11, stream->getPosition()); + VASSERT_EQ("Read", 6, read); + VASSERT_TRUE("EOF", stream->eof()); + VASSERT_EQ("Buffer", "BUFFER", vmime::string(buffer, 0, 6)); + } + + void testSkip() + { + vmime::ref stream = createStream(); + + stream->skip(5); + + VASSERT_EQ("Pos 1", 5, stream->getPosition()); + VASSERT_FALSE("EOF 1", stream->eof()); + + stream::value_type buffer[100]; + stream::size_type read = stream->read(buffer, 3); + + VASSERT_EQ("Pos 2", 8, stream->getPosition()); + VASSERT_EQ("Read", 3, read); + VASSERT_FALSE("EOF 2", stream->eof()); + VASSERT_EQ("Buffer", "BUF", vmime::string(buffer, 0, 3)); + + stream->skip(50); + + VASSERT_EQ("Pos 3", 11, stream->getPosition()); + VASSERT_TRUE("EOF 3", stream->eof()); + } + + void testReset() + { + vmime::ref stream = createStream(); + + stream->skip(100); + stream->reset(); + + VASSERT_EQ("Pos", 0, stream->getPosition()); + VASSERT_FALSE("EOF", stream->eof()); + } + + void testOwnPosition() + { + // seekableInputStreamRegionAdapter should keep track of its own position + // in the underlying stream, and not be affected by possible seek/read + // operations on it... + vmime::ref ustream; + vmime::ref stream = createStream(&ustream); + + stream->seek(5); + + stream::value_type buffer1[100]; + stream::size_type read = ustream->read(buffer1, 7); + + stream::value_type buffer2[100]; + stream::size_type read2 = stream->read(buffer2, 6); + + VASSERT_EQ("Buffer 1", "THIS IS", vmime::string(buffer1, 0, 7)); + VASSERT_EQ("Buffer 2", "BUFFER", vmime::string(buffer2, 0, 6)); + + // ...but the underlying stream position is affected by read operations + // from the region adapter (FIXME?) + VASSERT_EQ("Pos", 21, ustream->getPosition()); + } + +VMIME_TEST_SUITE_END