comparison 3rdparty/vmime/tests/utility/seekableInputStreamRegionAdapterTest.cpp @ 0:a4671277546c tip

created the repository for the thymian project
author ferencd
date Tue, 17 Aug 2021 11:19:54 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:a4671277546c
1 //
2 // VMime library (http://www.vmime.org)
3 // Copyright (C) 2002-2013 Vincent Richard <vincent@vmime.org>
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 3 of
8 // the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 //
19 // Linking this library statically or dynamically with other modules is making
20 // a combined work based on this library. Thus, the terms and conditions of
21 // the GNU General Public License cover the whole combination.
22 //
23
24 #include "tests/testUtils.hpp"
25
26 #include "vmime/utility/inputStreamStringAdapter.hpp"
27 #include "vmime/utility/seekableInputStreamRegionAdapter.hpp"
28 #include "vmime/utility/stringUtils.hpp"
29
30
31 using namespace vmime::utility;
32
33
34 VMIME_TEST_SUITE_BEGIN(seekableInputStreamRegionAdapterTest)
35
36 VMIME_TEST_LIST_BEGIN
37 VMIME_TEST(testInitialPosition)
38 VMIME_TEST(testSeekAndGetPosition)
39 VMIME_TEST(testRead)
40 VMIME_TEST(testSkip)
41 VMIME_TEST(testReset)
42 VMIME_TEST(testOwnPosition)
43 VMIME_TEST_LIST_END
44
45
46 vmime::shared_ptr <seekableInputStreamRegionAdapter> createStream
47 (vmime::shared_ptr <seekableInputStream>* underlyingStream = NULL)
48 {
49 vmime::string buffer("THIS IS A TEST BUFFER");
50
51 vmime::shared_ptr <seekableInputStream> strStream =
52 vmime::make_shared <inputStreamStringAdapter>(buffer);
53
54 vmime::shared_ptr <seekableInputStreamRegionAdapter> rgnStream =
55 vmime::make_shared <seekableInputStreamRegionAdapter>(strStream, 10, 11);
56
57 if (underlyingStream)
58 *underlyingStream = strStream;
59
60 return rgnStream;
61 }
62
63 void testInitialPosition()
64 {
65 vmime::shared_ptr <seekableInputStreamRegionAdapter> stream = createStream();
66
67 VASSERT_EQ("Pos", 0, stream->getPosition());
68 VASSERT_FALSE("EOF", stream->eof());
69 }
70
71 void testSeekAndGetPosition()
72 {
73 vmime::shared_ptr <seekableInputStreamRegionAdapter> stream = createStream();
74
75 stream->seek(5);
76
77 VASSERT_EQ("Pos 1", 5, stream->getPosition());
78 VASSERT_FALSE("EOF 1", stream->eof());
79
80 stream->seek(20);
81
82 VASSERT_EQ("Pos 2", 11, stream->getPosition());
83 VASSERT_TRUE("EOF 2", stream->eof());
84 }
85
86 void testRead()
87 {
88 vmime::shared_ptr <seekableInputStreamRegionAdapter> stream = createStream();
89
90 stream->seek(5);
91
92 vmime::byte_t buffer[100];
93 std::fill(vmime::begin(buffer), vmime::end(buffer), 0);
94 vmime::size_t read = stream->read(buffer, 6);
95
96 VASSERT_EQ("Pos", 11, stream->getPosition());
97 VASSERT_EQ("Read", 6, read);
98 VASSERT_TRUE("EOF", stream->eof());
99 VASSERT_EQ("Buffer", "BUFFER",
100 vmime::utility::stringUtils::makeStringFromBytes(buffer, 6));
101 }
102
103 void testSkip()
104 {
105 vmime::shared_ptr <seekableInputStreamRegionAdapter> stream = createStream();
106
107 stream->skip(5);
108
109 VASSERT_EQ("Pos 1", 5, stream->getPosition());
110 VASSERT_FALSE("EOF 1", stream->eof());
111
112 vmime::byte_t buffer[100];
113 std::fill(vmime::begin(buffer), vmime::end(buffer), 0);
114 vmime::size_t read = stream->read(buffer, 3);
115
116 VASSERT_EQ("Pos 2", 8, stream->getPosition());
117 VASSERT_EQ("Read", 3, read);
118 VASSERT_FALSE("EOF 2", stream->eof());
119 VASSERT_EQ("Buffer", "BUF",
120 vmime::utility::stringUtils::makeStringFromBytes(buffer, 3));
121
122 stream->skip(50);
123
124 VASSERT_EQ("Pos 3", 11, stream->getPosition());
125 VASSERT_TRUE("EOF 3", stream->eof());
126 }
127
128 void testReset()
129 {
130 vmime::shared_ptr <seekableInputStreamRegionAdapter> stream = createStream();
131
132 stream->skip(100);
133 stream->reset();
134
135 VASSERT_EQ("Pos", 0, stream->getPosition());
136 VASSERT_FALSE("EOF", stream->eof());
137 }
138
139 void testOwnPosition()
140 {
141 // seekableInputStreamRegionAdapter should keep track of its own position
142 // in the underlying stream, and not be affected by possible seek/read
143 // operations on it...
144 vmime::shared_ptr <seekableInputStream> ustream;
145 vmime::shared_ptr <seekableInputStreamRegionAdapter> stream = createStream(&ustream);
146
147 stream->seek(5);
148
149 vmime::byte_t buffer1[100];
150 std::fill(vmime::begin(buffer1), vmime::end(buffer1), 0);
151 vmime::size_t read = ustream->read(buffer1, 7);
152
153 vmime::byte_t buffer2[100];
154 std::fill(vmime::begin(buffer2), vmime::end(buffer2), 0);
155 vmime::size_t read2 = stream->read(buffer2, 6);
156
157 VASSERT_EQ("Buffer 1", "THIS IS",
158 vmime::utility::stringUtils::makeStringFromBytes(buffer1, 7));
159 VASSERT_EQ("Buffer 2", "BUFFER",
160 vmime::utility::stringUtils::makeStringFromBytes(buffer2, 6));
161
162 // ...but the underlying stream position is affected by read operations
163 // from the region adapter (FIXME?)
164 VASSERT_EQ("Pos", 21, ustream->getPosition());
165 }
166
167 VMIME_TEST_SUITE_END