[gnome-keyring] gnome2-store: Test the gnome2-store with the gcr importer



commit 7cbb87873a10b2adc97bcbf1e3eb4962adf6f8fc
Author: Stef Walter <stefw gnome org>
Date:   Tue Jun 19 13:55:46 2012 +0200

    gnome2-store: Test the gnome2-store with the gcr importer

 pkcs11/gkm/gkm-module.c                         |    3 -
 pkcs11/gkm/gkm-module.h                         |    3 +
 pkcs11/gnome2-store/tests/Makefile.am           |    6 +-
 pkcs11/gnome2-store/tests/files/personal.p12    |  Bin 0 -> 3396 bytes
 pkcs11/gnome2-store/tests/test-gnome2-storage.c |    1 +
 pkcs11/gnome2-store/tests/test-import.c         |  200 +++++++++++++++++++++++
 6 files changed, 209 insertions(+), 4 deletions(-)
---
diff --git a/pkcs11/gkm/gkm-module.c b/pkcs11/gkm/gkm-module.c
index 5a9ff4c..fe0aabc 100644
--- a/pkcs11/gkm/gkm-module.c
+++ b/pkcs11/gkm/gkm-module.c
@@ -85,9 +85,6 @@ typedef struct _Apartment {
 	CK_USER_TYPE logged_in;
 } Apartment;
 
-/* Our slot identifier is 1 */
-#define GKM_SLOT_ID  1
-
 G_DEFINE_TYPE (GkmModule, gkm_module, G_TYPE_OBJECT);
 
 /* These info blocks are used unless derived class overrides */
