PolicyKit: Branch 'master'

David Zeuthen david at kemper.freedesktop.org
Sun Nov 11 17:11:36 PST 2007


 Makefile.am                                  |    4 +
 doc/polkit-docs.xml                          |    1 
 src/kit/Makefile.am                          |    5 -
 src/kit/kit-memory.c                         |    1 
 src/kit/kit-test-main.c                      |   47 +++++++++++++++
 src/kit/kit-test.c                           |   60 +++++++++-----------
 src/kit/kit-test.h                           |   13 +++-
 src/kit/kit.h                                |    1 
 src/polkit-dbus/Makefile.am                  |   52 +++++++++++++++++
 src/polkit-dbus/polkit-dbus-test.c           |   59 +++++++++++++++++++
 src/polkit-dbus/polkit-dbus-test.h           |   43 ++++++++++++++
 src/polkit-dbus/polkit-dbus.c                |   19 ++++++
 src/polkit-grant/Makefile.am                 |   58 +++++++++++++++++++
 src/polkit-grant/polkit-grant-test.c         |   59 +++++++++++++++++++
 src/polkit-grant/polkit-grant-test.h         |   43 ++++++++++++++
 src/polkit-grant/polkit-grant.c              |   18 ++++++
 src/polkit/Makefile.am                       |    2 
 src/polkit/polkit-action.c                   |    2 
 src/polkit/polkit-authorization-constraint.c |    2 
 src/polkit/polkit-authorization-db-dummy.c   |    2 
 src/polkit/polkit-authorization-db.c         |    2 
 src/polkit/polkit-authorization.c            |    2 
 src/polkit/polkit-caller.c                   |    2 
 src/polkit/polkit-config.c                   |    2 
 src/polkit/polkit-context.c                  |    2 
 src/polkit/polkit-error.c                    |    2 
 src/polkit/polkit-policy-cache.c             |    2 
 src/polkit/polkit-policy-default.c           |    2 
 src/polkit/polkit-policy-file-entry.c        |    2 
 src/polkit/polkit-policy-file.c              |    2 
 src/polkit/polkit-result.c                   |    2 
 src/polkit/polkit-seat.c                     |    2 
 src/polkit/polkit-session.c                  |    2 
 src/polkit/polkit-sysdeps.c                  |    2 
 src/polkit/polkit-test.c                     |   81 +--------------------------
 src/polkit/polkit-test.h                     |   49 +++++-----------
 test/create-coverage-report.sh               |   38 +++++++-----
 37 files changed, 501 insertions(+), 186 deletions(-)

New commits:
commit 4b340f87709d93904eb4868384b555227e656317
Author: David Zeuthen <davidz at redhat.com>
Date:   Sun Nov 11 20:08:02 2007 -0500

    add unit test framework to the rest of the library sources

diff --git a/Makefile.am b/Makefile.am
index 492a1bf..7473e23 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -24,7 +24,9 @@ if POLKIT_GCOV_ENABLED
 coverage-report.txt :
 	make -C src/kit coverage-report.txt
 	make -C src/polkit coverage-report.txt
-	$(top_srcdir)/test/create-coverage-report.sh "PolicyKit" `cat src/kit/covered-files.txt src/polkit/covered-files.txt` > coverage-report.txt
+	make -C src/polkit-dbus coverage-report.txt
+	make -C src/polkit-grant coverage-report.txt
+	$(top_srcdir)/test/create-coverage-report.sh "PolicyKit" `cat src/kit/covered-files.txt src/polkit/covered-files.txt src/polkit-dbus/covered-files.txt src/polkit-grant/covered-files.txt` > coverage-report.txt
 
 check-coverage: coverage-report.txt
 	cat coverage-report.txt
diff --git a/doc/polkit-docs.xml b/doc/polkit-docs.xml
index ce1536c..5e54e02 100644
--- a/doc/polkit-docs.xml
+++ b/doc/polkit-docs.xml
@@ -85,6 +85,7 @@
     <xi:include href="xml/kit-hash.xml"/>
     <xi:include href="xml/kit-file.xml"/>
     <xi:include href="xml/kit-spawn.xml"/>
+    <xi:include href="xml/kit-test.xml"/>
   </reference>
 
   <reference id="ref-core">
diff --git a/src/kit/Makefile.am b/src/kit/Makefile.am
index 217a585..1145f50 100644
--- a/src/kit/Makefile.am
+++ b/src/kit/Makefile.am
@@ -20,6 +20,7 @@ noinst_LTLIBRARIES=libkit.la
 
 libkit_la_SOURCES =					\
 	kit.h						\
+	kit-test.h		kit-test.c		\
 	kit-memory.h		kit-memory.c		\
 	kit-string.h		kit-string.c		\
 	kit-list.h		kit-list.c		\
@@ -69,12 +70,10 @@ endif
 noinst_PROGRAMS=$(TESTS)
 
 kit_test_SOURCES=				\
-	kit-test.h		kit-test.c
+	kit-test-main.c
 
 kit_test_LDADD=$(top_builddir)/src/kit/libkit.la
 kit_test_LDFLAGS=
