Debian Codegraph @

src/test/RVNGBinaryDataTest.cpp

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* librevenge
3 * Version: MPL 2.0 / LGPLv2.1+
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * Alternatively, the contents of this file may be used under the terms
10 * of the GNU Lesser General Public License Version 2.1 or later
11 * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are
12 * applicable instead of those above.
13 */
14
15 #include <algorithm>
16 #include <cstring>
17 #include <string>
18
19 #include <cppunit/TestFixture.h>
20 #include <cppunit/extensions/HelperMacros.h>
21
22 #include <librevenge/librevenge.h>
23 #include <librevenge-stream/librevenge-stream.h>
24
25 #include "librevenge_internal.h"
26
27 using namespace librevenge;
28
29 using std::equal;
30 using std::strlen;
31
32 namespace test
33 {
34
35 namespace
36 {
37
38 bool equalBinaryData(const RVNGBinaryData &binaryData, const char *const str)
39 {
40 const size_t len = strlen(str);
41 if (binaryData.size() != len)
42 return false;
43
44 if (0 == len) // there is nothing to compare here
45 return true;
46
47 return equal(str, str + len, reinterpret_cast<const char *>(binaryData.getDataBuffer()));
48 }
49
50 void implTestBase64(const char *const plain, const char *const base64)
51 {
52 const std::string msg(std::string("plain text: '") + plain + '\'');
53 // construct (decode)
54 {
55 const RVNGBinaryData data(base64);
56 CPPUNIT_ASSERT_MESSAGE(msg, equalBinaryData(data, plain));
57 }
58 {
59 const RVNGString base64Str(base64);
60 const RVNGBinaryData data(base64Str);
61 CPPUNIT_ASSERT_MESSAGE(msg, equalBinaryData(data, plain));
62 }
63
64 // append (decode)
65 {
66 RVNGBinaryData data;
67 data.appendBase64Data(base64);
68 CPPUNIT_ASSERT_MESSAGE(msg, equalBinaryData(data, plain));
69 }
70 {
71 RVNGBinaryData data;
72 const RVNGString base64Str(base64);
73 data.appendBase64Data(base64Str);
74 CPPUNIT_ASSERT_MESSAGE(msg, equalBinaryData(data, plain));
75 }
76
77 // encode
78 {
79 const RVNGBinaryData data(reinterpret_cast<const unsigned char *>(plain), strlen(plain));
80 CPPUNIT_ASSERT_EQUAL_MESSAGE(msg, 0, std::strcmp(base64, data.getBase64Data().cstr()));
81 }
82 }
83
84 }
85
86 class RVNGBinaryDataTest : public CPPUNIT_NS::TestFixture
87 {
88 public:
89 virtual void setUp();
90 virtual void tearDown();
91
92 private:
93 CPPUNIT_TEST_SUITE(RVNGBinaryDataTest);
94 CPPUNIT_TEST(testConstruction);
95 CPPUNIT_TEST(testAppend);
96 CPPUNIT_TEST(testClear);
97 CPPUNIT_TEST(testBase64);
98 CPPUNIT_TEST(testStream);
99 CPPUNIT_TEST_SUITE_END();
100
101 private:
102 void testConstruction();
103 void testAppend();
104 void testClear();
105 void testBase64();
106 void testStream();
107 };
108
109 void RVNGBinaryDataTest::setUp()
110 {
111 }
112
113 void RVNGBinaryDataTest::tearDown()
114 {
115 }
116
117 void RVNGBinaryDataTest::testConstruction()
118 {
119 // default ctor creates empty data
120 RVNGBinaryData empty;
121 CPPUNIT_ASSERT_EQUAL(0ul, empty.size());
122 CPPUNIT_ASSERT(empty.empty());
123
124 // construction from a buffer
125 const unsigned char input[] = "hello world";
126 const size_t len = RVNG_NUM_ELEMENTS(input);
127 RVNGBinaryData data(input, len);
128 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned long>(len), data.size());
129 CPPUNIT_ASSERT(!data.empty());
130 CPPUNIT_ASSERT(equal(input, input + len, data.getDataBuffer()));
131
132 {
133 // copy construction
134 RVNGBinaryData copy(data);
135 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned long>(len), copy.size());
136 CPPUNIT_ASSERT(equal(input, input + len, copy.getDataBuffer()));
137 }
138 // COW: destruction of a copy does not touch the original
139 // this should be tested under valgrind
140 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned long>(len), data.size());
141 CPPUNIT_ASSERT(equal(input, input + len, data.getDataBuffer()));
142
143 // assignment
144 RVNGBinaryData assign;
145 CPPUNIT_ASSERT_EQUAL(0ul, assign.size());
146 CPPUNIT_ASSERT(assign.empty());
147 assign = data;
148 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned long>(len), assign.size());
149 CPPUNIT_ASSERT(!assign.empty());
150 CPPUNIT_ASSERT(equal(input, input + len, assign.getDataBuffer()));
151
152 }
153
154 void RVNGBinaryDataTest::testAppend()
155 {
156 const unsigned char input[] = "hello world";
157 const size_t len = RVNG_NUM_ELEMENTS(input);
158 RVNGBinaryData data;
159
160 // appending a character
161 data.append('h');
162 CPPUNIT_ASSERT_EQUAL(1ul, data.size());
163 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned char>('h'), data.getDataBuffer()[0]);
164
165 RVNGBinaryData data2;
166 // appending other RVNGBinaryData
167 data2.append(data);
168 CPPUNIT_ASSERT_EQUAL(1ul, data2.size());
169 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned char>('h'), data2.getDataBuffer()[0]);
170 // the original is unchanged
171 CPPUNIT_ASSERT_EQUAL(1ul, data.size());
172 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned char>('h'), data.getDataBuffer()[0]);
173
174 data2.append(input + 1, len - 1);
175 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned long>(len), data2.size());
176 CPPUNIT_ASSERT(equal(input, input + len, data2.getDataBuffer()));
177 // again, the original is unchanged
178 CPPUNIT_ASSERT_EQUAL(1ul, data.size());
179 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned char>('h'), data.getDataBuffer()[0]);
180 }
181
182 void RVNGBinaryDataTest::testClear()
183 {
184 RVNGBinaryData data;
185 // clearing empty data does nothing
186 data.clear();
187 CPPUNIT_ASSERT_EQUAL(0ul, data.size());
188
189 data.append('a');
190 RVNGBinaryData copy(data);
191 // clearing empties the content
192 data.clear();
193 CPPUNIT_ASSERT_EQUAL(0ul, data.size());
194 // but the copy remains unchanged
195 CPPUNIT_ASSERT_EQUAL(1ul, copy.size());
196 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned char>('a'), copy.getDataBuffer()[0]);
197 }
198
199 void RVNGBinaryDataTest::testBase64()
200 {
201 // NOTE: these patterns come from http://www.ietf.org/rfc/rfc4648.txt
202 implTestBase64("", "");
203 implTestBase64("f", "Zg==");
204 implTestBase64("fo", "Zm8=");
205 implTestBase64("foo", "Zm9v");
206 implTestBase64("foob", "Zm9vYg==");
207 implTestBase64("fooba", "Zm9vYmE=");
208 implTestBase64("foobar", "Zm9vYmFy");
209
210 // more test patterns
211 implTestBase64("e=mc^2", "ZT1tY14y");
212 }
213
214 void RVNGBinaryDataTest::testStream()
215 {
216 RVNGBinaryData data((const unsigned char *)("\1\2\3\4\0\5\6\7"), 8);
217 RVNGInputStream *input = const_cast<RVNGInputStream *>(data.getDataStream());
218 unsigned long numBytesRead = 0;
219
220 CPPUNIT_ASSERT_EQUAL(false, input->isStructured());
221 CPPUNIT_ASSERT_EQUAL((RVNGInputStream *) NULL, input->getSubStreamByName("foo"));
222
223 // test read()
224 input->seek(0, RVNG_SEEK_SET);
225 CPPUNIT_ASSERT(NULL == input->read(0, numBytesRead));
226 CPPUNIT_ASSERT_EQUAL((unsigned long) 0, numBytesRead);
227 CPPUNIT_ASSERT_EQUAL((long) 0, input->tell());
228 CPPUNIT_ASSERT(NULL != input->read(1, numBytesRead));
229 CPPUNIT_ASSERT_EQUAL((long) 1, input->tell());
230
231 input->seek(0, RVNG_SEEK_SET);
232 CPPUNIT_ASSERT(NULL != input->read(50, numBytesRead));
233 CPPUNIT_ASSERT_EQUAL((long) 8, input->tell());
234
235 // test seek(), tell(), isEnd()
236 input->seek(1, RVNG_SEEK_SET);
237 CPPUNIT_ASSERT_EQUAL((long) 1, input->tell());
238
239 input->seek(0, RVNG_SEEK_SET);
240 CPPUNIT_ASSERT_EQUAL((long) 0, input->tell());
241
242 input->seek(8, RVNG_SEEK_SET);
243 CPPUNIT_ASSERT_EQUAL((long) 8, input->tell());
244
245 input->seek(-1, RVNG_SEEK_SET);
246 CPPUNIT_ASSERT_EQUAL((long) 0, input->tell());
247
248 input->seek(8, RVNG_SEEK_SET);
249 CPPUNIT_ASSERT_EQUAL(true, input->isEnd());
250
251 input->seek(10000, RVNG_SEEK_SET);
252 CPPUNIT_ASSERT(10000 != input->tell());
253 CPPUNIT_ASSERT(input->isEnd());
254 }
255
256 CPPUNIT_TEST_SUITE_REGISTRATION(RVNGBinaryDataTest);
257
258 }
259
260 /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */