diff options
Diffstat (limited to 'gck/tests')
-rw-r--r-- | gck/tests/Makefile.am | 58 | ||||
-rw-r--r-- | gck/tests/mock-interaction.c | 97 | ||||
-rw-r--r-- | gck/tests/mock-interaction.h | 43 | ||||
-rw-r--r-- | gck/tests/mock-test-module.c | 10 | ||||
-rw-r--r-- | gck/tests/test-gck-attributes.c | 617 | ||||
-rw-r--r-- | gck/tests/test-gck-crypto.c | 647 | ||||
-rw-r--r-- | gck/tests/test-gck-enumerator.c | 359 | ||||
-rw-r--r-- | gck/tests/test-gck-module.c | 176 | ||||
-rw-r--r-- | gck/tests/test-gck-modules.c | 210 | ||||
-rw-r--r-- | gck/tests/test-gck-object.c | 419 | ||||
-rw-r--r-- | gck/tests/test-gck-session.c | 319 | ||||
-rw-r--r-- | gck/tests/test-gck-slot.c | 257 | ||||
-rw-r--r-- | gck/tests/test-gck-uri.c | 556 |
13 files changed, 0 insertions, 3768 deletions
diff --git a/gck/tests/Makefile.am b/gck/tests/Makefile.am deleted file mode 100644 index 734e9d86..00000000 --- a/gck/tests/Makefile.am +++ /dev/null @@ -1,58 +0,0 @@ - -INCLUDES = \ - -I$(top_builddir) \ - -I$(top_srcdir) \ - -DSRCDIR="\"@abs_srcdir@\"" \ - -DBUILDDIR="\"$(builddir)\"" \ - -DGCK_API_SUBJECT_TO_CHANGE \ - $(GLIB_CFLAGS) - -LDADD = \ - libmock-test-module.la \ - $(top_builddir)/gck/libgck-testable.la \ - $(top_builddir)/egg/libegg-test.la \ - $(top_builddir)/egg/libegg-hex.la \ - $(GTHREAD_LIBS) \ - $(GLIB_LIBS) \ - $(GIO_LIBS) - -TEST_PROGS = \ - test-gck-attributes \ - test-gck-module \ - test-gck-slot \ - test-gck-session \ - test-gck-object \ - test-gck-crypto \ - test-gck-uri \ - test-gck-enumerator \ - test-gck-modules - -test_gck_enumerator_SOURCES = \ - test-gck-enumerator.c \ - mock-interaction.c mock-interaction.h - -check_PROGRAMS = $(TEST_PROGS) - -test: $(TEST_PROGS) - gtester --verbose -m $(TEST_MODE) --g-fatal-warnings $(TEST_PROGS) - -check-local: test - -all-local: $(check_PROGRAMS) - -lib_LTLIBRARIES = libmock-test-module.la - -libmock_test_module_la_LDFLAGS = \ - -avoid-version - -libmock_test_module_la_CFLAGS = \ - -I$(top_srcdir)/gck \ - -I$(top_srcdir) \ - $(GLIB_CFLAGS) \ - -DGCK_API_SUBJECT_TO_CHANGE - -libmock_test_module_la_SOURCES = \ - mock-test-module.c - -libmock_test_module_la_LIBADD = \ - $(top_builddir)/gck/libgck-testable.la diff --git a/gck/tests/mock-interaction.c b/gck/tests/mock-interaction.c deleted file mode 100644 index ed25a47f..00000000 --- a/gck/tests/mock-interaction.c +++ /dev/null @@ -1,97 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* mock-interaction.c - - Copyright (C) 2011 Collabora Ltd - - 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 <stefw@collabora.co.uk> -*/ - -#include "config.h" - -#include "mock-interaction.h" - -#define MOCK_INTERACTION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), MOCK_TYPE_INTERACTION, MockInteraction)) -#define MOCK_IS_INTERACTION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), MOCK_TYPE_INTERACTION)) -#define MOCK_INTERACTION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), MOCK_TYPE_INTERACTION, MockInteractionClass)) - -typedef struct _MockInteractionClass MockInteractionClass; - -struct _MockInteraction { - GTlsInteraction interaction; - gchar *password; -}; - -struct _MockInteractionClass { - GTlsInteractionClass parent; -}; - -G_DEFINE_TYPE (MockInteraction, mock_interaction, G_TYPE_TLS_INTERACTION); - -static void -mock_interaction_init (MockInteraction *self) -{ - -} - -static void -mock_interaction_finalize (GObject *obj) -{ - MockInteraction *self = MOCK_INTERACTION (obj); - - g_free (self->password); - - G_OBJECT_CLASS (mock_interaction_parent_class)->dispose (obj); -} - -static GTlsInteractionResult -mock_interaction_ask_password (GTlsInteraction *interaction, - GTlsPassword *password, - GCancellable *cancellable, - GError **error) -{ - MockInteraction *self = MOCK_INTERACTION (interaction); - - if (self->password) { - g_tls_password_set_value (password, (const guchar *)self->password, -1); - return G_TLS_INTERACTION_HANDLED; - } else { - return G_TLS_INTERACTION_UNHANDLED; - } -} - -static void -mock_interaction_class_init (MockInteractionClass *klass) -{ - GObjectClass *object_class = G_OBJECT_CLASS (klass); - GTlsInteractionClass *interaction_class = G_TLS_INTERACTION_CLASS (klass); - - object_class->finalize = mock_interaction_finalize; - - interaction_class->ask_password = mock_interaction_ask_password; -} - -GTlsInteraction * -mock_interaction_new (const gchar *password) -{ - MockInteraction *result; - - result = g_object_new (MOCK_TYPE_INTERACTION, NULL); - result->password = g_strdup (password); - - return G_TLS_INTERACTION (result); -} diff --git a/gck/tests/mock-interaction.h b/gck/tests/mock-interaction.h deleted file mode 100644 index 0747f4b0..00000000 --- a/gck/tests/mock-interaction.h +++ /dev/null @@ -1,43 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* mock-interaction.h - - Copyright (C) 2011 Collabora Ltd - - 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 <stefw@collabora.co.uk> -*/ - -#ifndef MOCK_INTERACTION_H -#define MOCK_INTERACTION_H - -#include <gio/gio.h> - -G_BEGIN_DECLS - -#define MOCK_TYPE_INTERACTION (mock_interaction_get_type ()) -#define MOCK_INTERACTION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), MOCK_TYPE_INTERACTION, MockInteraction)) -#define MOCK_IS_INTERACTION(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), MOCK_TYPE_INTERACTION)) - -typedef struct _MockInteraction MockInteraction; - -GType mock_interaction_get_type (void) G_GNUC_CONST; - -GTlsInteraction * mock_interaction_new (const gchar *password); - -G_END_DECLS - -#endif /* MOCK_INTERACTION_H */ diff --git a/gck/tests/mock-test-module.c b/gck/tests/mock-test-module.c deleted file mode 100644 index c5431e70..00000000 --- a/gck/tests/mock-test-module.c +++ /dev/null @@ -1,10 +0,0 @@ -#include "config.h" - -#include "gck-mock.h" -#include "pkcs11.h" - -CK_RV -C_GetFunctionList (CK_FUNCTION_LIST_PTR_PTR list) -{ - return gck_mock_C_GetFunctionList (list); -} diff --git a/gck/tests/test-gck-attributes.c b/gck/tests/test-gck-attributes.c deleted file mode 100644 index bf2fc039..00000000 --- a/gck/tests/test-gck-attributes.c +++ /dev/null @@ -1,617 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* test-gck-attributes.c - the GObject PKCS#11 wrapper library - - Copyright (C) 2011 Collabora Ltd. - - 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 <stefw@collabora.co.uk> -*/ - -#include "config.h" - -#include <glib.h> -#include <string.h> - -#include "gck/gck.h" - -#define ATTR_TYPE 55 -#define ATTR_DATA "TEST DATA" -#define N_ATTR_DATA ((gsize)9) - -static void -test_init_memory (void) -{ - GckAttribute attr; - - g_assert (sizeof (attr) == sizeof (CK_ATTRIBUTE)); - - gck_attribute_init (&attr, ATTR_TYPE, (const guchar *)ATTR_DATA, N_ATTR_DATA); - g_assert (attr.type == ATTR_TYPE); - g_assert (attr.length == N_ATTR_DATA); - g_assert (memcmp (attr.value, ATTR_DATA, attr.length) == 0); - - gck_attribute_clear (&attr); -} - -static void -test_value_to_boolean (void) -{ - CK_BBOOL data = CK_TRUE; - gboolean result = FALSE; - - if (!gck_value_to_boolean (&data, sizeof (data), &result)) - g_assert_not_reached (); - - g_assert (result == TRUE); - - if (!gck_value_to_boolean (&data, sizeof (data), NULL)) - g_assert_not_reached (); - - /* Should fail */ - if (gck_value_to_boolean (&data, 0, NULL)) - g_assert_not_reached (); - if (gck_value_to_boolean (&data, 2, NULL)) - g_assert_not_reached (); - if (gck_value_to_boolean (&data, (CK_ULONG)-1, NULL)) - g_assert_not_reached (); -} - -static void -test_value_to_ulong (void) -{ - CK_ULONG data = 34343; - gulong result = 0; - - if (!gck_value_to_ulong ((const guchar *)&data, sizeof (data), &result)) - g_assert_not_reached (); - - g_assert (result == 34343); - - if (!gck_value_to_ulong ((const guchar *)&data, sizeof (data), NULL)) - g_assert_not_reached (); - - /* Should fail */ - if (gck_value_to_ulong ((const guchar *)&data, 0, NULL)) - g_assert_not_reached (); - if (gck_value_to_ulong ((const guchar *)&data, 2, NULL)) - g_assert_not_reached (); - if (gck_value_to_ulong ((const guchar *)&data, (CK_ULONG)-1, NULL)) - g_assert_not_reached (); -} - -static void -test_init_boolean (void) -{ - GckAttribute attr; - - gck_attribute_init_boolean (&attr, ATTR_TYPE, TRUE); - g_assert (attr.type == ATTR_TYPE); - g_assert (attr.length == sizeof (CK_BBOOL)); - g_assert (*((CK_BBOOL*)attr.value) == CK_TRUE); - - gck_attribute_clear (&attr); -} - -static void -test_init_date (void) -{ - GckAttribute attr; - CK_DATE ck_date; - GDate *date; - - date = g_date_new_dmy(05, 06, 1960); - memcpy (ck_date.year, "1960", 4); - memcpy (ck_date.month, "06", 2); - memcpy (ck_date.day, "05", 2); - gck_attribute_init_date (&attr, ATTR_TYPE, date); - g_date_free (date); - g_assert (attr.type == ATTR_TYPE); - g_assert (attr.length == sizeof (CK_DATE)); - g_assert (memcmp (attr.value, &ck_date, attr.length) == 0); - - gck_attribute_clear (&attr); -} - -static void -test_init_ulong (void) -{ - GckAttribute attr; - - gck_attribute_init_ulong (&attr, ATTR_TYPE, 88); - g_assert (attr.type == ATTR_TYPE); - g_assert (attr.length == sizeof (CK_ULONG)); - g_assert (*((CK_ULONG*)attr.value) == 88); - - gck_attribute_clear (&attr); -} - -static void -test_init_string (void) -{ - GckAttribute attr; - - gck_attribute_init_string (&attr, ATTR_TYPE, "a test string"); - g_assert (attr.type == ATTR_TYPE); - g_assert (attr.length == strlen ("a test string")); - g_assert (memcmp (attr.value, "a test string", attr.length) == 0); - - gck_attribute_clear (&attr); -} - -static void -test_init_invalid (void) -{ - GckAttribute attr; - - gck_attribute_init_invalid (&attr, ATTR_TYPE); - g_assert (attr.type == ATTR_TYPE); - g_assert (attr.length == (gulong)-1); - g_assert (attr.value == NULL); - - g_assert (gck_attribute_is_invalid (&attr)); - gck_attribute_clear (&attr); -} - -static void -test_init_empty (void) -{ - GckAttribute attr; - - gck_attribute_init_empty (&attr, ATTR_TYPE); - g_assert (attr.type == ATTR_TYPE); - g_assert (attr.length == 0); - g_assert (attr.value == NULL); - - gck_attribute_clear (&attr); -} - -static void -test_new_memory (void) -{ - GckAttribute *attr; - - attr = gck_attribute_new (ATTR_TYPE, ATTR_DATA, N_ATTR_DATA); - g_assert (attr->type == ATTR_TYPE); - g_assert (attr->length == N_ATTR_DATA); - g_assert (memcmp (attr->value, ATTR_DATA, attr->length) == 0); - - gck_attribute_free (attr); -} - -static void -test_new_boolean (void) -{ - GckAttribute *attr; - - attr = gck_attribute_new_boolean (ATTR_TYPE, TRUE); - g_assert (attr->type == ATTR_TYPE); - g_assert (attr->length == sizeof (CK_BBOOL)); - g_assert (*((CK_BBOOL*)attr->value) == CK_TRUE); - - gck_attribute_free (attr); -} - -static void -test_new_date (void) -{ - GckAttribute *attr; - CK_DATE ck_date; - GDate *date; - - date = g_date_new_dmy(05, 06, 1800); - memcpy (ck_date.year, "1800", 4); - memcpy (ck_date.month, "06", 2); - memcpy (ck_date.day, "05", 2); - attr = gck_attribute_new_date (ATTR_TYPE, date); - g_date_free (date); - g_assert (attr->type == ATTR_TYPE); - g_assert (attr->length == sizeof (CK_DATE)); - g_assert (memcmp (attr->value, &ck_date, attr->length) == 0); - - gck_attribute_free (attr); -} - -static void -test_new_ulong (void) -{ - GckAttribute *attr; - - attr = gck_attribute_new_ulong (ATTR_TYPE, 88); - g_assert (attr->type == ATTR_TYPE); - g_assert (attr->length == sizeof (CK_ULONG)); - g_assert (*((CK_ULONG*)attr->value) == 88); - - gck_attribute_free (attr); -} - -static void -test_new_string (void) -{ - GckAttribute *attr; - - attr = gck_attribute_new_string (ATTR_TYPE, "a test string"); - g_assert (attr->type == ATTR_TYPE); - g_assert (attr->length == strlen ("a test string")); - g_assert (memcmp (attr->value, "a test string", attr->length) == 0); - - gck_attribute_free (attr); -} - -static void -test_new_invalid (void) -{ - GckAttribute *attr; - - attr = gck_attribute_new_invalid (ATTR_TYPE); - g_assert (attr->type == ATTR_TYPE); - g_assert (attr->length == (gulong)-1); - g_assert (attr->value == NULL); - - g_assert (gck_attribute_is_invalid (attr)); - - gck_attribute_free (attr); -} - -static void -test_new_empty (void) -{ - GckAttribute *attr; - - attr = gck_attribute_new_empty (ATTR_TYPE); - g_assert (attr->type == ATTR_TYPE); - g_assert (attr->length == 0); - g_assert (attr->value == NULL); - - gck_attribute_free (attr); -} - -static void -test_get_boolean (void) -{ - GckAttribute *attr; - - attr = gck_attribute_new_boolean (ATTR_TYPE, TRUE); - g_assert (gck_attribute_get_boolean (attr) == TRUE); - gck_attribute_free (attr); -} - -static void -test_get_date (void) -{ - GckAttribute *attr; - CK_DATE ck_date; - GDate date, date2; - - g_date_set_dmy(&date, 05, 06, 1800); - memcpy (ck_date.year, "1800", 4); - memcpy (ck_date.month, "06", 2); - memcpy (ck_date.day, "05", 2); - attr = gck_attribute_new_date (ATTR_TYPE, &date); - gck_attribute_get_date (attr, &date2); - g_assert (g_date_compare (&date, &date2) == 0); - gck_attribute_free (attr); -} - -static void -test_get_ulong (void) -{ - GckAttribute *attr; - - attr = gck_attribute_new_ulong (ATTR_TYPE, 88); - g_assert (gck_attribute_get_ulong (attr) == 88); - gck_attribute_free (attr); -} - -static void -test_get_string (void) -{ - GckAttribute *attr; - gchar *value; - - attr = gck_attribute_new_string (ATTR_TYPE, "a test string"); - value = gck_attribute_get_string (attr); - g_assert (strcmp ("a test string", value) == 0); - g_free (value); - gck_attribute_free (attr); - - /* Should be able to store null strings */ - attr = gck_attribute_new_string (ATTR_TYPE, NULL); - value = gck_attribute_get_string (attr); - g_assert (value == NULL); - gck_attribute_free (attr); -} - -static void -test_dup_attribute (void) -{ - GckAttribute attr, *dup; - - gck_attribute_init_ulong (&attr, ATTR_TYPE, 88); - dup = gck_attribute_dup (&attr); - gck_attribute_clear (&attr); - g_assert (gck_attribute_get_ulong (dup) == 88); - g_assert (dup->type == ATTR_TYPE); - gck_attribute_free (dup); - - /* Should be able to dup null */ - dup = gck_attribute_dup (NULL); - g_assert (dup == NULL); -} - -static void -test_copy_attribute (void) -{ - GckAttribute attr, copy; - - gck_attribute_init_ulong (&attr, ATTR_TYPE, 88); - gck_attribute_init_copy (©, &attr); - gck_attribute_clear (&attr); - g_assert (gck_attribute_get_ulong (©) == 88); - g_assert (copy.type == ATTR_TYPE); - gck_attribute_clear (©); -} - -static void -test_new_attributes (void) -{ - GckAttributes *attrs; - - attrs = gck_attributes_new (); - g_assert (attrs != NULL); - g_assert (gck_attributes_count (attrs) == 0); - - gck_attributes_ref (attrs); - gck_attributes_unref (attrs); - - gck_attributes_unref (attrs); - - /* Can unref NULL */ - gck_attributes_unref (NULL); -} - -static void -test_attributes_contents (GckAttributes *attrs, gboolean extras) -{ - GckAttribute *attr; - gchar *value; - GDate date, *check; - guint count; - - g_assert (attrs != NULL); - count = extras ? 7 : 5; - g_assert_cmpuint (gck_attributes_count (attrs), ==, count); - - attr = gck_attributes_at (attrs, 0); - g_assert (attr->type == 0); - g_assert (gck_attribute_get_boolean (attr) == TRUE); - - attr = gck_attributes_at (attrs, 1); - g_assert (attr->type == 101); - g_assert (gck_attribute_get_ulong (attr) == 888); - - attr = gck_attributes_at (attrs, 2); - g_assert (attr->type == 202); - value = gck_attribute_get_string (attr); - g_assert (strcmp (value, "string") == 0); - g_free (value); - - attr = gck_attributes_at (attrs, 3); - g_assert (attr->type == 303); - check = g_date_new_dmy (11, 12, 2008); - gck_attribute_get_date (attr, &date); - g_assert (g_date_compare (&date, check) == 0); - g_date_free (check); - - attr = gck_attributes_at (attrs, 4); - g_assert (attr->type == 404); - g_assert (attr->length == N_ATTR_DATA); - g_assert (memcmp (attr->value, ATTR_DATA, N_ATTR_DATA) == 0); - - if (!extras) - return; - - attr = gck_attributes_at (attrs, 5); - g_assert (attr->type == 505); - g_assert (attr->length == (gulong)-1); - g_assert (attr->value == NULL); - g_assert (gck_attribute_is_invalid (attr)); - - attr = gck_attributes_at (attrs, 6); - g_assert (attr->type == 606); - g_assert (attr->length == 0); - g_assert (attr->value == NULL); -} - -static void -test_new_empty_attributes (void) -{ - GckAttributes *attrs = gck_attributes_new_empty (101UL, 202UL, 303UL, 404UL, GCK_INVALID); - GckAttribute *attr; - guint i; - - g_assert_cmpuint (gck_attributes_count (attrs), ==, 4); - for (i = 0; i < gck_attributes_count (attrs); ++i) { - attr = gck_attributes_at (attrs, i); - g_assert (attr->type == ((i + 1) * 100) + i + 1); - g_assert (attr->value == NULL); - g_assert (attr->length == 0); - } -} - -static void -test_add_data_attributes (void) -{ - GckAttributes *attrs; - GDate *date = g_date_new_dmy (11, 12, 2008); - attrs = gck_attributes_new (); - gck_attributes_add_boolean (attrs, 0UL, TRUE); - gck_attributes_add_ulong (attrs, 101UL, 888); - gck_attributes_add_string (attrs, 202UL, "string"); - gck_attributes_add_date (attrs, 303UL, date); - g_date_free (date); - gck_attributes_add_data (attrs, 404UL, (const guchar *)ATTR_DATA, N_ATTR_DATA); - gck_attributes_add_invalid (attrs, 505UL); - gck_attributes_add_empty (attrs, 606UL); - test_attributes_contents (attrs, TRUE); - gck_attributes_unref (attrs); -} - -static void -test_add_attributes (void) -{ - GckAttributes *attrs; - GckAttribute attr; - - GDate *date = g_date_new_dmy (11, 12, 2008); - attrs = gck_attributes_new (); - - gck_attribute_init_boolean (&attr, 0UL, TRUE); - gck_attributes_add (attrs, &attr); - gck_attribute_clear (&attr); - - gck_attribute_init_ulong (&attr, 101UL, 888); - gck_attributes_add (attrs, &attr); - gck_attribute_clear (&attr); - gck_attribute_init_string (&attr, 202UL, "string"); - gck_attributes_add (attrs, &attr); - gck_attribute_clear (&attr); - - gck_attribute_init_date (&attr, 303UL, date); - gck_attributes_add (attrs, &attr); - gck_attribute_clear (&attr); - g_date_free (date); - - gck_attribute_init (&attr, 404UL, (const guchar *)ATTR_DATA, N_ATTR_DATA); - gck_attributes_add (attrs, &attr); - gck_attribute_clear (&attr); - - gck_attribute_init_invalid (&attr, 505UL); - gck_attributes_add (attrs, &attr); - gck_attribute_clear (&attr); - - gck_attribute_init_empty (&attr, 606UL); - gck_attributes_add (attrs, &attr); - gck_attribute_clear (&attr); - - test_attributes_contents (attrs, TRUE); - gck_attributes_unref (attrs); -} - -static void -test_add_all_attributes (void) -{ - GckAttributes *attrs; - GckAttributes *copy; - GDate *date = g_date_new_dmy (11, 12, 2008); - attrs = gck_attributes_new (); - gck_attributes_add_boolean (attrs, 0UL, TRUE); - gck_attributes_add_ulong (attrs, 101UL, 888); - gck_attributes_add_string (attrs, 202UL, "string"); - gck_attributes_add_date (attrs, 303UL, date); - g_date_free (date); - gck_attributes_add_data (attrs, 404UL, (const guchar *)ATTR_DATA, N_ATTR_DATA); - gck_attributes_add_invalid (attrs, 505UL); - gck_attributes_add_empty (attrs, 606UL); - - copy = gck_attributes_new (); - gck_attributes_add_all (copy, attrs); - test_attributes_contents (copy, TRUE); - - gck_attributes_unref (attrs); - gck_attributes_unref (copy); -} - - -static void -test_find_attributes (void) -{ - GckAttribute *attr; - GDate check, *date = g_date_new_dmy (13, 12, 2008); - gboolean bvalue, ret; - gulong uvalue; - gchar *svalue; - - GckAttributes *attrs = gck_attributes_new (); - gck_attributes_add_boolean (attrs, 0UL, TRUE); - gck_attributes_add_ulong (attrs, 101UL, 888UL); - gck_attributes_add_string (attrs, 202UL, "string"); - gck_attributes_add_date (attrs, 303UL, date); - gck_attributes_add_data (attrs, 404UL, (const guchar *)ATTR_DATA, N_ATTR_DATA); - - attr = gck_attributes_find (attrs, 404); - g_assert (attr != NULL); - g_assert (attr->length == N_ATTR_DATA); - g_assert (memcmp (attr->value, ATTR_DATA, N_ATTR_DATA) == 0); - - ret = gck_attributes_find_boolean (attrs, 0UL, &bvalue); - g_assert (ret == TRUE); - g_assert (bvalue == TRUE); - - ret = gck_attributes_find_ulong (attrs, 101UL, &uvalue); - g_assert (ret == TRUE); - g_assert (uvalue == 888); - - ret = gck_attributes_find_string (attrs, 202UL, &svalue); - g_assert (ret == TRUE); - g_assert (svalue != NULL); - g_assert (strcmp (svalue, "string") == 0); - g_free (svalue); - - ret = gck_attributes_find_date (attrs, 303UL, &check); - g_assert (ret == TRUE); - g_assert (g_date_compare (date, &check) == 0); - - gck_attributes_unref (attrs); - g_date_free (date); -} - -int -main (int argc, char **argv) -{ - g_test_init (&argc, &argv, NULL); - - g_test_add_func ("/gck/attributes/init_memory", test_init_memory); - g_test_add_func ("/gck/attributes/value_to_boolean", test_value_to_boolean); - g_test_add_func ("/gck/attributes/value_to_ulong", test_value_to_ulong); - g_test_add_func ("/gck/attributes/init_boolean", test_init_boolean); - g_test_add_func ("/gck/attributes/init_date", test_init_date); - g_test_add_func ("/gck/attributes/init_ulong", test_init_ulong); - g_test_add_func ("/gck/attributes/init_string", test_init_string); - g_test_add_func ("/gck/attributes/init_invalid", test_init_invalid); - g_test_add_func ("/gck/attributes/init_empty", test_init_empty); - g_test_add_func ("/gck/attributes/new_memory", test_new_memory); - g_test_add_func ("/gck/attributes/new_boolean", test_new_boolean); - g_test_add_func ("/gck/attributes/new_date", test_new_date); - g_test_add_func ("/gck/attributes/new_ulong", test_new_ulong); - g_test_add_func ("/gck/attributes/new_string", test_new_string); - g_test_add_func ("/gck/attributes/new_invalid", test_new_invalid); - g_test_add_func ("/gck/attributes/new_empty", test_new_empty); - g_test_add_func ("/gck/attributes/get_boolean", test_get_boolean); - g_test_add_func ("/gck/attributes/get_date", test_get_date); - g_test_add_func ("/gck/attributes/get_ulong", test_get_ulong); - g_test_add_func ("/gck/attributes/get_string", test_get_string); - g_test_add_func ("/gck/attributes/dup_attribute", test_dup_attribute); - g_test_add_func ("/gck/attributes/copy_attribute", test_copy_attribute); - g_test_add_func ("/gck/attributes/new_attributes", test_new_attributes); - g_test_add_func ("/gck/attributes/new_empty_attributes", test_new_empty_attributes); - g_test_add_func ("/gck/attributes/add_data_attributes", test_add_data_attributes); - g_test_add_func ("/gck/attributes/add_attributes", test_add_attributes); - g_test_add_func ("/gck/attributes/add_all_attributes", test_add_all_attributes); - g_test_add_func ("/gck/attributes/find_attributes", test_find_attributes); - - return g_test_run (); -} diff --git a/gck/tests/test-gck-crypto.c b/gck/tests/test-gck-crypto.c deleted file mode 100644 index 9630123c..00000000 --- a/gck/tests/test-gck-crypto.c +++ /dev/null @@ -1,647 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* test-gck-crypto.c - the GObject PKCS#11 wrapper library - - Copyright (C) 2011 Collabora Ltd. - - 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 <stefw@collabora.co.uk> -*/ - -#include "config.h" - -#include "gck/gck.h" -#include "gck/gck-mock.h" -#include "gck/gck-test.h" - -#include "egg/egg-testing.h" - -#include <glib.h> - -#include <errno.h> -#include <stdlib.h> -#include <stdio.h> -#include <string.h> - -typedef struct { - GckModule *module; - GckSession *session; - GckSession *session_with_auth; -} Test; - -static gboolean -on_discard_handle_ignore (GckSession *self, CK_OBJECT_HANDLE handle, gpointer unused) -{ - /* Don't close the handle for this session, since it's a duplicate */ - return TRUE; -} - -static void -setup (Test *test, gconstpointer unused) -{ - GError *err = NULL; - GList *slots; - GckSlot *slot; - - /* Successful load */ - test->module = gck_module_initialize (BUILDDIR "/.libs/libmock-test-module.so", NULL, &err); - g_assert_no_error (err); - g_assert (GCK_IS_MODULE (test->module)); - - slots = gck_module_get_slots (test->module, TRUE); - g_assert (slots != NULL); - - test->session = gck_slot_open_session (slots->data, 0, NULL, &err); - g_assert_no_error (err); - g_assert (GCK_IS_SESSION (test->session)); - - slot = gck_session_get_slot (test->session); - g_assert (slot); - - test->session_with_auth = gck_session_from_handle (slot, gck_session_get_handle (test->session), GCK_SESSION_AUTHENTICATE); - g_signal_connect (test->session_with_auth, "discard-handle", G_CALLBACK (on_discard_handle_ignore), NULL); - g_assert (test->session_with_auth); - - g_object_unref (slot); - gck_list_unref_free (slots); -} - -static void -teardown (Test *test, gconstpointer unused) -{ - g_object_unref (test->session); - g_object_unref (test->module); - g_object_unref (test->session_with_auth); -} - -static void -fetch_async_result (GObject *source, GAsyncResult *result, gpointer user_data) -{ - *((GAsyncResult**)user_data) = result; - g_object_ref (result); - egg_test_wait_stop (); -} - -static GckObject* -find_key (GckSession *session, CK_ATTRIBUTE_TYPE method, CK_MECHANISM_TYPE mech) -{ - GList *objects, *l; - GckAttributes *attrs; - GckObject *object = NULL; - CK_MECHANISM_TYPE_PTR mechs; - gsize n_mechs; - - attrs = gck_attributes_new (); - gck_attributes_add_boolean (attrs, method, TRUE); - objects = gck_session_find_objects (session, attrs, NULL, NULL); - gck_attributes_unref (attrs); - g_assert (objects); - - for (l = objects; l; l = g_list_next (l)) { - if (mech) { - mechs = (gulong *)gck_object_get_data (l->data, CKA_ALLOWED_MECHANISMS, - NULL, &n_mechs, NULL); - g_assert (mechs); - g_assert (n_mechs == sizeof (CK_MECHANISM_TYPE)); - /* We know all of them only have one allowed mech */ - if (*mechs != mech) - continue; - } - object = l->data; - g_object_ref (object); - break; - } - - gck_list_unref_free (objects); - return object; -} - -static GckObject* -find_key_with_value (GckSession *session, const gchar *value) -{ - GList *objects; - GckAttributes *attrs; - GckObject *object; - - attrs = gck_attributes_new (); - gck_attributes_add_string (attrs, CKA_VALUE, value); - objects = gck_session_find_objects (session, attrs, NULL, NULL); - gck_attributes_unref (attrs); - g_assert (objects); - - object = g_object_ref (objects->data); - gck_list_unref_free (objects); - return object; -} - -static void -check_key_with_value (GckSession *session, GckObject *key, CK_OBJECT_CLASS klass, const gchar *value) -{ - GckAttributes *attrs; - GckAttribute *attr; - gulong check; - - attrs = gck_object_get (key, NULL, NULL, CKA_CLASS, CKA_VALUE, GCK_INVALID); - g_assert (attrs); - - if (!gck_attributes_find_ulong (attrs, CKA_CLASS, &check)) - g_assert_not_reached (); - g_assert (check == klass); - - attr = gck_attributes_find (attrs, CKA_VALUE); - g_assert (attr); - g_assert (!gck_attribute_is_invalid (attr)); - egg_assert_cmpsize (attr->length, ==, strlen (value)); - g_assert (memcmp (attr->value, value, attr->length) == 0); - - gck_attributes_unref (attrs); -} - -static gboolean -authenticate_object (GckSlot *module, GckObject *object, gchar *label, gchar **password) -{ - g_assert (GCK_IS_MODULE (module)); - g_assert (GCK_IS_OBJECT (object)); - g_assert (password); - g_assert (!*password); - - *password = g_strdup ("booo"); - return TRUE; -} - -static void -test_encrypt (Test *test, gconstpointer unused) -{ - GckMechanism mech = { CKM_MOCK_CAPITALIZE, NULL, 0 }; - GError *error = NULL; - GAsyncResult *result = NULL; - GckObject *key; - guchar *output; - gsize n_output; - - /* Find the right key */ - key = find_key (test->session, CKA_ENCRYPT, CKM_MOCK_CAPITALIZE); - g_assert (key); - - /* Simple one */ - output = gck_session_encrypt (test->session, key, CKM_MOCK_CAPITALIZE, (const guchar*)"blah blah", 10, &n_output, NULL, &error); - g_assert_no_error (error); - g_assert (output); - g_assert (n_output == 10); - g_assert_cmpstr ((gchar*)output, ==, "BLAH BLAH"); - g_free (output); - - /* Asynchronous one */ - gck_session_encrypt_async (test->session, key, &mech, (const guchar*)"second chance", 14, NULL, fetch_async_result, &result); - - egg_test_wait_until (500); - g_assert (result != NULL); - - /* Get the result */ - output = gck_session_encrypt_finish (test->session, result, &n_output, &error); - g_assert_no_error (error); - g_assert (output); - g_assert (n_output == 14); - g_assert_cmpstr ((gchar*)output, ==, "SECOND CHANCE"); - g_free (output); - - g_object_unref (result); - g_object_unref (key); -} - -static void -test_decrypt (Test *test, gconstpointer unused) -{ - GckMechanism mech = { CKM_MOCK_CAPITALIZE, NULL, 0 }; - GError *error = NULL; - GAsyncResult *result = NULL; - GckObject *key; - guchar *output; - gsize n_output; - - /* Find the right key */ - key = find_key (test->session, CKA_DECRYPT, CKM_MOCK_CAPITALIZE); - g_assert (key); - - /* Simple one */ - output = gck_session_decrypt (test->session, key, CKM_MOCK_CAPITALIZE, (const guchar*)"FRY???", 7, &n_output, NULL, &error); - g_assert_no_error (error); - g_assert (output); - g_assert (n_output == 7); - g_assert_cmpstr ((gchar*)output, ==, "fry???"); - g_free (output); - - /* Asynchronous one */ - gck_session_decrypt_async (test->session, key, &mech, (const guchar*)"FAT CHANCE", 11, NULL, fetch_async_result, &result); - - egg_test_wait_until (500); - g_assert (result != NULL); - - /* Get the result */ - output = gck_session_decrypt_finish (test->session, result, &n_output, &error); - g_assert_no_error (error); - g_assert (output); - g_assert (n_output == 11); - g_assert_cmpstr ((gchar*)output, ==, "fat chance"); - g_free (output); - - g_object_unref (result); - g_object_unref (key); -} - -static void -test_login_context_specific (Test *test, gconstpointer unused) -{ - /* The test module won't let us sign without doing a login, check that */ - - GError *error = NULL; - GckObject *key; - guchar *output; - gsize n_output; - - /* Find the right key */ - key = find_key (test->session, CKA_SIGN, CKM_MOCK_PREFIX); - g_assert (key); - - /* Simple one */ - output = gck_session_sign (test->session, key, CKM_MOCK_PREFIX, (const guchar*)"TV Monster", 11, &n_output, NULL, &error); - g_assert_error (error, GCK_ERROR, CKR_USER_NOT_LOGGED_IN); - g_assert (output == NULL); - - g_object_unref (key); -} - -static void -test_sign (Test *test, gconstpointer unused) -{ - GckMechanism mech = { CKM_MOCK_PREFIX, (guchar *)"my-prefix:", 10 }; - GError *error = NULL; - GAsyncResult *result = NULL; - GckObject *key; - guchar *output; - gsize n_output; - - /* Enable auto-login on this test->session, see previous test */ - g_signal_connect (test->module, "authenticate-object", G_CALLBACK (authenticate_object), NULL); - - /* Find the right key */ - key = find_key (test->session_with_auth, CKA_SIGN, CKM_MOCK_PREFIX); - g_assert (key); - - /* Simple one */ - output = gck_session_sign (test->session_with_auth, key, CKM_MOCK_PREFIX, (const guchar*)"Labarbara", 10, &n_output, NULL, &error); - g_assert_no_error (error); - g_assert (output); - g_assert_cmpuint (n_output, ==, 24); - g_assert_cmpstr ((gchar*)output, ==, "signed-prefix:Labarbara"); - g_free (output); - - /* Asynchronous one */ - gck_session_sign_async (test->session_with_auth, key, &mech, (const guchar*)"Conrad", 7, NULL, fetch_async_result, &result); - - egg_test_wait_until (500); - g_assert (result != NULL); - - /* Get the result */ - output = gck_session_sign_finish (test->session_with_auth, result, &n_output, &error); - g_assert_no_error (error); - g_assert (output); - g_assert_cmpuint (n_output, ==, 17); - g_assert_cmpstr ((gchar*)output, ==, "my-prefix:Conrad"); - g_free (output); - - g_object_unref (result); - g_object_unref (key); -} - -static void -test_verify (Test *test, gconstpointer unused) -{ - GckMechanism mech = { CKM_MOCK_PREFIX, (guchar *)"my-prefix:", 10 }; - GError *error = NULL; - GAsyncResult *result = NULL; - GckObject *key; - gboolean ret; - - /* Enable auto-login on this session, shouldn't be needed */ - g_signal_connect (test->module, "authenticate-object", G_CALLBACK (authenticate_object), NULL); - - /* Find the right key */ - key = find_key (test->session, CKA_VERIFY, CKM_MOCK_PREFIX); - g_assert (key); - - /* Simple one */ - ret = gck_session_verify (test->session, key, CKM_MOCK_PREFIX, (const guchar*)"Labarbara", 10, - (const guchar*)"signed-prefix:Labarbara", 24, NULL, &error); - g_assert_no_error (error); - g_assert (ret); - - /* Failure one */ - ret = gck_session_verify_full (test->session, key, &mech, (const guchar*)"Labarbara", 10, - (const guchar*)"my-prefix:Loborboro", 20, NULL, &error); - g_assert (error != NULL); - g_assert (!ret); - g_clear_error (&error); - - /* Asynchronous one */ - gck_session_verify_async (test->session, key, &mech, (const guchar*)"Labarbara", 10, - (const guchar*)"my-prefix:Labarbara", 20, NULL, fetch_async_result, &result); - egg_test_wait_until (500); - g_assert (result != NULL); - ret = gck_session_verify_finish (test->session, result, &error); - g_assert_no_error (error); - g_assert (ret); - g_object_unref (result); - - /* Asynchronous failure */ - result = NULL; - gck_session_verify_async (test->session, key, &mech, (const guchar*)"Labarbara", 10, - (const guchar*)"my-prefix:Labarxoro", 20, NULL, fetch_async_result, &result); - egg_test_wait_until (500); - g_assert (result != NULL); - ret = gck_session_verify_finish (test->session, result, &error); - g_assert (error != NULL); - g_assert (!ret); - g_clear_error (&error); - g_object_unref (result); - - g_object_unref (key); -} - -static void -test_generate_key_pair (Test *test, gconstpointer unused) -{ - GckMechanism mech = { CKM_MOCK_GENERATE, (guchar *)"generate", 9 }; - GckAttributes *pub_attrs, *prv_attrs; - GError *error = NULL; - GAsyncResult *result = NULL; - GckObject *pub_key, *prv_key; - gboolean ret; - - pub_attrs = gck_attributes_new (); - gck_attributes_add_ulong (pub_attrs, CKA_CLASS, CKO_PUBLIC_KEY); - prv_attrs = gck_attributes_new (); - gck_attributes_add_ulong (prv_attrs, CKA_CLASS, CKO_PRIVATE_KEY); - - /* Full One*/ - ret = gck_session_generate_key_pair_full (test->session, &mech, pub_attrs, prv_attrs, - &pub_key, &prv_key, NULL, &error); - g_assert_no_error (error); - g_assert (ret); - g_object_unref (pub_key); - g_object_unref (prv_key); - - /* Failure one */ - mech.type = 0; - pub_key = prv_key = NULL; - ret = gck_session_generate_key_pair_full (test->session, &mech, pub_attrs, prv_attrs, - &pub_key, &prv_key, NULL, &error); - g_assert (error != NULL); - g_assert (!ret); - g_clear_error (&error); - g_assert (pub_key == NULL); - g_assert (prv_key == NULL); - - /* Asynchronous one */ - mech.type = CKM_MOCK_GENERATE; - gck_session_generate_key_pair_async (test->session, &mech, pub_attrs, prv_attrs, NULL, fetch_async_result, &result); - egg_test_wait_until (500); - g_assert (result != NULL); - ret = gck_session_generate_key_pair_finish (test->session, result, &pub_key, &prv_key, &error); - g_assert_no_error (error); - g_assert (ret); - g_object_unref (result); - g_object_unref (pub_key); - g_object_unref (prv_key); - - /* Asynchronous failure */ - result = NULL; - mech.type = 0; - pub_key = prv_key = NULL; - gck_session_generate_key_pair_async (test->session, &mech, pub_attrs, prv_attrs, NULL, fetch_async_result, &result); - egg_test_wait_until (500); - g_assert (result != NULL); - ret = gck_session_generate_key_pair_finish (test->session, result, &pub_key, &prv_key, &error); - g_assert (error != NULL); - g_assert (!ret); - g_clear_error (&error); - g_object_unref (result); - g_assert (pub_key == NULL); - g_assert (prv_key == NULL); - - gck_attributes_unref (pub_attrs); - gck_attributes_unref (prv_attrs); -} - -static void -test_wrap_key (Test *test, gconstpointer unused) -{ - GckMechanism mech = { CKM_MOCK_WRAP, (guchar *)"wrap", 4 }; - GError *error = NULL; - GAsyncResult *result = NULL; - GckObject *wrapper, *wrapped; - gpointer output; - gsize n_output; - - wrapper = find_key (test->session, CKA_WRAP, 0); - wrapped = find_key_with_value (test->session, "value"); - - /* Simple One */ - output = gck_session_wrap_key (test->session, wrapper, CKM_MOCK_WRAP, wrapped, &n_output, NULL, &error); - g_assert_no_error (error); - g_assert (output); - egg_assert_cmpsize (n_output, ==, 5); - g_assert (memcmp (output, "value", 5) == 0); - g_free (output); - - /* Full One*/ - output = gck_session_wrap_key_full (test->session, wrapper, &mech, wrapped, &n_output, NULL, &error); - g_assert_no_error (error); - g_assert (output); - egg_assert_cmpsize (n_output, ==, 5); - g_assert (memcmp (output, "value", 5) == 0); - g_free (output); - - /* Failure one */ - mech.type = 0; - n_output = 0; - output = gck_session_wrap_key_full (test->session, wrapper, &mech, wrapped, &n_output, NULL, &error); - g_assert (error != NULL); - g_assert (!output); - g_clear_error (&error); - egg_assert_cmpsize (n_output, ==, 0); - - /* Asynchronous one */ - mech.type = CKM_MOCK_WRAP; - gck_session_wrap_key_async (test->session, wrapper, &mech, wrapped, NULL, fetch_async_result, &result); - egg_test_wait_until (500); - g_assert (result != NULL); - output = gck_session_wrap_key_finish (test->session, result, &n_output, &error); - g_assert_no_error (error); - g_assert (output); - egg_assert_cmpsize (n_output, ==, 5); - g_assert (memcmp (output, "value", 5) == 0); - g_object_unref (result); - g_free (output); - - /* Asynchronous failure */ - result = NULL; - mech.type = 0; - n_output = 0; - gck_session_wrap_key_async (test->session, wrapper, &mech, wrapped, NULL, fetch_async_result, &result); - egg_test_wait_until (500); - g_assert (result != NULL); - output = gck_session_wrap_key_finish (test->session, result, &n_output, &error); - g_assert (error != NULL); - g_assert (!output); - g_clear_error (&error); - egg_assert_cmpsize (n_output, ==, 0); - g_object_unref (result); - - g_object_unref (wrapper); - g_object_unref (wrapped); -} - -static void -test_unwrap_key (Test *test, gconstpointer unused) -{ - GckMechanism mech = { CKM_MOCK_WRAP, (guchar *)"wrap", 4 }; - GError *error = NULL; - GAsyncResult *result = NULL; - GckObject *wrapper, *unwrapped; - GckAttributes *attrs; - - wrapper = find_key (test->session, CKA_UNWRAP, 0); - attrs = gck_attributes_new (); - gck_attributes_add_ulong (attrs, CKA_CLASS, CKO_SECRET_KEY); - - /* Full One*/ - unwrapped = gck_session_unwrap_key_full (test->session, wrapper, &mech, (const guchar *)"special", 7, attrs, NULL, &error); - g_assert_no_error (error); - g_assert (GCK_IS_OBJECT (unwrapped)); - check_key_with_value (test->session, unwrapped, CKO_SECRET_KEY, "special"); - g_object_unref (unwrapped); - - /* Failure one */ - mech.type = 0; - unwrapped = gck_session_unwrap_key_full (test->session, wrapper, &mech, (const guchar *)"special", 7, attrs, NULL, &error); - g_assert (error != NULL); - g_assert (!unwrapped); - g_clear_error (&error); - - /* Asynchronous one */ - mech.type = CKM_MOCK_WRAP; - gck_session_unwrap_key_async (test->session, wrapper, &mech, (const guchar *)"special", 7, attrs, NULL, fetch_async_result, &result); - egg_test_wait_until (500); - g_assert (result != NULL); - unwrapped = gck_session_unwrap_key_finish (test->session, result, &error); - g_assert_no_error (error); - g_assert (GCK_IS_OBJECT (unwrapped)); - check_key_with_value (test->session, unwrapped, CKO_SECRET_KEY, "special"); - g_object_unref (unwrapped); - g_object_unref (result); - - /* Asynchronous failure */ - result = NULL; - mech.type = 0; - gck_session_unwrap_key_async (test->session, wrapper, &mech, (const guchar *)"special", 6, attrs, NULL, fetch_async_result, &result); - egg_test_wait_until (500); - g_assert (result != NULL); - unwrapped = gck_session_unwrap_key_finish (test->session, result, &error); - g_assert (error != NULL); - g_assert (!unwrapped); - g_clear_error (&error); - g_object_unref (result); - - g_object_unref (wrapper); - gck_attributes_unref (attrs); -} - -static void -test_derive_key (Test *test, gconstpointer unused) -{ - GckMechanism mech = { CKM_MOCK_DERIVE, (guchar *)"derive", 6 }; - GError *error = NULL; - GAsyncResult *result = NULL; - GckObject *wrapper, *derived; - GckAttributes *attrs; - - wrapper = find_key (test->session, CKA_DERIVE, 0); - attrs = gck_attributes_new (); - gck_attributes_add_ulong (attrs, CKA_CLASS, CKO_SECRET_KEY); - - /* Full One*/ - derived = gck_session_derive_key_full (test->session, wrapper, &mech, attrs, NULL, &error); - g_assert_no_error (error); - g_assert (GCK_IS_OBJECT (derived)); - check_key_with_value (test->session, derived, CKO_SECRET_KEY, "derived"); - g_object_unref (derived); - - /* Failure one */ - mech.type = 0; - derived = gck_session_derive_key_full (test->session, wrapper, &mech, attrs, NULL, &error); - g_assert (error != NULL); - g_assert (!derived); - g_clear_error (&error); - - /* Asynchronous one */ - mech.type = CKM_MOCK_DERIVE; - gck_session_derive_key_async (test->session, wrapper, &mech, attrs, NULL, fetch_async_result, &result); - egg_test_wait_until (500); - g_assert (result != NULL); - derived = gck_session_derive_key_finish (test->session, result, &error); - g_assert_no_error (error); - g_assert (GCK_IS_OBJECT (derived)); - check_key_with_value (test->session, derived, CKO_SECRET_KEY, "derived"); - g_object_unref (derived); - g_object_unref (result); - - /* Asynchronous failure */ - result = NULL; - mech.type = 0; - gck_session_derive_key_async (test->session, wrapper, &mech, attrs, NULL, fetch_async_result, &result); - egg_test_wait_until (500); - g_assert (result != NULL); - derived = gck_session_derive_key_finish (test->session, result, &error); - g_assert (error != NULL); - g_assert (!derived); - g_clear_error (&error); - g_object_unref (result); - - g_object_unref (wrapper); - gck_attributes_unref (attrs); -} - -int -main (int argc, char **argv) -{ - g_type_init (); - g_test_init (&argc, &argv, NULL); - - g_set_prgname ("test-gck-crypto"); - - g_test_add ("/gck/crypto/encrypt", Test, NULL, setup, test_encrypt, teardown); - g_test_add ("/gck/crypto/decrypt", Test, NULL, setup, test_decrypt, teardown); - g_test_add ("/gck/crypto/login_context_specific", Test, NULL, setup, test_login_context_specific, teardown); - g_test_add ("/gck/crypto/sign", Test, NULL, setup, test_sign, teardown); - g_test_add ("/gck/crypto/verify", Test, NULL, setup, test_verify, teardown); - g_test_add ("/gck/crypto/generate_key_pair", Test, NULL, setup, test_generate_key_pair, teardown); - g_test_add ("/gck/crypto/wrap_key", Test, NULL, setup, test_wrap_key, teardown); - g_test_add ("/gck/crypto/unwrap_key", Test, NULL, setup, test_unwrap_key, teardown); - g_test_add ("/gck/crypto/derive_key", Test, NULL, setup, test_derive_key, teardown); - - return egg_tests_run_in_thread_with_loop (); -} diff --git a/gck/tests/test-gck-enumerator.c b/gck/tests/test-gck-enumerator.c deleted file mode 100644 index d5d669e3..00000000 --- a/gck/tests/test-gck-enumerator.c +++ /dev/null @@ -1,359 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* test-gck-enumerator.c - the GObject PKCS#11 wrapper library - - Copyright (C) 2011 Collabora Ltd. - - 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 <stefw@collabora.co.uk> -*/ - -#include "config.h" - -#include "gck/gck.h" -#include "gck/gck-mock.h" -#include "gck/gck-private.h" -#include "gck/gck-test.h" - -#include "mock-interaction.h" - -#include "egg/egg-testing.h" - -#include <glib.h> - -#include <errno.h> -#include <stdlib.h> -#include <stdio.h> -#include <string.h> - -typedef struct { - GList *modules; - GckModule *module; -} Test; - -static void -setup (Test *test, gconstpointer unused) -{ - GError *err = NULL; - - /* Successful load */ - test->module = gck_module_initialize (BUILDDIR "/.libs/libmock-test-module.so", NULL, &err); - g_assert_no_error (err); - g_assert (GCK_IS_MODULE (test->module)); - - test->modules = g_list_append (NULL, g_object_ref (test->module)); -} - -static void -teardown (Test *test, gconstpointer unused) -{ - gck_list_unref_free (test->modules); - test->modules = NULL; - - g_object_unref (test->module); - test->module = NULL; -} - -static void -test_create (Test *test, gconstpointer unused) -{ - GckUriData *uri_data; - GckEnumerator *en; - - uri_data = gck_uri_data_new (); - en = _gck_enumerator_new (test->modules, 0, uri_data); - g_assert (GCK_IS_ENUMERATOR (en)); - g_object_unref (en); -} - -static void -test_create_slots (Test *test, gconstpointer unused) -{ - GckUriData *uri_data; - GckEnumerator *en; - GList *slots; - - uri_data = gck_uri_data_new (); - slots = gck_module_get_slots (test->module, FALSE); - en = _gck_enumerator_new (slots, 0, uri_data); - g_assert (GCK_IS_ENUMERATOR (en)); - g_object_unref (en); - gck_list_unref_free (slots); -} - -static void -test_next (Test *test, gconstpointer unused) -{ - GckUriData *uri_data; - GError *error = NULL; - GckEnumerator *en; - GckObject *obj; - - uri_data = gck_uri_data_new (); - en = _gck_enumerator_new (test->modules, 0, uri_data); - g_assert (GCK_IS_ENUMERATOR (en)); - - obj = gck_enumerator_next (en, NULL, &error); - g_assert (GCK_IS_OBJECT (obj)); - - g_object_unref (obj); - g_object_unref (en); -} - -static void -test_next_slots (Test *test, gconstpointer unused) -{ - GckUriData *uri_data; - GError *error = NULL; - GList *slots = NULL; - GckEnumerator *en; - GckObject *obj; - - uri_data = gck_uri_data_new (); - slots = gck_module_get_slots (test->module, FALSE); - en = _gck_enumerator_new (slots, 0, uri_data); - g_assert (GCK_IS_ENUMERATOR (en)); - - obj = gck_enumerator_next (en, NULL, &error); - g_assert (GCK_IS_OBJECT (obj)); - - g_object_unref (obj); - g_object_unref (en); - gck_list_unref_free (slots); -} - -static void -test_next_and_resume (Test *test, gconstpointer unused) -{ - GckUriData *uri_data; - GError *error = NULL; - GckEnumerator *en; - GckObject *obj, *obj2; - - uri_data = gck_uri_data_new (); - en = _gck_enumerator_new (test->modules, 0, uri_data); - g_assert (GCK_IS_ENUMERATOR (en)); - - obj = gck_enumerator_next (en, NULL, &error); - g_assert_no_error (error); - g_assert (GCK_IS_OBJECT (obj)); - - obj2 = gck_enumerator_next (en, NULL, &error); - g_assert_no_error (error); - g_assert (GCK_IS_OBJECT (obj2)); - - g_assert (!gck_object_equal (obj, obj2)); - - g_object_unref (obj); - g_object_unref (obj2); - g_object_unref (en); -} - -static void -test_next_n (Test *test, gconstpointer unused) -{ - GckUriData *uri_data; - GError *error = NULL; - GckEnumerator *en; - GList *objects, *l; - - uri_data = gck_uri_data_new (); - en = _gck_enumerator_new (test->modules, 0, uri_data); - g_assert (GCK_IS_ENUMERATOR (en)); - - objects = gck_enumerator_next_n (en, -1, NULL, &error); - g_assert_no_error (error); - g_assert_cmpint (g_list_length (objects), ==, 5); - for (l = objects; l; l = g_list_next (l)) - g_assert (GCK_IS_OBJECT (l->data)); - - gck_list_unref_free (objects); - g_object_unref (en); -} - -static void -fetch_async_result (GObject *source, GAsyncResult *result, gpointer user_data) -{ - *((GAsyncResult**)user_data) = result; - g_object_ref (result); - egg_test_wait_stop (); -} - -static void -test_next_async (Test *test, gconstpointer unused) -{ - GckUriData *uri_data; - GAsyncResult *result = NULL; - GError *error = NULL; - GckEnumerator *en; - GList *objects, *l; - - uri_data = gck_uri_data_new (); - en = _gck_enumerator_new (test->modules, 0, uri_data); - g_assert (GCK_IS_ENUMERATOR (en)); - - gck_enumerator_next_async (en, -1, NULL, fetch_async_result, &result); - egg_test_wait_until (500); - g_assert (result); - - objects = gck_enumerator_next_finish (en, result, &error); - g_assert_no_error (error); - g_assert_cmpint (g_list_length (objects), ==, 5); - for (l = objects; l; l = g_list_next (l)) - g_assert (GCK_IS_OBJECT (l->data)); - - g_object_unref (result); - gck_list_unref_free (objects); - g_object_unref (en); -} - -static void -test_attributes (Test *test, gconstpointer unused) -{ - GckUriData *uri_data; - GError *error = NULL; - GckEnumerator *en; - GList *objects; - - uri_data = gck_uri_data_new (); - uri_data->attributes = gck_attributes_new (); - gck_attributes_add_string (uri_data->attributes, CKA_LABEL, "Private Capitalize Key"); - en = _gck_enumerator_new (test->modules, 0, uri_data); - g_assert (GCK_IS_ENUMERATOR (en)); - - objects = gck_enumerator_next_n (en, -1, NULL, &error); - g_assert_no_error (error); - g_assert_cmpint (g_list_length (objects), ==, 1); - g_assert (GCK_IS_OBJECT (objects->data)); - - gck_list_unref_free (objects); - g_object_unref (en); -} - -static void -test_authenticate_interaction (Test *test, - gconstpointer unused) -{ - GTlsInteraction *interaction; - GTlsInteraction *check; - GckUriData *uri_data; - GError *error = NULL; - GckEnumerator *en; - GckObject *obj; - - uri_data = gck_uri_data_new (); - en = _gck_enumerator_new (test->modules, GCK_SESSION_LOGIN_USER, uri_data); - g_assert (GCK_IS_ENUMERATOR (en)); - - interaction = mock_interaction_new ("booo"); - g_object_set (en, "interaction", interaction, NULL); - - check = NULL; - g_object_get (en, "interaction", &check, NULL); - g_assert (interaction == check); - g_object_unref (interaction); - - obj = gck_enumerator_next (en, NULL, &error); - g_assert (GCK_IS_OBJECT (obj)); - - g_object_unref (obj); - g_object_unref (en); -} - -static gboolean -on_authenticate_token (GckModule *module, - GckSlot *slot, - gchar *label, - gchar **password, - gpointer unused) -{ - g_assert (unused == GUINT_TO_POINTER (35)); - g_assert (password != NULL); - g_assert (*password == NULL); - g_assert (GCK_IS_MODULE (module)); - g_assert (GCK_IS_SLOT (slot)); - - *password = g_strdup ("booo"); - return TRUE; -} - -static void -test_authenticate_compat (Test *test, - gconstpointer unused) -{ - GckUriData *uri_data; - GError *error = NULL; - GckEnumerator *en; - GckObject *obj; - - g_signal_connect (test->modules->data, "authenticate-slot", - G_CALLBACK (on_authenticate_token), GUINT_TO_POINTER (35)); - - uri_data = gck_uri_data_new (); - en = _gck_enumerator_new (test->modules, GCK_SESSION_LOGIN_USER, uri_data); - g_assert (GCK_IS_ENUMERATOR (en)); - - obj = gck_enumerator_next (en, NULL, &error); - g_assert (GCK_IS_OBJECT (obj)); - - g_object_unref (obj); - g_object_unref (en); -} - -static void -test_token_match (Test *test, gconstpointer unused) -{ - GckUriData *uri_data; - GError *error = NULL; - GckEnumerator *en; - GList *objects; - - uri_data = gck_uri_data_new (); - uri_data->token_info = g_new0 (GckTokenInfo, 1); - uri_data->token_info->label = g_strdup ("Invalid token name"); - en = _gck_enumerator_new (test->modules, 0, uri_data); - g_assert (GCK_IS_ENUMERATOR (en)); - - objects = gck_enumerator_next_n (en, -1, NULL, &error); - g_assert_cmpint (g_list_length (objects), ==, 0); - g_assert (error == NULL); - - gck_list_unref_free (objects); - g_object_unref (en); -} - -int -main (int argc, char **argv) -{ - g_type_init (); - g_test_init (&argc, &argv, NULL); - - g_set_prgname ("test-gck-enumerator"); - - g_test_add ("/gck/enumerator/create", Test, NULL, setup, test_create, teardown); - g_test_add ("/gck/enumerator/create_slots", Test, NULL, setup, test_create_slots, teardown); - g_test_add ("/gck/enumerator/next", Test, NULL, setup, test_next, teardown); - g_test_add ("/gck/enumerator/next_slots", Test, NULL, setup, test_next_slots, teardown); - g_test_add ("/gck/enumerator/next_and_resume", Test, NULL, setup, test_next_and_resume, teardown); - g_test_add ("/gck/enumerator/next_n", Test, NULL, setup, test_next_n, teardown); - g_test_add ("/gck/enumerator/next_async", Test, NULL, setup, test_next_async, teardown); - g_test_add ("/gck/enumerator/authenticate-interaction", Test, NULL, setup, test_authenticate_interaction, teardown); - g_test_add ("/gck/enumerator/authenticate-compat", Test, NULL, setup, test_authenticate_compat, teardown); - g_test_add ("/gck/enumerator/attributes", Test, NULL, setup, test_attributes, teardown); - g_test_add ("/gck/enumerator/token_match", Test, NULL, setup, test_token_match, teardown); - - return egg_tests_run_in_thread_with_loop (); -} diff --git a/gck/tests/test-gck-module.c b/gck/tests/test-gck-module.c deleted file mode 100644 index b5db530a..00000000 --- a/gck/tests/test-gck-module.c +++ /dev/null @@ -1,176 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* test-gck-module.c - the GObject PKCS#11 wrapper library - - Copyright (C) 2011 Collabora Ltd. - - 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 <stefw@collabora.co.uk> -*/ - -#include "config.h" - -#include <errno.h> -#include <glib.h> -#include <string.h> - -#include "egg/egg-testing.h" - -#include "gck/gck.h" -#include "gck/gck-test.h" - -typedef struct { - GckModule *module; -} Test; - -static void -setup (Test *test, gconstpointer unused) -{ - GError *err = NULL; - - /* Successful load */ - test->module = gck_module_initialize (BUILDDIR "/.libs/libmock-test-module.so", NULL, &err); - g_assert_no_error (err); - g_assert (test->module); -} - -static void -teardown (Test *test, gconstpointer unused) -{ - g_object_unref (test->module); -} - -static void -fetch_async_result (GObject *source, GAsyncResult *result, gpointer user_data) -{ - *((GAsyncResult**)user_data) = result; - g_object_ref (result); - egg_test_wait_stop (); -} - -static void -test_initialize_async (void) -{ - GckModule *module; - GAsyncResult *result; - GError *error = NULL; - - /* Shouldn't be able to load modules */ - gck_module_initialize_async (BUILDDIR "/.libs/libmock-test-module.so", - NULL, fetch_async_result, &result); - - egg_test_wait_until (500); - g_assert (result != NULL); - - /* Get the result */ - module = gck_module_initialize_finish (result, &error); - g_assert_no_error (error); - g_assert (GCK_IS_MODULE (module)); - - g_object_unref (result); - g_object_unref (module); -} - - -static void -test_invalid_modules (Test *test, gconstpointer unused) -{ - GckModule *invalid; - GError *error = NULL; - - /* Shouldn't be able to load modules */ - invalid = gck_module_initialize ("blah-blah-non-existant", NULL, &error); - g_assert_error (error, GCK_ERROR, (int)CKR_GCK_MODULE_PROBLEM); - g_assert (invalid == NULL); - - g_clear_error (&error); - - /* Shouldn't be able to load any file successfully */ - invalid = gck_module_initialize ("/usr/lib/libm.so", NULL, &error); - g_assert_error (error, GCK_ERROR, (int)CKR_GCK_MODULE_PROBLEM); - g_assert (invalid == NULL); - - g_clear_error (&error); -} - -static void -test_module_equals_hash (Test *test, gconstpointer unused) -{ - GckModule *other; - GObject *obj; - guint hash; - - hash = gck_module_hash (test->module); - g_assert (hash != 0); - - g_assert (gck_module_equal (test->module, test->module)); - - other = gck_module_new (gck_module_get_functions (test->module)); - obj = g_object_new (G_TYPE_OBJECT, NULL); - - g_assert (gck_module_equal (test->module, other)); - - /* TODO: Could do with another test for inequality */ - g_assert (!gck_module_equal (test->module, obj)); - - g_object_unref (other); - g_object_unref (obj); -} - -static void -test_module_props (Test *test, gconstpointer unused) -{ - gchar *path; - - g_object_get (test->module, "path", &path, NULL); - g_assert (path != NULL && "no module-path"); - g_assert (strcmp (BUILDDIR "/.libs/libmock-test-module.so", path) == 0 && "module path wrong"); - g_free (path); -} - -static void -test_module_info (Test *test, gconstpointer unused) -{ - GckModuleInfo *info; - - info = gck_module_get_info (test->module); - g_assert (info != NULL && "no module info"); - - g_assert (info->pkcs11_version_major == CRYPTOKI_VERSION_MAJOR && "wrong major version"); - g_assert (info->pkcs11_version_minor == CRYPTOKI_VERSION_MINOR && "wrong minor version"); - g_assert (strcmp ("TEST MANUFACTURER", info->manufacturer_id) == 0); - g_assert (strcmp ("TEST LIBRARY", info->library_description) == 0); - g_assert (0 == info->flags); - g_assert (45 == info->library_version_major); - g_assert (145 == info->library_version_minor); - - gck_module_info_free (info); -} - -int -main (int argc, char **argv) -{ - g_type_init (); - g_test_init (&argc, &argv, NULL); - - g_test_add_func ("/gck/module/initialize_async", test_initialize_async); - g_test_add ("/gck/module/invalid_modules", Test, NULL, setup, test_invalid_modules, teardown); - g_test_add ("/gck/module/module_equals_hash", Test, NULL, setup, test_module_equals_hash, teardown); - g_test_add ("/gck/module/module_props", Test, NULL, setup, test_module_props, teardown); - g_test_add ("/gck/module/module_info", Test, NULL, setup, test_module_info, teardown); - - return egg_tests_run_in_thread_with_loop (); -} diff --git a/gck/tests/test-gck-modules.c b/gck/tests/test-gck-modules.c deleted file mode 100644 index 246c771f..00000000 --- a/gck/tests/test-gck-modules.c +++ /dev/null @@ -1,210 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* test-gck-modules.c - the GObject PKCS#11 wrapper library - - Copyright (C) 2011 Collabora Ltd. - - 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 <stefw@collabora.co.uk> -*/ - -#include "config.h" - -#include "gck/gck.h" -#include "gck/gck-mock.h" -#include "gck/gck-private.h" -#include "gck/gck-test.h" - -#include "egg/egg-testing.h" - -#include <glib.h> - -#include <errno.h> -#include <stdlib.h> -#include <stdio.h> -#include <string.h> - -typedef struct { - GList *modules; -} Test; - -static void -setup (Test *test, gconstpointer unused) -{ - GckModule *module; - GError *err = NULL; - - /* Successful load */ - module = gck_module_initialize (BUILDDIR "/.libs/libmock-test-module.so", NULL, &err); - g_assert_no_error (err); - g_assert (GCK_IS_MODULE (module)); - - test->modules = g_list_append (NULL, module); -} - -static void -teardown (Test *test, gconstpointer unused) -{ - gck_list_unref_free (test->modules); - test->modules = NULL; -} - -static void -test_enumerate_objects (Test *test, gconstpointer unused) -{ - GckAttributes *attrs; - GError *error = NULL; - GckEnumerator *en; - GList *objects; - - attrs = gck_attributes_new (); - gck_attributes_add_string (attrs, CKA_LABEL, "Private Capitalize Key"); - en = gck_modules_enumerate_objects (test->modules, attrs, 0); - g_assert (GCK_IS_ENUMERATOR (en)); - gck_attributes_unref (attrs); - - objects = gck_enumerator_next_n (en, -1, NULL, &error); - g_assert_no_error (error); - g_assert_cmpint (g_list_length (objects), ==, 1); - g_assert (GCK_IS_OBJECT (objects->data)); - - gck_list_unref_free (objects); - g_object_unref (en); -} - - -static void -test_token_for_uri (Test *test, gconstpointer unused) -{ - GckSlot *slot; - GError *error = NULL; - - slot = gck_modules_token_for_uri (test->modules, "pkcs11:token=TEST%20LABEL", &error); - g_assert (GCK_IS_SLOT (slot)); - - g_object_unref (slot); -} - -static void -test_token_for_uri_not_found (Test *test, gconstpointer unused) -{ - GckSlot *slot; - GError *error = NULL; - - slot = gck_modules_token_for_uri (test->modules, "pkcs11:token=UNKNOWN", &error); - g_assert (slot == NULL); - g_assert (error == NULL); -} - -static void -test_token_for_uri_error (Test *test, gconstpointer unused) -{ - GckSlot *slot; - GError *error = NULL; - - slot = gck_modules_token_for_uri (test->modules, "http://invalid.uri", &error); - g_assert (slot == NULL); - g_assert (error != NULL); - g_assert (g_error_matches (error, GCK_URI_ERROR, GCK_URI_BAD_PREFIX)); - g_error_free (error); -} - -static void -test_object_for_uri (Test *test, gconstpointer unused) -{ - GckObject *object; - GError *error = NULL; - - object = gck_modules_object_for_uri (test->modules, "pkcs11:object=Public%20Capitalize%20Key;objecttype=public", 0, &error); - g_assert (GCK_IS_OBJECT (object)); - g_object_unref (object); -} - -static void -test_object_for_uri_not_found (Test *test, gconstpointer unused) -{ - GckObject *object; - GError *error = NULL; - - object = gck_modules_object_for_uri (test->modules, "pkcs11:object=Unknown%20Label", 0, &error); - g_assert (object == NULL); - g_assert (error == NULL); -} - -static void -test_object_for_uri_error (Test *test, gconstpointer unused) -{ - GckObject *object; - GError *error = NULL; - - object = gck_modules_object_for_uri (test->modules, "http://invalid.uri", 0, &error); - g_assert (object == NULL); - g_assert (error != NULL); - g_assert (g_error_matches (error, GCK_URI_ERROR, GCK_URI_BAD_PREFIX)); - g_error_free (error); -} - -static void -test_objects_for_uri (Test *test, gconstpointer unused) -{ - GList *objects; - GError *error = NULL; - - objects = gck_modules_objects_for_uri (test->modules, "pkcs11:token=TEST%20LABEL", 0, &error); - g_assert (objects); - g_assert (!error); - g_assert_cmpint (g_list_length (objects), ==, 5); - - gck_list_unref_free (objects); -} - -static void -test_enumerate_uri (Test *test, gconstpointer unused) -{ - GckEnumerator *en; - GList *objects; - GError *error = NULL; - - en = gck_modules_enumerate_uri (test->modules, "pkcs11:token=TEST%20LABEL", 0, &error); - g_assert (GCK_IS_ENUMERATOR (en)); - g_assert (!error); - - objects = gck_enumerator_next_n (en, -1, NULL, &error); - g_assert_cmpint (g_list_length (objects), ==, 5); - g_assert (!error); - - g_object_unref (en); - gck_list_unref_free (objects); -} - -int -main (int argc, char **argv) -{ - g_type_init (); - g_test_init (&argc, &argv, NULL); - - g_test_add ("/gck/modules/enumerate_objects", Test, NULL, setup, test_enumerate_objects, teardown); - g_test_add ("/gck/modules/token_for_uri", Test, NULL, setup, test_token_for_uri, teardown); - g_test_add ("/gck/modules/token_for_uri_not_found", Test, NULL, setup, test_token_for_uri_not_found, teardown); - g_test_add ("/gck/modules/token_for_uri_error", Test, NULL, setup, test_token_for_uri_error, teardown); - g_test_add ("/gck/modules/object_for_uri", Test, NULL, setup, test_object_for_uri, teardown); - g_test_add ("/gck/modules/object_for_uri_not_found", Test, NULL, setup, test_object_for_uri_not_found, teardown); - g_test_add ("/gck/modules/object_for_uri_error", Test, NULL, setup, test_object_for_uri_error, teardown); - g_test_add ("/gck/modules/objects_for_uri", Test, NULL, setup, test_objects_for_uri, teardown); - g_test_add ("/gck/modules/enumerate_uri", Test, NULL, setup, test_enumerate_uri, teardown); - - return egg_tests_run_in_thread_with_loop (); -} diff --git a/gck/tests/test-gck-object.c b/gck/tests/test-gck-object.c deleted file mode 100644 index f1026201..00000000 --- a/gck/tests/test-gck-object.c +++ /dev/null @@ -1,419 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* test-gck-object.c - the GObject PKCS#11 wrapper library - - Copyright (C) 2011 Collabora Ltd. - - 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 <stefw@collabora.co.uk> -*/ - -#include "config.h" - -#include "gck/gck.h" -#include "gck/gck-mock.h" -#include "gck/gck-test.h" - -#include "egg/egg-testing.h" - -#include <glib.h> - -#include <errno.h> -#include <stdlib.h> -#include <stdio.h> -#include <string.h> - -typedef struct { - GckModule *module; - GckSlot *slot; - GckSession *session; - GckObject *object; -} Test; - -static void -setup (Test *test, gconstpointer unused) -{ - GError *err = NULL; - GList *slots; - - /* Successful load */ - test->module = gck_module_initialize (BUILDDIR "/.libs/libmock-test-module.so", NULL, &err); - g_assert_no_error (err); - g_assert (GCK_IS_MODULE (test->module)); - - slots = gck_module_get_slots (test->module, TRUE); - g_assert (slots != NULL); - - test->slot = GCK_SLOT (slots->data); - g_object_ref (test->slot); - gck_list_unref_free (slots); - - test->session = gck_slot_open_session (test->slot, 0, NULL, &err); - g_assert_no_error (err); - g_assert (GCK_IS_SESSION (test->session)); - - /* Our module always exports a token object with this */ - test->object = gck_object_from_handle (test->session, 2); - g_assert (test->object != NULL); -} - -static void -teardown (Test *test, gconstpointer unused) -{ - g_object_unref (test->object); - g_object_unref (test->session); - g_object_unref (test->slot); - g_object_unref (test->module); -} - -static void -test_object_props (Test *test, gconstpointer unused) -{ - GckSession *sess; - GckModule *mod; - CK_OBJECT_HANDLE handle; - g_object_get (test->object, "session", &sess, "module", &mod, "handle", &handle, NULL); - g_assert (test->session == sess); - g_object_unref (sess); - g_assert (test->module == mod); - g_object_unref (mod); - g_assert (handle == 2); -} - -static void -test_object_equals_hash (Test *test, gconstpointer unused) -{ - GckSlot *other_slot; - GckSession *other_session; - GckObject *other_object; - GObject *obj; - GError *err = NULL; - guint hash; - - hash = gck_object_hash (test->object); - g_assert (hash != 0); - - g_assert (gck_object_equal (test->object, test->object)); - - other_slot = g_object_new (GCK_TYPE_SLOT, "module", test->module, "handle", GCK_MOCK_SLOT_TWO_ID, NULL); - other_session = gck_slot_open_session (other_slot, 0, NULL, &err); - g_assert_no_error (err); - g_assert (GCK_IS_SESSION (other_session)); - other_object = gck_object_from_handle (other_session, gck_object_get_handle (test->object)); - g_assert (!gck_object_equal (test->object, other_object)); - g_object_unref (other_slot); - g_object_unref (other_session); - g_object_unref (other_object); - - obj = g_object_new (G_TYPE_OBJECT, NULL); - g_assert (!gck_object_equal (test->object, obj)); - g_object_unref (obj); - - other_object = gck_object_from_handle (test->session, 383838); - g_assert (!gck_object_equal (test->object, other_object)); - g_object_unref (other_object); - - other_object = gck_object_from_handle (test->session, gck_object_get_handle (test->object)); - g_assert (gck_object_equal (test->object, other_object)); - g_object_unref (other_object); -} - -static void -fetch_async_result (GObject *source, GAsyncResult *result, gpointer user_data) -{ - *((GAsyncResult**)user_data) = result; - g_object_ref (result); - egg_test_wait_stop (); -} - -static void -test_create_object (Test *test, gconstpointer unused) -{ - GAsyncResult *result = NULL; - GckAttributes *attrs; - GckObject *object; - CK_OBJECT_HANDLE last_handle; - GError *err = NULL; - - attrs = gck_attributes_new (); - gck_attributes_add_ulong (attrs, CKA_CLASS, CKO_DATA); - gck_attributes_add_string (attrs, CKA_LABEL, "TEST LABEL"); - gck_attributes_add_boolean (attrs, CKA_TOKEN, CK_FALSE); - gck_attributes_add_data (attrs, CKA_VALUE, (const guchar *)"BLAH", 4); - - object = gck_session_create_object (test->session, attrs, NULL, &err); - g_assert (GCK_IS_OBJECT (object)); - g_assert_no_error (err); - - last_handle = gck_object_get_handle (object); - g_object_unref (object); - - /* Using async */ - gck_session_create_object_async (test->session, attrs, NULL, fetch_async_result, &result); - egg_test_wait_until (500); - g_assert (result != NULL); - - object = gck_session_create_object_finish (test->session, result, &err); - g_object_unref (result); - g_assert_no_error (err); - g_assert (GCK_IS_OBJECT (object)); - - g_assert (last_handle != gck_object_get_handle (object)); - g_object_unref (object); - - gck_attributes_unref (attrs); -} - -static void -test_destroy_object (Test *test, gconstpointer unused) -{ - GAsyncResult *result = NULL; - GckAttributes *attrs; - GckObject *object; - GError *err = NULL; - gboolean ret; - - attrs = gck_attributes_new (); - gck_attributes_add_ulong (attrs, CKA_CLASS, CKO_DATA); - gck_attributes_add_string (attrs, CKA_LABEL, "TEST OBJECT"); - gck_attributes_add_boolean (attrs, CKA_TOKEN, CK_TRUE); - - /* Using simple */ - object = gck_session_create_object (test->session, attrs, NULL, &err); - g_assert_no_error (err); - g_assert (GCK_IS_OBJECT (object)); - - ret = gck_object_destroy (object, NULL, &err); - g_assert_no_error (err); - g_assert (ret); - g_object_unref (object); - - /* Using async */ - object = gck_session_create_object (test->session, attrs, NULL, &err); - g_assert_no_error (err); - g_assert (GCK_IS_OBJECT (object)); - - /* Using async */ - gck_object_destroy_async (object, NULL, fetch_async_result, &result); - egg_test_wait_until (500); - g_assert (result != NULL); - - ret = gck_object_destroy_finish (object, result, &err); - g_object_unref (result); - g_assert_no_error (err); - g_assert (ret); - g_object_unref (object); -} - -static void -test_get_attributes (Test *test, gconstpointer unused) -{ - GAsyncResult *result = NULL; - GckAttributes *attrs; - gulong attr_types[2]; - GError *err = NULL; - gulong klass; - gchar *value = NULL; - - attr_types[0] = CKA_CLASS; - attr_types[1] = CKA_LABEL; - - /* Simple */ - attrs = gck_object_get (test->object, NULL, &err, CKA_CLASS, CKA_LABEL, GCK_INVALID); - g_assert_no_error (err); - if (attrs != NULL) { - g_assert (gck_attributes_find_ulong (attrs, CKA_CLASS, &klass) && klass == CKO_DATA); - g_assert (gck_attributes_find_string (attrs, CKA_LABEL, &value) && strcmp (value, "TEST LABEL") == 0); - g_free (value); value = NULL; - } - gck_attributes_unref (attrs); - - /* Full */ - attrs = gck_object_get_full (test->object, attr_types, G_N_ELEMENTS (attr_types), NULL, &err); - g_assert_no_error (err); - g_assert (attrs); - g_assert (gck_attributes_find_ulong (attrs, CKA_CLASS, &klass) && klass == CKO_DATA); - g_assert (gck_attributes_find_string (attrs, CKA_LABEL, &value) && strcmp (value, "TEST LABEL") == 0); - g_free (value); value = NULL; - gck_attributes_unref (attrs); - - /* Async */ - gck_object_get_async (test->object, attr_types, G_N_ELEMENTS (attr_types), NULL, fetch_async_result, &result); - egg_test_wait_until (500); - g_assert (result != NULL); - - attrs = gck_object_get_finish (test->object, result, &err); - g_object_unref (result); - g_assert_no_error (err); - g_assert (attrs); - g_assert (gck_attributes_find_ulong (attrs, CKA_CLASS, &klass) && klass == CKO_DATA); - g_assert (gck_attributes_find_string (attrs, CKA_LABEL, &value) && strcmp (value, "TEST LABEL") == 0); - g_free (value); value = NULL; - gck_attributes_unref (attrs); -} - -static void -test_get_data_attribute (Test *test, gconstpointer unused) -{ - GAsyncResult *result = NULL; - CK_OBJECT_CLASS_PTR klass; - gsize n_data; - GError *err = NULL; - - /* Simple */ - klass = (gulong *)gck_object_get_data (test->object, CKA_CLASS, NULL, &n_data, &err); - g_assert_no_error (err); - g_assert (klass); - g_assert (n_data == sizeof (CK_OBJECT_CLASS)); - g_assert (*klass == CKO_DATA); - g_free (klass); - - /* Full */ - klass = (gulong *)gck_object_get_data_full (test->object, CKA_CLASS, NULL, NULL, &n_data, &err); - g_assert_no_error (err); - g_assert (klass); - g_assert (n_data == sizeof (CK_OBJECT_CLASS)); - g_assert (*klass == CKO_DATA); - g_free (klass); - - /* Async */ - gck_object_get_data_async (test->object, CKA_CLASS, NULL, NULL, fetch_async_result, &result); - egg_test_wait_until (500); - g_assert (result != NULL); - - klass = (gulong *)gck_object_get_data_finish (test->object, result, &n_data, &err); - g_object_unref (result); - g_assert_no_error (err); - g_assert (klass); - g_assert (n_data == sizeof (CK_OBJECT_CLASS)); - g_assert (*klass == CKO_DATA); - g_free (klass); - -} - -static void -test_set_attributes (Test *test, gconstpointer unused) -{ - GAsyncResult *result = NULL; - GckAttributes *attrs, *templ; - GError *err = NULL; - gulong klass; - gchar *value = NULL; - gboolean ret; - - templ = gck_attributes_new (); - gck_attributes_add_ulong (templ, CKA_CLASS, 6); - gck_attributes_add_string (templ, CKA_LABEL, "CHANGE TWO"); - - /* Full */ - ret = gck_object_set (test->object, templ, NULL, &err); - gck_attributes_unref (templ); - g_assert_no_error (err); - g_assert (ret); - attrs = gck_object_get (test->object, NULL, &err, CKA_CLASS, CKA_LABEL, GCK_INVALID); - g_assert (gck_attributes_find_ulong (attrs, CKA_CLASS, &klass) && klass == 6); - g_assert (gck_attributes_find_string (attrs, CKA_LABEL, &value) && strcmp (value, "CHANGE TWO") == 0); - g_free (value); value = NULL; - gck_attributes_unref (attrs); - - templ = gck_attributes_new (); - gck_attributes_add_ulong (templ, CKA_CLASS, 7); - gck_attributes_add_string (templ, CKA_LABEL, "CHANGE THREE"); - - /* Async */ - gck_object_set_async (test->object, templ, NULL, fetch_async_result, &result); - egg_test_wait_until (500); - g_assert (result != NULL); - - ret = gck_object_set_finish (test->object, result, &err); - g_object_unref (result); - g_assert_no_error (err); - g_assert (ret); - attrs = gck_object_get (test->object, NULL, &err, CKA_CLASS, CKA_LABEL, GCK_INVALID); - g_assert (gck_attributes_find_ulong (attrs, CKA_CLASS, &klass) && klass == 7); - g_assert (gck_attributes_find_string (attrs, CKA_LABEL, &value) && strcmp (value, "CHANGE THREE") == 0); - g_free (value); value = NULL; - gck_attributes_unref (attrs); -} - -static void -test_find_objects (Test *test, gconstpointer unused) -{ - GAsyncResult *result = NULL; - GckAttributes *templ, *attrs; - GList *objects; - GckObject *testobj; - GError *err = NULL; - - attrs = gck_attributes_new (); - gck_attributes_add_ulong (attrs, CKA_CLASS, CKO_DATA); - gck_attributes_add_string (attrs, CKA_LABEL, "UNIQUE LABEL"); - testobj = gck_session_create_object (test->session, attrs, NULL, &err); - gck_attributes_unref (attrs); - g_object_unref (testobj); - - attrs = gck_attributes_new (); - gck_attributes_add_ulong (attrs, CKA_CLASS, CKO_DATA); - gck_attributes_add_string (attrs, CKA_LABEL, "OTHER LABEL"); - testobj = gck_session_create_object (test->session, attrs, NULL, &err); - gck_attributes_unref (attrs); - g_object_unref (testobj); - - /* Simple, "TEST LABEL" */ - attrs = gck_attributes_new (); - gck_attributes_add_string (attrs, CKA_LABEL, "UNIQUE LABEL"); - objects = gck_session_find_objects (test->session, attrs, NULL, &err); - g_assert_no_error (err); - g_assert (g_list_length (objects) == 1); - gck_list_unref_free (objects); - gck_attributes_unref (attrs); - - /* Full, All */ - templ = gck_attributes_new (); - objects = gck_session_find_objects (test->session, templ, NULL, &err); - g_assert_no_error (err); - g_assert (g_list_length (objects) > 1); - gck_list_unref_free (objects); - - /* Async, None */ - gck_attributes_add_string (templ, CKA_LABEL, "blah blah"); - gck_session_find_objects_async (test->session, templ, NULL, fetch_async_result, &result); - egg_test_wait_until (500); - g_assert (result != NULL); - - objects = gck_session_find_objects_finish (test->session, result, &err); - g_object_unref (result); - g_assert (objects == NULL); - gck_list_unref_free (objects); -} - -int -main (int argc, char **argv) -{ - g_type_init (); - g_test_init (&argc, &argv, NULL); - - g_test_add ("/gck/object/object_props", Test, NULL, setup, test_object_props, teardown); - g_test_add ("/gck/object/object_equals_hash", Test, NULL, setup, test_object_equals_hash, teardown); - g_test_add ("/gck/object/create_object", Test, NULL, setup, test_create_object, teardown); - g_test_add ("/gck/object/destroy_object", Test, NULL, setup, test_destroy_object, teardown); - g_test_add ("/gck/object/get_attributes", Test, NULL, setup, test_get_attributes, teardown); - g_test_add ("/gck/object/get_data_attribute", Test, NULL, setup, test_get_data_attribute, teardown); - g_test_add ("/gck/object/set_attributes", Test, NULL, setup, test_set_attributes, teardown); - g_test_add ("/gck/object/find_objects", Test, NULL, setup, test_find_objects, teardown); - - return egg_tests_run_in_thread_with_loop (); -} diff --git a/gck/tests/test-gck-session.c b/gck/tests/test-gck-session.c deleted file mode 100644 index 632a7600..00000000 --- a/gck/tests/test-gck-session.c +++ /dev/null @@ -1,319 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* test-gck-session.c - the GObject PKCS#11 wrapper library - - Copyright (C) 2011 Collabora Ltd. - - 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 <stefw@collabora.co.uk> -*/ - -#include "config.h" - -#include "gck/gck.h" -#include "gck/gck-test.h" - -#include "egg/egg-testing.h" - -#include <glib.h> - -#include <errno.h> -#include <stdlib.h> -#include <stdio.h> -#include <string.h> - -typedef struct { - GckModule *module; - GckSlot *slot; - GckSession *session; -} Test; - -static void -setup (Test *test, gconstpointer unused) -{ - GError *err = NULL; - GList *slots; - - /* Successful load */ - test->module = gck_module_initialize (BUILDDIR "/.libs/libmock-test-module.so", NULL, &err); - g_assert_no_error (err); - g_assert (GCK_IS_MODULE (test->module)); - - slots = gck_module_get_slots (test->module, TRUE); - g_assert (slots != NULL); - - test->slot = GCK_SLOT (slots->data); - g_object_ref (test->slot); - gck_list_unref_free (slots); - - test->session = gck_slot_open_session (test->slot, 0, NULL, &err); - g_assert_no_error (err); - g_assert (GCK_IS_SESSION (test->session)); -} - -static void -teardown (Test *test, gconstpointer unused) -{ - g_object_unref (test->session); - g_object_unref (test->slot); - g_object_unref (test->module); -} - -static void -test_session_props (Test *test, gconstpointer unused) -{ - GckModule *mod; - GckSlot *sl; - gulong handle; - - g_object_get (test->session, "module", &mod, "handle", &handle, "slot", &sl, NULL); - g_assert (mod == test->module); - g_assert (sl == test->slot); - g_object_unref (mod); - g_object_unref (sl); - - g_assert (handle != 0); - g_assert (gck_session_get_handle (test->session) == handle); -} - -static void -test_session_info (Test *test, gconstpointer unused) -{ - GckSessionInfo *info; - - info = gck_session_get_info (test->session); - g_assert (info != NULL && "no session info"); - - g_assert (info->slot_id == gck_slot_get_handle (test->slot)); - g_assert ((info->flags & CKF_SERIAL_SESSION) == CKF_SERIAL_SESSION); - g_assert (info->device_error == 1414); - gck_session_info_free (info); -} - -static void -fetch_async_result (GObject *source, GAsyncResult *result, gpointer user_data) -{ - *((GAsyncResult**)user_data) = result; - g_object_ref (result); - egg_test_wait_stop (); -} - -static void -test_open_close_session (Test *test, gconstpointer unused) -{ - GckSession *sess; - GAsyncResult *result = NULL; - GError *err = NULL; - - sess = gck_slot_open_session (test->slot, 0, NULL, &err); - g_assert_no_error (err); - g_assert (GCK_IS_SESSION (sess)); - - g_object_unref (sess); - - /* Test opening async */ - gck_slot_open_session_async (test->slot, 0, NULL, fetch_async_result, &result); - - egg_test_wait_until (500); - g_assert (result != NULL); - - /* Get the result */ - sess = gck_slot_open_session_finish (test->slot, result, &err); - g_assert_no_error (err); - g_assert (GCK_IS_SESSION (sess)); - - g_object_unref (result); - g_object_unref (sess); -} - -static void -test_init_set_pin (Test *test, gconstpointer unused) -{ - GAsyncResult *result = NULL; - GError *err = NULL; - gboolean ret; - - /* init pin */ - ret = gck_session_init_pin (test->session, (guchar*)"booo", 4, NULL, &err); - g_assert_no_error (err); - g_assert (ret); - - /* set pin */ - ret = gck_session_set_pin (test->session, (guchar*)"booo", 4, (guchar*)"tooo", 4, NULL, &err); - g_assert_no_error (err); - g_assert (ret); - - /* init pin async */ - gck_session_init_pin_async (test->session, (guchar*)"booo", 4, NULL, fetch_async_result, &result); - egg_test_wait_until (500); - g_assert (result != NULL); - ret = gck_session_init_pin_finish (test->session, result, &err); - g_assert_no_error (err); - g_assert (ret); - g_object_unref (result); - result = NULL; - - /* set pin async */ - gck_session_set_pin_async (test->session, (guchar*)"booo", 4, (guchar*)"tooo", 4, NULL, fetch_async_result, &result); - egg_test_wait_until (500); - g_assert (result != NULL); - ret = gck_session_set_pin_finish (test->session, result, &err); - g_assert_no_error (err); - g_assert (ret); - g_object_unref (result); - result = NULL; -} - - -static void -test_login_logout (Test *test, gconstpointer unused) -{ - GAsyncResult *result = NULL; - GError *err = NULL; - gboolean ret; - - /* login/logout */ - ret = gck_session_login (test->session, CKU_USER, (guchar*)"booo", 4, NULL, &err); - g_assert_no_error (err); - g_assert (ret); - - ret = gck_session_logout (test->session, NULL, &err); - g_assert_no_error (err); - g_assert (ret); - - /* login async */ - gck_session_login_async (test->session, CKU_USER, (guchar*)"booo", 4, NULL, fetch_async_result, &result); - egg_test_wait_until (500); - g_assert (result != NULL); - - ret = gck_session_login_finish (test->session, result, &err); - g_assert_no_error (err); - g_assert (ret); - - g_object_unref (result); - result = NULL; - - /* logout async */ - gck_session_logout_async (test->session, NULL, fetch_async_result, &result); - egg_test_wait_until (500); - g_assert (result != NULL); - - ret = gck_session_logout_finish (test->session, result, &err); - g_assert_no_error (err); - g_assert (ret); - - g_object_unref (result); - result = NULL; - -} - -static gboolean -authenticate_token (GckModule *module, GckSlot *slot, gchar *label, gchar **password, gpointer unused) -{ - g_assert (unused == GUINT_TO_POINTER (35)); - g_assert (password != NULL); - g_assert (*password == NULL); - g_assert (GCK_IS_MODULE (module)); - g_assert (GCK_IS_SLOT (slot)); - - *password = g_strdup ("booo"); - return TRUE; -} - -static void -test_auto_login (Test *test, gconstpointer unused) -{ - GckObject *object; - GckSession *new_session; - GAsyncResult *result = NULL; - GError *err = NULL; - GckAttributes *attrs; - gboolean ret; - - attrs = gck_attributes_new (); - gck_attributes_add_ulong (attrs, CKA_CLASS, CKO_DATA); - gck_attributes_add_string (attrs, CKA_LABEL, "TEST OBJECT"); - gck_attributes_add_boolean (attrs, CKA_PRIVATE, CK_TRUE); - - /* Try to do something that requires a login */ - object = gck_session_create_object (test->session, attrs, NULL, &err); - g_assert (!object); - g_assert (err && err->code == CKR_USER_NOT_LOGGED_IN); - g_clear_error (&err); - - /* Setup for auto login */ - g_signal_connect (test->module, "authenticate-slot", G_CALLBACK (authenticate_token), GUINT_TO_POINTER (35)); - new_session = gck_slot_open_session (test->slot, GCK_SESSION_READ_WRITE | GCK_SESSION_LOGIN_USER, NULL, &err); - g_assert_no_error (err); - g_assert (GCK_IS_SESSION (new_session)); - - /* Try again to do something that requires a login */ - object = gck_session_create_object (new_session, attrs, NULL, &err); - g_assert_no_error (err); - g_assert (GCK_IS_OBJECT (object)); - g_object_unref (object); - - /* We should now be logged in, try to log out */ - ret = gck_session_logout (new_session, NULL, &err); - g_assert_no_error (err); - g_assert (ret); - - g_object_unref (new_session); - - /* Now try the same thing, but asyncronously */ - gck_slot_open_session_async (test->slot, GCK_SESSION_READ_WRITE | GCK_SESSION_LOGIN_USER, NULL, fetch_async_result, &result); - egg_test_wait_until (500); - g_assert (result != NULL); - new_session = gck_slot_open_session_finish (test->slot, result, &err); - g_assert_no_error (err); - g_assert (GCK_IS_SESSION (new_session)); - g_object_unref (result); - - result = NULL; - gck_session_create_object_async (new_session, attrs, NULL, fetch_async_result, &result); - egg_test_wait_until (500); - g_assert (result != NULL); - object = gck_session_create_object_finish (new_session, result, &err); - g_assert_no_error (err); - g_assert (GCK_IS_OBJECT (object)); - g_object_unref (result); - g_object_unref (object); - - /* We should now be logged in, try to log out */ - ret = gck_session_logout (new_session, NULL, &err); - g_assert_no_error (err); - g_assert (ret); - - g_object_unref (new_session); -} - -int -main (int argc, char **argv) -{ - g_type_init (); - g_test_init (&argc, &argv, NULL); - - g_set_prgname ("test-gck-session"); - - g_test_add ("/gck/session/session_props", Test, NULL, setup, test_session_props, teardown); - g_test_add ("/gck/session/session_info", Test, NULL, setup, test_session_info, teardown); - g_test_add ("/gck/session/open_close_session", Test, NULL, setup, test_open_close_session, teardown); - g_test_add ("/gck/session/init_set_pin", Test, NULL, setup, test_init_set_pin, teardown); - g_test_add ("/gck/session/login_logout", Test, NULL, setup, test_login_logout, teardown); - g_test_add ("/gck/session/auto_login", Test, NULL, setup, test_auto_login, teardown); - - return egg_tests_run_in_thread_with_loop (); -} diff --git a/gck/tests/test-gck-slot.c b/gck/tests/test-gck-slot.c deleted file mode 100644 index affc3b26..00000000 --- a/gck/tests/test-gck-slot.c +++ /dev/null @@ -1,257 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* test-gck-slot.c - the GObject PKCS#11 wrapper library - - Copyright (C) 2011 Collabora Ltd. - - 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 <stefw@collabora.co.uk> -*/ - -#include "config.h" - -#include <errno.h> -#include <glib.h> -#include <string.h> - -#include "gck/gck.h" -#include "gck/gck-private.h" -#include "gck/gck-test.h" - -typedef struct { - GckModule *module; - GckSlot *slot; -} Test; - -static void -setup (Test *test, gconstpointer unused) -{ - GError *err = NULL; - GList *slots; - - /* Successful load */ - test->module = gck_module_initialize (BUILDDIR "/.libs/libmock-test-module.so", NULL, &err); - g_assert_no_error (err); - g_assert (GCK_IS_MODULE (test->module)); - - slots = gck_module_get_slots (test->module, TRUE); - g_assert (slots != NULL); - - test->slot = GCK_SLOT (slots->data); - g_object_ref (test->slot); - gck_list_unref_free (slots); - -} - -static void -teardown (Test *test, gconstpointer unused) -{ - g_object_unref (test->slot); - g_object_unref (test->module); -} - -static void -test_slot_info (Test *test, gconstpointer unused) -{ - GckSlotInfo *info; - GckTokenInfo *token; - GList *slots, *l; - - slots = gck_module_get_slots (test->module, FALSE); - g_assert (2 == g_list_length (slots) && "wrong number of slots returned"); - g_assert (GCK_IS_SLOT (slots->data) && "missing slot one"); - g_assert (GCK_IS_SLOT (slots->next->data) && "missing slot two"); - - for (l = slots; l; l = g_list_next (l)) { - info = gck_slot_get_info (GCK_SLOT (l->data)); - g_assert (info != NULL && "no slot info"); - - g_assert (strcmp("TEST MANUFACTURER", info->manufacturer_id) == 0); - g_assert (strcmp("TEST SLOT", info->slot_description) == 0); - g_assert (55 == info->hardware_version_major); - g_assert (155 == info->hardware_version_minor); - g_assert (65 == info->firmware_version_major); - g_assert (165 == info->firmware_version_minor); - - if (info->flags & CKF_TOKEN_PRESENT) { - token = gck_slot_get_token_info (test->slot); - g_assert (token != NULL && "no token info"); - - g_assert (strcmp ("TEST MANUFACTURER", token->manufacturer_id) == 0); - g_assert (strcmp ("TEST LABEL", token->label) == 0); - g_assert (strcmp ("TEST MODEL", token->model) == 0); - g_assert (strcmp ("TEST SERIAL", token->serial_number) == 0); - g_assert (1 == token->max_session_count); - g_assert (2 == token->session_count); - g_assert (3 == token->max_rw_session_count); - g_assert (4 == token->rw_session_count); - g_assert (5 == token->max_pin_len); - g_assert (6 == token->min_pin_len); - g_assert (7 == token->total_public_memory); - g_assert (8 == token->free_public_memory); - g_assert (9 == token->total_private_memory); - g_assert (10 == token->free_private_memory); - g_assert (75 == token->hardware_version_major); - g_assert (175 == token->hardware_version_minor); - g_assert (85 == token->firmware_version_major); - g_assert (185 == token->firmware_version_minor); - g_assert (927623999 == token->utc_time); - - gck_token_info_free (token); - } - - gck_slot_info_free (info); - } - - gck_list_unref_free (slots); -} - -static void -test_slot_props (Test *test, gconstpointer unused) -{ - GckModule *mod; - CK_SLOT_ID slot_id; - - g_object_get (test->slot, "module", &mod, "handle", &slot_id, NULL); - g_assert (mod == test->module); - g_assert (slot_id == 52); - - g_object_unref (mod); -} - -static void -test_slot_equals_hash (Test *test, gconstpointer unused) -{ - GckModule *other_mod; - GckSlot *other_slot; - GObject *obj; - guint hash; - - hash = gck_slot_hash (test->slot); - g_assert (hash != 0); - - g_assert (gck_slot_equal (test->slot, test->slot)); - - other_mod = gck_module_new (gck_module_get_functions (test->module)); - other_slot = g_object_new (GCK_TYPE_SLOT, "module", other_mod, "handle", gck_slot_get_handle (test->slot), NULL); - g_assert (gck_slot_equal (test->slot, other_slot)); - g_object_unref (other_mod); - g_object_unref (other_slot); - - obj = g_object_new (G_TYPE_OBJECT, NULL); - g_assert (!gck_slot_equal (test->slot, obj)); - g_object_unref (obj); - - other_slot = g_object_new (GCK_TYPE_SLOT, "module", test->module, "handle", 8909, NULL); - g_assert (!gck_slot_equal (test->slot, obj)); - g_object_unref (other_slot); -} - -static void -test_slot_mechanisms (Test *test, gconstpointer unused) -{ - GArray *mechs; - GckMechanismInfo *info; - guint i; - - mechs = gck_slot_get_mechanisms (test->slot); - g_assert (2 == gck_mechanisms_length (mechs) && "wrong number of mech types returned"); - - for (i = 0; i < gck_mechanisms_length (mechs); ++i) { - - info = gck_slot_get_mechanism_info (test->slot, gck_mechanisms_at (mechs, i)); - g_assert (info != NULL && "no mech info returned"); - - gck_mechanism_info_free (info); - } - - gck_mechanisms_free (mechs); -} - -static void -test_token_info_match_null (Test *test, gconstpointer unused) -{ - GckTokenInfo *match; - GckTokenInfo *token; - gboolean ret; - - token = gck_slot_get_token_info (test->slot); - match = g_new0 (GckTokenInfo, 1); - - /* Should match, since no fields are set */ - ret = _gck_token_info_match (match, token); - g_assert (ret); - - gck_token_info_free (match); - gck_token_info_free (token); -} - -static void -test_token_info_match_label (Test *test, gconstpointer unused) -{ - GckTokenInfo *match; - GckTokenInfo *token; - gboolean ret; - - token = gck_slot_get_token_info (test->slot); - match = g_new0 (GckTokenInfo, 1); - - /* Should match since the label and serial are matching */ - match->label = g_strdup (token->label); - match->serial_number = g_strdup (token->serial_number); - ret = _gck_token_info_match (match, token); - g_assert (ret); - - gck_token_info_free (match); - gck_token_info_free (token); -} - -static void -test_token_info_match_different (Test *test, gconstpointer unused) -{ - GckTokenInfo *match; - GckTokenInfo *token; - gboolean ret; - - token = gck_slot_get_token_info (test->slot); - match = g_new0 (GckTokenInfo, 1); - - /* Should not match since serial is different */ - match->label = g_strdup (token->label); - match->serial_number = g_strdup ("393939393939393"); - ret = _gck_token_info_match (match, token); - g_assert (!ret); - - gck_token_info_free (match); - gck_token_info_free (token); -} - -int -main (int argc, char **argv) -{ - g_type_init (); - g_test_init (&argc, &argv, NULL); - - g_test_add ("/gck/slot/slot_info", Test, NULL, setup, test_slot_info, teardown); - g_test_add ("/gck/slot/slot_props", Test, NULL, setup, test_slot_props, teardown); - g_test_add ("/gck/slot/slot_equals_hash", Test, NULL, setup, test_slot_equals_hash, teardown); - g_test_add ("/gck/slot/slot_mechanisms", Test, NULL, setup, test_slot_mechanisms, teardown); - g_test_add ("/gck/slot/token_info_match_null", Test, NULL, setup, test_token_info_match_null, teardown); - g_test_add ("/gck/slot/token_info_match_label", Test, NULL, setup, test_token_info_match_label, teardown); - g_test_add ("/gck/slot/token_info_match_different", Test, NULL, setup, test_token_info_match_different, teardown); - - return g_test_run (); -} diff --git a/gck/tests/test-gck-uri.c b/gck/tests/test-gck-uri.c deleted file mode 100644 index b8d640f0..00000000 --- a/gck/tests/test-gck-uri.c +++ /dev/null @@ -1,556 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* test-gck-uri.c: Test routines for PKCS#11 URIs - - Copyright (C) 2011, Collabora Ltd. - - 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 <stefw@collabora.co.uk> -*/ - -#include "config.h" - -#include "gck/gck.h" -#include "gck/gck-private.h" -#include "gck/gck-test.h" - -#include <glib.h> - -#include <errno.h> -#include <string.h> - -static void -test_parse (void) -{ - GError *error = NULL; - GckUriData *uri_data; - - uri_data = gck_uri_parse ("pkcs11:", GCK_URI_FOR_MODULE, &error); - g_assert (uri_data != NULL); - g_assert_no_error (error); - - g_assert (uri_data->attributes == NULL); - g_assert (uri_data->token_info == NULL); - - g_assert (uri_data->module_info != NULL); - g_assert (uri_data->module_info->library_description == NULL); - g_assert (uri_data->module_info->manufacturer_id == NULL); - - gck_uri_data_free (uri_data); -} - -static void -test_parse_bad_scheme (void) -{ - GError *error = NULL; - GckUriData *uri_data; - - uri_data = gck_uri_parse ("http:\\example.com\test", GCK_URI_FOR_ANY, &error); - g_assert (uri_data == NULL); - g_assert_error (error, GCK_URI_ERROR, GCK_URI_BAD_PREFIX); - g_error_free (error); -} - -static void -test_parse_with_label (void) -{ - GError *error = NULL; - GckUriData *uri_data; - gchar *value; - - uri_data = gck_uri_parse ("pkcs11:object=Test%20Label", GCK_URI_FOR_ANY, &error); - g_assert (uri_data != NULL); - g_assert (uri_data->attributes != NULL); - - if (!gck_attributes_find_string (uri_data->attributes, CKA_LABEL, &value)) - g_assert_not_reached (); - - g_assert_cmpstr (value, ==, "Test Label"); - g_free (value); - - gck_uri_data_free (uri_data); -} - -static void -test_parse_with_label_and_klass (void) -{ - GError *error = NULL; - GckUriData *uri_data; - gchar *value; - gulong klass; - - uri_data = gck_uri_parse ("pkcs11:object=Test%20Label;objecttype=cert", GCK_URI_FOR_ANY, &error); - g_assert (uri_data); - g_assert (uri_data->attributes); - - if (!gck_attributes_find_string (uri_data->attributes, CKA_LABEL, &value)) - g_assert_not_reached (); - - if (!gck_attributes_find_ulong (uri_data->attributes, CKA_CLASS, &klass)) - g_assert_not_reached (); - - g_assert_cmpstr (value, ==, "Test Label"); - g_assert (klass == CKO_CERTIFICATE); - g_free (value); - - gck_uri_data_free (uri_data); -} - -static void -test_parse_with_id (void) -{ - GError *error = NULL; - GckAttribute *attr; - GckUriData *uri_data; - - uri_data = gck_uri_parse ("pkcs11:id=%54%45%53%54%00", GCK_URI_FOR_OBJECT, &error); - g_assert (uri_data != NULL); - g_assert (uri_data->attributes != NULL); - - attr = gck_attributes_find (uri_data->attributes, CKA_ID); - g_assert (attr); - g_assert (attr->value); - g_assert (attr->length == 5); - g_assert (memcmp (attr->value, "TEST", 5) == 0); - - gck_uri_data_free (uri_data); -} - -static void -test_parse_with_bad_string_encoding (void) -{ - GError *error = NULL; - GckUriData *uri_data; - - uri_data = gck_uri_parse ("pkcs11:object=Test%", GCK_URI_FOR_OBJECT, &error); - g_assert (uri_data == NULL); - g_assert_error (error, GCK_URI_ERROR, GCK_URI_BAD_ENCODING); - g_error_free (error); -} - -static void -test_parse_with_bad_binary_encoding (void) -{ - GError *error = NULL; - GckUriData *uri_data; - uri_data = gck_uri_parse ("pkcs11:id=%%", GCK_URI_FOR_ANY, &error); - g_assert (!uri_data); - g_assert_error (error, GCK_URI_ERROR, GCK_URI_BAD_ENCODING); - g_error_free (error); -} - -static void -test_parse_with_token (void) -{ - GError *error = NULL; - GckUriData *uri_data = NULL; - - uri_data = gck_uri_parse ("pkcs11:token=Token%20Label;serial=3333;model=Deluxe;manufacturer=Me", - GCK_URI_FOR_TOKEN, &error); - - g_assert (uri_data); - g_assert (uri_data->token_info); - g_assert_cmpstr (uri_data->token_info->label, ==, "Token Label"); - g_assert_cmpstr (uri_data->token_info->serial_number, ==, "3333"); - g_assert_cmpstr (uri_data->token_info->model, ==, "Deluxe"); - g_assert_cmpstr (uri_data->token_info->manufacturer_id, ==, "Me"); - gck_uri_data_free (uri_data); -} - -static void -test_parse_with_token_bad_encoding (void) -{ - GError *error = NULL; - GckUriData *uri_data; - - uri_data = gck_uri_parse ("pkcs11:token=Token%", GCK_URI_FOR_TOKEN, &error); - g_assert (!uri_data); - g_assert_error (error, GCK_URI_ERROR, GCK_URI_BAD_ENCODING); - g_error_free (error); -} - -static void -test_parse_with_bad_syntax (void) -{ - GError *error = NULL; - GckUriData *uri_data; - - uri_data = gck_uri_parse ("pkcs11:token", GCK_URI_FOR_ANY, &error); - g_assert (uri_data == NULL); - g_assert (g_error_matches (error, GCK_URI_ERROR, GCK_URI_BAD_SYNTAX)); - g_error_free (error); -} - -static void -test_parse_with_library (void) -{ - GError *error = NULL; - GckUriData *uri_data = NULL; - - uri_data = gck_uri_parse ("pkcs11:library-description=The%20Library;library-manufacturer=Me", - GCK_URI_FOR_MODULE, &error); - - g_assert (uri_data); - g_assert (uri_data->module_info); - g_assert_cmpstr (uri_data->module_info->manufacturer_id, ==, "Me"); - g_assert_cmpstr (uri_data->module_info->library_description, ==, "The Library"); - gck_uri_data_free (uri_data); -} - -static void -test_parse_with_library_bad_encoding (void) -{ - GError *error = NULL; - GckUriData *uri_data; - - uri_data = gck_uri_parse ("pkcs11:library-description=Library%", GCK_URI_FOR_MODULE, &error); - g_assert (!uri_data); - g_assert_error (error, GCK_URI_ERROR, GCK_URI_BAD_ENCODING); - g_error_free (error); -} - -static void -test_build_empty (void) -{ - GckUriData uri_data; - gchar *uri; - - memset (&uri_data, 0, sizeof (uri_data)); - uri = gck_uri_build (&uri_data, 0); - g_assert_cmpstr (uri, ==, "pkcs11:"); - g_free (uri); -} - -static void -test_build_with_token_info (void) -{ - gchar *uri = NULL; - GckUriData uri_data; - GckUriData *check; - - memset (&uri_data, 0, sizeof (uri_data)); - uri_data.token_info = g_new0 (GckTokenInfo, 1); - uri_data.token_info->label = g_strdup ("The Label"); - uri_data.token_info->serial_number = g_strdup ("44444"); - uri_data.token_info->manufacturer_id = g_strdup ("Me"); - uri_data.token_info->model = g_strdup ("Deluxe"); - - uri = gck_uri_build (&uri_data, GCK_URI_FOR_TOKEN); - g_assert (uri); - - check = gck_uri_parse (uri, GCK_URI_FOR_TOKEN, NULL); - g_assert (check); - g_assert (check->token_info); - - g_assert (_gck_token_info_match (uri_data.token_info, check->token_info)); - - gck_token_info_free (uri_data.token_info); - gck_uri_data_free (check); - - g_assert (g_str_has_prefix (uri, "pkcs11:")); - g_assert (strstr (uri, "token=The%20Label")); - g_assert (strstr (uri, "serial=44444")); - g_assert (strstr (uri, "manufacturer=Me")); - g_assert (strstr (uri, "model=Deluxe")); - - g_free (uri); -} - -static void -test_build_with_token_null_info (void) -{ - gchar *uri = NULL; - GckUriData uri_data; - - memset (&uri_data, 0, sizeof (uri_data)); - uri_data.token_info = g_new0 (GckTokenInfo, 1); - uri_data.token_info->label = g_strdup ("The Label"); - - uri = gck_uri_build (&uri_data, GCK_URI_FOR_TOKEN); - g_assert (uri); - - g_assert (g_str_has_prefix (uri, "pkcs11:")); - g_assert (strstr (uri, "token=The%20Label")); - g_assert (!strstr (uri, "serial=")); - - gck_token_info_free (uri_data.token_info); - g_free (uri); -} - -static void -test_build_with_token_empty_info (void) -{ - gchar *uri = NULL; - GckUriData uri_data; - - memset (&uri_data, 0, sizeof (uri_data)); - uri_data.token_info = g_new0 (GckTokenInfo, 1); - uri_data.token_info->label = g_strdup ("The Label"); - uri_data.token_info->serial_number = g_strdup (""); - - uri = gck_uri_build (&uri_data, GCK_URI_FOR_TOKEN); - g_assert (uri); - - g_assert (g_str_has_prefix (uri, "pkcs11:")); - g_assert (strstr (uri, "token=The%20Label")); - g_assert (strstr (uri, "serial=")); - - gck_token_info_free (uri_data.token_info); - g_free (uri); -} - -static void -test_build_with_attributes (void) -{ - gchar *uri = NULL; - GckUriData uri_data; - GckUriData *check; - gchar *string; - gulong value; - GckAttribute *attr; - - memset (&uri_data, 0, sizeof (uri_data)); - uri_data.attributes = gck_attributes_new (); - gck_attributes_add_string (uri_data.attributes, CKA_LABEL, "The Label"); - gck_attributes_add_ulong (uri_data.attributes, CKA_CLASS, CKO_DATA); - gck_attributes_add_data (uri_data.attributes, CKA_ID, (const guchar *)"TEST", 5); - - uri = gck_uri_build (&uri_data, GCK_URI_FOR_OBJECT); - g_assert (uri); - - gck_attributes_unref (uri_data.attributes); - - check = gck_uri_parse (uri, GCK_URI_FOR_ANY, NULL); - g_assert (check); - g_assert (check->attributes); - - if (!gck_attributes_find_string (check->attributes, CKA_LABEL, &string)) - g_assert_not_reached (); - g_assert_cmpstr (string, ==, "The Label"); - - if (!gck_attributes_find_ulong (check->attributes, CKA_CLASS, &value)) - g_assert_not_reached (); - g_assert (value == CKO_DATA); - - attr = gck_attributes_find (check->attributes, CKA_ID); - g_assert (attr); - g_assert (attr->length == 5); - g_assert (memcmp (attr->value, "TEST", 5) == 0); - - gck_uri_data_free (check); - - g_assert (g_str_has_prefix (uri, "pkcs11:")); - g_assert (strstr (uri, "object=The%20Label")); - g_assert (strstr (uri, "object-type=data")); - g_assert (strstr (uri, "id=TEST%00")); - - g_free (uri); -} - -static void -test_parse_private_key (void) -{ - GckUriData *uri_data; - GError *error = NULL; - gulong klass; - - uri_data = gck_uri_parse ("pkcs11:objecttype=private", GCK_URI_FOR_OBJECT, &error); - g_assert (uri_data); - g_assert_no_error (error); - - g_assert (uri_data->attributes); - if (!gck_attributes_find_ulong (uri_data->attributes, CKA_CLASS, &klass)) - g_assert_not_reached (); - gck_assert_cmpulong (klass, ==, CKO_PRIVATE_KEY); - - gck_uri_data_free (uri_data); -} - -static void -test_parse_secret_key (void) -{ - GckUriData *uri_data; - GError *error = NULL; - gulong klass; - - uri_data = gck_uri_parse ("pkcs11:objecttype=secretkey", GCK_URI_FOR_OBJECT, &error); - g_assert (uri_data); - g_assert_no_error (error); - - g_assert (uri_data->attributes); - if (!gck_attributes_find_ulong (uri_data->attributes, CKA_CLASS, &klass)) - g_assert_not_reached (); - gck_assert_cmpulong (klass, ==, CKO_SECRET_KEY); - - gck_uri_data_free (uri_data); -} - - -static void -test_parse_unknown_objecttype (void) -{ - GckUriData *uri_data; - GError *error = NULL; - gulong klass; - - uri_data = gck_uri_parse ("pkcs11:objecttype=unknown", GCK_URI_FOR_OBJECT, &error); - g_assert (uri_data); - g_assert_no_error (error); - - g_assert (uri_data->attributes); - g_assert (uri_data->any_unrecognized == TRUE); - if (gck_attributes_find_ulong (uri_data->attributes, CKA_CLASS, &klass)) - g_assert_not_reached (); - - gck_uri_data_free (uri_data); -} - -static void -test_build_objecttype_cert (void) -{ - GckUriData *uri_data; - gchar *uri; - - uri_data = gck_uri_data_new (); - uri_data->attributes = gck_attributes_new (); - gck_attributes_add_ulong (uri_data->attributes, CKA_CLASS, CKO_CERTIFICATE); - - uri = gck_uri_build (uri_data, GCK_URI_FOR_OBJECT); - g_assert (uri); - g_assert (strstr (uri, "object-type=cert")); - - gck_uri_data_free (uri_data); - g_free (uri); -} - -static void -test_build_objecttype_private (void) -{ - GckUriData *uri_data; - gchar *uri; - - uri_data = gck_uri_data_new (); - uri_data->attributes = gck_attributes_new (); - gck_attributes_add_ulong (uri_data->attributes, CKA_CLASS, CKO_PRIVATE_KEY); - - uri = gck_uri_build (uri_data, GCK_URI_FOR_OBJECT); - g_assert (uri); - g_assert (strstr (uri, "object-type=private")); - - gck_uri_data_free (uri_data); - g_free (uri); -} - -static void -test_build_objecttype_public (void) -{ - GckUriData *uri_data; - gchar *uri; - - uri_data = gck_uri_data_new (); - uri_data->attributes = gck_attributes_new (); - gck_attributes_add_ulong (uri_data->attributes, CKA_CLASS, CKO_PUBLIC_KEY); - - uri = gck_uri_build (uri_data, GCK_URI_FOR_OBJECT); - g_assert (uri); - g_assert (strstr (uri, "object-type=public")); - - gck_uri_data_free (uri_data); - g_free (uri); -} - -static void -test_build_objecttype_secret (void) -{ - GckUriData *uri_data; - gchar *uri; - - uri_data = gck_uri_data_new (); - uri_data->attributes = gck_attributes_new (); - gck_attributes_add_ulong (uri_data->attributes, CKA_CLASS, CKO_SECRET_KEY); - - uri = gck_uri_build (uri_data, GCK_URI_FOR_OBJECT); - g_assert (uri); - g_assert (strstr (uri, "object-type=secret-key")); - - gck_uri_data_free (uri_data); - g_free (uri); -} - -static void -test_build_with_library (void) -{ - GckUriData *uri_data; - gchar *uri; - - uri_data = gck_uri_data_new (); - uri_data->module_info = g_new0 (GckModuleInfo, 1); - uri_data->module_info->library_description = g_strdup ("The Description"); - - uri = gck_uri_build (uri_data, GCK_URI_FOR_MODULE); - g_assert (uri); - g_assert (strstr (uri, "library-description=The%20Description")); - - gck_uri_data_free (uri_data); - g_free (uri); -} - - -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); - - /* Suppress these messages in tests */ - g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, - null_log_handler, NULL); - - g_test_add_func ("/gck/uri/parse", test_parse); - g_test_add_func ("/gck/uri/parse_bad_scheme", test_parse_bad_scheme); - g_test_add_func ("/gck/uri/parse_with_label", test_parse_with_label); - g_test_add_func ("/gck/uri/parse_with_label_and_klass", test_parse_with_label_and_klass); - g_test_add_func ("/gck/uri/parse_with_id", test_parse_with_id); - g_test_add_func ("/gck/uri/parse_with_bad_string_encoding", test_parse_with_bad_string_encoding); - g_test_add_func ("/gck/uri/parse_with_bad_binary_encoding", test_parse_with_bad_binary_encoding); - g_test_add_func ("/gck/uri/parse_with_token", test_parse_with_token); - g_test_add_func ("/gck/uri/parse_with_token_bad_encoding", test_parse_with_token_bad_encoding); - g_test_add_func ("/gck/uri/parse_with_bad_syntax", test_parse_with_bad_syntax); - g_test_add_func ("/gck/uri/parse_with_library", test_parse_with_library); - g_test_add_func ("/gck/uri/parse_with_library_bad_encoding", test_parse_with_library_bad_encoding); - g_test_add_func ("/gck/uri/build_empty", test_build_empty); - g_test_add_func ("/gck/uri/build_with_token_info", test_build_with_token_info); - g_test_add_func ("/gck/uri/build_with_token_null_info", test_build_with_token_null_info); - g_test_add_func ("/gck/uri/build_with_token_empty_info", test_build_with_token_empty_info); - g_test_add_func ("/gck/uri/build_with_attributes", test_build_with_attributes); - g_test_add_func ("/gck/uri/parse_private_key", test_parse_private_key); - g_test_add_func ("/gck/uri/parse_secret_key", test_parse_secret_key); - g_test_add_func ("/gck/uri/parse_unknown_objecttype", test_parse_unknown_objecttype); - g_test_add_func ("/gck/uri/build_objecttype_cert", test_build_objecttype_cert); - g_test_add_func ("/gck/uri/build_objecttype_private", test_build_objecttype_private); - g_test_add_func ("/gck/uri/build_objecttype_public", test_build_objecttype_public); - g_test_add_func ("/gck/uri/build_objecttype_secret", test_build_objecttype_secret); - g_test_add_func ("/gck/uri/build_with_library", test_build_with_library); - - return g_test_run (); -} |