-#@R_DYNAMIC_LDFLAG@
-
 
 clean-local :
 	rm -f *~ *.bb *.bbg *.da *.gcov .libs/*.da .libs/*.bbg 
diff --git a/src/kit/kit-memory.c b/src/kit/kit-memory.c
index 397245c..e1a79d9 100644
--- a/src/kit/kit-memory.c
+++ b/src/kit/kit-memory.c
@@ -44,7 +44,6 @@
  * Functions used for memory management.
  **/
 
-
 #ifdef KIT_BUILD_TESTS
 
 static int _cur_allocs = 0;
diff --git a/src/kit/kit-test-main.c b/src/kit/kit-test-main.c
new file mode 100644
index 0000000..83dddae
--- /dev/null
+++ b/src/kit/kit-test-main.c
@@ -0,0 +1,47 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
+/***************************************************************************
+ *
+ * kit-test-main.c : Run test suites for libkit
+ *
+ * Copyright (C) 2007 David Zeuthen, <david at fubar.dk>
+ *
+ * Licensed under the Academic Free License version 2.1
+ *
+ * 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 St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ **************************************************************************/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <kit/kit-test.h>
+
+static KitTest *tests[] = {
+        &_test_message,
+        &_test_memory,
+        &_test_string,
+        &_test_list,
+        &_test_hash,
+        &_test_file,
+        &_test_spawn,
+};
+
+int 
+main (int argc, char *argv[])
+{
+        if (kit_test_run (tests, sizeof (tests) / sizeof (KitTest*)))
+                return 0;
+        else
+                return 1;
+}
diff --git a/src/kit/kit-test.c b/src/kit/kit-test.c
index 9299bcf..b2c162a 100644
--- a/src/kit/kit-test.c
+++ b/src/kit/kit-test.c
@@ -28,39 +28,35 @@
 #include <kit/kit-test.h>
 #include <kit/kit-memory.h>
 
-#define MAX_TESTS 64
-
 /**
  * SECTION:kit-test
- * @short_description: Testing code for libkit
+ * @title: Unit testing
+ * @short_description: Unit testing
  *
- * Testing code for libkit.
+ * Functions used for unit testing.
  */
 
