summaryrefslogtreecommitdiff
path: root/p11-kit/tests
diff options
context:
space:
mode:
Diffstat (limited to 'p11-kit/tests')
-rw-r--r--p11-kit/tests/Makefile.am91
-rw-r--r--p11-kit/tests/files/package-modules/four.module5
-rw-r--r--p11-kit/tests/files/package-modules/win32/four.module4
-rw-r--r--p11-kit/tests/files/system-modules/one.module5
-rw-r--r--p11-kit/tests/files/system-modules/two-duplicate.module4
-rw-r--r--p11-kit/tests/files/system-modules/two.badname6
-rw-r--r--p11-kit/tests/files/system-modules/win32/one.module4
-rw-r--r--p11-kit/tests/files/system-modules/win32/two-duplicate.module4
-rw-r--r--p11-kit/tests/files/system-modules/win32/two.badname6
-rw-r--r--p11-kit/tests/files/system-pkcs11.conf6
-rw-r--r--p11-kit/tests/files/test-1.conf6
-rw-r--r--p11-kit/tests/files/test-pinfile1
-rw-r--r--p11-kit/tests/files/test-pinfile-large53
-rw-r--r--p11-kit/tests/files/test-system-invalid.conf3
-rw-r--r--p11-kit/tests/files/test-system-merge.conf7
-rw-r--r--p11-kit/tests/files/test-system-none.conf8
-rw-r--r--p11-kit/tests/files/test-system-only.conf8
-rw-r--r--p11-kit/tests/files/test-user-invalid.conf3
-rw-r--r--p11-kit/tests/files/test-user-only.conf4
-rw-r--r--p11-kit/tests/files/test-user.conf3
-rw-r--r--p11-kit/tests/files/user-modules/one.module4
-rw-r--r--p11-kit/tests/files/user-modules/three.module6
-rw-r--r--p11-kit/tests/files/user-modules/win32/one.module2
-rw-r--r--p11-kit/tests/files/user-modules/win32/three.module6
-rw-r--r--p11-kit/tests/frob-setuid.c95
-rw-r--r--p11-kit/tests/mock-module-ep.c54
-rw-r--r--p11-kit/tests/mock-module-ep2.c56
-rw-r--r--p11-kit/tests/print-messages.c137
-rw-r--r--p11-kit/tests/test-conf.c456
-rw-r--r--p11-kit/tests/test-deprecated.c513
-rw-r--r--p11-kit/tests/test-init.c420
-rw-r--r--p11-kit/tests/test-iter.c1331
-rw-r--r--p11-kit/tests/test-log.c112
-rw-r--r--p11-kit/tests/test-managed.c262
-rw-r--r--p11-kit/tests/test-mock.c1685
-rw-r--r--p11-kit/tests/test-modules.c415
-rw-r--r--p11-kit/tests/test-pin.c313
-rw-r--r--p11-kit/tests/test-progname.c86
-rw-r--r--p11-kit/tests/test-proxy.c195
-rw-r--r--p11-kit/tests/test-rpc.c1061
-rw-r--r--p11-kit/tests/test-transport.c281
-rw-r--r--p11-kit/tests/test-uri.c1314
-rw-r--r--p11-kit/tests/test-util.c59
-rw-r--r--p11-kit/tests/test-virtual.c171
44 files changed, 0 insertions, 9265 deletions
diff --git a/p11-kit/tests/Makefile.am b/p11-kit/tests/Makefile.am
deleted file mode 100644
index a7049a4..0000000
--- a/p11-kit/tests/Makefile.am
+++ /dev/null
@@ -1,91 +0,0 @@
-
-include $(top_srcdir)/build/Makefile.tests
-
-COMMON = $(top_srcdir)/common
-
-AM_CPPFLAGS = \
- -I$(top_srcdir) \
- -I$(srcdir)/.. \
- -I$(COMMON) \
- $(TEST_CFLAGS)
-
-LDADD = \
- $(top_builddir)/p11-kit/libp11-kit-testable.la \
- $(top_builddir)/common/libp11-test.la \
- $(top_builddir)/common/libp11-common.la \
- $(LTLIBINTL)
-
-CHECK_PROGS = \
- test-progname \
- test-util \
- test-conf \
- test-uri \
- test-pin \
- test-init \
- test-modules \
- test-deprecated \
- test-proxy \
- test-iter \
- test-rpc \
- $(NULL)
-
-noinst_PROGRAMS = \
- print-messages \
- frob-setuid \
- $(CHECK_PROGS)
-
-if WITH_FFI
-
-CHECK_PROGS += \
- test-virtual \
- test-managed \
- test-log \
- test-transport \
- $(NULL)
-
-endif
-
-TESTS = $(CHECK_PROGS)
-
-noinst_LTLIBRARIES = \
- mock-one.la \
- mock-two.la \
- mock-three.la \
- mock-four.la
-
-mock_one_la_SOURCES = \
- mock-module-ep.c
-
-mock_one_la_CFLAGS = \
- $(AM_CFLAGS)
-
-mock_one_la_LIBADD = \
- $(top_builddir)/common/libp11-test.la \
- $(top_builddir)/common/libp11-common.la \
- $(NULL)
-
-mock_one_la_LDFLAGS = \
- -module -avoid-version -rpath /nowhere \
- -no-undefined -export-symbols-regex 'C_GetFunctionList'
-
-mock_two_la_SOURCES = \
- mock-module-ep2.c
-
-mock_two_la_CFLAGS = $(mock_one_la_CFLAGS)
-mock_two_la_LDFLAGS = $(mock_one_la_LDFLAGS)
-mock_two_la_LIBADD = $(mock_one_la_LIBADD)
-
-mock_three_la_SOURCES = $(mock_one_la_SOURCES)
-mock_three_la_CFLAGS = $(mock_one_la_CFLAGS)
-mock_three_la_LDFLAGS = $(mock_one_la_LDFLAGS)
-mock_three_la_LIBADD = $(mock_one_la_LIBADD)
-
-mock_four_la_SOURCES = $(mock_one_la_SOURCES)
-mock_four_la_CFLAGS = $(mock_one_la_CFLAGS)
-mock_four_la_LDFLAGS = $(mock_one_la_LDFLAGS)
-mock_four_la_LIBADD = $(mock_one_la_LIBADD)
-
-EXTRA_DIST = \
- files \
- test-mock.c \
- $(NULL)
diff --git a/p11-kit/tests/files/package-modules/four.module b/p11-kit/tests/files/package-modules/four.module
deleted file mode 100644
index 933af2b..0000000
--- a/p11-kit/tests/files/package-modules/four.module
+++ /dev/null
@@ -1,5 +0,0 @@
-
-module: mock-four.so
-disable-in: test-disable, test-other
-priority: 4
-trust-policy: no \ No newline at end of file
diff --git a/p11-kit/tests/files/package-modules/win32/four.module b/p11-kit/tests/files/package-modules/win32/four.module
deleted file mode 100644
index 6dc87c9..0000000
--- a/p11-kit/tests/files/package-modules/win32/four.module
+++ /dev/null
@@ -1,4 +0,0 @@
-
-module: mock-four.dll
-disable-in: test-disable, test-other
-priority: 4 \ No newline at end of file
diff --git a/p11-kit/tests/files/system-modules/one.module b/p11-kit/tests/files/system-modules/one.module
deleted file mode 100644
index 5f49a8f..0000000
--- a/p11-kit/tests/files/system-modules/one.module
+++ /dev/null
@@ -1,5 +0,0 @@
-
-module: mock-one.so
-setting: system1
-trust-policy: yes
-number: 18
diff --git a/p11-kit/tests/files/system-modules/two-duplicate.module b/p11-kit/tests/files/system-modules/two-duplicate.module
deleted file mode 100644
index 756af69..0000000
--- a/p11-kit/tests/files/system-modules/two-duplicate.module
+++ /dev/null
@@ -1,4 +0,0 @@
-
-# This is a duplicate of the 'two' module
-module: mock-two.so
-# no priority, use name \ No newline at end of file
diff --git a/p11-kit/tests/files/system-modules/two.badname b/p11-kit/tests/files/system-modules/two.badname
deleted file mode 100644
index eec3af0..0000000
--- a/p11-kit/tests/files/system-modules/two.badname
+++ /dev/null
@@ -1,6 +0,0 @@
-# This module doesn't have a .module extension, but p11-kit doesn't yet
-# enforce the naming, just warns, so it should still be loaded
-
-module: mock-two.so
-setting: system2
-# no priority, use name \ No newline at end of file
diff --git a/p11-kit/tests/files/system-modules/win32/one.module b/p11-kit/tests/files/system-modules/win32/one.module
deleted file mode 100644
index d153ce5..0000000
--- a/p11-kit/tests/files/system-modules/win32/one.module
+++ /dev/null
@@ -1,4 +0,0 @@
-
-module: mock-one.dll
-setting: system1
-# no order, use name \ No newline at end of file
diff --git a/p11-kit/tests/files/system-modules/win32/two-duplicate.module b/p11-kit/tests/files/system-modules/win32/two-duplicate.module
deleted file mode 100644
index 54ef1cc..0000000
--- a/p11-kit/tests/files/system-modules/win32/two-duplicate.module
+++ /dev/null
@@ -1,4 +0,0 @@
-
-# This is a duplicate of the 'two' module
-module: mock-two.dll
-# no order, use name \ No newline at end of file
diff --git a/p11-kit/tests/files/system-modules/win32/two.badname b/p11-kit/tests/files/system-modules/win32/two.badname
deleted file mode 100644
index af63cf9..0000000
--- a/p11-kit/tests/files/system-modules/win32/two.badname
+++ /dev/null
@@ -1,6 +0,0 @@
-# This module doesn't have a .module extension, but p11-kit doesn't yet
-# enforce the naming, just warns, so it should still be loaded
-
-module: mock-two.dll
-setting: system2
-# no order, use name \ No newline at end of file
diff --git a/p11-kit/tests/files/system-pkcs11.conf b/p11-kit/tests/files/system-pkcs11.conf
deleted file mode 100644
index a3aa273..0000000
--- a/p11-kit/tests/files/system-pkcs11.conf
+++ /dev/null
@@ -1,6 +0,0 @@
-
-# Merge in user config
-user-config: merge
-
-# Another option
-new: world \ No newline at end of file
diff --git a/p11-kit/tests/files/test-1.conf b/p11-kit/tests/files/test-1.conf
deleted file mode 100644
index d4ae0a1..0000000
--- a/p11-kit/tests/files/test-1.conf
+++ /dev/null
@@ -1,6 +0,0 @@
-key1:value1
-with-whitespace : value-with-whitespace
-with-colon: value-of-colon
-
-# A comment
-embedded-comment: this is # not a comment
diff --git a/p11-kit/tests/files/test-pinfile b/p11-kit/tests/files/test-pinfile
deleted file mode 100644
index f646f3d..0000000
--- a/p11-kit/tests/files/test-pinfile
+++ /dev/null
@@ -1 +0,0 @@
-yogabbagabba \ No newline at end of file
diff --git a/p11-kit/tests/files/test-pinfile-large b/p11-kit/tests/files/test-pinfile-large
deleted file mode 100644
index 506668d..0000000
--- a/p11-kit/tests/files/test-pinfile-large
+++ /dev/null
@@ -1,53 +0,0 @@
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba
-yogabbagabba yogabbagabba yogabbagabba yo \ No newline at end of file
diff --git a/p11-kit/tests/files/test-system-invalid.conf b/p11-kit/tests/files/test-system-invalid.conf
deleted file mode 100644
index 344ee96..0000000
--- a/p11-kit/tests/files/test-system-invalid.conf
+++ /dev/null
@@ -1,3 +0,0 @@
-
-# Invalid user-config setting
-user-config: bad
diff --git a/p11-kit/tests/files/test-system-merge.conf b/p11-kit/tests/files/test-system-merge.conf
deleted file mode 100644
index 978427d..0000000
--- a/p11-kit/tests/files/test-system-merge.conf
+++ /dev/null
@@ -1,7 +0,0 @@
-
-# Merge in user config
-user-config: merge
-
-key1: system1
-key2: system2
-key3: system3 \ No newline at end of file
diff --git a/p11-kit/tests/files/test-system-none.conf b/p11-kit/tests/files/test-system-none.conf
deleted file mode 100644
index 95351e6..0000000
--- a/p11-kit/tests/files/test-system-none.conf
+++ /dev/null
@@ -1,8 +0,0 @@
-
-# Only user config
-user-config: none
-
-# These values will not be overriden
-key1: system1
-key2: system2
-key3: system3 \ No newline at end of file
diff --git a/p11-kit/tests/files/test-system-only.conf b/p11-kit/tests/files/test-system-only.conf
deleted file mode 100644
index 589f1c7..0000000
--- a/p11-kit/tests/files/test-system-only.conf
+++ /dev/null
@@ -1,8 +0,0 @@
-
-# Only user config
-user-config: only
-
-# This stuff will be ignored
-key1: system1
-key2: system2
-key3: system3 \ No newline at end of file
diff --git a/p11-kit/tests/files/test-user-invalid.conf b/p11-kit/tests/files/test-user-invalid.conf
deleted file mode 100644
index 344ee96..0000000
--- a/p11-kit/tests/files/test-user-invalid.conf
+++ /dev/null
@@ -1,3 +0,0 @@
-
-# Invalid user-config setting
-user-config: bad
diff --git a/p11-kit/tests/files/test-user-only.conf b/p11-kit/tests/files/test-user-only.conf
deleted file mode 100644
index 3224c01..0000000
--- a/p11-kit/tests/files/test-user-only.conf
+++ /dev/null
@@ -1,4 +0,0 @@
-
-user-config: only
-key2: user2
-key3: user3 \ No newline at end of file
diff --git a/p11-kit/tests/files/test-user.conf b/p11-kit/tests/files/test-user.conf
deleted file mode 100644
index 369544a..0000000
--- a/p11-kit/tests/files/test-user.conf
+++ /dev/null
@@ -1,3 +0,0 @@
-
-key2: user2
-key3: user3 \ 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
deleted file mode 100644
index 5197daf..0000000
--- a/p11-kit/tests/files/user-modules/one.module
+++ /dev/null
@@ -1,4 +0,0 @@
-
-setting: user1
-managed: yes
-number: 33
diff --git a/p11-kit/tests/files/user-modules/three.module b/p11-kit/tests/files/user-modules/three.module
deleted file mode 100644
index 3a2366d..0000000
--- a/p11-kit/tests/files/user-modules/three.module
+++ /dev/null
@@ -1,6 +0,0 @@
-
-module: mock-three.so
-setting: user3
-
-enable-in: test-enable
-priority: 3 \ No newline at end of file
diff --git a/p11-kit/tests/files/user-modules/win32/one.module b/p11-kit/tests/files/user-modules/win32/one.module
deleted file mode 100644
index c371e4a..0000000
--- a/p11-kit/tests/files/user-modules/win32/one.module
+++ /dev/null
@@ -1,2 +0,0 @@
-
-setting: user1 \ No newline at end of file
diff --git a/p11-kit/tests/files/user-modules/win32/three.module b/p11-kit/tests/files/user-modules/win32/three.module
deleted file mode 100644
index 30a3b63..0000000
--- a/p11-kit/tests/files/user-modules/win32/three.module
+++ /dev/null
@@ -1,6 +0,0 @@
-
-module: mock-three.dll
-setting: user3
-
-enable-in: test-enable
-priority: 3 \ No newline at end of file
diff --git a/p11-kit/tests/frob-setuid.c b/p11-kit/tests/frob-setuid.c
deleted file mode 100644
index e546ece..0000000
--- a/p11-kit/tests/frob-setuid.c
+++ /dev/null
@@ -1,95 +0,0 @@
-/*
- * 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 <assert.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include "compat.h"
-#include "p11-kit.h"
-
-int
-main (void)
-{
- CK_FUNCTION_LIST **modules;
- CK_FUNCTION_LIST *module;
- char *field;
- char *name;
- int ret;
- int i;
-
- /*
- * Use 'chmod ug+s frob-setuid' to change this program
- * and test the output with/without setuid or setgid.
- */
-
- putenv ("P11_KIT_STRICT=1");
-
- modules = p11_kit_modules_load_and_initialize (0);
- assert (modules != NULL);
-
- /* This is a system configured module */
- module = p11_kit_module_for_name (modules, "one");
- assert (module != NULL);
-
- field = p11_kit_config_option (module, "setting");
- printf ("'setting' on module 'one': %s\n", field ? field : "(null)");
-
- assert (field != NULL);
- if (getauxval (AT_SECURE))
- assert (strcmp (field, "system1") == 0);
- else
- assert (strcmp (field, "user1") == 0);
-
- free (field);
-
- for (i = 0; modules[i] != NULL; i++) {
- name = p11_kit_module_get_name (modules[i]);
- printf ("%s\n", name);
- free (name);
- }
-
- field = p11_kit_config_option (module, "number");
- printf ("'number' on module 'one': %s\n", field ? field : "(null)");
-
- ret = atoi (field ? field : "0");
- assert (ret != 0);
- free (field);
-
- p11_kit_modules_finalize_and_release (modules);
- return ret;
-}
diff --git a/p11-kit/tests/mock-module-ep.c b/p11-kit/tests/mock-module-ep.c
deleted file mode 100644
index 9ba739a..0000000
--- a/p11-kit/tests/mock-module-ep.c
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
- * Copyright (c) 2012 Stefan Walter
- *
- * 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"
-
-#define CRYPTOKI_EXPORTS 1
-#include "pkcs11.h"
-
-#include "mock.h"
-
-#ifdef OS_WIN32
-__declspec(dllexport)
-#endif
-CK_RV
-C_GetFunctionList (CK_FUNCTION_LIST_PTR_PTR list)
-{
- mock_module_init ();
- mock_module_no_slots.C_GetFunctionList = C_GetFunctionList;
- if (list == NULL)
- return CKR_ARGUMENTS_BAD;
- *list = &mock_module;
- return CKR_OK;
-}
diff --git a/p11-kit/tests/mock-module-ep2.c b/p11-kit/tests/mock-module-ep2.c
deleted file mode 100644
index ee71711..0000000
--- a/p11-kit/tests/mock-module-ep2.c
+++ /dev/null
@@ -1,56 +0,0 @@
-/*
- * Copyright (c) 2012 Stefan Walter
- *
- * 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"
-
-#define CRYPTOKI_EXPORTS 1
-#include "pkcs11.h"
-
-#include "mock.h"
-
-#include <stdio.h>
-
-#ifdef OS_WIN32
-__declspec(dllexport)
-#endif
-CK_RV
-C_GetFunctionList (CK_FUNCTION_LIST_PTR_PTR list)
-{
- mock_module_init ();
- mock_module.C_GetFunctionList = C_GetFunctionList;
- if (list == NULL)
- return CKR_ARGUMENTS_BAD;
- *list = &mock_module;
- return CKR_OK;
-}
diff --git a/p11-kit/tests/print-messages.c b/p11-kit/tests/print-messages.c
deleted file mode 100644
index 5870ad1..0000000
--- a/p11-kit/tests/print-messages.c
+++ /dev/null
@@ -1,137 +0,0 @@
-/*
- * Copyright (c) 2011, Collabora Ltd.
- *
- * 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@collabora.co.uk>
- */
-
-#include "config.h"
-
-#include <assert.h>
-#include <string.h>
-#include <stdio.h>
-#include <stdlib.h>
-
-#include "p11-kit.h"
-
-int
-main (int argc, char *argv[])
-{
- if (argc != 1) {
- fprintf (stderr, "usage: print-messages\n");
- exit (2);
- }
-
- #define X(x) printf ("%s: %s\n", #x, p11_kit_strerror (x))
- X(CKR_CANCEL);
- X(CKR_FUNCTION_CANCELED);
- X(CKR_HOST_MEMORY);
- X(CKR_SLOT_ID_INVALID);
- X(CKR_GENERAL_ERROR);
- X(CKR_FUNCTION_FAILED);
- X(CKR_ARGUMENTS_BAD);
- X(CKR_NEED_TO_CREATE_THREADS);
- X(CKR_CANT_LOCK);
- X(CKR_ATTRIBUTE_READ_ONLY);
- X(CKR_ATTRIBUTE_SENSITIVE);
- X(CKR_ATTRIBUTE_TYPE_INVALID);
- X(CKR_ATTRIBUTE_VALUE_INVALID);
- X(CKR_DATA_INVALID);
- X(CKR_DATA_LEN_RANGE);
- X(CKR_DEVICE_ERROR);
- X(CKR_DEVICE_MEMORY);
- X(CKR_DEVICE_REMOVED);
- X(CKR_ENCRYPTED_DATA_INVALID);
- X(CKR_ENCRYPTED_DATA_LEN_RANGE);
- X(CKR_FUNCTION_NOT_SUPPORTED);
- X(CKR_KEY_HANDLE_INVALID);
- X(CKR_KEY_SIZE_RANGE);
- X(CKR_KEY_TYPE_INCONSISTENT);
- X(CKR_KEY_NOT_NEEDED);
- X(CKR_KEY_CHANGED);
- X(CKR_KEY_NEEDED);
- X(CKR_KEY_INDIGESTIBLE);
- X(CKR_KEY_FUNCTION_NOT_PERMITTED);
- X(CKR_KEY_NOT_WRAPPABLE);
- X(CKR_KEY_UNEXTRACTABLE);
- X(CKR_MECHANISM_INVALID);
- X(CKR_MECHANISM_PARAM_INVALID);
- X(CKR_OBJECT_HANDLE_INVALID);
- X(CKR_OPERATION_ACTIVE);
- X(CKR_OPERATION_NOT_INITIALIZED);
- X(CKR_PIN_INCORRECT);
- X(CKR_PIN_INVALID);
- X(CKR_PIN_LEN_RANGE);
- X(CKR_PIN_EXPIRED);
- X(CKR_PIN_LOCKED);
- X(CKR_SESSION_CLOSED);
- X(CKR_SESSION_COUNT);
- X(CKR_SESSION_HANDLE_INVALID);
- X(CKR_SESSION_READ_ONLY);
- X(CKR_SESSION_EXISTS);
- X(CKR_SESSION_READ_ONLY_EXISTS);
- X(CKR_SESSION_READ_WRITE_SO_EXISTS);
- X(CKR_SIGNATURE_INVALID);
- X(CKR_SIGNATURE_LEN_RANGE);
- X(CKR_TEMPLATE_INCOMPLETE);
- X(CKR_TEMPLATE_INCONSISTENT);
- X(CKR_TOKEN_NOT_PRESENT);
- X(CKR_TOKEN_NOT_RECOGNIZED);
- X(CKR_TOKEN_WRITE_PROTECTED);
- X(CKR_UNWRAPPING_KEY_HANDLE_INVALID);
- X(CKR_UNWRAPPING_KEY_SIZE_RANGE);
- X(CKR_UNWRAPPING_KEY_TYPE_INCONSISTENT);
- X(CKR_USER_ALREADY_LOGGED_IN);
- X(CKR_USER_NOT_LOGGED_IN);
- X(CKR_USER_PIN_NOT_INITIALIZED);
- X(CKR_USER_TYPE_INVALID);
- X(CKR_USER_ANOTHER_ALREADY_LOGGED_IN);
- X(CKR_USER_TOO_MANY_TYPES);
- X(CKR_WRAPPED_KEY_INVALID);
- X(CKR_WRAPPED_KEY_LEN_RANGE);
- X(CKR_WRAPPING_KEY_HANDLE_INVALID);
- X(CKR_WRAPPING_KEY_SIZE_RANGE);
- X(CKR_WRAPPING_KEY_TYPE_INCONSISTENT);
- X(CKR_RANDOM_SEED_NOT_SUPPORTED);
- X(CKR_RANDOM_NO_RNG);
- X(CKR_DOMAIN_PARAMS_INVALID);
- X(CKR_BUFFER_TOO_SMALL);
- X(CKR_SAVED_STATE_INVALID);
- X(CKR_INFORMATION_SENSITIVE);
- X(CKR_STATE_UNSAVEABLE);
- X(CKR_CRYPTOKI_NOT_INITIALIZED);
- X(CKR_CRYPTOKI_ALREADY_INITIALIZED);
- X(CKR_MUTEX_BAD);
- X(CKR_MUTEX_NOT_LOCKED);
- X(CKR_FUNCTION_REJECTED);
- #undef X
-
- return 0;
-}
diff --git a/p11-kit/tests/test-conf.c b/p11-kit/tests/test-conf.c
deleted file mode 100644
index d5dc415..0000000
--- a/p11-kit/tests/test-conf.c
+++ /dev/null
@@ -1,456 +0,0 @@
-/*
- * Copyright (c) 2011, Collabora Ltd.
- *
- * 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@collabora.co.uk>
- */
-
-#include "config.h"
-#include "test.h"
-
-#include <errno.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-
-#include "conf.h"
-#include "debug.h"
-#include "message.h"
-#include "p11-kit.h"
-#include "private.h"
-
-#ifdef OS_UNIX
-#include <sys/stat.h>
-#include <sys/wait.h>
-#include <unistd.h>
-#endif
-
-static void
-test_parse_conf_1 (void)
-{
- p11_dict *map;
- const char *value;
-
- map = _p11_conf_parse_file (SRCDIR "/files/test-1.conf", NULL, 0);
- assert_ptr_not_null (map);
-
- value = p11_dict_get (map, "key1");
- assert_str_eq ("value1", value);
-
- value = p11_dict_get (map, "with-colon");
- assert_str_eq ("value-of-colon", value);
-
- value = p11_dict_get (map, "with-whitespace");
- assert_str_eq ("value-with-whitespace", value);
-
- value = p11_dict_get (map, "embedded-comment");
- assert_str_eq ("this is # not a comment", value);
-
- p11_dict_free (map);
-}
-
-static void
-test_parse_ignore_missing (void)
-{
- p11_dict *map;
-
- map = _p11_conf_parse_file (SRCDIR "/files/non-existant.conf", NULL, CONF_IGNORE_MISSING);
- assert_ptr_not_null (map);
-
- assert_num_eq (0, p11_dict_size (map));
- assert (p11_message_last () == NULL);
- p11_dict_free (map);
-}
-
-static void
-test_parse_fail_missing (void)
-{
- p11_dict *map;
-
- map = _p11_conf_parse_file (SRCDIR "/files/non-existant.conf", NULL, 0);
- assert (map == NULL);
- assert_ptr_not_null (p11_message_last ());
-}
-
-static void
-test_merge_defaults (void)
-{
- p11_dict *values;
- p11_dict *defaults;
-
- values = p11_dict_new (p11_dict_str_hash, p11_dict_str_equal, free, free);
- defaults = p11_dict_new (p11_dict_str_hash, p11_dict_str_equal, free, free);
-
- p11_dict_set (values, strdup ("one"), strdup ("real1"));
- p11_dict_set (values, strdup ("two"), strdup ("real2"));
-
- p11_dict_set (defaults, strdup ("two"), strdup ("default2"));
- p11_dict_set (defaults, strdup ("three"), strdup ("default3"));
-
- if (!_p11_conf_merge_defaults (values, defaults))
- assert_not_reached ();
-
- p11_dict_free (defaults);
-
- 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 (void)
-{
- int user_mode = -1;
- p11_dict *config;
-
- p11_message_clear ();
-
- config = _p11_conf_load_globals (SRCDIR "/files/test-system-merge.conf",
- SRCDIR "/files/test-user.conf",
- &user_mode);
- assert_ptr_not_null (config);
- assert (NULL == p11_message_last ());
- assert_num_eq (CONF_USER_MERGE, user_mode);
-
- 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 (void)
-{
- int user_mode = -1;
- p11_dict *config;
-
- p11_message_clear ();
-
- config = _p11_conf_load_globals (SRCDIR "/files/test-system-none.conf",
- SRCDIR "/files/test-user.conf",
- &user_mode);
- assert_ptr_not_null (config);
- assert (NULL == p11_message_last ());
- assert_num_eq (CONF_USER_NONE, user_mode);
-
- 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 (void)
-{
- int user_mode = -1;
- p11_dict *config;
-
- p11_message_clear ();
-
- config = _p11_conf_load_globals (SRCDIR "/files/test-system-merge.conf",
- SRCDIR "/files/test-user-only.conf",
- &user_mode);
- assert_ptr_not_null (config);
- assert (NULL == p11_message_last ());
- assert_num_eq (CONF_USER_ONLY, user_mode);
-
- 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 (void)
-{
- int user_mode = -1;
- p11_dict *config;
-
- p11_message_clear ();
-
- config = _p11_conf_load_globals (SRCDIR "/files/test-system-only.conf",
- SRCDIR "/files/test-user.conf",
- &user_mode);
- assert_ptr_not_null (config);
- assert (NULL == p11_message_last ());
- assert_num_eq (CONF_USER_ONLY, user_mode);
-
- 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 (void)
-{
- int user_mode = -1;
- p11_dict *config;
- int error;
-
- p11_message_clear ();
-
- config = _p11_conf_load_globals (SRCDIR "/files/test-system-invalid.conf",
- SRCDIR "/files/non-existant.conf",
- &user_mode);
- error = errno;
- 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 (void)
-{
- int user_mode = -1;
- p11_dict *config;
- int error;
-
- p11_message_clear ();
-
- config = _p11_conf_load_globals (SRCDIR "/files/test-system-merge.conf",
- SRCDIR "/files/test-user-invalid.conf",
- &user_mode);
- error = errno;
- assert_ptr_eq (NULL, config);
- assert_num_eq (EINVAL, error);
- assert_ptr_not_null (p11_message_last ());
-
- p11_dict_free (config);
-}
-
-static bool
-assert_msg_contains (const char *msg,
- const char *text)
-{
- return (msg && strstr (msg, text)) ? true : false;
-}
-
-static void
-test_load_modules_merge (void)
-{
- p11_dict *configs;
- p11_dict *config;
-
- p11_message_clear ();
-
- configs = _p11_conf_load_modules (CONF_USER_MERGE,
- SRCDIR "/files/package-modules",
- SRCDIR "/files/system-modules",
- SRCDIR "/files/user-modules");
- assert_ptr_not_null (configs);
- assert (assert_msg_contains (p11_message_last (), "invalid config filename"));
-
- config = p11_dict_get (configs, "one");
- 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");
- 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");
- 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 (void)
-{
- p11_dict *configs;
- p11_dict *config;
-
- p11_message_clear ();
-
- configs = _p11_conf_load_modules (CONF_USER_NONE,
- SRCDIR "/files/package-modules",
- SRCDIR "/files/system-modules",
- SRCDIR "/files/user-modules");
- assert_ptr_not_null (configs);
- assert (assert_msg_contains (p11_message_last (), "invalid config filename"));
-
- config = p11_dict_get (configs, "one");
- 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");
- 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");
- assert_ptr_eq (NULL, config);
-
- p11_dict_free (configs);
-}
-
-static void
-test_load_modules_user_only (void)
-{
- p11_dict *configs;
- p11_dict *config;
-
- p11_message_clear ();
-
- configs = _p11_conf_load_modules (CONF_USER_ONLY,
- SRCDIR "/files/package-modules",
- SRCDIR "/files/system-modules",
- SRCDIR "/files/user-modules");
- assert_ptr_not_null (configs);
- assert_ptr_eq (NULL, (void *)p11_message_last ());
-
- config = p11_dict_get (configs, "one");
- 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");
- assert_ptr_eq (NULL, config);
-
- config = p11_dict_get (configs, "three");
- 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 (void)
-{
- p11_dict *configs;
- p11_dict *config;
-
- p11_message_clear ();
-
- configs = _p11_conf_load_modules (CONF_USER_MERGE,
- SRCDIR "/files/package-modules",
- SRCDIR "/files/system-modules",
- SRCDIR "/files/non-existant");
- assert_ptr_not_null (configs);
- assert (assert_msg_contains (p11_message_last (), "invalid config filename"));
-
- config = p11_dict_get (configs, "one");
- 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");
- 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");
- assert_ptr_eq (NULL, config);
-
- p11_dict_free (configs);
-}
-
-static void
-test_parse_boolean (void)
-{
- p11_message_quiet ();
-
- 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));
-}
-
-#ifdef OS_UNIX
-
-static void
-test_setuid (void)
-{
- const char *args[] = { BUILDDIR "/frob-setuid", NULL, };
- char *path;
- int ret;
-
- /* This is the 'number' setting set in one.module user configuration. */
- ret = p11_test_run_child (args, true);
- assert_num_eq (ret, 33);
-
- path = p11_test_copy_setgid (args[0]);
- if (path == NULL)
- return;
-
- args[0] = path;
-
- /* This is the 'number' setting set in one.module system configuration. */
- ret = p11_test_run_child (args, true);
- assert_num_eq (ret, 18);
-
- if (unlink (path) < 0)
- assert_fail ("unlink failed", strerror (errno));
- free (path);
-}
-
-#endif /* OS_UNIX */
-
-int
-main (int argc,
- char *argv[])
-{
- 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");
-#ifdef OS_UNIX
- /* Don't run this test when under fakeroot */
- if (!getenv ("FAKED_MODE")) {
- p11_test (test_setuid, "/conf/setuid");
- }
-#endif
- return p11_test_run (argc, argv);
-}
diff --git a/p11-kit/tests/test-deprecated.c b/p11-kit/tests/test-deprecated.c
deleted file mode 100644
index c8b8001..0000000
--- a/p11-kit/tests/test-deprecated.c
+++ /dev/null
@@ -1,513 +0,0 @@
-/*
- * 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) {
- close (1);
- 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;
-
- p11_mutex_lock (&race_mutex);
- initialization_count = 0;
- finalization_count = 0;
- p11_mutex_unlock (&race_mutex);
-
- 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 */
- p11_mutex_lock (&race_mutex);
- assert_num_eq (1, initialization_count);
- assert_num_eq (1, finalization_count);
- p11_mutex_unlock (&race_mutex);
-
- 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_num_eq (rv, 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
deleted file mode 100644
index c4fcecb..0000000
--- a/p11-kit/tests/test-init.c
+++ /dev/null
@@ -1,420 +0,0 @@
-/*
- * Copyright (c) 2011, Collabora Ltd.
- *
- * 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@collabora.co.uk>
- */
-
-#include "config.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>
-#include <stdlib.h>
-#include <string.h>
-#include <time.h>
-#include <unistd.h>
-
-static CK_FUNCTION_LIST module;
-static p11_mutex_t race_mutex;
-
-#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) {
- close (1);
- 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_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;
-
- 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);
-
- 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)
-{
- CK_RV rv;
-
- rv = mock_C_Initialize (init_args);
- assert (rv == CKR_OK);
-
- return p11_kit_module_initialize (recursive_managed);
-}
-
-static void
-test_recursive_initialization (void)
-{
- 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__with_recursive;
-
- 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 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_FUNCTION_LIST *module = data;
- CK_RV rv;
-
- assert (module != NULL);
- rv = p11_kit_module_initialize (module);
- assert_num_eq (rv, CKR_OK);
-
- return module;
-}
-
-static void *
-finalization_thread (void *data)
-{
- CK_FUNCTION_LIST *module = data;
- CK_RV rv;
-
- assert (module != NULL);
- rv = p11_kit_module_finalize (module);
- assert_num_eq (rv, CKR_OK);
-
- return module;
-}
-
-static void
-test_threaded_initialization (void)
-{
- 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;
-
- /* 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;
-
- memset (&data, 0, sizeof (data));
-
- p11_mutex_lock (&race_mutex);
- initialization_count = 0;
- finalization_count = 0;
- p11_mutex_unlock (&race_mutex);
-
- 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, data[i]);
- 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, data[i]);
- 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;
- }
-
- 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 */
- assert_num_eq (1, initialization_count);
- assert_num_eq (1, finalization_count);
-}
-
-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;
-
- 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_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;
-
- 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);
-
- p11_unlock ();
-}
-
-static void
-test_load_and_initialize (void)
-{
- CK_FUNCTION_LIST_PTR module;
- CK_INFO info;
- CK_RV rv;
- int ret;
-
- 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);
- assert (rv == CKR_OK);
-
- ret = memcmp (info.manufacturerID, "MOCK MANUFACTURER ", 32);
- assert (ret == 0);
-
- rv = p11_kit_module_finalize (module);
- assert (rv == CKR_OK);
-
- p11_kit_module_release (module);
-}
-
-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)
-{
-
-}
-
-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
- p11_test (test_fork_initialization, "/init/test_fork_initialization");
-#endif
- }
-
- p11_test (test_initalize_fail, "/init/test_initalize_fail");
- p11_test (test_finalize_fail, "/init/test_finalize_fail");
-
- return p11_test_run (argc, argv);
-}
diff --git a/p11-kit/tests/test-iter.c b/p11-kit/tests/test-iter.c
deleted file mode 100644
index 055a4b3..0000000
--- a/p11-kit/tests/test-iter.c
+++ /dev/null
@@ -1,1331 +0,0 @@
-/*
- * 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@collabora.co.uk>
- */
-
-#include "config.h"
-#include "test.h"
-
-#define P11_KIT_FUTURE_UNSTABLE_API 1
-
-#include "attrs.h"
-#include "dict.h"
-#include "iter.h"
-#include "library.h"
-#include "message.h"
-#include "mock.h"
-
-#include <assert.h>
-#include <string.h>
-#include <stdio.h>
-#include <stdlib.h>
-
-static CK_FUNCTION_LIST_PTR_PTR
-initialize_and_get_modules (void)
-{
- CK_FUNCTION_LIST_PTR_PTR modules;
-
- p11_message_quiet ();
-
- modules = p11_kit_modules_load_and_initialize (0);
- assert (modules != NULL && modules[0] != NULL);
-
- p11_message_loud ();
-
- return modules;
-}
-
-static void
-finalize_and_free_modules (CK_FUNCTION_LIST_PTR_PTR modules)
-{
- p11_kit_modules_finalize (modules);
- p11_kit_modules_release (modules);
-}
-
-static int
-has_handle (CK_ULONG *objects,
- int count,
- CK_ULONG handle)
-{
- int i;
- for (i = 0; i < count; i++) {
- if (objects[i] == handle)
- return 1;
- }
-
- return 0;
-}
-
-
-static void
-test_all (void)
-{
- CK_OBJECT_HANDLE objects[128];
- CK_FUNCTION_LIST_PTR *modules;
- CK_FUNCTION_LIST_PTR module;
- CK_SESSION_HANDLE session;
- CK_ULONG size;
- P11KitIter *iter;
- CK_RV rv;
- int at;
-
- modules = initialize_and_get_modules ();
-
- iter = p11_kit_iter_new (NULL, P11_KIT_ITER_BUSY_SESSIONS);
- p11_kit_iter_begin (iter, modules);
-
- at = 0;
- while ((rv = p11_kit_iter_next (iter)) == CKR_OK) {
- assert (at < 128);
- objects[at] = p11_kit_iter_get_object (iter);
-
- module = p11_kit_iter_get_module (iter);
- assert_ptr_not_null (module);
-
- session = p11_kit_iter_get_session (iter);
- assert (session != 0);
-
- /* Do something with the object */
- size = 0;
- rv = (module->C_GetObjectSize) (session, objects[at], &size);
- assert (rv == CKR_OK);
- assert (size > 0);
-
- at++;
- }
-
- assert (rv == CKR_CANCEL);
-
- /* Three modules, each with 1 slot, and 3 public objects */
- assert_num_eq (9, at);
-
- 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 (modules);
-}
-
-static CK_RV
-on_iter_callback (P11KitIter *iter,
- CK_BBOOL *matches,
- void *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;
- return CKR_OK;
- }
-
- module = p11_kit_iter_get_module (iter);
- assert_ptr_not_null (module);
-
- session = p11_kit_iter_get_session (iter);
- assert (session != 0);
-
- /* Do something with the object */
- size = 0;
- rv = (module->C_GetObjectSize) (session, object, &size);
- assert (rv == CKR_OK);
- assert (size > 0);
-
- return CKR_OK;
-}
-
-static void
-test_callback (void)
-{
- CK_OBJECT_HANDLE objects[128];
- CK_FUNCTION_LIST_PTR *modules;
- P11KitIter *iter;
- CK_RV rv;
- int at;
-
- modules = initialize_and_get_modules ();
-
- iter = p11_kit_iter_new (NULL, 0);
- 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) {
- assert (at < 128);
- objects[at] = p11_kit_iter_get_object (iter);
- at++;
- }
-
- assert (rv == CKR_CANCEL);
-
- /* Three modules, each with 1 slot, and 2 public keys */
- assert_num_eq (6, at);
-
- 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 (modules);
-}
-
-static CK_RV
-on_callback_fail (P11KitIter *iter,
- CK_BBOOL *matches,
- void *data)
-{
- return CKR_DATA_INVALID;
-}
-
-static void
-test_callback_fails (void)
-{
- CK_FUNCTION_LIST_PTR *modules;
- P11KitIter *iter;
- CK_RV rv;
- int at;
-
- modules = initialize_and_get_modules ();
-
- iter = p11_kit_iter_new (NULL, 0);
- 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++;
-
- assert (rv == CKR_DATA_INVALID);
-
- /* Shouldn't have succeeded at all */
- assert_num_eq (0, at);
-
- p11_kit_iter_free (iter);
- finalize_and_free_modules (modules);
-}
-
-static void
-on_destroy_increment (void *data)
-{
- int *value = data;
- (*value)++;
-}
-
-static void
-test_callback_destroyer (void)
-{
- P11KitIter *iter;
- int value = 1;
-
- iter = p11_kit_iter_new (NULL, 0);
- p11_kit_iter_add_callback (iter, on_callback_fail, &value, on_destroy_increment);
- p11_kit_iter_free (iter);
-
- assert_num_eq (2, value);
-}
-
-static void
-test_with_session (void)
-{
- CK_OBJECT_HANDLE objects[128];
- CK_SESSION_HANDLE session;
- CK_FUNCTION_LIST_PTR module;
- CK_SLOT_ID slot;
- P11KitIter *iter;
- CK_RV rv;
- int at;
-
- 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);
- assert (rv == CKR_OK);
-
- iter = p11_kit_iter_new (NULL, 0);
- p11_kit_iter_begin_with (iter, &mock_module, 0, session);
-
- at= 0;
- while ((rv = p11_kit_iter_next (iter)) == CKR_OK) {
- assert (at < 128);
- objects[at] = p11_kit_iter_get_object (iter);
-
- slot = p11_kit_iter_get_slot (iter);
- assert (slot == MOCK_SLOT_ONE_ID);
-
- module = p11_kit_iter_get_module (iter);
- assert_ptr_eq (module, &mock_module);
-
- assert (session == p11_kit_iter_get_session (iter));
- at++;
- }
-
- assert (rv == CKR_CANCEL);
-
- /* 1 modules, each with 1 slot, and 3 public objects */
- assert_num_eq (3, at);
-
- 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);
- assert (rv == CKR_OK);
-
- rv = mock_module.C_Finalize (NULL);
- assert (rv == CKR_OK);
-}
-
-static void
-test_with_slot (void)
-{
- CK_OBJECT_HANDLE objects[128];
- CK_FUNCTION_LIST_PTR module;
- CK_SLOT_ID slot;
- P11KitIter *iter;
- CK_RV rv;
- int at;
-
- mock_module_reset ();
- rv = mock_module.C_Initialize (NULL);
- assert (rv == CKR_OK);
-
- iter = p11_kit_iter_new (NULL, 0);
- p11_kit_iter_begin_with (iter, &mock_module, MOCK_SLOT_ONE_ID, 0);
-
- at= 0;
- while ((rv = p11_kit_iter_next (iter)) == CKR_OK) {
- assert (at < 128);
- objects[at] = p11_kit_iter_get_object (iter);
-
- slot = p11_kit_iter_get_slot (iter);
- assert (slot == MOCK_SLOT_ONE_ID);
-
- module = p11_kit_iter_get_module (iter);
- assert_ptr_eq (module, &mock_module);
- at++;
- }
-
- assert (rv == CKR_CANCEL);
-
- /* 1 modules, each with 1 slot, and 3 public objects */
- assert_num_eq (3, at);
-
- 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 = (mock_module.C_Finalize) (NULL);
- assert (rv == CKR_OK);
-}
-
-static void
-test_with_module (void)
-{
- CK_OBJECT_HANDLE objects[128];
- CK_FUNCTION_LIST_PTR module;
- P11KitIter *iter;
- CK_RV rv;
- int at;
-
- mock_module_reset ();
- rv = mock_module.C_Initialize (NULL);
- assert (rv == CKR_OK);
-
- iter = p11_kit_iter_new (NULL, 0);
- p11_kit_iter_begin_with (iter, &mock_module, 0, 0);
-
- at= 0;
- while ((rv = p11_kit_iter_next (iter)) == CKR_OK) {
- assert (at < 128);
- objects[at] = p11_kit_iter_get_object (iter);
-
- module = p11_kit_iter_get_module (iter);
- assert_ptr_eq (module, &mock_module);
- at++;
- }
-
- assert (rv == CKR_CANCEL);
-
- /* 1 modules, each with 1 slot, and 3 public objects */
- assert_num_eq (3, at);
-
- 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 = mock_module.C_Finalize (NULL);
- assert (rv == CKR_OK);
-}
-
-static void
-test_keep_session (void)
-{
- CK_SESSION_HANDLE session;
- P11KitIter *iter;
- CK_RV rv;
-
- mock_module_reset ();
- rv = mock_module.C_Initialize (NULL);
- assert (rv == CKR_OK);
-
- iter = p11_kit_iter_new (NULL, 0);
- p11_kit_iter_begin_with (iter, &mock_module, 0, 0);
-
- rv = p11_kit_iter_next (iter);
- 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);
- assert (rv == CKR_OK);
-
- rv = mock_module.C_Finalize (NULL);
- assert (rv == CKR_OK);
-}
-
-static void
-test_unrecognized (void)
-{
- CK_FUNCTION_LIST_PTR *modules;
- P11KitIter *iter;
- P11KitUri *uri;
- CK_RV rv;
- int count;
-
- modules = initialize_and_get_modules ();
-
- uri = p11_kit_uri_new ();
- p11_kit_uri_set_unrecognized (uri, 1);
- iter = p11_kit_iter_new (uri, 0);
- p11_kit_uri_free (uri);
-
- p11_kit_iter_begin (iter, modules);
-
- count = 0;
- while ((rv = p11_kit_iter_next (iter)) == CKR_OK)
- count++;
-
- assert (rv == CKR_CANCEL);
-
- /* Nothing should have matched */
- assert_num_eq (0, count);
-
- p11_kit_iter_free (iter);
-
- finalize_and_free_modules (modules);
-}
-
-static void
-test_uri_with_type (void)
-{
- CK_OBJECT_HANDLE objects[128];
- CK_FUNCTION_LIST_PTR *modules;
- P11KitIter *iter;
- P11KitUri *uri;
- CK_RV rv;
- int at;
- int ret;
-
- 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);
- assert_num_eq (ret, P11_KIT_URI_OK);
-
- iter = p11_kit_iter_new (uri, 0);
- p11_kit_uri_free (uri);
-
- p11_kit_iter_begin (iter, modules);
-
- at = 0;
- while ((rv = p11_kit_iter_next (iter)) == CKR_OK) {
- assert (at < 128);
- objects[at] = p11_kit_iter_get_object (iter);
- at++;
- }
-
- assert (rv == CKR_CANCEL);
-
- /* Three modules, each with 1 slot, and 2 public keys */
- assert_num_eq (6, at);
-
- 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 (modules);
-}
-
-static void
-test_set_uri (void)
-{
- CK_FUNCTION_LIST_PTR *modules;
- P11KitIter *iter;
- P11KitUri *uri;
- CK_RV rv;
-
- modules = initialize_and_get_modules ();
-
- uri = p11_kit_uri_new ();
- p11_kit_uri_set_unrecognized (uri, 1);
- iter = p11_kit_iter_new (NULL, 0);
- p11_kit_iter_set_uri (iter, uri);
- p11_kit_uri_free (uri);
-
- p11_kit_iter_begin (iter, modules);
-
- /* Nothing should have matched */
- rv = p11_kit_iter_next (iter);
- assert_num_eq (rv, CKR_CANCEL);
-
- p11_kit_iter_free (iter);
-
- finalize_and_free_modules (modules);
-}
-
-static void
-test_filter (void)
-{
- CK_OBJECT_HANDLE objects[128];
- CK_FUNCTION_LIST_PTR *modules;
- P11KitIter *iter;
- CK_RV rv;
- int at;
-
- CK_BBOOL vfalse = CK_FALSE;
- CK_OBJECT_CLASS public_key = CKO_PUBLIC_KEY;
- CK_ATTRIBUTE attrs[] = {
- { CKA_PRIVATE, &vfalse, sizeof (vfalse) },
- { CKA_CLASS, &public_key, sizeof (public_key) },
- };
-
- modules = initialize_and_get_modules ();
-
- iter = p11_kit_iter_new (NULL, 0);
- p11_kit_iter_add_filter (iter, attrs, 2);
-
- p11_kit_iter_begin (iter, modules);
-
- at = 0;
- while ((rv = p11_kit_iter_next (iter)) == CKR_OK) {
- assert (at < 128);
- objects[at] = p11_kit_iter_get_object (iter);
- at++;
- }
-
- assert (rv == CKR_CANCEL);
-
- /* Three modules, each with 1 slot, and 2 public keys */
- assert_num_eq (6, at);
-
- 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 (modules);
-}
-
-static void
-test_session_flags (void)
-{
- CK_FUNCTION_LIST_PTR *modules;
- CK_FUNCTION_LIST_PTR module;
- CK_SESSION_HANDLE session;
- CK_SESSION_INFO info;
- P11KitIter *iter;
- CK_RV rv;
-
- modules = initialize_and_get_modules ();
-
- iter = p11_kit_iter_new (NULL, P11_KIT_ITER_WANT_WRITABLE);
- p11_kit_iter_begin (iter, modules);
-
- while ((rv = p11_kit_iter_next (iter)) == CKR_OK) {
- module = p11_kit_iter_get_module (iter);
- assert_ptr_not_null (module);
-
- session = p11_kit_iter_get_session (iter);
- assert (session != 0);
-
- rv = (module->C_GetSessionInfo) (session, &info);
- assert (rv == CKR_OK);
-
- assert_num_eq (CKS_RW_PUBLIC_SESSION, info.state);
- }
-
- assert (rv == CKR_CANCEL);
-
- p11_kit_iter_free (iter);
-
- finalize_and_free_modules (modules);
-}
-
-static void
-test_module_match (void)
-{
- CK_FUNCTION_LIST_PTR *modules;
- P11KitIter *iter;
- P11KitUri *uri;
- CK_RV rv;
- int count;
- int ret;
-
- 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);
- assert_num_eq (P11_KIT_URI_OK, ret);
-
- iter = p11_kit_iter_new (uri, 0);
- p11_kit_uri_free (uri);
-
- p11_kit_iter_begin (iter, modules);
-
- count = 0;
- while ((rv = p11_kit_iter_next (iter)) == CKR_OK)
- count++;
-
- assert (rv == CKR_CANCEL);
-
- /* Three modules, each with 1 slot, and 3 public objects */
- assert_num_eq (9, count);
-
- p11_kit_iter_free (iter);
-
- finalize_and_free_modules (modules);
-}
-
-static void
-test_module_mismatch (void)
-{
- CK_FUNCTION_LIST_PTR *modules;
- P11KitIter *iter;
- P11KitUri *uri;
- CK_RV rv;
- int count;
- int ret;
-
- 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);
- assert_num_eq (P11_KIT_URI_OK, ret);
-
- iter = p11_kit_iter_new (uri, 0);
- p11_kit_uri_free (uri);
-
- p11_kit_iter_begin (iter, modules);
-
- count = 0;
- while ((rv = p11_kit_iter_next (iter)) == CKR_OK)
- count++;
-
- assert (rv == CKR_CANCEL);
-
- /* Nothing should have matched */
- assert_num_eq (0, count);
-
- p11_kit_iter_free (iter);
-
- finalize_and_free_modules (modules);
-}
-
-static void
-test_token_match (void)
-{
- CK_FUNCTION_LIST_PTR *modules;
- P11KitIter *iter;
- P11KitUri *uri;
- CK_RV rv;
- int count;
- int ret;
-
- 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);
- assert_num_eq (P11_KIT_URI_OK, ret);
-
- iter = p11_kit_iter_new (uri, 0);
- p11_kit_uri_free (uri);
-
- p11_kit_iter_begin (iter, modules);
-
- count = 0;
- while ((rv = p11_kit_iter_next (iter)) == CKR_OK)
- count++;
-
- assert (rv == CKR_CANCEL);
-
- /* Three modules, each with 1 slot, and 3 public objects */
- assert_num_eq (9, count);
-
- p11_kit_iter_free (iter);
-
- finalize_and_free_modules (modules);
-}
-
-static void
-test_token_mismatch (void)
-{
- CK_FUNCTION_LIST_PTR *modules;
- P11KitIter *iter;
- P11KitUri *uri;
- CK_RV rv;
- int count;
- int ret;
-
- modules = initialize_and_get_modules ();
-
- uri = p11_kit_uri_new ();
- ret = p11_kit_uri_parse ("pkcs11:manufacturer=blah", P11_KIT_URI_FOR_TOKEN, uri);
- assert_num_eq (P11_KIT_URI_OK, ret);
-
- iter = p11_kit_iter_new (uri, 0);
- p11_kit_uri_free (uri);
-
- p11_kit_iter_begin (iter, modules);
-
- count = 0;
- while ((rv = p11_kit_iter_next (iter)) == CKR_OK)
- count++;
-
- assert (rv == CKR_CANCEL);
-
- /* Nothing should have matched */
- assert_num_eq (0, count);
-
- p11_kit_iter_free (iter);
-
- finalize_and_free_modules (modules);
-}
-
-static void
-test_token_info (void)
-{
- CK_FUNCTION_LIST_PTR *modules;
- CK_TOKEN_INFO *info;
- P11KitIter *iter;
- char *string;
- CK_RV rv;
-
- modules = initialize_and_get_modules ();
-
- iter = p11_kit_iter_new (NULL, 0);
- p11_kit_iter_begin (iter, modules);
-
- rv = p11_kit_iter_next (iter);
- assert_num_eq (rv, CKR_OK);
-
- info = p11_kit_iter_get_token (iter);
- assert_ptr_not_null (info);
-
- string = p11_kit_space_strdup (info->label, sizeof (info->label));
- assert_ptr_not_null (string);
-
- assert_str_eq (string, "TEST LABEL");
-
- free (string);
- p11_kit_iter_free (iter);
-
- finalize_and_free_modules (modules);
-}
-
-static void
-test_getslotlist_fail_first (void)
-{
- CK_FUNCTION_LIST module;
- P11KitIter *iter;
- CK_RV rv;
- int at;
-
- 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;
-
- iter = p11_kit_iter_new (NULL, 0);
- p11_kit_iter_begin_with (iter, &module, 0, 0);
-
- at= 0;
- while ((rv = p11_kit_iter_next (iter)) == CKR_OK)
- at++;
-
- assert (rv == CKR_VENDOR_DEFINED);
-
- /* Should fail on the first iteration */
- assert_num_eq (0, at);
-
- p11_kit_iter_free (iter);
-
- rv = mock_module.C_Finalize (NULL);
- assert (rv == CKR_OK);
-}
-
-static void
-test_getslotlist_fail_late (void)
-{
- CK_FUNCTION_LIST module;
- P11KitIter *iter;
- CK_RV rv;
- int at;
-
- 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;
-
- iter = p11_kit_iter_new (NULL, 0);
- p11_kit_iter_begin_with (iter, &module, 0, 0);
-
- at= 0;
- while ((rv = p11_kit_iter_next (iter)) == CKR_OK)
- at++;
-
- assert (rv == CKR_VENDOR_DEFINED);
-
- /* Should fail on the first iteration */
- assert_num_eq (0, at);
-
- p11_kit_iter_free (iter);
-
- rv = mock_module.C_Finalize (NULL);
- assert (rv == CKR_OK);
-}
-
-static void
-test_open_session_fail (void)
-{
- CK_FUNCTION_LIST module;
- P11KitIter *iter;
- CK_RV rv;
- int at;
-
- 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;
-
- iter = p11_kit_iter_new (NULL, 0);
- p11_kit_iter_begin_with (iter, &module, 0, 0);
-
- at= 0;
- while ((rv = p11_kit_iter_next (iter)) == CKR_OK)
- at++;
-
- assert (rv == CKR_DEVICE_ERROR);
-
- /* Should fail on the first iteration */
- assert_num_eq (0, at);
-
- p11_kit_iter_free (iter);
-
- rv = mock_module.C_Finalize (NULL);
- assert (rv == CKR_OK);
-}
-
-static void
-test_find_init_fail (void)
-{
- CK_FUNCTION_LIST module;
- P11KitIter *iter;
- CK_RV rv;
- int at;
-
- 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;
-
- iter = p11_kit_iter_new (NULL, 0);
- p11_kit_iter_begin_with (iter, &module, 0, 0);
-
- at= 0;
- while ((rv = p11_kit_iter_next (iter)) == CKR_OK)
- at++;
-
- assert (rv == CKR_DEVICE_MEMORY);
-
- /* Should fail on the first iteration */
- assert_num_eq (0, at);
-
- p11_kit_iter_free (iter);
-
- rv = mock_module.C_Finalize (NULL);
- assert (rv == CKR_OK);
-}
-
-static void
-test_find_objects_fail (void)
-{
- CK_FUNCTION_LIST module;
- P11KitIter *iter;
- CK_RV rv;
- int at;
-
- 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;
-
- iter = p11_kit_iter_new (NULL, 0);
- p11_kit_iter_begin_with (iter, &module, 0, 0);
-
- at= 0;
- while ((rv = p11_kit_iter_next (iter)) == CKR_OK)
- at++;
-
- assert (rv == CKR_DEVICE_REMOVED);
-
- /* Should fail on the first iteration */
- assert_num_eq (0, at);
-
- p11_kit_iter_free (iter);
-
- rv = mock_module.C_Finalize (NULL);
- assert (rv == CKR_OK);
-}
-
-static void
-test_get_attributes (void)
-{
- CK_FUNCTION_LIST_PTR *modules;
- P11KitIter *iter;
- CK_OBJECT_HANDLE object;
- char label[128];
- CK_ULONG klass;
- CK_ULONG ulong;
- CK_RV rv;
- int at;
-
- CK_ATTRIBUTE template[] = {
- { CKA_CLASS, &klass, sizeof (klass) },
- { CKA_LABEL, label, sizeof (label) },
- { CKA_INVALID },
- };
-
- CK_ATTRIBUTE attrs[3];
-
- modules = initialize_and_get_modules ();
-
- iter = p11_kit_iter_new (NULL, 0);
- p11_kit_iter_begin (iter, modules);
-
- at = 0;
- while ((rv = p11_kit_iter_next (iter)) == CKR_OK) {
- assert (sizeof (attrs) == sizeof (template));
- memcpy (&attrs, &template, sizeof (attrs));
-
- rv = p11_kit_iter_get_attributes (iter, attrs, 2);
- assert (rv == CKR_OK);
-
- object = p11_kit_iter_get_object (iter);
- switch (object) {
- case MOCK_DATA_OBJECT:
- 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:
- 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:
- 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:
- assert_fail ("Unknown object matched", NULL);
- break;
- }
-
- at++;
- }
-
- assert (rv == CKR_CANCEL);
-
- /* Three modules, each with 1 slot, and 3 public objects */
- assert_num_eq (9, at);
-
- p11_kit_iter_free (iter);
-
- finalize_and_free_modules (modules);
-}
-
-
-
-static void
-test_load_attributes (void)
-{
- CK_FUNCTION_LIST_PTR *modules;
- P11KitIter *iter;
- CK_ATTRIBUTE *attrs;
- CK_OBJECT_HANDLE object;
- CK_ULONG ulong;
- CK_RV rv;
- int at;
-
- CK_ATTRIBUTE types[] = {
- { CKA_CLASS },
- { CKA_LABEL },
- };
-
- modules = initialize_and_get_modules ();
-
- iter = p11_kit_iter_new (NULL, 0);
- p11_kit_iter_begin (iter, modules);
-
- attrs = p11_attrs_buildn (NULL, types, 2);
-
- at = 0;
- while ((rv = p11_kit_iter_next (iter)) == CKR_OK) {
- rv = p11_kit_iter_load_attributes (iter, attrs, 2);
- assert (rv == CKR_OK);
-
- object = p11_kit_iter_get_object (iter);
- switch (object) {
- case MOCK_DATA_OBJECT:
- 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:
- 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:
- 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:
- assert_fail ("Unknown object matched", NULL);
- break;
- }
-
- at++;
- }
-
- p11_attrs_free (attrs);
-
- assert (rv == CKR_CANCEL);
-
- /* Three modules, each with 1 slot, and 3 public objects */
- assert_num_eq (9, at);
-
- p11_kit_iter_free (iter);
-
- finalize_and_free_modules (modules);
-}
-
-static void
-test_load_attributes_none (void)
-{
- CK_FUNCTION_LIST module;
- P11KitIter *iter;
- CK_ATTRIBUTE *attrs;
- CK_RV rv;
-
- mock_module_reset ();
- rv = mock_module.C_Initialize (NULL);
- assert (rv == CKR_OK);
-
- memcpy (&module, &mock_module, sizeof (CK_FUNCTION_LIST));
-
- iter = p11_kit_iter_new (NULL, 0);
- p11_kit_iter_begin_with (iter, &module, 0, 0);
-
- 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);
- assert (rv == CKR_OK);
- p11_attrs_free (attrs);
- }
-
- assert (rv == CKR_CANCEL);
-
- p11_kit_iter_free (iter);
-
- rv = mock_module.C_Finalize (NULL);
- assert (rv == CKR_OK);
-}
-
-static void
-test_load_attributes_fail_first (void)
-{
- CK_ATTRIBUTE label = { CKA_LABEL, };
- CK_FUNCTION_LIST module;
- P11KitIter *iter;
- CK_ATTRIBUTE *attrs;
- CK_RV rv;
-
- 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;
-
- iter = p11_kit_iter_new (NULL, 0);
- p11_kit_iter_begin_with (iter, &module, 0, 0);
-
- 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);
- assert (rv == CKR_FUNCTION_REJECTED);
- p11_attrs_free (attrs);
- }
-
- assert (rv == CKR_CANCEL);
-
- p11_kit_iter_free (iter);
-
- rv = mock_module.C_Finalize (NULL);
- assert (rv == CKR_OK);
-}
-
-static void
-test_load_attributes_fail_late (void)
-{
- CK_ATTRIBUTE label = { CKA_LABEL, };
- CK_FUNCTION_LIST module;
- P11KitIter *iter;
- CK_ATTRIBUTE *attrs;
- CK_RV rv;
-
- 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;
-
- iter = p11_kit_iter_new (NULL, 0);
- p11_kit_iter_begin_with (iter, &module, 0, 0);
-
- 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);
- assert (rv == CKR_FUNCTION_FAILED);
- p11_attrs_free (attrs);
- }
-
- assert (rv == CKR_CANCEL);
-
- p11_kit_iter_free (iter);
-
- rv = mock_module.C_Finalize (NULL);
- assert (rv == CKR_OK);
-}
-
-static void
-test_many (void *flags)
-{
- P11KitIterBehavior behavior;
- CK_SESSION_HANDLE session;
- CK_OBJECT_HANDLE handle;
- p11_dict *seen;
- P11KitIter *iter;
- CK_RV rv;
- int count;
- int i;
-
- static CK_OBJECT_CLASS data = CKO_DATA;
- static CK_ATTRIBUTE object[] = {
- { CKA_VALUE, "blah", 4 },
- { CKA_CLASS, &data, sizeof (data) },
- { CKA_ID, "ID1", 3 },
- { CKA_INVALID },
- };
-
- behavior = 0;
- if (strstr (flags, "busy-sessions"))
- behavior |= P11_KIT_ITER_BUSY_SESSIONS;
-
- mock_module_reset ();
- rv = mock_module.C_Initialize (NULL);
- assert_num_eq (rv, CKR_OK);
-
- rv = mock_C_OpenSession (MOCK_SLOT_ONE_ID, CKF_SERIAL_SESSION, NULL, NULL, &session);
- assert_num_eq (rv, CKR_OK);
-
- for (i = 0; i < 10000; i++)
- mock_module_add_object (MOCK_SLOT_ONE_ID, object);
-
- seen = p11_dict_new (p11_dict_ulongptr_hash, p11_dict_ulongptr_equal, free, NULL);
- iter = p11_kit_iter_new (NULL, behavior);
- p11_kit_iter_add_filter (iter, object, 3);
- p11_kit_iter_begin_with (iter, &mock_module, 0, session);
-
- count = 0;
- while ((rv = p11_kit_iter_next (iter)) == CKR_OK) {
- handle = p11_kit_iter_get_object (iter);
- assert (p11_dict_get (seen, &handle) == NULL);
- if (!p11_dict_set (seen, memdup (&handle, sizeof (handle)), "x"))
- assert_not_reached ();
- count++;
- }
-
- assert_num_eq (rv, CKR_CANCEL);
- assert_num_eq (count, 10000);
-
- p11_kit_iter_free (iter);
- p11_dict_free (seen);
-
- rv = mock_module.C_Finalize (NULL);
- assert (rv == CKR_OK);
-}
-
-static void
-test_destroy_object (void)
-{
- CK_FUNCTION_LIST **modules;
- P11KitIter *iter;
- CK_OBJECT_HANDLE object;
- CK_SESSION_HANDLE session;
- CK_FUNCTION_LIST *module;
- CK_ULONG size;
- CK_RV rv;
-
- modules = initialize_and_get_modules ();
-
- iter = p11_kit_iter_new (NULL, P11_KIT_ITER_WANT_WRITABLE);
-
- p11_kit_iter_begin (iter, modules);
-
- /* Should have matched */
- rv = p11_kit_iter_next (iter);
- assert_num_eq (rv, CKR_OK);
-
- object = p11_kit_iter_get_object (iter);
- session = p11_kit_iter_get_session (iter);
- module = p11_kit_iter_get_module (iter);
-
- rv = (module->C_GetObjectSize) (session, object, &size);
- assert_num_eq (rv, CKR_OK);
-
- rv = p11_kit_iter_destroy_object (iter);
- assert_num_eq (rv, CKR_OK);
-
- rv = (module->C_GetObjectSize) (session, object, &size);
- assert_num_eq (rv, CKR_OBJECT_HANDLE_INVALID);
-
- p11_kit_iter_free (iter);
-
- finalize_and_free_modules (modules);
-}
-
-int
-main (int argc,
- char *argv[])
-{
- p11_library_init ();
- mock_module_init ();
-
- 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_set_uri, "/iter/set-uri");
- 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_token_info, "/iter/token-info");
- 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_get_attributes, "/iter/get-attributes");
- 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");
- p11_testx (test_many, "", "/iter/test-many");
- p11_testx (test_many, "busy-sessions", "/iter/test-many-busy");
- p11_test (test_destroy_object, "/iter/destroy-object");
-
- return p11_test_run (argc, argv);
-}
diff --git a/p11-kit/tests/test-log.c b/p11-kit/tests/test-log.c
deleted file mode 100644
index e7dab70..0000000
--- a/p11-kit/tests/test-log.c
+++ /dev/null
@@ -1,112 +0,0 @@
-/*
- * 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
deleted file mode 100644
index c4ccd9a..0000000
--- a/p11-kit/tests/test-managed.c
+++ /dev/null
@@ -1,262 +0,0 @@
-/*
- * 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 <sys/types.h>
-#include <sys/wait.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);
-}
-
-static void
-test_fork_and_reinitialize (void)
-{
- CK_FUNCTION_LIST *module;
- CK_INFO info;
- int status;
- CK_RV rv;
- pid_t pid;
- int i;
-
- module = setup_mock_module (NULL);
- assert_ptr_not_null (module);
-
- pid = fork ();
- assert_num_cmp (pid, >=, 0);
-
- /* The child */
- if (pid == 0) {
- rv = (module->C_Initialize) (NULL);
- assert_num_eq (CKR_OK, rv);
-
- for (i = 0; i < 32; i++) {
- rv = (module->C_GetInfo) (&info);
- assert_num_eq (CKR_OK, rv);
- }
-
- rv = (module->C_Finalize) (NULL);
- assert_num_eq (CKR_OK, rv);
-
- _exit (66);
- }
-
- for (i = 0; i < 128; i++) {
- rv = (module->C_GetInfo) (&info);
- assert_num_eq (CKR_OK, rv);
- }
-
- assert_num_eq (waitpid (pid, &status, 0), pid);
- assert_num_eq (WEXITSTATUS (status), 66);
-
- teardown_mock_module (module);
-}
-
-/* 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");
- p11_test (test_fork_and_reinitialize, "/managed/fork-and-reinitialize");
-
- 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
deleted file mode 100644
index 8454f1f..0000000
--- a/p11-kit/tests/test-mock.c
+++ /dev/null
@@ -1,1685 +0,0 @@
-/*
- * 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 <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_num_eq (rv, CKR_OK);
- assert_num_eq (MOCK_INFO.cryptokiVersion.major, info.cryptokiVersion.major);
- assert_num_eq (MOCK_INFO.cryptokiVersion.minor, info.cryptokiVersion.minor);
- assert (memcmp (MOCK_INFO.manufacturerID, info.manufacturerID, sizeof (info.manufacturerID)) == 0);
- assert_num_eq (MOCK_INFO.flags, info.flags);
- assert (memcmp (MOCK_INFO.libraryDescription, info.libraryDescription, sizeof (info.libraryDescription)) == 0);
- assert_num_eq (MOCK_INFO.libraryVersion.major, info.libraryVersion.major);
- assert_num_eq (MOCK_INFO.libraryVersion.minor, info.libraryVersion.minor);
-
- 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_num_eq (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_fixture (NULL, NULL);
- 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
deleted file mode 100644
index f274502..0000000
--- a/p11-kit/tests/test-modules.c
+++ /dev/null
@@ -1,415 +0,0 @@
-/*
- * 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 <errno.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-
-#include "debug.h"
-#include "library.h"
-#include "p11-kit.h"
-#include "private.h"
-#include "dict.h"
-
-static CK_FUNCTION_LIST_PTR_PTR
-initialize_and_get_modules (void)
-{
- CK_FUNCTION_LIST_PTR_PTR modules;
-
- modules = p11_kit_modules_load_and_initialize (0);
- assert (modules != NULL && modules[0] != NULL);
-
- return modules;
-}
-
-static void
-finalize_and_free_modules (CK_FUNCTION_LIST_PTR_PTR modules)
-{
- p11_kit_modules_finalize_and_release (modules);
-}
-
-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_config_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_module_get_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_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 (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;
-
- /*
- * The module two shouldn't be matched, because in its config
- * file we have:
- *
- * disable-in: test-disable
- */
-
- p11_kit_set_progname ("test-disable");
-
- modules = p11_kit_modules_load_and_initialize (0);
- 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);
-}
-
-static void
-test_priority (void)
-{
- CK_FUNCTION_LIST_PTR_PTR modules;
- char *name;
- int i;
-
- /*
- * The expected order.
- * - four is marked with a priority of 4, the highest therefore first
- * - three is marked with a priority of 3, next highest
- * - one and two do not have priority marked, so they default to zero
- * and fallback to sorting alphabetically. 'o' comes before 't'
- */
-
- const char *expected[] = { "four", "three", "one", "two.badname" };
-
- /* This enables module three */
- p11_kit_set_progname ("test-enable");
-
- modules = initialize_and_get_modules ();
-
- /* The loaded modules should not contain duplicates */
- for (i = 0; modules[i] != NULL; i++) {
- 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) {
- free (name);
- name = strdup ("two.badname");
- }
-
- assert_str_eq (expected[i], name);
- free (name);
- }
-
- assert_num_eq (4, i);
- finalize_and_free_modules (modules);
-}
-
-static void
-test_module_name (void)
-{
- 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 ();
-
- 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_module_trusted_only (void)
-{
- CK_FUNCTION_LIST_PTR_PTR modules;
- char *name;
-
- modules = p11_kit_modules_load_and_initialize (P11_KIT_MODULE_TRUSTED);
- assert_ptr_not_null (modules);
- assert_ptr_not_null (modules[0]);
- assert (modules[1] == NULL);
-
- name = p11_kit_module_get_name (modules[0]);
- assert_str_eq (name, "one");
- free (name);
-
- assert_num_eq (p11_kit_module_get_flags (modules[0]), P11_KIT_MODULE_TRUSTED);
-
- finalize_and_free_modules (modules);
-}
-
-static void
-test_module_trust_flags (void)
-{
- CK_FUNCTION_LIST_PTR_PTR modules;
- char *name;
- int flags;
- int i;
-
- modules = initialize_and_get_modules ();
- assert_ptr_not_null (modules);
-
- for (i = 0; modules[i] != NULL; i++) {
- name = p11_kit_module_get_name (modules[i]);
- assert_ptr_not_null (name);
-
- flags = p11_kit_module_get_flags (modules[i]);
- if (strcmp (name, "one") == 0) {
- assert_num_eq (flags, P11_KIT_MODULE_TRUSTED);
- } else {
- assert_num_eq (flags, 0);
- }
-
- free (name);
- }
-
- finalize_and_free_modules (modules);
-}
-
-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 ();
-
- 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_test (test_module_trusted_only, "/modules/trusted-only");
- p11_test (test_module_trust_flags, "/modules/trust-flags");
-
- p11_kit_be_quiet ();
-
- return p11_test_run (argc, argv);
-}
diff --git a/p11-kit/tests/test-pin.c b/p11-kit/tests/test-pin.c
deleted file mode 100644
index ebe3efc..0000000
--- a/p11-kit/tests/test-pin.c
+++ /dev/null
@@ -1,313 +0,0 @@
-/*
- * Copyright (c) 2011, Collabora Ltd.
- *
- * 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@collabora.co.uk>
- */
-
-#include "config.h"
-#include "test.h"
-
-#include "library.h"
-
-#include <assert.h>
-#include <errno.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-
-#include "p11-kit/pin.h"
-#include "p11-kit/private.h"
-
-static P11KitPin *
-callback_one (const char *pin_source, P11KitUri *pin_uri, const char *pin_description,
- P11KitPinFlags pin_flags, void *callback_data)
-{
- int *data = callback_data;
- assert (*data == 33);
- return p11_kit_pin_new_for_buffer ((unsigned char*)strdup ("one"), 3, free);
-}
-
-static P11KitPin*
-callback_other (const char *pin_source, P11KitUri *pin_uri, const char *pin_description,
- P11KitPinFlags pin_flags, void *callback_data)
-{
- char *data = callback_data;
- return p11_kit_pin_new_for_string (data);
-}
-
-static void
-destroy_data (void *callback_data)
-{
- int *data = callback_data;
- (*data)++;
-}
-
-static void
-test_pin_register_unregister (void)
-{
- int data = 33;
-
- p11_kit_pin_register_callback ("/the/pin_source", callback_one,
- &data, destroy_data);
-
- p11_kit_pin_unregister_callback ("/the/pin_source", callback_one,
- &data);
-
- assert_num_eq (34, data);
-}
-
-static void
-test_pin_read (void)
-{
- P11KitUri *uri;
- P11KitPin *pin;
- int data = 33;
- size_t length;
- const unsigned char *ptr;
-
- p11_kit_pin_register_callback ("/the/pin_source", callback_one,
- &data, destroy_data);
-
- uri = p11_kit_uri_new ();
- pin = p11_kit_pin_request ("/the/pin_source", uri, "The token",
- P11_KIT_PIN_FLAGS_USER_LOGIN);
- p11_kit_uri_free (uri);
-
- assert_ptr_not_null (pin);
- ptr = p11_kit_pin_get_value (pin, &length);
- assert_num_eq (3, length);
- assert (memcmp (ptr, "one", 3) == 0);
-
- p11_kit_pin_unregister_callback ("/the/pin_source", callback_one,
- &data);
-
- p11_kit_pin_unref (pin);
-}
-
-static void
-test_pin_read_no_match (void)
-{
- P11KitUri *uri;
- P11KitPin *pin;
-
- uri = p11_kit_uri_new ();
- pin = p11_kit_pin_request ("/the/pin_source", uri, "The token",
- P11_KIT_PIN_FLAGS_USER_LOGIN);
- p11_kit_uri_free (uri);
-
- assert_ptr_eq (NULL, pin);
-}
-
-static void
-test_pin_register_duplicate (void)
-{
- P11KitUri *uri;
- P11KitPin *pin;
- char *value = "secret";
- int data = 33;
- size_t length;
- const unsigned char *ptr;
-
- uri = p11_kit_uri_new ();
-
- p11_kit_pin_register_callback ("/the/pin_source", callback_one,
- &data, destroy_data);
-
- p11_kit_pin_register_callback ("/the/pin_source", callback_other,
- value, NULL);
-
- pin = p11_kit_pin_request ("/the/pin_source", uri, "The token",
- P11_KIT_PIN_FLAGS_USER_LOGIN);
-
- assert_ptr_not_null (pin);
- ptr = p11_kit_pin_get_value (pin, &length);
- 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,
- value);
-
- pin = p11_kit_pin_request ("/the/pin_source", uri, "The token",
- P11_KIT_PIN_FLAGS_USER_LOGIN);
-
- assert_ptr_not_null (pin);
- ptr = p11_kit_pin_get_value (pin, &length);
- 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,
- &data);
-
- pin = p11_kit_pin_request ("/the/pin_source", uri, "The token",
- P11_KIT_PIN_FLAGS_USER_LOGIN);
-
- assert_ptr_eq (NULL, pin);
-
- p11_kit_uri_free (uri);
-}
-
-static void
-test_pin_register_fallback (void)
-{
- char *value = "secret";
- P11KitUri *uri;
- P11KitPin *pin;
- int data = 33;
- size_t length;
- const unsigned char *ptr;
-
- uri = p11_kit_uri_new ();
-
- p11_kit_pin_register_callback (P11_KIT_PIN_FALLBACK, callback_one,
- &data, destroy_data);
-
- pin = p11_kit_pin_request ("/the/pin_source", uri, "The token",
- P11_KIT_PIN_FLAGS_USER_LOGIN);
-
- assert_ptr_not_null (pin);
- ptr = p11_kit_pin_get_value (pin, &length);
- 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,
- value, NULL);
-
- pin = p11_kit_pin_request ("/the/pin_source", uri, "The token",
- P11_KIT_PIN_FLAGS_USER_LOGIN);
-
- assert_ptr_not_null (pin);
- ptr = p11_kit_pin_get_value (pin, &length);
- 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,
- value);
-
- p11_kit_pin_unregister_callback (P11_KIT_PIN_FALLBACK, callback_one,
- &data);
-
- p11_kit_uri_free (uri);
-}
-
-static void
-test_pin_file (void)
-{
- P11KitUri *uri;
- P11KitPin *pin;
- size_t length;
- const unsigned char *ptr;
-
- uri = p11_kit_uri_new ();
-
- p11_kit_pin_register_callback (P11_KIT_PIN_FALLBACK, p11_kit_pin_file_callback,
- NULL, NULL);
-
- pin = p11_kit_pin_request (SRCDIR "/files/test-pinfile", uri, "The token",
- P11_KIT_PIN_FLAGS_USER_LOGIN);
-
- assert_ptr_not_null (pin);
- ptr = p11_kit_pin_get_value (pin, &length);
- 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);
-
- assert_ptr_eq (NULL, pin);
-
- p11_kit_pin_unregister_callback (P11_KIT_PIN_FALLBACK, p11_kit_pin_file_callback,
- NULL);
-
- p11_kit_uri_free (uri);
-}
-
-static void
-test_pin_file_large (void)
-{
- P11KitUri *uri;
- P11KitPin *pin;
- int error;
-
- uri = p11_kit_uri_new ();
-
- p11_kit_pin_register_callback (P11_KIT_PIN_FALLBACK, p11_kit_pin_file_callback,
- NULL, NULL);
-
- pin = p11_kit_pin_request (SRCDIR "/files/test-pinfile-large", uri, "The token",
- P11_KIT_PIN_FLAGS_USER_LOGIN);
-
- error = errno;
- 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);
-
- p11_kit_uri_free (uri);
-}
-
-static void
-test_pin_ref_unref (void)
-{
- P11KitPin *pin;
- P11KitPin *check;
-
- pin = p11_kit_pin_new_for_string ("crack of lies");
-
- check = p11_kit_pin_ref (pin);
- assert_ptr_eq (pin, check);
-
- p11_kit_pin_unref (pin);
- p11_kit_pin_unref (check);
-}
-
-int
-main (int argc,
- char *argv[])
-{
- p11_library_init ();
-
- 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/test-progname.c b/p11-kit/tests/test-progname.c
deleted file mode 100644
index 76b136d..0000000
--- a/p11-kit/tests/test-progname.c
+++ /dev/null
@@ -1,86 +0,0 @@
-/*
- * Copyright (c) 2012 Stefan Walter
- *
- * 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 "test.h"
-
-#include "library.h"
-
-#include <assert.h>
-#include <string.h>
-#include <stdio.h>
-#include <stdlib.h>
-
-#include "p11-kit/uri.h"
-#include "p11-kit/p11-kit.h"
-#include "p11-kit/private.h"
-
-static void
-test_progname_default (void)
-{
- const char *progname;
-
- progname = _p11_get_progname_unlocked ();
- assert_str_eq ("test-progname", progname);
-}
-
-static void
-test_progname_set (void)
-{
- const char *progname;
-
- p11_kit_set_progname ("love-generation");
-
- progname = _p11_get_progname_unlocked ();
- assert_str_eq ("love-generation", progname);
-
- _p11_set_progname_unlocked (NULL);
-
- progname = _p11_get_progname_unlocked ();
- assert_str_eq ("test-progname", progname);
-}
-
-/* Defined in util.c */
-extern char p11_my_progname[];
-
-int
-main (int argc,
- char *argv[])
-{
- p11_library_init ();
-
- 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
deleted file mode 100644
index bf5007d..0000000
--- a/p11-kit/tests/test-proxy.c
+++ /dev/null
@@ -1,195 +0,0 @@
-/*
- * 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/test-rpc.c b/p11-kit/tests/test-rpc.c
deleted file mode 100644
index 0ce2c55..0000000
--- a/p11-kit/tests/test-rpc.c
+++ /dev/null
@@ -1,1061 +0,0 @@
-/*
- * 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 "test.h"
-
-#include "debug.h"
-#include "library.h"
-#include "message.h"
-#include "mock.h"
-#include "p11-kit.h"
-#include "private.h"
-#include "rpc.h"
-#include "rpc-message.h"
-#include "virtual.h"
-
-#include <sys/types.h>
-#include <sys/wait.h>
-#include <assert.h>
-#include <string.h>
-#include <stdio.h>
-#include <stdlib.h>
-
-static void
-test_new_free (void)
-{
- p11_buffer *buf;
-
- buf = p11_rpc_buffer_new (0);
-
- assert_ptr_not_null (buf->data);
- assert_num_eq (0, buf->len);
- assert_num_eq (0, buf->flags);
- assert (buf->size == 0);
- assert_ptr_not_null (buf->ffree);
- assert_ptr_not_null (buf->frealloc);
-
- p11_rpc_buffer_free (buf);
-}
-
-static void
-test_uint16 (void)
-{
- p11_buffer buffer;
- uint16_t val = 0xFFFF;
- size_t next;
- bool ret;
-
- p11_buffer_init (&buffer, 0);
-
- next = 0;
- ret = p11_rpc_buffer_get_uint16 (&buffer, &next, &val);
- assert_num_eq (false, ret);
- assert_num_eq (0, next);
- assert_num_eq (0xFFFF, val);
-
- p11_buffer_reset (&buffer, 0);
-
- ret = p11_rpc_buffer_set_uint16 (&buffer, 0, 0x6789);
- assert_num_eq (false, ret);
-
- p11_buffer_reset (&buffer, 0);
-
- p11_buffer_add (&buffer, (unsigned char *)"padding", 7);
-
- p11_rpc_buffer_add_uint16 (&buffer, 0x6789);
- assert_num_eq (9, buffer.len);
- assert (!p11_buffer_failed (&buffer));
-
- next = 7;
- ret = p11_rpc_buffer_get_uint16 (&buffer, &next, &val);
- assert_num_eq (true, ret);
- assert_num_eq (9, next);
- assert_num_eq (0x6789, val);
-
- p11_buffer_uninit (&buffer);
-}
-
-static void
-test_uint16_static (void)
-{
- p11_buffer buf = { (unsigned char *)"pad0\x67\x89", 6, };
- uint16_t val = 0xFFFF;
- size_t next;
- bool ret;
-
- next = 4;
- ret = p11_rpc_buffer_get_uint16 (&buf, &next, &val);
- assert_num_eq (true, ret);
- assert_num_eq (6, next);
- assert_num_eq (0x6789, val);
-}
-
-static void
-test_uint32 (void)
-{
- p11_buffer buffer;
- uint32_t val = 0xFFFFFFFF;
- size_t next;
- bool ret;
-
- p11_buffer_init (&buffer, 0);
-
- next = 0;
- ret = p11_rpc_buffer_get_uint32 (&buffer, &next, &val);
- assert_num_eq (false, ret);
- assert_num_eq (0, next);
- assert_num_eq (0xFFFFFFFF, val);
-
- p11_buffer_reset (&buffer, 0);
-
- ret = p11_rpc_buffer_set_uint32 (&buffer, 0, 0x12345678);
- assert_num_eq (false, ret);
-
- p11_buffer_reset (&buffer, 0);
-
- p11_buffer_add (&buffer, (unsigned char *)"padding", 7);
-
- p11_rpc_buffer_add_uint32 (&buffer, 0x12345678);
- assert_num_eq (11, buffer.len);
- assert (!p11_buffer_failed (&buffer));
-
- next = 7;
- ret = p11_rpc_buffer_get_uint32 (&buffer, &next, &val);
- assert_num_eq (true, ret);
- assert_num_eq (11, next);
- assert_num_eq (0x12345678, val);
-
- p11_buffer_uninit (&buffer);
-}
-
-static void
-test_uint32_static (void)
-{
- p11_buffer buf = { (unsigned char *)"pad0\x23\x45\x67\x89", 8, };
- uint32_t val = 0xFFFFFFFF;
- size_t next;
- bool ret;
-
- next = 4;
- ret = p11_rpc_buffer_get_uint32 (&buf, &next, &val);
- assert_num_eq (true, ret);
- assert_num_eq (8, next);
- assert_num_eq (0x23456789, val);
-}
-
-static void
-test_uint64 (void)
-{
- p11_buffer buffer;
- uint64_t val = 0xFFFFFFFFFFFFFFFF;
- size_t next;
- bool ret;
-
- p11_buffer_init (&buffer, 0);
-
- next = 0;
- ret = p11_rpc_buffer_get_uint64 (&buffer, &next, &val);
- assert_num_eq (0, ret);
- assert_num_eq (0, next);
- assert (0xFFFFFFFFFFFFFFFF == val);
-
- p11_buffer_reset (&buffer, 0);
-
- p11_buffer_add (&buffer, (unsigned char *)"padding", 7);
-
- p11_rpc_buffer_add_uint64 (&buffer, 0x0123456708ABCDEF);
- assert_num_eq (15, buffer.len);
- assert (!p11_buffer_failed (&buffer));
-
- next = 7;
- ret = p11_rpc_buffer_get_uint64 (&buffer, &next, &val);
- assert_num_eq (true, ret);
- assert_num_eq (15, next);
- assert (0x0123456708ABCDEF == val);
-
- p11_buffer_uninit (&buffer);
-}
-
-static void
-test_uint64_static (void)
-{
- p11_buffer buf = { (unsigned char *)"pad0\x89\x67\x45\x23\x11\x22\x33\x44", 12, };
- uint64_t val = 0xFFFFFFFFFFFFFFFF;
- size_t next;
- bool ret;
-
- next = 4;
- ret = p11_rpc_buffer_get_uint64 (&buf, &next, &val);
- assert_num_eq (true, ret);
- assert_num_eq (12, next);
- assert (0x8967452311223344 == val);
-}
-
-static void
-test_byte_array (void)
-{
- p11_buffer buffer;
- unsigned char bytes[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
- 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
- 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
- 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F };
-
- const unsigned char *val;
- size_t length = ~0;
- size_t next;
- bool ret;
-
- p11_buffer_init (&buffer, 0);
-
- /* Invalid read */
-
- next = 0;
- ret = p11_rpc_buffer_get_byte_array (&buffer, &next, &val, &length);
- assert_num_eq (false, ret);
- assert_num_eq (0, next);
- assert_num_eq (~0, length);
-
- /* Test full array */
-
- p11_buffer_reset (&buffer, 0);
- p11_buffer_add (&buffer, (unsigned char *)"padding", 7);
-
- p11_rpc_buffer_add_byte_array (&buffer, bytes, 32);
- assert_num_eq (43, buffer.len);
- assert (!p11_buffer_failed (&buffer));
-
- next = 7;
- ret = p11_rpc_buffer_get_byte_array (&buffer, &next, &val, &length);
- assert_num_eq (true, ret);
- assert_num_eq (43, next);
- assert_num_eq (32, length);
- assert (memcmp (val, bytes, 32) == 0);
-
- p11_buffer_uninit (&buffer);
-}
-
-static void
-test_byte_array_null (void)
-{
- p11_buffer buffer;
- const unsigned char *val;
- size_t length = ~0;
- size_t next;
- bool ret;
-
- p11_buffer_init (&buffer, 0);
-
- p11_buffer_reset (&buffer, 0);
- p11_buffer_add (&buffer, (unsigned char *)"padding", 7);
-
- p11_rpc_buffer_add_byte_array (&buffer, NULL, 0);
- assert_num_eq (11, buffer.len);
- assert (!p11_buffer_failed (&buffer));
-
- next = 7;
- ret = p11_rpc_buffer_get_byte_array (&buffer, &next, &val, &length);
- assert_num_eq (true, ret);
- assert_num_eq (11, next);
- assert_num_eq (0, length);
- assert_ptr_eq (NULL, (void*)val);
-
- p11_buffer_uninit (&buffer);
-}
-
-static void
-test_byte_array_too_long (void)
-{
- p11_buffer buffer;
- const unsigned char *val = NULL;
- size_t length = ~0;
- size_t next;
- bool ret;
-
- p11_buffer_init (&buffer, 0);
-
- p11_buffer_reset (&buffer, 0);
- p11_buffer_add (&buffer, (unsigned char *)"padding", 7);
- assert (!p11_buffer_failed (&buffer));
-
- /* Passing a too short buffer here shouldn't matter, as length is checked for sanity */
- p11_rpc_buffer_add_byte_array (&buffer, (unsigned char *)"", 0x9fffffff);
- assert (p11_buffer_failed (&buffer));
-
- /* Force write a too long byte arary to buffer */
- p11_buffer_reset (&buffer, 0);
- p11_rpc_buffer_add_uint32 (&buffer, 0x9fffffff);
-
- next = 0;
- ret = p11_rpc_buffer_get_byte_array (&buffer, &next, &val, &length);
- assert_num_eq (false, ret);
- assert_num_eq (0, next);
- assert_num_eq (~0, length);
- assert_ptr_eq (NULL, (void*)val);
-
- p11_buffer_uninit (&buffer);
-}
-
-static void
-test_byte_array_static (void)
-{
- unsigned char data[] = { 'p', 'a', 'd', 0x00, 0x00, 0x00, 0x00, 0x20,
- 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
- 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
- 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
- 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F };
- p11_buffer buf = { data, 0x40, };
- const unsigned char *val;
- size_t length = ~0;
- size_t next;
- bool ret;
-
- next = 4;
- ret = p11_rpc_buffer_get_byte_array (&buf, &next, &val, &length);
- assert_num_eq (true, ret);
- assert_num_eq (40, next);
- assert_num_eq (32, length);
- assert (memcmp (data + 8, val, 32) == 0);
-}
-
-static p11_virtual base;
-static pid_t rpc_initialized = 0;
-
-static CK_RV
-rpc_initialize (p11_rpc_client_vtable *vtable,
- void *init_reserved)
-{
- pid_t pid = getpid ();
-
- assert_str_eq (vtable->data, "vtable-data");
- assert_num_cmp (pid, !=, rpc_initialized);
- rpc_initialized = pid;
-
- return CKR_OK;
-}
-
-static CK_RV
-rpc_initialize_fails (p11_rpc_client_vtable *vtable,
- void *init_reserved)
-{
- pid_t pid = getpid ();
-
- assert_str_eq (vtable->data, "vtable-data");
- assert_num_cmp (pid, !=, rpc_initialized);
- return CKR_FUNCTION_FAILED;
-}
-
-static CK_RV
-rpc_initialize_device_removed (p11_rpc_client_vtable *vtable,
- void *init_reserved)
-{
- pid_t pid = getpid ();
-
- assert_str_eq (vtable->data, "vtable-data");
- assert_num_cmp (pid, !=, rpc_initialized);
- return CKR_DEVICE_REMOVED;
-}
-
-static CK_RV
-rpc_transport (p11_rpc_client_vtable *vtable,
- p11_buffer *request,
- p11_buffer *response)
-{
- bool ret;
-
- assert_str_eq (vtable->data, "vtable-data");
-
- /* Just pass directly to the server code */
- ret = p11_rpc_server_handle (&base.funcs, request, response);
- assert (ret == true);
-
- return CKR_OK;
-}
-
-static void
-rpc_finalize (p11_rpc_client_vtable *vtable,
- void *fini_reserved)
-{
- pid_t pid = getpid ();
-
- assert_str_eq (vtable->data, "vtable-data");
- assert_num_cmp (pid, ==, rpc_initialized);
- rpc_initialized = 0;
-}
-
-static void
-test_initialize (void)
-{
- p11_rpc_client_vtable vtable = { "vtable-data", rpc_initialize, rpc_transport, rpc_finalize };
- pid_t pid = getpid ();
- p11_virtual mixin;
- bool ret;
- CK_RV rv;
-
- /* Build up our own function list */
- rpc_initialized = 0;
- p11_virtual_init (&base, &p11_virtual_base, &mock_module_no_slots, NULL);
-
- ret = p11_rpc_client_init (&mixin, &vtable);
- assert_num_eq (true, ret);
-
- rv = mixin.funcs.C_Initialize (&mixin.funcs, NULL);
- assert (rv == CKR_OK);
- assert_num_eq (pid, rpc_initialized);
-
- rv = mixin.funcs.C_Finalize (&mixin.funcs, NULL);
- assert (rv == CKR_OK);
- assert_num_cmp (pid, !=, rpc_initialized);
-
- p11_virtual_uninit (&mixin);
-}
-
-static void
-test_not_initialized (void)
-{
- p11_rpc_client_vtable vtable = { "vtable-data", rpc_initialize, rpc_transport, rpc_finalize };
- p11_virtual mixin;
- CK_INFO info;
- bool ret;
- CK_RV rv;
-
- /* Build up our own function list */
- rpc_initialized = 0;
- p11_virtual_init (&base, &p11_virtual_base, &mock_module_no_slots, NULL);
-
- ret = p11_rpc_client_init (&mixin, &vtable);
- assert_num_eq (true, ret);
-
- rv = (mixin.funcs.C_GetInfo) (&mixin.funcs, &info);
- assert (rv == CKR_CRYPTOKI_NOT_INITIALIZED);
-
- p11_virtual_uninit (&mixin);
-}
-
-static void
-test_initialize_fails_on_client (void)
-{
- p11_rpc_client_vtable vtable = { "vtable-data", rpc_initialize_fails, rpc_transport, rpc_finalize };
- p11_virtual mixin;
- bool ret;
- CK_RV rv;
-
- /* Build up our own function list */
- rpc_initialized = 0;
- p11_virtual_init (&base, &p11_virtual_base, &mock_module_no_slots, NULL);
-
- ret = p11_rpc_client_init (&mixin, &vtable);
- assert_num_eq (true, ret);
-
- rv = (mixin.funcs.C_Initialize) (&mixin.funcs, NULL);
- assert (rv == CKR_FUNCTION_FAILED);
- assert_num_eq (0, rpc_initialized);
-
- p11_virtual_uninit (&mixin);
-}
-
-static CK_RV
-rpc_transport_fails (p11_rpc_client_vtable *vtable,
- p11_buffer *request,
- p11_buffer *response)
-{
- return CKR_FUNCTION_REJECTED;
-}
-
-static void
-test_transport_fails (void)
-{
- p11_rpc_client_vtable vtable = { "vtable-data", rpc_initialize, rpc_transport_fails, rpc_finalize };
- p11_virtual mixin;
- bool ret;
- CK_RV rv;
-
- /* Build up our own function list */
- rpc_initialized = 0;
- p11_virtual_init (&base, &p11_virtual_base, &mock_module_no_slots, NULL);
-
- ret = p11_rpc_client_init (&mixin, &vtable);
- assert_num_eq (true, ret);
-
- rv = (mixin.funcs.C_Initialize) (&mixin.funcs, NULL);
- assert (rv == CKR_FUNCTION_REJECTED);
- assert_num_eq (0, rpc_initialized);
-
- p11_virtual_uninit (&mixin);
-}
-
-static void
-test_initialize_fails_on_server (void)
-{
- p11_rpc_client_vtable vtable = { "vtable-data", rpc_initialize, rpc_transport, rpc_finalize };
- p11_virtual mixin;
- bool ret;
- CK_RV rv;
-
- /* Build up our own function list */
- p11_virtual_init (&base, &p11_virtual_base, &mock_module_no_slots, NULL);
- base.funcs.C_Initialize = mock_X_Initialize__fails;
-
- ret = p11_rpc_client_init (&mixin, &vtable);
- assert_num_eq (true, ret);
-
- rv = (mixin.funcs.C_Initialize) (&mixin.funcs, NULL);
- assert (rv == CKR_FUNCTION_FAILED);
- assert_num_eq (0, rpc_initialized);
-
- p11_virtual_uninit (&mixin);
-}
-
-static CK_RV
-rpc_transport_bad_parse (p11_rpc_client_vtable *vtable,
- p11_buffer *request,
- p11_buffer *response)
-{
- int rc;
-
- assert_str_eq (vtable->data, "vtable-data");
-
- /* Just zero bytes is an invalid message */
- rc = p11_buffer_reset (response, 2);
- assert (rc >= 0);
-
- memset (response->data, 0, 2);
- response->len = 2;
- return CKR_OK;
-}
-
-static void
-test_transport_bad_parse (void)
-{
- p11_rpc_client_vtable vtable = { "vtable-data", rpc_initialize, rpc_transport_bad_parse, rpc_finalize };
- p11_virtual mixin;
- bool ret;
- CK_RV rv;
-
- /* Build up our own function list */
- rpc_initialized = 0;
- p11_virtual_init (&base, &p11_virtual_base, &mock_module_no_slots, NULL);
-
- ret = p11_rpc_client_init (&mixin, &vtable);
- assert_num_eq (true, ret);
-
- p11_kit_be_quiet ();
-
- rv = (mixin.funcs.C_Initialize) (&mixin.funcs, NULL);
- assert (rv == CKR_DEVICE_ERROR);
- assert_num_eq (0, rpc_initialized);
-
- p11_message_loud ();
- p11_virtual_uninit (&mixin);
-}
-
-static CK_RV
-rpc_transport_short_error (p11_rpc_client_vtable *vtable,
- p11_buffer *request,
- p11_buffer *response)
-{
- int rc;
-
- unsigned char data[] = {
- 0x00, 0x00, 0x00, 0x00, /* RPC_CALL_ERROR */
- 0x00, 0x00, 0x00, 0x01, 0x75, /* signature 'u' */
- 0x00, 0x01, /* short error */
- };
-
- assert_str_eq (vtable->data, "vtable-data");
-
- rc = p11_buffer_reset (response, sizeof (data));
- assert (rc >= 0);
-
- memcpy (response->data, data, sizeof (data));
- response->len = sizeof (data);
- return CKR_OK;
-}
-
-static void
-test_transport_short_error (void)
-{
- p11_rpc_client_vtable vtable = { "vtable-data", rpc_initialize, rpc_transport_short_error, rpc_finalize };
- p11_virtual mixin;
- bool ret;
- CK_RV rv;
-
- /* Build up our own function list */
- p11_virtual_init (&base, &p11_virtual_base, &mock_module_no_slots, NULL);
-
- ret = p11_rpc_client_init (&mixin, &vtable);
- assert_num_eq (true, ret);
-
- p11_kit_be_quiet ();
-
- rv = (mixin.funcs.C_Initialize) (&mixin.funcs, NULL);
- assert (rv == CKR_DEVICE_ERROR);
- assert_num_eq (0, rpc_initialized);
-
- p11_message_loud ();
- p11_virtual_uninit (&mixin);
-}
-
-static CK_RV
-rpc_transport_invalid_error (p11_rpc_client_vtable *vtable,
- p11_buffer *request,
- p11_buffer *response)
-{
- int rc;
-
- unsigned char data[] = {
- 0x00, 0x00, 0x00, 0x00, /* RPC_CALL_ERROR */
- 0x00, 0x00, 0x00, 0x01, 0x75, /* signature 'u' */
- 0x00, 0x00, 0x00, 0x00, /* a CKR_OK error*/
- 0x00, 0x00, 0x00, 0x00,
- };
-
- assert_str_eq (vtable->data, "vtable-data");
-
- rc = p11_buffer_reset (response, sizeof (data));
- assert (rc >= 0);
- memcpy (response->data, data, sizeof (data));
- response->len = sizeof (data);
- return CKR_OK;
-}
-
-static void
-test_transport_invalid_error (void)
-{
- p11_rpc_client_vtable vtable = { "vtable-data", rpc_initialize, rpc_transport_invalid_error, rpc_finalize };
- p11_virtual mixin;
- bool ret;
- CK_RV rv;
-
- /* Build up our own function list */
- p11_virtual_init (&base, &p11_virtual_base, &mock_module_no_slots, NULL);
-
- ret = p11_rpc_client_init (&mixin, &vtable);
- assert_num_eq (true, ret);
-
- p11_kit_be_quiet ();
-
- rv = (mixin.funcs.C_Initialize) (&mixin.funcs, NULL);
- assert (rv == CKR_DEVICE_ERROR);
- assert_num_eq (0, rpc_initialized);
-
- p11_message_loud ();
- p11_virtual_uninit (&mixin);
-}
-
-static CK_RV
-rpc_transport_wrong_response (p11_rpc_client_vtable *vtable,
- p11_buffer *request,
- p11_buffer *response)
-{
- int rc;
-
- unsigned char data[] = {
- 0x00, 0x00, 0x00, 0x02, /* RPC_CALL_C_Finalize */
- 0x00, 0x00, 0x00, 0x00, /* signature '' */
- };
-
- assert_str_eq (vtable->data, "vtable-data");
-
- rc = p11_buffer_reset (response, sizeof (data));
- assert (rc >= 0);
- memcpy (response->data, data, sizeof (data));
- response->len = sizeof (data);
- return CKR_OK;
-}
-
-static void
-test_transport_wrong_response (void)
-{
- p11_rpc_client_vtable vtable = { "vtable-data", rpc_initialize, rpc_transport_wrong_response, rpc_finalize };
- p11_virtual mixin;
- bool ret;
- CK_RV rv;
-
- /* Build up our own function list */
- p11_virtual_init (&base, &p11_virtual_base, &mock_module_no_slots, NULL);
-
- ret = p11_rpc_client_init (&mixin, &vtable);
- assert_num_eq (true, ret);
-
- p11_kit_be_quiet ();
-
- rv = (mixin.funcs.C_Initialize) (&mixin.funcs, NULL);
- assert (rv == CKR_DEVICE_ERROR);
- assert_num_eq (0, rpc_initialized);
-
- p11_message_loud ();
- p11_virtual_uninit (&mixin);
-}
-
-static CK_RV
-rpc_transport_bad_contents (p11_rpc_client_vtable *vtable,
- p11_buffer *request,
- p11_buffer *response)
-{
- int rc;
-
- unsigned char data[] = {
- 0x00, 0x00, 0x00, 0x02, /* RPC_CALL_C_GetInfo */
- 0x00, 0x00, 0x00, 0x05, /* signature 'vsusv' */
- 'v', 's', 'u', 's', 'v',
- 0x00, 0x00, 0x00, 0x00, /* invalid data */
- };
-
- assert_str_eq (vtable->data, "vtable-data");
-
- rc = p11_buffer_reset (response, sizeof (data));
- assert (rc >= 0);
- memcpy (response->data, data, sizeof (data));
- response->len = sizeof (data);
- return CKR_OK;
-}
-
-static void
-test_transport_bad_contents (void)
-{
- p11_rpc_client_vtable vtable = { "vtable-data", rpc_initialize, rpc_transport_bad_contents, rpc_finalize };
- p11_virtual mixin;
- bool ret;
- CK_RV rv;
-
- /* Build up our own function list */
- p11_virtual_init (&base, &p11_virtual_base, &mock_module_no_slots, NULL);
-
- ret = p11_rpc_client_init (&mixin, &vtable);
- assert_num_eq (true, ret);
-
- p11_kit_be_quiet ();
-
- rv = (mixin.funcs.C_Initialize) (&mixin.funcs, NULL);
- assert (rv == CKR_DEVICE_ERROR);
- assert_num_eq (0, rpc_initialized);
-
- p11_message_loud ();
- p11_virtual_uninit (&mixin);
-}
-
-static p11_rpc_client_vtable test_normal_vtable = {
- NULL,
- rpc_initialize,
- rpc_transport,
- rpc_finalize,
-};
-
-static p11_rpc_client_vtable test_device_removed_vtable = {
- NULL,
- rpc_initialize_device_removed,
- rpc_transport,
- rpc_finalize,
-};
-
-static void
-mixin_free (void *data)
-{
- p11_virtual *mixin = data;
- p11_virtual_uninit (mixin);
- free (mixin);
-}
-
-static CK_FUNCTION_LIST_PTR
-setup_test_rpc_module (p11_rpc_client_vtable *vtable,
- CK_FUNCTION_LIST *module_template,
- CK_SESSION_HANDLE *session)
-{
- CK_FUNCTION_LIST *rpc_module;
- p11_virtual *mixin;
- CK_RV rv;
-
- /* Build up our own function list */
- p11_virtual_init (&base, &p11_virtual_base, module_template, NULL);
-
- mixin = calloc (1, sizeof (p11_virtual));
- assert (mixin != NULL);
-
- vtable->data = "vtable-data";
- if (!p11_rpc_client_init (mixin, vtable))
- assert_not_reached ();
-
- rpc_module = p11_virtual_wrap (mixin, mixin_free);
- assert_ptr_not_null (rpc_module);
-
- rv = p11_kit_module_initialize (rpc_module);
- assert (rv == CKR_OK);
-
- if (session) {
- rv = (rpc_module->C_OpenSession) (MOCK_SLOT_ONE_ID, CKF_RW_SESSION | CKF_SERIAL_SESSION,
- NULL, NULL, session);
- assert (rv == CKR_OK);
- }
-
- return rpc_module;
-}
-
-static CK_FUNCTION_LIST *
-setup_mock_module (CK_SESSION_HANDLE *session)
-{
- return setup_test_rpc_module (&test_normal_vtable, &mock_module, session);
-}
-
-static void
-teardown_mock_module (CK_FUNCTION_LIST *rpc_module)
-{
- p11_kit_module_finalize (rpc_module);
- p11_virtual_unwrap (rpc_module);
-}
-
-static void
-test_get_info_stand_in (void)
-{
- CK_FUNCTION_LIST_PTR rpc_module;
- CK_INFO info;
- CK_RV rv;
- char *string;
-
- rpc_module = setup_test_rpc_module (&test_device_removed_vtable,
- &mock_module_no_slots, NULL);
-
- rv = (rpc_module->C_GetInfo) (&info);
- assert (rv == CKR_OK);
-
- assert_num_eq (CRYPTOKI_VERSION_MAJOR, info.cryptokiVersion.major);
- assert_num_eq (CRYPTOKI_VERSION_MINOR, info.cryptokiVersion.minor);
- string = p11_kit_space_strdup (info.manufacturerID, sizeof (info.manufacturerID));
- assert_str_eq ("p11-kit", string);
- free (string);
- string = p11_kit_space_strdup (info.libraryDescription, sizeof (info.libraryDescription));
- assert_str_eq ("p11-kit (no connection)", string);
- free (string);
- assert_num_eq (0, info.flags);
- assert_num_eq (1, info.libraryVersion.major);
- assert_num_eq (1, info.libraryVersion.minor);
-
- teardown_mock_module (rpc_module);
-}
-
-static void
-test_get_slot_list_no_device (void)
-{
- CK_FUNCTION_LIST_PTR rpc_module;
- CK_SLOT_ID slot_list[8];
- CK_ULONG count;
- CK_RV rv;
-
- rpc_module = setup_test_rpc_module (&test_device_removed_vtable,
- &mock_module_no_slots, NULL);
-
- rv = (rpc_module->C_GetSlotList) (CK_TRUE, NULL, &count);
- assert (rv == CKR_OK);
- assert_num_eq (0, count);
- rv = (rpc_module->C_GetSlotList) (CK_FALSE, NULL, &count);
- assert (rv == CKR_OK);
- assert_num_eq (0, count);
-
- count = 8;
- rv = (rpc_module->C_GetSlotList) (CK_TRUE, slot_list, &count);
- assert (rv == CKR_OK);
- assert_num_eq (0, count);
-
- count = 8;
- rv = (rpc_module->C_GetSlotList) (CK_FALSE, slot_list, &count);
- assert (rv == CKR_OK);
- assert_num_eq (0, count);
-
- teardown_mock_module (rpc_module);
-}
-
-static void *
-invoke_in_thread (void *arg)
-{
- CK_FUNCTION_LIST *rpc_module = arg;
- CK_INFO info;
- CK_RV rv;
-
- rv = (rpc_module->C_GetInfo) (&info);
- assert_num_eq (rv, CKR_OK);
-
- assert (memcmp (info.manufacturerID, MOCK_INFO.manufacturerID,
- sizeof (info.manufacturerID)) == 0);
-
- return NULL;
-}
-
-static p11_mutex_t delay_mutex;
-
-static CK_RV
-delayed_C_GetInfo (CK_INFO_PTR info)
-{
- CK_RV rv;
-
- p11_sleep_ms (rand () % 100);
-
- p11_mutex_lock (&delay_mutex);
- rv = mock_C_GetInfo (info);
- p11_mutex_unlock (&delay_mutex);
-
- return rv;
-}
-
-static void
-test_simultaneous_functions (void)
-{
- CK_FUNCTION_LIST real_module;
- CK_FUNCTION_LIST *rpc_module;
- const int num_threads = 128;
- p11_thread_t threads[num_threads];
- int i, ret;
-
- p11_mutex_init (&delay_mutex);
-
- memcpy (&real_module, &mock_module_no_slots, sizeof (CK_FUNCTION_LIST));
- real_module.C_GetInfo = delayed_C_GetInfo;
-
- rpc_module = setup_test_rpc_module (&test_normal_vtable,
- &real_module, NULL);
-
- /* Make the invoked function (above) wait */
- p11_mutex_lock (&delay_mutex);
-
- for (i = 0; i < num_threads; i++) {
- ret = p11_thread_create (threads + i, invoke_in_thread, rpc_module);
- assert_num_eq (0, ret);
- }
-
- /* Let the invoked functions return */
- p11_mutex_unlock (&delay_mutex);
-
- for (i = 0; i < num_threads; i++)
- p11_thread_join (threads[i]);
-
- teardown_mock_module (rpc_module);
- p11_mutex_uninit (&delay_mutex);
-}
-
-static void
-test_fork_and_reinitialize (void)
-{
- CK_FUNCTION_LIST *rpc_module;
- CK_INFO info;
- int status;
- CK_RV rv;
- pid_t pid;
- int i;
-
- rpc_module = setup_test_rpc_module (&test_normal_vtable,
- &mock_module_no_slots, NULL);
-
- pid = fork ();
- assert_num_cmp (pid, >=, 0);
-
- /* The child */
- if (pid == 0) {
- rv = (rpc_module->C_Initialize) (NULL);
- assert_num_eq (CKR_OK, rv);
-
- for (i = 0; i < 32; i++) {
- rv = (rpc_module->C_GetInfo) (&info);
- assert_num_eq (CKR_OK, rv);
- }
-
- rv = (rpc_module->C_Finalize) (NULL);
- assert_num_eq (CKR_OK, rv);
-
- _exit (66);
- }
-
- for (i = 0; i < 128; i++) {
- rv = (rpc_module->C_GetInfo) (&info);
- assert_num_eq (CKR_OK, rv);
- }
-
- assert_num_eq (waitpid (pid, &status, 0), pid);
- assert_num_eq (WEXITSTATUS (status), 66);
-
- teardown_mock_module (rpc_module);
-}
-
-#include "test-mock.c"
-
-int
-main (int argc,
- char *argv[])
-{
- CK_MECHANISM_TYPE mechanisms[] = {
- CKM_MOCK_CAPITALIZE,
- CKM_MOCK_PREFIX,
- CKM_MOCK_GENERATE,
- CKM_MOCK_WRAP,
- CKM_MOCK_DERIVE,
- CKM_MOCK_COUNT,
- 0,
- };
-
- mock_module_init ();
- p11_library_init ();
-
- /* Override the mechanisms that the RPC mechanism will handle */
- p11_rpc_mechanisms_override_supported = mechanisms;
-
- p11_test (test_new_free, "/rpc/new-free");
- p11_test (test_uint16, "/rpc/uint16");
- p11_test (test_uint16_static, "/rpc/uint16-static");
- p11_test (test_uint32, "/rpc/uint32");
- p11_test (test_uint32_static, "/rpc/uint32-static");
- p11_test (test_uint64, "/rpc/uint64");
- p11_test (test_uint64_static, "/rpc/uint64-static");
- p11_test (test_byte_array, "/rpc/byte-array");
- p11_test (test_byte_array_null, "/rpc/byte-array-null");
- p11_test (test_byte_array_too_long, "/rpc/byte-array-too-long");
- p11_test (test_byte_array_static, "/rpc/byte-array-static");
-
- p11_test (test_initialize_fails_on_client, "/rpc/initialize-fails-on-client");
- p11_test (test_initialize_fails_on_server, "/rpc/initialize-fails-on-server");
- p11_test (test_initialize, "/rpc/initialize");
- p11_test (test_not_initialized, "/rpc/not-initialized");
- p11_test (test_transport_fails, "/rpc/transport-fails");
- p11_test (test_transport_bad_parse, "/rpc/transport-bad-parse");
- p11_test (test_transport_short_error, "/rpc/transport-short-error");
- p11_test (test_transport_invalid_error, "/rpc/transport-invalid-error");
- p11_test (test_transport_wrong_response, "/rpc/transport-wrong-response");
- p11_test (test_transport_bad_contents, "/rpc/transport-bad-contents");
- p11_test (test_get_info_stand_in, "/rpc/get-info-stand-in");
- p11_test (test_get_slot_list_no_device, "/rpc/get-slot-list-no-device");
- p11_test (test_simultaneous_functions, "/rpc/simultaneous-functions");
- p11_test (test_fork_and_reinitialize, "/rpc/fork-and-reinitialize");
-
- test_mock_add_tests ("/rpc");
-
- return p11_test_run (argc, argv);
-}
diff --git a/p11-kit/tests/test-transport.c b/p11-kit/tests/test-transport.c
deleted file mode 100644
index 4656d34..0000000
--- a/p11-kit/tests/test-transport.c
+++ /dev/null
@@ -1,281 +0,0 @@
-/*
- * 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 "test.h"
-
-#include "library.h"
-#include "mock.h"
-#include "path.h"
-#include "private.h"
-#include "p11-kit.h"
-#include "rpc.h"
-
-#include <sys/types.h>
-#include <sys/wait.h>
-#include <stdlib.h>
-#include <stdio.h>
-
-struct {
- char *directory;
- char *user_config;
- char *user_modules;
-} test;
-
-static void
-setup_remote (void *unused)
-{
- const char *data;
-
- test.directory = p11_test_directory ("p11-test-config");
- test.user_modules = p11_path_build (test.directory, "modules", NULL);
- if (mkdir (test.user_modules, 0700) < 0)
- assert_not_reached ();
-
- data = "user-config: only\n";
- test.user_config = p11_path_build (test.directory, "pkcs11.conf", NULL);
- p11_test_file_write (NULL, test.user_config, data, strlen (data));
-
- data = "remote: |" BUILDDIR "/../p11-kit remote " BUILDDIR "/.libs/mock-two.so\n";
- p11_test_file_write (test.user_modules, "remote.module", data, strlen (data));
-
- p11_config_user_modules = test.user_modules;
- p11_config_user_file = test.user_config;
-}
-
-static void
-teardown_remote (void *unused)
-{
- p11_test_directory_delete (test.user_modules);
- p11_test_directory_delete (test.directory);
-
- free (test.directory);
- free (test.user_config);
- free (test.user_modules);
-}
-
-static CK_FUNCTION_LIST *
-setup_mock_module (CK_SESSION_HANDLE *session)
-{
- CK_FUNCTION_LIST **modules;
- CK_FUNCTION_LIST *module;
- CK_RV rv;
- int i;
-
- setup_remote (NULL);
-
- modules = p11_kit_modules_load (NULL, 0);
-
- module = p11_kit_module_for_name (modules, "remote");
- assert (module != NULL);
-
- rv = p11_kit_module_initialize (module);
- assert_num_eq (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);
- }
-
- /* Release all the other modules */
- for (i = 0; modules[i] != NULL; i++) {
- if (modules[i] != module)
- p11_kit_module_release (modules[i]);
- }
-
- free (modules);
- return module;
-}
-
-static void
-teardown_mock_module (CK_FUNCTION_LIST *module)
-{
- p11_kit_module_finalize (module);
- teardown_remote (NULL);
-}
-
-static void
-test_basic_exec (void)
-{
- CK_FUNCTION_LIST **modules;
- CK_FUNCTION_LIST *module;
- CK_RV rv;
-
- modules = p11_kit_modules_load (NULL, 0);
-
- module = p11_kit_module_for_name (modules, "remote");
- assert (module != NULL);
-
- rv = p11_kit_module_initialize (module);
- assert_num_eq (rv, CKR_OK);
-
- rv = p11_kit_module_finalize (module);
- assert_num_eq (rv, CKR_OK);
-
- p11_kit_modules_release (modules);
-}
-
-static void *
-invoke_in_thread (void *arg)
-{
- CK_FUNCTION_LIST *rpc_module = arg;
- CK_INFO info;
- CK_RV rv;
-
- rv = (rpc_module->C_GetInfo) (&info);
- assert_num_eq (rv, CKR_OK);
-
- assert (memcmp (info.manufacturerID, MOCK_INFO.manufacturerID,
- sizeof (info.manufacturerID)) == 0);
-
- return NULL;
-}
-
-static void
-test_simultaneous_functions (void)
-{
- CK_FUNCTION_LIST **modules;
- CK_FUNCTION_LIST *module;
- const int num_threads = 128;
- p11_thread_t threads[num_threads];
- int i, ret;
- CK_RV rv;
-
- modules = p11_kit_modules_load (NULL, 0);
-
- module = p11_kit_module_for_name (modules, "remote");
- assert (module != NULL);
-
- rv = p11_kit_module_initialize (module);
- assert_num_eq (rv, CKR_OK);
-
- for (i = 0; i < num_threads; i++) {
- ret = p11_thread_create (threads + i, invoke_in_thread, module);
- assert_num_eq (0, ret);
- }
-
- for (i = 0; i < num_threads; i++)
- p11_thread_join (threads[i]);
-
- rv = p11_kit_module_finalize (module);
- assert_num_eq (rv, CKR_OK);
-
- p11_kit_modules_release (modules);
-}
-
-static void
-test_fork_and_reinitialize (void)
-{
- CK_FUNCTION_LIST **modules;
- CK_FUNCTION_LIST *module;
- CK_INFO info;
- int status;
- CK_RV rv;
- pid_t pid;
- int i;
-
- modules = p11_kit_modules_load (NULL, 0);
-
- module = p11_kit_module_for_name (modules, "remote");
- assert (module != NULL);
-
- rv = p11_kit_module_initialize (module);
- assert_num_eq (rv, CKR_OK);
-
- pid = fork ();
- assert_num_cmp (pid, >=, 0);
-
- /* The child */
- if (pid == 0) {
- rv = (module->C_Initialize) (NULL);
- assert_num_eq (CKR_OK, rv);
-
- for (i = 0; i < 32; i++) {
- rv = (module->C_GetInfo) (&info);
- assert_num_eq (CKR_OK, rv);
- }
-
- rv = (module->C_Finalize) (NULL);
- assert_num_eq (CKR_OK, rv);
-
- _exit (66);
- }
-
- for (i = 0; i < 128; i++) {
- rv = (module->C_GetInfo) (&info);
- assert_num_eq (CKR_OK, rv);
- }
-
- assert_num_eq (waitpid (pid, &status, 0), pid);
- assert_num_eq (WEXITSTATUS (status), 66);
-
- rv = p11_kit_module_finalize (module);
- assert_num_eq (rv, CKR_OK);
-
- p11_kit_modules_release (modules);
-}
-
-
-#include "test-mock.c"
-
-int
-main (int argc,
- char *argv[])
-{
- CK_MECHANISM_TYPE mechanisms[] = {
- CKM_MOCK_CAPITALIZE,
- CKM_MOCK_PREFIX,
- CKM_MOCK_GENERATE,
- CKM_MOCK_WRAP,
- CKM_MOCK_DERIVE,
- CKM_MOCK_COUNT,
- 0,
- };
-
- p11_library_init ();
-
- /* Override the mechanisms that the RPC mechanism will handle */
- p11_rpc_mechanisms_override_supported = mechanisms;
-
- p11_fixture (setup_remote, teardown_remote);
- p11_test (test_basic_exec, "/transport/basic");
- p11_test (test_simultaneous_functions, "/transport/simultaneous-functions");
- p11_test (test_fork_and_reinitialize, "/transport/fork-and-reinitialize");
-
- test_mock_add_tests ("/transport");
-
- return p11_test_run (argc, argv);
-}
diff --git a/p11-kit/tests/test-uri.c b/p11-kit/tests/test-uri.c
deleted file mode 100644
index 9b5b293..0000000
--- a/p11-kit/tests/test-uri.c
+++ /dev/null
@@ -1,1314 +0,0 @@
-/*
- * Copyright (c) 2011, Collabora Ltd.
- *
- * 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@collabora.co.uk>
- */
-
-#include "config.h"
-#include "test.h"
-
-#include "debug.h"
-#include "message.h"
-
-#include <assert.h>
-#include <string.h>
-#include <stdio.h>
-#include <stdlib.h>
-
-#include "p11-kit/uri.h"
-#include "p11-kit/private.h"
-
-static int
-is_module_empty (P11KitUri *uri)
-{
- CK_INFO_PTR info = p11_kit_uri_get_module_info (uri);
- return (info->libraryDescription[0] == 0 &&
- info->manufacturerID[0] == 0 &&
- info->libraryVersion.major == (CK_BYTE)-1 &&
- info->libraryVersion.minor == (CK_BYTE)-1);
-}
-
-static int
-is_token_empty (P11KitUri *uri)
-{
- CK_TOKEN_INFO_PTR token = p11_kit_uri_get_token_info (uri);
- return (token->serialNumber[0] == 0 &&
- token->manufacturerID[0] == 0 &&
- token->label[0] == 0 &&
- token->model[0] == 0);
-}
-
-static int
-are_attributes_empty (P11KitUri *uri)
-{
- return (p11_kit_uri_get_attribute (uri, CKA_LABEL) == NULL &&
- p11_kit_uri_get_attribute (uri, CKA_ID) == NULL &&
- p11_kit_uri_get_attribute (uri, CKA_CLASS) == NULL);
-}
-
-static void
-test_uri_parse (void)
-{
- P11KitUri *uri;
- int ret;
-
- uri = p11_kit_uri_new ();
- assert_ptr_not_null (uri);
-
- ret = p11_kit_uri_parse ("pkcs11:", P11_KIT_URI_FOR_MODULE, uri);
- assert_num_eq (P11_KIT_URI_OK, ret);
-
- 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 (void)
-{
- P11KitUri *uri;
- int ret;
-
- uri = p11_kit_uri_new ();
- assert_ptr_not_null (uri);
-
- ret = p11_kit_uri_parse ("http:\\example.com\test", P11_KIT_URI_FOR_ANY, uri);
- assert_num_eq (P11_KIT_URI_BAD_SCHEME, ret);
-
- p11_kit_uri_free (uri);
-}
-
-static void
-test_uri_parse_with_label (void)
-{
- CK_ATTRIBUTE_PTR attr;
- P11KitUri *uri;
- int ret;
-
- uri = p11_kit_uri_new ();
- assert_ptr_not_null (uri);
-
- ret = p11_kit_uri_parse ("pkcs11:object=Test%20Label", P11_KIT_URI_FOR_ANY, uri);
- assert_num_eq (P11_KIT_URI_OK, ret);
-
- assert (is_module_empty (uri));
- assert (is_token_empty (uri));
-
- attr = p11_kit_uri_get_attribute (uri, CKA_LABEL);
- 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 (void)
-{
- CK_ATTRIBUTE_PTR attr;
- P11KitUri *uri;
- int ret;
-
- uri = p11_kit_uri_new ();
- assert_ptr_not_null (uri);
-
- ret = p11_kit_uri_parse ("pkcs11:object=Test%20Label;object-type=cert", P11_KIT_URI_FOR_ANY, uri);
- assert_num_eq (P11_KIT_URI_OK, ret);
-
- attr = p11_kit_uri_get_attribute (uri, CKA_LABEL);
- 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);
- 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_empty_label (void)
-{
- CK_ATTRIBUTE_PTR attr;
- P11KitUri *uri;
- int ret;
-
- uri = p11_kit_uri_new ();
- assert_ptr_not_null (uri);
-
- ret = p11_kit_uri_parse ("pkcs11:object=;object-type=cert", P11_KIT_URI_FOR_ANY, uri);
- assert_num_eq (P11_KIT_URI_OK, ret);
-
- attr = p11_kit_uri_get_attribute (uri, CKA_LABEL);
- assert_ptr_not_null (attr);
-
- p11_kit_uri_free (uri);
-
- /* really empty */
-
- uri = p11_kit_uri_new ();
- assert_ptr_not_null (uri);
-
- ret = p11_kit_uri_parse ("pkcs11:object-type=cert", P11_KIT_URI_FOR_ANY, uri);
- assert_num_eq (P11_KIT_URI_OK, ret);
-
- attr = p11_kit_uri_get_attribute (uri, CKA_LABEL);
- assert (attr == NULL);
-
- p11_kit_uri_free (uri);
-}
-
-static void
-test_uri_parse_with_empty_id (void)
-{
- CK_ATTRIBUTE_PTR attr;
- P11KitUri *uri;
- int ret;
-
- uri = p11_kit_uri_new ();
- assert_ptr_not_null (uri);
-
- ret = p11_kit_uri_parse ("pkcs11:id=;object-type=cert", P11_KIT_URI_FOR_ANY, uri);
- assert_num_eq (P11_KIT_URI_OK, ret);
-
- attr = p11_kit_uri_get_attribute (uri, CKA_ID);
- assert_ptr_not_null (attr);
-
- p11_kit_uri_free (uri);
-
- /* really empty */
-
- uri = p11_kit_uri_new ();
- assert_ptr_not_null (uri);
-
- ret = p11_kit_uri_parse ("pkcs11:object-type=cert", P11_KIT_URI_FOR_ANY, uri);
- assert_num_eq (P11_KIT_URI_OK, ret);
-
- attr = p11_kit_uri_get_attribute (uri, CKA_ID);
- assert (attr == NULL);
-
- p11_kit_uri_free (uri);
-}
-
-static void
-test_uri_parse_with_id (void)
-{
- CK_ATTRIBUTE_PTR attr;
- P11KitUri *uri;
- int ret;
-
- uri = p11_kit_uri_new ();
- assert_ptr_not_null (uri);
-
- ret = p11_kit_uri_parse ("pkcs11:id=%54%45%53%54%00", P11_KIT_URI_FOR_OBJECT, uri);
- 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);
- 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 (void)
-{
- P11KitUri *uri;
- int ret;
-
- uri = p11_kit_uri_new ();
- assert_ptr_not_null (uri);
-
- ret = p11_kit_uri_parse ("pkcs11:object=Test%", P11_KIT_URI_FOR_OBJECT, uri);
- assert_num_eq (P11_KIT_URI_BAD_ENCODING, ret);
-
- p11_kit_uri_free (uri);
-}
-
-static void
-test_uri_parse_with_bad_hex_encoding (void)
-{
- P11KitUri *uri;
- int ret;
-
- uri = p11_kit_uri_new ();
- assert_ptr_not_null (uri);
-
- ret = p11_kit_uri_parse ("pkcs11:object=T%xxest", P11_KIT_URI_FOR_OBJECT, uri);
- assert_num_eq (P11_KIT_URI_BAD_ENCODING, ret);
-
- p11_kit_uri_free (uri);
-}
-
-static bool
-is_space_string (CK_UTF8CHAR_PTR string, CK_ULONG size, const char *check)
-{
- size_t i, len = strlen (check);
- if (len > size)
- return false;
- if (memcmp (string, check, len) != 0)
- return false;
- for (i = len; i < size; ++i)
- if (string[i] != ' ')
- return false;
- return true;
-}
-
-static void
-test_uri_parse_with_token (void)
-{
- P11KitUri *uri = NULL;
- CK_TOKEN_INFO_PTR token;
- int ret;
-
- uri = p11_kit_uri_new ();
- 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);
- assert_num_eq (P11_KIT_URI_OK, ret);
-
- token = p11_kit_uri_get_token_info (uri);
- 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 (void)
-{
- P11KitUri *uri;
- int ret;
-
- uri = p11_kit_uri_new ();
- assert_ptr_not_null (uri);
-
- ret = p11_kit_uri_parse ("pkcs11:token=Token%", P11_KIT_URI_FOR_TOKEN, uri);
- assert_num_eq (P11_KIT_URI_BAD_ENCODING, ret);
-
- p11_kit_uri_free (uri);
-}
-
-static void
-test_uri_parse_with_bad_syntax (void)
-{
- P11KitUri *uri;
- int ret;
-
- uri = p11_kit_uri_new ();
- assert_ptr_not_null (uri);
-
- ret = p11_kit_uri_parse ("pkcs11:token", P11_KIT_URI_FOR_ANY, uri);
- assert_num_eq (P11_KIT_URI_BAD_SYNTAX, ret);
-
- p11_kit_uri_free (uri);
-}
-
-static void
-test_uri_parse_with_spaces (void)
-{
- P11KitUri *uri = NULL;
- CK_INFO_PTR info;
- int ret;
-
- uri = p11_kit_uri_new ();
- 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);
- assert_num_eq (P11_KIT_URI_OK, ret);
-
- info = p11_kit_uri_get_module_info (uri);
-
- 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 (void)
-{
- P11KitUri *uri = NULL;
- CK_INFO_PTR info;
- int ret;
-
- uri = p11_kit_uri_new ();
- assert_ptr_not_null (uri);
-
- ret = p11_kit_uri_parse ("pkcs11:library-description=The%20Library;library-manufacturer=Me",
- P11_KIT_URI_FOR_MODULE, uri);
- assert_num_eq (P11_KIT_URI_OK, ret);
-
- info = p11_kit_uri_get_module_info (uri);
-
- 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 (void)
-{
- P11KitUri *uri;
- int ret;
-
- uri = p11_kit_uri_new ();
- assert_ptr_not_null (uri);
-
- ret = p11_kit_uri_parse ("pkcs11:library-description=Library%", P11_KIT_URI_FOR_MODULE, uri);
- assert_num_eq (P11_KIT_URI_BAD_ENCODING, ret);
-
- p11_kit_uri_free (uri);
-}
-
-static void
-test_uri_build_empty (void)
-{
- P11KitUri *uri;
- char *string;
- int ret;
-
- uri = p11_kit_uri_new ();
- assert_ptr_not_null (uri);
-
- ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string);
- assert_num_eq (P11_KIT_URI_OK, ret);
- assert_str_eq ("pkcs11:", string);
- free (string);
-
- p11_kit_uri_free (uri);
-}
-
-static void
-set_space_string (CK_BYTE_PTR buffer, CK_ULONG length, const char *string)
-{
- size_t len = strlen (string);
- assert (len <= length);
- memset (buffer, ' ', length);
- memcpy (buffer, string, len);
-}
-
-static void
-test_uri_build_with_token_info (void)
-{
- char *string = NULL;
- P11KitUri *uri;
- P11KitUri *check;
- CK_TOKEN_INFO_PTR token;
- int ret;
-
- uri = p11_kit_uri_new ();
- assert_ptr_not_null (uri);
-
- token = p11_kit_uri_get_token_info (uri);
- set_space_string (token->label, sizeof (token->label), "The Label");
- set_space_string (token->serialNumber, sizeof (token->serialNumber), "44444");
- set_space_string (token->manufacturerID, sizeof (token->manufacturerID), "Me");
- set_space_string (token->model, sizeof (token->model), "Deluxe");
-
- ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string);
- assert_num_eq (P11_KIT_URI_OK, ret);
- assert_ptr_not_null (string);
-
- check = p11_kit_uri_new ();
- assert_ptr_not_null (check);
-
- ret = p11_kit_uri_parse (string, P11_KIT_URI_FOR_TOKEN, check);
- 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);
-
- 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 (void)
-{
- char *string = NULL;
- P11KitUri *uri;
- CK_TOKEN_INFO_PTR token;
- int ret;
-
- uri = p11_kit_uri_new ();
- 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);
- assert_num_eq (P11_KIT_URI_OK, ret);
-
- 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 (void)
-{
- char *string = NULL;
- P11KitUri *uri;
- CK_TOKEN_INFO_PTR token;
- int ret;
-
- uri = p11_kit_uri_new ();
- 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);
- assert_num_eq (P11_KIT_URI_OK, ret);
-
- assert (strstr (string, "token=") != NULL);
- assert (strstr (string, "serial=") != NULL);
-
- free (string);
- p11_kit_uri_free (uri);
-}
-
-static void
-test_uri_build_with_attributes (void)
-{
- char *string = NULL;
- P11KitUri *uri;
- P11KitUri *check;
- CK_OBJECT_CLASS klass;
- CK_ATTRIBUTE_PTR attr;
- CK_ATTRIBUTE at;
- int ret;
-
- uri = p11_kit_uri_new ();
- assert_ptr_not_null (uri);
-
- at.type = CKA_LABEL;
- at.pValue = "The Label";
- at.ulValueLen = 9;
- ret = p11_kit_uri_set_attribute (uri, &at);
- assert_num_eq (P11_KIT_URI_OK, ret);
-
- at.type = CKA_ID;
- at.pValue = "HELLO";
- at.ulValueLen = 5;
- ret = p11_kit_uri_set_attribute (uri, &at);
- assert_num_eq (P11_KIT_URI_OK, ret);
-
- klass = CKO_DATA;
- at.type = CKA_CLASS;
- at.pValue = &klass;
- at.ulValueLen = sizeof (klass);
- ret = p11_kit_uri_set_attribute (uri, &at);
- assert_num_eq (P11_KIT_URI_OK, ret);
-
- ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string);
- assert_num_eq (P11_KIT_URI_OK, ret);
-
- check = p11_kit_uri_new ();
- assert_ptr_not_null (check);
-
- ret = p11_kit_uri_parse (string, P11_KIT_URI_FOR_ANY, check);
- assert_num_eq (P11_KIT_URI_OK, ret);
-
- attr = p11_kit_uri_get_attribute (check, CKA_LABEL);
- 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);
- 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);
- assert_ptr_not_null (attr);
- assert (attr->ulValueLen == 5);
- assert (memcmp (attr->pValue, "HELLO", attr->ulValueLen) == 0);
-
- p11_kit_uri_free (check);
-
- 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 (void)
-{
- P11KitUri *uri;
- CK_ATTRIBUTE_PTR attr;
- int ret;
-
- uri = p11_kit_uri_new ();
- assert_ptr_not_null (uri);
-
- ret = p11_kit_uri_parse ("pkcs11:object-type=private", P11_KIT_URI_FOR_OBJECT, uri);
- assert_num_eq (P11_KIT_URI_OK, ret);
-
- attr = p11_kit_uri_get_attribute (uri, CKA_CLASS);
- 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 (void)
-{
- P11KitUri *uri;
- CK_ATTRIBUTE_PTR attr;
- int ret;
-
- uri = p11_kit_uri_new ();
- assert_ptr_not_null (uri);
-
- ret = p11_kit_uri_parse ("pkcs11:object-type=secret-key", P11_KIT_URI_FOR_OBJECT, uri);
- assert_num_eq (P11_KIT_URI_OK, ret);
-
- attr = p11_kit_uri_get_attribute (uri, CKA_CLASS);
- 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 (void)
-{
- P11KitUri *uri;
- CK_INFO_PTR info;
- int ret;
-
- uri = p11_kit_uri_new ();
- assert_ptr_not_null (uri);
-
- ret = p11_kit_uri_parse ("pkcs11:library-version=2.101", P11_KIT_URI_FOR_MODULE_WITH_VERSION, uri);
- assert_num_eq (P11_KIT_URI_OK, ret);
-
- info = p11_kit_uri_get_module_info (uri);
- 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);
- assert_num_eq (P11_KIT_URI_OK, ret);
-
- info = p11_kit_uri_get_module_info (uri);
- 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);
- 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);
- 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);
- 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);
- 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);
- assert_num_eq (P11_KIT_URI_BAD_VERSION, ret);
-
- p11_kit_uri_free (uri);
-}
-
-static void
-test_uri_parse_parse_unknown_object_type (void)
-{
- P11KitUri *uri;
- CK_ATTRIBUTE_PTR attr;
- int ret;
-
- uri = p11_kit_uri_new ();
- assert_ptr_not_null (uri);
-
- ret = p11_kit_uri_parse ("pkcs11:object-type=unknown", P11_KIT_URI_FOR_OBJECT, uri);
- assert_num_eq (P11_KIT_URI_OK, ret);
-
- attr = p11_kit_uri_get_attribute (uri, CKA_CLASS);
- assert_ptr_eq (NULL, attr);
-
- p11_kit_uri_free (uri);
-}
-
-static void
-test_uri_parse_unrecognized (void)
-{
- P11KitUri *uri;
- int ret;
-
- uri = p11_kit_uri_new ();
- assert_ptr_not_null (uri);
-
- ret = p11_kit_uri_parse ("pkcs11:x-blah=some-value", P11_KIT_URI_FOR_ANY, uri);
- assert_num_eq (P11_KIT_URI_OK, ret);
-
- ret = p11_kit_uri_any_unrecognized (uri);
- assert_num_eq (1, ret);
-
- p11_kit_uri_free (uri);
-}
-
-static void
-test_uri_parse_too_long_is_unrecognized (void)
-{
- P11KitUri *uri;
- int ret;
-
- uri = p11_kit_uri_new ();
- 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);
- assert_num_eq (P11_KIT_URI_OK, ret);
-
- ret = p11_kit_uri_any_unrecognized (uri);
- assert_num_eq (1, ret);
-
- p11_kit_uri_free (uri);
-}
-
-
-
-static void
-test_uri_build_object_type_cert (void)
-{
- CK_ATTRIBUTE attr;
- CK_OBJECT_CLASS klass;
- P11KitUri *uri;
- char *string;
- int ret;
-
- uri = p11_kit_uri_new ();
- assert_ptr_not_null (uri);
-
- klass = CKO_CERTIFICATE;
- attr.type = CKA_CLASS;
- attr.pValue = &klass;
- attr.ulValueLen = sizeof (klass);
- p11_kit_uri_set_attribute (uri, &attr);
-
- ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string);
- 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 (void)
-{
- CK_ATTRIBUTE attr;
- CK_OBJECT_CLASS klass;
- P11KitUri *uri;
- char *string;
- int ret;
-
- uri = p11_kit_uri_new ();
- assert_ptr_not_null (uri);
-
- klass = CKO_PRIVATE_KEY;
- attr.type = CKA_CLASS;
- attr.pValue = &klass;
- attr.ulValueLen = sizeof (klass);
- p11_kit_uri_set_attribute (uri, &attr);
-
- ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string);
- 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 (void)
-{
- CK_ATTRIBUTE attr;
- CK_OBJECT_CLASS klass;
- P11KitUri *uri;
- char *string;
- int ret;
-
- uri = p11_kit_uri_new ();
- assert_ptr_not_null (uri);
-
- klass = CKO_PUBLIC_KEY;
- attr.type = CKA_CLASS;
- attr.pValue = &klass;
- attr.ulValueLen = sizeof (klass);
- p11_kit_uri_set_attribute (uri, &attr);
-
- ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string);
- 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 (void)
-{
- CK_ATTRIBUTE attr;
- CK_OBJECT_CLASS klass;
- P11KitUri *uri;
- char *string;
- int ret;
-
- uri = p11_kit_uri_new ();
- assert_ptr_not_null (uri);
-
- klass = CKO_SECRET_KEY;
- attr.type = CKA_CLASS;
- attr.pValue = &klass;
- attr.ulValueLen = sizeof (klass);
- p11_kit_uri_set_attribute (uri, &attr);
-
- ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string);
- 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 (void)
-{
- CK_INFO_PTR info;
- P11KitUri *uri;
- char *string;
- int ret;
-
- uri = p11_kit_uri_new ();
- 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);
- 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 (void)
-{
- CK_INFO_PTR info;
- P11KitUri *uri;
- char *string;
- int ret;
-
- uri = p11_kit_uri_new ();
- 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);
- 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 (void)
-{
- P11KitUri *uri;
- int ret;
-
- uri = p11_kit_uri_new ();
- assert_ptr_not_null (uri);
-
- ret = p11_kit_uri_any_unrecognized (uri);
- assert_num_eq (0, ret);
-
- p11_kit_uri_set_unrecognized (uri, 1);
-
- ret = p11_kit_uri_any_unrecognized (uri);
- assert_num_eq (1, ret);
-
- p11_kit_uri_set_unrecognized (uri, 0);
-
- ret = p11_kit_uri_any_unrecognized (uri);
- assert_num_eq (0, ret);
-
- p11_kit_uri_free (uri);
-}
-
-static void
-test_uri_match_token (void)
-{
- CK_TOKEN_INFO token;
- P11KitUri *uri;
- int ret;
-
- uri = p11_kit_uri_new ();
- assert_ptr_not_null (uri);
-
- ret = p11_kit_uri_parse ("pkcs11:model=Giselle", P11_KIT_URI_FOR_ANY, uri);
- 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);
- assert_num_eq (1, ret);
-
- set_space_string (token.label, sizeof (token.label), "Another label");
-
- ret = p11_kit_uri_match_token_info (uri, &token);
- assert_num_eq (1, ret);
-
- set_space_string (token.model, sizeof (token.model), "Zoolander");
-
- ret = p11_kit_uri_match_token_info (uri, &token);
- assert_num_eq (0, ret);
-
- p11_kit_uri_set_unrecognized (uri, 1);
-
- ret = p11_kit_uri_match_token_info (uri, &token);
- assert_num_eq (0, ret);
-
- p11_kit_uri_free (uri);
-}
-
-static void
-test_uri_match_module (void)
-{
- CK_INFO info;
- P11KitUri *uri;
- int ret;
-
- uri = p11_kit_uri_new ();
- assert_ptr_not_null (uri);
-
- ret = p11_kit_uri_parse ("pkcs11:library-description=Quiet", P11_KIT_URI_FOR_ANY, uri);
- 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);
- assert_num_eq (1, ret);
-
- set_space_string (info.manufacturerID, sizeof (info.manufacturerID), "Someone else");
-
- ret = p11_kit_uri_match_module_info (uri, &info);
- assert_num_eq (1, ret);
-
- set_space_string (info.libraryDescription, sizeof (info.libraryDescription), "Leise");
-
- ret = p11_kit_uri_match_module_info (uri, &info);
- assert_num_eq (0, ret);
-
- p11_kit_uri_set_unrecognized (uri, 1);
-
- ret = p11_kit_uri_match_module_info (uri, &info);
- assert_num_eq (0, ret);
-
- p11_kit_uri_free (uri);
-}
-
-static void
-test_uri_match_version (void)
-{
- CK_INFO info;
- P11KitUri *uri;
- int ret;
-
- memset (&info, 0, sizeof (info));
-
- uri = p11_kit_uri_new ();
- assert_ptr_not_null (uri);
-
- ret = p11_kit_uri_parse ("pkcs11:library-version=5.8", P11_KIT_URI_FOR_ANY, uri);
- 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);
- assert_num_eq (1, ret);
-
- info.libraryVersion.major = 2;
- info.libraryVersion.minor = 3;
-
- ret = p11_kit_uri_match_module_info (uri, &info);
- assert_num_eq (0, ret);
-
- p11_kit_uri_free (uri);
-}
-
-static void
-test_uri_match_attributes (void)
-{
- CK_ATTRIBUTE attrs[4];
- CK_OBJECT_CLASS klass;
- P11KitUri *uri;
- int ret;
-
- attrs[0].type = CKA_ID;
- attrs[0].pValue = "Blah";
- attrs[0].ulValueLen = 4;
-
- attrs[1].type = CKA_LABEL;
- attrs[1].pValue = "Junk";
- attrs[1].ulValueLen = 4;
-
- attrs[2].type = CKA_COLOR;
- attrs[2].pValue = "blue";
- attrs[2].ulValueLen = 4;
-
- klass = CKO_DATA;
- attrs[3].type = CKA_CLASS;
- attrs[3].pValue = &klass;
- attrs[3].ulValueLen = sizeof (klass);
-
- uri = p11_kit_uri_new ();
- assert_ptr_not_null (uri);
-
- ret = p11_kit_uri_parse ("pkcs11:object=Fancy;id=Blah;object-type=data", P11_KIT_URI_FOR_ANY, uri);
- assert_num_eq (P11_KIT_URI_OK, ret);
-
- ret = p11_kit_uri_match_attributes (uri, attrs, 4);
- assert_num_eq (0, ret);
-
- attrs[1].pValue = "Fancy";
- attrs[1].ulValueLen = 5;
-
- ret = p11_kit_uri_match_attributes (uri, attrs, 4);
- assert_num_eq (1, ret);
-
- p11_kit_uri_clear_attribute (uri, CKA_CLASS);
-
- ret = p11_kit_uri_match_attributes (uri, attrs, 4);
- assert_num_eq (1, ret);
-
- attrs[2].pValue = "pink";
-
- ret = p11_kit_uri_match_attributes (uri, attrs, 4);
- assert_num_eq (1, ret);
-
- p11_kit_uri_set_unrecognized (uri, 1);
-
- ret = p11_kit_uri_match_attributes (uri, attrs, 4);
- assert_num_eq (0, ret);
-
- p11_kit_uri_free (uri);
-}
-
-static void
-test_uri_get_set_attribute (void)
-{
- CK_ATTRIBUTE attr;
- CK_ATTRIBUTE_PTR ptr;
- P11KitUri *uri;
- int ret;
-
- uri = p11_kit_uri_new ();
- assert_ptr_not_null (uri);
-
- ptr = p11_kit_uri_get_attribute (uri, CKA_LABEL);
- assert_ptr_eq (NULL, ptr);
-
- ret = p11_kit_uri_clear_attribute (uri, CKA_LABEL);
- assert_num_eq (P11_KIT_URI_OK, ret);
-
- ret = p11_kit_uri_clear_attribute (uri, CKA_COLOR);
- 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);
- 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);
- assert_num_eq (P11_KIT_URI_OK, ret);
-
- /* And get them too */
- ptr = p11_kit_uri_get_attribute (uri, CKA_COLOR);
- assert_ptr_not_null (ptr);
-
- ptr = p11_kit_uri_get_attribute (uri, CKA_LABEL);
- assert_ptr_not_null (ptr);
-
- 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);
- assert_num_eq (P11_KIT_URI_OK, ret);
-
- ptr = p11_kit_uri_get_attribute (uri, CKA_LABEL);
- assert_ptr_eq (NULL, ptr);
-
- p11_kit_uri_free (uri);
-}
-
-static void
-test_uri_get_set_attributes (void)
-{
- CK_ATTRIBUTE_PTR attrs;
- CK_OBJECT_CLASS klass;
- CK_ATTRIBUTE attr;
- CK_ULONG n_attrs;
- P11KitUri *uri;
- int ret;
-
- uri = p11_kit_uri_new ();
- assert_ptr_not_null (uri);
-
- attrs = p11_kit_uri_get_attributes (uri, &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);
- assert_num_eq (P11_KIT_URI_OK, ret);
-
- attrs = p11_kit_uri_get_attributes (uri, &n_attrs);
- 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);
- assert_num_eq (P11_KIT_URI_OK, ret);
-
- attrs = p11_kit_uri_get_attributes (uri, &n_attrs);
- 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;
- attr.pValue = &klass;
- attr.ulValueLen = sizeof (klass);
-
- ret = p11_kit_uri_set_attribute (uri, &attr);
- assert_num_eq (P11_KIT_URI_OK, ret);
-
- attrs = p11_kit_uri_get_attributes (uri, &n_attrs);
- 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);
- assert_num_eq (P11_KIT_URI_OK, ret);
-
- attrs = p11_kit_uri_get_attributes (uri, &n_attrs);
- 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);
- assert_num_eq (P11_KIT_URI_OK, ret);
-
- attrs = p11_kit_uri_get_attributes (uri, &n_attrs);
- 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);
- assert_ptr_not_null (attrs);
- assert_num_eq (0, n_attrs);
-
- p11_kit_uri_free (uri);
-}
-static void
-test_uri_pin_source (void)
-{
- P11KitUri *uri;
- const char *pin_source;
- char *string;
- int ret;
-
- uri = p11_kit_uri_new ();
- assert_ptr_not_null (uri);
-
- p11_kit_uri_set_pin_source (uri, "|my-pin-source");
-
- pin_source = p11_kit_uri_get_pin_source (uri);
- assert_str_eq ("|my-pin-source", pin_source);
-
- pin_source = p11_kit_uri_get_pinfile (uri);
- 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);
- assert_str_eq ("|my-pin-file", pin_source);
-
- ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string);
- 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);
- assert_num_eq (P11_KIT_URI_OK, ret);
-
- pin_source = p11_kit_uri_get_pin_source (uri);
- assert_str_eq ("blah/blah", pin_source);
-
- p11_kit_uri_free (uri);
-}
-
-static void
-test_uri_free_null (void)
-{
- p11_kit_uri_free (NULL);
-}
-
-static void
-test_uri_message (void)
-{
- 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 (int argc,
- char *argv[])
-{
- 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_empty_label, "/uri/test_uri_parse_with_empty_label");
- p11_test (test_uri_parse_with_empty_id, "/uri/test_uri_parse_with_empty_id");
- 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-util.c b/p11-kit/tests/test-util.c
deleted file mode 100644
index 0e579cd..0000000
--- a/p11-kit/tests/test-util.c
+++ /dev/null
@@ -1,59 +0,0 @@
-/*
- * 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@collabora.co.uk>
- */
-
-#include "config.h"
-#include "test.h"
-
-#include "p11-kit.h"
-
-#include <stdio.h>
-#include <stdlib.h>
-
-static void
-test_space_strlen (void)
-{
- assert_num_eq (4, p11_kit_space_strlen ((const unsigned char *)"Test ", 20));
- assert_num_eq (20, p11_kit_space_strlen ((const unsigned char *)"01234567890123456789", 20));
- assert_num_eq (0, p11_kit_space_strlen ((const unsigned char *)" ", 20));
-}
-
-int
-main (int argc,
- char *argv[])
-{
- putenv ("P11_KIT_STRICT=1");
-
- p11_test (test_space_strlen, "/util/space-strlen");
- return p11_test_run (argc, argv);
-}
diff --git a/p11-kit/tests/test-virtual.c b/p11-kit/tests/test-virtual.c
deleted file mode 100644
index 73777d3..0000000
--- a/p11-kit/tests/test-virtual.c
+++ /dev/null
@@ -1,171 +0,0 @@
-/*
- * 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);
-}