diff options
Diffstat (limited to 'p11-kit/tests')
-rw-r--r-- | p11-kit/tests/Makefile.am | 32 | ||||
-rw-r--r-- | p11-kit/tests/files/system-pkcs11.conf | 5 | ||||
-rw-r--r-- | p11-kit/tests/files/user-modules/one.module | 3 | ||||
-rw-r--r-- | p11-kit/tests/test-conf.c (renamed from p11-kit/tests/conf-test.c) | 252 | ||||
-rw-r--r-- | p11-kit/tests/test-deprecated.c | 508 | ||||
-rw-r--r-- | p11-kit/tests/test-init.c | 239 | ||||
-rw-r--r-- | p11-kit/tests/test-iter.c | 553 | ||||
-rw-r--r-- | p11-kit/tests/test-log.c | 112 | ||||
-rw-r--r-- | p11-kit/tests/test-managed.c | 215 | ||||
-rw-r--r-- | p11-kit/tests/test-mock.c | 1679 | ||||
-rw-r--r-- | p11-kit/tests/test-modules.c | 229 | ||||
-rw-r--r-- | p11-kit/tests/test-pin.c (renamed from p11-kit/tests/pin-test.c) | 104 | ||||
-rw-r--r-- | p11-kit/tests/test-progname.c (renamed from p11-kit/tests/progname-test.c) | 34 | ||||
-rw-r--r-- | p11-kit/tests/test-proxy.c | 195 | ||||
-rw-r--r-- | p11-kit/tests/test-uri.c (renamed from p11-kit/tests/uri-test.c) | 633 | ||||
-rw-r--r-- | p11-kit/tests/test-virtual.c | 171 |
16 files changed, 3987 insertions, 977 deletions
diff --git a/p11-kit/tests/Makefile.am b/p11-kit/tests/Makefile.am index c7b87ae..6963850 100644 --- a/p11-kit/tests/Makefile.am +++ b/p11-kit/tests/Makefile.am @@ -3,29 +3,41 @@ include $(top_srcdir)/build/Makefile.tests COMMON = $(top_srcdir)/common -INCLUDES = \ +AM_CPPFLAGS = \ -I$(top_srcdir) \ -I$(srcdir)/.. \ -I$(COMMON) \ - $(CUTEST_CFLAGS) + $(TEST_CFLAGS) LDADD = \ $(top_builddir)/p11-kit/libp11-kit-testable.la \ - $(top_builddir)/common/libp11-mock.la \ + $(top_builddir)/common/libp11-test.la \ $(top_builddir)/common/libp11-common.la \ $(CUTEST_LIBS) \ $(LTLIBINTL) CHECK_PROGS = \ - progname-test \ - conf-test \ - uri-test \ - pin-test \ + test-progname \ + test-conf \ + test-uri \ + test-pin \ test-init \ test-modules \ + test-deprecated \ + test-proxy \ test-iter \ $(NULL) +if WITH_FFI + +CHECK_PROGS += \ + test-virtual \ + test-managed \ + test-log \ + $(NULL) + +endif + noinst_PROGRAMS = \ print-messages \ $(CHECK_PROGS) @@ -45,7 +57,7 @@ mock_one_la_CFLAGS = \ $(AM_CFLAGS) mock_one_la_LIBADD = \ - $(top_builddir)/common/libp11-mock.la \ + $(top_builddir)/common/libp11-test.la \ $(top_builddir)/common/libp11-common.la \ $(NULL) @@ -68,4 +80,6 @@ mock_four_la_LDFLAGS = $(mock_one_la_LDFLAGS) mock_four_la_LIBADD = $(mock_one_la_LIBADD) EXTRA_DIST = \ - files + files \ + test-mock.c \ + $(NULL) diff --git a/p11-kit/tests/files/system-pkcs11.conf b/p11-kit/tests/files/system-pkcs11.conf index 20741e7..a3aa273 100644 --- a/p11-kit/tests/files/system-pkcs11.conf +++ b/p11-kit/tests/files/system-pkcs11.conf @@ -1,3 +1,6 @@ # Merge in user config -user-config: merge
\ No newline at end of file +user-config: merge + +# Another option +new: world
\ No newline at end of file diff --git a/p11-kit/tests/files/user-modules/one.module b/p11-kit/tests/files/user-modules/one.module index c371e4a..6f1a2e8 100644 --- a/p11-kit/tests/files/user-modules/one.module +++ b/p11-kit/tests/files/user-modules/one.module @@ -1,2 +1,3 @@ -setting: user1
\ No newline at end of file +setting: user1 +managed: yes
\ No newline at end of file diff --git a/p11-kit/tests/conf-test.c b/p11-kit/tests/test-conf.c index d259cf8..c214bac 100644 --- a/p11-kit/tests/conf-test.c +++ b/p11-kit/tests/test-conf.c @@ -33,7 +33,7 @@ */ #include "config.h" -#include "CuTest.h" +#include "test.h" #include <errno.h> #include <stdlib.h> @@ -47,54 +47,54 @@ #include "private.h" static void -test_parse_conf_1 (CuTest *tc) +test_parse_conf_1 (void) { p11_dict *map; const char *value; map = _p11_conf_parse_file (SRCDIR "/files/test-1.conf", 0); - CuAssertPtrNotNull (tc, map); + assert_ptr_not_null (map); value = p11_dict_get (map, "key1"); - CuAssertStrEquals (tc, "value1", value); + assert_str_eq ("value1", value); value = p11_dict_get (map, "with-colon"); - CuAssertStrEquals (tc, "value-of-colon", value); + assert_str_eq ("value-of-colon", value); value = p11_dict_get (map, "with-whitespace"); - CuAssertStrEquals (tc, "value-with-whitespace", value); + assert_str_eq ("value-with-whitespace", value); value = p11_dict_get (map, "embedded-comment"); - CuAssertStrEquals (tc, "this is # not a comment", value); + assert_str_eq ("this is # not a comment", value); p11_dict_free (map); } static void -test_parse_ignore_missing (CuTest *tc) +test_parse_ignore_missing (void) { p11_dict *map; map = _p11_conf_parse_file (SRCDIR "/files/non-existant.conf", CONF_IGNORE_MISSING); - CuAssertPtrNotNull (tc, map); + assert_ptr_not_null (map); - CuAssertIntEquals (tc, 0, p11_dict_size (map)); - CuAssertPtrEquals (tc, NULL, (void*)p11_message_last ()); + assert_num_eq (0, p11_dict_size (map)); + assert (p11_message_last () == NULL); p11_dict_free (map); } static void -test_parse_fail_missing (CuTest *tc) +test_parse_fail_missing (void) { p11_dict *map; map = _p11_conf_parse_file (SRCDIR "/files/non-existant.conf", 0); - CuAssertPtrEquals (tc, map, NULL); - CuAssertPtrNotNull (tc, p11_message_last ()); + assert (map == NULL); + assert_ptr_not_null (p11_message_last ()); } static void -test_merge_defaults (CuTest *tc) +test_merge_defaults (void) { p11_dict *values; p11_dict *defaults; @@ -109,19 +109,19 @@ test_merge_defaults (CuTest *tc) p11_dict_set (defaults, strdup ("three"), strdup ("default3")); if (!_p11_conf_merge_defaults (values, defaults)) - CuFail (tc, "should not be reached"); + assert_not_reached (); p11_dict_free (defaults); - CuAssertStrEquals (tc, p11_dict_get (values, "one"), "real1"); - CuAssertStrEquals (tc, p11_dict_get (values, "two"), "real2"); - CuAssertStrEquals (tc, p11_dict_get (values, "three"), "default3"); + assert_str_eq (p11_dict_get (values, "one"), "real1"); + assert_str_eq (p11_dict_get (values, "two"), "real2"); + assert_str_eq (p11_dict_get (values, "three"), "default3"); p11_dict_free (values); } static void -test_load_globals_merge (CuTest *tc) +test_load_globals_merge (void) { int user_mode = -1; p11_dict *config; @@ -131,19 +131,19 @@ test_load_globals_merge (CuTest *tc) config = _p11_conf_load_globals (SRCDIR "/files/test-system-merge.conf", SRCDIR "/files/test-user.conf", &user_mode); - CuAssertPtrNotNull (tc, config); - CuAssertStrEquals (tc, NULL, p11_message_last ()); - CuAssertIntEquals (tc, CONF_USER_MERGE, user_mode); + assert_ptr_not_null (config); + assert (NULL == p11_message_last ()); + assert_num_eq (CONF_USER_MERGE, user_mode); - CuAssertStrEquals (tc, p11_dict_get (config, "key1"), "system1"); - CuAssertStrEquals (tc, p11_dict_get (config, "key2"), "user2"); - CuAssertStrEquals (tc, p11_dict_get (config, "key3"), "user3"); + assert_str_eq (p11_dict_get (config, "key1"), "system1"); + assert_str_eq (p11_dict_get (config, "key2"), "user2"); + assert_str_eq (p11_dict_get (config, "key3"), "user3"); p11_dict_free (config); } static void -test_load_globals_no_user (CuTest *tc) +test_load_globals_no_user (void) { int user_mode = -1; p11_dict *config; @@ -153,19 +153,19 @@ test_load_globals_no_user (CuTest *tc) config = _p11_conf_load_globals (SRCDIR "/files/test-system-none.conf", SRCDIR "/files/test-user.conf", &user_mode); - CuAssertPtrNotNull (tc, config); - CuAssertStrEquals (tc, NULL, p11_message_last ()); - CuAssertIntEquals (tc, CONF_USER_NONE, user_mode); + assert_ptr_not_null (config); + assert (NULL == p11_message_last ()); + assert_num_eq (CONF_USER_NONE, user_mode); - CuAssertStrEquals (tc, p11_dict_get (config, "key1"), "system1"); - CuAssertStrEquals (tc, p11_dict_get (config, "key2"), "system2"); - CuAssertStrEquals (tc, p11_dict_get (config, "key3"), "system3"); + assert_str_eq (p11_dict_get (config, "key1"), "system1"); + assert_str_eq (p11_dict_get (config, "key2"), "system2"); + assert_str_eq (p11_dict_get (config, "key3"), "system3"); p11_dict_free (config); } static void -test_load_globals_user_sets_only (CuTest *tc) +test_load_globals_user_sets_only (void) { int user_mode = -1; p11_dict *config; @@ -175,19 +175,19 @@ test_load_globals_user_sets_only (CuTest *tc) config = _p11_conf_load_globals (SRCDIR "/files/test-system-merge.conf", SRCDIR "/files/test-user-only.conf", &user_mode); - CuAssertPtrNotNull (tc, config); - CuAssertStrEquals (tc, NULL, p11_message_last ()); - CuAssertIntEquals (tc, CONF_USER_ONLY, user_mode); + assert_ptr_not_null (config); + assert (NULL == p11_message_last ()); + assert_num_eq (CONF_USER_ONLY, user_mode); - CuAssertStrEquals (tc, p11_dict_get (config, "key1"), NULL); - CuAssertStrEquals (tc, p11_dict_get (config, "key2"), "user2"); - CuAssertStrEquals (tc, p11_dict_get (config, "key3"), "user3"); + assert (p11_dict_get (config, "key1") == NULL); + assert_str_eq (p11_dict_get (config, "key2"), "user2"); + assert_str_eq (p11_dict_get (config, "key3"), "user3"); p11_dict_free (config); } static void -test_load_globals_system_sets_only (CuTest *tc) +test_load_globals_system_sets_only (void) { int user_mode = -1; p11_dict *config; @@ -197,19 +197,19 @@ test_load_globals_system_sets_only (CuTest *tc) config = _p11_conf_load_globals (SRCDIR "/files/test-system-only.conf", SRCDIR "/files/test-user.conf", &user_mode); - CuAssertPtrNotNull (tc, config); - CuAssertStrEquals (tc, NULL, p11_message_last ()); - CuAssertIntEquals (tc, CONF_USER_ONLY, user_mode); + assert_ptr_not_null (config); + assert (NULL == p11_message_last ()); + assert_num_eq (CONF_USER_ONLY, user_mode); - CuAssertStrEquals (tc, p11_dict_get (config, "key1"), NULL); - CuAssertStrEquals (tc, p11_dict_get (config, "key2"), "user2"); - CuAssertStrEquals (tc, p11_dict_get (config, "key3"), "user3"); + assert (p11_dict_get (config, "key1") == NULL); + assert_str_eq (p11_dict_get (config, "key2"), "user2"); + assert_str_eq (p11_dict_get (config, "key3"), "user3"); p11_dict_free (config); } static void -test_load_globals_system_sets_invalid (CuTest *tc) +test_load_globals_system_sets_invalid (void) { int user_mode = -1; p11_dict *config; @@ -221,15 +221,15 @@ test_load_globals_system_sets_invalid (CuTest *tc) SRCDIR "/files/non-existant.conf", &user_mode); error = errno; - CuAssertPtrEquals (tc, NULL, config); - CuAssertIntEquals (tc, EINVAL, error); - CuAssertPtrNotNull (tc, p11_message_last ()); + assert_ptr_eq (NULL, config); + assert_num_eq (EINVAL, error); + assert_ptr_not_null (p11_message_last ()); p11_dict_free (config); } static void -test_load_globals_user_sets_invalid (CuTest *tc) +test_load_globals_user_sets_invalid (void) { int user_mode = -1; p11_dict *config; @@ -241,9 +241,9 @@ test_load_globals_user_sets_invalid (CuTest *tc) SRCDIR "/files/test-user-invalid.conf", &user_mode); error = errno; - CuAssertPtrEquals (tc, NULL, config); - CuAssertIntEquals (tc, EINVAL, error); - CuAssertPtrNotNull (tc, p11_message_last ()); + assert_ptr_eq (NULL, config); + assert_num_eq (EINVAL, error); + assert_ptr_not_null (p11_message_last ()); p11_dict_free (config); } @@ -256,7 +256,7 @@ assert_msg_contains (const char *msg, } static void -test_load_modules_merge (CuTest *tc) +test_load_modules_merge (void) { p11_dict *configs; p11_dict *config; @@ -267,29 +267,29 @@ test_load_modules_merge (CuTest *tc) SRCDIR "/files/package-modules", SRCDIR "/files/system-modules", SRCDIR "/files/user-modules"); - CuAssertPtrNotNull (tc, configs); - CuAssertTrue (tc, assert_msg_contains (p11_message_last (), "invalid config filename")); + assert_ptr_not_null (configs); + assert (assert_msg_contains (p11_message_last (), "invalid config filename")); config = p11_dict_get (configs, "one"); - CuAssertPtrNotNull (tc, config); - CuAssertStrEquals (tc, "mock-one.so", p11_dict_get (config, "module")); - CuAssertStrEquals (tc, p11_dict_get (config, "setting"), "user1"); + assert_ptr_not_null (config); + assert_str_eq ("mock-one.so", p11_dict_get (config, "module")); + assert_str_eq (p11_dict_get (config, "setting"), "user1"); config = p11_dict_get (configs, "two.badname"); - CuAssertPtrNotNull (tc, config); - CuAssertStrEquals (tc, "mock-two.so", p11_dict_get (config, "module")); - CuAssertStrEquals (tc, p11_dict_get (config, "setting"), "system2"); + assert_ptr_not_null (config); + assert_str_eq ("mock-two.so", p11_dict_get (config, "module")); + assert_str_eq (p11_dict_get (config, "setting"), "system2"); config = p11_dict_get (configs, "three"); - CuAssertPtrNotNull (tc, config); - CuAssertStrEquals (tc, "mock-three.so", p11_dict_get (config, "module")); - CuAssertStrEquals (tc, p11_dict_get (config, "setting"), "user3"); + assert_ptr_not_null (config); + assert_str_eq ("mock-three.so", p11_dict_get (config, "module")); + assert_str_eq (p11_dict_get (config, "setting"), "user3"); p11_dict_free (configs); } static void -test_load_modules_user_none (CuTest *tc) +test_load_modules_user_none (void) { p11_dict *configs; p11_dict *config; @@ -300,27 +300,27 @@ test_load_modules_user_none (CuTest *tc) SRCDIR "/files/package-modules", SRCDIR "/files/system-modules", SRCDIR "/files/user-modules"); - CuAssertPtrNotNull (tc, configs); - CuAssertTrue (tc, assert_msg_contains (p11_message_last (), "invalid config filename")); + assert_ptr_not_null (configs); + assert (assert_msg_contains (p11_message_last (), "invalid config filename")); config = p11_dict_get (configs, "one"); - CuAssertPtrNotNull (tc, config); - CuAssertStrEquals (tc, "mock-one.so", p11_dict_get (config, "module")); - CuAssertStrEquals (tc, p11_dict_get (config, "setting"), "system1"); + assert_ptr_not_null (config); + assert_str_eq ("mock-one.so", p11_dict_get (config, "module")); + assert_str_eq (p11_dict_get (config, "setting"), "system1"); config = p11_dict_get (configs, "two.badname"); - CuAssertPtrNotNull (tc, config); - CuAssertStrEquals (tc, "mock-two.so", p11_dict_get (config, "module")); - CuAssertStrEquals (tc, p11_dict_get (config, "setting"), "system2"); + assert_ptr_not_null (config); + assert_str_eq ("mock-two.so", p11_dict_get (config, "module")); + assert_str_eq (p11_dict_get (config, "setting"), "system2"); config = p11_dict_get (configs, "three"); - CuAssertPtrEquals (tc, NULL, config); + assert_ptr_eq (NULL, config); p11_dict_free (configs); } static void -test_load_modules_user_only (CuTest *tc) +test_load_modules_user_only (void) { p11_dict *configs; p11_dict *config; @@ -331,27 +331,27 @@ test_load_modules_user_only (CuTest *tc) SRCDIR "/files/package-modules", SRCDIR "/files/system-modules", SRCDIR "/files/user-modules"); - CuAssertPtrNotNull (tc, configs); - CuAssertPtrEquals (tc, NULL, (void *)p11_message_last ()); + assert_ptr_not_null (configs); + assert_ptr_eq (NULL, (void *)p11_message_last ()); config = p11_dict_get (configs, "one"); - CuAssertPtrNotNull (tc, config); - CuAssertStrEquals (tc, p11_dict_get (config, "module"), NULL); - CuAssertStrEquals (tc, p11_dict_get (config, "setting"), "user1"); + assert_ptr_not_null (config); + assert (p11_dict_get (config, "module") == NULL); + assert_str_eq (p11_dict_get (config, "setting"), "user1"); config = p11_dict_get (configs, "two.badname"); - CuAssertPtrEquals (tc, NULL, config); + assert_ptr_eq (NULL, config); config = p11_dict_get (configs, "three"); - CuAssertPtrNotNull (tc, config); - CuAssertStrEquals (tc, "mock-three.so", p11_dict_get (config, "module")); - CuAssertStrEquals (tc, p11_dict_get (config, "setting"), "user3"); + assert_ptr_not_null (config); + assert_str_eq ("mock-three.so", p11_dict_get (config, "module")); + assert_str_eq (p11_dict_get (config, "setting"), "user3"); p11_dict_free (configs); } static void -test_load_modules_no_user (CuTest *tc) +test_load_modules_no_user (void) { p11_dict *configs; p11_dict *config; @@ -362,67 +362,53 @@ test_load_modules_no_user (CuTest *tc) SRCDIR "/files/package-modules", SRCDIR "/files/system-modules", SRCDIR "/files/non-existant"); - CuAssertPtrNotNull (tc, configs); - CuAssertTrue (tc, assert_msg_contains (p11_message_last (), "invalid config filename")); + assert_ptr_not_null (configs); + assert (assert_msg_contains (p11_message_last (), "invalid config filename")); config = p11_dict_get (configs, "one"); - CuAssertPtrNotNull (tc, config); - CuAssertStrEquals (tc, "mock-one.so", p11_dict_get (config, "module")); - CuAssertStrEquals (tc, p11_dict_get (config, "setting"), "system1"); + assert_ptr_not_null (config); + assert_str_eq ("mock-one.so", p11_dict_get (config, "module")); + assert_str_eq (p11_dict_get (config, "setting"), "system1"); config = p11_dict_get (configs, "two.badname"); - CuAssertPtrNotNull (tc, config); - CuAssertStrEquals (tc, "mock-two.so", p11_dict_get (config, "module")); - CuAssertStrEquals (tc, p11_dict_get (config, "setting"), "system2"); + assert_ptr_not_null (config); + assert_str_eq ("mock-two.so", p11_dict_get (config, "module")); + assert_str_eq (p11_dict_get (config, "setting"), "system2"); config = p11_dict_get (configs, "three"); - CuAssertPtrEquals (tc, NULL, config); + assert_ptr_eq (NULL, config); p11_dict_free (configs); } static void -test_parse_boolean (CuTest *tc) +test_parse_boolean (void) { p11_message_quiet (); - CuAssertIntEquals (tc, true, _p11_conf_parse_boolean ("yes", false)); - CuAssertIntEquals (tc, false, _p11_conf_parse_boolean ("no", true)); - CuAssertIntEquals (tc, true, _p11_conf_parse_boolean ("!!!", true)); + assert_num_eq (true, _p11_conf_parse_boolean ("yes", false)); + assert_num_eq (false, _p11_conf_parse_boolean ("no", true)); + assert_num_eq (true, _p11_conf_parse_boolean ("!!!", true)); } int -main (void) +main (int argc, + char *argv[]) { - CuString *output = CuStringNew (); - CuSuite* suite = CuSuiteNew (); - int ret; - - putenv ("P11_KIT_STRICT=1"); - p11_debug_init (); - - SUITE_ADD_TEST (suite, test_parse_conf_1); - SUITE_ADD_TEST (suite, test_parse_ignore_missing); - SUITE_ADD_TEST (suite, test_parse_fail_missing); - SUITE_ADD_TEST (suite, test_merge_defaults); - SUITE_ADD_TEST (suite, test_load_globals_merge); - SUITE_ADD_TEST (suite, test_load_globals_no_user); - SUITE_ADD_TEST (suite, test_load_globals_system_sets_only); - SUITE_ADD_TEST (suite, test_load_globals_user_sets_only); - SUITE_ADD_TEST (suite, test_load_globals_system_sets_invalid); - SUITE_ADD_TEST (suite, test_load_globals_user_sets_invalid); - SUITE_ADD_TEST (suite, test_load_modules_merge); - SUITE_ADD_TEST (suite, test_load_modules_no_user); - SUITE_ADD_TEST (suite, test_load_modules_user_only); - SUITE_ADD_TEST (suite, test_load_modules_user_none); - SUITE_ADD_TEST (suite, test_parse_boolean); - - CuSuiteRun (suite); - CuSuiteSummary (suite, output); - CuSuiteDetails (suite, output); - printf ("%s\n", output->buffer); - ret = suite->failCount; - CuSuiteDelete (suite); - CuStringDelete (output); - return ret; + p11_test (test_parse_conf_1, "/conf/test_parse_conf_1"); + p11_test (test_parse_ignore_missing, "/conf/test_parse_ignore_missing"); + p11_test (test_parse_fail_missing, "/conf/test_parse_fail_missing"); + p11_test (test_merge_defaults, "/conf/test_merge_defaults"); + p11_test (test_load_globals_merge, "/conf/test_load_globals_merge"); + p11_test (test_load_globals_no_user, "/conf/test_load_globals_no_user"); + p11_test (test_load_globals_system_sets_only, "/conf/test_load_globals_system_sets_only"); + p11_test (test_load_globals_user_sets_only, "/conf/test_load_globals_user_sets_only"); + p11_test (test_load_globals_system_sets_invalid, "/conf/test_load_globals_system_sets_invalid"); + p11_test (test_load_globals_user_sets_invalid, "/conf/test_load_globals_user_sets_invalid"); + p11_test (test_load_modules_merge, "/conf/test_load_modules_merge"); + p11_test (test_load_modules_no_user, "/conf/test_load_modules_no_user"); + p11_test (test_load_modules_user_only, "/conf/test_load_modules_user_only"); + p11_test (test_load_modules_user_none, "/conf/test_load_modules_user_none"); + p11_test (test_parse_boolean, "/conf/test_parse_boolean"); + return p11_test_run (argc, argv); } diff --git a/p11-kit/tests/test-deprecated.c b/p11-kit/tests/test-deprecated.c new file mode 100644 index 0000000..7ea8260 --- /dev/null +++ b/p11-kit/tests/test-deprecated.c @@ -0,0 +1,508 @@ +/* + * Copyright (c) 2011, Collabora Ltd. + * Copyright (c) 2012 Red Hat Inc + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * * The names of contributors to this software may not be + * used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * Author: Stef Walter <stefw@redhat.com> + */ + +#define P11_KIT_NO_DEPRECATIONS + +#include "config.h" +#include "test.h" + +#include "dict.h" +#include "library.h" +#include "p11-kit.h" +#include "private.h" +#include "mock.h" + +#include <sys/types.h> + +#include <assert.h> +#include <errno.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <time.h> +#include <unistd.h> + +static CK_FUNCTION_LIST_PTR_PTR +initialize_and_get_modules (void) +{ + CK_FUNCTION_LIST_PTR_PTR modules; + CK_RV rv; + + rv = p11_kit_initialize_registered (); + assert_num_eq (CKR_OK, rv); + modules = p11_kit_registered_modules (); + assert (modules != NULL && modules[0] != NULL); + + return modules; +} + +static void +finalize_and_free_modules (CK_FUNCTION_LIST_PTR_PTR modules) +{ + CK_RV rv; + + free (modules); + rv = p11_kit_finalize_registered (); + assert_num_eq (CKR_OK, rv); + +} + +static void +test_no_duplicates (void) +{ + CK_FUNCTION_LIST_PTR_PTR modules; + p11_dict *paths; + p11_dict *funcs; + char *path; + int i; + + modules = initialize_and_get_modules (); + paths = p11_dict_new (p11_dict_str_hash, p11_dict_str_equal, NULL, NULL); + funcs = p11_dict_new (p11_dict_direct_hash, p11_dict_direct_equal, NULL, NULL); + + /* The loaded modules should not contain duplicates */ + for (i = 0; modules[i] != NULL; i++) { + path = p11_kit_registered_option (modules[i], "module"); + + if (p11_dict_get (funcs, modules[i])) + assert_fail ("found duplicate function list pointer", NULL); + if (p11_dict_get (paths, path)) + assert_fail ("found duplicate path name", NULL); + + if (!p11_dict_set (funcs, modules[i], "")) + assert_not_reached (); + if (!p11_dict_set (paths, path, "")) + assert_not_reached (); + + free (path); + } + + p11_dict_free (paths); + p11_dict_free (funcs); + finalize_and_free_modules (modules); +} + +static CK_FUNCTION_LIST_PTR +lookup_module_with_name (CK_FUNCTION_LIST_PTR_PTR modules, + const char *name) +{ + CK_FUNCTION_LIST_PTR match = NULL; + CK_FUNCTION_LIST_PTR module; + char *module_name; + int i; + + for (i = 0; match == NULL && modules[i] != NULL; i++) { + module_name = p11_kit_registered_module_to_name (modules[i]); + assert_ptr_not_null (module_name); + if (strcmp (module_name, name) == 0) + match = modules[i]; + free (module_name); + } + + /* + * As a side effect, we should check that the results of this function + * matches the above search. + */ + module = p11_kit_registered_name_to_module (name); + if (module != match) + assert_fail ("different result from p11_kit_registered_name_to_module()", NULL); + + return match; +} + +static void +test_disable (void) +{ + CK_FUNCTION_LIST_PTR_PTR modules; + + /* + * The module four should be present, as we don't match any prognames + * that it has disabled. + */ + + modules = initialize_and_get_modules (); + assert (lookup_module_with_name (modules, "four") != NULL); + finalize_and_free_modules (modules); + + /* + * The module two shouldn't have been loaded, because in its config + * file we have: + * + * disable-in: test-disable + */ + + p11_kit_set_progname ("test-disable"); + + modules = initialize_and_get_modules (); + assert (lookup_module_with_name (modules, "four") == NULL); + finalize_and_free_modules (modules); + + p11_kit_set_progname (NULL); +} + +static void +test_disable_later (void) +{ + CK_FUNCTION_LIST_PTR_PTR modules; + CK_RV rv; + + /* + * The module two shouldn't be matched, because in its config + * file we have: + * + * disable-in: test-disable + */ + + rv = p11_kit_initialize_registered (); + assert_num_eq (CKR_OK, rv); + + p11_kit_set_progname ("test-disable"); + + modules = p11_kit_registered_modules (); + assert (modules != NULL && modules[0] != NULL); + + assert (lookup_module_with_name (modules, "two") == NULL); + finalize_and_free_modules (modules); + + p11_kit_set_progname (NULL); +} + +static void +test_enable (void) +{ + CK_FUNCTION_LIST_PTR_PTR modules; + + /* + * The module three should not be present, as we don't match the current + * program. + */ + + modules = initialize_and_get_modules (); + assert (lookup_module_with_name (modules, "three") == NULL); + finalize_and_free_modules (modules); + + /* + * The module three should be loaded here , because in its config + * file we have: + * + * enable-in: test-enable + */ + + p11_kit_set_progname ("test-enable"); + + modules = initialize_and_get_modules (); + assert (lookup_module_with_name (modules, "three") != NULL); + finalize_and_free_modules (modules); + + p11_kit_set_progname (NULL); +} + +CK_FUNCTION_LIST module; + +#ifdef OS_UNIX + +#include <sys/wait.h> + +static CK_RV +mock_C_Initialize__with_fork (CK_VOID_PTR init_args) +{ + struct timespec ts = { 0, 100 * 1000 * 1000 }; + CK_RV rv; + pid_t child; + pid_t ret; + int status; + + rv = mock_C_Initialize (init_args); + assert (rv == CKR_OK); + + /* Fork during the initialization */ + child = fork (); + if (child == 0) { + nanosleep (&ts, NULL); + exit (66); + } + + ret = waitpid (child, &status, 0); + assert (ret == child); + assert (WIFEXITED (status)); + assert (WEXITSTATUS (status) == 66); + + return CKR_OK; +} + +static void +test_fork_initialization (void) +{ + CK_RV rv; + + assert (!mock_module_initialized ()); + + /* Build up our own function list */ + memcpy (&module, &mock_module_no_slots, sizeof (CK_FUNCTION_LIST)); + module.C_Initialize = mock_C_Initialize__with_fork; + + rv = p11_kit_initialize_module (&module); + assert (rv == CKR_OK); + + rv = p11_kit_finalize_module (&module); + assert (rv == CKR_OK); + + assert (!mock_module_initialized ()); +} + +#endif /* OS_UNIX */ + +static CK_RV +mock_C_Initialize__with_recursive (CK_VOID_PTR init_args) +{ + /* Recursively initialize, this is broken */ + return p11_kit_initialize_module (&module); +} + +static void +test_recursive_initialization (void) +{ + CK_RV rv; + + assert (!mock_module_initialized ()); + + /* Build up our own function list */ + memcpy (&module, &mock_module_no_slots, sizeof (CK_FUNCTION_LIST)); + module.C_Initialize = mock_C_Initialize__with_recursive; + + rv = p11_kit_initialize_module (&module); + assert (rv == CKR_FUNCTION_FAILED); + + assert (!mock_module_initialized ()); +} + +static p11_mutex_t race_mutex; +static int initialization_count = 0; +static int finalization_count = 0; + +static CK_RV +mock_C_Initialize__threaded_race (CK_VOID_PTR init_args) +{ + /* Atomically increment value */ + p11_mutex_lock (&race_mutex); + initialization_count += 1; + p11_mutex_unlock (&race_mutex); + + p11_sleep_ms (100); + return CKR_OK; +} + +static CK_RV +mock_C_Finalize__threaded_race (CK_VOID_PTR reserved) +{ + /* Atomically increment value */ + p11_mutex_lock (&race_mutex); + finalization_count += 1; + p11_mutex_unlock (&race_mutex); + + p11_sleep_ms (100); + return CKR_OK; +} + +static void * +initialization_thread (void *data) +{ + CK_RV rv; + + assert_str_eq (data, "thread-data"); + rv = p11_kit_initialize_module (&module); + assert (rv == CKR_OK); + + return "thread-data"; +} + +static void * +finalization_thread (void *data) +{ + CK_RV rv; + + assert_str_eq (data, "thread-data"); + rv = p11_kit_finalize_module (&module); + assert (rv == CKR_OK); + + return "thread-data"; +} + +static void +test_threaded_initialization (void) +{ + static const int num_threads = 2; + p11_thread_t threads[num_threads]; + int ret; + int i; + + assert (!mock_module_initialized ()); + + /* Build up our own function list */ + memcpy (&module, &mock_module_no_slots, sizeof (CK_FUNCTION_LIST)); + module.C_Initialize = mock_C_Initialize__threaded_race; + module.C_Finalize = mock_C_Finalize__threaded_race; + + initialization_count = 0; + finalization_count = 0; + + for (i = 0; i < num_threads; i++) { + ret = p11_thread_create (&threads[i], initialization_thread, "thread-data"); + assert_num_eq (0, ret); + assert (threads[i] != 0); + } + + for (i = 0; i < num_threads; i++) { + ret = p11_thread_join (threads[i]); + assert_num_eq (0, ret); + threads[i] = 0; + } + + for (i = 0; i < num_threads; i++) { + ret = p11_thread_create (&threads[i], finalization_thread, "thread-data"); + assert_num_eq (0, ret); + assert (threads[i] != 0); + } + + for (i = 0; i < num_threads; i++) { + ret = p11_thread_join (threads[i]); + assert_num_eq (0, ret); + threads[i] = 0; + } + + /* C_Initialize should have been called exactly once */ + assert_num_eq (1, initialization_count); + assert_num_eq (1, finalization_count); + + assert (!mock_module_initialized ()); +} + +static CK_RV +mock_C_Initialize__test_mutexes (CK_VOID_PTR args) +{ + CK_C_INITIALIZE_ARGS_PTR init_args; + void *mutex = NULL; + CK_RV rv; + + rv = mock_C_Initialize (NULL); + if (rv != CKR_OK) + return rv; + + assert (args != NULL); + init_args = args; + + rv = (init_args->CreateMutex) (&mutex); + assert (rv == CKR_OK); + + rv = (init_args->LockMutex) (mutex); + assert (rv == CKR_OK); + + rv = (init_args->UnlockMutex) (mutex); + assert (rv == CKR_OK); + + rv = (init_args->DestroyMutex) (mutex); + assert (rv == CKR_OK); + + return CKR_OK; +} + +static void +test_mutexes (void) +{ + CK_RV rv; + + assert (!mock_module_initialized ()); + + /* Build up our own function list */ + memcpy (&module, &mock_module_no_slots, sizeof (CK_FUNCTION_LIST)); + module.C_Initialize = mock_C_Initialize__test_mutexes; + + rv = p11_kit_initialize_module (&module); + assert (rv == CKR_OK); + + rv = p11_kit_finalize_module (&module); + assert (rv == CKR_OK); + + assert (!mock_module_initialized ()); +} + +static void +test_load_and_initialize (void) +{ + CK_FUNCTION_LIST_PTR module; + CK_INFO info; + CK_RV rv; + int ret; + + rv = p11_kit_load_initialize_module (BUILDDIR "/.libs/mock-one" SHLEXT, &module); + assert (rv == CKR_OK); + assert (module != NULL); + + rv = (module->C_GetInfo) (&info); + assert (rv == CKR_OK); + + ret = memcmp (info.manufacturerID, "MOCK MANUFACTURER ", 32); + assert (ret == 0); + + rv = p11_kit_finalize_module (module); + assert (ret == CKR_OK); +} + +int +main (int argc, + char *argv[]) +{ + p11_mutex_init (&race_mutex); + mock_module_init (); + p11_library_init (); + + p11_test (test_no_duplicates, "/deprecated/test_no_duplicates"); + p11_test (test_disable, "/deprecated/test_disable"); + p11_test (test_disable_later, "/deprecated/test_disable_later"); + p11_test (test_enable, "/deprecated/test_enable"); + +#ifdef OS_UNIX + p11_test (test_fork_initialization, "/deprecated/test_fork_initialization"); +#endif + + p11_test (test_recursive_initialization, "/deprecated/test_recursive_initialization"); + p11_test (test_threaded_initialization, "/deprecated/test_threaded_initialization"); + p11_test (test_mutexes, "/deprecated/test_mutexes"); + p11_test (test_load_and_initialize, "/deprecated/test_load_and_initialize"); + + p11_kit_be_quiet (); + + return p11_test_run (argc, argv); +} diff --git a/p11-kit/tests/test-init.c b/p11-kit/tests/test-init.c index 7df4be9..76805ee 100644 --- a/p11-kit/tests/test-init.c +++ b/p11-kit/tests/test-init.c @@ -33,11 +33,16 @@ */ #include "config.h" -#include "CuTest.h" +#include "test.h" #include <sys/types.h> #include "library.h" +#include "mock.h" +#include "modules.h" +#include "p11-kit.h" +#include "private.h" +#include "virtual.h" #include <assert.h> #include <stdio.h> @@ -46,11 +51,8 @@ #include <time.h> #include <unistd.h> -#include "p11-kit/p11-kit.h" - -#include "mock.h" - -CK_FUNCTION_LIST module; +static CK_FUNCTION_LIST module; +static p11_mutex_t race_mutex; #ifdef OS_UNIX @@ -84,23 +86,42 @@ mock_C_Initialize__with_fork (CK_VOID_PTR init_args) } static void -test_fork_initialization (CuTest *tc) +test_fork_initialization (void) { + CK_FUNCTION_LIST_PTR result; CK_RV rv; + mock_module_reset (); + /* Build up our own function list */ memcpy (&module, &mock_module_no_slots, sizeof (CK_FUNCTION_LIST)); module.C_Initialize = mock_C_Initialize__with_fork; - rv = p11_kit_initialize_module (&module); - CuAssertTrue (tc, rv == CKR_OK); + p11_lock (); + + rv = p11_module_load_inlock_reentrant (&module, 0, &result); + assert (rv == CKR_OK); + + p11_unlock (); + + rv = p11_kit_module_initialize (result); + assert (rv == CKR_OK); + + rv = p11_kit_module_finalize (result); + assert (rv == CKR_OK); - rv = p11_kit_finalize_module (&module); - CuAssertTrue (tc, rv == CKR_OK); + p11_lock (); + + rv = p11_module_release_inlock_reentrant (result); + assert (rv == CKR_OK); + + p11_unlock (); } #endif /* OS_UNIX */ +static CK_FUNCTION_LIST *recursive_managed; + static CK_RV mock_C_Initialize__with_recursive (CK_VOID_PTR init_args) { @@ -109,12 +130,11 @@ mock_C_Initialize__with_recursive (CK_VOID_PTR init_args) rv = mock_C_Initialize (init_args); assert (rv == CKR_OK); - /* Recursively initialize, this is broken */ - return p11_kit_initialize_module (&module); + return p11_kit_module_initialize (recursive_managed); } static void -test_recursive_initialization (CuTest *tc) +test_recursive_initialization (void) { CK_RV rv; @@ -122,16 +142,31 @@ test_recursive_initialization (CuTest *tc) memcpy (&module, &mock_module_no_slots, sizeof (CK_FUNCTION_LIST)); module.C_Initialize = mock_C_Initialize__with_recursive; - rv = p11_kit_initialize_module (&module); - CuAssertTrue (tc, rv == CKR_FUNCTION_FAILED); + p11_kit_be_quiet (); + + p11_lock (); + + rv = p11_module_load_inlock_reentrant (&module, 0, &recursive_managed); + assert (rv == CKR_OK); + + p11_unlock (); + + rv = p11_kit_module_initialize (recursive_managed); + assert_num_eq (CKR_FUNCTION_FAILED, rv); + + p11_lock (); + + rv = p11_module_release_inlock_reentrant (recursive_managed); + assert (rv == CKR_OK); + + p11_unlock (); + + p11_kit_be_loud (); } -static p11_mutex_t race_mutex; static int initialization_count = 0; static int finalization_count = 0; -#include "private.h" - static CK_RV mock_C_Initialize__threaded_race (CK_VOID_PTR init_args) { @@ -159,32 +194,36 @@ mock_C_Finalize__threaded_race (CK_VOID_PTR reserved) static void * initialization_thread (void *data) { - CuTest *tc = data; + CK_FUNCTION_LIST *module = data; CK_RV rv; - rv = p11_kit_initialize_module (&module); - CuAssertTrue (tc, rv == CKR_OK); + assert (module != NULL); + rv = p11_kit_module_initialize (module); + assert_num_eq (rv, CKR_OK); - return tc; + return module; } static void * finalization_thread (void *data) { - CuTest *tc = data; + CK_FUNCTION_LIST *module = data; CK_RV rv; - rv = p11_kit_finalize_module (&module); - CuAssertTrue (tc, rv == CKR_OK); + assert (module != NULL); + rv = p11_kit_module_finalize (module); + assert_num_eq (rv, CKR_OK); - return tc; + return module; } static void -test_threaded_initialization (CuTest *tc) +test_threaded_initialization (void) { - static const int num_threads = 2; + static const int num_threads = 1; + CK_FUNCTION_LIST *data[num_threads]; p11_thread_t threads[num_threads]; + CK_RV rv; int ret; int i; @@ -193,36 +232,57 @@ test_threaded_initialization (CuTest *tc) module.C_Initialize = mock_C_Initialize__threaded_race; module.C_Finalize = mock_C_Finalize__threaded_race; + memset (&data, 0, sizeof (data)); initialization_count = 0; finalization_count = 0; + p11_lock (); + + for (i = 0; i < num_threads; i++) { + assert (data[i] == NULL); + rv = p11_module_load_inlock_reentrant (&module, 0, &data[i]); + assert (rv == CKR_OK); + } + + p11_unlock (); + for (i = 0; i < num_threads; i++) { - ret = p11_thread_create (&threads[i], initialization_thread, tc); - CuAssertIntEquals (tc, 0, ret); - CuAssertTrue (tc, threads[i] != 0); + ret = p11_thread_create (&threads[i], initialization_thread, data[i]); + assert_num_eq (0, ret); + assert (threads[i] != 0); } for (i = 0; i < num_threads; i++) { ret = p11_thread_join (threads[i]); - CuAssertIntEquals (tc, 0, ret); + assert_num_eq (0, ret); threads[i] = 0; } for (i = 0; i < num_threads; i++) { - ret = p11_thread_create (&threads[i], finalization_thread, tc); - CuAssertIntEquals (tc, 0, ret); - CuAssertTrue (tc, threads[i] != 0); + ret = p11_thread_create (&threads[i], finalization_thread, data[i]); + assert_num_eq (0, ret); + assert (threads[i] != 0); } for (i = 0; i < num_threads; i++) { ret = p11_thread_join (threads[i]); - CuAssertIntEquals (tc, 0, ret); + assert_num_eq (0, ret); threads[i] = 0; } + p11_lock (); + + for (i = 0; i < num_threads; i++) { + assert (data[i] != NULL); + rv = p11_module_release_inlock_reentrant (data[i]); + assert (rv == CKR_OK); + } + + p11_unlock (); + /* C_Initialize should have been called exactly once */ - CuAssertIntEquals (tc, 1, initialization_count); - CuAssertIntEquals (tc, 1, finalization_count); + assert_num_eq (1, initialization_count); + assert_num_eq (1, finalization_count); } static CK_RV @@ -251,71 +311,106 @@ mock_C_Initialize__test_mutexes (CK_VOID_PTR args) } static void -test_mutexes (CuTest *tc) +test_mutexes (void) { + CK_FUNCTION_LIST_PTR result; CK_RV rv; /* Build up our own function list */ memcpy (&module, &mock_module_no_slots, sizeof (CK_FUNCTION_LIST)); module.C_Initialize = mock_C_Initialize__test_mutexes; - rv = p11_kit_initialize_module (&module); - CuAssertTrue (tc, rv == CKR_OK); + p11_lock (); + + rv = p11_module_load_inlock_reentrant (&module, 0, &result); + assert (rv == CKR_OK); + + rv = p11_module_release_inlock_reentrant (result); + assert (rv == CKR_OK); - rv = p11_kit_finalize_module (&module); - CuAssertTrue (tc, rv == CKR_OK); + p11_unlock (); } static void -test_load_and_initialize (CuTest *tc) +test_load_and_initialize (void) { CK_FUNCTION_LIST_PTR module; CK_INFO info; CK_RV rv; int ret; - rv = p11_kit_load_initialize_module (BUILDDIR "/.libs/mock-one" SHLEXT, &module); - CuAssertTrue (tc, rv == CKR_OK); - CuAssertTrue (tc, module != NULL); + module = p11_kit_module_load (BUILDDIR "/.libs/mock-one" SHLEXT, 0); + assert (module != NULL); + + rv = p11_kit_module_initialize (module); + assert (rv == CKR_OK); rv = (module->C_GetInfo) (&info); - CuAssertTrue (tc, rv == CKR_OK); + assert (rv == CKR_OK); ret = memcmp (info.manufacturerID, "MOCK MANUFACTURER ", 32); - CuAssertTrue (tc, ret == 0); + assert (ret == 0); - rv = p11_kit_finalize_module (module); - CuAssertTrue (tc, ret == CKR_OK); + rv = p11_kit_module_finalize (module); + assert (rv == CKR_OK); + + p11_kit_module_release (module); } -int -main (void) +static void +test_initalize_fail (void) +{ + CK_FUNCTION_LIST failer; + CK_FUNCTION_LIST *modules[3] = { &mock_module_no_slots, &failer, NULL }; + CK_RV rv; + + memcpy (&failer, &mock_module, sizeof (CK_FUNCTION_LIST)); + failer.C_Initialize = mock_C_Initialize__fails; + + mock_module_reset (); + p11_kit_be_quiet (); + + rv = p11_kit_modules_initialize (modules, NULL); + assert_num_eq (CKR_FUNCTION_FAILED, rv); + + p11_kit_be_loud (); + + /* Failed modules get removed from the list */ + assert_ptr_eq (&mock_module_no_slots, modules[0]); + assert_ptr_eq (NULL, modules[1]); + assert_ptr_eq (NULL, modules[2]); + + p11_kit_modules_finalize (modules); +} + +static void +test_finalize_fail (void) { - CuString *output = CuStringNew (); - CuSuite* suite = CuSuiteNew (); - int ret; - putenv ("P11_KIT_STRICT=1"); +} + +int +main (int argc, + char *argv[]) +{ p11_mutex_init (&race_mutex); mock_module_init (); p11_library_init (); + /* These only work when managed */ + if (p11_virtual_can_wrap ()) { + p11_test (test_recursive_initialization, "/init/test_recursive_initialization"); + p11_test (test_threaded_initialization, "/init/test_threaded_initialization"); + p11_test (test_mutexes, "/init/test_mutexes"); + p11_test (test_load_and_initialize, "/init/test_load_and_initialize"); + #ifdef OS_UNIX - SUITE_ADD_TEST (suite, test_fork_initialization); + p11_test (test_fork_initialization, "/init/test_fork_initialization"); #endif + } - SUITE_ADD_TEST (suite, test_recursive_initialization); - SUITE_ADD_TEST (suite, test_threaded_initialization); - SUITE_ADD_TEST (suite, test_mutexes); - SUITE_ADD_TEST (suite, test_load_and_initialize); - - CuSuiteRun (suite); - CuSuiteSummary (suite, output); - CuSuiteDetails (suite, output); - printf ("%s\n", output->buffer); - ret = suite->failCount; - CuSuiteDelete (suite); - CuStringDelete (output); + p11_test (test_initalize_fail, "/init/test_initalize_fail"); + p11_test (test_finalize_fail, "/init/test_finalize_fail"); - return ret; + return p11_test_run (argc, argv); } diff --git a/p11-kit/tests/test-iter.c b/p11-kit/tests/test-iter.c index 08e43b3..18b5ed6 100644 --- a/p11-kit/tests/test-iter.c +++ b/p11-kit/tests/test-iter.c @@ -33,7 +33,7 @@ */ #include "config.h" -#include "CuTest.h" +#include "test.h" #define P11_KIT_FUTURE_UNSTABLE_API 1 @@ -49,17 +49,14 @@ #include <stdlib.h> static CK_FUNCTION_LIST_PTR_PTR -initialize_and_get_modules (CuTest *tc) +initialize_and_get_modules (void) { CK_FUNCTION_LIST_PTR_PTR modules; - CK_RV rv; p11_message_quiet (); - rv = p11_kit_initialize_registered (); - CuAssertIntEquals (tc, CKR_OK, rv); - modules = p11_kit_registered_modules (); - CuAssertTrue (tc, modules != NULL && modules[0] != NULL); + modules = p11_kit_modules_load_and_initialize (0); + assert (modules != NULL && modules[0] != NULL); p11_message_loud (); @@ -67,14 +64,10 @@ initialize_and_get_modules (CuTest *tc) } static void -finalize_and_free_modules (CuTest *tc, - CK_FUNCTION_LIST_PTR_PTR modules) +finalize_and_free_modules (CK_FUNCTION_LIST_PTR_PTR modules) { - CK_RV rv; - - free (modules); - rv = p11_kit_finalize_registered (); - CuAssertIntEquals (tc, CKR_OK, rv); + p11_kit_modules_finalize (modules); + p11_kit_modules_release (modules); } static int @@ -93,7 +86,7 @@ has_handle (CK_ULONG *objects, static void -test_all (CuTest *tc) +test_all (void) { CK_OBJECT_HANDLE objects[128]; CK_FUNCTION_LIST_PTR *modules; @@ -104,59 +97,60 @@ test_all (CuTest *tc) CK_RV rv; int at; - modules = initialize_and_get_modules (tc); + modules = initialize_and_get_modules (); iter = p11_kit_iter_new (NULL); p11_kit_iter_begin (iter, modules); at = 0; while ((rv = p11_kit_iter_next (iter)) == CKR_OK) { - CuAssertTrue (tc, at < 128); + assert (at < 128); objects[at] = p11_kit_iter_get_object (iter); module = p11_kit_iter_get_module (iter); - CuAssertPtrNotNull (tc, module); + assert_ptr_not_null (module); session = p11_kit_iter_get_session (iter); - CuAssertTrue (tc, session != 0); + assert (session != 0); /* Do something with the object */ size = 0; rv = (module->C_GetObjectSize) (session, objects[at], &size); - CuAssertTrue (tc, rv == CKR_OK); - CuAssertTrue (tc, size > 0); + assert (rv == CKR_OK); + assert (size > 0); at++; } - CuAssertTrue (tc, rv == CKR_CANCEL); + assert (rv == CKR_CANCEL); /* Three modules, each with 1 slot, and 3 public objects */ - CuAssertIntEquals (tc, 9, at); + assert_num_eq (9, at); - CuAssertTrue (tc, has_handle (objects, at, MOCK_DATA_OBJECT)); - CuAssertTrue (tc, !has_handle (objects, at, MOCK_PRIVATE_KEY_CAPITALIZE)); - CuAssertTrue (tc, has_handle (objects, at, MOCK_PUBLIC_KEY_CAPITALIZE)); - CuAssertTrue (tc, !has_handle (objects, at, MOCK_PRIVATE_KEY_PREFIX)); - CuAssertTrue (tc, has_handle (objects, at, MOCK_PUBLIC_KEY_PREFIX)); + assert (has_handle (objects, at, MOCK_DATA_OBJECT)); + assert (!has_handle (objects, at, MOCK_PRIVATE_KEY_CAPITALIZE)); + assert (has_handle (objects, at, MOCK_PUBLIC_KEY_CAPITALIZE)); + assert (!has_handle (objects, at, MOCK_PRIVATE_KEY_PREFIX)); + assert (has_handle (objects, at, MOCK_PUBLIC_KEY_PREFIX)); p11_kit_iter_free (iter); - finalize_and_free_modules (tc, modules); + finalize_and_free_modules (modules); } static CK_RV on_iter_callback (P11KitIter *iter, - CK_BBOOL *matches, - void *data) + CK_BBOOL *matches, + void *data) { - CuTest *tc = data; CK_OBJECT_HANDLE object; CK_FUNCTION_LIST_PTR module; CK_SESSION_HANDLE session; CK_ULONG size; CK_RV rv; + assert_str_eq (data, "callback"); + object = p11_kit_iter_get_object (iter); if (object != MOCK_PUBLIC_KEY_CAPITALIZE && object != MOCK_PUBLIC_KEY_PREFIX) { *matches = CK_FALSE; @@ -164,22 +158,22 @@ on_iter_callback (P11KitIter *iter, } module = p11_kit_iter_get_module (iter); - CuAssertPtrNotNull (tc, module); + assert_ptr_not_null (module); session = p11_kit_iter_get_session (iter); - CuAssertTrue (tc, session != 0); + assert (session != 0); /* Do something with the object */ size = 0; rv = (module->C_GetObjectSize) (session, object, &size); - CuAssertTrue (tc, rv == CKR_OK); - CuAssertTrue (tc, size > 0); + assert (rv == CKR_OK); + assert (size > 0); return CKR_OK; } static void -test_callback (CuTest *tc) +test_callback (void) { CK_OBJECT_HANDLE objects[128]; CK_FUNCTION_LIST_PTR *modules; @@ -187,33 +181,33 @@ test_callback (CuTest *tc) CK_RV rv; int at; - modules = initialize_and_get_modules (tc); + modules = initialize_and_get_modules (); iter = p11_kit_iter_new (NULL); - p11_kit_iter_add_callback (iter, on_iter_callback, tc, NULL); + p11_kit_iter_add_callback (iter, on_iter_callback, "callback", NULL); p11_kit_iter_begin (iter, modules); at= 0; while ((rv = p11_kit_iter_next (iter)) == CKR_OK) { - CuAssertTrue (tc, at < 128); + assert (at < 128); objects[at] = p11_kit_iter_get_object (iter); at++; } - CuAssertTrue (tc, rv == CKR_CANCEL); + assert (rv == CKR_CANCEL); /* Three modules, each with 1 slot, and 2 public keys */ - CuAssertIntEquals (tc, 6, at); + assert_num_eq (6, at); - CuAssertTrue (tc, !has_handle (objects, at, MOCK_DATA_OBJECT)); - CuAssertTrue (tc, !has_handle (objects, at, MOCK_PRIVATE_KEY_CAPITALIZE)); - CuAssertTrue (tc, has_handle (objects, at, MOCK_PUBLIC_KEY_CAPITALIZE)); - CuAssertTrue (tc, !has_handle (objects, at, MOCK_PRIVATE_KEY_PREFIX)); - CuAssertTrue (tc, has_handle (objects, at, MOCK_PUBLIC_KEY_PREFIX)); + assert (!has_handle (objects, at, MOCK_DATA_OBJECT)); + assert (!has_handle (objects, at, MOCK_PRIVATE_KEY_CAPITALIZE)); + assert (has_handle (objects, at, MOCK_PUBLIC_KEY_CAPITALIZE)); + assert (!has_handle (objects, at, MOCK_PRIVATE_KEY_PREFIX)); + assert (has_handle (objects, at, MOCK_PUBLIC_KEY_PREFIX)); p11_kit_iter_free (iter); - finalize_and_free_modules (tc, modules); + finalize_and_free_modules (modules); } static CK_RV @@ -225,30 +219,30 @@ on_callback_fail (P11KitIter *iter, } static void -test_callback_fails (CuTest *tc) +test_callback_fails (void) { CK_FUNCTION_LIST_PTR *modules; P11KitIter *iter; CK_RV rv; int at; - modules = initialize_and_get_modules (tc); + modules = initialize_and_get_modules (); iter = p11_kit_iter_new (NULL); - p11_kit_iter_add_callback (iter, on_callback_fail, tc, NULL); + p11_kit_iter_add_callback (iter, on_callback_fail, "callback", NULL); p11_kit_iter_begin (iter, modules); at= 0; while ((rv = p11_kit_iter_next (iter)) == CKR_OK) at++; - CuAssertTrue (tc, rv == CKR_DATA_INVALID); + assert (rv == CKR_DATA_INVALID); /* Shouldn't have succeeded at all */ - CuAssertIntEquals (tc, 0, at); + assert_num_eq (0, at); p11_kit_iter_free (iter); - finalize_and_free_modules (tc, modules); + finalize_and_free_modules (modules); } static void @@ -259,7 +253,7 @@ on_destroy_increment (void *data) } static void -test_callback_destroyer (CuTest *tc) +test_callback_destroyer (void) { P11KitIter *iter; int value = 1; @@ -268,11 +262,11 @@ test_callback_destroyer (CuTest *tc) p11_kit_iter_add_callback (iter, on_callback_fail, &value, on_destroy_increment); p11_kit_iter_free (iter); - CuAssertIntEquals (tc, 2, value); + assert_num_eq (2, value); } static void -test_with_session (CuTest *tc) +test_with_session (void) { CK_OBJECT_HANDLE objects[128]; CK_SESSION_HANDLE session; @@ -282,53 +276,54 @@ test_with_session (CuTest *tc) CK_RV rv; int at; - rv = p11_kit_initialize_module (&mock_module); - CuAssertTrue (tc, rv == CKR_OK); + mock_module_reset (); + rv = mock_module.C_Initialize (NULL); + assert (rv == CKR_OK); rv = mock_C_OpenSession (MOCK_SLOT_ONE_ID, CKF_SERIAL_SESSION, NULL, NULL, &session); - CuAssertTrue (tc, rv == CKR_OK); + assert (rv == CKR_OK); iter = p11_kit_iter_new (NULL); p11_kit_iter_begin_with (iter, &mock_module, 0, session); at= 0; while ((rv = p11_kit_iter_next (iter)) == CKR_OK) { - CuAssertTrue (tc, at < 128); + assert (at < 128); objects[at] = p11_kit_iter_get_object (iter); slot = p11_kit_iter_get_slot (iter); - CuAssertTrue (tc, slot == MOCK_SLOT_ONE_ID); + assert (slot == MOCK_SLOT_ONE_ID); module = p11_kit_iter_get_module (iter); - CuAssertPtrEquals (tc, module, &mock_module); + assert_ptr_eq (module, &mock_module); - CuAssertTrue (tc, session == p11_kit_iter_get_session (iter)); + assert (session == p11_kit_iter_get_session (iter)); at++; } - CuAssertTrue (tc, rv == CKR_CANCEL); + assert (rv == CKR_CANCEL); /* 1 modules, each with 1 slot, and 3 public objects */ - CuAssertIntEquals (tc, 3, at); + assert_num_eq (3, at); - CuAssertTrue (tc, has_handle (objects, at, MOCK_DATA_OBJECT)); - CuAssertTrue (tc, !has_handle (objects, at, MOCK_PRIVATE_KEY_CAPITALIZE)); - CuAssertTrue (tc, has_handle (objects, at, MOCK_PUBLIC_KEY_CAPITALIZE)); - CuAssertTrue (tc, !has_handle (objects, at, MOCK_PRIVATE_KEY_PREFIX)); - CuAssertTrue (tc, has_handle (objects, at, MOCK_PUBLIC_KEY_PREFIX)); + assert (has_handle (objects, at, MOCK_DATA_OBJECT)); + assert (!has_handle (objects, at, MOCK_PRIVATE_KEY_CAPITALIZE)); + assert (has_handle (objects, at, MOCK_PUBLIC_KEY_CAPITALIZE)); + assert (!has_handle (objects, at, MOCK_PRIVATE_KEY_PREFIX)); + assert (has_handle (objects, at, MOCK_PUBLIC_KEY_PREFIX)); p11_kit_iter_free (iter); /* The session is still valid ... */ rv = mock_module.C_CloseSession (session); - CuAssertTrue (tc, rv == CKR_OK); + assert (rv == CKR_OK); - rv = p11_kit_finalize_module (&mock_module); - CuAssertTrue (tc, rv == CKR_OK); + rv = mock_module.C_Finalize (NULL); + assert (rv == CKR_OK); } static void -test_with_slot (CuTest *tc) +test_with_slot (void) { CK_OBJECT_HANDLE objects[128]; CK_FUNCTION_LIST_PTR module; @@ -337,44 +332,45 @@ test_with_slot (CuTest *tc) CK_RV rv; int at; - rv = p11_kit_initialize_module (&mock_module); - CuAssertTrue (tc, rv == CKR_OK); + mock_module_reset (); + rv = mock_module.C_Initialize (NULL); + assert (rv == CKR_OK); iter = p11_kit_iter_new (NULL); p11_kit_iter_begin_with (iter, &mock_module, MOCK_SLOT_ONE_ID, 0); at= 0; while ((rv = p11_kit_iter_next (iter)) == CKR_OK) { - CuAssertTrue (tc, at < 128); + assert (at < 128); objects[at] = p11_kit_iter_get_object (iter); slot = p11_kit_iter_get_slot (iter); - CuAssertTrue (tc, slot == MOCK_SLOT_ONE_ID); + assert (slot == MOCK_SLOT_ONE_ID); module = p11_kit_iter_get_module (iter); - CuAssertPtrEquals (tc, module, &mock_module); + assert_ptr_eq (module, &mock_module); at++; } - CuAssertTrue (tc, rv == CKR_CANCEL); + assert (rv == CKR_CANCEL); /* 1 modules, each with 1 slot, and 3 public objects */ - CuAssertIntEquals (tc, 3, at); + assert_num_eq (3, at); - CuAssertTrue (tc, has_handle (objects, at, MOCK_DATA_OBJECT)); - CuAssertTrue (tc, !has_handle (objects, at, MOCK_PRIVATE_KEY_CAPITALIZE)); - CuAssertTrue (tc, has_handle (objects, at, MOCK_PUBLIC_KEY_CAPITALIZE)); - CuAssertTrue (tc, !has_handle (objects, at, MOCK_PRIVATE_KEY_PREFIX)); - CuAssertTrue (tc, has_handle (objects, at, MOCK_PUBLIC_KEY_PREFIX)); + assert (has_handle (objects, at, MOCK_DATA_OBJECT)); + assert (!has_handle (objects, at, MOCK_PRIVATE_KEY_CAPITALIZE)); + assert (has_handle (objects, at, MOCK_PUBLIC_KEY_CAPITALIZE)); + assert (!has_handle (objects, at, MOCK_PRIVATE_KEY_PREFIX)); + assert (has_handle (objects, at, MOCK_PUBLIC_KEY_PREFIX)); p11_kit_iter_free (iter); - rv = p11_kit_finalize_module (&mock_module); - CuAssertTrue (tc, rv == CKR_OK); + rv = (mock_module.C_Finalize) (NULL); + assert (rv == CKR_OK); } static void -test_with_module (CuTest *tc) +test_with_module (void) { CK_OBJECT_HANDLE objects[128]; CK_FUNCTION_LIST_PTR module; @@ -382,68 +378,70 @@ test_with_module (CuTest *tc) CK_RV rv; int at; - rv = p11_kit_initialize_module (&mock_module); - CuAssertTrue (tc, rv == CKR_OK); + mock_module_reset (); + rv = mock_module.C_Initialize (NULL); + assert (rv == CKR_OK); iter = p11_kit_iter_new (NULL); p11_kit_iter_begin_with (iter, &mock_module, 0, 0); at= 0; while ((rv = p11_kit_iter_next (iter)) == CKR_OK) { - CuAssertTrue (tc, at < 128); + assert (at < 128); objects[at] = p11_kit_iter_get_object (iter); module = p11_kit_iter_get_module (iter); - CuAssertPtrEquals (tc, module, &mock_module); + assert_ptr_eq (module, &mock_module); at++; } - CuAssertTrue (tc, rv == CKR_CANCEL); + assert (rv == CKR_CANCEL); /* 1 modules, each with 1 slot, and 3 public objects */ - CuAssertIntEquals (tc, 3, at); + assert_num_eq (3, at); - CuAssertTrue (tc, has_handle (objects, at, MOCK_DATA_OBJECT)); - CuAssertTrue (tc, !has_handle (objects, at, MOCK_PRIVATE_KEY_CAPITALIZE)); - CuAssertTrue (tc, has_handle (objects, at, MOCK_PUBLIC_KEY_CAPITALIZE)); - CuAssertTrue (tc, !has_handle (objects, at, MOCK_PRIVATE_KEY_PREFIX)); - CuAssertTrue (tc, has_handle (objects, at, MOCK_PUBLIC_KEY_PREFIX)); + assert (has_handle (objects, at, MOCK_DATA_OBJECT)); + assert (!has_handle (objects, at, MOCK_PRIVATE_KEY_CAPITALIZE)); + assert (has_handle (objects, at, MOCK_PUBLIC_KEY_CAPITALIZE)); + assert (!has_handle (objects, at, MOCK_PRIVATE_KEY_PREFIX)); + assert (has_handle (objects, at, MOCK_PUBLIC_KEY_PREFIX)); p11_kit_iter_free (iter); - rv = p11_kit_finalize_module (&mock_module); - CuAssertTrue (tc, rv == CKR_OK); + rv = mock_module.C_Finalize (NULL); + assert (rv == CKR_OK); } static void -test_keep_session (CuTest *tc) +test_keep_session (void) { CK_SESSION_HANDLE session; P11KitIter *iter; CK_RV rv; - rv = p11_kit_initialize_module (&mock_module); - CuAssertTrue (tc, rv == CKR_OK); + mock_module_reset (); + rv = mock_module.C_Initialize (NULL); + assert (rv == CKR_OK); iter = p11_kit_iter_new (NULL); p11_kit_iter_begin_with (iter, &mock_module, 0, 0); rv = p11_kit_iter_next (iter); - CuAssertTrue (tc, rv == CKR_OK); + assert (rv == CKR_OK); session = p11_kit_iter_keep_session (iter); p11_kit_iter_free (iter); /* The session is still valid ... */ rv = mock_module.C_CloseSession (session); - CuAssertTrue (tc, rv == CKR_OK); + assert (rv == CKR_OK); - rv = p11_kit_finalize_module (&mock_module); - CuAssertTrue (tc, rv == CKR_OK); + rv = mock_module.C_Finalize (NULL); + assert (rv == CKR_OK); } static void -test_unrecognized (CuTest *tc) +test_unrecognized (void) { CK_FUNCTION_LIST_PTR *modules; P11KitIter *iter; @@ -451,7 +449,7 @@ test_unrecognized (CuTest *tc) CK_RV rv; int count; - modules = initialize_and_get_modules (tc); + modules = initialize_and_get_modules (); uri = p11_kit_uri_new (); p11_kit_uri_set_unrecognized (uri, 1); @@ -464,18 +462,18 @@ test_unrecognized (CuTest *tc) while ((rv = p11_kit_iter_next (iter)) == CKR_OK) count++; - CuAssertTrue (tc, rv == CKR_CANCEL); + assert (rv == CKR_CANCEL); /* Nothing should have matched */ - CuAssertIntEquals (tc, 0, count); + assert_num_eq (0, count); p11_kit_iter_free (iter); - finalize_and_free_modules (tc, modules); + finalize_and_free_modules (modules); } static void -test_uri_with_type (CuTest *tc) +test_uri_with_type (void) { CK_OBJECT_HANDLE objects[128]; CK_FUNCTION_LIST_PTR *modules; @@ -485,11 +483,11 @@ test_uri_with_type (CuTest *tc) int at; int ret; - modules = initialize_and_get_modules (tc); + modules = initialize_and_get_modules (); uri = p11_kit_uri_new (); ret = p11_kit_uri_parse ("pkcs11:object-type=public", P11_KIT_URI_FOR_OBJECT, uri); - CuAssertIntEquals (tc, ret, P11_KIT_URI_OK); + assert_num_eq (ret, P11_KIT_URI_OK); iter = p11_kit_iter_new (uri); p11_kit_uri_free (uri); @@ -498,29 +496,29 @@ test_uri_with_type (CuTest *tc) at = 0; while ((rv = p11_kit_iter_next (iter)) == CKR_OK) { - CuAssertTrue (tc, at < 128); + assert (at < 128); objects[at] = p11_kit_iter_get_object (iter); at++; } - CuAssertTrue (tc, rv == CKR_CANCEL); + assert (rv == CKR_CANCEL); /* Three modules, each with 1 slot, and 2 public keys */ - CuAssertIntEquals (tc, 6, at); + assert_num_eq (6, at); - CuAssertTrue (tc, !has_handle (objects, at, MOCK_DATA_OBJECT)); - CuAssertTrue (tc, !has_handle (objects, at, MOCK_PRIVATE_KEY_CAPITALIZE)); - CuAssertTrue (tc, has_handle (objects, at, MOCK_PUBLIC_KEY_CAPITALIZE)); - CuAssertTrue (tc, !has_handle (objects, at, MOCK_PRIVATE_KEY_PREFIX)); - CuAssertTrue (tc, has_handle (objects, at, MOCK_PUBLIC_KEY_PREFIX)); + assert (!has_handle (objects, at, MOCK_DATA_OBJECT)); + assert (!has_handle (objects, at, MOCK_PRIVATE_KEY_CAPITALIZE)); + assert (has_handle (objects, at, MOCK_PUBLIC_KEY_CAPITALIZE)); + assert (!has_handle (objects, at, MOCK_PRIVATE_KEY_PREFIX)); + assert (has_handle (objects, at, MOCK_PUBLIC_KEY_PREFIX)); p11_kit_iter_free (iter); - finalize_and_free_modules (tc, modules); + finalize_and_free_modules (modules); } static void -test_filter (CuTest *tc) +test_filter (void) { CK_OBJECT_HANDLE objects[128]; CK_FUNCTION_LIST_PTR *modules; @@ -535,7 +533,7 @@ test_filter (CuTest *tc) { CKA_CLASS, &public_key, sizeof (public_key) }, }; - modules = initialize_and_get_modules (tc); + modules = initialize_and_get_modules (); iter = p11_kit_iter_new (NULL); p11_kit_iter_add_filter (iter, attrs, 2); @@ -544,29 +542,29 @@ test_filter (CuTest *tc) at = 0; while ((rv = p11_kit_iter_next (iter)) == CKR_OK) { - CuAssertTrue (tc, at < 128); + assert (at < 128); objects[at] = p11_kit_iter_get_object (iter); at++; } - CuAssertTrue (tc, rv == CKR_CANCEL); + assert (rv == CKR_CANCEL); /* Three modules, each with 1 slot, and 2 public keys */ - CuAssertIntEquals (tc, 6, at); + assert_num_eq (6, at); - CuAssertTrue (tc, !has_handle (objects, at, MOCK_DATA_OBJECT)); - CuAssertTrue (tc, !has_handle (objects, at, MOCK_PRIVATE_KEY_CAPITALIZE)); - CuAssertTrue (tc, has_handle (objects, at, MOCK_PUBLIC_KEY_CAPITALIZE)); - CuAssertTrue (tc, !has_handle (objects, at, MOCK_PRIVATE_KEY_PREFIX)); - CuAssertTrue (tc, has_handle (objects, at, MOCK_PUBLIC_KEY_PREFIX)); + assert (!has_handle (objects, at, MOCK_DATA_OBJECT)); + assert (!has_handle (objects, at, MOCK_PRIVATE_KEY_CAPITALIZE)); + assert (has_handle (objects, at, MOCK_PUBLIC_KEY_CAPITALIZE)); + assert (!has_handle (objects, at, MOCK_PRIVATE_KEY_PREFIX)); + assert (has_handle (objects, at, MOCK_PUBLIC_KEY_PREFIX)); p11_kit_iter_free (iter); - finalize_and_free_modules (tc, modules); + finalize_and_free_modules (modules); } static void -test_session_flags (CuTest *tc) +test_session_flags (void) { CK_FUNCTION_LIST_PTR *modules; CK_FUNCTION_LIST_PTR module; @@ -575,7 +573,7 @@ test_session_flags (CuTest *tc) P11KitIter *iter; CK_RV rv; - modules = initialize_and_get_modules (tc); + modules = initialize_and_get_modules (); iter = p11_kit_iter_new (NULL); p11_kit_iter_set_session_flags (iter, CKF_RW_SESSION); @@ -584,26 +582,26 @@ test_session_flags (CuTest *tc) while ((rv = p11_kit_iter_next (iter)) == CKR_OK) { module = p11_kit_iter_get_module (iter); - CuAssertPtrNotNull (tc, module); + assert_ptr_not_null (module); session = p11_kit_iter_get_session (iter); - CuAssertTrue (tc, session != 0); + assert (session != 0); rv = (module->C_GetSessionInfo) (session, &info); - CuAssertTrue (tc, rv == CKR_OK); + assert (rv == CKR_OK); - CuAssertIntEquals (tc, CKS_RW_PUBLIC_SESSION, info.state); + assert_num_eq (CKS_RW_PUBLIC_SESSION, info.state); } - CuAssertTrue (tc, rv == CKR_CANCEL); + assert (rv == CKR_CANCEL); p11_kit_iter_free (iter); - finalize_and_free_modules (tc, modules); + finalize_and_free_modules (modules); } static void -test_module_match (CuTest *tc) +test_module_match (void) { CK_FUNCTION_LIST_PTR *modules; P11KitIter *iter; @@ -612,11 +610,11 @@ test_module_match (CuTest *tc) int count; int ret; - modules = initialize_and_get_modules (tc); + modules = initialize_and_get_modules (); uri = p11_kit_uri_new (); ret = p11_kit_uri_parse ("pkcs11:library-description=MOCK%20LIBRARY", P11_KIT_URI_FOR_MODULE, uri); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + assert_num_eq (P11_KIT_URI_OK, ret); iter = p11_kit_iter_new (uri); p11_kit_uri_free (uri); @@ -627,18 +625,18 @@ test_module_match (CuTest *tc) while ((rv = p11_kit_iter_next (iter)) == CKR_OK) count++; - CuAssertTrue (tc, rv == CKR_CANCEL); + assert (rv == CKR_CANCEL); /* Three modules, each with 1 slot, and 3 public objects */ - CuAssertIntEquals (tc, 9, count); + assert_num_eq (9, count); p11_kit_iter_free (iter); - finalize_and_free_modules (tc, modules); + finalize_and_free_modules (modules); } static void -test_module_mismatch (CuTest *tc) +test_module_mismatch (void) { CK_FUNCTION_LIST_PTR *modules; P11KitIter *iter; @@ -647,11 +645,11 @@ test_module_mismatch (CuTest *tc) int count; int ret; - modules = initialize_and_get_modules (tc); + modules = initialize_and_get_modules (); uri = p11_kit_uri_new (); ret = p11_kit_uri_parse ("pkcs11:library-description=blah", P11_KIT_URI_FOR_MODULE, uri); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + assert_num_eq (P11_KIT_URI_OK, ret); iter = p11_kit_iter_new (uri); p11_kit_uri_free (uri); @@ -662,18 +660,18 @@ test_module_mismatch (CuTest *tc) while ((rv = p11_kit_iter_next (iter)) == CKR_OK) count++; - CuAssertTrue (tc, rv == CKR_CANCEL); + assert (rv == CKR_CANCEL); /* Nothing should have matched */ - CuAssertIntEquals (tc, 0, count); + assert_num_eq (0, count); p11_kit_iter_free (iter); - finalize_and_free_modules (tc, modules); + finalize_and_free_modules (modules); } static void -test_token_match (CuTest *tc) +test_token_match (void) { CK_FUNCTION_LIST_PTR *modules; P11KitIter *iter; @@ -682,11 +680,11 @@ test_token_match (CuTest *tc) int count; int ret; - modules = initialize_and_get_modules (tc); + modules = initialize_and_get_modules (); uri = p11_kit_uri_new (); ret = p11_kit_uri_parse ("pkcs11:manufacturer=TEST%20MANUFACTURER", P11_KIT_URI_FOR_TOKEN, uri); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + assert_num_eq (P11_KIT_URI_OK, ret); iter = p11_kit_iter_new (uri); p11_kit_uri_free (uri); @@ -697,18 +695,18 @@ test_token_match (CuTest *tc) while ((rv = p11_kit_iter_next (iter)) == CKR_OK) count++; - CuAssertTrue (tc, rv == CKR_CANCEL); + assert (rv == CKR_CANCEL); /* Three modules, each with 1 slot, and 3 public objects */ - CuAssertIntEquals (tc, 9, count); + assert_num_eq (9, count); p11_kit_iter_free (iter); - finalize_and_free_modules (tc, modules); + finalize_and_free_modules (modules); } static void -test_token_mismatch (CuTest *tc) +test_token_mismatch (void) { CK_FUNCTION_LIST_PTR *modules; P11KitIter *iter; @@ -717,11 +715,11 @@ test_token_mismatch (CuTest *tc) int count; int ret; - modules = initialize_and_get_modules (tc); + modules = initialize_and_get_modules (); uri = p11_kit_uri_new (); ret = p11_kit_uri_parse ("pkcs11:manufacturer=blah", P11_KIT_URI_FOR_TOKEN, uri); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + assert_num_eq (P11_KIT_URI_OK, ret); iter = p11_kit_iter_new (uri); p11_kit_uri_free (uri); @@ -732,26 +730,27 @@ test_token_mismatch (CuTest *tc) while ((rv = p11_kit_iter_next (iter)) == CKR_OK) count++; - CuAssertTrue (tc, rv == CKR_CANCEL); + assert (rv == CKR_CANCEL); /* Nothing should have matched */ - CuAssertIntEquals (tc, 0, count); + assert_num_eq (0, count); p11_kit_iter_free (iter); - finalize_and_free_modules (tc, modules); + finalize_and_free_modules (modules); } static void -test_getslotlist_fail_first (CuTest *tc) +test_getslotlist_fail_first (void) { CK_FUNCTION_LIST module; P11KitIter *iter; CK_RV rv; int at; - rv = p11_kit_initialize_module (&mock_module); - CuAssertTrue (tc, rv == CKR_OK); + mock_module_reset (); + rv = mock_module.C_Initialize (NULL); + assert (rv == CKR_OK); memcpy (&module, &mock_module, sizeof (CK_FUNCTION_LIST)); module.C_GetSlotList = mock_C_GetSlotList__fail_first; @@ -763,27 +762,28 @@ test_getslotlist_fail_first (CuTest *tc) while ((rv = p11_kit_iter_next (iter)) == CKR_OK) at++; - CuAssertTrue (tc, rv == CKR_VENDOR_DEFINED); + assert (rv == CKR_VENDOR_DEFINED); /* Should fail on the first iteration */ - CuAssertIntEquals (tc, 0, at); + assert_num_eq (0, at); p11_kit_iter_free (iter); - rv = p11_kit_finalize_module (&mock_module); - CuAssertTrue (tc, rv == CKR_OK); + rv = mock_module.C_Finalize (NULL); + assert (rv == CKR_OK); } static void -test_getslotlist_fail_late (CuTest *tc) +test_getslotlist_fail_late (void) { CK_FUNCTION_LIST module; P11KitIter *iter; CK_RV rv; int at; - rv = p11_kit_initialize_module (&mock_module); - CuAssertTrue (tc, rv == CKR_OK); + mock_module_reset (); + rv = mock_module.C_Initialize (NULL); + assert (rv == CKR_OK); memcpy (&module, &mock_module, sizeof (CK_FUNCTION_LIST)); module.C_GetSlotList = mock_C_GetSlotList__fail_late; @@ -795,27 +795,28 @@ test_getslotlist_fail_late (CuTest *tc) while ((rv = p11_kit_iter_next (iter)) == CKR_OK) at++; - CuAssertTrue (tc, rv == CKR_VENDOR_DEFINED); + assert (rv == CKR_VENDOR_DEFINED); /* Should fail on the first iteration */ - CuAssertIntEquals (tc, 0, at); + assert_num_eq (0, at); p11_kit_iter_free (iter); - rv = p11_kit_finalize_module (&mock_module); - CuAssertTrue (tc, rv == CKR_OK); + rv = mock_module.C_Finalize (NULL); + assert (rv == CKR_OK); } static void -test_open_session_fail (CuTest *tc) +test_open_session_fail (void) { CK_FUNCTION_LIST module; P11KitIter *iter; CK_RV rv; int at; - rv = p11_kit_initialize_module (&mock_module); - CuAssertTrue (tc, rv == CKR_OK); + mock_module_reset (); + rv = mock_module.C_Initialize (NULL); + assert (rv == CKR_OK); memcpy (&module, &mock_module, sizeof (CK_FUNCTION_LIST)); module.C_OpenSession = mock_C_OpenSession__fails; @@ -827,27 +828,28 @@ test_open_session_fail (CuTest *tc) while ((rv = p11_kit_iter_next (iter)) == CKR_OK) at++; - CuAssertTrue (tc, rv == CKR_DEVICE_ERROR); + assert (rv == CKR_DEVICE_ERROR); /* Should fail on the first iteration */ - CuAssertIntEquals (tc, 0, at); + assert_num_eq (0, at); p11_kit_iter_free (iter); - rv = p11_kit_finalize_module (&mock_module); - CuAssertTrue (tc, rv == CKR_OK); + rv = mock_module.C_Finalize (NULL); + assert (rv == CKR_OK); } static void -test_find_init_fail (CuTest *tc) +test_find_init_fail (void) { CK_FUNCTION_LIST module; P11KitIter *iter; CK_RV rv; int at; - rv = p11_kit_initialize_module (&mock_module); - CuAssertTrue (tc, rv == CKR_OK); + mock_module_reset (); + rv = mock_module.C_Initialize (NULL); + assert (rv == CKR_OK); memcpy (&module, &mock_module, sizeof (CK_FUNCTION_LIST)); module.C_FindObjectsInit = mock_C_FindObjectsInit__fails; @@ -859,27 +861,28 @@ test_find_init_fail (CuTest *tc) while ((rv = p11_kit_iter_next (iter)) == CKR_OK) at++; - CuAssertTrue (tc, rv == CKR_DEVICE_MEMORY); + assert (rv == CKR_DEVICE_MEMORY); /* Should fail on the first iteration */ - CuAssertIntEquals (tc, 0, at); + assert_num_eq (0, at); p11_kit_iter_free (iter); - rv = p11_kit_finalize_module (&mock_module); - CuAssertTrue (tc, rv == CKR_OK); + rv = mock_module.C_Finalize (NULL); + assert (rv == CKR_OK); } static void -test_find_objects_fail (CuTest *tc) +test_find_objects_fail (void) { CK_FUNCTION_LIST module; P11KitIter *iter; CK_RV rv; int at; - rv = p11_kit_initialize_module (&mock_module); - CuAssertTrue (tc, rv == CKR_OK); + mock_module_reset (); + rv = mock_module.C_Initialize (NULL); + assert (rv == CKR_OK); memcpy (&module, &mock_module, sizeof (CK_FUNCTION_LIST)); module.C_FindObjects = mock_C_FindObjects__fails; @@ -891,19 +894,19 @@ test_find_objects_fail (CuTest *tc) while ((rv = p11_kit_iter_next (iter)) == CKR_OK) at++; - CuAssertTrue (tc, rv == CKR_DEVICE_REMOVED); + assert (rv == CKR_DEVICE_REMOVED); /* Should fail on the first iteration */ - CuAssertIntEquals (tc, 0, at); + assert_num_eq (0, at); p11_kit_iter_free (iter); - rv = p11_kit_finalize_module (&mock_module); - CuAssertTrue (tc, rv == CKR_OK); + rv = mock_module.C_Finalize (NULL); + assert (rv == CKR_OK); } static void -test_load_attributes (CuTest *tc) +test_load_attributes (void) { CK_FUNCTION_LIST_PTR *modules; P11KitIter *iter; @@ -918,7 +921,7 @@ test_load_attributes (CuTest *tc) { CKA_LABEL }, }; - modules = initialize_and_get_modules (tc); + modules = initialize_and_get_modules (); iter = p11_kit_iter_new (NULL); p11_kit_iter_begin (iter, modules); @@ -928,24 +931,24 @@ test_load_attributes (CuTest *tc) at = 0; while ((rv = p11_kit_iter_next (iter)) == CKR_OK) { rv = p11_kit_iter_load_attributes (iter, attrs, 2); - CuAssertTrue (tc, rv == CKR_OK); + assert (rv == CKR_OK); object = p11_kit_iter_get_object (iter); switch (object) { case MOCK_DATA_OBJECT: - CuAssertTrue (tc, p11_attrs_find_ulong (attrs, CKA_CLASS, &ulong) && ulong == CKO_DATA); - CuAssertTrue (tc, p11_attr_match_value (p11_attrs_find (attrs, CKA_LABEL), "TEST LABEL", -1)); + assert (p11_attrs_find_ulong (attrs, CKA_CLASS, &ulong) && ulong == CKO_DATA); + assert (p11_attr_match_value (p11_attrs_find (attrs, CKA_LABEL), "TEST LABEL", -1)); break; case MOCK_PUBLIC_KEY_CAPITALIZE: - CuAssertTrue (tc, p11_attrs_find_ulong (attrs, CKA_CLASS, &ulong) && ulong == CKO_PUBLIC_KEY); - CuAssertTrue (tc, p11_attr_match_value (p11_attrs_find (attrs, CKA_LABEL), "Public Capitalize Key", -1)); + assert (p11_attrs_find_ulong (attrs, CKA_CLASS, &ulong) && ulong == CKO_PUBLIC_KEY); + assert (p11_attr_match_value (p11_attrs_find (attrs, CKA_LABEL), "Public Capitalize Key", -1)); break; case MOCK_PUBLIC_KEY_PREFIX: - CuAssertTrue (tc, p11_attrs_find_ulong (attrs, CKA_CLASS, &ulong) && ulong == CKO_PUBLIC_KEY); - CuAssertTrue (tc, p11_attr_match_value (p11_attrs_find (attrs, CKA_LABEL), "Public prefix key", -1)); + assert (p11_attrs_find_ulong (attrs, CKA_CLASS, &ulong) && ulong == CKO_PUBLIC_KEY); + assert (p11_attr_match_value (p11_attrs_find (attrs, CKA_LABEL), "Public prefix key", -1)); break; default: - CuFail (tc, "Unknown object matched"); + assert_fail ("Unknown object matched", NULL); break; } @@ -954,26 +957,27 @@ test_load_attributes (CuTest *tc) p11_attrs_free (attrs); - CuAssertTrue (tc, rv == CKR_CANCEL); + assert (rv == CKR_CANCEL); /* Three modules, each with 1 slot, and 3 public objects */ - CuAssertIntEquals (tc, 9, at); + assert_num_eq (9, at); p11_kit_iter_free (iter); - finalize_and_free_modules (tc, modules); + finalize_and_free_modules (modules); } static void -test_load_attributes_none (CuTest *tc) +test_load_attributes_none (void) { CK_FUNCTION_LIST module; P11KitIter *iter; CK_ATTRIBUTE *attrs; CK_RV rv; - rv = p11_kit_initialize_module (&mock_module); - CuAssertTrue (tc, rv == CKR_OK); + mock_module_reset (); + rv = mock_module.C_Initialize (NULL); + assert (rv == CKR_OK); memcpy (&module, &mock_module, sizeof (CK_FUNCTION_LIST)); @@ -983,20 +987,20 @@ test_load_attributes_none (CuTest *tc) while ((rv = p11_kit_iter_next (iter)) == CKR_OK) { attrs = p11_attrs_buildn (NULL, NULL, 0); rv = p11_kit_iter_load_attributes (iter, attrs, 0); - CuAssertTrue (tc, rv == CKR_OK); + assert (rv == CKR_OK); p11_attrs_free (attrs); } - CuAssertTrue (tc, rv == CKR_CANCEL); + assert (rv == CKR_CANCEL); p11_kit_iter_free (iter); - rv = p11_kit_finalize_module (&mock_module); - CuAssertTrue (tc, rv == CKR_OK); + rv = mock_module.C_Finalize (NULL); + assert (rv == CKR_OK); } static void -test_load_attributes_fail_first (CuTest *tc) +test_load_attributes_fail_first (void) { CK_ATTRIBUTE label = { CKA_LABEL, }; CK_FUNCTION_LIST module; @@ -1004,8 +1008,9 @@ test_load_attributes_fail_first (CuTest *tc) CK_ATTRIBUTE *attrs; CK_RV rv; - rv = p11_kit_initialize_module (&mock_module); - CuAssertTrue (tc, rv == CKR_OK); + mock_module_reset (); + rv = mock_module.C_Initialize (NULL); + assert (rv == CKR_OK); memcpy (&module, &mock_module, sizeof (CK_FUNCTION_LIST)); module.C_GetAttributeValue = mock_C_GetAttributeValue__fail_first; @@ -1016,20 +1021,20 @@ test_load_attributes_fail_first (CuTest *tc) while ((rv = p11_kit_iter_next (iter)) == CKR_OK) { attrs = p11_attrs_build (NULL, &label, NULL); rv = p11_kit_iter_load_attributes (iter, attrs, 1); - CuAssertTrue (tc, rv == CKR_FUNCTION_REJECTED); + assert (rv == CKR_FUNCTION_REJECTED); p11_attrs_free (attrs); } - CuAssertTrue (tc, rv == CKR_CANCEL); + assert (rv == CKR_CANCEL); p11_kit_iter_free (iter); - rv = p11_kit_finalize_module (&mock_module); - CuAssertTrue (tc, rv == CKR_OK); + rv = mock_module.C_Finalize (NULL); + assert (rv == CKR_OK); } static void -test_load_attributes_fail_late (CuTest *tc) +test_load_attributes_fail_late (void) { CK_ATTRIBUTE label = { CKA_LABEL, }; CK_FUNCTION_LIST module; @@ -1037,8 +1042,9 @@ test_load_attributes_fail_late (CuTest *tc) CK_ATTRIBUTE *attrs; CK_RV rv; - rv = p11_kit_initialize_module (&mock_module); - CuAssertTrue (tc, rv == CKR_OK); + mock_module_reset (); + rv = mock_module.C_Initialize (NULL); + assert (rv == CKR_OK); memcpy (&module, &mock_module, sizeof (CK_FUNCTION_LIST)); module.C_GetAttributeValue = mock_C_GetAttributeValue__fail_late; @@ -1049,61 +1055,50 @@ test_load_attributes_fail_late (CuTest *tc) while ((rv = p11_kit_iter_next (iter)) == CKR_OK) { attrs = p11_attrs_build (NULL, &label, NULL); rv = p11_kit_iter_load_attributes (iter, attrs, 1); - CuAssertTrue (tc, rv == CKR_FUNCTION_FAILED); + assert (rv == CKR_FUNCTION_FAILED); p11_attrs_free (attrs); } - CuAssertTrue (tc, rv == CKR_CANCEL); + assert (rv == CKR_CANCEL); p11_kit_iter_free (iter); - rv = p11_kit_finalize_module (&mock_module); - CuAssertTrue (tc, rv == CKR_OK); + rv = mock_module.C_Finalize (NULL); + assert (rv == CKR_OK); } int -main (void) +main (int argc, + char *argv[]) { - CuString *output = CuStringNew (); - CuSuite* suite = CuSuiteNew (); - int ret; - - putenv ("P11_KIT_STRICT=1"); p11_library_init (); mock_module_init (); - SUITE_ADD_TEST (suite, test_all); - SUITE_ADD_TEST (suite, test_unrecognized); - SUITE_ADD_TEST (suite, test_uri_with_type); - SUITE_ADD_TEST (suite, test_session_flags); - SUITE_ADD_TEST (suite, test_callback); - SUITE_ADD_TEST (suite, test_callback_fails); - SUITE_ADD_TEST (suite, test_callback_destroyer); - SUITE_ADD_TEST (suite, test_filter); - SUITE_ADD_TEST (suite, test_with_session); - SUITE_ADD_TEST (suite, test_with_slot); - SUITE_ADD_TEST (suite, test_with_module); - SUITE_ADD_TEST (suite, test_keep_session); - SUITE_ADD_TEST (suite, test_token_match); - SUITE_ADD_TEST (suite, test_token_mismatch); - SUITE_ADD_TEST (suite, test_module_match); - SUITE_ADD_TEST (suite, test_module_mismatch); - SUITE_ADD_TEST (suite, test_getslotlist_fail_first); - SUITE_ADD_TEST (suite, test_getslotlist_fail_late); - SUITE_ADD_TEST (suite, test_open_session_fail); - SUITE_ADD_TEST (suite, test_find_init_fail); - SUITE_ADD_TEST (suite, test_find_objects_fail); - SUITE_ADD_TEST (suite, test_load_attributes); - SUITE_ADD_TEST (suite, test_load_attributes_none); - SUITE_ADD_TEST (suite, test_load_attributes_fail_first); - SUITE_ADD_TEST (suite, test_load_attributes_fail_late); - - CuSuiteRun (suite); - CuSuiteSummary (suite, output); - CuSuiteDetails (suite, output); - printf ("%s\n", output->buffer); - ret = suite->failCount; - CuSuiteDelete (suite); - CuStringDelete (output); - return ret; + p11_test (test_all, "/iter/test_all"); + p11_test (test_unrecognized, "/iter/test_unrecognized"); + p11_test (test_uri_with_type, "/iter/test_uri_with_type"); + p11_test (test_session_flags, "/iter/test_session_flags"); + p11_test (test_callback, "/iter/test_callback"); + p11_test (test_callback_fails, "/iter/test_callback_fails"); + p11_test (test_callback_destroyer, "/iter/test_callback_destroyer"); + p11_test (test_filter, "/iter/test_filter"); + p11_test (test_with_session, "/iter/test_with_session"); + p11_test (test_with_slot, "/iter/test_with_slot"); + p11_test (test_with_module, "/iter/test_with_module"); + p11_test (test_keep_session, "/iter/test_keep_session"); + p11_test (test_token_match, "/iter/test_token_match"); + p11_test (test_token_mismatch, "/iter/test_token_mismatch"); + p11_test (test_module_match, "/iter/test_module_match"); + p11_test (test_module_mismatch, "/iter/test_module_mismatch"); + p11_test (test_getslotlist_fail_first, "/iter/test_getslotlist_fail_first"); + p11_test (test_getslotlist_fail_late, "/iter/test_getslotlist_fail_late"); + p11_test (test_open_session_fail, "/iter/test_open_session_fail"); + p11_test (test_find_init_fail, "/iter/test_find_init_fail"); + p11_test (test_find_objects_fail, "/iter/test_find_objects_fail"); + p11_test (test_load_attributes, "/iter/test_load_attributes"); + p11_test (test_load_attributes_none, "/iter/test_load_attributes_none"); + p11_test (test_load_attributes_fail_first, "/iter/test_load_attributes_fail_first"); + p11_test (test_load_attributes_fail_late, "/iter/test_load_attributes_fail_late"); + + return p11_test_run (argc, argv); } diff --git a/p11-kit/tests/test-log.c b/p11-kit/tests/test-log.c new file mode 100644 index 0000000..e7dab70 --- /dev/null +++ b/p11-kit/tests/test-log.c @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2013 Red Hat Inc + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * * The names of contributors to this software may not be + * used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * Author: Stef Walter <stefw@redhat.com> + */ + +#include "config.h" +#include "test.h" + +#include "dict.h" +#include "library.h" +#include "log.h" +#include "mock.h" +#include "modules.h" +#include "p11-kit.h" +#include "virtual.h" + +#include <errno.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +static CK_FUNCTION_LIST_PTR +setup_mock_module (CK_SESSION_HANDLE *session) +{ + CK_FUNCTION_LIST_PTR module; + CK_RV rv; + + p11_lock (); + p11_log_force = true; + + rv = p11_module_load_inlock_reentrant (&mock_module, 0, &module); + assert (rv == CKR_OK); + assert_ptr_not_null (module); + assert (p11_virtual_is_wrapper (module)); + + p11_unlock (); + + rv = p11_kit_module_initialize (module); + assert (rv == CKR_OK); + + if (session) { + rv = (module->C_OpenSession) (MOCK_SLOT_ONE_ID, + CKF_RW_SESSION | CKF_SERIAL_SESSION, + NULL, NULL, session); + assert (rv == CKR_OK); + } + + return module; +} + +static void +teardown_mock_module (CK_FUNCTION_LIST_PTR module) +{ + CK_RV rv; + + rv = p11_kit_module_finalize (module); + assert (rv == CKR_OK); + + p11_lock (); + + rv = p11_module_release_inlock_reentrant (module); + assert (rv == CKR_OK); + + p11_unlock (); +} + +/* Bring in all the mock module tests */ +#include "test-mock.c" + +int +main (int argc, + char *argv[]) +{ + p11_library_init (); + mock_module_init (); + + test_mock_add_tests ("/log"); + + p11_kit_be_quiet (); + p11_log_output = false; + + return p11_test_run (argc, argv); +} diff --git a/p11-kit/tests/test-managed.c b/p11-kit/tests/test-managed.c new file mode 100644 index 0000000..9fc9ffb --- /dev/null +++ b/p11-kit/tests/test-managed.c @@ -0,0 +1,215 @@ +/* + * Copyright (c) 2012 Red Hat Inc + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * * The names of contributors to this software may not be + * used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * Author: Stef Walter <stefw@redhat.com> + */ + +#include "config.h" +#include "test.h" + +#include "dict.h" +#include "library.h" +#include "mock.h" +#include "modules.h" +#include "p11-kit.h" +#include "virtual.h" + +#include <errno.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +static CK_FUNCTION_LIST_PTR +setup_mock_module (CK_SESSION_HANDLE *session) +{ + CK_FUNCTION_LIST_PTR module; + CK_RV rv; + + p11_lock (); + + rv = p11_module_load_inlock_reentrant (&mock_module, 0, &module); + assert (rv == CKR_OK); + assert_ptr_not_null (module); + assert (p11_virtual_is_wrapper (module)); + + p11_unlock (); + + rv = p11_kit_module_initialize (module); + assert (rv == CKR_OK); + + if (session) { + rv = (module->C_OpenSession) (MOCK_SLOT_ONE_ID, + CKF_RW_SESSION | CKF_SERIAL_SESSION, + NULL, NULL, session); + assert (rv == CKR_OK); + } + + return module; +} + +static void +teardown_mock_module (CK_FUNCTION_LIST_PTR module) +{ + CK_RV rv; + + rv = p11_kit_module_finalize (module); + assert (rv == CKR_OK); + + p11_lock (); + + rv = p11_module_release_inlock_reentrant (module); + assert (rv == CKR_OK); + + p11_unlock (); +} + +static CK_RV +fail_C_Initialize (void *init_reserved) +{ + return CKR_FUNCTION_FAILED; +} + +static void +test_initialize_finalize (void) +{ + CK_FUNCTION_LIST_PTR module; + CK_RV rv; + + p11_lock (); + + rv = p11_module_load_inlock_reentrant (&mock_module, 0, &module); + assert (rv == CKR_OK); + assert_ptr_not_null (module); + assert (p11_virtual_is_wrapper (module)); + + p11_unlock (); + + rv = module->C_Initialize (NULL); + assert (rv == CKR_OK); + + rv = module->C_Initialize (NULL); + assert (rv == CKR_CRYPTOKI_ALREADY_INITIALIZED); + + rv = module->C_Finalize (NULL); + assert (rv == CKR_OK); + + rv = module->C_Finalize (NULL); + assert (rv == CKR_CRYPTOKI_NOT_INITIALIZED); + + p11_lock (); + + rv = p11_module_release_inlock_reentrant (module); + assert (rv == CKR_OK); + + p11_unlock (); +} + +static void +test_initialize_fail (void) +{ + CK_FUNCTION_LIST_PTR module; + CK_FUNCTION_LIST base; + CK_RV rv; + + memcpy (&base, &mock_module, sizeof (CK_FUNCTION_LIST)); + base.C_Initialize = fail_C_Initialize; + + p11_lock (); + + rv = p11_module_load_inlock_reentrant (&base, 0, &module); + assert (rv == CKR_OK); + + p11_unlock (); + + rv = p11_kit_module_initialize (module); + assert (rv == CKR_FUNCTION_FAILED); +} + +static void +test_separate_close_all_sessions (void) +{ + CK_FUNCTION_LIST *first; + CK_FUNCTION_LIST *second; + CK_SESSION_HANDLE s1; + CK_SESSION_HANDLE s2; + CK_SESSION_INFO info; + CK_RV rv; + + first = setup_mock_module (&s1); + second = setup_mock_module (&s2); + + rv = first->C_GetSessionInfo (s1, &info); + assert (rv == CKR_OK); + + rv = second->C_GetSessionInfo (s2, &info); + assert (rv == CKR_OK); + + first->C_CloseAllSessions (MOCK_SLOT_ONE_ID); + assert (rv == CKR_OK); + + rv = first->C_GetSessionInfo (s1, &info); + assert (rv == CKR_SESSION_HANDLE_INVALID); + + rv = second->C_GetSessionInfo (s2, &info); + assert (rv == CKR_OK); + + second->C_CloseAllSessions (MOCK_SLOT_ONE_ID); + assert (rv == CKR_OK); + + rv = first->C_GetSessionInfo (s1, &info); + assert (rv == CKR_SESSION_HANDLE_INVALID); + + rv = second->C_GetSessionInfo (s2, &info); + assert (rv == CKR_SESSION_HANDLE_INVALID); + + teardown_mock_module (first); + teardown_mock_module (second); +} + +/* Bring in all the mock module tests */ +#include "test-mock.c" + +int +main (int argc, + char *argv[]) +{ + mock_module_init (); + p11_library_init (); + + p11_test (test_initialize_finalize, "/managed/test_initialize_finalize"); + p11_test (test_initialize_fail, "/managed/test_initialize_fail"); + p11_test (test_separate_close_all_sessions, "/managed/test_separate_close_all_sessions"); + test_mock_add_tests ("/managed"); + + p11_kit_be_quiet (); + + return p11_test_run (argc, argv); +} diff --git a/p11-kit/tests/test-mock.c b/p11-kit/tests/test-mock.c new file mode 100644 index 0000000..5fba7ec --- /dev/null +++ b/p11-kit/tests/test-mock.c @@ -0,0 +1,1679 @@ +/* + * Copyright (c) 2012 Stefan Walter + * Copyright (c) 2012-2013 Red Hat Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * * The names of contributors to this software may not be + * used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * Author: Stef Walter <stef@thewalter.net> + */ + +#include "test.h" + +#include "library.h" +#include "mock.h" +#include "p11-kit.h" + +#include <sys/types.h> +#include <assert.h> +#include <string.h> +#include <stdio.h> +#include <stdlib.h> + +static void +test_get_info (void) +{ + CK_FUNCTION_LIST_PTR module; + CK_INFO info; + CK_RV rv; + + module = setup_mock_module (NULL); + + rv = (module->C_GetInfo) (&info); + assert (rv == CKR_OK); + assert (memcmp (&info, &MOCK_INFO, sizeof (CK_INFO)) == 0); + + teardown_mock_module (module); +} + +static void +test_get_slot_list (void) +{ + CK_FUNCTION_LIST_PTR module; + CK_SLOT_ID slot_list[8]; + CK_ULONG count = 0; + CK_RV rv; + + module = setup_mock_module (NULL); + + /* Normal module has 2 slots, one with token present */ + rv = (module->C_GetSlotList) (CK_TRUE, NULL, &count); + assert (rv == CKR_OK); + assert_num_eq (MOCK_SLOTS_PRESENT, count); + rv = (module->C_GetSlotList) (CK_FALSE, NULL, &count); + assert (rv == CKR_OK); + assert_num_eq (MOCK_SLOTS_ALL, count); + + count = 8; + rv = (module->C_GetSlotList) (CK_TRUE, slot_list, &count); + assert (rv == CKR_OK); + assert_num_eq (MOCK_SLOTS_PRESENT, count); + assert_num_eq (MOCK_SLOT_ONE_ID, slot_list[0]); + + count = 8; + rv = (module->C_GetSlotList) (CK_FALSE, slot_list, &count); + assert (rv == CKR_OK); + assert_num_eq (MOCK_SLOTS_ALL, count); + assert_num_eq (MOCK_SLOT_ONE_ID, slot_list[0]); + assert_num_eq (MOCK_SLOT_TWO_ID, slot_list[1]); + + teardown_mock_module (module); +} + +static void +test_get_slot_info (void) +{ + CK_FUNCTION_LIST_PTR module; + CK_SLOT_INFO info; + char *string; + CK_RV rv; + + module = setup_mock_module (NULL); + + rv = (module->C_GetSlotInfo) (MOCK_SLOT_ONE_ID, &info); + assert (rv == CKR_OK); + string = p11_kit_space_strdup (info.slotDescription, sizeof (info.slotDescription)); + assert_str_eq ("TEST SLOT", string); + free (string); + string = p11_kit_space_strdup (info.manufacturerID, sizeof (info.manufacturerID)); + assert_str_eq ("TEST MANUFACTURER", string); + free (string); + assert_num_eq (CKF_TOKEN_PRESENT | CKF_REMOVABLE_DEVICE, info.flags); + assert_num_eq (55, info.hardwareVersion.major); + assert_num_eq (155, info.hardwareVersion.minor); + assert_num_eq (65, info.firmwareVersion.major); + assert_num_eq (165, info.firmwareVersion.minor); + + rv = (module->C_GetSlotInfo) (MOCK_SLOT_TWO_ID, &info); + assert (rv == CKR_OK); + assert_num_eq (CKF_REMOVABLE_DEVICE, info.flags); + + rv = (module->C_GetSlotInfo) (0, &info); + assert (rv == CKR_SLOT_ID_INVALID); + + teardown_mock_module (module); +} + +static void +test_get_token_info (void) +{ + CK_FUNCTION_LIST_PTR module; + CK_TOKEN_INFO info; + char *string; + CK_RV rv; + + module = setup_mock_module (NULL); + + rv = (module->C_GetTokenInfo) (MOCK_SLOT_ONE_ID, &info); + assert (rv == CKR_OK); + + string = p11_kit_space_strdup (info.label, sizeof (info.label)); + assert_str_eq ("TEST LABEL", string); + free (string); + string = p11_kit_space_strdup (info.manufacturerID, sizeof (info.manufacturerID)); + assert_str_eq ("TEST MANUFACTURER", string); + free (string); + string = p11_kit_space_strdup (info.model, sizeof (info.model)); + assert_str_eq ("TEST MODEL", string); + free (string); + string = p11_kit_space_strdup (info.serialNumber, sizeof (info.serialNumber)); + assert_str_eq ("TEST SERIAL", string); + free (string); + assert_num_eq (CKF_LOGIN_REQUIRED | CKF_USER_PIN_INITIALIZED | CKF_CLOCK_ON_TOKEN | CKF_TOKEN_INITIALIZED, info.flags); + assert_num_eq (1, info.ulMaxSessionCount); + assert_num_eq (2, info.ulSessionCount); + assert_num_eq (3, info.ulMaxRwSessionCount); + assert_num_eq (4, info.ulRwSessionCount); + assert_num_eq (5, info.ulMaxPinLen); + assert_num_eq (6, info.ulMinPinLen); + assert_num_eq (7, info.ulTotalPublicMemory); + assert_num_eq (8, info.ulFreePublicMemory); + assert_num_eq (9, info.ulTotalPrivateMemory); + assert_num_eq (10, info.ulFreePrivateMemory); + assert_num_eq (75, info.hardwareVersion.major); + assert_num_eq (175, info.hardwareVersion.minor); + assert_num_eq (85, info.firmwareVersion.major); + assert_num_eq (185, info.firmwareVersion.minor); + assert (memcmp (info.utcTime, "1999052509195900", sizeof (info.utcTime)) == 0); + + rv = (module->C_GetTokenInfo) (MOCK_SLOT_TWO_ID, &info); + assert (rv == CKR_TOKEN_NOT_PRESENT); + + rv = (module->C_GetTokenInfo) (0, &info); + assert (rv == CKR_SLOT_ID_INVALID); + + teardown_mock_module (module); +} + +static void +test_get_mechanism_list (void) +{ + CK_FUNCTION_LIST_PTR module; + CK_MECHANISM_TYPE mechs[8]; + CK_ULONG count = 0; + CK_RV rv; + + module = setup_mock_module (NULL); + + rv = (module->C_GetMechanismList) (MOCK_SLOT_ONE_ID, NULL, &count); + assert (rv == CKR_OK); + assert_num_eq (2, count); + rv = (module->C_GetMechanismList) (MOCK_SLOT_TWO_ID, NULL, &count); + assert (rv == CKR_TOKEN_NOT_PRESENT); + rv = (module->C_GetMechanismList) (0, NULL, &count); + assert (rv == CKR_SLOT_ID_INVALID); + + count = 8; + rv = (module->C_GetMechanismList) (MOCK_SLOT_ONE_ID, mechs, &count); + assert (rv == CKR_OK); + assert_num_eq (2, count); + assert_num_eq (mechs[0], CKM_MOCK_CAPITALIZE); + assert_num_eq (mechs[1], CKM_MOCK_PREFIX); + + teardown_mock_module (module); +} + +static void +test_get_mechanism_info (void) +{ + CK_FUNCTION_LIST_PTR module; + CK_MECHANISM_INFO info; + CK_RV rv; + + module = setup_mock_module (NULL); + + rv = (module->C_GetMechanismInfo) (MOCK_SLOT_ONE_ID, CKM_MOCK_CAPITALIZE, &info); + assert (rv == CKR_OK); + assert_num_eq (512, info.ulMinKeySize); + assert_num_eq (4096, info.ulMaxKeySize); + assert_num_eq (CKF_ENCRYPT | CKF_DECRYPT, info.flags); + + rv = (module->C_GetMechanismInfo) (MOCK_SLOT_ONE_ID, CKM_MOCK_PREFIX, &info); + assert (rv == CKR_OK); + assert_num_eq (2048, info.ulMinKeySize); + assert_num_eq (2048, info.ulMaxKeySize); + assert_num_eq (CKF_SIGN | CKF_VERIFY, info.flags); + + rv = (module->C_GetMechanismInfo) (MOCK_SLOT_TWO_ID, CKM_MOCK_PREFIX, &info); + assert (rv == CKR_TOKEN_NOT_PRESENT); + rv = (module->C_GetMechanismInfo) (MOCK_SLOT_ONE_ID, 0, &info); + assert (rv == CKR_MECHANISM_INVALID); + rv = (module->C_GetMechanismInfo) (0, CKM_MOCK_PREFIX, &info); + assert (rv == CKR_SLOT_ID_INVALID); + + teardown_mock_module (module); +} + +static void +test_init_token (void) +{ + CK_FUNCTION_LIST_PTR module; + CK_RV rv; + + module = setup_mock_module (NULL); + + rv = (module->C_InitToken) (MOCK_SLOT_ONE_ID, (CK_UTF8CHAR_PTR)"TEST PIN", 8, (CK_UTF8CHAR_PTR)"TEST LABEL"); + assert (rv == CKR_OK); + + rv = (module->C_InitToken) (MOCK_SLOT_ONE_ID, (CK_UTF8CHAR_PTR)"OTHER", 5, (CK_UTF8CHAR_PTR)"TEST LABEL"); + assert (rv == CKR_PIN_INVALID); + rv = (module->C_InitToken) (MOCK_SLOT_TWO_ID, (CK_UTF8CHAR_PTR)"TEST PIN", 8, (CK_UTF8CHAR_PTR)"TEST LABEL"); + assert (rv == CKR_TOKEN_NOT_PRESENT); + rv = (module->C_InitToken) (0, (CK_UTF8CHAR_PTR)"TEST PIN", 8, (CK_UTF8CHAR_PTR)"TEST LABEL"); + assert (rv == CKR_SLOT_ID_INVALID); + + teardown_mock_module (module); +} + +static void +test_wait_for_slot_event (void) +{ + CK_FUNCTION_LIST_PTR module; + CK_SLOT_ID slot; + CK_RV rv; + +#ifdef MOCK_SKIP_WAIT_TEST + return; +#endif + + module = setup_mock_module (NULL); + + rv = (module->C_WaitForSlotEvent) (0, &slot, NULL); + assert (rv == CKR_OK); + assert_num_eq (slot, MOCK_SLOT_TWO_ID); + + rv = (module->C_WaitForSlotEvent) (CKF_DONT_BLOCK, &slot, NULL); + assert (rv == CKR_NO_EVENT); + + teardown_mock_module (module); +} + +static void +test_open_close_session (void) +{ + CK_FUNCTION_LIST_PTR module; + CK_SESSION_HANDLE session = 0; + CK_RV rv; + + module = setup_mock_module (NULL); + + rv = (module->C_OpenSession) (MOCK_SLOT_TWO_ID, CKF_SERIAL_SESSION, NULL, NULL, &session); + assert (rv == CKR_TOKEN_NOT_PRESENT); + rv = (module->C_OpenSession) (0, CKF_SERIAL_SESSION, NULL, NULL, &session); + assert (rv == CKR_SLOT_ID_INVALID); + + rv = (module->C_OpenSession) (MOCK_SLOT_ONE_ID, CKF_SERIAL_SESSION, NULL, NULL, &session); + assert (rv == CKR_OK); + assert (session != 0); + + rv = (module->C_CloseSession) (session); + assert (rv == CKR_OK); + + rv = (module->C_CloseSession) (session); + assert (rv == CKR_SESSION_HANDLE_INVALID); + + teardown_mock_module (module); +} + +static void +test_close_all_sessions (void) +{ + CK_FUNCTION_LIST_PTR module; + CK_SESSION_HANDLE session = 0; + CK_RV rv; + + module = setup_mock_module (NULL); + + rv = (module->C_OpenSession) (MOCK_SLOT_ONE_ID, CKF_SERIAL_SESSION, NULL, NULL, &session); + assert (rv == CKR_OK); + assert (session != 0); + + rv = (module->C_CloseAllSessions) (MOCK_SLOT_ONE_ID); + assert (rv == CKR_OK); + + rv = (module->C_CloseSession) (session); + assert (rv == CKR_SESSION_HANDLE_INVALID); + + teardown_mock_module (module); +} + +static void +test_get_function_status (void) +{ + CK_FUNCTION_LIST_PTR module; + CK_SESSION_HANDLE session = 0; + CK_RV rv; + + module = setup_mock_module (&session); + + rv = (module->C_GetFunctionStatus) (session); + assert (rv == CKR_FUNCTION_NOT_PARALLEL); + + teardown_mock_module (module); +} + +static void +test_cancel_function (void) +{ + CK_FUNCTION_LIST_PTR module; + CK_SESSION_HANDLE session = 0; + CK_RV rv; + + module = setup_mock_module (&session); + + rv = (module->C_CancelFunction) (session); + assert (rv == CKR_FUNCTION_NOT_PARALLEL); + + teardown_mock_module (module); +} + +static void +test_get_session_info (void) +{ + CK_FUNCTION_LIST_PTR module; + CK_SESSION_HANDLE session = 0; + CK_SESSION_INFO info; + CK_RV rv; + + module = setup_mock_module (NULL); + + rv = (module->C_GetSessionInfo) (0, &info); + assert (rv == CKR_SESSION_HANDLE_INVALID); + + rv = (module->C_OpenSession) (MOCK_SLOT_ONE_ID, CKF_SERIAL_SESSION, NULL, NULL, &session); + assert (rv == CKR_OK); + assert (session != 0); + + rv = (module->C_GetSessionInfo) (session, &info); + assert (rv == CKR_OK); + assert_num_eq (MOCK_SLOT_ONE_ID, info.slotID); + assert_num_eq (CKS_RO_PUBLIC_SESSION, info.state); + assert_num_eq (CKF_SERIAL_SESSION, info.flags); + assert_num_eq (1414, info.ulDeviceError); + + rv = (module->C_OpenSession) (MOCK_SLOT_ONE_ID, CKF_RW_SESSION | CKF_SERIAL_SESSION, NULL, NULL, &session); + assert (rv == CKR_OK); + assert (session != 0); + + rv = (module->C_GetSessionInfo) (session, &info); + assert (rv == CKR_OK); + assert_num_eq (MOCK_SLOT_ONE_ID, info.slotID); + assert_num_eq (CKS_RW_PUBLIC_SESSION, info.state); + assert_num_eq (CKF_SERIAL_SESSION | CKF_RW_SESSION, info.flags); + assert_num_eq (1414, info.ulDeviceError); + + teardown_mock_module (module); +} + +static void +test_init_pin (void) +{ + CK_FUNCTION_LIST_PTR module; + CK_SESSION_HANDLE session = 0; + CK_RV rv; + + module = setup_mock_module (&session); + + rv = (module->C_InitPIN) (0, (CK_UTF8CHAR_PTR)"TEST PIN", 8); + assert (rv == CKR_SESSION_HANDLE_INVALID); + + rv = (module->C_InitPIN) (session, (CK_UTF8CHAR_PTR)"TEST PIN", 8); + assert (rv == CKR_OK); + + rv = (module->C_InitPIN) (session, (CK_UTF8CHAR_PTR)"OTHER", 5); + assert (rv == CKR_PIN_INVALID); + + teardown_mock_module (module); +} + +static void +test_set_pin (void) +{ + CK_FUNCTION_LIST_PTR module; + CK_SESSION_HANDLE session = 0; + CK_RV rv; + + module = setup_mock_module (&session); + + rv = (module->C_SetPIN) (0, (CK_UTF8CHAR_PTR)"booo", 4, (CK_UTF8CHAR_PTR)"TEST PIN", 8); + assert (rv == CKR_SESSION_HANDLE_INVALID); + + rv = (module->C_SetPIN) (session, (CK_UTF8CHAR_PTR)"booo", 4, (CK_UTF8CHAR_PTR)"TEST PIN", 8); + assert (rv == CKR_OK); + + rv = (module->C_SetPIN) (session, (CK_UTF8CHAR_PTR)"other", 5, (CK_UTF8CHAR_PTR)"OTHER", 5); + assert (rv == CKR_PIN_INCORRECT); + + teardown_mock_module (module); +} + +static void +test_operation_state (void) +{ + CK_FUNCTION_LIST_PTR module; + CK_BYTE state[128]; + CK_ULONG state_len; + CK_SESSION_HANDLE session = 0; + CK_RV rv; + + module = setup_mock_module (&session); + + state_len = sizeof (state); + rv = (module->C_GetOperationState) (0, state, &state_len); + assert (rv == CKR_SESSION_HANDLE_INVALID); + + state_len = sizeof (state); + rv = (module->C_GetOperationState) (session, state, &state_len); + assert (rv == CKR_OK); + + rv = (module->C_SetOperationState) (session, state, state_len, 355, 455); + assert (rv == CKR_OK); + + rv = (module->C_SetOperationState) (0, state, state_len, 355, 455); + assert (rv == CKR_SESSION_HANDLE_INVALID); + + teardown_mock_module (module); +} + +static void +test_login_logout (void) +{ + CK_FUNCTION_LIST_PTR module; + CK_SESSION_HANDLE session = 0; + CK_RV rv; + + module = setup_mock_module (&session); + + rv = (module->C_Login) (0, CKU_USER, (CK_UTF8CHAR_PTR)"booo", 4); + assert (rv == CKR_SESSION_HANDLE_INVALID); + + rv = (module->C_Login) (session, CKU_USER, (CK_UTF8CHAR_PTR)"bo", 2); + assert (rv == CKR_PIN_INCORRECT); + + rv = (module->C_Login) (session, CKU_USER, (CK_UTF8CHAR_PTR)"booo", 4); + assert (rv == CKR_OK); + + rv = (module->C_Logout) (session); + assert (rv == CKR_OK); + + rv = (module->C_Logout) (session); + assert (rv == CKR_USER_NOT_LOGGED_IN); + + teardown_mock_module (module); +} + +static void +test_get_attribute_value (void) +{ + CK_FUNCTION_LIST_PTR module; + CK_SESSION_HANDLE session = 0; + CK_ATTRIBUTE attrs[8]; + char label[32]; + CK_OBJECT_CLASS klass; + CK_RV rv; + + module = setup_mock_module (&session); + + attrs[0].type = CKA_CLASS; + attrs[0].pValue = &klass; + attrs[0].ulValueLen = sizeof (klass); + attrs[1].type = CKA_LABEL; + attrs[1].pValue = label; + attrs[1].ulValueLen = 2; /* too small */ + attrs[2].type = CKA_BITS_PER_PIXEL; + attrs[2].pValue = NULL; + attrs[2].ulValueLen = 0; + + rv = (module->C_GetAttributeValue) (session, MOCK_PRIVATE_KEY_CAPITALIZE, attrs, 3); + assert (rv == CKR_USER_NOT_LOGGED_IN); + + rv = (module->C_GetAttributeValue) (session, MOCK_PUBLIC_KEY_CAPITALIZE, attrs, 2); + assert (rv == CKR_BUFFER_TOO_SMALL); + + /* Get right size */ + attrs[1].pValue = NULL; + attrs[1].ulValueLen = 0; + + rv = (module->C_GetAttributeValue) (session, MOCK_PUBLIC_KEY_CAPITALIZE, attrs, 2); + assert (rv == CKR_OK); + + rv = (module->C_GetAttributeValue) (session, MOCK_PUBLIC_KEY_CAPITALIZE, attrs, 3); + assert (rv == CKR_ATTRIBUTE_TYPE_INVALID); + + assert_num_eq (CKO_PUBLIC_KEY, klass); + assert_num_eq (21, attrs[1].ulValueLen); + assert_ptr_eq (NULL, attrs[1].pValue); + attrs[1].pValue = label; + attrs[1].ulValueLen = sizeof (label); + assert ((CK_ULONG)-1 == attrs[2].ulValueLen); + assert_ptr_eq (NULL, attrs[2].pValue); + + rv = (module->C_GetAttributeValue) (session, MOCK_PUBLIC_KEY_CAPITALIZE, attrs, 3); + assert (rv == CKR_ATTRIBUTE_TYPE_INVALID); + + assert_num_eq (CKO_PUBLIC_KEY, klass); + assert_num_eq (21, attrs[1].ulValueLen); + assert_ptr_eq (label, attrs[1].pValue); + assert (memcmp (label, "Public Capitalize Key", attrs[1].ulValueLen) == 0); + assert ((CK_ULONG)-1 == attrs[2].ulValueLen); + assert_ptr_eq (NULL, attrs[2].pValue); + + teardown_mock_module (module); +} + +static void +test_set_attribute_value (void) +{ + CK_FUNCTION_LIST_PTR module; + CK_SESSION_HANDLE session = 0; + CK_ATTRIBUTE attrs[8]; + char label[32]; + CK_ULONG bits; + CK_RV rv; + + module = setup_mock_module (&session); + + strcpy (label, "Blahooo"); + bits = 1555; + + attrs[0].type = CKA_LABEL; + attrs[0].pValue = label; + attrs[0].ulValueLen = strlen (label); + attrs[1].type = CKA_BITS_PER_PIXEL; + attrs[1].pValue = &bits; + attrs[1].ulValueLen = sizeof (bits); + + rv = (module->C_SetAttributeValue) (session, MOCK_PRIVATE_KEY_CAPITALIZE, attrs, 2); + assert (rv == CKR_USER_NOT_LOGGED_IN); + + rv = (module->C_SetAttributeValue) (session, MOCK_PUBLIC_KEY_CAPITALIZE, attrs, 2); + assert (rv == CKR_OK); + + memset (label, 0, sizeof (label)); + bits = 0; + + rv = (module->C_GetAttributeValue) (session, MOCK_PUBLIC_KEY_CAPITALIZE, attrs, 2); + assert (rv == CKR_OK); + + assert_num_eq (bits, 1555); + assert_num_eq (7, attrs[0].ulValueLen); + assert (memcmp (label, "Blahooo", attrs[0].ulValueLen) == 0); + + teardown_mock_module (module); +} + +static void +test_create_object (void) +{ + CK_FUNCTION_LIST_PTR module; + CK_SESSION_HANDLE session = 0; + CK_OBJECT_HANDLE object; + CK_ATTRIBUTE attrs[8]; + char label[32]; + CK_ULONG bits; + CK_RV rv; + + module = setup_mock_module (&session); + + strcpy (label, "Blahooo"); + bits = 1555; + + attrs[0].type = CKA_LABEL; + attrs[0].pValue = label; + attrs[0].ulValueLen = strlen (label); + attrs[1].type = CKA_BITS_PER_PIXEL; + attrs[1].pValue = &bits; + attrs[1].ulValueLen = sizeof (bits); + + rv = (module->C_CreateObject) (0, attrs, 2, &object); + assert (rv == CKR_SESSION_HANDLE_INVALID); + + rv = (module->C_CreateObject) (session, attrs, 2, &object); + assert (rv == CKR_OK); + + attrs[0].ulValueLen = sizeof (label); + memset (label, 0, sizeof (label)); + bits = 0; + + rv = (module->C_GetAttributeValue) (session, object, attrs, 2); + assert (rv == CKR_OK); + + assert_num_eq (bits, 1555); + assert_num_eq (7, attrs[0].ulValueLen); + assert (memcmp (label, "Blahooo", attrs[0].ulValueLen) == 0); + + teardown_mock_module (module); +} + +static void +test_copy_object (void) +{ + CK_FUNCTION_LIST_PTR module; + CK_SESSION_HANDLE session = 0; + CK_OBJECT_HANDLE object; + CK_ATTRIBUTE attrs[8]; + char label[32]; + CK_ULONG bits; + CK_RV rv; + + module = setup_mock_module (&session); + + bits = 1555; + + attrs[0].type = CKA_BITS_PER_PIXEL; + attrs[0].pValue = &bits; + attrs[0].ulValueLen = sizeof (bits); + + rv = (module->C_CopyObject) (session, 1333, attrs, 1, &object); + assert (rv == CKR_OBJECT_HANDLE_INVALID); + + rv = (module->C_CopyObject) (session, MOCK_PUBLIC_KEY_CAPITALIZE, attrs, 1, &object); + assert (rv == CKR_OK); + + attrs[1].type = CKA_LABEL; + attrs[1].pValue = label; + attrs[1].ulValueLen = sizeof (label); + bits = 0; + + rv = (module->C_GetAttributeValue) (session, object, attrs, 2); + assert (rv == CKR_OK); + + assert_num_eq (bits, 1555); + assert_num_eq (21, attrs[1].ulValueLen); + assert (memcmp (label, "Public Capitalize Key", attrs[1].ulValueLen) == 0); + + teardown_mock_module (module); +} + +static void +test_destroy_object (void) +{ + CK_FUNCTION_LIST_PTR module; + CK_SESSION_HANDLE session = 0; + CK_ATTRIBUTE attrs[8]; + char label[32]; + CK_RV rv; + + module = setup_mock_module (&session); + + attrs[0].type = CKA_LABEL; + attrs[0].pValue = label; + attrs[0].ulValueLen = sizeof (label); + + rv = (module->C_GetAttributeValue) (session, MOCK_PUBLIC_KEY_CAPITALIZE, attrs, 1); + assert (rv == CKR_OK); + + rv = (module->C_DestroyObject) (0, MOCK_PUBLIC_KEY_CAPITALIZE); + assert (rv == CKR_SESSION_HANDLE_INVALID); + + rv = (module->C_DestroyObject) (session, MOCK_PUBLIC_KEY_CAPITALIZE); + assert (rv == CKR_OK); + + rv = (module->C_GetAttributeValue) (session, MOCK_PUBLIC_KEY_CAPITALIZE, attrs, 1); + assert (rv == CKR_OBJECT_HANDLE_INVALID); + + teardown_mock_module (module); +} + +static void +test_get_object_size (void) +{ + CK_FUNCTION_LIST_PTR module; + CK_SESSION_HANDLE session = 0; + CK_ULONG size; + CK_RV rv; + + module = setup_mock_module (&session); + + rv = (module->C_GetObjectSize) (session, 1333, &size); + assert (rv == CKR_OBJECT_HANDLE_INVALID); + + rv = (module->C_GetObjectSize) (session, MOCK_PUBLIC_KEY_CAPITALIZE, &size); + assert (rv == CKR_OK); + + /* The number here is the length of all attributes added up */ + assert_num_eq (sizeof (CK_ULONG) == 8 ? 44 : 36, size); + + teardown_mock_module (module); +} + +static void +test_find_objects (void) +{ + CK_FUNCTION_LIST_PTR module; + CK_SESSION_HANDLE session = 0; + CK_OBJECT_CLASS klass = CKO_PUBLIC_KEY; + CK_ATTRIBUTE attr = { CKA_CLASS, &klass, sizeof (klass) }; + CK_OBJECT_HANDLE objects[16]; + CK_ULONG count; + CK_ULONG i; + CK_RV rv; + + module = setup_mock_module (&session); + + rv = (module->C_FindObjectsInit) (0, &attr, 1); + assert (rv == CKR_SESSION_HANDLE_INVALID); + + rv = (module->C_FindObjectsInit) (session, &attr, 1); + assert (rv == CKR_OK); + + rv = (module->C_FindObjects) (0, objects, 16, &count); + assert (rv == CKR_SESSION_HANDLE_INVALID); + + rv = (module->C_FindObjects) (session, objects, 16, &count); + assert (rv == CKR_OK); + + assert (count < 16); + + /* Make sure we get the capitalize public key */ + for (i = 0; i < count; i++) { + if (objects[i] == MOCK_PUBLIC_KEY_CAPITALIZE) + break; + } + assert (i != count); + + /* Make sure we get the prefix public key */ + for (i = 0; i < count; i++) { + if (objects[i] == MOCK_PUBLIC_KEY_PREFIX) + break; + } + assert (i != count); + + /* Make sure all public keys */ + for (i = 0; i < count; i++) { + klass = (CK_ULONG)-1; + rv = (module->C_GetAttributeValue) (session, objects[i], &attr, 1); + assert (rv == CKR_OK); + assert_num_eq (CKO_PUBLIC_KEY, klass); + } + + rv = (module->C_FindObjectsFinal) (session); + assert (rv == CKR_OK); + + rv = (module->C_FindObjectsFinal) (session); + assert (rv == CKR_OPERATION_NOT_INITIALIZED); + + teardown_mock_module (module); +} + +static void +test_encrypt (void) +{ + CK_FUNCTION_LIST_PTR module; + CK_SESSION_HANDLE session = 0; + CK_MECHANISM mech = { CKM_MOCK_CAPITALIZE, NULL, 0 }; + CK_BYTE data[128]; + CK_ULONG length; + CK_RV rv; + + module = setup_mock_module (&session); + + rv = (module->C_EncryptInit) (session, &mech, MOCK_PUBLIC_KEY_PREFIX); + assert (rv == CKR_KEY_HANDLE_INVALID); + + rv = (module->C_EncryptInit) (session, &mech, MOCK_PUBLIC_KEY_CAPITALIZE); + assert (rv == CKR_OK); + + length = sizeof (data); + rv = (module->C_Encrypt) (0, (CK_BYTE_PTR)"blah", 4, data, &length); + assert (rv == CKR_SESSION_HANDLE_INVALID); + + length = sizeof (data); + rv = (module->C_Encrypt) (session, (CK_BYTE_PTR)"blah", 4, data, &length); + assert (rv == CKR_OK); + + assert_num_eq (4, length); + assert (memcmp (data, "BLAH", 4) == 0); + + rv = (module->C_EncryptInit) (session, &mech, MOCK_PUBLIC_KEY_CAPITALIZE); + assert (rv == CKR_OK); + + length = sizeof (data); + rv = (module->C_EncryptUpdate) (0, (CK_BYTE_PTR)"blah", 4, data, &length); + assert (rv == CKR_SESSION_HANDLE_INVALID); + + length = sizeof (data); + rv = (module->C_EncryptUpdate) (session, (CK_BYTE_PTR)"sLurm", 5, data, &length); + assert (rv == CKR_OK); + + assert_num_eq (5, length); + assert (memcmp (data, "SLURM", 5) == 0); + + length = sizeof (data); + rv = (module->C_EncryptFinal) (0, data, &length); + assert (rv == CKR_SESSION_HANDLE_INVALID); + + length = sizeof (data); + rv = (module->C_EncryptFinal) (session, data, &length); + assert (rv == CKR_OK); + + teardown_mock_module (module); +} + +static void +test_decrypt (void) +{ + CK_FUNCTION_LIST_PTR module; + CK_SESSION_HANDLE session = 0; + CK_MECHANISM mech = { CKM_MOCK_CAPITALIZE, NULL, 0 }; + CK_BYTE data[128]; + CK_ULONG length; + CK_RV rv; + + module = setup_mock_module (&session); + + rv = (module->C_Login) (session, CKU_USER, (CK_BYTE_PTR)"booo", 4); + assert (rv == CKR_OK); + + rv = (module->C_DecryptInit) (session, &mech, MOCK_PRIVATE_KEY_PREFIX); + assert (rv == CKR_KEY_HANDLE_INVALID); + + rv = (module->C_DecryptInit) (session, &mech, MOCK_PRIVATE_KEY_CAPITALIZE); + assert (rv == CKR_OK); + + length = sizeof (data); + rv = (module->C_Decrypt) (0, (CK_BYTE_PTR)"bLAH", 4, data, &length); + assert (rv == CKR_SESSION_HANDLE_INVALID); + + length = sizeof (data); + rv = (module->C_Decrypt) (session, (CK_BYTE_PTR)"BLAh", 4, data, &length); + assert (rv == CKR_OK); + + assert_num_eq (4, length); + assert (memcmp (data, "blah", 4) == 0); + + rv = (module->C_DecryptInit) (session, &mech, MOCK_PRIVATE_KEY_CAPITALIZE); + assert (rv == CKR_OK); + + length = sizeof (data); + rv = (module->C_DecryptUpdate) (0, (CK_BYTE_PTR)"blah", 4, data, &length); + assert (rv == CKR_SESSION_HANDLE_INVALID); + + length = sizeof (data); + rv = (module->C_DecryptUpdate) (session, (CK_BYTE_PTR)"sLuRM", 5, data, &length); + assert (rv == CKR_OK); + + assert_num_eq (5, length); + assert (memcmp (data, "slurm", 5) == 0); + + length = sizeof (data); + rv = (module->C_DecryptFinal) (0, data, &length); + assert (rv == CKR_SESSION_HANDLE_INVALID); + + length = sizeof (data); + rv = (module->C_DecryptFinal) (session, data, &length); + assert (rv == CKR_OK); + + teardown_mock_module (module); +} + +static void +test_digest (void) +{ + CK_FUNCTION_LIST_PTR module; + CK_SESSION_HANDLE session = 0; + CK_MECHANISM mech = { CKM_MOCK_COUNT, NULL, 0 }; + CK_BYTE digest[128]; + CK_ULONG length; + CK_RV rv; + + module = setup_mock_module (&session); + + rv = (module->C_DigestInit) (0, &mech); + assert (rv == CKR_SESSION_HANDLE_INVALID); + + rv = (module->C_DigestInit) (session, &mech); + assert (rv == CKR_OK); + + length = sizeof (digest); + rv = (module->C_Digest) (0, (CK_BYTE_PTR)"bLAH", 4, digest, &length); + assert (rv == CKR_SESSION_HANDLE_INVALID); + + length = sizeof (digest); + rv = (module->C_Digest) (session, (CK_BYTE_PTR)"BLAh", 4, digest, &length); + assert (rv == CKR_OK); + + assert_num_eq (1, length); + assert (memcmp (digest, "4", 1) == 0); + + rv = (module->C_DigestInit) (session, &mech); + assert (rv == CKR_OK); + + rv = (module->C_DigestUpdate) (0, (CK_BYTE_PTR)"blah", 4); + assert (rv == CKR_SESSION_HANDLE_INVALID); + + rv = (module->C_DigestUpdate) (session, (CK_BYTE_PTR)"sLuRM", 5); + assert (rv == CKR_OK); + + /* Adds the the value of object handle to hash: 6 */ + assert_num_eq (6, MOCK_PUBLIC_KEY_PREFIX); + rv = (module->C_DigestKey) (session, MOCK_PUBLIC_KEY_PREFIX); + assert (rv == CKR_OK); + + rv = (module->C_DigestUpdate) (session, (CK_BYTE_PTR)"Other", 5); + assert (rv == CKR_OK); + + length = sizeof (digest); + rv = (module->C_DigestFinal) (0, digest, &length); + assert (rv == CKR_SESSION_HANDLE_INVALID); + + length = sizeof (digest); + rv = (module->C_DigestFinal) (session, digest, &length); + assert (rv == CKR_OK); + + assert_num_eq (2, length); + assert (memcmp (digest, "16", 2) == 0); + + teardown_mock_module (module); +} + +static void +test_sign (void) +{ + CK_FUNCTION_LIST_PTR module; + CK_SESSION_HANDLE session = 0; + CK_MECHANISM mech = { CKM_MOCK_PREFIX, "prefix:", 7 }; + CK_BYTE signature[128]; + CK_ULONG length; + CK_RV rv; + + module = setup_mock_module (&session); + + rv = (module->C_Login) (session, CKU_USER, (CK_BYTE_PTR)"booo", 4); + assert (rv == CKR_OK); + + rv = (module->C_SignInit) (0, &mech, MOCK_PRIVATE_KEY_PREFIX); + assert (rv == CKR_SESSION_HANDLE_INVALID); + + rv = (module->C_SignInit) (session, &mech, MOCK_PRIVATE_KEY_PREFIX); + assert (rv == CKR_OK); + + rv = (module->C_Login) (session, CKU_CONTEXT_SPECIFIC, (CK_BYTE_PTR)"booo", 4); + assert (rv == CKR_OK); + + length = sizeof (signature); + rv = (module->C_Sign) (0, (CK_BYTE_PTR)"bLAH", 4, signature, &length); + assert (rv == CKR_SESSION_HANDLE_INVALID); + + length = sizeof (signature); + rv = (module->C_Sign) (session, (CK_BYTE_PTR)"BLAh", 4, signature, &length); + assert (rv == CKR_OK); + + assert_num_eq (13, length); + assert (memcmp (signature, "prefix:value4", 13) == 0); + + rv = (module->C_SignInit) (session, &mech, MOCK_PRIVATE_KEY_PREFIX); + assert (rv == CKR_OK); + + rv = (module->C_Login) (session, CKU_CONTEXT_SPECIFIC, (CK_BYTE_PTR)"booo", 4); + assert (rv == CKR_OK); + + rv = (module->C_SignUpdate) (0, (CK_BYTE_PTR)"blah", 4); + assert (rv == CKR_SESSION_HANDLE_INVALID); + + rv = (module->C_SignUpdate) (session, (CK_BYTE_PTR)"sLuRM", 5); + assert (rv == CKR_OK); + + rv = (module->C_SignUpdate) (session, (CK_BYTE_PTR)"Other", 5); + assert (rv == CKR_OK); + + length = sizeof (signature); + rv = (module->C_SignFinal) (0, signature, &length); + assert (rv == CKR_SESSION_HANDLE_INVALID); + + length = sizeof (signature); + rv = (module->C_SignFinal) (session, signature, &length); + assert (rv == CKR_OK); + + assert_num_eq (14, length); + assert (memcmp (signature, "prefix:value10", 2) == 0); + + teardown_mock_module (module); +} + +static void +test_sign_recover (void) +{ + CK_FUNCTION_LIST_PTR module; + CK_SESSION_HANDLE session = 0; + CK_MECHANISM mech = { CKM_MOCK_PREFIX, "prefix:", 7 }; + CK_BYTE signature[128]; + CK_ULONG length; + CK_RV rv; + + module = setup_mock_module (&session); + + rv = (module->C_Login) (session, CKU_USER, (CK_BYTE_PTR)"booo", 4); + assert (rv == CKR_OK); + + rv = (module->C_SignRecoverInit) (0, &mech, MOCK_PRIVATE_KEY_PREFIX); + assert (rv == CKR_SESSION_HANDLE_INVALID); + + rv = (module->C_SignRecoverInit) (session, &mech, MOCK_PRIVATE_KEY_PREFIX); + assert (rv == CKR_OK); + + rv = (module->C_Login) (session, CKU_CONTEXT_SPECIFIC, (CK_BYTE_PTR)"booo", 4); + assert (rv == CKR_OK); + + length = sizeof (signature); + rv = (module->C_SignRecover) (0, (CK_BYTE_PTR)"bLAH", 4, signature, &length); + assert (rv == CKR_SESSION_HANDLE_INVALID); + + length = sizeof (signature); + rv = (module->C_SignRecover) (session, (CK_BYTE_PTR)"BLAh", 4, signature, &length); + assert (rv == CKR_OK); + + assert_num_eq (16, length); + assert (memcmp (signature, "prefix:valueBLAh", 16) == 0); + + teardown_mock_module (module); +} + +static void +test_verify (void) +{ + CK_FUNCTION_LIST_PTR module; + CK_SESSION_HANDLE session = 0; + CK_MECHANISM mech = { CKM_MOCK_PREFIX, "prefix:", 7 }; + CK_BYTE signature[128]; + CK_ULONG length; + CK_RV rv; + + module = setup_mock_module (&session); + + rv = (module->C_VerifyInit) (0, &mech, MOCK_PUBLIC_KEY_PREFIX); + assert (rv == CKR_SESSION_HANDLE_INVALID); + + rv = (module->C_VerifyInit) (session, &mech, MOCK_PUBLIC_KEY_PREFIX); + assert (rv == CKR_OK); + + length = 13; + memcpy (signature, "prefix:value4", length); + rv = (module->C_Verify) (0, (CK_BYTE_PTR)"bLAH", 4, signature, 5); + assert (rv == CKR_SESSION_HANDLE_INVALID); + + rv = (module->C_Verify) (session, (CK_BYTE_PTR)"BLAh", 4, signature, length); + assert (rv == CKR_OK); + + rv = (module->C_VerifyInit) (session, &mech, MOCK_PUBLIC_KEY_PREFIX); + assert (rv == CKR_OK); + + rv = (module->C_VerifyUpdate) (0, (CK_BYTE_PTR)"blah", 4); + assert (rv == CKR_SESSION_HANDLE_INVALID); + + rv = (module->C_VerifyUpdate) (session, (CK_BYTE_PTR)"sLuRM", 5); + assert (rv == CKR_OK); + + rv = (module->C_VerifyUpdate) (session, (CK_BYTE_PTR)"Other", 5); + assert (rv == CKR_OK); + + length = 14; + memcpy (signature, "prefix:value10", length); + + rv = (module->C_VerifyFinal) (session, signature, 5); + assert (rv == CKR_SIGNATURE_LEN_RANGE); + + rv = (module->C_VerifyFinal) (session, signature, length); + assert (rv == CKR_OK); + + teardown_mock_module (module); +} + +static void +test_verify_recover (void) +{ + CK_FUNCTION_LIST_PTR module; + CK_SESSION_HANDLE session = 0; + CK_MECHANISM mech = { CKM_MOCK_PREFIX, "prefix:", 7 }; + CK_BYTE data[128]; + CK_ULONG length; + CK_RV rv; + + module = setup_mock_module (&session); + + rv = (module->C_VerifyRecoverInit) (0, &mech, MOCK_PUBLIC_KEY_PREFIX); + assert (rv == CKR_SESSION_HANDLE_INVALID); + + rv = (module->C_VerifyRecoverInit) (session, &mech, MOCK_PUBLIC_KEY_PREFIX); + assert (rv == CKR_OK); + + length = sizeof (data); + rv = (module->C_VerifyRecover) (0, (CK_BYTE_PTR)"prefix:valueBLah", 16, data, &length); + assert (rv == CKR_SESSION_HANDLE_INVALID); + + length = sizeof (data); + rv = (module->C_VerifyRecover) (session, (CK_BYTE_PTR)"prefix:valueBLah", 16, data, &length); + assert (rv == CKR_OK); + + assert_num_eq (4, length); + assert (memcmp (data, "BLah", 4) == 0); + + teardown_mock_module (module); +} + +static void +test_digest_encrypt (void) +{ + CK_FUNCTION_LIST_PTR module; + CK_SESSION_HANDLE session = 0; + CK_MECHANISM mech = { CKM_MOCK_CAPITALIZE, NULL, 0 }; + CK_MECHANISM dmech = { CKM_MOCK_COUNT, NULL, 0 }; + CK_BYTE data[128]; + CK_ULONG length; + CK_RV rv; + + module = setup_mock_module (&session); + + rv = (module->C_EncryptInit) (session, &mech, MOCK_PUBLIC_KEY_CAPITALIZE); + assert (rv == CKR_OK); + + rv = (module->C_DigestInit) (session, &dmech); + assert (rv == CKR_OK); + + length = sizeof (data); + rv = (module->C_DigestEncryptUpdate) (0, (CK_BYTE_PTR)"blah", 4, data, &length); + assert (rv == CKR_SESSION_HANDLE_INVALID); + + length = sizeof (data); + rv = (module->C_DigestEncryptUpdate) (session, (CK_BYTE_PTR)"blah", 4, data, &length); + assert (rv == CKR_OK); + + assert_num_eq (4, length); + assert (memcmp (data, "BLAH", 4) == 0); + + length = sizeof (data); + rv = (module->C_EncryptFinal) (session, data, &length); + assert (rv == CKR_OK); + + length = sizeof (data); + rv = (module->C_DigestFinal) (session, data, &length); + assert (rv == CKR_OK); + + assert_num_eq (1, length); + assert (memcmp (data, "4", 1) == 0); + + teardown_mock_module (module); +} + +static void +test_decrypt_digest (void) +{ + CK_FUNCTION_LIST_PTR module; + CK_SESSION_HANDLE session = 0; + CK_MECHANISM mech = { CKM_MOCK_CAPITALIZE, NULL, 0 }; + CK_MECHANISM dmech = { CKM_MOCK_COUNT, NULL, 0 }; + CK_BYTE data[128]; + CK_ULONG length; + CK_RV rv; + + module = setup_mock_module (&session); + + rv = (module->C_Login) (session, CKU_USER, (CK_BYTE_PTR)"booo", 4); + assert (rv == CKR_OK); + + rv = (module->C_DecryptInit) (session, &mech, MOCK_PRIVATE_KEY_CAPITALIZE); + assert (rv == CKR_OK); + + rv = (module->C_DigestInit) (session, &dmech); + assert (rv == CKR_OK); + + length = sizeof (data); + rv = (module->C_DecryptDigestUpdate) (0, (CK_BYTE_PTR)"BLAH", 4, data, &length); + assert (rv == CKR_SESSION_HANDLE_INVALID); + + length = sizeof (data); + rv = (module->C_DecryptDigestUpdate) (session, (CK_BYTE_PTR)"BLAH", 4, data, &length); + assert (rv == CKR_OK); + + assert_num_eq (4, length); + assert (memcmp (data, "blah", 4) == 0); + + length = sizeof (data); + rv = (module->C_DecryptFinal) (session, data, &length); + assert (rv == CKR_OK); + + length = sizeof (data); + rv = (module->C_DigestFinal) (session, data, &length); + assert (rv == CKR_OK); + + assert_num_eq (1, length); + assert (memcmp (data, "4", 1) == 0); + + teardown_mock_module (module); +} + +static void +test_sign_encrypt (void) +{ + CK_FUNCTION_LIST_PTR module; + CK_SESSION_HANDLE session = 0; + CK_MECHANISM mech = { CKM_MOCK_CAPITALIZE, NULL, 0 }; + CK_MECHANISM smech = { CKM_MOCK_PREFIX, "p:", 2 }; + CK_BYTE data[128]; + CK_ULONG length; + CK_RV rv; + + module = setup_mock_module (&session); + + rv = (module->C_Login) (session, CKU_USER, (CK_BYTE_PTR)"booo", 4); + assert (rv == CKR_OK); + + rv = (module->C_EncryptInit) (session, &mech, MOCK_PUBLIC_KEY_CAPITALIZE); + assert (rv == CKR_OK); + + rv = (module->C_SignInit) (session, &smech, MOCK_PRIVATE_KEY_PREFIX); + assert (rv == CKR_OK); + + rv = (module->C_Login) (session, CKU_CONTEXT_SPECIFIC, (CK_BYTE_PTR)"booo", 4); + assert (rv == CKR_OK); + + length = sizeof (data); + rv = (module->C_SignEncryptUpdate) (0, (CK_BYTE_PTR)"blah", 4, data, &length); + assert (rv == CKR_SESSION_HANDLE_INVALID); + + length = sizeof (data); + rv = (module->C_SignEncryptUpdate) (session, (CK_BYTE_PTR)"blah", 4, data, &length); + assert (rv == CKR_OK); + + assert_num_eq (4, length); + assert (memcmp (data, "BLAH", 4) == 0); + + length = sizeof (data); + rv = (module->C_EncryptFinal) (session, data, &length); + assert (rv == CKR_OK); + + length = sizeof (data); + rv = (module->C_SignFinal) (session, data, &length); + assert (rv == CKR_OK); + + assert_num_eq (8, length); + assert (memcmp (data, "p:value4", 1) == 0); + + teardown_mock_module (module); +} + +static void +test_decrypt_verify (void) +{ + CK_FUNCTION_LIST_PTR module; + CK_SESSION_HANDLE session = 0; + CK_MECHANISM mech = { CKM_MOCK_CAPITALIZE, NULL, 0 }; + CK_MECHANISM vmech = { CKM_MOCK_PREFIX, "p:", 2 }; + CK_BYTE data[128]; + CK_ULONG length; + CK_RV rv; + + module = setup_mock_module (&session); + + rv = (module->C_Login) (session, CKU_USER, (CK_BYTE_PTR)"booo", 4); + assert (rv == CKR_OK); + + rv = (module->C_DecryptInit) (session, &mech, MOCK_PRIVATE_KEY_CAPITALIZE); + assert (rv == CKR_OK); + + rv = (module->C_VerifyInit) (session, &vmech, MOCK_PUBLIC_KEY_PREFIX); + assert (rv == CKR_OK); + + length = sizeof (data); + rv = (module->C_DecryptVerifyUpdate) (0, (CK_BYTE_PTR)"BLAH", 4, data, &length); + assert (rv == CKR_SESSION_HANDLE_INVALID); + + length = sizeof (data); + rv = (module->C_DecryptVerifyUpdate) (session, (CK_BYTE_PTR)"BLAH", 4, data, &length); + assert (rv == CKR_OK); + + assert_num_eq (4, length); + assert (memcmp (data, "blah", 4) == 0); + + length = sizeof (data); + rv = (module->C_DecryptFinal) (session, data, &length); + assert (rv == CKR_OK); + + rv = (module->C_VerifyFinal) (session, (CK_BYTE_PTR)"p:value4", 8); + assert (rv == CKR_OK); + + teardown_mock_module (module); +} + +static void +test_generate_key (void) +{ + CK_FUNCTION_LIST_PTR module; + CK_SESSION_HANDLE session = 0; + CK_OBJECT_HANDLE object; + CK_MECHANISM mech = { CKM_MOCK_GENERATE, NULL, 0 }; + CK_ATTRIBUTE attrs[8]; + char label[32]; + char value[64]; + CK_ULONG bits; + CK_RV rv; + + module = setup_mock_module (&session); + + strcpy (label, "Blahooo"); + bits = 1555; + + attrs[0].type = CKA_LABEL; + attrs[0].pValue = label; + attrs[0].ulValueLen = strlen (label); + attrs[1].type = CKA_BITS_PER_PIXEL; + attrs[1].pValue = &bits; + attrs[1].ulValueLen = sizeof (bits); + + rv = (module->C_GenerateKey) (session, &mech, attrs, 2, &object); + assert (rv == CKR_MECHANISM_PARAM_INVALID); + + mech.pParameter = "generate"; + mech.ulParameterLen = 9; + + rv = (module->C_GenerateKey) (session, &mech, attrs, 2, &object); + assert (rv == CKR_OK); + + attrs[0].ulValueLen = sizeof (label); + memset (label, 0, sizeof (label)); + bits = 0; + attrs[2].type = CKA_VALUE; + attrs[2].pValue = value; + attrs[2].ulValueLen = sizeof (value); + + rv = (module->C_GetAttributeValue) (session, object, attrs, 3); + assert (rv == CKR_OK); + + assert_num_eq (bits, 1555); + assert_num_eq (7, attrs[0].ulValueLen); + assert (memcmp (label, "Blahooo", attrs[0].ulValueLen) == 0); + assert_num_eq (9, attrs[2].ulValueLen); + assert (memcmp (value, "generated", attrs[2].ulValueLen) == 0); + + teardown_mock_module (module); +} + +static void +test_generate_key_pair (void) +{ + CK_FUNCTION_LIST_PTR module; + CK_SESSION_HANDLE session = 0; + CK_OBJECT_HANDLE pub_object; + CK_OBJECT_HANDLE priv_object; + CK_MECHANISM mech = { CKM_MOCK_GENERATE, "generated", 9 }; + CK_ATTRIBUTE pub_attrs[8]; + CK_ATTRIBUTE priv_attrs[8]; + char pub_label[32]; + char pub_value[64]; + char priv_label[32]; + char priv_value[64]; + CK_ULONG pub_bits; + CK_ULONG priv_bits; + CK_RV rv; + + module = setup_mock_module (&session); + + strcpy (pub_label, "Blahooo"); + pub_bits = 1555; + pub_attrs[0].type = CKA_LABEL; + pub_attrs[0].pValue = pub_label; + pub_attrs[0].ulValueLen = strlen (pub_label); + pub_attrs[1].type = CKA_BITS_PER_PIXEL; + pub_attrs[1].pValue = &pub_bits; + pub_attrs[1].ulValueLen = sizeof (pub_bits); + + strcpy (priv_label, "Private"); + priv_bits = 1666; + priv_attrs[0].type = CKA_LABEL; + priv_attrs[0].pValue = priv_label; + priv_attrs[0].ulValueLen = strlen (priv_label); + priv_attrs[1].type = CKA_BITS_PER_PIXEL; + priv_attrs[1].pValue = &priv_bits; + priv_attrs[1].ulValueLen = sizeof (priv_bits); + + rv = (module->C_GenerateKeyPair) (0, &mech, pub_attrs, 2, priv_attrs, 2, + &pub_object, &priv_object); + assert (rv == CKR_SESSION_HANDLE_INVALID); + + mech.pParameter = "generate"; + mech.ulParameterLen = 9; + + rv = (module->C_GenerateKeyPair) (session, &mech, pub_attrs, 2, priv_attrs, 2, + &pub_object, &priv_object); + assert (rv == CKR_OK); + + pub_bits = 0; + pub_attrs[0].ulValueLen = sizeof (pub_label); + memset (pub_label, 0, sizeof (pub_label)); + pub_attrs[2].type = CKA_VALUE; + pub_attrs[2].pValue = pub_value; + pub_attrs[2].ulValueLen = sizeof (pub_value); + + rv = (module->C_GetAttributeValue) (session, pub_object, pub_attrs, 3); + assert (rv == CKR_OK); + + assert_num_eq (1555, pub_bits); + assert_num_eq (7, pub_attrs[0].ulValueLen); + assert (memcmp (pub_label, "Blahooo", pub_attrs[0].ulValueLen) == 0); + assert_num_eq (9, pub_attrs[2].ulValueLen); + assert (memcmp (pub_value, "generated", pub_attrs[2].ulValueLen) == 0); + + priv_bits = 0; + priv_attrs[0].ulValueLen = sizeof (priv_label); + memset (priv_label, 0, sizeof (priv_label)); + priv_attrs[2].type = CKA_VALUE; + priv_attrs[2].pValue = priv_value; + priv_attrs[2].ulValueLen = sizeof (priv_value); + + rv = (module->C_GetAttributeValue) (session, priv_object, priv_attrs, 3); + assert (rv == CKR_OK); + + assert_num_eq (1666, priv_bits); + assert_num_eq (7, priv_attrs[0].ulValueLen); + assert (memcmp (priv_label, "Private", priv_attrs[0].ulValueLen) == 0); + assert_num_eq (9, priv_attrs[2].ulValueLen); + assert (memcmp (priv_value, "generated", priv_attrs[2].ulValueLen) == 0); + + teardown_mock_module (module); +} + +static void +test_wrap_key (void) +{ + CK_FUNCTION_LIST_PTR module; + CK_SESSION_HANDLE session = 0; + CK_MECHANISM mech = { CKM_MOCK_WRAP, NULL, 0 }; + CK_BYTE data[128]; + CK_ULONG length; + CK_RV rv; + + module = setup_mock_module (&session); + + length = sizeof (data); + rv = (module->C_WrapKey) (session, &mech, MOCK_PUBLIC_KEY_PREFIX, MOCK_PUBLIC_KEY_PREFIX, data, &length); + assert (rv == CKR_MECHANISM_PARAM_INVALID); + + mech.pParameter = "wrap"; + mech.ulParameterLen = 4; + + rv = (module->C_WrapKey) (session, &mech, MOCK_PUBLIC_KEY_PREFIX, MOCK_PUBLIC_KEY_PREFIX, data, &length); + assert (rv == CKR_OK); + + assert_num_eq (5, length); + assert (memcmp (data, "value", 5) == 0); + + teardown_mock_module (module); +} + +static void +test_unwrap_key (void) +{ + CK_FUNCTION_LIST_PTR module; + CK_SESSION_HANDLE session = 0; + CK_OBJECT_HANDLE object; + CK_MECHANISM mech = { CKM_MOCK_WRAP, NULL, 0 }; + CK_ATTRIBUTE attrs[8]; + char label[32]; + char value[64]; + CK_ULONG bits; + CK_RV rv; + + module = setup_mock_module (&session); + + strcpy (label, "Blahooo"); + bits = 1555; + + attrs[0].type = CKA_LABEL; + attrs[0].pValue = label; + attrs[0].ulValueLen = strlen (label); + attrs[1].type = CKA_BITS_PER_PIXEL; + attrs[1].pValue = &bits; + attrs[1].ulValueLen = sizeof (bits); + + rv = (module->C_UnwrapKey) (session, &mech, MOCK_PUBLIC_KEY_PREFIX, + (CK_BYTE_PTR)"wheee", 5, attrs, 2, &object); + assert (rv == CKR_MECHANISM_PARAM_INVALID); + + mech.pParameter = "wrap"; + mech.ulParameterLen = 4; + + rv = (module->C_UnwrapKey) (session, &mech, MOCK_PUBLIC_KEY_PREFIX, + (CK_BYTE_PTR)"wheee", 5, attrs, 2, &object); + assert (rv == CKR_OK); + + attrs[0].ulValueLen = sizeof (label); + memset (label, 0, sizeof (label)); + bits = 0; + attrs[2].type = CKA_VALUE; + attrs[2].pValue = value; + attrs[2].ulValueLen = sizeof (value); + + rv = (module->C_GetAttributeValue) (session, object, attrs, 3); + assert (rv == CKR_OK); + + assert_num_eq (bits, 1555); + assert_num_eq (7, attrs[0].ulValueLen); + assert (memcmp (label, "Blahooo", attrs[0].ulValueLen) == 0); + assert_num_eq (5, attrs[2].ulValueLen); + assert (memcmp (value, "wheee", attrs[2].ulValueLen) == 0); + + teardown_mock_module (module); +} + +static void +test_derive_key (void) +{ + CK_FUNCTION_LIST_PTR module; + CK_SESSION_HANDLE session = 0; + CK_OBJECT_HANDLE object; + CK_MECHANISM mech = { CKM_MOCK_DERIVE, NULL, 0 }; + CK_ATTRIBUTE attrs[8]; + char label[32]; + char value[64]; + CK_ULONG bits; + CK_RV rv; + + module = setup_mock_module (&session); + + strcpy (label, "Blahooo"); + bits = 1555; + + attrs[0].type = CKA_LABEL; + attrs[0].pValue = label; + attrs[0].ulValueLen = strlen (label); + attrs[1].type = CKA_BITS_PER_PIXEL; + attrs[1].pValue = &bits; + attrs[1].ulValueLen = sizeof (bits); + + rv = (module->C_DeriveKey) (session, &mech, MOCK_PUBLIC_KEY_PREFIX, + attrs, 2, &object); + assert (rv == CKR_MECHANISM_PARAM_INVALID); + + mech.pParameter = "derive"; + mech.ulParameterLen = 6; + + rv = (module->C_DeriveKey) (session, &mech, MOCK_PUBLIC_KEY_PREFIX, + attrs, 2, &object); + assert (rv == CKR_OK); + + attrs[0].ulValueLen = sizeof (label); + memset (label, 0, sizeof (label)); + bits = 0; + attrs[2].type = CKA_VALUE; + attrs[2].pValue = value; + attrs[2].ulValueLen = sizeof (value); + + rv = (module->C_GetAttributeValue) (session, object, attrs, 3); + assert (rv == CKR_OK); + + assert_num_eq (bits, 1555); + assert_num_eq (7, attrs[0].ulValueLen); + assert (memcmp (label, "Blahooo", attrs[0].ulValueLen) == 0); + assert_num_eq (7, attrs[2].ulValueLen); + assert (memcmp (value, "derived", attrs[2].ulValueLen) == 0); + + teardown_mock_module (module); +} + +static void +test_random (void) +{ + CK_FUNCTION_LIST_PTR module; + CK_SESSION_HANDLE session = 0; + CK_BYTE data[10]; + CK_RV rv; + + module = setup_mock_module (&session); + + rv = (module->C_SeedRandom) (0, (CK_BYTE_PTR)"seed", 4); + assert (rv == CKR_SESSION_HANDLE_INVALID); + + rv = (module->C_SeedRandom) (session, (CK_BYTE_PTR)"seed", 4); + assert (rv == CKR_OK); + + rv = (module->C_GenerateRandom) (0, data, sizeof (data)); + assert (rv == CKR_SESSION_HANDLE_INVALID); + + rv = (module->C_GenerateRandom) (session, data, sizeof (data)); + assert (rv == CKR_OK); + + assert (memcmp (data, "seedseedse", sizeof (data)) == 0); + + teardown_mock_module (module); +} + +static void +test_mock_add_tests (const char *prefix) +{ + p11_test (test_get_info, "%s/test_get_info", prefix); + p11_test (test_get_slot_list, "%s/test_get_slot_list", prefix); + p11_test (test_get_slot_info, "%s/test_get_slot_info", prefix); + p11_test (test_get_token_info, "%s/test_get_token_info", prefix); + p11_test (test_get_mechanism_list, "%s/test_get_mechanism_list", prefix); + p11_test (test_get_mechanism_info, "%s/test_get_mechanism_info", prefix); + p11_test (test_init_token, "%s/test_init_token", prefix); + p11_test (test_wait_for_slot_event, "%s/test_wait_for_slot_event", prefix); + p11_test (test_open_close_session, "%s/test_open_close_session", prefix); + p11_test (test_close_all_sessions, "%s/test_close_all_sessions", prefix); + p11_test (test_get_function_status, "%s/test_get_function_status", prefix); + p11_test (test_cancel_function, "%s/test_cancel_function", prefix); + p11_test (test_get_session_info, "%s/test_get_session_info", prefix); + p11_test (test_init_pin, "%s/test_init_pin", prefix); + p11_test (test_set_pin, "%s/test_set_pin", prefix); + p11_test (test_operation_state, "%s/test_operation_state", prefix); + p11_test (test_login_logout, "%s/test_login_logout", prefix); + p11_test (test_get_attribute_value, "%s/test_get_attribute_value", prefix); + p11_test (test_set_attribute_value, "%s/test_set_attribute_value", prefix); + p11_test (test_create_object, "%s/test_create_object", prefix); + p11_test (test_copy_object, "%s/test_copy_object", prefix); + p11_test (test_destroy_object, "%s/test_destroy_object", prefix); + p11_test (test_get_object_size, "%s/test_get_object_size", prefix); + p11_test (test_find_objects, "%s/test_find_objects", prefix); + p11_test (test_encrypt, "%s/test_encrypt", prefix); + p11_test (test_decrypt, "%s/test_decrypt", prefix); + p11_test (test_digest, "%s/test_digest", prefix); + p11_test (test_sign, "%s/test_sign", prefix); + p11_test (test_sign_recover, "%s/test_sign_recover", prefix); + p11_test (test_verify, "%s/test_verify", prefix); + p11_test (test_verify_recover, "%s/test_verify_recover", prefix); + p11_test (test_digest_encrypt, "%s/test_digest_encrypt", prefix); + p11_test (test_decrypt_digest, "%s/test_decrypt_digest", prefix); + p11_test (test_sign_encrypt, "%s/test_sign_encrypt", prefix); + p11_test (test_decrypt_verify, "%s/test_decrypt_verify", prefix); + p11_test (test_generate_key, "%s/test_generate_key", prefix); + p11_test (test_generate_key_pair, "%s/test_generate_key_pair", prefix); + p11_test (test_wrap_key, "%s/test_wrap_key", prefix); + p11_test (test_unwrap_key, "%s/test_unwrap_key", prefix); + p11_test (test_derive_key, "%s/test_derive_key", prefix); + p11_test (test_random, "%s/test_random", prefix); +} diff --git a/p11-kit/tests/test-modules.c b/p11-kit/tests/test-modules.c index 3a6e968..d50b2d5 100644 --- a/p11-kit/tests/test-modules.c +++ b/p11-kit/tests/test-modules.c @@ -33,7 +33,7 @@ */ #include "config.h" -#include "CuTest.h" +#include "test.h" #include <errno.h> #include <stdlib.h> @@ -47,32 +47,24 @@ #include "dict.h" static CK_FUNCTION_LIST_PTR_PTR -initialize_and_get_modules (CuTest *tc) +initialize_and_get_modules (void) { CK_FUNCTION_LIST_PTR_PTR modules; - CK_RV rv; - rv = p11_kit_initialize_registered (); - CuAssertIntEquals (tc, CKR_OK, rv); - modules = p11_kit_registered_modules (); - CuAssertTrue (tc, modules != NULL && modules[0] != NULL); + modules = p11_kit_modules_load_and_initialize (0); + assert (modules != NULL && modules[0] != NULL); return modules; } static void -finalize_and_free_modules (CuTest *tc, - CK_FUNCTION_LIST_PTR_PTR modules) +finalize_and_free_modules (CK_FUNCTION_LIST_PTR_PTR modules) { - CK_RV rv; - - free (modules); - rv = p11_kit_finalize_registered (); - CuAssertIntEquals (tc, CKR_OK, rv); + p11_kit_modules_finalize_and_release (modules); } static void -test_no_duplicates (CuTest *tc) +test_no_duplicates (void) { CK_FUNCTION_LIST_PTR_PTR modules; p11_dict *paths; @@ -80,35 +72,34 @@ test_no_duplicates (CuTest *tc) char *path; int i; - modules = initialize_and_get_modules (tc); + modules = initialize_and_get_modules (); paths = p11_dict_new (p11_dict_str_hash, p11_dict_str_equal, NULL, NULL); funcs = p11_dict_new (p11_dict_direct_hash, p11_dict_direct_equal, NULL, NULL); /* The loaded modules should not contain duplicates */ for (i = 0; modules[i] != NULL; i++) { - path = p11_kit_registered_option (modules[i], "module"); + path = p11_kit_config_option (modules[i], "module"); if (p11_dict_get (funcs, modules[i])) - CuAssert (tc, "found duplicate function list pointer", 0); + assert_fail ("found duplicate function list pointer", NULL); if (p11_dict_get (paths, path)) - CuAssert (tc, "found duplicate path name", 0); + assert_fail ("found duplicate path name", NULL); if (!p11_dict_set (funcs, modules[i], "")) - CuAssert (tc, "shouldn't be reached", 0); + assert_not_reached (); if (!p11_dict_set (paths, path, "")) - CuAssert (tc, "shouldn't be reached", 0); + assert_not_reached (); free (path); } p11_dict_free (paths); p11_dict_free (funcs); - finalize_and_free_modules (tc, modules); + finalize_and_free_modules (modules); } static CK_FUNCTION_LIST_PTR -lookup_module_with_name (CuTest *tc, - CK_FUNCTION_LIST_PTR_PTR modules, +lookup_module_with_name (CK_FUNCTION_LIST_PTR_PTR modules, const char *name) { CK_FUNCTION_LIST_PTR match = NULL; @@ -117,8 +108,8 @@ lookup_module_with_name (CuTest *tc, int i; for (i = 0; match == NULL && modules[i] != NULL; i++) { - module_name = p11_kit_registered_module_to_name (modules[i]); - CuAssertPtrNotNull (tc, module_name); + module_name = p11_kit_module_get_name (modules[i]); + assert_ptr_not_null (module_name); if (strcmp (module_name, name) == 0) match = modules[i]; free (module_name); @@ -128,15 +119,15 @@ lookup_module_with_name (CuTest *tc, * As a side effect, we should check that the results of this function * matches the above search. */ - module = p11_kit_registered_name_to_module (name); - CuAssert(tc, "different result from p11_kit_registered_name_to_module()", - module == match); + module = p11_kit_module_for_name (modules, name); + if (module != match) + assert_fail ("different result from p11_kit_module_for_name ()", NULL); return match; } static void -test_disable (CuTest *tc) +test_disable (void) { CK_FUNCTION_LIST_PTR_PTR modules; @@ -145,9 +136,9 @@ test_disable (CuTest *tc) * that it has disabled. */ - modules = initialize_and_get_modules (tc); - CuAssertTrue (tc, lookup_module_with_name (tc, modules, "four") != NULL); - finalize_and_free_modules (tc, modules); + modules = initialize_and_get_modules (); + assert (lookup_module_with_name (modules, "four") != NULL); + finalize_and_free_modules (modules); /* * The module two shouldn't have been loaded, because in its config @@ -158,18 +149,17 @@ test_disable (CuTest *tc) p11_kit_set_progname ("test-disable"); - modules = initialize_and_get_modules (tc); - CuAssertTrue (tc, lookup_module_with_name (tc, modules, "four") == NULL); - finalize_and_free_modules (tc, modules); + modules = initialize_and_get_modules (); + assert (lookup_module_with_name (modules, "four") == NULL); + finalize_and_free_modules (modules); p11_kit_set_progname (NULL); } static void -test_disable_later (CuTest *tc) +test_disable_later (void) { CK_FUNCTION_LIST_PTR_PTR modules; - CK_RV rv; /* * The module two shouldn't be matched, because in its config @@ -178,22 +168,19 @@ test_disable_later (CuTest *tc) * disable-in: test-disable */ - rv = p11_kit_initialize_registered (); - CuAssertIntEquals (tc, CKR_OK, rv); - p11_kit_set_progname ("test-disable"); - modules = p11_kit_registered_modules (); - CuAssertTrue (tc, modules != NULL && modules[0] != NULL); + modules = p11_kit_modules_load_and_initialize (0); + assert (modules != NULL && modules[0] != NULL); - CuAssertTrue (tc, lookup_module_with_name (tc, modules, "two") == NULL); - finalize_and_free_modules (tc, modules); + assert (lookup_module_with_name (modules, "two") == NULL); + finalize_and_free_modules (modules); p11_kit_set_progname (NULL); } static void -test_enable (CuTest *tc) +test_enable (void) { CK_FUNCTION_LIST_PTR_PTR modules; @@ -202,9 +189,9 @@ test_enable (CuTest *tc) * program. */ - modules = initialize_and_get_modules (tc); - CuAssertTrue (tc, lookup_module_with_name (tc, modules, "three") == NULL); - finalize_and_free_modules (tc, modules); + modules = initialize_and_get_modules (); + assert (lookup_module_with_name (modules, "three") == NULL); + finalize_and_free_modules (modules); /* * The module three should be loaded here , because in its config @@ -215,15 +202,15 @@ test_enable (CuTest *tc) p11_kit_set_progname ("test-enable"); - modules = initialize_and_get_modules (tc); - CuAssertTrue (tc, lookup_module_with_name (tc, modules, "three") != NULL); - finalize_and_free_modules (tc, modules); + modules = initialize_and_get_modules (); + assert (lookup_module_with_name (modules, "three") != NULL); + finalize_and_free_modules (modules); p11_kit_set_progname (NULL); } static void -test_priority (CuTest *tc) +test_priority (void) { CK_FUNCTION_LIST_PTR_PTR modules; char *name; @@ -242,12 +229,12 @@ test_priority (CuTest *tc) /* This enables module three */ p11_kit_set_progname ("test-enable"); - modules = initialize_and_get_modules (tc); + modules = initialize_and_get_modules (); /* The loaded modules should not contain duplicates */ for (i = 0; modules[i] != NULL; i++) { - name = p11_kit_registered_module_to_name (modules[i]); - CuAssertPtrNotNull (tc, name); + name = p11_kit_module_get_name (modules[i]); + assert_ptr_not_null (name); /* Either one of these can be loaded, as this is a duplicate module */ if (strcmp (name, "two-duplicate") == 0) { @@ -255,38 +242,124 @@ test_priority (CuTest *tc) name = strdup ("two.badname"); } - CuAssertStrEquals (tc, expected[i], name); + assert_str_eq (expected[i], name); free (name); } - CuAssertIntEquals (tc, 4, i); - finalize_and_free_modules (tc, modules); + assert_num_eq (4, i); + finalize_and_free_modules (modules); } -int -main (void) +static void +test_module_name (void) { - CuString *output = CuStringNew (); - CuSuite* suite = CuSuiteNew (); - int ret; + CK_FUNCTION_LIST_PTR_PTR modules; + CK_FUNCTION_LIST_PTR module; + char *name; + + /* + * The module three should not be present, as we don't match the current + * program. + */ + + modules = initialize_and_get_modules (); + + module = p11_kit_module_for_name (modules, "one"); + assert_ptr_not_null (module); + name = p11_kit_module_get_name (module); + assert_str_eq ("one", name); + free (name); + + module = p11_kit_module_for_name (modules, "invalid"); + assert_ptr_eq (NULL, module); + + module = p11_kit_module_for_name (NULL, "one"); + assert_ptr_eq (NULL, module); + + finalize_and_free_modules (modules); +} + +static void +test_module_flags (void) +{ + CK_FUNCTION_LIST **modules; + CK_FUNCTION_LIST **unmanaged; + int flags; + + /* + * The module three should not be present, as we don't match the current + * program. + */ + + modules = initialize_and_get_modules (); - putenv ("P11_KIT_STRICT=1"); + flags = p11_kit_module_get_flags (modules[0]); + assert_num_eq (0, flags); + + unmanaged = p11_kit_modules_load (NULL, P11_KIT_MODULE_UNMANAGED); + assert (unmanaged != NULL && unmanaged[0] != NULL); + + flags = p11_kit_module_get_flags (unmanaged[0]); + assert_num_eq (P11_KIT_MODULE_UNMANAGED, flags); + + finalize_and_free_modules (modules); + p11_kit_modules_release (unmanaged); +} + +static void +test_config_option (void) +{ + CK_FUNCTION_LIST_PTR_PTR modules; + CK_FUNCTION_LIST_PTR module; + char *value; + + /* + * The module three should not be present, as we don't match the current + * program. + */ + + modules = initialize_and_get_modules (); + + value = p11_kit_config_option (NULL, "new"); + assert_str_eq ("world", value); + free (value); + + module = p11_kit_module_for_name (modules, "one"); + assert_ptr_not_null (module); + + value = p11_kit_config_option (module, "setting"); + assert_str_eq ("user1", value); + free (value); + + value = p11_kit_config_option (NULL, "invalid"); + assert_ptr_eq (NULL, value); + + value = p11_kit_config_option (module, "invalid"); + assert_ptr_eq (NULL, value); + + /* Invalid but non-NULL module pointer */ + value = p11_kit_config_option (module + 1, "setting"); + assert_ptr_eq (NULL, value); + + finalize_and_free_modules (modules); +} + +int +main (int argc, + char *argv[]) +{ p11_library_init (); - SUITE_ADD_TEST (suite, test_no_duplicates); - SUITE_ADD_TEST (suite, test_disable); - SUITE_ADD_TEST (suite, test_disable_later); - SUITE_ADD_TEST (suite, test_enable); - SUITE_ADD_TEST (suite, test_priority); + p11_test (test_no_duplicates, "/modules/test_no_duplicates"); + p11_test (test_disable, "/modules/test_disable"); + p11_test (test_disable_later, "/modules/test_disable_later"); + p11_test (test_enable, "/modules/test_enable"); + p11_test (test_priority, "/modules/test_priority"); + p11_test (test_module_name, "/modules/test_module_name"); + p11_test (test_module_flags, "/modules/test_module_flags"); + p11_test (test_config_option, "/modules/test_config_option"); p11_kit_be_quiet (); - CuSuiteRun (suite); - CuSuiteSummary (suite, output); - CuSuiteDetails (suite, output); - printf ("%s\n", output->buffer); - ret = suite->failCount; - CuSuiteDelete (suite); - CuStringDelete (output); - return ret; + return p11_test_run (argc, argv); } diff --git a/p11-kit/tests/pin-test.c b/p11-kit/tests/test-pin.c index dd020bc..ebe3efc 100644 --- a/p11-kit/tests/pin-test.c +++ b/p11-kit/tests/test-pin.c @@ -33,7 +33,7 @@ */ #include "config.h" -#include "CuTest.h" +#include "test.h" #include "library.h" @@ -71,7 +71,7 @@ destroy_data (void *callback_data) } static void -test_pin_register_unregister (CuTest *tc) +test_pin_register_unregister (void) { int data = 33; @@ -81,11 +81,11 @@ test_pin_register_unregister (CuTest *tc) p11_kit_pin_unregister_callback ("/the/pin_source", callback_one, &data); - CuAssertIntEquals (tc, 34, data); + assert_num_eq (34, data); } static void -test_pin_read (CuTest *tc) +test_pin_read (void) { P11KitUri *uri; P11KitPin *pin; @@ -101,10 +101,10 @@ test_pin_read (CuTest *tc) P11_KIT_PIN_FLAGS_USER_LOGIN); p11_kit_uri_free (uri); - CuAssertPtrNotNull (tc, pin); + assert_ptr_not_null (pin); ptr = p11_kit_pin_get_value (pin, &length); - CuAssertIntEquals (tc, 3, length); - CuAssertTrue (tc, memcmp (ptr, "one", 3) == 0); + assert_num_eq (3, length); + assert (memcmp (ptr, "one", 3) == 0); p11_kit_pin_unregister_callback ("/the/pin_source", callback_one, &data); @@ -113,7 +113,7 @@ test_pin_read (CuTest *tc) } static void -test_pin_read_no_match (CuTest *tc) +test_pin_read_no_match (void) { P11KitUri *uri; P11KitPin *pin; @@ -123,11 +123,11 @@ test_pin_read_no_match (CuTest *tc) P11_KIT_PIN_FLAGS_USER_LOGIN); p11_kit_uri_free (uri); - CuAssertPtrEquals (tc, NULL, pin); + assert_ptr_eq (NULL, pin); } static void -test_pin_register_duplicate (CuTest *tc) +test_pin_register_duplicate (void) { P11KitUri *uri; P11KitPin *pin; @@ -147,10 +147,10 @@ test_pin_register_duplicate (CuTest *tc) pin = p11_kit_pin_request ("/the/pin_source", uri, "The token", P11_KIT_PIN_FLAGS_USER_LOGIN); - CuAssertPtrNotNull (tc, pin); + assert_ptr_not_null (pin); ptr = p11_kit_pin_get_value (pin, &length); - CuAssertIntEquals (tc, 6, length); - CuAssertTrue (tc, memcmp (ptr, "secret", length) == 0); + assert_num_eq (6, length); + assert (memcmp (ptr, "secret", length) == 0); p11_kit_pin_unref (pin); p11_kit_pin_unregister_callback ("/the/pin_source", callback_other, @@ -159,10 +159,10 @@ test_pin_register_duplicate (CuTest *tc) pin = p11_kit_pin_request ("/the/pin_source", uri, "The token", P11_KIT_PIN_FLAGS_USER_LOGIN); - CuAssertPtrNotNull (tc, pin); + assert_ptr_not_null (pin); ptr = p11_kit_pin_get_value (pin, &length); - CuAssertIntEquals (tc, 3, length); - CuAssertTrue (tc, memcmp (ptr, "one", length) == 0); + assert_num_eq (3, length); + assert (memcmp (ptr, "one", length) == 0); p11_kit_pin_unref (pin); p11_kit_pin_unregister_callback ("/the/pin_source", callback_one, @@ -171,13 +171,13 @@ test_pin_register_duplicate (CuTest *tc) pin = p11_kit_pin_request ("/the/pin_source", uri, "The token", P11_KIT_PIN_FLAGS_USER_LOGIN); - CuAssertPtrEquals (tc, NULL, pin); + assert_ptr_eq (NULL, pin); p11_kit_uri_free (uri); } static void -test_pin_register_fallback (CuTest *tc) +test_pin_register_fallback (void) { char *value = "secret"; P11KitUri *uri; @@ -194,10 +194,10 @@ test_pin_register_fallback (CuTest *tc) pin = p11_kit_pin_request ("/the/pin_source", uri, "The token", P11_KIT_PIN_FLAGS_USER_LOGIN); - CuAssertPtrNotNull (tc, pin); + assert_ptr_not_null (pin); ptr = p11_kit_pin_get_value (pin, &length); - CuAssertIntEquals (tc, 3, length); - CuAssertTrue (tc, memcmp (ptr, "one", length) == 0); + assert_num_eq (3, length); + assert (memcmp (ptr, "one", length) == 0); p11_kit_pin_unref (pin); p11_kit_pin_register_callback ("/the/pin_source", callback_other, @@ -206,10 +206,10 @@ test_pin_register_fallback (CuTest *tc) pin = p11_kit_pin_request ("/the/pin_source", uri, "The token", P11_KIT_PIN_FLAGS_USER_LOGIN); - CuAssertPtrNotNull (tc, pin); + assert_ptr_not_null (pin); ptr = p11_kit_pin_get_value (pin, &length); - CuAssertIntEquals (tc, 6, length); - CuAssertTrue (tc, memcmp (ptr, "secret", length) == 0); + assert_num_eq (6, length); + assert (memcmp (ptr, "secret", length) == 0); p11_kit_pin_unref (pin); p11_kit_pin_unregister_callback ("/the/pin_source", callback_other, @@ -222,7 +222,7 @@ test_pin_register_fallback (CuTest *tc) } static void -test_pin_file (CuTest *tc) +test_pin_file (void) { P11KitUri *uri; P11KitPin *pin; @@ -237,16 +237,16 @@ test_pin_file (CuTest *tc) pin = p11_kit_pin_request (SRCDIR "/files/test-pinfile", uri, "The token", P11_KIT_PIN_FLAGS_USER_LOGIN); - CuAssertPtrNotNull (tc, pin); + assert_ptr_not_null (pin); ptr = p11_kit_pin_get_value (pin, &length); - CuAssertIntEquals (tc, 12, length); - CuAssertTrue (tc, memcmp (ptr, "yogabbagabba", length) == 0); + assert_num_eq (12, length); + assert (memcmp (ptr, "yogabbagabba", length) == 0); p11_kit_pin_unref (pin); pin = p11_kit_pin_request (SRCDIR "/files/nonexistant", uri, "The token", P11_KIT_PIN_FLAGS_USER_LOGIN); - CuAssertPtrEquals (tc, NULL, pin); + assert_ptr_eq (NULL, pin); p11_kit_pin_unregister_callback (P11_KIT_PIN_FALLBACK, p11_kit_pin_file_callback, NULL); @@ -255,7 +255,7 @@ test_pin_file (CuTest *tc) } static void -test_pin_file_large (CuTest *tc) +test_pin_file_large (void) { P11KitUri *uri; P11KitPin *pin; @@ -270,8 +270,8 @@ test_pin_file_large (CuTest *tc) P11_KIT_PIN_FLAGS_USER_LOGIN); error = errno; - CuAssertPtrEquals (tc, NULL, pin); - CuAssertIntEquals (tc, EFBIG, error); + assert_ptr_eq (NULL, pin); + assert_num_eq (EFBIG, error); p11_kit_pin_unregister_callback (P11_KIT_PIN_FALLBACK, p11_kit_pin_file_callback, NULL); @@ -280,7 +280,7 @@ test_pin_file_large (CuTest *tc) } static void -test_pin_ref_unref (CuTest *tc) +test_pin_ref_unref (void) { P11KitPin *pin; P11KitPin *check; @@ -288,38 +288,26 @@ test_pin_ref_unref (CuTest *tc) pin = p11_kit_pin_new_for_string ("crack of lies"); check = p11_kit_pin_ref (pin); - CuAssertPtrEquals (tc, pin, check); + assert_ptr_eq (pin, check); p11_kit_pin_unref (pin); p11_kit_pin_unref (check); } int -main (void) +main (int argc, + char *argv[]) { - CuString *output = CuStringNew (); - CuSuite* suite = CuSuiteNew (); - int ret; - - putenv ("P11_KIT_STRICT=1"); p11_library_init (); - SUITE_ADD_TEST (suite, test_pin_register_unregister); - SUITE_ADD_TEST (suite, test_pin_read); - SUITE_ADD_TEST (suite, test_pin_read_no_match); - SUITE_ADD_TEST (suite, test_pin_register_duplicate); - SUITE_ADD_TEST (suite, test_pin_register_fallback); - SUITE_ADD_TEST (suite, test_pin_file); - SUITE_ADD_TEST (suite, test_pin_file_large); - SUITE_ADD_TEST (suite, test_pin_ref_unref); - - CuSuiteRun (suite); - CuSuiteSummary (suite, output); - CuSuiteDetails (suite, output); - printf ("%s\n", output->buffer); - ret = suite->failCount; - CuSuiteDelete (suite); - CuStringDelete (output); - - return ret; + p11_test (test_pin_register_unregister, "/pin/test_pin_register_unregister"); + p11_test (test_pin_read, "/pin/test_pin_read"); + p11_test (test_pin_read_no_match, "/pin/test_pin_read_no_match"); + p11_test (test_pin_register_duplicate, "/pin/test_pin_register_duplicate"); + p11_test (test_pin_register_fallback, "/pin/test_pin_register_fallback"); + p11_test (test_pin_file, "/pin/test_pin_file"); + p11_test (test_pin_file_large, "/pin/test_pin_file_large"); + p11_test (test_pin_ref_unref, "/pin/test_pin_ref_unref"); + + return p11_test_run (argc, argv); } diff --git a/p11-kit/tests/progname-test.c b/p11-kit/tests/test-progname.c index 18a8c55..76b136d 100644 --- a/p11-kit/tests/progname-test.c +++ b/p11-kit/tests/test-progname.c @@ -33,7 +33,7 @@ */ #include "config.h" -#include "CuTest.h" +#include "test.h" #include "library.h" @@ -47,52 +47,40 @@ #include "p11-kit/private.h" static void -test_progname_default (CuTest *tc) +test_progname_default (void) { const char *progname; progname = _p11_get_progname_unlocked (); - CuAssertStrEquals (tc, "progname-test", progname); + assert_str_eq ("test-progname", progname); } static void -test_progname_set (CuTest *tc) +test_progname_set (void) { const char *progname; p11_kit_set_progname ("love-generation"); progname = _p11_get_progname_unlocked (); - CuAssertStrEquals (tc, "love-generation", progname); + assert_str_eq ("love-generation", progname); _p11_set_progname_unlocked (NULL); progname = _p11_get_progname_unlocked (); - CuAssertStrEquals (tc, "progname-test", progname); + assert_str_eq ("test-progname", progname); } /* Defined in util.c */ extern char p11_my_progname[]; int -main (void) +main (int argc, + char *argv[]) { - CuString *output = CuStringNew (); - CuSuite* suite = CuSuiteNew (); - int ret; - - putenv ("P11_KIT_STRICT=1"); p11_library_init (); - SUITE_ADD_TEST (suite, test_progname_default); - SUITE_ADD_TEST (suite, test_progname_set); - - CuSuiteRun (suite); - CuSuiteSummary (suite, output); - CuSuiteDetails (suite, output); - printf ("%s\n", output->buffer); - ret = suite->failCount; - CuSuiteDelete (suite); - CuStringDelete (output); - return ret; + p11_test (test_progname_default, "/progname/test_progname_default"); + p11_test (test_progname_set, "/progname/test_progname_set"); + return p11_test_run (argc, argv); } diff --git a/p11-kit/tests/test-proxy.c b/p11-kit/tests/test-proxy.c new file mode 100644 index 0000000..bf5007d --- /dev/null +++ b/p11-kit/tests/test-proxy.c @@ -0,0 +1,195 @@ +/* + * Copyright (c) 2013 Red Hat Inc + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * * The names of contributors to this software may not be + * used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * Author: Stef Walter <stefw@redhat.com> + */ + +#define CRYPTOKI_EXPORTS + +#include "config.h" +#include "test.h" + +#include "library.h" +#include "mock.h" +#include "p11-kit.h" +#include "pkcs11.h" +#include "proxy.h" + +#include <sys/types.h> + +#include <assert.h> +#include <errno.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <time.h> +#include <unistd.h> + +/* This is the proxy module entry point in proxy.c, and linked to this test */ +CK_RV C_GetFunctionList (CK_FUNCTION_LIST_PTR_PTR list); + +static CK_SLOT_ID mock_slot_one_id; +static CK_SLOT_ID mock_slot_two_id; +static CK_ULONG mock_slots_present; +static CK_ULONG mock_slots_all; + +static void +test_initialize_finalize (void) +{ + CK_FUNCTION_LIST_PTR proxy; + CK_RV rv; + + rv = C_GetFunctionList (&proxy); + assert (rv == CKR_OK); + + assert (p11_proxy_module_check (proxy)); + + rv = proxy->C_Initialize (NULL); + assert (rv == CKR_OK); + + rv = proxy->C_Finalize (NULL); + assert (rv == CKR_OK); + + p11_proxy_module_cleanup (); +} + +static void +test_initialize_multiple (void) +{ + CK_FUNCTION_LIST_PTR proxy; + CK_RV rv; + + rv = C_GetFunctionList (&proxy); + assert (rv == CKR_OK); + + assert (p11_proxy_module_check (proxy)); + + rv = proxy->C_Initialize (NULL); + assert (rv == CKR_OK); + + rv = proxy->C_Initialize (NULL); + assert (rv == CKR_OK); + + rv = proxy->C_Finalize (NULL); + assert (rv == CKR_OK); + + rv = proxy->C_Finalize (NULL); + assert (rv == CKR_OK); + + rv = proxy->C_Finalize (NULL); + assert (rv == CKR_CRYPTOKI_NOT_INITIALIZED); + + p11_proxy_module_cleanup (); +} + +static CK_FUNCTION_LIST_PTR +setup_mock_module (CK_SESSION_HANDLE *session) +{ + CK_FUNCTION_LIST_PTR proxy; + CK_SLOT_ID slots[32]; + CK_RV rv; + + rv = C_GetFunctionList (&proxy); + assert (rv == CKR_OK); + + assert (p11_proxy_module_check (proxy)); + + rv = proxy->C_Initialize (NULL); + assert (rv == CKR_OK); + + mock_slots_all = 32; + rv = proxy->C_GetSlotList (CK_FALSE, slots, &mock_slots_all); + assert (rv == CKR_OK); + assert (mock_slots_all >= 2); + + /* Assume this is the slot we want to deal with */ + mock_slot_one_id = slots[0]; + mock_slot_two_id = slots[1]; + + rv = proxy->C_GetSlotList (CK_TRUE, NULL, &mock_slots_present); + assert (rv == CKR_OK); + assert (mock_slots_present > 1); + + if (session) { + rv = (proxy->C_OpenSession) (mock_slot_one_id, + CKF_RW_SESSION | CKF_SERIAL_SESSION, + NULL, NULL, session); + assert (rv == CKR_OK); + } + + return proxy; +} + +static void +teardown_mock_module (CK_FUNCTION_LIST_PTR module) +{ + CK_RV rv; + + rv = module->C_Finalize (NULL); + assert (rv == CKR_OK); +} + +/* + * We redefine the mock module slot id so that the tests in test-mock.c + * use the proxy mapped slot id rather than the hard coded one + */ +#define MOCK_SLOT_ONE_ID mock_slot_one_id +#define MOCK_SLOT_TWO_ID mock_slot_two_id +#define MOCK_SLOTS_PRESENT mock_slots_present +#define MOCK_SLOTS_ALL mock_slots_all +#define MOCK_INFO mock_info +#define MOCK_SKIP_WAIT_TEST + +static const CK_INFO mock_info = { + { CRYPTOKI_VERSION_MAJOR, CRYPTOKI_VERSION_MINOR }, + "PKCS#11 Kit ", + 0, + "PKCS#11 Kit Proxy Module ", + { 1, 1 } +}; + +/* Bring in all the mock module tests */ +#include "test-mock.c" + +int +main (int argc, + char *argv[]) +{ + p11_library_init (); + p11_kit_be_quiet (); + + p11_test (test_initialize_finalize, "/proxy/initialize-finalize"); + p11_test (test_initialize_multiple, "/proxy/initialize-multiple"); + + test_mock_add_tests ("/proxy"); + + return p11_test_run (argc, argv); +} diff --git a/p11-kit/tests/uri-test.c b/p11-kit/tests/test-uri.c index 2bc121c..f514f7a 100644 --- a/p11-kit/tests/uri-test.c +++ b/p11-kit/tests/test-uri.c @@ -33,7 +33,7 @@ */ #include "config.h" -#include "CuTest.h" +#include "test.h" #include "debug.h" #include "message.h" @@ -75,138 +75,138 @@ are_attributes_empty (P11KitUri *uri) } static void -test_uri_parse (CuTest *tc) +test_uri_parse (void) { P11KitUri *uri; int ret; uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); + assert_ptr_not_null (uri); ret = p11_kit_uri_parse ("pkcs11:", P11_KIT_URI_FOR_MODULE, uri); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + assert_num_eq (P11_KIT_URI_OK, ret); - CuAssertTrue (tc, is_module_empty (uri)); - CuAssertTrue (tc, is_token_empty (uri)); - CuAssertTrue (tc, are_attributes_empty (uri)); + assert (is_module_empty (uri)); + assert (is_token_empty (uri)); + assert (are_attributes_empty (uri)); p11_kit_uri_free (uri); } static void -test_uri_parse_bad_scheme (CuTest *tc) +test_uri_parse_bad_scheme (void) { P11KitUri *uri; int ret; uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); + assert_ptr_not_null (uri); ret = p11_kit_uri_parse ("http:\\example.com\test", P11_KIT_URI_FOR_ANY, uri); - CuAssertIntEquals (tc, P11_KIT_URI_BAD_SCHEME, ret); + assert_num_eq (P11_KIT_URI_BAD_SCHEME, ret); p11_kit_uri_free (uri); } static void -test_uri_parse_with_label (CuTest *tc) +test_uri_parse_with_label (void) { CK_ATTRIBUTE_PTR attr; P11KitUri *uri; int ret; uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); + assert_ptr_not_null (uri); ret = p11_kit_uri_parse ("pkcs11:object=Test%20Label", P11_KIT_URI_FOR_ANY, uri); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + assert_num_eq (P11_KIT_URI_OK, ret); - CuAssertTrue (tc, is_module_empty (uri)); - CuAssertTrue (tc, is_token_empty (uri)); + assert (is_module_empty (uri)); + assert (is_token_empty (uri)); attr = p11_kit_uri_get_attribute (uri, CKA_LABEL); - CuAssertPtrNotNull (tc, attr); - CuAssertTrue (tc, attr->ulValueLen == strlen ("Test Label")); - CuAssertTrue (tc, memcmp (attr->pValue, "Test Label", attr->ulValueLen) == 0); + assert_ptr_not_null (attr); + assert (attr->ulValueLen == strlen ("Test Label")); + assert (memcmp (attr->pValue, "Test Label", attr->ulValueLen) == 0); p11_kit_uri_free (uri); } static void -test_uri_parse_with_label_and_klass (CuTest *tc) +test_uri_parse_with_label_and_klass (void) { CK_ATTRIBUTE_PTR attr; P11KitUri *uri; int ret; uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); + assert_ptr_not_null (uri); ret = p11_kit_uri_parse ("pkcs11:object=Test%20Label;object-type=cert", P11_KIT_URI_FOR_ANY, uri); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + assert_num_eq (P11_KIT_URI_OK, ret); attr = p11_kit_uri_get_attribute (uri, CKA_LABEL); - CuAssertPtrNotNull (tc, attr); - CuAssertTrue (tc, attr->ulValueLen == strlen ("Test Label")); - CuAssertTrue (tc, memcmp (attr->pValue, "Test Label", attr->ulValueLen) == 0); + assert_ptr_not_null (attr); + assert (attr->ulValueLen == strlen ("Test Label")); + assert (memcmp (attr->pValue, "Test Label", attr->ulValueLen) == 0); attr = p11_kit_uri_get_attribute (uri, CKA_CLASS); - CuAssertPtrNotNull (tc, attr); - CuAssertTrue (tc, attr->ulValueLen == sizeof (CK_OBJECT_CLASS)); - CuAssertTrue (tc, *((CK_OBJECT_CLASS_PTR)attr->pValue) == CKO_CERTIFICATE); + assert_ptr_not_null (attr); + assert (attr->ulValueLen == sizeof (CK_OBJECT_CLASS)); + assert (*((CK_OBJECT_CLASS_PTR)attr->pValue) == CKO_CERTIFICATE); p11_kit_uri_free (uri); } static void -test_uri_parse_with_id (CuTest *tc) +test_uri_parse_with_id (void) { CK_ATTRIBUTE_PTR attr; P11KitUri *uri; int ret; uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); + assert_ptr_not_null (uri); ret = p11_kit_uri_parse ("pkcs11:id=%54%45%53%54%00", P11_KIT_URI_FOR_OBJECT, uri); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + assert_num_eq (P11_KIT_URI_OK, ret); /* Note that there's a NULL in the attribute (end) */ attr = p11_kit_uri_get_attribute (uri, CKA_ID); - CuAssertPtrNotNull (tc, attr); - CuAssertTrue (tc, attr->ulValueLen == 5); - CuAssertTrue (tc, memcmp (attr->pValue, "TEST", 5) == 0); + assert_ptr_not_null (attr); + assert (attr->ulValueLen == 5); + assert (memcmp (attr->pValue, "TEST", 5) == 0); p11_kit_uri_free (uri); } static void -test_uri_parse_with_bad_string_encoding (CuTest *tc) +test_uri_parse_with_bad_string_encoding (void) { P11KitUri *uri; int ret; uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); + assert_ptr_not_null (uri); ret = p11_kit_uri_parse ("pkcs11:object=Test%", P11_KIT_URI_FOR_OBJECT, uri); - CuAssertIntEquals (tc, P11_KIT_URI_BAD_ENCODING, ret); + assert_num_eq (P11_KIT_URI_BAD_ENCODING, ret); p11_kit_uri_free (uri); } static void -test_uri_parse_with_bad_hex_encoding (CuTest *tc) +test_uri_parse_with_bad_hex_encoding (void) { P11KitUri *uri; int ret; uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); + assert_ptr_not_null (uri); ret = p11_kit_uri_parse ("pkcs11:object=T%xxest", P11_KIT_URI_FOR_OBJECT, uri); - CuAssertIntEquals (tc, P11_KIT_URI_BAD_ENCODING, ret); + assert_num_eq (P11_KIT_URI_BAD_ENCODING, ret); p11_kit_uri_free (uri); } @@ -226,131 +226,131 @@ is_space_string (CK_UTF8CHAR_PTR string, CK_ULONG size, const char *check) } static void -test_uri_parse_with_token (CuTest *tc) +test_uri_parse_with_token (void) { P11KitUri *uri = NULL; CK_TOKEN_INFO_PTR token; int ret; uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); + assert_ptr_not_null (uri); ret = p11_kit_uri_parse ("pkcs11:token=Token%20Label;serial=3333;model=Deluxe;manufacturer=Me", P11_KIT_URI_FOR_TOKEN, uri); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + assert_num_eq (P11_KIT_URI_OK, ret); token = p11_kit_uri_get_token_info (uri); - CuAssertTrue (tc, is_space_string (token->label, sizeof (token->label), "Token Label")); - CuAssertTrue (tc, is_space_string (token->serialNumber, sizeof (token->serialNumber), "3333")); - CuAssertTrue (tc, is_space_string (token->model, sizeof (token->model), "Deluxe")); - CuAssertTrue (tc, is_space_string (token->manufacturerID, sizeof (token->manufacturerID), "Me")); + assert (is_space_string (token->label, sizeof (token->label), "Token Label")); + assert (is_space_string (token->serialNumber, sizeof (token->serialNumber), "3333")); + assert (is_space_string (token->model, sizeof (token->model), "Deluxe")); + assert (is_space_string (token->manufacturerID, sizeof (token->manufacturerID), "Me")); p11_kit_uri_free (uri); } static void -test_uri_parse_with_token_bad_encoding (CuTest *tc) +test_uri_parse_with_token_bad_encoding (void) { P11KitUri *uri; int ret; uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); + assert_ptr_not_null (uri); ret = p11_kit_uri_parse ("pkcs11:token=Token%", P11_KIT_URI_FOR_TOKEN, uri); - CuAssertIntEquals (tc, P11_KIT_URI_BAD_ENCODING, ret); + assert_num_eq (P11_KIT_URI_BAD_ENCODING, ret); p11_kit_uri_free (uri); } static void -test_uri_parse_with_bad_syntax (CuTest *tc) +test_uri_parse_with_bad_syntax (void) { P11KitUri *uri; int ret; uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); + assert_ptr_not_null (uri); ret = p11_kit_uri_parse ("pkcs11:token", P11_KIT_URI_FOR_ANY, uri); - CuAssertIntEquals (tc, P11_KIT_URI_BAD_SYNTAX, ret); + assert_num_eq (P11_KIT_URI_BAD_SYNTAX, ret); p11_kit_uri_free (uri); } static void -test_uri_parse_with_spaces (CuTest *tc) +test_uri_parse_with_spaces (void) { P11KitUri *uri = NULL; CK_INFO_PTR info; int ret; uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); + assert_ptr_not_null (uri); ret = p11_kit_uri_parse ("pkc\ns11: lib rary-desc\rrip \n tion =The%20Library;\n\n\nlibrary-manufacturer=\rMe", P11_KIT_URI_FOR_MODULE, uri); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + assert_num_eq (P11_KIT_URI_OK, ret); info = p11_kit_uri_get_module_info (uri); - CuAssertTrue (tc, is_space_string (info->manufacturerID, sizeof (info->manufacturerID), "Me")); - CuAssertTrue (tc, is_space_string (info->libraryDescription, sizeof (info->libraryDescription), "The Library")); + assert (is_space_string (info->manufacturerID, sizeof (info->manufacturerID), "Me")); + assert (is_space_string (info->libraryDescription, sizeof (info->libraryDescription), "The Library")); p11_kit_uri_free (uri); } static void -test_uri_parse_with_library (CuTest *tc) +test_uri_parse_with_library (void) { P11KitUri *uri = NULL; CK_INFO_PTR info; int ret; uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); + assert_ptr_not_null (uri); ret = p11_kit_uri_parse ("pkcs11:library-description=The%20Library;library-manufacturer=Me", P11_KIT_URI_FOR_MODULE, uri); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + assert_num_eq (P11_KIT_URI_OK, ret); info = p11_kit_uri_get_module_info (uri); - CuAssertTrue (tc, is_space_string (info->manufacturerID, sizeof (info->manufacturerID), "Me")); - CuAssertTrue (tc, is_space_string (info->libraryDescription, sizeof (info->libraryDescription), "The Library")); + assert (is_space_string (info->manufacturerID, sizeof (info->manufacturerID), "Me")); + assert (is_space_string (info->libraryDescription, sizeof (info->libraryDescription), "The Library")); p11_kit_uri_free (uri); } static void -test_uri_parse_with_library_bad_encoding (CuTest *tc) +test_uri_parse_with_library_bad_encoding (void) { P11KitUri *uri; int ret; uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); + assert_ptr_not_null (uri); ret = p11_kit_uri_parse ("pkcs11:library-description=Library%", P11_KIT_URI_FOR_MODULE, uri); - CuAssertIntEquals (tc, P11_KIT_URI_BAD_ENCODING, ret); + assert_num_eq (P11_KIT_URI_BAD_ENCODING, ret); p11_kit_uri_free (uri); } static void -test_uri_build_empty (CuTest *tc) +test_uri_build_empty (void) { P11KitUri *uri; char *string; int ret; uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); + assert_ptr_not_null (uri); ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - CuAssertStrEquals (tc, "pkcs11:", string); + assert_num_eq (P11_KIT_URI_OK, ret); + assert_str_eq ("pkcs11:", string); free (string); p11_kit_uri_free (uri); @@ -366,7 +366,7 @@ set_space_string (CK_BYTE_PTR buffer, CK_ULONG length, const char *string) } static void -test_uri_build_with_token_info (CuTest *tc) +test_uri_build_with_token_info (void) { char *string = NULL; P11KitUri *uri; @@ -375,7 +375,7 @@ test_uri_build_with_token_info (CuTest *tc) int ret; uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); + assert_ptr_not_null (uri); token = p11_kit_uri_get_token_info (uri); set_space_string (token->label, sizeof (token->label), "The Label"); @@ -384,30 +384,30 @@ test_uri_build_with_token_info (CuTest *tc) set_space_string (token->model, sizeof (token->model), "Deluxe"); ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - CuAssertPtrNotNull (tc, string); + assert_num_eq (P11_KIT_URI_OK, ret); + assert_ptr_not_null (string); check = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, check); + assert_ptr_not_null (check); ret = p11_kit_uri_parse (string, P11_KIT_URI_FOR_TOKEN, check); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + assert_num_eq (P11_KIT_URI_OK, ret); p11_kit_uri_match_token_info (check, p11_kit_uri_get_token_info (uri)); p11_kit_uri_free (uri); p11_kit_uri_free (check); - CuAssertTrue (tc, strstr (string, "token=The%20Label") != NULL); - CuAssertTrue (tc, strstr (string, "serial=44444") != NULL); - CuAssertTrue (tc, strstr (string, "manufacturer=Me") != NULL); - CuAssertTrue (tc, strstr (string, "model=Deluxe") != NULL); + assert (strstr (string, "token=The%20Label") != NULL); + assert (strstr (string, "serial=44444") != NULL); + assert (strstr (string, "manufacturer=Me") != NULL); + assert (strstr (string, "model=Deluxe") != NULL); free (string); } static void -test_uri_build_with_token_null_info (CuTest *tc) +test_uri_build_with_token_null_info (void) { char *string = NULL; P11KitUri *uri; @@ -415,23 +415,23 @@ test_uri_build_with_token_null_info (CuTest *tc) int ret; uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); + assert_ptr_not_null (uri); token = p11_kit_uri_get_token_info (uri); set_space_string (token->label, sizeof (token->label), "The Label"); ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + assert_num_eq (P11_KIT_URI_OK, ret); - CuAssertTrue (tc, strstr (string, "token=The%20Label") != NULL); - CuAssertTrue (tc, strstr (string, "serial=") == NULL); + assert (strstr (string, "token=The%20Label") != NULL); + assert (strstr (string, "serial=") == NULL); free (string); p11_kit_uri_free (uri); } static void -test_uri_build_with_token_empty_info (CuTest *tc) +test_uri_build_with_token_empty_info (void) { char *string = NULL; P11KitUri *uri; @@ -439,24 +439,24 @@ test_uri_build_with_token_empty_info (CuTest *tc) int ret; uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); + assert_ptr_not_null (uri); token = p11_kit_uri_get_token_info (uri); set_space_string (token->label, sizeof (token->label), ""); set_space_string (token->serialNumber, sizeof (token->serialNumber), ""); ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + assert_num_eq (P11_KIT_URI_OK, ret); - CuAssertTrue (tc, strstr (string, "token=") != NULL); - CuAssertTrue (tc, strstr (string, "serial=") != NULL); + assert (strstr (string, "token=") != NULL); + assert (strstr (string, "serial=") != NULL); free (string); p11_kit_uri_free (uri); } static void -test_uri_build_with_attributes (CuTest *tc) +test_uri_build_with_attributes (void) { char *string = NULL; P11KitUri *uri; @@ -467,7 +467,7 @@ test_uri_build_with_attributes (CuTest *tc) int ret; uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); + assert_ptr_not_null (uri); at.type = CKA_LABEL; at.pValue = "The Label"; @@ -486,175 +486,175 @@ test_uri_build_with_attributes (CuTest *tc) ret = p11_kit_uri_set_attribute (uri, &at); ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + assert_num_eq (P11_KIT_URI_OK, ret); check = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, check); + assert_ptr_not_null (check); ret = p11_kit_uri_parse (string, P11_KIT_URI_FOR_ANY, check); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + assert_num_eq (P11_KIT_URI_OK, ret); attr = p11_kit_uri_get_attribute (check, CKA_LABEL); - CuAssertPtrNotNull (tc, attr); - CuAssertTrue (tc, attr->ulValueLen == 9); - CuAssertTrue (tc, memcmp (attr->pValue, "The Label", attr->ulValueLen) == 0); + assert_ptr_not_null (attr); + assert (attr->ulValueLen == 9); + assert (memcmp (attr->pValue, "The Label", attr->ulValueLen) == 0); attr = p11_kit_uri_get_attribute (check, CKA_CLASS); - CuAssertPtrNotNull (tc, attr); - CuAssertTrue (tc, attr->ulValueLen == sizeof (klass)); - CuAssertTrue (tc, *((CK_OBJECT_CLASS_PTR)attr->pValue) == klass); + assert_ptr_not_null (attr); + assert (attr->ulValueLen == sizeof (klass)); + assert (*((CK_OBJECT_CLASS_PTR)attr->pValue) == klass); attr = p11_kit_uri_get_attribute (check, CKA_ID); - CuAssertPtrNotNull (tc, attr); - CuAssertTrue (tc, attr->ulValueLen == 5); - CuAssertTrue (tc, memcmp (attr->pValue, "HELLO", attr->ulValueLen) == 0); + assert_ptr_not_null (attr); + assert (attr->ulValueLen == 5); + assert (memcmp (attr->pValue, "HELLO", attr->ulValueLen) == 0); p11_kit_uri_free (check); - CuAssertTrue (tc, strstr (string, "object=The%20Label") != NULL); - CuAssertTrue (tc, strstr (string, "object-type=data") != NULL); - CuAssertTrue (tc, strstr (string, "id=%48%45%4c%4c%4f") != NULL); + assert (strstr (string, "object=The%20Label") != NULL); + assert (strstr (string, "object-type=data") != NULL); + assert (strstr (string, "id=%48%45%4c%4c%4f") != NULL); free (string); p11_kit_uri_free (uri); } static void -test_uri_parse_private_key (CuTest *tc) +test_uri_parse_private_key (void) { P11KitUri *uri; CK_ATTRIBUTE_PTR attr; int ret; uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); + assert_ptr_not_null (uri); ret = p11_kit_uri_parse ("pkcs11:object-type=private", P11_KIT_URI_FOR_OBJECT, uri); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + assert_num_eq (P11_KIT_URI_OK, ret); attr = p11_kit_uri_get_attribute (uri, CKA_CLASS); - CuAssertPtrNotNull (tc, attr); - CuAssertTrue (tc, attr->ulValueLen == sizeof (CK_OBJECT_CLASS)); - CuAssertTrue (tc, *((CK_OBJECT_CLASS_PTR)attr->pValue) == CKO_PRIVATE_KEY); + assert_ptr_not_null (attr); + assert (attr->ulValueLen == sizeof (CK_OBJECT_CLASS)); + assert (*((CK_OBJECT_CLASS_PTR)attr->pValue) == CKO_PRIVATE_KEY); p11_kit_uri_free (uri); } static void -test_uri_parse_secret_key (CuTest *tc) +test_uri_parse_secret_key (void) { P11KitUri *uri; CK_ATTRIBUTE_PTR attr; int ret; uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); + assert_ptr_not_null (uri); ret = p11_kit_uri_parse ("pkcs11:object-type=secret-key", P11_KIT_URI_FOR_OBJECT, uri); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + assert_num_eq (P11_KIT_URI_OK, ret); attr = p11_kit_uri_get_attribute (uri, CKA_CLASS); - CuAssertPtrNotNull (tc, attr); - CuAssertTrue (tc, attr->ulValueLen == sizeof (CK_OBJECT_CLASS)); - CuAssertTrue (tc, *((CK_OBJECT_CLASS_PTR)attr->pValue) == CKO_SECRET_KEY); + assert_ptr_not_null (attr); + assert (attr->ulValueLen == sizeof (CK_OBJECT_CLASS)); + assert (*((CK_OBJECT_CLASS_PTR)attr->pValue) == CKO_SECRET_KEY); p11_kit_uri_free (uri); } static void -test_uri_parse_library_version (CuTest *tc) +test_uri_parse_library_version (void) { P11KitUri *uri; CK_INFO_PTR info; int ret; uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); + assert_ptr_not_null (uri); ret = p11_kit_uri_parse ("pkcs11:library-version=2.101", P11_KIT_URI_FOR_MODULE_WITH_VERSION, uri); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + assert_num_eq (P11_KIT_URI_OK, ret); info = p11_kit_uri_get_module_info (uri); - CuAssertIntEquals (tc, 2, info->libraryVersion.major); - CuAssertIntEquals (tc, 101, info->libraryVersion.minor); + assert_num_eq (2, info->libraryVersion.major); + assert_num_eq (101, info->libraryVersion.minor); ret = p11_kit_uri_parse ("pkcs11:library-version=23", P11_KIT_URI_FOR_MODULE_WITH_VERSION, uri); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + assert_num_eq (P11_KIT_URI_OK, ret); info = p11_kit_uri_get_module_info (uri); - CuAssertIntEquals (tc, 23, info->libraryVersion.major); - CuAssertIntEquals (tc, 0, info->libraryVersion.minor); + assert_num_eq (23, info->libraryVersion.major); + assert_num_eq (0, info->libraryVersion.minor); ret = p11_kit_uri_parse ("pkcs11:library-version=23.", P11_KIT_URI_FOR_MODULE_WITH_VERSION, uri); - CuAssertIntEquals (tc, P11_KIT_URI_BAD_VERSION, ret); + assert_num_eq (P11_KIT_URI_BAD_VERSION, ret); ret = p11_kit_uri_parse ("pkcs11:library-version=a.a", P11_KIT_URI_FOR_MODULE_WITH_VERSION, uri); - CuAssertIntEquals (tc, P11_KIT_URI_BAD_VERSION, ret); + assert_num_eq (P11_KIT_URI_BAD_VERSION, ret); ret = p11_kit_uri_parse ("pkcs11:library-version=.23", P11_KIT_URI_FOR_MODULE_WITH_VERSION, uri); - CuAssertIntEquals (tc, P11_KIT_URI_BAD_VERSION, ret); + assert_num_eq (P11_KIT_URI_BAD_VERSION, ret); ret = p11_kit_uri_parse ("pkcs11:library-version=1000", P11_KIT_URI_FOR_MODULE_WITH_VERSION, uri); - CuAssertIntEquals (tc, P11_KIT_URI_BAD_VERSION, ret); + assert_num_eq (P11_KIT_URI_BAD_VERSION, ret); ret = p11_kit_uri_parse ("pkcs11:library-version=2.1000", P11_KIT_URI_FOR_MODULE_WITH_VERSION, uri); - CuAssertIntEquals (tc, P11_KIT_URI_BAD_VERSION, ret); + assert_num_eq (P11_KIT_URI_BAD_VERSION, ret); p11_kit_uri_free (uri); } static void -test_uri_parse_parse_unknown_object_type (CuTest *tc) +test_uri_parse_parse_unknown_object_type (void) { P11KitUri *uri; CK_ATTRIBUTE_PTR attr; int ret; uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); + assert_ptr_not_null (uri); ret = p11_kit_uri_parse ("pkcs11:object-type=unknown", P11_KIT_URI_FOR_OBJECT, uri); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + assert_num_eq (P11_KIT_URI_OK, ret); attr = p11_kit_uri_get_attribute (uri, CKA_CLASS); - CuAssertPtrEquals (tc, NULL, attr); + assert_ptr_eq (NULL, attr); p11_kit_uri_free (uri); } static void -test_uri_parse_unrecognized (CuTest *tc) +test_uri_parse_unrecognized (void) { P11KitUri *uri; int ret; uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); + assert_ptr_not_null (uri); ret = p11_kit_uri_parse ("pkcs11:x-blah=some-value", P11_KIT_URI_FOR_ANY, uri); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + assert_num_eq (P11_KIT_URI_OK, ret); ret = p11_kit_uri_any_unrecognized (uri); - CuAssertIntEquals (tc, 1, ret); + assert_num_eq (1, ret); p11_kit_uri_free (uri); } static void -test_uri_parse_too_long_is_unrecognized (CuTest *tc) +test_uri_parse_too_long_is_unrecognized (void) { P11KitUri *uri; int ret; uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); + assert_ptr_not_null (uri); ret = p11_kit_uri_parse ("pkcs11:model=a-value-that-is-too-long-for-the-field-that-it-goes-with", P11_KIT_URI_FOR_ANY, uri); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + assert_num_eq (P11_KIT_URI_OK, ret); ret = p11_kit_uri_any_unrecognized (uri); - CuAssertIntEquals (tc, 1, ret); + assert_num_eq (1, ret); p11_kit_uri_free (uri); } @@ -662,7 +662,7 @@ test_uri_parse_too_long_is_unrecognized (CuTest *tc) static void -test_uri_build_object_type_cert (CuTest *tc) +test_uri_build_object_type_cert (void) { CK_ATTRIBUTE attr; CK_OBJECT_CLASS klass; @@ -671,7 +671,7 @@ test_uri_build_object_type_cert (CuTest *tc) int ret; uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); + assert_ptr_not_null (uri); klass = CKO_CERTIFICATE; attr.type = CKA_CLASS; @@ -680,15 +680,15 @@ test_uri_build_object_type_cert (CuTest *tc) p11_kit_uri_set_attribute (uri, &attr); ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - CuAssertTrue (tc, strstr (string, "object-type=cert") != NULL); + assert_num_eq (P11_KIT_URI_OK, ret); + assert (strstr (string, "object-type=cert") != NULL); p11_kit_uri_free (uri); free (string); } static void -test_uri_build_object_type_private (CuTest *tc) +test_uri_build_object_type_private (void) { CK_ATTRIBUTE attr; CK_OBJECT_CLASS klass; @@ -697,7 +697,7 @@ test_uri_build_object_type_private (CuTest *tc) int ret; uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); + assert_ptr_not_null (uri); klass = CKO_PRIVATE_KEY; attr.type = CKA_CLASS; @@ -706,15 +706,15 @@ test_uri_build_object_type_private (CuTest *tc) p11_kit_uri_set_attribute (uri, &attr); ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - CuAssertTrue (tc, strstr (string, "object-type=private") != NULL); + assert_num_eq (P11_KIT_URI_OK, ret); + assert (strstr (string, "object-type=private") != NULL); p11_kit_uri_free (uri); free (string); } static void -test_uri_build_object_type_public (CuTest *tc) +test_uri_build_object_type_public (void) { CK_ATTRIBUTE attr; CK_OBJECT_CLASS klass; @@ -723,7 +723,7 @@ test_uri_build_object_type_public (CuTest *tc) int ret; uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); + assert_ptr_not_null (uri); klass = CKO_PUBLIC_KEY; attr.type = CKA_CLASS; @@ -732,15 +732,15 @@ test_uri_build_object_type_public (CuTest *tc) p11_kit_uri_set_attribute (uri, &attr); ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - CuAssertTrue (tc, strstr (string, "object-type=public") != NULL); + assert_num_eq (P11_KIT_URI_OK, ret); + assert (strstr (string, "object-type=public") != NULL); p11_kit_uri_free (uri); free (string); } static void -test_uri_build_object_type_secret (CuTest *tc) +test_uri_build_object_type_secret (void) { CK_ATTRIBUTE attr; CK_OBJECT_CLASS klass; @@ -749,7 +749,7 @@ test_uri_build_object_type_secret (CuTest *tc) int ret; uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); + assert_ptr_not_null (uri); klass = CKO_SECRET_KEY; attr.type = CKA_CLASS; @@ -758,15 +758,15 @@ test_uri_build_object_type_secret (CuTest *tc) p11_kit_uri_set_attribute (uri, &attr); ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - CuAssertTrue (tc, strstr (string, "object-type=secret-key") != NULL); + assert_num_eq (P11_KIT_URI_OK, ret); + assert (strstr (string, "object-type=secret-key") != NULL); p11_kit_uri_free (uri); free (string); } static void -test_uri_build_with_library (CuTest *tc) +test_uri_build_with_library (void) { CK_INFO_PTR info; P11KitUri *uri; @@ -774,21 +774,21 @@ test_uri_build_with_library (CuTest *tc) int ret; uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); + assert_ptr_not_null (uri); info = p11_kit_uri_get_module_info (uri); set_space_string (info->libraryDescription, sizeof (info->libraryDescription), "The Description"); ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - CuAssertTrue (tc, strstr (string, "library-description=The%20Description") != NULL); + assert_num_eq (P11_KIT_URI_OK, ret); + assert (strstr (string, "library-description=The%20Description") != NULL); p11_kit_uri_free (uri); free (string); } static void -test_uri_build_library_version (CuTest *tc) +test_uri_build_library_version (void) { CK_INFO_PTR info; P11KitUri *uri; @@ -796,121 +796,121 @@ test_uri_build_library_version (CuTest *tc) int ret; uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); + assert_ptr_not_null (uri); info = p11_kit_uri_get_module_info (uri); info->libraryVersion.major = 2; info->libraryVersion.minor = 10; ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - CuAssertTrue (tc, strstr (string, "library-version=2.10") != NULL); + assert_num_eq (P11_KIT_URI_OK, ret); + assert (strstr (string, "library-version=2.10") != NULL); p11_kit_uri_free (uri); free (string); } static void -test_uri_get_set_unrecognized (CuTest *tc) +test_uri_get_set_unrecognized (void) { P11KitUri *uri; int ret; uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); + assert_ptr_not_null (uri); ret = p11_kit_uri_any_unrecognized (uri); - CuAssertIntEquals (tc, 0, ret); + assert_num_eq (0, ret); p11_kit_uri_set_unrecognized (uri, 1); ret = p11_kit_uri_any_unrecognized (uri); - CuAssertIntEquals (tc, 1, ret); + assert_num_eq (1, ret); p11_kit_uri_set_unrecognized (uri, 0); ret = p11_kit_uri_any_unrecognized (uri); - CuAssertIntEquals (tc, 0, ret); + assert_num_eq (0, ret); p11_kit_uri_free (uri); } static void -test_uri_match_token (CuTest *tc) +test_uri_match_token (void) { CK_TOKEN_INFO token; P11KitUri *uri; int ret; uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); + assert_ptr_not_null (uri); ret = p11_kit_uri_parse ("pkcs11:model=Giselle", P11_KIT_URI_FOR_ANY, uri); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + assert_num_eq (P11_KIT_URI_OK, ret); set_space_string (token.label, sizeof (token.label), "A label"); set_space_string (token.model, sizeof (token.model), "Giselle"); ret = p11_kit_uri_match_token_info (uri, &token); - CuAssertIntEquals (tc, 1, ret); + assert_num_eq (1, ret); set_space_string (token.label, sizeof (token.label), "Another label"); ret = p11_kit_uri_match_token_info (uri, &token); - CuAssertIntEquals (tc, 1, ret); + assert_num_eq (1, ret); set_space_string (token.model, sizeof (token.model), "Zoolander"); ret = p11_kit_uri_match_token_info (uri, &token); - CuAssertIntEquals (tc, 0, ret); + assert_num_eq (0, ret); p11_kit_uri_set_unrecognized (uri, 1); ret = p11_kit_uri_match_token_info (uri, &token); - CuAssertIntEquals (tc, 0, ret); + assert_num_eq (0, ret); p11_kit_uri_free (uri); } static void -test_uri_match_module (CuTest *tc) +test_uri_match_module (void) { CK_INFO info; P11KitUri *uri; int ret; uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); + assert_ptr_not_null (uri); ret = p11_kit_uri_parse ("pkcs11:library-description=Quiet", P11_KIT_URI_FOR_ANY, uri); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + assert_num_eq (P11_KIT_URI_OK, ret); set_space_string (info.libraryDescription, sizeof (info.libraryDescription), "Quiet"); set_space_string (info.manufacturerID, sizeof (info.manufacturerID), "Someone"); ret = p11_kit_uri_match_module_info (uri, &info); - CuAssertIntEquals (tc, 1, ret); + assert_num_eq (1, ret); set_space_string (info.manufacturerID, sizeof (info.manufacturerID), "Someone else"); ret = p11_kit_uri_match_module_info (uri, &info); - CuAssertIntEquals (tc, 1, ret); + assert_num_eq (1, ret); set_space_string (info.libraryDescription, sizeof (info.libraryDescription), "Leise"); ret = p11_kit_uri_match_module_info (uri, &info); - CuAssertIntEquals (tc, 0, ret); + assert_num_eq (0, ret); p11_kit_uri_set_unrecognized (uri, 1); ret = p11_kit_uri_match_module_info (uri, &info); - CuAssertIntEquals (tc, 0, ret); + assert_num_eq (0, ret); p11_kit_uri_free (uri); } static void -test_uri_match_version (CuTest *tc) +test_uri_match_version (void) { CK_INFO info; P11KitUri *uri; @@ -919,28 +919,28 @@ test_uri_match_version (CuTest *tc) memset (&info, 0, sizeof (info)); uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); + assert_ptr_not_null (uri); ret = p11_kit_uri_parse ("pkcs11:library-version=5.8", P11_KIT_URI_FOR_ANY, uri); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + assert_num_eq (P11_KIT_URI_OK, ret); info.libraryVersion.major = 5; info.libraryVersion.minor = 8; ret = p11_kit_uri_match_module_info (uri, &info); - CuAssertIntEquals (tc, 1, ret); + assert_num_eq (1, ret); info.libraryVersion.major = 2; info.libraryVersion.minor = 3; ret = p11_kit_uri_match_module_info (uri, &info); - CuAssertIntEquals (tc, 0, ret); + assert_num_eq (0, ret); p11_kit_uri_free (uri); } static void -test_uri_match_attributes (CuTest *tc) +test_uri_match_attributes (void) { CK_ATTRIBUTE attrs[4]; CK_OBJECT_CLASS klass; @@ -965,40 +965,40 @@ test_uri_match_attributes (CuTest *tc) attrs[3].ulValueLen = sizeof (klass); uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); + assert_ptr_not_null (uri); ret = p11_kit_uri_parse ("pkcs11:object=Fancy;id=Blah;object-type=data", P11_KIT_URI_FOR_ANY, uri); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + assert_num_eq (P11_KIT_URI_OK, ret); ret = p11_kit_uri_match_attributes (uri, attrs, 4); - CuAssertIntEquals (tc, 0, ret); + assert_num_eq (0, ret); attrs[1].pValue = "Fancy"; attrs[1].ulValueLen = 5; ret = p11_kit_uri_match_attributes (uri, attrs, 4); - CuAssertIntEquals (tc, 1, ret); + assert_num_eq (1, ret); p11_kit_uri_clear_attribute (uri, CKA_CLASS); ret = p11_kit_uri_match_attributes (uri, attrs, 4); - CuAssertIntEquals (tc, 1, ret); + assert_num_eq (1, ret); attrs[2].pValue = "pink"; ret = p11_kit_uri_match_attributes (uri, attrs, 4); - CuAssertIntEquals (tc, 1, ret); + assert_num_eq (1, ret); p11_kit_uri_set_unrecognized (uri, 1); ret = p11_kit_uri_match_attributes (uri, attrs, 4); - CuAssertIntEquals (tc, 0, ret); + assert_num_eq (0, ret); p11_kit_uri_free (uri); } static void -test_uri_get_set_attribute (CuTest *tc) +test_uri_get_set_attribute (void) { CK_ATTRIBUTE attr; CK_ATTRIBUTE_PTR ptr; @@ -1006,51 +1006,51 @@ test_uri_get_set_attribute (CuTest *tc) int ret; uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); + assert_ptr_not_null (uri); ptr = p11_kit_uri_get_attribute (uri, CKA_LABEL); - CuAssertPtrEquals (tc, NULL, ptr); + assert_ptr_eq (NULL, ptr); ret = p11_kit_uri_clear_attribute (uri, CKA_LABEL); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + assert_num_eq (P11_KIT_URI_OK, ret); ret = p11_kit_uri_clear_attribute (uri, CKA_COLOR); - CuAssertIntEquals (tc, P11_KIT_URI_NOT_FOUND, ret); + assert_num_eq (P11_KIT_URI_NOT_FOUND, ret); attr.type = CKA_LABEL; attr.pValue = "Test"; attr.ulValueLen = 4; ret = p11_kit_uri_set_attribute (uri, &attr); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + assert_num_eq (P11_KIT_URI_OK, ret); /* We can set other attributes */ attr.type = CKA_COLOR; ret = p11_kit_uri_set_attribute (uri, &attr); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + assert_num_eq (P11_KIT_URI_OK, ret); /* And get them too */ ptr = p11_kit_uri_get_attribute (uri, CKA_COLOR); - CuAssertPtrNotNull (tc, ptr); + assert_ptr_not_null (ptr); ptr = p11_kit_uri_get_attribute (uri, CKA_LABEL); - CuAssertPtrNotNull (tc, ptr); + assert_ptr_not_null (ptr); - CuAssertTrue (tc, ptr->type == CKA_LABEL); - CuAssertTrue (tc, ptr->ulValueLen == 4); - CuAssertTrue (tc, memcmp (ptr->pValue, "Test", 4) == 0); + assert (ptr->type == CKA_LABEL); + assert (ptr->ulValueLen == 4); + assert (memcmp (ptr->pValue, "Test", 4) == 0); ret = p11_kit_uri_clear_attribute (uri, CKA_LABEL); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + assert_num_eq (P11_KIT_URI_OK, ret); ptr = p11_kit_uri_get_attribute (uri, CKA_LABEL); - CuAssertPtrEquals (tc, NULL, ptr); + assert_ptr_eq (NULL, ptr); p11_kit_uri_free (uri); } static void -test_uri_get_set_attributes (CuTest *tc) +test_uri_get_set_attributes (void) { CK_ATTRIBUTE_PTR attrs; CK_OBJECT_CLASS klass; @@ -1060,39 +1060,39 @@ test_uri_get_set_attributes (CuTest *tc) int ret; uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); + assert_ptr_not_null (uri); attrs = p11_kit_uri_get_attributes (uri, &n_attrs); - CuAssertPtrNotNull (tc, attrs); - CuAssertIntEquals (tc, 0, n_attrs); + assert_ptr_not_null (attrs); + assert_num_eq (0, n_attrs); attr.type = CKA_LABEL; attr.pValue = "Test"; attr.ulValueLen = 4; ret = p11_kit_uri_set_attribute (uri, &attr); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + assert_num_eq (P11_KIT_URI_OK, ret); attrs = p11_kit_uri_get_attributes (uri, &n_attrs); - CuAssertPtrNotNull (tc, attrs); - CuAssertIntEquals (tc, 1, n_attrs); - CuAssertTrue (tc, attrs[0].type == CKA_LABEL); - CuAssertTrue (tc, attrs[0].ulValueLen == 4); - CuAssertTrue (tc, memcmp (attrs[0].pValue, "Test", 4) == 0); + assert_ptr_not_null (attrs); + assert_num_eq (1, n_attrs); + assert (attrs[0].type == CKA_LABEL); + assert (attrs[0].ulValueLen == 4); + assert (memcmp (attrs[0].pValue, "Test", 4) == 0); attr.type = CKA_LABEL; attr.pValue = "Kablooey"; attr.ulValueLen = 8; ret = p11_kit_uri_set_attribute (uri, &attr); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + assert_num_eq (P11_KIT_URI_OK, ret); attrs = p11_kit_uri_get_attributes (uri, &n_attrs); - CuAssertPtrNotNull (tc, attrs); - CuAssertIntEquals (tc, 1, n_attrs); - CuAssertTrue (tc, attrs[0].type == CKA_LABEL); - CuAssertTrue (tc, attrs[0].ulValueLen == 8); - CuAssertTrue (tc, memcmp (attrs[0].pValue, "Kablooey", 8) == 0); + assert_ptr_not_null (attrs); + assert_num_eq (1, n_attrs); + assert (attrs[0].type == CKA_LABEL); + assert (attrs[0].ulValueLen == 8); + assert (memcmp (attrs[0].pValue, "Kablooey", 8) == 0); klass = CKO_DATA; attr.type = CKA_CLASS; @@ -1100,52 +1100,52 @@ test_uri_get_set_attributes (CuTest *tc) attr.ulValueLen = sizeof (klass); ret = p11_kit_uri_set_attribute (uri, &attr); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + assert_num_eq (P11_KIT_URI_OK, ret); attrs = p11_kit_uri_get_attributes (uri, &n_attrs); - CuAssertPtrNotNull (tc, attrs); - CuAssertIntEquals (tc, 2, n_attrs); - CuAssertTrue (tc, attrs[0].type == CKA_LABEL); - CuAssertTrue (tc, attrs[0].ulValueLen == 8); - CuAssertTrue (tc, memcmp (attrs[0].pValue, "Kablooey", 8) == 0); - CuAssertTrue (tc, attrs[1].type == CKA_CLASS); - CuAssertTrue (tc, attrs[1].ulValueLen == sizeof (klass)); - CuAssertTrue (tc, memcmp (attrs[1].pValue, &klass, sizeof (klass)) == 0); + assert_ptr_not_null (attrs); + assert_num_eq (2, n_attrs); + assert (attrs[0].type == CKA_LABEL); + assert (attrs[0].ulValueLen == 8); + assert (memcmp (attrs[0].pValue, "Kablooey", 8) == 0); + assert (attrs[1].type == CKA_CLASS); + assert (attrs[1].ulValueLen == sizeof (klass)); + assert (memcmp (attrs[1].pValue, &klass, sizeof (klass)) == 0); ret = p11_kit_uri_clear_attribute (uri, CKA_LABEL); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + assert_num_eq (P11_KIT_URI_OK, ret); attrs = p11_kit_uri_get_attributes (uri, &n_attrs); - CuAssertPtrNotNull (tc, attrs); - CuAssertIntEquals (tc, 1, n_attrs); - CuAssertTrue (tc, attrs[0].type == CKA_CLASS); - CuAssertTrue (tc, attrs[0].ulValueLen == sizeof (klass)); - CuAssertTrue (tc, memcmp (attrs[0].pValue, &klass, sizeof (klass)) == 0); + assert_ptr_not_null (attrs); + assert_num_eq (1, n_attrs); + assert (attrs[0].type == CKA_CLASS); + assert (attrs[0].ulValueLen == sizeof (klass)); + assert (memcmp (attrs[0].pValue, &klass, sizeof (klass)) == 0); attr.type = CKA_LABEL; attr.pValue = "Three"; attr.ulValueLen = 5; ret = p11_kit_uri_set_attributes (uri, &attr, 1); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + assert_num_eq (P11_KIT_URI_OK, ret); attrs = p11_kit_uri_get_attributes (uri, &n_attrs); - CuAssertPtrNotNull (tc, attrs); - CuAssertIntEquals (tc, 1, n_attrs); - CuAssertTrue (tc, attrs[0].type == CKA_LABEL); - CuAssertTrue (tc, attrs[0].ulValueLen == 5); - CuAssertTrue (tc, memcmp (attrs[0].pValue, "Three", 5) == 0); + assert_ptr_not_null (attrs); + assert_num_eq (1, n_attrs); + assert (attrs[0].type == CKA_LABEL); + assert (attrs[0].ulValueLen == 5); + assert (memcmp (attrs[0].pValue, "Three", 5) == 0); p11_kit_uri_clear_attributes (uri); attrs = p11_kit_uri_get_attributes (uri, &n_attrs); - CuAssertPtrNotNull (tc, attrs); - CuAssertIntEquals (tc, 0, n_attrs); + assert_ptr_not_null (attrs); + assert_num_eq (0, n_attrs); p11_kit_uri_free (uri); } static void -test_uri_pin_source (CuTest *tc) +test_uri_pin_source (void) { P11KitUri *uri; const char *pin_source; @@ -1153,106 +1153,93 @@ test_uri_pin_source (CuTest *tc) int ret; uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); + assert_ptr_not_null (uri); p11_kit_uri_set_pin_source (uri, "|my-pin-source"); pin_source = p11_kit_uri_get_pin_source (uri); - CuAssertStrEquals (tc, "|my-pin-source", pin_source); + assert_str_eq ("|my-pin-source", pin_source); pin_source = p11_kit_uri_get_pinfile (uri); - CuAssertStrEquals (tc, "|my-pin-source", pin_source); + assert_str_eq ("|my-pin-source", pin_source); p11_kit_uri_set_pinfile (uri, "|my-pin-file"); pin_source = p11_kit_uri_get_pin_source (uri); - CuAssertStrEquals (tc, "|my-pin-file", pin_source); + assert_str_eq ("|my-pin-file", pin_source); ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - CuAssertTrue (tc, strstr (string, "pin-source=%7cmy-pin-file") != NULL); + assert_num_eq (P11_KIT_URI_OK, ret); + assert (strstr (string, "pin-source=%7cmy-pin-file") != NULL); free (string); ret = p11_kit_uri_parse ("pkcs11:pin-source=blah%2Fblah", P11_KIT_URI_FOR_ANY, uri); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + assert_num_eq (P11_KIT_URI_OK, ret); pin_source = p11_kit_uri_get_pin_source (uri); - CuAssertStrEquals (tc, "blah/blah", pin_source); + assert_str_eq ("blah/blah", pin_source); p11_kit_uri_free (uri); } static void -test_uri_free_null (CuTest *tc) +test_uri_free_null (void) { p11_kit_uri_free (NULL); } static void -test_uri_message (CuTest *tc) +test_uri_message (void) { - CuAssertTrue (tc, p11_kit_uri_message (P11_KIT_URI_OK) == NULL); - CuAssertPtrNotNull (tc, p11_kit_uri_message (P11_KIT_URI_UNEXPECTED)); - CuAssertPtrNotNull (tc, p11_kit_uri_message (-555555)); + assert (p11_kit_uri_message (P11_KIT_URI_OK) == NULL); + assert_ptr_not_null (p11_kit_uri_message (P11_KIT_URI_UNEXPECTED)); + assert_ptr_not_null (p11_kit_uri_message (-555555)); } int -main (void) +main (int argc, + char *argv[]) { - CuString *output = CuStringNew (); - CuSuite* suite = CuSuiteNew (); - int ret; - - putenv ("P11_KIT_STRICT=1"); - p11_debug_init (); - - SUITE_ADD_TEST (suite, test_uri_parse); - SUITE_ADD_TEST (suite, test_uri_parse_bad_scheme); - SUITE_ADD_TEST (suite, test_uri_parse_with_label); - SUITE_ADD_TEST (suite, test_uri_parse_with_label_and_klass); - SUITE_ADD_TEST (suite, test_uri_parse_with_id); - SUITE_ADD_TEST (suite, test_uri_parse_with_bad_string_encoding); - SUITE_ADD_TEST (suite, test_uri_parse_with_bad_hex_encoding); - SUITE_ADD_TEST (suite, test_uri_parse_with_token); - SUITE_ADD_TEST (suite, test_uri_parse_with_token_bad_encoding); - SUITE_ADD_TEST (suite, test_uri_parse_with_bad_syntax); - SUITE_ADD_TEST (suite, test_uri_parse_with_spaces); - SUITE_ADD_TEST (suite, test_uri_parse_with_library); - SUITE_ADD_TEST (suite, test_uri_parse_with_library_bad_encoding); - SUITE_ADD_TEST (suite, test_uri_build_empty); - SUITE_ADD_TEST (suite, test_uri_build_with_token_info); - SUITE_ADD_TEST (suite, test_uri_build_with_token_null_info); - SUITE_ADD_TEST (suite, test_uri_build_with_token_empty_info); - SUITE_ADD_TEST (suite, test_uri_build_with_attributes); - SUITE_ADD_TEST (suite, test_uri_parse_private_key); - SUITE_ADD_TEST (suite, test_uri_parse_secret_key); - SUITE_ADD_TEST (suite, test_uri_parse_library_version); - SUITE_ADD_TEST (suite, test_uri_parse_parse_unknown_object_type); - SUITE_ADD_TEST (suite, test_uri_parse_unrecognized); - SUITE_ADD_TEST (suite, test_uri_parse_too_long_is_unrecognized); - SUITE_ADD_TEST (suite, test_uri_build_object_type_cert); - SUITE_ADD_TEST (suite, test_uri_build_object_type_private); - SUITE_ADD_TEST (suite, test_uri_build_object_type_public); - SUITE_ADD_TEST (suite, test_uri_build_object_type_secret); - SUITE_ADD_TEST (suite, test_uri_build_with_library); - SUITE_ADD_TEST (suite, test_uri_build_library_version); - SUITE_ADD_TEST (suite, test_uri_get_set_unrecognized); - SUITE_ADD_TEST (suite, test_uri_match_token); - SUITE_ADD_TEST (suite, test_uri_match_module); - SUITE_ADD_TEST (suite, test_uri_match_version); - SUITE_ADD_TEST (suite, test_uri_match_attributes); - SUITE_ADD_TEST (suite, test_uri_get_set_attribute); - SUITE_ADD_TEST (suite, test_uri_get_set_attributes); - SUITE_ADD_TEST (suite, test_uri_pin_source); - SUITE_ADD_TEST (suite, test_uri_free_null); - SUITE_ADD_TEST (suite, test_uri_message); - - CuSuiteRun (suite); - CuSuiteSummary (suite, output); - CuSuiteDetails (suite, output); - printf ("%s\n", output->buffer); - ret = suite->failCount; - CuSuiteDelete (suite); - CuStringDelete (output); - return ret; + p11_test (test_uri_parse, "/uri/test_uri_parse"); + p11_test (test_uri_parse_bad_scheme, "/uri/test_uri_parse_bad_scheme"); + p11_test (test_uri_parse_with_label, "/uri/test_uri_parse_with_label"); + p11_test (test_uri_parse_with_label_and_klass, "/uri/test_uri_parse_with_label_and_klass"); + p11_test (test_uri_parse_with_id, "/uri/test_uri_parse_with_id"); + p11_test (test_uri_parse_with_bad_string_encoding, "/uri/test_uri_parse_with_bad_string_encoding"); + p11_test (test_uri_parse_with_bad_hex_encoding, "/uri/test_uri_parse_with_bad_hex_encoding"); + p11_test (test_uri_parse_with_token, "/uri/test_uri_parse_with_token"); + p11_test (test_uri_parse_with_token_bad_encoding, "/uri/test_uri_parse_with_token_bad_encoding"); + p11_test (test_uri_parse_with_bad_syntax, "/uri/test_uri_parse_with_bad_syntax"); + p11_test (test_uri_parse_with_spaces, "/uri/test_uri_parse_with_spaces"); + p11_test (test_uri_parse_with_library, "/uri/test_uri_parse_with_library"); + p11_test (test_uri_parse_with_library_bad_encoding, "/uri/test_uri_parse_with_library_bad_encoding"); + p11_test (test_uri_build_empty, "/uri/test_uri_build_empty"); + p11_test (test_uri_build_with_token_info, "/uri/test_uri_build_with_token_info"); + p11_test (test_uri_build_with_token_null_info, "/uri/test_uri_build_with_token_null_info"); + p11_test (test_uri_build_with_token_empty_info, "/uri/test_uri_build_with_token_empty_info"); + p11_test (test_uri_build_with_attributes, "/uri/test_uri_build_with_attributes"); + p11_test (test_uri_parse_private_key, "/uri/test_uri_parse_private_key"); + p11_test (test_uri_parse_secret_key, "/uri/test_uri_parse_secret_key"); + p11_test (test_uri_parse_library_version, "/uri/test_uri_parse_library_version"); + p11_test (test_uri_parse_parse_unknown_object_type, "/uri/test_uri_parse_parse_unknown_object_type"); + p11_test (test_uri_parse_unrecognized, "/uri/test_uri_parse_unrecognized"); + p11_test (test_uri_parse_too_long_is_unrecognized, "/uri/test_uri_parse_too_long_is_unrecognized"); + p11_test (test_uri_build_object_type_cert, "/uri/test_uri_build_object_type_cert"); + p11_test (test_uri_build_object_type_private, "/uri/test_uri_build_object_type_private"); + p11_test (test_uri_build_object_type_public, "/uri/test_uri_build_object_type_public"); + p11_test (test_uri_build_object_type_secret, "/uri/test_uri_build_object_type_secret"); + p11_test (test_uri_build_with_library, "/uri/test_uri_build_with_library"); + p11_test (test_uri_build_library_version, "/uri/test_uri_build_library_version"); + p11_test (test_uri_get_set_unrecognized, "/uri/test_uri_get_set_unrecognized"); + p11_test (test_uri_match_token, "/uri/test_uri_match_token"); + p11_test (test_uri_match_module, "/uri/test_uri_match_module"); + p11_test (test_uri_match_version, "/uri/test_uri_match_version"); + p11_test (test_uri_match_attributes, "/uri/test_uri_match_attributes"); + p11_test (test_uri_get_set_attribute, "/uri/test_uri_get_set_attribute"); + p11_test (test_uri_get_set_attributes, "/uri/test_uri_get_set_attributes"); + p11_test (test_uri_pin_source, "/uri/test_uri_pin_source"); + p11_test (test_uri_free_null, "/uri/test_uri_free_null"); + p11_test (test_uri_message, "/uri/test_uri_message"); + + return p11_test_run (argc, argv); } diff --git a/p11-kit/tests/test-virtual.c b/p11-kit/tests/test-virtual.c new file mode 100644 index 0000000..73777d3 --- /dev/null +++ b/p11-kit/tests/test-virtual.c @@ -0,0 +1,171 @@ +/* + * Copyright (c) 2012 Stefan Walter + * Copyright (c) 2012 Red Hat Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * * The names of contributors to this software may not be + * used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * Author: Stef Walter <stef@thewalter.net> + */ + +#include "config.h" + +#include "library.h" +#include "p11-kit.h" +#include "private.h" +#include "virtual.h" + +#include "test.h" + +#include "mock.h" + +#include <sys/types.h> +#include <assert.h> +#include <string.h> +#include <stdio.h> +#include <stdlib.h> + +/* + * test-managed.c is a pretty good test of the closure code, so we + * just test a few things here. + */ + +typedef struct { + p11_virtual virt; + void *check; +} Override; + +static CK_RV +override_initialize (CK_X_FUNCTION_LIST *self, + CK_VOID_PTR args) +{ + Override *over = (Override *)self; + + assert_str_eq ("initialize-arg", args); + assert_str_eq ("overide-arg", over->check); + + /* An arbitrary error code to check */ + return CKR_NEED_TO_CREATE_THREADS; +} + +static bool test_destroyed = false; + +static void +test_destroyer (void *data) +{ + assert (data == &mock_x_module_no_slots); + assert (test_destroyed == false); + test_destroyed = true; +} + +static void +test_initialize (void) +{ + CK_FUNCTION_LIST_PTR module; + Override over = { }; + CK_RV rv; + + p11_virtual_init (&over.virt, &p11_virtual_stack, &mock_x_module_no_slots, test_destroyer); + over.virt.funcs.C_Initialize = override_initialize; + over.check = "overide-arg"; + test_destroyed = false; + + module = p11_virtual_wrap (&over.virt, (p11_destroyer)p11_virtual_uninit); + assert_ptr_not_null (module); + + rv = (module->C_Initialize) ("initialize-arg"); + assert_num_eq (CKR_NEED_TO_CREATE_THREADS, rv); + + p11_virtual_unwrap (module); + assert_num_eq (true, test_destroyed); +} + +static void +test_fall_through (void) +{ + CK_FUNCTION_LIST_PTR module; + Override over = { }; + p11_virtual base; + CK_RV rv; + + p11_virtual_init (&base, &p11_virtual_base, &mock_module_no_slots, NULL); + p11_virtual_init (&over.virt, &p11_virtual_stack, &base, NULL); + over.virt.funcs.C_Initialize = override_initialize; + over.check = "overide-arg"; + + module = p11_virtual_wrap (&over.virt, NULL); + assert_ptr_not_null (module); + + rv = (module->C_Initialize) ("initialize-arg"); + assert_num_eq (CKR_NEED_TO_CREATE_THREADS, rv); + + /* All other functiosn should have just fallen through */ + assert_ptr_eq (mock_module_no_slots.C_Finalize, module->C_Finalize); + + p11_virtual_unwrap (module); +} + +static void +test_get_function_list (void) +{ + CK_FUNCTION_LIST_PTR module; + CK_FUNCTION_LIST_PTR list; + p11_virtual virt; + CK_RV rv; + + p11_virtual_init (&virt, &p11_virtual_base, &mock_x_module_no_slots, NULL); + module = p11_virtual_wrap (&virt, NULL); + assert_ptr_not_null (module); + + rv = (module->C_GetFunctionList) (&list); + assert_num_eq (CKR_OK, rv); + assert_ptr_eq (module, list); + + rv = (module->C_GetFunctionList) (&list); + assert_num_eq (CKR_OK, rv); + + rv = (module->C_GetFunctionList) (NULL); + assert_num_eq (CKR_ARGUMENTS_BAD, rv); + + p11_virtual_unwrap (module); +} + +int +main (int argc, + char *argv[]) +{ + mock_module_init (); + p11_library_init (); + + assert (p11_virtual_can_wrap ()); + p11_test (test_initialize, "/virtual/test_initialize"); + p11_test (test_fall_through, "/virtual/test_fall_through"); + p11_test (test_get_function_list, "/virtual/test_get_function_list"); + + return p11_test_run (argc, argv); +} |