1 /* Standalone program to test functionality of tvbuffs.
3 * tvbtest : tvbtest.o tvbuff.o except.o
5 * $Id: tvbtest.c,v 1.1 2000/05/11 08:16:00 gram Exp $
7 * Copyright (c) 2000 by Gilbert Ramirez <gram@xiexie.org>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 #define pntohs(p) ((guint16) \
32 ((guint16)*((guint8 *)p+0)<<8| \
33 (guint16)*((guint8 *)p+1)<<0))
35 #define pntohl(p) ((guint32)*((guint8 *)p+0)<<24| \
36 (guint32)*((guint8 *)p+1)<<16| \
37 (guint32)*((guint8 *)p+2)<<8| \
38 (guint32)*((guint8 *)p+3)<<0)
40 #define pletohs(p) ((guint16) \
41 ((guint16)*((guint8 *)p+1)<<8| \
42 (guint16)*((guint8 *)p+0)<<0))
44 #define pletohl(p) ((guint32)*((guint8 *)p+3)<<24| \
45 (guint32)*((guint8 *)p+2)<<16| \
46 (guint32)*((guint8 *)p+1)<<8| \
47 (guint32)*((guint8 *)p+0)<<0)
49 /* Tests a tvbuff against the expected pattern/length.
50 * Returns TRUE if all tests succeeed, FALSE if any test fails */
52 test(tvbuff_t *tvb, gchar* name,
53 guint8* expected_data, guint expected_length)
58 guint32 val32, expected32;
61 length = tvb_length(tvb);
63 if (length != expected_length) {
64 printf("Failed TVB=%s Length of tvb=%u while expected length=%u\n",
65 name, length, expected_length);
69 /* Test boundary case. A BoundsError exception should be thrown. */
72 ptr = tvb_get_ptr(tvb, 0, length + 1);
80 printf("Failed TVB=%s No BoundsError when retrieving %u bytes\n",
85 /* Test boundary case. A BoundsError exception should be thrown. */
88 ptr = tvb_get_ptr(tvb, -1, 2);
96 printf("Failed TVB=%s No BoundsError when retrieving 2 bytes from"
97 " offset -1\n", name);
101 /* Test boundary case. A BoundsError exception should not be thrown. */
104 ptr = tvb_get_ptr(tvb, 0, 1);
112 printf("Failed TVB=%s BoundsError when retrieving 1 bytes from"
113 " offset 0\n", name);
117 /* Test boundary case. A BoundsError exception should not be thrown. */
120 ptr = tvb_get_ptr(tvb, -1, 1);
128 printf("Failed TVB=%s BoundsError when retrieving 1 bytes from"
129 " offset -1\n", name);
134 /* Check data at boundary. An exception should not be thrown. */
138 val32 = tvb_get_ntohl(tvb, 0);
146 printf("Failed TVB=%s BoundsError when retrieving "
147 "guint32 from offset 0\n", name);
151 expected32 = pntohl(expected_data);
152 if (val32 != expected32) {
153 printf("Failed TVB=%s guint32 @ 0 %u != expected %u\n",
154 name, val32, expected32);
159 /* Check data at boundary. An exception should not be thrown. */
163 val32 = tvb_get_ntohl(tvb, -4);
171 printf("Failed TVB=%s BoundsError when retrieving "
172 "guint32 from offset 0\n", name);
176 expected32 = pntohl(&expected_data[length-4]);
177 if (val32 != expected32) {
178 printf("Failed TVB=%s guint32 @ -4 %u != expected %u\n",
179 name, val32, expected32);
184 /* Sweep across data in various sized increments checking
186 for (incr = 1; incr < length; incr++) {
187 for (i = 0; i < length - incr; i += incr) {
188 ptr = tvb_memdup(tvb, i, incr);
189 if (memcmp(ptr, &expected_data[i], incr) != 0) {
190 printf("Failed TVB=%s Offset=%d Length=%d "
201 ptr = tvb_memdup(tvb, 0, -1);
202 if (memcmp(ptr, expected_data, length) != 0) {
203 printf("Failed TVB=%s Offset=0 Length=-1 "
204 "Bad memdup\n", name);
211 printf("Passed TVB=%s\n", name);
223 tvbuff_t *tvb_small[3];
224 tvbuff_t *tvb_large[3];
225 tvbuff_t *tvb_subset[6];
226 tvbuff_t *tvb_comp[6];
230 guint subset_length[6];
232 guint comp_length[6];
236 for (i = 0; i < 3; i++) {
237 small[i] = g_new(guint8, 16);
240 for (j = 0; j < 16; j++) {
241 small[i][j] = temp + j;
244 tvb_small[i] = tvb_new_real_data(small[i], 16);
247 for (i = 0; i < 3; i++) {
248 large[i] = g_new(guint8, 19);
251 for (j = 0; j < 19; j++) {
252 large[i][j] = temp + j;
255 tvb_large[i] = tvb_new_real_data(large[i], 19);
258 /* Test the TVBUFF_REAL_DATA objects. */
259 test(tvb_small[0], "Small 0", small[0], 16);
260 test(tvb_small[1], "Small 1", small[1], 16);
261 test(tvb_small[2], "Small 2", small[2], 16);
263 test(tvb_large[0], "Large 0", large[0], 19);
264 test(tvb_large[1], "Large 1", large[1], 19);
265 test(tvb_large[2], "Large 2", large[2], 19);
267 tvb_subset[0] = tvb_new_subset(tvb_small[0], 0, 8);
268 subset[0] = &small[0][0];
269 subset_length[0] = 8;
271 tvb_subset[1] = tvb_new_subset(tvb_large[0], -10, 10);
272 subset[1] = &large[0][9];
273 subset_length[1] = 10;
275 tvb_subset[2] = tvb_new_subset(tvb_small[1], -16, -1);
276 subset[2] = &small[1][0];
277 subset_length[2] = 16;
279 tvb_subset[3] = tvb_new_subset(tvb_subset[0], 0, 3);
280 subset[3] = &small[0][0];
281 subset_length[3] = 3;
283 tvb_subset[4] = tvb_new_subset(tvb_subset[1], -5, 5);
284 subset[4] = &large[0][14];
285 subset_length[4] = 5;
287 tvb_subset[5] = tvb_new_subset(tvb_subset[2], 4, 8);
288 subset[5] = &small[1][4];
289 subset_length[5] = 8;
291 /* Test the TVBUFF_SUBSET objects. */
292 test(tvb_subset[0], "Subset 0", subset[0], subset_length[0]);
293 test(tvb_subset[1], "Subset 1", subset[1], subset_length[1]);
294 test(tvb_subset[2], "Subset 2", subset[2], subset_length[2]);
295 test(tvb_subset[3], "Subset 3", subset[3], subset_length[3]);
296 test(tvb_subset[4], "Subset 4", subset[4], subset_length[4]);
297 test(tvb_subset[5], "Subset 5", subset[5], subset_length[5]);
300 printf("Making Composite 0\n");
301 tvb_comp[0] = tvb_new_composite();
304 tvb_composite_append(tvb_comp[0], tvb_small[0]);
305 tvb_composite_finalize(tvb_comp[0]);
308 printf("Making Composite 1\n");
309 tvb_comp[1] = tvb_new_composite();
310 comp[1] = g_malloc(32);
312 memcpy(comp[1], small[0], 16);
313 memcpy(&comp[1][16], small[1], 16);
314 tvb_composite_append(tvb_comp[1], tvb_small[0]);
315 tvb_composite_append(tvb_comp[1], tvb_small[1]);
316 tvb_composite_finalize(tvb_comp[1]);
319 printf("Making Composite 2\n");
320 tvb_comp[2] = tvb_new_composite();
322 comp_length[2] = subset_length[1];
323 tvb_composite_append(tvb_comp[2], tvb_subset[1]);
324 tvb_composite_finalize(tvb_comp[2]);
327 printf("Making Composite 3\n");
328 tvb_comp[3] = tvb_new_composite();
329 comp[3] = g_malloc(13);
331 memcpy(comp[3], &large[0][14], 5);
332 memcpy(&comp[3][5], &small[1][4], 8);
333 tvb_composite_append(tvb_comp[3], tvb_subset[4]);
334 tvb_composite_append(tvb_comp[3], tvb_subset[5]);
335 tvb_composite_finalize(tvb_comp[3]);
337 /* One real, one subset */
338 printf("Making Composite 4\n");
339 tvb_comp[4] = tvb_new_composite();
340 comp[4] = g_malloc(16 + subset_length[1]);
341 comp_length[4] = 16 + subset_length[1];
342 memcpy(comp[4], small[0], 16);
343 memcpy(&comp[4][16], subset[1], subset_length[1]);
344 tvb_composite_append(tvb_comp[4], tvb_small[0]);
345 tvb_composite_append(tvb_comp[4], tvb_subset[1]);
346 tvb_composite_finalize(tvb_comp[4]);
349 printf("Making Composite 5\n");
350 tvb_comp[5] = tvb_new_composite();
351 comp_length[5] = comp_length[0] +
355 comp[5] = g_malloc(comp_length[5]);
358 memcpy(&comp[5][len], comp[0], comp_length[0]);
359 len += comp_length[0];
360 memcpy(&comp[5][len], comp[1], comp_length[1]);
361 len += comp_length[1];
362 memcpy(&comp[5][len], comp[2], comp_length[2]);
363 len += comp_length[2];
364 memcpy(&comp[5][len], comp[3], comp_length[3]);
366 tvb_composite_append(tvb_comp[5], tvb_comp[0]);
367 tvb_composite_append(tvb_comp[5], tvb_comp[1]);
368 tvb_composite_append(tvb_comp[5], tvb_comp[2]);
369 tvb_composite_append(tvb_comp[5], tvb_comp[3]);
370 tvb_composite_finalize(tvb_comp[5]);
372 /* Test the TVBUFF_COMPOSITE objects. */
373 test(tvb_comp[0], "Composite 0", comp[0], comp_length[0]);
374 test(tvb_comp[1], "Composite 1", comp[1], comp_length[1]);
375 test(tvb_comp[2], "Composite 2", comp[2], comp_length[2]);
376 test(tvb_comp[3], "Composite 3", comp[3], comp_length[3]);
377 test(tvb_comp[4], "Composite 4", comp[4], comp_length[4]);
378 test(tvb_comp[5], "Composite 5", comp[5], comp_length[5]);