[gnome-keyring] Fix tests so they work from 'make distwcheck'



commit 5f7e6f921cb4bc01aca90416ba83cb046fee1506
Author: Stef Walter <stef memberwebs com>
Date:   Mon Jul 13 19:44:37 2009 +0000

    Fix tests so they work from 'make distwcheck'
    
    Fix path issues in the tests which need data from their
    test-data directory, so that they work when running 'make distcheck'

 daemon/keyrings/tests/unit-test-keyring-file.c |   20 ++++--------
 gcr/gcr-library.c                              |    2 +-
 gcr/tests/unit-test-certificate.c              |   11 ++-----
 gcr/tests/unit-test-parser.c                   |    2 +-
 pkcs11/gck/tests/unit-test-data-der.c          |    2 +-
 pkcs11/gck/tests/unit-test-data-file.c         |    4 +-
 pkcs11/ssh-store/tests/Makefile.am             |    4 ++-
 tests/gtest-helpers.c                          |   38 ++++++++++++++++++++---
 tests/gtest-helpers.h                          |    1 +
 tests/gtest.make                               |    2 +-
 10 files changed, 53 insertions(+), 33 deletions(-)
---
diff --git a/daemon/keyrings/tests/unit-test-keyring-file.c b/daemon/keyrings/tests/unit-test-keyring-file.c
index 25d0908..4453c80 100644
--- a/daemon/keyrings/tests/unit-test-keyring-file.c
+++ b/daemon/keyrings/tests/unit-test-keyring-file.c
@@ -40,13 +40,9 @@ static GQuark
 location_for_test_data (const gchar *filename)
 {
 	GQuark quark;
-	gchar *dir;
 	gchar *path;
 	
-	dir = g_get_current_dir ();
-	g_assert (dir);
-	
-	path = g_build_filename (dir, "test-data", filename, NULL);
+	path = g_build_filename (test_dir_testdata (), filename, NULL);
 	quark = gkr_location_from_path (path);
 	g_free (path);
 	
@@ -125,7 +121,7 @@ DEFINE_TEST(keyring_parse_encrypted)
 {
 	GkrKeyring *encrypted, *plain;
 	EggBuffer buffer, output;
-	gchar *data;
+	guchar *data;
 	gsize n_data;
 	gint ret;
 	gboolean success;
@@ -134,11 +130,10 @@ DEFINE_TEST(keyring_parse_encrypted)
 	encrypted->password = "my-keyring-password";
 	plain = gkr_keyring_new ("plain", 0);
 	
-	if (!g_file_get_contents ("test-data/encrypted.keyring", &data, &n_data, NULL))
-		g_assert (FALSE && "couldn't read in encrypted.keyring");
-		
+	data = test_read_testdata ("encrypted.keyring", &n_data);
+
 	/* Parse it */
-	egg_buffer_init_allocated (&buffer, (guchar*)data, n_data, NULL);
+	egg_buffer_init_allocated (&buffer, data, n_data, NULL);
 	data = g_memdup (data, n_data); /* Make a copy for double parse */
 	ret = gkr_keyring_binary_parse (encrypted, &buffer);
 	egg_buffer_uninit (&buffer);
@@ -180,14 +175,13 @@ DEFINE_TEST(keyring_parse_plain)
 {
 	GkrKeyring *keyring;
 	EggBuffer buffer;
-	gchar *data;
+	guchar *data;
 	gsize n_data;
 	gint ret;
 	
 	keyring = gkr_keyring_new ("plain", 0);
 	
-	if (!g_file_get_contents ("test-data/plain.keyring", &data, &n_data, NULL))
-		g_assert (FALSE && "couldn't read in plain.keyring");
+	data = test_read_testdata ("plain.keyring", &n_data);
 		
 	/* Parse it */
 	egg_buffer_init_static (&buffer, (guchar*)data, n_data);
diff --git a/gcr/gcr-library.c b/gcr/gcr-library.c
index 3883d81..6f91ae2 100644
--- a/gcr/gcr-library.c
+++ b/gcr/gcr-library.c
@@ -126,7 +126,7 @@ _gcr_initialize (void)
 			gp11_module_set_auto_authenticate (module, TRUE);
 			all_modules = g_list_prepend (all_modules, module);
 		} else { 
-			g_warning ("couldn't initialize PKCS#11 module: %s", 
+			g_message ("couldn't initialize PKCS#11 module: %s",
 			           error && error->message ? error->message : "");
 		}
 
diff --git a/gcr/tests/unit-test-certificate.c b/gcr/tests/unit-test-certificate.c
index e1b76d9..1f16288 100644
--- a/gcr/tests/unit-test-certificate.c
+++ b/gcr/tests/unit-test-certificate.c
@@ -13,16 +13,11 @@ static GcrCertificate *certificate = NULL;
 
 DEFINE_SETUP(certificate)
 {
-	GError *err = NULL;
-	gchar *contents;
+	guchar *contents;
 	gsize n_contents;
 	
-	if (!g_file_get_contents ("test-data/der-certificate.crt", &contents, &n_contents, &err)) {
-		g_warning ("couldn't read test-data/test-certificate-1.der: %s", err->message);
-		return;
-	}
-
-	certificate = gcr_simple_certificate_new ((const guchar*)contents, n_contents);
+	contents = test_read_testdata ("der-certificate.crt", &n_contents);
+	certificate = gcr_simple_certificate_new (contents, n_contents);
 	g_assert (certificate);
 	g_free (contents);
 }
diff --git a/gcr/tests/unit-test-parser.c b/gcr/tests/unit-test-parser.c
index 4f175d5..d22ede5 100644
--- a/gcr/tests/unit-test-parser.c
+++ b/gcr/tests/unit-test-parser.c
@@ -113,7 +113,7 @@ DEFINE_TEST(parse_all)
 	gsize len;
 	GDir *dir;
 	
-	dir = g_dir_open ("test-data", 0, NULL);
+	dir = g_dir_open (test_dir_testdata (), 0, NULL);
 	g_assert (dir);
 
 	for (;;) {
diff --git a/pkcs11/gck/tests/unit-test-data-der.c b/pkcs11/gck/tests/unit-test-data-der.c
index 608d3ed..f78148e 100644
--- a/pkcs11/gck/tests/unit-test-data-der.c
+++ b/pkcs11/gck/tests/unit-test-data-der.c
@@ -369,7 +369,7 @@ DEFINE_TEST(read_all_pkcs8)
 	guchar *data;
 	gsize n_data;
 	
-	dir = g_dir_open ("test-data", 0, NULL);
+	dir = g_dir_open (test_dir_testdata (), 0, NULL);
 	g_assert (dir);
 	
 	for(;;) {
diff --git a/pkcs11/gck/tests/unit-test-data-file.c b/pkcs11/gck/tests/unit-test-data-file.c
index 78389b8..5a8996d 100644
--- a/pkcs11/gck/tests/unit-test-data-file.c
+++ b/pkcs11/gck/tests/unit-test-data-file.c
@@ -42,8 +42,8 @@ static GckLogin *login = NULL;
 
 DEFINE_SETUP(file_store)
 {
-	public_filename = g_build_filename ("test-data", "data-file-public.store", NULL); 
-	private_filename = g_build_filename ("test-data", "data-file-private.store", NULL); 
+	public_filename = g_build_filename (test_dir_testdata (), "data-file-public.store", NULL);
+	private_filename = g_build_filename (test_dir_testdata (), "data-file-private.store", NULL);
 	write_filename = test_build_filename ("unit-test-file.store");
 
 	data_file = gck_data_file_new ();
diff --git a/pkcs11/ssh-store/tests/Makefile.am b/pkcs11/ssh-store/tests/Makefile.am
index e98645b..aae4b68 100644
--- a/pkcs11/ssh-store/tests/Makefile.am
+++ b/pkcs11/ssh-store/tests/Makefile.am
@@ -12,4 +12,6 @@ include $(top_srcdir)/tests/gtest.make
 
 p11-tests:
 	p11-tests -f p11-tests.conf ../.libs/gck-ssh-store-standalone.so
-	
\ No newline at end of file
+
+EXTRA_DIST = \
+	test-data
diff --git a/tests/gtest-helpers.c b/tests/gtest-helpers.c
index 4357ae0..0bf09ba 100644
--- a/tests/gtest-helpers.c
+++ b/tests/gtest-helpers.c
@@ -94,13 +94,44 @@ test_build_filename (const gchar *basename)
 	return g_build_filename (test_path, basename, NULL);
 }
 
+const gchar*
+test_dir_testdata (void)
+{
+	const gchar *dir;
+	gchar *cur, *env;
+
+	dir = g_getenv ("TEST_DATA");
+	if (dir == NULL)
+		dir = "./test-data";
+	if (!g_path_is_absolute (dir)) {
+		cur = g_get_current_dir ();
+		if (strncmp (dir, "./", 2) == 0)
+			dir += 2;
+		env = g_build_filename (cur, dir, NULL);
+		g_free (cur);
+		g_setenv ("TEST_DATA", env, TRUE);
+		g_free (env);
+		dir = g_getenv ("TEST_DATA");
+	}
+
+	return dir;
+}
+
 guchar* 
 test_read_testdata (const gchar *basename, gsize *n_result)
 {
-	gchar *file = g_build_filename ("test-data", basename, NULL);
+	GError *error = NULL;
 	gchar *result;
-	if (!g_file_get_contents (file, &result, n_result, NULL))
+	gchar *file;
+
+	file = g_build_filename (test_dir_testdata (), basename, NULL);
+	if (!g_file_get_contents (file, &result, n_result, &error)) {
+		g_warning ("could not read test data file: %s: %s", file,
+		           error && error->message ? error->message : "");
 		g_assert_not_reached ();
+	}
+
+	g_free (file);
 	return (guchar*)result;
 }
 
@@ -120,9 +151,6 @@ chdir_base_dir (char* argv0)
 			g_warning ("couldn't change directory to ..: %s",
 			           g_strerror (errno));
 	}
-
-	g_free (base);
-	g_free (dir);
 }
 
 int
diff --git a/tests/gtest-helpers.h b/tests/gtest-helpers.h
index 880a7cb..417d791 100644
--- a/tests/gtest-helpers.h
+++ b/tests/gtest-helpers.h
@@ -39,6 +39,7 @@ void test_mainloop_quit (void);
 void test_mainloop_run (int timeout);
 GMainLoop* test_mainloop_get (void);
 
+const gchar* test_dir_testdata (void);
 guchar* test_read_testdata (const gchar *basename, gsize *n_data);
 gchar* test_build_filename (const gchar *basename);
 
diff --git a/tests/gtest.make b/tests/gtest.make
index 01d293f..6f420f9 100644
--- a/tests/gtest.make
+++ b/tests/gtest.make
@@ -62,4 +62,4 @@ test-prompt: $(noinst_PROGRAMS)
 	gtester --verbose -k -m=slow ./run-prompt-test
 
 check-am: $(noinst_PROGRAMS)
-	gtester -m=slow ./run-auto-test
+	TEST_DATA=$(srcdir)/test-data gtester -m=slow ./run-auto-test



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]