diff options
author | Stef Walter <stefw@collabora.co.uk> | 2011-04-18 13:36:01 +0200 |
---|---|---|
committer | Stef Walter <stefw@collabora.co.uk> | 2011-04-18 13:36:01 +0200 |
commit | e4faefb8ed07f6e6fa220c96c4591bc7b5f004e7 (patch) | |
tree | 5392b1a7c52adc3cc5976578f05a2c9a56811519 | |
parent | c0dc9725f6674090e05301b9fb8b4295be468d6a (diff) | |
parent | 00052136a0fd42b23956dfd76892912f7fb58e00 (diff) |
Merge branch 'gnome-3-0'
Conflicts:
docs/reference/gcr/gcr-sections.txt
gcr/Makefile.am
gcr/gcr-library.c
-rw-r--r-- | .gitignore | 13 | ||||
-rw-r--r-- | docs/reference/gcr/gcr-sections.txt | 2 | ||||
-rw-r--r-- | egg/egg-openssl.c | 8 | ||||
-rw-r--r-- | egg/egg-openssl.h | 2 | ||||
-rw-r--r-- | gcr/Makefile.am | 1 | ||||
-rw-r--r-- | gcr/gcr-certificate-chain.c | 46 | ||||
-rw-r--r-- | gcr/gcr-debug.c | 106 | ||||
-rw-r--r-- | gcr/gcr-debug.h | 86 | ||||
-rw-r--r-- | gcr/gcr-library.c | 4 | ||||
-rw-r--r-- | gcr/tests/frob-certificate.c | 1 | ||||
-rw-r--r-- | gcr/tests/frob-key.c | 1 | ||||
-rw-r--r-- | gcr/tests/frob-unlock-options.c | 1 | ||||
-rw-r--r-- | gcr/tests/test-certificate-chain.c | 1 | ||||
-rw-r--r-- | gcr/tests/test-certificate.c | 1 | ||||
-rw-r--r-- | gcr/tests/test-parser.c | 1 | ||||
-rw-r--r-- | gcr/tests/test-pkcs11-certificate.c | 1 | ||||
-rw-r--r-- | gcr/tests/test-simple-certificate.c | 1 | ||||
-rw-r--r-- | gcr/tests/test-trust.c | 1 | ||||
-rw-r--r-- | pkcs11/gkm/gkm-data-der.c | 58 | ||||
-rw-r--r-- | pkcs11/gkm/gkm-data-der.h | 58 | ||||
-rw-r--r-- | pkcs11/gkm/tests/Makefile.am | 94 | ||||
-rw-r--r-- | pkcs11/gkm/tests/files/ca-certificates.crt (renamed from pkcs11/gkm/tests/test-data/ca-certificates.crt) | 0 | ||||
-rw-r--r-- | pkcs11/gkm/tests/files/der-pkcs8-PBE-MD5-DES.key (renamed from pkcs11/gkm/tests/test-data/der-pkcs8-PBE-MD5-DES.key) | bin | 677 -> 677 bytes | |||
-rw-r--r-- | pkcs11/gkm/tests/files/der-pkcs8-PBE-SHA1-3DES.key (renamed from pkcs11/gkm/tests/test-data/der-pkcs8-PBE-SHA1-3DES.key) | bin | 678 -> 678 bytes | |||
-rw-r--r-- | pkcs11/gkm/tests/files/der-pkcs8-PBE-SHA1-DES.key (renamed from pkcs11/gkm/tests/test-data/der-pkcs8-PBE-SHA1-DES.key) | bin | 677 -> 677 bytes | |||
-rw-r--r-- | pkcs11/gkm/tests/files/der-pkcs8-PBE-SHA1-RC2-40.key (renamed from pkcs11/gkm/tests/test-data/der-pkcs8-PBE-SHA1-RC2-40.key) | bin | 678 -> 678 bytes | |||
-rw-r--r-- | pkcs11/gkm/tests/files/der-pkcs8-PBE-SHA1-RC4-128.key (renamed from pkcs11/gkm/tests/test-data/der-pkcs8-PBE-SHA1-RC4-128.key) | bin | 673 -> 673 bytes | |||
-rw-r--r-- | pkcs11/gkm/tests/files/der-pkcs8-dsa.key (renamed from pkcs11/gkm/tests/test-data/der-pkcs8-dsa.key) | bin | 335 -> 335 bytes | |||
-rw-r--r-- | pkcs11/gkm/tests/files/der-pkcs8-encrypted-pkcs5.key (renamed from pkcs11/gkm/tests/test-data/der-pkcs8-encrypted-pkcs5.key) | bin | 677 -> 677 bytes | |||
-rw-r--r-- | pkcs11/gkm/tests/files/der-pkcs8-v2-des.key (renamed from pkcs11/gkm/tests/test-data/der-pkcs8-v2-des.key) | bin | 711 -> 711 bytes | |||
-rw-r--r-- | pkcs11/gkm/tests/files/der-pkcs8-v2-des3.key (renamed from pkcs11/gkm/tests/test-data/der-pkcs8-v2-des3.key) | bin | 714 -> 714 bytes | |||
-rw-r--r-- | pkcs11/gkm/tests/files/der-pkcs8.key (renamed from pkcs11/gkm/tests/test-data/der-pkcs8.key) | bin | 635 -> 635 bytes | |||
-rw-r--r-- | pkcs11/gkm/tests/files/pem-rsa-enc.key (renamed from pkcs11/gkm/tests/test-data/pem-rsa-enc.key) | 0 | ||||
-rw-r--r-- | pkcs11/gkm/tests/files/test-certificate-1.der (renamed from pkcs11/gkm/tests/test-data/test-certificate-1.der) | bin | 813 -> 813 bytes | |||
-rw-r--r-- | pkcs11/gkm/tests/files/test-certificate-2.der (renamed from pkcs11/gkm/tests/test-data/test-certificate-2.der) | bin | 1346 -> 1346 bytes | |||
-rw-r--r-- | pkcs11/gkm/tests/mock-locked-object.h | 4 | ||||
-rw-r--r-- | pkcs11/gkm/tests/mock-module.c (renamed from pkcs11/gkm/tests/test-module.c) | 36 | ||||
-rw-r--r-- | pkcs11/gkm/tests/mock-module.h (renamed from pkcs11/gkm/tests/test-module.h) | 22 | ||||
-rw-r--r-- | pkcs11/gkm/tests/test-attributes.c (renamed from pkcs11/gkm/tests/unit-test-attributes.c) | 260 | ||||
-rw-r--r-- | pkcs11/gkm/tests/test-credential.c (renamed from pkcs11/gkm/tests/unit-test-credential.c) | 123 | ||||
-rw-r--r-- | pkcs11/gkm/tests/test-data-asn1.c (renamed from pkcs11/gkm/tests/unit-test-data-asn1.c) | 46 | ||||
-rw-r--r-- | pkcs11/gkm/tests/test-data-der.c (renamed from pkcs11/gkm/tests/unit-test-data-der.c) | 167 | ||||
-rw-r--r-- | pkcs11/gkm/tests/test-file-tracker.c | 244 | ||||
-rw-r--r-- | pkcs11/gkm/tests/test-memory-store.c | 482 | ||||
-rw-r--r-- | pkcs11/gkm/tests/test-object.c (renamed from pkcs11/gkm/tests/unit-test-object.c) | 132 | ||||
-rw-r--r-- | pkcs11/gkm/tests/test-secret.c (renamed from pkcs11/gkm/tests/unit-test-secret.c) | 48 | ||||
-rw-r--r-- | pkcs11/gkm/tests/test-sexp.c (renamed from pkcs11/gkm/tests/unit-test-sexp.c) | 64 | ||||
-rw-r--r-- | pkcs11/gkm/tests/test-store.c (renamed from pkcs11/gkm/tests/unit-test-store.c) | 59 | ||||
-rw-r--r-- | pkcs11/gkm/tests/test-timer.c | 175 | ||||
-rw-r--r-- | pkcs11/gkm/tests/test-transaction.c (renamed from pkcs11/gkm/tests/unit-test-transaction.c) | 117 | ||||
-rw-r--r-- | pkcs11/gkm/tests/unit-test-file-tracker.c | 221 | ||||
-rw-r--r-- | pkcs11/gkm/tests/unit-test-memory-store.c | 438 | ||||
-rw-r--r-- | pkcs11/gkm/tests/unit-test-timer.c | 144 |
53 files changed, 2027 insertions, 1242 deletions
@@ -115,6 +115,19 @@ run-tests /gcr/tests/test-simple-certificate /gcr/tests/test-trust +/pkcs11/gkm/tests/test-attributes +/pkcs11/gkm/tests/test-credential +/pkcs11/gkm/tests/test-data-asn1 +/pkcs11/gkm/tests/test-data-der +/pkcs11/gkm/tests/test-file-tracker +/pkcs11/gkm/tests/test-memory-store +/pkcs11/gkm/tests/test-object +/pkcs11/gkm/tests/test-secret +/pkcs11/gkm/tests/test-sexp +/pkcs11/gkm/tests/test-store +/pkcs11/gkm/tests/test-timer +/pkcs11/gkm/tests/test-transaction + /daemon/dbus/tests/test-secret-util /ui/gnome-keyring-prompt.desktop diff --git a/docs/reference/gcr/gcr-sections.txt b/docs/reference/gcr/gcr-sections.txt index 8951cccd..114bfb67 100644 --- a/docs/reference/gcr/gcr-sections.txt +++ b/docs/reference/gcr/gcr-sections.txt @@ -163,6 +163,8 @@ gcr_pkcs11_get_trust_lookup_uris gcr_pkcs11_set_trust_lookup_uris gcr_pkcs11_get_trust_store_uri gcr_pkcs11_set_trust_store_uri +<SUBSECTION Private> +GcrDebugFlags </SECTION> <SECTION> diff --git a/egg/egg-openssl.c b/egg/egg-openssl.c index 2e83e63e..f22a2bdc 100644 --- a/egg/egg-openssl.c +++ b/egg/egg-openssl.c @@ -234,7 +234,7 @@ egg_openssl_headers_new (void) } guint -egg_openssl_pem_parse (const guchar *data, gsize n_data, +egg_openssl_pem_parse (gconstpointer data, gsize n_data, EggOpensslPemCallback callback, gpointer user_data) { const gchar *beg, *end; @@ -258,7 +258,7 @@ egg_openssl_pem_parse (const guchar *data, gsize n_data, g_assert (type); /* This returns the character position before the PEM END header */ - end = pem_find_end ((const gchar*)beg, n_data - ((const guchar*)beg - data), type); + end = pem_find_end ((const gchar*)beg, n_data - ((const gchar*)beg - (const gchar *)data), type); if (!end) break; @@ -274,8 +274,8 @@ egg_openssl_pem_parse (const guchar *data, gsize n_data, /* Try for another block */ end += PEM_SUFF_L; - n_data -= (const guchar*)end - data; - data = (const guchar*)end; + n_data -= (const gchar*)end - (const gchar*)data; + data = end; } if (headers) diff --git a/egg/egg-openssl.h b/egg/egg-openssl.h index 878cf589..3253d73e 100644 --- a/egg/egg-openssl.h +++ b/egg/egg-openssl.h @@ -31,7 +31,7 @@ typedef void (*EggOpensslPemCallback) (GQuark type, const guchar *data, gsize n_ GHashTable* egg_openssl_headers_new (void); -guint egg_openssl_pem_parse (const guchar *data, gsize n_data, +guint egg_openssl_pem_parse (gconstpointer data, gsize n_data, EggOpensslPemCallback callback, gpointer user_data); diff --git a/gcr/Makefile.am b/gcr/Makefile.am index e55df6cb..532462c1 100644 --- a/gcr/Makefile.am +++ b/gcr/Makefile.am @@ -65,6 +65,7 @@ LIB_SOURCES = \ gcr-certificate-widget.c gcr-certificate-widget.h \ gcr-collection.c gcr-collection.h \ gcr-collection-model.c gcr-collection-model.h \ + gcr-debug.c gcr-debug.h \ gcr-display-scrolled.c gcr-display-scrolled.h \ gcr-comparable.c gcr-comparable.h \ gcr-display-view.c gcr-display-view.h \ diff --git a/gcr/gcr-certificate-chain.c b/gcr/gcr-certificate-chain.c index 047ac5f7..0c385828 100644 --- a/gcr/gcr-certificate-chain.c +++ b/gcr/gcr-certificate-chain.c @@ -26,11 +26,14 @@ #include "gcr-certificate-chain.h" #include "gcr-certificate.h" +#define DEBUG_FLAG GCR_DEBUG_CERTIFICATE_CHAIN +#include "gcr-debug.h" #include "gcr-pkcs11-certificate.h" #include "gcr-simple-certificate.h" - #include "gcr-trust.h" +#include "egg/egg-error.h" + /** * SECTION:gcr-certificate-chain * @title: GcrCertificateChain @@ -182,6 +185,8 @@ prep_chain_private_thread_safe (GcrCertificateChainPrivate *orig, const gchar *p g_return_val_if_fail (der, NULL); safe = gcr_simple_certificate_new (der, n_der); + _gcr_debug ("copying certificate so it's thread safe"); + /* Always set the original certificate onto the safe one */ g_object_set_qdata_full (G_OBJECT (safe), Q_ORIGINAL_CERT, g_object_ref (certificate), g_object_unref); @@ -223,6 +228,7 @@ perform_build_chain (GcrCertificateChainPrivate *pv, GCancellable *cancellable, gboolean lookups; gboolean ret; guint length; + gchar *subject; g_assert (pv); g_assert (pv->certificates); @@ -231,15 +237,25 @@ perform_build_chain (GcrCertificateChainPrivate *pv, GCancellable *cancellable, lookups = !((pv->flags & GCR_CERTIFICATE_CHAIN_FLAG_NO_LOOKUPS) == GCR_CERTIFICATE_CHAIN_FLAG_NO_LOOKUPS); /* This chain is built */ - if (!pv->certificates->len) + if (!pv->certificates->len) { + _gcr_debug ("empty certificate chain"); return TRUE; + } /* First check for pinned certificates */ certificate = g_ptr_array_index (pv->certificates, 0); + if (_gcr_debugging) { + subject = gcr_certificate_get_subject_dn (certificate); + _gcr_debug ("first certificate: %s", subject); + g_free (subject); + } + if (lookups && pv->peer) { ret = gcr_trust_is_certificate_pinned (certificate, pv->purpose, pv->peer, cancellable, &error); if (!ret && error) { + _gcr_debug ("failed to lookup pinned certificate: %s", + egg_error_message (error)); g_propagate_error (rerror, error); return FALSE; } @@ -249,6 +265,9 @@ perform_build_chain (GcrCertificateChainPrivate *pv, GCancellable *cancellable, * is irrelevant, so truncate chain and consider built. */ if (ret) { + _gcr_debug ("found pinned certificate for peer '%s', truncating chain", + pv->peer); + g_ptr_array_set_size (pv->certificates, 1); pv->status = GCR_CERTIFICATE_CHAIN_PINNED; return TRUE; @@ -262,6 +281,7 @@ perform_build_chain (GcrCertificateChainPrivate *pv, GCancellable *cancellable, /* Stop the chain if previous was self-signed */ if (gcr_certificate_is_issuer (certificate, certificate)) { + _gcr_debug ("found self-signed certificate"); pv->status = GCR_CERTIFICATE_CHAIN_SELFSIGNED; break; } @@ -269,25 +289,42 @@ perform_build_chain (GcrCertificateChainPrivate *pv, GCancellable *cancellable, /* Try the next certificate in the chain */ if (length < pv->certificates->len) { certificate = g_ptr_array_index (pv->certificates, length); + if (_gcr_debugging) { + subject = gcr_certificate_get_subject_dn (certificate); + _gcr_debug ("next certificate: %s", subject); + g_free (subject); + } /* No more in chain, try to lookup */ } else if (lookups) { certificate = gcr_pkcs11_certificate_lookup_issuer (certificate, cancellable, &error); if (error != NULL) { + _gcr_debug ("failed to lookup issuer: %s", error->message); g_propagate_error (rerror, error); return FALSE; + } else if (certificate) { g_ptr_array_add (pv->certificates, certificate); + if (_gcr_debugging) { + subject = gcr_certificate_get_subject_dn (certificate); + _gcr_debug ("found issuer certificate: %s", subject); + g_free (subject); + } + + } else { + _gcr_debug ("no issuer found"); } /* No more in chain, and can't lookup */ } else { + _gcr_debug ("no more certificates available, and no lookups"); certificate = NULL; } /* Stop the chain if nothing found */ if (certificate == NULL) { + _gcr_debug ("chain is incomplete"); pv->status = GCR_CERTIFICATE_CHAIN_INCOMPLETE; break; } @@ -300,11 +337,14 @@ perform_build_chain (GcrCertificateChainPrivate *pv, GCancellable *cancellable, cancellable, &error); if (!ret && error) { + _gcr_debug ("failed to lookup anchored certificate: %s", + egg_error_message (error)); g_propagate_error (rerror, error); return FALSE; /* Stop the chain at the first anchor */ } else if (ret) { + _gcr_debug ("found anchored certificate"); pv->status = GCR_CERTIFICATE_CHAIN_ANCHORED; break; } @@ -329,6 +369,8 @@ thread_build_chain (GSimpleAsyncResult *result, GObject *object, pv = g_object_get_qdata (G_OBJECT (result), Q_OPERATION_DATA); g_assert (pv); + _gcr_debug ("building asynchronously in another thread"); + if (!perform_build_chain (pv, cancellable, &error)) { g_simple_async_result_set_from_error (result, error); g_clear_error (&error); diff --git a/gcr/gcr-debug.c b/gcr/gcr-debug.c new file mode 100644 index 00000000..04a35302 --- /dev/null +++ b/gcr/gcr-debug.c @@ -0,0 +1,106 @@ +/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */ +/* + * Copyright (C) 2007 Collabora Ltd. + * Copyright (C) 2007 Nokia Corporation + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "config.h" + +#include "gcr-debug.h" + +#include <errno.h> +#include <fcntl.h> +#include <stdarg.h> +#include <sys/stat.h> +#include <unistd.h> + +#include <glib.h> +#include <glib/gstdio.h> + +#ifdef WITH_DEBUG + +static GcrDebugFlags current_flags = 0; + +static GDebugKey keys[] = { + { "certificate-chain", GCR_DEBUG_CERTIFICATE_CHAIN }, + { 0, } +}; + +static void +debug_set_flags (GcrDebugFlags new_flags) +{ + current_flags |= new_flags; +} + +void +_gcr_debug_set_flags (const gchar *flags_string) +{ + guint nkeys; + + for (nkeys = 0; keys[nkeys].value; nkeys++); + + if (flags_string) + debug_set_flags (g_parse_debug_string (flags_string, keys, nkeys)); +} + +gboolean +_gcr_debug_flag_is_set (GcrDebugFlags flag) +{ + return (flag & current_flags) != 0; +} + +void +_gcr_debug_message (GcrDebugFlags flag, const gchar *format, ...) +{ + static gsize initialized_flags = 0; + gchar *message; + va_list args; + + if (g_once_init_enter (&initialized_flags)) { + _gcr_debug_set_flags (g_getenv ("GCR_DEBUG")); + g_once_init_leave (&initialized_flags, 1); + } + + va_start (args, format); + message = g_strdup_vprintf (format, args); + va_end (args); + + if (flag & current_flags) + g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "%s", message); + + g_free (message); +} + +#else /* !WITH_DEBUG */ + +gboolean +_gcr_debug_flag_is_set (GcrDebugFlags flag) +{ + return FALSE; +} + +void +_gcr_debug_message (GcrDebugFlags flag, const gchar *format, ...) +{ +} + +void +_gcr_debug_set_flags (const gchar *flags_string) +{ +} + +#endif /* !WITH_DEBUG */ diff --git a/gcr/gcr-debug.h b/gcr/gcr-debug.h new file mode 100644 index 00000000..46de32c9 --- /dev/null +++ b/gcr/gcr-debug.h @@ -0,0 +1,86 @@ +/* + * Copyright (C) 2007 Nokia Corporation + * Copyright (C) 2007-2011 Collabora Ltd. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef GCR_DEBUG_H +#define GCR_DEBUG_H + +#include "config.h" + +#include <glib.h> + +G_BEGIN_DECLS + +/* Please keep this enum in sync with #keys in gcr-debug.c */ +typedef enum { + GCR_DEBUG_LIBRARY = 1 << 1, + GCR_DEBUG_CERTIFICATE_CHAIN = 1 << 2, +} GcrDebugFlags; + +gboolean _gcr_debug_flag_is_set (GcrDebugFlags flag); + +void _gcr_debug_set_flags (const gchar *flags_string); + +void _gcr_debug_message (GcrDebugFlags flag, + const gchar *format, + ...) G_GNUC_PRINTF (2, 3); + +G_END_DECLS + +#endif /* GCR_DEBUG_H */ + +/* ----------------------------------------------------------------------------- + * Below this point is outside the GCR_DEBUG_H guard - so it can take effect + * more than once. So you can do: + * + * #define DEBUG_FLAG GCR_DEBUG_ONE_THING + * #include "gcr-debug.h" + * ... + * DEBUG ("if we're debugging one thing"); + * ... + * #undef DEBUG_FLAG + * #define DEBUG_FLAG GCR_DEBUG_OTHER_THING + * #include "gcr-debug.h" + * ... + * DEBUG ("if we're debugging the other thing"); + * ... + */ + +#ifdef DEBUG_FLAG +#ifdef WITH_DEBUG + +#undef _gcr_debug +#define _gcr_debug(format, ...) \ + _gcr_debug_message (DEBUG_FLAG, "%s: " format, G_STRFUNC, ##__VA_ARGS__) + +#undef _gcr_debugging +#define _gcr_debugging \ + _gcr_debug_flag_is_set (DEBUG_FLAG) + +#else /* !defined (WITH_DEBUG) */ + +#undef _gcr_debug +#define _gcr_debug(format, ...) \ + do {} while (0) + +#undef _gcr_debugging +#define _gcr_debugging 0 + +#endif /* !defined (WITH_DEBUG) */ + +#endif /* defined (DEBUG_FLAG) */ diff --git a/gcr/gcr-library.c b/gcr/gcr-library.c index 36765582..c52ba385 100644 --- a/gcr/gcr-library.c +++ b/gcr/gcr-library.c @@ -23,6 +23,8 @@ #include "gcr.h" #include "gcr-certificate-renderer.h" +#define DEBUG_FLAG GCR_DEBUG_LIBRARY +#include "gcr-debug.h" #include "gcr-internal.h" #include "gcr-library.h" #include "gcr-key-renderer.h" @@ -182,6 +184,8 @@ _gcr_initialize (void) g_type_class_unref (g_type_class_ref (GCR_TYPE_CERTIFICATE_RENDERER)); g_type_class_unref (g_type_class_ref (GCR_TYPE_KEY_RENDERER)); + + _gcr_debug ("initialized library"); } /** diff --git a/gcr/tests/frob-certificate.c b/gcr/tests/frob-certificate.c index cfb2d8d2..939b9cb7 100644 --- a/gcr/tests/frob-certificate.c +++ b/gcr/tests/frob-certificate.c @@ -102,6 +102,7 @@ int main(int argc, char *argv[]) { gtk_init (&argc, &argv); + g_set_prgname ("frob-certificate"); if (argc > 1) { test_certificate (argv[1]); diff --git a/gcr/tests/frob-key.c b/gcr/tests/frob-key.c index cd02be6c..0c79504e 100644 --- a/gcr/tests/frob-key.c +++ b/gcr/tests/frob-key.c @@ -97,6 +97,7 @@ int main(int argc, char *argv[]) { gtk_init (&argc, &argv); + g_set_prgname ("frob-key"); if (argc > 1) { test_key (argv[1]); diff --git a/gcr/tests/frob-unlock-options.c b/gcr/tests/frob-unlock-options.c index 96b9ff53..eb604e00 100644 --- a/gcr/tests/frob-unlock-options.c +++ b/gcr/tests/frob-unlock-options.c @@ -97,6 +97,7 @@ int main(int argc, char *argv[]) { gtk_init (&argc, &argv); + g_set_prgname ("frob-unlock-options"); chdir_base_dir (argv[0]); test_unlock_options (); diff --git a/gcr/tests/test-certificate-chain.c b/gcr/tests/test-certificate-chain.c index 701fb6d6..d7a0a3c9 100644 --- a/gcr/tests/test-certificate-chain.c +++ b/gcr/tests/test-certificate-chain.c @@ -639,6 +639,7 @@ main (int argc, char **argv) g_type_init (); g_test_init (&argc, &argv, NULL); + g_set_prgname ("test-certificate-chain"); srcdir = g_getenv ("SRCDIR"); if (srcdir && chdir (srcdir) < 0) diff --git a/gcr/tests/test-certificate.c b/gcr/tests/test-certificate.c index 89d17411..137fe2a4 100644 --- a/gcr/tests/test-certificate.c +++ b/gcr/tests/test-certificate.c @@ -259,6 +259,7 @@ main (int argc, char **argv) g_type_init (); g_test_init (&argc, &argv, NULL); + g_set_prgname ("test-certificate"); srcdir = g_getenv ("SRCDIR"); if (srcdir && chdir (srcdir) < 0) diff --git a/gcr/tests/test-parser.c b/gcr/tests/test-parser.c index ae2c466b..e23b2a86 100644 --- a/gcr/tests/test-parser.c +++ b/gcr/tests/test-parser.c @@ -156,6 +156,7 @@ main (int argc, char **argv) g_type_init (); g_test_init (&argc, &argv, NULL); + g_set_prgname ("test-parser"); srcdir = g_getenv ("SRCDIR"); if (srcdir && chdir (srcdir) < 0) diff --git a/gcr/tests/test-pkcs11-certificate.c b/gcr/tests/test-pkcs11-certificate.c index 52379aa5..c5d4c90b 100644 --- a/gcr/tests/test-pkcs11-certificate.c +++ b/gcr/tests/test-pkcs11-certificate.c @@ -269,6 +269,7 @@ main (int argc, char **argv) g_type_init (); g_test_init (&argc, &argv, NULL); + g_set_prgname ("test-pkcs11-certificate"); srcdir = g_getenv ("SRCDIR"); if (srcdir && chdir (srcdir) < 0) diff --git a/gcr/tests/test-simple-certificate.c b/gcr/tests/test-simple-certificate.c index 8b7d4169..707d2949 100644 --- a/gcr/tests/test-simple-certificate.c +++ b/gcr/tests/test-simple-certificate.c @@ -97,6 +97,7 @@ main (int argc, char **argv) g_type_init (); g_test_init (&argc, &argv, NULL); + g_set_prgname ("test-simple-certificate"); srcdir = g_getenv ("SRCDIR"); if (srcdir && chdir (srcdir) < 0) diff --git a/gcr/tests/test-trust.c b/gcr/tests/test-trust.c index f21f80df..a7a33eb8 100644 --- a/gcr/tests/test-trust.c +++ b/gcr/tests/test-trust.c @@ -313,6 +313,7 @@ main (int argc, char **argv) g_type_init (); g_test_init (&argc, &argv, NULL); + g_set_prgname ("test-trust"); srcdir = g_getenv ("SRCDIR"); if (srcdir && chdir (srcdir) < 0) diff --git a/pkcs11/gkm/gkm-data-der.c b/pkcs11/gkm/gkm-data-der.c index 492259ac..4421ef69 100644 --- a/pkcs11/gkm/gkm-data-der.c +++ b/pkcs11/gkm/gkm-data-der.c @@ -75,7 +75,7 @@ init_quarks (void) " (e %m)))" GkmDataResult -gkm_data_der_read_public_key_rsa (const guchar *data, gsize n_data, gcry_sexp_t *s_key) +gkm_data_der_read_public_key_rsa (gconstpointer data, gsize n_data, gcry_sexp_t *s_key) { GkmDataResult ret = GKM_DATA_UNRECOGNIZED; GNode *asn = NULL; @@ -123,7 +123,7 @@ done: " (u %m)))" GkmDataResult -gkm_data_der_read_private_key_rsa (const guchar *data, gsize n_data, gcry_sexp_t *s_key) +gkm_data_der_read_private_key_rsa (gconstpointer data, gsize n_data, gcry_sexp_t *s_key) { GkmDataResult ret = GKM_DATA_UNRECOGNIZED; gcry_mpi_t n, e, d, p, q, u; @@ -200,7 +200,7 @@ done: " (y %m)))" GkmDataResult -gkm_data_der_read_public_key_dsa (const guchar *data, gsize n_data, gcry_sexp_t *s_key) +gkm_data_der_read_public_key_dsa (gconstpointer data, gsize n_data, gcry_sexp_t *s_key) { GkmDataResult ret = GKM_DATA_UNRECOGNIZED; GNode *asn = NULL; @@ -242,8 +242,8 @@ done: } GkmDataResult -gkm_data_der_read_public_key_dsa_parts (const guchar *keydata, gsize n_keydata, - const guchar *params, gsize n_params, +gkm_data_der_read_public_key_dsa_parts (gconstpointer keydata, gsize n_keydata, + gconstpointer params, gsize n_params, gcry_sexp_t *s_key) { gcry_mpi_t p, q, g, y; @@ -300,7 +300,7 @@ done: " (x %m)))" GkmDataResult -gkm_data_der_read_private_key_dsa (const guchar *data, gsize n_data, gcry_sexp_t *s_key) +gkm_data_der_read_private_key_dsa (gconstpointer data, gsize n_data, gcry_sexp_t *s_key) { gcry_mpi_t p, q, g, y, x; GkmDataResult ret = GKM_DATA_UNRECOGNIZED; @@ -344,8 +344,8 @@ done: } GkmDataResult -gkm_data_der_read_private_key_dsa_parts (const guchar *keydata, gsize n_keydata, - const guchar *params, gsize n_params, +gkm_data_der_read_private_key_dsa_parts (gconstpointer keydata, gsize n_keydata, + gconstpointer params, gsize n_params, gcry_sexp_t *s_key) { gcry_mpi_t p, q, g, y, x; @@ -398,7 +398,7 @@ done: } GkmDataResult -gkm_data_der_read_public_key (const guchar *data, gsize n_data, gcry_sexp_t *s_key) +gkm_data_der_read_public_key (gconstpointer data, gsize n_data, gcry_sexp_t *s_key) { GkmDataResult res; @@ -410,7 +410,7 @@ gkm_data_der_read_public_key (const guchar *data, gsize n_data, gcry_sexp_t *s_k } GkmDataResult -gkm_data_der_read_public_key_info (const guchar* data, gsize n_data, gcry_sexp_t* s_key) +gkm_data_der_read_public_key_info (gconstpointer data, gsize n_data, gcry_sexp_t* s_key) { GkmDataResult ret = GKM_DATA_UNRECOGNIZED; GQuark oid; @@ -466,7 +466,7 @@ done: } GkmDataResult -gkm_data_der_read_private_key (const guchar *data, gsize n_data, gcry_sexp_t *s_key) +gkm_data_der_read_private_key (gconstpointer data, gsize n_data, gcry_sexp_t *s_key) { GkmDataResult res; @@ -478,7 +478,7 @@ gkm_data_der_read_private_key (const guchar *data, gsize n_data, gcry_sexp_t *s_ } GkmDataResult -gkm_data_der_read_private_pkcs8_plain (const guchar *data, gsize n_data, gcry_sexp_t *s_key) +gkm_data_der_read_private_pkcs8_plain (gconstpointer data, gsize n_data, gcry_sexp_t *s_key) { GNode *asn = NULL; GkmDataResult ret; @@ -552,7 +552,7 @@ done: } GkmDataResult -gkm_data_der_read_private_pkcs8_crypted (const guchar *data, gsize n_data, const gchar *password, +gkm_data_der_read_private_pkcs8_crypted (gconstpointer data, gsize n_data, const gchar *password, gsize n_password, gcry_sexp_t *s_key) { GNode *asn = NULL; @@ -637,7 +637,7 @@ done: } GkmDataResult -gkm_data_der_read_private_pkcs8 (const guchar *data, gsize n_data, const gchar *password, +gkm_data_der_read_private_pkcs8 (gconstpointer data, gsize n_data, const gchar *password, gsize n_password, gcry_sexp_t *s_key) { GkmDataResult res; @@ -649,7 +649,7 @@ gkm_data_der_read_private_pkcs8 (const guchar *data, gsize n_data, const gchar * return res; } -guchar* +gpointer gkm_data_der_write_public_key_rsa (gcry_sexp_t s_key, gsize *len) { GNode *asn = NULL; @@ -681,7 +681,7 @@ done: return result; } -guchar* +gpointer gkm_data_der_write_private_key_rsa (gcry_sexp_t s_key, gsize *n_key) { GNode *asn = NULL; @@ -747,7 +747,7 @@ done: return result; } -guchar* +gpointer gkm_data_der_write_public_key_dsa (gcry_sexp_t s_key, gsize *len) { GNode *asn = NULL; @@ -788,7 +788,7 @@ done: return result; } -guchar* +gpointer gkm_data_der_write_private_key_dsa_part (gcry_sexp_t skey, gsize *n_key) { GNode *asn = NULL; @@ -817,7 +817,7 @@ done: return result; } -guchar* +gpointer gkm_data_der_write_private_key_dsa_params (gcry_sexp_t skey, gsize *n_params) { GNode *asn = NULL; @@ -852,7 +852,7 @@ done: return result; } -guchar* +gpointer gkm_data_der_write_private_key_dsa (gcry_sexp_t s_key, gsize *len) { GNode *asn = NULL; @@ -896,7 +896,7 @@ done: return result; } -guchar* +gpointer gkm_data_der_write_public_key (gcry_sexp_t s_key, gsize *len) { gboolean is_priv; @@ -919,7 +919,7 @@ gkm_data_der_write_public_key (gcry_sexp_t s_key, gsize *len) } } -guchar* +gpointer gkm_data_der_write_private_key (gcry_sexp_t s_key, gsize *len) { gboolean is_priv; @@ -1011,7 +1011,7 @@ prepare_and_encode_pkcs8_cipher (GNode *asn, const gchar *password, return cih; } -guchar* +gpointer gkm_data_der_write_private_pkcs8_plain (gcry_sexp_t skey, gsize *n_data) { GNode *asn = NULL; @@ -1082,7 +1082,7 @@ gkm_data_der_write_private_pkcs8_plain (gcry_sexp_t skey, gsize *n_data) return data; } -guchar* +gpointer gkm_data_der_write_private_pkcs8_crypted (gcry_sexp_t skey, const gchar *password, gsize n_password, gsize *n_data) { @@ -1140,7 +1140,7 @@ gkm_data_der_write_private_pkcs8_crypted (gcry_sexp_t skey, const gchar *passwor */ GkmDataResult -gkm_data_der_read_certificate (const guchar *data, gsize n_data, GNode **asn1) +gkm_data_der_read_certificate (gconstpointer data, gsize n_data, GNode **asn1) { *asn1 = egg_asn1x_create_and_decode (pkix_asn1_tab, "Certificate", data, n_data); if (!*asn1) @@ -1150,7 +1150,7 @@ gkm_data_der_read_certificate (const guchar *data, gsize n_data, GNode **asn1) } GkmDataResult -gkm_data_der_read_basic_constraints (const guchar *data, gsize n_data, +gkm_data_der_read_basic_constraints (gconstpointer data, gsize n_data, gboolean *is_ca, gint *path_len) { GkmDataResult ret = GKM_DATA_UNRECOGNIZED; @@ -1193,7 +1193,7 @@ done: } GkmDataResult -gkm_data_der_read_key_usage (const guchar *data, gsize n_data, gulong *key_usage) +gkm_data_der_read_key_usage (gconstpointer data, gsize n_data, gulong *key_usage) { GkmDataResult ret = GKM_DATA_UNRECOGNIZED; GNode *asn = NULL; @@ -1216,7 +1216,7 @@ done: } GkmDataResult -gkm_data_der_read_enhanced_usage (const guchar *data, gsize n_data, GQuark **usage_oids) +gkm_data_der_read_enhanced_usage (gconstpointer data, gsize n_data, GQuark **usage_oids) { GkmDataResult ret = GKM_DATA_UNRECOGNIZED; GNode *asn = NULL; @@ -1250,7 +1250,7 @@ done: } -guchar* +gpointer gkm_data_der_write_certificate (GNode *asn1, gsize *n_data) { gpointer result; diff --git a/pkcs11/gkm/gkm-data-der.h b/pkcs11/gkm/gkm-data-der.h index d8321fe3..664259a3 100644 --- a/pkcs11/gkm/gkm-data-der.h +++ b/pkcs11/gkm/gkm-data-der.h @@ -35,89 +35,89 @@ * PRIVATE KEYS */ -GkmDataResult gkm_data_der_read_private_key_rsa (const guchar *data, gsize n_data, +GkmDataResult gkm_data_der_read_private_key_rsa (gconstpointer data, gsize n_data, gcry_sexp_t *s_key); -GkmDataResult gkm_data_der_read_private_key_dsa (const guchar *data, gsize n_data, +GkmDataResult gkm_data_der_read_private_key_dsa (gconstpointer data, gsize n_data, gcry_sexp_t *s_key); -GkmDataResult gkm_data_der_read_private_key_dsa_parts (const guchar *keydata, gsize n_keydata, - const guchar *params, gsize n_params, +GkmDataResult gkm_data_der_read_private_key_dsa_parts (gconstpointer keydata, gsize n_keydata, + gconstpointer params, gsize n_params, gcry_sexp_t *s_key); -GkmDataResult gkm_data_der_read_private_key (const guchar *data, gsize n_data, +GkmDataResult gkm_data_der_read_private_key (gconstpointer data, gsize n_data, gcry_sexp_t *s_key); -GkmDataResult gkm_data_der_read_private_pkcs8 (const guchar *data, gsize n_data, +GkmDataResult gkm_data_der_read_private_pkcs8 (gconstpointer data, gsize n_data, const gchar *password, gsize n_password, gcry_sexp_t *s_key); -GkmDataResult gkm_data_der_read_private_pkcs8_plain (const guchar *data, gsize n_data, +GkmDataResult gkm_data_der_read_private_pkcs8_plain (gconstpointer data, gsize n_data, gcry_sexp_t *s_key); -GkmDataResult gkm_data_der_read_private_pkcs8_crypted (const guchar *data, gsize n_data, +GkmDataResult gkm_data_der_read_private_pkcs8_crypted (gconstpointer data, gsize n_data, const gchar *password, gsize n_password, gcry_sexp_t *s_key); -guchar* gkm_data_der_write_private_key_rsa (gcry_sexp_t s_key, gsize *n_data); +gpointer gkm_data_der_write_private_key_rsa (gcry_sexp_t s_key, gsize *n_data); -guchar* gkm_data_der_write_private_key_dsa (gcry_sexp_t s_key, gsize *len); +gpointer gkm_data_der_write_private_key_dsa (gcry_sexp_t s_key, gsize *len); -guchar* gkm_data_der_write_private_key_dsa_part (gcry_sexp_t skey, gsize *n_key); +gpointer gkm_data_der_write_private_key_dsa_part (gcry_sexp_t skey, gsize *n_key); -guchar* gkm_data_der_write_private_key_dsa_params (gcry_sexp_t skey, gsize *n_params); +gpointer gkm_data_der_write_private_key_dsa_params (gcry_sexp_t skey, gsize *n_params); -guchar* gkm_data_der_write_private_key (gcry_sexp_t s_key, gsize *n_data); +gpointer gkm_data_der_write_private_key (gcry_sexp_t s_key, gsize *n_data); -guchar* gkm_data_der_write_private_pkcs8_plain (gcry_sexp_t skey, gsize *n_data); +gpointer gkm_data_der_write_private_pkcs8_plain (gcry_sexp_t skey, gsize *n_data); -guchar* gkm_data_der_write_private_pkcs8_crypted (gcry_sexp_t skey, const gchar *password, +gpointer gkm_data_der_write_private_pkcs8_crypted (gcry_sexp_t skey, const gchar *password, gsize n_password, gsize *n_data); /* ----------------------------------------------------------------------------- * PUBLIC KEYS */ -GkmDataResult gkm_data_der_read_public_key_rsa (const guchar *data, gsize n_data, +GkmDataResult gkm_data_der_read_public_key_rsa (gconstpointer data, gsize n_data, gcry_sexp_t *s_key); -GkmDataResult gkm_data_der_read_public_key_dsa (const guchar *data, gsize n_data, +GkmDataResult gkm_data_der_read_public_key_dsa (gconstpointer data, gsize n_data, gcry_sexp_t *s_key); -GkmDataResult gkm_data_der_read_public_key_dsa_parts (const guchar *keydata, gsize n_keydata, - const guchar *params, gsize n_params, +GkmDataResult gkm_data_der_read_public_key_dsa_parts (gconstpointer keydata, gsize n_keydata, + gconstpointer params, gsize n_params, gcry_sexp_t *s_key); -GkmDataResult gkm_data_der_read_public_key (const guchar *data, gsize n_data, +GkmDataResult gkm_data_der_read_public_key (gconstpointer data, gsize n_data, gcry_sexp_t *s_key); -GkmDataResult gkm_data_der_read_public_key_info (const guchar *data, gsize n_data, +GkmDataResult gkm_data_der_read_public_key_info (gconstpointer data, gsize n_data, gcry_sexp_t *s_key); -guchar* gkm_data_der_write_public_key_rsa (gcry_sexp_t s_key, gsize *len); +gpointer gkm_data_der_write_public_key_rsa (gcry_sexp_t s_key, gsize *len); -guchar* gkm_data_der_write_public_key_dsa (gcry_sexp_t s_key, gsize *len); +gpointer gkm_data_der_write_public_key_dsa (gcry_sexp_t s_key, gsize *len); -guchar* gkm_data_der_write_public_key (gcry_sexp_t s_key, gsize *len); +gpointer gkm_data_der_write_public_key (gcry_sexp_t s_key, gsize *len); /* ----------------------------------------------------------------------------- * CERTIFICATES */ -GkmDataResult gkm_data_der_read_certificate (const guchar *data, gsize n_data, +GkmDataResult gkm_data_der_read_certificate (gconstpointer data, gsize n_data, GNode **asn1); -GkmDataResult gkm_data_der_read_basic_constraints (const guchar *data, gsize n_data, +GkmDataResult gkm_data_der_read_basic_constraints (gconstpointer data, gsize n_data, gboolean *is_ca, gint *path_len); -GkmDataResult gkm_data_der_read_key_usage (const guchar *data, +GkmDataResult gkm_data_der_read_key_usage (gconstpointer data, gsize n_data, gulong *key_usage); -GkmDataResult gkm_data_der_read_enhanced_usage (const guchar *data, gsize n_data, +GkmDataResult gkm_data_der_read_enhanced_usage (gconstpointer data, gsize n_data, GQuark **oids); -guchar* gkm_data_der_write_certificate (GNode *asn1, gsize *n_data); +gpointer gkm_data_der_write_certificate (GNode *asn1, gsize *n_data); #endif /*GKRPKIXDER_H_*/ diff --git a/pkcs11/gkm/tests/Makefile.am b/pkcs11/gkm/tests/Makefile.am index 662fad92..91cff0ac 100644 --- a/pkcs11/gkm/tests/Makefile.am +++ b/pkcs11/gkm/tests/Makefile.am @@ -1,33 +1,75 @@ -asn1-def-test.h: test.asn - $(ASN1PARSER) -o asn1-def-test.h $(srcdir)/test.asn +INCLUDES = \ + -I$(top_srcdir)/egg \ + -I$(top_srcdir)/pkcs11 \ + -DSRCDIR=$(srcdir) \ + $(GLIB_CFLAGS) \ + $(LIBGCRYPT_CFLAGS) + +LDADD = \ + $(top_builddir)/pkcs11/gkm/libgkm.la \ + $(top_builddir)/egg/libegg.la \ + $(GLIB_LIBS) \ + $(LIBGCRYPT_LIBS) + +noinst_LIBRARIES = libgkm-mock.a -TESTING_SOURCES = \ - asn1-def-test.h \ - test-module.c test-module.h \ +BUILT_SOURCES = \ + asn1-def-test.h + +libgkm_mock_a_SOURCES = \ + mock-module.c mock-module.h \ mock-locked-object.c mock-locked-object.h + $(BUILT_SOURCES) -# Test files should be listed in order they need to run -TESTING_FILES = \ - unit-test-attributes.c \ - unit-test-sexp.c \ - unit-test-data-asn1.c \ - unit-test-data-der.c \ - unit-test-object.c \ - unit-test-credential.c \ - unit-test-timer.c \ - unit-test-transaction.c \ - unit-test-store.c \ - unit-test-memory-store.c \ - unit-test-secret.c \ - unit-test-file-tracker.c - -TESTING_LIBS = \ - $(top_builddir)/pkcs11/gkm/libgkm.la \ - $(top_builddir)/egg/libegg.la +TEST_PROGS = \ + test-attributes \ + test-credential \ + test-data-asn1 \ + test-data-der \ + test-file-tracker \ + test-memory-store \ + test-object \ + test-secret \ + test-sexp \ + test-store \ + test-timer \ + test-transaction + +test_credential_LDADD = \ + libgkm-mock.a \ + $(LDADD) + +test_memory_store_LDADD = \ + libgkm-mock.a \ + $(LDADD) -include $(top_srcdir)/testing/testing.make +test_object_LDADD = \ + libgkm-mock.a \ + $(LDADD) + +test_store_LDADD = \ + libgkm-mock.a \ + $(LDADD) + +test_timer_LDADD = \ + libgkm-mock.a \ + $(LDADD) + +check_PROGRAMS = $(TEST_PROGS) + +test: $(TEST_PROGS) + SRCDIR='$(srcdir)' gtester -k --verbose ${TEST_PROGS} + +check-local: test + +if WITH_TESTS +all-local: $(check_PROGRAMS) +endif + +asn1-def-test.h: test.asn + $(ASN1PARSER) -o asn1-def-test.h $(srcdir)/test.asn -EXTRA_DIST += \ +EXTRA_DIST = \ test.asn \ - test-data + files diff --git a/pkcs11/gkm/tests/test-data/ca-certificates.crt b/pkcs11/gkm/tests/files/ca-certificates.crt index 1eb5c99b..1eb5c99b 100644 --- a/pkcs11/gkm/tests/test-data/ca-certificates.crt +++ b/pkcs11/gkm/tests/files/ca-certificates.crt diff --git a/pkcs11/gkm/tests/test-data/der-pkcs8-PBE-MD5-DES.key b/pkcs11/gkm/tests/files/der-pkcs8-PBE-MD5-DES.key Binary files differindex 71b7d61b..71b7d61b 100644 --- a/pkcs11/gkm/tests/test-data/der-pkcs8-PBE-MD5-DES.key +++ b/pkcs11/gkm/tests/files/der-pkcs8-PBE-MD5-DES.key diff --git a/pkcs11/gkm/tests/test-data/der-pkcs8-PBE-SHA1-3DES.key b/pkcs11/gkm/tests/files/der-pkcs8-PBE-SHA1-3DES.key Binary files differindex 1c37fe10..1c37fe10 100644 --- a/pkcs11/gkm/tests/test-data/der-pkcs8-PBE-SHA1-3DES.key +++ b/pkcs11/gkm/tests/files/der-pkcs8-PBE-SHA1-3DES.key diff --git a/pkcs11/gkm/tests/test-data/der-pkcs8-PBE-SHA1-DES.key b/pkcs11/gkm/tests/files/der-pkcs8-PBE-SHA1-DES.key Binary files differindex 99896139..99896139 100644 --- a/pkcs11/gkm/tests/test-data/der-pkcs8-PBE-SHA1-DES.key +++ b/pkcs11/gkm/tests/files/der-pkcs8-PBE-SHA1-DES.key diff --git a/pkcs11/gkm/tests/test-data/der-pkcs8-PBE-SHA1-RC2-40.key b/pkcs11/gkm/tests/files/der-pkcs8-PBE-SHA1-RC2-40.key Binary files differindex 820926cc..820926cc 100644 --- a/pkcs11/gkm/tests/test-data/der-pkcs8-PBE-SHA1-RC2-40.key +++ b/pkcs11/gkm/tests/files/der-pkcs8-PBE-SHA1-RC2-40.key diff --git a/pkcs11/gkm/tests/test-data/der-pkcs8-PBE-SHA1-RC4-128.key b/pkcs11/gkm/tests/files/der-pkcs8-PBE-SHA1-RC4-128.key Binary files differindex 2888450c..2888450c 100644 --- a/pkcs11/gkm/tests/test-data/der-pkcs8-PBE-SHA1-RC4-128.key +++ b/pkcs11/gkm/tests/files/der-pkcs8-PBE-SHA1-RC4-128.key diff --git a/pkcs11/gkm/tests/test-data/der-pkcs8-dsa.key b/pkcs11/gkm/tests/files/der-pkcs8-dsa.key Binary files differindex 8b61684e..8b61684e 100644 --- a/pkcs11/gkm/tests/test-data/der-pkcs8-dsa.key +++ b/pkcs11/gkm/tests/files/der-pkcs8-dsa.key diff --git a/pkcs11/gkm/tests/test-data/der-pkcs8-encrypted-pkcs5.key b/pkcs11/gkm/tests/files/der-pkcs8-encrypted-pkcs5.key Binary files differindex 68c6b4a5..68c6b4a5 100644 --- a/pkcs11/gkm/tests/test-data/der-pkcs8-encrypted-pkcs5.key +++ b/pkcs11/gkm/tests/files/der-pkcs8-encrypted-pkcs5.key diff --git a/pkcs11/gkm/tests/test-data/der-pkcs8-v2-des.key b/pkcs11/gkm/tests/files/der-pkcs8-v2-des.key Binary files differindex 4cd80348..4cd80348 100644 --- a/pkcs11/gkm/tests/test-data/der-pkcs8-v2-des.key +++ b/pkcs11/gkm/tests/files/der-pkcs8-v2-des.key diff --git a/pkcs11/gkm/tests/test-data/der-pkcs8-v2-des3.key b/pkcs11/gkm/tests/files/der-pkcs8-v2-des3.key Binary files differindex 9e2999c8..9e2999c8 100644 --- a/pkcs11/gkm/tests/test-data/der-pkcs8-v2-des3.key +++ b/pkcs11/gkm/tests/files/der-pkcs8-v2-des3.key diff --git a/pkcs11/gkm/tests/test-data/der-pkcs8.key b/pkcs11/gkm/tests/files/der-pkcs8.key Binary files differindex 16505632..16505632 100644 --- a/pkcs11/gkm/tests/test-data/der-pkcs8.key +++ b/pkcs11/gkm/tests/files/der-pkcs8.key diff --git a/pkcs11/gkm/tests/test-data/pem-rsa-enc.key b/pkcs11/gkm/tests/files/pem-rsa-enc.key index 65439fe6..65439fe6 100644 --- a/pkcs11/gkm/tests/test-data/pem-rsa-enc.key +++ b/pkcs11/gkm/tests/files/pem-rsa-enc.key diff --git a/pkcs11/gkm/tests/test-data/test-certificate-1.der b/pkcs11/gkm/tests/files/test-certificate-1.der Binary files differindex 1c084370..1c084370 100644 --- a/pkcs11/gkm/tests/test-data/test-certificate-1.der +++ b/pkcs11/gkm/tests/files/test-certificate-1.der diff --git a/pkcs11/gkm/tests/test-data/test-certificate-2.der b/pkcs11/gkm/tests/files/test-certificate-2.der Binary files differindex c5d0ce1a..c5d0ce1a 100644 --- a/pkcs11/gkm/tests/test-data/test-certificate-2.der +++ b/pkcs11/gkm/tests/files/test-certificate-2.der diff --git a/pkcs11/gkm/tests/mock-locked-object.h b/pkcs11/gkm/tests/mock-locked-object.h index f68c863c..ad1d0dd2 100644 --- a/pkcs11/gkm/tests/mock-locked-object.h +++ b/pkcs11/gkm/tests/mock-locked-object.h @@ -24,8 +24,8 @@ #include <glib-object.h> -#include "gkm-object.h" -#include "gkm-types.h" +#include "gkm/gkm-object.h" +#include "gkm/gkm-types.h" #define MOCK_TYPE_LOCKED_OBJECT (mock_locked_object_get_type ()) #define MOCK_LOCKED_OBJECT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), MOCK_TYPE_LOCKED_OBJECT, MockLockedObject)) diff --git a/pkcs11/gkm/tests/test-module.c b/pkcs11/gkm/tests/mock-module.c index 20acca6e..b53544bd 100644 --- a/pkcs11/gkm/tests/test-module.c +++ b/pkcs11/gkm/tests/mock-module.c @@ -22,8 +22,10 @@ */ #include "config.h" -#include "test-module.h" -#include "test-suite.h" + +#include "mock-module.h" + +#include "egg/egg-secure-memory.h" /* Include all the module entry points */ #include "gkm/gkm-module-ep.h" @@ -31,8 +33,10 @@ GKM_DEFINE_MODULE (test_module, GKM_TYPE_MODULE); #include "gkm/gkm-certificate.h" +EGG_SECURE_GLIB_DEFINITIONS (); + GkmModule* -test_module_initialize_and_enter (void) +mock_module_initialize_and_enter (void) { CK_RV rv; @@ -42,34 +46,34 @@ test_module_initialize_and_enter (void) g_return_val_if_fail (pkcs11_module, NULL); - test_module_enter (); + mock_module_enter (); return pkcs11_module; } void -test_module_leave_and_finalize (void) +mock_module_leave_and_finalize (void) { CK_RV rv; - test_module_leave (); + mock_module_leave (); rv = test_module_function_list->C_Finalize (NULL); g_return_if_fail (rv == CKR_OK); } void -test_module_leave (void) +mock_module_leave (void) { g_static_mutex_unlock (&pkcs11_module_mutex); } void -test_module_enter (void) +mock_module_enter (void) { g_static_mutex_lock (&pkcs11_module_mutex); } GkmSession* -test_module_open_session (gboolean writable) +mock_module_open_session (gboolean writable) { CK_ULONG flags = CKF_SERIAL_SESSION; CK_SESSION_HANDLE handle; @@ -89,7 +93,7 @@ test_module_open_session (gboolean writable) } GkmObject* -test_module_object_new (GkmSession *session) +mock_module_object_new (GkmSession *session) { CK_BBOOL token = CK_FALSE; CK_OBJECT_CLASS klass = CKO_CERTIFICATE; @@ -97,18 +101,26 @@ test_module_object_new (GkmSession *session) GkmObject *object; gsize n_data; - guchar *data = testing_data_read ("test-certificate-1.der", &n_data); + gchar *data; CK_ATTRIBUTE attrs[] = { + { CKA_VALUE, data, n_data }, { CKA_TOKEN, &token, sizeof (token) }, { CKA_CLASS, &klass, sizeof (klass) }, { CKA_CERTIFICATE_TYPE, &type, sizeof (type) }, - { CKA_VALUE, data, n_data }, }; + if (!g_file_get_contents ("files/test-certificate-1.der", &data, &n_data, NULL)) + g_assert_not_reached (); + + attrs[0].pValue = data; + attrs[0].ulValueLen = n_data; + object = gkm_session_create_object_for_factory (session, GKM_FACTORY_CERTIFICATE, NULL, attrs, G_N_ELEMENTS (attrs)); if (object) /* Owned by storage */ g_object_unref (object); + + g_free (data); return object; } diff --git a/pkcs11/gkm/tests/test-module.h b/pkcs11/gkm/tests/mock-module.h index f4223b50..eb42c5e7 100644 --- a/pkcs11/gkm/tests/test-module.h +++ b/pkcs11/gkm/tests/mock-module.h @@ -1,5 +1,5 @@ /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* test-module.h: A test PKCS#11 module implementation +/* mock-module.h: A test PKCS#11 module implementation Copyright (C) 2009 Stefan Walter @@ -21,25 +21,25 @@ Author: Stef Walter <stef@memberwebs.com> */ -#ifndef TESTMODULE_H_ -#define TESTMODULE_H_ +#ifndef MOCK_MODULE_H_ +#define MOCK_MODULE_H_ #include <glib.h> -#include "gkm-types.h" +#include "gkm/gkm-types.h" #include "pkcs11.h" -void test_module_leave (void); +void mock_module_leave (void); -void test_module_enter (void); +void mock_module_enter (void); -GkmModule* test_module_initialize_and_enter (void); +GkmModule* mock_module_initialize_and_enter (void); -void test_module_leave_and_finalize (void); +void mock_module_leave_and_finalize (void); -GkmSession* test_module_open_session (gboolean writable); +GkmSession* mock_module_open_session (gboolean writable); -GkmObject* test_module_object_new (GkmSession *session); +GkmObject* mock_module_object_new (GkmSession *session); -#endif /* TESTMODULE_H_ */ +#endif /* MOCK_MODULE_H_ */ diff --git a/pkcs11/gkm/tests/unit-test-attributes.c b/pkcs11/gkm/tests/test-attributes.c index a7b1540c..18e20722 100644 --- a/pkcs11/gkm/tests/unit-test-attributes.c +++ b/pkcs11/gkm/tests/test-attributes.c @@ -1,5 +1,5 @@ /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* unit-test-attributes.c: Test attributes functionality +/* test-attributes.c: Test attributes functionality Copyright (C) 2009 Stefan Walter @@ -21,10 +21,12 @@ Author: Stef Walter <stef@memberwebs.com> */ -#include "test-suite.h" +#include "config.h" #include "gkm/gkm-attributes.h" +#include <glib-object.h> + /* Some test data */ static CK_OBJECT_CLASS attr_template_klass = CKO_DATA; static CK_BBOOL attr_template_token = CK_TRUE; @@ -36,14 +38,16 @@ static CK_ATTRIBUTE attr_template[] = { { CKA_VALUE, "\a[\315\025", 4 } }; -TESTING_TEST(attribute_equal_zero_len_null_ptr) +static void +test_attribute_equal_zero_len_null_ptr (void) { CK_ATTRIBUTE attr1 = { CKA_LABEL, "", 0 }; CK_ATTRIBUTE attr2 = { CKA_LABEL, NULL, 0 }; g_assert (gkm_attribute_equal (&attr1, &attr2)); } -TESTING_TEST(attribute_consume) +static void +test_attribute_consume (void) { CK_ATTRIBUTE attr; attr.type = CKA_LABEL; @@ -52,7 +56,8 @@ TESTING_TEST(attribute_consume) g_assert (attr.type == (gulong)-1); } -TESTING_TEST(attribute_consumed) +static void +test_attribute_consumed (void) { CK_ATTRIBUTE attr; gboolean ret; @@ -68,7 +73,8 @@ TESTING_TEST(attribute_consumed) g_assert (ret == TRUE); } -TESTING_TEST(attribute_set_data) +static void +test_attribute_set_data (void) { guchar buffer[32]; CK_ATTRIBUTE attr = { 0, buffer, sizeof (buffer) }; @@ -80,7 +86,8 @@ TESTING_TEST(attribute_set_data) g_assert (memcmp (buffer, "mytest", 6) == 0); } -TESTING_TEST(attribute_set_data_short) +static void +test_attribute_set_data_short (void) { guchar buffer[32]; CK_ATTRIBUTE attr = { 0, buffer, 4 }; @@ -91,7 +98,8 @@ TESTING_TEST(attribute_set_data_short) g_assert (attr.ulValueLen == (CK_ULONG)-1); } -TESTING_TEST(attribute_set_data_length) +static void +test_attribute_set_data_length (void) { CK_ATTRIBUTE attr = { 0, NULL, 0 }; CK_RV rv; @@ -101,7 +109,8 @@ TESTING_TEST(attribute_set_data_length) g_assert (attr.ulValueLen == 6); } -TESTING_TEST(attribute_set_empty) +static void +test_attribute_set_empty (void) { CK_ATTRIBUTE attr; gchar buf[30]; @@ -114,7 +123,8 @@ TESTING_TEST(attribute_set_empty) g_assert (attr.ulValueLen == 0); } -TESTING_TEST(attribute_get_bool) +static void +test_attribute_get_bool (void) { CK_ATTRIBUTE attr; CK_BBOOL val = CK_TRUE; @@ -128,7 +138,8 @@ TESTING_TEST(attribute_get_bool) g_assert (value == TRUE); } -TESTING_TEST(attribute_get_bool_invalid) +static void +test_attribute_get_bool_invalid (void) { CK_ATTRIBUTE attr; CK_ULONG val = 4; @@ -141,7 +152,8 @@ TESTING_TEST(attribute_get_bool_invalid) g_assert (rv == CKR_ATTRIBUTE_VALUE_INVALID); } -TESTING_TEST(attribute_set_time) +static void +test_attribute_set_time (void) { CK_ATTRIBUTE attr; gchar buf[30]; @@ -155,7 +167,8 @@ TESTING_TEST(attribute_set_time) g_assert (memcmp (attr.pValue, "2009071815161100", 16) == 0); } -TESTING_TEST(attribute_set_time_empty) +static void +test_attribute_set_time_empty (void) { CK_ATTRIBUTE attr; gchar buf[30]; @@ -168,7 +181,8 @@ TESTING_TEST(attribute_set_time_empty) g_assert (attr.ulValueLen == 0); } -TESTING_TEST(attribute_set_time_length) +static void +test_attribute_set_time_length (void) { CK_ATTRIBUTE attr; CK_RV rv; @@ -181,7 +195,8 @@ TESTING_TEST(attribute_set_time_length) g_assert (attr.pValue == NULL); } -TESTING_TEST(attribute_get_time) +static void +test_attribute_get_time (void) { CK_ATTRIBUTE attr; glong when; @@ -194,7 +209,8 @@ TESTING_TEST(attribute_get_time) g_assert (when == 1247930171); } -TESTING_TEST(attribute_get_time_empty) +static void +test_attribute_get_time_empty (void) { CK_ATTRIBUTE attr; glong when; @@ -207,7 +223,8 @@ TESTING_TEST(attribute_get_time_empty) g_assert (when == -1); } -TESTING_TEST(attribute_get_time_invalid) +static void +test_attribute_get_time_invalid (void) { CK_ATTRIBUTE attr; glong when; @@ -219,7 +236,8 @@ TESTING_TEST(attribute_get_time_invalid) g_assert (rv == CKR_ATTRIBUTE_VALUE_INVALID); } -TESTING_TEST(attribute_get_time_invalid_length) +static void +test_attribute_get_time_invalid_length (void) { CK_ATTRIBUTE attr; glong when; @@ -231,7 +249,8 @@ TESTING_TEST(attribute_get_time_invalid_length) g_assert (rv == CKR_ATTRIBUTE_VALUE_INVALID); } -TESTING_TEST(attribute_get_string) +static void +test_attribute_get_string (void) { CK_ATTRIBUTE attr; gchar *value; @@ -247,7 +266,8 @@ TESTING_TEST(attribute_get_string) g_free (value); } -TESTING_TEST(attribute_get_string_null) +static void +test_attribute_get_string_null (void) { CK_ATTRIBUTE attr; gchar *value; @@ -261,7 +281,8 @@ TESTING_TEST(attribute_get_string_null) g_assert (value == NULL); } -TESTING_TEST(attribute_get_string_not_utf8) +static void +test_attribute_get_string_not_utf8 (void) { CK_ATTRIBUTE attr; gchar *value; @@ -275,7 +296,8 @@ TESTING_TEST(attribute_get_string_not_utf8) g_assert (rv == CKR_ATTRIBUTE_VALUE_INVALID); } -TESTING_TEST(attribute_get_string_bad_pointer) +static void +test_attribute_get_string_bad_pointer (void) { CK_ATTRIBUTE attr; gchar *value; @@ -289,7 +311,8 @@ TESTING_TEST(attribute_get_string_bad_pointer) g_assert (rv == CKR_ATTRIBUTE_VALUE_INVALID); } -TESTING_TEST(attribute_set_bool) +static void +test_attribute_set_bool (void) { guchar buffer[32]; CK_ATTRIBUTE attr = { 0, buffer, sizeof (buffer) }; @@ -306,7 +329,8 @@ TESTING_TEST(attribute_set_bool) g_assert (memcmp (buffer, "\0", 1) == 0); } -TESTING_TEST(attribute_set_bool_short) +static void +test_attribute_set_bool_short (void) { guchar buffer[32]; CK_ATTRIBUTE attr = { 0, buffer, 0 }; @@ -317,7 +341,8 @@ TESTING_TEST(attribute_set_bool_short) g_assert (attr.ulValueLen == (CK_ULONG)-1); } -TESTING_TEST(attribute_set_bool_length) +static void +test_attribute_set_bool_length (void) { CK_ATTRIBUTE attr = { 0, NULL, 0 }; CK_RV rv; @@ -327,7 +352,8 @@ TESTING_TEST(attribute_set_bool_length) g_assert (attr.ulValueLen == 1); } -TESTING_TEST(attribute_set_ulong) +static void +test_attribute_set_ulong (void) { guchar buffer[32]; CK_ATTRIBUTE attr = { 0, buffer, sizeof (buffer) }; @@ -340,7 +366,8 @@ TESTING_TEST(attribute_set_ulong) g_assert (memcmp (buffer, &value, sizeof (CK_ULONG)) == 0); } -TESTING_TEST(attribute_set_ulong_short) +static void +test_attribute_set_ulong_short (void) { guchar buffer[32]; CK_ATTRIBUTE attr = { 0, buffer, 0 }; @@ -351,7 +378,8 @@ TESTING_TEST(attribute_set_ulong_short) g_assert (attr.ulValueLen == (CK_ULONG)-1); } -TESTING_TEST(attribute_set_ulong_length) +static void +test_attribute_set_ulong_length (void) { CK_ATTRIBUTE attr = { 0, NULL, 0 }; CK_RV rv; @@ -361,7 +389,8 @@ TESTING_TEST(attribute_set_ulong_length) g_assert (attr.ulValueLen == sizeof (CK_ULONG)); } -TESTING_TEST(attribute_set_string) +static void +test_attribute_set_string (void) { guchar buffer[32]; CK_ATTRIBUTE attr = { 0, buffer, sizeof (buffer) }; @@ -373,7 +402,8 @@ TESTING_TEST(attribute_set_string) g_assert (memcmp (buffer, "hello", 5) == 0); } -TESTING_TEST(attribute_set_string_null) +static void +test_attribute_set_string_null (void) { guchar buffer[32]; CK_ATTRIBUTE attr = { 0, buffer, sizeof (buffer) }; @@ -384,7 +414,8 @@ TESTING_TEST(attribute_set_string_null) g_assert (attr.ulValueLen == 0); } -TESTING_TEST(attribute_set_string_short) +static void +test_attribute_set_string_short (void) { guchar buffer[32]; CK_ATTRIBUTE attr = { 0, buffer, 3 }; @@ -395,7 +426,8 @@ TESTING_TEST(attribute_set_string_short) g_assert (attr.ulValueLen == (CK_ULONG)-1); } -TESTING_TEST(attribute_set_string_length) +static void +test_attribute_set_string_length (void) { CK_ATTRIBUTE attr = { 0, NULL, 0 }; CK_RV rv; @@ -405,7 +437,8 @@ TESTING_TEST(attribute_set_string_length) g_assert (attr.ulValueLen == 5); } -TESTING_TEST(attribute_set_date) +static void +test_attribute_set_date (void) { guchar buffer[32]; CK_ATTRIBUTE attr = { 0, buffer, sizeof (buffer) }; @@ -421,7 +454,8 @@ TESTING_TEST(attribute_set_date) g_assert (memcmp (date->year, "2009", 4) == 0); } -TESTING_TEST(attribute_set_date_none) +static void +test_attribute_set_date_none (void) { guchar buffer[32]; CK_ATTRIBUTE attr = { 0, buffer, sizeof (buffer) }; @@ -432,7 +466,8 @@ TESTING_TEST(attribute_set_date_none) g_assert (attr.ulValueLen == 0); } -TESTING_TEST(attribute_set_date_short) +static void +test_attribute_set_date_short (void) { guchar buffer[32]; CK_ATTRIBUTE attr = { 0, buffer, 5 }; @@ -443,7 +478,8 @@ TESTING_TEST(attribute_set_date_short) g_assert (attr.ulValueLen == (CK_ULONG)-1); } -TESTING_TEST(attribute_set_date_length) +static void +test_attribute_set_date_length (void) { CK_ATTRIBUTE attr = { 0, NULL, 0 }; CK_RV rv; @@ -453,7 +489,8 @@ TESTING_TEST(attribute_set_date_length) g_assert (attr.ulValueLen == sizeof (CK_DATE)); } -TESTING_TEST(attribute_set_mpi) +static void +test_attribute_set_mpi (void) { guchar buffer[32]; CK_ATTRIBUTE attr = { 0, buffer, sizeof (buffer) }; @@ -471,7 +508,8 @@ TESTING_TEST(attribute_set_mpi) gcry_mpi_release (mpi); } -TESTING_TEST(attribute_set_mpi_short) +static void +test_attribute_set_mpi_short (void) { guchar buffer[32]; CK_ATTRIBUTE attr = { 0, buffer, 2 }; @@ -488,7 +526,8 @@ TESTING_TEST(attribute_set_mpi_short) gcry_mpi_release (mpi); } -TESTING_TEST(attribute_set_mpi_length) +static void +test_attribute_set_mpi_length (void) { CK_ATTRIBUTE attr = { 0, NULL, 0 }; gcry_mpi_t mpi; @@ -504,7 +543,8 @@ TESTING_TEST(attribute_set_mpi_length) gcry_mpi_release (mpi); } -TESTING_TEST(attribute_equal) +static void +test_attribute_equal (void) { /* Make sure we actually have two different strings */ gchar *val1 = g_strdup ("my-identifier"); @@ -520,7 +560,8 @@ TESTING_TEST(attribute_equal) g_free (val2); } -TESTING_TEST(attribute_equal_same) +static void +test_attribute_equal_same (void) { CK_ATTRIBUTE attr = { CKA_ID, "my-identifier", 13 }; gboolean ret; @@ -529,7 +570,8 @@ TESTING_TEST(attribute_equal_same) g_assert (ret == TRUE); } -TESTING_TEST(attribute_equal_same_pointer) +static void +test_attribute_equal_same_pointer (void) { gchar *val = "my-identifier"; CK_ATTRIBUTE attr1 = { CKA_ID, val, 13 }; @@ -540,7 +582,8 @@ TESTING_TEST(attribute_equal_same_pointer) g_assert (ret == TRUE); } -TESTING_TEST(attribute_equal_diff_types) +static void +test_attribute_equal_diff_types (void) { gchar *val = "my-identifier"; CK_ATTRIBUTE attr1 = { CKA_ID, val, 13 }; @@ -551,7 +594,8 @@ TESTING_TEST(attribute_equal_diff_types) g_assert (ret == FALSE); } -TESTING_TEST(attribute_equal_diff_length) +static void +test_attribute_equal_diff_length (void) { CK_ATTRIBUTE attr1 = { CKA_ID, "my-identifier", 13 }; CK_ATTRIBUTE attr2 = { CKA_ID, "my-identifier", 2 }; @@ -561,7 +605,8 @@ TESTING_TEST(attribute_equal_diff_length) g_assert (ret == FALSE); } -TESTING_TEST(attribute_equal_diff_value) +static void +test_attribute_equal_diff_value (void) { CK_ATTRIBUTE attr1 = { CKA_ID, "my-identifier", 13 }; CK_ATTRIBUTE attr2 = { CKA_ID, "xy-identifier", 13 }; @@ -571,7 +616,8 @@ TESTING_TEST(attribute_equal_diff_value) g_assert (ret == FALSE); } -TESTING_TEST(attribute_hash) +static void +test_attribute_hash (void) { CK_ATTRIBUTE attr = { CKA_VALUE, "value", 5 }; guint hash; @@ -581,7 +627,8 @@ TESTING_TEST(attribute_hash) g_assert_cmpuint (hash, !=, 0U); } -TESTING_TEST(attribute_contains) +static void +test_attribute_contains (void) { CK_ATTRIBUTE attr = { CKA_ID, "my-identifier", 13 }; gboolean ret; @@ -590,7 +637,8 @@ TESTING_TEST(attribute_contains) g_assert (ret == TRUE); } -TESTING_TEST(attribute_contains_no_value) +static void +test_attribute_contains_no_value (void) { CK_ATTRIBUTE attr = { CKA_ID, "other-identifier", 16 }; gboolean ret; @@ -599,7 +647,8 @@ TESTING_TEST(attribute_contains_no_value) g_assert (ret == FALSE); } -TESTING_TEST(attribute_contains_no_type) +static void +test_attribute_contains_no_type (void) { CK_ATTRIBUTE attr = { CKA_VALUE, "value", 5 }; gboolean ret; @@ -608,7 +657,8 @@ TESTING_TEST(attribute_contains_no_type) g_assert (ret == FALSE); } -TESTING_TEST(attributes_find) +static void +test_attributes_find (void) { CK_ATTRIBUTE_PTR attr; @@ -617,7 +667,8 @@ TESTING_TEST(attributes_find) g_assert (attr->type == CKA_LABEL); } -TESTING_TEST(attributes_find_not_found) +static void +test_attributes_find_not_found (void) { CK_ATTRIBUTE_PTR attr; @@ -625,7 +676,8 @@ TESTING_TEST(attributes_find_not_found) g_assert (attr == NULL); } -TESTING_TEST(attribute_find_boolean) +static void +test_attribute_find_boolean (void) { gboolean value; gboolean ret; @@ -635,7 +687,8 @@ TESTING_TEST(attribute_find_boolean) g_assert (value == TRUE); } -TESTING_TEST(attribute_find_boolean_no_type) +static void +test_attribute_find_boolean_no_type (void) { gboolean value; gboolean ret; @@ -644,7 +697,8 @@ TESTING_TEST(attribute_find_boolean_no_type) g_assert (ret == FALSE); } -TESTING_TEST(attribute_find_boolean_not_bbool) +static void +test_attribute_find_boolean_not_bbool (void) { gboolean value; gboolean ret; @@ -653,7 +707,8 @@ TESTING_TEST(attribute_find_boolean_not_bbool) g_assert (ret == FALSE); } -TESTING_TEST(attribute_find_ulong) +static void +test_attribute_find_ulong (void) { gulong value; gboolean ret; @@ -663,7 +718,8 @@ TESTING_TEST(attribute_find_ulong) g_assert (value == CKO_DATA); } -TESTING_TEST(attribute_find_ulong_no_type) +static void +test_attribute_find_ulong_no_type (void) { gulong value; gboolean ret; @@ -672,7 +728,8 @@ TESTING_TEST(attribute_find_ulong_no_type) g_assert (ret == FALSE); } -TESTING_TEST(attribute_find_ulong_not_ulong) +static void +test_attribute_find_ulong_not_ulong (void) { gulong value; gboolean ret; @@ -681,7 +738,8 @@ TESTING_TEST(attribute_find_ulong_not_ulong) g_assert (ret == FALSE); } -TESTING_TEST(attribute_find_mpi) +static void +test_attribute_find_mpi (void) { gcry_mpi_t mpi = NULL; gboolean ret; @@ -694,7 +752,8 @@ TESTING_TEST(attribute_find_mpi) gcry_mpi_release (mpi); } -TESTING_TEST(attribute_find_mpi_no_type) +static void +test_attribute_find_mpi_no_type (void) { gcry_mpi_t mpi = NULL; gboolean ret; @@ -704,7 +763,8 @@ TESTING_TEST(attribute_find_mpi_no_type) g_assert (mpi == NULL); } -TESTING_TEST(attributes_consume) +static void +test_attributes_consume (void) { CK_ATTRIBUTE_PTR attrs; CK_ULONG n_attrs; @@ -729,14 +789,16 @@ TESTING_TEST(attributes_consume) g_free (attrs); } -TESTING_TEST(template_new_free) +static void +test_template_new_free (void) { GArray *template = gkm_template_new (attr_template, G_N_ELEMENTS (attr_template)); g_assert (template); gkm_template_free (template); } -TESTING_TEST(template_find) +static void +test_template_find (void) { GArray *template = gkm_template_new (attr_template, G_N_ELEMENTS (attr_template)); gulong uvalue; @@ -757,7 +819,8 @@ TESTING_TEST(template_find) gkm_template_free (template); } -TESTING_TEST(template_set_replace) +static void +test_template_set_replace (void) { GArray *template = gkm_template_new (attr_template, G_N_ELEMENTS (attr_template)); CK_OBJECT_CLASS klass = CKO_HW_FEATURE; @@ -777,3 +840,74 @@ TESTING_TEST(template_set_replace) gkm_template_free (template); } + +int +main (int argc, char **argv) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + + g_test_add_func ("/gkm/attributes/attribute_equal_zero_len_null_ptr", test_attribute_equal_zero_len_null_ptr); + g_test_add_func ("/gkm/attributes/attribute_consume", test_attribute_consume); + g_test_add_func ("/gkm/attributes/attribute_consumed", test_attribute_consumed); + g_test_add_func ("/gkm/attributes/attribute_set_data", test_attribute_set_data); + g_test_add_func ("/gkm/attributes/attribute_set_data_short", test_attribute_set_data_short); + g_test_add_func ("/gkm/attributes/attribute_set_data_length", test_attribute_set_data_length); + g_test_add_func ("/gkm/attributes/attribute_set_empty", test_attribute_set_empty); + g_test_add_func ("/gkm/attributes/attribute_get_bool", test_attribute_get_bool); + g_test_add_func ("/gkm/attributes/attribute_get_bool_invalid", test_attribute_get_bool_invalid); + g_test_add_func ("/gkm/attributes/attribute_set_time", test_attribute_set_time); + g_test_add_func ("/gkm/attributes/attribute_set_time_empty", test_attribute_set_time_empty); + g_test_add_func ("/gkm/attributes/attribute_set_time_length", test_attribute_set_time_length); + g_test_add_func ("/gkm/attributes/attribute_get_time", test_attribute_get_time); + g_test_add_func ("/gkm/attributes/attribute_get_time_empty", test_attribute_get_time_empty); + g_test_add_func ("/gkm/attributes/attribute_get_time_invalid", test_attribute_get_time_invalid); + g_test_add_func ("/gkm/attributes/attribute_get_time_invalid_length", test_attribute_get_time_invalid_length); + g_test_add_func ("/gkm/attributes/attribute_get_string", test_attribute_get_string); + g_test_add_func ("/gkm/attributes/attribute_get_string_null", test_attribute_get_string_null); + g_test_add_func ("/gkm/attributes/attribute_get_string_not_utf8", test_attribute_get_string_not_utf8); + g_test_add_func ("/gkm/attributes/attribute_get_string_bad_pointer", test_attribute_get_string_bad_pointer); + g_test_add_func ("/gkm/attributes/attribute_set_bool", test_attribute_set_bool); + g_test_add_func ("/gkm/attributes/attribute_set_bool_short", test_attribute_set_bool_short); + g_test_add_func ("/gkm/attributes/attribute_set_bool_length", test_attribute_set_bool_length); + g_test_add_func ("/gkm/attributes/attribute_set_ulong", test_attribute_set_ulong); + g_test_add_func ("/gkm/attributes/attribute_set_ulong_short", test_attribute_set_ulong_short); + g_test_add_func ("/gkm/attributes/attribute_set_ulong_length", test_attribute_set_ulong_length); + g_test_add_func ("/gkm/attributes/attribute_set_string", test_attribute_set_string); + g_test_add_func ("/gkm/attributes/attribute_set_string_null", test_attribute_set_string_null); + g_test_add_func ("/gkm/attributes/attribute_set_string_short", test_attribute_set_string_short); + g_test_add_func ("/gkm/attributes/attribute_set_string_length", test_attribute_set_string_length); + g_test_add_func ("/gkm/attributes/attribute_set_date", test_attribute_set_date); + g_test_add_func ("/gkm/attributes/attribute_set_date_none", test_attribute_set_date_none); + g_test_add_func ("/gkm/attributes/attribute_set_date_short", test_attribute_set_date_short); + g_test_add_func ("/gkm/attributes/attribute_set_date_length", test_attribute_set_date_length); + g_test_add_func ("/gkm/attributes/attribute_set_mpi", test_attribute_set_mpi); + g_test_add_func ("/gkm/attributes/attribute_set_mpi_short", test_attribute_set_mpi_short); + g_test_add_func ("/gkm/attributes/attribute_set_mpi_length", test_attribute_set_mpi_length); + g_test_add_func ("/gkm/attributes/attribute_equal", test_attribute_equal); + g_test_add_func ("/gkm/attributes/attribute_equal_same", test_attribute_equal_same); + g_test_add_func ("/gkm/attributes/attribute_equal_same_pointer", test_attribute_equal_same_pointer); + g_test_add_func ("/gkm/attributes/attribute_equal_diff_types", test_attribute_equal_diff_types); + g_test_add_func ("/gkm/attributes/attribute_equal_diff_length", test_attribute_equal_diff_length); + g_test_add_func ("/gkm/attributes/attribute_equal_diff_value", test_attribute_equal_diff_value); + g_test_add_func ("/gkm/attributes/attribute_hash", test_attribute_hash); + g_test_add_func ("/gkm/attributes/attribute_contains", test_attribute_contains); + g_test_add_func ("/gkm/attributes/attribute_contains_no_value", test_attribute_contains_no_value); + g_test_add_func ("/gkm/attributes/attribute_contains_no_type", test_attribute_contains_no_type); + g_test_add_func ("/gkm/attributes/attributes_find", test_attributes_find); + g_test_add_func ("/gkm/attributes/attributes_find_not_found", test_attributes_find_not_found); + g_test_add_func ("/gkm/attributes/attribute_find_boolean", test_attribute_find_boolean); + g_test_add_func ("/gkm/attributes/attribute_find_boolean_no_type", test_attribute_find_boolean_no_type); + g_test_add_func ("/gkm/attributes/attribute_find_boolean_not_bbool", test_attribute_find_boolean_not_bbool); + g_test_add_func ("/gkm/attributes/attribute_find_ulong", test_attribute_find_ulong); + g_test_add_func ("/gkm/attributes/attribute_find_ulong_no_type", test_attribute_find_ulong_no_type); + g_test_add_func ("/gkm/attributes/attribute_find_ulong_not_ulong", test_attribute_find_ulong_not_ulong); + g_test_add_func ("/gkm/attributes/attribute_find_mpi", test_attribute_find_mpi); + g_test_add_func ("/gkm/attributes/attribute_find_mpi_no_type", test_attribute_find_mpi_no_type); + g_test_add_func ("/gkm/attributes/attributes_consume", test_attributes_consume); + g_test_add_func ("/gkm/attributes/template_new_free", test_template_new_free); + g_test_add_func ("/gkm/attributes/template_find", test_template_find); + g_test_add_func ("/gkm/attributes/template_set_replace", test_template_set_replace); + + return g_test_run (); +} diff --git a/pkcs11/gkm/tests/unit-test-credential.c b/pkcs11/gkm/tests/test-credential.c index 40b605e7..5a7b70e4 100644 --- a/pkcs11/gkm/tests/unit-test-credential.c +++ b/pkcs11/gkm/tests/test-credential.c @@ -1,5 +1,5 @@ /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* unit-test-timer.c: Test thread timer functionality +/* test-credential.c: Test credentials Copyright (C) 2009 Stefan Walter @@ -21,8 +21,9 @@ Author: Stef Walter <stef@memberwebs.com> */ -#include "test-suite.h" -#include "test-module.h" +#include "config.h" + +#include "mock-module.h" #include "mock-locked-object.h" #include "gkm/gkm-attributes.h" @@ -34,37 +35,38 @@ #include "pkcs11i.h" -static GkmModule *module = NULL; -static GkmSession *session = NULL; -static GkmObject *object = NULL; +typedef struct { + GkmModule *module; + GkmSession *session; + GkmObject *object; +} Test; -TESTING_SETUP(credential_setup) +static void +setup (Test *test, gconstpointer unused) { CK_RV rv; - module = test_module_initialize_and_enter (); - session = test_module_open_session (TRUE); + test->module = mock_module_initialize_and_enter (); + test->session = mock_module_open_session (TRUE); - rv = gkm_module_C_Login (module, gkm_session_get_handle (session), CKU_USER, NULL, 0); + rv = gkm_module_C_Login (test->module, gkm_session_get_handle (test->session), CKU_USER, NULL, 0); g_assert (rv == CKR_OK); - object = mock_locked_object_new (module, gkm_module_get_manager (module)); - gkm_object_expose (object, TRUE); + test->object = mock_locked_object_new (test->module, gkm_module_get_manager (test->module)); + gkm_object_expose (test->object, TRUE); } -TESTING_TEARDOWN(credential_teardown) +static void +teardown (Test *test, gconstpointer unused) { - g_object_unref (object); - object = NULL; - - test_module_leave_and_finalize (); - module = NULL; - session = NULL; + g_object_unref (test->object); + mock_module_leave_and_finalize (); } -TESTING_TEST(credential_create) +static void +test_create (Test *test, gconstpointer unused) { CK_OBJECT_CLASS klass = CKO_G_CREDENTIAL; - CK_OBJECT_HANDLE locked = gkm_object_get_handle (object); + CK_OBJECT_HANDLE locked = gkm_object_get_handle (test->object); CK_ATTRIBUTE attrs[] = { { CKA_CLASS, &klass, sizeof (klass) }, @@ -75,18 +77,19 @@ TESTING_TEST(credential_create) CK_OBJECT_HANDLE handle; CK_RV rv; - rv = gkm_session_C_CreateObject (session, attrs, G_N_ELEMENTS (attrs), &handle); + rv = gkm_session_C_CreateObject (test->session, attrs, G_N_ELEMENTS (attrs), &handle); g_assert (rv == CKR_OK); g_assert (handle != 0); - rv = gkm_session_C_DestroyObject (session, handle); + rv = gkm_session_C_DestroyObject (test->session, handle); g_assert (rv == CKR_OK); } -TESTING_TEST(credential_create_missing_pin) +static void +test_create_missing_pin (Test *test, gconstpointer unused) { CK_OBJECT_CLASS klass = CKO_G_CREDENTIAL; - CK_OBJECT_HANDLE locked = gkm_object_get_handle (object); + CK_OBJECT_HANDLE locked = gkm_object_get_handle (test->object); CK_ATTRIBUTE attrs[] = { { CKA_CLASS, &klass, sizeof (klass) }, @@ -96,11 +99,12 @@ TESTING_TEST(credential_create_missing_pin) CK_OBJECT_HANDLE handle; CK_RV rv; - rv = gkm_session_C_CreateObject (session, attrs, G_N_ELEMENTS (attrs), &handle); + rv = gkm_session_C_CreateObject (test->session, attrs, G_N_ELEMENTS (attrs), &handle); g_assert (rv == CKR_USER_NOT_LOGGED_IN); } -TESTING_TEST(credential_create_no_object) +static void +test_create_no_object (Test *test, gconstpointer unused) { CK_OBJECT_CLASS klass = CKO_G_CREDENTIAL; CK_BBOOL token = CK_FALSE; @@ -115,19 +119,20 @@ TESTING_TEST(credential_create_no_object) CK_OBJECT_HANDLE handle; CK_RV rv; - rv = gkm_session_C_CreateObject (session, attrs, G_N_ELEMENTS (attrs), &handle); + rv = gkm_session_C_CreateObject (test->session, attrs, G_N_ELEMENTS (attrs), &handle); g_assert (rv == CKR_OK); g_assert (handle != 0); attr.type = CKA_G_OBJECT; attr.pValue = &objhand; attr.ulValueLen = sizeof (objhand); - rv = gkm_session_C_GetAttributeValue (session, handle, &attr, 1); + rv = gkm_session_C_GetAttributeValue (test->session, handle, &attr, 1); g_assert (rv == CKR_OK); g_assert (objhand == 0); } -TESTING_TEST(credential_create_invalid_object) +static void +test_create_invalid_object (Test *test, gconstpointer unused) { CK_OBJECT_CLASS klass = CKO_G_CREDENTIAL; CK_OBJECT_HANDLE locked = 0; @@ -142,14 +147,15 @@ TESTING_TEST(credential_create_invalid_object) CK_OBJECT_HANDLE handle; CK_RV rv; - rv = gkm_session_C_CreateObject (session, attrs, G_N_ELEMENTS (attrs), &handle); + rv = gkm_session_C_CreateObject (test->session, attrs, G_N_ELEMENTS (attrs), &handle); g_assert (rv == CKR_OBJECT_HANDLE_INVALID); } -TESTING_TEST(credential_get_attributes) +static void +test_get_attributes (Test *test, gconstpointer unused) { CK_OBJECT_CLASS klass = CKO_G_CREDENTIAL; - CK_OBJECT_HANDLE locked = gkm_object_get_handle (object); + CK_OBJECT_HANDLE locked = gkm_object_get_handle (test->object); CK_ATTRIBUTE attrs[] = { { CKA_CLASS, &klass, sizeof (klass) }, @@ -162,7 +168,7 @@ TESTING_TEST(credential_get_attributes) CK_ULONG value; CK_RV rv; - rv = gkm_session_C_CreateObject (session, attrs, G_N_ELEMENTS (attrs), &handle); + rv = gkm_session_C_CreateObject (test->session, attrs, G_N_ELEMENTS (attrs), &handle); g_assert (rv == CKR_OK); g_assert (handle != 0); @@ -170,33 +176,35 @@ TESTING_TEST(credential_get_attributes) check.pValue = &value; check.ulValueLen = sizeof (value); - rv = gkm_session_C_GetAttributeValue (session, handle, &check, 1); + rv = gkm_session_C_GetAttributeValue (test->session, handle, &check, 1); g_assert (rv == CKR_OK); g_assert (check.ulValueLen == sizeof (value)); g_assert (value == locked); } -TESTING_TEST(credential_object_property) +static void +test_object_property (Test *test, gconstpointer unused) { GkmCredential *auth; GkmObject *check; CK_RV rv; - rv = gkm_credential_create (module, NULL, object, (guchar*)"mock", 4, &auth); + rv = gkm_credential_create (test->module, NULL, test->object, (guchar*)"mock", 4, &auth); g_assert (rv == CKR_OK); g_assert (auth); g_object_get (auth, "object", &check, NULL); - g_assert (check == object); + g_assert (check == test->object); g_object_unref (check); check = gkm_credential_get_object (auth); - g_assert (check == object); + g_assert (check == test->object); g_object_unref (auth); } -TESTING_TEST(credential_login_property) +static void +test_login_property (Test *test, gconstpointer unused) { GkmCredential *cred; GkmSecret *check, *secret; @@ -204,7 +212,7 @@ TESTING_TEST(credential_login_property) gsize n_password; CK_RV rv; - rv = gkm_credential_create (module, NULL, object, (guchar*)"mock", 4, &cred); + rv = gkm_credential_create (test->module, NULL, test->object, (guchar*)"mock", 4, &cred); g_assert (rv == CKR_OK); g_assert (cred); @@ -239,14 +247,15 @@ boxed_string (void) return type; } -TESTING_TEST(credential_data) +static void +test_data (Test *test, gconstpointer unused) { GkmCredential *cred; GType type = boxed_string (); gchar *check; CK_RV rv; - rv = gkm_credential_create (module, NULL, object, (guchar*)"mock", 4, &cred); + rv = gkm_credential_create (test->module, NULL, test->object, (guchar*)"mock", 4, &cred); g_assert (rv == CKR_OK); g_assert (cred); @@ -269,17 +278,37 @@ TESTING_TEST(credential_data) g_object_unref (cred); } -TESTING_TEST(credential_connect_object) +static void +test_connect_object (Test *test, gconstpointer unused) { GkmCredential *cred; CK_RV rv; - rv = gkm_credential_create (module, NULL, NULL, (guchar*)"mock", 4, &cred); + rv = gkm_credential_create (test->module, NULL, NULL, (guchar*)"mock", 4, &cred); g_assert (rv == CKR_OK); g_assert (cred); - gkm_credential_connect (cred, object); - g_assert (gkm_credential_get_object (cred) == object); + gkm_credential_connect (cred, test->object); + g_assert (gkm_credential_get_object (cred) == test->object); g_object_unref (cred); } + +int +main (int argc, char **argv) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + + g_test_add ("/gkm/credential/create", Test, NULL, setup, test_create, teardown); + g_test_add ("/gkm/credential/create_missing_pin", Test, NULL, setup, test_create_missing_pin, teardown); + g_test_add ("/gkm/credential/create_no_object", Test, NULL, setup, test_create_no_object, teardown); + g_test_add ("/gkm/credential/create_invalid_object", Test, NULL, setup, test_create_invalid_object, teardown); + g_test_add ("/gkm/credential/get_attributes", Test, NULL, setup, test_get_attributes, teardown); + g_test_add ("/gkm/credential/object_property", Test, NULL, setup, test_object_property, teardown); + g_test_add ("/gkm/credential/login_property", Test, NULL, setup, test_login_property, teardown); + g_test_add ("/gkm/credential/data", Test, NULL, setup, test_data, teardown); + g_test_add ("/gkm/credential/connect_object", Test, NULL, setup, test_connect_object, teardown); + + return g_test_run (); +} diff --git a/pkcs11/gkm/tests/unit-test-data-asn1.c b/pkcs11/gkm/tests/test-data-asn1.c index dc7ef6d8..d6e3dec3 100644 --- a/pkcs11/gkm/tests/unit-test-data-asn1.c +++ b/pkcs11/gkm/tests/test-data-asn1.c @@ -1,5 +1,5 @@ /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* unit-test-data-asn1.c: Test ASN.1 routines +/* test-data-asn1.c: Test ASN.1 routines Copyright (C) 2007 Stefan Walter @@ -23,11 +23,10 @@ #include "config.h" -#include "test-suite.h" - #include "gkm/gkm-data-asn1.h" #include <glib.h> +#include <glib-object.h> #include <gcrypt.h> #include <stdlib.h> @@ -39,25 +38,31 @@ #include "asn1-def-test.h" -static GNode *asn1_cert = NULL; -static guchar *data_cert = NULL; -static gsize n_data_cert = 0; +typedef struct { + GNode *asn1_cert; + gchar *data_cert; + gsize n_data_cert; +} Test; -TESTING_SETUP(asn1_tree) +static void +setup (Test *test, gconstpointer unused) { - data_cert = testing_data_read ("test-certificate-1.der", &n_data_cert); - asn1_cert = egg_asn1x_create_and_decode (pkix_asn1_tab, "Certificate", data_cert, n_data_cert); - g_assert (asn1_cert); + if (!g_file_get_contents ("files/test-certificate-1.der", &test->data_cert, &test->n_data_cert, NULL)) + g_assert_not_reached (); + + test->asn1_cert = egg_asn1x_create_and_decode (pkix_asn1_tab, "Certificate", test->data_cert, test->n_data_cert); + g_assert (test->asn1_cert); } -TESTING_TEARDOWN(asn1_tree) +static void +teardown (Test *test, gconstpointer unused) { - egg_asn1x_destroy (asn1_cert); - g_free (data_cert); - data_cert = NULL; + egg_asn1x_destroy (test->asn1_cert); + g_free (test->data_cert); } -TESTING_TEST(asn1_integers) +static void +test_asn1_integers (Test *test, gconstpointer unused) { GNode *asn; gcry_mpi_t mpi, mpt; @@ -93,3 +98,14 @@ TESTING_TEST(asn1_integers) g_free (data); } + +int +main (int argc, char **argv) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + + g_test_add ("/gkm/data-asn1/integers", Test, NULL, setup, test_asn1_integers, teardown); + + return g_test_run (); +} diff --git a/pkcs11/gkm/tests/unit-test-data-der.c b/pkcs11/gkm/tests/test-data-der.c index a6aba538..f3b7ab10 100644 --- a/pkcs11/gkm/tests/unit-test-data-der.c +++ b/pkcs11/gkm/tests/test-data-der.c @@ -23,8 +23,6 @@ #include "config.h" -#include "test-suite.h" - #include "gkm/gkm-crypto.h" #include "gkm/gkm-data-asn1.h" #include "gkm/gkm-data-der.h" @@ -42,13 +40,16 @@ #include <stdio.h> #include <string.h> -static GNode *certificate = NULL; -static guchar *certificate_data = NULL; -static gsize n_certificate_data = 0; +EGG_SECURE_GLIB_DEFINITIONS (); -static GNode *certificate2 = NULL; -static guchar *certificate2_data = NULL; -static gsize n_certificate2_data = 0; +typedef struct { + GNode *certificate; + gchar *certificate_data; + gsize n_certificate_data; + GNode *certificate2; + gchar *certificate2_data; + gsize n_certificate2_data; +} Test; const gchar *rsapub = "(public-key (rsa" \ " (n #00AE4B381CF43F7DC24CF90827325E2FB2EB57EDDE29562DF391C8942AA8E6423410E2D3FE26381F9DE0395E74BF2D17621AE46992C72CF895F6FA5FBE98054FBF#)" \ @@ -112,29 +113,30 @@ test_der_public (gcry_sexp_t key) g_assert ("key parsed differently" && compare_keys (key, sexp)); } -TESTING_SETUP(preload) +static void +setup (Test *test, gconstpointer unused) { - certificate_data = testing_data_read ("test-certificate-1.der", &n_certificate_data); - certificate = egg_asn1x_create_and_decode (pkix_asn1_tab, "Certificate", certificate_data, n_certificate_data); - g_assert (certificate); - - certificate2_data = testing_data_read ("test-certificate-2.der", &n_certificate2_data); - certificate2 = egg_asn1x_create_and_decode (pkix_asn1_tab, "Certificate", certificate2_data, n_certificate2_data); - g_assert (certificate2); + if (!g_file_get_contents ("files/test-certificate-1.der", &test->certificate_data, &test->n_certificate_data, NULL)) + g_assert_not_reached (); + test->certificate = egg_asn1x_create_and_decode (pkix_asn1_tab, "Certificate", test->certificate_data, test->n_certificate_data); + g_assert (test->certificate); + if (!g_file_get_contents ("files/test-certificate-2.der", &test->certificate2_data, &test->n_certificate2_data, NULL)) + g_assert_not_reached (); + test->certificate2 = egg_asn1x_create_and_decode (pkix_asn1_tab, "Certificate", test->certificate2_data, test->n_certificate2_data); + g_assert (test->certificate2); } -TESTING_TEARDOWN(preload) +static void +teardown (Test *test, gconstpointer unused) { - egg_asn1x_destroy (certificate); - g_free (certificate_data); - certificate_data = NULL; - - egg_asn1x_destroy (certificate2); - g_free (certificate2_data); - certificate2_data = NULL; + egg_asn1x_destroy (test->certificate); + g_free (test->certificate_data); + egg_asn1x_destroy (test->certificate2); + g_free (test->certificate2_data); } -TESTING_TEST(der_rsa_public) +static void +test_der_rsa_public (Test *test, gconstpointer unused) { gcry_sexp_t key; gcry_error_t gcry; @@ -145,7 +147,8 @@ TESTING_TEST(der_rsa_public) test_der_public (key); } -TESTING_TEST(der_dsa_public) +static void +test_der_dsa_public (Test *test, gconstpointer unused) { gcry_sexp_t key; gcry_error_t gcry; @@ -180,7 +183,8 @@ test_der_private (gcry_sexp_t key) egg_secure_free (data); } -TESTING_TEST(der_rsa_private) +static void +test_der_rsa_private (Test *test, gconstpointer unused) { gcry_sexp_t key; gcry_error_t gcry; @@ -191,7 +195,8 @@ TESTING_TEST(der_rsa_private) test_der_private (key); } -TESTING_TEST(der_dsa_private) +static void +test_der_dsa_private (Test *test, gconstpointer unused) { gcry_sexp_t key; gcry_error_t gcry; @@ -202,7 +207,8 @@ TESTING_TEST(der_dsa_private) test_der_private (key); } -TESTING_TEST(der_dsa_private_parts) +static void +test_der_dsa_private_parts (Test *test, gconstpointer unused) { guchar *params, *key; gsize n_params, n_key; @@ -235,7 +241,8 @@ const gchar *certpub = "(public-key (rsa " \ "(n #00C966D9F80744CFB98C2EF0A1EF13456C05DFDE2716513641116C6C3BEDFE107D129EE59B429AFE6031C366B7733A48AE4ED032379488B50DB6D9F3F244D9D58812DD764DF21AFC6F231E7AF1D898454E0710EF1642D043756D4ADEE2AAC931FF1F00707C66CF102508BAFAEE00E94603662711153BAA5BF298DD3642B2DA8875#) " \ "(e #010001#) ) )"; -TESTING_TEST(read_public_key_info) +static void +test_read_public_key_info (Test *test, gconstpointer unused) { const guchar *data; guchar hash[20]; @@ -244,7 +251,7 @@ TESTING_TEST(read_public_key_info) gcry_sexp_t sexp, match; gcry_error_t gcry; - data = egg_asn1x_get_raw_element (egg_asn1x_node (certificate, "tbsCertificate", "subjectPublicKeyInfo", NULL), &n_data); + data = egg_asn1x_get_raw_element (egg_asn1x_node (test->certificate, "tbsCertificate", "subjectPublicKeyInfo", NULL), &n_data); g_assert (data); res = gkm_data_der_read_public_key_info (data, n_data, &sexp); @@ -263,27 +270,29 @@ TESTING_TEST(read_public_key_info) gcry_sexp_release (match); } -TESTING_TEST(read_certificate) +static void +test_read_certificate (Test *test, gconstpointer unused) { GNode *asn = NULL; GkmDataResult res; - res = gkm_data_der_read_certificate (certificate_data, n_certificate_data, &asn); + res = gkm_data_der_read_certificate (test->certificate_data, test->n_certificate_data, &asn); g_assert (res == GKM_DATA_SUCCESS); g_assert (asn != NULL); egg_asn1x_destroy (asn); } -TESTING_TEST(write_certificate) +static void +test_write_certificate (Test *test, gconstpointer unused) { guchar *data; gsize n_data; - data = gkm_data_der_write_certificate (certificate, &n_data); + data = gkm_data_der_write_certificate (test->certificate, &n_data); g_assert (data); - g_assert (n_data == n_certificate_data); - g_assert (memcmp (data, certificate_data, n_data) == 0); + g_assert (n_data == test->n_certificate_data); + g_assert (memcmp (data, test->certificate_data, n_data) == 0); g_free (data); } @@ -303,7 +312,7 @@ on_ca_certificate_public_key_info (GQuark type, const guchar *data, gsize n_data res = gkm_data_der_read_certificate (data, n_data, &asn1); g_assert (res == GKM_DATA_SUCCESS); - /* Generate a raw public key from our certificate */ + /* Generate a raw public key from our test->certificate */ keydata = egg_asn1x_encode (egg_asn1x_node (asn1, "tbsCertificate", "subjectPublicKeyInfo", NULL), NULL, &n_keydata); g_assert (keydata); @@ -316,18 +325,20 @@ on_ca_certificate_public_key_info (GQuark type, const guchar *data, gsize n_data g_free (keydata); } -TESTING_TEST(read_ca_certificates_public_key_info) +static void +test_read_ca_certificates_public_key_info (Test *test, gconstpointer unused) { - gpointer data; + gchar *data; gsize n_data; - data = testing_data_read ("ca-certificates.crt", &n_data); + if (!g_file_get_contents ("files/ca-certificates.crt", &data, &n_data, NULL)) + g_assert_not_reached (); egg_openssl_pem_parse (data, n_data, on_ca_certificate_public_key_info, NULL); g_free (data); } static const guchar* -find_extension (GNode *asn, const guchar *data, gsize n_data, const gchar *oid, gsize *n_extension) +find_extension (GNode *asn, gconstpointer data, gsize n_data, const gchar *oid, gsize *n_extension) { const guchar *value; GNode *node = NULL; @@ -361,7 +372,8 @@ find_extension (GNode *asn, const guchar *data, gsize n_data, const gchar *oid, g_assert_not_reached (); } -TESTING_TEST(read_basic_constraints) +static void +test_read_basic_constraints (Test *test, gconstpointer unused) { const guchar *extension; gsize n_extension; @@ -369,8 +381,8 @@ TESTING_TEST(read_basic_constraints) gint path_len; GkmDataResult res; - extension = egg_asn1x_get_raw_value (egg_asn1x_node (certificate, "tbsCertificate", "extensions", 1, "extnValue", NULL), - &n_extension); + extension = egg_asn1x_get_raw_value (egg_asn1x_node (test->certificate, "tbsCertificate", "extensions", 1, "extnValue", NULL), + &n_extension); g_assert (extension); res = gkm_data_der_read_basic_constraints (extension, n_extension, &is_ca, &path_len); @@ -379,14 +391,15 @@ TESTING_TEST(read_basic_constraints) g_assert (path_len == -1); } -TESTING_TEST(read_key_usage) +static void +test_read_key_usage (Test *test, gconstpointer unused) { const guchar *extension; gsize n_extension; gulong key_usage; GkmDataResult res; - extension = find_extension (certificate2, certificate2_data, n_certificate2_data, "2.5.29.15", &n_extension); + extension = find_extension (test->certificate2, test->certificate2_data, test->n_certificate2_data, "2.5.29.15", &n_extension); g_assert (extension); res = gkm_data_der_read_key_usage (extension, n_extension, &key_usage); @@ -394,14 +407,15 @@ TESTING_TEST(read_key_usage) g_assert_cmpuint (key_usage, ==, 0x01); } -TESTING_TEST(read_enhanced_usage) +static void +test_read_enhanced_usage (Test *test, gconstpointer unused) { const guchar *extension; gsize n_extension; GQuark *usages; GkmDataResult res; - extension = find_extension (certificate2, certificate2_data, n_certificate2_data, "2.5.29.37", &n_extension); + extension = find_extension (test->certificate2, test->certificate2_data, test->n_certificate2_data, "2.5.29.37", &n_extension); g_assert (extension); res = gkm_data_der_read_enhanced_usage (extension, n_extension, &usages); @@ -410,16 +424,18 @@ TESTING_TEST(read_enhanced_usage) g_free (usages); } -TESTING_TEST(read_all_pkcs8) +static void +test_read_all_pkcs8 (Test *test, gconstpointer unused) { gcry_sexp_t sexp; GkmDataResult res; GDir *dir; const gchar *name; - guchar *data; + gchar *data; gsize n_data; + gchar *path; - dir = g_dir_open (testing_data_directory (), 0, NULL); + dir = g_dir_open ("files", 0, NULL); g_assert (dir); for(;;) { @@ -430,7 +446,11 @@ TESTING_TEST(read_all_pkcs8) if (!g_pattern_match_simple ("der-pkcs8-*", name)) continue; - data = testing_data_read (name, &n_data); + path = g_build_filename ("files", name, NULL); + if (!g_file_get_contents (path, &data, &n_data, NULL)) + g_assert_not_reached (); + g_free (path); + res = gkm_data_der_read_private_pkcs8 (data, n_data, "booo", 4, &sexp); g_assert (res == GKM_DATA_SUCCESS); @@ -442,21 +462,25 @@ TESTING_TEST(read_all_pkcs8) g_dir_close (dir); } -TESTING_TEST(read_pkcs8_bad_password) +static void +test_read_pkcs8_bad_password (Test *test, gconstpointer unused) { gcry_sexp_t sexp; GkmDataResult res; - guchar *data; + gchar *data; gsize n_data; - data = testing_data_read ("der-pkcs8-encrypted-pkcs5.key", &n_data); + if (!g_file_get_contents ("files/der-pkcs8-encrypted-pkcs5.key", &data, &n_data, NULL)) + g_assert_not_reached (); + res = gkm_data_der_read_private_pkcs8 (data, n_data, "wrong password", 4, &sexp); g_assert (res == GKM_DATA_LOCKED); g_free (data); } -TESTING_TEST(write_pkcs8_plain) +static void +test_write_pkcs8_plain (Test *test, gconstpointer unused) { gcry_sexp_t sexp, check; gcry_error_t gcry; @@ -503,7 +527,8 @@ TESTING_TEST(write_pkcs8_plain) } -TESTING_TEST(write_pkcs8_encrypted) +static void +test_write_pkcs8_encrypted (Test *test, gconstpointer unused) { gcry_sexp_t sexp, check; gcry_error_t gcry; @@ -548,3 +573,29 @@ TESTING_TEST(write_pkcs8_encrypted) gcry_sexp_release (sexp); gcry_sexp_release (check); } + +int +main (int argc, char **argv) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + + g_test_add ("/gkm/data-der/der_rsa_public", Test, NULL, setup, test_der_rsa_public, teardown); + g_test_add ("/gkm/data-der/der_dsa_public", Test, NULL, setup, test_der_dsa_public, teardown); + g_test_add ("/gkm/data-der/der_rsa_private", Test, NULL, setup, test_der_rsa_private, teardown); + g_test_add ("/gkm/data-der/der_dsa_private", Test, NULL, setup, test_der_dsa_private, teardown); + g_test_add ("/gkm/data-der/der_dsa_private_parts", Test, NULL, setup, test_der_dsa_private_parts, teardown); + g_test_add ("/gkm/data-der/read_public_key_info", Test, NULL, setup, test_read_public_key_info, teardown); + g_test_add ("/gkm/data-der/read_certificate", Test, NULL, setup, test_read_certificate, teardown); + g_test_add ("/gkm/data-der/write_certificate", Test, NULL, setup, test_write_certificate, teardown); + g_test_add ("/gkm/data-der/read_ca_certificates_public_key_info", Test, NULL, setup, test_read_ca_certificates_public_key_info, teardown); + g_test_add ("/gkm/data-der/read_basic_constraints", Test, NULL, setup, test_read_basic_constraints, teardown); + g_test_add ("/gkm/data-der/read_key_usage", Test, NULL, setup, test_read_key_usage, teardown); + g_test_add ("/gkm/data-der/read_enhanced_usage", Test, NULL, setup, test_read_enhanced_usage, teardown); + g_test_add ("/gkm/data-der/read_all_pkcs8", Test, NULL, setup, test_read_all_pkcs8, teardown); + g_test_add ("/gkm/data-der/read_pkcs8_bad_password", Test, NULL, setup, test_read_pkcs8_bad_password, teardown); + g_test_add ("/gkm/data-der/write_pkcs8_plain", Test, NULL, setup, test_write_pkcs8_plain, teardown); + g_test_add ("/gkm/data-der/write_pkcs8_encrypted", Test, NULL, setup, test_write_pkcs8_encrypted, teardown); + + return g_test_run (); +} diff --git a/pkcs11/gkm/tests/test-file-tracker.c b/pkcs11/gkm/tests/test-file-tracker.c new file mode 100644 index 00000000..1e606e3d --- /dev/null +++ b/pkcs11/gkm/tests/test-file-tracker.c @@ -0,0 +1,244 @@ +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ +/* test-file-tracker.c: File tracker tests + + Copyright (C) 2007 Stefan Walter + + The Gnome Keyring Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The Gnome Keyring Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the Gnome Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. + + Author: Stef Walter <stef@memberwebs.com> +*/ + +#include "config.h" + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +#include "gkm/gkm-file-tracker.h" + +#include <glib/gstdio.h> + +#define DATA "test-data" +#define SUBDIR "test-subdir" +#define WILDCARD "*.woo?" + +typedef struct { + GkmFileTracker *the_tracker; + gchar *test_dir; + gchar *test_file; + guint n_files_added; + gchar* last_file_added; + guint n_files_changed; + gchar* last_file_changed; + guint n_files_removed; + gchar* last_file_removed; +} Test; + +static void +file_added (GkmFileTracker *tracker, const gchar *path, gpointer user_data) +{ + Test *test = user_data; + + g_assert ("should be a non-null path" && path != NULL); + + ++test->n_files_added; + g_free (test->last_file_added); + test->last_file_added = g_strdup (path); +} + +static void +file_changed (GkmFileTracker *tracker, const gchar *path, gpointer user_data) +{ + Test *test = user_data; + + g_assert ("should be a non-null path" && path != NULL); + + ++test->n_files_changed; + g_free (test->last_file_changed); + test->last_file_changed = g_strdup (path); +} + +static void +file_removed (GkmFileTracker *tracker, const gchar *path, gpointer user_data) +{ + Test *test = user_data; + + g_assert ("should be a non-null path" && path != NULL); + + ++test->n_files_removed; + g_free (test->last_file_removed); + test->last_file_removed = g_strdup (path); +} + +static void +file_reset_stats (Test *test) +{ + g_free (test->last_file_removed); + g_free (test->last_file_added); + g_free (test->last_file_changed); + test->last_file_removed = test->last_file_added = test->last_file_changed = NULL; + test->n_files_added = test->n_files_changed = test->n_files_removed = 0; +} + +static void +setup (Test *test, gconstpointer unused) +{ + /* Make a test directory */ + test->test_dir = g_build_filename ("/tmp", SUBDIR, NULL); + + test->the_tracker = gkm_file_tracker_new (test->test_dir, WILDCARD, NULL); + g_signal_connect (test->the_tracker, "file-added", G_CALLBACK (file_added), test); + g_signal_connect (test->the_tracker, "file-removed", G_CALLBACK (file_removed), test); + g_signal_connect (test->the_tracker, "file-changed", G_CALLBACK (file_changed), test); + + /* Mtime must change so wait between tests */ + sleep (1); + + test->test_file = g_build_filename (test->test_dir, "my-file.woof", NULL); + g_unlink (test->test_file); +} + +static void +teardown (Test *test, gconstpointer unused) +{ + file_reset_stats (test); + g_object_unref (test->the_tracker); + g_free (test->test_dir); + g_free (test->test_file); +} + +static void +test_file_watch (Test *test, gconstpointer unused) +{ + /* A watch for an non-existant directory, should have no responses */ + gkm_file_tracker_refresh (test->the_tracker, FALSE); + + g_assert_cmpint (0, ==, test->n_files_added); + g_assert_cmpint (0, ==, test->n_files_changed); + g_assert_cmpint (0, ==, test->n_files_removed); + + g_mkdir_with_parents (test->test_dir, 0700); + + /* Should still have no responses even though it exists */ + gkm_file_tracker_refresh (test->the_tracker, FALSE); + + g_assert_cmpint (0, ==, test->n_files_added); + g_assert_cmpint (0, ==, test->n_files_changed); + g_assert_cmpint (0, ==, test->n_files_removed); +} + +static void +test_watch_file (Test *test, gconstpointer unused) +{ + gboolean ret; + + /* Make sure things are clean */ + gkm_file_tracker_refresh (test->the_tracker, FALSE); + + test->n_files_added = test->n_files_changed = test->n_files_removed = 0; + test->last_file_added = test->last_file_changed = test->last_file_removed = 0; + + ret = g_file_set_contents (test->test_file, DATA, strlen (DATA), NULL); + g_assert (ret); + + /* Now make sure that file is located */ + gkm_file_tracker_refresh (test->the_tracker, FALSE); + + g_assert_cmpint (1, ==, test->n_files_added); + g_assert_cmpint (0, ==, test->n_files_changed); + g_assert_cmpint (0, ==, test->n_files_removed); + + /* The added one should match our file */ + g_assert_cmpstr (test->last_file_added, ==, test->test_file); + + file_reset_stats (test); + sleep (1); + + /* Shouldn't find the file again */ + gkm_file_tracker_refresh (test->the_tracker, FALSE); + g_assert_cmpint (0, ==, test->n_files_added); + g_assert_cmpint (0, ==, test->n_files_changed); + g_assert_cmpint (0, ==, test->n_files_removed); + + /* But we should find the file if forced to */ + gkm_file_tracker_refresh (test->the_tracker, TRUE); + g_assert_cmpint (0, ==, test->n_files_added); + g_assert_cmpint (1, ==, test->n_files_changed); + g_assert_cmpint (0, ==, test->n_files_removed); + g_assert_cmpstr (test->last_file_changed, ==, test->test_file); + + file_reset_stats (test); + + ret = g_file_set_contents (test->test_file, DATA, strlen (DATA), NULL); + g_assert (ret); + + /* File was updated */ + gkm_file_tracker_refresh (test->the_tracker, FALSE); + g_assert_cmpint (0, ==, test->n_files_added); + g_assert_cmpint (1, ==, test->n_files_changed); + g_assert_cmpint (0, ==, test->n_files_removed); + g_assert_cmpstr (test->last_file_changed, ==, test->test_file); + + file_reset_stats (test); + g_unlink (test->test_file); + + /* Now file should be removed */ + gkm_file_tracker_refresh (test->the_tracker, FALSE); + + g_assert_cmpint (0, ==, test->n_files_added); + g_assert_cmpint (0, ==, test->n_files_changed); + g_assert_cmpint (1, ==, test->n_files_removed); + g_assert_cmpstr (test->last_file_removed, ==, test->test_file); +} + +static void +test_nomatch (Test *test, gconstpointer unused) +{ + gchar *file = g_build_filename (test->test_dir, "my-file.toot", NULL); + gboolean ret; + + /* Mtime must change so wait between tests */ + sleep (1); + + ret = g_file_set_contents (file, DATA, strlen (DATA), NULL); + g_assert (ret); + + file_reset_stats (test); + + /* Now make sure that file is not located */ + gkm_file_tracker_refresh (test->the_tracker, FALSE); + + g_assert_cmpint (0, ==, test->n_files_added); + g_assert_cmpint (0, ==, test->n_files_changed); + g_assert_cmpint (0, ==, test->n_files_removed); + + g_unlink (file); + g_free (file); +} + +int +main (int argc, char **argv) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + + g_test_add ("/gkm/file-tracker/file_watch", Test, NULL, setup, test_file_watch, teardown); + g_test_add ("/gkm/file-tracker/watch_file", Test, NULL, setup, test_watch_file, teardown); + g_test_add ("/gkm/file-tracker/nomatch", Test, NULL, setup, test_nomatch, teardown); + + return g_test_run (); +} diff --git a/pkcs11/gkm/tests/test-memory-store.c b/pkcs11/gkm/tests/test-memory-store.c new file mode 100644 index 00000000..07c6b0ec --- /dev/null +++ b/pkcs11/gkm/tests/test-memory-store.c @@ -0,0 +1,482 @@ +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ +/* unit-test-memory-test->store.c: Test memory test->store functionality + + Copyright (C) 2008 Stefan Walter + + The Gnome Keyring Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The Gnome Keyring Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the Gnome Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. + + Author: Stef Walter <stef@memberwebs.com> +*/ + +#include "config.h" + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +#include "mock-module.h" + +#include "egg/egg-secure-memory.h" +#include "gkm/gkm-object.h" +#include "gkm/gkm-memory-store.h" +#include "gkm/gkm-transaction.h" + +typedef struct { + GkmModule *module; + GkmStore *store; + GkmObject *object; + GkmTransaction *transaction; + guchar buffer[1024]; +} Test; + +static CK_RV +check_validator (GkmObject *obj, CK_ATTRIBUTE_PTR attr) +{ + const gchar *data; + guint i; + + g_assert (attr); + g_assert (attr->type == CKA_LABEL); + + /* Test that the whole string is ascii and lower case */ + data = attr->pValue; + for (i = 0; i < attr->ulValueLen; ++i) { + if (!g_ascii_isprint(data[i]) || !g_ascii_islower (data[i])) + return CKR_ATTRIBUTE_VALUE_INVALID; + } + + return CKR_OK; +} + +static void +setup (Test *test, gconstpointer unused) +{ + CK_ATTRIBUTE attr; + CK_ULONG twentyfour = 24; + + test->module = mock_module_initialize_and_enter (); + + attr.type = CKA_LABEL; + attr.pValue = "label"; + attr.ulValueLen = 5; + + test->store = GKM_STORE (gkm_memory_store_new ()); + + gkm_store_register_schema (test->store, &attr, check_validator, 0); + g_assert (gkm_store_lookup_schema (test->store, CKA_LABEL, NULL)); + + attr.type = CKA_VALUE; + attr.pValue = NULL; + attr.ulValueLen = 0; + + gkm_store_register_schema (test->store, &attr, NULL, GKM_STORE_IS_SENSITIVE); + + attr.type = CKA_BITS_PER_PIXEL; + attr.pValue = &twentyfour; + attr.ulValueLen = sizeof (twentyfour); + + gkm_store_register_schema (test->store, &attr, NULL, GKM_STORE_IS_INTERNAL); + + test->object = g_object_new (GKM_TYPE_OBJECT, "module", test->module, NULL); + + test->transaction = gkm_transaction_new (); +} + +static void +teardown (Test *test, gconstpointer unused) +{ + g_object_unref (test->store); + test->store = NULL; + + g_object_unref (test->transaction); + test->transaction = NULL; + + if (test->object != NULL) + g_object_unref (test->object); + test->object = NULL; + + mock_module_leave_and_finalize (); + test->module = NULL; +} + +static void +test_get_attribute_default (Test *test, gconstpointer unused) +{ + CK_ATTRIBUTE attr; + CK_RV rv; + + attr.type = CKA_LABEL; + attr.pValue = NULL; + rv = gkm_store_get_attribute (test->store, test->object, &attr); + g_assert (rv == CKR_OK); + g_assert (attr.ulValueLen == 5); + attr.pValue = test->buffer; + rv = gkm_store_get_attribute (test->store, test->object, &attr); + g_assert (rv == CKR_OK); + g_assert (attr.ulValueLen == 5); + g_assert (memcmp (attr.pValue, "label", 5) == 0); +} + +static void +test_read_value_default (Test *test, gconstpointer unused) +{ + gconstpointer value; + gsize n_value; + + value = gkm_store_read_value (test->store, test->object, CKA_LABEL, &n_value); + g_assert (value); + g_assert (n_value == 5); + g_assert (memcmp (value, "label", 5) == 0); + + value = gkm_store_read_value (test->store, test->object, CKA_BITS_PER_PIXEL, &n_value); + g_assert (value); + g_assert (n_value == sizeof (CK_ULONG)); + g_assert (*((CK_ULONG_PTR)value) == 24); +} + +static void +test_read_string (Test *test, gconstpointer unused) +{ + gchar *str; + + str = gkm_store_read_string (test->store, test->object, CKA_LABEL); + g_assert_cmpstr (str, ==, "label"); + g_free (str); +} + +static void +test_get_invalid (Test *test, gconstpointer unused) +{ + CK_ATTRIBUTE attr; + CK_RV rv; + + attr.type = CKA_APPLICATION; + attr.pValue = NULL; + attr.ulValueLen = 0; + + rv = gkm_store_get_attribute (test->store, test->object, &attr); + g_assert (rv == CKR_ATTRIBUTE_TYPE_INVALID); +} + +static void +test_get_sensitive (Test *test, gconstpointer unused) +{ + CK_ATTRIBUTE attr; + CK_RV rv; + + attr.type = CKA_VALUE; + attr.pValue = NULL; + attr.ulValueLen = 0; + + rv = gkm_store_get_attribute (test->store, test->object, &attr); + g_assert (rv == CKR_ATTRIBUTE_SENSITIVE); +} + +static void +test_get_internal (Test *test, gconstpointer unused) +{ + CK_ATTRIBUTE attr; + CK_RV rv; + + attr.type = CKA_BITS_PER_PIXEL; + attr.pValue = NULL; + attr.ulValueLen = 0; + + rv = gkm_store_get_attribute (test->store, test->object, &attr); + g_assert (rv == CKR_ATTRIBUTE_TYPE_INVALID); +} + +static void +test_set_invalid (Test *test, gconstpointer unused) +{ + CK_ATTRIBUTE attr; + + attr.type = CKA_APPLICATION; + attr.pValue = "me"; + attr.ulValueLen = 2; + + gkm_store_set_attribute (test->store, test->transaction, test->object, &attr); + g_assert (gkm_transaction_get_result (test->transaction) == CKR_ATTRIBUTE_TYPE_INVALID); +} + +static void +test_set_internal (Test *test, gconstpointer unused) +{ + CK_ATTRIBUTE attr; + CK_ULONG five = 5; + + attr.type = CKA_BITS_PER_PIXEL; + attr.pValue = &five; + attr.ulValueLen = sizeof (five); + + gkm_store_set_attribute (test->store, test->transaction, test->object, &attr); + g_assert (gkm_transaction_get_result (test->transaction) == CKR_ATTRIBUTE_TYPE_INVALID); +} + +static void +test_set_get_attribute (Test *test, gconstpointer unused) +{ + CK_ATTRIBUTE attr; + CK_RV rv; + + attr.type = CKA_LABEL; + attr.pValue = "booyah"; + attr.ulValueLen = 6; + + gkm_store_set_attribute (test->store, test->transaction, test->object, &attr); + + gkm_transaction_complete (test->transaction); + g_assert (gkm_transaction_get_result (test->transaction) == CKR_OK); + + attr.pValue = test->buffer; + attr.ulValueLen = 1024; + rv = gkm_store_get_attribute (test->store, test->object, &attr); + g_assert (rv == CKR_OK); + g_assert (attr.ulValueLen == 6); + g_assert (memcmp (attr.pValue, "booyah", 6) == 0); +} + +static void +test_write_read_value (Test *test, gconstpointer unused) +{ + CK_ATTRIBUTE attr; + CK_ULONG five = 5; + gconstpointer value; + gsize n_value; + + attr.type = CKA_BITS_PER_PIXEL; + attr.pValue = &five; + attr.ulValueLen = sizeof (five); + + gkm_store_write_value (test->store, test->transaction, test->object, &attr); + + gkm_transaction_complete (test->transaction); + g_assert (gkm_transaction_get_result (test->transaction) == CKR_OK); + + value = gkm_store_read_value (test->store, test->object, CKA_BITS_PER_PIXEL, &n_value); + g_assert (value); + g_assert (n_value == sizeof (five)); + g_assert (memcmp (value, &five, sizeof (five)) == 0); +} + +static void +test_set_no_validate (Test *test, gconstpointer unused) +{ + CK_ATTRIBUTE attr; + + attr.type = CKA_LABEL; + attr.pValue = "CAPITALS"; + attr.ulValueLen = 8; + + gkm_store_set_attribute (test->store, test->transaction, test->object, &attr); + g_assert (gkm_transaction_get_failed (test->transaction)); + + gkm_transaction_complete (test->transaction); + g_assert (gkm_transaction_get_result (test->transaction) == CKR_ATTRIBUTE_VALUE_INVALID); +} + +static void +test_set_transaction_default (Test *test, gconstpointer unused) +{ + CK_ATTRIBUTE attr; + gconstpointer value; + gsize n_value; + + + attr.type = CKA_LABEL; + attr.pValue = "another"; + attr.ulValueLen = 7; + + /* Change the attribute */ + gkm_store_set_attribute (test->store, test->transaction, test->object, &attr); + g_assert (gkm_transaction_get_failed (test->transaction) == FALSE); + + /* Should get new value */ + value = gkm_store_read_value (test->store, test->object, CKA_LABEL, &n_value); + g_assert (value && n_value == attr.ulValueLen); + g_assert (memcmp (attr.pValue, value, n_value) == 0); + + /* Fail for some arbitrary reason */ + gkm_transaction_fail (test->transaction, CKR_ATTRIBUTE_VALUE_INVALID); + + /* Value should not have changed yet */ + value = gkm_store_read_value (test->store, test->object, CKA_LABEL, &n_value); + g_assert (value && n_value == attr.ulValueLen); + g_assert (memcmp (attr.pValue, value, n_value) == 0); + + /* Now complete the test->transaction */ + gkm_transaction_complete (test->transaction); + g_assert (gkm_transaction_get_failed (test->transaction) == TRUE); + + /* Value should now have changed, back to default */ + value = gkm_store_read_value (test->store, test->object, CKA_LABEL, &n_value); + g_assert (value && n_value == 5); + g_assert (memcmp (value, "label", 5) == 0); +} + +static void +test_set_transaction_revert_first (Test *test, gconstpointer unused) +{ + CK_ATTRIBUTE attr, prev; + gconstpointer value; + gsize n_value; + + prev.type = CKA_LABEL; + prev.pValue = "numberone"; + prev.ulValueLen = 9; + + /* Change the attribute */ + gkm_store_set_attribute (test->store, test->transaction, test->object, &prev); + gkm_transaction_complete (test->transaction); + g_assert (gkm_transaction_get_failed (test->transaction) == FALSE); + + /* Value should be new value */ + value = gkm_store_read_value (test->store, test->object, CKA_LABEL, &n_value); + g_assert (value && n_value == prev.ulValueLen); + g_assert (memcmp (prev.pValue, value, n_value) == 0); + + /* A new test->transaction */ + g_object_unref (test->transaction); + test->transaction = gkm_transaction_new (); + + attr.type = CKA_LABEL; + attr.pValue = "second"; + attr.ulValueLen = 6; + + gkm_store_set_attribute (test->store, test->transaction, test->object, &attr); + g_assert (gkm_transaction_get_failed (test->transaction) == FALSE); + + /* Should get new value */ + value = gkm_store_read_value (test->store, test->object, CKA_LABEL, &n_value); + g_assert (value && n_value == attr.ulValueLen); + g_assert (memcmp (attr.pValue, value, n_value) == 0); + + attr.type = CKA_LABEL; + attr.pValue = "third"; + attr.ulValueLen = 5; + + gkm_store_set_attribute (test->store, test->transaction, test->object, &attr); + g_assert (gkm_transaction_get_failed (test->transaction) == FALSE); + + /* Should get new value */ + value = gkm_store_read_value (test->store, test->object, CKA_LABEL, &n_value); + g_assert (value && n_value == attr.ulValueLen); + g_assert (memcmp (attr.pValue, value, n_value) == 0); + + /* Fail for some arbitrary reason */ + gkm_transaction_fail (test->transaction, CKR_ATTRIBUTE_VALUE_INVALID); + + /* Value should not have changed yet */ + value = gkm_store_read_value (test->store, test->object, CKA_LABEL, &n_value); + g_assert (value && n_value == attr.ulValueLen); + g_assert (memcmp (attr.pValue, value, n_value) == 0); + + /* Now complete the test->transaction */ + gkm_transaction_complete (test->transaction); + g_assert (gkm_transaction_get_failed (test->transaction) == TRUE); + + /* Value should now have changed, back to default */ + value = gkm_store_read_value (test->store, test->object, CKA_LABEL, &n_value); + g_assert (value && n_value == prev.ulValueLen); + g_assert (memcmp (prev.pValue, value, n_value) == 0); +} + +static void +notify_attribute (GkmObject *obj, CK_ATTRIBUTE_TYPE type, gpointer data) +{ + g_assert (type == CKA_LABEL); + g_assert (data); + + *((CK_ATTRIBUTE_TYPE*)data) = type; +} + +static void +test_set_notifies (Test *test, gconstpointer unused) +{ + CK_ATTRIBUTE attr; + CK_ATTRIBUTE_TYPE type = 0; + + attr.type = CKA_LABEL; + attr.pValue = "valid"; + attr.ulValueLen = 5; + + g_signal_connect (test->object, "notify-attribute", G_CALLBACK (notify_attribute), &type); + + gkm_store_set_attribute (test->store, test->transaction, test->object, &attr); + + /* We should have been notified that the attribute changed at this point */ + g_assert (type == CKA_LABEL); + + /* Reset for next notify */ + type = 0; + + /* Fail for some arbitrary reason */ + gkm_transaction_fail (test->transaction, CKR_ATTRIBUTE_VALUE_INVALID); + + /* We should not have been notified yet */ + g_assert (type == 0); + + /* Now complete the test->transaction */ + gkm_transaction_complete (test->transaction); + g_assert (gkm_transaction_get_failed (test->transaction) == TRUE); + + /* Now we should have been notified that this changed back */ + g_assert (type == CKA_LABEL); +} + +static void +test_set_object_gone_first (Test *test, gconstpointer unused) +{ + CK_ATTRIBUTE attr; + + attr.type = CKA_LABEL; + attr.pValue = "valid"; + attr.ulValueLen = 5; + + gkm_store_set_attribute (test->store, test->transaction, test->object, &attr); + gkm_transaction_complete (test->transaction); + g_assert (gkm_transaction_get_result (test->transaction) == CKR_OK); + + /* This tests memory test->store internal tracking */ + g_object_unref (test->object); + test->object = NULL; +} + +int +main (int argc, char **argv) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + + g_test_add ("/gkm/memory-test->store/get_attribute_default", Test, NULL, setup, test_get_attribute_default, teardown); + g_test_add ("/gkm/memory-test->store/read_value_default", Test, NULL, setup, test_read_value_default, teardown); + g_test_add ("/gkm/memory-test->store/read_string", Test, NULL, setup, test_read_string, teardown); + g_test_add ("/gkm/memory-test->store/get_invalid", Test, NULL, setup, test_get_invalid, teardown); + g_test_add ("/gkm/memory-test->store/get_sensitive", Test, NULL, setup, test_get_sensitive, teardown); + g_test_add ("/gkm/memory-test->store/get_internal", Test, NULL, setup, test_get_internal, teardown); + g_test_add ("/gkm/memory-test->store/set_invalid", Test, NULL, setup, test_set_invalid, teardown); + g_test_add ("/gkm/memory-test->store/set_internal", Test, NULL, setup, test_set_internal, teardown); + g_test_add ("/gkm/memory-test->store/set_get_attribute", Test, NULL, setup, test_set_get_attribute, teardown); + g_test_add ("/gkm/memory-test->store/write_read_value", Test, NULL, setup, test_write_read_value, teardown); + g_test_add ("/gkm/memory-test->store/set_no_validate", Test, NULL, setup, test_set_no_validate, teardown); + g_test_add ("/gkm/memory-test->store/set_transaction_default", Test, NULL, setup, test_set_transaction_default, teardown); + g_test_add ("/gkm/memory-test->store/set_transaction_revert_first", Test, NULL, setup, test_set_transaction_revert_first, teardown); + g_test_add ("/gkm/memory-test->store/set_notifies", Test, NULL, setup, test_set_notifies, teardown); + g_test_add ("/gkm/memory-test->store/set_object_gone_first", Test, NULL, setup, test_set_object_gone_first, teardown); + + return g_test_run (); +} diff --git a/pkcs11/gkm/tests/unit-test-object.c b/pkcs11/gkm/tests/test-object.c index fc2ce4f7..0c3c826c 100644 --- a/pkcs11/gkm/tests/unit-test-object.c +++ b/pkcs11/gkm/tests/test-object.c @@ -1,5 +1,5 @@ /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* unit-test-timer.c: Test thread timer functionality +/* test-object.c: Test GkmObject Copyright (C) 2009 Stefan Walter @@ -21,8 +21,9 @@ Author: Stef Walter <stef@memberwebs.com> */ -#include "test-suite.h" -#include "test-module.h" +#include "config.h" + +#include "mock-module.h" #include "gkm/gkm-attributes.h" #include "gkm/gkm-object.h" @@ -30,39 +31,42 @@ #include "gkm/gkm-module.h" #include "gkm/gkm-transaction.h" +#include "egg/egg-testing.h" + #include "pkcs11i.h" -static GkmModule *module = NULL; -static GkmSession *session = NULL; -static guchar *certificate_data = NULL; -static gsize certificate_n_data = 0; +typedef struct { + GkmModule *module; + GkmSession *session; + gchar *certificate_data; + gsize n_certificate_data; +} Test; -TESTING_SETUP(object_setup) +static void +setup (Test* test, gconstpointer unused) { - module = test_module_initialize_and_enter (); - session = test_module_open_session (TRUE); - certificate_data = testing_data_read ("test-certificate-1.der", &certificate_n_data); + test->module = mock_module_initialize_and_enter (); + test->session = mock_module_open_session (TRUE); + + if (!g_file_get_contents ("files/test-certificate-1.der", &test->certificate_data, &test->n_certificate_data, NULL)) + g_assert_not_reached (); } -TESTING_TEARDOWN(object_teardown) +static void +teardown (Test* test, gconstpointer unused) { - g_free (certificate_data); - certificate_data = NULL; - certificate_n_data = 0; - - test_module_leave_and_finalize (); - module = NULL; - session = NULL; + g_free (test->certificate_data); + mock_module_leave_and_finalize (); } static gboolean -check_object_exists (CK_OBJECT_HANDLE handle) +check_object_exists (CK_OBJECT_HANDLE handle, Test *test) { CK_BBOOL token; CK_ATTRIBUTE attr = { CKA_TOKEN, &token, sizeof (token) }; CK_RV rv; - rv = gkm_session_C_GetAttributeValue (session, handle, &attr, 1); + rv = gkm_session_C_GetAttributeValue (test->session, handle, &attr, 1); if (rv == CKR_OBJECT_HANDLE_INVALID) return FALSE; @@ -70,7 +74,8 @@ check_object_exists (CK_OBJECT_HANDLE handle) return TRUE; } -TESTING_TEST(object_create_destroy_transient) +static void +test_create_destroy_transient (Test* test, gconstpointer unused) { CK_BBOOL transient = CK_TRUE; CK_BBOOL token = CK_TRUE; @@ -82,31 +87,32 @@ TESTING_TEST(object_create_destroy_transient) { CKA_GNOME_TRANSIENT, &transient, sizeof (transient) }, { CKA_CLASS, &klass, sizeof (klass) }, { CKA_CERTIFICATE_TYPE, &type, sizeof (type) }, - { CKA_VALUE, certificate_data, certificate_n_data }, + { CKA_VALUE, test->certificate_data, test->n_certificate_data }, }; CK_ATTRIBUTE lookup = { CKA_GNOME_TRANSIENT, &transient, sizeof (transient) }; CK_OBJECT_HANDLE handle; CK_RV rv; - rv = gkm_session_C_CreateObject (session, attrs, G_N_ELEMENTS (attrs), &handle); + rv = gkm_session_C_CreateObject (test->session, attrs, G_N_ELEMENTS (attrs), &handle); g_assert (rv == CKR_OK); g_assert (handle != 0); - g_assert (check_object_exists (handle)); + g_assert (check_object_exists (handle, test)); transient = CK_FALSE; - rv = gkm_session_C_GetAttributeValue (session, handle, &lookup, 1); + rv = gkm_session_C_GetAttributeValue (test->session, handle, &lookup, 1); g_assert (rv == CKR_OK); g_assert (transient == CK_TRUE); - rv = gkm_session_C_DestroyObject (session, handle); + rv = gkm_session_C_DestroyObject (test->session, handle); g_assert (rv == CKR_OK); - g_assert (!check_object_exists (handle)); + g_assert (!check_object_exists (handle, test)); } -TESTING_TEST(object_transient_transacted_fail) +static void +test_transient_transacted_fail (Test* test, gconstpointer unused) { CK_BBOOL transient = CK_TRUE; CK_BBOOL token = CK_TRUE; @@ -119,7 +125,7 @@ TESTING_TEST(object_transient_transacted_fail) { CKA_GNOME_TRANSIENT, &transient, sizeof (transient) }, { CKA_CLASS, &klass, sizeof (klass) }, { CKA_CERTIFICATE_TYPE, &type, sizeof (type) }, - { CKA_VALUE, certificate_data, certificate_n_data }, + { CKA_VALUE, test->certificate_data, test->n_certificate_data }, /* An invalid attribute, should cause transaction to fail */ { CKA_BITS_PER_PIXEL, &invalid, sizeof (invalid) } @@ -128,11 +134,12 @@ TESTING_TEST(object_transient_transacted_fail) CK_OBJECT_HANDLE handle; CK_RV rv; - rv = gkm_session_C_CreateObject (session, attrs, G_N_ELEMENTS (attrs), &handle); + rv = gkm_session_C_CreateObject (test->session, attrs, G_N_ELEMENTS (attrs), &handle); g_assert (rv == CKR_ATTRIBUTE_TYPE_INVALID); } -TESTING_TEST(object_create_transient_bad_value) +static void +test_create_transient_bad_value (Test* test, gconstpointer unused) { CK_OBJECT_CLASS klass = CKO_CERTIFICATE; CK_CERTIFICATE_TYPE type = CKC_X_509; @@ -141,18 +148,19 @@ TESTING_TEST(object_create_transient_bad_value) { CKA_GNOME_TRANSIENT, NULL, 0 }, { CKA_CLASS, &klass, sizeof (klass) }, { CKA_CERTIFICATE_TYPE, &type, sizeof (type) }, - { CKA_VALUE, certificate_data, certificate_n_data }, + { CKA_VALUE, test->certificate_data, test->n_certificate_data }, }; CK_OBJECT_HANDLE handle; CK_RV rv; /* Can't have a non-transient object that auto-destructs */ - rv = gkm_session_C_CreateObject (session, attrs, G_N_ELEMENTS (attrs), &handle); + rv = gkm_session_C_CreateObject (test->session, attrs, G_N_ELEMENTS (attrs), &handle); g_assert (rv == CKR_ATTRIBUTE_VALUE_INVALID); } -TESTING_TEST(object_create_auto_destruct) +static void +test_create_auto_destruct (Test* test, gconstpointer unused) { CK_BBOOL token = CK_FALSE; CK_OBJECT_CLASS klass = CKO_CERTIFICATE; @@ -165,7 +173,7 @@ TESTING_TEST(object_create_auto_destruct) { CKA_TOKEN, &token, sizeof (token) }, { CKA_CLASS, &klass, sizeof (klass) }, { CKA_CERTIFICATE_TYPE, &type, sizeof (type) }, - { CKA_VALUE, certificate_data, certificate_n_data }, + { CKA_VALUE, test->certificate_data, test->n_certificate_data }, }; CK_BBOOL transient; @@ -178,26 +186,27 @@ TESTING_TEST(object_create_auto_destruct) CK_OBJECT_HANDLE handle; CK_RV rv; - rv = gkm_session_C_CreateObject (session, attrs, G_N_ELEMENTS (attrs), &handle); + rv = gkm_session_C_CreateObject (test->session, attrs, G_N_ELEMENTS (attrs), &handle); g_assert (rv == CKR_OK); g_assert (handle != 0); - g_assert (check_object_exists (handle)); + g_assert (check_object_exists (handle, test)); transient = CK_FALSE; - rv = gkm_session_C_GetAttributeValue (session, handle, lookups, G_N_ELEMENTS (lookups)); + rv = gkm_session_C_GetAttributeValue (test->session, handle, lookups, G_N_ELEMENTS (lookups)); g_assert (rv == CKR_OK); g_assert (transient == TRUE); g_assert (memcmp (&lifetime, &check, sizeof (lifetime)) == 0); - test_module_leave (); - testing_wait_until (2200); - test_module_enter (); + mock_module_leave (); + egg_test_wait_until (2200); + mock_module_enter (); - g_assert (!check_object_exists (handle)); + g_assert (!check_object_exists (handle, test)); } -TESTING_TEST(object_create_auto_destruct_not_transient) +static void +test_create_auto_destruct_not_transient (Test* test, gconstpointer unused) { CK_OBJECT_CLASS klass = CKO_CERTIFICATE; CK_CERTIFICATE_TYPE type = CKC_X_509; @@ -209,25 +218,26 @@ TESTING_TEST(object_create_auto_destruct_not_transient) { CKA_GNOME_TRANSIENT, &transient, sizeof (transient) }, { CKA_CLASS, &klass, sizeof (klass) }, { CKA_CERTIFICATE_TYPE, &type, sizeof (type) }, - { CKA_VALUE, certificate_data, certificate_n_data }, + { CKA_VALUE, test->certificate_data, test->n_certificate_data }, }; CK_OBJECT_HANDLE handle; CK_RV rv; /* Can't have a non-transient object that auto-destructs */ - rv = gkm_session_C_CreateObject (session, attrs, G_N_ELEMENTS (attrs), &handle); + rv = gkm_session_C_CreateObject (test->session, attrs, G_N_ELEMENTS (attrs), &handle); g_assert (rv == CKR_TEMPLATE_INCONSISTENT); } -TESTING_TEST(object_expose) +static void +test_expose (Test* test, gconstpointer unused) { CK_OBJECT_HANDLE handle; GkmManager *manager; GkmObject *check, *object; - manager = gkm_session_get_manager (session); - object = test_module_object_new (session); + manager = gkm_session_get_manager (test->session); + object = mock_module_object_new (test->session); handle = gkm_object_get_handle (object); gkm_object_expose (object, TRUE); @@ -243,15 +253,16 @@ TESTING_TEST(object_expose) g_assert (check == NULL); } -TESTING_TEST(object_expose_transaction) +static void +test_expose_transaction (Test* test, gconstpointer unused) { CK_OBJECT_HANDLE handle; GkmManager *manager; GkmObject *check, *object; GkmTransaction *transaction; - manager = gkm_session_get_manager (session); - object = test_module_object_new (session); + manager = gkm_session_get_manager (test->session); + object = mock_module_object_new (test->session); handle = gkm_object_get_handle (object); transaction = gkm_transaction_new (); @@ -275,3 +286,20 @@ TESTING_TEST(object_expose_transaction) g_object_unref (transaction); } + +int +main (int argc, char **argv) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + + g_test_add ("/gkm/object/create_destroy_transient", Test, NULL, setup, test_create_destroy_transient, teardown); + g_test_add ("/gkm/object/transient_transacted_fail", Test, NULL, setup, test_transient_transacted_fail, teardown); + g_test_add ("/gkm/object/create_transient_bad_value", Test, NULL, setup, test_create_transient_bad_value, teardown); + g_test_add ("/gkm/object/create_auto_destruct", Test, NULL, setup, test_create_auto_destruct, teardown); + g_test_add ("/gkm/object/create_auto_destruct_not_transient", Test, NULL, setup, test_create_auto_destruct_not_transient, teardown); + g_test_add ("/gkm/object/expose", Test, NULL, setup, test_expose, teardown); + g_test_add ("/gkm/object/expose_transaction", Test, NULL, setup, test_expose_transaction, teardown); + + return egg_tests_run_in_thread_with_loop (); +} diff --git a/pkcs11/gkm/tests/unit-test-secret.c b/pkcs11/gkm/tests/test-secret.c index 5dcea3c6..67e7a629 100644 --- a/pkcs11/gkm/tests/unit-test-secret.c +++ b/pkcs11/gkm/tests/test-secret.c @@ -1,5 +1,5 @@ /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* unit-test-secret.c: Test gkm-secret.c +/* test-secret.c: Test gkm-secret.c Copyright (C) 2007 Stefan Walter @@ -21,15 +21,20 @@ Author: Stef Walter <stef@memberwebs.com> */ +#include "config.h" + #include <stdlib.h> #include <stdio.h> #include <string.h> -#include "test-suite.h" - #include "gkm/gkm-secret.h" -TESTING_TEST(test_secret) +#include "egg/egg-secure-memory.h" + +EGG_SECURE_GLIB_DEFINITIONS (); + +static void +test_secret (void) { GkmSecret *secret; const gchar *password; @@ -52,7 +57,8 @@ TESTING_TEST(test_secret) g_object_unref (secret); } -TESTING_TEST(test_secret_from_login) +static void +test_secret_from_login (void) { GkmSecret *secret; const gchar *password; @@ -75,7 +81,8 @@ TESTING_TEST(test_secret_from_login) g_object_unref (secret); } -TESTING_TEST(test_null_terminated) +static void +test_null_terminated (void) { GkmSecret *secret; const gchar *password; @@ -98,7 +105,8 @@ TESTING_TEST(test_null_terminated) g_object_unref (secret); } -TESTING_TEST(test_always_has_null) +static void +test_always_has_null (void) { GkmSecret *secret; const guchar *memory; @@ -117,7 +125,8 @@ TESTING_TEST(test_always_has_null) g_object_unref (secret); } -TESTING_TEST(test_null) +static void +test_null (void) { GkmSecret *secret; const gchar *password; @@ -139,7 +148,8 @@ TESTING_TEST(test_null) g_object_unref (secret); } -TESTING_TEST(test_empty) +static void +test_empty (void) { GkmSecret *secret; const gchar *password; @@ -161,7 +171,8 @@ TESTING_TEST(test_empty) g_object_unref (secret); } -TESTING_TEST(test_equal) +static void +test_equal (void) { GkmSecret *one; GkmSecret *two; @@ -176,3 +187,20 @@ TESTING_TEST(test_equal) one = gkm_secret_new_from_password ("other"); g_assert (!gkm_secret_equal (one, two)); } + +int +main (int argc, char **argv) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + + g_test_add_func ("/gkm/secret/secret", test_secret); + g_test_add_func ("/gkm/secret/secret_from_login", test_secret_from_login); + g_test_add_func ("/gkm/secret/null_terminated", test_null_terminated); + g_test_add_func ("/gkm/secret/always_has_null", test_always_has_null); + g_test_add_func ("/gkm/secret/null", test_null); + g_test_add_func ("/gkm/secret/empty", test_empty); + g_test_add_func ("/gkm/secret/equal", test_equal); + + return g_test_run (); +} diff --git a/pkcs11/gkm/tests/unit-test-sexp.c b/pkcs11/gkm/tests/test-sexp.c index c4c25246..5f3e0625 100644 --- a/pkcs11/gkm/tests/unit-test-sexp.c +++ b/pkcs11/gkm/tests/test-sexp.c @@ -1,5 +1,5 @@ /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* unit-test-sexp.c: Test sexp stuff +/* test-sexp.c: Test sexp stuff Copyright (C) 2007 Stefan Walter @@ -21,17 +21,21 @@ Author: Stef Walter <stef@memberwebs.com> */ +#include "config.h" + #include <stdlib.h> #include <stdio.h> #include <string.h> -#include "test-suite.h" - #include "gkm/gkm-crypto.h" #include "gkm/gkm-sexp.h" +#include "egg/egg-secure-memory.h" + #include <gcrypt.h> +EGG_SECURE_GLIB_DEFINITIONS (); + #define TEST_RSA \ "(private-key (rsa " \ "(n #00B78758D55EBFFAB61D07D0DC49B5309A6F1DA2AE51C275DFC2370959BB81AC0C39093B1C618E396161A0DECEB8768D0FFB14F197B96C3DA14190EE0F20D51315#)" \ @@ -50,30 +54,35 @@ " (y #54734451DB79D4EEDF0BBCEBD43BB6CBB7B8584603B957080075DD318EB5B0266D4B20DC5EFF376BDFC4EA2983B1F7F02A39ED4C619ED68712729FFF3B7C696ADD1B6D748F56A4B4BEC5C4385E528423A3B88AE65E6D5500F97839E7A486255982189C3B4FA8D94338C76F0E5CAFC9A30A1ED728BB9F2091D594E3250A09EA00#)" \ " (x #00876F84F709D51108DFB0CBFA1F1C569C09C413EC#)))" -gcry_sexp_t rsakey = NULL; -gcry_sexp_t dsakey = NULL; +typedef struct { + gcry_sexp_t rsakey; + gcry_sexp_t dsakey; +} Test; -TESTING_SETUP(crypto_setup) +static void +setup (Test *test, gconstpointer unused) { gcry_error_t gcry; gkm_crypto_initialize (); - gcry = gcry_sexp_new (&rsakey, TEST_RSA, strlen (TEST_RSA), 1); + gcry = gcry_sexp_new (&test->rsakey, TEST_RSA, strlen (TEST_RSA), 1); g_return_if_fail (gcry == 0); - gcry = gcry_sexp_new (&dsakey, TEST_DSA, strlen (TEST_DSA), 1); + gcry = gcry_sexp_new (&test->dsakey, TEST_DSA, strlen (TEST_DSA), 1); g_return_if_fail (gcry == 0); } -TESTING_TEARDOWN(crypto_setup) +static void +teardown (Test *test, gconstpointer unused) { - gcry_sexp_release (rsakey); - rsakey = NULL; - gcry_sexp_release (dsakey); - dsakey = NULL; + gcry_sexp_release (test->rsakey); + test->rsakey = NULL; + gcry_sexp_release (test->dsakey); + test->dsakey = NULL; } -TESTING_TEST(parse_key) +static void +test_parse_key (Test *test, gconstpointer unused) { gcry_sexp_t sexp = NULL; gcry_mpi_t mpi = NULL; @@ -82,18 +91,19 @@ TESTING_TEST(parse_key) int algorithm = 0; /* Get the private key out */ - ret = gkm_sexp_parse_key (rsakey, &algorithm, &is_priv, &sexp); + ret = gkm_sexp_parse_key (test->rsakey, &algorithm, &is_priv, &sexp); g_assert (ret); g_assert (algorithm == GCRY_PK_RSA); g_assert (is_priv == TRUE); g_assert (sexp != NULL); - ret = gkm_sexp_extract_mpi (rsakey, &mpi, "p", NULL); + ret = gkm_sexp_extract_mpi (test->rsakey, &mpi, "p", NULL); g_assert (ret); g_assert (mpi != NULL); } -TESTING_TEST(sexp_key_to_public) +static void +test_key_to_public (Test *test, gconstpointer unused) { gcry_sexp_t pubkey = NULL; guchar id1[20], id2[20]; @@ -101,11 +111,11 @@ TESTING_TEST(sexp_key_to_public) guchar *p; /* RSA */ - ret = gkm_sexp_key_to_public (rsakey, &pubkey); + ret = gkm_sexp_key_to_public (test->rsakey, &pubkey); g_assert (ret); g_assert (pubkey != NULL); - p = gcry_pk_get_keygrip (rsakey, id1); + p = gcry_pk_get_keygrip (test->rsakey, id1); g_return_if_fail (p == id1); p = gcry_pk_get_keygrip (pubkey, id2); g_return_if_fail (p == id2); @@ -116,11 +126,11 @@ TESTING_TEST(sexp_key_to_public) /* DSA */ - ret = gkm_sexp_key_to_public (dsakey, &pubkey); + ret = gkm_sexp_key_to_public (test->dsakey, &pubkey); g_assert (ret); g_assert (pubkey != NULL); - p = gcry_pk_get_keygrip (dsakey, id1); + p = gcry_pk_get_keygrip (test->dsakey, id1); g_return_if_fail (p == id1); p = gcry_pk_get_keygrip (pubkey, id2); g_return_if_fail (p == id2); @@ -130,3 +140,15 @@ TESTING_TEST(sexp_key_to_public) gcry_sexp_release (pubkey); } + +int +main (int argc, char **argv) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + + g_test_add ("/gkm/sexp/parse_key", Test, NULL, setup, test_parse_key, teardown); + g_test_add ("/gkm/sexp/key_to_public", Test, NULL, setup, test_key_to_public, teardown); + + return g_test_run (); +} diff --git a/pkcs11/gkm/tests/unit-test-store.c b/pkcs11/gkm/tests/test-store.c index 859aa02d..4dab4c40 100644 --- a/pkcs11/gkm/tests/unit-test-store.c +++ b/pkcs11/gkm/tests/test-store.c @@ -1,5 +1,5 @@ /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* unit-test-store.c: Test general store functionality +/* test-store.c: Test general store functionality Copyright (C) 2008 Stefan Walter @@ -21,35 +21,38 @@ Author: Stef Walter <stef@memberwebs.com> */ +#include "config.h" + #include <stdlib.h> #include <stdio.h> #include <string.h> #include <unistd.h> -#include "test-suite.h" -#include "test-module.h" +#include "mock-module.h" #include "gkm/gkm-store.h" -static GkmModule *module = NULL; -static GkmStore *store = NULL; +typedef struct { + GkmModule *module; + GkmStore *store; +} Test; -TESTING_SETUP(store) +static void +setup (Test *test, gconstpointer unused) { - module = test_module_initialize_and_enter (); - store = g_object_new (GKM_TYPE_STORE, NULL); + test->module = mock_module_initialize_and_enter (); + test->store = g_object_new (GKM_TYPE_STORE, NULL); } -TESTING_TEARDOWN(store) +static void +teardown (Test *test, gconstpointer unused) { - g_object_unref (store); - store = NULL; - - test_module_leave_and_finalize (); - module = NULL; + g_object_unref (test->store); + mock_module_leave_and_finalize (); } -TESTING_TEST(store_schema) +static void +test_schema (Test *test, gconstpointer unused) { CK_ATTRIBUTE attr; @@ -57,14 +60,15 @@ TESTING_TEST(store_schema) attr.pValue = "Label"; attr.ulValueLen = 5; - gkm_store_register_schema (store, &attr, NULL, 0); - g_assert (gkm_store_lookup_schema (store, CKA_LABEL, NULL)); + gkm_store_register_schema (test->store, &attr, NULL, 0); + g_assert (gkm_store_lookup_schema (test->store, CKA_LABEL, NULL)); /* Not in the schema */ - g_assert (!gkm_store_lookup_schema (store, CKA_VALUE, NULL)); + g_assert (!gkm_store_lookup_schema (test->store, CKA_VALUE, NULL)); } -TESTING_TEST(store_schema_flags) +static void +test_schema_flags (Test *test, gconstpointer unused) { CK_ATTRIBUTE attr; guint flags; @@ -73,8 +77,8 @@ TESTING_TEST(store_schema_flags) attr.pValue = NULL; attr.ulValueLen = 0; - gkm_store_register_schema (store, &attr, NULL, GKM_STORE_IS_SENSITIVE); - g_assert (gkm_store_lookup_schema (store, CKA_VALUE, &flags)); + gkm_store_register_schema (test->store, &attr, NULL, GKM_STORE_IS_SENSITIVE); + g_assert (gkm_store_lookup_schema (test->store, CKA_VALUE, &flags)); g_assert (flags == GKM_STORE_IS_SENSITIVE); } @@ -83,3 +87,16 @@ TESTING_TEST(store_schema_flags) * derived class. For more tests see unit-test-memory-store.c and * unit-test-file-store.c */ + + +int +main (int argc, char **argv) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + + g_test_add ("/gkm/store/schema", Test, NULL, setup, test_schema, teardown); + g_test_add ("/gkm/store/schema_flags", Test, NULL, setup, test_schema_flags, teardown); + + return g_test_run (); +} diff --git a/pkcs11/gkm/tests/test-timer.c b/pkcs11/gkm/tests/test-timer.c new file mode 100644 index 00000000..dca99712 --- /dev/null +++ b/pkcs11/gkm/tests/test-timer.c @@ -0,0 +1,175 @@ +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ +/* test-timer.c: Test thread timer functionality + + Copyright (C) 2009 Stefan Walter + + The Gnome Keyring Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The Gnome Keyring Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the Gnome Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. + + Author: Stef Walter <stef@memberwebs.com> +*/ + +#include "config.h" + +#include "mock-module.h" + +#include "gkm/gkm-timer.h" + +#include "egg/egg-testing.h" + +#include <glib-object.h> + +typedef struct { + GkmModule *module; +} Test; + +static void +setup (Test* test, gconstpointer unused) +{ + test->module = mock_module_initialize_and_enter (); +} + +static void +teardown (Test* test, gconstpointer unused) +{ + mock_module_leave_and_finalize (); +} + +static void +test_extra_initialize (Test* test, gconstpointer unused) +{ + gkm_timer_initialize (); + gkm_timer_shutdown (); +} + +static void +timer_callback (GkmTimer *timer, gpointer user_data) +{ + GkmTimer **value = user_data; + g_assert (timer); + g_assert (timer == *value); + *value = NULL; +} + +static void +test_simple (Test* test, gconstpointer unused) +{ + GkmTimer *timer; + + timer = gkm_timer_start (test->module, 2, timer_callback, &timer); + + mock_module_leave (); + egg_test_wait_until (2200); + mock_module_enter (); + + g_assert (timer == NULL); +} + +static void +test_cancel (Test* test, gconstpointer unused) +{ + GkmTimer *timer; + + timer = gkm_timer_start (test->module, 2, timer_callback, &timer); + + mock_module_leave (); + egg_test_wait_until (500); + mock_module_enter (); + + gkm_timer_cancel (timer); + + mock_module_leave (); + egg_test_wait_until (2000); + mock_module_enter (); + + /* The callback should not have been called */ + g_assert (timer != NULL); +} + +static void +test_immediate (Test* test, gconstpointer unused) +{ + GkmTimer *timer; + + /* Setup timer in the past, should execute as soon as possible */ + timer = gkm_timer_start (test->module, -5, timer_callback, &timer); + + /* Should not be called immediately */ + g_assert (timer != NULL); + + mock_module_leave (); + egg_test_wait_until (50); + mock_module_enter (); + + /* Should have been called now */ + g_assert (timer == NULL); +} + +static GkmTimer *timer_last = NULL; +static gint timer_check = 0; + +static void +multiple_callback (GkmTimer *timer, gpointer user_data) +{ + gint value = GPOINTER_TO_INT (user_data); + g_assert (timer); + g_assert (timer != timer_last); + g_assert (value == timer_check); + timer_last = timer; + timer_check += 1; +} + +static void +test_multiple (Test* test, gconstpointer unused) +{ + timer_check = 0; + + /* Multiple timers, add out of order, should be called in order */ + gkm_timer_start (test->module, 1, multiple_callback, GINT_TO_POINTER (1)); + gkm_timer_start (test->module, 3, multiple_callback, GINT_TO_POINTER (3)); + gkm_timer_start (test->module, 2, multiple_callback, GINT_TO_POINTER (2)); + gkm_timer_start (test->module, 0, multiple_callback, GINT_TO_POINTER (0)); + + mock_module_leave (); + egg_test_wait_until (3500); + mock_module_enter (); + + g_assert (timer_check == 4); +} + +static void +test_outstanding (Test* test, gconstpointer unused) +{ + /* A timer that can't be called */ + gkm_timer_start (test->module, 5, timer_callback, NULL); + gkm_timer_start (test->module, 10, timer_callback, NULL); + gkm_timer_start (test->module, 1, timer_callback, NULL); +} + +int +main (int argc, char **argv) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + + g_test_add ("/gkm/secret/extra_initialize", Test, NULL, setup, test_extra_initialize, teardown); + g_test_add ("/gkm/secret/simple", Test, NULL, setup, test_simple, teardown); + g_test_add ("/gkm/secret/cancel", Test, NULL, setup, test_cancel, teardown); + g_test_add ("/gkm/secret/immediate", Test, NULL, setup, test_immediate, teardown); + g_test_add ("/gkm/secret/multiple", Test, NULL, setup, test_multiple, teardown); + g_test_add ("/gkm/secret/outstanding", Test, NULL, setup, test_outstanding, teardown); + + return egg_tests_run_in_thread_with_loop (); +} diff --git a/pkcs11/gkm/tests/unit-test-transaction.c b/pkcs11/gkm/tests/test-transaction.c index 78ab2d4d..ac6317b4 100644 --- a/pkcs11/gkm/tests/unit-test-transaction.c +++ b/pkcs11/gkm/tests/test-transaction.c @@ -1,5 +1,5 @@ /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* unit-test-store.c: Test general store functionality +/* test-transaction.c: Test transaction functionality Copyright (C) 2008 Stefan Walter @@ -21,18 +21,25 @@ Author: Stef Walter <stef@memberwebs.com> */ -#include "test-suite.h" +#include "config.h" #include "gkm/gkm-transaction.h" -TESTING_SETUP (transaction_setup) +#include <glib/gstdio.h> + +typedef struct { + int unused; +} Test; + +static void +setup (Test* test, gconstpointer unused) { GDir *dir; const gchar *directory; const gchar *basename; gchar *filename; - directory = testing_scratch_directory (); + directory = "/tmp"; dir = g_dir_open (directory, 0, NULL); g_assert (dir); @@ -50,7 +57,14 @@ TESTING_SETUP (transaction_setup) g_dir_close (dir); } -TESTING_TEST(transaction_empty) +static void +teardown (Test *test, gconstpointer unused) +{ + +} + +static void +test_transaction_empty (Test* test, gconstpointer unused) { GkmTransaction *transaction; gboolean completed, failed; @@ -77,7 +91,8 @@ TESTING_TEST(transaction_empty) g_object_unref (transaction); } -TESTING_TEST(transaction_fail) +static void +test_transaction_fail (Test* test, gconstpointer unused) { GkmTransaction *transaction; @@ -123,7 +138,8 @@ completed_callback (GkmTransaction *transaction, GObject *object, gpointer data) return TRUE; } -TESTING_TEST(transaction_signals_success) +static void +test_transaction_signals_success (Test* test, gconstpointer unused) { GkmTransaction *transaction = gkm_transaction_new (); @@ -146,7 +162,8 @@ TESTING_TEST(transaction_signals_success) g_object_unref (transaction); } -TESTING_TEST(transaction_signals_failure) +static void +test_transaction_signals_failure (Test* test, gconstpointer unused) { GkmTransaction *transaction = gkm_transaction_new (); @@ -183,7 +200,8 @@ order_callback (GkmTransaction *transaction, GObject *object, gpointer data) return TRUE; } -TESTING_TEST(transaction_order_is_reverse) +static void +test_transaction_order_is_reverse (Test* test, gconstpointer unused) { GkmTransaction *transaction = gkm_transaction_new (); @@ -196,7 +214,8 @@ TESTING_TEST(transaction_order_is_reverse) g_object_unref (transaction); } -TESTING_TEST(transaction_dispose_completes) +static void +test_transaction_dispose_completes (Test* test, gconstpointer unused) { GkmTransaction *transaction = gkm_transaction_new (); @@ -212,10 +231,11 @@ TESTING_TEST(transaction_dispose_completes) g_object_unref (transaction); } -TESTING_TEST(remove_file_success) +static void +test_remove_file_success (Test* test, gconstpointer unused) { GkmTransaction *transaction = gkm_transaction_new (); - gchar *filename = testing_scratch_filename ("transaction-remove"); + const gchar *filename = "/tmp/transaction-remove"; g_assert (g_file_set_contents (filename, "xxx", 3, NULL)); g_assert (g_file_test (filename, G_FILE_TEST_IS_REGULAR)); @@ -229,13 +249,13 @@ TESTING_TEST(remove_file_success) g_assert (!g_file_test (filename, G_FILE_TEST_IS_REGULAR)); g_object_unref (transaction); - g_free (filename); } -TESTING_TEST(remove_file_abort) +static void +test_remove_file_abort (Test* test, gconstpointer unused) { GkmTransaction *transaction = gkm_transaction_new (); - gchar *filename = testing_scratch_filename ("transaction-remove"); + const gchar *filename = "/tmp/transaction-remove"; gchar *data; gsize n_data; @@ -261,13 +281,13 @@ TESTING_TEST(remove_file_abort) g_unlink (filename); g_object_unref (transaction); - g_free (filename); } -TESTING_TEST(remove_file_non_exist) +static void +test_remove_file_non_exist (Test* test, gconstpointer unused) { GkmTransaction *transaction = gkm_transaction_new (); - gchar *filename = testing_scratch_filename ("transaction-non-existant"); + const gchar *filename = "/tmp/transaction-non-existant"; g_unlink (filename); @@ -277,13 +297,13 @@ TESTING_TEST(remove_file_non_exist) gkm_transaction_complete (transaction); g_object_unref (transaction); - g_free (filename); } -TESTING_TEST(write_file) +static void +test_write_file (Test* test, gconstpointer unused) { GkmTransaction *transaction = gkm_transaction_new (); - gchar *filename = testing_scratch_filename ("transaction-test"); + const gchar *filename = "/tmp/transaction-test"; gchar *data; gsize n_data; @@ -303,13 +323,13 @@ TESTING_TEST(write_file) g_free (data); g_object_unref (transaction); - g_free (filename); } -TESTING_TEST(write_file_abort_gone) +static void +test_write_file_abort_gone (Test* test, gconstpointer unused) { GkmTransaction *transaction = gkm_transaction_new (); - gchar *filename = testing_scratch_filename ("transaction-test"); + const gchar *filename = "/tmp/transaction-test"; gchar *data; gsize n_data; @@ -329,13 +349,13 @@ TESTING_TEST(write_file_abort_gone) g_assert (!g_file_test (filename, G_FILE_TEST_IS_REGULAR)); g_object_unref (transaction); - g_free (filename); } -TESTING_TEST(write_file_abort_revert) +static void +test_write_file_abort_revert (Test* test, gconstpointer unused) { GkmTransaction *transaction = gkm_transaction_new (); - gchar *filename = testing_scratch_filename ("transaction-test"); + const gchar *filename = "/tmp/transaction-test"; gchar *data; g_assert (g_file_set_contents (filename, "my original", -1, NULL)); @@ -355,13 +375,13 @@ TESTING_TEST(write_file_abort_revert) g_free (data); g_object_unref (transaction); - g_free (filename); } -TESTING_TEST (unique_file_conflict) +static void +test_unique_file_conflict (Test* test, gconstpointer unused) { GkmTransaction *transaction = gkm_transaction_new (); - gchar *filename = testing_scratch_filename ("transaction-test"); + const gchar *filename = "/tmp/transaction-test"; gchar *dirname; gchar *basename; gchar *result; @@ -383,13 +403,13 @@ TESTING_TEST (unique_file_conflict) g_free (result); g_object_unref (transaction); - g_free (filename); } -TESTING_TEST (unique_file_conflict_with_ext) +static void +test_unique_file_conflict_with_ext (Test* test, gconstpointer unused) { GkmTransaction *transaction = gkm_transaction_new (); - gchar *filename = testing_scratch_filename ("transaction-test.ext"); + const gchar *filename = "/tmp/transaction-test.ext"; gchar *dirname; gchar *basename; gchar *result; @@ -411,13 +431,13 @@ TESTING_TEST (unique_file_conflict_with_ext) g_free (result); g_object_unref (transaction); - g_free (filename); } -TESTING_TEST (unique_file_no_conflict) +static void +test_unique_file_no_conflict (Test* test, gconstpointer unused) { GkmTransaction *transaction = gkm_transaction_new (); - const gchar *dirname = testing_scratch_directory (); + const gchar *dirname = "/tmp"; gchar *result; result = gkm_transaction_unique_file (transaction, dirname, "transaction-another"); @@ -430,3 +450,28 @@ TESTING_TEST (unique_file_no_conflict) g_object_unref (transaction); } + +int +main (int argc, char **argv) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + + g_test_add ("/gkm/transaction/transaction_empty", Test, NULL, setup, test_transaction_empty, teardown); + g_test_add ("/gkm/transaction/transaction_fail", Test, NULL, setup, test_transaction_fail, teardown); + g_test_add ("/gkm/transaction/transaction_signals_success", Test, NULL, setup, test_transaction_signals_success, teardown); + g_test_add ("/gkm/transaction/transaction_signals_failure", Test, NULL, setup, test_transaction_signals_failure, teardown); + g_test_add ("/gkm/transaction/transaction_order_is_reverse", Test, NULL, setup, test_transaction_order_is_reverse, teardown); + g_test_add ("/gkm/transaction/transaction_dispose_completes", Test, NULL, setup, test_transaction_dispose_completes, teardown); + g_test_add ("/gkm/transaction/remove_file_success", Test, NULL, setup, test_remove_file_success, teardown); + g_test_add ("/gkm/transaction/remove_file_abort", Test, NULL, setup, test_remove_file_abort, teardown); + g_test_add ("/gkm/transaction/remove_file_non_exist", Test, NULL, setup, test_remove_file_non_exist, teardown); + g_test_add ("/gkm/transaction/write_file", Test, NULL, setup, test_write_file, teardown); + g_test_add ("/gkm/transaction/write_file_abort_gone", Test, NULL, setup, test_write_file_abort_gone, teardown); + g_test_add ("/gkm/transaction/write_file_abort_revert", Test, NULL, setup, test_write_file_abort_revert, teardown); + g_test_add ("/gkm/transaction/unique_file_conflict", Test, NULL, setup, test_unique_file_conflict, teardown); + g_test_add ("/gkm/transaction/unique_file_conflict_with_ext", Test, NULL, setup, test_unique_file_conflict_with_ext, teardown); + g_test_add ("/gkm/transaction/unique_file_no_conflict", Test, NULL, setup, test_unique_file_no_conflict, teardown); + + return g_test_run (); +} diff --git a/pkcs11/gkm/tests/unit-test-file-tracker.c b/pkcs11/gkm/tests/unit-test-file-tracker.c deleted file mode 100644 index 49d28d54..00000000 --- a/pkcs11/gkm/tests/unit-test-file-tracker.c +++ /dev/null @@ -1,221 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* unit-test-location.c: Test location functionality - - Copyright (C) 2007 Stefan Walter - - The Gnome Keyring Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The Gnome Keyring Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with the Gnome Library; see the file COPYING.LIB. If not, - write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. - - Author: Stef Walter <stef@memberwebs.com> -*/ - -#include <stdlib.h> -#include <stdio.h> -#include <string.h> -#include <unistd.h> - -#include "test-suite.h" - -#include "gkm/gkm-file-tracker.h" - -#include <glib/gstdio.h> - -#define DATA "test-data" -#define SUBDIR "test-subdir" -#define WILDCARD "*.woo?" - -static GkmFileTracker *the_tracker = NULL; -static gchar *test_dir = NULL; -static gchar *test_file = NULL; - -static guint n_files_added = 0; -static gchar* last_file_added = 0; - -static guint n_files_changed = 0; -static gchar* last_file_changed = 0; - -static guint n_files_removed = 0; -static gchar* last_file_removed = 0; - -static void -file_added (GkmFileTracker *tracker, const gchar *path, gpointer unused) -{ - g_assert ("should be a non-null path" && path != NULL); - - ++n_files_added; - g_free (last_file_added); - last_file_added = g_strdup (path); -} - -static void -file_changed (GkmFileTracker *tracker, const gchar *path, gpointer unused) -{ - g_assert ("should be a non-null path" && path != NULL); - - ++n_files_changed; - g_free (last_file_changed); - last_file_changed = g_strdup (path); -} - -static void -file_removed (GkmFileTracker *tracker, const gchar *path, gpointer unused) -{ - g_assert ("should be a non-null path" && path != NULL); - - ++n_files_removed; - g_free (last_file_removed); - last_file_removed = g_strdup (path); -} - -static void -file_reset_stats (void) -{ - g_free (last_file_removed); - g_free (last_file_added); - g_free (last_file_changed); - last_file_removed = last_file_added = last_file_changed = NULL; - n_files_added = n_files_changed = n_files_removed = 0; -} - -TESTING_SETUP(tracker) -{ - /* Make a test directory */ - test_dir = g_build_filename ("/tmp", SUBDIR, NULL); - - the_tracker = gkm_file_tracker_new (test_dir, WILDCARD, NULL); - g_signal_connect (the_tracker, "file-added", G_CALLBACK (file_added), NULL); - g_signal_connect (the_tracker, "file-removed", G_CALLBACK (file_removed), NULL); - g_signal_connect (the_tracker, "file-changed", G_CALLBACK (file_changed), NULL); - - /* Mtime must change so wait between tests */ - sleep (1); - - test_file = g_build_filename (test_dir, "my-file.woof", NULL); - g_unlink (test_file); -} - -TESTING_TEARDOWN(tracker) -{ - file_reset_stats (); - g_object_unref (the_tracker); - g_free (test_dir); - g_free (test_file); -} - -TESTING_TEST(file_watch) -{ - /* A watch for an non-existant directory, should have no responses */ - gkm_file_tracker_refresh (the_tracker, FALSE); - - g_assert_cmpint (0, ==, n_files_added); - g_assert_cmpint (0, ==, n_files_changed); - g_assert_cmpint (0, ==, n_files_removed); - - g_mkdir_with_parents (test_dir, 0700); - - /* Should still have no responses even though it exists */ - gkm_file_tracker_refresh (the_tracker, FALSE); - - g_assert_cmpint (0, ==, n_files_added); - g_assert_cmpint (0, ==, n_files_changed); - g_assert_cmpint (0, ==, n_files_removed); -} - -TESTING_TEST(watch_file) -{ - gboolean ret; - - /* Make sure things are clean */ - gkm_file_tracker_refresh (the_tracker, FALSE); - - n_files_added = n_files_changed = n_files_removed = 0; - last_file_added = last_file_changed = last_file_removed = 0; - - ret = g_file_set_contents (test_file, DATA, strlen (DATA), NULL); - g_assert (ret); - - /* Now make sure that file is located */ - gkm_file_tracker_refresh (the_tracker, FALSE); - - g_assert_cmpint (1, ==, n_files_added); - g_assert_cmpint (0, ==, n_files_changed); - g_assert_cmpint (0, ==, n_files_removed); - - /* The added one should match our file */ - g_assert_cmpstr (last_file_added, ==, test_file); - - file_reset_stats (); - sleep (1); - - /* Shouldn't find the file again */ - gkm_file_tracker_refresh (the_tracker, FALSE); - g_assert_cmpint (0, ==, n_files_added); - g_assert_cmpint (0, ==, n_files_changed); - g_assert_cmpint (0, ==, n_files_removed); - - /* But we should find the file if forced to */ - gkm_file_tracker_refresh (the_tracker, TRUE); - g_assert_cmpint (0, ==, n_files_added); - g_assert_cmpint (1, ==, n_files_changed); - g_assert_cmpint (0, ==, n_files_removed); - g_assert_cmpstr (last_file_changed, ==, test_file); - - file_reset_stats (); - - ret = g_file_set_contents (test_file, DATA, strlen (DATA), NULL); - g_assert (ret); - - /* File was updated */ - gkm_file_tracker_refresh (the_tracker, FALSE); - g_assert_cmpint (0, ==, n_files_added); - g_assert_cmpint (1, ==, n_files_changed); - g_assert_cmpint (0, ==, n_files_removed); - g_assert_cmpstr (last_file_changed, ==, test_file); - - file_reset_stats (); - g_unlink (test_file); - - /* Now file should be removed */ - gkm_file_tracker_refresh (the_tracker, FALSE); - - g_assert_cmpint (0, ==, n_files_added); - g_assert_cmpint (0, ==, n_files_changed); - g_assert_cmpint (1, ==, n_files_removed); - g_assert_cmpstr (last_file_removed, ==, test_file); -} - -TESTING_TEST(nomatch) -{ - gchar *file = g_build_filename (test_dir, "my-file.toot", NULL); - gboolean ret; - - /* Mtime must change so wait between tests */ - sleep (1); - - ret = g_file_set_contents (file, DATA, strlen (DATA), NULL); - g_assert (ret); - - file_reset_stats (); - - /* Now make sure that file is not located */ - gkm_file_tracker_refresh (the_tracker, FALSE); - - g_assert_cmpint (0, ==, n_files_added); - g_assert_cmpint (0, ==, n_files_changed); - g_assert_cmpint (0, ==, n_files_removed); - - g_unlink (file); - g_free (file); -} diff --git a/pkcs11/gkm/tests/unit-test-memory-store.c b/pkcs11/gkm/tests/unit-test-memory-store.c deleted file mode 100644 index 7c1e60e2..00000000 --- a/pkcs11/gkm/tests/unit-test-memory-store.c +++ /dev/null @@ -1,438 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* unit-test-memory-store.c: Test memory store functionality - - Copyright (C) 2008 Stefan Walter - - The Gnome Keyring Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The Gnome Keyring Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with the Gnome Library; see the file COPYING.LIB. If not, - write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. - - Author: Stef Walter <stef@memberwebs.com> -*/ - -#include <stdlib.h> -#include <stdio.h> -#include <string.h> -#include <unistd.h> - -#include "test-suite.h" -#include "test-module.h" - -#include "gkm/gkm-object.h" -#include "gkm/gkm-memory-store.h" -#include "gkm/gkm-transaction.h" - -static GkmModule *module = NULL; -static GkmStore *store = NULL; -static GkmObject *object = NULL; -static GkmTransaction *transaction = NULL; -static guchar buffer[1024]; - -static CK_RV -test_validator (GkmObject *obj, CK_ATTRIBUTE_PTR attr) -{ - const gchar *data; - guint i; - - g_assert (obj == object); - g_assert (attr); - g_assert (attr->type == CKA_LABEL); - - /* Test that the whole string is ascii and lower case */ - data = attr->pValue; - for (i = 0; i < attr->ulValueLen; ++i) { - if (!g_ascii_isprint(data[i]) || !g_ascii_islower (data[i])) - return CKR_ATTRIBUTE_VALUE_INVALID; - } - - return CKR_OK; -} - -TESTING_SETUP(memory_store) -{ - CK_ATTRIBUTE attr; - CK_ULONG twentyfour = 24; - - module = test_module_initialize_and_enter (); - - attr.type = CKA_LABEL; - attr.pValue = "label"; - attr.ulValueLen = 5; - - store = GKM_STORE (gkm_memory_store_new ()); - - gkm_store_register_schema (store, &attr, test_validator, 0); - g_assert (gkm_store_lookup_schema (store, CKA_LABEL, NULL)); - - attr.type = CKA_VALUE; - attr.pValue = NULL; - attr.ulValueLen = 0; - - gkm_store_register_schema (store, &attr, NULL, GKM_STORE_IS_SENSITIVE); - - attr.type = CKA_BITS_PER_PIXEL; - attr.pValue = &twentyfour; - attr.ulValueLen = sizeof (twentyfour); - - gkm_store_register_schema (store, &attr, NULL, GKM_STORE_IS_INTERNAL); - - object = g_object_new (GKM_TYPE_OBJECT, "module", module, NULL); - - transaction = gkm_transaction_new (); -} - -TESTING_TEARDOWN(memory_store) -{ - g_object_unref (store); - store = NULL; - - g_object_unref (transaction); - transaction = NULL; - - if (object != NULL) - g_object_unref (object); - object = NULL; - - test_module_leave_and_finalize (); - module = NULL; -} - -TESTING_TEST(get_attribute_default) -{ - CK_ATTRIBUTE attr; - CK_RV rv; - - attr.type = CKA_LABEL; - attr.pValue = NULL; - rv = gkm_store_get_attribute (store, object, &attr); - g_assert (rv == CKR_OK); - g_assert (attr.ulValueLen == 5); - attr.pValue = buffer; - rv = gkm_store_get_attribute (store, object, &attr); - g_assert (rv == CKR_OK); - g_assert (attr.ulValueLen == 5); - g_assert (memcmp (attr.pValue, "label", 5) == 0); -} - -TESTING_TEST(read_value_default) -{ - gconstpointer value; - gsize n_value; - - value = gkm_store_read_value (store, object, CKA_LABEL, &n_value); - g_assert (value); - g_assert (n_value == 5); - g_assert (memcmp (value, "label", 5) == 0); - - value = gkm_store_read_value (store, object, CKA_BITS_PER_PIXEL, &n_value); - g_assert (value); - g_assert (n_value == sizeof (CK_ULONG)); - g_assert (*((CK_ULONG_PTR)value) == 24); -} - -TESTING_TEST(read_string) -{ - gchar *str; - - str = gkm_store_read_string (store, object, CKA_LABEL); - g_assert_cmpstr (str, ==, "label"); - g_free (str); -} - -TESTING_TEST(get_invalid) -{ - CK_ATTRIBUTE attr; - CK_RV rv; - - attr.type = CKA_APPLICATION; - attr.pValue = NULL; - attr.ulValueLen = 0; - - rv = gkm_store_get_attribute (store, object, &attr); - g_assert (rv == CKR_ATTRIBUTE_TYPE_INVALID); -} - -TESTING_TEST(get_sensitive) -{ - CK_ATTRIBUTE attr; - CK_RV rv; - - attr.type = CKA_VALUE; - attr.pValue = NULL; - attr.ulValueLen = 0; - - rv = gkm_store_get_attribute (store, object, &attr); - g_assert (rv == CKR_ATTRIBUTE_SENSITIVE); -} - -TESTING_TEST(get_internal) -{ - CK_ATTRIBUTE attr; - CK_RV rv; - - attr.type = CKA_BITS_PER_PIXEL; - attr.pValue = NULL; - attr.ulValueLen = 0; - - rv = gkm_store_get_attribute (store, object, &attr); - g_assert (rv == CKR_ATTRIBUTE_TYPE_INVALID); -} - -TESTING_TEST(set_invalid) -{ - CK_ATTRIBUTE attr; - - attr.type = CKA_APPLICATION; - attr.pValue = "me"; - attr.ulValueLen = 2; - - gkm_store_set_attribute (store, transaction, object, &attr); - g_assert (gkm_transaction_get_result (transaction) == CKR_ATTRIBUTE_TYPE_INVALID); -} - -TESTING_TEST(set_internal) -{ - CK_ATTRIBUTE attr; - CK_ULONG five = 5; - - attr.type = CKA_BITS_PER_PIXEL; - attr.pValue = &five; - attr.ulValueLen = sizeof (five); - - gkm_store_set_attribute (store, transaction, object, &attr); - g_assert (gkm_transaction_get_result (transaction) == CKR_ATTRIBUTE_TYPE_INVALID); -} - -TESTING_TEST(set_get_attribute) -{ - CK_ATTRIBUTE attr; - CK_RV rv; - - attr.type = CKA_LABEL; - attr.pValue = "booyah"; - attr.ulValueLen = 6; - - gkm_store_set_attribute (store, transaction, object, &attr); - - gkm_transaction_complete (transaction); - g_assert (gkm_transaction_get_result (transaction) == CKR_OK); - - attr.pValue = buffer; - attr.ulValueLen = 1024; - rv = gkm_store_get_attribute (store, object, &attr); - g_assert (rv == CKR_OK); - g_assert (attr.ulValueLen == 6); - g_assert (memcmp (attr.pValue, "booyah", 6) == 0); -} - -TESTING_TEST(write_read_value) -{ - CK_ATTRIBUTE attr; - CK_ULONG five = 5; - gconstpointer value; - gsize n_value; - - attr.type = CKA_BITS_PER_PIXEL; - attr.pValue = &five; - attr.ulValueLen = sizeof (five); - - gkm_store_write_value (store, transaction, object, &attr); - - gkm_transaction_complete (transaction); - g_assert (gkm_transaction_get_result (transaction) == CKR_OK); - - value = gkm_store_read_value (store, object, CKA_BITS_PER_PIXEL, &n_value); - g_assert (value); - g_assert (n_value == sizeof (five)); - g_assert (memcmp (value, &five, sizeof (five)) == 0); -} - -TESTING_TEST(set_no_validate) -{ - CK_ATTRIBUTE attr; - - attr.type = CKA_LABEL; - attr.pValue = "CAPITALS"; - attr.ulValueLen = 8; - - gkm_store_set_attribute (store, transaction, object, &attr); - g_assert (gkm_transaction_get_failed (transaction)); - - gkm_transaction_complete (transaction); - g_assert (gkm_transaction_get_result (transaction) == CKR_ATTRIBUTE_VALUE_INVALID); -} - -TESTING_TEST(set_transaction_default) -{ - CK_ATTRIBUTE attr; - gconstpointer value; - gsize n_value; - - - attr.type = CKA_LABEL; - attr.pValue = "another"; - attr.ulValueLen = 7; - - /* Change the attribute */ - gkm_store_set_attribute (store, transaction, object, &attr); - g_assert (gkm_transaction_get_failed (transaction) == FALSE); - - /* Should get new value */ - value = gkm_store_read_value (store, object, CKA_LABEL, &n_value); - g_assert (value && n_value == attr.ulValueLen); - g_assert (memcmp (attr.pValue, value, n_value) == 0); - - /* Fail for some arbitrary reason */ - gkm_transaction_fail (transaction, CKR_ATTRIBUTE_VALUE_INVALID); - - /* Value should not have changed yet */ - value = gkm_store_read_value (store, object, CKA_LABEL, &n_value); - g_assert (value && n_value == attr.ulValueLen); - g_assert (memcmp (attr.pValue, value, n_value) == 0); - - /* Now complete the transaction */ - gkm_transaction_complete (transaction); - g_assert (gkm_transaction_get_failed (transaction) == TRUE); - - /* Value should now have changed, back to default */ - value = gkm_store_read_value (store, object, CKA_LABEL, &n_value); - g_assert (value && n_value == 5); - g_assert (memcmp (value, "label", 5) == 0); -} - -TESTING_TEST(set_transaction_revert_first) -{ - CK_ATTRIBUTE attr, prev; - gconstpointer value; - gsize n_value; - - prev.type = CKA_LABEL; - prev.pValue = "numberone"; - prev.ulValueLen = 9; - - /* Change the attribute */ - gkm_store_set_attribute (store, transaction, object, &prev); - gkm_transaction_complete (transaction); - g_assert (gkm_transaction_get_failed (transaction) == FALSE); - - /* Value should be new value */ - value = gkm_store_read_value (store, object, CKA_LABEL, &n_value); - g_assert (value && n_value == prev.ulValueLen); - g_assert (memcmp (prev.pValue, value, n_value) == 0); - - /* A new transaction */ - g_object_unref (transaction); - transaction = gkm_transaction_new (); - - attr.type = CKA_LABEL; - attr.pValue = "second"; - attr.ulValueLen = 6; - - gkm_store_set_attribute (store, transaction, object, &attr); - g_assert (gkm_transaction_get_failed (transaction) == FALSE); - - /* Should get new value */ - value = gkm_store_read_value (store, object, CKA_LABEL, &n_value); - g_assert (value && n_value == attr.ulValueLen); - g_assert (memcmp (attr.pValue, value, n_value) == 0); - - attr.type = CKA_LABEL; - attr.pValue = "third"; - attr.ulValueLen = 5; - - gkm_store_set_attribute (store, transaction, object, &attr); - g_assert (gkm_transaction_get_failed (transaction) == FALSE); - - /* Should get new value */ - value = gkm_store_read_value (store, object, CKA_LABEL, &n_value); - g_assert (value && n_value == attr.ulValueLen); - g_assert (memcmp (attr.pValue, value, n_value) == 0); - - /* Fail for some arbitrary reason */ - gkm_transaction_fail (transaction, CKR_ATTRIBUTE_VALUE_INVALID); - - /* Value should not have changed yet */ - value = gkm_store_read_value (store, object, CKA_LABEL, &n_value); - g_assert (value && n_value == attr.ulValueLen); - g_assert (memcmp (attr.pValue, value, n_value) == 0); - - /* Now complete the transaction */ - gkm_transaction_complete (transaction); - g_assert (gkm_transaction_get_failed (transaction) == TRUE); - - /* Value should now have changed, back to default */ - value = gkm_store_read_value (store, object, CKA_LABEL, &n_value); - g_assert (value && n_value == prev.ulValueLen); - g_assert (memcmp (prev.pValue, value, n_value) == 0); -} - -static void -notify_attribute (GkmObject *obj, CK_ATTRIBUTE_TYPE type, gpointer data) -{ - g_assert (obj == object); - g_assert (type == CKA_LABEL); - g_assert (data); - - *((CK_ATTRIBUTE_TYPE*)data) = type; -} - -TESTING_TEST(set_notifies) -{ - CK_ATTRIBUTE attr; - CK_ATTRIBUTE_TYPE type = 0; - - attr.type = CKA_LABEL; - attr.pValue = "valid"; - attr.ulValueLen = 5; - - g_signal_connect (object, "notify-attribute", G_CALLBACK (notify_attribute), &type); - - gkm_store_set_attribute (store, transaction, object, &attr); - - /* We should have been notified that the attribute changed at this point */ - g_assert (type == CKA_LABEL); - - /* Reset for next notify */ - type = 0; - - /* Fail for some arbitrary reason */ - gkm_transaction_fail (transaction, CKR_ATTRIBUTE_VALUE_INVALID); - - /* We should not have been notified yet */ - g_assert (type == 0); - - /* Now complete the transaction */ - gkm_transaction_complete (transaction); - g_assert (gkm_transaction_get_failed (transaction) == TRUE); - - /* Now we should have been notified that this changed back */ - g_assert (type == CKA_LABEL); -} - -TESTING_TEST(set_object_gone_first) -{ - CK_ATTRIBUTE attr; - - attr.type = CKA_LABEL; - attr.pValue = "valid"; - attr.ulValueLen = 5; - - gkm_store_set_attribute (store, transaction, object, &attr); - gkm_transaction_complete (transaction); - g_assert (gkm_transaction_get_result (transaction) == CKR_OK); - - /* This tests memory store internal tracking */ - g_object_unref (object); - object = NULL; -} diff --git a/pkcs11/gkm/tests/unit-test-timer.c b/pkcs11/gkm/tests/unit-test-timer.c deleted file mode 100644 index 13bdd245..00000000 --- a/pkcs11/gkm/tests/unit-test-timer.c +++ /dev/null @@ -1,144 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* unit-test-timer.c: Test thread timer functionality - - Copyright (C) 2009 Stefan Walter - - The Gnome Keyring Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The Gnome Keyring Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with the Gnome Library; see the file COPYING.LIB. If not, - write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. - - Author: Stef Walter <stef@memberwebs.com> -*/ - -#include "test-suite.h" -#include "test-module.h" - -#include "gkm/gkm-timer.h" - -static GkmModule *module = NULL; - -TESTING_SETUP(timer_setup) -{ - module = test_module_initialize_and_enter (); -} - -TESTING_TEARDOWN(timer_teardown) -{ - test_module_leave_and_finalize (); -} - -TESTING_TEST(timer_extra_initialize) -{ - gkm_timer_initialize (); - gkm_timer_shutdown (); -} - -static void -timer_callback (GkmTimer *timer, gpointer user_data) -{ - GkmTimer **value = user_data; - g_assert (timer); - g_assert (timer == *value); - *value = NULL; -} - -TESTING_TEST(timer_simple) -{ - GkmTimer *timer; - - timer = gkm_timer_start (module, 2, timer_callback, &timer); - - test_module_leave (); - testing_wait_until (2200); - test_module_enter (); - - g_assert (timer == NULL); -} - -TESTING_TEST(timer_cancel) -{ - GkmTimer *timer; - - timer = gkm_timer_start (module, 2, timer_callback, &timer); - - test_module_leave (); - testing_wait_until (500); - test_module_enter (); - - gkm_timer_cancel (timer); - - test_module_leave (); - testing_wait_until (2000); - test_module_enter (); - - /* The callback should not have been called */ - g_assert (timer != NULL); -} - -TESTING_TEST(timer_immediate) -{ - GkmTimer *timer; - - /* Setup timer in the past, should execute as soon as possible */ - timer = gkm_timer_start (module, -5, timer_callback, &timer); - - /* Should not be called immediately */ - g_assert (timer != NULL); - - test_module_leave (); - testing_wait_until (50); - test_module_enter (); - - /* Should have been called now */ - g_assert (timer == NULL); -} - -static GkmTimer *timer_last = NULL; -static gint timer_check = 0; - -static void -multiple_callback (GkmTimer *timer, gpointer user_data) -{ - gint value = GPOINTER_TO_INT (user_data); - g_assert (timer); - g_assert (timer != timer_last); - g_assert (value == timer_check); - timer_last = timer; - timer_check += 1; -} - -TESTING_TEST(timer_multiple) -{ - timer_check = 0; - - /* Multiple timers, add out of order, should be called in order */ - gkm_timer_start (module, 1, multiple_callback, GINT_TO_POINTER (1)); - gkm_timer_start (module, 3, multiple_callback, GINT_TO_POINTER (3)); - gkm_timer_start (module, 2, multiple_callback, GINT_TO_POINTER (2)); - gkm_timer_start (module, 0, multiple_callback, GINT_TO_POINTER (0)); - - test_module_leave (); - testing_wait_until (3500); - test_module_enter (); - - g_assert (timer_check == 4); -} - -TESTING_TEST(timer_outstanding) -{ - /* A timer that can't be called */ - gkm_timer_start (module, 5, timer_callback, NULL); - gkm_timer_start (module, 10, timer_callback, NULL); - gkm_timer_start (module, 1, timer_callback, NULL); -} |