summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog18
-rw-r--r--Makefile.am10
-rw-r--r--configure.in1
-rw-r--r--daemon/gnome-keyring-daemon-dbus.c7
-rw-r--r--daemon/gnome-keyring-daemon-io.c43
-rw-r--r--keyrings/gkr-keyrings.c25
-rw-r--r--tests/Makefile.am18
-rw-r--r--tests/unit-test-daemon-setup.c81
-rw-r--r--tests/unit-test-keyrings-prompt.c12
-rw-r--r--tests/unit-test-keyrings.c46
-rw-r--r--tests/unit-test-memory.c12
-rw-r--r--tests/unit-test-other.c12
-rw-r--r--tests/unit-test-secmem.c12
-rwxr-xr-xtests/unit-tests-prep.sh25
14 files changed, 262 insertions, 60 deletions
diff --git a/ChangeLog b/ChangeLog
index 5efa5895..31f9f531 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,21 @@
+2007-06-22 Stef Walter <stef@memberwebs.com>
+
+ * configure.in:
+ * Makefile.am:
+ * daemon/gonme-keyring-daemon-dbus.c:
+ * daemon/gnome-keyring-daemon-io.c:
+ * keyrings/gkr-keyrings.c:
+ * tests/Makefile.am:
+ * tests/unit-test-daemon-setup.c: (added)
+ * tests/unit-test-keyrings.c:
+ * tests/unit-test-keyrings-prompt.c:
+ * tests/unit-test-memory.c:
+ * tests/unit-test-other.c:
+ * tests/unit-test-secmem.c:
+ * tests/unit-test-prep.sh: Automatic unit testing on distcheck,
+ modified daemon so it behaves slightly differently (where it
+ creates sockets, keyrings) when testing.
+
=== gnome-keyring 2.19.4.1 ===
2007-06-17 Stef Walter <stef@memberwebs.com>
diff --git a/Makefile.am b/Makefile.am
index f6eae5cc..93c4b0ab 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -32,3 +32,13 @@ DISTCLEANFILES = \
# Clean up any EXTRA_DIST we're distributing
dist-hook:
rm -rf `find $(distdir)/ -name .svn`
+
+distcheck-hook:
+ @echo "RUNNING AUTOMATIC UNIT TESTS ---------------------------"
+ @echo
+ @echo
+ $(MAKE) -C tests run-auto-tests
+ @echo
+ @echo
+ @echo "DONE AUTOMATIC UNIT TESTS ------------------------------"
+
diff --git a/configure.in b/configure.in
index 8b2c8f83..2188b65c 100644
--- a/configure.in
+++ b/configure.in
@@ -73,6 +73,7 @@ AC_ARG_ENABLE(tests,
if test "$enable_tests" = "yes"; then
echo "building tests and unit tests"
+ AC_DEFINE_UNQUOTED(WITH_TESTS, 1, [Build with unit test support])
else
echo "not building tests and unit tests"
fi
diff --git a/daemon/gnome-keyring-daemon-dbus.c b/daemon/gnome-keyring-daemon-dbus.c
index 3fb79dcc..b5243d08 100644
--- a/daemon/gnome-keyring-daemon-dbus.c
+++ b/daemon/gnome-keyring-daemon-dbus.c
@@ -453,6 +453,13 @@ gnome_keyring_daemon_dbus_setup (GMainLoop *loop, const gchar *socket)
{
dbus_uint32_t res = 0;
DBusError derr = { 0 };
+
+#ifdef WITH_TESTS
+ /* If running as a test, don't do DBUS stuff */
+ const gchar *env = g_getenv ("GNOME_KEYRING_TEST_PATH");
+ if (env && *env)
+ return;
+#endif
socket_path = socket;
dbus_error_init (&derr);
diff --git a/daemon/gnome-keyring-daemon-io.c b/daemon/gnome-keyring-daemon-io.c
index b28f5512..1d33bbf2 100644
--- a/daemon/gnome-keyring-daemon-io.c
+++ b/daemon/gnome-keyring-daemon-io.c
@@ -88,9 +88,9 @@ typedef struct {
gint output_pos;
} GnomeKeyringClient;
-char tmp_dir[1024];
-char socket_path[1024];
-GList *clients = NULL;
+static char tmp_dir[1024] = { 0, };
+static char socket_path[1024] = { 0, };
+static GList *clients = NULL;
#if 0
#define debug_print(x) g_print x
@@ -670,25 +670,44 @@ new_client (GIOChannel *channel,
void
cleanup_socket_dir (void)
{
- unlink (socket_path);
- rmdir (tmp_dir);
+ if(*socket_path)
+ unlink (socket_path);
+ if(*tmp_dir)
+ rmdir (tmp_dir);
}
gboolean
create_master_socket (const char **path)
{
+ gboolean have_path = FALSE;
int sock;
struct sockaddr_un addr;
GIOChannel *channel;
- gchar *tmp_tmp_dir;
+ gchar *tmp_tmp_dir = NULL;
- /* Create private directory for agent socket */
- tmp_tmp_dir = g_build_filename (g_get_tmp_dir (), "keyring-XXXXXX", NULL);
- strncpy (tmp_dir, tmp_tmp_dir, sizeof (tmp_dir));
- if (mkdtemp (tmp_dir) == NULL) {
- perror ("mkdtemp: socket dir");
- return FALSE;
+ /*
+ * When run under control of unit tests, we let the parent process
+ * pass in the socket path that we're going to create our main socket on.
+ */
+
+#ifdef WITH_TESTS
+ const gchar* env = g_getenv ("GNOME_KEYRING_TEST_PATH");
+ if (env && *env) {
+ g_strlcpy (tmp_dir, env, sizeof (tmp_dir));
+ have_path = TRUE;
+ }
+#endif /* WITH_TESTS */
+
+ if (!have_path) {
+ /* Create private directory for agent socket */
+ tmp_tmp_dir = g_build_filename (g_get_tmp_dir (), "keyring-XXXXXX", NULL);
+ strncpy (tmp_dir, tmp_tmp_dir, sizeof (tmp_dir));
+ if (mkdtemp (tmp_dir) == NULL) {
+ perror ("mkdtemp: socket dir");
+ return FALSE;
+ }
}
+
snprintf (socket_path, sizeof (socket_path), "%s/socket", tmp_dir);
sock = socket(AF_UNIX, SOCK_STREAM, 0);
diff --git a/keyrings/gkr-keyrings.c b/keyrings/gkr-keyrings.c
index a2a3a9b0..38729ded 100644
--- a/keyrings/gkr-keyrings.c
+++ b/keyrings/gkr-keyrings.c
@@ -46,6 +46,7 @@ static GkrKeyring *session_keyring = NULL;
static GkrKeyring *default_keyring = NULL;
static time_t keyring_dir_mtime = 0;
+static gboolean dirs_created = FALSE;
/* -----------------------------------------------------------------------------
* HELPERS
@@ -114,20 +115,24 @@ update_default (void)
gchar*
gkr_keyrings_get_dir (void)
{
- char *dir, *gnome2_dir;
+ gchar *dir = NULL;
- dir = g_build_filename (g_get_home_dir (), ".gnome2/keyrings", NULL);
- if (!g_file_test (dir, G_FILE_TEST_IS_DIR)) {
- gnome2_dir = g_build_filename (g_get_home_dir (), ".gnome2", NULL);
- if (!g_file_test (gnome2_dir, G_FILE_TEST_IS_DIR)) {
- mkdir (gnome2_dir, S_IRWXU);
- }
- g_free (gnome2_dir);
+#ifdef WITH_TESTS
+ const gchar* env = g_getenv ("GNOME_KEYRING_TEST_PATH");
+ if (env && *env)
+ dir = g_build_filename (env, "keyrings", NULL);
+#endif
+
+ if (!dir)
+ dir = g_build_filename (g_get_home_dir (), ".gnome2", "keyrings", NULL);
- if (mkdir (dir, S_IRWXU) < 0) {
+ if (!dirs_created) {
+ if (g_mkdir_with_parents (dir, S_IRWXU) < 0)
g_warning ("unable to create keyring dir");
- }
+ else
+ dirs_created = TRUE;
}
+
return dir;
}
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 0b31b6b9..a98dd3c2 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -56,7 +56,8 @@ run_base_test_SOURCES = \
UNIT_TESTS_LIBRARY = \
unit-test-keyrings.c \
- unit-test-other.c
+ unit-test-other.c \
+ unit-test-daemon-setup.c
run-library-test.c: $(UNIT_TESTS_LIBRARY) unit-tests-prep.sh
sh unit-tests-prep.sh -b run-library-test $(UNIT_TESTS_LIBRARY)
@@ -72,7 +73,8 @@ run_library_test_SOURCES = \
# Propmting tests
UNIT_TESTS_PROMPT = \
- unit-test-keyrings-prompt.c
+ unit-test-keyrings-prompt.c \
+ unit-test-daemon-setup.c
run-prompt-test.c: $(UNIT_TESTS_PROMPT) unit-tests-prep.sh
sh unit-tests-prep.sh -b run-prompt-test $(UNIT_TESTS_PROMPT)
@@ -84,3 +86,15 @@ run_prompt_test_SOURCES = \
$(srcdir)/cu-test/CuTest.c \
$(UNIT_TESTS_PROMPT)
+# -----------------------------------------------------------------------------
+# Run the tests
+
+run-auto-tests: $(noinst_PROGRAMS)
+ ./run-base-test
+ ./run-library-test
+
+run: $(noinst_PROGRAMS)
+ ./run-base-test
+ ./run-library-test
+ ./run-prompt-test
+
diff --git a/tests/unit-test-daemon-setup.c b/tests/unit-test-daemon-setup.c
new file mode 100644
index 00000000..4671fe01
--- /dev/null
+++ b/tests/unit-test-daemon-setup.c
@@ -0,0 +1,81 @@
+/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
+/* unit-test-keyrings.c: Test basic keyring functionality
+
+ Copyright (C) 2007 Stefan Walter
+
+ The Gnome Keyring Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version.
+
+ The Gnome Keyring Library 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
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public
+ License along with the Gnome Library; see the file COPYING.LIB. If not,
+ write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA.
+
+ Author: Stef Walter <stef@memberwebs.com>
+*/
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <signal.h>
+#include <unistd.h>
+
+#include "run-library-test.h"
+#include "library/gnome-keyring.h"
+
+/*
+ * Each test looks like (on one line):
+ * void unit_test_xxxxx (CuTest* cu)
+ *
+ * Each setup looks like (on one line):
+ * void unit_setup_xxxxx (void);
+ *
+ * Each teardown looks like (on one line):
+ * void unit_teardown_xxxxx (void);
+ *
+ * Tests be run in the order specified here.
+ */
+
+#define TEST_PATH "/tmp/test-gnome-keyring-daemon"
+
+static GPid daemon_pid;
+
+void unit_setup_daemon (void)
+{
+ GError *err = NULL;
+ gchar *args[3];
+
+ if (g_mkdir_with_parents (TEST_PATH, 0777) < 0)
+ g_error ("couldn't create test directory");
+
+ g_setenv ("GNOME_KEYRING_SOCKET", TEST_PATH "/socket", TRUE);
+ g_setenv ("GNOME_KEYRING_TEST_PATH", TEST_PATH, TRUE);
+
+ args[0] = "../daemon/gnome-keyring-daemon";
+ args[1] = "-f";
+ args[2] = NULL;
+
+ if (!g_spawn_async (NULL, args, NULL, G_SPAWN_LEAVE_DESCRIPTORS_OPEN | G_SPAWN_DO_NOT_REAP_CHILD,
+ NULL, NULL, &daemon_pid, &err)) {
+ g_error ("couldn't start gnome-keyring-daemon for testing: %s",
+ err && err->message ? err->message : "");
+ g_assert_not_reached ();
+ }
+
+ /* Let it startup properly */
+ sleep (2);
+}
+
+void unit_teardown_daemon (void)
+{
+ if (daemon_pid)
+ kill (daemon_pid, SIGTERM);
+ /* We're exiting soon anyway, no need to wait */
+}
diff --git a/tests/unit-test-keyrings-prompt.c b/tests/unit-test-keyrings-prompt.c
index 5d41d414..6da8de13 100644
--- a/tests/unit-test-keyrings-prompt.c
+++ b/tests/unit-test-keyrings-prompt.c
@@ -8,10 +8,16 @@
#include "library/gnome-keyring.h"
/*
- * Each test function must begin with (on the same line):
- * void unit_test_
+ * Each test looks like (on one line):
+ * void unit_test_xxxxx (CuTest* cu)
*
- * Tests will be run in the order specified here.
+ * Each setup looks like (on one line):
+ * void unit_setup_xxxxx (void);
+ *
+ * Each teardown looks like (on one line):
+ * void unit_teardown_xxxxx (void);
+ *
+ * Tests be run in the order specified here.
*/
static void
diff --git a/tests/unit-test-keyrings.c b/tests/unit-test-keyrings.c
index 111a5e3d..2750c8eb 100644
--- a/tests/unit-test-keyrings.c
+++ b/tests/unit-test-keyrings.c
@@ -28,6 +28,19 @@
#include "run-library-test.h"
#include "library/gnome-keyring.h"
+/*
+ * Each test looks like (on one line):
+ * void unit_test_xxxxx (CuTest* cu)
+ *
+ * Each setup looks like (on one line):
+ * void unit_setup_xxxxx (void);
+ *
+ * Each teardown looks like (on one line):
+ * void unit_teardown_xxxxx (void);
+ *
+ * Tests be run in the order specified here.
+ */
+
static GList* keyrings = NULL;
gchar* default_keyring = NULL;
@@ -37,13 +50,6 @@ gchar* default_keyring = NULL;
#define DISPLAY_NAME "Item Display Name"
#define SECRET "item-secret"
-/*
- * Each test function must begin with (on the same line):
- * void unit_test_
- *
- * Tests will be run in the order specified here.
- */
-
void unit_test_stash_default (CuTest* cu)
{
GnomeKeyringResult res;
@@ -51,19 +57,6 @@ void unit_test_stash_default (CuTest* cu)
CuAssertIntEquals(cu, GNOME_KEYRING_RESULT_OK, res);
}
-void unit_test_list_keyrings (CuTest* cu)
-{
- GnomeKeyringResult res;
- GList *l;
-
- res = gnome_keyring_list_keyring_names_sync (&keyrings);
- CuAssertIntEquals(cu, GNOME_KEYRING_RESULT_OK, res);
-
- printf("\t\tkeyrings:\n");
- for (l = keyrings; l; l = g_list_next (l))
- printf("\t\t %s\n", (gchar*)l->data);
-}
-
void unit_test_create_keyring (CuTest* cu)
{
GnomeKeyringResult res;
@@ -299,6 +292,19 @@ void unit_test_keyring_info (CuTest* cu)
CuAssertIntEquals(cu, GNOME_KEYRING_RESULT_OK, res);
}
+void unit_test_list_keyrings (CuTest* cu)
+{
+ GnomeKeyringResult res;
+ GList *l;
+
+ res = gnome_keyring_list_keyring_names_sync (&keyrings);
+ CuAssertIntEquals(cu, GNOME_KEYRING_RESULT_OK, res);
+
+ printf("\t\tkeyrings:\n");
+ for (l = keyrings; l; l = g_list_next (l))
+ printf("\t\t %s\n", (gchar*)l->data);
+}
+
void unit_test_cleaup (CuTest* cu)
{
GnomeKeyringResult res;
diff --git a/tests/unit-test-memory.c b/tests/unit-test-memory.c
index c19fdcc0..71119a5a 100644
--- a/tests/unit-test-memory.c
+++ b/tests/unit-test-memory.c
@@ -30,10 +30,16 @@
#include "library/gnome-keyring-memory.h"
/*
- * Each test function must begin with (on the same line):
- * void unit_test_
+ * Each test looks like (on one line):
+ * void unit_test_xxxxx (CuTest* cu)
*
- * Tests will be run in the order specified here.
+ * Each setup looks like (on one line):
+ * void unit_setup_xxxxx (void);
+ *
+ * Each teardown looks like (on one line):
+ * void unit_teardown_xxxxx (void);
+ *
+ * Tests be run in the order specified here.
*/
#define IS_ZERO ~0
diff --git a/tests/unit-test-other.c b/tests/unit-test-other.c
index 6b056dc8..273dc4a1 100644
--- a/tests/unit-test-other.c
+++ b/tests/unit-test-other.c
@@ -29,10 +29,16 @@
#include "library/gnome-keyring.h"
/*
- * Each test function must begin with (on the same line):
- * void unit_test_
+ * Each test looks like (on one line):
+ * void unit_test_xxxxx (CuTest* cu)
*
- * Tests will be run in the order specified here.
+ * Each setup looks like (on one line):
+ * void unit_setup_xxxxx (void);
+ *
+ * Each teardown looks like (on one line):
+ * void unit_teardown_xxxxx (void);
+ *
+ * Tests be run in the order specified here.
*/
void unit_test_set_display (CuTest* cu)
diff --git a/tests/unit-test-secmem.c b/tests/unit-test-secmem.c
index 09d232a7..a88711ed 100644
--- a/tests/unit-test-secmem.c
+++ b/tests/unit-test-secmem.c
@@ -30,10 +30,16 @@
#include "common/gkr-secure-memory.h"
/*
- * Each test function must begin with (on the same line):
- * void unit_test_
+ * Each test looks like (on one line):
+ * void unit_test_xxxxx (CuTest* cu)
*
- * Tests will be run in the order specified here.
+ * Each setup looks like (on one line):
+ * void unit_setup_xxxxx (void);
+ *
+ * Each teardown looks like (on one line):
+ * void unit_teardown_xxxxx (void);
+ *
+ * Tests be run in the order specified here.
*/
#define IS_ZERO ~0
diff --git a/tests/unit-tests-prep.sh b/tests/unit-tests-prep.sh
index d75ebc0e..ea8e62a4 100755
--- a/tests/unit-tests-prep.sh
+++ b/tests/unit-tests-prep.sh
@@ -31,6 +31,8 @@ END
source_top()
{
cat << END
+/* This is auto-generated code. Edit at your own peril. */
+#include "$BASE.h"
static void RunAllTests(void)
{
CuString *output = CuStringNew();
@@ -39,13 +41,19 @@ static void RunAllTests(void)
END
}
-source_bottom()
+source_middle()
{
cat << END
CuSuiteRun(suite);
CuSuiteSummary(suite, output);
CuSuiteDetails(suite, output);
printf("%s\\n", output->buffer);
+END
+}
+
+source_bottom()
+{
+cat << END
}
int main(int argc, char* argv[])
@@ -90,17 +98,26 @@ FILES=$*
(
header_top
+ cat $FILES | grep '^void unit_setup_' | sed -e 's/$/;/'
cat $FILES | grep '^void unit_test_' | sed -e 's/$/;/'
+ cat $FILES | grep '^void unit_teardown_' | sed -e 's/$/;/'
header_bottom
) > $BASE.h
(
- echo "/* This is auto-generated code. Edit at your own peril. */"
- echo "#include \"$BASE.h\""
source_top
+ cat $FILES | grep '^void unit_setup_' | \
+ sed -e 's/^void //' -e 's/(.*$//' -e 's/$/();/'
+
cat $FILES | grep '^void unit_test_' | \
sed -e 's/^void //' -e 's/(.*$//' \
- -e 's/^/ SUITE_ADD_TEST(suite, /' -e 's/$/);/'
+ -e 's/^/SUITE_ADD_TEST(suite, /' -e 's/$/);/'
+
+ source_middle
+
+ cat $FILES | grep '^void unit_teardown_' | \
+ sed -e 's/^void //' -e 's/(.*$//' -e 's/$/();/'
+
source_bottom
) > $BASE.c