[dpkg] 120/187: libdpkg: Add new unit test for error handling

Reiner Herrmann reiner at reiner-h.de
Sun Nov 6 12:46:31 UTC 2016


This is an automated email from the git hooks/post-receive script.

deki-guest pushed a commit to branch master
in repository dpkg.

commit cd08e06004d615073be86e8fe5b233ccf9d6c0ed
Author: Guillem Jover <guillem at debian.org>
Date:   Sat Sep 3 18:12:49 2016 +0200

    libdpkg: Add new unit test for error handling
---
 debian/changelog       |   1 +
 lib/dpkg/t/.gitignore  |   1 +
 lib/dpkg/t/Makefile.am |   1 +
 lib/dpkg/t/t-ehandle.c | 129 +++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 132 insertions(+)

diff --git a/debian/changelog b/debian/changelog
index 45f9b24..86188cd 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -143,6 +143,7 @@ dpkg (1.18.11) UNRELEASED; urgency=medium
     - Add new dpkg-buildpackage functional tests.
     - Add an initial functional test suite for dpkg-deb and dpkg-split.
     - Skip the involved tests if IO::String is missing.
+    - Add new unit test for libdpkg error handling.
   * Build system:
     - Add support for profiling perl modules.
     - Clean up compiler and linker automatic flag usage in configure.
diff --git a/lib/dpkg/t/.gitignore b/lib/dpkg/t/.gitignore
index 3a2b4d3..d2b628b 100644
--- a/lib/dpkg/t/.gitignore
+++ b/lib/dpkg/t/.gitignore
@@ -7,6 +7,7 @@ t-buffer
 t-c-ctype
 t-command
 t-deb-version
+t-ehandle
 t-error
 t-macros
 t-mod-db
diff --git a/lib/dpkg/t/Makefile.am b/lib/dpkg/t/Makefile.am
index 034f3f1..db449d6 100644
--- a/lib/dpkg/t/Makefile.am
+++ b/lib/dpkg/t/Makefile.am
@@ -23,6 +23,7 @@ test_programs = \
 	t-test-skip \
 	t-macros \
 	t-c-ctype \
+	t-ehandle \
 	t-error \
 	t-string \
 	t-buffer \
diff --git a/lib/dpkg/t/t-ehandle.c b/lib/dpkg/t/t-ehandle.c
new file mode 100644
index 0000000..e2b364c
--- /dev/null
+++ b/lib/dpkg/t/t-ehandle.c
@@ -0,0 +1,129 @@
+/*
+ * libdpkg - Debian packaging suite library routines
+ * t-ehandle.c - test error handling implementation
+ *
+ * Copyright © 2016 Guillem Jover <guillem at debian.org>
+ *
+ * This 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 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+#include <compat.h>
+
+#include <stdbool.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include <dpkg/test.h>
+#include <dpkg/ehandle.h>
+
+jmp_buf global_handler_jump;
+
+static void
+printer_empty(const char *msg, const void *data)
+{
+}
+
+static void
+handler_func(void)
+{
+	longjmp(global_handler_jump, 1);
+}
+
+static void
+test_error_handler_func(void)
+{
+	bool pass;
+
+	if (setjmp(global_handler_jump)) {
+		pass = true;
+		pop_error_context(ehflag_normaltidy);
+	} else {
+		pass = false;
+		push_error_context_func(handler_func, printer_empty, "test func");
+		ohshit("test func error");
+		test_bail("ohshit() is not supposed to return");
+	}
+	test_pass(pass);
+}
+
+static void
+test_error_handler_jump(void)
+{
+	jmp_buf handler_jump;
+	bool pass;
+
+	if (setjmp(handler_jump)) {
+		pass = true;
+		pop_error_context(ehflag_normaltidy);
+	} else {
+		pass = false;
+		push_error_context_jump(&handler_jump, printer_empty, "test jump");
+		ohshit("test jump error");
+		test_bail("ohshit() is not supposed to return");
+	}
+	test_pass(pass);
+}
+
+static void
+cleanup_error(int argc, void **argv)
+{
+	ohshit("cleanup error");
+}
+
+static void
+test_cleanup_error(void)
+{
+	jmp_buf handler_jump;
+	bool pass;
+
+	if (setjmp(handler_jump)) {
+		/* The ohshit() is not supposed to get us here, as it should
+		 * be catched by the internal recursive error context. */
+		pass = false;
+
+		pop_cleanup(ehflag_normaltidy);
+		pop_error_context(ehflag_normaltidy);
+	} else {
+		/* Mark any error before this as not passing. */
+		pass = false;
+
+		push_error_context_jump(&handler_jump, printer_empty, "test cleanup");
+		push_cleanup(cleanup_error, ~ehflag_normaltidy, NULL, 0, 0);
+		pop_error_context(ehflag_bombout);
+
+		/* We should have recovered from the cleanup handler failing,
+		 * and arrived here corerctly. */
+		pass = true;
+	}
+
+	test_pass(pass);
+}
+
+TEST_ENTRY(test)
+{
+	int fd;
+
+	test_plan(3);
+
+	/* XXX: Shut up stderr, we don't want the error output. */
+	fd = open("/dev/null", O_RDWR);
+	if (fd < 0)
+		test_bail("cannot open /dev/null");
+	dup2(fd, 2);
+
+	test_error_handler_func();
+	test_error_handler_jump();
+	test_cleanup_error();
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/dpkg.git



More information about the Reproducible-commits mailing list