diff options
author | Stef Walter <stefw@collabora.co.uk> | 2011-10-17 14:51:31 +0200 |
---|---|---|
committer | Stef Walter <stefw@collabora.co.uk> | 2011-10-17 15:32:41 +0200 |
commit | db92b76e3acb11e330309ebce071ec2e61400a71 (patch) | |
tree | cb11139880974ef8db53dd83a70de8f32d94cc09 /tests | |
parent | b1d9fd5f88ade222fbd2206c7e11c5514c8b5634 (diff) |
Initial port to win32
* Tests do not all yet pass, at least not on wine
* Added abstraction of some non-portable functions in compat.h/c
* Build with an argument like this for win32 support:
./autogen.sh --host=i586-mingw32msvc
* This win32 port needs more work from interested parties
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Makefile.am | 16 | ||||
-rw-r--r-- | tests/conf-test.c | 2 | ||||
-rw-r--r-- | tests/mock-module.c | 20 | ||||
-rw-r--r-- | tests/mock-module.h | 1 | ||||
-rw-r--r-- | tests/pin-test.c | 3 | ||||
-rw-r--r-- | tests/test-init.c | 56 |
6 files changed, 60 insertions, 38 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am index ab7d8f8..243b869 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -10,26 +10,26 @@ LDADD = \ $(top_builddir)/p11-kit/libp11-kit-testable.la \ $(LTLIBINTL) -noinst_PROGRAMS = \ +CHECK_PROGS = \ hash-test \ ptr-array-test \ conf-test \ uri-test \ pin-test \ - print-messages \ test-init +noinst_PROGRAMS = \ + print-messages \ + $(CHECK_PROGS) + test_init_SOURCES = \ test-init.c \ mock-module.c mock-module.h +CHECK_EXECUTABLES = $(CHECK_PROGS:=$(EXEEXT)) + check-am: - ./hash-test - ./ptr-array-test - ./conf-test - ./uri-test - ./pin-test - ./test-init + for prog in $(CHECK_EXECUTABLES); do ./$$prog; done EXTRA_DIST = \ cutest \ diff --git a/tests/conf-test.c b/tests/conf-test.c index a273c7b..58cb2f4 100644 --- a/tests/conf-test.c +++ b/tests/conf-test.c @@ -375,6 +375,8 @@ main (void) CuSuite* suite = CuSuiteNew (); int ret; + _p11_library_init (); + SUITE_ADD_TEST (suite, test_parse_conf_1); SUITE_ADD_TEST (suite, test_parse_ignore_missing); SUITE_ADD_TEST (suite, test_parse_fail_missing); diff --git a/tests/mock-module.c b/tests/mock-module.c index 201fffc..1b7822a 100644 --- a/tests/mock-module.c +++ b/tests/mock-module.c @@ -34,10 +34,12 @@ #include "config.h" +#define CRYPTOKI_EXPORTS #include "pkcs11.h" #include "mock-module.h" -#include <pthread.h> +#include "p11-kit/compat.h" + #include <stdarg.h> #include <stdio.h> #include <string.h> @@ -48,7 +50,7 @@ */ /* Various mutexes */ -static pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER; +static mutex_t init_mutex; /* Whether we've been initialized, and on what process id it happened */ static int pkcs11_initialized = 0; @@ -95,7 +97,7 @@ mock_C_Initialize (CK_VOID_PTR init_args) debug (("C_Initialize: enter")); - pthread_mutex_lock (&init_mutex); + mutex_lock (&init_mutex); if (init_args != NULL) { int supplied_ok; @@ -146,7 +148,7 @@ done: pkcs11_initialized_pid = 0; } - pthread_mutex_unlock (&init_mutex); + mutex_unlock (&init_mutex); debug (("C_Initialize: %d", ret)); return ret; @@ -159,13 +161,13 @@ mock_C_Finalize (CK_VOID_PTR reserved) return_val_if_fail (pkcs11_initialized != 0, CKR_CRYPTOKI_NOT_INITIALIZED); return_val_if_fail (reserved == NULL, CKR_ARGUMENTS_BAD); - pthread_mutex_lock (&init_mutex); + mutex_lock (&init_mutex); /* This should stop all other calls in */ pkcs11_initialized = 0; pkcs11_initialized_pid = 0; - pthread_mutex_unlock (&init_mutex); + mutex_unlock (&init_mutex); debug (("C_Finalize: %d", CKR_OK)); return CKR_OK; @@ -884,3 +886,9 @@ CK_FUNCTION_LIST mock_module_no_slots = { mock_C_CancelFunction__not_parallel, mock_C_WaitForSlotEvent__no_event, }; + +void +mock_module_init (void) +{ + mutex_init (&init_mutex); +} diff --git a/tests/mock-module.h b/tests/mock-module.h index f198e46..21fa17a 100644 --- a/tests/mock-module.h +++ b/tests/mock-module.h @@ -28,6 +28,7 @@ CK_FUNCTION_LIST mock_module_no_slots; +void mock_module_init (void); CK_RV mock_C_Initialize (CK_VOID_PTR init_args); diff --git a/tests/pin-test.c b/tests/pin-test.c index 0117908..f4cfd8d 100644 --- a/tests/pin-test.c +++ b/tests/pin-test.c @@ -42,6 +42,7 @@ #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, @@ -274,6 +275,8 @@ main (void) CuSuite* suite = CuSuiteNew (); int ret; + _p11_library_init (); + SUITE_ADD_TEST (suite, test_pin_register_unregister); SUITE_ADD_TEST (suite, test_pin_read); SUITE_ADD_TEST (suite, test_pin_read_no_match); diff --git a/tests/test-init.c b/tests/test-init.c index d57b49f..8367f10 100644 --- a/tests/test-init.c +++ b/tests/test-init.c @@ -36,22 +36,25 @@ #include "CuTest.h" #include <sys/types.h> -#include <sys/wait.h> #include <assert.h> -#include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <unistd.h> +#include "p11-kit/compat.h" #include "p11-kit/p11-kit.h" #include "mock-module.h" CK_FUNCTION_LIST module; +#ifdef OS_UNIX + +#include <sys/wait.h> + static CK_RV mock_C_Initialize__with_fork (CK_VOID_PTR init_args) { @@ -95,6 +98,8 @@ test_fork_initialization (CuTest *tc) CuAssertTrue (tc, rv == CKR_OK); } +#endif /* OS_UNIX */ + static CK_RV mock_C_Initialize__with_recursive (CK_VOID_PTR init_args) { @@ -120,7 +125,7 @@ test_recursive_initialization (CuTest *tc) CuAssertTrue (tc, rv == CKR_FUNCTION_FAILED); } -static pthread_mutex_t race_mutex = PTHREAD_MUTEX_INITIALIZER; +static mutex_t race_mutex; static int initialization_count = 0; static int finalization_count = 0; @@ -129,30 +134,25 @@ static int finalization_count = 0; static CK_RV mock_C_Initialize__threaded_race (CK_VOID_PTR init_args) { - struct timespec ts = { 0, 100 * 1000 * 1000 }; - /* Atomically increment value */ - pthread_mutex_lock (&race_mutex); + mutex_lock (&race_mutex); initialization_count += 1; - pthread_mutex_unlock (&race_mutex); + mutex_unlock (&race_mutex); - nanosleep (&ts, NULL); + sleep_ms (100); return CKR_OK; } static CK_RV mock_C_Finalize__threaded_race (CK_VOID_PTR reserved) { - struct timespec ts = { 0, 100 * 1000 * 1000 }; - /* Atomically increment value */ - pthread_mutex_lock (&race_mutex); + mutex_lock (&race_mutex); finalization_count += 1; - pthread_mutex_unlock (&race_mutex); + mutex_unlock (&race_mutex); - nanosleep (&ts, NULL); - return CKR_OK; -} + sleep_ms (100); + return CKR_OK;} static void * initialization_thread (void *data) @@ -181,9 +181,8 @@ finalization_thread (void *data) static void test_threaded_initialization (CuTest *tc) { - static const int num_threads = 100; - pthread_t threads[num_threads]; - void *retval; + static const int num_threads = 2; + thread_t threads[num_threads]; int ret; int i; @@ -196,25 +195,27 @@ test_threaded_initialization (CuTest *tc) finalization_count = 0; for (i = 0; i < num_threads; i++) { - ret = pthread_create (&threads[i], NULL, initialization_thread, tc); + ret = thread_create (&threads[i], initialization_thread, tc); CuAssertIntEquals (tc, 0, ret); + CuAssertTrue (tc, threads[i] != 0); } for (i = 0; i < num_threads; i++) { - ret = pthread_join (threads[i], &retval); + ret = thread_join (threads[i]); CuAssertIntEquals (tc, 0, ret); - CuAssertPtrEquals (tc, tc, retval); + threads[i] = 0; } for (i = 0; i < num_threads; i++) { - ret = pthread_create (&threads[i], NULL, finalization_thread, tc); + ret = thread_create (&threads[i], finalization_thread, tc); CuAssertIntEquals (tc, 0, ret); + CuAssertTrue (tc, threads[i] != 0); } for (i = 0; i < num_threads; i++) { - ret = pthread_join (threads[i], &retval); + ret = thread_join (threads[i]); CuAssertIntEquals (tc, 0, ret); - CuAssertPtrEquals (tc, tc, retval); + threads[i] = 0; } /* C_Initialize should have been called exactly once */ @@ -229,7 +230,14 @@ main (void) CuSuite* suite = CuSuiteNew (); int ret; + mutex_init (&race_mutex); + mock_module_init (); + _p11_library_init (); + +#ifdef OS_UNIX SUITE_ADD_TEST (suite, test_fork_initialization); +#endif + SUITE_ADD_TEST (suite, test_recursive_initialization); SUITE_ADD_TEST (suite, test_threaded_initialization); |