Commit 4b340f87 authored by David Zeuthen's avatar David Zeuthen

add unit test framework to the rest of the library sources

parent 20fa8e0d
......@@ -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
......
......@@ -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">
......
......@@ -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
......@@ -44,7 +44,6 @@
* Functions used for memory management.
**/
#ifdef KIT_BUILD_TESTS
static int _cur_allocs = 0;
......
/* -*- 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@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;
}
......@@ -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;
}
}
......
......@@ -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;
......
......@@ -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
......
......@@ -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
/* -*- 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@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;
}
/* -*- 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@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 */
......@@ -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 */
......@@ -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
/* -*- 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@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;
}
/* -*- 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@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 */
......@@ -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 */
......@@ -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
......
......@@ -294,7 +294,7 @@ _run_test (void)
return TRUE;
}
PolKitTest _test_action = {
KitTest _test_action = {
"polkit_action",
NULL,
NULL,
......
......@@ -731,7 +731,7 @@ _run_test (void)
}
PolKitTest _test_authorization_constraint = {
KitTest _test_authorization_constraint = {
"polkit_authorization_constraint",
NULL,
NULL,
......
......@@ -197,7 +197,7 @@ _run_test (void)
return TRUE;
}
PolKitTest _test_authorization_db = {
KitTest _test_authorization_db = {
"polkit_authorization_db",
NULL,
NULL,
......
......@@ -909,7 +909,7 @@ fail:
}
PolKitTest _test_authorization_db = {
KitTest _test_authorization_db = {
"polkit_authorization_db",
NULL,
NULL,
......
......@@ -782,7 +782,7 @@ _run_test (void)
#endif /* POLKIT_AUTHDB_DEFAULT */
PolKitTest _test_authorization = {
KitTest _test_authorization = {
"polkit_authorization",
NULL,
NULL,
......
......@@ -444,7 +444,7 @@ _run_test (void)