1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
//
// VMime library (http://www.vmime.org)
// Copyright (C) 2002-2013 Vincent Richard <vincent@vmime.org>
//
// 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"
#include "vmime/utility/stringUtils.hpp"
using namespace vmime::utility;
VMIME_TEST_SUITE_BEGIN(seekableInputStreamRegionAdapterTest)
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::shared_ptr <seekableInputStreamRegionAdapter> createStream
(vmime::shared_ptr <seekableInputStream>* underlyingStream = NULL)
{
vmime::string buffer("THIS IS A TEST BUFFER");
vmime::shared_ptr <seekableInputStream> strStream =
vmime::make_shared <inputStreamStringAdapter>(buffer);
vmime::shared_ptr <seekableInputStreamRegionAdapter> rgnStream =
vmime::make_shared <seekableInputStreamRegionAdapter>(strStream, 10, 11);
if (underlyingStream)
*underlyingStream = strStream;
return rgnStream;
}
void testInitialPosition()
{
vmime::shared_ptr <seekableInputStreamRegionAdapter> stream = createStream();
VASSERT_EQ("Pos", 0, stream->getPosition());
VASSERT_FALSE("EOF", stream->eof());
}
void testSeekAndGetPosition()
{
vmime::shared_ptr <seekableInputStreamRegionAdapter> 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::shared_ptr <seekableInputStreamRegionAdapter> stream = createStream();
stream->seek(5);
vmime::byte_t buffer[100];
std::fill(vmime::begin(buffer), vmime::end(buffer), 0);
vmime::size_t 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::utility::stringUtils::makeStringFromBytes(buffer, 6));
}
void testSkip()
{
vmime::shared_ptr <seekableInputStreamRegionAdapter> stream = createStream();
stream->skip(5);
VASSERT_EQ("Pos 1", 5, stream->getPosition());
VASSERT_FALSE("EOF 1", stream->eof());
vmime::byte_t buffer[100];
std::fill(vmime::begin(buffer), vmime::end(buffer), 0);
vmime::size_t 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::utility::stringUtils::makeStringFromBytes(buffer, 3));
stream->skip(50);
VASSERT_EQ("Pos 3", 11, stream->getPosition());
VASSERT_TRUE("EOF 3", stream->eof());
}
void testReset()
{
vmime::shared_ptr <seekableInputStreamRegionAdapter> 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::shared_ptr <seekableInputStream> ustream;
vmime::shared_ptr <seekableInputStreamRegionAdapter> stream = createStream(&ustream);
stream->seek(5);
vmime::byte_t buffer1[100];
std::fill(vmime::begin(buffer1), vmime::end(buffer1), 0);
VASSERT_EQ("Read 1", 7, ustream->read(buffer1, 7));
vmime::byte_t buffer2[100];
std::fill(vmime::begin(buffer2), vmime::end(buffer2), 0);
VASSERT_EQ("Read 2", 6, stream->read(buffer2, 6));
VASSERT_EQ("Buffer 1", "THIS IS",
vmime::utility::stringUtils::makeStringFromBytes(buffer1, 7));
VASSERT_EQ("Buffer 2", "BUFFER",
vmime::utility::stringUtils::makeStringFromBytes(buffer2, 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
|