Forráskód Böngészése

libdpkg: Add new mod-db, progname and subproc modules test cases

Guillem Jover 15 éve
szülő
commit
b6b23a8259

+ 3 - 0
lib/dpkg/test/.gitignore

@@ -2,11 +2,14 @@ t-ar
 t-buffer
 t-command
 t-macros
+t-mod-db
 t-path
 t-pkginfo
 t-pkg-list
 t-pkg-queue
+t-progname
 t-string
+t-subproc
 t-test
 t-varbuf
 t-version

+ 5 - 1
lib/dpkg/test/Makefile.am

@@ -1,6 +1,7 @@
 ## Process this file with automake to produce Makefile.in
 
 AM_CPPFLAGS = \
+	-DADMINDIR=\"$(admindir)\" \
 	-idirafter $(top_srcdir)/lib/compat \
 	-I$(top_builddir) \
 	-I$(top_srcdir)/lib
@@ -14,12 +15,15 @@ check_PROGRAMS = \
 	t-string \
 	t-buffer \
 	t-path \
+	t-progname \
+	t-subproc \
 	t-command \
 	t-varbuf \
 	t-ar \
 	t-version \
 	t-pkginfo \
 	t-pkg-list \
-	t-pkg-queue
+	t-pkg-queue \
+	t-mod-db
 
 TESTS = $(check_PROGRAMS)

+ 56 - 0
lib/dpkg/test/t-mod-db.c

@@ -0,0 +1,56 @@
+/*
+ * libdpkg - Debian packaging suite library routines
+ * t-db.c - test database implementation
+ *
+ * Copyright © 2011 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 <stdlib.h>
+
+#include <dpkg/test.h>
+#include <dpkg/dpkg-db.h>
+
+static void
+test_db_dir(void)
+{
+	char *dir;
+
+	test_str(dpkg_db_get_dir(), ==, ADMINDIR);
+
+	dpkg_db_set_dir("testdir");
+	test_str(dpkg_db_get_dir(), ==, "testdir");
+
+	setenv("DPKG_ADMINDIR", "testenvdir", 1);
+	dpkg_db_set_dir(NULL);
+	test_str(dpkg_db_get_dir(), ==, "testenvdir");
+
+	unsetenv("DPKG_ADMINDIR");
+	dpkg_db_set_dir(NULL);
+	test_str(dpkg_db_get_dir(), ==, ADMINDIR);
+
+	dir = dpkg_db_get_path("testfile");
+	test_str(dir, ==, ADMINDIR "/testfile");
+	free(dir);
+}
+
+static void
+test(void)
+{
+	test_db_dir();
+}

+ 46 - 0
lib/dpkg/test/t-progname.c

@@ -0,0 +1,46 @@
+/*
+ * libdpkg - Debian packaging suite library routines
+ * t-progname.c - test program name handling
+ *
+ * Copyright © 2011 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 <dpkg/test.h>
+#include <dpkg/progname.h>
+
+static void
+test_progname(void)
+{
+	/* Test initially empty progname. */
+	test_str(dpkg_get_progname(), ==, "t-progname");
+
+	/* Test setting a new progname. */
+	dpkg_set_progname("newname");
+	test_str(dpkg_get_progname(), ==, "newname");
+
+	/* Test setting a new progname with path. */
+	dpkg_set_progname("path/newprogname");
+	test_str(dpkg_get_progname(), ==, "newprogname");
+}
+
+static void
+test(void)
+{
+	test_progname();
+}

+ 91 - 0
lib/dpkg/test/t-subproc.c

@@ -0,0 +1,91 @@
+/*
+ * libdpkg - Debian packaging suite library routines
+ * t-subproc.c - test sub-process module
+ *
+ * Copyright © 2011 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 <sys/types.h>
+
+#include <fcntl.h>
+#include <signal.h>
+#include <unistd.h>
+#include <stdlib.h>
+
+#include <dpkg/test.h>
+#include <dpkg/subproc.h>
+
+static void
+test_subproc_fork(void)
+{
+	pid_t pid;
+	int ret;
+
+	/* Test exit(). */
+	pid = subproc_fork();
+	if (pid == 0)
+		exit(0);
+	ret = subproc_wait_check(pid, "subproc exit pass", PROCNOERR);
+	test_pass(ret == 0);
+
+	pid = subproc_fork();
+	if (pid == 0)
+		exit(128);
+	ret = subproc_wait_check(pid, "subproc exit fail", PROCNOERR);
+	test_pass(ret == 128);
+
+	/* Test signals. */
+	pid = subproc_fork();
+	if (pid == 0)
+		raise(SIGINT);
+	ret = subproc_wait_check(pid, "subproc signal", PROCWARN);
+	test_pass(ret == -1);
+
+	pid = subproc_fork();
+	if (pid == 0)
+		raise(SIGTERM);
+	ret = subproc_wait_check(pid, "subproc signal", PROCWARN);
+	test_pass(ret == -1);
+
+	pid = subproc_fork();
+	if (pid == 0)
+		raise(SIGPIPE);
+	ret = subproc_wait_check(pid, "subproc SIGPIPE", PROCWARN | PROCPIPE);
+	test_pass(ret == 0);
+
+	pid = subproc_fork();
+	if (pid == 0)
+		raise(SIGPIPE);
+	ret = subproc_wait_check(pid, "subproc SIGPIPE", PROCWARN);
+	test_pass(ret == -1);
+}
+
+static void
+test(void)
+{
+	int fd;
+
+	/* XXX: Shut up output, we just want the error code. */
+	fd = open("/dev/null", O_RDWR);
+	test_pass(fd >= 0);
+	dup2(fd, 1);
+	dup2(fd, 2);
+
+	test_subproc_fork();
+}