Debian Codegraph @

src/lib/data_mgr/test/ByteStringTests.cpp

raw
1 /*
2 * Copyright (c) 2010 SURFnet bv
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 /*****************************************************************************
28 ByteStringTests.cpp
29
30 Contains test cases to test the ByteString class
31 *****************************************************************************/
32
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <cppunit/extensions/HelperMacros.h>
36 #include <string>
37 #include "ByteStringTests.h"
38 #include "ByteString.h"
39
40 CPPUNIT_TEST_SUITE_REGISTRATION(ByteStringTests);
41
42 void ByteStringTests::setUp()
43 {
44 }
45
46 void ByteStringTests::tearDown()
47 {
48 fflush(stdout);
49 }
50
51 void ByteStringTests::testIntegrity()
52 {
53 unsigned char testData[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
54 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10 };
55
56 ByteString b(testData, sizeof(testData));
57
58 // Test if the right data is returned
59 CPPUNIT_ASSERT(memcmp(testData, b.byte_str(), sizeof(testData)) == 0);
60
61 // Test size
62 CPPUNIT_ASSERT(b.size() == sizeof(testData));
63
64 // Test the copy constructor
65 ByteString b2(b);
66
67 // Test using comparison operator
68 CPPUNIT_ASSERT(b == b2);
69
70 // Test using memcmp
71 CPPUNIT_ASSERT(memcmp(b.byte_str(), b2.byte_str(), b.size()) == 0);
72
73 // Modify the copied version and test again
74 b2[1] = 0x20;
75
76 // Test using comparison operator
77 CPPUNIT_ASSERT(b != b2);
78
79 // Test using memcmp directly
80 CPPUNIT_ASSERT(memcmp(b.byte_str(), b2.byte_str(), b.size()) != 0);
81
82 // Verify that b was not affected
83 CPPUNIT_ASSERT(memcmp(b.byte_str(), testData, sizeof(testData)) == 0);
84
85 // Modify the source data and check if the array operator has functioned correctly
86 testData[1] = 0x20;
87
88 // Test if the right data is in b2
89 CPPUNIT_ASSERT(memcmp(b2.byte_str(), testData, sizeof(testData)) == 0);
90 }
91
92 void ByteStringTests::testAppend()
93 {
94 unsigned char testData[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
95 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10 };
96
97 ByteString b;
98 ByteString b1(testData, sizeof(testData));
99
100 // Test that b is empty and b1 is not
101 CPPUNIT_ASSERT((b.size() == 0) && (b1.size() == sizeof(testData)));
102
103 // Append 1 byte to b
104 b += 0x01;
105
106 // Check the contents of b
107 CPPUNIT_ASSERT(b.size() == 1);
108 CPPUNIT_ASSERT(b[0] == 0x01);
109
110 // Append another byte to b
111 b += 0x02;
112
113 // Check the contents of b
114 CPPUNIT_ASSERT(b.size() == 2);
115 CPPUNIT_ASSERT((b[0] == 0x01) && (b[1] == 0x02));
116
117 // Append b1 to b
118 b += b1;
119
120 // Check the contents of b
121 CPPUNIT_ASSERT(b.size() == 2 + sizeof(testData));
122 CPPUNIT_ASSERT((b[0] == 0x01) && (b[1] == 0x02));
123 CPPUNIT_ASSERT(memcmp(&b[2], testData, sizeof(testData)) == 0);
124
125 // Append b to b
126 b += b;
127
128 // Check the contents of b
129 CPPUNIT_ASSERT(b.size() == 2 * (2 + sizeof(testData)));
130 CPPUNIT_ASSERT((b[0] == 0x01) && (b[1] == 0x02) &&
131 (b[(2 + sizeof(testData)) + 0] == 0x01) &&
132 (b[(2 + sizeof(testData)) + 1] == 0x02));
133 CPPUNIT_ASSERT((memcmp(&b[2], testData, sizeof(testData)) == 0) &&
134 (memcmp(&b[2 + 2 + sizeof(testData)], testData, sizeof(testData)) == 0));
135 }
136
137 void ByteStringTests::testSubstr()
138 {
139 unsigned char testData[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
140 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10 };
141
142 ByteString b;
143 ByteString b1(testData, sizeof(testData));
144
145 // Take a substring
146 b = b1.substr(8, 4);
147
148 // Check b
149 CPPUNIT_ASSERT(b.size() == 4);
150 CPPUNIT_ASSERT(memcmp(b.byte_str(), &testData[8], 4) == 0);
151
152 // Take another substring
153 b = b1.substr(8);
154
155 // Check b
156 CPPUNIT_ASSERT(b.size() == 8);
157 CPPUNIT_ASSERT(memcmp(b.byte_str(), &testData[8], 8) == 0);
158
159 // Two substrings added should yield the original string
160 b = b1.substr(0, 8) + b1.substr(8);
161
162 // Check b
163 CPPUNIT_ASSERT(b.size() == sizeof(testData));
164 CPPUNIT_ASSERT(memcmp(b.byte_str(), testData, sizeof(testData)) == 0);
165 }
166
167 void ByteStringTests::testFromHexStr()
168 {
169 unsigned char testData[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
170 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10 };
171
172 ByteString b("0102030405060708090a0b0c0d0e0f10");
173 ByteString b1("0102030405060708090A0B0C0D0E0F10");
174
175 CPPUNIT_ASSERT(memcmp(b.byte_str(), testData, sizeof(testData)) == 0);
176 CPPUNIT_ASSERT(memcmp(b1.byte_str(), testData, sizeof(testData)) == 0);
177 }
178
179 void ByteStringTests::testXOR()
180 {
181 unsigned char left[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
182 unsigned char right[] = { 0x80, 0x70, 0x60, 0x50, 0x40, 0x30, 0x20, 0x10 };
183 unsigned char xorred[] = { 0x81, 0x72, 0x63, 0x54, 0x45, 0x36, 0x27, 0x18 };
184
185 ByteString l(left, 8);
186 ByteString r(right, 8);
187 ByteString x(xorred, 8);
188 ByteString xed;
189
190 xed = l ^ r;
191
192 CPPUNIT_ASSERT(xed == x);
193
194 ByteString l1(left, 8);
195 ByteString r1(right, 8);
196
197 l1 ^= r1;
198
199 CPPUNIT_ASSERT(l1 == x);
200
201 l1 ^= l;
202
203 CPPUNIT_ASSERT(l1 == r);
204
205 ByteString l_(left, 7);
206
207 xed = l_ ^ r;
208
209 CPPUNIT_ASSERT((xed.size() == 7) && (xed == x.substr(0, 7)));
210
211 ByteString r_(right, 7);
212
213 xed = l ^ r_;
214
215 CPPUNIT_ASSERT((xed.size() == 7) && (xed == x.substr(0, 7)));
216
217 ByteString l1_(left, 8);
218
219 l1_ ^= r_;
220
221 CPPUNIT_ASSERT((l1.size() == 8) && (l1_.substr(0, 7) == x.substr(0,7)) && (l1_[7] == l[7]));
222
223 ByteString l1__(left, 7);
224
225 l1__ ^= r;
226
227 CPPUNIT_ASSERT((l1__ == x.substr(0,7)) && (l1__.size() == 7));
228 }
229
230 void ByteStringTests::testToHexStr()
231 {
232 ByteString b("0102030405060708090A0B0C0D0E0F");
233 ByteString b1("DEADBEEF");
234 ByteString b2("deadC0FFEE");
235
236 std::string s = b.hex_str();
237 std::string s1 = b1.hex_str();
238 std::string s2 = b2.hex_str();
239
240 CPPUNIT_ASSERT(s.compare("0102030405060708090A0B0C0D0E0F") == 0);
241 CPPUNIT_ASSERT(s1.compare("DEADBEEF") == 0);
242 CPPUNIT_ASSERT(s2.compare("DEADC0FFEE") == 0);
243 }
244
245 void ByteStringTests::testLongValues()
246 {
247 unsigned long ul1 = 0x00112233;
248 unsigned long ul2 = 0x10203040;
249 unsigned long ul3 = 0xF0E0D0C0;
250
251 ByteString b1(ul1);
252 ByteString b2(ul2);
253 ByteString b3(ul3);
254
255 CPPUNIT_ASSERT(b1 == ByteString("0000000000112233"));
256 CPPUNIT_ASSERT(b2 == ByteString("0000000010203040"));
257 CPPUNIT_ASSERT(b3 == ByteString("00000000F0E0D0C0"));
258
259 CPPUNIT_ASSERT(b1.long_val() == ul1);
260 CPPUNIT_ASSERT(b2.long_val() == ul2);
261 CPPUNIT_ASSERT(b3.long_val() == ul3);
262
263 ByteString concat = b1 + b2 + b3;
264
265 CPPUNIT_ASSERT(concat == ByteString("0000000000112233000000001020304000000000F0E0D0C0"));
266
267 unsigned long ulr1 = concat.firstLong();
268
269 CPPUNIT_ASSERT(ulr1 == ul1);
270 CPPUNIT_ASSERT(concat == ByteString("000000001020304000000000F0E0D0C0"));
271
272 unsigned long ulr2 = concat.firstLong();
273
274 CPPUNIT_ASSERT(ulr2 == ul2);
275 CPPUNIT_ASSERT(concat == ByteString("00000000F0E0D0C0"));
276
277 unsigned long ulr3 = concat.firstLong();
278
279 CPPUNIT_ASSERT(ulr3 == ul3);
280 CPPUNIT_ASSERT(concat.size() == 0);
281
282 ByteString b4("ABCDEF");
283
284 CPPUNIT_ASSERT(b4.long_val() == 0xABCDEF);
285 CPPUNIT_ASSERT(b4.size() == 3);
286 CPPUNIT_ASSERT(b4.firstLong() == 0xABCDEF);
287 CPPUNIT_ASSERT(b4.size() == 0);
288 }
289
290 void ByteStringTests::testSplitting()
291 {
292 ByteString b("AABBCCDDEEFF112233445566");
293
294 ByteString b1 = b.split(6);
295
296 CPPUNIT_ASSERT(b == ByteString("112233445566"));
297 CPPUNIT_ASSERT(b1 == ByteString("AABBCCDDEEFF"));
298
299 ByteString b2 = b1.split(8);
300
301 CPPUNIT_ASSERT(b2 == ByteString("AABBCCDDEEFF"));
302 CPPUNIT_ASSERT(b1.size() == 0);
303 }
304
305 void ByteStringTests::testBits()
306 {
307 ByteString b1("0");
308 ByteString b2("08");
309 ByteString b3("00FFFFF");
310 ByteString b4("123456");
311
312 CPPUNIT_ASSERT(b1.bits() == 0);
313 CPPUNIT_ASSERT(b2.bits() == 4);
314 CPPUNIT_ASSERT(b3.bits() == 20);
315 CPPUNIT_ASSERT(b4.bits() == 21);
316 }
317
318 void ByteStringTests::testSerialising()
319 {
320 ByteString b1("AA11AA11AA11AA11AA11AA11AA11");
321 ByteString b2("BB22BB22BB22BB22BB22BB22");
322 ByteString b3("CC33CC33CC33CC33CC33CC33CC33CC33");
323
324 ByteString s1 = b1.serialise();
325
326 CPPUNIT_ASSERT(s1.size() == b1.size() + 8);
327
328 ByteString d1 = ByteString::chainDeserialise(s1);
329
330 CPPUNIT_ASSERT(s1.size() == 0);
331 CPPUNIT_ASSERT(d1 == b1);
332
333 ByteString s2 = b3.serialise() + b2.serialise() + b1.serialise();
334
335 CPPUNIT_ASSERT(s2.size() == b1.size() + b2.size() + b3.size() + (3*8));
336
337 d1 = ByteString::chainDeserialise(s2);
338
339 CPPUNIT_ASSERT(d1.size() == b3.size());
340 CPPUNIT_ASSERT(s2.size() == b1.size() + b2.size() + (2*8));
341
342 ByteString d2 = ByteString::chainDeserialise(s2);
343
344 CPPUNIT_ASSERT(d2.size() == b2.size());
345 CPPUNIT_ASSERT(s2.size() == b1.size() + 8);
346
347 ByteString d3 = ByteString::chainDeserialise(s2);
348
349 CPPUNIT_ASSERT(d3.size() == b1.size());
350 CPPUNIT_ASSERT(s2.size() == 0);
351
352 CPPUNIT_ASSERT(d1 == b3);
353 CPPUNIT_ASSERT(d2 == b2);
354 CPPUNIT_ASSERT(d3 == b1);
355 }
356