TODO SMB2 NegotiateContext....
[metze/wireshark/wip.git] / epan / tvbtest.c
index 94b91d2dc3e1adf59b6c1e3c1ccbc5231227b025..fb5984d1c8a0d47a3f167f73775ff8d25558eb35 100644 (file)
@@ -1,24 +1,11 @@
-/* Standalone program to test functionality of tvbuffs.
+/* tvbtest.c
+ * Standalone program to test functionality of tvbuffs.
  *
  * tvbtest : tvbtest.o tvbuff.o except.o
  *
- * $Id$
- *
  * Copyright (c) 2000 by Gilbert Ramirez <gram@alumni.rice.edu>
  *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ * SPDX-License-Identifier: GPL-2.0-or-later
  *
  */
 
 #include <string.h>
 
 #include "tvbuff.h"
-#include "pint.h"
+#include "exceptions.h"
+#include "wsutil/pint.h"
 
 gboolean failed = FALSE;
 
 /* Tests a tvbuff against the expected pattern/length.
  * Returns TRUE if all tests succeeed, FALSE if any test fails */
-gboolean
-test(tvbuff_t *tvb, gchar* name,
+static gboolean
+test(tvbuff_t *tvb, const gchar* name,
      guint8* expected_data, guint expected_length, guint expected_reported_length)
 {
        guint                   length;
@@ -47,7 +35,7 @@ test(tvbuff_t *tvb, gchar* name,
        guint32                 expected32;
        guint                   incr, i;
 
-       length = tvb_length(tvb);
+       length = tvb_captured_length(tvb);
 
        if (length != expected_length) {
                printf("01: Failed TVB=%s Length of tvb=%u while expected length=%u\n",
@@ -73,9 +61,15 @@ test(tvbuff_t *tvb, gchar* name,
        CATCH(BoundsError) {
                ex_thrown = TRUE;
        }
+       CATCH(FragmentBoundsError) {
+               printf("02: Caught wrong exception: FragmentBoundsError\n");
+       }
        CATCH(ReportedBoundsError) {
                printf("02: Caught wrong exception: ReportedBoundsError\n");
        }
+       CATCH_ALL {
+               printf("02: Caught wrong exception: %lu\n", exc->except_id.except_code);
+       }
        ENDTRY;
 
        if (!ex_thrown) {
@@ -94,9 +88,15 @@ test(tvbuff_t *tvb, gchar* name,
        CATCH(BoundsError) {
                printf("03: Caught wrong exception: BoundsError\n");
        }
+       CATCH(FragmentBoundsError) {
+               printf("03: Caught wrong exception: FragmentBoundsError\n");
+       }
        CATCH(ReportedBoundsError) {
                ex_thrown = TRUE;
        }
+       CATCH_ALL {
+               printf("03: Caught wrong exception: %lu\n", exc->except_id.except_code);
+       }
        ENDTRY;
 
        if (!ex_thrown) {
@@ -114,9 +114,15 @@ test(tvbuff_t *tvb, gchar* name,
        CATCH(BoundsError) {
                ex_thrown = TRUE;
        }
+       CATCH(FragmentBoundsError) {
+               printf("04: Caught wrong exception: FragmentBoundsError\n");
+       }
        CATCH(ReportedBoundsError) {
                printf("04: Caught wrong exception: ReportedBoundsError\n");
        }
+       CATCH_ALL {
+               printf("04: Caught wrong exception: %lu\n", exc->except_id.except_code);
+       }
        ENDTRY;
 
        if (!ex_thrown) {
@@ -129,14 +135,20 @@ test(tvbuff_t *tvb, gchar* name,
        /* Test boundary case. A BoundsError exception should not be thrown. */
        ex_thrown = FALSE;
        TRY {
-               tvb_get_ptr(tvb, 0, 1);
+               tvb_get_ptr(tvb, 0, length ? 1 : 0);
        }
        CATCH(BoundsError) {
                ex_thrown = TRUE;
        }
+       CATCH(FragmentBoundsError) {
+               printf("05: Caught wrong exception: FragmentBoundsError\n");
+       }
        CATCH(ReportedBoundsError) {
                printf("05: Caught wrong exception: ReportedBoundsError\n");
        }
+       CATCH_ALL {
+               printf("05: Caught wrong exception: %lu\n", exc->except_id.except_code);
+       }
        ENDTRY;
 
        if (ex_thrown) {
@@ -149,14 +161,20 @@ test(tvbuff_t *tvb, gchar* name,
        /* Test boundary case. A BoundsError exception should not be thrown. */
        ex_thrown = FALSE;
        TRY {
-               tvb_get_ptr(tvb, -1, 1);
+               tvb_get_ptr(tvb, -1, length ? 1 : 0);
        }
        CATCH(BoundsError) {
                ex_thrown = TRUE;
        }
+       CATCH(FragmentBoundsError) {
+               printf("06: Caught wrong exception: FragmentBoundsError\n");
+       }
        CATCH(ReportedBoundsError) {
                printf("06: Caught wrong exception: ReportedBoundsError\n");
        }
+       CATCH_ALL {
+               printf("06: Caught wrong exception: %lu\n", exc->except_id.except_code);
+       }
        ENDTRY;
 
        if (ex_thrown) {
@@ -185,7 +203,7 @@ test(tvbuff_t *tvb, gchar* name,
                        return FALSE;
                }
 
-               expected32 = pntohl(expected_data);
+               expected32 = pntoh32(expected_data);
                if (val32 != expected32) {
                        printf("08: Failed TVB=%s  guint32 @ 0 %u != expected %u\n",
                                        name, val32, expected32);
@@ -212,7 +230,7 @@ test(tvbuff_t *tvb, gchar* name,
                        return FALSE;
                }
 
-               expected32 = pntohl(&expected_data[length-4]);
+               expected32 = pntoh32(&expected_data[length-4]);
                if (val32 != expected32) {
                        printf("10: Failed TVB=%s guint32 @ -4 %u != expected %u\n",
                                        name, val32, expected32);
@@ -225,29 +243,30 @@ test(tvbuff_t *tvb, gchar* name,
         * tvb_memdup() */
        for (incr = 1; incr < length; incr++) {
                for (i = 0; i < length - incr; i += incr) {
-                       ptr = tvb_memdup(tvb, i, incr);
+                       ptr = (guint8*)tvb_memdup(NULL, tvb, i, incr);
                        if (memcmp(ptr, &expected_data[i], incr) != 0) {
-                               printf("11: Failed TVB=%s Offset=%d Length=%d "
+                               printf("11: Failed TVB=%s Offset=%u Length=%u "
                                                "Bad memdup\n",
                                                name, i, incr);
                                failed = TRUE;
-                               g_free(ptr);
+                               wmem_free(NULL, ptr);
                                return FALSE;
                        }
-                       g_free(ptr);
+                       wmem_free(NULL, ptr);
                }
        }
 
        /* One big memdup */
-       ptr = tvb_memdup(tvb, 0, -1);
-       if (memcmp(ptr, expected_data, length) != 0) {
+       ptr = (guint8*)tvb_memdup(NULL, tvb, 0, -1);
+       if ((length != 0 && memcmp(ptr, expected_data, length) != 0) ||
+           (length == 0 && ptr != NULL)) {
                printf("12: Failed TVB=%s Offset=0 Length=-1 "
                                "Bad memdup\n", name);
                failed = TRUE;
-               g_free(ptr);
+               wmem_free(NULL, ptr);
                return FALSE;
        }
-       g_free(ptr);
+       wmem_free(NULL, ptr);
 
 
        printf("Passed TVB=%s\n", name);
@@ -255,24 +274,17 @@ test(tvbuff_t *tvb, gchar* name,
        return TRUE;
 }
 
-gboolean
-skip(tvbuff_t *tvb _U_, gchar* name,
-               guint8* expected_data _U_, guint expected_length _U_)
-{
-       printf("Skipping TVB=%s\n", name);
-       return FALSE;
-}
-
-
-void
+static void
 run_tests(void)
 {
        int             i, j;
 
        tvbuff_t        *tvb_parent;
+       tvbuff_t        *tvb_empty;
        tvbuff_t        *tvb_small[3];
        tvbuff_t        *tvb_large[3];
        tvbuff_t        *tvb_subset[6];
+       tvbuff_t        *tvb_empty_subset;
        guint8          *small[3];
        guint           small_length[3];
        guint           small_reported_length[3];
@@ -317,7 +329,11 @@ run_tests(void)
                tvb_set_free_cb(tvb_large[i], g_free);
        }
 
-       /* Test the TVBUFF_REAL_DATA objects. */
+       /* Test empty tvb */
+       tvb_empty = tvb_new_child_real_data(tvb_parent, NULL, 0, 1);
+       test(tvb_empty, "Empty", NULL, 0, 1);
+
+       /* Test the "real" tvbuff objects. */
        test(tvb_small[0], "Small 0", small[0], small_length[0], small_reported_length[0]);
        test(tvb_small[1], "Small 1", small[1], small_length[1], small_reported_length[1]);
        test(tvb_small[2], "Small 2", small[2], small_length[2], small_reported_length[2]);
@@ -328,35 +344,35 @@ run_tests(void)
 
        subset_length[0]          = 8;
        subset_reported_length[0] = 9;
-       tvb_subset[0]             = tvb_new_subset(tvb_small[0], 0, 8, 9);
+       tvb_subset[0]             = tvb_new_subset_length_caplen(tvb_small[0], 0, 8, 9);
        subset[0]                 = &small[0][0];
 
        subset_length[1]          = 10;
        subset_reported_length[1] = 11;
-       tvb_subset[1]             = tvb_new_subset(tvb_large[0], -10, 10, 11);
+       tvb_subset[1]             = tvb_new_subset_length_caplen(tvb_large[0], -10, 10, 11);
        subset[1]                 = &large[0][9];
 
        subset_length[2]          = 16;
        subset_reported_length[2] = 17;
-       tvb_subset[2]             = tvb_new_subset(tvb_small[1], -16, -1, 17);
+       tvb_subset[2]             = tvb_new_subset_length_caplen(tvb_small[1], -16, -1, 17);
        subset[2]                 = &small[1][0];
 
        subset_length[3]          = 3;
        subset_reported_length[3] = 4;
-       tvb_subset[3]             = tvb_new_subset(tvb_subset[0], 0, 3, 4);
+       tvb_subset[3]             = tvb_new_subset_length_caplen(tvb_subset[0], 0, 3, 4);
        subset[3]                 = &small[0][0];
 
        subset_length[4]          = 5;
        subset_reported_length[4] = 6;
-       tvb_subset[4]             = tvb_new_subset(tvb_subset[1], -5, 5, 6);
+       tvb_subset[4]             = tvb_new_subset_length_caplen(tvb_subset[1], -5, 5, 6);
        subset[4]                 = &large[0][14];
 
        subset_length[5]          = 8;
        subset_reported_length[5] = 9;
-       tvb_subset[5]             = tvb_new_subset(tvb_subset[2], 4, 8, 9);
+       tvb_subset[5]             = tvb_new_subset_length_caplen(tvb_subset[2], 4, 8, 9);
        subset[5]                 = &small[1][4];
 
-       /* Test the TVBUFF_SUBSET objects. */
+       /* Test the "subset" tvbuff objects. */
        test(tvb_subset[0], "Subset 0", subset[0], subset_length[0], subset_reported_length[0]);
        test(tvb_subset[1], "Subset 1", subset[1], subset_length[1], subset_reported_length[1]);
        test(tvb_subset[2], "Subset 2", subset[2], subset_length[2], subset_reported_length[2]);
@@ -364,6 +380,10 @@ run_tests(void)
        test(tvb_subset[4], "Subset 4", subset[4], subset_length[4], subset_reported_length[4]);
        test(tvb_subset[5], "Subset 5", subset[5], subset_length[5], subset_reported_length[5]);
 
+       /* Subset of an empty tvb. */
+       tvb_empty_subset = tvb_new_subset_length_caplen(tvb_empty, 0, 0, 1);
+       test(tvb_empty_subset, "Empty Subset", NULL, 0, 1);
+
        /* One Real */
        printf("Making Composite 0\n");
        tvb_comp[0]             = tvb_new_composite();
@@ -378,7 +398,7 @@ run_tests(void)
        tvb_comp[1]             = tvb_new_composite();
        comp_length[1]          = small_length[0] + small_length[1];
        comp_reported_length[1] = small_reported_length[0] + small_reported_length[1];
-       comp[1]                 = g_malloc(comp_length[1]);
+       comp[1]                 = (guint8*)g_malloc(comp_length[1]);
        memcpy(comp[1], small[0], small_length[0]);
        memcpy(&comp[1][small_length[0]], small[1], small_length[1]);
        tvb_composite_append(tvb_comp[1], tvb_small[0]);
@@ -399,7 +419,7 @@ run_tests(void)
        tvb_comp[3]             = tvb_new_composite();
        comp_length[3]          = subset_length[4] + subset_length[5];
        comp_reported_length[3] = subset_reported_length[4] + subset_reported_length[5];
-       comp[3]                 = g_malloc(comp_length[3]);
+       comp[3]                 = (guint8*)g_malloc(comp_length[3]);
        memcpy(comp[3], subset[4], subset_length[4]);
        memcpy(&comp[3][subset_length[4]], subset[5], subset_length[5]);
        tvb_composite_append(tvb_comp[3], tvb_subset[4]);
@@ -411,7 +431,7 @@ run_tests(void)
        tvb_comp[4]             = tvb_new_composite();
        comp_length[4]          = small_length[0] + subset_length[1];
        comp_reported_length[4] = small_reported_length[0] + subset_reported_length[1];
-       comp[4]                 = g_malloc(comp_length[4]);
+       comp[4]                 = (guint8*)g_malloc(comp_length[4]);
        memcpy(&comp[4][0], small[0], small_length[0]);
        memcpy(&comp[4][small_length[0]], subset[1], subset_length[1]);
        tvb_composite_append(tvb_comp[4], tvb_small[0]);
@@ -429,7 +449,7 @@ run_tests(void)
                                        comp_reported_length[1] +
                                        comp_reported_length[2] +
                                        comp_reported_length[3];
-       comp[5]                 = g_malloc(comp_length[5]);
+       comp[5]                 = (guint8*)g_malloc(comp_length[5]);
 
        len = 0;
        memcpy(&comp[5][len], comp[0], comp_length[0]);
@@ -446,7 +466,7 @@ run_tests(void)
        tvb_composite_append(tvb_comp[5], tvb_comp[3]);
        tvb_composite_finalize(tvb_comp[5]);
 
-       /* Test the TVBUFF_COMPOSITE objects. */
+       /* Test the "composite" tvbuff objects. */
        test(tvb_comp[0], "Composite 0", comp[0], comp_length[0], comp_reported_length[0]);
        test(tvb_comp[1], "Composite 1", comp[1], comp_length[1], comp_reported_length[1]);
        test(tvb_comp[2], "Composite 2", comp[2], comp_length[2], comp_reported_length[2]);
@@ -478,3 +498,16 @@ main(void)
        except_deinit();
        exit(failed?1:0);
 }
+
+/*
+ * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
+ *
+ * Local variables:
+ * c-basic-offset: 8
+ * tab-width: 8
+ * indent-tabs-mode: t
+ * End:
+ *
+ * vi: set shiftwidth=8 tabstop=8 noexpandtab:
+ * :indentSize=8:tabSize=8:noTabs=false:
+ */