-static KitTest *tests[] = {
-        &_test_message,
-        &_test_memory,
-        &_test_string,
-        &_test_list,
-        &_test_hash,
-        &_test_file,
-        &_test_spawn,
-};
-
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-
-int 
-main (int argc, char *argv[])
+/**
+ * kit_test_run:
+ * @tests: array of #KitTest objects
+ * @num_tests: size of @tests array
+ *
+ * Runs a number of tests simulating Out Of Memory. Checks for both
+ * memory and file descriptor leaks. 
+ *
+ * This function is only available if libkit have been built with
+ * KIT_BUILD_TESTS.
+ *
+ * Returns: %TRUE only if all tests succeed without memory or file descriptor leaks
+ */
+kit_bool_t
+kit_test_run (KitTest **tests, size_t num_tests)
 {
-        int ret;
-        int n;
-        int num_tests;
-
-        ret = 0;
+        kit_bool_t ret;
+        unsigned int n;
 
-        num_tests = sizeof (tests) / sizeof (KitTest*);
+        /* be optimistic! */
+        ret = TRUE;
 
         printf ("Running %d unit tests\n", num_tests);
         for (n = 0; n < num_tests; n++) {
@@ -80,7 +76,7 @@ main (int argc, char *argv[])
                 printf ("Running: %s\n", test->name);
                 if (!test->run ()) {
                         printf ("Failed\n");
-                        ret = 1;
+                        ret = FALSE;
                         goto test_done;
                 }
                 num_fd_after = _kit_get_num_fd ();
@@ -91,11 +87,11 @@ main (int argc, char *argv[])
                 delta = _kit_memory_get_current_allocations ();
                 if (delta != 0) {
                         printf ("  Unit test leaked %d allocations\n", delta);
-                        ret = 1;
+                        ret = FALSE;
                 }
                 if (num_fd != num_fd_after) {
                         printf ("  Unit test leaked %d file descriptors\n", num_fd_after - num_fd);
-                        ret = 1;
+                        ret = FALSE;
                 }
                 
                 for (m = 0; m < total_allocs; m++) {
@@ -107,7 +103,7 @@ main (int argc, char *argv[])
                         num_fd = _kit_get_num_fd ();
                         if (!test->run ()) {
                                 printf ("  Failed\n");
-                                ret = 1;
+                                ret = FALSE;
                                 continue;
                         }
                         num_fd_after = _kit_get_num_fd ();
@@ -115,11 +111,11 @@ main (int argc, char *argv[])
                         delta = _kit_memory_get_current_allocations ();
                         if (delta != 0) {
                                 printf ("  Unit test leaked %d allocations\n", delta);
-                                ret = 1;
+                                ret = FALSE;
                         }
                         if (num_fd != num_fd_after) {
                                 printf ("  Unit test leaked %d file descriptors\n", num_fd_after - num_fd);
-                                ret = 1;
+                                ret = FALSE;
                         }
 
                 }
diff --git a/src/kit/kit-test.h b/src/kit/kit-test.h
index 8c12b2e..439add5 100644
--- a/src/kit/kit-test.h
+++ b/src/kit/kit-test.h
@@ -23,8 +23,8 @@
  *
  **************************************************************************/
 
-#if !defined (KIT_COMPILATION)
-#error "kit-test.h is a private file."
+#if !defined (KIT_COMPILATION) && !defined(_KIT_INSIDE_KIT_H)
+#error "Only <kit/kit.h> can be included directly, this file may disappear or change contents."
 #endif
 
 #ifndef KIT_TEST_H
@@ -36,17 +36,22 @@ KIT_BEGIN_DECLS
 
 /**
  * KitTest:
+ * @name: name of the unit test
+ * @setup: setup function
+ * @teardown: teardown function
+ * @run: actual test function.
  *
- * Test suite abstraction.
+ * Test suite abstraction. See kit_test_run() for details.
  */
 typedef struct {
-        /*< private >*/
         const char *name;
         void (*setup) (void);
         void (*teardown) (void);
         kit_bool_t (*run) (void);
 } KitTest;
 
+kit_bool_t kit_test_run (KitTest **tests, size_t num_tests);
+
 extern KitTest _test_memory;
 extern KitTest _test_string;
 extern KitTest _test_hash;
diff --git a/src/kit/kit.h b/src/kit/kit.h
index 77da0d9..324cb67 100644
--- a/src/kit/kit.h
+++ b/src/kit/kit.h
@@ -134,6 +134,7 @@ do {
 #include <kit/kit-file.h>
 #include <kit/kit-spawn.h>
 #include <kit/kit-message.h>
+#include <kit/kit-test.h>
 
 #undef _KIT_INSIDE_KIT_H
 
diff --git a/src/polkit-dbus/Makefile.am b/src/polkit-dbus/Makefile.am
index 7e0da48..813d932 100644
--- a/src/polkit-dbus/Makefile.am
+++ b/src/polkit-dbus/Makefile.am
@@ -43,5 +43,55 @@ install-exec-hook:
 	-chmod 2755 $(DESTDIR)$(libexecdir)/polkit-read-auth-helper
 endif
 
+## note that TESTS has special meaning (stuff to use in make check)
+## so if adding tests not to be run in make check, don't add them to 
+## TESTS
+if KIT_BUILD_TESTS
+TESTS_ENVIRONMENT=
+TESTS=polkit-dbus-test
+
+if KIT_GCOV_ENABLED
+clean-gcov:
+	rm -f *.gcov .libs/*.gcda
+
+.PHONY: coverage-report.txt covered-files.txt
+
+covered-files.txt :
+	echo $(addprefix src/polkit-dbus/,$(filter %.c,$(libpolkit_dbus_la_SOURCES))) > covered-files.txt
+if POLKIT_AUTHDB_DEFAULT
+	echo src/polkit-dbus/polkit-read-auth-helper.c >> covered-files.txt
+endif
+
+coverage-report.txt : covered-files.txt clean-gcov all check 
+	gcov $(filter %.c,$(libpolkit_dbus_la_SOURCES)) -o .libs/ > /dev/null
+if POLKIT_AUTHDB_DEFAULT
+	gcov polkit-read-auth-helper.c -o .libs/ > /dev/null
+endif
+	$(top_srcdir)/test/create-coverage-report.sh "module polkit-dbus" `cat covered-files.txt` > coverage-report.txt
+
+check-coverage : coverage-report.txt
+	cat coverage-report.txt
+else
+coverage-report.txt:
+	@echo "Need to reconfigure with --enable-gcov"
+
+check-coverage:
+	@echo "Need to reconfigure with --enable-gcov"
+endif
+
+else
+TESTS=
+endif
+
+## we use noinst_PROGRAMS not check_PROGRAMS so that we build 
+## even when not doing "make check"
+noinst_PROGRAMS=$(TESTS)
+
+polkit_dbus_test_SOURCES=				\
+	polkit-dbus-test.h	polkit-dbus-test.c
+
+polkit_dbus_test_LDADD=$(top_builddir)/src/polkit-dbus/libpolkit-dbus.la
+polkit_dbus_test_LDFLAGS=
+
 clean-local :
-	rm -f *~ $(BUILT_SOURCES)
+	rm -f *~ *.bb *.bbg *.da *.gcov .libs/*.da .libs/*.bbg 
diff --git a/src/polkit-dbus/polkit-dbus-test.c b/src/polkit-dbus/polkit-dbus-test.c
new file mode 100644
index 0000000..d847c31
--- /dev/null
+++ b/src/polkit-dbus/polkit-dbus-test.c
@@ -0,0 +1,59 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
+/***************************************************************************
+ *
+ * polkit-dbus-test.c : polkit-dbus tests
+ *
+ * Copyright (C) 2007 David Zeuthen, <david at fubar.dk>
+ *
+ * Licensed under the Academic Free License version 2.1
+ *
+ * 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 St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ **************************************************************************/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <syslog.h>
+#include <polkit/polkit-private.h>
+#include <polkit-dbus/polkit-dbus-test.h>
+
+#define MAX_TESTS 64
+
+/**
+ * SECTION:polkit-dbus-test
+ * @short_description: Testing code for libpolkit-dbus
+ *
+ * Testing code for libpolkit-dbus
+ */
+
+static KitTest *tests[] = {
+        &_test_polkit_dbus,
+};
+
+int 
+main (int argc, char *argv[])
+{
+        /* Some of the code will log to syslog because .policy files
+         * etc. may be malformed. Since this will open a socket to the
+         * system logger preempt this so the fd-leak checking don't
+         * freak out.
+         */
+        syslog (LOG_INFO, "libpolkit-dbus: initiating test; bogus alerts may be written to syslog");
+
+        if (kit_test_run (tests, sizeof (tests) / sizeof (KitTest*)))
+                return 0;
+        else
+                return 1;
+}
diff --git a/src/polkit-dbus/polkit-dbus-test.h b/src/polkit-dbus/polkit-dbus-test.h
new file mode 100644
index 0000000..828acae
--- /dev/null
+++ b/src/polkit-dbus/polkit-dbus-test.h
@@ -0,0 +1,43 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
+/***************************************************************************
+ *
+ * polkit-dbus-test.h : polkit-dbus tests
+ *
+ * Copyright (C) 2007 David Zeuthen, <david at fubar.dk>
+ *
+ * Licensed under the Academic Free License version 2.1
+ *
+ * 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 St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ **************************************************************************/
+
+#if !defined (POLKIT_COMPILATION)
+#error "polkit-dbus-test.h is a private file"
+#endif
+
+#ifndef POLKIT_DBUS_TEST_H
+#define POLKIT_DBUS_TEST_H
+
+#include <kit/kit.h>
+
+POLKIT_BEGIN_DECLS
+
+extern KitTest _test_polkit_dbus;
+
+POLKIT_END_DECLS
+
+#endif /* POLKIT_DBUS_TEST_H */
+
+
diff --git a/src/polkit-dbus/polkit-dbus.c b/src/polkit-dbus/polkit-dbus.c
index 9ead04a..cb102e9 100644
--- a/src/polkit-dbus/polkit-dbus.c
+++ b/src/polkit-dbus/polkit-dbus.c
@@ -65,6 +65,8 @@
 
 #include "polkit-dbus.h"
 #include <polkit/polkit-debug.h>
+#include <polkit/polkit-test.h>
+#include <polkit/polkit-private.h>
 
 /**
  * polkit_session_new_from_objpath:
@@ -1490,3 +1492,20 @@ polkit_tracker_is_authorization_relevant (PolKitTracker *pk_tracker, PolKitAutho
          */
         return _polkit_is_authorization_relevant_internal (pk_tracker->con, auth, NULL, error);
 }
+
+#ifdef POLKIT_BUILD_TESTS
+
+static polkit_bool_t
+_run_test (void)
+{
+        return TRUE;
+}
+
+KitTest _test_polkit_dbus = {
+        "polkit_dbus",
+        NULL,
+        NULL,
+        _run_test
+};
+
+#endif /* POLKIT_BUILD_TESTS */
diff --git a/src/polkit-grant/Makefile.am b/src/polkit-grant/Makefile.am
index ed7a5f7..12dec1c 100644
--- a/src/polkit-grant/Makefile.am
+++ b/src/polkit-grant/Makefile.am
@@ -85,5 +85,61 @@ install-exec-hook:
 	-chmod 2755 $(DESTDIR)$(libexecdir)/polkit-revoke-helper
 endif
 
+## note that TESTS has special meaning (stuff to use in make check)
+## so if adding tests not to be run in make check, don't add them to 
+## TESTS
+if KIT_BUILD_TESTS
+TESTS_ENVIRONMENT=
+TESTS=polkit-grant-test
+
+if KIT_GCOV_ENABLED
+clean-gcov:
+	rm -f *.gcov .libs/*.gcda
+
+.PHONY: coverage-report.txt covered-files.txt
+
+covered-files.txt :
+	echo $(addprefix src/polkit-grant/,$(filter %.c,$(libpolkit_grant_la_SOURCES))) > covered-files.txt
+if POLKIT_AUTHDB_DEFAULT
+	echo src/polkit-grant/polkit-explicit-grant-helper.c >> covered-files.txt
+	echo src/polkit-grant/polkit-grant-helper.c >> covered-files.txt
+	echo src/polkit-grant/polkit-grant-helper-pam.c >> covered-files.txt
+	echo src/polkit-grant/polkit-revoke-helper.c >> covered-files.txt
+endif
+
+coverage-report.txt : covered-files.txt clean-gcov all check 
+	gcov $(filter %.c,$(libpolkit_grant_la_SOURCES)) -o .libs/ > /dev/null
+if POLKIT_AUTHDB_DEFAULT
+	gcov polkit-explicit-grant-helper.c -o .libs/ > /dev/null
+	gcov polkit-grant-helper.c -o .libs/ > /dev/null
+	gcov polkit-grant-helper-pam.c -o .libs/ > /dev/null
+	gcov polkit-revoke-helper.c -o .libs/ > /dev/null
+endif
+	$(top_srcdir)/test/create-coverage-report.sh "module polkit-grant" `cat covered-files.txt` > coverage-report.txt
+
+check-coverage : coverage-report.txt
+	cat coverage-report.txt
+else
+coverage-report.txt:
+	@echo "Need to reconfigure with --enable-gcov"
+
+check-coverage:
+	@echo "Need to reconfigure with --enable-gcov"
+endif
+
+else
+TESTS=
+endif
+
+## we use noinst_PROGRAMS not check_PROGRAMS so that we build 
+## even when not doing "make check"
+noinst_PROGRAMS=$(TESTS)
+
+polkit_grant_test_SOURCES=				\
+	polkit-grant-test.h	polkit-grant-test.c
+
+polkit_grant_test_LDADD=$(top_builddir)/src/polkit-grant/libpolkit-grant.la
+polkit_grant_test_LDFLAGS=
+
 clean-local :
-	rm -f *~ $(BUILT_SOURCES)
+	rm -f *~ *.bb *.bbg *.da *.gcov .libs/*.da .libs/*.bbg 
diff --git a/src/polkit-grant/polkit-grant-test.c b/src/polkit-grant/polkit-grant-test.c
new file mode 100644
index 0000000..d7a4e06
--- /dev/null
+++ b/src/polkit-grant/polkit-grant-test.c
@@ -0,0 +1,59 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
+/***************************************************************************
+ *
+ * polkit-grant-test.c : polkit-grant tests
+ *
+ * Copyright (C) 2007 David Zeuthen, <david at fubar.dk>
+ *
+ * Licensed under the Academic Free License version 2.1
+ *
+ * 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 St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ **************************************************************************/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <syslog.h>
+#include <polkit/polkit-private.h>
+#include <polkit-grant/polkit-grant-test.h>
+
+#define MAX_TESTS 64
+
+/**
+ * SECTION:polkit-grant-test
+ * @short_description: Testing code for libpolkit-grant
+ *
+ * Testing code for libpolkit-grant
+ */
+
+static KitTest *tests[] = {
+        &_test_polkit_grant,
+};
+
+int 
+main (int argc, char *argv[])
+{
+        /* Some of the code will log to syslog because .policy files
+         * etc. may be malformed. Since this will open a socket to the
+         * system logger preempt this so the fd-leak checking don't
+         * freak out.
+         */
+        syslog (LOG_INFO, "libpolkit-grant: initiating test; bogus alerts may be written to syslog");
+
+        if (kit_test_run (tests, sizeof (tests) / sizeof (KitTest*)))
+                return 0;
+        else
+                return 1;
+}
diff --git a/src/polkit-grant/polkit-grant-test.h b/src/polkit-grant/polkit-grant-test.h
new file mode 100644
index 0000000..82f53f5
--- /dev/null
+++ b/src/polkit-grant/polkit-grant-test.h
@@ -0,0 +1,43 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
+/***************************************************************************
+ *
+ * polkit-grant-test.h : polkit-grant tests
+ *
+ * Copyright (C) 2007 David Zeuthen, <david at fubar.dk>
+ *
+ * Licensed under the Academic Free License version 2.1
+ *
+ * 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 St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ **************************************************************************/
+
+#if !defined (POLKIT_COMPILATION)
+#error "polkit-dbus-test.h is a private file"
+#endif
+
+#ifndef POLKIT_GRANT_TEST_H
+#define POLKIT_GRANT_TEST_H
+
+#include <kit/kit.h>
+
+POLKIT_BEGIN_DECLS
+
+extern KitTest _test_polkit_grant;
+
+POLKIT_END_DECLS
+
+#endif /* POLKIT_GRANT_TEST_H */
+
+
diff --git a/src/polkit-grant/polkit-grant.c b/src/polkit-grant/polkit-grant.c
index 0e7a43d..191bdde 100644
--- a/src/polkit-grant/polkit-grant.c
+++ b/src/polkit-grant/polkit-grant.c
@@ -37,6 +37,7 @@
 
 #include <glib.h>
 #include "polkit-grant.h"
+#include "polkit-grant-test.h"
 
 /**
  * SECTION:polkit-grant
@@ -536,3 +537,20 @@ polkit_grant_initiate_auth (PolKitGrant  *polkit_grant,
 error:
         return FALSE;
 }
+
+#ifdef POLKIT_BUILD_TESTS
+
+static polkit_bool_t
+_run_test (void)
+{
+        return TRUE;
+}
+
+KitTest _test_polkit_grant = {
+        "polkit_grant",
+        NULL,
+        NULL,
+        _run_test
+};
+
+#endif /* POLKIT_BUILD_TESTS */
diff --git a/src/polkit/Makefile.am b/src/polkit/Makefile.am
index 54d89e4..61f1f0d 100644
--- a/src/polkit/Makefile.am
+++ b/src/polkit/Makefile.am
@@ -117,8 +117,6 @@ polkit_test_SOURCES=				\
 
 polkit_test_LDADD=$(top_builddir)/src/polkit/libpolkit.la
 polkit_test_LDFLAGS=
-#@R_DYNAMIC_LDFLAG@
-
 
 clean-local :
 	rm -f *~ $(BUILT_SOURCES) *.bb *.bbg *.da *.gcov .libs/*.da .libs/*.bbg 
diff --git a/src/polkit/polkit-action.c b/src/polkit/polkit-action.c
index bc2c333..2994aa8 100644
--- a/src/polkit/polkit-action.c
+++ b/src/polkit/polkit-action.c
@@ -294,7 +294,7 @@ _run_test (void)
         return TRUE;
 }
 
-PolKitTest _test_action = {
+KitTest _test_action = {
         "polkit_action",
         NULL,
         NULL,
diff --git a/src/polkit/polkit-authorization-constraint.c b/src/polkit/polkit-authorization-constraint.c
index 5e0c98d..22069d5 100644
--- a/src/polkit/polkit-authorization-constraint.c
+++ b/src/polkit/polkit-authorization-constraint.c
@@ -731,7 +731,7 @@ _run_test (void)
 }
 
 
-PolKitTest _test_authorization_constraint = {
+KitTest _test_authorization_constraint = {
         "polkit_authorization_constraint",
         NULL,
         NULL,
diff --git a/src/polkit/polkit-authorization-db-dummy.c b/src/polkit/polkit-authorization-db-dummy.c
index 5bc3822..536b395 100644
--- a/src/polkit/polkit-authorization-db-dummy.c
+++ b/src/polkit/polkit-authorization-db-dummy.c
@@ -197,7 +197,7 @@ _run_test (void)
         return TRUE;
 }
 
-PolKitTest _test_authorization_db = {
+KitTest _test_authorization_db = {
         "polkit_authorization_db",
         NULL,
         NULL,
diff --git a/src/polkit/polkit-authorization-db.c b/src/polkit/polkit-authorization-db.c
index 99b6d64..3577294 100644
--- a/src/polkit/polkit-authorization-db.c
+++ b/src/polkit/polkit-authorization-db.c
@@ -909,7 +909,7 @@ fail:
 }
 
 
-PolKitTest _test_authorization_db = {
+KitTest _test_authorization_db = {
         "polkit_authorization_db",
         NULL,
         NULL,
diff --git a/src/polkit/polkit-authorization.c b/src/polkit/polkit-authorization.c
index d4121d5..265d37f 100644
--- a/src/polkit/polkit-authorization.c
+++ b/src/polkit/polkit-authorization.c
@@ -782,7 +782,7 @@ _run_test (void)
 
 #endif /* POLKIT_AUTHDB_DEFAULT */
 
-PolKitTest _test_authorization = {
+KitTest _test_authorization = {
         "polkit_authorization",
         NULL,
         NULL,
diff --git a/src/polkit/polkit-caller.c b/src/polkit/polkit-caller.c
index faf0a54..b8ac256 100644
--- a/src/polkit/polkit-caller.c
+++ b/src/polkit/polkit-caller.c
@@ -444,7 +444,7 @@ _run_test (void)
         return TRUE;
 }
 
-PolKitTest _test_caller = {
+KitTest _test_caller = {
         "polkit_caller",
         NULL,
         NULL,
diff --git a/src/polkit/polkit-config.c b/src/polkit/polkit-config.c
index e707c11..985e283 100644
--- a/src/polkit/polkit-config.c
+++ b/src/polkit/polkit-config.c
@@ -775,7 +775,7 @@ _run_test (void)
         return TRUE;
 }
 
-PolKitTest _test_config = {
+KitTest _test_config = {
         "polkit_config",
         NULL,
         NULL,
diff --git a/src/polkit/polkit-context.c b/src/polkit/polkit-context.c
index de37dfa..a7e0091 100644
--- a/src/polkit/polkit-context.c
+++ b/src/polkit/polkit-context.c
@@ -810,7 +810,7 @@ _run_test (void)
         return TRUE;
 }
 
-PolKitTest _test_context = {
+KitTest _test_context = {
         "polkit_context",
         NULL,
         NULL,
diff --git a/src/polkit/polkit-error.c b/src/polkit/polkit-error.c
index aa886cb..e7105db 100644
--- a/src/polkit/polkit-error.c
+++ b/src/polkit/polkit-error.c
@@ -236,7 +236,7 @@ _run_test (void)
 }
 
 
-PolKitTest _test_error = {
+KitTest _test_error = {
         "polkit_error",
         NULL,
         NULL,
diff --git a/src/polkit/polkit-policy-cache.c b/src/polkit/polkit-policy-cache.c
index 4e344c1..a1285a6 100644
--- a/src/polkit/polkit-policy-cache.c
+++ b/src/polkit/polkit-policy-cache.c
@@ -504,7 +504,7 @@ out:
         return TRUE;
 }
 
-PolKitTest _test_policy_cache = {
+KitTest _test_policy_cache = {
         "polkit_policy_cache",
         NULL,
         NULL,
diff --git a/src/polkit/polkit-policy-default.c b/src/polkit/polkit-policy-default.c
index e0b6009..c1e8173 100644
--- a/src/polkit/polkit-policy-default.c
+++ b/src/polkit/polkit-policy-default.c
@@ -431,7 +431,7 @@ _run_test (void)
         return TRUE;
 }
 
-PolKitTest _test_policy_default = {
+KitTest _test_policy_default = {
         "polkit_policy_default",
         NULL,
         NULL,
diff --git a/src/polkit/polkit-policy-file-entry.c b/src/polkit/polkit-policy-file-entry.c
index dc78ff9..13ddd31 100644
--- a/src/polkit/polkit-policy-file-entry.c
+++ b/src/polkit/polkit-policy-file-entry.c
@@ -468,7 +468,7 @@ oom:
         return TRUE;
 }
 
-PolKitTest _test_policy_file_entry = {
+KitTest _test_policy_file_entry = {
         "polkit_policy_file_entry",
         NULL,
         NULL,
diff --git a/src/polkit/polkit-policy-file.c b/src/polkit/polkit-policy-file.c
index 595b168..a164367 100644
--- a/src/polkit/polkit-policy-file.c
+++ b/src/polkit/polkit-policy-file.c
@@ -807,7 +807,7 @@ _run_test (void)
         return TRUE;
 }
 
-PolKitTest _test_policy_file = {
+KitTest _test_policy_file = {
         "polkit_policy_file",
         NULL,
         NULL,
diff --git a/src/polkit/polkit-result.c b/src/polkit/polkit-result.c
index 6bd05c7..a49f376 100644
--- a/src/polkit/polkit-result.c
+++ b/src/polkit/polkit-result.c
@@ -141,7 +141,7 @@ _run_test (void)
         return TRUE;
 }
 
-PolKitTest _test_result = {
+KitTest _test_result = {
         "polkit_result",
         NULL,
         NULL,
diff --git a/src/polkit/polkit-seat.c b/src/polkit/polkit-seat.c
index 69d8325..d1f512e 100644
--- a/src/polkit/polkit-seat.c
+++ b/src/polkit/polkit-seat.c
@@ -220,7 +220,7 @@ _run_test (void)
         return TRUE;
 }
 
-PolKitTest _test_seat = {
+KitTest _test_seat = {
         "polkit_seat",
         NULL,
         NULL,
diff --git a/src/polkit/polkit-session.c b/src/polkit/polkit-session.c
index 189e11f..698fb93 100644
--- a/src/polkit/polkit-session.c
+++ b/src/polkit/polkit-session.c
@@ -490,7 +490,7 @@ _run_test (void)
         return TRUE;
 }
 
-PolKitTest _test_session = {
+KitTest _test_session = {
         "polkit_session",
         NULL,
         NULL,
diff --git a/src/polkit/polkit-sysdeps.c b/src/polkit/polkit-sysdeps.c
index 63f604d..5d923d7 100644
--- a/src/polkit/polkit-sysdeps.c
+++ b/src/polkit/polkit-sysdeps.c
@@ -168,7 +168,7 @@ _run_test (void)
         return TRUE;
 }
 
-PolKitTest _test_sysdeps = {
+KitTest _test_sysdeps = {
         "polkit_sysdeps",
         NULL,
         NULL,
diff --git a/src/polkit/polkit-test.c b/src/polkit/polkit-test.c
index de87063..4a37ce1 100644
--- a/src/polkit/polkit-test.c
+++ b/src/polkit/polkit-test.c
@@ -39,7 +39,7 @@
  * Testing code for libpolkit.
  */
 
-static PolKitTest *tests[] = {
+static KitTest *tests[] = {
         &_test_action,
         &_test_error,
         &_test_result,
@@ -61,14 +61,6 @@ static PolKitTest *tests[] = {
 int 
 main (int argc, char *argv[])
 {
-        int ret;
-        int n;
-        int num_tests;
-
-        ret = 0;
-
-        num_tests = sizeof (tests) / sizeof (PolKitTest*);
-
         /* Some of the code will log to syslog because .policy files
          * etc. may be malformed. Since this will open a socket to the
          * system logger preempt this so the fd-leak checking don't
@@ -76,71 +68,8 @@ main (int argc, char *argv[])
          */
         syslog (LOG_INFO, "libpolkit: initiating test; bogus alerts may be written to syslog");
 
-        printf ("Running %d unit tests\n", num_tests);
-        for (n = 0; n < num_tests; n++) {
-                int m;
-                int total_allocs;
-                int delta;
-                int num_fd;
-                int num_fd_after;
-                PolKitTest *test = tests[n];
-
-                _kit_memory_reset ();
-
-                if (test->setup != NULL)
-                        test->setup ();
-
-                num_fd = _kit_get_num_fd ();
-                printf ("Running: %s\n", test->name);
-                if (!test->run ()) {
-                        printf ("Failed\n");
-                        ret = 1;
-                        goto test_done;
-                }
-                num_fd_after = _kit_get_num_fd ();
-
-                total_allocs = _kit_memory_get_total_allocations ();
-                printf ("  Unit test made %d allocations in total\n", total_allocs);
-                
-                delta = _kit_memory_get_current_allocations ();
-                if (delta != 0) {
-                        printf ("  Unit test leaked %d allocations\n", delta);
-                        ret = 1;
-                }
-                if (num_fd != num_fd_after) {
-                        printf ("  Unit test leaked %d file descriptors\n", num_fd_after - num_fd);
-                        ret = 1;
-                }
-                
-                for (m = 0; m < total_allocs; m++) {
-                        printf ("  Failing allocation %d of %d\n", m + 1, total_allocs);
-                        
-                        _kit_memory_reset ();
-                        _kit_memory_fail_nth_alloc (m);
-                        
-                        num_fd = _kit_get_num_fd ();
-                        if (!test->run ()) {
-                                printf ("  Failed\n");
-                                ret = 1;
-                                continue;
-                        }
-                        num_fd_after = _kit_get_num_fd ();
-                        
-                        delta = _kit_memory_get_current_allocations ();
-                        if (delta != 0) {
-                                printf ("  Unit test leaked %d allocations\n", delta);
-                                ret = 1;
-                        }
-                        if (num_fd != num_fd_after) {
-                                printf ("  Unit test leaked %d file descriptors\n", num_fd_after - num_fd);
-                                ret = 1;
-                        }
-                }
-
-        test_done:
-                if (test->teardown != NULL)
-                        test->teardown ();
-        }
-
-        return ret;
+        if (kit_test_run (tests, sizeof (tests) / sizeof (KitTest*)))
+                return 0;
+        else
+                return 1;
 }
diff --git a/src/polkit/polkit-test.h b/src/polkit/polkit-test.h
index e9dfa5c..45e033d 100644
--- a/src/polkit/polkit-test.h
+++ b/src/polkit/polkit-test.h
@@ -30,42 +30,27 @@
 #ifndef POLKIT_TEST_H
 #define POLKIT_TEST_H
 
+#include <kit/kit.h>
 #include <polkit/polkit-types.h>
 
 POLKIT_BEGIN_DECLS
 
-polkit_bool_t _test_polkit_action (void);
-polkit_bool_t _test_polkit_error (void);
-
-/**
- * PolKitTest:
- *
- * Test suite abstraction.
- */
-typedef struct {
-        /*< private >*/
-        const char *name;
-        void (*setup) (void);
-        void (*teardown) (void);
-        polkit_bool_t (*run) (void);
-} PolKitTest;
-
-extern PolKitTest _test_action;
-extern PolKitTest _test_error;
-extern PolKitTest _test_result;
-extern PolKitTest _test_seat;
-extern PolKitTest _test_session;
-extern PolKitTest _test_caller;
-extern PolKitTest _test_policy_default;
-extern PolKitTest _test_policy_file_entry;
-extern PolKitTest _test_policy_file;
-extern PolKitTest _test_policy_cache;
-extern PolKitTest _test_authorization_constraint;
-extern PolKitTest _test_authorization;
-extern PolKitTest _test_authorization_db;
-extern PolKitTest _test_config;
-extern PolKitTest _test_sysdeps;
-extern PolKitTest _test_context;
+extern KitTest _test_action;
+extern KitTest _test_error;
+extern KitTest _test_result;
+extern KitTest _test_seat;
+extern KitTest _test_session;
+extern KitTest _test_caller;
+extern KitTest _test_policy_default;
+extern KitTest _test_policy_file_entry;
+extern KitTest _test_policy_file;
+extern KitTest _test_policy_cache;
+extern KitTest _test_authorization_constraint;
+extern KitTest _test_authorization;
+extern KitTest _test_authorization_db;
+extern KitTest _test_config;
+extern KitTest _test_sysdeps;
+extern KitTest _test_context;
 
 POLKIT_END_DECLS
 
diff --git a/test/create-coverage-report.sh b/test/create-coverage-report.sh
index cb14a1e..68ce8f6 100755
--- a/test/create-coverage-report.sh
+++ b/test/create-coverage-report.sh
@@ -16,35 +16,41 @@ echo "==========================================================================
 
 while [ $# -gt 0 ] ; do
     SOURCE=`cat $P/$1 |wc -l`
-    ACTUAL=`grep -v "        -:" $P/$1.gcov  |wc -l`
-    NOT_COVERED=`grep "    #####:" $P/$1.gcov  |wc -l`
-    COVERED=$(($ACTUAL - $NOT_COVERED))
-    PERCENT=$((100 * $COVERED / $ACTUAL))
-
     TOTAL_SOURCE=$(($TOTAL_SOURCE + $SOURCE))
-    TOTAL_ACTUAL=$(($TOTAL_ACTUAL + $ACTUAL))
-    TOTAL_COVERED=$(($TOTAL_COVERED + $COVERED))
 
     echo -n "$1"
-
+        
     n=${#1}
     while [ $n -lt 55 ] ; do
         echo -n " "
         n=$(($n + 1))
     done
-
+    
     echo -n " : "
 
-    if [ $PERCENT -lt 10 ] ; then
-        echo -n "  $PERCENT%"
-    elif [ $PERCENT -lt 100 ] ; then
-        echo -n " $PERCENT%"
+    if [ -e $P/$1.gcov ] ; then
+        ACTUAL=`grep -v "        -:" $P/$1.gcov  |wc -l`
+        NOT_COVERED=`grep "    #####:" $P/$1.gcov  |wc -l`
+        COVERED=$(($ACTUAL - $NOT_COVERED))
+        PERCENT=$((100 * $COVERED / $ACTUAL))
+
+        TOTAL_ACTUAL=$(($TOTAL_ACTUAL + $ACTUAL))
+        TOTAL_COVERED=$(($TOTAL_COVERED + $COVERED))
+        
+        if [ $PERCENT -lt 10 ] ; then
+            echo -n "  $PERCENT%"
+        elif [ $PERCENT -lt 100 ] ; then
+            echo -n " $PERCENT%"
+        else
+            echo -n "100%"
+        fi
+        
+        echo " ($COVERED of $ACTUAL)"
+
     else
-        echo -n "100%"
+        echo "  0% (not tested)"
     fi
 
-    echo " ($COVERED of $ACTUAL)"
-
     shift
 done
 


More information about the hal-commit mailing list