1 /* Standalone program to test functionality of tvbuffs.
3 * tvbtest : tvbtest.o tvbuff.o except.o
7 * Copyright (c) 2000 by Gilbert Ramirez <gram@alumni.rice.edu>
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.
36 gboolean failed = FALSE;
38 /* Tests a tvbuff against the expected pattern/length.
39 * Returns TRUE if all tests succeeed, FALSE if any test fails */
41 test(tvbuff_t *tvb, gchar* name,
42 guint8* expected_data, guint expected_length, guint expected_reported_length)
45 guint reported_length;
47 volatile gboolean ex_thrown;
48 volatile guint32 val32;
52 length = tvb_length(tvb);
54 if (length != expected_length) {
55 printf("01: Failed TVB=%s Length of tvb=%u while expected length=%u\n",
56 name, length, expected_length);
61 reported_length = tvb_reported_length(tvb);
63 if (reported_length != expected_reported_length) {
64 printf("01: Failed TVB=%s Reported length of tvb=%u while expected reported length=%u\n",
65 name, reported_length, expected_reported_length);
70 /* Test boundary case. A BoundsError exception should be thrown. */
73 tvb_get_ptr(tvb, 0, length + 1);
78 CATCH(ReportedBoundsError) {
79 printf("02: Caught wrong exception: ReportedBoundsError\n");
84 printf("02: Failed TVB=%s No BoundsError when retrieving %u bytes\n",
90 /* Test boundary case with reported_length+1. A ReportedBoundsError
91 exception should be thrown. */
94 tvb_get_ptr(tvb, 0, reported_length + 1);
97 printf("03: Caught wrong exception: BoundsError\n");
99 CATCH(ReportedBoundsError) {
105 printf("03: Failed TVB=%s No ReportedBoundsError when retrieving %u bytes\n",
106 name, reported_length + 1);
111 /* Test boundary case. A BoundsError exception should be thrown. */
114 tvb_get_ptr(tvb, -1, 2);
119 CATCH(ReportedBoundsError) {
120 printf("04: Caught wrong exception: ReportedBoundsError\n");
125 printf("04: Failed TVB=%s No BoundsError when retrieving 2 bytes from"
126 " offset -1\n", name);
131 /* Test boundary case. A BoundsError exception should not be thrown. */
134 tvb_get_ptr(tvb, 0, 1);
139 CATCH(ReportedBoundsError) {
140 printf("05: Caught wrong exception: ReportedBoundsError\n");
145 printf("05: Failed TVB=%s BoundsError when retrieving 1 bytes from"
146 " offset 0\n", name);
151 /* Test boundary case. A BoundsError exception should not be thrown. */
154 tvb_get_ptr(tvb, -1, 1);
159 CATCH(ReportedBoundsError) {
160 printf("06: Caught wrong exception: ReportedBoundsError\n");
165 printf("06: Failed TVB=%s BoundsError when retrieving 1 bytes from"
166 " offset -1\n", name);
172 /* Check data at boundary. An exception should not be thrown. */
176 val32 = tvb_get_ntohl(tvb, 0);
184 printf("07: Failed TVB=%s Exception when retrieving "
185 "guint32 from offset 0\n", name);
190 expected32 = pntohl(expected_data);
191 if (val32 != expected32) {
192 printf("08: Failed TVB=%s guint32 @ 0 %u != expected %u\n",
193 name, val32, expected32);
199 /* Check data at boundary. An exception should not be thrown. */
203 val32 = tvb_get_ntohl(tvb, -4);
211 printf("09: Failed TVB=%s Exception when retrieving "
212 "guint32 from offset 0\n", name);
217 expected32 = pntohl(&expected_data[length-4]);
218 if (val32 != expected32) {
219 printf("10: Failed TVB=%s guint32 @ -4 %u != expected %u\n",
220 name, val32, expected32);
226 /* Sweep across data in various sized increments checking
228 for (incr = 1; incr < length; incr++) {
229 for (i = 0; i < length - incr; i += incr) {
230 ptr = tvb_memdup(tvb, i, incr);
231 if (memcmp(ptr, &expected_data[i], incr) != 0) {
232 printf("11: Failed TVB=%s Offset=%d Length=%d "
244 ptr = tvb_memdup(tvb, 0, -1);
245 if (memcmp(ptr, expected_data, length) != 0) {
246 printf("12: Failed TVB=%s Offset=0 Length=-1 "
247 "Bad memdup\n", name);
255 printf("Passed TVB=%s\n", name);
261 skip(tvbuff_t *tvb _U_, gchar* name,
262 guint8* expected_data _U_, guint expected_length _U_)
264 printf("Skipping TVB=%s\n", name);
274 tvbuff_t *tvb_parent;
275 tvbuff_t *tvb_small[3];
276 tvbuff_t *tvb_large[3];
277 tvbuff_t *tvb_subset[6];
279 guint small_length[3];
280 guint small_reported_length[3];
282 guint large_length[3];
283 guint large_reported_length[3];
285 guint subset_length[6];
286 guint subset_reported_length[6];
289 tvbuff_t *tvb_comp[6];
290 guint comp_length[6];
291 guint comp_reported_length[6];
294 tvb_parent = tvb_new_real_data("", 0, 0);
295 for (i = 0; i < 3; i++) {
296 small[i] = g_new(guint8, 16);
299 for (j = 0; j < 16; j++) {
300 small[i][j] = temp + j;
302 small_length[i] = 16;
303 small_reported_length[i] = 17;
304 tvb_small[i] = tvb_new_child_real_data(tvb_parent, small[i], 16, 17);
305 tvb_set_free_cb(tvb_small[i], g_free);
308 for (i = 0; i < 3; i++) {
309 large[i] = g_new(guint8, 19);
312 for (j = 0; j < 19; j++) {
313 large[i][j] = temp + j;
316 large_length[i] = 19;
317 large_reported_length[i] = 20;
318 tvb_large[i] = tvb_new_child_real_data(tvb_parent, large[i], 19, 20);
319 tvb_set_free_cb(tvb_large[i], g_free);
322 /* Test the TVBUFF_REAL_DATA objects. */
323 test(tvb_small[0], "Small 0", small[0], small_length[0], small_reported_length[0]);
324 test(tvb_small[1], "Small 1", small[1], small_length[1], small_reported_length[1]);
325 test(tvb_small[2], "Small 2", small[2], small_length[2], small_reported_length[2]);
327 test(tvb_large[0], "Large 0", large[0], large_length[0], large_reported_length[0]);
328 test(tvb_large[1], "Large 1", large[1], large_length[1], large_reported_length[1]);
329 test(tvb_large[2], "Large 2", large[2], large_length[2], large_reported_length[2]);
331 subset_length[0] = 8;
332 subset_reported_length[0] = 9;
333 tvb_subset[0] = tvb_new_subset(tvb_small[0], 0, 8, 9);
334 subset[0] = &small[0][0];
336 subset_length[1] = 10;
337 subset_reported_length[1] = 11;
338 tvb_subset[1] = tvb_new_subset(tvb_large[0], -10, 10, 11);
339 subset[1] = &large[0][9];
341 subset_length[2] = 16;
342 subset_reported_length[2] = 17;
343 tvb_subset[2] = tvb_new_subset(tvb_small[1], -16, -1, 17);
344 subset[2] = &small[1][0];
346 subset_length[3] = 3;
347 subset_reported_length[3] = 4;
348 tvb_subset[3] = tvb_new_subset(tvb_subset[0], 0, 3, 4);
349 subset[3] = &small[0][0];
351 subset_length[4] = 5;
352 subset_reported_length[4] = 6;
353 tvb_subset[4] = tvb_new_subset(tvb_subset[1], -5, 5, 6);
354 subset[4] = &large[0][14];
356 subset_length[5] = 8;
357 subset_reported_length[5] = 9;
358 tvb_subset[5] = tvb_new_subset(tvb_subset[2], 4, 8, 9);
359 subset[5] = &small[1][4];
361 /* Test the TVBUFF_SUBSET objects. */
362 test(tvb_subset[0], "Subset 0", subset[0], subset_length[0], subset_reported_length[0]);
363 test(tvb_subset[1], "Subset 1", subset[1], subset_length[1], subset_reported_length[1]);
364 test(tvb_subset[2], "Subset 2", subset[2], subset_length[2], subset_reported_length[2]);
365 test(tvb_subset[3], "Subset 3", subset[3], subset_length[3], subset_reported_length[3]);
366 test(tvb_subset[4], "Subset 4", subset[4], subset_length[4], subset_reported_length[4]);
367 test(tvb_subset[5], "Subset 5", subset[5], subset_length[5], subset_reported_length[5]);
369 /* Composite tvbuffs don't work at the moment -- tests commented out until
373 printf("Making Composite 0\n");
374 tvb_comp[0] = tvb_new_composite();
375 comp_length[0] = small_length[0];
376 comp_reported_length[0] = small_reported_length[0];
378 tvb_composite_append(tvb_comp[0], tvb_small[0]);
379 tvb_composite_finalize(tvb_comp[0]);
382 printf("Making Composite 1\n");
383 tvb_comp[1] = tvb_new_composite();
384 comp_length[1] = small_length[0] + small_length[1];
385 comp_reported_length[1] = small_reported_length[0] + small_reported_length[1];
386 comp[1] = g_malloc(comp_length[1]);
387 memcpy(comp[1], small[0], small_length[0]);
388 memcpy(&comp[1][small_length[0]], small[1], small_length[1]);
389 tvb_composite_append(tvb_comp[1], tvb_small[0]);
390 tvb_composite_append(tvb_comp[1], tvb_small[1]);
391 tvb_composite_finalize(tvb_comp[1]);
394 printf("Making Composite 2\n");
395 tvb_comp[2] = tvb_new_composite();
396 comp_length[2] = subset_length[1];
397 comp_reported_length[2] = subset_reported_length[1];
399 tvb_composite_append(tvb_comp[2], tvb_subset[1]);
400 tvb_composite_finalize(tvb_comp[2]);
403 printf("Making Composite 3\n");
404 tvb_comp[3] = tvb_new_composite();
405 comp_length[3] = subset_length[4] + subset_length[5];
406 comp_reported_length[3] = subset_reported_length[4] + subset_reported_length[5];
407 comp[3] = g_malloc(comp_length[3]);
408 memcpy(comp[3], subset[4], subset_length[4]);
409 memcpy(&comp[3][subset_length[4]], subset[5], subset_length[5]);
410 tvb_composite_append(tvb_comp[3], tvb_subset[4]);
411 tvb_composite_append(tvb_comp[3], tvb_subset[5]);
412 tvb_composite_finalize(tvb_comp[3]);
414 /* One real, one subset */
415 printf("Making Composite 4\n");
416 tvb_comp[4] = tvb_new_composite();
417 comp_length[4] = small_length[0] + subset_length[1];
418 comp_reported_length[4] = small_reported_length[0] + subset_reported_length[1];
419 comp[4] = g_malloc(comp_length[4]);
420 memcpy(&comp[4][0], small[0], small_length[0]);
421 memcpy(&comp[4][small_length[0]], subset[1], subset_length[1]);
422 tvb_composite_append(tvb_comp[4], tvb_small[0]);
423 tvb_composite_append(tvb_comp[4], tvb_subset[1]);
424 tvb_composite_finalize(tvb_comp[4]);
427 printf("Making Composite 5\n");
428 tvb_comp[5] = tvb_new_composite();
429 comp_length[5] = comp_length[0] +
433 comp_reported_length[5] = comp_reported_length[0] +
434 comp_reported_length[1] +
435 comp_reported_length[2] +
436 comp_reported_length[3];
437 comp[5] = g_malloc(comp_length[5]);
440 memcpy(&comp[5][len], comp[0], comp_length[0]);
441 len += comp_length[0];
442 memcpy(&comp[5][len], comp[1], comp_length[1]);
443 len += comp_length[1];
444 memcpy(&comp[5][len], comp[2], comp_length[2]);
445 len += comp_length[2];
446 memcpy(&comp[5][len], comp[3], comp_length[3]);
448 tvb_composite_append(tvb_comp[5], tvb_comp[0]);
449 tvb_composite_append(tvb_comp[5], tvb_comp[1]);
450 tvb_composite_append(tvb_comp[5], tvb_comp[2]);
451 tvb_composite_append(tvb_comp[5], tvb_comp[3]);
452 tvb_composite_finalize(tvb_comp[5]);
454 /* Test the TVBUFF_COMPOSITE objects. */
455 test(tvb_comp[0], "Composite 0", comp[0], comp_length[0], comp_reported_length[0]);
456 test(tvb_comp[1], "Composite 1", comp[1], comp_length[1], comp_reported_length[1]);
457 test(tvb_comp[2], "Composite 2", comp[2], comp_length[2], comp_reported_length[2]);
458 test(tvb_comp[3], "Composite 3", comp[3], comp_length[3], comp_reported_length[3]);
459 test(tvb_comp[4], "Composite 4", comp[4], comp_length[4], comp_reported_length[4]);
460 test(tvb_comp[5], "Composite 5", comp[5], comp_length[5], comp_reported_length[5]);
463 /* Don't free: comp[0] */
465 /* Don't free: comp[2] */
470 tvb_free_chain(tvb_parent); /* should free all tvb's and associated data */
473 /* Note: valgrind can be used to check for tvbuff memory leaks */
477 /* For valgrind: See GLib documentation: "Running GLib Applications" */
478 g_setenv("G_DEBUG", "gc-friendly", 1);
479 g_setenv("G_SLICE", "always-malloc", 1);