Wireshark-dev: [Wireshark-dev] [PATCH] make buildbot run unit tests

From: Richard van der Hoff <richardv@xxxxxxxxxxxxx>
Date: Mon, 05 Mar 2007 12:24:56 +0000
Hi,

The epan directory is accumulating a collection of unit-tests, which it seems would be a good idea to have the buildbot run, to catch regressions sooner. For instance, it might have picked up the fact that tvbtest currently appears to fail... (I assume that's just the fact that composite TVBs are known to be flaky, but I'll leave it to someone else to disable those tests if they are happy with that).

The attached patch plugs tvbtest, exntest and reassemble_test into the test/test.sh script. Please could it be applied!


Index: epan/tvbtest.c
===================================================================
--- epan/tvbtest.c	(revision 12064)
+++ epan/tvbtest.c	(working copy)
@@ -29,6 +29,8 @@
 #include "tvbuff.h"
 #include "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
@@ -47,6 +49,7 @@
 	if (length != expected_length) {
 		printf("01: Failed TVB=%s Length of tvb=%u while expected length=%u\n",
 				name, length, expected_length);
+		failed = TRUE;
 		return FALSE;
 	}
 
@@ -66,6 +69,7 @@
 	if (!ex_thrown) {
 		printf("02: Failed TVB=%s No BoundsError when retrieving %u bytes\n",
 				name, length + 1);
+		failed = TRUE;
 		return FALSE;
 	}
 
@@ -86,6 +90,7 @@
 	if (!ex_thrown) {
 		printf("03: Failed TVB=%s No ReportedBoundsError when retrieving %u bytes\n",
 				name, length + 2);
+		failed = TRUE;
 		return FALSE;
 	}
 
@@ -105,6 +110,7 @@
 	if (!ex_thrown) {
 		printf("04: Failed TVB=%s No BoundsError when retrieving 2 bytes from"
 				" offset -1\n", name);
+		failed = TRUE;
 		return FALSE;
 	}
 
@@ -124,6 +130,7 @@
 	if (ex_thrown) {
 		printf("05: Failed TVB=%s BoundsError when retrieving 1 bytes from"
 				" offset 0\n", name);
+		failed = TRUE;
 		return FALSE;
 	}
 
@@ -143,6 +150,7 @@
 	if (ex_thrown) {
 		printf("06: Failed TVB=%s BoundsError when retrieving 1 bytes from"
 				" offset -1\n", name);
+		failed = TRUE;
 		return FALSE;
 	}
 
@@ -161,6 +169,7 @@
 		if (ex_thrown) {
 			printf("07: Failed TVB=%s Exception when retrieving "
 					"guint32 from offset 0\n", name);
+			failed = TRUE;
 			return FALSE;
 		}
 
@@ -168,6 +177,7 @@
 		if (val32 != expected32) {
 			printf("08: Failed TVB=%s  guint32 @ 0 %u != expected %u\n",
 					name, val32, expected32);
+			failed = TRUE;
 			return FALSE;
 		}
 	}
@@ -186,6 +196,7 @@
 		if (ex_thrown) {
 			printf("09: Failed TVB=%s Exception when retrieving "
 					"guint32 from offset 0\n", name);
+			failed = TRUE;
 			return FALSE;
 		}
 
@@ -193,6 +204,7 @@
 		if (val32 != expected32) {
 			printf("10: Failed TVB=%s guint32 @ -4 %u != expected %u\n",
 					name, val32, expected32);
+			failed = TRUE;
 			return FALSE;
 		}
 	}
@@ -206,6 +218,7 @@
 				printf("11: Failed TVB=%s Offset=%d Length=%d "
 						"Bad memdup\n",
 						name, i, incr);
+				failed = TRUE;
 				g_free(ptr);
 				return FALSE;
 			}
@@ -218,6 +231,7 @@
 	if (memcmp(ptr, expected_data, length) != 0) {
 		printf("12: Failed TVB=%s Offset=0 Length=-1 "
 				"Bad memdup\n", name);
+		failed = TRUE;
 		g_free(ptr);
 		return FALSE;
 	}
@@ -402,5 +416,5 @@
 	run_tests();
 	tvbuff_cleanup();
 	except_deinit();
-	exit(0);
+	exit(failed?1:0);
 }
Index: test/suite-unittests.sh
===================================================================
--- test/suite-unittests.sh	(revision 0)
+++ test/suite-unittests.sh	(revision 0)
@@ -0,0 +1,71 @@
+#!/bin/bash
+#
+# Run the epan unit tests
+#
+# $Id$
+#
+# Wireshark - Network traffic analyzer
+# By Gerald Combs <gerald@xxxxxxxxxxxxx>
+#
+# 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+# 
+
+unittests_step_test() {
+	make -C `dirname $DUT` `basename $DUT` >testout.txt 2>&1
+	if [ $? -ne 0 ]; then
+		echo
+		cat ./testout.txt
+		test_step_failed "make $DUT failed"
+		return
+	fi
+	
+	$DUT > testout.txt 2>&1
+	RETURNVALUE=$?
+	if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
+		echo
+		cat ./testout.txt
+		test_step_failed "exit status of $DUT: $RETURNVALUE"
+		return
+	fi
+	test_step_ok
+}
+
+
+unittests_step_exntest() {
+	DUT=../epan/exntest
+	unittests_step_test
+}
+
+unittests_step_reassemble_test() {
+	DUT=../epan/reassemble_test
+	unittests_step_test
+}
+
+unittests_step_tvbtest() {
+	DUT=../epan/tvbtest
+	unittests_step_test
+}
+
+unittests_cleanup_step() {
+	rm -f ./testout.txt
+}
+
+unittests_suite() {
+	test_step_set_pre unittests_cleanup_step
+	test_step_set_post unittests_cleanup_step
+	test_step_add "exntest" unittests_step_exntest
+	test_step_add "reassemble_test" unittests_step_reassemble_test
+	test_step_add "tvbtest" unittests_step_tvbtest
+}
Index: test/test.sh
===================================================================
--- test/test.sh	(revision 12064)
+++ test/test.sh	(working copy)
@@ -61,6 +61,7 @@
 source suite-clopts.sh
 source suite-io.sh
 source suite-capture.sh
+source suite-unittests.sh
 
 
 #check prerequisites
@@ -91,6 +92,7 @@
 	test_suite_add "Command line options" clopt_suite
 	test_suite_add "File I/O" io_suite
 	test_suite_add "Capture" capture_suite
+	test_suite_add "Unittests" unittests_suite
 }