diff options
-rw-r--r-- | .gitignore | 42 | ||||
-rw-r--r-- | Makefile.am | 4 | ||||
-rw-r--r-- | daemon/control/tests/Makefile.am | 32 | ||||
-rw-r--r-- | daemon/control/tests/frob-control-change.c (renamed from daemon/control/tests/test-control-change.c) | 11 | ||||
-rw-r--r-- | daemon/control/tests/frob-control-init.c (renamed from daemon/control/tests/test-control-init.c) | 11 | ||||
-rw-r--r-- | daemon/control/tests/frob-control-quit.c (renamed from daemon/control/tests/test-control-quit.c) | 11 | ||||
-rw-r--r-- | daemon/control/tests/frob-control-unlock.c (renamed from daemon/control/tests/test-control-unlock.c) | 11 | ||||
-rw-r--r-- | daemon/dbus/tests/Makefile.am | 4 | ||||
-rw-r--r-- | egg/tests/Makefile.am | 10 | ||||
-rw-r--r-- | egg/tests/test-asn1.c | 10 | ||||
-rw-r--r-- | egg/tests/test-asn1x.c | 23 | ||||
-rw-r--r-- | egg/tests/test-cleanup.c | 2 | ||||
-rw-r--r-- | egg/tests/test-dh.c | 6 | ||||
-rw-r--r-- | egg/tests/test-dn.c | 12 | ||||
-rw-r--r-- | egg/tests/test-hex.c | 2 | ||||
-rw-r--r-- | egg/tests/test-hkdf.c | 6 | ||||
-rw-r--r-- | egg/tests/test-oid.c | 2 | ||||
-rw-r--r-- | egg/tests/test-openssl.c | 10 | ||||
-rw-r--r-- | egg/tests/test-padding.c | 4 | ||||
-rw-r--r-- | egg/tests/test-secmem.c | 2 | ||||
-rw-r--r-- | egg/tests/test-spawn.c | 20 | ||||
-rw-r--r-- | egg/tests/test-symkey.c | 6 | ||||
-rw-r--r-- | gck/tests/Makefile.am | 8 | ||||
-rw-r--r-- | gck/tests/test-gck-crypto.c | 8 | ||||
-rw-r--r-- | gck/tests/test-gck-enumerator.c | 8 | ||||
-rw-r--r-- | gck/tests/test-gck-module.c | 10 | ||||
-rw-r--r-- | gck/tests/test-gck-modules.c | 8 | ||||
-rw-r--r-- | gck/tests/test-gck-object.c | 8 | ||||
-rw-r--r-- | gck/tests/test-gck-session.c | 8 | ||||
-rw-r--r-- | gck/tests/test-gck-slot.c | 8 | ||||
-rw-r--r-- | gck/tests/test-gck-uri.c | 6 | ||||
-rw-r--r-- | gcr/tests/Makefile.am | 8 | ||||
-rw-r--r-- | gcr/tests/frob-certificate.c | 2 | ||||
-rw-r--r-- | gcr/tests/frob-key.c | 2 | ||||
-rw-r--r-- | gcr/tests/test-certificate-chain.c | 12 | ||||
-rw-r--r-- | gcr/tests/test-certificate.c | 12 | ||||
-rw-r--r-- | gcr/tests/test-parser.c | 10 | ||||
-rw-r--r-- | gcr/tests/test-pkcs11-certificate.c | 12 | ||||
-rw-r--r-- | gcr/tests/test-simple-certificate.c | 10 | ||||
-rw-r--r-- | gcr/tests/test-trust.c | 10 | ||||
-rw-r--r-- | pkcs11/gkm/tests/Makefile.am | 10 | ||||
-rw-r--r-- | pkcs11/gkm/tests/mock-module.c | 2 | ||||
-rw-r--r-- | pkcs11/gkm/tests/test-data-asn1.c | 2 | ||||
-rw-r--r-- | pkcs11/gkm/tests/test-data-der.c | 12 | ||||
-rw-r--r-- | pkcs11/gkm/tests/test-object.c | 2 | ||||
-rw-r--r-- | pkcs11/gnome2-store/tests/Makefile.am | 56 | ||||
-rw-r--r-- | pkcs11/gnome2-store/tests/check-gnome2-module.c | 68 | ||||
-rw-r--r-- | pkcs11/gnome2-store/tests/files/.gitempty (renamed from pkcs11/gnome2-store/tests/test-data/.gitempty) | 0 | ||||
-rw-r--r-- | pkcs11/gnome2-store/tests/files/data-file-private.store (renamed from pkcs11/gnome2-store/tests/test-data/data-file-private.store) | bin | 494 -> 494 bytes | |||
-rw-r--r-- | pkcs11/gnome2-store/tests/files/data-file-public.store (renamed from pkcs11/gnome2-store/tests/test-data/data-file-public.store) | bin | 216 -> 216 bytes | |||
-rw-r--r-- | pkcs11/gnome2-store/tests/frob-gnome2-file.c (renamed from pkcs11/gnome2-store/tests/dump-gnome2-file.c) | 4 | ||||
-rw-r--r-- | pkcs11/gnome2-store/tests/test-gnome2-file.c | 642 | ||||
-rw-r--r-- | pkcs11/gnome2-store/tests/test-module.c | 34 | ||||
-rw-r--r-- | pkcs11/gnome2-store/tests/unit-test-gnome2-file.c | 592 | ||||
-rw-r--r-- | pkcs11/roots-store/tests/Makefile.am | 40 | ||||
-rw-r--r-- | pkcs11/roots-store/tests/check-roots-module.c (renamed from pkcs11/roots-store/tests/test-module.c) | 44 | ||||
-rw-r--r-- | pkcs11/roots-store/tests/files/RSA_Root_Certificate_1.pem (renamed from pkcs11/roots-store/tests/test-data/RSA_Root_Certificate_1.pem) | 0 | ||||
-rw-r--r-- | pkcs11/roots-store/tests/files/RSA_Security_1024_v3.pem (renamed from pkcs11/roots-store/tests/test-data/RSA_Security_1024_v3.pem) | 0 | ||||
-rw-r--r-- | pkcs11/roots-store/tests/files/RSA_Security_2048_v3.pem (renamed from pkcs11/roots-store/tests/test-data/RSA_Security_2048_v3.pem) | 0 | ||||
-rw-r--r-- | pkcs11/roots-store/tests/files/Thawte_Personal_Basic_CA.pem (renamed from pkcs11/roots-store/tests/test-data/Thawte_Personal_Basic_CA.pem) | 0 | ||||
-rw-r--r-- | pkcs11/roots-store/tests/files/Thawte_Personal_Freemail_CA.pem (renamed from pkcs11/roots-store/tests/test-data/Thawte_Personal_Freemail_CA.pem) | 0 | ||||
-rw-r--r-- | pkcs11/roots-store/tests/files/Thawte_Personal_Premium_CA.pem (renamed from pkcs11/roots-store/tests/test-data/Thawte_Personal_Premium_CA.pem) | 0 | ||||
-rw-r--r-- | pkcs11/roots-store/tests/files/Thawte_Premium_Server_CA.pem (renamed from pkcs11/roots-store/tests/test-data/Thawte_Premium_Server_CA.pem) | 0 | ||||
-rw-r--r-- | pkcs11/roots-store/tests/files/Thawte_Server_CA.pem (renamed from pkcs11/roots-store/tests/test-data/Thawte_Server_CA.pem) | 0 | ||||
-rw-r--r-- | pkcs11/roots-store/tests/files/Thawte_Time_Stamping_CA.pem (renamed from pkcs11/roots-store/tests/test-data/Thawte_Time_Stamping_CA.pem) | 0 | ||||
-rw-r--r-- | pkcs11/roots-store/tests/files/ca-certificates.crt (renamed from pkcs11/roots-store/tests/test-data/ca-certificates.crt) | 0 | ||||
-rw-r--r-- | pkcs11/roots-store/tests/files/cacert.org.pem (renamed from pkcs11/roots-store/tests/test-data/cacert.org.pem) | 0 | ||||
-rw-r--r-- | pkcs11/roots-store/tests/files/der-certificate.crt (renamed from pkcs11/roots-store/tests/test-data/der-certificate.crt) | bin | 747 -> 747 bytes | |||
-rw-r--r-- | pkcs11/secret-store/gkm-secret-binary.c | 4 | ||||
-rw-r--r-- | pkcs11/secret-store/gkm-secret-binary.h | 4 | ||||
-rw-r--r-- | pkcs11/secret-store/gkm-secret-collection.c | 2 | ||||
-rw-r--r-- | pkcs11/secret-store/gkm-secret-textual.c | 4 | ||||
-rw-r--r-- | pkcs11/secret-store/gkm-secret-textual.h | 4 | ||||
-rw-r--r-- | pkcs11/secret-store/tests/Makefile.am | 61 | ||||
-rw-r--r-- | pkcs11/secret-store/tests/files/encrypted.keyring (renamed from pkcs11/secret-store/tests/test-data/encrypted.keyring) | bin | 733 -> 733 bytes | |||
-rw-r--r-- | pkcs11/secret-store/tests/files/plain-bad-number.keyring (renamed from pkcs11/secret-store/tests/test-data/plain-bad-number.keyring) | 0 | ||||
-rw-r--r-- | pkcs11/secret-store/tests/files/plain.keyring (renamed from pkcs11/secret-store/tests/test-data/plain.keyring) | 0 | ||||
-rw-r--r-- | pkcs11/secret-store/tests/mock-secret-module.c (renamed from pkcs11/secret-store/tests/test-secret-module.c) | 55 | ||||
-rw-r--r-- | pkcs11/secret-store/tests/mock-secret-module.h (renamed from pkcs11/secret-store/tests/test-secret-module.h) | 3 | ||||
-rw-r--r-- | pkcs11/secret-store/tests/test-secret-binary.c | 222 | ||||
-rw-r--r-- | pkcs11/secret-store/tests/test-secret-collection.c | 598 | ||||
-rw-r--r-- | pkcs11/secret-store/tests/test-secret-compat.c (renamed from pkcs11/secret-store/tests/unit-test-secret-compat.c) | 31 | ||||
-rw-r--r-- | pkcs11/secret-store/tests/test-secret-data.c (renamed from pkcs11/secret-store/tests/unit-test-secret-data.c) | 56 | ||||
-rw-r--r-- | pkcs11/secret-store/tests/test-secret-fields.c (renamed from pkcs11/secret-store/tests/unit-test-secret-fields.c) | 104 | ||||
-rw-r--r-- | pkcs11/secret-store/tests/test-secret-item.c | 482 | ||||
-rw-r--r-- | pkcs11/secret-store/tests/test-secret-object.c | 302 | ||||
-rw-r--r-- | pkcs11/secret-store/tests/test-secret-search.c (renamed from pkcs11/secret-store/tests/unit-test-secret-search.c) | 173 | ||||
-rw-r--r-- | pkcs11/secret-store/tests/test-secret-textual.c | 205 | ||||
-rw-r--r-- | pkcs11/secret-store/tests/unit-test-secret-binary.c | 200 | ||||
-rw-r--r-- | pkcs11/secret-store/tests/unit-test-secret-collection.c | 563 | ||||
-rw-r--r-- | pkcs11/secret-store/tests/unit-test-secret-item.c | 438 | ||||
-rw-r--r-- | pkcs11/secret-store/tests/unit-test-secret-object.c | 269 | ||||
-rw-r--r-- | pkcs11/secret-store/tests/unit-test-secret-textual.c | 184 | ||||
-rw-r--r-- | pkcs11/ssh-store/gkm-ssh-openssh.c | 5 | ||||
-rw-r--r-- | pkcs11/ssh-store/gkm-ssh-openssh.h | 4 | ||||
-rw-r--r-- | pkcs11/ssh-store/tests/Makefile.am | 58 | ||||
-rw-r--r-- | pkcs11/ssh-store/tests/check-ssh-module.c | 68 | ||||
-rw-r--r-- | pkcs11/ssh-store/tests/files/id_dsa_encrypted (renamed from pkcs11/ssh-store/tests/test-data/id_dsa_encrypted) | 0 | ||||
-rw-r--r-- | pkcs11/ssh-store/tests/files/id_dsa_encrypted.pub (renamed from pkcs11/ssh-store/tests/test-data/id_dsa_encrypted.pub) | 0 | ||||
-rw-r--r-- | pkcs11/ssh-store/tests/files/id_dsa_plain (renamed from pkcs11/ssh-store/tests/test-data/id_dsa_plain) | 0 | ||||
-rw-r--r-- | pkcs11/ssh-store/tests/files/id_dsa_plain.pub (renamed from pkcs11/ssh-store/tests/test-data/id_dsa_plain.pub) | 0 | ||||
-rw-r--r-- | pkcs11/ssh-store/tests/files/id_dsa_test.pub (renamed from pkcs11/ssh-store/tests/test-data/id_dsa_test.pub) | 0 | ||||
-rw-r--r-- | pkcs11/ssh-store/tests/files/id_rsa_encrypted (renamed from pkcs11/ssh-store/tests/test-data/id_rsa_encrypted) | 0 | ||||
-rw-r--r-- | pkcs11/ssh-store/tests/files/id_rsa_encrypted.pub (renamed from pkcs11/ssh-store/tests/test-data/id_rsa_encrypted.pub) | 0 | ||||
-rw-r--r-- | pkcs11/ssh-store/tests/files/id_rsa_plain (renamed from pkcs11/ssh-store/tests/test-data/id_rsa_plain) | 0 | ||||
-rw-r--r-- | pkcs11/ssh-store/tests/files/id_rsa_plain.pub (renamed from pkcs11/ssh-store/tests/test-data/id_rsa_plain.pub) | 0 | ||||
-rw-r--r-- | pkcs11/ssh-store/tests/files/id_rsa_test.pub (renamed from pkcs11/ssh-store/tests/test-data/id_rsa_test.pub) | 0 | ||||
-rw-r--r-- | pkcs11/ssh-store/tests/mock-ssh-module.c (renamed from pkcs11/ssh-store/tests/test-ssh-module.c) | 13 | ||||
-rw-r--r-- | pkcs11/ssh-store/tests/mock-ssh-module.h (renamed from pkcs11/ssh-store/tests/test-ssh-module.h) | 0 | ||||
-rw-r--r-- | pkcs11/ssh-store/tests/test-private-key.c (renamed from pkcs11/ssh-store/tests/unit-test-private-key.c) | 67 | ||||
-rw-r--r-- | pkcs11/ssh-store/tests/test-ssh-openssh.c (renamed from pkcs11/ssh-store/tests/unit-test-ssh-openssh.c) | 49 | ||||
-rw-r--r-- | pkcs11/wrap-layer/tests/Makefile.am | 52 | ||||
-rw-r--r-- | pkcs11/wrap-layer/tests/mock-secret-store.c | 4 | ||||
-rw-r--r-- | pkcs11/wrap-layer/tests/test-create-credential.c | 88 | ||||
-rw-r--r-- | pkcs11/wrap-layer/tests/test-init-pin.c | 60 | ||||
-rw-r--r-- | pkcs11/wrap-layer/tests/test-login-auto.c | 129 | ||||
-rw-r--r-- | pkcs11/wrap-layer/tests/test-login-hints.c | 19 | ||||
-rw-r--r-- | pkcs11/wrap-layer/tests/test-login-keyring.c | 92 | ||||
-rw-r--r-- | pkcs11/wrap-layer/tests/test-login-specific.c | 74 | ||||
-rw-r--r-- | pkcs11/wrap-layer/tests/test-login-user.c | 94 | ||||
-rw-r--r-- | pkcs11/wrap-layer/tests/test-set-pin.c | 58 | ||||
-rw-r--r-- | pkcs11/xdg-store/tests/Makefile.am | 65 | ||||
-rw-r--r-- | pkcs11/xdg-store/tests/check-xdg-module.c | 68 | ||||
-rw-r--r-- | pkcs11/xdg-store/tests/files/test-certificate-1.cer (renamed from pkcs11/xdg-store/tests/test-data/test-certificate-1.cer) | bin | 813 -> 813 bytes | |||
-rw-r--r-- | pkcs11/xdg-store/tests/files/test-certificate-2.cer (renamed from pkcs11/xdg-store/tests/test-data/test-certificate-2.cer) | bin | 813 -> 813 bytes | |||
-rw-r--r-- | pkcs11/xdg-store/tests/files/test-refer-1.trust (renamed from pkcs11/xdg-store/tests/test-data/test-refer-1.trust) | bin | 241 -> 241 bytes | |||
-rw-r--r-- | pkcs11/xdg-store/tests/frob-trust-file.c (renamed from pkcs11/xdg-store/tests/diddle-trust-file.c) | 0 | ||||
-rw-r--r-- | pkcs11/xdg-store/tests/mock-xdg-module.c | 214 | ||||
-rw-r--r-- | pkcs11/xdg-store/tests/mock-xdg-module.h (renamed from pkcs11/xdg-store/tests/test-xdg-module.h) | 24 | ||||
-rw-r--r-- | pkcs11/xdg-store/tests/test-xdg-module.c | 257 | ||||
-rw-r--r-- | pkcs11/xdg-store/tests/test-xdg-trust.c | 352 | ||||
-rw-r--r-- | testing/Makefile.am | 18 | ||||
-rwxr-xr-x | testing/testing-build.sh | 139 | ||||
-rw-r--r-- | testing/testing.c | 394 | ||||
-rw-r--r-- | testing/testing.h | 104 | ||||
-rw-r--r-- | testing/testing.make | 77 | ||||
-rw-r--r-- | ui/tests/Makefile.am | 35 | ||||
-rw-r--r-- | ui/tests/files/prompt-empty (renamed from ui/tests/test-data/prompt-empty) | 0 | ||||
-rw-r--r-- | ui/tests/files/prompt-full (renamed from ui/tests/test-data/prompt-full) | 0 | ||||
-rw-r--r-- | ui/tests/files/prompt-test (renamed from ui/tests/test-data/prompt-test) | 0 | ||||
-rw-r--r-- | ui/tests/test-util.c (renamed from ui/tests/unit-test-util.c) | 72 |
141 files changed, 4516 insertions, 4100 deletions
@@ -95,6 +95,11 @@ run-tests /egg/tests/test-spawn /egg/tests/test-symkey +/daemon/control/tests/frob-control-change +/daemon/control/tests/frob-control-init +/daemon/control/tests/frob-control-quit +/daemon/control/tests/frob-control-unlock + /gck/tests/test-gck-attributes /gck/tests/test-gck-crypto /gck/tests/test-gck-enumerator @@ -129,6 +134,43 @@ run-tests /pkcs11/gkm/tests/test-timer /pkcs11/gkm/tests/test-transaction +/pkcs11/gnome2-store/tests/frob-gnome2-file +/pkcs11/gnome2-store/tests/test-gnome2-file +/pkcs11/gnome2-store/tests/check-gnome2-module +/pkcs11/gnome2-store/tests/check-module + +/pkcs11/roots-store/tests/check-roots-module + +/pkcs11/secret-store/tests/test-secret-binary +/pkcs11/secret-store/tests/test-secret-collection +/pkcs11/secret-store/tests/test-secret-compat +/pkcs11/secret-store/tests/test-secret-data +/pkcs11/secret-store/tests/test-secret-fields +/pkcs11/secret-store/tests/test-secret-item +/pkcs11/secret-store/tests/test-secret-object +/pkcs11/secret-store/tests/test-secret-search +/pkcs11/secret-store/tests/test-secret-textual + +/pkcs11/ssh-store/tests/check-ssh-module +/pkcs11/ssh-store/tests/test-private-key +/pkcs11/ssh-store/tests/test-ssh-openssh + +/pkcs11/wrap-layer/tests/test-create-credential +/pkcs11/wrap-layer/tests/test-init-pin +/pkcs11/wrap-layer/tests/test-login-auto +/pkcs11/wrap-layer/tests/test-login-hints +/pkcs11/wrap-layer/tests/test-login-keyring +/pkcs11/wrap-layer/tests/test-login-specific +/pkcs11/wrap-layer/tests/test-login-user +/pkcs11/wrap-layer/tests/test-set-pin + +/pkcs11/xdg-store/tests/check-xdg-module +/pkcs11/xdg-store/tests/frob-trust-file +/pkcs11/xdg-store/tests/test-xdg-module +/pkcs11/xdg-store/tests/test-xdg-trust + +/ui/tests/test-util + /daemon/dbus/tests/test-secret-util /ui/gnome-keyring-prompt.desktop diff --git a/Makefile.am b/Makefile.am index b14f7a8e..b560ab43 100644 --- a/Makefile.am +++ b/Makefile.am @@ -36,8 +36,8 @@ EXTRA_DIST = \ DISTCHECK_CONFIGURE_FLAGS = \ --enable-gtk-doc \ - --enable-tests \ - --disable-debug \ + --enable-tests=full \ + --enable-debug=yes \ --disable-gcov DISTCLEANFILES = \ diff --git a/daemon/control/tests/Makefile.am b/daemon/control/tests/Makefile.am index 2ab531a4..5d08d9f2 100644 --- a/daemon/control/tests/Makefile.am +++ b/daemon/control/tests/Makefile.am @@ -12,45 +12,45 @@ LIBS = \ $(P11_TESTS_LIBS) noinst_PROGRAMS= \ - test-control-change \ - test-control-init \ - test-control-unlock \ - test-control-quit + frob-control-change \ + frob-control-init \ + frob-control-unlock \ + frob-control-quit # ------------------------------------------------------------------------------ # Test unlocking the login keyring -test_control_change_SOURCES = \ - test-control-change.c +frob_control_change_SOURCES = \ + frob-control-change.c -test_control_change_LDADD = \ +frob_control_change_LDADD = \ $(top_builddir)/daemon/control/libgkd-control-client.la \ $(top_builddir)/egg/libegg-buffer.la \ $(top_builddir)/egg/libegg-creds.la \ $(top_builddir)/egg/libegg-secure.la -test_control_init_SOURCES = \ - test-control-init.c +frob_control_init_SOURCES = \ + frob-control-init.c -test_control_init_LDADD = \ +frob_control_init_LDADD = \ $(top_builddir)/daemon/control/libgkd-control-client.la \ $(top_builddir)/egg/libegg-buffer.la \ $(top_builddir)/egg/libegg-creds.la \ $(top_builddir)/egg/libegg-secure.la -test_control_quit_SOURCES = \ - test-control-quit.c +frob_control_quit_SOURCES = \ + frob-control-quit.c -test_control_quit_LDADD = \ +frob_control_quit_LDADD = \ $(top_builddir)/daemon/control/libgkd-control-client.la \ $(top_builddir)/egg/libegg-buffer.la \ $(top_builddir)/egg/libegg-creds.la \ $(top_builddir)/egg/libegg-secure.la -test_control_unlock_SOURCES = \ - test-control-unlock.c +frob_control_unlock_SOURCES = \ + frob-control-unlock.c -test_control_unlock_LDADD = \ +frob_control_unlock_LDADD = \ $(top_builddir)/daemon/control/libgkd-control-client.la \ $(top_builddir)/egg/libegg-buffer.la \ $(top_builddir)/egg/libegg-creds.la \ diff --git a/daemon/control/tests/test-control-change.c b/daemon/control/tests/frob-control-change.c index b910f777..9b9c9ba4 100644 --- a/daemon/control/tests/test-control-change.c +++ b/daemon/control/tests/frob-control-change.c @@ -1,12 +1,15 @@ #include "control/gkd-control.h" -#include "testing/testing.h" + +#include "egg/egg-secure-memory.h" #include <pwd.h> #include <unistd.h> -static int -run (void) +EGG_SECURE_GLIB_DEFINITIONS (); + +int +main (int argc, char *argv[]) { gchar *original; const char *password; @@ -26,5 +29,3 @@ run (void) g_free (original); return 0; } - -#include "testing/testing.c" diff --git a/daemon/control/tests/test-control-init.c b/daemon/control/tests/frob-control-init.c index 4b1d6b4e..3e83d03d 100644 --- a/daemon/control/tests/test-control-init.c +++ b/daemon/control/tests/frob-control-init.c @@ -1,13 +1,16 @@ #include "control/gkd-control.h" -#include "testing/testing.h" + +#include "egg/egg-secure-memory.h" #include <pwd.h> #include <stdlib.h> #include <unistd.h> -static int -run (void) +EGG_SECURE_GLIB_DEFINITIONS (); + +int +main (int argc, char *argv[]) { const char *directory; const gchar *env[] = { NULL }; @@ -26,5 +29,3 @@ run (void) return 0; } - -#include "testing/testing.c" diff --git a/daemon/control/tests/test-control-quit.c b/daemon/control/tests/frob-control-quit.c index f3ec3cc7..bf73a4e9 100644 --- a/daemon/control/tests/test-control-quit.c +++ b/daemon/control/tests/frob-control-quit.c @@ -1,13 +1,16 @@ #include "control/gkd-control.h" -#include "testing/testing.h" + +#include "egg/egg-secure-memory.h" #include <pwd.h> #include <stdlib.h> #include <unistd.h> -static int -run (void) +EGG_SECURE_GLIB_DEFINITIONS (); + +int +main (int argc, char *argv[]) { const char *directory; @@ -21,5 +24,3 @@ run (void) return 0; } - -#include "testing/testing.c" diff --git a/daemon/control/tests/test-control-unlock.c b/daemon/control/tests/frob-control-unlock.c index ba6cca4b..b3610660 100644 --- a/daemon/control/tests/test-control-unlock.c +++ b/daemon/control/tests/frob-control-unlock.c @@ -1,12 +1,15 @@ #include "control/gkd-control.h" -#include "testing/testing.h" + +#include "egg/egg-secure-memory.h" #include <pwd.h> #include <unistd.h> -static int -run (void) +EGG_SECURE_GLIB_DEFINITIONS (); + +int +main (int argc, char *argv[]) { const char *password; const char *directory; @@ -20,5 +23,3 @@ run (void) gkd_control_unlock (directory, password); return 0; } - -#include "testing/testing.c" diff --git a/daemon/dbus/tests/Makefile.am b/daemon/dbus/tests/Makefile.am index d5f7eb85..a4d4bf4d 100644 --- a/daemon/dbus/tests/Makefile.am +++ b/daemon/dbus/tests/Makefile.am @@ -1,7 +1,7 @@ INCLUDES = \ -I$(top_srcdir)/daemon/dbus \ - -DSRCDIR=$(srcdir) \ + -DSRCDIR="\"$(srcdir)\"" \ $(DAEMON_CFLAGS) \ $(GLIB_CFLAGS) @@ -14,7 +14,7 @@ TEST_PROGS = \ check_PROGRAMS = $(TEST_PROGS) test: $(TEST_PROGS) - SRCDIR='$(srcdir)' gtester -k --verbose -m $(TEST_MODE) --g-fatal-warnings $(TEST_PROGS) + gtester -k --verbose -m $(TEST_MODE) --g-fatal-warnings $(TEST_PROGS) check-local: test diff --git a/egg/tests/Makefile.am b/egg/tests/Makefile.am index 72f0d377..6de8baf3 100644 --- a/egg/tests/Makefile.am +++ b/egg/tests/Makefile.am @@ -3,8 +3,9 @@ asn1-def-test.c: test.asn $(ASN1PARSER) -o asn1-def-test.c $(srcdir)/test.asn INCLUDES = \ - -I$(top_srcdir)/egg \ - -DSRCDIR=$(srcdir) \ + -I$(top_builddir) \ + -I$(top_srcdir) \ + -DSRCDIR="\"$(srcdir)\"" \ $(GLIB_CFLAGS) LDADD = \ @@ -34,7 +35,7 @@ test_asn1_SOURCES = \ check_PROGRAMS = $(TEST_PROGS) test: $(TEST_PROGS) - SRCDIR='$(srcdir)' gtester -k --verbose -m $(TEST_MODE) --g-fatal-warnings $(TEST_PROGS) + gtester -k --verbose -m $(TEST_MODE) --g-fatal-warnings $(TEST_PROGS) check-local: test @@ -44,6 +45,9 @@ EXTRA_DIST = \ test.asn \ files +DISTCLEANFILES = \ + asn1-def-test.c + # ------------------------------------------------------------------------------ noinst_PROGRAMS = \ diff --git a/egg/tests/test-asn1.c b/egg/tests/test-asn1.c index 39f188dc..cf855dab 100644 --- a/egg/tests/test-asn1.c +++ b/egg/tests/test-asn1.c @@ -23,9 +23,9 @@ #include "config.h" -#include "egg-asn1x.h" -#include "egg-asn1-defs.h" -#include "egg-testing.h" +#include "egg/egg-asn1x.h" +#include "egg/egg-asn1-defs.h" +#include "egg/egg-testing.h" #include <glib.h> #include <libtasn1.h> @@ -726,8 +726,8 @@ typedef struct { static void setup (Test *test, gconstpointer unused) { - if (!g_file_get_contents ("files/test-certificate-1.der", (gchar**)&test->data, - &test->n_data, NULL)) + if (!g_file_get_contents (SRCDIR "/files/test-certificate-1.der", + (gchar**)&test->data, &test->n_data, NULL)) g_assert_not_reached (); test->asn1 = egg_asn1x_create (pkix_asn1_tab, "Certificate"); diff --git a/egg/tests/test-asn1x.c b/egg/tests/test-asn1x.c index 553ad6f9..7556e5de 100644 --- a/egg/tests/test-asn1x.c +++ b/egg/tests/test-asn1x.c @@ -21,15 +21,16 @@ Author: Stef Walter <stef@memberwebs.com> */ -#include "egg-asn1x.h" +#include "config.h" + +#include "egg/egg-asn1x.h" +#include "egg/egg-asn1-defs.h" +#include "egg/egg-testing.h" #include <pwd.h> #include <stdlib.h> #include <unistd.h> -#include "egg-asn1-defs.h" -#include "egg-testing.h" - #if 0 static void build_personal_name (void) @@ -101,13 +102,13 @@ main (int argc, char **argv) egg_tests_chdir_base (argv[0]); - test_some_asn1_stuff (pkix_asn1_tab, "files/test-certificate-1.der", "Certificate"); - test_some_asn1_stuff (pkix_asn1_tab, "files/test-pkcs8-1.der", "pkcs-8-PrivateKeyInfo"); - test_some_asn1_stuff (pk_asn1_tab, "files/test-rsakey-1.der", "RSAPrivateKey"); - test_some_asn1_stuff (pkix_asn1_tab, "files/test-personalname-1.der", "PersonalName"); - test_some_asn1_stuff (pkix_asn1_tab, "files/test-pkcs7-1.der", "pkcs-7-ContentInfo"); - test_some_asn1_stuff (pkix_asn1_tab, "files/test-pkcs7-2.der", "pkcs-7-ContentInfo"); - test_some_asn1_stuff (pkix_asn1_tab, "files/test-pkcs12-1.der", "pkcs-12-PFX"); + test_some_asn1_stuff (pkix_asn1_tab, SRCDIR "/files/test-certificate-1.der", "Certificate"); + test_some_asn1_stuff (pkix_asn1_tab, SRCDIR "/files/test-pkcs8-1.der", "pkcs-8-PrivateKeyInfo"); + test_some_asn1_stuff (pk_asn1_tab, SRCDIR "/files/test-rsakey-1.der", "RSAPrivateKey"); + test_some_asn1_stuff (pkix_asn1_tab, SRCDIR "/files/test-personalname-1.der", "PersonalName"); + test_some_asn1_stuff (pkix_asn1_tab, SRCDIR "/files/test-pkcs7-1.der", "pkcs-7-ContentInfo"); + test_some_asn1_stuff (pkix_asn1_tab, SRCDIR "/files/test-pkcs7-2.der", "pkcs-7-ContentInfo"); + test_some_asn1_stuff (pkix_asn1_tab, SRCDIR "/files/test-pkcs12-1.der", "pkcs-12-PFX"); return 0; } diff --git a/egg/tests/test-cleanup.c b/egg/tests/test-cleanup.c index 90e3477b..0a89f19b 100644 --- a/egg/tests/test-cleanup.c +++ b/egg/tests/test-cleanup.c @@ -25,7 +25,7 @@ #include <stdio.h> #include <string.h> -#include "egg-cleanup.h" +#include "egg/egg-cleanup.h" #define DATA "some string" diff --git a/egg/tests/test-dh.c b/egg/tests/test-dh.c index 58098ccc..ba9fcfc7 100644 --- a/egg/tests/test-dh.c +++ b/egg/tests/test-dh.c @@ -23,9 +23,9 @@ #include "config.h" -#include "egg-dh.h" -#include "egg-secure-memory.h" -#include "egg-testing.h" +#include "egg/egg-dh.h" +#include "egg/egg-secure-memory.h" +#include "egg/egg-testing.h" #include <stdlib.h> #include <stdio.h> diff --git a/egg/tests/test-dn.c b/egg/tests/test-dn.c index eae139fe..98f5a43d 100644 --- a/egg/tests/test-dn.c +++ b/egg/tests/test-dn.c @@ -23,10 +23,10 @@ #include "config.h" -#include "egg-asn1-defs.h" -#include "egg-asn1x.h" -#include "egg-dn.h" -#include "egg-oid.h" +#include "egg/egg-asn1-defs.h" +#include "egg/egg-asn1x.h" +#include "egg/egg-dn.h" +#include "egg/egg-oid.h" #include <glib.h> #include <gcrypt.h> @@ -45,8 +45,8 @@ typedef struct { static void setup (Test *test, gconstpointer unused) { - if (!g_file_get_contents ("files/test-certificate-1.der", (gchar**)&test->data, - &test->n_data, NULL)) + if (!g_file_get_contents (SRCDIR "/files/test-certificate-1.der", + (gchar**)&test->data, &test->n_data, NULL)) g_assert_not_reached (); test->asn1 = egg_asn1x_create (pkix_asn1_tab, "Certificate"); diff --git a/egg/tests/test-hex.c b/egg/tests/test-hex.c index 713bf609..e2cb9f06 100644 --- a/egg/tests/test-hex.c +++ b/egg/tests/test-hex.c @@ -23,7 +23,7 @@ #include "config.h" -#include "egg-hex.h" +#include "egg/egg-hex.h" #include <stdlib.h> #include <stdio.h> diff --git a/egg/tests/test-hkdf.c b/egg/tests/test-hkdf.c index 44c463d3..93f16dfa 100644 --- a/egg/tests/test-hkdf.c +++ b/egg/tests/test-hkdf.c @@ -27,9 +27,9 @@ #include <stdio.h> #include <string.h> -#include "egg-hkdf.h" -#include "egg-secure-memory.h" -#include "egg-testing.h" +#include "egg/egg-hkdf.h" +#include "egg/egg-secure-memory.h" +#include "egg/egg-testing.h" #include <gcrypt.h> diff --git a/egg/tests/test-oid.c b/egg/tests/test-oid.c index 189a2009..37b0e2e8 100644 --- a/egg/tests/test-oid.c +++ b/egg/tests/test-oid.c @@ -22,7 +22,7 @@ #include "config.h" -#include "egg-oid.h" +#include "egg/egg-oid.h" #include <glib.h> diff --git a/egg/tests/test-openssl.c b/egg/tests/test-openssl.c index 18f9fd92..e80c1b14 100644 --- a/egg/tests/test-openssl.c +++ b/egg/tests/test-openssl.c @@ -23,10 +23,10 @@ #include "config.h" -#include "egg-symkey.h" -#include "egg-openssl.h" -#include "egg-secure-memory.h" -#include "egg-testing.h" +#include "egg/egg-symkey.h" +#include "egg/egg-openssl.h" +#include "egg/egg-secure-memory.h" +#include "egg/egg-testing.h" #include <glib.h> @@ -51,7 +51,7 @@ typedef struct { static void setup (Test *test, gconstpointer unused) { - if (!g_file_get_contents ("files/pem-rsa-enc.key", (gchar**)&test->input, &test->n_input, NULL)) + if (!g_file_get_contents (SRCDIR "/files/pem-rsa-enc.key", (gchar**)&test->input, &test->n_input, NULL)) g_assert_not_reached (); } diff --git a/egg/tests/test-padding.c b/egg/tests/test-padding.c index 236d9411..10a6c0eb 100644 --- a/egg/tests/test-padding.c +++ b/egg/tests/test-padding.c @@ -23,8 +23,8 @@ #include "config.h" -#include "egg-padding.h" -#include "egg-testing.h" +#include "egg/egg-padding.h" +#include "egg/egg-testing.h" #include <gcrypt.h> diff --git a/egg/tests/test-secmem.c b/egg/tests/test-secmem.c index ddd74a18..b5ee4651 100644 --- a/egg/tests/test-secmem.c +++ b/egg/tests/test-secmem.c @@ -23,7 +23,7 @@ #include "config.h" -#include "egg-secure-memory.h" +#include "egg/egg-secure-memory.h" #include <glib.h> diff --git a/egg/tests/test-spawn.c b/egg/tests/test-spawn.c index 07488c01..e0ac386e 100644 --- a/egg/tests/test-spawn.c +++ b/egg/tests/test-spawn.c @@ -23,8 +23,8 @@ #include "config.h" -#include "egg-spawn.h" -#include "egg-testing.h" +#include "egg/egg-spawn.h" +#include "egg/egg-testing.h" #include <sys/wait.h> @@ -176,7 +176,7 @@ test_sync (void) data.parent_pid = getpid(); data.index = 80; - ret = egg_spawn_sync_with_callbacks ("./files", + ret = egg_spawn_sync_with_callbacks (SRCDIR "/files", echo_argv, NULL, 0, &pid, &echo_callbacks, &data, &exit_status, &error); @@ -197,7 +197,7 @@ test_sync_error (void) GError *error = NULL; gboolean ret; - ret = egg_spawn_sync_with_callbacks ("./files", + ret = egg_spawn_sync_with_callbacks (SRCDIR "/files", error_argv, NULL, 0, NULL, NULL, NULL, NULL, &error); @@ -220,7 +220,7 @@ test_async (void) data.index = 80; data.is_async = TRUE; - ret = egg_spawn_async_with_callbacks ("./files", + ret = egg_spawn_async_with_callbacks (SRCDIR "/files", echo_argv, NULL, 0, &pid, &echo_callbacks, &data, NULL, &error); @@ -247,7 +247,7 @@ test_async_none (void) data.parent_pid = getpid(); data.is_async = TRUE; - ret = egg_spawn_async_with_callbacks ("./files", + ret = egg_spawn_async_with_callbacks (SRCDIR "/files", echo_argv, NULL, 0, NULL, &null_callbacks, &data, NULL, &error); @@ -268,7 +268,7 @@ test_async_error (void) GError *error = NULL; guint ret; - ret = egg_spawn_async_with_callbacks ("./files", + ret = egg_spawn_async_with_callbacks (SRCDIR "/files", error_argv, NULL, 0, NULL, NULL, NULL, NULL, &error); @@ -280,14 +280,8 @@ test_async_error (void) int main (int argc, char **argv) { - const gchar *srcdir; - g_test_init (&argc, &argv, NULL); - srcdir = g_getenv ("SRCDIR"); - if (srcdir && chdir (srcdir) < 0) - g_error ("couldn't change directory to: %s: %s", srcdir, g_strerror (errno)); - g_test_add_func ("/spawn/sync", test_sync); g_test_add_func ("/spawn/sync_error", test_sync_error); g_test_add_func ("/spawn/async", test_async); diff --git a/egg/tests/test-symkey.c b/egg/tests/test-symkey.c index c2c1b781..b65ee302 100644 --- a/egg/tests/test-symkey.c +++ b/egg/tests/test-symkey.c @@ -23,9 +23,9 @@ #include "config.h" -#include "egg-libgcrypt.h" -#include "egg-secure-memory.h" -#include "egg-symkey.h" +#include "egg/egg-libgcrypt.h" +#include "egg/egg-secure-memory.h" +#include "egg/egg-symkey.h" #include <gcrypt.h> diff --git a/gck/tests/Makefile.am b/gck/tests/Makefile.am index b06a1e46..e43f2e68 100644 --- a/gck/tests/Makefile.am +++ b/gck/tests/Makefile.am @@ -1,7 +1,9 @@ INCLUDES = \ - -I$(top_srcdir)/egg \ - -DSRCDIR=$(srcdir) \ + -I$(top_builddir) \ + -I$(top_srcdir) \ + -DSRCDIR="\"$(srcdir)\"" \ + -DBUILDDIR="\"$(builddir)\"" \ -DGCK_API_SUBJECT_TO_CHANGE \ $(GLIB_CFLAGS) @@ -28,7 +30,7 @@ TEST_PROGS = \ check_PROGRAMS = $(TEST_PROGS) test: $(TEST_PROGS) - SRCDIR='$(srcdir)' gtester -k --verbose -m $(TEST_MODE) --g-fatal-warnings $(TEST_PROGS) + gtester -k --verbose -m $(TEST_MODE) --g-fatal-warnings $(TEST_PROGS) check-local: test diff --git a/gck/tests/test-gck-crypto.c b/gck/tests/test-gck-crypto.c index d6dfc9e8..208673fe 100644 --- a/gck/tests/test-gck-crypto.c +++ b/gck/tests/test-gck-crypto.c @@ -57,7 +57,7 @@ setup (Test *test, gconstpointer unused) GckSlot *slot; /* Successful load */ - test->module = gck_module_initialize (".libs/libmock-test-module.so", NULL, 0, &err); + test->module = gck_module_initialize (BUILDDIR "/.libs/libmock-test-module.so", NULL, 0, &err); g_assert_no_error (err); g_assert (GCK_IS_MODULE (test->module)); @@ -627,15 +627,9 @@ test_derive_key (Test *test, gconstpointer unused) int main (int argc, char **argv) { - const gchar *srcdir; - g_type_init (); g_test_init (&argc, &argv, NULL); - srcdir = g_getenv ("SRCDIR"); - if (srcdir && chdir (srcdir) < 0) - g_error ("couldn't change directory to: %s: %s", srcdir, g_strerror (errno)); - g_test_add ("/gck/crypto/encrypt", Test, NULL, setup, test_encrypt, teardown); g_test_add ("/gck/crypto/decrypt", Test, NULL, setup, test_decrypt, teardown); g_test_add ("/gck/crypto/login_context_specific", Test, NULL, setup, test_login_context_specific, teardown); diff --git a/gck/tests/test-gck-enumerator.c b/gck/tests/test-gck-enumerator.c index a92128b2..83458f04 100644 --- a/gck/tests/test-gck-enumerator.c +++ b/gck/tests/test-gck-enumerator.c @@ -48,7 +48,7 @@ setup (Test *test, gconstpointer unused) GError *err = NULL; /* Successful load */ - test->module = gck_module_initialize (".libs/libmock-test-module.so", NULL, 0, &err); + test->module = gck_module_initialize (BUILDDIR "/.libs/libmock-test-module.so", NULL, 0, &err); g_assert_no_error (err); g_assert (GCK_IS_MODULE (test->module)); @@ -266,15 +266,9 @@ test_token_match (Test *test, gconstpointer unused) int main (int argc, char **argv) { - const gchar *srcdir; - g_type_init (); g_test_init (&argc, &argv, NULL); - srcdir = g_getenv ("SRCDIR"); - if (srcdir && chdir (srcdir) < 0) - g_error ("couldn't change directory to: %s: %s", srcdir, g_strerror (errno)); - g_test_add ("/gck/enumerator/create", Test, NULL, setup, test_create, teardown); g_test_add ("/gck/enumerator/create_slots", Test, NULL, setup, test_create_slots, teardown); g_test_add ("/gck/enumerator/next", Test, NULL, setup, test_next, teardown); diff --git a/gck/tests/test-gck-module.c b/gck/tests/test-gck-module.c index 48050b22..4e6a3372 100644 --- a/gck/tests/test-gck-module.c +++ b/gck/tests/test-gck-module.c @@ -40,7 +40,7 @@ setup (Test *test, gconstpointer unused) GError *err = NULL; /* Successful load */ - test->module = gck_module_initialize (".libs/libmock-test-module.so", NULL, 0, &err); + test->module = gck_module_initialize (BUILDDIR "/.libs/libmock-test-module.so", NULL, 0, &err); g_assert_no_error (err); g_assert (test->module); } @@ -101,7 +101,7 @@ test_module_props (Test *test, gconstpointer unused) g_object_get (test->module, "path", &path, NULL); g_assert (path != NULL && "no module-path"); - g_assert (strcmp (".libs/libmock-test-module.so", path) == 0 && "module path wrong"); + g_assert (strcmp (BUILDDIR "/.libs/libmock-test-module.so", path) == 0 && "module path wrong"); g_free (path); } @@ -127,15 +127,9 @@ test_module_info (Test *test, gconstpointer unused) int main (int argc, char **argv) { - const gchar *srcdir; - g_type_init (); g_test_init (&argc, &argv, NULL); - srcdir = g_getenv ("SRCDIR"); - if (srcdir && chdir (srcdir) < 0) - g_error ("couldn't change directory to: %s: %s", srcdir, g_strerror (errno)); - g_test_add ("/gck/module/invalid_modules", Test, NULL, setup, test_invalid_modules, teardown); g_test_add ("/gck/module/module_equals_hash", Test, NULL, setup, test_module_equals_hash, teardown); g_test_add ("/gck/module/module_props", Test, NULL, setup, test_module_props, teardown); diff --git a/gck/tests/test-gck-modules.c b/gck/tests/test-gck-modules.c index 0fe287e2..3b38d05d 100644 --- a/gck/tests/test-gck-modules.c +++ b/gck/tests/test-gck-modules.c @@ -48,7 +48,7 @@ setup (Test *test, gconstpointer unused) GError *err = NULL; /* Successful load */ - module = gck_module_initialize (".libs/libmock-test-module.so", NULL, 0, &err); + module = gck_module_initialize (BUILDDIR "/.libs/libmock-test-module.so", NULL, 0, &err); g_assert_no_error (err); g_assert (GCK_IS_MODULE (module)); @@ -193,15 +193,9 @@ test_enumerate_uri (Test *test, gconstpointer unused) int main (int argc, char **argv) { - const gchar *srcdir; - g_type_init (); g_test_init (&argc, &argv, NULL); - srcdir = g_getenv ("SRCDIR"); - if (srcdir && chdir (srcdir) < 0) - g_error ("couldn't change directory to: %s: %s", srcdir, g_strerror (errno)); - g_test_add ("/gck/modules/enumerate_objects", Test, NULL, setup, test_enumerate_objects, teardown); g_test_add ("/gck/modules/token_for_uri", Test, NULL, setup, test_token_for_uri, teardown); g_test_add ("/gck/modules/token_for_uri_not_found", Test, NULL, setup, test_token_for_uri_not_found, teardown); diff --git a/gck/tests/test-gck-object.c b/gck/tests/test-gck-object.c index e41e4dbc..672a102a 100644 --- a/gck/tests/test-gck-object.c +++ b/gck/tests/test-gck-object.c @@ -50,7 +50,7 @@ setup (Test *test, gconstpointer unused) GList *slots; /* Successful load */ - test->module = gck_module_initialize (".libs/libmock-test-module.so", NULL, 0, &err); + test->module = gck_module_initialize (BUILDDIR "/.libs/libmock-test-module.so", NULL, 0, &err); g_assert_no_error (err); g_assert (GCK_IS_MODULE (test->module)); @@ -403,15 +403,9 @@ test_find_objects (Test *test, gconstpointer unused) int main (int argc, char **argv) { - const gchar *srcdir; - g_type_init (); g_test_init (&argc, &argv, NULL); - srcdir = g_getenv ("SRCDIR"); - if (srcdir && chdir (srcdir) < 0) - g_error ("couldn't change directory to: %s: %s", srcdir, g_strerror (errno)); - g_test_add ("/gck/object/object_props", Test, NULL, setup, test_object_props, teardown); g_test_add ("/gck/object/object_equals_hash", Test, NULL, setup, test_object_equals_hash, teardown); g_test_add ("/gck/object/create_object", Test, NULL, setup, test_create_object, teardown); diff --git a/gck/tests/test-gck-session.c b/gck/tests/test-gck-session.c index 5cc15640..356819bb 100644 --- a/gck/tests/test-gck-session.c +++ b/gck/tests/test-gck-session.c @@ -48,7 +48,7 @@ setup (Test *test, gconstpointer unused) GList *slots; /* Successful load */ - test->module = gck_module_initialize (".libs/libmock-test-module.so", NULL, 0, &err); + test->module = gck_module_initialize (BUILDDIR "/.libs/libmock-test-module.so", NULL, 0, &err); g_assert_no_error (err); g_assert (GCK_IS_MODULE (test->module)); @@ -303,15 +303,9 @@ test_auto_login (Test *test, gconstpointer unused) int main (int argc, char **argv) { - const gchar *srcdir; - g_type_init (); g_test_init (&argc, &argv, NULL); - srcdir = g_getenv ("SRCDIR"); - if (srcdir && chdir (srcdir) < 0) - g_error ("couldn't change directory to: %s: %s", srcdir, g_strerror (errno)); - g_test_add ("/gck/session/session_props", Test, NULL, setup, test_session_props, teardown); g_test_add ("/gck/session/session_info", Test, NULL, setup, test_session_info, teardown); g_test_add ("/gck/session/open_close_session", Test, NULL, setup, test_open_close_session, teardown); diff --git a/gck/tests/test-gck-slot.c b/gck/tests/test-gck-slot.c index e36dc6de..8a61479e 100644 --- a/gck/tests/test-gck-slot.c +++ b/gck/tests/test-gck-slot.c @@ -43,7 +43,7 @@ setup (Test *test, gconstpointer unused) GList *slots; /* Successful load */ - test->module = gck_module_initialize (".libs/libmock-test-module.so", NULL, 0, &err); + test->module = gck_module_initialize (BUILDDIR "/.libs/libmock-test-module.so", NULL, 0, &err); g_assert_no_error (err); g_assert (GCK_IS_MODULE (test->module)); @@ -242,15 +242,9 @@ test_token_info_match_different (Test *test, gconstpointer unused) int main (int argc, char **argv) { - const gchar *srcdir; - g_type_init (); g_test_init (&argc, &argv, NULL); - srcdir = g_getenv ("SRCDIR"); - if (srcdir && chdir (srcdir) < 0) - g_error ("couldn't change directory to: %s: %s", srcdir, g_strerror (errno)); - g_test_add ("/gck/slot/slot_info", Test, NULL, setup, test_slot_info, teardown); g_test_add ("/gck/slot/slot_props", Test, NULL, setup, test_slot_props, teardown); g_test_add ("/gck/slot/slot_equals_hash", Test, NULL, setup, test_slot_equals_hash, teardown); diff --git a/gck/tests/test-gck-uri.c b/gck/tests/test-gck-uri.c index 865aa0f6..8483ed91 100644 --- a/gck/tests/test-gck-uri.c +++ b/gck/tests/test-gck-uri.c @@ -519,15 +519,9 @@ null_log_handler (const gchar *log_domain, GLogLevelFlags log_level, int main (int argc, char **argv) { - const gchar *srcdir; - g_type_init (); g_test_init (&argc, &argv, NULL); - srcdir = g_getenv ("SRCDIR"); - if (srcdir && chdir (srcdir) < 0) - g_error ("couldn't change directory to: %s: %s", srcdir, g_strerror (errno)); - /* Suppress these messages in tests */ g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, null_log_handler, NULL); diff --git a/gcr/tests/Makefile.am b/gcr/tests/Makefile.am index 544a4065..9d161684 100644 --- a/gcr/tests/Makefile.am +++ b/gcr/tests/Makefile.am @@ -1,8 +1,8 @@ INCLUDES = \ - -I$(top_srcdir)/egg \ - -I$(top_srcdir)/gcr \ - -DSRCDIR=$(srcdir) \ + -I$(top_builddir) \ + -I$(top_srcdir) \ + -DSRCDIR="\"$(srcdir)\"" \ -DGCR_API_SUBJECT_TO_CHANGE \ -DGCK_API_SUBJECT_TO_CHANGE \ $(GLIB_CFLAGS) \ @@ -29,7 +29,7 @@ TEST_PROGS = \ check_PROGRAMS = $(TEST_PROGS) test: $(TEST_PROGS) - SRCDIR='$(srcdir)' gtester -k --verbose -m $(TEST_MODE) --g-fatal-warnings $(TEST_PROGS) + gtester -k --verbose -m $(TEST_MODE) --g-fatal-warnings $(TEST_PROGS) check-local: test diff --git a/gcr/tests/frob-certificate.c b/gcr/tests/frob-certificate.c index d14215cd..667a42f6 100644 --- a/gcr/tests/frob-certificate.c +++ b/gcr/tests/frob-certificate.c @@ -105,7 +105,7 @@ main(int argc, char *argv[]) test_certificate (argv[1]); } else { chdir_base_dir (argv[0]); - test_certificate ("files/der-certificate.crt"); + test_certificate (SRCDIR "/files/der-certificate.crt"); } return 0; diff --git a/gcr/tests/frob-key.c b/gcr/tests/frob-key.c index cd71b012..3292e2fc 100644 --- a/gcr/tests/frob-key.c +++ b/gcr/tests/frob-key.c @@ -103,7 +103,7 @@ main(int argc, char *argv[]) test_key (argv[1]); } else { chdir_base_dir (argv[0]); - test_key ("files/pem-dsa-1024.key"); + test_key (SRCDIR "/files/pem-dsa-1024.key"); } return 0; diff --git a/gcr/tests/test-certificate-chain.c b/gcr/tests/test-certificate-chain.c index ade0eac2..b0bedfb9 100644 --- a/gcr/tests/test-certificate-chain.c +++ b/gcr/tests/test-certificate-chain.c @@ -155,19 +155,19 @@ setup (Test *test, gconstpointer unused) gck_list_unref_free (modules); /* A self-signed certificate */ - if (!g_file_get_contents ("files/der-certificate.crt", &contents, &n_contents, NULL)) + if (!g_file_get_contents (SRCDIR "/files/der-certificate.crt", &contents, &n_contents, NULL)) g_assert_not_reached (); test->cert_self = gcr_simple_certificate_new (contents, n_contents); g_free (contents); /* A signed certificate */ - if (!g_file_get_contents ("files/dhansak-collabora.cer", &contents, &n_contents, NULL)) + if (!g_file_get_contents (SRCDIR "/files/dhansak-collabora.cer", &contents, &n_contents, NULL)) g_assert_not_reached (); test->cert_signed = mock_certificate_new (contents, n_contents); g_free (contents); /* The signer for the above certificate */ - if (!g_file_get_contents ("files/collabora-ca.cer", &contents, &n_contents, NULL)) + if (!g_file_get_contents (SRCDIR "/files/collabora-ca.cer", &contents, &n_contents, NULL)) g_assert_not_reached (); test->cert_ca = mock_certificate_new (contents, n_contents); g_free (contents); @@ -632,16 +632,10 @@ test_with_anchor_error_async (Test *test, gconstpointer unused) int main (int argc, char **argv) { - const gchar *srcdir; - g_type_init (); g_test_init (&argc, &argv, NULL); g_set_prgname ("test-certificate-chain"); - srcdir = g_getenv ("SRCDIR"); - if (srcdir && chdir (srcdir) < 0) - g_error ("couldn't change directory to: %s: %s", srcdir, g_strerror (errno)); - g_test_add ("/gcr/certificate-chain/new", Test, NULL, setup, test_new, teardown); g_test_add ("/gcr/certificate-chain/new_with_cert", Test, NULL, setup, test_new_with_cert, teardown); g_test_add ("/gcr/certificate-chain/selfsigned", Test, NULL, setup, test_selfsigned, teardown); diff --git a/gcr/tests/test-certificate.c b/gcr/tests/test-certificate.c index 137fe2a4..aed032d8 100644 --- a/gcr/tests/test-certificate.c +++ b/gcr/tests/test-certificate.c @@ -44,19 +44,19 @@ setup (Test *test, gconstpointer unused) gchar *contents; gsize n_contents; - if (!g_file_get_contents ("files/der-certificate.crt", &contents, &n_contents, NULL)) + if (!g_file_get_contents (SRCDIR "/files/der-certificate.crt", &contents, &n_contents, NULL)) g_assert_not_reached (); test->certificate = gcr_simple_certificate_new (contents, n_contents); g_assert (test->certificate); g_free (contents); - if (!g_file_get_contents ("files/der-certificate-dsa.cer", &contents, &n_contents, NULL)) + if (!g_file_get_contents (SRCDIR "/files/der-certificate-dsa.cer", &contents, &n_contents, NULL)) g_assert_not_reached (); test->dsa_cert = gcr_simple_certificate_new (contents, n_contents); g_assert (test->dsa_cert); g_free (contents); - if (!g_file_get_contents ("files/dhansak-collabora.cer", &contents, &n_contents, NULL)) + if (!g_file_get_contents (SRCDIR "/files/dhansak-collabora.cer", &contents, &n_contents, NULL)) g_assert_not_reached (); test->dhansak_cert = gcr_simple_certificate_new (contents, n_contents); g_assert (test->certificate); @@ -255,16 +255,10 @@ test_certificate_is_issuer (Test *test, gconstpointer unused) int main (int argc, char **argv) { - const gchar *srcdir; - g_type_init (); g_test_init (&argc, &argv, NULL); g_set_prgname ("test-certificate"); - srcdir = g_getenv ("SRCDIR"); - if (srcdir && chdir (srcdir) < 0) - g_error ("couldn't change directory to: %s: %s", srcdir, g_strerror (errno)); - g_test_add ("/gcr/certificate/issuer_cn", Test, NULL, setup, test_issuer_cn, teardown); g_test_add ("/gcr/certificate/issuer_dn", Test, NULL, setup, test_issuer_dn, teardown); g_test_add ("/gcr/certificate/issuer_part", Test, NULL, setup, test_issuer_part, teardown); diff --git a/gcr/tests/test-parser.c b/gcr/tests/test-parser.c index e8c5e303..39fbefcc 100644 --- a/gcr/tests/test-parser.c +++ b/gcr/tests/test-parser.c @@ -121,7 +121,7 @@ test_parse_all (Test *test, gconstpointer unused) gsize len; GDir *dir; - dir = g_dir_open ("files", 0, NULL); + dir = g_dir_open (SRCDIR "/files", 0, NULL); g_assert (dir); for (;;) { @@ -132,7 +132,7 @@ test_parse_all (Test *test, gconstpointer unused) continue; g_free (test->filedesc); - test->filedesc = g_build_filename ("files", filename, NULL); + test->filedesc = g_build_filename (SRCDIR "/files", filename, NULL); if (g_file_test (test->filedesc, G_FILE_TEST_IS_DIR)) continue; @@ -157,16 +157,10 @@ test_parse_all (Test *test, gconstpointer unused) int main (int argc, char **argv) { - const gchar *srcdir; - g_type_init (); g_test_init (&argc, &argv, NULL); g_set_prgname ("test-parser"); - srcdir = g_getenv ("SRCDIR"); - if (srcdir && chdir (srcdir) < 0) - g_error ("couldn't change directory to: %s: %s", srcdir, g_strerror (errno)); - g_test_add ("/gcr/parser/parse_all", Test, NULL, setup, test_parse_all, teardown); return g_test_run (); diff --git a/gcr/tests/test-pkcs11-certificate.c b/gcr/tests/test-pkcs11-certificate.c index c5d4c90b..feed8643 100644 --- a/gcr/tests/test-pkcs11-certificate.c +++ b/gcr/tests/test-pkcs11-certificate.c @@ -25,7 +25,7 @@ #include "egg/egg-asn1x.h" #include "egg/egg-asn1-defs.h" -#include "gcr.h" +#include "gcr/gcr.h" #include "gcr/gcr-internal.h" #include "egg/egg-testing.h" @@ -59,12 +59,12 @@ setup (Test *test, gconstpointer unused) GNode *asn, *node; CK_RV rv; - if (!g_file_get_contents ("files/der-certificate.crt", (gchar**)&test->cert_data, + if (!g_file_get_contents (SRCDIR "/files/der-certificate.crt", (gchar**)&test->cert_data, &test->n_cert_data, NULL)) g_assert_not_reached (); g_assert (test->cert_data); - if (!g_file_get_contents ("files/der-certificate-dsa.cer", (gchar**)&test->cert2_data, + if (!g_file_get_contents (SRCDIR "/files/der-certificate-dsa.cer", (gchar**)&test->cert2_data, &test->n_cert2_data, NULL)) g_assert_not_reached (); g_assert (test->cert2_data); @@ -265,16 +265,10 @@ test_lookup_certificate_issuer_fail_async (Test *test, gconstpointer unused) int main (int argc, char **argv) { - const gchar *srcdir; - g_type_init (); g_test_init (&argc, &argv, NULL); g_set_prgname ("test-pkcs11-certificate"); - srcdir = g_getenv ("SRCDIR"); - if (srcdir && chdir (srcdir) < 0) - g_error ("couldn't change directory to: %s: %s", srcdir, g_strerror (errno)); - g_test_add ("/gcr/pkcs11-certificate/lookup_certificate_issuer", Test, NULL, setup, test_lookup_certificate_issuer, teardown); g_test_add ("/gcr/pkcs11-certificate/lookup_certificate_issuer_not_found", Test, NULL, setup, test_lookup_certificate_issuer_not_found, teardown); g_test_add ("/gcr/pkcs11-certificate/lookup_certificate_issuer_async", Test, NULL, setup, test_lookup_certificate_issuer_async, teardown); diff --git a/gcr/tests/test-simple-certificate.c b/gcr/tests/test-simple-certificate.c index 707d2949..f3039cb1 100644 --- a/gcr/tests/test-simple-certificate.c +++ b/gcr/tests/test-simple-certificate.c @@ -22,7 +22,7 @@ #include "config.h" -#include "gcr.h" +#include "gcr/gcr.h" #include "gcr/gcr-internal.h" #include "gck/gck-test.h" @@ -43,7 +43,7 @@ typedef struct { static void setup (Test *test, gconstpointer unused) { - if (!g_file_get_contents ("files/der-certificate.crt", (gchar**)&test->cert_data, + if (!g_file_get_contents (SRCDIR "/files/der-certificate.crt", (gchar**)&test->cert_data, &test->n_cert_data, NULL)) g_assert_not_reached (); g_assert (test->cert_data); @@ -93,16 +93,10 @@ test_new_static (Test *test, gconstpointer unused) int main (int argc, char **argv) { - const gchar *srcdir; - g_type_init (); g_test_init (&argc, &argv, NULL); g_set_prgname ("test-simple-certificate"); - srcdir = g_getenv ("SRCDIR"); - if (srcdir && chdir (srcdir) < 0) - g_error ("couldn't change directory to: %s: %s", srcdir, g_strerror (errno)); - g_test_add ("/gcr/simple-certificate/new", Test, NULL, setup, test_new, teardown); g_test_add ("/gcr/simple-certificate/new_static", Test, NULL, setup, test_new_static, teardown); diff --git a/gcr/tests/test-trust.c b/gcr/tests/test-trust.c index a7a33eb8..1ddf8a8c 100644 --- a/gcr/tests/test-trust.c +++ b/gcr/tests/test-trust.c @@ -22,7 +22,7 @@ #include "config.h" -#include "gcr.h" +#include "gcr/gcr.h" #include "gcr/gcr-internal.h" #include "gck/gck-mock.h" @@ -54,7 +54,7 @@ setup (Test *test, gconstpointer unused) gsize len; CK_RV rv; - if (!g_file_get_contents ("files/der-certificate.crt", &contents, &len, NULL)) + if (!g_file_get_contents (SRCDIR "/files/der-certificate.crt", &contents, &len, NULL)) g_assert_not_reached (); g_assert (contents); @@ -309,16 +309,10 @@ test_is_certificate_anchored_async (Test *test, gconstpointer unused) int main (int argc, char **argv) { - const gchar *srcdir; - g_type_init (); g_test_init (&argc, &argv, NULL); g_set_prgname ("test-trust"); - srcdir = g_getenv ("SRCDIR"); - if (srcdir && chdir (srcdir) < 0) - g_error ("couldn't change directory to: %s: %s", srcdir, g_strerror (errno)); - g_test_add ("/gcr/trust/is_pinned_none", Test, NULL, setup, test_is_pinned_none, teardown); g_test_add ("/gcr/trust/add_and_is_pinned", Test, NULL, setup, test_add_and_is_pinned, teardown); g_test_add ("/gcr/trust/add_certificate_pinned_fail", Test, NULL, setup, test_add_certificate_pinned_fail, teardown); diff --git a/pkcs11/gkm/tests/Makefile.am b/pkcs11/gkm/tests/Makefile.am index c49d4acc..27ef48d0 100644 --- a/pkcs11/gkm/tests/Makefile.am +++ b/pkcs11/gkm/tests/Makefile.am @@ -1,8 +1,9 @@ INCLUDES = \ - -I$(top_srcdir)/egg \ + -I$(top_builddir) \ + -I$(top_srcdir) \ -I$(top_srcdir)/pkcs11 \ - -DSRCDIR=$(srcdir) \ + -DSRCDIR="\"$(srcdir)\"" \ $(GLIB_CFLAGS) \ $(LIBGCRYPT_CFLAGS) @@ -59,7 +60,7 @@ test_timer_LDADD = \ check_PROGRAMS = $(TEST_PROGS) test: $(TEST_PROGS) - SRCDIR='$(srcdir)' gtester -k --verbose -m $(TEST_MODE) --g-fatal-warnings $(TEST_PROGS) + gtester -k --verbose -m $(TEST_MODE) --g-fatal-warnings $(TEST_PROGS) check-local: test @@ -71,3 +72,6 @@ asn1-def-test.h: test.asn EXTRA_DIST = \ test.asn \ files + +DISTCLEANFILES = \ + $(BUILT_SOURCES) diff --git a/pkcs11/gkm/tests/mock-module.c b/pkcs11/gkm/tests/mock-module.c index b53544bd..3abe1162 100644 --- a/pkcs11/gkm/tests/mock-module.c +++ b/pkcs11/gkm/tests/mock-module.c @@ -110,7 +110,7 @@ mock_module_object_new (GkmSession *session) { CKA_CERTIFICATE_TYPE, &type, sizeof (type) }, }; - if (!g_file_get_contents ("files/test-certificate-1.der", &data, &n_data, NULL)) + if (!g_file_get_contents (SRCDIR "/files/test-certificate-1.der", &data, &n_data, NULL)) g_assert_not_reached (); attrs[0].pValue = data; diff --git a/pkcs11/gkm/tests/test-data-asn1.c b/pkcs11/gkm/tests/test-data-asn1.c index d6e3dec3..b29ea0bb 100644 --- a/pkcs11/gkm/tests/test-data-asn1.c +++ b/pkcs11/gkm/tests/test-data-asn1.c @@ -47,7 +47,7 @@ typedef struct { static void setup (Test *test, gconstpointer unused) { - if (!g_file_get_contents ("files/test-certificate-1.der", &test->data_cert, &test->n_data_cert, NULL)) + if (!g_file_get_contents (SRCDIR "/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); diff --git a/pkcs11/gkm/tests/test-data-der.c b/pkcs11/gkm/tests/test-data-der.c index 6be10c65..57823ac7 100644 --- a/pkcs11/gkm/tests/test-data-der.c +++ b/pkcs11/gkm/tests/test-data-der.c @@ -116,11 +116,11 @@ test_der_public (gcry_sexp_t key) static void setup (Test *test, gconstpointer unused) { - if (!g_file_get_contents ("files/test-certificate-1.der", &test->certificate_data, &test->n_certificate_data, NULL)) + if (!g_file_get_contents (SRCDIR "/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)) + if (!g_file_get_contents (SRCDIR "/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); @@ -331,7 +331,7 @@ test_read_ca_certificates_public_key_info (Test *test, gconstpointer unused) gchar *data; gsize n_data; - if (!g_file_get_contents ("files/ca-certificates.crt", &data, &n_data, NULL)) + if (!g_file_get_contents (SRCDIR "/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); @@ -432,7 +432,7 @@ test_read_all_pkcs8 (Test *test, gconstpointer unused) gsize n_data; gchar *path; - dir = g_dir_open ("files", 0, NULL); + dir = g_dir_open (SRCDIR "/files", 0, NULL); g_assert (dir); for(;;) { @@ -443,7 +443,7 @@ test_read_all_pkcs8 (Test *test, gconstpointer unused) if (!g_pattern_match_simple ("der-pkcs8-*", name)) continue; - path = g_build_filename ("files", name, NULL); + path = g_build_filename (SRCDIR "/files", name, NULL); if (!g_file_get_contents (path, &data, &n_data, NULL)) g_assert_not_reached (); g_free (path); @@ -467,7 +467,7 @@ test_read_pkcs8_bad_password (Test *test, gconstpointer unused) gchar *data; gsize n_data; - if (!g_file_get_contents ("files/der-pkcs8-encrypted-pkcs5.key", &data, &n_data, NULL)) + if (!g_file_get_contents (SRCDIR "/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); diff --git a/pkcs11/gkm/tests/test-object.c b/pkcs11/gkm/tests/test-object.c index 0c3c826c..00fa770e 100644 --- a/pkcs11/gkm/tests/test-object.c +++ b/pkcs11/gkm/tests/test-object.c @@ -48,7 +48,7 @@ setup (Test* test, gconstpointer unused) 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)) + if (!g_file_get_contents (SRCDIR "/files/test-certificate-1.der", &test->certificate_data, &test->n_certificate_data, NULL)) g_assert_not_reached (); } diff --git a/pkcs11/gnome2-store/tests/Makefile.am b/pkcs11/gnome2-store/tests/Makefile.am index 458ee250..dbf0ade7 100644 --- a/pkcs11/gnome2-store/tests/Makefile.am +++ b/pkcs11/gnome2-store/tests/Makefile.am @@ -1,29 +1,45 @@ -TESTING_FILES = \ - unit-test-gnome2-file.c \ - test-module.c - -TESTING_LIBS = \ +INCLUDES = \ + -I$(top_builddir) \ + -I$(top_srcdir) \ + -I$(top_srcdir)/pkcs11 \ + -DSRCDIR="\"$(srcdir)\"" \ + $(GLIB_CFLAGS) \ + $(LIBGCRYPT_CFLAGS) + +LDADD = \ $(top_builddir)/pkcs11/gnome2-store/libgkm-gnome2-store.la \ $(top_builddir)/pkcs11/gkm/libgkm.la \ - $(top_builddir)/egg/libegg.la + $(top_builddir)/egg/libegg.la \ + $(GLIB_LIBS) \ + $(LIBGCRYPT_LIBS) -include $(top_srcdir)/testing/testing.make +if WITH_P11_TESTS +CHECK_PROGS = check-gnome2-module +else +CHECK_PROGS = +endif -EXTRA_DIST += \ - test-data \ - p11-tests.conf +TEST_PROGS = \ + test-gnome2-file -# --------------------------------------------------------------------- +check_PROGRAMS = $(TEST_PROGS) -noinst_PROGRAMS += \ - dump-gnome2-file +test: $(TEST_PROGS) $(CHECK_PROGS) + gtester -k --verbose -m $(TEST_MODE) --g-fatal-warnings $(TEST_PROGS) + @for prog in $(CHECK_PROGS); do SRCDIR='.' ./$$prog || exit 1; done -dump_gnome2_file_SOURCES = \ - dump-gnome2-file.c +check-local: test -dump_gnome2_file_LDADD = \ - $(top_builddir)/pkcs11/gkm/libgkm.la \ - $(top_builddir)/pkcs11/gnome2-store/libgkm-gnome2-store.la \ - $(top_builddir)/egg/libegg.la \ - $(DAEMON_LIBS) +all-local: $(check_PROGRAMS) + +EXTRA_DIST = \ + p11-tests.conf \ + files + +noinst_PROGRAMS = \ + frob-gnome2-file \ + $(CHECK_PROGS) + +check_gnome2_module_CFLAGS = $(P11_TESTS_CFLAGS) +check_gnome2_module_LDADD = $(P11_TESTS_LIBS) $(LDADD) diff --git a/pkcs11/gnome2-store/tests/check-gnome2-module.c b/pkcs11/gnome2-store/tests/check-gnome2-module.c new file mode 100644 index 00000000..53857786 --- /dev/null +++ b/pkcs11/gnome2-store/tests/check-gnome2-module.c @@ -0,0 +1,68 @@ +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ +/* check-module.c: Check PKCS#11 implementation + + 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 "gnome2-store/gkm-gnome2-store.h" + +#include "egg/egg-secure-memory.h" + +#include <glib.h> +#include <glib-object.h> + +#include <p11-tests.h> + +static int failures = 0; + +EGG_SECURE_GLIB_DEFINITIONS (); + +static void +on_p11_tests_log (int level, const char *section, const char *message) +{ + if (level == P11_TESTS_NONE) { + g_message ("%s", message); + } else if (level != P11_TESTS_FAIL) { + g_message ("%s: %s", section, message); + } else { + g_print (" /gnome2-store/%s: FAIL: %s\n", section, message); + ++failures; + } +} + +int +main (int argc, const char *argv[]) +{ + g_type_init (); + + p11_tests_set_log_func (on_p11_tests_log); + p11_tests_set_unexpected (1); + p11_tests_set_verbose (0); + p11_tests_set_write_session (1); + p11_tests_load_config (SRCDIR "/p11-tests.conf"); + + g_print ("CHECK: check-gnome2-module...\n"); + p11_tests_perform (gkm_gnome2_store_get_functions ()); + + g_print ("%s: check-gnome2-module\n", failures ? "FAIL" : "PASS"); + return failures; +} diff --git a/pkcs11/gnome2-store/tests/test-data/.gitempty b/pkcs11/gnome2-store/tests/files/.gitempty index e69de29b..e69de29b 100644 --- a/pkcs11/gnome2-store/tests/test-data/.gitempty +++ b/pkcs11/gnome2-store/tests/files/.gitempty diff --git a/pkcs11/gnome2-store/tests/test-data/data-file-private.store b/pkcs11/gnome2-store/tests/files/data-file-private.store Binary files differindex 4f978a0c..4f978a0c 100644 --- a/pkcs11/gnome2-store/tests/test-data/data-file-private.store +++ b/pkcs11/gnome2-store/tests/files/data-file-private.store diff --git a/pkcs11/gnome2-store/tests/test-data/data-file-public.store b/pkcs11/gnome2-store/tests/files/data-file-public.store Binary files differindex 9c19087a..9c19087a 100644 --- a/pkcs11/gnome2-store/tests/test-data/data-file-public.store +++ b/pkcs11/gnome2-store/tests/files/data-file-public.store diff --git a/pkcs11/gnome2-store/tests/dump-gnome2-file.c b/pkcs11/gnome2-store/tests/frob-gnome2-file.c index 5b2e579e..e3515ef5 100644 --- a/pkcs11/gnome2-store/tests/dump-gnome2-file.c +++ b/pkcs11/gnome2-store/tests/frob-gnome2-file.c @@ -21,7 +21,9 @@ Author: Stef Walter <stef@memberwebs.com> */ -#include "gkm-gnome2-file.h" +#include "config.h" + +#include "gnome2-store/gkm-gnome2-file.h" #include "gkm/gkm-crypto.h" diff --git a/pkcs11/gnome2-store/tests/test-gnome2-file.c b/pkcs11/gnome2-store/tests/test-gnome2-file.c new file mode 100644 index 00000000..7b9d9f85 --- /dev/null +++ b/pkcs11/gnome2-store/tests/test-gnome2-file.c @@ -0,0 +1,642 @@ +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ +/* unit-test-file-store.c: Test file store functionality + + Copyright (C) 2008 Stefan Walter + + The Gnome Keyring Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The Gnome Keyring Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the Gnome Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. + + Author: Stef Walter <stef@memberwebs.com> +*/ + +#include "config.h" + +#include "gnome2-store/gkm-gnome2-file.h" + +#include "gkm/gkm-object.h" + +#include "egg/egg-libgcrypt.h" +#include "egg/egg-secure-memory.h" + +#include <glib/gstdio.h> + +#include <fcntl.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +typedef struct { + GkmGnome2File *data_file; + gchar *public_filename; + gchar *private_filename; + gchar *write_filename; + int write_fd; + int public_fd; + int private_fd; + GkmSecret *login; +} Test; + +EGG_SECURE_GLIB_DEFINITIONS (); + +static void +setup (Test *test, gconstpointer unused) +{ + test->public_filename = g_build_filename (SRCDIR "/files", "data-file-public.store", NULL); + test->private_filename = g_build_filename (SRCDIR "/files", "data-file-private.store", NULL); + test->write_filename = g_build_filename ("/tmp", "unit-test-file.store", NULL); + + test->data_file = gkm_gnome2_file_new (); + + test->public_fd = g_open (test->public_filename, O_RDONLY, 0); + test->private_fd = g_open (test->private_filename, O_RDONLY, 0); + test->write_fd = g_open (test->write_filename, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); + g_assert (test->write_fd != -1); + + test->login = gkm_secret_new ((guchar*)"booo", 4); +} + +static void +teardown (Test *test, gconstpointer unused) +{ + g_free (test->public_filename); + g_free (test->private_filename); + g_free (test->write_filename); + + g_object_unref (test->data_file); + + if (test->public_fd != -1) + close (test->public_fd); + if (test->private_fd != -1) + close (test->private_fd); + if (test->write_fd != -1) + close (test->write_fd); + test->public_fd = test->private_fd = test->write_fd = -1; + + g_object_unref (test->login); +} + +static void +test_file_create (Test *test, gconstpointer unused) +{ + GkmDataResult res; + + res = gkm_gnome2_file_create_entry (test->data_file, "identifier-public", GKM_GNOME2_FILE_SECTION_PUBLIC); + g_assert (res == GKM_DATA_SUCCESS); + + /* Should be able to create private in a new file */ + res = gkm_gnome2_file_create_entry (test->data_file, "identifier-public", GKM_GNOME2_FILE_SECTION_PRIVATE); + g_assert (res == GKM_DATA_SUCCESS); +} + +static void +test_file_write_value (Test *test, gconstpointer unused) +{ + GkmDataResult res; + + /* Can't write when no identifier present */ + res = gkm_gnome2_file_write_value (test->data_file, "identifier-public", CKA_LABEL, "public-label", 12); + g_assert (res == GKM_DATA_UNRECOGNIZED); + + res = gkm_gnome2_file_create_entry (test->data_file, "identifier-public", GKM_GNOME2_FILE_SECTION_PUBLIC); + g_assert (res == GKM_DATA_SUCCESS); + + /* Should be able to write now */ + res = gkm_gnome2_file_write_value (test->data_file, "identifier-public", CKA_LABEL, "public-label", 12); + g_assert (res == GKM_DATA_SUCCESS); +} + +static void +test_file_read_value (Test *test, gconstpointer unused) +{ + gconstpointer value = NULL; + GkmDataResult res; + gsize n_value; + guint number = 7778; + + /* Write some stuff in */ + res = gkm_gnome2_file_create_entry (test->data_file, "ident", GKM_GNOME2_FILE_SECTION_PUBLIC); + g_assert (res == GKM_DATA_SUCCESS); + res = gkm_gnome2_file_write_value (test->data_file, "ident", CKA_LABEL, "TWO-label", 10); + g_assert (res == GKM_DATA_SUCCESS); + res = gkm_gnome2_file_write_value (test->data_file, "ident", CKA_VALUE, &number, sizeof (number)); + g_assert (res == GKM_DATA_SUCCESS); + + /* Read for an invalid item */ + res = gkm_gnome2_file_read_value (test->data_file, "non-existant", CKA_LABEL, &value, &n_value); + g_assert (res == GKM_DATA_UNRECOGNIZED); + + /* Read for an invalid attribute */ + res = gkm_gnome2_file_read_value (test->data_file, "ident", CKA_ID, &value, &n_value); + g_assert (res == GKM_DATA_UNRECOGNIZED); + + /* Read out a valid number */ + res = gkm_gnome2_file_read_value (test->data_file, "ident", CKA_VALUE, &value, &n_value); + g_assert (res == GKM_DATA_SUCCESS); + g_assert (value); + g_assert (n_value == sizeof (number)); + g_assert_cmpuint (*((guint*)value), ==, number); + + /* Read out the valid string */ + res = gkm_gnome2_file_read_value (test->data_file, "ident", CKA_LABEL, &value, &n_value); + g_assert (res == GKM_DATA_SUCCESS); + g_assert (value); + g_assert (n_value == 10); + g_assert_cmpstr ((const gchar*)value, ==, "TWO-label"); +} + +static void +test_file_read (Test *test, gconstpointer unused) +{ + GkmDataResult res; + + res = gkm_gnome2_file_read_fd (test->data_file, test->public_fd, NULL); + g_assert (res == GKM_DATA_SUCCESS); +} + +static void +test_file_lookup (Test *test, gconstpointer unused) +{ + GkmDataResult res; + guint section; + gboolean ret; + + /* Invalid shouldn't succeed */ + ret = gkm_gnome2_file_lookup_entry (test->data_file, "non-existant", §ion); + g_assert (ret == FALSE); + + /* Create a test item */ + res = gkm_gnome2_file_create_entry (test->data_file, "test-ident", GKM_GNOME2_FILE_SECTION_PUBLIC); + g_assert (res == GKM_DATA_SUCCESS); + + ret = gkm_gnome2_file_lookup_entry (test->data_file, "test-ident", §ion); + g_assert (ret == TRUE); + g_assert (section == GKM_GNOME2_FILE_SECTION_PUBLIC); + + /* Should be able to call without asking for section */ + ret = gkm_gnome2_file_lookup_entry (test->data_file, "test-ident", NULL); + g_assert (ret == TRUE); +} + +static void +file_read_private_without_login (Test *test, gconstpointer unused) +{ + GkmDataResult res; + guint section; + gconstpointer value; + gsize n_value; + gboolean ret; + + res = gkm_gnome2_file_read_fd (test->data_file, test->private_fd, NULL); + g_assert (res == GKM_DATA_SUCCESS); + + /* Items from the private section should exist */ + ret = gkm_gnome2_file_lookup_entry (test->data_file, "identifier-private", §ion); + g_assert (ret); + g_assert (section == GKM_GNOME2_FILE_SECTION_PRIVATE); + + /* But we shouldn't be able to read values from those private items */ + ret = gkm_gnome2_file_read_value (test->data_file, "identifier-private", CKA_LABEL, &value, &n_value); + g_assert (ret == GKM_DATA_LOCKED); + + /* Shouldn't be able to create private items */ + res = gkm_gnome2_file_create_entry (test->data_file, "dummy-private", GKM_GNOME2_FILE_SECTION_PRIVATE); + g_assert (res == GKM_DATA_LOCKED); + + /* Shouldn't be able to write with another test->login */ + res = gkm_gnome2_file_write_fd (test->data_file, test->write_fd, test->login); + g_assert (res == GKM_DATA_LOCKED); + + /* Now load a public file without private bits*/ + res = gkm_gnome2_file_read_fd (test->data_file, test->public_fd, NULL); + g_assert (res == GKM_DATA_SUCCESS); + + /* Now we should be able to load private stuff */ + res = gkm_gnome2_file_create_entry (test->data_file, "dummy-private", GKM_GNOME2_FILE_SECTION_PRIVATE); + g_assert (res == GKM_DATA_SUCCESS); +} + +static void +test_file_write (Test *test, gconstpointer unused) +{ + GkmDataResult res; + + res = gkm_gnome2_file_create_entry (test->data_file, "identifier-public", GKM_GNOME2_FILE_SECTION_PUBLIC); + g_assert (res == GKM_DATA_SUCCESS); + + res = gkm_gnome2_file_write_value (test->data_file, "identifier-public", CKA_LABEL, "public-label", 12); + g_assert (res == GKM_DATA_SUCCESS); + + res = gkm_gnome2_file_create_entry (test->data_file, "identifier-two", GKM_GNOME2_FILE_SECTION_PUBLIC); + g_assert (res == GKM_DATA_SUCCESS); + + res = gkm_gnome2_file_write_fd (test->data_file, test->write_fd, NULL); + g_assert (res == GKM_DATA_SUCCESS); +} + +static void +test_cant_write_private_without_login (Test *test, gconstpointer unused) +{ + GkmDataResult res; + + res = gkm_gnome2_file_create_entry (test->data_file, "identifier_private", GKM_GNOME2_FILE_SECTION_PRIVATE); + g_assert (res == GKM_DATA_SUCCESS); + + res = gkm_gnome2_file_write_fd (test->data_file, test->write_fd, NULL); + g_assert (res == GKM_DATA_LOCKED); +} + +static void +test_write_private_with_login (Test *test, gconstpointer unused) +{ + GkmDataResult res; + gulong value; + + res = gkm_gnome2_file_create_entry (test->data_file, "identifier-public", GKM_GNOME2_FILE_SECTION_PUBLIC); + g_assert (res == GKM_DATA_SUCCESS); + res = gkm_gnome2_file_write_value (test->data_file, "identifier-public", CKA_LABEL, "public-label", 12); + g_assert (res == GKM_DATA_SUCCESS); + + res = gkm_gnome2_file_create_entry (test->data_file, "identifier-two", GKM_GNOME2_FILE_SECTION_PUBLIC); + g_assert (res == GKM_DATA_SUCCESS); + res = gkm_gnome2_file_write_value (test->data_file, "identifier-two", CKA_LABEL, "TWO-label", 9); + g_assert (res == GKM_DATA_SUCCESS); + value = 555; + res = gkm_gnome2_file_write_value (test->data_file, "identifier-two", CKA_VALUE, &value, sizeof (value)); + g_assert (res == GKM_DATA_SUCCESS); + + res = gkm_gnome2_file_create_entry (test->data_file, "identifier-private", GKM_GNOME2_FILE_SECTION_PRIVATE); + g_assert (res == GKM_DATA_SUCCESS); + res = gkm_gnome2_file_write_value (test->data_file, "identifier-private", CKA_LABEL, "private-label", 13); + g_assert (res == GKM_DATA_SUCCESS); + + res = gkm_gnome2_file_write_fd (test->data_file, test->write_fd, test->login); + g_assert (res == GKM_DATA_SUCCESS); +} + +static void +test_read_private_with_login (Test *test, gconstpointer unused) +{ + GkmDataResult res; + gconstpointer value; + gsize n_value; + + res = gkm_gnome2_file_read_fd (test->data_file, test->private_fd, test->login); + g_assert (res == GKM_DATA_SUCCESS); + + /* Should be able to read private items */ + res = gkm_gnome2_file_read_value (test->data_file, "identifier-private", CKA_LABEL, &value, &n_value); + g_assert (res == GKM_DATA_SUCCESS); + g_assert_cmpuint (n_value, ==, 13); + g_assert (memcmp (value, "private-label", 13) == 0); +} + +static void +test_destroy_entry (Test *test, gconstpointer unused) +{ + GkmDataResult res; + + res = gkm_gnome2_file_destroy_entry (test->data_file, "non-existant"); + g_assert (res == GKM_DATA_UNRECOGNIZED); + + res = gkm_gnome2_file_read_fd (test->data_file, test->public_fd, NULL); + g_assert (res == GKM_DATA_SUCCESS); + + /* Make sure it's here */ + g_assert (gkm_gnome2_file_lookup_entry (test->data_file, "identifier-public", NULL)); + + res = gkm_gnome2_file_destroy_entry (test->data_file, "identifier-public"); + g_assert (res == GKM_DATA_SUCCESS); + + /* Make sure it's gone */ + g_assert (!gkm_gnome2_file_lookup_entry (test->data_file, "identifier-public", NULL)); +} + +static void +test_destroy_entry_by_loading (Test *test, gconstpointer unused) +{ + GkmDataResult res; + + /* Create some extra idenifiers */ + res = gkm_gnome2_file_create_entry (test->data_file, "my-public", GKM_GNOME2_FILE_SECTION_PUBLIC); + g_assert (res == GKM_DATA_SUCCESS); + res = gkm_gnome2_file_create_entry (test->data_file, "my-private", GKM_GNOME2_FILE_SECTION_PRIVATE); + g_assert (res == GKM_DATA_SUCCESS); + + /* Now read from the file */ + res = gkm_gnome2_file_read_fd (test->data_file, test->public_fd, NULL); + g_assert (res == GKM_DATA_SUCCESS); + + /* Both should be gone */ + g_assert (!gkm_gnome2_file_lookup_entry (test->data_file, "my-public", NULL)); + g_assert (!gkm_gnome2_file_lookup_entry (test->data_file, "my-private", NULL)); +} + + +static void +test_destroy_private_without_login (Test *test, gconstpointer unused) +{ + GkmDataResult res; + + res = gkm_gnome2_file_read_fd (test->data_file, test->private_fd, NULL); + g_assert (res == GKM_DATA_SUCCESS); + + /* Make sure it's here */ + g_assert (gkm_gnome2_file_lookup_entry (test->data_file, "identifier-private", NULL)); + + /* Shouldn't be able to destroy */ + res = gkm_gnome2_file_destroy_entry (test->data_file, "identifier-private"); + g_assert (res == GKM_DATA_LOCKED); + + /* Make sure it's still here */ + g_assert (gkm_gnome2_file_lookup_entry (test->data_file, "identifier-private", NULL)); +} + +static void +entry_added_one (GkmGnome2File *df, const gchar *identifier, gboolean *added) +{ + g_assert (GKM_IS_GNOME2_FILE (df)); + g_assert (identifier); + g_assert (added); + + /* Should only be called once */ + g_assert (!*added); + *added = TRUE; +} + +static void +test_entry_added_signal (Test *test, gconstpointer unused) +{ + GkmDataResult res; + gboolean added; + + g_signal_connect (test->data_file, "entry-added", G_CALLBACK (entry_added_one), &added); + + /* Should fire the signal */ + added = FALSE; + res = gkm_gnome2_file_create_entry (test->data_file, "identifier-public", GKM_GNOME2_FILE_SECTION_PUBLIC); + g_assert (res == GKM_DATA_SUCCESS); + g_assert (added == TRUE); + + /* Another one should be added when we load */ + added = FALSE; + res = gkm_gnome2_file_read_fd (test->data_file, test->public_fd, NULL); + g_assert (res == GKM_DATA_SUCCESS); + g_assert (added == TRUE); +} + +static void +entry_changed_one (GkmGnome2File *df, const gchar *identifier, gulong type, gboolean *changed) +{ + g_assert (GKM_IS_GNOME2_FILE (df)); + g_assert (identifier); + g_assert (changed); + g_assert (type == CKA_LABEL); + + /* Should only be called once */ + g_assert (!*changed); + *changed = TRUE; +} + +static void +test_entry_changed_signal (Test *test, gconstpointer unused) +{ + GkmDataResult res; + gboolean changed; + + g_signal_connect (test->data_file, "entry-changed", G_CALLBACK (entry_changed_one), &changed); + + /* Loading shouldn't fire the signal */ + changed = FALSE; + res = gkm_gnome2_file_read_fd (test->data_file, test->public_fd, NULL); + g_assert (res == GKM_DATA_SUCCESS); + g_assert (changed == FALSE); + + /* Shouldn't fire the signal on nonexistant */ + changed = FALSE; + res = gkm_gnome2_file_write_value (test->data_file, "non-existant", CKA_LABEL, "new-value", 10); + g_assert (res == GKM_DATA_UNRECOGNIZED); + g_assert (changed == FALSE); + + /* Should fire the signal */ + changed = FALSE; + res = gkm_gnome2_file_write_value (test->data_file, "identifier-public", CKA_LABEL, "new-value", 10); + g_assert (res == GKM_DATA_SUCCESS); + g_assert (changed == TRUE); + + /* Shouldn't fire the signal, same value again */ + changed = FALSE; + res = gkm_gnome2_file_write_value (test->data_file, "identifier-public", CKA_LABEL, "new-value", 10); + g_assert (res == GKM_DATA_SUCCESS); + g_assert (changed == FALSE); + + /* Reload file, should revert, fire signal */ + changed = FALSE; + g_assert (lseek (test->public_fd, 0, SEEK_SET) != -1); + res = gkm_gnome2_file_read_fd (test->data_file, test->public_fd, NULL); + g_assert (res == GKM_DATA_SUCCESS); + g_assert (changed == TRUE); +} + +static void +entry_removed_one (GkmGnome2File *df, const gchar *identifier, gboolean *removed) +{ + g_assert (GKM_IS_GNOME2_FILE (df)); + g_assert (identifier); + g_assert (removed); + + /* Should only be called once */ + g_assert (!*removed); + *removed = TRUE; +} + +static void +test_entry_removed_signal (Test *test, gconstpointer unused) +{ + GkmDataResult res; + gboolean removed; + + g_signal_connect (test->data_file, "entry-removed", G_CALLBACK (entry_removed_one), &removed); + + /* Loading shouldn't fire the signal */ + removed = FALSE; + res = gkm_gnome2_file_read_fd (test->data_file, test->public_fd, NULL); + g_assert (res == GKM_DATA_SUCCESS); + g_assert (removed == FALSE); + + /* Shouldn't fire the signal on removing nonexistant */ + removed = FALSE; + res = gkm_gnome2_file_destroy_entry (test->data_file, "non-existant"); + g_assert (res == GKM_DATA_UNRECOGNIZED); + g_assert (removed == FALSE); + + /* Remove a real entry */ + removed = FALSE; + res = gkm_gnome2_file_destroy_entry (test->data_file, "identifier-public"); + g_assert (res == GKM_DATA_SUCCESS); + g_assert (removed == TRUE); + + /* Add a dummy entry */ + res = gkm_gnome2_file_create_entry (test->data_file, "extra-dummy", GKM_GNOME2_FILE_SECTION_PUBLIC); + g_assert (res == GKM_DATA_SUCCESS); + + /* That one should go away when we reload, fire signal */ + removed = FALSE; + g_assert (lseek (test->public_fd, 0, SEEK_SET) != -1); + res = gkm_gnome2_file_read_fd (test->data_file, test->public_fd, NULL); + g_assert (res == GKM_DATA_SUCCESS); + g_assert (removed == TRUE); +} + +static void +foreach_entry (GkmGnome2File *df, const gchar *identifier, gpointer data) +{ + GPtrArray *array = data; + const gchar *ident; + int i; + + g_assert (data); + g_assert (identifier); + g_assert (GKM_IS_GNOME2_FILE (df)); + + /* Check that this is unique */ + for (i = 0; i < array->len; ++i) { + ident = g_ptr_array_index (array, i); + g_assert (ident); + g_assert_cmpstr (ident, !=, identifier); + } + + /* Add it */ + g_ptr_array_add (array, g_strdup (identifier)); +} + +static void +test_data_file_foreach (Test *test, gconstpointer unused) +{ + GkmDataResult res; + GPtrArray *array; + + res = gkm_gnome2_file_read_fd (test->data_file, test->private_fd, test->login); + g_assert (res == GKM_DATA_SUCCESS); + + array = g_ptr_array_new (); + gkm_gnome2_file_foreach_entry (test->data_file, foreach_entry, array); + g_assert (array->len == 4); + + g_ptr_array_add (array, NULL); + g_strfreev ((gchar**)g_ptr_array_free (array, FALSE)); +} + +static void +test_unique_entry (Test *test, gconstpointer unused) +{ + GkmDataResult res; + gchar *identifier; + + res = gkm_gnome2_file_read_fd (test->data_file, test->public_fd, NULL); + g_assert (res == GKM_DATA_SUCCESS); + + /* Should change an identifier that conflicts */ + identifier = g_strdup ("identifier-public"); + res = gkm_gnome2_file_unique_entry (test->data_file, &identifier); + g_assert (res == GKM_DATA_SUCCESS); + g_assert_cmpstr (identifier, !=, "identifier-public"); + g_free (identifier); + + /* Shouldn't change a unique identifier */ + identifier = g_strdup ("identifier-unique"); + res = gkm_gnome2_file_unique_entry (test->data_file, &identifier); + g_assert (res == GKM_DATA_SUCCESS); + g_assert_cmpstr (identifier, ==, "identifier-unique"); + g_free (identifier); + + /* Should be able to get from NULL */ + identifier = NULL; + res = gkm_gnome2_file_unique_entry (test->data_file, &identifier); + g_assert (res == GKM_DATA_SUCCESS); + g_assert (identifier != NULL); + g_assert (identifier[0] != 0); + g_free (identifier); +} + +static void +test_have_sections (Test *test, gconstpointer unused) +{ + GkmDataResult res; + + res = gkm_gnome2_file_read_fd (test->data_file, test->public_fd, NULL); + g_assert (res == GKM_DATA_SUCCESS); + + /* No private section */ + g_assert (gkm_gnome2_file_have_section (test->data_file, GKM_GNOME2_FILE_SECTION_PUBLIC)); + g_assert (!gkm_gnome2_file_have_section (test->data_file, GKM_GNOME2_FILE_SECTION_PRIVATE)); + + /* Read private stuff into file, without test->login */ + res = gkm_gnome2_file_read_fd (test->data_file, test->private_fd, NULL); + g_assert (res == GKM_DATA_SUCCESS); + + /* Should have a private section even without test->login */ + g_assert (gkm_gnome2_file_have_section (test->data_file, GKM_GNOME2_FILE_SECTION_PUBLIC)); + g_assert (gkm_gnome2_file_have_section (test->data_file, GKM_GNOME2_FILE_SECTION_PRIVATE)); + + /* Read private stuff into file, with test->login */ + g_assert (lseek (test->private_fd, 0, SEEK_SET) == 0); + res = gkm_gnome2_file_read_fd (test->data_file, test->private_fd, test->login); + g_assert (res == GKM_DATA_SUCCESS); + + /* Should have a private section now with test->login */ + g_assert (gkm_gnome2_file_have_section (test->data_file, GKM_GNOME2_FILE_SECTION_PUBLIC)); + g_assert (gkm_gnome2_file_have_section (test->data_file, GKM_GNOME2_FILE_SECTION_PRIVATE)); + + /* Read public stuff back into file*/ + g_assert (lseek (test->public_fd, 0, SEEK_SET) == 0); + res = gkm_gnome2_file_read_fd (test->data_file, test->public_fd, test->login); + g_assert (res == GKM_DATA_SUCCESS); + + /* Shouldn't have a private section now */ + g_assert (gkm_gnome2_file_have_section (test->data_file, GKM_GNOME2_FILE_SECTION_PUBLIC)); + g_assert (!gkm_gnome2_file_have_section (test->data_file, GKM_GNOME2_FILE_SECTION_PRIVATE)); +} + + +int +main (int argc, char **argv) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + + egg_libgcrypt_initialize (); + + g_test_add ("/gnome2-store/gnome2-file/file_create", Test, NULL, setup, test_file_create, teardown); + g_test_add ("/gnome2-store/gnome2-file/file_write_value", Test, NULL, setup, test_file_write_value, teardown); + g_test_add ("/gnome2-store/gnome2-file/file_read_value", Test, NULL, setup, test_file_read_value, teardown); + g_test_add ("/gnome2-store/gnome2-file/file_read", Test, NULL, setup, test_file_read, teardown); + g_test_add ("/gnome2-store/gnome2-file/file_lookup", Test, NULL, setup, test_file_lookup, teardown); + g_test_add ("/gnome2-store/gnome2-file/file_read_private_without_login", Test, NULL, setup, file_read_private_without_login, teardown); + g_test_add ("/gnome2-store/gnome2-file/file_write", Test, NULL, setup, test_file_write, teardown); + g_test_add ("/gnome2-store/gnome2-file/cant_write_private_without_login", Test, NULL, setup, test_cant_write_private_without_login, teardown); + g_test_add ("/gnome2-store/gnome2-file/write_private_with_login", Test, NULL, setup, test_write_private_with_login, teardown); + g_test_add ("/gnome2-store/gnome2-file/read_private_with_login", Test, NULL, setup, test_read_private_with_login, teardown); + g_test_add ("/gnome2-store/gnome2-file/destroy_entry", Test, NULL, setup, test_destroy_entry, teardown); + g_test_add ("/gnome2-store/gnome2-file/destroy_entry_by_loading", Test, NULL, setup, test_destroy_entry_by_loading, teardown); + g_test_add ("/gnome2-store/gnome2-file/destroy_private_without_login", Test, NULL, setup, test_destroy_private_without_login, teardown); + g_test_add ("/gnome2-store/gnome2-file/entry_added_signal", Test, NULL, setup, test_entry_added_signal, teardown); + g_test_add ("/gnome2-store/gnome2-file/entry_changed_signal", Test, NULL, setup, test_entry_changed_signal, teardown); + g_test_add ("/gnome2-store/gnome2-file/entry_removed_signal", Test, NULL, setup, test_entry_removed_signal, teardown); + g_test_add ("/gnome2-store/gnome2-file/data_file_foreach", Test, NULL, setup, test_data_file_foreach, teardown); + g_test_add ("/gnome2-store/gnome2-file/unique_entry", Test, NULL, setup, test_unique_entry, teardown); + g_test_add ("/gnome2-store/gnome2-file/have_sections", Test, NULL, setup, test_have_sections, teardown); + + return g_test_run (); +} diff --git a/pkcs11/gnome2-store/tests/test-module.c b/pkcs11/gnome2-store/tests/test-module.c deleted file mode 100644 index d7ce0244..00000000 --- a/pkcs11/gnome2-store/tests/test-module.c +++ /dev/null @@ -1,34 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* test-module.c: A test PKCS#11 implementation - - 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 "gkm-gnome2-store.h" - -#include "test-suite.h" - -TESTING_EXTERNAL(user_module) -{ - CK_FUNCTION_LIST_PTR funcs = gkm_gnome2_store_get_functions (); - testing_test_p11_module (funcs, "p11-tests.conf"); -} diff --git a/pkcs11/gnome2-store/tests/unit-test-gnome2-file.c b/pkcs11/gnome2-store/tests/unit-test-gnome2-file.c deleted file mode 100644 index dec2a26f..00000000 --- a/pkcs11/gnome2-store/tests/unit-test-gnome2-file.c +++ /dev/null @@ -1,592 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* unit-test-file-store.c: Test file store functionality - - Copyright (C) 2008 Stefan Walter - - The Gnome Keyring Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The Gnome Keyring Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with the Gnome Library; see the file COPYING.LIB. If not, - write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. - - Author: Stef Walter <stef@memberwebs.com> -*/ - -#include "test-suite.h" - -#include "egg/egg-libgcrypt.h" - -#include "gkm/gkm-object.h" - -#include "gkm-gnome2-file.h" - -#include <glib/gstdio.h> - -#include <fcntl.h> - -/* Both point to the same thing */ -static GkmGnome2File *data_file = NULL; -static gchar *public_filename = NULL; -static gchar *private_filename = NULL; -static gchar *write_filename = NULL; -static int write_fd = -1; -static int public_fd = -1; -static int private_fd = -1; -static GkmSecret *login = NULL; - -TESTING_SETUP(file_store) -{ - egg_libgcrypt_initialize (); - - public_filename = testing_data_filename ("data-file-public.store"); - private_filename = testing_data_filename ("data-file-private.store"); - write_filename = testing_scratch_filename ("unit-test-file.store"); - - data_file = gkm_gnome2_file_new (); - - public_fd = g_open (public_filename, O_RDONLY, 0); - g_assert (public_fd != -1); - - private_fd = g_open (private_filename, O_RDONLY, 0); - g_assert (private_fd != -1); - - write_fd = g_open (write_filename, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); - g_assert (write_fd != -1); - - login = gkm_secret_new ((guchar*)"booo", 4); -} - -TESTING_TEARDOWN(file_store) -{ - g_free (public_filename); - g_free (private_filename); - g_free (write_filename); - - g_object_unref (data_file); - data_file = NULL; - - if (public_fd != -1) - close (public_fd); - if (private_fd != -1) - close (private_fd); - if (write_fd != -1) - close (write_fd); - public_fd = private_fd = write_fd = -1; - - g_object_unref (login); -} - -TESTING_TEST(test_file_create) -{ - GkmDataResult res; - - res = gkm_gnome2_file_create_entry (data_file, "identifier-public", GKM_GNOME2_FILE_SECTION_PUBLIC); - g_assert (res == GKM_DATA_SUCCESS); - - /* Should be able to create private in a new file */ - res = gkm_gnome2_file_create_entry (data_file, "identifier-public", GKM_GNOME2_FILE_SECTION_PRIVATE); - g_assert (res == GKM_DATA_SUCCESS); -} - -TESTING_TEST(test_file_write_value) -{ - GkmDataResult res; - - /* Can't write when no identifier present */ - res = gkm_gnome2_file_write_value (data_file, "identifier-public", CKA_LABEL, "public-label", 12); - g_assert (res == GKM_DATA_UNRECOGNIZED); - - res = gkm_gnome2_file_create_entry (data_file, "identifier-public", GKM_GNOME2_FILE_SECTION_PUBLIC); - g_assert (res == GKM_DATA_SUCCESS); - - /* Should be able to write now */ - res = gkm_gnome2_file_write_value (data_file, "identifier-public", CKA_LABEL, "public-label", 12); - g_assert (res == GKM_DATA_SUCCESS); -} - -TESTING_TEST(test_file_read_value) -{ - gconstpointer value = NULL; - GkmDataResult res; - gsize n_value; - guint number = 7778; - - /* Write some stuff in */ - res = gkm_gnome2_file_create_entry (data_file, "ident", GKM_GNOME2_FILE_SECTION_PUBLIC); - g_assert (res == GKM_DATA_SUCCESS); - res = gkm_gnome2_file_write_value (data_file, "ident", CKA_LABEL, "TWO-label", 10); - g_assert (res == GKM_DATA_SUCCESS); - res = gkm_gnome2_file_write_value (data_file, "ident", CKA_VALUE, &number, sizeof (number)); - g_assert (res == GKM_DATA_SUCCESS); - - /* Read for an invalid item */ - res = gkm_gnome2_file_read_value (data_file, "non-existant", CKA_LABEL, &value, &n_value); - g_assert (res == GKM_DATA_UNRECOGNIZED); - - /* Read for an invalid attribute */ - res = gkm_gnome2_file_read_value (data_file, "ident", CKA_ID, &value, &n_value); - g_assert (res == GKM_DATA_UNRECOGNIZED); - - /* Read out a valid number */ - res = gkm_gnome2_file_read_value (data_file, "ident", CKA_VALUE, &value, &n_value); - g_assert (res == GKM_DATA_SUCCESS); - g_assert (value); - g_assert (n_value == sizeof (number)); - g_assert_cmpuint (*((guint*)value), ==, number); - - /* Read out the valid string */ - res = gkm_gnome2_file_read_value (data_file, "ident", CKA_LABEL, &value, &n_value); - g_assert (res == GKM_DATA_SUCCESS); - g_assert (value); - g_assert (n_value == 10); - g_assert_cmpstr ((const gchar*)value, ==, "TWO-label"); -} - -TESTING_TEST(test_file_read) -{ - GkmDataResult res; - - res = gkm_gnome2_file_read_fd (data_file, public_fd, NULL); - g_assert (res == GKM_DATA_SUCCESS); -} - -TESTING_TEST(test_file_lookup) -{ - GkmDataResult res; - guint section; - gboolean ret; - - /* Invalid shouldn't succeed */ - ret = gkm_gnome2_file_lookup_entry (data_file, "non-existant", §ion); - g_assert (ret == FALSE); - - /* Create a test item */ - res = gkm_gnome2_file_create_entry (data_file, "test-ident", GKM_GNOME2_FILE_SECTION_PUBLIC); - g_assert (res == GKM_DATA_SUCCESS); - - ret = gkm_gnome2_file_lookup_entry (data_file, "test-ident", §ion); - g_assert (ret == TRUE); - g_assert (section == GKM_GNOME2_FILE_SECTION_PUBLIC); - - /* Should be able to call without asking for section */ - ret = gkm_gnome2_file_lookup_entry (data_file, "test-ident", NULL); - g_assert (ret == TRUE); -} - -TESTING_TEST(file_read_private_without_login) -{ - GkmDataResult res; - guint section; - gconstpointer value; - gsize n_value; - gboolean ret; - - res = gkm_gnome2_file_read_fd (data_file, private_fd, NULL); - g_assert (res == GKM_DATA_SUCCESS); - - /* Items from the private section should exist */ - ret = gkm_gnome2_file_lookup_entry (data_file, "identifier-private", §ion); - g_assert (ret); - g_assert (section == GKM_GNOME2_FILE_SECTION_PRIVATE); - - /* But we shouldn't be able to read values from those private items */ - ret = gkm_gnome2_file_read_value (data_file, "identifier-private", CKA_LABEL, &value, &n_value); - g_assert (ret == GKM_DATA_LOCKED); - - /* Shouldn't be able to create private items */ - res = gkm_gnome2_file_create_entry (data_file, "dummy-private", GKM_GNOME2_FILE_SECTION_PRIVATE); - g_assert (res == GKM_DATA_LOCKED); - - /* Shouldn't be able to write with another login */ - res = gkm_gnome2_file_write_fd (data_file, write_fd, login); - g_assert (res == GKM_DATA_LOCKED); - - /* Now load a public file without private bits*/ - res = gkm_gnome2_file_read_fd (data_file, public_fd, NULL); - g_assert (res == GKM_DATA_SUCCESS); - - /* Now we should be able to load private stuff */ - res = gkm_gnome2_file_create_entry (data_file, "dummy-private", GKM_GNOME2_FILE_SECTION_PRIVATE); - g_assert (res == GKM_DATA_SUCCESS); -} - -TESTING_TEST(test_file_write) -{ - GkmDataResult res; - - res = gkm_gnome2_file_create_entry (data_file, "identifier-public", GKM_GNOME2_FILE_SECTION_PUBLIC); - g_assert (res == GKM_DATA_SUCCESS); - - res = gkm_gnome2_file_write_value (data_file, "identifier-public", CKA_LABEL, "public-label", 12); - g_assert (res == GKM_DATA_SUCCESS); - - res = gkm_gnome2_file_create_entry (data_file, "identifier-two", GKM_GNOME2_FILE_SECTION_PUBLIC); - g_assert (res == GKM_DATA_SUCCESS); - - res = gkm_gnome2_file_write_fd (data_file, write_fd, NULL); - g_assert (res == GKM_DATA_SUCCESS); -} - -TESTING_TEST(cant_write_private_without_login) -{ - GkmDataResult res; - - res = gkm_gnome2_file_create_entry (data_file, "identifier_private", GKM_GNOME2_FILE_SECTION_PRIVATE); - g_assert (res == GKM_DATA_SUCCESS); - - res = gkm_gnome2_file_write_fd (data_file, write_fd, NULL); - g_assert (res == GKM_DATA_LOCKED); -} - -TESTING_TEST(write_private_with_login) -{ - GkmDataResult res; - gulong value; - - res = gkm_gnome2_file_create_entry (data_file, "identifier-public", GKM_GNOME2_FILE_SECTION_PUBLIC); - g_assert (res == GKM_DATA_SUCCESS); - res = gkm_gnome2_file_write_value (data_file, "identifier-public", CKA_LABEL, "public-label", 12); - g_assert (res == GKM_DATA_SUCCESS); - - res = gkm_gnome2_file_create_entry (data_file, "identifier-two", GKM_GNOME2_FILE_SECTION_PUBLIC); - g_assert (res == GKM_DATA_SUCCESS); - res = gkm_gnome2_file_write_value (data_file, "identifier-two", CKA_LABEL, "TWO-label", 9); - g_assert (res == GKM_DATA_SUCCESS); - value = 555; - res = gkm_gnome2_file_write_value (data_file, "identifier-two", CKA_VALUE, &value, sizeof (value)); - g_assert (res == GKM_DATA_SUCCESS); - - res = gkm_gnome2_file_create_entry (data_file, "identifier-private", GKM_GNOME2_FILE_SECTION_PRIVATE); - g_assert (res == GKM_DATA_SUCCESS); - res = gkm_gnome2_file_write_value (data_file, "identifier-private", CKA_LABEL, "private-label", 13); - g_assert (res == GKM_DATA_SUCCESS); - - res = gkm_gnome2_file_write_fd (data_file, write_fd, login); - g_assert (res == GKM_DATA_SUCCESS); -} - -TESTING_TEST(read_private_with_login) -{ - GkmDataResult res; - gconstpointer value; - gsize n_value; - - res = gkm_gnome2_file_read_fd (data_file, private_fd, login); - g_assert (res == GKM_DATA_SUCCESS); - - /* Should be able to read private items */ - res = gkm_gnome2_file_read_value (data_file, "identifier-private", CKA_LABEL, &value, &n_value); - g_assert (res == GKM_DATA_SUCCESS); - g_assert_cmpuint (n_value, ==, 13); - g_assert (memcmp (value, "private-label", 13) == 0); -} - -TESTING_TEST(destroy_entry) -{ - GkmDataResult res; - - res = gkm_gnome2_file_destroy_entry (data_file, "non-existant"); - g_assert (res == GKM_DATA_UNRECOGNIZED); - - res = gkm_gnome2_file_read_fd (data_file, public_fd, NULL); - g_assert (res == GKM_DATA_SUCCESS); - - /* Make sure it's here */ - g_assert (gkm_gnome2_file_lookup_entry (data_file, "identifier-public", NULL)); - - res = gkm_gnome2_file_destroy_entry (data_file, "identifier-public"); - g_assert (res == GKM_DATA_SUCCESS); - - /* Make sure it's gone */ - g_assert (!gkm_gnome2_file_lookup_entry (data_file, "identifier-public", NULL)); -} - -TESTING_TEST(destroy_entry_by_loading) -{ - GkmDataResult res; - - /* Create some extra idenifiers */ - res = gkm_gnome2_file_create_entry (data_file, "my-public", GKM_GNOME2_FILE_SECTION_PUBLIC); - g_assert (res == GKM_DATA_SUCCESS); - res = gkm_gnome2_file_create_entry (data_file, "my-private", GKM_GNOME2_FILE_SECTION_PRIVATE); - g_assert (res == GKM_DATA_SUCCESS); - - /* Now read from the file */ - res = gkm_gnome2_file_read_fd (data_file, public_fd, NULL); - g_assert (res == GKM_DATA_SUCCESS); - - /* Both should be gone */ - g_assert (!gkm_gnome2_file_lookup_entry (data_file, "my-public", NULL)); - g_assert (!gkm_gnome2_file_lookup_entry (data_file, "my-private", NULL)); -} - - -TESTING_TEST(destroy_private_without_login) -{ - GkmDataResult res; - - res = gkm_gnome2_file_read_fd (data_file, private_fd, NULL); - g_assert (res == GKM_DATA_SUCCESS); - - /* Make sure it's here */ - g_assert (gkm_gnome2_file_lookup_entry (data_file, "identifier-private", NULL)); - - /* Shouldn't be able to destroy */ - res = gkm_gnome2_file_destroy_entry (data_file, "identifier-private"); - g_assert (res == GKM_DATA_LOCKED); - - /* Make sure it's still here */ - g_assert (gkm_gnome2_file_lookup_entry (data_file, "identifier-private", NULL)); -} - -static void -entry_added_one (GkmGnome2File *df, const gchar *identifier, gboolean *added) -{ - g_assert (GKM_IS_GNOME2_FILE (df)); - g_assert (df == data_file); - g_assert (identifier); - g_assert (added); - - /* Should only be called once */ - g_assert (!*added); - *added = TRUE; -} - -TESTING_TEST(entry_added_signal) -{ - GkmDataResult res; - gboolean added; - - g_signal_connect (data_file, "entry-added", G_CALLBACK (entry_added_one), &added); - - /* Should fire the signal */ - added = FALSE; - res = gkm_gnome2_file_create_entry (data_file, "identifier-public", GKM_GNOME2_FILE_SECTION_PUBLIC); - g_assert (res == GKM_DATA_SUCCESS); - g_assert (added == TRUE); - - /* Another one should be added when we load */ - added = FALSE; - res = gkm_gnome2_file_read_fd (data_file, public_fd, NULL); - g_assert (res == GKM_DATA_SUCCESS); - g_assert (added == TRUE); -} - -static void -entry_changed_one (GkmGnome2File *df, const gchar *identifier, gulong type, gboolean *changed) -{ - g_assert (GKM_IS_GNOME2_FILE (df)); - g_assert (df == data_file); - g_assert (identifier); - g_assert (changed); - g_assert (type == CKA_LABEL); - - /* Should only be called once */ - g_assert (!*changed); - *changed = TRUE; -} - -TESTING_TEST(entry_changed_signal) -{ - GkmDataResult res; - gboolean changed; - - g_signal_connect (data_file, "entry-changed", G_CALLBACK (entry_changed_one), &changed); - - /* Loading shouldn't fire the signal */ - changed = FALSE; - res = gkm_gnome2_file_read_fd (data_file, public_fd, NULL); - g_assert (res == GKM_DATA_SUCCESS); - g_assert (changed == FALSE); - - /* Shouldn't fire the signal on nonexistant */ - changed = FALSE; - res = gkm_gnome2_file_write_value (data_file, "non-existant", CKA_LABEL, "new-value", 10); - g_assert (res == GKM_DATA_UNRECOGNIZED); - g_assert (changed == FALSE); - - /* Should fire the signal */ - changed = FALSE; - res = gkm_gnome2_file_write_value (data_file, "identifier-public", CKA_LABEL, "new-value", 10); - g_assert (res == GKM_DATA_SUCCESS); - g_assert (changed == TRUE); - - /* Shouldn't fire the signal, same value again */ - changed = FALSE; - res = gkm_gnome2_file_write_value (data_file, "identifier-public", CKA_LABEL, "new-value", 10); - g_assert (res == GKM_DATA_SUCCESS); - g_assert (changed == FALSE); - - /* Reload file, should revert, fire signal */ - changed = FALSE; - g_assert (lseek (public_fd, 0, SEEK_SET) != -1); - res = gkm_gnome2_file_read_fd (data_file, public_fd, NULL); - g_assert (res == GKM_DATA_SUCCESS); - g_assert (changed == TRUE); -} - -static void -entry_removed_one (GkmGnome2File *df, const gchar *identifier, gboolean *removed) -{ - g_assert (GKM_IS_GNOME2_FILE (df)); - g_assert (df == data_file); - g_assert (identifier); - g_assert (removed); - - /* Should only be called once */ - g_assert (!*removed); - *removed = TRUE; -} - -TESTING_TEST(entry_removed_signal) -{ - GkmDataResult res; - gboolean removed; - - g_signal_connect (data_file, "entry-removed", G_CALLBACK (entry_removed_one), &removed); - - /* Loading shouldn't fire the signal */ - removed = FALSE; - res = gkm_gnome2_file_read_fd (data_file, public_fd, NULL); - g_assert (res == GKM_DATA_SUCCESS); - g_assert (removed == FALSE); - - /* Shouldn't fire the signal on removing nonexistant */ - removed = FALSE; - res = gkm_gnome2_file_destroy_entry (data_file, "non-existant"); - g_assert (res == GKM_DATA_UNRECOGNIZED); - g_assert (removed == FALSE); - - /* Remove a real entry */ - removed = FALSE; - res = gkm_gnome2_file_destroy_entry (data_file, "identifier-public"); - g_assert (res == GKM_DATA_SUCCESS); - g_assert (removed == TRUE); - - /* Add a dummy entry */ - res = gkm_gnome2_file_create_entry (data_file, "extra-dummy", GKM_GNOME2_FILE_SECTION_PUBLIC); - g_assert (res == GKM_DATA_SUCCESS); - - /* That one should go away when we reload, fire signal */ - removed = FALSE; - g_assert (lseek (public_fd, 0, SEEK_SET) != -1); - res = gkm_gnome2_file_read_fd (data_file, public_fd, NULL); - g_assert (res == GKM_DATA_SUCCESS); - g_assert (removed == TRUE); -} - -static void -foreach_entry (GkmGnome2File *df, const gchar *identifier, gpointer data) -{ - GPtrArray *array = data; - const gchar *ident; - int i; - - g_assert (data); - g_assert (identifier); - g_assert (GKM_IS_GNOME2_FILE (df)); - - /* Check that this is unique */ - for (i = 0; i < array->len; ++i) { - ident = g_ptr_array_index (array, i); - g_assert (ident); - g_assert_cmpstr (ident, !=, identifier); - } - - /* Add it */ - g_ptr_array_add (array, g_strdup (identifier)); -} - -TESTING_TEST(data_file_foreach) -{ - GkmDataResult res; - GPtrArray *array; - - res = gkm_gnome2_file_read_fd (data_file, private_fd, login); - g_assert (res == GKM_DATA_SUCCESS); - - array = g_ptr_array_new (); - gkm_gnome2_file_foreach_entry (data_file, foreach_entry, array); - g_assert (array->len == 4); - - g_ptr_array_add (array, NULL); - g_strfreev ((gchar**)g_ptr_array_free (array, FALSE)); -} - -TESTING_TEST(unique_entry) -{ - GkmDataResult res; - gchar *identifier; - - res = gkm_gnome2_file_read_fd (data_file, public_fd, NULL); - g_assert (res == GKM_DATA_SUCCESS); - - /* Should change an identifier that conflicts */ - identifier = g_strdup ("identifier-public"); - res = gkm_gnome2_file_unique_entry (data_file, &identifier); - g_assert (res == GKM_DATA_SUCCESS); - g_assert_cmpstr (identifier, !=, "identifier-public"); - g_free (identifier); - - /* Shouldn't change a unique identifier */ - identifier = g_strdup ("identifier-unique"); - res = gkm_gnome2_file_unique_entry (data_file, &identifier); - g_assert (res == GKM_DATA_SUCCESS); - g_assert_cmpstr (identifier, ==, "identifier-unique"); - g_free (identifier); - - /* Should be able to get from NULL */ - identifier = NULL; - res = gkm_gnome2_file_unique_entry (data_file, &identifier); - g_assert (res == GKM_DATA_SUCCESS); - g_assert (identifier != NULL); - g_assert (identifier[0] != 0); - g_free (identifier); -} - -TESTING_TEST(have_sections) -{ - GkmDataResult res; - - res = gkm_gnome2_file_read_fd (data_file, public_fd, NULL); - g_assert (res == GKM_DATA_SUCCESS); - - /* No private section */ - g_assert (gkm_gnome2_file_have_section (data_file, GKM_GNOME2_FILE_SECTION_PUBLIC)); - g_assert (!gkm_gnome2_file_have_section (data_file, GKM_GNOME2_FILE_SECTION_PRIVATE)); - - /* Read private stuff into file, without login */ - res = gkm_gnome2_file_read_fd (data_file, private_fd, NULL); - g_assert (res == GKM_DATA_SUCCESS); - - /* Should have a private section even without login */ - g_assert (gkm_gnome2_file_have_section (data_file, GKM_GNOME2_FILE_SECTION_PUBLIC)); - g_assert (gkm_gnome2_file_have_section (data_file, GKM_GNOME2_FILE_SECTION_PRIVATE)); - - /* Read private stuff into file, with login */ - g_assert (lseek (private_fd, 0, SEEK_SET) == 0); - res = gkm_gnome2_file_read_fd (data_file, private_fd, login); - g_assert (res == GKM_DATA_SUCCESS); - - /* Should have a private section now with login */ - g_assert (gkm_gnome2_file_have_section (data_file, GKM_GNOME2_FILE_SECTION_PUBLIC)); - g_assert (gkm_gnome2_file_have_section (data_file, GKM_GNOME2_FILE_SECTION_PRIVATE)); - - /* Read public stuff back into file*/ - g_assert (lseek (public_fd, 0, SEEK_SET) == 0); - res = gkm_gnome2_file_read_fd (data_file, public_fd, login); - g_assert (res == GKM_DATA_SUCCESS); - - /* Shouldn't have a private section now */ - g_assert (gkm_gnome2_file_have_section (data_file, GKM_GNOME2_FILE_SECTION_PUBLIC)); - g_assert (!gkm_gnome2_file_have_section (data_file, GKM_GNOME2_FILE_SECTION_PRIVATE)); -} diff --git a/pkcs11/roots-store/tests/Makefile.am b/pkcs11/roots-store/tests/Makefile.am index e80f1cd5..146d9e21 100644 --- a/pkcs11/roots-store/tests/Makefile.am +++ b/pkcs11/roots-store/tests/Makefile.am @@ -1,14 +1,38 @@ -TESTING_FILES = \ - test-module.c +INCLUDES = \ + -I$(top_builddir) \ + -I$(top_srcdir) \ + -I$(top_srcdir)/pkcs11 \ + -DSRCDIR="\"$(srcdir)\"" \ + $(GLIB_CFLAGS) \ + $(LIBGCRYPT_CFLAGS) -TESTING_LIBS = \ +LDADD = \ $(top_builddir)/pkcs11/roots-store/libgkm-roots-store.la \ $(top_builddir)/pkcs11/gkm/libgkm.la \ - $(top_builddir)/egg/libegg.la + $(top_builddir)/egg/libegg.la \ + $(GLIB_LIBS) \ + $(LIBGCRYPT_LIBS) -include $(top_srcdir)/testing/testing.make +if WITH_P11_TESTS +CHECK_PROGS = check-roots-module +else +CHECK_PROGS = +endif -EXTRA_DIST += \ - test-data \ - p11-tests.conf +test: $(TEST_PROGS) $(CHECK_PROGS) + @for prog in $(CHECK_PROGS); do SRCDIR='.' ./$$prog || exit 1; done + +check-local: test + +all-local: $(check_PROGRAMS) + +EXTRA_DIST = \ + p11-tests.conf \ + files + +noinst_PROGRAMS = \ + $(CHECK_PROGS) + +check_roots_module_CFLAGS = $(P11_TESTS_CFLAGS) +check_roots_module_LDADD = $(P11_TESTS_LIBS) $(LDADD) diff --git a/pkcs11/roots-store/tests/test-module.c b/pkcs11/roots-store/tests/check-roots-module.c index 42975b61..1bc9e167 100644 --- a/pkcs11/roots-store/tests/test-module.c +++ b/pkcs11/roots-store/tests/check-roots-module.c @@ -1,5 +1,5 @@ /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* test-module.c: A test PKCS#11 implementation +/* check-roots-module.c: Test PKCS#11 implementation Copyright (C) 2009 Stefan Walter @@ -25,10 +25,44 @@ #include "roots-store/gkm-roots-store.h" -#include "test-suite.h" +#include "egg/egg-secure-memory.h" -TESTING_EXTERNAL(roots_module) +#include <glib.h> +#include <glib-object.h> + +#include <p11-tests.h> + +static int failures = 0; + +EGG_SECURE_GLIB_DEFINITIONS (); + +static void +on_p11_tests_log (int level, const char *section, const char *message) +{ + if (level == P11_TESTS_NONE) { + g_message ("%s", message); + } else if (level != P11_TESTS_FAIL) { + g_message ("%s: %s", section, message); + } else { + g_print (" /roots-store/%s: FAIL: %s\n", section, message); + ++failures; + } +} + +int +main (int argc, const char *argv[]) { - CK_FUNCTION_LIST_PTR funcs = gkm_roots_store_get_functions (); - testing_test_p11_module (funcs, "p11-tests.conf"); + g_type_init (); + + p11_tests_set_log_func (on_p11_tests_log); + p11_tests_set_unexpected (1); + p11_tests_set_verbose (0); + p11_tests_set_write_session (1); + p11_tests_load_config (SRCDIR "/p11-tests.conf"); + + g_print ("CHECK: check-roots-module...\n"); + p11_tests_perform (gkm_roots_store_get_functions ()); + + g_print ("%s: check-roots-module\n", failures ? "FAIL" : "PASS"); + return failures; } diff --git a/pkcs11/roots-store/tests/test-data/RSA_Root_Certificate_1.pem b/pkcs11/roots-store/tests/files/RSA_Root_Certificate_1.pem index f6cdfcd0..f6cdfcd0 100644 --- a/pkcs11/roots-store/tests/test-data/RSA_Root_Certificate_1.pem +++ b/pkcs11/roots-store/tests/files/RSA_Root_Certificate_1.pem diff --git a/pkcs11/roots-store/tests/test-data/RSA_Security_1024_v3.pem b/pkcs11/roots-store/tests/files/RSA_Security_1024_v3.pem index 94e0ad13..94e0ad13 100644 --- a/pkcs11/roots-store/tests/test-data/RSA_Security_1024_v3.pem +++ b/pkcs11/roots-store/tests/files/RSA_Security_1024_v3.pem diff --git a/pkcs11/roots-store/tests/test-data/RSA_Security_2048_v3.pem b/pkcs11/roots-store/tests/files/RSA_Security_2048_v3.pem index 86c907eb..86c907eb 100644 --- a/pkcs11/roots-store/tests/test-data/RSA_Security_2048_v3.pem +++ b/pkcs11/roots-store/tests/files/RSA_Security_2048_v3.pem diff --git a/pkcs11/roots-store/tests/test-data/Thawte_Personal_Basic_CA.pem b/pkcs11/roots-store/tests/files/Thawte_Personal_Basic_CA.pem index 22c2a8db..22c2a8db 100644 --- a/pkcs11/roots-store/tests/test-data/Thawte_Personal_Basic_CA.pem +++ b/pkcs11/roots-store/tests/files/Thawte_Personal_Basic_CA.pem diff --git a/pkcs11/roots-store/tests/test-data/Thawte_Personal_Freemail_CA.pem b/pkcs11/roots-store/tests/files/Thawte_Personal_Freemail_CA.pem index 565a4be1..565a4be1 100644 --- a/pkcs11/roots-store/tests/test-data/Thawte_Personal_Freemail_CA.pem +++ b/pkcs11/roots-store/tests/files/Thawte_Personal_Freemail_CA.pem diff --git a/pkcs11/roots-store/tests/test-data/Thawte_Personal_Premium_CA.pem b/pkcs11/roots-store/tests/files/Thawte_Personal_Premium_CA.pem index 688c3ae1..688c3ae1 100644 --- a/pkcs11/roots-store/tests/test-data/Thawte_Personal_Premium_CA.pem +++ b/pkcs11/roots-store/tests/files/Thawte_Personal_Premium_CA.pem diff --git a/pkcs11/roots-store/tests/test-data/Thawte_Premium_Server_CA.pem b/pkcs11/roots-store/tests/files/Thawte_Premium_Server_CA.pem index aa37cfc9..aa37cfc9 100644 --- a/pkcs11/roots-store/tests/test-data/Thawte_Premium_Server_CA.pem +++ b/pkcs11/roots-store/tests/files/Thawte_Premium_Server_CA.pem diff --git a/pkcs11/roots-store/tests/test-data/Thawte_Server_CA.pem b/pkcs11/roots-store/tests/files/Thawte_Server_CA.pem index ac4e25bf..ac4e25bf 100644 --- a/pkcs11/roots-store/tests/test-data/Thawte_Server_CA.pem +++ b/pkcs11/roots-store/tests/files/Thawte_Server_CA.pem diff --git a/pkcs11/roots-store/tests/test-data/Thawte_Time_Stamping_CA.pem b/pkcs11/roots-store/tests/files/Thawte_Time_Stamping_CA.pem index fecbd15e..fecbd15e 100644 --- a/pkcs11/roots-store/tests/test-data/Thawte_Time_Stamping_CA.pem +++ b/pkcs11/roots-store/tests/files/Thawte_Time_Stamping_CA.pem diff --git a/pkcs11/roots-store/tests/test-data/ca-certificates.crt b/pkcs11/roots-store/tests/files/ca-certificates.crt index c30335f8..c30335f8 100644 --- a/pkcs11/roots-store/tests/test-data/ca-certificates.crt +++ b/pkcs11/roots-store/tests/files/ca-certificates.crt diff --git a/pkcs11/roots-store/tests/test-data/cacert.org.pem b/pkcs11/roots-store/tests/files/cacert.org.pem index e7dfc829..e7dfc829 100644 --- a/pkcs11/roots-store/tests/test-data/cacert.org.pem +++ b/pkcs11/roots-store/tests/files/cacert.org.pem diff --git a/pkcs11/roots-store/tests/test-data/der-certificate.crt b/pkcs11/roots-store/tests/files/der-certificate.crt Binary files differindex 56fa8491..56fa8491 100644 --- a/pkcs11/roots-store/tests/test-data/der-certificate.crt +++ b/pkcs11/roots-store/tests/files/der-certificate.crt diff --git a/pkcs11/secret-store/gkm-secret-binary.c b/pkcs11/secret-store/gkm-secret-binary.c index 942ffe7b..29cd9cdc 100644 --- a/pkcs11/secret-store/gkm-secret-binary.c +++ b/pkcs11/secret-store/gkm-secret-binary.c @@ -561,7 +561,7 @@ generate_hashed_items (GkmSecretCollection *collection, EggBuffer *buffer) GkmDataResult gkm_secret_binary_write (GkmSecretCollection *collection, GkmSecretData *sdata, - guchar **data, gsize *n_data) + gpointer *data, gsize *n_data) { GkmSecretObject *obj; EggBuffer to_encrypt; @@ -843,7 +843,7 @@ free_item_info (ItemInfo *info) gint gkm_secret_binary_read (GkmSecretCollection *collection, GkmSecretData *sdata, - const guchar *data, gsize n_data) + gconstpointer data, gsize n_data) { gsize offset; guchar major, minor, crypto, hash; diff --git a/pkcs11/secret-store/gkm-secret-binary.h b/pkcs11/secret-store/gkm-secret-binary.h index 05cc0643..a150f1be 100644 --- a/pkcs11/secret-store/gkm-secret-binary.h +++ b/pkcs11/secret-store/gkm-secret-binary.h @@ -29,12 +29,12 @@ GkmDataResult gkm_secret_binary_read (GkmSecretCollection *collection, GkmSecretData *sdata, - const guchar *data, + gconstpointer data, gsize n_data); GkmDataResult gkm_secret_binary_write (GkmSecretCollection *collection, GkmSecretData *sdata, - guchar **data, + gpointer *data, gsize *n_data); #endif /* __GKM_SECRET_BINARY_H__ */ diff --git a/pkcs11/secret-store/gkm-secret-collection.c b/pkcs11/secret-store/gkm-secret-collection.c index 1157255d..0aedc5c9 100644 --- a/pkcs11/secret-store/gkm-secret-collection.c +++ b/pkcs11/secret-store/gkm-secret-collection.c @@ -888,7 +888,7 @@ gkm_secret_collection_save (GkmSecretCollection *self, GkmTransaction *transacti { GkmSecret *master; GkmDataResult res; - guchar *data; + gpointer data; gsize n_data; g_return_if_fail (GKM_IS_SECRET_COLLECTION (self)); diff --git a/pkcs11/secret-store/gkm-secret-textual.c b/pkcs11/secret-store/gkm-secret-textual.c index 8d671173..af7bc372 100644 --- a/pkcs11/secret-store/gkm-secret-textual.c +++ b/pkcs11/secret-store/gkm-secret-textual.c @@ -380,7 +380,7 @@ parse_item (GKeyFile *file, GkmSecretItem *item, GkmSecretData *sdata, GkmDataResult gkm_secret_textual_write (GkmSecretCollection *collection, GkmSecretData *sdata, - guchar **data, gsize *n_data) + gpointer *data, gsize *n_data) { GkmSecretObject *obj; GList *items, *l; @@ -445,7 +445,7 @@ remove_unavailable_item (gpointer key, gpointer dummy, gpointer user_data) GkmDataResult gkm_secret_textual_read (GkmSecretCollection *collection, GkmSecretData *sdata, - const guchar *data, gsize n_data) + gconstpointer data, gsize n_data) { GkmSecretObject *obj; GkmSecretItem *item; diff --git a/pkcs11/secret-store/gkm-secret-textual.h b/pkcs11/secret-store/gkm-secret-textual.h index bb96e8ce..ca568e38 100644 --- a/pkcs11/secret-store/gkm-secret-textual.h +++ b/pkcs11/secret-store/gkm-secret-textual.h @@ -28,12 +28,12 @@ GkmDataResult gkm_secret_textual_read (GkmSecretCollection *collection, GkmSecretData *sdata, - const guchar *data, + gconstpointer data, gsize n_data); GkmDataResult gkm_secret_textual_write (GkmSecretCollection *collection, GkmSecretData *sdata, - guchar **data, + gpointer *data, gsize *n_data); #endif /* __GKM_SECRET_TEXTUAL_H__ */ diff --git a/pkcs11/secret-store/tests/Makefile.am b/pkcs11/secret-store/tests/Makefile.am index ff14313b..814bcc5c 100644 --- a/pkcs11/secret-store/tests/Makefile.am +++ b/pkcs11/secret-store/tests/Makefile.am @@ -1,25 +1,44 @@ -TESTING_SOURCES = \ - test-secret-module.c test-secret-module.h - -TESTING_FILES = \ - unit-test-secret-compat.c \ - unit-test-secret-fields.c \ - unit-test-secret-data.c \ - unit-test-secret-object.c \ - unit-test-secret-collection.c \ - unit-test-secret-item.c \ - unit-test-secret-search.c \ - unit-test-secret-textual.c \ - unit-test-secret-binary.c - -UNIT_PROMPT = - -TESTING_LIBS = \ + +INCLUDES = \ + -I$(top_builddir) \ + -I$(top_srcdir) \ + -I$(top_srcdir)/pkcs11 \ + -DSRCDIR="\"$(srcdir)\"" \ + $(GLIB_CFLAGS) \ + $(LIBGCRYPT_CFLAGS) + +LDADD = \ + libgkm-mock-secret-module.a \ $(top_builddir)/pkcs11/secret-store/libgkm-secret-store.la \ $(top_builddir)/pkcs11/gkm/libgkm.la \ - $(top_builddir)/egg/libegg.la + $(top_builddir)/egg/libegg.la \ + $(GLIB_LIBS) \ + $(LIBGCRYPT_LIBS) + +noinst_LIBRARIES = libgkm-mock-secret-module.a + +libgkm_mock_secret_module_a_SOURCES = \ + mock-secret-module.c mock-secret-module.h + +TEST_PROGS = \ + test-secret-compat \ + test-secret-fields \ + test-secret-data \ + test-secret-object \ + test-secret-collection \ + test-secret-item \ + test-secret-search \ + test-secret-textual \ + test-secret-binary + +check_PROGRAMS = $(TEST_PROGS) + +test: $(TEST_PROGS) + gtester -k --verbose -m $(TEST_MODE) --g-fatal-warnings $(TEST_PROGS) + +check-local: test -include $(top_srcdir)/testing/testing.make +all-local: $(check_PROGRAMS) -EXTRA_DIST += \ - test-data +EXTRA_DIST = \ + files diff --git a/pkcs11/secret-store/tests/test-data/encrypted.keyring b/pkcs11/secret-store/tests/files/encrypted.keyring Binary files differindex 742aecc3..742aecc3 100644 --- a/pkcs11/secret-store/tests/test-data/encrypted.keyring +++ b/pkcs11/secret-store/tests/files/encrypted.keyring diff --git a/pkcs11/secret-store/tests/test-data/plain-bad-number.keyring b/pkcs11/secret-store/tests/files/plain-bad-number.keyring index e5ff5ac9..e5ff5ac9 100644 --- a/pkcs11/secret-store/tests/test-data/plain-bad-number.keyring +++ b/pkcs11/secret-store/tests/files/plain-bad-number.keyring diff --git a/pkcs11/secret-store/tests/test-data/plain.keyring b/pkcs11/secret-store/tests/files/plain.keyring index c048aa10..c048aa10 100644 --- a/pkcs11/secret-store/tests/test-data/plain.keyring +++ b/pkcs11/secret-store/tests/files/plain.keyring diff --git a/pkcs11/secret-store/tests/test-secret-module.c b/pkcs11/secret-store/tests/mock-secret-module.c index 3215c81b..6cb698c0 100644 --- a/pkcs11/secret-store/tests/test-secret-module.c +++ b/pkcs11/secret-store/tests/mock-secret-module.c @@ -22,26 +22,54 @@ */ #include "config.h" -#include "test-secret-module.h" -#include "test-suite.h" + +#include "mock-secret-module.h" #include "gkm/gkm-secret.h" #include "gkm/gkm-module.h" -#include "gkm-secret-collection.h" -#include "gkm-secret-data.h" -#include "gkm-secret-fields.h" -#include "gkm-secret-item.h" -#include "gkm-secret-object.h" -#include "gkm-secret-store.h" +#include "secret-store/gkm-secret-collection.h" +#include "secret-store/gkm-secret-data.h" +#include "secret-store/gkm-secret-fields.h" +#include "secret-store/gkm-secret-item.h" +#include "secret-store/gkm-secret-object.h" +#include "secret-store/gkm-secret-store.h" + +#include "egg/egg-mkdtemp.h" +#include "egg/egg-secure-memory.h" + +#include <glib.h> #include <string.h> +EGG_SECURE_GLIB_DEFINITIONS (); + static GMutex *mutex = NULL; +static gchar *directory = NULL; GkmModule* _gkm_secret_store_get_module_for_testing (void); GMutex* _gkm_module_get_scary_mutex_that_you_should_not_touch (GkmModule *module); +static void +copy_file_to_directory (const gchar *from, const gchar *directory) +{ + gchar *filename; + gchar *basename; + gchar *data; + gsize n_data; + + if (!g_file_get_contents (from, &data, &n_data, NULL)) + g_error ("couldn't read: %s", from); + + basename = g_path_get_basename (from); + filename = g_build_filename (directory, basename, NULL); + if (!g_file_set_contents (filename, data, n_data, NULL)) + g_error ("couldn't write: %s", filename); + g_free (filename); + g_free (basename); + g_free (data); +} + GkmModule* test_secret_module_initialize_and_enter (void) { @@ -51,15 +79,17 @@ test_secret_module_initialize_and_enter (void) gchar *string; CK_RV rv; + directory = egg_mkdtemp (g_strdup ("/tmp/mock-secret-XXXXXX")); + /* Setup test directory to work in */ memset (&args, 0, sizeof (args)); - string = g_strdup_printf ("directory='%s'", testing_scratch_directory ()); + string = g_strdup_printf ("directory='%s'", directory); args.pReserved = string; args.flags = CKF_OS_LOCKING_OK; /* Copy files from test-data to scratch */ - testing_data_to_scratch ("encrypted.keyring", NULL); - testing_data_to_scratch ("plain.keyring", NULL); + copy_file_to_directory (SRCDIR "/files/encrypted.keyring", directory); + copy_file_to_directory (SRCDIR "/files/plain.keyring", directory); funcs = gkm_secret_store_get_functions (); rv = (funcs->C_Initialize) (&args); @@ -87,6 +117,9 @@ test_secret_module_leave_and_finalize (void) funcs = gkm_secret_store_get_functions (); rv = (funcs->C_Finalize) (NULL); g_return_if_fail (rv == CKR_OK); + + g_free (directory); + directory = NULL; } void diff --git a/pkcs11/secret-store/tests/test-secret-module.h b/pkcs11/secret-store/tests/mock-secret-module.h index 46413e3d..72afc154 100644 --- a/pkcs11/secret-store/tests/test-secret-module.h +++ b/pkcs11/secret-store/tests/mock-secret-module.h @@ -27,7 +27,8 @@ #include <glib.h> #include "gkm/gkm-types.h" -#include "gkm-secret-types.h" + +#include "secret-store/gkm-secret-types.h" #include "pkcs11.h" diff --git a/pkcs11/secret-store/tests/test-secret-binary.c b/pkcs11/secret-store/tests/test-secret-binary.c new file mode 100644 index 00000000..6f2e71d4 --- /dev/null +++ b/pkcs11/secret-store/tests/test-secret-binary.c @@ -0,0 +1,222 @@ +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ +/* test-secret-binary.c: Test binary keyring read and write + + 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-secret-module.h" + +#include "secret-store/gkm-secret-binary.h" +#include "secret-store/gkm-secret-collection.h" +#include "secret-store/gkm-secret-data.h" +#include "secret-store/gkm-secret-fields.h" +#include "secret-store/gkm-secret-item.h" + +#include "gkm/gkm-secret.h" + +#include "pkcs11/pkcs11i.h" + +#include <glib.h> + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +typedef struct { + GkmModule *module; + GkmSession *session; + GkmSecretCollection *collection; + GkmSecretData *sdata; +} Test; + +static void +setup (Test *test, gconstpointer unused) +{ + GkmSecret *master; + + test->module = test_secret_module_initialize_and_enter (); + test->session = test_secret_module_open_session (TRUE); + + test->collection = g_object_new (GKM_TYPE_SECRET_COLLECTION, + "module", test->module, + "identifier", "test", + "label", "brigadooooooooooooon", + NULL); + + test->sdata = g_object_new (GKM_TYPE_SECRET_DATA, NULL); + master = gkm_secret_new_from_password ("my-keyring-password"); + gkm_secret_data_set_master (test->sdata, master); + g_object_unref (master); + + g_assert (GKM_IS_SECRET_COLLECTION (test->collection)); + +} + +static void +teardown (Test *test, gconstpointer unused) +{ + g_object_unref (test->collection); + g_object_unref (test->sdata); + test_secret_module_leave_and_finalize (); +} + +static void +test_read (Test *test, gconstpointer unused) +{ + GkmDataResult res; + gchar *data; + gsize n_data; + + if (!g_file_get_contents (SRCDIR "/files/encrypted.keyring", &data, &n_data, NULL)) + g_assert_not_reached (); + res = gkm_secret_binary_read (test->collection, test->sdata, data, n_data); + g_free (data); + + test_secret_collection_validate (test->collection, test->sdata); + + g_assert (res == GKM_DATA_SUCCESS); +} + +static void +test_read_wrong_format (Test *test, gconstpointer unused) +{ + GkmDataResult res; + gchar *data; + gsize n_data; + + if (!g_file_get_contents (SRCDIR "/files/plain.keyring", &data, &n_data, NULL)) + g_assert_not_reached (); + res = gkm_secret_binary_read (test->collection, test->sdata, data, n_data); + g_free (data); + + g_assert (res == GKM_DATA_UNRECOGNIZED); +} + +static void +test_read_wrong_master (Test *test, gconstpointer unused) +{ + GkmDataResult res; + GkmSecret *master; + gchar *data; + gsize n_data; + + master = gkm_secret_new_from_password ("wrong"); + gkm_secret_data_set_master (test->sdata, master); + g_object_unref (master); + + if (!g_file_get_contents (SRCDIR "/files/encrypted.keyring", &data, &n_data, NULL)) + g_assert_not_reached (); + res = gkm_secret_binary_read (test->collection, test->sdata, data, n_data); + g_free (data); + + g_assert (res == GKM_DATA_LOCKED); +} + +static void +test_read_sdata_but_no_master (Test *test, gconstpointer unused) +{ + GkmDataResult res; + gchar *data; + gsize n_data; + + gkm_secret_data_set_master (test->sdata, NULL); + + if (!g_file_get_contents (SRCDIR "/files/encrypted.keyring", &data, &n_data, NULL)) + g_assert_not_reached (); + res = gkm_secret_binary_read (test->collection, test->sdata, data, n_data); + g_free (data); + + g_assert (res == GKM_DATA_LOCKED); +} + +static void +test_write (Test *test, gconstpointer unused) +{ + GkmDataResult res; + gpointer data; + gsize n_data; + + test_secret_collection_populate (test->collection, test->sdata); + + res = gkm_secret_binary_write (test->collection, test->sdata, &data, &n_data); + g_assert (res == GKM_DATA_SUCCESS); + g_assert (data); + g_assert (n_data); + + /* Try parsing it again */ + res = gkm_secret_binary_read (test->collection, test->sdata, data, n_data); + g_assert (res == GKM_DATA_SUCCESS); +} + +static void +test_remove_unavailable (Test *test, gconstpointer unused) +{ + GkmDataResult res; + GList *items; + gchar *data; + gsize n_data; + + if (!g_file_get_contents (SRCDIR "/files/encrypted.keyring", &data, &n_data, NULL)) + g_assert_not_reached (); + res = gkm_secret_binary_read (test->collection, test->sdata, data, n_data); + g_assert (res == GKM_DATA_SUCCESS); + + /* Two items from the file */ + items = gkm_secret_collection_get_items (test->collection); + g_assert_cmpint (g_list_length (items), ==, 2); + g_list_free (items); + + /* Fill in some more data */ + test_secret_collection_populate (test->collection, test->sdata); + + /* Should have added three more */ + items = gkm_secret_collection_get_items (test->collection); + g_assert_cmpint (g_list_length (items), ==, 5); + g_list_free (items); + + /* Re-read the keyring */ + res = gkm_secret_binary_read (test->collection, test->sdata, data, n_data); + g_assert (res == GKM_DATA_SUCCESS); + + /* And we're back to two */ + items = gkm_secret_collection_get_items (test->collection); + g_assert_cmpint (g_list_length (items), ==, 2); + g_list_free (items); + + g_free (data); +} + +int +main (int argc, char **argv) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + + g_test_add ("/secret-store/binary/read", Test, NULL, setup, test_read, teardown); + g_test_add ("/secret-store/binary/read_wrong_format", Test, NULL, setup, test_read_wrong_format, teardown); + g_test_add ("/secret-store/binary/read_wrong_master", Test, NULL, setup, test_read_wrong_master, teardown); + g_test_add ("/secret-store/binary/read_sdata_but_no_master", Test, NULL, setup, test_read_sdata_but_no_master, teardown); + g_test_add ("/secret-store/binary/write", Test, NULL, setup, test_write, teardown); + g_test_add ("/secret-store/binary/remove_unavailable", Test, NULL, setup, test_remove_unavailable, teardown); + + return g_test_run (); +} diff --git a/pkcs11/secret-store/tests/test-secret-collection.c b/pkcs11/secret-store/tests/test-secret-collection.c new file mode 100644 index 00000000..203d8abe --- /dev/null +++ b/pkcs11/secret-store/tests/test-secret-collection.c @@ -0,0 +1,598 @@ +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ +/* test-secret-test->collection.c: Test the test->collection keyring + + 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-secret-module.h" + +#include "secret-store/gkm-secret-data.h" +#include "secret-store/gkm-secret-collection.h" +#include "secret-store/gkm-secret-item.h" + +#include "gkm/gkm-credential.h" +#include "gkm/gkm-session.h" +#include "gkm/gkm-transaction.h" + +#include "pkcs11/pkcs11i.h" + +#include <glib.h> + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +typedef struct { + GkmModule *module; + GkmSession *session; + CK_OBJECT_HANDLE credential; + CK_OBJECT_HANDLE credential2; + GkmSecretCollection *collection; +} Test; + +static void +setup (Test *test, gconstpointer unused) +{ + CK_OBJECT_CLASS klass = CKO_G_CREDENTIAL; + GkmObject *cred; + + CK_ATTRIBUTE attrs[] = { + { CKA_CLASS, &klass, sizeof (klass) }, + { CKA_VALUE, NULL, 0 } + }; + + test->module = test_secret_module_initialize_and_enter (); + test->session = test_secret_module_open_session (TRUE); + + test->collection = g_object_new (GKM_TYPE_SECRET_COLLECTION, + "module", test->module, + "identifier", "test", + NULL); + g_assert (GKM_IS_SECRET_COLLECTION (test->collection)); + + /* Make two credentials */ + cred = gkm_session_create_object_for_factory (test->session, GKM_FACTORY_CREDENTIAL, NULL, + attrs, G_N_ELEMENTS (attrs)); + g_assert (cred != NULL); + test->credential = gkm_object_get_handle (GKM_OBJECT (cred)); + g_object_unref (cred); + + cred = gkm_session_create_object_for_factory (test->session, GKM_FACTORY_CREDENTIAL, NULL, + attrs, G_N_ELEMENTS (attrs)); + g_assert (cred != NULL); + test->credential2 = gkm_object_get_handle (GKM_OBJECT (cred)); + g_object_unref (cred); +} + +static void +teardown (Test *test, gconstpointer unused) +{ + if (test->collection) + g_object_unref (test->collection); + test->collection = NULL; + + test_secret_module_leave_and_finalize (); + test->module = NULL; + test->session = NULL; + test->credential = 0; +} + +static void +test_is_locked (Test *test, gconstpointer unused) +{ + gboolean locked; + + /* By default is locked */ + locked = gkm_secret_object_is_locked (GKM_SECRET_OBJECT (test->collection), test->session); + g_assert (locked == TRUE); +} + +static void +test_unlocked_data (Test *test, gconstpointer unused) +{ + GkmCredential *cred; + GkmSecretData *sdata; + CK_RV rv; + + /* Create test->credential, which unlocks test->collection */ + rv = gkm_credential_create (test->module, gkm_session_get_manager (test->session), GKM_OBJECT (test->collection), NULL, 0, &cred); + g_assert (rv == CKR_OK); + gkm_session_add_session_object (test->session, NULL, GKM_OBJECT (cred)); + g_object_unref (cred); + + /* Collection should now be unlocked */ + sdata = gkm_secret_collection_unlocked_use (test->collection, test->session); + g_assert (GKM_IS_SECRET_DATA (sdata)); + g_assert (!gkm_secret_object_is_locked (GKM_SECRET_OBJECT (test->collection), test->session)); + g_object_unref (sdata); +} + +static void +test_get_filename (Test *test, gconstpointer unused) +{ + GkmSecretCollection *other; + const gchar *filename; + + other = g_object_new (GKM_TYPE_SECRET_COLLECTION, + "module", test->module, + "identifier", "test", + "filename", "/tmp/filename.keyring", + NULL); + + filename = gkm_secret_collection_get_filename (other); + g_assert_cmpstr (filename, ==, "/tmp/filename.keyring"); + + g_object_unref (other); +} + +static void +test_set_filename (Test *test, gconstpointer unused) +{ + const gchar *filename; + + gkm_secret_collection_set_filename (test->collection, "/tmp/filename.keyring"); + + filename = gkm_secret_collection_get_filename (test->collection); + g_assert_cmpstr (filename, ==, "/tmp/filename.keyring"); +} + +static void +test_has_item (Test *test, gconstpointer unused) +{ + GkmSecretItem *item; + + item = gkm_secret_collection_new_item (test->collection, "testo"); + g_assert (gkm_secret_collection_has_item (test->collection, item)); +} + +static void +test_load_unlock_plain (Test *test, gconstpointer unused) +{ + GkmCredential *cred; + GkmSecretData *sdata; + GkmDataResult res; + CK_RV rv; + + gkm_secret_collection_set_filename (test->collection, SRCDIR "/files/plain.keyring"); + + /* Load the data in the file */ + res = gkm_secret_collection_load (test->collection); + g_assert (res == GKM_DATA_SUCCESS); + + /* Unlock the keyring, which should load again */ + rv = gkm_credential_create (test->module, gkm_session_get_manager (test->session), GKM_OBJECT (test->collection), NULL, 0, &cred); + g_assert (rv == CKR_OK); + gkm_session_add_session_object (test->session, NULL, GKM_OBJECT (cred)); + g_object_unref (cred); + + sdata = gkm_secret_collection_unlocked_use (test->collection, test->session); + g_assert (sdata != NULL && GKM_IS_SECRET_DATA (sdata)); + test_secret_collection_validate (test->collection, sdata); + g_object_unref (sdata); +} + +static void +test_load_unlock_encrypted (Test *test, gconstpointer unused) +{ + GkmCredential *cred; + GkmSecretData *sdata; + GkmDataResult res; + CK_RV rv; + + gkm_secret_collection_set_filename (test->collection, SRCDIR "/files/encrypted.keyring"); + + /* Load the data in the file */ + res = gkm_secret_collection_load (test->collection); + g_assert (res == GKM_DATA_SUCCESS); + + /* Unlock the keyring, which should load again */ + rv = gkm_credential_create (test->module, gkm_session_get_manager (test->session), GKM_OBJECT (test->collection), + (guchar*)"my-keyring-password", 19, &cred); + g_assert (rv == CKR_OK); + gkm_session_add_session_object (test->session, NULL, GKM_OBJECT (cred)); + g_object_unref (cred); + + sdata = gkm_secret_collection_unlocked_use (test->collection, test->session); + g_assert (sdata != NULL && GKM_IS_SECRET_DATA (sdata)); + test_secret_collection_validate (test->collection, sdata); + g_object_unref (sdata); +} + +static void +test_load_unlock_bad_password (Test *test, gconstpointer unused) +{ + GkmCredential *cred; + GkmDataResult res; + CK_RV rv; + + gkm_secret_collection_set_filename (test->collection, SRCDIR "/files/encrypted.keyring"); + + /* Load the data in the file */ + res = gkm_secret_collection_load (test->collection); + g_assert (res == GKM_DATA_SUCCESS); + + /* Unlock the keyring, which should load again */ + rv = gkm_credential_create (test->module, gkm_session_get_manager (test->session), GKM_OBJECT (test->collection), + (guchar*)"wrong", 5, &cred); + g_assert (rv == CKR_PIN_INCORRECT); +} + +static void +test_unlock_without_load (Test *test, gconstpointer unused) +{ + GkmCredential *cred; + GkmSecretData *sdata; + CK_RV rv; + + gkm_secret_collection_set_filename (test->collection, SRCDIR "/files/encrypted.keyring"); + + /* Unlock the keyring, which should load it */ + rv = gkm_credential_create (test->module, gkm_session_get_manager (test->session), GKM_OBJECT (test->collection), + (guchar*)"my-keyring-password", 19, &cred); + g_assert (rv == CKR_OK); + gkm_session_add_session_object (test->session, NULL, GKM_OBJECT (cred)); + g_object_unref (cred); + + sdata = gkm_secret_collection_unlocked_use (test->collection, test->session); + g_assert (sdata != NULL && GKM_IS_SECRET_DATA (sdata)); + test_secret_collection_validate (test->collection, sdata); + g_object_unref (sdata); +} + +static void +test_twice_unlock (Test *test, gconstpointer unused) +{ + GkmCredential *cred; + GkmSecretData *sdata; + CK_RV rv; + + gkm_secret_collection_set_filename (test->collection, SRCDIR "/files/encrypted.keyring"); + + /* Unlock the keyring, which should load */ + rv = gkm_credential_create (test->module, gkm_session_get_manager (test->session), GKM_OBJECT (test->collection), + (guchar*)"my-keyring-password", 19, &cred); + g_assert (rv == CKR_OK); + gkm_session_add_session_object (test->session, NULL, GKM_OBJECT (cred)); + g_object_unref (cred); + + /* Unlock the keyring again, which should not reload */ + rv = gkm_credential_create (test->module, gkm_session_get_manager (test->session), GKM_OBJECT (test->collection), + (guchar*)"my-keyring-password", 19, &cred); + g_assert (rv == CKR_OK); + gkm_session_add_session_object (test->session, NULL, GKM_OBJECT (cred)); + g_object_unref (cred); + + sdata = gkm_secret_collection_unlocked_use (test->collection, test->session); + g_assert (sdata != NULL && GKM_IS_SECRET_DATA (sdata)); + test_secret_collection_validate (test->collection, sdata); + g_object_unref (sdata); +} + +static void +test_twice_unlock_bad_password (Test *test, gconstpointer unused) +{ + GkmCredential *cred; + GkmSecretData *sdata; + CK_RV rv; + + gkm_secret_collection_set_filename (test->collection, SRCDIR "/files/encrypted.keyring"); + + /* Unlock the keyring, which should load */ + rv = gkm_credential_create (test->module, gkm_session_get_manager (test->session), GKM_OBJECT (test->collection), + (guchar*)"my-keyring-password", 19, &cred); + g_assert (rv == CKR_OK); + gkm_session_add_session_object (test->session, NULL, GKM_OBJECT (cred)); + g_object_unref (cred); + + /* Unlock the keyring again, wrong password */ + rv = gkm_credential_create (test->module, gkm_session_get_manager (test->session), GKM_OBJECT (test->collection), + (guchar*)"wrong", 5, &cred); + g_assert (rv == CKR_PIN_INCORRECT); + + sdata = gkm_secret_collection_unlocked_use (test->collection, test->session); + g_assert (sdata != NULL && GKM_IS_SECRET_DATA (sdata)); + test_secret_collection_validate (test->collection, sdata); + g_object_unref (sdata); +} + +static void +test_memory_unlock (Test *test, gconstpointer unused) +{ + GkmCredential *cred; + GkmDataResult res; + CK_RV rv; + + /* Load the data in the file */ + res = gkm_secret_collection_load (test->collection); + g_assert (res == GKM_DATA_SUCCESS); + + /* Unlock the keyring, which should load again */ + rv = gkm_credential_create (test->module, gkm_session_get_manager (test->session), GKM_OBJECT (test->collection), + NULL, 0, &cred); + g_assert (rv == CKR_OK); + gkm_session_add_session_object (test->session, NULL, GKM_OBJECT (cred)); + g_object_unref (cred); +} + +static void +test_memory_unlock_bad_password (Test *test, gconstpointer unused) +{ + GkmCredential *cred; + GkmDataResult res; + CK_RV rv; + + /* Load the data in the file */ + res = gkm_secret_collection_load (test->collection); + g_assert (res == GKM_DATA_SUCCESS); + + /* Unlock the keyring, which should load again */ + rv = gkm_credential_create (test->module, gkm_session_get_manager (test->session), GKM_OBJECT (test->collection), + (guchar*)"wrong", 5, &cred); + g_assert (rv == CKR_PIN_INCORRECT); +} + +static void +test_factory (Test *test, gconstpointer unused) +{ + CK_OBJECT_CLASS klass = CKO_G_COLLECTION; + GkmObject *object; + + CK_ATTRIBUTE attrs[] = { + { CKA_CLASS, &klass, sizeof (klass) }, + { CKA_LABEL, "blah", 4 }, + { CKA_G_CREDENTIAL, &test->credential, sizeof (test->credential) }, + }; + + object = gkm_session_create_object_for_factory (test->session, GKM_FACTORY_SECRET_COLLECTION, NULL, + attrs, G_N_ELEMENTS (attrs)); + g_assert (object != NULL); + g_assert (GKM_IS_SECRET_COLLECTION (object)); + + g_assert_cmpstr (gkm_secret_object_get_label (GKM_SECRET_OBJECT (object)), ==, "blah"); + g_object_unref (object); +} + +static void +test_factory_unnamed (Test *test, gconstpointer unused) +{ + CK_OBJECT_CLASS klass = CKO_G_COLLECTION; + const gchar *identifier; + GkmObject *object; + + CK_ATTRIBUTE attrs[] = { + { CKA_CLASS, &klass, sizeof (klass) }, + { CKA_G_CREDENTIAL, &test->credential, sizeof (test->credential) }, + }; + + object = gkm_session_create_object_for_factory (test->session, GKM_FACTORY_SECRET_COLLECTION, NULL, + attrs, G_N_ELEMENTS (attrs)); + g_assert (object != NULL); + g_assert (GKM_IS_SECRET_COLLECTION (object)); + + identifier = gkm_secret_object_get_identifier (GKM_SECRET_OBJECT (object)); + g_assert_cmpstr (identifier, !=, ""); + g_object_unref (object); +} + +static void +test_factory_token (Test *test, gconstpointer unused) +{ + CK_OBJECT_CLASS klass = CKO_G_COLLECTION; + const gchar *identifier; + GkmObject *object; + CK_BBOOL token = CK_TRUE; + + CK_ATTRIBUTE attrs[] = { + { CKA_CLASS, &klass, sizeof (klass) }, + { CKA_TOKEN, &token, sizeof (token) }, + { CKA_LABEL, "blah", 4 }, + { CKA_G_CREDENTIAL, &test->credential, sizeof (test->credential) }, + }; + + object = gkm_session_create_object_for_factory (test->session, GKM_FACTORY_SECRET_COLLECTION, NULL, + attrs, G_N_ELEMENTS (attrs)); + g_assert (object != NULL); + g_assert (GKM_IS_SECRET_COLLECTION (object)); + + identifier = gkm_secret_object_get_identifier (GKM_SECRET_OBJECT (object)); + g_assert (strstr (identifier, "blah")); + g_object_unref (object); +} + +static void +test_factory_duplicate (Test *test, gconstpointer unused) +{ + CK_OBJECT_CLASS klass = CKO_G_COLLECTION; + const gchar *identifier1, *identifier2; + GkmObject *object; + + CK_ATTRIBUTE attrs[] = { + { CKA_G_CREDENTIAL, &test->credential, sizeof (test->credential) }, + { CKA_CLASS, &klass, sizeof (klass) }, + { CKA_LABEL, "blah", 4 }, + }; + + object = gkm_session_create_object_for_factory (test->session, GKM_FACTORY_SECRET_COLLECTION, NULL, + attrs, G_N_ELEMENTS (attrs)); + g_assert (object != NULL); + g_assert (GKM_IS_SECRET_COLLECTION (object)); + + identifier1 = gkm_secret_object_get_identifier (GKM_SECRET_OBJECT (object)); + g_assert (strstr (identifier1, "blah")); + g_object_unref (object); + + /* Use second test->credential for second object */ + attrs[0].pValue = &test->credential2; + object = gkm_session_create_object_for_factory (test->session, GKM_FACTORY_SECRET_COLLECTION, NULL, + attrs, G_N_ELEMENTS (attrs)); + g_assert (object != NULL); + g_assert (GKM_IS_SECRET_COLLECTION (object)); + + identifier2 = gkm_secret_object_get_identifier (GKM_SECRET_OBJECT (object)); + g_assert (strstr (identifier2, "blah")); + g_object_unref (object); + + g_assert_cmpstr (identifier1, !=, identifier2); +} + +static void +test_factory_item (Test *test, gconstpointer unused) +{ + CK_OBJECT_CLASS c_klass = CKO_G_COLLECTION; + CK_OBJECT_CLASS i_klass = CKO_SECRET_KEY; + const gchar *identifier; + GkmObject *object; + CK_BBOOL token = CK_TRUE; + + CK_ATTRIBUTE c_attrs[] = { + { CKA_CLASS, &c_klass, sizeof (c_klass) }, + { CKA_TOKEN, &token, sizeof (token) }, + { CKA_LABEL, "three", 5 }, + { CKA_G_CREDENTIAL, &test->credential, sizeof (test->credential) }, + }; + + CK_ATTRIBUTE i_attrs[] = { + { CKA_G_COLLECTION, NULL, 0 }, /* Filled below */ + { CKA_CLASS, &i_klass, sizeof (i_klass) }, + { CKA_TOKEN, &token, sizeof (token) }, + { CKA_LABEL, "Item", 4 }, + }; + + object = gkm_session_create_object_for_factory (test->session, GKM_FACTORY_SECRET_COLLECTION, NULL, + c_attrs, G_N_ELEMENTS (c_attrs)); + g_assert (object != NULL); + g_assert (GKM_IS_SECRET_COLLECTION (object)); + identifier = gkm_secret_object_get_identifier (GKM_SECRET_OBJECT (object)); + g_object_unref (object); + + i_attrs[0].pValue = (gpointer)identifier; + i_attrs[0].ulValueLen = strlen (identifier); + object = gkm_session_create_object_for_factory (test->session, GKM_FACTORY_SECRET_ITEM, NULL, + i_attrs, G_N_ELEMENTS (i_attrs)); + g_assert (object != NULL); + g_assert (GKM_IS_SECRET_ITEM (object)); + g_object_unref (object); +} + +static void +test_token_remove (Test *test, gconstpointer unused) +{ + CK_OBJECT_CLASS klass = CKO_G_COLLECTION; + GkmTransaction *transaction; + GkmObject *object; + CK_BBOOL token = CK_TRUE; + + CK_ATTRIBUTE attrs[] = { + { CKA_CLASS, &klass, sizeof (klass) }, + { CKA_TOKEN, &token, sizeof (token) }, + { CKA_LABEL, "blah", 4 }, + { CKA_G_CREDENTIAL, &test->credential, sizeof (test->credential) }, + }; + + object = gkm_session_create_object_for_factory (test->session, GKM_FACTORY_SECRET_COLLECTION, NULL, + attrs, G_N_ELEMENTS (attrs)); + g_assert (object != NULL); + g_assert (GKM_IS_SECRET_COLLECTION (object)); + + transaction = gkm_transaction_new (); + gkm_module_remove_token_object (test->module, transaction, object); + g_assert (!gkm_transaction_get_failed (transaction)); + gkm_transaction_complete (transaction); + g_object_unref (transaction); + g_object_unref (object); +} + +static void +test_token_item_remove (Test *test, gconstpointer unused) +{ + CK_OBJECT_CLASS c_klass = CKO_G_COLLECTION; + CK_OBJECT_CLASS i_klass = CKO_SECRET_KEY; + GkmTransaction *transaction; + const gchar *identifier; + GkmObject *object; + CK_BBOOL token = CK_TRUE; + + CK_ATTRIBUTE c_attrs[] = { + { CKA_CLASS, &c_klass, sizeof (c_klass) }, + { CKA_TOKEN, &token, sizeof (token) }, + { CKA_LABEL, "three", 5 }, + { CKA_G_CREDENTIAL, &test->credential, sizeof (test->credential) }, + }; + + CK_ATTRIBUTE i_attrs[] = { + { CKA_G_COLLECTION, NULL, 0 }, /* Filled below */ + { CKA_CLASS, &i_klass, sizeof (i_klass) }, + { CKA_TOKEN, &token, sizeof (token) }, + { CKA_LABEL, "Item", 4 }, + }; + + object = gkm_session_create_object_for_factory (test->session, GKM_FACTORY_SECRET_COLLECTION, NULL, + c_attrs, G_N_ELEMENTS (c_attrs)); + g_assert (object != NULL); + g_assert (GKM_IS_SECRET_COLLECTION (object)); + identifier = gkm_secret_object_get_identifier (GKM_SECRET_OBJECT (object)); + g_object_unref (object); + + i_attrs[0].pValue = (gpointer)identifier; + i_attrs[0].ulValueLen = strlen (identifier); + object = gkm_session_create_object_for_factory (test->session, GKM_FACTORY_SECRET_ITEM, NULL, + i_attrs, G_N_ELEMENTS (i_attrs)); + g_assert (object != NULL); + g_assert (GKM_IS_SECRET_ITEM (object)); + + transaction = gkm_transaction_new (); + gkm_module_remove_token_object (test->module, transaction, object); + g_assert (!gkm_transaction_get_failed (transaction)); + gkm_transaction_complete (transaction); + g_object_unref (transaction); + g_object_unref (object); +} + +int +main (int argc, char **argv) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + + g_test_add ("/secret-store/collection/is_locked", Test, NULL, setup, test_is_locked, teardown); + g_test_add ("/secret-store/collection/unlocked_data", Test, NULL, setup, test_unlocked_data, teardown); + g_test_add ("/secret-store/collection/get_filename", Test, NULL, setup, test_get_filename, teardown); + g_test_add ("/secret-store/collection/set_filename", Test, NULL, setup, test_set_filename, teardown); + g_test_add ("/secret-store/collection/has_item", Test, NULL, setup, test_has_item, teardown); + g_test_add ("/secret-store/collection/load_unlock_plain", Test, NULL, setup, test_load_unlock_plain, teardown); + g_test_add ("/secret-store/collection/load_unlock_encrypted", Test, NULL, setup, test_load_unlock_encrypted, teardown); + g_test_add ("/secret-store/collection/load_unlock_bad_password", Test, NULL, setup, test_load_unlock_bad_password, teardown); + g_test_add ("/secret-store/collection/unlock_without_load", Test, NULL, setup, test_unlock_without_load, teardown); + g_test_add ("/secret-store/collection/twice_unlock", Test, NULL, setup, test_twice_unlock, teardown); + g_test_add ("/secret-store/collection/twice_unlock_bad_password", Test, NULL, setup, test_twice_unlock_bad_password, teardown); + g_test_add ("/secret-store/collection/memory_unlock", Test, NULL, setup, test_memory_unlock, teardown); + g_test_add ("/secret-store/collection/memory_unlock_bad_password", Test, NULL, setup, test_memory_unlock_bad_password, teardown); + g_test_add ("/secret-store/collection/factory", Test, NULL, setup, test_factory, teardown); + g_test_add ("/secret-store/collection/factory_unnamed", Test, NULL, setup, test_factory_unnamed, teardown); + g_test_add ("/secret-store/collection/factory_token", Test, NULL, setup, test_factory_token, teardown); + g_test_add ("/secret-store/collection/factory_duplicate", Test, NULL, setup, test_factory_duplicate, teardown); + g_test_add ("/secret-store/collection/factory_item", Test, NULL, setup, test_factory_item, teardown); + g_test_add ("/secret-store/collection/token_remove", Test, NULL, setup, test_token_remove, teardown); + g_test_add ("/secret-store/collection/token_item_remove", Test, NULL, setup, test_token_item_remove, teardown); + + return g_test_run (); +} diff --git a/pkcs11/secret-store/tests/unit-test-secret-compat.c b/pkcs11/secret-store/tests/test-secret-compat.c index 925c729c..f11b0e2e 100644 --- a/pkcs11/secret-store/tests/unit-test-secret-compat.c +++ b/pkcs11/secret-store/tests/test-secret-compat.c @@ -1,5 +1,5 @@ /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* unit-test-secret-compat.c: Test secret compat files +/* test-secret-compat.c: Test secret compat files Copyright (C) 2008 Stefan Walter @@ -23,9 +23,7 @@ #include "config.h" -#include "test-suite.h" - -#include "gkm-secret-compat.h" +#include "secret-store/gkm-secret-compat.h" #include <glib.h> @@ -33,7 +31,8 @@ #include <stdio.h> #include <string.h> -TESTING_TEST(access_free) +static void +test_access_free (void) { GkmSecretAccess *ac; @@ -45,7 +44,8 @@ TESTING_TEST(access_free) gkm_secret_compat_access_free (ac); } -TESTING_TEST(acl_free) +static void +test_acl_free (void) { GkmSecretAccess *ac; GList *acl = NULL; @@ -62,7 +62,8 @@ TESTING_TEST(acl_free) gkm_secret_compat_acl_free (acl); } -TESTING_TEST(parse_item_type) +static void +test_parse_item_type (void) { guint type; @@ -88,7 +89,8 @@ TESTING_TEST(parse_item_type) g_assert_cmpuint (type, ==, 0); } -TESTING_TEST(format_item_type) +static void +test_format_item_type (void) { const gchar *type; @@ -113,3 +115,16 @@ TESTING_TEST(format_item_type) type = gkm_secret_compat_format_item_type (32); g_assert (type == NULL); } + +int +main (int argc, char **argv) +{ + g_test_init (&argc, &argv, NULL); + + g_test_add_func ("/secret-store/compat/access_free", test_access_free); + g_test_add_func ("/secret-store/compat/acl_free", test_acl_free); + g_test_add_func ("/secret-store/compat/parse_item_type", test_parse_item_type); + g_test_add_func ("/secret-store/compat/format_item_type", test_format_item_type); + + return g_test_run (); +} diff --git a/pkcs11/secret-store/tests/unit-test-secret-data.c b/pkcs11/secret-store/tests/test-secret-data.c index efa6b259..a71e3019 100644 --- a/pkcs11/secret-store/tests/unit-test-secret-data.c +++ b/pkcs11/secret-store/tests/test-secret-data.c @@ -1,5 +1,5 @@ /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* unit-test-secret-compat.c: Test secret compat files +/* test-secret-compat.c: Test secret compat files Copyright (C) 2008 Stefan Walter @@ -23,27 +23,31 @@ #include "config.h" -#include "test-suite.h" - -#include "gkm-secret-data.h" +#include "secret-store/gkm-secret-data.h" #include "gkm/gkm-secret.h" #include "gkm/gkm-transaction.h" +#include "egg/egg-secure-memory.h" + #include <glib.h> #include <stdlib.h> #include <stdio.h> #include <string.h> -TESTING_TEST(secret_data_new) +EGG_SECURE_GLIB_DEFINITIONS (); + +static void +test_new (void) { GkmSecretData *data = g_object_new (GKM_TYPE_SECRET_DATA, NULL); g_assert (GKM_IS_SECRET_DATA (data)); g_object_unref (data); } -TESTING_TEST(secret_data_get_set) +static void +test_get_set (void) { GkmSecretData *data = g_object_new (GKM_TYPE_SECRET_DATA, NULL); GkmSecret *secret = gkm_secret_new_from_password ("barn"); @@ -60,7 +64,8 @@ TESTING_TEST(secret_data_get_set) g_object_unref (data); } -TESTING_TEST(secret_data_get_raw) +static void +test_get_raw (void) { GkmSecretData *data = g_object_new (GKM_TYPE_SECRET_DATA, NULL); GkmSecret *secret = gkm_secret_new_from_password ("barn"); @@ -81,7 +86,8 @@ TESTING_TEST(secret_data_get_raw) g_object_unref (data); } -TESTING_TEST(secret_data_remove) +static void +test_remove (void) { GkmSecretData *data = g_object_new (GKM_TYPE_SECRET_DATA, NULL); GkmSecret *secret = gkm_secret_new_from_password ("barn"); @@ -99,7 +105,8 @@ TESTING_TEST(secret_data_remove) g_object_unref (data); } -TESTING_TEST(secret_data_set_transacted) +static void +test_set_transacted (void) { GkmTransaction *transaction = gkm_transaction_new (); GkmSecretData *data = g_object_new (GKM_TYPE_SECRET_DATA, NULL); @@ -120,7 +127,8 @@ TESTING_TEST(secret_data_set_transacted) g_object_unref (transaction); } -TESTING_TEST(secret_data_set_transacted_replace) +static void +test_set_transacted_replace (void) { GkmTransaction *transaction = gkm_transaction_new (); GkmSecretData *data = g_object_new (GKM_TYPE_SECRET_DATA, NULL); @@ -147,7 +155,8 @@ TESTING_TEST(secret_data_set_transacted_replace) g_object_unref (transaction); } -TESTING_TEST(secret_data_set_transacted_fail) +static void +test_set_transacted_fail (void) { GkmTransaction *transaction = gkm_transaction_new (); GkmSecretData *data = g_object_new (GKM_TYPE_SECRET_DATA, NULL); @@ -169,7 +178,8 @@ TESTING_TEST(secret_data_set_transacted_fail) g_object_unref (transaction); } -TESTING_TEST(secret_data_set_transacted_fail_revert) +static void +test_set_transacted_fail_revert (void) { GkmTransaction *transaction = gkm_transaction_new (); GkmSecretData *data = g_object_new (GKM_TYPE_SECRET_DATA, NULL); @@ -197,7 +207,8 @@ TESTING_TEST(secret_data_set_transacted_fail_revert) g_object_unref (transaction); } -TESTING_TEST(secret_data_get_set_master) +static void +test_get_set_master (void) { GkmSecretData *data = g_object_new (GKM_TYPE_SECRET_DATA, NULL); GkmSecret *master = gkm_secret_new_from_password ("master"); @@ -213,3 +224,22 @@ TESTING_TEST(secret_data_get_set_master) g_object_unref (data); } + +int +main (int argc, char **argv) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + + g_test_add_func ("/secret-store/data/new", test_new); + g_test_add_func ("/secret-store/data/get_set", test_get_set); + g_test_add_func ("/secret-store/data/get_raw", test_get_raw); + g_test_add_func ("/secret-store/data/remove", test_remove); + g_test_add_func ("/secret-store/data/set_transacted", test_set_transacted); + g_test_add_func ("/secret-store/data/set_transacted_replace", test_set_transacted_replace); + g_test_add_func ("/secret-store/data/set_transacted_fail", test_set_transacted_fail); + g_test_add_func ("/secret-store/data/set_transacted_fail_revert", test_set_transacted_fail_revert); + g_test_add_func ("/secret-store/data/get_set_master", test_get_set_master); + + return g_test_run (); +} diff --git a/pkcs11/secret-store/tests/unit-test-secret-fields.c b/pkcs11/secret-store/tests/test-secret-fields.c index d69939d3..96bb6071 100644 --- a/pkcs11/secret-store/tests/unit-test-secret-fields.c +++ b/pkcs11/secret-store/tests/test-secret-fields.c @@ -1,5 +1,5 @@ /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* unit-test-secret-fields.c: Test secret fields +/* test-secret-fields.c: Test secret fields Copyright (C) 2009 Stefan Walter @@ -23,9 +23,7 @@ #include "config.h" -#include "test-suite.h" - -#include "gkm-secret-fields.h" +#include "secret-store/gkm-secret-fields.h" #include "pkcs11/pkcs11i.h" @@ -35,20 +33,23 @@ #include <stdio.h> #include <string.h> -TESTING_TEST(fields_new) +static void +test_new (void) { GHashTable *fields = gkm_secret_fields_new (); g_hash_table_unref (fields); } -TESTING_TEST(fields_boxed) +static void +test_boxed (void) { GType boxed = gkm_secret_fields_boxed_type (); GType check = gkm_secret_fields_boxed_type (); g_assert (boxed == check); } -TESTING_TEST(fields_add_get_values) +static void +test_add_get_values (void) { GHashTable *fields = gkm_secret_fields_new (); const gchar *value; @@ -69,7 +70,8 @@ TESTING_TEST(fields_add_get_values) g_hash_table_unref (fields); } -TESTING_TEST(fields_parse) +static void +test_parse (void) { CK_ATTRIBUTE attr = { CKA_G_FIELDS, "one\0value1\0two\0value2\0three\0value3\0", 35 }; GHashTable *fields; @@ -90,7 +92,8 @@ TESTING_TEST(fields_parse) g_hash_table_unref (fields); } -TESTING_TEST(fields_parse_empty) +static void +test_parse_empty (void) { CK_ATTRIBUTE attr = { CKA_G_FIELDS, "", 0 }; GHashTable *fields; @@ -104,7 +107,8 @@ TESTING_TEST(fields_parse_empty) g_hash_table_unref (fields); } -TESTING_TEST(fields_parse_null_invalid) +static void +test_parse_null_invalid (void) { CK_ATTRIBUTE attr = { CKA_G_FIELDS, NULL, 5 }; GHashTable *fields; @@ -114,7 +118,8 @@ TESTING_TEST(fields_parse_null_invalid) g_assert (rv == CKR_ATTRIBUTE_VALUE_INVALID); } -TESTING_TEST(fields_parse_missing_value) +static void +test_parse_missing_value (void) { CK_ATTRIBUTE attr = { CKA_G_FIELDS, "one", 3 }; GHashTable *fields; @@ -124,7 +129,8 @@ TESTING_TEST(fields_parse_missing_value) g_assert (rv == CKR_ATTRIBUTE_VALUE_INVALID); } -TESTING_TEST(fields_parse_missing_terminator) +static void +test_parse_missing_terminator (void) { CK_ATTRIBUTE attr = { CKA_G_FIELDS, "one\0value", 9 }; GHashTable *fields; @@ -134,7 +140,8 @@ TESTING_TEST(fields_parse_missing_terminator) g_assert (rv == CKR_ATTRIBUTE_VALUE_INVALID); } -TESTING_TEST(fields_parse_not_utf8) +static void +test_parse_not_utf8 (void) { CK_ATTRIBUTE attr = { CKA_G_FIELDS, "one\0not\234utf8\0", 13 }; GHashTable *fields; @@ -144,7 +151,8 @@ TESTING_TEST(fields_parse_not_utf8) g_assert (rv == CKR_ATTRIBUTE_VALUE_INVALID); } -TESTING_TEST(fields_serialize) +static void +test_serialize (void) { gchar buffer[32]; CK_ATTRIBUTE attr = { CKA_G_FIELDS, buffer, 32 }; @@ -162,7 +170,8 @@ TESTING_TEST(fields_serialize) g_hash_table_unref (fields); } -TESTING_TEST(fields_serialize_length) +static void +test_serialize_length (void) { CK_ATTRIBUTE attr = { CKA_G_FIELDS, NULL, 0 }; GHashTable *fields; @@ -178,7 +187,8 @@ TESTING_TEST(fields_serialize_length) g_hash_table_unref (fields); } -TESTING_TEST(fields_add_get_compat_uint32) +static void +test_add_get_compat_uint32 (void) { GHashTable *fields; gboolean ret; @@ -194,7 +204,8 @@ TESTING_TEST(fields_add_get_compat_uint32) g_hash_table_unref (fields); } -TESTING_TEST(fields_get_compat_uint32_fail) +static void +test_get_compat_uint32_fail (void) { GHashTable *fields; gboolean ret; @@ -209,7 +220,8 @@ TESTING_TEST(fields_get_compat_uint32_fail) g_hash_table_unref (fields); } -TESTING_TEST(fields_get_compat_hashed_string) +static void +test_get_compat_hashed_string (void) { GHashTable *fields; gboolean ret; @@ -226,7 +238,8 @@ TESTING_TEST(fields_get_compat_hashed_string) g_hash_table_unref (fields); } -TESTING_TEST(fields_get_compat_hashed_already) +static void +test_get_compat_hashed_already (void) { GHashTable *fields; gboolean ret; @@ -243,7 +256,8 @@ TESTING_TEST(fields_get_compat_hashed_already) g_hash_table_unref (fields); } -TESTING_TEST(fields_get_compat_hashed_uint32) +static void +test_get_compat_hashed_uint32 (void) { GHashTable *fields; gboolean ret; @@ -260,7 +274,8 @@ TESTING_TEST(fields_get_compat_hashed_uint32) g_hash_table_unref (fields); } -TESTING_TEST(fields_get_compat_hashed_uint32_already) +static void +test_get_compat_hashed_uint32_already (void) { GHashTable *fields; gboolean ret; @@ -277,7 +292,8 @@ TESTING_TEST(fields_get_compat_hashed_uint32_already) g_hash_table_unref (fields); } -TESTING_TEST(fields_get_names) +static void +test_get_names (void) { GHashTable *fields; GList *names, *l; @@ -303,7 +319,8 @@ TESTING_TEST(fields_get_names) g_hash_table_unref (fields); } -TESTING_TEST(fields_match) +static void +test_match (void) { GHashTable *haystack; GHashTable *needle; @@ -329,7 +346,8 @@ TESTING_TEST(fields_match) g_hash_table_unref (needle); } -TESTING_TEST(fields_match_mismatch_value) +static void +test_match_mismatch_value (void) { GHashTable *haystack; GHashTable *needle; @@ -348,7 +366,8 @@ TESTING_TEST(fields_match_mismatch_value) g_hash_table_unref (needle); } -TESTING_TEST(fields_match_mismatch_field) +static void +test_match_mismatch_field (void) { GHashTable *haystack; GHashTable *needle; @@ -367,7 +386,8 @@ TESTING_TEST(fields_match_mismatch_field) g_hash_table_unref (needle); } -TESTING_TEST(fields_match_wrong_hashed) +static void +test_match_wrong_hashed (void) { GHashTable *haystack; GHashTable *needle; @@ -385,3 +405,35 @@ TESTING_TEST(fields_match_wrong_hashed) g_hash_table_unref (haystack); g_hash_table_unref (needle); } + +int +main (int argc, char **argv) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + + g_test_add_func ("/secret-store/fields/new", test_new); + g_test_add_func ("/secret-store/fields/boxed", test_boxed); + g_test_add_func ("/secret-store/fields/add_get_values", test_add_get_values); + g_test_add_func ("/secret-store/fields/parse", test_parse); + g_test_add_func ("/secret-store/fields/parse_empty", test_parse_empty); + g_test_add_func ("/secret-store/fields/parse_null_invalid", test_parse_null_invalid); + g_test_add_func ("/secret-store/fields/parse_missing_value", test_parse_missing_value); + g_test_add_func ("/secret-store/fields/parse_missing_terminator", test_parse_missing_terminator); + g_test_add_func ("/secret-store/fields/parse_not_utf8", test_parse_not_utf8); + g_test_add_func ("/secret-store/fields/serialize", test_serialize); + g_test_add_func ("/secret-store/fields/serialize_length", test_serialize_length); + g_test_add_func ("/secret-store/fields/add_get_compat_uint32", test_add_get_compat_uint32); + g_test_add_func ("/secret-store/fields/get_compat_uint32_fail", test_get_compat_uint32_fail); + g_test_add_func ("/secret-store/fields/get_compat_hashed_string", test_get_compat_hashed_string); + g_test_add_func ("/secret-store/fields/get_compat_hashed_already", test_get_compat_hashed_already); + g_test_add_func ("/secret-store/fields/get_compat_hashed_uint32", test_get_compat_hashed_uint32); + g_test_add_func ("/secret-store/fields/get_compat_hashed_uint32_already", test_get_compat_hashed_uint32_already); + g_test_add_func ("/secret-store/fields/get_names", test_get_names); + g_test_add_func ("/secret-store/fields/match", test_match); + g_test_add_func ("/secret-store/fields/match_mismatch_value", test_match_mismatch_value); + g_test_add_func ("/secret-store/fields/match_mismatch_field", test_match_mismatch_field); + g_test_add_func ("/secret-store/fields/match_wrong_hashed", test_match_wrong_hashed); + + return g_test_run (); +} diff --git a/pkcs11/secret-store/tests/test-secret-item.c b/pkcs11/secret-store/tests/test-secret-item.c new file mode 100644 index 00000000..2198cf2e --- /dev/null +++ b/pkcs11/secret-store/tests/test-secret-item.c @@ -0,0 +1,482 @@ +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ +/* unit-test-secret-item.c: Test secret item + + 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-secret-module.h" + +#include "secret-store/gkm-secret-collection.h" +#include "secret-store/gkm-secret-fields.h" +#include "secret-store/gkm-secret-item.h" + +#include "gkm/gkm-credential.h" +#include "gkm/gkm-session.h" +#include "gkm/gkm-transaction.h" + +#include "pkcs11/pkcs11i.h" + +#include <glib.h> + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +typedef struct { + GkmModule *module; + GkmSession *session; + GkmSecretCollection *collection; +} Test; + +static void +setup (Test *test, gconstpointer unused) +{ + test->module = test_secret_module_initialize_and_enter (); + test->session = test_secret_module_open_session (TRUE); + + test->collection = g_object_new (GKM_TYPE_SECRET_COLLECTION, + "module", test->module, + "identifier", "test", + NULL); +} + +static void +teardown (Test *test, gconstpointer unused) +{ + if (test->collection) + g_object_unref (test->collection); + test_secret_module_leave_and_finalize (); +} + +static void +unlock_collection (Test *test) +{ + GkmCredential *cred; + GkmObject *object; + CK_RV rv; + + /* Create credential, which unlocks test->collection */ + object = GKM_OBJECT (test->collection); + rv = gkm_credential_create (gkm_object_get_module (object), + gkm_session_get_manager (test->session), + object, NULL, 0, &cred); + g_assert (rv == CKR_OK); + + gkm_session_add_session_object (test->session, NULL, GKM_OBJECT (cred)); + g_object_unref (cred); +} + +static void +test_new (Test *test, gconstpointer unused) +{ + GkmSecretItem *item; + + item = gkm_secret_collection_new_item (test->collection, "the-identifier"); + g_assert (GKM_IS_SECRET_ITEM (item)); + g_assert_cmpstr (gkm_secret_object_get_identifier (GKM_SECRET_OBJECT (item)), ==, "the-identifier"); +} + +static void +test_create (Test *test, gconstpointer unused) +{ + GkmTransaction *transaction; + GkmSecretItem *item; + + transaction = gkm_transaction_new (); + item = gkm_secret_collection_create_item (test->collection, transaction); + g_assert (GKM_IS_SECRET_ITEM (item)); + g_object_ref (item); + g_assert (gkm_secret_collection_has_item (test->collection, item)); + + gkm_transaction_complete (transaction); + g_object_unref (transaction); + + /* Should still be there */ + g_assert (gkm_secret_collection_has_item (test->collection, item)); + g_object_unref (item); +} + +static void +test_create_failed (Test *test, gconstpointer unused) +{ + GkmTransaction *transaction; + GkmSecretItem *item; + + transaction = gkm_transaction_new (); + item = gkm_secret_collection_create_item (test->collection, transaction); + g_assert (GKM_IS_SECRET_ITEM (item)); + g_object_ref (item); + g_assert (gkm_secret_collection_has_item (test->collection, item)); + + gkm_transaction_fail (transaction, CKR_GENERAL_ERROR); + gkm_transaction_complete (transaction); + g_object_unref (transaction); + + /* Should no longer be there */ + g_assert (!gkm_secret_collection_has_item (test->collection, item)); + g_object_unref (item); +} + +static void +test_destroy (Test *test, gconstpointer unused) +{ + GkmTransaction *transaction; + GkmSecretItem *item; + + item = gkm_secret_collection_new_item (test->collection, "the-identifier"); + g_assert (gkm_secret_collection_has_item (test->collection, item)); + g_object_ref (item); + + transaction = gkm_transaction_new (); + gkm_secret_collection_destroy_item (test->collection, transaction, item); + g_assert (!gkm_secret_collection_has_item (test->collection, item)); + + gkm_transaction_complete (transaction); + g_object_unref (transaction); + + /* Should not be there */ + g_assert (!gkm_secret_collection_has_item (test->collection, item)); + g_object_unref (item); +} + +static void +test_destroy_failed (Test *test, gconstpointer unused) +{ + GkmTransaction *transaction; + GkmSecretItem *item; + + item = gkm_secret_collection_new_item (test->collection, "the-identifier"); + g_assert (gkm_secret_collection_has_item (test->collection, item)); + g_object_ref (item); + + transaction = gkm_transaction_new (); + gkm_secret_collection_destroy_item (test->collection, transaction, item); + g_assert (!gkm_secret_collection_has_item (test->collection, item)); + + gkm_transaction_fail (transaction, CKR_GENERAL_ERROR); + gkm_transaction_complete (transaction); + g_object_unref (transaction); + + /* Should be there */ + g_assert (gkm_secret_collection_has_item (test->collection, item)); + g_object_unref (item); +} + +static void +test_collection_get (Test *test, gconstpointer unused) +{ + GkmSecretItem *item, *check; + + item = gkm_secret_collection_new_item (test->collection, "the-identifier"); + g_assert (GKM_IS_SECRET_ITEM (item)); + + check = gkm_secret_collection_get_item (test->collection, "the-identifier"); + g_assert (item == check); +} + +static void +test_collection_items (Test *test, gconstpointer unused) +{ + GList *l, *items; + const gchar *identifier; + + gkm_secret_collection_new_item (test->collection, "one-identifier"); + gkm_secret_collection_new_item (test->collection, "two-identifier"); + gkm_secret_collection_new_item (test->collection, "three-identifier"); + + items = gkm_secret_collection_get_items (test->collection); + g_assert_cmpuint (g_list_length (items), ==, 3); + for (l = items; l; l = g_list_next (l)) { + identifier = gkm_secret_object_get_identifier (l->data); + if (!g_str_equal (identifier, "one-identifier") && + !g_str_equal (identifier, "two-identifier") && + !g_str_equal (identifier, "three-identifier")) + g_assert_not_reached (); + } + + g_list_free (items); +} + +static void +test_collection_remove (Test *test, gconstpointer unused) +{ + GkmSecretItem *item; + + item = gkm_secret_collection_new_item (test->collection, "the-identifier"); + g_assert (gkm_secret_collection_get_item (test->collection, "the-identifier") == item); + + gkm_secret_collection_remove_item (test->collection, item); + g_assert (gkm_secret_collection_get_item (test->collection, "the-identifier") == NULL); +} + +static void +test_is_locked (Test *test, gconstpointer unused) +{ + GkmSecretItem *item; + + item = gkm_secret_collection_new_item (test->collection, "the-identifier"); + g_assert (gkm_secret_object_is_locked (GKM_SECRET_OBJECT (item), test->session) == + gkm_secret_object_is_locked (GKM_SECRET_OBJECT (test->collection), test->session)); + + unlock_collection (test); + + g_assert (gkm_secret_object_is_locked (GKM_SECRET_OBJECT (item), test->session) == FALSE); +} + +static void +test_get_collection (Test *test, gconstpointer unused) +{ + GkmSecretItem *item; + item = gkm_secret_collection_new_item (test->collection, "the-identifier"); + g_assert (gkm_secret_item_get_collection (item) == test->collection); +} + +static void +test_tracks_collection (Test *test, gconstpointer unused) +{ + GkmSecretItem *item; + item = gkm_secret_collection_new_item (test->collection, "the-identifier"); + g_object_ref (item); + + unlock_collection (test); + + /* At this point the item should be 'unlocked' */ + g_assert (gkm_secret_object_is_locked (GKM_SECRET_OBJECT (item), test->session) == FALSE); + + g_object_unref (test->collection); + test->collection = NULL; + + /* Collection went away */ + g_assert (gkm_secret_item_get_collection (item) == NULL); + g_assert (gkm_secret_object_is_locked (GKM_SECRET_OBJECT (item), test->session) == TRUE); + + g_object_unref (item); +} + +static void +test_get_set_fields (Test *test, gconstpointer unused) +{ + GHashTable *fields = gkm_secret_fields_new (); + GHashTable *check; + GkmSecretItem *item; + + item = gkm_secret_collection_new_item (test->collection, "the-identifier"); + gkm_secret_item_set_fields (item, fields); + gkm_secret_item_set_fields (item, fields); + + check = gkm_secret_item_get_fields (item); + g_assert (check == fields); + + g_hash_table_unref (fields); +} + +static void +test_collection_attr (Test *test, gconstpointer unused) +{ + gchar buffer[32]; + CK_ATTRIBUTE check = { CKA_G_COLLECTION, buffer, 32 }; + GkmSecretItem *item; + CK_RV rv; + + item = gkm_secret_collection_new_item (test->collection, "the-identifier"); + rv = gkm_object_get_attribute (GKM_OBJECT (item), test->session, &check); + g_assert (rv == CKR_OK); + g_assert (check.ulValueLen == 4); + g_assert (memcmp (buffer, "test", 4) == 0); +} + +static void +test_secret_attr (Test *test, gconstpointer unused) +{ + GkmTransaction *transaction = gkm_transaction_new (); + CK_ATTRIBUTE attr = { CKA_VALUE, "hello", 5 }; + gchar buffer[32]; + CK_ATTRIBUTE check = { CKA_VALUE, buffer, 32 }; + GkmSecretItem *item; + CK_RV rv; + + unlock_collection (test); + + item = gkm_secret_collection_new_item (test->collection, "the-identifier"); + gkm_object_set_attribute (GKM_OBJECT (item), test->session, transaction, &attr); + g_assert (gkm_transaction_get_failed (transaction) == FALSE); + gkm_transaction_complete (transaction); + g_assert (gkm_transaction_get_result (transaction) == CKR_OK); + + g_object_unref (transaction); + + rv = gkm_object_get_attribute (GKM_OBJECT (item), test->session, &check); + g_assert (rv == CKR_OK); + g_assert (check.ulValueLen == 5); + g_assert (memcmp (buffer, "hello", 5) == 0); +} + +static void +test_secret_attr_locked (Test *test, gconstpointer unused) +{ + GkmTransaction *transaction = gkm_transaction_new (); + CK_ATTRIBUTE attr = { CKA_VALUE, "hello", 5 }; + gchar buffer[32]; + CK_ATTRIBUTE check = { CKA_VALUE, buffer, 32 }; + GkmSecretItem *item; + CK_RV rv; + + item = gkm_secret_collection_new_item (test->collection, "the-identifier"); + gkm_object_set_attribute (GKM_OBJECT (item), test->session, transaction, &attr); + g_assert (gkm_transaction_get_failed (transaction) == TRUE); + gkm_transaction_complete (transaction); + g_assert (gkm_transaction_get_result (transaction) == CKR_USER_NOT_LOGGED_IN); + + g_object_unref (transaction); + + rv = gkm_object_get_attribute (GKM_OBJECT (item), test->session, &check); + g_assert (rv == CKR_USER_NOT_LOGGED_IN); +} + +static void +test_fields_attr (Test *test, gconstpointer unused) +{ + GkmTransaction *transaction = gkm_transaction_new (); + CK_ATTRIBUTE attr = { CKA_G_FIELDS, "name1\0value1\0name2\0value2", 26 }; + gchar buffer[32]; + CK_ATTRIBUTE check = { CKA_G_FIELDS, buffer, 32 }; + GkmSecretItem *item; + GHashTable *fields; + const gchar *value; + CK_RV rv; + + unlock_collection (test); + + item = gkm_secret_collection_new_item (test->collection, "the-identifier"); + gkm_object_set_attribute (GKM_OBJECT (item), test->session, transaction, &attr); + g_assert (gkm_transaction_get_failed (transaction) == FALSE); + gkm_transaction_complete (transaction); + g_assert (gkm_transaction_get_result (transaction) == CKR_OK); + + g_object_unref (transaction); + + rv = gkm_object_get_attribute (GKM_OBJECT (item), test->session, &check); + g_assert (rv == CKR_OK); + g_assert (check.ulValueLen == 26); + g_assert (memcmp (buffer, "name1\0value1\0name2\0value2", 26) == 0); + + fields = gkm_secret_item_get_fields (item); + g_assert (fields); + value = gkm_secret_fields_get (fields, "name1"); + g_assert_cmpstr (value, ==, "value1"); + value = gkm_secret_fields_get (fields, "name2"); + g_assert_cmpstr (value, ==, "value2"); +} + +static void +test_fields_attr_locked (Test *test, gconstpointer unused) +{ + GkmTransaction *transaction = gkm_transaction_new (); + CK_ATTRIBUTE attr = { CKA_G_FIELDS, "name1\0value1\0name2\0value2", 26 }; + GkmSecretItem *item; + + item = gkm_secret_collection_new_item (test->collection, "the-identifier"); + gkm_object_set_attribute (GKM_OBJECT (item), test->session, transaction, &attr); + g_assert (gkm_transaction_get_failed (transaction) == TRUE); + gkm_transaction_complete (transaction); + g_assert (gkm_transaction_get_result (transaction) == CKR_USER_NOT_LOGGED_IN); + + g_object_unref (transaction); +} + +static void +test_fields_attr_reverts (Test *test, gconstpointer unused) +{ + GkmTransaction *transaction = gkm_transaction_new (); + CK_ATTRIBUTE attr = { CKA_G_FIELDS, "new\0value\0", 10 }; + gchar buffer[32]; + CK_ATTRIBUTE check = { CKA_G_FIELDS, buffer, 32 }; + GkmSecretItem *item; + GHashTable *fields; + CK_RV rv; + + unlock_collection (test); + + item = gkm_secret_collection_new_item (test->collection, "the-identifier"); + + /* Set the old value like so */ + fields = gkm_secret_fields_new (); + gkm_secret_fields_add (fields, "old", "value"); + gkm_secret_item_set_fields (item, fields); + g_hash_table_unref (fields); + + /* Should show old value */ + rv = gkm_object_get_attribute (GKM_OBJECT (item), test->session, &check); + g_assert (rv == CKR_OK); + g_assert (check.ulValueLen == 10); + g_assert (memcmp (buffer, "old\0value\0", 10) == 0); + + /* Set the new values */ + gkm_object_set_attribute (GKM_OBJECT (item), test->session, transaction, &attr); + g_assert (gkm_transaction_get_failed (transaction) == FALSE); + + /* Should have the new value */ + rv = gkm_object_get_attribute (GKM_OBJECT (item), test->session, &check); + g_assert (rv == CKR_OK); + g_assert (check.ulValueLen == 10); + g_assert (memcmp (buffer, "new\0value\0", 10) == 0); + + /* Fail the transaction */ + gkm_transaction_fail (transaction, CKR_CANCEL); + gkm_transaction_complete (transaction); + + /* Should show the old value */ + rv = gkm_object_get_attribute (GKM_OBJECT (item), test->session, &check); + g_assert (rv == CKR_OK); + g_assert (check.ulValueLen == 10); + g_assert (memcmp (buffer, "old\0value\0", 10) == 0); + + g_object_unref (transaction); +} + +int +main (int argc, char **argv) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + + g_test_add ("/secret-store/item/new", Test, NULL, setup, test_new, teardown); + g_test_add ("/secret-store/item/create", Test, NULL, setup, test_create, teardown); + g_test_add ("/secret-store/item/create_failed", Test, NULL, setup, test_create_failed, teardown); + g_test_add ("/secret-store/item/destroy", Test, NULL, setup, test_destroy, teardown); + g_test_add ("/secret-store/item/destroy_failed", Test, NULL, setup, test_destroy_failed, teardown); + g_test_add ("/secret-store/item/collection_get", Test, NULL, setup, test_collection_get, teardown); + g_test_add ("/secret-store/item/collection_items", Test, NULL, setup, test_collection_items, teardown); + g_test_add ("/secret-store/item/collection_remove", Test, NULL, setup, test_collection_remove, teardown); + g_test_add ("/secret-store/item/is_locked", Test, NULL, setup, test_is_locked, teardown); + g_test_add ("/secret-store/item/get_collection", Test, NULL, setup, test_get_collection, teardown); + g_test_add ("/secret-store/item/tracks_collection", Test, NULL, setup, test_tracks_collection, teardown); + g_test_add ("/secret-store/item/get_set_fields", Test, NULL, setup, test_get_set_fields, teardown); + g_test_add ("/secret-store/item/collection_attr", Test, NULL, setup, test_collection_attr, teardown); + g_test_add ("/secret-store/item/secret_attr", Test, NULL, setup, test_secret_attr, teardown); + g_test_add ("/secret-store/item/secret_attr_locked", Test, NULL, setup, test_secret_attr_locked, teardown); + g_test_add ("/secret-store/item/fields_attr", Test, NULL, setup, test_fields_attr, teardown); + g_test_add ("/secret-store/item/fields_attr_locked", Test, NULL, setup, test_fields_attr_locked, teardown); + g_test_add ("/secret-store/item/fields_attr_reverts", Test, NULL, setup, test_fields_attr_reverts, teardown); + + return g_test_run (); +} diff --git a/pkcs11/secret-store/tests/test-secret-object.c b/pkcs11/secret-store/tests/test-secret-object.c new file mode 100644 index 00000000..db235cb2 --- /dev/null +++ b/pkcs11/secret-store/tests/test-secret-object.c @@ -0,0 +1,302 @@ +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ +/* + 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-secret-module.h" + +#include "secret-store/gkm-secret-object.h" + +#include "gkm/gkm-transaction.h" + +#include "pkcs11/pkcs11i.h" + +#include <glib.h> + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +typedef struct { + GkmModule *module; + GkmSession *session; + GkmSecretObject *object; +} Test; + +static void +setup (Test *test, gconstpointer unused) +{ + test->module = test_secret_module_initialize_and_enter (); + test->session = test_secret_module_open_session (TRUE); + + test->object = g_object_new (GKM_TYPE_SECRET_OBJECT, + "module", test->module, + "identifier", "my-identifier", + NULL); + + g_assert (GKM_IS_SECRET_OBJECT (test->object)); +} + +static void +teardown (Test *test, gconstpointer unused) +{ + g_object_unref (test->object); + test_secret_module_leave_and_finalize (); +} + +static void +test_is_locked (Test *test, gconstpointer unused) +{ + /* Plain GkmSecretObject is never locked */ + g_assert (!gkm_secret_object_is_locked (test->object, test->session)); +} + +static void +test_identifier_prop (Test *test, gconstpointer unused) +{ + const gchar *identifier; + identifier = gkm_secret_object_get_identifier (test->object); + g_assert_cmpstr (identifier, ==, "my-identifier"); +} + +static void +was_notified (GObject *obj, GParamSpec *pspec, gpointer user_data) +{ + gboolean* notified = user_data; + g_assert (user_data); + *notified = TRUE; +} + +static void +test_created_prop (Test *test, gconstpointer unused) +{ + glong created; + + /* Monitor for changes */ + gboolean notified = FALSE; + g_signal_connect (test->object, "notify::created", G_CALLBACK (was_notified), ¬ified); + + /* Default value */ + created = gkm_secret_object_get_created (test->object); + g_assert (created == 0); + + /* Set a new value */ + gkm_secret_object_set_created (test->object, 1247930171); + g_assert (notified); + created = gkm_secret_object_get_created (test->object); + g_assert (created == 1247930171); +} + +static void +test_modified_prop (Test *test, gconstpointer unused) +{ + glong modified; + + /* Monitor for changes */ + gboolean notified = FALSE; + g_signal_connect (test->object, "notify::modified", G_CALLBACK (was_notified), ¬ified); + + /* Default value */ + modified = gkm_secret_object_get_modified (test->object); + g_assert (modified == 0); + + /* Set a new value */ + gkm_secret_object_set_modified (test->object, 1247930171); + g_assert (notified); + modified = gkm_secret_object_get_modified (test->object); + g_assert (modified == 1247930171); +} + +static void +test_was_modified (Test *test, gconstpointer unused) +{ + GTimeVal tv; + g_get_current_time (&tv); + gkm_secret_object_was_modified (test->object); + g_assert (tv.tv_sec == gkm_secret_object_get_modified (test->object)); +} + +static void +test_label_prop (Test *test, gconstpointer unused) +{ + const gchar *label; + + /* Monitor for changes */ + gboolean notified = FALSE; + g_signal_connect (test->object, "notify::label", G_CALLBACK (was_notified), ¬ified); + + /* Default value */ + label = gkm_secret_object_get_label (test->object); + g_assert_cmpstr (label, ==, ""); + + /* Set a new value */ + gkm_secret_object_set_label (test->object, "hello"); + g_assert (notified); + label = gkm_secret_object_get_label (test->object); + g_assert_cmpstr (label, ==, "hello"); +} + +static void +test_identifier_get_attr (Test *test, gconstpointer unused) +{ + gchar buffer[32]; + CK_ATTRIBUTE attr = { CKA_ID, buffer, 32 }; + CK_RV rv; + + rv = gkm_object_get_attribute (GKM_OBJECT (test->object), test->session, &attr); + g_assert (rv == CKR_OK); + g_assert (attr.ulValueLen == 13); + g_assert (memcmp (buffer, "my-identifier", 13) == 0); +} + +static void +test_label_get_attr (Test *test, gconstpointer unused) +{ + gchar buffer[32]; + CK_ATTRIBUTE attr = { CKA_LABEL, buffer, 32 }; + CK_RV rv; + + gkm_secret_object_set_label (test->object, "hello"); + rv = gkm_object_get_attribute (GKM_OBJECT (test->object), test->session, &attr); + g_assert (rv == CKR_OK); + g_assert (attr.ulValueLen == 5); + g_assert (memcmp (buffer, "hello", 5) == 0); +} + +static void +test_label_set_attr (Test *test, gconstpointer unused) +{ + CK_ATTRIBUTE attr = { CKA_LABEL, "hello", 5 }; + GkmTransaction *transaction = gkm_transaction_new (); + + /* Monitor for changes */ + gboolean notified = FALSE; + g_signal_connect (test->object, "notify::label", G_CALLBACK (was_notified), ¬ified); + + gkm_object_set_attribute (GKM_OBJECT (test->object), test->session, transaction, &attr); + g_assert (!gkm_transaction_get_failed (transaction)); + g_assert (!notified); /* Not notified yet */ + + g_assert_cmpstr (gkm_secret_object_get_label (test->object), ==, "hello"); + + gkm_transaction_complete (transaction); + g_assert_cmpstr (gkm_secret_object_get_label (test->object), ==, "hello"); + g_assert (notified); /* Notified after transaction complete */ + + g_object_unref (transaction); +} + +static void +test_label_set_attr_fail (Test *test, gconstpointer unused) +{ + CK_ATTRIBUTE attr = { CKA_LABEL, "hello", 5 }; + GkmTransaction *transaction = gkm_transaction_new (); + gboolean notified = FALSE; + + /* Set an old value */ + gkm_secret_object_set_label (test->object, "old"); + + /* Monitor for changes */ + g_signal_connect (test->object, "notify::label", G_CALLBACK (was_notified), ¬ified); + + /* Set a new value */ + gkm_object_set_attribute (GKM_OBJECT (test->object), test->session, transaction, &attr); + g_assert (!gkm_transaction_get_failed (transaction)); + g_assert (!notified); /* Not notified yet */ + + /* Temporarily has new value */ + g_assert_cmpstr (gkm_secret_object_get_label (test->object), ==, "hello"); + + /* Fail and complete transaction */ + gkm_transaction_fail (transaction, CKR_CANCEL); + gkm_transaction_complete (transaction); + + /* Back to old value */ + g_assert_cmpstr (gkm_secret_object_get_label (test->object), ==, "old"); + g_assert (!notified); /* Should never have notified */ + + g_object_unref (transaction); +} + +static void +test_modified_get_attr (Test *test, gconstpointer unused) +{ + gchar buffer[32]; + CK_ATTRIBUTE attr = { CKA_G_MODIFIED, buffer, 32 }; + CK_RV rv; + + gkm_secret_object_set_modified (test->object, 1247930171); + rv = gkm_object_get_attribute (GKM_OBJECT (test->object), test->session, &attr); + g_assert (rv == CKR_OK); + g_assert (attr.ulValueLen == 16); + g_assert (memcmp (buffer, "2009071815161100", 16) == 0); +} + +static void +test_created_get_attr (Test *test, gconstpointer unused) +{ + gchar buffer[32]; + CK_ATTRIBUTE attr = { CKA_G_CREATED, buffer, 32 }; + CK_RV rv; + + gkm_secret_object_set_created (test->object, 1247930171); + rv = gkm_object_get_attribute (GKM_OBJECT (test->object), test->session, &attr); + g_assert (rv == CKR_OK); + g_assert (attr.ulValueLen == 16); + g_assert (memcmp (buffer, "2009071815161100", 16) == 0); +} + +static void +test_locked_get_attr (Test *test, gconstpointer unused) +{ + gchar buffer[32]; + CK_ATTRIBUTE attr = { CKA_G_LOCKED, buffer, 32 }; + CK_RV rv; + + rv = gkm_object_get_attribute (GKM_OBJECT (test->object), test->session, &attr); + g_assert (rv == CKR_OK); + g_assert (attr.ulValueLen == 1); + g_assert (memcmp (buffer, "\0", 1) == 0); +} + +int +main (int argc, char **argv) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + + g_test_add ("/secret-store/object/is_locked", Test, NULL, setup, test_is_locked, teardown); + g_test_add ("/secret-store/object/identifier_prop", Test, NULL, setup, test_identifier_prop, teardown); + g_test_add ("/secret-store/object/created_prop", Test, NULL, setup, test_created_prop, teardown); + g_test_add ("/secret-store/object/modified_prop", Test, NULL, setup, test_modified_prop, teardown); + g_test_add ("/secret-store/object/was_modified", Test, NULL, setup, test_was_modified, teardown); + g_test_add ("/secret-store/object/label_prop", Test, NULL, setup, test_label_prop, teardown); + g_test_add ("/secret-store/object/identifier_get_attr", Test, NULL, setup, test_identifier_get_attr, teardown); + g_test_add ("/secret-store/object/label_get_attr", Test, NULL, setup, test_label_get_attr, teardown); + g_test_add ("/secret-store/object/label_set_attr", Test, NULL, setup, test_label_set_attr, teardown); + g_test_add ("/secret-store/object/label_set_attr_fail", Test, NULL, setup, test_label_set_attr_fail, teardown); + g_test_add ("/secret-store/object/modified_get_attr", Test, NULL, setup, test_modified_get_attr, teardown); + g_test_add ("/secret-store/object/created_get_attr", Test, NULL, setup, test_created_get_attr, teardown); + g_test_add ("/secret-store/object/locked_get_attr", Test, NULL, setup, test_locked_get_attr, teardown); + + return g_test_run (); +} diff --git a/pkcs11/secret-store/tests/unit-test-secret-search.c b/pkcs11/secret-store/tests/test-secret-search.c index ead2aa25..303a3144 100644 --- a/pkcs11/secret-store/tests/unit-test-secret-search.c +++ b/pkcs11/secret-store/tests/test-secret-search.c @@ -1,6 +1,5 @@ /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* unit-test-secret-collection.c: Test the collection keyring - +/* Copyright (C) 2009 Stefan Walter The Gnome Keyring Library is free software; you can redistribute it and/or @@ -23,13 +22,12 @@ #include "config.h" -#include "test-suite.h" -#include "test-secret-module.h" +#include "mock-secret-module.h" -#include "gkm-secret-collection.h" -#include "gkm-secret-fields.h" -#include "gkm-secret-item.h" -#include "gkm-secret-search.h" +#include "secret-store/gkm-secret-collection.h" +#include "secret-store/gkm-secret-fields.h" +#include "secret-store/gkm-secret-item.h" +#include "secret-store/gkm-secret-search.h" #include "gkm/gkm-session.h" #include "gkm/gkm-transaction.h" @@ -42,59 +40,62 @@ #include <stdio.h> #include <string.h> -static GkmModule *module = NULL; -static GkmSession *session = NULL; -static GkmFactory *factory = NULL; -static GkmSecretCollection *collection = NULL; -static GkmSecretItem *item = NULL; +typedef struct { + GkmModule *module; + GkmSession *session; + GkmFactory *factory; + GkmSecretCollection *collection; + GkmSecretItem *item; +} Test; -TESTING_SETUP(secret_search) +static void +setup (Test *test, gconstpointer unused) { GHashTable *fields; - module = test_secret_module_initialize_and_enter (); - session = test_secret_module_open_session (TRUE); - factory = GKM_FACTORY_SECRET_SEARCH; - g_assert (factory); + test->module = test_secret_module_initialize_and_enter (); + test->session = test_secret_module_open_session (TRUE); + test->factory = GKM_FACTORY_SECRET_SEARCH; + g_assert (test->factory); - collection = g_object_new (GKM_TYPE_SECRET_COLLECTION, - "module", module, - "manager", gkm_session_get_manager (session), + test->collection = g_object_new (GKM_TYPE_SECRET_COLLECTION, + "module", test->module, + "manager", gkm_session_get_manager (test->session), "identifier", "test-collection", NULL); - /* Create an item */ - item = gkm_secret_collection_new_item (collection, "test-item"); + /* Create an test->item */ + test->item = gkm_secret_collection_new_item (test->collection, "test-item"); fields = gkm_secret_fields_new (); gkm_secret_fields_add (fields, "name1", "value1"); gkm_secret_fields_add (fields, "name2", "value2"); - gkm_secret_item_set_fields (item, fields); + gkm_secret_item_set_fields (test->item, fields); g_hash_table_unref (fields); - gkm_object_expose (GKM_OBJECT (collection), TRUE); + gkm_object_expose (GKM_OBJECT (test->collection), TRUE); } -TESTING_TEARDOWN(secret_search) +static void +teardown (Test *test, gconstpointer unused) { - g_object_unref (collection); - + g_object_unref (test->collection); test_secret_module_leave_and_finalize (); - module = NULL; - session = NULL; } -TESTING_TEST(create_search_incomplete) +static void +test_incomplete (Test *test, gconstpointer unused) { CK_ATTRIBUTE attrs[1]; GkmObject *object = NULL; GkmTransaction *transaction = gkm_transaction_new (); - object = gkm_session_create_object_for_factory (session, factory, transaction, attrs, 0); + object = gkm_session_create_object_for_factory (test->session, test->factory, transaction, attrs, 0); g_assert (gkm_transaction_complete_and_unref (transaction) == CKR_TEMPLATE_INCOMPLETE); g_assert (object == NULL); } -TESTING_TEST(create_search_bad_fields) +static void +test_bad_fields (Test *test, gconstpointer unused) { CK_ATTRIBUTE attrs[] = { { CKA_G_FIELDS, "bad-value", 9 }, @@ -103,12 +104,13 @@ TESTING_TEST(create_search_bad_fields) GkmObject *object = NULL; GkmTransaction *transaction = gkm_transaction_new (); - object = gkm_session_create_object_for_factory (session, factory, transaction, attrs, 1); + object = gkm_session_create_object_for_factory (test->session, test->factory, transaction, attrs, 1); g_assert (gkm_transaction_complete_and_unref (transaction) == CKR_ATTRIBUTE_VALUE_INVALID); g_assert (object == NULL); } -TESTING_TEST(create_search) +static void +test_new (Test *test, gconstpointer unused) { CK_ATTRIBUTE attrs[] = { { CKA_G_FIELDS, "test\0value\0two\0value2", 22 }, @@ -122,30 +124,30 @@ TESTING_TEST(create_search) gboolean vbool; gsize vsize; - object = gkm_session_create_object_for_factory (session, factory, NULL, attrs, 1); + object = gkm_session_create_object_for_factory (test->session, test->factory, NULL, attrs, 1); g_assert (object != NULL); g_assert (GKM_IS_SECRET_SEARCH (object)); - if (!gkm_object_get_attribute_ulong (object, session, CKA_CLASS, &vulong)) + if (!gkm_object_get_attribute_ulong (object, test->session, CKA_CLASS, &vulong)) g_assert_not_reached (); g_assert (vulong == CKO_G_SEARCH); - if (!gkm_object_get_attribute_boolean (object, session, CKA_MODIFIABLE, &vbool)) + if (!gkm_object_get_attribute_boolean (object, test->session, CKA_MODIFIABLE, &vbool)) g_assert_not_reached (); g_assert (vbool == CK_TRUE); - vdata = gkm_object_get_attribute_data (object, session, CKA_G_FIELDS, &vsize); + vdata = gkm_object_get_attribute_data (object, test->session, CKA_G_FIELDS, &vsize); g_assert (vdata); g_assert (vsize == attrs[0].ulValueLen); g_free (vdata); - vdata = gkm_object_get_attribute_data (object, session, CKA_G_COLLECTION, &vsize); + vdata = gkm_object_get_attribute_data (object, test->session, CKA_G_COLLECTION, &vsize); g_assert (vdata); g_assert (vsize == 0); g_free (vdata); /* No objects matched */ - vdata = gkm_object_get_attribute_data (object, session, CKA_G_MATCHED, &vsize); + vdata = gkm_object_get_attribute_data (object, test->session, CKA_G_MATCHED, &vsize); g_assert (vdata); g_assert (vsize == 0); g_free (vdata); @@ -155,14 +157,15 @@ TESTING_TEST(create_search) g_assert (fields); g_assert_cmpstr (gkm_secret_fields_get (fields, "test"), ==, "value"); - /* No collection */ + /* No test->collection */ identifier = gkm_secret_search_get_collection_id (GKM_SECRET_SEARCH (object)); g_assert (identifier == NULL); g_object_unref (object); } -TESTING_TEST(create_search_and_match) +static void +test_and_match (Test *test, gconstpointer unused) { CK_ATTRIBUTE attrs[] = { { CKA_G_FIELDS, "name1\0value1\0name2\0value2", 26 }, @@ -172,21 +175,22 @@ TESTING_TEST(create_search_and_match) gpointer vdata; gsize vsize; - object = gkm_session_create_object_for_factory (session, factory, NULL, attrs, 1); + object = gkm_session_create_object_for_factory (test->session, test->factory, NULL, attrs, 1); g_assert (object != NULL); g_assert (GKM_IS_SECRET_SEARCH (object)); /* One object matched */ - vdata = gkm_object_get_attribute_data (object, session, CKA_G_MATCHED, &vsize); + vdata = gkm_object_get_attribute_data (object, test->session, CKA_G_MATCHED, &vsize); g_assert (vdata); g_assert (vsize == sizeof (CK_OBJECT_HANDLE)); - g_assert (*((CK_OBJECT_HANDLE_PTR)vdata) == gkm_object_get_handle (GKM_OBJECT (item))); + g_assert (*((CK_OBJECT_HANDLE_PTR)vdata) == gkm_object_get_handle (GKM_OBJECT (test->item))); g_free (vdata); g_object_unref (object); } -TESTING_TEST(create_search_and_change_to_match) +static void +test_and_change_to_match (Test *test, gconstpointer unused) { CK_ATTRIBUTE attrs[] = { { CKA_G_FIELDS, "name1\0value1", 13 }, @@ -199,15 +203,15 @@ TESTING_TEST(create_search_and_change_to_match) /* Make it not match */ fields = gkm_secret_fields_new (); - gkm_secret_item_set_fields (item, fields); + gkm_secret_item_set_fields (test->item, fields); g_hash_table_unref (fields); - object = gkm_session_create_object_for_factory (session, factory, NULL, attrs, 1); + object = gkm_session_create_object_for_factory (test->session, test->factory, NULL, attrs, 1); g_assert (object != NULL); g_assert (GKM_IS_SECRET_SEARCH (object)); /* Nothing matched */ - vdata = gkm_object_get_attribute_data (object, session, CKA_G_MATCHED, &vsize); + vdata = gkm_object_get_attribute_data (object, test->session, CKA_G_MATCHED, &vsize); g_assert (vsize == 0); g_free (vdata); @@ -215,20 +219,21 @@ TESTING_TEST(create_search_and_change_to_match) fields = gkm_secret_fields_new (); gkm_secret_fields_add (fields, "name1", "value1"); gkm_secret_fields_add (fields, "name2", "value2"); - gkm_secret_item_set_fields (item, fields); + gkm_secret_item_set_fields (test->item, fields); g_hash_table_unref (fields); /* One object matched */ - vdata = gkm_object_get_attribute_data (object, session, CKA_G_MATCHED, &vsize); + vdata = gkm_object_get_attribute_data (object, test->session, CKA_G_MATCHED, &vsize); g_assert (vdata); g_assert (vsize == sizeof (CK_OBJECT_HANDLE)); - g_assert (*((CK_OBJECT_HANDLE_PTR)vdata) == gkm_object_get_handle (GKM_OBJECT (item))); + g_assert (*((CK_OBJECT_HANDLE_PTR)vdata) == gkm_object_get_handle (GKM_OBJECT (test->item))); g_free (vdata); g_object_unref (object); } -TESTING_TEST(create_search_and_change_to_not_match) +static void +test_and_change_to_not_match (Test *test, gconstpointer unused) { CK_ATTRIBUTE attrs[] = { { CKA_G_FIELDS, "name1\0value1", 13 }, @@ -239,47 +244,49 @@ TESTING_TEST(create_search_and_change_to_not_match) gpointer vdata; gsize vsize; - object = gkm_session_create_object_for_factory (session, factory, NULL, attrs, 1); + object = gkm_session_create_object_for_factory (test->session, test->factory, NULL, attrs, 1); g_assert (object != NULL); g_assert (GKM_IS_SECRET_SEARCH (object)); /* One object matched */ - vdata = gkm_object_get_attribute_data (object, session, CKA_G_MATCHED, &vsize); + vdata = gkm_object_get_attribute_data (object, test->session, CKA_G_MATCHED, &vsize); g_assert (vdata); g_assert (vsize == sizeof (CK_OBJECT_HANDLE)); - g_assert (*((CK_OBJECT_HANDLE_PTR)vdata) == gkm_object_get_handle (GKM_OBJECT (item))); + g_assert (*((CK_OBJECT_HANDLE_PTR)vdata) == gkm_object_get_handle (GKM_OBJECT (test->item))); g_free (vdata); /* Make it not match */ fields = gkm_secret_fields_new (); - gkm_secret_item_set_fields (item, fields); + gkm_secret_item_set_fields (test->item, fields); g_hash_table_unref (fields); /* Nothing matched */ - vdata = gkm_object_get_attribute_data (object, session, CKA_G_MATCHED, &vsize); + vdata = gkm_object_get_attribute_data (object, test->session, CKA_G_MATCHED, &vsize); g_assert (vsize == 0); g_free (vdata); g_object_unref (object); } -TESTING_TEST(create_search_for_bad_collection) +static void +test_for_bad_collection (Test *test, gconstpointer unused) { CK_ATTRIBUTE attrs[] = { { CKA_G_FIELDS, "name1\0value1", 13 }, - { CKA_G_COLLECTION, "bad-collection", 14 }, + { CKA_G_COLLECTION, "bad-test->collection", 14 }, }; GkmObject *object = NULL; GkmTransaction *transaction = gkm_transaction_new (); - object = gkm_session_create_object_for_factory (session, factory, transaction, attrs, 2); + object = gkm_session_create_object_for_factory (test->session, test->factory, transaction, attrs, 2); g_assert (gkm_transaction_complete_and_unref (transaction) == CKR_OK); g_object_unref (object); } -TESTING_TEST(create_search_for_collection) +static void +test_for_collection (Test *test, gconstpointer unused) { CK_ATTRIBUTE attrs[] = { { CKA_G_FIELDS, "name1\0value1", 13 }, @@ -290,28 +297,29 @@ TESTING_TEST(create_search_for_collection) gpointer vdata; gsize vsize; - object = gkm_session_create_object_for_factory (session, factory, NULL, attrs, 2); + object = gkm_session_create_object_for_factory (test->session, test->factory, NULL, attrs, 2); g_assert (object != NULL); g_assert (GKM_IS_SECRET_SEARCH (object)); - /* Should have the collection set properly */ - vdata = gkm_object_get_attribute_data (object, session, CKA_G_COLLECTION , &vsize); + /* Should have the test->collection set properly */ + vdata = gkm_object_get_attribute_data (object, test->session, CKA_G_COLLECTION , &vsize); g_assert (vdata); g_assert (vsize == 15); g_assert (memcmp (vdata, "test-collection", 15) == 0); g_free (vdata); /* One object matched */ - vdata = gkm_object_get_attribute_data (object, session, CKA_G_MATCHED, &vsize); + vdata = gkm_object_get_attribute_data (object, test->session, CKA_G_MATCHED, &vsize); g_assert (vdata); g_assert (vsize == sizeof (CK_OBJECT_HANDLE)); - g_assert (*((CK_OBJECT_HANDLE_PTR)vdata) == gkm_object_get_handle (GKM_OBJECT (item))); + g_assert (*((CK_OBJECT_HANDLE_PTR)vdata) == gkm_object_get_handle (GKM_OBJECT (test->item))); g_free (vdata); g_object_unref (object); } -TESTING_TEST(create_search_for_collection_no_match) +static void +test_for_collection_no_match (Test *test, gconstpointer unused) { CK_ATTRIBUTE attrs[] = { { CKA_G_FIELDS, "test\0value", 11 }, @@ -326,8 +334,8 @@ TESTING_TEST(create_search_for_collection_no_match) gsize vsize; ocoll = g_object_new (GKM_TYPE_SECRET_COLLECTION, - "module", module, - "manager", gkm_session_get_manager (session), + "module", test->module, + "manager", gkm_session_get_manager (test->session), "identifier", "other-collection", NULL); oitem = gkm_secret_collection_new_item (ocoll, "other-item"); @@ -339,15 +347,34 @@ TESTING_TEST(create_search_for_collection_no_match) gkm_secret_item_set_fields (oitem, fields); g_hash_table_unref (fields); - object = gkm_session_create_object_for_factory (session, factory, NULL, attrs, 2); + object = gkm_session_create_object_for_factory (test->session, test->factory, NULL, attrs, 2); g_assert (object != NULL); g_assert (GKM_IS_SECRET_SEARCH (object)); /* No objects matched */ - vdata = gkm_object_get_attribute_data (object, session, CKA_G_MATCHED, &vsize); + vdata = gkm_object_get_attribute_data (object, test->session, CKA_G_MATCHED, &vsize); g_assert (vsize == 0); g_free (vdata); g_object_unref (object); g_object_unref (ocoll); } + +int +main (int argc, char **argv) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + + g_test_add ("/secret-store/search/new", Test, NULL, setup, test_new, teardown); + g_test_add ("/secret-store/search/incomplete", Test, NULL, setup, test_incomplete, teardown); + g_test_add ("/secret-store/search/bad_fields", Test, NULL, setup, test_bad_fields, teardown); + g_test_add ("/secret-store/search/and_match", Test, NULL, setup, test_and_match, teardown); + g_test_add ("/secret-store/search/and_change_to_match", Test, NULL, setup, test_and_change_to_match, teardown); + g_test_add ("/secret-store/search/and_change_to_not_match", Test, NULL, setup, test_and_change_to_not_match, teardown); + g_test_add ("/secret-store/search/for_bad_collection", Test, NULL, setup, test_for_bad_collection, teardown); + g_test_add ("/secret-store/search/for_collection", Test, NULL, setup, test_for_collection, teardown); + g_test_add ("/secret-store/search/for_collection_no_match", Test, NULL, setup, test_for_collection_no_match, teardown); + + return g_test_run (); +} diff --git a/pkcs11/secret-store/tests/test-secret-textual.c b/pkcs11/secret-store/tests/test-secret-textual.c new file mode 100644 index 00000000..4c8a5aed --- /dev/null +++ b/pkcs11/secret-store/tests/test-secret-textual.c @@ -0,0 +1,205 @@ +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ +/* + 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-secret-module.h" + +#include "secret-store/gkm-secret-collection.h" +#include "secret-store/gkm-secret-data.h" +#include "secret-store/gkm-secret-fields.h" +#include "secret-store/gkm-secret-item.h" +#include "secret-store/gkm-secret-textual.h" + +#include "gkm/gkm-secret.h" + +#include "pkcs11/pkcs11i.h" + +#include <glib.h> + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +typedef struct { + GkmModule *module; + GkmSession *session; + GkmSecretCollection *collection; + GkmSecretData *sdata; +} Test; + +static void +setup (Test *test, gconstpointer unused) +{ + test->module = test_secret_module_initialize_and_enter (); + test->session = test_secret_module_open_session (TRUE); + + test->collection = g_object_new (GKM_TYPE_SECRET_COLLECTION, + "module", test->module, + "identifier", "test", + "label", "brigadooooooooooooon", + NULL); + + test->sdata = g_object_new (GKM_TYPE_SECRET_DATA, NULL); + + g_assert (GKM_IS_SECRET_COLLECTION (test->collection)); + +} + +static void +teardown (Test *test, gconstpointer unused) +{ + if (test->collection) + g_object_unref (test->collection); + if (test->sdata) + g_object_unref (test->sdata); + test_secret_module_leave_and_finalize (); +} + +static void +test_read (Test *test, gconstpointer unused) +{ + GkmDataResult res; + gchar *data; + gsize n_data; + + if (!g_file_get_contents (SRCDIR "/files/plain.keyring", &data, &n_data, NULL)) + g_assert_not_reached (); + res = gkm_secret_textual_read (test->collection, test->sdata, data, n_data); + g_free (data); + + test_secret_collection_validate (test->collection, test->sdata); + + g_assert (res == GKM_DATA_SUCCESS); +} + +static void +test_read_wrong_format (Test *test, gconstpointer unused) +{ + GkmDataResult res; + gchar *data; + gsize n_data; + + if (!g_file_get_contents (SRCDIR "/files/encrypted.keyring", &data, &n_data, NULL)) + g_assert_not_reached (); + + res = gkm_secret_textual_read (test->collection, test->sdata, data, n_data); + g_free (data); + + g_assert (res == GKM_DATA_UNRECOGNIZED); +} + +static void +test_read_bad_number (Test *test, gconstpointer unused) +{ + GkmSecretItem *item; + GkmDataResult res; + const gchar *value; + gchar *data; + gsize n_data; + + if (!g_file_get_contents (SRCDIR "/files/plain-bad-number.keyring", &data, &n_data, NULL)) + g_assert_not_reached (); + res = gkm_secret_textual_read (test->collection, test->sdata, data, n_data); + g_free (data); + + g_assert (res == GKM_DATA_SUCCESS); + + item = gkm_secret_collection_get_item (test->collection, "1"); + g_assert (GKM_IS_SECRET_ITEM (item)); + value = gkm_secret_fields_get (gkm_secret_item_get_fields (item), "bad-number"); + g_assert (value == NULL); + value = gkm_secret_fields_get (gkm_secret_item_get_fields (item), "missing-number"); + g_assert (value == NULL); +} + +static void +test_write (Test *test, gconstpointer unused) +{ + GkmDataResult res; + gpointer data; + gsize n_data; + + test_secret_collection_populate (test->collection, test->sdata); + + res = gkm_secret_textual_write (test->collection, test->sdata, &data, &n_data); + g_assert (res == GKM_DATA_SUCCESS); + g_assert (data); + g_assert (n_data); + + /* Try parsing it again */ + res = gkm_secret_textual_read (test->collection, test->sdata, data, n_data); + g_assert (res == GKM_DATA_SUCCESS); +} + +static void +test_remove_unavailable (Test *test, gconstpointer unused) +{ + GkmDataResult res; + GList *items; + gchar *data; + gsize n_data; + + if (!g_file_get_contents (SRCDIR "/files/plain.keyring", &data, &n_data, NULL)) + g_assert_not_reached (); + res = gkm_secret_textual_read (test->collection, test->sdata, data, n_data); + g_assert (res == GKM_DATA_SUCCESS); + + /* Two items from the file */ + items = gkm_secret_collection_get_items (test->collection); + g_assert_cmpint (g_list_length (items), ==, 2); + g_list_free (items); + + /* Fill in some more data */ + test_secret_collection_populate (test->collection, test->sdata); + + /* Should have added three more */ + items = gkm_secret_collection_get_items (test->collection); + g_assert_cmpint (g_list_length (items), ==, 5); + g_list_free (items); + + /* Re-read the keyring */ + res = gkm_secret_textual_read (test->collection, test->sdata, data, n_data); + g_assert (res == GKM_DATA_SUCCESS); + + /* And we're back to two */ + items = gkm_secret_collection_get_items (test->collection); + g_assert_cmpint (g_list_length (items), ==, 2); + g_list_free (items); + + g_free (data); +} + +int +main (int argc, char **argv) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + + g_test_add ("/secret-store/search/read", Test, NULL, setup, test_read, teardown); + g_test_add ("/secret-store/search/read_wrong_format", Test, NULL, setup, test_read_wrong_format, teardown); + g_test_add ("/secret-store/search/read_bad_number", Test, NULL, setup, test_read_bad_number, teardown); + g_test_add ("/secret-store/search/write", Test, NULL, setup, test_write, teardown); + g_test_add ("/secret-store/search/remove_unavailable", Test, NULL, setup, test_remove_unavailable, teardown); + + return g_test_run (); +} diff --git a/pkcs11/secret-store/tests/unit-test-secret-binary.c b/pkcs11/secret-store/tests/unit-test-secret-binary.c deleted file mode 100644 index 6efc9493..00000000 --- a/pkcs11/secret-store/tests/unit-test-secret-binary.c +++ /dev/null @@ -1,200 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* unit-test-secret-binary.c: Test binary keyring read and write - - 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 "test-suite.h" -#include "test-secret-module.h" - -#include "gkm-secret-binary.h" -#include "gkm-secret-collection.h" -#include "gkm-secret-data.h" -#include "gkm-secret-fields.h" -#include "gkm-secret-item.h" - -#include "gkm/gkm-secret.h" - -#include "pkcs11/pkcs11i.h" - -#include <glib.h> - -#include <stdlib.h> -#include <stdio.h> -#include <string.h> - -static GkmModule *module = NULL; -static GkmSession *session = NULL; -static GkmSecretCollection *collection = NULL; -static GkmSecretData *sdata = NULL; - -TESTING_SETUP(binary) -{ - GkmSecret *master; - - module = test_secret_module_initialize_and_enter (); - session = test_secret_module_open_session (TRUE); - - collection = g_object_new (GKM_TYPE_SECRET_COLLECTION, - "module", module, - "identifier", "test", - "label", "brigadooooooooooooon", - NULL); - - sdata = g_object_new (GKM_TYPE_SECRET_DATA, NULL); - master = gkm_secret_new_from_password ("my-keyring-password"); - gkm_secret_data_set_master (sdata, master); - g_object_unref (master); - - g_assert (GKM_IS_SECRET_COLLECTION (collection)); - -} - -TESTING_TEARDOWN(binary) -{ - if (collection) - g_object_unref (collection); - collection = NULL; - - if (sdata) - g_object_unref (sdata); - sdata = NULL; - - test_secret_module_leave_and_finalize (); - module = NULL; - session = NULL; -} - -TESTING_TEST(binary_read) -{ - GkmDataResult res; - guchar *data; - gsize n_data; - - data = testing_data_read ("encrypted.keyring", &n_data); - res = gkm_secret_binary_read (collection, sdata, data, n_data); - g_free (data); - - test_secret_collection_validate (collection, sdata); - - g_assert (res == GKM_DATA_SUCCESS); -} - -TESTING_TEST(binary_read_wrong_format) -{ - GkmDataResult res; - guchar *data; - gsize n_data; - - data = testing_data_read ("plain.keyring", &n_data); - res = gkm_secret_binary_read (collection, sdata, data, n_data); - g_free (data); - - g_assert (res == GKM_DATA_UNRECOGNIZED); -} - -TESTING_TEST(binary_read_wrong_master) -{ - GkmDataResult res; - GkmSecret *master; - guchar *data; - gsize n_data; - - master = gkm_secret_new_from_password ("wrong"); - gkm_secret_data_set_master (sdata, master); - g_object_unref (master); - - data = testing_data_read ("encrypted.keyring", &n_data); - res = gkm_secret_binary_read (collection, sdata, data, n_data); - g_free (data); - - g_assert (res == GKM_DATA_LOCKED); -} - -TESTING_TEST(binary_read_sdata_but_no_master) -{ - GkmDataResult res; - guchar *data; - gsize n_data; - - gkm_secret_data_set_master (sdata, NULL); - - data = testing_data_read ("encrypted.keyring", &n_data); - res = gkm_secret_binary_read (collection, sdata, data, n_data); - g_free (data); - - g_assert (res == GKM_DATA_LOCKED); -} - -TESTING_TEST(binary_write) -{ - GkmDataResult res; - guchar *data; - gsize n_data; - - test_secret_collection_populate (collection, sdata); - - res = gkm_secret_binary_write (collection, sdata, &data, &n_data); - g_assert (res == GKM_DATA_SUCCESS); - g_assert (data); - g_assert (n_data); - - /* Try parsing it again */ - res = gkm_secret_binary_read (collection, sdata, data, n_data); - g_assert (res == GKM_DATA_SUCCESS); -} - -TESTING_TEST(binary_remove_unavailable) -{ - GkmDataResult res; - GList *items; - guchar *data; - gsize n_data; - - data = testing_data_read ("encrypted.keyring", &n_data); - res = gkm_secret_binary_read (collection, sdata, data, n_data); - g_assert (res == GKM_DATA_SUCCESS); - - /* Two items from the file */ - items = gkm_secret_collection_get_items (collection); - g_assert_cmpint (g_list_length (items), ==, 2); - g_list_free (items); - - /* Fill in some more data */ - test_secret_collection_populate (collection, sdata); - - /* Should have added three more */ - items = gkm_secret_collection_get_items (collection); - g_assert_cmpint (g_list_length (items), ==, 5); - g_list_free (items); - - /* Re-read the keyring */ - res = gkm_secret_binary_read (collection, sdata, data, n_data); - g_assert (res == GKM_DATA_SUCCESS); - - /* And we're back to two */ - items = gkm_secret_collection_get_items (collection); - g_assert_cmpint (g_list_length (items), ==, 2); - g_list_free (items); - - g_free (data); -} diff --git a/pkcs11/secret-store/tests/unit-test-secret-collection.c b/pkcs11/secret-store/tests/unit-test-secret-collection.c deleted file mode 100644 index e2d2f131..00000000 --- a/pkcs11/secret-store/tests/unit-test-secret-collection.c +++ /dev/null @@ -1,563 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* unit-test-secret-collection.c: Test the collection keyring - - 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 "test-suite.h" -#include "test-secret-module.h" - -#include "gkm-secret-data.h" -#include "gkm-secret-collection.h" -#include "gkm-secret-item.h" - -#include "gkm/gkm-credential.h" -#include "gkm/gkm-session.h" -#include "gkm/gkm-transaction.h" - -#include "pkcs11/pkcs11i.h" - -#include <glib.h> - -#include <stdlib.h> -#include <stdio.h> -#include <string.h> - -static GkmModule *module = NULL; -static GkmSession *session = NULL; -static CK_OBJECT_HANDLE credential = 0; -static CK_OBJECT_HANDLE credential2 = 0; -static GkmSecretCollection *collection = NULL; - -TESTING_SETUP(secret_collection) -{ - CK_OBJECT_CLASS klass = CKO_G_CREDENTIAL; - GkmObject *cred; - - CK_ATTRIBUTE attrs[] = { - { CKA_CLASS, &klass, sizeof (klass) }, - { CKA_VALUE, NULL, 0 } - }; - - module = test_secret_module_initialize_and_enter (); - session = test_secret_module_open_session (TRUE); - - collection = g_object_new (GKM_TYPE_SECRET_COLLECTION, - "module", module, - "identifier", "test", - NULL); - g_assert (GKM_IS_SECRET_COLLECTION (collection)); - - /* Make two credentials */ - cred = gkm_session_create_object_for_factory (session, GKM_FACTORY_CREDENTIAL, NULL, - attrs, G_N_ELEMENTS (attrs)); - g_assert (cred != NULL); - credential = gkm_object_get_handle (GKM_OBJECT (cred)); - g_object_unref (cred); - - cred = gkm_session_create_object_for_factory (session, GKM_FACTORY_CREDENTIAL, NULL, - attrs, G_N_ELEMENTS (attrs)); - g_assert (cred != NULL); - credential2 = gkm_object_get_handle (GKM_OBJECT (cred)); - g_object_unref (cred); -} - -TESTING_TEARDOWN(secret_collection) -{ - if (collection) - g_object_unref (collection); - collection = NULL; - - test_secret_module_leave_and_finalize (); - module = NULL; - session = NULL; - credential = 0; -} - -TESTING_TEST(secret_collection_is_locked) -{ - gboolean locked; - - /* By default is locked */ - locked = gkm_secret_object_is_locked (GKM_SECRET_OBJECT (collection), session); - g_assert (locked == TRUE); -} - -TESTING_TEST(secret_collection_unlocked_data) -{ - GkmCredential *cred; - GkmSecretData *sdata; - CK_RV rv; - - /* Create credential, which unlocks collection */ - rv = gkm_credential_create (module, gkm_session_get_manager (session), GKM_OBJECT (collection), NULL, 0, &cred); - g_assert (rv == CKR_OK); - gkm_session_add_session_object (session, NULL, GKM_OBJECT (cred)); - g_object_unref (cred); - - /* Collection should now be unlocked */ - sdata = gkm_secret_collection_unlocked_use (collection, session); - g_assert (GKM_IS_SECRET_DATA (sdata)); - g_assert (!gkm_secret_object_is_locked (GKM_SECRET_OBJECT (collection), session)); - g_object_unref (sdata); -} - -TESTING_TEST(secret_collection_get_filename) -{ - GkmSecretCollection *other; - const gchar *filename; - - other = g_object_new (GKM_TYPE_SECRET_COLLECTION, - "module", module, - "identifier", "test", - "filename", "/tmp/filename.keyring", - NULL); - - filename = gkm_secret_collection_get_filename (other); - g_assert_cmpstr (filename, ==, "/tmp/filename.keyring"); - - g_object_unref (other); -} - -TESTING_TEST(secret_collection_set_filename) -{ - const gchar *filename; - - gkm_secret_collection_set_filename (collection, "/tmp/filename.keyring"); - - filename = gkm_secret_collection_get_filename (collection); - g_assert_cmpstr (filename, ==, "/tmp/filename.keyring"); -} - -TESTING_TEST(secret_collection_has_item) -{ - GkmSecretItem *item; - - item = gkm_secret_collection_new_item (collection, "testo"); - g_assert (gkm_secret_collection_has_item (collection, item)); -} - -TESTING_TEST(secret_collection_load_unlock_plain) -{ - GkmCredential *cred; - GkmSecretData *sdata; - GkmDataResult res; - gchar *filename; - CK_RV rv; - - filename = testing_data_filename ("plain.keyring"); - gkm_secret_collection_set_filename (collection, filename); - g_free (filename); - - /* Load the data in the file */ - res = gkm_secret_collection_load (collection); - g_assert (res == GKM_DATA_SUCCESS); - - /* Unlock the keyring, which should load again */ - rv = gkm_credential_create (module, gkm_session_get_manager (session), GKM_OBJECT (collection), NULL, 0, &cred); - g_assert (rv == CKR_OK); - gkm_session_add_session_object (session, NULL, GKM_OBJECT (cred)); - g_object_unref (cred); - - sdata = gkm_secret_collection_unlocked_use (collection, session); - g_assert (sdata != NULL && GKM_IS_SECRET_DATA (sdata)); - test_secret_collection_validate (collection, sdata); - g_object_unref (sdata); -} - -TESTING_TEST(secret_collection_load_unlock_encrypted) -{ - GkmCredential *cred; - GkmSecretData *sdata; - GkmDataResult res; - gchar *filename; - CK_RV rv; - - filename = testing_data_filename ("encrypted.keyring"); - gkm_secret_collection_set_filename (collection, filename); - g_free (filename); - - /* Load the data in the file */ - res = gkm_secret_collection_load (collection); - g_assert (res == GKM_DATA_SUCCESS); - - /* Unlock the keyring, which should load again */ - rv = gkm_credential_create (module, gkm_session_get_manager (session), GKM_OBJECT (collection), - (guchar*)"my-keyring-password", 19, &cred); - g_assert (rv == CKR_OK); - gkm_session_add_session_object (session, NULL, GKM_OBJECT (cred)); - g_object_unref (cred); - - sdata = gkm_secret_collection_unlocked_use (collection, session); - g_assert (sdata != NULL && GKM_IS_SECRET_DATA (sdata)); - test_secret_collection_validate (collection, sdata); - g_object_unref (sdata); -} - -TESTING_TEST(secret_collection_load_unlock_bad_password) -{ - GkmCredential *cred; - GkmDataResult res; - gchar *filename; - CK_RV rv; - - filename = testing_data_filename ("encrypted.keyring"); - gkm_secret_collection_set_filename (collection, filename); - g_free (filename); - - /* Load the data in the file */ - res = gkm_secret_collection_load (collection); - g_assert (res == GKM_DATA_SUCCESS); - - /* Unlock the keyring, which should load again */ - rv = gkm_credential_create (module, gkm_session_get_manager (session), GKM_OBJECT (collection), - (guchar*)"wrong", 5, &cred); - g_assert (rv == CKR_PIN_INCORRECT); -} - -TESTING_TEST(secret_collection_unlock_without_load) -{ - GkmCredential *cred; - GkmSecretData *sdata; - gchar *filename; - CK_RV rv; - - filename = testing_data_filename ("encrypted.keyring"); - gkm_secret_collection_set_filename (collection, filename); - g_free (filename); - - /* Unlock the keyring, which should load it */ - rv = gkm_credential_create (module, gkm_session_get_manager (session), GKM_OBJECT (collection), - (guchar*)"my-keyring-password", 19, &cred); - g_assert (rv == CKR_OK); - gkm_session_add_session_object (session, NULL, GKM_OBJECT (cred)); - g_object_unref (cred); - - sdata = gkm_secret_collection_unlocked_use (collection, session); - g_assert (sdata != NULL && GKM_IS_SECRET_DATA (sdata)); - test_secret_collection_validate (collection, sdata); - g_object_unref (sdata); -} - -TESTING_TEST(secret_collection_twice_unlock) -{ - GkmCredential *cred; - GkmSecretData *sdata; - gchar *filename; - CK_RV rv; - - filename = testing_data_filename ("encrypted.keyring"); - gkm_secret_collection_set_filename (collection, filename); - g_free (filename); - - /* Unlock the keyring, which should load */ - rv = gkm_credential_create (module, gkm_session_get_manager (session), GKM_OBJECT (collection), - (guchar*)"my-keyring-password", 19, &cred); - g_assert (rv == CKR_OK); - gkm_session_add_session_object (session, NULL, GKM_OBJECT (cred)); - g_object_unref (cred); - - /* Unlock the keyring again, which should not reload */ - rv = gkm_credential_create (module, gkm_session_get_manager (session), GKM_OBJECT (collection), - (guchar*)"my-keyring-password", 19, &cred); - g_assert (rv == CKR_OK); - gkm_session_add_session_object (session, NULL, GKM_OBJECT (cred)); - g_object_unref (cred); - - sdata = gkm_secret_collection_unlocked_use (collection, session); - g_assert (sdata != NULL && GKM_IS_SECRET_DATA (sdata)); - test_secret_collection_validate (collection, sdata); - g_object_unref (sdata); -} - -TESTING_TEST(secret_collection_twice_unlock_bad_password) -{ - GkmCredential *cred; - GkmSecretData *sdata; - gchar *filename; - CK_RV rv; - - filename = testing_data_filename ("encrypted.keyring"); - gkm_secret_collection_set_filename (collection, filename); - g_free (filename); - - /* Unlock the keyring, which should load */ - rv = gkm_credential_create (module, gkm_session_get_manager (session), GKM_OBJECT (collection), - (guchar*)"my-keyring-password", 19, &cred); - g_assert (rv == CKR_OK); - gkm_session_add_session_object (session, NULL, GKM_OBJECT (cred)); - g_object_unref (cred); - - /* Unlock the keyring again, wrong password */ - rv = gkm_credential_create (module, gkm_session_get_manager (session), GKM_OBJECT (collection), - (guchar*)"wrong", 5, &cred); - g_assert (rv == CKR_PIN_INCORRECT); - - sdata = gkm_secret_collection_unlocked_use (collection, session); - g_assert (sdata != NULL && GKM_IS_SECRET_DATA (sdata)); - test_secret_collection_validate (collection, sdata); - g_object_unref (sdata); -} - -TESTING_TEST(secret_collection_memory_unlock) -{ - GkmCredential *cred; - GkmDataResult res; - CK_RV rv; - - /* Load the data in the file */ - res = gkm_secret_collection_load (collection); - g_assert (res == GKM_DATA_SUCCESS); - - /* Unlock the keyring, which should load again */ - rv = gkm_credential_create (module, gkm_session_get_manager (session), GKM_OBJECT (collection), - NULL, 0, &cred); - g_assert (rv == CKR_OK); - gkm_session_add_session_object (session, NULL, GKM_OBJECT (cred)); - g_object_unref (cred); -} - -TESTING_TEST(secret_collection_memory_unlock_bad_password) -{ - GkmCredential *cred; - GkmDataResult res; - CK_RV rv; - - /* Load the data in the file */ - res = gkm_secret_collection_load (collection); - g_assert (res == GKM_DATA_SUCCESS); - - /* Unlock the keyring, which should load again */ - rv = gkm_credential_create (module, gkm_session_get_manager (session), GKM_OBJECT (collection), - (guchar*)"wrong", 5, &cred); - g_assert (rv == CKR_PIN_INCORRECT); -} - -TESTING_TEST(secret_collection_factory) -{ - CK_OBJECT_CLASS klass = CKO_G_COLLECTION; - GkmObject *object; - - CK_ATTRIBUTE attrs[] = { - { CKA_CLASS, &klass, sizeof (klass) }, - { CKA_LABEL, "blah", 4 }, - { CKA_G_CREDENTIAL, &credential, sizeof (credential) }, - }; - - object = gkm_session_create_object_for_factory (session, GKM_FACTORY_SECRET_COLLECTION, NULL, - attrs, G_N_ELEMENTS (attrs)); - g_assert (object != NULL); - g_assert (GKM_IS_SECRET_COLLECTION (object)); - - g_assert_cmpstr (gkm_secret_object_get_label (GKM_SECRET_OBJECT (object)), ==, "blah"); - g_object_unref (object); -} - -TESTING_TEST(secret_collection_factory_unnamed) -{ - CK_OBJECT_CLASS klass = CKO_G_COLLECTION; - const gchar *identifier; - GkmObject *object; - - CK_ATTRIBUTE attrs[] = { - { CKA_CLASS, &klass, sizeof (klass) }, - { CKA_G_CREDENTIAL, &credential, sizeof (credential) }, - }; - - object = gkm_session_create_object_for_factory (session, GKM_FACTORY_SECRET_COLLECTION, NULL, - attrs, G_N_ELEMENTS (attrs)); - g_assert (object != NULL); - g_assert (GKM_IS_SECRET_COLLECTION (object)); - - identifier = gkm_secret_object_get_identifier (GKM_SECRET_OBJECT (object)); - g_assert_cmpstr (identifier, !=, ""); - g_object_unref (object); -} - -TESTING_TEST(secret_collection_factory_token) -{ - CK_OBJECT_CLASS klass = CKO_G_COLLECTION; - const gchar *identifier; - GkmObject *object; - CK_BBOOL token = CK_TRUE; - - CK_ATTRIBUTE attrs[] = { - { CKA_CLASS, &klass, sizeof (klass) }, - { CKA_TOKEN, &token, sizeof (token) }, - { CKA_LABEL, "blah", 4 }, - { CKA_G_CREDENTIAL, &credential, sizeof (credential) }, - }; - - object = gkm_session_create_object_for_factory (session, GKM_FACTORY_SECRET_COLLECTION, NULL, - attrs, G_N_ELEMENTS (attrs)); - g_assert (object != NULL); - g_assert (GKM_IS_SECRET_COLLECTION (object)); - - identifier = gkm_secret_object_get_identifier (GKM_SECRET_OBJECT (object)); - g_assert (strstr (identifier, "blah")); - g_object_unref (object); -} - -TESTING_TEST(secret_collection_factory_duplicate) -{ - CK_OBJECT_CLASS klass = CKO_G_COLLECTION; - const gchar *identifier1, *identifier2; - GkmObject *object; - - CK_ATTRIBUTE attrs[] = { - { CKA_G_CREDENTIAL, &credential, sizeof (credential) }, - { CKA_CLASS, &klass, sizeof (klass) }, - { CKA_LABEL, "blah", 4 }, - }; - - object = gkm_session_create_object_for_factory (session, GKM_FACTORY_SECRET_COLLECTION, NULL, - attrs, G_N_ELEMENTS (attrs)); - g_assert (object != NULL); - g_assert (GKM_IS_SECRET_COLLECTION (object)); - - identifier1 = gkm_secret_object_get_identifier (GKM_SECRET_OBJECT (object)); - g_assert (strstr (identifier1, "blah")); - g_object_unref (object); - - /* Use second credential for second object */ - attrs[0].pValue = &credential2; - object = gkm_session_create_object_for_factory (session, GKM_FACTORY_SECRET_COLLECTION, NULL, - attrs, G_N_ELEMENTS (attrs)); - g_assert (object != NULL); - g_assert (GKM_IS_SECRET_COLLECTION (object)); - - identifier2 = gkm_secret_object_get_identifier (GKM_SECRET_OBJECT (object)); - g_assert (strstr (identifier2, "blah")); - g_object_unref (object); - - g_assert_cmpstr (identifier1, !=, identifier2); -} - -TESTING_TEST(secret_collection_factory_item) -{ - CK_OBJECT_CLASS c_klass = CKO_G_COLLECTION; - CK_OBJECT_CLASS i_klass = CKO_SECRET_KEY; - const gchar *identifier; - GkmObject *object; - CK_BBOOL token = CK_TRUE; - - CK_ATTRIBUTE c_attrs[] = { - { CKA_CLASS, &c_klass, sizeof (c_klass) }, - { CKA_TOKEN, &token, sizeof (token) }, - { CKA_LABEL, "three", 5 }, - { CKA_G_CREDENTIAL, &credential, sizeof (credential) }, - }; - - CK_ATTRIBUTE i_attrs[] = { - { CKA_G_COLLECTION, NULL, 0 }, /* Filled below */ - { CKA_CLASS, &i_klass, sizeof (i_klass) }, - { CKA_TOKEN, &token, sizeof (token) }, - { CKA_LABEL, "Item", 4 }, - }; - - object = gkm_session_create_object_for_factory (session, GKM_FACTORY_SECRET_COLLECTION, NULL, - c_attrs, G_N_ELEMENTS (c_attrs)); - g_assert (object != NULL); - g_assert (GKM_IS_SECRET_COLLECTION (object)); - identifier = gkm_secret_object_get_identifier (GKM_SECRET_OBJECT (object)); - g_object_unref (object); - - i_attrs[0].pValue = (gpointer)identifier; - i_attrs[0].ulValueLen = strlen (identifier); - object = gkm_session_create_object_for_factory (session, GKM_FACTORY_SECRET_ITEM, NULL, - i_attrs, G_N_ELEMENTS (i_attrs)); - g_assert (object != NULL); - g_assert (GKM_IS_SECRET_ITEM (object)); - g_object_unref (object); -} - -TESTING_TEST(secret_collection_token_remove) -{ - CK_OBJECT_CLASS klass = CKO_G_COLLECTION; - GkmTransaction *transaction; - GkmObject *object; - CK_BBOOL token = CK_TRUE; - - CK_ATTRIBUTE attrs[] = { - { CKA_CLASS, &klass, sizeof (klass) }, - { CKA_TOKEN, &token, sizeof (token) }, - { CKA_LABEL, "blah", 4 }, - { CKA_G_CREDENTIAL, &credential, sizeof (credential) }, - }; - - object = gkm_session_create_object_for_factory (session, GKM_FACTORY_SECRET_COLLECTION, NULL, - attrs, G_N_ELEMENTS (attrs)); - g_assert (object != NULL); - g_assert (GKM_IS_SECRET_COLLECTION (object)); - - transaction = gkm_transaction_new (); - gkm_module_remove_token_object (module, transaction, object); - g_assert (!gkm_transaction_get_failed (transaction)); - gkm_transaction_complete (transaction); - g_object_unref (transaction); - g_object_unref (object); -} - -TESTING_TEST(secret_collection_token_item_remove) -{ - CK_OBJECT_CLASS c_klass = CKO_G_COLLECTION; - CK_OBJECT_CLASS i_klass = CKO_SECRET_KEY; - GkmTransaction *transaction; - const gchar *identifier; - GkmObject *object; - CK_BBOOL token = CK_TRUE; - - CK_ATTRIBUTE c_attrs[] = { - { CKA_CLASS, &c_klass, sizeof (c_klass) }, - { CKA_TOKEN, &token, sizeof (token) }, - { CKA_LABEL, "three", 5 }, - { CKA_G_CREDENTIAL, &credential, sizeof (credential) }, - }; - - CK_ATTRIBUTE i_attrs[] = { - { CKA_G_COLLECTION, NULL, 0 }, /* Filled below */ - { CKA_CLASS, &i_klass, sizeof (i_klass) }, - { CKA_TOKEN, &token, sizeof (token) }, - { CKA_LABEL, "Item", 4 }, - }; - - object = gkm_session_create_object_for_factory (session, GKM_FACTORY_SECRET_COLLECTION, NULL, - c_attrs, G_N_ELEMENTS (c_attrs)); - g_assert (object != NULL); - g_assert (GKM_IS_SECRET_COLLECTION (object)); - identifier = gkm_secret_object_get_identifier (GKM_SECRET_OBJECT (object)); - g_object_unref (object); - - i_attrs[0].pValue = (gpointer)identifier; - i_attrs[0].ulValueLen = strlen (identifier); - object = gkm_session_create_object_for_factory (session, GKM_FACTORY_SECRET_ITEM, NULL, - i_attrs, G_N_ELEMENTS (i_attrs)); - g_assert (object != NULL); - g_assert (GKM_IS_SECRET_ITEM (object)); - - transaction = gkm_transaction_new (); - gkm_module_remove_token_object (module, transaction, object); - g_assert (!gkm_transaction_get_failed (transaction)); - gkm_transaction_complete (transaction); - g_object_unref (transaction); - g_object_unref (object); -} diff --git a/pkcs11/secret-store/tests/unit-test-secret-item.c b/pkcs11/secret-store/tests/unit-test-secret-item.c deleted file mode 100644 index d82396ec..00000000 --- a/pkcs11/secret-store/tests/unit-test-secret-item.c +++ /dev/null @@ -1,438 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* unit-test-secret-item.c: Test secret item - - 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 "test-suite.h" -#include "test-secret-module.h" - -#include "gkm-secret-collection.h" -#include "gkm-secret-fields.h" -#include "gkm-secret-item.h" - -#include "gkm/gkm-credential.h" -#include "gkm/gkm-session.h" -#include "gkm/gkm-transaction.h" - -#include "pkcs11/pkcs11i.h" - -#include <glib.h> - -#include <stdlib.h> -#include <stdio.h> -#include <string.h> - -static GkmModule *module = NULL; -static GkmSession *session = NULL; -static GkmSecretCollection *collection = NULL; - -TESTING_SETUP(secret_item) -{ - module = test_secret_module_initialize_and_enter (); - session = test_secret_module_open_session (TRUE); - - collection = g_object_new (GKM_TYPE_SECRET_COLLECTION, - "module", module, - "identifier", "test", - NULL); -} - -TESTING_TEARDOWN(secret_item) -{ - if (collection) - g_object_unref (collection); - collection = NULL; - - test_secret_module_leave_and_finalize (); - module = NULL; - session = NULL; - -} - -static void -unlock_collection(void) -{ - GkmCredential *cred; - GkmObject *object; - CK_RV rv; - - /* Create credential, which unlocks collection */ - object = GKM_OBJECT (collection); - rv = gkm_credential_create (gkm_object_get_module (object), - gkm_session_get_manager (session), - object, NULL, 0, &cred); - g_assert (rv == CKR_OK); - - gkm_session_add_session_object (session, NULL, GKM_OBJECT (cred)); - g_object_unref (cred); -} - -TESTING_TEST(secret_item_new) -{ - GkmSecretItem *item; - - item = gkm_secret_collection_new_item (collection, "the-identifier"); - g_assert (GKM_IS_SECRET_ITEM (item)); - g_assert_cmpstr (gkm_secret_object_get_identifier (GKM_SECRET_OBJECT (item)), ==, "the-identifier"); -} - -TESTING_TEST(secret_item_create) -{ - GkmTransaction *transaction; - GkmSecretItem *item; - - transaction = gkm_transaction_new (); - item = gkm_secret_collection_create_item (collection, transaction); - g_assert (GKM_IS_SECRET_ITEM (item)); - g_object_ref (item); - g_assert (gkm_secret_collection_has_item (collection, item)); - - gkm_transaction_complete (transaction); - g_object_unref (transaction); - - /* Should still be there */ - g_assert (gkm_secret_collection_has_item (collection, item)); - g_object_unref (item); -} - -TESTING_TEST(secret_item_create_failed) -{ - GkmTransaction *transaction; - GkmSecretItem *item; - - transaction = gkm_transaction_new (); - item = gkm_secret_collection_create_item (collection, transaction); - g_assert (GKM_IS_SECRET_ITEM (item)); - g_object_ref (item); - g_assert (gkm_secret_collection_has_item (collection, item)); - - gkm_transaction_fail (transaction, CKR_GENERAL_ERROR); - gkm_transaction_complete (transaction); - g_object_unref (transaction); - - /* Should no longer be there */ - g_assert (!gkm_secret_collection_has_item (collection, item)); - g_object_unref (item); -} - -TESTING_TEST(secret_item_destroy) -{ - GkmTransaction *transaction; - GkmSecretItem *item; - - item = gkm_secret_collection_new_item (collection, "the-identifier"); - g_assert (gkm_secret_collection_has_item (collection, item)); - g_object_ref (item); - - transaction = gkm_transaction_new (); - gkm_secret_collection_destroy_item (collection, transaction, item); - g_assert (!gkm_secret_collection_has_item (collection, item)); - - gkm_transaction_complete (transaction); - g_object_unref (transaction); - - /* Should not be there */ - g_assert (!gkm_secret_collection_has_item (collection, item)); - g_object_unref (item); -} - -TESTING_TEST(secret_item_destroy_failed) -{ - GkmTransaction *transaction; - GkmSecretItem *item; - - item = gkm_secret_collection_new_item (collection, "the-identifier"); - g_assert (gkm_secret_collection_has_item (collection, item)); - g_object_ref (item); - - transaction = gkm_transaction_new (); - gkm_secret_collection_destroy_item (collection, transaction, item); - g_assert (!gkm_secret_collection_has_item (collection, item)); - - gkm_transaction_fail (transaction, CKR_GENERAL_ERROR); - gkm_transaction_complete (transaction); - g_object_unref (transaction); - - /* Should be there */ - g_assert (gkm_secret_collection_has_item (collection, item)); - g_object_unref (item); -} - -TESTING_TEST(secret_item_collection_get) -{ - GkmSecretItem *item, *check; - - item = gkm_secret_collection_new_item (collection, "the-identifier"); - g_assert (GKM_IS_SECRET_ITEM (item)); - - check = gkm_secret_collection_get_item (collection, "the-identifier"); - g_assert (item == check); -} - -TESTING_TEST(secret_item_collection_items) -{ - GList *l, *items; - const gchar *identifier; - - gkm_secret_collection_new_item (collection, "one-identifier"); - gkm_secret_collection_new_item (collection, "two-identifier"); - gkm_secret_collection_new_item (collection, "three-identifier"); - - items = gkm_secret_collection_get_items (collection); - g_assert_cmpuint (g_list_length (items), ==, 3); - for (l = items; l; l = g_list_next (l)) { - identifier = gkm_secret_object_get_identifier (l->data); - if (!g_str_equal (identifier, "one-identifier") && - !g_str_equal (identifier, "two-identifier") && - !g_str_equal (identifier, "three-identifier")) - g_assert_not_reached (); - } - - g_list_free (items); -} - -TESTING_TEST(secret_item_collection_remove) -{ - GkmSecretItem *item; - - item = gkm_secret_collection_new_item (collection, "the-identifier"); - g_assert (gkm_secret_collection_get_item (collection, "the-identifier") == item); - - gkm_secret_collection_remove_item (collection, item); - g_assert (gkm_secret_collection_get_item (collection, "the-identifier") == NULL); -} - -TESTING_TEST(secret_item_is_locked) -{ - GkmSecretItem *item; - - item = gkm_secret_collection_new_item (collection, "the-identifier"); - g_assert (gkm_secret_object_is_locked (GKM_SECRET_OBJECT (item), session) == - gkm_secret_object_is_locked (GKM_SECRET_OBJECT (collection), session)); - - unlock_collection(); - - g_assert (gkm_secret_object_is_locked (GKM_SECRET_OBJECT (item), session) == FALSE); -} - -TESTING_TEST(secret_item_get_collection) -{ - GkmSecretItem *item; - item = gkm_secret_collection_new_item (collection, "the-identifier"); - g_assert (gkm_secret_item_get_collection (item) == collection); -} - -TESTING_TEST(secret_item_tracks_collection) -{ - GkmSecretItem *item; - item = gkm_secret_collection_new_item (collection, "the-identifier"); - g_object_ref (item); - - unlock_collection(); - - /* At this point the item should be 'unlocked' */ - g_assert (gkm_secret_object_is_locked (GKM_SECRET_OBJECT (item), session) == FALSE); - - g_object_unref (collection); - collection = NULL; - - /* Collection went away */ - g_assert (gkm_secret_item_get_collection (item) == NULL); - g_assert (gkm_secret_object_is_locked (GKM_SECRET_OBJECT (item), session) == TRUE); - - g_object_unref (item); -} - -TESTING_TEST(secret_item_get_set_fields) -{ - GHashTable *fields = gkm_secret_fields_new (); - GHashTable *check; - GkmSecretItem *item; - - item = gkm_secret_collection_new_item (collection, "the-identifier"); - gkm_secret_item_set_fields (item, fields); - gkm_secret_item_set_fields (item, fields); - - check = gkm_secret_item_get_fields (item); - g_assert (check == fields); - - g_hash_table_unref (fields); -} - -TESTING_TEST(secret_item_collection_attr) -{ - gchar buffer[32]; - CK_ATTRIBUTE check = { CKA_G_COLLECTION, buffer, 32 }; - GkmSecretItem *item; - CK_RV rv; - - item = gkm_secret_collection_new_item (collection, "the-identifier"); - rv = gkm_object_get_attribute (GKM_OBJECT (item), session, &check); - g_assert (rv == CKR_OK); - g_assert (check.ulValueLen == 4); - g_assert (memcmp (buffer, "test", 4) == 0); -} - -TESTING_TEST(secret_item_secret_attr) -{ - GkmTransaction *transaction = gkm_transaction_new (); - CK_ATTRIBUTE attr = { CKA_VALUE, "hello", 5 }; - gchar buffer[32]; - CK_ATTRIBUTE check = { CKA_VALUE, buffer, 32 }; - GkmSecretItem *item; - CK_RV rv; - - unlock_collection (); - - item = gkm_secret_collection_new_item (collection, "the-identifier"); - gkm_object_set_attribute (GKM_OBJECT (item), session, transaction, &attr); - g_assert (gkm_transaction_get_failed (transaction) == FALSE); - gkm_transaction_complete (transaction); - g_assert (gkm_transaction_get_result (transaction) == CKR_OK); - - g_object_unref (transaction); - - rv = gkm_object_get_attribute (GKM_OBJECT (item), session, &check); - g_assert (rv == CKR_OK); - g_assert (check.ulValueLen == 5); - g_assert (memcmp (buffer, "hello", 5) == 0); -} - -TESTING_TEST(secret_item_secret_attr_locked) -{ - GkmTransaction *transaction = gkm_transaction_new (); - CK_ATTRIBUTE attr = { CKA_VALUE, "hello", 5 }; - gchar buffer[32]; - CK_ATTRIBUTE check = { CKA_VALUE, buffer, 32 }; - GkmSecretItem *item; - CK_RV rv; - - item = gkm_secret_collection_new_item (collection, "the-identifier"); - gkm_object_set_attribute (GKM_OBJECT (item), session, transaction, &attr); - g_assert (gkm_transaction_get_failed (transaction) == TRUE); - gkm_transaction_complete (transaction); - g_assert (gkm_transaction_get_result (transaction) == CKR_USER_NOT_LOGGED_IN); - - g_object_unref (transaction); - - rv = gkm_object_get_attribute (GKM_OBJECT (item), session, &check); - g_assert (rv == CKR_USER_NOT_LOGGED_IN); -} - -TESTING_TEST(secret_item_fields_attr) -{ - GkmTransaction *transaction = gkm_transaction_new (); - CK_ATTRIBUTE attr = { CKA_G_FIELDS, "name1\0value1\0name2\0value2", 26 }; - gchar buffer[32]; - CK_ATTRIBUTE check = { CKA_G_FIELDS, buffer, 32 }; - GkmSecretItem *item; - GHashTable *fields; - const gchar *value; - CK_RV rv; - - unlock_collection (); - - item = gkm_secret_collection_new_item (collection, "the-identifier"); - gkm_object_set_attribute (GKM_OBJECT (item), session, transaction, &attr); - g_assert (gkm_transaction_get_failed (transaction) == FALSE); - gkm_transaction_complete (transaction); - g_assert (gkm_transaction_get_result (transaction) == CKR_OK); - - g_object_unref (transaction); - - rv = gkm_object_get_attribute (GKM_OBJECT (item), session, &check); - g_assert (rv == CKR_OK); - g_assert (check.ulValueLen == 26); - g_assert (memcmp (buffer, "name1\0value1\0name2\0value2", 26) == 0); - - fields = gkm_secret_item_get_fields (item); - g_assert (fields); - value = gkm_secret_fields_get (fields, "name1"); - g_assert_cmpstr (value, ==, "value1"); - value = gkm_secret_fields_get (fields, "name2"); - g_assert_cmpstr (value, ==, "value2"); -} - -TESTING_TEST(secret_item_fields_attr_locked) -{ - GkmTransaction *transaction = gkm_transaction_new (); - CK_ATTRIBUTE attr = { CKA_G_FIELDS, "name1\0value1\0name2\0value2", 26 }; - GkmSecretItem *item; - - item = gkm_secret_collection_new_item (collection, "the-identifier"); - gkm_object_set_attribute (GKM_OBJECT (item), session, transaction, &attr); - g_assert (gkm_transaction_get_failed (transaction) == TRUE); - gkm_transaction_complete (transaction); - g_assert (gkm_transaction_get_result (transaction) == CKR_USER_NOT_LOGGED_IN); - - g_object_unref (transaction); -} - -TESTING_TEST(secret_item_fields_attr_reverts) -{ - GkmTransaction *transaction = gkm_transaction_new (); - CK_ATTRIBUTE attr = { CKA_G_FIELDS, "new\0value\0", 10 }; - gchar buffer[32]; - CK_ATTRIBUTE check = { CKA_G_FIELDS, buffer, 32 }; - GkmSecretItem *item; - GHashTable *fields; - CK_RV rv; - - unlock_collection (); - - item = gkm_secret_collection_new_item (collection, "the-identifier"); - - /* Set the old value like so */ - fields = gkm_secret_fields_new (); - gkm_secret_fields_add (fields, "old", "value"); - gkm_secret_item_set_fields (item, fields); - g_hash_table_unref (fields); - - /* Should show old value */ - rv = gkm_object_get_attribute (GKM_OBJECT (item), session, &check); - g_assert (rv == CKR_OK); - g_assert (check.ulValueLen == 10); - g_assert (memcmp (buffer, "old\0value\0", 10) == 0); - - /* Set the new values */ - gkm_object_set_attribute (GKM_OBJECT (item), session, transaction, &attr); - g_assert (gkm_transaction_get_failed (transaction) == FALSE); - - /* Should have the new value */ - rv = gkm_object_get_attribute (GKM_OBJECT (item), session, &check); - g_assert (rv == CKR_OK); - g_assert (check.ulValueLen == 10); - g_assert (memcmp (buffer, "new\0value\0", 10) == 0); - - /* Fail the transaction */ - gkm_transaction_fail (transaction, CKR_CANCEL); - gkm_transaction_complete (transaction); - - /* Should show the old value */ - rv = gkm_object_get_attribute (GKM_OBJECT (item), session, &check); - g_assert (rv == CKR_OK); - g_assert (check.ulValueLen == 10); - g_assert (memcmp (buffer, "old\0value\0", 10) == 0); - - g_object_unref (transaction); -} diff --git a/pkcs11/secret-store/tests/unit-test-secret-object.c b/pkcs11/secret-store/tests/unit-test-secret-object.c deleted file mode 100644 index b915e40a..00000000 --- a/pkcs11/secret-store/tests/unit-test-secret-object.c +++ /dev/null @@ -1,269 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* unit-test-secret-collection.c: Test the collection keyring - - 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 "test-suite.h" -#include "test-secret-module.h" - -#include "gkm-secret-object.h" - -#include "gkm/gkm-transaction.h" - -#include "pkcs11/pkcs11i.h" - -#include <glib.h> - -#include <stdlib.h> -#include <stdio.h> -#include <string.h> - -static GkmModule *module = NULL; -static GkmSession *session = NULL; -static GkmSecretObject *object = NULL; - -TESTING_SETUP(secret_object) -{ - module = test_secret_module_initialize_and_enter (); - session = test_secret_module_open_session (TRUE); - - object = g_object_new (GKM_TYPE_SECRET_OBJECT, - "module", module, - "identifier", "my-identifier", - NULL); - - g_assert (GKM_IS_SECRET_OBJECT (object)); -} - -TESTING_TEARDOWN(secret_object) -{ - g_object_unref (object); - object = NULL; - - test_secret_module_leave_and_finalize (); - module = NULL; - session = NULL; -} - -TESTING_TEST(secret_object_is_locked) -{ - /* Plain GkmSecretObject is never locked */ - g_assert (!gkm_secret_object_is_locked (object, session)); -} - -TESTING_TEST(secret_object_identifier_prop) -{ - const gchar *identifier; - identifier = gkm_secret_object_get_identifier (object); - g_assert_cmpstr (identifier, ==, "my-identifier"); -} - -static void -was_notified (GObject *obj, GParamSpec *pspec, gpointer user_data) -{ - gboolean* notified = user_data; - g_assert (GKM_SECRET_OBJECT (obj) == object); - g_assert (user_data); - *notified = TRUE; -} - -TESTING_TEST(secret_object_created_prop) -{ - glong created; - - /* Monitor for changes */ - gboolean notified = FALSE; - g_signal_connect (object, "notify::created", G_CALLBACK (was_notified), ¬ified); - - /* Default value */ - created = gkm_secret_object_get_created (object); - g_assert (created == 0); - - /* Set a new value */ - gkm_secret_object_set_created (object, 1247930171); - g_assert (notified); - created = gkm_secret_object_get_created (object); - g_assert (created == 1247930171); -} - -TESTING_TEST(secret_object_modified_prop) -{ - glong modified; - - /* Monitor for changes */ - gboolean notified = FALSE; - g_signal_connect (object, "notify::modified", G_CALLBACK (was_notified), ¬ified); - - /* Default value */ - modified = gkm_secret_object_get_modified (object); - g_assert (modified == 0); - - /* Set a new value */ - gkm_secret_object_set_modified (object, 1247930171); - g_assert (notified); - modified = gkm_secret_object_get_modified (object); - g_assert (modified == 1247930171); -} - -TESTING_TEST(secret_object_was_modified) -{ - GTimeVal tv; - g_get_current_time (&tv); - gkm_secret_object_was_modified (object); - g_assert (tv.tv_sec == gkm_secret_object_get_modified (object)); -} - -TESTING_TEST(secret_object_label_prop) -{ - const gchar *label; - - /* Monitor for changes */ - gboolean notified = FALSE; - g_signal_connect (object, "notify::label", G_CALLBACK (was_notified), ¬ified); - - /* Default value */ - label = gkm_secret_object_get_label (object); - g_assert_cmpstr (label, ==, ""); - - /* Set a new value */ - gkm_secret_object_set_label (object, "hello"); - g_assert (notified); - label = gkm_secret_object_get_label (object); - g_assert_cmpstr (label, ==, "hello"); -} - -TESTING_TEST(secret_object_identifier_get_attr) -{ - gchar buffer[32]; - CK_ATTRIBUTE attr = { CKA_ID, buffer, 32 }; - CK_RV rv; - - rv = gkm_object_get_attribute (GKM_OBJECT (object), session, &attr); - g_assert (rv == CKR_OK); - g_assert (attr.ulValueLen == 13); - g_assert (memcmp (buffer, "my-identifier", 13) == 0); -} - -TESTING_TEST(secret_object_label_get_attr) -{ - gchar buffer[32]; - CK_ATTRIBUTE attr = { CKA_LABEL, buffer, 32 }; - CK_RV rv; - - gkm_secret_object_set_label (object, "hello"); - rv = gkm_object_get_attribute (GKM_OBJECT (object), session, &attr); - g_assert (rv == CKR_OK); - g_assert (attr.ulValueLen == 5); - g_assert (memcmp (buffer, "hello", 5) == 0); -} - -TESTING_TEST(secret_object_label_set_attr) -{ - CK_ATTRIBUTE attr = { CKA_LABEL, "hello", 5 }; - GkmTransaction *transaction = gkm_transaction_new (); - - /* Monitor for changes */ - gboolean notified = FALSE; - g_signal_connect (object, "notify::label", G_CALLBACK (was_notified), ¬ified); - - gkm_object_set_attribute (GKM_OBJECT (object), session, transaction, &attr); - g_assert (!gkm_transaction_get_failed (transaction)); - g_assert (!notified); /* Not notified yet */ - - g_assert_cmpstr (gkm_secret_object_get_label (object), ==, "hello"); - - gkm_transaction_complete (transaction); - g_assert_cmpstr (gkm_secret_object_get_label (object), ==, "hello"); - g_assert (notified); /* Notified after transaction complete */ - - g_object_unref (transaction); -} - -TESTING_TEST(secret_object_label_set_attr_fail) -{ - CK_ATTRIBUTE attr = { CKA_LABEL, "hello", 5 }; - GkmTransaction *transaction = gkm_transaction_new (); - gboolean notified = FALSE; - - /* Set an old value */ - gkm_secret_object_set_label (object, "old"); - - /* Monitor for changes */ - g_signal_connect (object, "notify::label", G_CALLBACK (was_notified), ¬ified); - - /* Set a new value */ - gkm_object_set_attribute (GKM_OBJECT (object), session, transaction, &attr); - g_assert (!gkm_transaction_get_failed (transaction)); - g_assert (!notified); /* Not notified yet */ - - /* Temporarily has new value */ - g_assert_cmpstr (gkm_secret_object_get_label (object), ==, "hello"); - - /* Fail and complete transaction */ - gkm_transaction_fail (transaction, CKR_CANCEL); - gkm_transaction_complete (transaction); - - /* Back to old value */ - g_assert_cmpstr (gkm_secret_object_get_label (object), ==, "old"); - g_assert (!notified); /* Should never have notified */ - - g_object_unref (transaction); -} - -TESTING_TEST(secret_object_modified_get_attr) -{ - gchar buffer[32]; - CK_ATTRIBUTE attr = { CKA_G_MODIFIED, buffer, 32 }; - CK_RV rv; - - gkm_secret_object_set_modified (object, 1247930171); - rv = gkm_object_get_attribute (GKM_OBJECT (object), session, &attr); - g_assert (rv == CKR_OK); - g_assert (attr.ulValueLen == 16); - g_assert (memcmp (buffer, "2009071815161100", 16) == 0); -} - -TESTING_TEST(secret_object_created_get_attr) -{ - gchar buffer[32]; - CK_ATTRIBUTE attr = { CKA_G_CREATED, buffer, 32 }; - CK_RV rv; - - gkm_secret_object_set_created (object, 1247930171); - rv = gkm_object_get_attribute (GKM_OBJECT (object), session, &attr); - g_assert (rv == CKR_OK); - g_assert (attr.ulValueLen == 16); - g_assert (memcmp (buffer, "2009071815161100", 16) == 0); -} - -TESTING_TEST(secret_object_locked_get_attr) -{ - gchar buffer[32]; - CK_ATTRIBUTE attr = { CKA_G_LOCKED, buffer, 32 }; - CK_RV rv; - - rv = gkm_object_get_attribute (GKM_OBJECT (object), session, &attr); - g_assert (rv == CKR_OK); - g_assert (attr.ulValueLen == 1); - g_assert (memcmp (buffer, "\0", 1) == 0); -} diff --git a/pkcs11/secret-store/tests/unit-test-secret-textual.c b/pkcs11/secret-store/tests/unit-test-secret-textual.c deleted file mode 100644 index 4f4af27d..00000000 --- a/pkcs11/secret-store/tests/unit-test-secret-textual.c +++ /dev/null @@ -1,184 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* unit-test-secret-textual.c: Test textual keyring read and write - - 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 "test-suite.h" -#include "test-secret-module.h" - -#include "gkm-secret-collection.h" -#include "gkm-secret-data.h" -#include "gkm-secret-fields.h" -#include "gkm-secret-item.h" -#include "gkm-secret-textual.h" - -#include "gkm/gkm-secret.h" - -#include "pkcs11/pkcs11i.h" - -#include <glib.h> - -#include <stdlib.h> -#include <stdio.h> -#include <string.h> - -static GkmModule *module = NULL; -static GkmSession *session = NULL; -static GkmSecretCollection *collection = NULL; -static GkmSecretData *sdata = NULL; - -TESTING_SETUP(textual) -{ - module = test_secret_module_initialize_and_enter (); - session = test_secret_module_open_session (TRUE); - - collection = g_object_new (GKM_TYPE_SECRET_COLLECTION, - "module", module, - "identifier", "test", - "label", "brigadooooooooooooon", - NULL); - - sdata = g_object_new (GKM_TYPE_SECRET_DATA, NULL); - - g_assert (GKM_IS_SECRET_COLLECTION (collection)); - -} - -TESTING_TEARDOWN(textual) -{ - if (collection) - g_object_unref (collection); - collection = NULL; - - if (sdata) - g_object_unref (sdata); - sdata = NULL; - - test_secret_module_leave_and_finalize (); - module = NULL; - session = NULL; -} - -TESTING_TEST(textual_read) -{ - GkmDataResult res; - guchar *data; - gsize n_data; - - data = testing_data_read ("plain.keyring", &n_data); - res = gkm_secret_textual_read (collection, sdata, data, n_data); - g_free (data); - - test_secret_collection_validate (collection, sdata); - - g_assert (res == GKM_DATA_SUCCESS); -} - -TESTING_TEST(textual_read_wrong_format) -{ - GkmDataResult res; - guchar *data; - gsize n_data; - - data = testing_data_read ("encrypted.keyring", &n_data); - res = gkm_secret_textual_read (collection, sdata, data, n_data); - g_free (data); - - g_assert (res == GKM_DATA_UNRECOGNIZED); -} - -TESTING_TEST(textual_read_bad_number) -{ - GkmSecretItem *item; - GkmDataResult res; - const gchar *value; - guchar *data; - gsize n_data; - - data = testing_data_read ("plain-bad-number.keyring", &n_data); - res = gkm_secret_textual_read (collection, sdata, data, n_data); - g_free (data); - - g_assert (res == GKM_DATA_SUCCESS); - - item = gkm_secret_collection_get_item (collection, "1"); - g_assert (GKM_IS_SECRET_ITEM (item)); - value = gkm_secret_fields_get (gkm_secret_item_get_fields (item), "bad-number"); - g_assert (value == NULL); - value = gkm_secret_fields_get (gkm_secret_item_get_fields (item), "missing-number"); - g_assert (value == NULL); -} - -TESTING_TEST(textual_write) -{ - GkmDataResult res; - guchar *data; - gsize n_data; - - test_secret_collection_populate (collection, sdata); - - res = gkm_secret_textual_write (collection, sdata, &data, &n_data); - g_assert (res == GKM_DATA_SUCCESS); - g_assert (data); - g_assert (n_data); - - /* Try parsing it again */ - res = gkm_secret_textual_read (collection, sdata, data, n_data); - g_assert (res == GKM_DATA_SUCCESS); -} - -TESTING_TEST(textual_remove_unavailable) -{ - GkmDataResult res; - GList *items; - guchar *data; - gsize n_data; - - data = testing_data_read ("plain.keyring", &n_data); - res = gkm_secret_textual_read (collection, sdata, data, n_data); - g_assert (res == GKM_DATA_SUCCESS); - - /* Two items from the file */ - items = gkm_secret_collection_get_items (collection); - g_assert_cmpint (g_list_length (items), ==, 2); - g_list_free (items); - - /* Fill in some more data */ - test_secret_collection_populate (collection, sdata); - - /* Should have added three more */ - items = gkm_secret_collection_get_items (collection); - g_assert_cmpint (g_list_length (items), ==, 5); - g_list_free (items); - - /* Re-read the keyring */ - res = gkm_secret_textual_read (collection, sdata, data, n_data); - g_assert (res == GKM_DATA_SUCCESS); - - /* And we're back to two */ - items = gkm_secret_collection_get_items (collection); - g_assert_cmpint (g_list_length (items), ==, 2); - g_list_free (items); - - g_free (data); -} diff --git a/pkcs11/ssh-store/gkm-ssh-openssh.c b/pkcs11/ssh-store/gkm-ssh-openssh.c index c165cd28..46e86605 100644 --- a/pkcs11/ssh-store/gkm-ssh-openssh.c +++ b/pkcs11/ssh-store/gkm-ssh-openssh.c @@ -255,7 +255,7 @@ digest_pem_block (GQuark type, const guchar *data, gsize n_data, */ GkmDataResult -gkm_ssh_openssh_parse_public_key (const guchar *data, gsize n_data, +gkm_ssh_openssh_parse_public_key (gconstpointer input, gsize n_data, gcry_sexp_t *sexp, gchar **comment) { EggBuffer buf; @@ -267,6 +267,7 @@ gkm_ssh_openssh_parse_public_key (const guchar *data, gsize n_data, gboolean ret; gint state, algo; guint save; + const guchar *data = input; g_return_val_if_fail (data, FALSE); g_return_val_if_fail (sexp, FALSE); @@ -360,7 +361,7 @@ gkm_ssh_openssh_parse_public_key (const guchar *data, gsize n_data, } GkmDataResult -gkm_ssh_openssh_parse_private_key (const guchar *data, gsize n_data, +gkm_ssh_openssh_parse_private_key (gconstpointer data, gsize n_data, const gchar *password, gssize n_password, gcry_sexp_t *sexp) { diff --git a/pkcs11/ssh-store/gkm-ssh-openssh.h b/pkcs11/ssh-store/gkm-ssh-openssh.h index 2a2a7d06..fe8db8e4 100644 --- a/pkcs11/ssh-store/gkm-ssh-openssh.h +++ b/pkcs11/ssh-store/gkm-ssh-openssh.h @@ -7,12 +7,12 @@ #include "gkm/gkm-data-types.h" -GkmDataResult gkm_ssh_openssh_parse_public_key (const guchar *data, +GkmDataResult gkm_ssh_openssh_parse_public_key (gconstpointer data, gsize n_data, gcry_sexp_t *sexp, gchar **comment); -GkmDataResult gkm_ssh_openssh_parse_private_key (const guchar *data, +GkmDataResult gkm_ssh_openssh_parse_private_key (gconstpointer data, gsize n_data, const gchar *password, gssize n_password, diff --git a/pkcs11/ssh-store/tests/Makefile.am b/pkcs11/ssh-store/tests/Makefile.am index 8f962d20..15a9975b 100644 --- a/pkcs11/ssh-store/tests/Makefile.am +++ b/pkcs11/ssh-store/tests/Makefile.am @@ -1,19 +1,51 @@ -TESTING_SOURCES = \ - test-ssh-module.h -TESTING_FILES = \ - unit-test-ssh-openssh.c \ - unit-test-private-key.c \ - test-ssh-module.c +INCLUDES = \ + -I$(top_builddir) \ + -I$(top_srcdir) \ + -I$(top_srcdir)/pkcs11 \ + -DSRCDIR="\"$(srcdir)\"" \ + $(GLIB_CFLAGS) \ + $(LIBGCRYPT_CFLAGS) -UNIT_PROMPT = - -TESTING_LIBS = \ +LDADD = \ + libgkm-mock-ssh-module.a \ $(top_builddir)/pkcs11/ssh-store/libgkm-ssh-store.la \ $(top_builddir)/pkcs11/gkm/libgkm.la \ - $(top_builddir)/egg/libegg.la + $(top_builddir)/egg/libegg.la \ + $(GLIB_LIBS) \ + $(LIBGCRYPT_LIBS) + +if WITH_P11_TESTS +CHECK_PROGS = check-ssh-module +else +CHECK_PROGS = +endif + +TEST_PROGS = \ + test-ssh-openssh \ + test-private-key + +check_PROGRAMS = $(TEST_PROGS) + +test: $(TEST_PROGS) $(CHECK_PROGS) + gtester -k --verbose -m $(TEST_MODE) --g-fatal-warnings $(TEST_PROGS) + @for prog in $(CHECK_PROGS); do SRCDIR='.' ./$$prog || exit 1; done + +check-local: test + +all-local: $(check_PROGRAMS) + +EXTRA_DIST = \ + p11-tests.conf \ + files + +noinst_PROGRAMS = \ + $(CHECK_PROGS) + +check_ssh_module_CFLAGS = $(P11_TESTS_CFLAGS) +check_ssh_module_LDADD = $(P11_TESTS_LIBS) $(LDADD) -include $(top_srcdir)/testing/testing.make +noinst_LIBRARIES = libgkm-mock-ssh-module.a -EXTRA_DIST += \ - test-data +libgkm_mock_ssh_module_a_SOURCES = \ + mock-ssh-module.c mock-ssh-module.h diff --git a/pkcs11/ssh-store/tests/check-ssh-module.c b/pkcs11/ssh-store/tests/check-ssh-module.c new file mode 100644 index 00000000..eeeb2de0 --- /dev/null +++ b/pkcs11/ssh-store/tests/check-ssh-module.c @@ -0,0 +1,68 @@ +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ +/* check-module.c: Check PKCS#11 implementation + + 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 "ssh-store/gkm-ssh-store.h" + +#include "egg/egg-secure-memory.h" + +#include <glib.h> +#include <glib-object.h> + +#include <p11-tests.h> + +static int failures = 0; + +EGG_SECURE_GLIB_DEFINITIONS (); + +static void +on_p11_tests_log (int level, const char *section, const char *message) +{ + if (level == P11_TESTS_NONE) { + g_message ("%s", message); + } else if (level != P11_TESTS_FAIL) { + g_message ("%s: %s", section, message); + } else { + g_print (" /ssh-store/%s: FAIL: %s\n", section, message); + ++failures; + } +} + +int +main (int argc, const char *argv[]) +{ + g_type_init (); + + p11_tests_set_log_func (on_p11_tests_log); + p11_tests_set_unexpected (1); + p11_tests_set_verbose (0); + p11_tests_set_write_session (1); + p11_tests_load_config (SRCDIR "/p11-tests.conf"); + + g_print ("CHECK: check-ssh-module...\n"); + p11_tests_perform (gkm_ssh_store_get_functions ()); + + g_print ("%s: check-ssh-module\n", failures ? "FAIL" : "PASS"); + return failures; +} diff --git a/pkcs11/ssh-store/tests/test-data/id_dsa_encrypted b/pkcs11/ssh-store/tests/files/id_dsa_encrypted index c17fb80b..c17fb80b 100644 --- a/pkcs11/ssh-store/tests/test-data/id_dsa_encrypted +++ b/pkcs11/ssh-store/tests/files/id_dsa_encrypted diff --git a/pkcs11/ssh-store/tests/test-data/id_dsa_encrypted.pub b/pkcs11/ssh-store/tests/files/id_dsa_encrypted.pub index a64acac6..a64acac6 100644 --- a/pkcs11/ssh-store/tests/test-data/id_dsa_encrypted.pub +++ b/pkcs11/ssh-store/tests/files/id_dsa_encrypted.pub diff --git a/pkcs11/ssh-store/tests/test-data/id_dsa_plain b/pkcs11/ssh-store/tests/files/id_dsa_plain index 2687623d..2687623d 100644 --- a/pkcs11/ssh-store/tests/test-data/id_dsa_plain +++ b/pkcs11/ssh-store/tests/files/id_dsa_plain diff --git a/pkcs11/ssh-store/tests/test-data/id_dsa_plain.pub b/pkcs11/ssh-store/tests/files/id_dsa_plain.pub index 3b4717d7..3b4717d7 100644 --- a/pkcs11/ssh-store/tests/test-data/id_dsa_plain.pub +++ b/pkcs11/ssh-store/tests/files/id_dsa_plain.pub diff --git a/pkcs11/ssh-store/tests/test-data/id_dsa_test.pub b/pkcs11/ssh-store/tests/files/id_dsa_test.pub index 33653b23..33653b23 100644 --- a/pkcs11/ssh-store/tests/test-data/id_dsa_test.pub +++ b/pkcs11/ssh-store/tests/files/id_dsa_test.pub diff --git a/pkcs11/ssh-store/tests/test-data/id_rsa_encrypted b/pkcs11/ssh-store/tests/files/id_rsa_encrypted index 2c728602..2c728602 100644 --- a/pkcs11/ssh-store/tests/test-data/id_rsa_encrypted +++ b/pkcs11/ssh-store/tests/files/id_rsa_encrypted diff --git a/pkcs11/ssh-store/tests/test-data/id_rsa_encrypted.pub b/pkcs11/ssh-store/tests/files/id_rsa_encrypted.pub index 5883b320..5883b320 100644 --- a/pkcs11/ssh-store/tests/test-data/id_rsa_encrypted.pub +++ b/pkcs11/ssh-store/tests/files/id_rsa_encrypted.pub diff --git a/pkcs11/ssh-store/tests/test-data/id_rsa_plain b/pkcs11/ssh-store/tests/files/id_rsa_plain index 2e184e40..2e184e40 100644 --- a/pkcs11/ssh-store/tests/test-data/id_rsa_plain +++ b/pkcs11/ssh-store/tests/files/id_rsa_plain diff --git a/pkcs11/ssh-store/tests/test-data/id_rsa_plain.pub b/pkcs11/ssh-store/tests/files/id_rsa_plain.pub index a00a1e9b..a00a1e9b 100644 --- a/pkcs11/ssh-store/tests/test-data/id_rsa_plain.pub +++ b/pkcs11/ssh-store/tests/files/id_rsa_plain.pub diff --git a/pkcs11/ssh-store/tests/test-data/id_rsa_test.pub b/pkcs11/ssh-store/tests/files/id_rsa_test.pub index 61870b30..61870b30 100644 --- a/pkcs11/ssh-store/tests/test-data/id_rsa_test.pub +++ b/pkcs11/ssh-store/tests/files/id_rsa_test.pub diff --git a/pkcs11/ssh-store/tests/test-ssh-module.c b/pkcs11/ssh-store/tests/mock-ssh-module.c index 76b25016..2cec728e 100644 --- a/pkcs11/ssh-store/tests/test-ssh-module.c +++ b/pkcs11/ssh-store/tests/mock-ssh-module.c @@ -22,13 +22,16 @@ */ #include "config.h" -#include "test-ssh-module.h" + +#include "mock-ssh-module.h" + +#include "egg/egg-secure-memory.h" #include "gkm/gkm-module.h" #include "ssh-store/gkm-ssh-store.h" -#include "test-suite.h" +EGG_SECURE_GLIB_DEFINITIONS (); static GMutex *mutex = NULL; @@ -105,9 +108,3 @@ test_ssh_module_open_session (gboolean writable) return session; } - -TESTING_EXTERNAL(ssh_module) -{ - CK_FUNCTION_LIST_PTR funcs = gkm_ssh_store_get_functions (); - testing_test_p11_module (funcs, "p11-tests.conf"); -} diff --git a/pkcs11/ssh-store/tests/test-ssh-module.h b/pkcs11/ssh-store/tests/mock-ssh-module.h index fa46efda..fa46efda 100644 --- a/pkcs11/ssh-store/tests/test-ssh-module.h +++ b/pkcs11/ssh-store/tests/mock-ssh-module.h diff --git a/pkcs11/ssh-store/tests/unit-test-private-key.c b/pkcs11/ssh-store/tests/test-private-key.c index e8d69590..182f8482 100644 --- a/pkcs11/ssh-store/tests/unit-test-private-key.c +++ b/pkcs11/ssh-store/tests/test-private-key.c @@ -21,8 +21,9 @@ Author: Stef Walter <stef@memberwebs.com> */ -#include "test-suite.h" -#include "test-ssh-module.h" +#include "config.h" + +#include "mock-ssh-module.h" #include "gkm/gkm-credential.h" #include "gkm/gkm-session.h" @@ -32,66 +33,70 @@ #include "pkcs11i.h" -static GkmModule *module = NULL; -static GkmSession *session = NULL; +typedef struct { + GkmModule *module; + GkmSession *session; +} Test; -TESTING_SETUP(private_key_setup) +static void +setup (Test *test, gconstpointer unused) { - module = test_ssh_module_initialize_and_enter (); - session = test_ssh_module_open_session (TRUE); + test->module = test_ssh_module_initialize_and_enter (); + test->session = test_ssh_module_open_session (TRUE); } -TESTING_TEARDOWN(private_key_teardown) +static void +teardown (Test *test, gconstpointer unused) { test_ssh_module_leave_and_finalize (); - module = NULL; - session = NULL; } -TESTING_TEST(private_key_parse_plain) +static void +test_parse_plain (Test *test, gconstpointer unused) { GkmSshPrivateKey *key; - gchar *pub_path, *priv_path; gboolean ret; - key = gkm_ssh_private_key_new (module, "my-unique"); + key = gkm_ssh_private_key_new (test->module, "my-unique"); g_assert (GKM_IS_SSH_PRIVATE_KEY (key)); - pub_path = testing_data_filename ("id_dsa_plain.pub"); - priv_path = testing_data_filename ("id_dsa_plain"); - - ret = gkm_ssh_private_key_parse (key, pub_path, priv_path, NULL); + ret = gkm_ssh_private_key_parse (key, SRCDIR "/files/id_dsa_plain.pub", + SRCDIR "/files/id_dsa_plain", NULL); g_assert (ret == TRUE); g_object_unref (key); - g_free (pub_path); - g_free (priv_path); } - -TESTING_TEST(private_key_parse_and_unlock) +static void +test_parse_and_unlock (Test *test, gconstpointer unused) { GkmSshPrivateKey *key; GkmCredential *cred; - gchar *pub_path, *priv_path; gboolean ret; CK_RV rv; - key = gkm_ssh_private_key_new (module, "my-unique"); + key = gkm_ssh_private_key_new (test->module, "my-unique"); g_assert (GKM_IS_SSH_PRIVATE_KEY (key)); - pub_path = testing_data_filename ("id_dsa_encrypted.pub"); - priv_path = testing_data_filename ("id_dsa_encrypted"); - - ret = gkm_ssh_private_key_parse (key, pub_path, priv_path, NULL); + ret = gkm_ssh_private_key_parse (key, SRCDIR "/files/id_dsa_encrypted.pub", + SRCDIR "/files/id_dsa_encrypted", NULL); g_assert (ret == TRUE); - g_free (pub_path); - g_free (priv_path); - - rv = gkm_credential_create (module, NULL, GKM_OBJECT (key), (guchar*)"password", 8, &cred); + rv = gkm_credential_create (test->module, NULL, GKM_OBJECT (key), (guchar*)"password", 8, &cred); g_assert (rv == CKR_OK); g_object_unref (cred); g_object_unref (key); } + +int +main (int argc, char **argv) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + + g_test_add ("/ssh-store/private-key/parse_plain", Test, NULL, setup, test_parse_plain, teardown); + g_test_add ("/ssh-store/private-key/parse_and_unlock", Test, NULL, setup, test_parse_and_unlock, teardown); + + return g_test_run (); +} diff --git a/pkcs11/ssh-store/tests/unit-test-ssh-openssh.c b/pkcs11/ssh-store/tests/test-ssh-openssh.c index c243a4c7..12fc0cc9 100644 --- a/pkcs11/ssh-store/tests/unit-test-ssh-openssh.c +++ b/pkcs11/ssh-store/tests/test-ssh-openssh.c @@ -1,6 +1,5 @@ /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* unit-test-ssh-openssh.c: Test OpenSSH parsing - +/* Copyright (C) 2008 Stefan Walter The Gnome Keyring Library is free software; you can redistribute it and/or @@ -23,9 +22,9 @@ #include "config.h" -#include "test-suite.h" +#include "egg/egg-secure-memory.h" -#include "gkm-ssh-openssh.h" +#include "ssh-store/gkm-ssh-openssh.h" #include "gkm/gkm-sexp.h" @@ -35,25 +34,28 @@ #include <stdio.h> #include <string.h> +EGG_SECURE_GLIB_DEFINITIONS (); + static const gchar *PRIVATE_FILES[] = { - "id_rsa_encrypted", - "id_rsa_plain", - "id_dsa_encrypted", - "id_dsa_plain" + SRCDIR "/files/id_rsa_encrypted", + SRCDIR "/files/id_rsa_plain", + SRCDIR "/files/id_dsa_encrypted", + SRCDIR "/files/id_dsa_plain" }; static const gchar *PUBLIC_FILES[] = { - "id_rsa_test.pub", - "id_dsa_test.pub" + SRCDIR "/files/id_rsa_test.pub", + SRCDIR "/files/id_dsa_test.pub" }; #define COMMENT "A public key comment" -TESTING_TEST(parse_public) +static void +test_parse_public (void) { gcry_sexp_t sexp; gchar *comment; - guchar *data; + gchar *data; gsize n_data; int algorithm; gboolean is_private; @@ -63,7 +65,8 @@ TESTING_TEST(parse_public) for (i = 0; i < G_N_ELEMENTS (PUBLIC_FILES); ++i) { - data = testing_data_read (PUBLIC_FILES[i], &n_data); + if (!g_file_get_contents (PUBLIC_FILES[i], &data, &n_data, NULL)) + g_assert_not_reached (); res = gkm_ssh_openssh_parse_public_key (data, n_data, &sexp, &comment); if (res != GKM_DATA_SUCCESS) { @@ -84,10 +87,11 @@ TESTING_TEST(parse_public) } } -TESTING_TEST(parse_private) +static void +test_parse_private (void) { gcry_sexp_t sexp; - guchar *data; + gchar *data; gsize n_data; int algorithm; gboolean is_private; @@ -97,7 +101,8 @@ TESTING_TEST(parse_private) for (i = 0; i < G_N_ELEMENTS (PRIVATE_FILES); ++i) { - data = testing_data_read (PRIVATE_FILES[i], &n_data); + if (!g_file_get_contents (PRIVATE_FILES[i], &data, &n_data, NULL)) + g_assert_not_reached (); res = gkm_ssh_openssh_parse_private_key (data, n_data, "password", 8, &sexp); if (res != GKM_DATA_SUCCESS) { @@ -115,3 +120,15 @@ TESTING_TEST(parse_private) gcry_sexp_release (sexp); } } + +int +main (int argc, char **argv) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + + g_test_add_func ("/ssh-store/openssh/parse_private", test_parse_private); + g_test_add_func ("/ssh-store/openssh/parse_public", test_parse_public); + + return g_test_run (); +} diff --git a/pkcs11/wrap-layer/tests/Makefile.am b/pkcs11/wrap-layer/tests/Makefile.am index fb3ee7b6..bf4ab927 100644 --- a/pkcs11/wrap-layer/tests/Makefile.am +++ b/pkcs11/wrap-layer/tests/Makefile.am @@ -1,24 +1,40 @@ -TESTING_SOURCES = \ - mock-secret-store.c - -TESTING_FILES = \ - test-create-credential.c \ - test-init-pin.c \ - test-login-auto.c \ - test-login-hints.c \ - test-login-keyring.c \ - test-login-specific.c \ - test-login-user.c \ - test-set-pin.c - -TESTING_LIBS = \ +INCLUDES = \ + -I$(top_builddir) \ + -I$(top_srcdir) \ + -I$(top_srcdir)/pkcs11 \ + -DSRCDIR="\"$(srcdir)\"" \ + $(GLIB_CFLAGS) \ + -DWITH_TESTABLE + +LDADD = \ $(top_builddir)/pkcs11/wrap-layer/libgkm-wrap-layer.la \ + libgkm-mock-secret-store.a \ $(top_builddir)/pkcs11/gkm/libgkm.la \ $(top_builddir)/ui/libgku-prompt-testable.la \ - $(top_builddir)/egg/libegg.la + $(top_builddir)/egg/libegg.la \ + $(GLIB_LIBS) -TESTING_FLAGS = \ - -DWITH_TESTABLE +TEST_PROGS = \ + test-create-credential \ + test-init-pin \ + test-login-auto \ + test-login-hints \ + test-login-keyring \ + test-login-specific \ + test-login-user \ + test-set-pin + +check_PROGRAMS = $(TEST_PROGS) + +test: $(TEST_PROGS) $(CHECK_PROGS) + gtester -k --verbose -m $(TEST_MODE) --g-fatal-warnings $(TEST_PROGS) + +check-local: test + +all-local: $(check_PROGRAMS) + +noinst_LIBRARIES = libgkm-mock-secret-store.a -include $(top_srcdir)/testing/testing.make
\ No newline at end of file +libgkm_mock_secret_store_a_SOURCES = \ + mock-secret-store.c
\ No newline at end of file diff --git a/pkcs11/wrap-layer/tests/mock-secret-store.c b/pkcs11/wrap-layer/tests/mock-secret-store.c index d724919f..cb57b7f3 100644 --- a/pkcs11/wrap-layer/tests/mock-secret-store.c +++ b/pkcs11/wrap-layer/tests/mock-secret-store.c @@ -21,7 +21,7 @@ #include "config.h" -#include "test-suite.h" +#include "egg/egg-secure-memory.h" #include "gkm/gkm-attributes.h" #include "gkm/gkm-mock.h" @@ -33,6 +33,8 @@ static guint secret_identifier = 8800; +EGG_SECURE_GLIB_DEFINITIONS (); + static CK_RV mock_secret_C_Initialize (CK_VOID_PTR pInitArgs) { diff --git a/pkcs11/wrap-layer/tests/test-create-credential.c b/pkcs11/wrap-layer/tests/test-create-credential.c index c4e7bd6a..5a3b7c9a 100644 --- a/pkcs11/wrap-layer/tests/test-create-credential.c +++ b/pkcs11/wrap-layer/tests/test-create-credential.c @@ -21,21 +21,26 @@ #include "config.h" -#include "test-suite.h" - #include "gkm/gkm-mock.h" #include "gkm/gkm-test.h" +#include "egg/egg-testing.h" + #include "wrap-layer/gkm-wrap-layer.h" #include "ui/gku-prompt.h" -static CK_FUNCTION_LIST test_functions; -static CK_FUNCTION_LIST_PTR module = NULL; -static CK_SESSION_HANDLE session = 0; -static CK_OBJECT_HANDLE object = 0; +#include <string.h> + +typedef struct { + CK_FUNCTION_LIST functions; + CK_FUNCTION_LIST_PTR module; + CK_SESSION_HANDLE session; + CK_OBJECT_HANDLE object; +} Test; -TESTING_SETUP (create_credential) +static void +setup (Test *test, gconstpointer unused) { CK_FUNCTION_LIST_PTR funcs; CK_SLOT_ID slot_id; @@ -51,60 +56,58 @@ TESTING_SETUP (create_credential) /* Always start off with test functions */ rv = gkm_mock_C_GetFunctionList (&funcs); gkm_assert_cmprv (rv, ==, CKR_OK); - memcpy (&test_functions, funcs, sizeof (test_functions)); + memcpy (&test->functions, funcs, sizeof (test->functions)); gkm_wrap_layer_reset_modules (); - gkm_wrap_layer_add_module (&test_functions); - module = gkm_wrap_layer_get_functions (); + gkm_wrap_layer_add_module (&test->functions); + test->module = gkm_wrap_layer_get_functions (); gku_prompt_dummy_prepare_response (); - /* Open a session */ - rv = (module->C_Initialize) (NULL); + /* Open a test->session */ + rv = (test->module->C_Initialize) (NULL); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = (module->C_GetSlotList) (CK_TRUE, &slot_id, &n_slots); + rv = (test->module->C_GetSlotList) (CK_TRUE, &slot_id, &n_slots); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = (module->C_OpenSession) (slot_id, CKF_SERIAL_SESSION, NULL, NULL, &session); + rv = (test->module->C_OpenSession) (slot_id, CKF_SERIAL_SESSION, NULL, NULL, &test->session); gkm_assert_cmprv (rv, ==, CKR_OK); - /* Find the always authenticate object */ - rv = (module->C_FindObjectsInit) (session, attrs, 1); + /* Find the always authenticate test->object */ + rv = (test->module->C_FindObjectsInit) (test->session, attrs, 1); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = (module->C_FindObjects) (session, &object, 1, &count); + rv = (test->module->C_FindObjects) (test->session, &test->object, 1, &count); gkm_assert_cmprv (rv, ==, CKR_OK); gkm_assert_cmpulong (count, ==, 1); - gkm_assert_cmpulong (object, !=, 0); + gkm_assert_cmpulong (test->object, !=, 0); - rv = (module->C_FindObjectsFinal) (session); + rv = (test->module->C_FindObjectsFinal) (test->session); gkm_assert_cmprv (rv, ==, CKR_OK); } -TESTING_TEARDOWN (create_credential) +static void +teardown (Test *test, gconstpointer unused) { CK_RV rv; g_assert (!gku_prompt_dummy_have_response ()); - object = 0; - - rv = (module->C_CloseSession) (session); + rv = (test->module->C_CloseSession) (test->session); gkm_assert_cmprv (rv, ==, CKR_OK); - session = 0; - rv = (module->C_Finalize) (NULL); + rv = (test->module->C_Finalize) (NULL); gkm_assert_cmprv (rv, ==, CKR_OK); - module = NULL; } -TESTING_TEST (create_credential_ok_password) +static void +test_ok_password (Test *test, gconstpointer unused) { CK_OBJECT_CLASS klass = CKO_G_CREDENTIAL; CK_ATTRIBUTE attrs[] = { { CKA_CLASS, &klass, sizeof (klass) }, - { CKA_G_OBJECT, &object, sizeof (object) }, + { CKA_G_OBJECT, &test->object, sizeof (test->object) }, { CKA_VALUE, NULL, 0 } }; @@ -113,17 +116,18 @@ TESTING_TEST (create_credential_ok_password) gku_prompt_dummy_queue_ok_password ("booo"); - rv = (module->C_CreateObject) (session, attrs, G_N_ELEMENTS (attrs), &cred); + rv = (test->module->C_CreateObject) (test->session, attrs, G_N_ELEMENTS (attrs), &cred); gkm_assert_cmprv (rv, ==, CKR_OK); gkm_assert_cmpulong (cred, !=, 0); } -TESTING_TEST (create_credential_bad_password_then_cancel) +static void +test_bad_password_then_cancel (Test *test, gconstpointer unused) { CK_OBJECT_CLASS klass = CKO_G_CREDENTIAL; CK_ATTRIBUTE attrs[] = { { CKA_CLASS, &klass, sizeof (klass) }, - { CKA_G_OBJECT, &object, sizeof (object) }, + { CKA_G_OBJECT, &test->object, sizeof (test->object) }, { CKA_VALUE, NULL, 0 } }; @@ -133,16 +137,17 @@ TESTING_TEST (create_credential_bad_password_then_cancel) gku_prompt_dummy_queue_ok_password ("bad password"); gku_prompt_dummy_queue_no (); - rv = (module->C_CreateObject) (session, attrs, G_N_ELEMENTS (attrs), &cred); + rv = (test->module->C_CreateObject) (test->session, attrs, G_N_ELEMENTS (attrs), &cred); gkm_assert_cmprv (rv, ==, CKR_PIN_INCORRECT); } -TESTING_TEST (create_credentiaol_cancel_immediately) +static void +test_cancel_immediately (Test *test, gconstpointer unused) { CK_OBJECT_CLASS klass = CKO_G_CREDENTIAL; CK_ATTRIBUTE attrs[] = { { CKA_CLASS, &klass, sizeof (klass) }, - { CKA_G_OBJECT, &object, sizeof (object) }, + { CKA_G_OBJECT, &test->object, sizeof (test->object) }, { CKA_VALUE, NULL, 0 } }; @@ -151,6 +156,19 @@ TESTING_TEST (create_credentiaol_cancel_immediately) gku_prompt_dummy_queue_no (); - rv = (module->C_CreateObject) (session, attrs, G_N_ELEMENTS (attrs), &cred); + rv = (test->module->C_CreateObject) (test->session, attrs, G_N_ELEMENTS (attrs), &cred); gkm_assert_cmprv (rv, ==, CKR_PIN_INCORRECT); } + +int +main (int argc, char **argv) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + + g_test_add ("/wrap-layer/create-credential/ok_password", Test, NULL, setup, test_ok_password, teardown); + g_test_add ("/wrap-layer/create-credential/bad_password_then_cancel", Test, NULL, setup, test_bad_password_then_cancel, teardown); + g_test_add ("/wrap-layer/create-credential/cancel_immediately", Test, NULL, setup, test_cancel_immediately, teardown); + + return egg_tests_run_in_thread_with_loop (); +} diff --git a/pkcs11/wrap-layer/tests/test-init-pin.c b/pkcs11/wrap-layer/tests/test-init-pin.c index 58b02c58..1f9e7a15 100644 --- a/pkcs11/wrap-layer/tests/test-init-pin.c +++ b/pkcs11/wrap-layer/tests/test-init-pin.c @@ -21,7 +21,7 @@ #include "config.h" -#include "test-suite.h" +#include "egg/egg-testing.h" #include "gkm/gkm-mock.h" #include "gkm/gkm-test.h" @@ -30,63 +30,81 @@ #include "ui/gku-prompt.h" -static CK_FUNCTION_LIST functions; -static CK_FUNCTION_LIST_PTR module = NULL; -static CK_SESSION_HANDLE session = 0; +#include <string.h> -TESTING_SETUP (init_pin) +typedef struct { + CK_FUNCTION_LIST functions; + CK_FUNCTION_LIST_PTR module; + CK_SESSION_HANDLE session; +} Test; + +static void +setup (Test *test, gconstpointer unused) { CK_FUNCTION_LIST_PTR funcs; CK_SLOT_ID slot_id; CK_ULONG n_slots = 1; CK_RV rv; - /* Always start off with test functions */ + /* Always start off with test test->functions */ rv = gkm_mock_C_GetFunctionList (&funcs); gkm_assert_cmprv (rv, ==, CKR_OK); - memcpy (&functions, funcs, sizeof (functions)); + memcpy (&test->functions, funcs, sizeof (test->functions)); gkm_wrap_layer_reset_modules (); - gkm_wrap_layer_add_module (&functions); - module = gkm_wrap_layer_get_functions (); + gkm_wrap_layer_add_module (&test->functions); + test->module = gkm_wrap_layer_get_functions (); gku_prompt_dummy_prepare_response (); - /* Open a session */ - rv = (module->C_Initialize) (NULL); + /* Open a test->session */ + rv = (test->module->C_Initialize) (NULL); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = (module->C_GetSlotList) (CK_TRUE, &slot_id, &n_slots); + rv = (test->module->C_GetSlotList) (CK_TRUE, &slot_id, &n_slots); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = (module->C_OpenSession) (slot_id, CKF_SERIAL_SESSION, NULL, NULL, &session); + rv = (test->module->C_OpenSession) (slot_id, CKF_SERIAL_SESSION, NULL, NULL, &test->session); gkm_assert_cmprv (rv, ==, CKR_OK); } -TESTING_TEARDOWN (init_pin) +static void +teardown (Test *test, gconstpointer unused) { CK_RV rv; g_assert (!gku_prompt_dummy_have_response ()); - rv = (module->C_CloseSession) (session); + rv = (test->module->C_CloseSession) (test->session); gkm_assert_cmprv (rv, ==, CKR_OK); - session = 0; + test->session = 0; - rv = (module->C_Finalize) (NULL); + rv = (test->module->C_Finalize) (NULL); gkm_assert_cmprv (rv, ==, CKR_OK); - module = NULL; + test->module = NULL; } -TESTING_TEST (init_pin_ok_password) +static void +test_ok_password (Test *test, gconstpointer unused) { CK_RV rv; gku_prompt_dummy_queue_ok_password ("new"); - rv = (module->C_InitPIN) (session, NULL, 0); + rv = (test->module->C_InitPIN) (test->session, NULL, 0); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = (module->C_Login) (session, CKU_USER, (guchar*)"new", 3); + rv = (test->module->C_Login) (test->session, CKU_USER, (guchar*)"new", 3); gkm_assert_cmprv (rv, ==, CKR_OK); } + +int +main (int argc, char **argv) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + + g_test_add ("/wrap-layer/init-pin/ok_password", Test, NULL, setup, test_ok_password, teardown); + + return egg_tests_run_in_thread_with_loop (); +} diff --git a/pkcs11/wrap-layer/tests/test-login-auto.c b/pkcs11/wrap-layer/tests/test-login-auto.c index 56bcfc21..8ab80d9e 100644 --- a/pkcs11/wrap-layer/tests/test-login-auto.c +++ b/pkcs11/wrap-layer/tests/test-login-auto.c @@ -21,7 +21,7 @@ #include "config.h" -#include "test-suite.h" +#include "egg/egg-testing.h" #include "gkm/gkm-mock.h" #include "gkm/gkm-test.h" @@ -30,15 +30,21 @@ #include "ui/gku-prompt.h" +#include <string.h> + extern CK_FUNCTION_LIST mock_secret_store; -static CK_FUNCTION_LIST functions; -static CK_FUNCTION_LIST_PTR module = NULL; -static CK_SESSION_HANDLE session = 0; -static CK_OBJECT_HANDLE key = 0; -static CK_OBJECT_HANDLE collection = 0; -static CK_MECHANISM mech = { CKM_MOCK_PREFIX, NULL, 0 }; - -TESTING_SETUP (login_auto) + +typedef struct { + CK_FUNCTION_LIST functions; + CK_FUNCTION_LIST_PTR module; + CK_SESSION_HANDLE session; + CK_OBJECT_HANDLE key; + CK_OBJECT_HANDLE collection; + CK_MECHANISM mech; +} Test; + +static void +setup (Test *test, gconstpointer unused) { CK_SLOT_ID slot_id; CK_ULONG n_slots = 1; @@ -56,115 +62,120 @@ TESTING_SETUP (login_auto) { CKA_ID, "other", 5 }, }; - /* Always start off with test functions */ - memcpy (&functions, &mock_secret_store, sizeof (functions)); + test->mech.mechanism = CKM_MOCK_PREFIX; + + /* Always start off with test test->functions */ + memcpy (&test->functions, &mock_secret_store, sizeof (test->functions)); gkm_wrap_layer_reset_modules (); - gkm_wrap_layer_add_module (&functions); - module = gkm_wrap_layer_get_functions (); + gkm_wrap_layer_add_module (&test->functions); + test->module = gkm_wrap_layer_get_functions (); gku_prompt_dummy_prepare_response (); - /* Open a session */ - rv = (module->C_Initialize) (NULL); + /* Open a test->session */ + rv = (test->module->C_Initialize) (NULL); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = (module->C_GetSlotList) (CK_TRUE, &slot_id, &n_slots); + rv = (test->module->C_GetSlotList) (CK_TRUE, &slot_id, &n_slots); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = (module->C_OpenSession) (slot_id, CKF_SERIAL_SESSION, NULL, NULL, &session); + rv = (test->module->C_OpenSession) (slot_id, CKF_SERIAL_SESSION, NULL, NULL, &test->session); gkm_assert_cmprv (rv, ==, CKR_OK); /* Find keyring object */ - rv = (module->C_FindObjectsInit) (session, fattrs, 1); + rv = (test->module->C_FindObjectsInit) (test->session, fattrs, 1); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = (module->C_FindObjects) (session, &collection, 1, &count); + rv = (test->module->C_FindObjects) (test->session, &test->collection, 1, &count); gkm_assert_cmprv (rv, ==, CKR_OK); gkm_assert_cmpulong (count, ==, 1); - gkm_assert_cmpulong (collection, !=, 0); - rv = (module->C_FindObjectsFinal) (session); + gkm_assert_cmpulong (test->collection, !=, 0); + rv = (test->module->C_FindObjectsFinal) (test->session); gkm_assert_cmprv (rv, ==, CKR_OK); - /* Find the key object */ - rv = (module->C_FindObjectsInit) (session, kattrs, 1); + /* Find the test->key object */ + rv = (test->module->C_FindObjectsInit) (test->session, kattrs, 1); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = (module->C_FindObjects) (session, &key, 1, &count); + rv = (test->module->C_FindObjects) (test->session, &test->key, 1, &count); gkm_assert_cmprv (rv, ==, CKR_OK); gkm_assert_cmpulong (count, ==, 1); - gkm_assert_cmpulong (key, !=, 0); - rv = (module->C_FindObjectsFinal) (session); + gkm_assert_cmpulong (test->key, !=, 0); + rv = (test->module->C_FindObjectsFinal) (test->session); gkm_assert_cmprv (rv, ==, CKR_OK); /* Start a signing operation, that needs to be authenticated */ - rv = (module->C_SignInit) (session, &mech, key); + rv = (test->module->C_SignInit) (test->session, &test->mech, test->key); gkm_assert_cmprv (rv, ==, CKR_OK); } -TESTING_TEARDOWN (login_auto) +static void +teardown (Test *test, gconstpointer unused) { CK_RV rv; g_assert (!gku_prompt_dummy_have_response ()); - key = 0; - collection = 0; + test->key = 0; + test->collection = 0; - rv = (module->C_CloseSession) (session); + rv = (test->module->C_CloseSession) (test->session); gkm_assert_cmprv (rv, ==, CKR_OK); - session = 0; + test->session = 0; - rv = (module->C_Finalize) (NULL); + rv = (test->module->C_Finalize) (NULL); gkm_assert_cmprv (rv, ==, CKR_OK); - module = NULL; + test->module = NULL; } -TESTING_TEST (login_auto_specific) +static void +test_specific (Test *test, gconstpointer unused) { CK_RV rv; /* Login with prompt */ gku_prompt_dummy_queue_auto_password ("booo"); - rv = (module->C_Login) (session, CKU_CONTEXT_SPECIFIC, NULL, 0); + rv = (test->module->C_Login) (test->session, CKU_CONTEXT_SPECIFIC, NULL, 0); gkm_assert_cmprv (rv, ==, CKR_OK); /* Start a signing operation, that needs to be authenticated */ - rv = (module->C_SignInit) (session, &mech, key); + rv = (test->module->C_SignInit) (test->session, &test->mech, test->key); gkm_assert_cmprv (rv, ==, CKR_OK); /* No further prompting should be shown, uses stored password */ gku_prompt_dummy_prepare_response (); - rv = (module->C_Login) (session, CKU_CONTEXT_SPECIFIC, NULL, 0); + rv = (test->module->C_Login) (test->session, CKU_CONTEXT_SPECIFIC, NULL, 0); gkm_assert_cmprv (rv, ==, CKR_OK); /* Change the password */ gkm_mock_module_set_pin ("other"); /* Start a signing operation, that needs to be authenticated */ - rv = (module->C_SignInit) (session, &mech, key); + rv = (test->module->C_SignInit) (test->session, &test->mech, test->key); gkm_assert_cmprv (rv, ==, CKR_OK); /* This should prompt again, as stored password is now wrong */ gku_prompt_dummy_queue_ok_password ("other"); - rv = (module->C_Login) (session, CKU_CONTEXT_SPECIFIC, NULL, 0); + rv = (test->module->C_Login) (test->session, CKU_CONTEXT_SPECIFIC, NULL, 0); gkm_assert_cmprv (rv, ==, CKR_OK); } -TESTING_TEST (login_auto_user_token) +static void +test_user_token (Test *test, gconstpointer unused) { CK_RV rv; /* Login with prompt */ gku_prompt_dummy_queue_auto_password ("booo"); - rv = (module->C_Login) (session, CKU_USER, NULL, 0); + rv = (test->module->C_Login) (test->session, CKU_USER, NULL, 0); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = (module->C_Logout) (session); + rv = (test->module->C_Logout) (test->session); gkm_assert_cmprv (rv, ==, CKR_OK); /* No further prompting should be shown, uses stored password */ gku_prompt_dummy_prepare_response (); - rv = (module->C_Login) (session, CKU_USER, NULL, 0); + rv = (test->module->C_Login) (test->session, CKU_USER, NULL, 0); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = (module->C_Logout) (session); + rv = (test->module->C_Logout) (test->session); gkm_assert_cmprv (rv, ==, CKR_OK); /* Change the password */ @@ -172,11 +183,12 @@ TESTING_TEST (login_auto_user_token) /* This should prompt again, as stored password is now wrong */ gku_prompt_dummy_queue_ok_password ("other"); - rv = (module->C_Login) (session, CKU_USER, NULL, 0); + rv = (test->module->C_Login) (test->session, CKU_USER, NULL, 0); gkm_assert_cmprv (rv, ==, CKR_OK); } -TESTING_TEST (login_auto_unlock_keyring) +static void +test_unlock_keyring (Test *test, gconstpointer unused) { CK_OBJECT_HANDLE credential; CK_RV rv; @@ -185,19 +197,19 @@ TESTING_TEST (login_auto_unlock_keyring) CK_ATTRIBUTE attrs[] = { { CKA_CLASS, &klass, sizeof (klass) }, { CKA_VALUE, NULL, 0 }, - { CKA_G_OBJECT, &collection, sizeof (collection) }, + { CKA_G_OBJECT, &test->collection, sizeof (test->collection) }, }; /* Create credential with prompt */ gku_prompt_dummy_queue_auto_password ("booo"); - rv = (module->C_CreateObject) (session, attrs, G_N_ELEMENTS (attrs), &credential); + rv = (test->module->C_CreateObject) (test->session, attrs, G_N_ELEMENTS (attrs), &credential); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = (module->C_DestroyObject) (session, credential); + rv = (test->module->C_DestroyObject) (test->session, credential); gkm_assert_cmprv (rv, ==, CKR_OK); /* No further prompting should be shown, uses stored password */ gku_prompt_dummy_prepare_response (); - rv = (module->C_CreateObject) (session, attrs, G_N_ELEMENTS (attrs), &credential); + rv = (test->module->C_CreateObject) (test->session, attrs, G_N_ELEMENTS (attrs), &credential); gkm_assert_cmprv (rv, ==, CKR_OK); /* Change the password */ @@ -205,6 +217,19 @@ TESTING_TEST (login_auto_unlock_keyring) /* This should prompt again, as stored password is now wrong */ gku_prompt_dummy_queue_ok_password ("other"); - rv = (module->C_CreateObject) (session, attrs, G_N_ELEMENTS (attrs), &credential); + rv = (test->module->C_CreateObject) (test->session, attrs, G_N_ELEMENTS (attrs), &credential); gkm_assert_cmprv (rv, ==, CKR_OK); } + +int +main (int argc, char **argv) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + + g_test_add ("/wrap-layer/login-auto/specific", Test, NULL, setup, test_specific, teardown); + g_test_add ("/wrap-layer/login-auto/user_token", Test, NULL, setup, test_user_token, teardown); + g_test_add ("/wrap-layer/login-auto/unlock_keyring", Test, NULL, setup, test_unlock_keyring, teardown); + + return egg_tests_run_in_thread_with_loop (); +} diff --git a/pkcs11/wrap-layer/tests/test-login-hints.c b/pkcs11/wrap-layer/tests/test-login-hints.c index eb8d1359..48bb8309 100644 --- a/pkcs11/wrap-layer/tests/test-login-hints.c +++ b/pkcs11/wrap-layer/tests/test-login-hints.c @@ -21,14 +21,16 @@ #include "config.h" -#include "test-suite.h" - #include "egg/egg-secure-memory.h" +#include "egg/egg-testing.h" #include "wrap-layer/gkm-wrap-layer.h" #include "wrap-layer/gkm-wrap-login.h" -TESTING_TEST (login_did_unlock_fail) +#include <glib-object.h> + +static void +test_did_unlock_fail (void) { gchar *password; gboolean ret; @@ -49,3 +51,14 @@ TESTING_TEST (login_did_unlock_fail) ret = gkm_wrap_login_did_unlock_fail (); g_assert (ret == FALSE); } + +int +main (int argc, char **argv) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + + g_test_add_func ("/wrap-layer/login-hints/did_unlock_fail", test_did_unlock_fail); + + return egg_tests_run_in_thread_with_loop (); +} diff --git a/pkcs11/wrap-layer/tests/test-login-keyring.c b/pkcs11/wrap-layer/tests/test-login-keyring.c index ad430018..3c8fe5b6 100644 --- a/pkcs11/wrap-layer/tests/test-login-keyring.c +++ b/pkcs11/wrap-layer/tests/test-login-keyring.c @@ -21,9 +21,8 @@ #include "config.h" -#include "test-suite.h" - #include "egg/egg-secure-memory.h" +#include "egg/egg-testing.h" #include "gkm/gkm-attributes.h" #include "gkm/gkm-mock.h" @@ -32,35 +31,43 @@ #include "wrap-layer/gkm-wrap-layer.h" #include "wrap-layer/gkm-wrap-login.h" +#include <glib-object.h> + extern CK_FUNCTION_LIST mock_secret_store; -static CK_FUNCTION_LIST functions; -static CK_FUNCTION_LIST_PTR module = NULL; -TESTING_SETUP (login_keyring) +typedef struct { + CK_FUNCTION_LIST functions; + CK_FUNCTION_LIST_PTR module; +} Test; + +static void +setup (Test *test, gconstpointer unused) { CK_RV rv; - /* Always start off with test functions */ - memcpy (&functions, &mock_secret_store, sizeof (functions)); + /* Always start off with test test->functions */ + memcpy (&test->functions, &mock_secret_store, sizeof (test->functions)); gkm_wrap_layer_reset_modules (); - gkm_wrap_layer_add_module (&functions); - module = gkm_wrap_layer_get_functions (); + gkm_wrap_layer_add_module (&test->functions); + test->module = gkm_wrap_layer_get_functions (); /* Initialize */ - rv = (module->C_Initialize) (NULL); + rv = (test->module->C_Initialize) (NULL); gkm_assert_cmprv (rv, ==, CKR_OK); } -TESTING_TEARDOWN (login_keyring) +static void +teardown (Test *test, gconstpointer unused) { CK_RV rv; - rv = (module->C_Finalize) (NULL); + rv = (test->module->C_Finalize) (NULL); gkm_assert_cmprv (rv, ==, CKR_OK); - module = NULL; + test->module = NULL; } -TESTING_TEST (login_is_usable) +static void +test_is_usable (Test *test, gconstpointer unused) { gboolean ret; @@ -68,16 +75,18 @@ TESTING_TEST (login_is_usable) g_assert (ret == TRUE); } -TESTING_TEST (login_usable_fail_open_session) +static void +test_usable_fail_open_session (Test *test, gconstpointer unused) { gboolean ret; - functions.C_OpenSession = gkm_mock_fail_C_OpenSession; + test->functions.C_OpenSession = gkm_mock_fail_C_OpenSession; ret = gkm_wrap_login_is_usable (); g_assert (ret == FALSE); } -TESTING_TEST (login_usable_fail_not_trusted) +static void +test_usable_fail_not_trusted (Test *test, gconstpointer unused) { CK_OBJECT_HANDLE object; CK_ATTRIBUTE attr; @@ -107,7 +116,8 @@ TESTING_TEST (login_usable_fail_not_trusted) g_assert (ret == FALSE); } -TESTING_TEST (login_usable_fail_locked) +static void +test_usable_fail_locked (Test *test, gconstpointer unused) { CK_OBJECT_HANDLE object; CK_ATTRIBUTE attr; @@ -137,7 +147,8 @@ TESTING_TEST (login_usable_fail_locked) g_assert (ret == FALSE); } -TESTING_TEST (login_lookup_secret_no_match) +static void +test_lookup_secret_no_match (Test *test, gconstpointer unused) { gchar *password; @@ -146,7 +157,8 @@ TESTING_TEST (login_lookup_secret_no_match) g_assert_cmpstr (password, ==, NULL); } -TESTING_TEST (login_lookup_secret_and_match) +static void +test_lookup_secret_and_match (Test *test, gconstpointer unused) { gchar *password; @@ -158,7 +170,8 @@ TESTING_TEST (login_lookup_secret_and_match) egg_secure_free (password); } -TESTING_TEST (login_lookup_store_secret) +static void +test_lookup_store_secret (Test *test, gconstpointer unused) { CK_OBJECT_CLASS klass = CKO_SECRET_KEY; CK_BBOOL tval = CK_TRUE; @@ -182,7 +195,8 @@ TESTING_TEST (login_lookup_store_secret) gkm_assert_cmpulong (object, !=, 0); } -TESTING_TEST (login_lookup_store_secret_overwrite) +static void +test_lookup_store_secret_overwrite (Test *test, gconstpointer unused) { CK_OBJECT_CLASS klass = CKO_SECRET_KEY; CK_BBOOL tval = CK_TRUE; @@ -220,7 +234,8 @@ TESTING_TEST (login_lookup_store_secret_overwrite) gkm_assert_cmpulong (object1, ==, object2); } -TESTING_TEST (login_lookup_store_null_secret) +static void +test_lookup_store_null_secret (Test *test, gconstpointer unused) { CK_OBJECT_CLASS klass = CKO_SECRET_KEY; CK_BBOOL tval = CK_TRUE; @@ -243,7 +258,8 @@ TESTING_TEST (login_lookup_store_null_secret) gkm_assert_cmpulong (object, !=, 0); } -TESTING_TEST (login_lookup_store_no_attributes_not_stored) +static void +test_lookup_store_no_attributes_not_stored (Test *test, gconstpointer unused) { CK_OBJECT_CLASS klass = CKO_SECRET_KEY; CK_BBOOL tval = CK_TRUE; @@ -265,7 +281,8 @@ TESTING_TEST (login_lookup_store_no_attributes_not_stored) } -TESTING_TEST (login_lookup_remove_present) +static void +test_lookup_remove_present (Test *test, gconstpointer unused) { CK_OBJECT_CLASS klass = CKO_SECRET_KEY; CK_BBOOL tval = CK_TRUE; @@ -291,7 +308,8 @@ TESTING_TEST (login_lookup_remove_present) gkm_assert_cmpulong (object, ==, 0); } -TESTING_TEST (login_lookup_remove_no_attributes) +static void +test_lookup_remove_no_attributes (Test *test, gconstpointer unused) { guint n_objects, check; @@ -304,3 +322,25 @@ TESTING_TEST (login_lookup_remove_no_attributes) check = gkm_mock_module_count_objects (0); g_assert_cmpuint (check, ==, n_objects); } + +int +main (int argc, char **argv) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + + g_test_add ("/wrap-layer/login-keyring/is_usable", Test, NULL, setup, test_is_usable, teardown); + g_test_add ("/wrap-layer/login-keyring/usable_fail_open_session", Test, NULL, setup, test_usable_fail_open_session, teardown); + g_test_add ("/wrap-layer/login-keyring/usable_fail_not_trusted", Test, NULL, setup, test_usable_fail_not_trusted, teardown); + g_test_add ("/wrap-layer/login-keyring/usable_fail_locked", Test, NULL, setup, test_usable_fail_locked, teardown); + g_test_add ("/wrap-layer/login-keyring/lookup_secret_no_match", Test, NULL, setup, test_lookup_secret_no_match, teardown); + g_test_add ("/wrap-layer/login-keyring/lookup_secret_and_match", Test, NULL, setup, test_lookup_secret_and_match, teardown); + g_test_add ("/wrap-layer/login-keyring/lookup_store_secret", Test, NULL, setup, test_lookup_store_secret, teardown); + g_test_add ("/wrap-layer/login-keyring/lookup_store_secret_overwrite", Test, NULL, setup, test_lookup_store_secret_overwrite, teardown); + g_test_add ("/wrap-layer/login-keyring/lookup_store_null_secret", Test, NULL, setup, test_lookup_store_null_secret, teardown); + g_test_add ("/wrap-layer/login-keyring/lookup_store_no_attributes_not_stored", Test, NULL, setup, test_lookup_store_no_attributes_not_stored, teardown); + g_test_add ("/wrap-layer/login-keyring/lookup_remove_present", Test, NULL, setup, test_lookup_remove_present, teardown); + g_test_add ("/wrap-layer/login-keyring/lookup_remove_no_attributes", Test, NULL, setup, test_lookup_remove_no_attributes, teardown); + + return egg_tests_run_in_thread_with_loop (); +} diff --git a/pkcs11/wrap-layer/tests/test-login-specific.c b/pkcs11/wrap-layer/tests/test-login-specific.c index 3957404c..a91480e5 100644 --- a/pkcs11/wrap-layer/tests/test-login-specific.c +++ b/pkcs11/wrap-layer/tests/test-login-specific.c @@ -21,7 +21,7 @@ #include "config.h" -#include "test-suite.h" +#include "egg/egg-testing.h" #include "gkm/gkm-mock.h" #include "gkm/gkm-test.h" @@ -30,11 +30,14 @@ #include "ui/gku-prompt.h" -static CK_FUNCTION_LIST prompt_login_functions; -static CK_FUNCTION_LIST_PTR module = NULL; -static CK_SESSION_HANDLE session = 0; +typedef struct { + CK_FUNCTION_LIST prompt_login_functions; + CK_FUNCTION_LIST_PTR module; + CK_SESSION_HANDLE session; +} Test; -TESTING_SETUP (login_specific) +static void +setup (Test *test, gconstpointer unused) { CK_FUNCTION_LIST_PTR funcs; CK_OBJECT_HANDLE key; @@ -53,83 +56,100 @@ TESTING_SETUP (login_specific) /* Always start off with test functions */ rv = gkm_mock_C_GetFunctionList (&funcs); gkm_assert_cmprv (rv, ==, CKR_OK); - memcpy (&prompt_login_functions, funcs, sizeof (prompt_login_functions)); + memcpy (&test->prompt_login_functions, funcs, sizeof (test->prompt_login_functions)); gkm_wrap_layer_reset_modules (); - gkm_wrap_layer_add_module (&prompt_login_functions); - module = gkm_wrap_layer_get_functions (); + gkm_wrap_layer_add_module (&test->prompt_login_functions); + test->module = gkm_wrap_layer_get_functions (); gku_prompt_dummy_prepare_response (); - /* Open a session */ - rv = (module->C_Initialize) (NULL); + /* Open a test->session */ + rv = (test->module->C_Initialize) (NULL); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = (module->C_GetSlotList) (CK_TRUE, &slot_id, &n_slots); + rv = (test->module->C_GetSlotList) (CK_TRUE, &slot_id, &n_slots); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = (module->C_OpenSession) (slot_id, CKF_SERIAL_SESSION, NULL, NULL, &session); + rv = (test->module->C_OpenSession) (slot_id, CKF_SERIAL_SESSION, NULL, NULL, &test->session); gkm_assert_cmprv (rv, ==, CKR_OK); /* Find the always authenticate object */ - rv = (module->C_FindObjectsInit) (session, attrs, 1); + rv = (test->module->C_FindObjectsInit) (test->session, attrs, 1); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = (module->C_FindObjects) (session, &key, 1, &count); + rv = (test->module->C_FindObjects) (test->session, &key, 1, &count); gkm_assert_cmprv (rv, ==, CKR_OK); gkm_assert_cmpulong (count, ==, 1); gkm_assert_cmpulong (key, !=, 0); - rv = (module->C_FindObjectsFinal) (session); + rv = (test->module->C_FindObjectsFinal) (test->session); gkm_assert_cmprv (rv, ==, CKR_OK); /* Start a signing operation, that needs to be authenticated */ - rv = (module->C_SignInit) (session, &mech, key); + rv = (test->module->C_SignInit) (test->session, &mech, key); gkm_assert_cmprv (rv, ==, CKR_OK); } -TESTING_TEARDOWN (login_specific) +static void +teardown (Test *test, gconstpointer unused) { CK_RV rv; g_assert (!gku_prompt_dummy_have_response ()); - rv = (module->C_CloseSession) (session); + rv = (test->module->C_CloseSession) (test->session); gkm_assert_cmprv (rv, ==, CKR_OK); - session = 0; + test->session = 0; - rv = (module->C_Finalize) (NULL); + rv = (test->module->C_Finalize) (NULL); gkm_assert_cmprv (rv, ==, CKR_OK); - module = NULL; + test->module = NULL; } -TESTING_TEST (login_specific_ok_password) +static void +test_ok_password (Test *test, gconstpointer unused) { CK_RV rv; gku_prompt_dummy_queue_ok_password ("booo"); - rv = (module->C_Login) (session, CKU_CONTEXT_SPECIFIC, NULL, 0); + rv = (test->module->C_Login) (test->session, CKU_CONTEXT_SPECIFIC, NULL, 0); gkm_assert_cmprv (rv, ==, CKR_OK); } -TESTING_TEST (login_specific_bad_password_then_cancel) +static void +test_bad_password_then_cancel (Test *test, gconstpointer unused) { CK_RV rv; gku_prompt_dummy_queue_ok_password ("bad password"); gku_prompt_dummy_queue_no (); - rv = (module->C_Login) (session, CKU_CONTEXT_SPECIFIC, NULL, 0); + rv = (test->module->C_Login) (test->session, CKU_CONTEXT_SPECIFIC, NULL, 0); gkm_assert_cmprv (rv, ==, CKR_PIN_INCORRECT); } -TESTING_TEST (login_specific_cancel_immediately) +static void +test_cancel_immediately (Test *test, gconstpointer unused) { CK_RV rv; gku_prompt_dummy_queue_no (); - rv = (module->C_Login) (session, CKU_CONTEXT_SPECIFIC, NULL, 0); + rv = (test->module->C_Login) (test->session, CKU_CONTEXT_SPECIFIC, NULL, 0); gkm_assert_cmprv (rv, ==, CKR_PIN_INCORRECT); } + +int +main (int argc, char **argv) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + + g_test_add ("/wrap-layer/login-specific/ok_password", Test, NULL, setup, test_ok_password, teardown); + g_test_add ("/wrap-layer/login-specific/bad_password_then_cancel", Test, NULL, setup, test_bad_password_then_cancel, teardown); + g_test_add ("/wrap-layer/login-specific/cancel_immediately", Test, NULL, setup, test_cancel_immediately, teardown); + + return egg_tests_run_in_thread_with_loop (); +} diff --git a/pkcs11/wrap-layer/tests/test-login-user.c b/pkcs11/wrap-layer/tests/test-login-user.c index d2d47c4f..61bd34cc 100644 --- a/pkcs11/wrap-layer/tests/test-login-user.c +++ b/pkcs11/wrap-layer/tests/test-login-user.c @@ -21,7 +21,7 @@ #include "config.h" -#include "test-suite.h" +#include "egg/egg-testing.h" #include "gkm/gkm-mock.h" #include "gkm/gkm-test.h" @@ -30,11 +30,14 @@ #include "ui/gku-prompt.h" -static CK_FUNCTION_LIST prompt_login_functions; -static CK_FUNCTION_LIST_PTR module = NULL; -static CK_SESSION_HANDLE session = 0; +typedef struct { + CK_FUNCTION_LIST prompt_login_functions; + CK_FUNCTION_LIST_PTR module; + CK_SESSION_HANDLE session; +} Test; -TESTING_SETUP (login_user) +static void +setup (Test *test, gconstpointer unused) { CK_FUNCTION_LIST_PTR funcs; CK_SLOT_ID slot_id; @@ -44,101 +47,126 @@ TESTING_SETUP (login_user) /* Always start off with test functions */ rv = gkm_mock_C_GetFunctionList (&funcs); gkm_assert_cmprv (rv, ==, CKR_OK); - memcpy (&prompt_login_functions, funcs, sizeof (prompt_login_functions)); + memcpy (&test->prompt_login_functions, funcs, sizeof (test->prompt_login_functions)); gkm_wrap_layer_reset_modules (); - gkm_wrap_layer_add_module (&prompt_login_functions); - module = gkm_wrap_layer_get_functions (); + gkm_wrap_layer_add_module (&test->prompt_login_functions); + test->module = gkm_wrap_layer_get_functions (); gku_prompt_dummy_prepare_response (); - /* Open a session */ - rv = (module->C_Initialize) (NULL); + /* Open a test->session */ + rv = (test->module->C_Initialize) (NULL); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = (module->C_GetSlotList) (CK_TRUE, &slot_id, &n_slots); + rv = (test->module->C_GetSlotList) (CK_TRUE, &slot_id, &n_slots); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = (module->C_OpenSession) (slot_id, CKF_SERIAL_SESSION, NULL, NULL, &session); + rv = (test->module->C_OpenSession) (slot_id, CKF_SERIAL_SESSION, NULL, NULL, &test->session); gkm_assert_cmprv (rv, ==, CKR_OK); } -TESTING_TEARDOWN (login_user) +static void +teardown (Test *test, gconstpointer unused) { CK_RV rv; g_assert (!gku_prompt_dummy_have_response ()); - rv = (module->C_CloseSession) (session); + rv = (test->module->C_CloseSession) (test->session); gkm_assert_cmprv (rv, ==, CKR_OK); - session = 0; + test->session = 0; - rv = (module->C_Finalize) (NULL); + rv = (test->module->C_Finalize) (NULL); gkm_assert_cmprv (rv, ==, CKR_OK); - module = NULL; + test->module = NULL; } -TESTING_TEST (login_fail_unsupported_so) +static void +test_fail_unsupported_so (Test *test, gconstpointer unused) { CK_RV rv; - rv = (module->C_Login) (session, CKU_SO, NULL, 0); + rv = (test->module->C_Login) (test->session, CKU_SO, NULL, 0); gkm_assert_cmprv (rv, ==, CKR_PIN_INCORRECT); } -TESTING_TEST (login_skip_prompt_because_pin) +static void +test_skip_prompt_because_pin (Test *test, gconstpointer unused) { CK_RV rv; - rv = (module->C_Login) (session, CKU_USER, (guchar*)"booo", 4); + rv = (test->module->C_Login) (test->session, CKU_USER, (guchar*)"booo", 4); gkm_assert_cmprv (rv, ==, CKR_OK); } -TESTING_TEST (login_user_ok_password) +static void +test_ok_password (Test *test, gconstpointer unused) { CK_RV rv; gku_prompt_dummy_queue_ok_password ("booo"); - rv = (module->C_Login) (session, CKU_USER, NULL, 0); + rv = (test->module->C_Login) (test->session, CKU_USER, NULL, 0); gkm_assert_cmprv (rv, ==, CKR_OK); } -TESTING_TEST (login_user_bad_password_then_cancel) +static void +test_bad_password_then_cancel (Test *test, gconstpointer unused) { CK_RV rv; gku_prompt_dummy_queue_ok_password ("bad password"); gku_prompt_dummy_queue_no (); - rv = (module->C_Login) (session, CKU_USER, NULL, 0); + rv = (test->module->C_Login) (test->session, CKU_USER, NULL, 0); gkm_assert_cmprv (rv, ==, CKR_PIN_INCORRECT); } -TESTING_TEST (login_user_cancel_immediately) +static void +test_cancel_immediately (Test *test, gconstpointer unused) { CK_RV rv; gku_prompt_dummy_queue_no (); - rv = (module->C_Login) (session, CKU_USER, NULL, 0); + rv = (test->module->C_Login) (test->session, CKU_USER, NULL, 0); gkm_assert_cmprv (rv, ==, CKR_PIN_INCORRECT); } -TESTING_TEST (login_user_fail_get_session_info) +static void +test_fail_get_session_info (Test *test, gconstpointer unused) { CK_RV rv; - prompt_login_functions.C_GetSessionInfo = gkm_mock_fail_C_GetSessionInfo; - rv = (module->C_Login) (session, CKU_USER, NULL, 0); + test->prompt_login_functions.C_GetSessionInfo = gkm_mock_fail_C_GetSessionInfo; + rv = (test->module->C_Login) (test->session, CKU_USER, NULL, 0); gkm_assert_cmprv (rv, ==, CKR_PIN_INCORRECT); } -TESTING_TEST (login_user_fail_get_token_info) +static void +test_fail_get_token_info (Test *test, gconstpointer unused) { CK_RV rv; - prompt_login_functions.C_GetTokenInfo = gkm_mock_fail_C_GetTokenInfo; - rv = (module->C_Login) (session, CKU_USER, NULL, 0); + test->prompt_login_functions.C_GetTokenInfo = gkm_mock_fail_C_GetTokenInfo; + rv = (test->module->C_Login) (test->session, CKU_USER, NULL, 0); gkm_assert_cmprv (rv, ==, CKR_PIN_INCORRECT); } + +int +main (int argc, char **argv) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + + g_test_add ("/wrap-layer/login-user/fail_unsupported_so", Test, NULL, setup, test_fail_unsupported_so, teardown); + g_test_add ("/wrap-layer/login-user/skip_prompt_because_pin", Test, NULL, setup, test_skip_prompt_because_pin, teardown); + g_test_add ("/wrap-layer/login-user/ok_password", Test, NULL, setup, test_ok_password, teardown); + g_test_add ("/wrap-layer/login-user/bad_password_then_cancel", Test, NULL, setup, test_bad_password_then_cancel, teardown); + g_test_add ("/wrap-layer/login-user/cancel_immediately", Test, NULL, setup, test_cancel_immediately, teardown); + g_test_add ("/wrap-layer/login-user/fail_get_session_info", Test, NULL, setup, test_fail_get_session_info, teardown); + g_test_add ("/wrap-layer/login-user/fail_get_token_info", Test, NULL, setup, test_fail_get_token_info, teardown); + + return egg_tests_run_in_thread_with_loop (); +} diff --git a/pkcs11/wrap-layer/tests/test-set-pin.c b/pkcs11/wrap-layer/tests/test-set-pin.c index 766f8b89..164bde1c 100644 --- a/pkcs11/wrap-layer/tests/test-set-pin.c +++ b/pkcs11/wrap-layer/tests/test-set-pin.c @@ -21,7 +21,7 @@ #include "config.h" -#include "test-suite.h" +#include "egg/egg-testing.h" #include "gkm/gkm-mock.h" #include "gkm/gkm-test.h" @@ -30,63 +30,79 @@ #include "ui/gku-prompt.h" -static CK_FUNCTION_LIST functions; -static CK_FUNCTION_LIST_PTR module = NULL; -static CK_SESSION_HANDLE session = 0; +typedef struct { + CK_FUNCTION_LIST functions; + CK_FUNCTION_LIST_PTR module; + CK_SESSION_HANDLE session; +} Test; -TESTING_SETUP (set_pin) +static void +setup (Test *test, gconstpointer unused) { CK_FUNCTION_LIST_PTR funcs; CK_SLOT_ID slot_id; CK_ULONG n_slots = 1; CK_RV rv; - /* Always start off with test functions */ + /* Always start off with test test->functions */ rv = gkm_mock_C_GetFunctionList (&funcs); gkm_assert_cmprv (rv, ==, CKR_OK); - memcpy (&functions, funcs, sizeof (functions)); + memcpy (&test->functions, funcs, sizeof (test->functions)); gkm_wrap_layer_reset_modules (); - gkm_wrap_layer_add_module (&functions); - module = gkm_wrap_layer_get_functions (); + gkm_wrap_layer_add_module (&test->functions); + test->module = gkm_wrap_layer_get_functions (); gku_prompt_dummy_prepare_response (); - /* Open a session */ - rv = (module->C_Initialize) (NULL); + /* Open a test->session */ + rv = (test->module->C_Initialize) (NULL); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = (module->C_GetSlotList) (CK_TRUE, &slot_id, &n_slots); + rv = (test->module->C_GetSlotList) (CK_TRUE, &slot_id, &n_slots); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = (module->C_OpenSession) (slot_id, CKF_SERIAL_SESSION, NULL, NULL, &session); + rv = (test->module->C_OpenSession) (slot_id, CKF_SERIAL_SESSION, NULL, NULL, &test->session); gkm_assert_cmprv (rv, ==, CKR_OK); } -TESTING_TEARDOWN (set_pin) +static void +teardown (Test *test, gconstpointer unused) { CK_RV rv; g_assert (!gku_prompt_dummy_have_response ()); - rv = (module->C_CloseSession) (session); + rv = (test->module->C_CloseSession) (test->session); gkm_assert_cmprv (rv, ==, CKR_OK); - session = 0; + test->session = 0; - rv = (module->C_Finalize) (NULL); + rv = (test->module->C_Finalize) (NULL); gkm_assert_cmprv (rv, ==, CKR_OK); - module = NULL; + test->module = NULL; } -TESTING_TEST (set_pin_ok_passwords) +static void +test_ok_passwords (Test *test, gconstpointer unused) { CK_RV rv; gku_prompt_dummy_queue_ok_passwords ("booo", "new"); - rv = (module->C_SetPIN) (session, NULL, 0, NULL, 0); + rv = (test->module->C_SetPIN) (test->session, NULL, 0, NULL, 0); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = (module->C_Login) (session, CKU_USER, (guchar*)"new", 3); + rv = (test->module->C_Login) (test->session, CKU_USER, (guchar*)"new", 3); gkm_assert_cmprv (rv, ==, CKR_OK); } + +int +main (int argc, char **argv) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + + g_test_add ("/wrap-layer/set-pin/ok_passwords", Test, NULL, setup, test_ok_passwords, teardown); + + return egg_tests_run_in_thread_with_loop (); +} diff --git a/pkcs11/xdg-store/tests/Makefile.am b/pkcs11/xdg-store/tests/Makefile.am index 51ef8879..d009a630 100644 --- a/pkcs11/xdg-store/tests/Makefile.am +++ b/pkcs11/xdg-store/tests/Makefile.am @@ -1,30 +1,53 @@ -TESTING_SOURCES = \ - test-xdg-module.h - -# Test files should be listed in order they need to run -TESTING_FILES = \ - test-xdg-module.c \ - test-xdg-trust.c - -TESTING_LIBS = \ +INCLUDES = \ + -I$(top_builddir) \ + -I$(top_srcdir) \ + -I$(top_srcdir)/pkcs11 \ + -DSRCDIR="\"$(srcdir)\"" \ + $(GLIB_CFLAGS) \ + $(LIBGCRYPT_CFLAGS) + +LDADD = \ + libgkm-mock-xdg-module.a \ $(top_builddir)/pkcs11/xdg-store/libgkm-xdg-store.la \ $(top_builddir)/pkcs11/gkm/libgkm.la \ - $(top_builddir)/egg/libegg.la + $(top_builddir)/egg/libegg.la \ + $(GLIB_LIBS) \ + $(LIBGCRYPT_LIBS) + +if WITH_P11_TESTS +CHECK_PROGS = check-xdg-module +else +CHECK_PROGS = +endif + +TEST_PROGS = \ + test-xdg-module \ + test-xdg-trust -include $(top_srcdir)/testing/testing.make +check_PROGRAMS = $(TEST_PROGS) -EXTRA_DIST += \ +test: $(TEST_PROGS) $(CHECK_PROGS) + gtester -k --verbose -m $(TEST_MODE) --g-fatal-warnings $(TEST_PROGS) + @for prog in $(CHECK_PROGS); do SRCDIR='.' ./$$prog || exit 1; done + +check-local: test + +all-local: $(check_PROGRAMS) + +EXTRA_DIST = \ p11-tests.conf \ - test-data + files + +noinst_PROGRAMS = \ + frob-trust-file \ + dump-trust-file \ + $(CHECK_PROGS) -# ------------------------------------------------------------------------------ +check_xdg_module_CFLAGS = $(P11_TESTS_CFLAGS) +check_xdg_module_LDADD = $(P11_TESTS_LIBS) $(LDADD) -noinst_PROGRAMS += \ - diddle-trust-file \ - dump-trust-file +noinst_LIBRARIES = libgkm-mock-xdg-module.a -diddle_trust_file_LDADD = \ - $(top_builddir)/egg/libegg.la -dump_trust_file_LDADD = \ - $(top_builddir)/egg/libegg.la +libgkm_mock_xdg_module_a_SOURCES = \ + mock-xdg-module.c mock-xdg-module.h diff --git a/pkcs11/xdg-store/tests/check-xdg-module.c b/pkcs11/xdg-store/tests/check-xdg-module.c new file mode 100644 index 00000000..64ea3875 --- /dev/null +++ b/pkcs11/xdg-store/tests/check-xdg-module.c @@ -0,0 +1,68 @@ +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ +/* check-module.c: Check PKCS#11 implementation + + 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 "xdg-store/gkm-xdg-store.h" + +#include "egg/egg-secure-memory.h" + +#include <glib.h> +#include <glib-object.h> + +#include <p11-tests.h> + +static int failures = 0; + +EGG_SECURE_GLIB_DEFINITIONS (); + +static void +on_p11_tests_log (int level, const char *section, const char *message) +{ + if (level == P11_TESTS_NONE) { + g_message ("%s", message); + } else if (level != P11_TESTS_FAIL) { + g_message ("%s: %s", section, message); + } else { + g_print (" /xdg-store/%s: FAIL: %s\n", section, message); + ++failures; + } +} + +int +main (int argc, const char *argv[]) +{ + g_type_init (); + + p11_tests_set_log_func (on_p11_tests_log); + p11_tests_set_unexpected (1); + p11_tests_set_verbose (0); + p11_tests_set_write_session (1); + p11_tests_load_config (SRCDIR "/p11-tests.conf"); + + g_print ("CHECK: check-xdg-module...\n"); + p11_tests_perform (gkm_xdg_store_get_functions ()); + + g_print ("%s: check-xdg-module\n", failures ? "FAIL" : "PASS"); + return failures; +} diff --git a/pkcs11/xdg-store/tests/test-data/test-certificate-1.cer b/pkcs11/xdg-store/tests/files/test-certificate-1.cer Binary files differindex 1c084370..1c084370 100644 --- a/pkcs11/xdg-store/tests/test-data/test-certificate-1.cer +++ b/pkcs11/xdg-store/tests/files/test-certificate-1.cer diff --git a/pkcs11/xdg-store/tests/test-data/test-certificate-2.cer b/pkcs11/xdg-store/tests/files/test-certificate-2.cer Binary files differindex 1c084370..1c084370 100644 --- a/pkcs11/xdg-store/tests/test-data/test-certificate-2.cer +++ b/pkcs11/xdg-store/tests/files/test-certificate-2.cer diff --git a/pkcs11/xdg-store/tests/test-data/test-refer-1.trust b/pkcs11/xdg-store/tests/files/test-refer-1.trust Binary files differindex 8db12b91..8db12b91 100644 --- a/pkcs11/xdg-store/tests/test-data/test-refer-1.trust +++ b/pkcs11/xdg-store/tests/files/test-refer-1.trust diff --git a/pkcs11/xdg-store/tests/diddle-trust-file.c b/pkcs11/xdg-store/tests/frob-trust-file.c index a9c19796..a9c19796 100644 --- a/pkcs11/xdg-store/tests/diddle-trust-file.c +++ b/pkcs11/xdg-store/tests/frob-trust-file.c diff --git a/pkcs11/xdg-store/tests/mock-xdg-module.c b/pkcs11/xdg-store/tests/mock-xdg-module.c new file mode 100644 index 00000000..f8271c25 --- /dev/null +++ b/pkcs11/xdg-store/tests/mock-xdg-module.c @@ -0,0 +1,214 @@ +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ +/* test-xdg-module.c: A test PKCS#11 module implementation + + Copyright (C) 2010 Stefan Walter + Copyright (C) 2010 Collabora Ltd + + The Gnome Keyring Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The Gnome Keyring Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the Gnome Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. + + Author: Stef Walter <stef@memberwebs.com> +*/ + +#include "config.h" + +#include "mock-xdg-module.h" + +#include "xdg-store/gkm-xdg-store.h" + +#include "egg/egg-mkdtemp.h" +#include "egg/egg-secure-memory.h" + +#include "gkm/gkm-session.h" +#include "gkm/gkm-module.h" + +#include <glib/gstdio.h> + +#include <errno.h> +#include <sys/times.h> + +#include <string.h> + +EGG_SECURE_GLIB_DEFINITIONS (); + +static GMutex *mutex = NULL; +static gchar *directory = NULL; + +GkmModule* _gkm_xdg_store_get_module_for_testing (void); +GMutex* _gkm_module_get_scary_mutex_that_you_should_not_touch (GkmModule *module); + +static void +copy_file_to_directory (const gchar *from, const gchar *directory) +{ + gchar *filename; + gchar *basename; + gchar *data; + gsize n_data; + + if (!g_file_get_contents (from, &data, &n_data, NULL)) + g_error ("couldn't read: %s", from); + + basename = g_path_get_basename (from); + filename = g_build_filename (directory, basename, NULL); + if (!g_file_set_contents (filename, data, n_data, NULL)) + g_error ("couldn't write: %s", filename); + g_free (filename); + g_free (basename); + g_free (data); +} + +void +mock_xdg_module_remove_file (const gchar *name) +{ + gchar *filename; + gchar *basename; + + basename = g_path_get_basename (name); + filename = g_build_filename (directory, basename, NULL); + if (g_unlink (filename) < 0) + g_error ("couldn't remove: %s", filename); + g_free (filename); + g_free (basename); +} + + +void +mock_xdg_module_empty_file (const gchar *name) +{ + gchar *filename; + gchar *basename; + + basename = g_path_get_basename (name); + filename = g_build_filename (directory, basename, NULL); + if (!g_file_set_contents (filename, "", 0, NULL)) + g_error ("couldn't write: %s", filename); + g_free (filename); + g_free (basename); +} + +void +mock_xdg_module_touch_file (const gchar *name, gint future) +{ + gchar *basename; + gchar *filename; + struct timeval tv[2]; + + basename = g_path_get_basename (name); + filename = g_build_filename (directory, basename, NULL); + + /* Initialize the access and modification times */ + gettimeofday (tv, NULL); + tv[0].tv_sec += future; + memcpy (tv + 1, tv, sizeof (struct timeval)); + + if (utimes (filename, tv) < 0) + g_error ("couldn't update file time: %s: %s", filename, g_strerror (errno)); + + g_free (basename); + g_free (filename); +} + +GkmModule* +mock_xdg_module_initialize_and_enter (void) +{ + CK_FUNCTION_LIST_PTR funcs; + CK_C_INITIALIZE_ARGS args; + GkmModule *module; + gchar *string; + CK_RV rv; + + directory = egg_mkdtemp (g_strdup ("/tmp/mock-secret-XXXXXX")); + + /* Setup test directory to work in */ + memset (&args, 0, sizeof (args)); + string = g_strdup_printf ("directory='%s'", directory); + args.pReserved = string; + args.flags = CKF_OS_LOCKING_OK; + + /* Copy files from test-data to scratch */ + copy_file_to_directory (SRCDIR "/files/test-refer-1.trust", directory); + copy_file_to_directory (SRCDIR "/files/test-certificate-1.cer", directory); + mock_xdg_module_empty_file ("invalid-without-ext"); + mock_xdg_module_empty_file ("test-file.unknown"); + mock_xdg_module_empty_file ("test-invalid.trust"); + + funcs = gkm_xdg_store_get_functions (); + rv = (funcs->C_Initialize) (&args); + g_return_val_if_fail (rv == CKR_OK, NULL); + + module = _gkm_xdg_store_get_module_for_testing (); + g_return_val_if_fail (module, NULL); + + mutex = _gkm_module_get_scary_mutex_that_you_should_not_touch (module); + mock_xdg_module_enter (); + + g_free (string); + + return module; +} + +void +mock_xdg_module_leave_and_finalize (void) +{ + CK_FUNCTION_LIST_PTR funcs; + CK_RV rv; + + mock_xdg_module_leave (); + + funcs = gkm_xdg_store_get_functions (); + rv = (funcs->C_Finalize) (NULL); + g_return_if_fail (rv == CKR_OK); + + g_free (directory); + directory = NULL; +} + +void +mock_xdg_module_leave (void) +{ + g_assert (mutex); + g_mutex_unlock (mutex); +} + +void +mock_xdg_module_enter (void) +{ + g_assert (mutex); + g_mutex_lock (mutex); +} + +GkmSession* +mock_xdg_module_open_session (gboolean writable) +{ + CK_ULONG flags = CKF_SERIAL_SESSION; + CK_SESSION_HANDLE handle; + GkmModule *module; + GkmSession *session; + CK_RV rv; + + module = _gkm_xdg_store_get_module_for_testing (); + g_return_val_if_fail (module, NULL); + + if (writable) + flags |= CKF_RW_SESSION; + + rv = gkm_module_C_OpenSession (module, 1, flags, NULL, NULL, &handle); + g_assert (rv == CKR_OK); + + session = gkm_module_lookup_session (module, handle); + g_assert (session); + + return session; +} diff --git a/pkcs11/xdg-store/tests/test-xdg-module.h b/pkcs11/xdg-store/tests/mock-xdg-module.h index 8a6b78ba..d2a7b813 100644 --- a/pkcs11/xdg-store/tests/test-xdg-module.h +++ b/pkcs11/xdg-store/tests/mock-xdg-module.h @@ -21,8 +21,8 @@ Author: Stef Walter <stef@memberwebs.com> */ -#ifndef TEST_XDG_MODULE_H_ -#define TEST_XDG_MODULE_H_ +#ifndef MOCK_XDG_MODULE_H_ +#define MOCK_XDG_MODULE_H_ #include <glib.h> @@ -30,16 +30,22 @@ #include "gkm/gkm-types.h" #include "pkcs11.h" -#include "test-suite.h" -void test_xdg_module_leave (void); +void mock_xdg_module_leave (void); -void test_xdg_module_enter (void); +void mock_xdg_module_enter (void); -GkmModule* test_xdg_module_initialize_and_enter (void); +GkmModule* mock_xdg_module_initialize_and_enter (void); -void test_xdg_module_leave_and_finalize (void); +void mock_xdg_module_leave_and_finalize (void); -GkmSession* test_xdg_module_open_session (gboolean writable); +GkmSession* mock_xdg_module_open_session (gboolean writable); -#endif /* TEST_XDG_MODULE_H_ */ +void mock_xdg_module_empty_file (const gchar *name); + +void mock_xdg_module_touch_file (const gchar *name, + gint future); + +void mock_xdg_module_remove_file (const gchar *name); + +#endif /* MOCK_XDG_MODULE_H_ */ diff --git a/pkcs11/xdg-store/tests/test-xdg-module.c b/pkcs11/xdg-store/tests/test-xdg-module.c index d2541730..86eedf8a 100644 --- a/pkcs11/xdg-store/tests/test-xdg-module.c +++ b/pkcs11/xdg-store/tests/test-xdg-module.c @@ -1,5 +1,5 @@ /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* test-xdg-module.c: A test PKCS#11 module implementation +/* test-xdg-test->module.c: A test PKCS#11 test->module implementation Copyright (C) 2010 Stefan Walter Copyright (C) 2010 Collabora Ltd @@ -24,249 +24,154 @@ #include "config.h" -#include "test-xdg-module.h" -#include "gkm-xdg-store.h" +#include "mock-xdg-module.h" + +#include "xdg-store/gkm-xdg-store.h" #include "gkm/gkm-session.h" #include "gkm/gkm-module.h" +#include "egg/egg-testing.h" + #include <errno.h> #include <sys/times.h> #include <string.h> -static GMutex *mutex = NULL; - -GkmModule* _gkm_xdg_store_get_module_for_testing (void); -GMutex* _gkm_module_get_scary_mutex_that_you_should_not_touch (GkmModule *module); - -GkmModule* -test_xdg_module_initialize_and_enter (void) -{ - CK_FUNCTION_LIST_PTR funcs; - CK_C_INITIALIZE_ARGS args; - GkmModule *module; - gchar *string; - CK_RV rv; - - /* Setup test directory to work in */ - memset (&args, 0, sizeof (args)); - string = g_strdup_printf ("directory='%s'", testing_scratch_directory ()); - args.pReserved = string; - args.flags = CKF_OS_LOCKING_OK; - - /* Delete all files in this directory */ - testing_scratch_remove_all (); - - /* Copy files from test-data to scratch */ - testing_data_to_scratch ("test-refer-1.trust", NULL); - testing_data_to_scratch ("test-certificate-1.cer", NULL); - testing_scratch_empty ("invalid-without-ext"); - testing_scratch_empty ("test-file.unknown"); - testing_scratch_empty ("test-invalid.trust"); - - funcs = gkm_xdg_store_get_functions (); - rv = (funcs->C_Initialize) (&args); - g_return_val_if_fail (rv == CKR_OK, NULL); - - module = _gkm_xdg_store_get_module_for_testing (); - g_return_val_if_fail (module, NULL); - - mutex = _gkm_module_get_scary_mutex_that_you_should_not_touch (module); - test_xdg_module_enter (); - - g_free (string); - - return module; -} - -void -test_xdg_module_leave_and_finalize (void) -{ - CK_FUNCTION_LIST_PTR funcs; - CK_RV rv; - - test_xdg_module_leave (); - - funcs = gkm_xdg_store_get_functions (); - rv = (funcs->C_Finalize) (NULL); - g_return_if_fail (rv == CKR_OK); -} - -void -test_xdg_module_leave (void) -{ - g_assert (mutex); - g_mutex_unlock (mutex); -} - -void -test_xdg_module_enter (void) -{ - g_assert (mutex); - g_mutex_lock (mutex); -} - -GkmSession* -test_xdg_module_open_session (gboolean writable) -{ - CK_ULONG flags = CKF_SERIAL_SESSION; - CK_SESSION_HANDLE handle; +typedef struct { GkmModule *module; GkmSession *session; - CK_RV rv; - - module = _gkm_xdg_store_get_module_for_testing (); - g_return_val_if_fail (module, NULL); - - if (writable) - flags |= CKF_RW_SESSION; - - rv = gkm_module_C_OpenSession (module, 1, flags, NULL, NULL, &handle); - g_assert (rv == CKR_OK); - - session = gkm_module_lookup_session (module, handle); - g_assert (session); - - return session; -} - -/* -------------------------------------------------------------------------------------- - * MODULE TESTS - */ - -static GkmModule *module = NULL; -static GkmSession *session = NULL; -static CK_SLOT_ID slot_id = 0; + CK_SLOT_ID slot_id; +} Test; -TESTING_EXTERNAL(xdg_module) -{ - CK_FUNCTION_LIST_PTR funcs = gkm_xdg_store_get_functions (); - testing_test_p11_module (funcs, "p11-tests.conf"); -} - -TESTING_SETUP(xdg_module_setup) +static void +setup (Test *test, gconstpointer unused) { CK_SESSION_INFO info; CK_RV rv; - module = test_xdg_module_initialize_and_enter (); - session = test_xdg_module_open_session (TRUE); + test->module = mock_xdg_module_initialize_and_enter (); + test->session = mock_xdg_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); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = gkm_session_C_GetSessionInfo (session, &info); + rv = gkm_session_C_GetSessionInfo (test->session, &info); gkm_assert_cmprv (rv, ==, CKR_OK); - slot_id = info.slotID; + test->slot_id = info.slotID; } -TESTING_TEARDOWN(xdg_module_teardown) +static void +teardown (Test *test, gconstpointer unused) { - test_xdg_module_leave_and_finalize (); - module = NULL; - session = NULL; + mock_xdg_module_leave_and_finalize (); + test->module = NULL; + test->session = NULL; } -TESTING_TEST (xdg_module_find_twice_is_same) +static void +test_module_find_twice_is_same (Test *test, gconstpointer unused) { CK_OBJECT_HANDLE objects[256]; CK_ULONG n_objects; CK_ULONG n_check; CK_RV rv; - rv = gkm_session_C_FindObjectsInit (session, NULL, 0); + rv = gkm_session_C_FindObjectsInit (test->session, NULL, 0); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = gkm_session_C_FindObjects (session, objects, G_N_ELEMENTS (objects), &n_objects); + rv = gkm_session_C_FindObjects (test->session, objects, G_N_ELEMENTS (objects), &n_objects); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = gkm_session_C_FindObjectsFinal (session); + rv = gkm_session_C_FindObjectsFinal (test->session); gkm_assert_cmprv (rv, ==, CKR_OK); gkm_assert_cmpulong (n_objects, >, 0); /* Update the time on the file */ - testing_scratch_touch ("test-refer-1.trust", 1); + mock_xdg_module_touch_file ("test-refer-1.trust", 1); - rv = gkm_session_C_FindObjectsInit (session, NULL, 0); + rv = gkm_session_C_FindObjectsInit (test->session, NULL, 0); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = gkm_session_C_FindObjects (session, objects, G_N_ELEMENTS (objects), &n_check); + rv = gkm_session_C_FindObjects (test->session, objects, G_N_ELEMENTS (objects), &n_check); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = gkm_session_C_FindObjectsFinal (session); + rv = gkm_session_C_FindObjectsFinal (test->session); gkm_assert_cmprv (rv, ==, CKR_OK); /* Should have same objects after reload */ gkm_assert_cmpulong (n_check, ==, n_objects); } -TESTING_TEST (xdg_module_file_becomes_invalid) +static void +test_module_file_becomes_invalid (Test *test, gconstpointer unused) { CK_OBJECT_HANDLE objects[256]; CK_ULONG n_objects; CK_ULONG n_check; CK_RV rv; - rv = gkm_session_C_FindObjectsInit (session, NULL, 0); + rv = gkm_session_C_FindObjectsInit (test->session, NULL, 0); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = gkm_session_C_FindObjects (session, objects, G_N_ELEMENTS (objects), &n_objects); + rv = gkm_session_C_FindObjects (test->session, objects, G_N_ELEMENTS (objects), &n_objects); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = gkm_session_C_FindObjectsFinal (session); + rv = gkm_session_C_FindObjectsFinal (test->session); gkm_assert_cmprv (rv, ==, CKR_OK); gkm_assert_cmpulong (n_objects, >, 0); /* Overwrite the file with empty */ - testing_scratch_empty ("test-refer-1.trust"); - testing_scratch_touch ("test-refer-1.trust", 2); + mock_xdg_module_empty_file ("test-refer-1.trust"); + mock_xdg_module_touch_file ("test-refer-1.trust", 2); - rv = gkm_session_C_FindObjectsInit (session, NULL, 0); + rv = gkm_session_C_FindObjectsInit (test->session, NULL, 0); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = gkm_session_C_FindObjects (session, objects, G_N_ELEMENTS (objects), &n_check); + rv = gkm_session_C_FindObjects (test->session, objects, G_N_ELEMENTS (objects), &n_check); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = gkm_session_C_FindObjectsFinal (session); + rv = gkm_session_C_FindObjectsFinal (test->session); gkm_assert_cmprv (rv, ==, CKR_OK); /* Should have less objects */ gkm_assert_cmpulong (n_check, <, n_objects); } -TESTING_TEST (xdg_module_file_remove) +static void +test_module_file_remove (Test *test, gconstpointer unused) { CK_OBJECT_HANDLE objects[256]; CK_ULONG n_objects; CK_ULONG n_check; CK_RV rv; - rv = gkm_session_C_FindObjectsInit (session, NULL, 0); + rv = gkm_session_C_FindObjectsInit (test->session, NULL, 0); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = gkm_session_C_FindObjects (session, objects, G_N_ELEMENTS (objects), &n_objects); + rv = gkm_session_C_FindObjects (test->session, objects, G_N_ELEMENTS (objects), &n_objects); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = gkm_session_C_FindObjectsFinal (session); + rv = gkm_session_C_FindObjectsFinal (test->session); gkm_assert_cmprv (rv, ==, CKR_OK); gkm_assert_cmpulong (n_objects, >, 0); /* This file goes away */ - testing_scratch_remove ("test-refer-1.trust"); + mock_xdg_module_remove_file ("test-refer-1.trust"); - rv = gkm_session_C_FindObjectsInit (session, NULL, 0); + rv = gkm_session_C_FindObjectsInit (test->session, NULL, 0); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = gkm_session_C_FindObjects (session, objects, G_N_ELEMENTS (objects), &n_check); + rv = gkm_session_C_FindObjects (test->session, objects, G_N_ELEMENTS (objects), &n_check); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = gkm_session_C_FindObjectsFinal (session); + rv = gkm_session_C_FindObjectsFinal (test->session); gkm_assert_cmprv (rv, ==, CKR_OK); /* Should have less objects */ gkm_assert_cmpulong (n_check, <, n_objects); } -TESTING_TEST (xdg_create_and_add_object) +static void +test_create_and_add_object (Test *test, gconstpointer unused) { CK_OBJECT_HANDLE object = 0; CK_OBJECT_CLASS klass = CKO_CERTIFICATE; CK_CERTIFICATE_TYPE ctype = CKC_X_509; CK_BBOOL tval = CK_TRUE; - gpointer data; + gchar *data; gsize n_data; CK_RV rv; @@ -277,16 +182,19 @@ TESTING_TEST (xdg_create_and_add_object) { CKA_CERTIFICATE_TYPE, &ctype, sizeof (ctype) } }; - data = testing_data_read ("test-certificate-2.cer", &n_data); + if (!g_file_get_contents (SRCDIR "/files/test-certificate-2.cer", &data, &n_data, NULL)) + g_assert_not_reached (); + attrs[0].pValue = data; attrs[0].ulValueLen = n_data; - rv = gkm_session_C_CreateObject (session, attrs, G_N_ELEMENTS (attrs), &object); + rv = gkm_session_C_CreateObject (test->session, attrs, G_N_ELEMENTS (attrs), &object); gkm_assert_cmprv (rv, ==, CKR_OK); gkm_assert_cmpulong (object, !=, 0); } -TESTING_TEST (xdg_destroy_object) +static void +test_destroy_object (Test *test, gconstpointer unused) { CK_OBJECT_HANDLE object = 0; CK_CERTIFICATE_TYPE ctype = CKC_X_509; @@ -299,30 +207,31 @@ TESTING_TEST (xdg_destroy_object) { CKA_TOKEN, &tval, sizeof (tval) } }; - rv = gkm_session_C_FindObjectsInit (session, attrs, G_N_ELEMENTS (attrs)); + rv = gkm_session_C_FindObjectsInit (test->session, attrs, G_N_ELEMENTS (attrs)); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = gkm_session_C_FindObjects (session, &object, 1, &n_objects); + rv = gkm_session_C_FindObjects (test->session, &object, 1, &n_objects); gkm_assert_cmprv (rv, ==, CKR_OK); gkm_assert_cmpulong (n_objects, ==, 1); - rv = gkm_session_C_FindObjectsFinal (session); + rv = gkm_session_C_FindObjectsFinal (test->session); gkm_assert_cmprv (rv, ==, CKR_OK); /* Destroy this object, which should be stored on the disk */ - rv = gkm_session_C_DestroyObject (session, object); + rv = gkm_session_C_DestroyObject (test->session, object); gkm_assert_cmprv (rv, ==, CKR_OK); /* Make sure it's really gone */ - rv = gkm_session_C_DestroyObject (session, object); + rv = gkm_session_C_DestroyObject (test->session, object); gkm_assert_cmprv (rv, ==, CKR_OBJECT_HANDLE_INVALID); } -TESTING_TEST (xdg_get_slot_info) +static void +test_get_slot_info (Test *test, gconstpointer unused) { CK_SLOT_INFO info; const gchar *str; CK_RV rv; - rv = gkm_module_C_GetSlotInfo (module, slot_id, &info); + rv = gkm_module_C_GetSlotInfo (test->module, test->slot_id, &info); gkm_assert_cmprv (rv, ==, CKR_OK); str = g_strstr_len ((gchar*)info.slotDescription, sizeof (info.slotDescription), @@ -330,16 +239,46 @@ TESTING_TEST (xdg_get_slot_info) g_assert (str != NULL); } -TESTING_TEST (xdg_get_token_info) +static void +test_get_token_info (Test *test, gconstpointer unused) { CK_TOKEN_INFO info; const gchar *str; CK_RV rv; - rv = gkm_module_C_GetTokenInfo (module, slot_id, &info); + rv = gkm_module_C_GetTokenInfo (test->module, test->slot_id, &info); gkm_assert_cmprv (rv, ==, CKR_OK); str = g_strstr_len ((gchar*)info.label, sizeof (info.label), "User Key Storage"); g_assert (str != NULL); } + + +static void +null_log_handler (const gchar *log_domain, GLogLevelFlags log_level, + const gchar *message, gpointer user_data) +{ + +} + +int +main (int argc, char **argv) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + + /* Suppress these messages in tests */ + g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, + null_log_handler, NULL); + + g_test_add ("/xdg-store/module/module_find_twice_is_same", Test, NULL, setup, test_module_find_twice_is_same, teardown); + g_test_add ("/xdg-store/module/module_file_becomes_invalid", Test, NULL, setup, test_module_file_becomes_invalid, teardown); + g_test_add ("/xdg-store/module/module_file_remove", Test, NULL, setup, test_module_file_remove, teardown); + g_test_add ("/xdg-store/module/create_and_add_object", Test, NULL, setup, test_create_and_add_object, teardown); + g_test_add ("/xdg-store/module/destroy_object", Test, NULL, setup, test_destroy_object, teardown); + g_test_add ("/xdg-store/module/get_slot_info", Test, NULL, setup, test_get_slot_info, teardown); + g_test_add ("/xdg-store/module/get_token_info", Test, NULL, setup, test_get_token_info, teardown); + + return egg_tests_run_in_thread_with_loop (); +} diff --git a/pkcs11/xdg-store/tests/test-xdg-trust.c b/pkcs11/xdg-store/tests/test-xdg-trust.c index bb2d3293..7fb993b3 100644 --- a/pkcs11/xdg-store/tests/test-xdg-trust.c +++ b/pkcs11/xdg-store/tests/test-xdg-trust.c @@ -23,7 +23,9 @@ #include "config.h" -#include "test-xdg-module.h" +#include "mock-xdg-module.h" + +#include "egg/egg-testing.h" #include "gkm/gkm-module.h" #include "gkm/gkm-session.h" @@ -32,10 +34,12 @@ #include "pkcs11/pkcs11n.h" #include "pkcs11/pkcs11x.h" -static GkmModule *module = NULL; -static GkmSession *session = NULL; -static gpointer cert_data = NULL; -static gsize n_cert_data; +typedef struct { + GkmModule *module; + GkmSession *session; + gchar *cert_data; + gsize n_cert_data; +} Test; /* * C=ZA, ST=Western Cape, L=Cape Town, O=Thawte Consulting, OU=Certification Services Division, @@ -114,32 +118,35 @@ debug_print_certificate_info (const gchar *path) #endif -TESTING_SETUP (trust_setup) +static void +setup (Test *test, gconstpointer unused) { CK_RV rv; - module = test_xdg_module_initialize_and_enter (); - session = test_xdg_module_open_session (TRUE); + test->module = mock_xdg_module_initialize_and_enter (); + test->session = mock_xdg_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); - cert_data = testing_data_read ("test-certificate-2.cer", &n_cert_data); - g_assert (cert_data); + if (!g_file_get_contents (SRCDIR "/files/test-certificate-2.cer", &test->cert_data, &test->n_cert_data, NULL)) + g_assert_not_reached (); } -TESTING_TEARDOWN (trust_teardown) +static void +teardown (Test *test, gconstpointer unused) { - test_xdg_module_leave_and_finalize (); - module = NULL; - session = NULL; + mock_xdg_module_leave_and_finalize (); + test->module = NULL; + test->session = NULL; - g_free (cert_data); - cert_data = NULL; - n_cert_data = 0; + g_free (test->cert_data); + test->cert_data = NULL; + test->n_cert_data = 0; } -TESTING_TEST (trust_load_objects) +static void +test_load_objects (Test *test, gconstpointer unused) { CK_OBJECT_CLASS klass = CKO_NETSCAPE_TRUST; @@ -151,17 +158,18 @@ TESTING_TEST (trust_load_objects) CK_OBJECT_HANDLE objects[16]; CK_RV rv; - rv = gkm_session_C_FindObjectsInit (session, attrs, G_N_ELEMENTS (attrs)); + rv = gkm_session_C_FindObjectsInit (test->session, attrs, G_N_ELEMENTS (attrs)); g_assert (rv == CKR_OK); - rv = gkm_session_C_FindObjects (session, objects, G_N_ELEMENTS (objects), &n_objects); + rv = gkm_session_C_FindObjects (test->session, objects, G_N_ELEMENTS (objects), &n_objects); g_assert (rv == CKR_OK); - rv = gkm_session_C_FindObjectsFinal (session); + rv = gkm_session_C_FindObjectsFinal (test->session); g_assert (rv == CKR_OK); gkm_assert_cmpulong (n_objects, >=, 1); } -TESTING_TEST (trust_create_assertion_complete) +static void +test_create_assertion_complete (Test *test, gconstpointer unused) { CK_OBJECT_CLASS klass = CKO_X_TRUST_ASSERTION; CK_X_ASSERTION_TYPE atype = CKT_X_ANCHORED_CERTIFICATE; @@ -171,28 +179,29 @@ TESTING_TEST (trust_create_assertion_complete) CK_RV rv; CK_ATTRIBUTE attrs[] = { - { CKA_X_CERTIFICATE_VALUE, cert_data, n_cert_data }, + { CKA_X_CERTIFICATE_VALUE, test->cert_data, test->n_cert_data }, { CKA_CLASS, &klass, sizeof (klass) }, { CKA_X_ASSERTION_TYPE, &atype, sizeof (atype) }, { CKA_X_PURPOSE, "test-purpose", 12 }, }; - rv = gkm_session_C_CreateObject (session, attrs, G_N_ELEMENTS (attrs), &object); + rv = gkm_session_C_CreateObject (test->session, attrs, G_N_ELEMENTS (attrs), &object); gkm_assert_cmprv (rv, ==, CKR_OK); gkm_assert_cmpulong (object, !=, 0); - rv = gkm_session_C_FindObjectsInit (session, attrs, G_N_ELEMENTS (attrs)); + rv = gkm_session_C_FindObjectsInit (test->session, attrs, G_N_ELEMENTS (attrs)); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = gkm_session_C_FindObjects (session, &check, 1, &n_objects); + rv = gkm_session_C_FindObjects (test->session, &check, 1, &n_objects); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = gkm_session_C_FindObjectsFinal (session); + rv = gkm_session_C_FindObjectsFinal (test->session); gkm_assert_cmprv (rv, ==, CKR_OK); gkm_assert_cmpulong (n_objects, ==, 1); gkm_assert_cmpulong (check, ==, object); } -TESTING_TEST (trust_complete_assertion_has_no_serial_or_issuer) +static void +test_complete_assertion_has_no_serial_or_issuer (Test *test, gconstpointer unused) { CK_OBJECT_CLASS klass = CKO_X_TRUST_ASSERTION; CK_X_ASSERTION_TYPE atype = CKT_X_ANCHORED_CERTIFICATE; @@ -201,30 +210,31 @@ TESTING_TEST (trust_complete_assertion_has_no_serial_or_issuer) CK_RV rv; CK_ATTRIBUTE attrs[] = { - { CKA_X_CERTIFICATE_VALUE, cert_data, n_cert_data }, + { CKA_X_CERTIFICATE_VALUE, test->cert_data, test->n_cert_data }, { CKA_CLASS, &klass, sizeof (klass) }, { CKA_X_ASSERTION_TYPE, &atype, sizeof (atype) }, { CKA_X_PURPOSE, "test-purpose", 12 }, }; - rv = gkm_session_C_CreateObject (session, attrs, G_N_ELEMENTS (attrs), &object); + rv = gkm_session_C_CreateObject (test->session, attrs, G_N_ELEMENTS (attrs), &object); gkm_assert_cmprv (rv, ==, CKR_OK); gkm_assert_cmpulong (object, !=, 0); check.type = CKA_SERIAL_NUMBER; check.pValue = NULL; check.ulValueLen = 0; - rv = gkm_session_C_GetAttributeValue (session, object, &check, 1); + rv = gkm_session_C_GetAttributeValue (test->session, object, &check, 1); gkm_assert_cmprv (rv, ==, CKR_ATTRIBUTE_TYPE_INVALID); check.type = CKA_ISSUER; check.pValue = NULL; check.ulValueLen = 0; - rv = gkm_session_C_GetAttributeValue (session, object, &check, 1); + rv = gkm_session_C_GetAttributeValue (test->session, object, &check, 1); gkm_assert_cmprv (rv, ==, CKR_ATTRIBUTE_TYPE_INVALID); } -TESTING_TEST (trust_complete_assertion_netscape_md5_hash) +static void +test_complete_assertion_netscape_md5_hash (Test *test, gconstpointer unused) { CK_OBJECT_CLASS klass = CKO_X_TRUST_ASSERTION; CK_OBJECT_CLASS nklass = CKO_NETSCAPE_TRUST; @@ -235,7 +245,7 @@ TESTING_TEST (trust_complete_assertion_netscape_md5_hash) CK_RV rv; CK_ATTRIBUTE attrs[] = { - { CKA_X_CERTIFICATE_VALUE, cert_data, n_cert_data }, + { CKA_X_CERTIFICATE_VALUE, test->cert_data, test->n_cert_data }, { CKA_CLASS, &klass, sizeof (klass) }, { CKA_X_ASSERTION_TYPE, &atype, sizeof (atype) }, { CKA_X_PURPOSE, "test-purpose", 12 }, @@ -246,22 +256,23 @@ TESTING_TEST (trust_complete_assertion_netscape_md5_hash) { CKA_CLASS, &nklass, sizeof (nklass) }, }; - rv = gkm_session_C_CreateObject (session, attrs, G_N_ELEMENTS (attrs), &object); + rv = gkm_session_C_CreateObject (test->session, attrs, G_N_ELEMENTS (attrs), &object); gkm_assert_cmprv (rv, ==, CKR_OK); gkm_assert_cmpulong (object, !=, 0); - rv = gkm_session_C_FindObjectsInit (session, lookup, G_N_ELEMENTS (lookup)); + rv = gkm_session_C_FindObjectsInit (test->session, lookup, G_N_ELEMENTS (lookup)); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = gkm_session_C_FindObjects (session, &check, 1, &n_objects); + rv = gkm_session_C_FindObjects (test->session, &check, 1, &n_objects); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = gkm_session_C_FindObjectsFinal (session); + rv = gkm_session_C_FindObjectsFinal (test->session); gkm_assert_cmprv (rv, ==, CKR_OK); gkm_assert_cmpulong (check, !=, 0); gkm_assert_cmpulong (n_objects, >, 0); } -TESTING_TEST (trust_complete_assertion_netscape_sha1_hash) +static void +test_complete_assertion_netscape_sha1_hash (Test *test, gconstpointer unused) { CK_OBJECT_CLASS klass = CKO_X_TRUST_ASSERTION; CK_OBJECT_CLASS nklass = CKO_NETSCAPE_TRUST; @@ -272,7 +283,7 @@ TESTING_TEST (trust_complete_assertion_netscape_sha1_hash) CK_RV rv; CK_ATTRIBUTE attrs[] = { - { CKA_X_CERTIFICATE_VALUE, cert_data, n_cert_data }, + { CKA_X_CERTIFICATE_VALUE, test->cert_data, test->n_cert_data }, { CKA_CLASS, &klass, sizeof (klass) }, { CKA_X_ASSERTION_TYPE, &atype, sizeof (atype) }, { CKA_X_PURPOSE, "test-purpose", 12 }, @@ -283,22 +294,23 @@ TESTING_TEST (trust_complete_assertion_netscape_sha1_hash) { CKA_CLASS, &nklass, sizeof (nklass) }, }; - rv = gkm_session_C_CreateObject (session, attrs, G_N_ELEMENTS (attrs), &object); + rv = gkm_session_C_CreateObject (test->session, attrs, G_N_ELEMENTS (attrs), &object); gkm_assert_cmprv (rv, ==, CKR_OK); gkm_assert_cmpulong (object, !=, 0); - rv = gkm_session_C_FindObjectsInit (session, lookup, G_N_ELEMENTS (lookup)); + rv = gkm_session_C_FindObjectsInit (test->session, lookup, G_N_ELEMENTS (lookup)); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = gkm_session_C_FindObjects (session, &check, 1, &n_objects); + rv = gkm_session_C_FindObjects (test->session, &check, 1, &n_objects); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = gkm_session_C_FindObjectsFinal (session); + rv = gkm_session_C_FindObjectsFinal (test->session); gkm_assert_cmprv (rv, ==, CKR_OK); gkm_assert_cmpulong (check, !=, 0); gkm_assert_cmpulong (n_objects, >, 0); } -TESTING_TEST (trust_create_assertion_missing_type) +static void +test_create_assertion_missing_type (Test *test, gconstpointer unused) { CK_OBJECT_CLASS klass = CKO_X_TRUST_ASSERTION; CK_OBJECT_HANDLE object = 0; @@ -306,16 +318,17 @@ TESTING_TEST (trust_create_assertion_missing_type) /* Missing CKT_X_ANCHORED_CERTIFICATE */ CK_ATTRIBUTE attrs[] = { - { CKA_X_CERTIFICATE_VALUE, cert_data, n_cert_data }, + { CKA_X_CERTIFICATE_VALUE, test->cert_data, test->n_cert_data }, { CKA_CLASS, &klass, sizeof (klass) }, { CKA_X_PURPOSE, "test-purpose", 12 }, }; - rv = gkm_session_C_CreateObject (session, attrs, G_N_ELEMENTS (attrs), &object); + rv = gkm_session_C_CreateObject (test->session, attrs, G_N_ELEMENTS (attrs), &object); gkm_assert_cmprv (rv, ==, CKR_TEMPLATE_INCOMPLETE); } -TESTING_TEST (trust_create_assertion_bad_type) +static void +test_create_assertion_bad_type (Test *test, gconstpointer unused) { CK_OBJECT_CLASS klass = CKO_X_TRUST_ASSERTION; CK_X_ASSERTION_TYPE atype = 0xFFFF; @@ -329,11 +342,12 @@ TESTING_TEST (trust_create_assertion_bad_type) { CKA_X_PURPOSE, "test-purpose", 12 }, }; - rv = gkm_session_C_CreateObject (session, attrs, G_N_ELEMENTS (attrs), &object); + rv = gkm_session_C_CreateObject (test->session, attrs, G_N_ELEMENTS (attrs), &object); gkm_assert_cmprv (rv, ==, CKR_TEMPLATE_INCONSISTENT); } -TESTING_TEST (trust_create_assertion_missing_cert_value) +static void +test_create_assertion_missing_cert_value (Test *test, gconstpointer unused) { CK_OBJECT_CLASS klass = CKO_X_TRUST_ASSERTION; CK_X_ASSERTION_TYPE atype = CKT_X_ANCHORED_CERTIFICATE; @@ -347,11 +361,12 @@ TESTING_TEST (trust_create_assertion_missing_cert_value) { CKA_X_PURPOSE, "test-purpose", 12 }, }; - rv = gkm_session_C_CreateObject (session, attrs, G_N_ELEMENTS (attrs), &object); + rv = gkm_session_C_CreateObject (test->session, attrs, G_N_ELEMENTS (attrs), &object); gkm_assert_cmprv (rv, ==, CKR_TEMPLATE_INCOMPLETE); } -TESTING_TEST (trust_create_assertion_bad_cert_value) +static void +test_create_assertion_bad_cert_value (Test *test, gconstpointer unused) { CK_OBJECT_CLASS klass = CKO_X_TRUST_ASSERTION; CK_X_ASSERTION_TYPE atype = CKT_X_ANCHORED_CERTIFICATE; @@ -366,11 +381,12 @@ TESTING_TEST (trust_create_assertion_bad_cert_value) { CKA_X_PURPOSE, "test-purpose", 12 }, }; - rv = gkm_session_C_CreateObject (session, attrs, G_N_ELEMENTS (attrs), &object); + rv = gkm_session_C_CreateObject (test->session, attrs, G_N_ELEMENTS (attrs), &object); gkm_assert_cmprv (rv, ==, CKR_ATTRIBUTE_VALUE_INVALID); } -TESTING_TEST (trust_create_assertion_null_cert_value) +static void +test_create_assertion_null_cert_value (Test *test, gconstpointer unused) { CK_OBJECT_CLASS klass = CKO_X_TRUST_ASSERTION; CK_X_ASSERTION_TYPE atype = CKT_X_ANCHORED_CERTIFICATE; @@ -385,11 +401,12 @@ TESTING_TEST (trust_create_assertion_null_cert_value) { CKA_X_PURPOSE, "test-purpose", 12 }, }; - rv = gkm_session_C_CreateObject (session, attrs, G_N_ELEMENTS (attrs), &object); + rv = gkm_session_C_CreateObject (test->session, attrs, G_N_ELEMENTS (attrs), &object); gkm_assert_cmprv (rv, ==, CKR_ATTRIBUTE_VALUE_INVALID); } -TESTING_TEST (trust_create_assertion_for_distrusted) +static void +test_create_assertion_for_distrusted (Test *test, gconstpointer unused) { CK_OBJECT_CLASS klass = CKO_X_TRUST_ASSERTION; CK_X_ASSERTION_TYPE atype = CKT_X_DISTRUSTED_CERTIFICATE; @@ -406,22 +423,23 @@ TESTING_TEST (trust_create_assertion_for_distrusted) { CKA_ISSUER, (void*)DER_ISSUER, XL (DER_ISSUER) } }; - rv = gkm_session_C_CreateObject (session, attrs, G_N_ELEMENTS (attrs), &object); + rv = gkm_session_C_CreateObject (test->session, attrs, G_N_ELEMENTS (attrs), &object); gkm_assert_cmprv (rv, ==, CKR_OK); gkm_assert_cmpulong (object, !=, 0); - rv = gkm_session_C_FindObjectsInit (session, attrs, G_N_ELEMENTS (attrs)); + rv = gkm_session_C_FindObjectsInit (test->session, attrs, G_N_ELEMENTS (attrs)); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = gkm_session_C_FindObjects (session, &check, 1, &n_objects); + rv = gkm_session_C_FindObjects (test->session, &check, 1, &n_objects); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = gkm_session_C_FindObjectsFinal (session); + rv = gkm_session_C_FindObjectsFinal (test->session); gkm_assert_cmprv (rv, ==, CKR_OK); gkm_assert_cmpulong (n_objects, ==, 1); gkm_assert_cmpulong (check, ==, object); } -TESTING_TEST (trust_create_assertion_for_distrusted_no_purpose) +static void +test_create_assertion_for_distrusted_no_purpose (Test *test, gconstpointer unused) { CK_OBJECT_CLASS klass = CKO_X_TRUST_ASSERTION; CK_X_ASSERTION_TYPE atype = CKT_X_DISTRUSTED_CERTIFICATE; @@ -435,11 +453,12 @@ TESTING_TEST (trust_create_assertion_for_distrusted_no_purpose) { CKA_ISSUER, (void*)DER_ISSUER, XL (DER_ISSUER) } }; - rv = gkm_session_C_CreateObject (session, attrs, G_N_ELEMENTS (attrs), &object); + rv = gkm_session_C_CreateObject (test->session, attrs, G_N_ELEMENTS (attrs), &object); gkm_assert_cmprv (rv, ==, CKR_TEMPLATE_INCOMPLETE); } -TESTING_TEST (trust_create_assertion_for_distrusted_no_serial) +static void +test_create_assertion_for_distrusted_no_serial (Test *test, gconstpointer unused) { CK_OBJECT_CLASS klass = CKO_X_TRUST_ASSERTION; CK_X_ASSERTION_TYPE atype = CKT_X_DISTRUSTED_CERTIFICATE; @@ -453,11 +472,12 @@ TESTING_TEST (trust_create_assertion_for_distrusted_no_serial) { CKA_ISSUER, (void*)DER_ISSUER, XL (DER_ISSUER) } }; - rv = gkm_session_C_CreateObject (session, attrs, G_N_ELEMENTS (attrs), &object); + rv = gkm_session_C_CreateObject (test->session, attrs, G_N_ELEMENTS (attrs), &object); gkm_assert_cmprv (rv, ==, CKR_TEMPLATE_INCOMPLETE); } -TESTING_TEST (trust_create_assertion_twice) +static void +test_create_assertion_twice (Test *test, gconstpointer unused) { CK_OBJECT_CLASS klass = CKO_X_TRUST_ASSERTION; CK_X_ASSERTION_TYPE atype = CKT_X_DISTRUSTED_CERTIFICATE; @@ -475,22 +495,23 @@ TESTING_TEST (trust_create_assertion_twice) /* First object should go away when we create an overlapping assertion */ - rv = gkm_session_C_CreateObject (session, attrs, G_N_ELEMENTS (attrs), &object_1); + rv = gkm_session_C_CreateObject (test->session, attrs, G_N_ELEMENTS (attrs), &object_1); gkm_assert_cmprv (rv, ==, CKR_OK); gkm_assert_cmpulong (object_1, !=, 0); - rv = gkm_session_C_CreateObject (session, attrs, G_N_ELEMENTS (attrs), &object_2); + rv = gkm_session_C_CreateObject (test->session, attrs, G_N_ELEMENTS (attrs), &object_2); gkm_assert_cmprv (rv, ==, CKR_OK); gkm_assert_cmpulong (object_2, !=, 0); gkm_assert_cmpulong (object_1, !=, object_2); /* First object no longer exists */ - rv = gkm_session_C_DestroyObject (session, object_1); + rv = gkm_session_C_DestroyObject (test->session, object_1); gkm_assert_cmprv (rv, ==, CKR_OBJECT_HANDLE_INVALID); } -TESTING_TEST (trust_distrusted_assertion_has_no_cert_value) +static void +test_distrusted_assertion_has_no_cert_value (Test *test, gconstpointer unused) { CK_OBJECT_CLASS klass = CKO_X_TRUST_ASSERTION; CK_X_ASSERTION_TYPE atype = CKT_X_DISTRUSTED_CERTIFICATE; @@ -508,18 +529,19 @@ TESTING_TEST (trust_distrusted_assertion_has_no_cert_value) /* Created as distrusted, should have no CKA_X_CERTIFICATE_VALUE */ - rv = gkm_session_C_CreateObject (session, attrs, G_N_ELEMENTS (attrs), &object); + rv = gkm_session_C_CreateObject (test->session, attrs, G_N_ELEMENTS (attrs), &object); gkm_assert_cmprv (rv, ==, CKR_OK); gkm_assert_cmpulong (object, !=, 0); check.type = CKA_X_CERTIFICATE_VALUE; check.pValue = NULL; check.ulValueLen = 0; - rv = gkm_session_C_GetAttributeValue (session, object, &check, 1); + rv = gkm_session_C_GetAttributeValue (test->session, object, &check, 1); gkm_assert_cmprv (rv, ==, CKR_ATTRIBUTE_TYPE_INVALID); } -TESTING_TEST (trust_create_assertion_complete_on_token) +static void +test_create_assertion_complete_on_token (Test *test, gconstpointer unused) { CK_OBJECT_CLASS klass = CKO_X_TRUST_ASSERTION; CK_X_ASSERTION_TYPE atype = CKT_X_PINNED_CERTIFICATE; @@ -531,26 +553,26 @@ TESTING_TEST (trust_create_assertion_complete_on_token) CK_RV rv; CK_ATTRIBUTE attrs[] = { - { CKA_X_CERTIFICATE_VALUE, cert_data, n_cert_data }, + { CKA_X_CERTIFICATE_VALUE, test->cert_data, test->n_cert_data }, { CKA_CLASS, &klass, sizeof (klass) }, { CKA_X_ASSERTION_TYPE, &atype, sizeof (atype) }, { CKA_X_PURPOSE, "other", 5 }, { CKA_TOKEN, &token, sizeof (token) }, }; - rv = gkm_session_C_CreateObject (session, attrs, G_N_ELEMENTS (attrs), &object); + rv = gkm_session_C_CreateObject (test->session, attrs, G_N_ELEMENTS (attrs), &object); gkm_assert_cmprv (rv, ==, CKR_OK); gkm_assert_cmpulong (object, !=, 0); - rv = gkm_session_C_CreateObject (session, attrs, G_N_ELEMENTS (attrs), &check); + rv = gkm_session_C_CreateObject (test->session, attrs, G_N_ELEMENTS (attrs), &check); gkm_assert_cmprv (rv, ==, CKR_OK); gkm_assert_cmpulong (check, !=, 0); - rv = gkm_session_C_FindObjectsInit (session, attrs, G_N_ELEMENTS (attrs)); + rv = gkm_session_C_FindObjectsInit (test->session, attrs, G_N_ELEMENTS (attrs)); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = gkm_session_C_FindObjects (session, results, G_N_ELEMENTS (results), &n_objects); + rv = gkm_session_C_FindObjects (test->session, results, G_N_ELEMENTS (results), &n_objects); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = gkm_session_C_FindObjectsFinal (session); + rv = gkm_session_C_FindObjectsFinal (test->session); gkm_assert_cmprv (rv, ==, CKR_OK); /* Second should have overwritten the first */ @@ -558,7 +580,8 @@ TESTING_TEST (trust_create_assertion_complete_on_token) gkm_assert_cmpulong (results[0], ==, check); } -TESTING_TEST (trust_destroy_assertion_on_token) +static void +test_destroy_assertion_on_token (Test *test, gconstpointer unused) { CK_X_ASSERTION_TYPE atype = CKT_X_PINNED_CERTIFICATE; CK_OBJECT_HANDLE results[8]; @@ -571,30 +594,30 @@ TESTING_TEST (trust_destroy_assertion_on_token) { CKA_TOKEN, &token, sizeof (token) }, }; - rv = gkm_session_C_FindObjectsInit (session, attrs, G_N_ELEMENTS (attrs)); + rv = gkm_session_C_FindObjectsInit (test->session, attrs, G_N_ELEMENTS (attrs)); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = gkm_session_C_FindObjects (session, results, G_N_ELEMENTS (results), &n_objects); + rv = gkm_session_C_FindObjects (test->session, results, G_N_ELEMENTS (results), &n_objects); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = gkm_session_C_FindObjectsFinal (session); + rv = gkm_session_C_FindObjectsFinal (test->session); gkm_assert_cmprv (rv, ==, CKR_OK); gkm_assert_cmpulong (n_objects, ==, 1); - rv = gkm_session_C_DestroyObject (session, results[0]); + rv = gkm_session_C_DestroyObject (test->session, results[0]); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = gkm_session_C_FindObjectsInit (session, attrs, G_N_ELEMENTS (attrs)); + rv = gkm_session_C_FindObjectsInit (test->session, attrs, G_N_ELEMENTS (attrs)); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = gkm_session_C_FindObjects (session, results, G_N_ELEMENTS (results), &n_objects); + rv = gkm_session_C_FindObjects (test->session, results, G_N_ELEMENTS (results), &n_objects); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = gkm_session_C_FindObjectsFinal (session); + rv = gkm_session_C_FindObjectsFinal (test->session); gkm_assert_cmprv (rv, ==, CKR_OK); gkm_assert_cmpulong (n_objects, ==, 0); } static void -_assert_positive_netscape (CK_X_ASSERTION_TYPE assertion_type, const gchar *purpose, +_assert_positive_netscape (Test *test, CK_X_ASSERTION_TYPE assertion_type, const gchar *purpose, CK_ATTRIBUTE_TYPE netscape_type, CK_TRUST netscape_trust, const gchar *description) { @@ -612,7 +635,7 @@ _assert_positive_netscape (CK_X_ASSERTION_TYPE assertion_type, const gchar *purp CK_RV rv; CK_ATTRIBUTE attrs[] = { - { CKA_X_CERTIFICATE_VALUE, cert_data, n_cert_data }, + { CKA_X_CERTIFICATE_VALUE, test->cert_data, test->n_cert_data }, { CKA_CLASS, &aklass, sizeof (aklass) }, { CKA_X_ASSERTION_TYPE, &assertion_type, sizeof (assertion_type) }, { CKA_X_PURPOSE, (void*)purpose, strlen (purpose) }, @@ -625,21 +648,21 @@ _assert_positive_netscape (CK_X_ASSERTION_TYPE assertion_type, const gchar *purp { CKA_CERT_SHA1_HASH, checksum, sizeof (checksum) }, }; - rv = gkm_session_C_CreateObject (session, attrs, G_N_ELEMENTS (attrs), &object); + rv = gkm_session_C_CreateObject (test->session, attrs, G_N_ELEMENTS (attrs), &object); gkm_assert_cmprv (rv, ==, CKR_OK); gkm_assert_cmpulong (object, !=, 0); md = g_checksum_new (G_CHECKSUM_SHA1); - g_checksum_update (md, cert_data, n_cert_data); + g_checksum_update (md, (guchar*)test->cert_data, test->n_cert_data); n_checksum = sizeof (checksum); g_checksum_get_digest (md, checksum, &n_checksum); g_assert (n_checksum == sizeof (checksum)); - rv = gkm_session_C_FindObjectsInit (session, lookup, G_N_ELEMENTS (lookup)); + rv = gkm_session_C_FindObjectsInit (test->session, lookup, G_N_ELEMENTS (lookup)); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = gkm_session_C_FindObjects (session, results, G_N_ELEMENTS (results), &n_results); + rv = gkm_session_C_FindObjects (test->session, results, G_N_ELEMENTS (results), &n_results); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = gkm_session_C_FindObjectsFinal (session); + rv = gkm_session_C_FindObjectsFinal (test->session); gkm_assert_cmprv (rv, ==, CKR_OK); gkm_assert_cmpulong (n_results, ==, 1); @@ -649,7 +672,7 @@ _assert_positive_netscape (CK_X_ASSERTION_TYPE assertion_type, const gchar *purp attr.pValue = ✓ attr.ulValueLen = sizeof (check); - rv = gkm_session_C_GetAttributeValue (session, results[0], &attr, 1); + rv = gkm_session_C_GetAttributeValue (test->session, results[0], &attr, 1); gkm_assert_cmprv (rv, ==, CKR_OK); if (check != netscape_trust) @@ -658,7 +681,7 @@ _assert_positive_netscape (CK_X_ASSERTION_TYPE assertion_type, const gchar *purp } static void -_assert_negative_netscape (CK_X_ASSERTION_TYPE assertion_type, const gchar *purpose, +_assert_negative_netscape (Test *test, CK_X_ASSERTION_TYPE assertion_type, const gchar *purpose, CK_ATTRIBUTE_TYPE netscape_type, CK_TRUST netscape_trust, const gchar *description) { @@ -688,15 +711,15 @@ _assert_negative_netscape (CK_X_ASSERTION_TYPE assertion_type, const gchar *purp { CKA_ISSUER, (void*)DER_ISSUER, XL (DER_ISSUER) }, }; - rv = gkm_session_C_CreateObject (session, attrs, G_N_ELEMENTS (attrs), &object); + rv = gkm_session_C_CreateObject (test->session, attrs, G_N_ELEMENTS (attrs), &object); gkm_assert_cmprv (rv, ==, CKR_OK); gkm_assert_cmpulong (object, !=, 0); - rv = gkm_session_C_FindObjectsInit (session, lookup, G_N_ELEMENTS (lookup)); + rv = gkm_session_C_FindObjectsInit (test->session, lookup, G_N_ELEMENTS (lookup)); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = gkm_session_C_FindObjects (session, results, G_N_ELEMENTS (results), &n_results); + rv = gkm_session_C_FindObjects (test->session, results, G_N_ELEMENTS (results), &n_results); gkm_assert_cmprv (rv, ==, CKR_OK); - rv = gkm_session_C_FindObjectsFinal (session); + rv = gkm_session_C_FindObjectsFinal (test->session); gkm_assert_cmprv (rv, ==, CKR_OK); gkm_assert_cmpulong (n_results, ==, 1); @@ -706,7 +729,7 @@ _assert_negative_netscape (CK_X_ASSERTION_TYPE assertion_type, const gchar *purp attr.pValue = ✓ attr.ulValueLen = sizeof (check); - rv = gkm_session_C_GetAttributeValue (session, results[0], &attr, 1); + rv = gkm_session_C_GetAttributeValue (test->session, results[0], &attr, 1); gkm_assert_cmprv (rv, ==, CKR_OK); if (check != netscape_trust) @@ -715,87 +738,142 @@ _assert_negative_netscape (CK_X_ASSERTION_TYPE assertion_type, const gchar *purp } /* Some macros for intelligent failure messages */ -#define assert_positive_netscape(a, b, c, d) \ - _assert_positive_netscape (a, b, c, d, #a ", " #b ", " #c ", " #d) -#define assert_negative_netscape(a, b, c, d) \ - _assert_negative_netscape (a, b, c, d, #a ", " #b ", " #c ", " #d) +#define assert_positive_netscape(test, a, b, c, d) \ + _assert_positive_netscape (test, a, b, c, d, #a ", " #b ", " #c ", " #d) +#define assert_negative_netscape(test, a, b, c, d) \ + _assert_negative_netscape (test, a, b, c, d, #a ", " #b ", " #c ", " #d) -TESTING_TEST (trust_netscape_map_server_auth) +static void +test_netscape_map_server_auth (Test *test, gconstpointer unused) { - assert_positive_netscape (CKT_X_PINNED_CERTIFICATE, "1.3.6.1.5.5.7.3.1", + assert_positive_netscape (test, CKT_X_PINNED_CERTIFICATE, "1.3.6.1.5.5.7.3.1", CKA_TRUST_SERVER_AUTH, CKT_NETSCAPE_TRUSTED); - assert_positive_netscape (CKT_X_ANCHORED_CERTIFICATE, "1.3.6.1.5.5.7.3.1", + assert_positive_netscape (test, CKT_X_ANCHORED_CERTIFICATE, "1.3.6.1.5.5.7.3.1", CKA_TRUST_SERVER_AUTH, CKT_NETSCAPE_TRUSTED_DELEGATOR); - assert_negative_netscape (CKT_X_DISTRUSTED_CERTIFICATE, "1.3.6.1.5.5.7.3.1", + assert_negative_netscape (test, CKT_X_DISTRUSTED_CERTIFICATE, "1.3.6.1.5.5.7.3.1", CKA_TRUST_SERVER_AUTH, CKT_NETSCAPE_UNTRUSTED); } -TESTING_TEST (trust_netscape_map_client_auth) +static void +test_netscape_map_client_auth (Test *test, gconstpointer unused) { - assert_positive_netscape (CKT_X_PINNED_CERTIFICATE, "1.3.6.1.5.5.7.3.2", + assert_positive_netscape (test, CKT_X_PINNED_CERTIFICATE, "1.3.6.1.5.5.7.3.2", CKA_TRUST_CLIENT_AUTH, CKT_NETSCAPE_TRUSTED); - assert_positive_netscape (CKT_X_ANCHORED_CERTIFICATE, "1.3.6.1.5.5.7.3.2", + assert_positive_netscape (test, CKT_X_ANCHORED_CERTIFICATE, "1.3.6.1.5.5.7.3.2", CKA_TRUST_CLIENT_AUTH, CKT_NETSCAPE_TRUSTED_DELEGATOR); - assert_negative_netscape (CKT_X_DISTRUSTED_CERTIFICATE, "1.3.6.1.5.5.7.3.2", + assert_negative_netscape (test, CKT_X_DISTRUSTED_CERTIFICATE, "1.3.6.1.5.5.7.3.2", CKA_TRUST_CLIENT_AUTH, CKT_NETSCAPE_UNTRUSTED); } -TESTING_TEST (trust_netscape_map_code_signing) +static void +test_netscape_map_code_signing (Test *test, gconstpointer unused) { - assert_positive_netscape (CKT_X_PINNED_CERTIFICATE, "1.3.6.1.5.5.7.3.3", + assert_positive_netscape (test, CKT_X_PINNED_CERTIFICATE, "1.3.6.1.5.5.7.3.3", CKA_TRUST_CODE_SIGNING, CKT_NETSCAPE_TRUSTED); - assert_positive_netscape (CKT_X_ANCHORED_CERTIFICATE, "1.3.6.1.5.5.7.3.3", + assert_positive_netscape (test, CKT_X_ANCHORED_CERTIFICATE, "1.3.6.1.5.5.7.3.3", CKA_TRUST_CODE_SIGNING, CKT_NETSCAPE_TRUSTED_DELEGATOR); - assert_negative_netscape (CKT_X_DISTRUSTED_CERTIFICATE, "1.3.6.1.5.5.7.3.3", + assert_negative_netscape (test, CKT_X_DISTRUSTED_CERTIFICATE, "1.3.6.1.5.5.7.3.3", CKA_TRUST_CODE_SIGNING, CKT_NETSCAPE_UNTRUSTED); } -TESTING_TEST (trust_netscape_map_email) +static void +test_netscape_map_email (Test *test, gconstpointer unused) { - assert_positive_netscape (CKT_X_PINNED_CERTIFICATE, "1.3.6.1.5.5.7.3.4", + assert_positive_netscape (test, CKT_X_PINNED_CERTIFICATE, "1.3.6.1.5.5.7.3.4", CKA_TRUST_EMAIL_PROTECTION, CKT_NETSCAPE_TRUSTED); - assert_positive_netscape (CKT_X_ANCHORED_CERTIFICATE, "1.3.6.1.5.5.7.3.4", + assert_positive_netscape (test, CKT_X_ANCHORED_CERTIFICATE, "1.3.6.1.5.5.7.3.4", CKA_TRUST_EMAIL_PROTECTION, CKT_NETSCAPE_TRUSTED_DELEGATOR); - assert_negative_netscape (CKT_X_DISTRUSTED_CERTIFICATE, "1.3.6.1.5.5.7.3.4", + assert_negative_netscape (test, CKT_X_DISTRUSTED_CERTIFICATE, "1.3.6.1.5.5.7.3.4", CKA_TRUST_EMAIL_PROTECTION, CKT_NETSCAPE_UNTRUSTED); } -TESTING_TEST (trust_netscape_map_ipsec_endpoint) +static void +test_netscape_map_ipsec_endpoint (Test *test, gconstpointer unused) { - assert_positive_netscape (CKT_X_PINNED_CERTIFICATE, "1.3.6.1.5.5.7.3.5", + assert_positive_netscape (test, CKT_X_PINNED_CERTIFICATE, "1.3.6.1.5.5.7.3.5", CKA_TRUST_IPSEC_END_SYSTEM, CKT_NETSCAPE_TRUSTED); - assert_positive_netscape (CKT_X_ANCHORED_CERTIFICATE, "1.3.6.1.5.5.7.3.5", + assert_positive_netscape (test, CKT_X_ANCHORED_CERTIFICATE, "1.3.6.1.5.5.7.3.5", CKA_TRUST_IPSEC_END_SYSTEM, CKT_NETSCAPE_TRUSTED_DELEGATOR); - assert_negative_netscape (CKT_X_DISTRUSTED_CERTIFICATE, "1.3.6.1.5.5.7.3.5", + assert_negative_netscape (test, CKT_X_DISTRUSTED_CERTIFICATE, "1.3.6.1.5.5.7.3.5", CKA_TRUST_IPSEC_END_SYSTEM, CKT_NETSCAPE_UNTRUSTED); } -TESTING_TEST (trust_netscape_map_ipsec_tunnel) +static void +test_netscape_map_ipsec_tunnel (Test *test, gconstpointer unused) { - assert_positive_netscape (CKT_X_PINNED_CERTIFICATE, "1.3.6.1.5.5.7.3.6", + assert_positive_netscape (test, CKT_X_PINNED_CERTIFICATE, "1.3.6.1.5.5.7.3.6", CKA_TRUST_IPSEC_TUNNEL, CKT_NETSCAPE_TRUSTED); - assert_positive_netscape (CKT_X_ANCHORED_CERTIFICATE, "1.3.6.1.5.5.7.3.6", + assert_positive_netscape (test, CKT_X_ANCHORED_CERTIFICATE, "1.3.6.1.5.5.7.3.6", CKA_TRUST_IPSEC_TUNNEL, CKT_NETSCAPE_TRUSTED_DELEGATOR); - assert_negative_netscape (CKT_X_DISTRUSTED_CERTIFICATE, "1.3.6.1.5.5.7.3.6", + assert_negative_netscape (test, CKT_X_DISTRUSTED_CERTIFICATE, "1.3.6.1.5.5.7.3.6", CKA_TRUST_IPSEC_TUNNEL, CKT_NETSCAPE_UNTRUSTED); } -TESTING_TEST (trust_netscape_map_ipsec_user) +static void +test_netscape_map_ipsec_user (Test *test, gconstpointer unused) { - assert_positive_netscape (CKT_X_PINNED_CERTIFICATE, "1.3.6.1.5.5.7.3.7", + assert_positive_netscape (test, CKT_X_PINNED_CERTIFICATE, "1.3.6.1.5.5.7.3.7", CKA_TRUST_IPSEC_USER, CKT_NETSCAPE_TRUSTED); - assert_positive_netscape (CKT_X_ANCHORED_CERTIFICATE, "1.3.6.1.5.5.7.3.7", + assert_positive_netscape (test, CKT_X_ANCHORED_CERTIFICATE, "1.3.6.1.5.5.7.3.7", CKA_TRUST_IPSEC_USER, CKT_NETSCAPE_TRUSTED_DELEGATOR); - assert_negative_netscape (CKT_X_DISTRUSTED_CERTIFICATE, "1.3.6.1.5.5.7.3.7", + assert_negative_netscape (test, CKT_X_DISTRUSTED_CERTIFICATE, "1.3.6.1.5.5.7.3.7", CKA_TRUST_IPSEC_USER, CKT_NETSCAPE_UNTRUSTED); } -TESTING_TEST (trust_netscape_map_time_stamping) +static void +test_netscape_map_time_stamping (Test *test, gconstpointer unused) { - assert_positive_netscape (CKT_X_PINNED_CERTIFICATE, "1.3.6.1.5.5.7.3.8", + assert_positive_netscape (test, CKT_X_PINNED_CERTIFICATE, "1.3.6.1.5.5.7.3.8", CKA_TRUST_TIME_STAMPING, CKT_NETSCAPE_TRUSTED); - assert_positive_netscape (CKT_X_ANCHORED_CERTIFICATE, "1.3.6.1.5.5.7.3.8", + assert_positive_netscape (test, CKT_X_ANCHORED_CERTIFICATE, "1.3.6.1.5.5.7.3.8", CKA_TRUST_TIME_STAMPING, CKT_NETSCAPE_TRUSTED_DELEGATOR); - assert_negative_netscape (CKT_X_DISTRUSTED_CERTIFICATE, "1.3.6.1.5.5.7.3.8", + assert_negative_netscape (test, CKT_X_DISTRUSTED_CERTIFICATE, "1.3.6.1.5.5.7.3.8", CKA_TRUST_TIME_STAMPING, CKT_NETSCAPE_UNTRUSTED); } + + +static void +null_log_handler (const gchar *log_domain, GLogLevelFlags log_level, + const gchar *message, gpointer user_data) +{ + +} + +int +main (int argc, char **argv) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + + /* Suppress these messages in tests */ + g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, + null_log_handler, NULL); + + g_test_add ("/xdg-store/trust/load_objects", Test, NULL, setup, test_load_objects, teardown); + g_test_add ("/xdg-store/trust/create_assertion_complete", Test, NULL, setup, test_create_assertion_complete, teardown); + g_test_add ("/xdg-store/trust/complete_assertion_has_no_serial_or_issuer", Test, NULL, setup, test_complete_assertion_has_no_serial_or_issuer, teardown); + g_test_add ("/xdg-store/trust/complete_assertion_netscape_md5_hash", Test, NULL, setup, test_complete_assertion_netscape_md5_hash, teardown); + g_test_add ("/xdg-store/trust/complete_assertion_netscape_sha1_hash", Test, NULL, setup, test_complete_assertion_netscape_sha1_hash, teardown); + g_test_add ("/xdg-store/trust/create_assertion_missing_type", Test, NULL, setup, test_create_assertion_missing_type, teardown); + g_test_add ("/xdg-store/trust/create_assertion_bad_type", Test, NULL, setup, test_create_assertion_bad_type, teardown); + g_test_add ("/xdg-store/trust/create_assertion_missing_cert_value", Test, NULL, setup, test_create_assertion_missing_cert_value, teardown); + g_test_add ("/xdg-store/trust/create_assertion_bad_cert_value", Test, NULL, setup, test_create_assertion_bad_cert_value, teardown); + g_test_add ("/xdg-store/trust/create_assertion_null_cert_value", Test, NULL, setup, test_create_assertion_null_cert_value, teardown); + g_test_add ("/xdg-store/trust/create_assertion_for_distrusted", Test, NULL, setup, test_create_assertion_for_distrusted, teardown); + g_test_add ("/xdg-store/trust/create_assertion_for_distrusted_no_purpose", Test, NULL, setup, test_create_assertion_for_distrusted_no_purpose, teardown); + g_test_add ("/xdg-store/trust/create_assertion_for_distrusted_no_serial", Test, NULL, setup, test_create_assertion_for_distrusted_no_serial, teardown); + g_test_add ("/xdg-store/trust/create_assertion_twice", Test, NULL, setup, test_create_assertion_twice, teardown); + g_test_add ("/xdg-store/trust/distrusted_assertion_has_no_cert_value", Test, NULL, setup, test_distrusted_assertion_has_no_cert_value, teardown); + g_test_add ("/xdg-store/trust/create_assertion_complete_on_token", Test, NULL, setup, test_create_assertion_complete_on_token, teardown); + g_test_add ("/xdg-store/trust/destroy_assertion_on_token", Test, NULL, setup, test_destroy_assertion_on_token, teardown); + g_test_add ("/xdg-store/trust/netscape_map_server_auth", Test, NULL, setup, test_netscape_map_server_auth, teardown); + g_test_add ("/xdg-store/trust/netscape_map_client_auth", Test, NULL, setup, test_netscape_map_client_auth, teardown); + g_test_add ("/xdg-store/trust/netscape_map_code_signing", Test, NULL, setup, test_netscape_map_code_signing, teardown); + g_test_add ("/xdg-store/trust/netscape_map_email", Test, NULL, setup, test_netscape_map_email, teardown); + g_test_add ("/xdg-store/trust/netscape_map_ipsec_endpoint", Test, NULL, setup, test_netscape_map_ipsec_endpoint, teardown); + g_test_add ("/xdg-store/trust/netscape_map_ipsec_tunnel", Test, NULL, setup, test_netscape_map_ipsec_tunnel, teardown); + g_test_add ("/xdg-store/trust/netscape_map_ipsec_user", Test, NULL, setup, test_netscape_map_ipsec_user, teardown); + g_test_add ("/xdg-store/trust/netscape_map_time_stamping", Test, NULL, setup, test_netscape_map_time_stamping, teardown); + + return egg_tests_run_in_thread_with_loop (); +} diff --git a/testing/Makefile.am b/testing/Makefile.am index 6304d6ff..926eaaf6 100644 --- a/testing/Makefile.am +++ b/testing/Makefile.am @@ -1,22 +1,6 @@ EXTRA_DIST = \ - ca-example \ - testing.c testing.h \ - testing-build.sh \ - testing.make - -INCLUDES = \ - -I$(top_srcdir) \ - -I$(top_builddir) \ - $(GTK_CFLAGS) \ - $(GLIB_CFLAGS) - -LIBS = \ - $(GTK_LIBS) \ - $(GLIB_LIBS) \ - $(GTHREAD_LIBS) - -noinst_PROGRAMS = + ca-example CLEANFILES = \ coverage \ diff --git a/testing/testing-build.sh b/testing/testing-build.sh deleted file mode 100755 index fe15122a..00000000 --- a/testing/testing-build.sh +++ /dev/null @@ -1,139 +0,0 @@ -#!/bin/sh -eu - -set -eu - -# -------------------------------------------------------------------- -# FUNCTIONS - -usage() -{ - echo "usage: testing-build.sh -b base-name files.c ..." >&2 - exit 2 -} - -# -------------------------------------------------------------------- -# SOURCE FILE - -file_to_name() -{ - echo -n $1 | sed -E \ - -e 's/^(unit-)?test-//' \ - -e 's/\.c//' \ - -e 's/\.i//' | tr -c '[a-z0-9_]' '_' -} - -testing_lines() -{ - grep -h "testing__" $@ /dev/null || true -} - -build_header() -{ - echo "/* This is auto-generated code. Edit at your own peril. */" - echo "#include \"testing/testing.h\"" - echo - echo "#ifndef TESTING_HEADER_INCLUDED" - echo "#define TESTING_HEADER_INCLUDED" - echo - - testing_lines $@ | sed -ne 's/\(.*\)/\1;/p' - - echo - echo "#endif /* TESTING_HEADER_INCLUDED */" - echo -} - -build_source() -{ - echo '/* This is auto-generated code. Edit at your own peril. */' - echo "#include \"testing/testing.h\"" - echo "#include \"$BASE.h\"" - echo - echo "typedef void (*TestingFunc)(int *, const void *);" - echo - - lines="$(testing_lines $@)" - - # Startup function - echo "static void start_tests (void) {" - echo $lines | sed -n \ - -e "s/.*\(testing__start__[0-9a-z_]\+\).*/ \1 ();/p" - echo "}" - echo - - # Shutdown function - echo "static void stop_tests (void) {" - echo $lines | sed -n \ - -e "s/.*\(testing__stop__[0-9a-z_]\+\).*/ \1 ();/p" - echo "}" - echo - - # Add all tests to the test case - echo "static void initialize_tests (void) {" - first=YES - for file in $@; do - if [ "$first" = "YES" ]; then - echo " TestingFunc setup = NULL;" - echo " TestingFunc teardown = NULL;" - first=NO - fi - - name=$(file_to_name $file) - echo " setup = teardown = NULL;" - - testing_lines $file | sed -n \ - -e "s/.*\(testing__setup__[0-9a-z_]\+\).*/setup = \1;/p" \ - -e "s/.*\(testing__teardown__[0-9a-z_]\+\).*/teardown = \1;/p" \ - -e "s/.*testing__test__\([0-9a-z_]\+\).*/g_test_add(\"\/$name\/\1\", int, NULL, setup, testing__test__\1, teardown);/p" - done - echo "}" - echo - - # External function - echo "static void run_externals (int *ret) {" - echo $lines | sed -n \ - -e "s/.*\(testing__external__[0-9a-z_]\+\).*/ testing_external_run (\"\1\", \1, ret);/p" - echo "}" - echo - - echo "static int run(void) {" - echo " int ret;" - echo " initialize_tests ();" - echo " start_tests ();" - echo " ret = g_test_run ();" - echo " if (ret == 0)" - echo " run_externals (&ret);" - echo " stop_tests();" - echo " return ret;" - echo "}" - - echo "#include \"testing/testing.c\"" -} - -# -------------------------------------------------------------------- -# ARGUMENT PARSING - -BASE=unit - -while [ $# -gt 0 ]; do - case "$1" in - -b) - BASE="$2" - shift - ;; - --) - shift - break - ;; - -*) - usage - ;; - *) - break - ;; - esac - shift -done - -build_header $@ > $BASE.h -build_source $@ > $BASE.c diff --git a/testing/testing.c b/testing/testing.c deleted file mode 100644 index 2d98bc09..00000000 --- a/testing/testing.c +++ /dev/null @@ -1,394 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* test-helpers.c: Common functions called from gtest unit tests - - Copyright (C) 2008 Stefan Walter - Copyright (C) 2010 Collabora Ltd - - The Gnome Keyring Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The Gnome Keyring Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with the Gnome Library; see the file COPYING.LIB. If not, - write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. - - Author: Stef Walter <stef@memberwebs.com> -*/ - -/* This file is included into the main .c file for each gtest unit-test program */ - -#include "config.h" - -#include <glib.h> -#include <gtk/gtk.h> -#include <stdio.h> -#include <unistd.h> -#include <stdlib.h> -#include <string.h> -#include <errno.h> -#include <time.h> -#include <sys/time.h> - -#include "testing.h" - -#include "egg/egg-error.h" -#include "egg/egg-secure-memory.h" - -#include "pkcs11/pkcs11.h" - -#ifdef WITH_P11_TESTS -#include <p11-tests.h> -#endif - -/* Forward declaration */ -void testing_test_p11_module (CK_FUNCTION_LIST_PTR module, const gchar *config); - -static const gchar *test_path = NULL; - -EGG_SECURE_GLIB_DEFINITIONS (); - -static GCond *wait_condition = NULL; -static GCond *wait_start = NULL; -static GMutex *wait_mutex = NULL; -static gboolean wait_waiting = FALSE; - -void -testing_wait_stop (void) -{ - GTimeVal tv; - - g_get_current_time (&tv); - g_time_val_add (&tv, 1000); - - g_assert (wait_mutex); - g_assert (wait_condition); - g_mutex_lock (wait_mutex); - if (!wait_waiting) - g_cond_timed_wait (wait_start, wait_mutex, &tv); - g_assert (wait_waiting); - g_cond_broadcast (wait_condition); - g_mutex_unlock (wait_mutex); -} - -gboolean -testing_wait_until (int timeout) -{ - GTimeVal tv; - gboolean ret; - - g_get_current_time (&tv); - g_time_val_add (&tv, timeout * 1000); - - g_assert (wait_mutex); - g_assert (wait_condition); - g_mutex_lock (wait_mutex); - g_assert (!wait_waiting); - wait_waiting = TRUE; - g_cond_broadcast (wait_start); - ret = g_cond_timed_wait (wait_condition, wait_mutex, &tv); - g_assert (wait_waiting); - wait_waiting = FALSE; - g_mutex_unlock (wait_mutex); - - return ret; -} - -const gchar* -testing_scratch_directory (void) -{ - return test_path; -} - -gchar* -testing_scratch_filename (const gchar *basename) -{ - return g_build_filename (test_path, basename, NULL); -} - -gchar* -testing_data_filename (const gchar *basename) -{ - return g_build_filename (testing_data_directory (), basename, NULL); -} - -const gchar* -testing_data_directory (void) -{ - const gchar *dir; - gchar *cur, *env; - - dir = g_getenv ("TEST_DATA"); - if (dir == NULL) - dir = "./test-data"; - if (!g_path_is_absolute (dir)) { - cur = g_get_current_dir (); - if (strncmp (dir, "./", 2) == 0) - dir += 2; - env = g_build_filename (cur, dir, NULL); - g_free (cur); - g_setenv ("TEST_DATA", env, TRUE); - g_free (env); - dir = g_getenv ("TEST_DATA"); - } - - return dir; -} - -guchar* -testing_data_read (const gchar *basename, gsize *n_result) -{ - GError *error = NULL; - gchar *result; - gchar *file; - - file = testing_data_filename (basename); - if (!g_file_get_contents (file, &result, n_result, &error)) { - g_warning ("could not read test data file: %s: %s", file, - egg_error_message (error)); - g_assert_not_reached (); - } - - g_free (file); - return (guchar*)result; -} - -void -testing_data_to_scratch (const gchar *basename, const gchar *newname) -{ - gchar *filename; - gchar *data; - gsize n_data; - - filename = testing_data_filename (basename); - if (!g_file_get_contents (filename, &data, &n_data, NULL)) - g_error ("couldn't read: %s", filename); - g_free (filename); - - filename = testing_scratch_filename (newname ? newname : basename); - if (!g_file_set_contents (filename, data, n_data, NULL)) - g_error ("couldn't write: %s", filename); - g_free (filename); - g_free (data); -} - - -void -testing_scratch_empty (const gchar *basename) -{ - GError *err = NULL; - gchar *filename; - filename = testing_scratch_filename (basename); - if (!g_file_set_contents (filename, "", 0, &err)) - g_error ("couldn't write to file: %s: %s", filename, - err && err->message ? err->message : ""); - g_free (filename); -} - -void -testing_scratch_touch (const gchar *basename, gint future) -{ - gchar *filename; - struct timeval tv[2]; - - filename = testing_scratch_filename (basename); - - /* Initialize the access and modification times */ - gettimeofday (tv, NULL); - tv[0].tv_sec += future; - memcpy (tv + 1, tv, sizeof (struct timeval)); - - if (utimes (filename, tv) < 0) - g_error ("couldn't update file time: %s: %s", filename, g_strerror (errno)); - - g_free (filename); -} - -void -testing_scratch_remove (const gchar *basename) -{ - gchar *filename; - - filename = testing_scratch_filename (basename); - if (g_unlink (filename) < 0) - g_error ("couldn't delete file: %s: %s", filename, g_strerror (errno)); - - g_free (filename); -} - -void -testing_scratch_remove_all (void) -{ - GError *err = NULL; - const gchar *basename; - GDir *dir; - - /* WARNING: Don't change path without changing remove below */ - dir = g_dir_open (testing_scratch_directory (), 0, &err); - if (dir == NULL) - g_error ("couldn't open directory: %s", - err && err->message ? err->message : ""); - - while ((basename = g_dir_read_name (dir)) != NULL) - testing_scratch_remove (basename); - - g_dir_close (dir); -} - -#if WITH_P11_TESTS - -static void -on_p11_tests_log (int level, const char *section, const char *message) -{ - if (level == P11_TESTS_NONE) { - g_message ("%s", message); - } else if (level != P11_TESTS_FAIL) { - g_message ("%s: %s", section, message); - } else { - g_print ("/%s/%s: FAIL: %s\n", testing_external_name (), section, message); - testing_external_fail (); - } -} - -void -testing_test_p11_module (CK_FUNCTION_LIST_PTR module, const gchar *config) -{ - p11_tests_set_log_func (on_p11_tests_log); - p11_tests_set_unexpected (1); - p11_tests_set_verbose (0); - p11_tests_set_write_session (1); - if (config) - p11_tests_load_config (config); - p11_tests_perform (module); -} - -#else /* !WITH_P11_TESTS */ - -void -testing_test_p11_module (CK_FUNCTION_LIST_PTR module, const gchar *config) -{ - g_message ("p11-tests support not built in"); -} - -#endif /* !WITH_P11_TESTS */ - -static const gchar *external_name = NULL; -static gint external_fails = 0; - -void -testing_external_run (const gchar *name, TestingExternalFunc func, int *result) -{ - if (result != 0) - return; - - external_fails = 0; - external_name = name; - func (); - if (external_fails) { - g_printerr ("/%s: FAIL: %d failures", name, external_fails); - *result = external_fails; - } -} - -const gchar* -testing_external_name (void) -{ - return external_name; -} - -void -testing_external_fail (void) -{ - ++external_fails; -} - -static void -chdir_base_dir (char* argv0) -{ - gchar *dir, *base; - - dir = g_path_get_dirname (argv0); - if (chdir (dir) < 0) - g_warning ("couldn't change directory to: %s: %s", - dir, g_strerror (errno)); - - base = g_path_get_basename (dir); - if (strcmp (base, ".libs") == 0) { - if (chdir ("..") < 0) - g_warning ("couldn't change directory to ..: %s", - g_strerror (errno)); - } - - g_free (base); - g_free (dir); -} - -static gpointer -testing_thread (gpointer loop) -{ - /* Must have been defined by the test including this file */ - gint ret = run(); - g_main_loop_quit (loop); - return GINT_TO_POINTER (ret); -} - -static void -null_log_handler (const gchar *log_domain, GLogLevelFlags log_level, - const gchar *message, gpointer user_data) -{ - -} - -int -main (int argc, char* argv[]) -{ - GLogLevelFlags fatal_mask; - GThread *thread; - GMainLoop *loop; - gpointer ret; - - g_type_init (); - g_thread_init (NULL); - - test_path = getenv ("GNOME_KEYRING_TEST_PATH"); - if (!test_path) { - test_path = "/tmp/test-gnome-keyring"; - setenv ("GNOME_KEYRING_TEST_PATH", test_path, 1); - g_mkdir_with_parents (test_path, 0777); - } - - chdir_base_dir (argv[0]); - g_test_init (&argc, &argv, NULL); - gtk_init (&argc, &argv); - - loop = g_main_loop_new (NULL, FALSE); - wait_condition = g_cond_new (); - wait_start = g_cond_new (); - wait_mutex = g_mutex_new (); - - fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK); - fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL; - g_log_set_always_fatal (fatal_mask); - - /* Suppress these messages in tests */ - g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, - null_log_handler, NULL); - - thread = g_thread_create (testing_thread, loop, TRUE, NULL); - g_assert (thread); - - g_main_loop_run (loop); - ret = g_thread_join (thread); - g_main_loop_unref (loop); - - g_cond_free (wait_condition); - g_mutex_free (wait_mutex); - - return GPOINTER_TO_INT (ret); -} diff --git a/testing/testing.h b/testing/testing.h deleted file mode 100644 index 9cbbf5b4..00000000 --- a/testing/testing.h +++ /dev/null @@ -1,104 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* testing.h: Declarations for common functions called from gtest unit tests - - Copyright (C) 2008 Stefan Walter - Copyright (C) 2010 Collabora Ltd - - The Gnome Keyring Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The Gnome Keyring Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with the Gnome Library; see the file COPYING.LIB. If not, - write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. - - Author: Stef Walter <stef@memberwebs.com> -*/ - -#ifndef TESTING_H_ -#define TESTING_H_ - -/* Don't use this header while preparing tests */ -#ifndef TESTING_PREPARING - -#include <glib.h> -#include <glib-object.h> -#include <glib/gstdio.h> - -#include <stdlib.h> -#include <stdio.h> -#include <string.h> -#include <unistd.h> - -gboolean testing_wait_until (gint timeout); - -void testing_wait_stop (void); - -const gchar* testing_data_directory (void); - -const gchar* testing_scratch_directory (void); - -guchar* testing_data_read (const gchar *basename, - gsize *n_data); - -void testing_data_to_scratch (const gchar *basename, - const gchar *newname); - -gchar* testing_scratch_filename (const gchar *basename); - -void testing_scratch_empty (const gchar *basename); - -void testing_scratch_touch (const gchar *basename, - gint future); - -void testing_scratch_remove (const gchar *basename); - -void testing_scratch_remove_all (void); - -gchar* testing_data_filename (const gchar *basename); - -#ifdef CRYPTOKI_VERSION_MAJOR - -void testing_test_p11_module (CK_FUNCTION_LIST_PTR module, - const gchar *config); - -#endif - -typedef void (*TestingExternalFunc) (void); - -void testing_external_run (const gchar *name, - TestingExternalFunc func, - int *result); - -const gchar* testing_external_name (void); - -void testing_external_fail (void); - -#define TESTING_SETUP(x) \ - void testing__setup__##x(int *__unused, gconstpointer __data) -#define TESTING_TEARDOWN(x) \ - void testing__teardown__##x(int *__unused, gconstpointer __data) -#define TESTING_TEST(x) \ - void testing__test__##x(int *__unused, gconstpointer __data) -#define TESTING_START(x) \ - void testing__start__##x(void) -#define TESTING_STOP(x) \ - void testing__stop__##x(void) -#define TESTING_EXTERNAL(x) \ - void testing__external__##x(void) - -#ifndef g_assert_cmpsize -#define g_assert_cmpsize(a, o, b) \ - g_assert_cmpuint ((guint)(a), o, (guint)(b)) -#endif - -#endif /* TESTING_PREPARING */ - -#endif /* TESTING_H_ */ diff --git a/testing/testing.make b/testing/testing.make deleted file mode 100644 index cfc9c0e6..00000000 --- a/testing/testing.make +++ /dev/null @@ -1,77 +0,0 @@ - -# The following need to be declared before this file is included: -# TESTING_FILES A list of C files with tests -# TESTING_LIBS Libraries to link the tests to -# TESTING_FLAGS C flags for tests -# TESTING_SOURCES Other sources for tests - -# ------------------------------------------------------------------------------ - -INCLUDES= \ - -I$(top_srcdir) \ - -I$(top_builddir) \ - -I$(srcdir)/.. \ - -I$(srcdir)/../.. \ - $(GTK_CFLAGS) \ - $(GLIB_CFLAGS) \ - $(P11_TESTS_CFLAGS) \ - $(TESTING_FLAGS) - -LIBS = \ - $(GTK_LIBS) \ - $(GLIB_LIBS) \ - $(GTHREAD_LIBS) \ - $(P11_TESTS_LIBS) - -noinst_PROGRAMS= \ - run-tests - -PREPROCESSED = \ - $(TESTING_FILES:.c=.i) - -.c.i: - @echo "#include \"testing/testing.h\"" > test-suite.h - $(AM_V_GEN)$(CC) -E $(CFLAGS) $(INCLUDES) -o $@ $< - -.i.o: - $(AM_V_GEN)$(CC) -c $(CFLAGS) $(INCLUDES) -o $@ $< - -test-suite.c: $(PREPROCESSED) Makefile.am $(top_srcdir)/testing/testing-build.sh - sh $(top_srcdir)/testing/testing-build.sh -b test-suite $(PREPROCESSED) - -test-suite.h: test-suite.c - -run_tests_SOURCES = \ - test-suite.c test-suite.h \ - $(TESTING_SOURCES) \ - $(PREPROCESSED) - -run_tests_LDADD = \ - $(TESTING_LIBS) \ - $(DAEMON_LIBS) - -run_tests_CFLAGS = \ - $(TESTING_FLAGS) \ - $(GCOV_CFLAGS) - -run_tests_LDFLAGS = \ - $(GCOV_LDFLAGS) - -BUILT_SOURCES = \ - test-suite.c \ - test-suite.h - -CLEANFILES = \ - $(PREPROCESSED) \ - $(BUILT_SOURCES) - -EXTRA_DIST = \ - $(TESTING_FILES) - -# ------------------------------------------------------------------------------ -# Run the tests - -test: $(noinst_PROGRAMS) - TEST_DATA=$(srcdir)/test-data gtester --verbose -k -m=slow ./run-tests - -check-am: $(noinst_PROGRAMS) test diff --git a/ui/tests/Makefile.am b/ui/tests/Makefile.am index cf2c5301..46f7b352 100644 --- a/ui/tests/Makefile.am +++ b/ui/tests/Makefile.am @@ -1,14 +1,29 @@ -# Test files should be listed in order they need to run -TESTING_FILES = \ - unit-test-util.c -UNIT_PROMPT = +INCLUDES = \ + -I$(top_builddir) \ + -I$(top_srcdir) \ + -DSRCDIR="\"$(srcdir)\"" \ + $(GLIB_CFLAGS) \ + $(LIBGCRYPT_CFLAGS) -TESTING_LIBS = \ - $(top_builddir)/ui/libgku-prompt.la \ - $(top_builddir)/egg/libegg-prompt.la +LDADD = \ + $(top_builddir)/ui/libgku-prompt-testable.la \ + $(top_builddir)/egg/libegg.la \ + $(GLIB_LIBS) \ + $(GOBJECT_LIBS) \ + $(LIBGCRYPT_LIBS) -include $(top_srcdir)/testing/testing.make +TEST_PROGS = \ + test-util -EXTRA_DIST += \ - test-data
\ No newline at end of file +check_PROGRAMS = $(TEST_PROGS) + +test: $(TEST_PROGS) $(CHECK_PROGS) + gtester -k --verbose -m $(TEST_MODE) --g-fatal-warnings $(TEST_PROGS) + +check-local: test + +all-local: $(check_PROGRAMS) + +EXTRA_DIST = \ + files diff --git a/ui/tests/test-data/prompt-empty b/ui/tests/files/prompt-empty index 825111e2..825111e2 100644 --- a/ui/tests/test-data/prompt-empty +++ b/ui/tests/files/prompt-empty diff --git a/ui/tests/test-data/prompt-full b/ui/tests/files/prompt-full index ad504908..ad504908 100644 --- a/ui/tests/test-data/prompt-full +++ b/ui/tests/files/prompt-full diff --git a/ui/tests/test-data/prompt-test b/ui/tests/files/prompt-test index c00e826f..c00e826f 100644 --- a/ui/tests/test-data/prompt-test +++ b/ui/tests/files/prompt-test diff --git a/ui/tests/unit-test-util.c b/ui/tests/test-util.c index 6c9d1cdc..0cb92121 100644 --- a/ui/tests/unit-test-util.c +++ b/ui/tests/test-util.c @@ -21,39 +21,48 @@ Author: Stef Walter <stef@memberwebs.com> */ -#include "test-suite.h" +#include "config.h" -#include "gku-prompt-util.h" +#include "ui/gku-prompt-util.h" -#include <egg/egg-dh.h> -#include <egg/egg-libgcrypt.h> -#include <egg/egg-secure-memory.h> +#include "egg/egg-dh.h" +#include "egg/egg-libgcrypt.h" +#include "egg/egg-secure-memory.h" #include <gcrypt.h> -static GKeyFile *key_file = NULL; +#include <glib-object.h> -TESTING_SETUP(prompt_util) +EGG_SECURE_GLIB_DEFINITIONS (); + +typedef struct { + GKeyFile *key_file; +} Test; + +static void +setup (Test *test, gconstpointer unused) { egg_libgcrypt_initialize (); - key_file = g_key_file_new (); + test->key_file = g_key_file_new (); } -TESTING_TEARDOWN(prompt_util) +static void +teardown (Test *test, gconstpointer unused) { - g_key_file_free (key_file); - key_file = NULL; + g_key_file_free (test->key_file); + test->key_file = NULL; } -TESTING_TEST(encode_decode_mpi) +static void +test_encode_decode_mpi (Test *test, gconstpointer unused) { gcry_mpi_t mpi, check; mpi = gcry_mpi_new (512); gcry_mpi_randomize (mpi, 512, GCRY_WEAK_RANDOM); - gku_prompt_util_encode_mpi (key_file, "section", "field", mpi); - if (!gku_prompt_util_decode_mpi (key_file, "section", "field", &check)) + gku_prompt_util_encode_mpi (test->key_file, "section", "field", mpi); + if (!gku_prompt_util_decode_mpi (test->key_file, "section", "field", &check)) g_assert_not_reached (); g_assert (gcry_mpi_cmp (mpi, check) == 0); @@ -61,23 +70,25 @@ TESTING_TEST(encode_decode_mpi) gcry_mpi_release (check); } -TESTING_TEST(decode_nonexistant_mpi) +static void +test_decode_nonexistant_mpi (Test *test, gconstpointer unused) { gcry_mpi_t mpi; - if (gku_prompt_util_decode_mpi (key_file, "nonexist", "nope", &mpi)) + if (gku_prompt_util_decode_mpi (test->key_file, "nonexist", "nope", &mpi)) g_assert_not_reached (); } -TESTING_TEST(encode_decode_hex) +static void +test_encode_decode_hex (Test *test, gconstpointer unused) { gchar buffer[32]; gpointer check; gsize n_check; gcry_create_nonce (buffer, 32); - gku_prompt_util_encode_hex (key_file, "section", "field", buffer, 32); - check = gku_prompt_util_decode_hex (key_file, "section", "field", &n_check); + gku_prompt_util_encode_hex (test->key_file, "section", "field", buffer, 32); + check = gku_prompt_util_decode_hex (test->key_file, "section", "field", &n_check); g_assert (check); g_assert (n_check == 32); g_assert (memcmp (buffer, check, 32) == 0); @@ -85,11 +96,12 @@ TESTING_TEST(encode_decode_hex) g_free (check); } -TESTING_TEST(decode_nonexistant_hex) +static void +test_decode_nonexistant_hex (Test *test, gconstpointer unused) { gsize n_data; - if (gku_prompt_util_decode_hex (key_file, "nonexist", "nope", &n_data)) + if (gku_prompt_util_decode_hex (test->key_file, "nonexist", "nope", &n_data)) g_assert_not_reached (); } @@ -126,7 +138,8 @@ do_encrypt_decrypt_text (const gchar *text) g_assert_cmpstr (check, ==, text); } -TESTING_TEST(encrypt_decrypt_text) +static void +test_encrypt_decrypt_text (Test *test, gconstpointer unused) { do_encrypt_decrypt_text (""); do_encrypt_decrypt_text ("blah"); @@ -134,3 +147,18 @@ TESTING_TEST(encrypt_decrypt_text) do_encrypt_decrypt_text ("0123456789ABCDE"); do_encrypt_decrypt_text ("0123456789ABCDEF 12345"); } + +int +main (int argc, char **argv) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + + g_test_add ("/ui/util/encode_decode_mpi", Test, NULL, setup, test_encode_decode_mpi, teardown); + g_test_add ("/ui/util/decode_nonexistant_mpi", Test, NULL, setup, test_decode_nonexistant_mpi, teardown); + g_test_add ("/ui/util/encode_decode_hex", Test, NULL, setup, test_encode_decode_hex, teardown); + g_test_add ("/ui/util/decode_nonexistant_hex", Test, NULL, setup, test_decode_nonexistant_hex, teardown); + g_test_add ("/ui/util/encrypt_decrypt_text", Test, NULL, setup, test_encrypt_decrypt_text, teardown); + + return g_test_run (); +} |