소스 검색

libdpkg: Add new unit test for error handling

Guillem Jover 9 년 전
부모
커밋
cd08e06004
4개의 변경된 파일132개의 추가작업 그리고 0개의 파일을 삭제
  1. 1 0
      debian/changelog
  2. 1 0
      lib/dpkg/t/.gitignore
  3. 1 0
      lib/dpkg/t/Makefile.am
  4. 129 0
      lib/dpkg/t/t-ehandle.c

+ 1 - 0
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.

+ 1 - 0
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

+ 1 - 0
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 \

+ 129 - 0
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@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();
+}