diff --git a/pkcs11/gkm/gkm-module.h b/pkcs11/gkm/gkm-module.h
index e806d7c..63a656a 100644
--- a/pkcs11/gkm/gkm-module.h
+++ b/pkcs11/gkm/gkm-module.h
@@ -100,6 +100,9 @@ struct _GkmModuleClass {
 		{ return g_object_new ((type), "initialize-args", args, "mutex", mutex, NULL); } \
 	const CK_FUNCTION_LIST_PTR prefix ## _function_list = &gkm_module_function_list;
 
+/* Our slot identifier is 1 */
+#define GKM_SLOT_ID  1
+
 GType                  gkm_module_get_type                        (void);
 
 GkmManager*            gkm_module_get_manager                     (GkmModule *self);
diff --git a/pkcs11/gnome2-store/tests/Makefile.am b/pkcs11/gnome2-store/tests/Makefile.am
index 9751350..64731dd 100644
--- a/pkcs11/gnome2-store/tests/Makefile.am
+++ b/pkcs11/gnome2-store/tests/Makefile.am
@@ -27,7 +27,8 @@ CHECK_FILES = \
 TEST_PROGS = \
 	test-gnome2-file \
 	test-gnome2-storage \
-	test-gnome2-private-key
+	test-gnome2-private-key \
+	test-import
 
 test_gnome2_storage_SOURCES = \
 	test-gnome2-storage.c \
@@ -37,6 +38,9 @@ test_gnome2_private_key_SOURCES = \
 	test-gnome2-private-key.c \
 	mock-gnome2-module.c mock-gnome2-module.h
 
+test_import_CFLAGS = $(GCR_BASE_CFLAGS) $(GCK_CFLAGS)
+test_import_LDADD = $(GCR_BASE_LIBS) $(GCK_LIBS) $(LDADD)
+
 check_PROGRAMS = $(TEST_PROGS)
 
 test: $(TEST_PROGS) $(CHECK_PROGS) $(CHECK_FILES)
diff --git a/pkcs11/gnome2-store/tests/files/personal.p12 b/pkcs11/gnome2-store/tests/files/personal.p12
new file mode 100644
index 0000000..7ae3d05
Binary files /dev/null and b/pkcs11/gnome2-store/tests/files/personal.p12 differ
diff --git a/pkcs11/gnome2-store/tests/test-gnome2-storage.c b/pkcs11/gnome2-store/tests/test-gnome2-storage.c
index 862547a..c72b374 100644
--- a/pkcs11/gnome2-store/tests/test-gnome2-storage.c
+++ b/pkcs11/gnome2-store/tests/test-gnome2-storage.c
@@ -176,6 +176,7 @@ teardown_directory (Test *test,
 		filename = g_build_filename (test->directory, name, NULL);
 		if (g_unlink (filename) < 0)
 			g_assert_not_reached ();
+		g_free (filename);
 	}
 
 	g_dir_close (dir);
diff --git a/pkcs11/gnome2-store/tests/test-import.c b/pkcs11/gnome2-store/tests/test-import.c
new file mode 100644
index 0000000..ac6a9df
--- /dev/null
+++ b/pkcs11/gnome2-store/tests/test-import.c
@@ -0,0 +1,200 @@
+/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
+/* unit-test-file-store.c: Test file store functionality
+
+   Copyright (C) 2008 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 "config.h"
+
+#include "egg/egg-mkdtemp.h"
+
+#include "gkm/gkm-module.h"
+#include "gkm/gkm-test.h"
+
+#include "gnome2-store/gkm-gnome2-store.h"
+
+#define GCR_API_SUBJECT_TO_CHANGE
+#define GCK_API_SUBJECT_TO_CHANGE
+#include <gck/gck.h>
+#include <gcr/gcr-base.h>
+
+#include <p11-kit/p11-kit.h>
+
+#include <glib/gstdio.h>
+
+#include <string.h>
+
+typedef struct {
+	CK_FUNCTION_LIST_PTR funcs;
+	GList *importers;
+	gchar *directory;
+} Test;
+
+static void
+setup (Test *test,
+       gconstpointer unused)
+{
+	CK_C_INITIALIZE_ARGS args;
+	CK_SESSION_HANDLE session;
+	GckModule *module;
+	GList *modules;
+	CK_RV rv;
+
+	test->directory = g_strdup ("/tmp/gkd-test.XXXXXX");
+	if (!egg_mkdtemp (test->directory))
+		g_assert_not_reached ();
+
+	memset (&args, 0, sizeof (args));
+	args.flags = CKF_OS_LOCKING_OK;
+	args.pReserved = g_strdup_printf ("directory='%s'", test->directory);
+
+	test->funcs = gkm_gnome2_store_get_functions ();
+	rv = (test->funcs->C_Initialize) (&args);
+	gkm_assert_cmprv (rv, ==, CKR_OK);
+
+	/* And now need to log in */
+	rv = (test->funcs->C_OpenSession) (GKM_SLOT_ID, CKF_SERIAL_SESSION | CKF_RW_SESSION,
+	                                   NULL, NULL, &session);
+	gkm_assert_cmprv (rv, ==, CKR_OK);
+
+	/* The directory is empty, so we need to initialize */
+	rv = (test->funcs->C_SetPIN) (session, NULL, 0, (CK_BYTE_PTR)"mypin", 5);
+	gkm_assert_cmprv (rv, ==, CKR_OK);
+
+	/* Login so the importer doesn't have to */
+	rv = (test->funcs->C_Login) (session, CKU_USER, (CK_BYTE_PTR)"mypin", 5);
+	gkm_assert_cmprv (rv, ==, CKR_OK);
+
+	module = gck_module_new (test->funcs);
+	modules = g_list_prepend (NULL, module);
+	gcr_pkcs11_set_modules (modules);
+	g_list_free (modules);
+	g_object_unref (module);
+}
+
+static void
+teardown (Test *test,
+          gconstpointer unused)
+{
+	GError *error = NULL;
+	const gchar *name;
+	gchar *filename;
+	GDir *dir;
+	CK_RV rv;
+
+	g_list_free_full (test->importers, g_object_unref);
+
+	gcr_pkcs11_set_modules (NULL);
+
+	rv = (test->funcs->C_Finalize) (NULL);
+	gkm_assert_cmprv (rv, ==, CKR_OK);
+
+	/* Cleanup the directory */
+	dir = g_dir_open (test->directory, 0, &error);
+	g_assert_no_error (error);
+	while ((name = g_dir_read_name (dir)) != NULL) {
+		filename = g_build_filename (test->directory, name, NULL);
+		if (g_unlink (filename) < 0)
+			g_assert_not_reached ();
+		g_free (filename);
+	}
+	g_dir_close (dir);
+	if (g_rmdir (test->directory) < 0)
+		g_assert_not_reached ();
+	g_free (test->directory);
+}
+
+static void
+on_parser_parsed (GcrParser *parser,
+                  gpointer user_data)
+{
+	Test *test = user_data;
+	GcrParsed *parsed;
+	GList *importers;
+
+	parsed = gcr_parser_get_parsed (parser);
+
+	if (test->importers == NULL)
+		importers = gcr_importer_create_for_parsed (parsed);
+	else
+		importers = gcr_importer_queue_and_filter_for_parsed (test->importers, parsed);
+
+	g_list_free_full (test->importers, g_object_unref);
+	test->importers = importers;
+}
+
+static void
+test_pkcs12_import (Test *test,
+                    gconstpointer unused)
+{
+	GcrParser *parser;
+	GError *error;
+	gchar *contents;
+	gsize length;
+	GList *l;
+
+	g_file_get_contents (SRCDIR "/files/personal.p12", &contents, &length, &error);
+	g_assert_no_error (error);
+
+	/* Parse the pkcs12 file */
+	parser = gcr_parser_new ();
+	gcr_parser_add_password (parser, "booo");
+	gcr_parser_format_enable (parser, GCR_FORMAT_DER_PKCS12);
+	g_signal_connect (parser, "parsed", G_CALLBACK (on_parser_parsed), test);
+	gcr_parser_parse_data (parser, (const guchar *)contents, length, &error);
+	g_assert_no_error (error);
+	g_object_unref (parser);
+	g_free (contents);
+
+	/* Should have found importers */
+	g_assert (test->importers != NULL);
+
+	for (l = test->importers; l != NULL; l = g_list_next (l)) {
+		gcr_importer_import (l->data, NULL, &error);
+		g_assert_no_error (error);
+	}
+}
+
+static void
+null_log_handler (const gchar *log_domain,
+                  GLogLevelFlags log_level,
+                  const gchar *message,
+                  gpointer user_data)
+{
+
+}
+
+int
+main (int argc, char **argv)
+{
+	g_type_init ();
+	g_test_init (&argc, &argv, NULL);
+
+	g_set_prgname ("test-import");
+
+	/* Suppress these messages in tests */
+	g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE,
+	                   null_log_handler, NULL);
+
+	g_test_add ("/gnome2-store/import/pkcs12", Test, NULL,
+	            setup, test_pkcs12_import, teardown);
+
+	return g_test_run ();
+}



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