diff options
-rw-r--r-- | .gitignore | 32 | ||||
-rw-r--r-- | Makefile.am | 13 | ||||
-rw-r--r-- | build/Makefile.am | 11 | ||||
-rw-r--r-- | build/Makefile.tests | 8 | ||||
-rw-r--r-- | build/cutest/CuTest.c (renamed from tests/cutest/CuTest.c) | 0 | ||||
-rw-r--r-- | build/cutest/CuTest.h (renamed from tests/cutest/CuTest.h) | 0 | ||||
-rw-r--r-- | build/cutest/README.txt (renamed from tests/cutest/README.txt) | 0 | ||||
-rw-r--r-- | build/cutest/license.txt (renamed from tests/cutest/license.txt) | 0 | ||||
-rw-r--r-- | build/m4/.empty (renamed from m4/.empty) | 0 | ||||
-rw-r--r-- | common/Makefile.am | 11 | ||||
-rw-r--r-- | common/array.c (renamed from p11-kit/ptr-array.c) | 89 | ||||
-rw-r--r-- | common/array.h (renamed from p11-kit/ptr-array.h) | 35 | ||||
-rw-r--r-- | common/compat.c | 95 | ||||
-rw-r--r-- | common/compat.h | 123 | ||||
-rw-r--r-- | common/debug.c (renamed from p11-kit/debug.c) | 20 | ||||
-rw-r--r-- | common/debug.h (renamed from p11-kit/debug.h) | 74 | ||||
-rw-r--r-- | common/dict.c (renamed from p11-kit/hashmap.c) | 244 | ||||
-rw-r--r-- | common/dict.h (renamed from p11-kit/hashmap.h) | 101 | ||||
-rw-r--r-- | common/library.c | 286 | ||||
-rw-r--r-- | common/library.h | 80 | ||||
-rw-r--r-- | common/tests/Makefile.am | 32 | ||||
-rw-r--r-- | common/tests/test-array.c | 194 | ||||
-rw-r--r-- | common/tests/test-dict.c (renamed from tests/hash-test.c) | 158 | ||||
-rw-r--r-- | configure.ac | 7 | ||||
-rw-r--r-- | doc/Makefile.am | 11 | ||||
-rw-r--r-- | p11-kit/Makefile.am | 54 | ||||
-rw-r--r-- | p11-kit/conf.c | 116 | ||||
-rw-r--r-- | p11-kit/conf.h | 14 | ||||
-rw-r--r-- | p11-kit/modules.c | 318 | ||||
-rw-r--r-- | p11-kit/pin.c | 78 | ||||
-rw-r--r-- | p11-kit/private.h | 41 | ||||
-rw-r--r-- | p11-kit/proxy.c | 86 | ||||
-rw-r--r-- | p11-kit/tests/Makefile.am (renamed from tests/Makefile.am) | 25 | ||||
-rw-r--r-- | p11-kit/tests/conf-test.c (renamed from tests/conf-test.c) | 201 | ||||
-rw-r--r-- | p11-kit/tests/files/system-modules/four.module (renamed from tests/files/system-modules/four.module) | 0 | ||||
-rw-r--r-- | p11-kit/tests/files/system-modules/one.module (renamed from tests/files/system-modules/one.module) | 0 | ||||
-rw-r--r-- | p11-kit/tests/files/system-modules/two-duplicate.module (renamed from tests/files/system-modules/two-duplicate.module) | 0 | ||||
-rw-r--r-- | p11-kit/tests/files/system-modules/two.badname (renamed from tests/files/system-modules/two.badname) | 0 | ||||
-rw-r--r-- | p11-kit/tests/files/system-modules/win32/four.module (renamed from tests/files/system-modules/win32/four.module) | 0 | ||||
-rw-r--r-- | p11-kit/tests/files/system-modules/win32/one.module (renamed from tests/files/system-modules/win32/one.module) | 0 | ||||
-rw-r--r-- | p11-kit/tests/files/system-modules/win32/two-duplicate.module (renamed from tests/files/system-modules/win32/two-duplicate.module) | 0 | ||||
-rw-r--r-- | p11-kit/tests/files/system-modules/win32/two.badname (renamed from tests/files/system-modules/win32/two.badname) | 0 | ||||
-rw-r--r-- | p11-kit/tests/files/system-pkcs11.conf (renamed from tests/files/system-pkcs11.conf) | 0 | ||||
-rw-r--r-- | p11-kit/tests/files/test-1.conf (renamed from tests/files/test-1.conf) | 0 | ||||
-rw-r--r-- | p11-kit/tests/files/test-pinfile (renamed from tests/files/test-pinfile) | 0 | ||||
-rw-r--r-- | p11-kit/tests/files/test-pinfile-large (renamed from tests/files/test-pinfile-large) | 0 | ||||
-rw-r--r-- | p11-kit/tests/files/test-system-invalid.conf (renamed from tests/files/test-system-invalid.conf) | 0 | ||||
-rw-r--r-- | p11-kit/tests/files/test-system-merge.conf (renamed from tests/files/test-system-merge.conf) | 0 | ||||
-rw-r--r-- | p11-kit/tests/files/test-system-none.conf (renamed from tests/files/test-system-none.conf) | 0 | ||||
-rw-r--r-- | p11-kit/tests/files/test-system-only.conf (renamed from tests/files/test-system-only.conf) | 0 | ||||
-rw-r--r-- | p11-kit/tests/files/test-user-invalid.conf (renamed from tests/files/test-user-invalid.conf) | 0 | ||||
-rw-r--r-- | p11-kit/tests/files/test-user-only.conf (renamed from tests/files/test-user-only.conf) | 0 | ||||
-rw-r--r-- | p11-kit/tests/files/test-user.conf (renamed from tests/files/test-user.conf) | 0 | ||||
-rw-r--r-- | p11-kit/tests/files/user-modules/one.module (renamed from tests/files/user-modules/one.module) | 0 | ||||
-rw-r--r-- | p11-kit/tests/files/user-modules/three.module (renamed from tests/files/user-modules/three.module) | 0 | ||||
-rw-r--r-- | p11-kit/tests/files/user-modules/win32/one.module (renamed from tests/files/user-modules/win32/one.module) | 0 | ||||
-rw-r--r-- | p11-kit/tests/files/user-modules/win32/three.module (renamed from tests/files/user-modules/win32/three.module) | 0 | ||||
-rw-r--r-- | p11-kit/tests/mock-module-ep.c (renamed from tests/mock-module-ep.c) | 0 | ||||
-rw-r--r-- | p11-kit/tests/mock-module.c (renamed from tests/mock-module.c) | 16 | ||||
-rw-r--r-- | p11-kit/tests/mock-module.h (renamed from tests/mock-module.h) | 0 | ||||
-rw-r--r-- | p11-kit/tests/pin-test.c (renamed from tests/pin-test.c) | 4 | ||||
-rw-r--r-- | p11-kit/tests/print-messages.c (renamed from tests/print-messages.c) | 0 | ||||
-rw-r--r-- | p11-kit/tests/progname-test.c (renamed from tests/progname-test.c) | 21 | ||||
-rw-r--r-- | p11-kit/tests/test-init.c (renamed from tests/test-init.c) | 31 | ||||
-rw-r--r-- | p11-kit/tests/test-modules.c (renamed from tests/test-modules.c) | 25 | ||||
-rw-r--r-- | p11-kit/tests/uri-test.c (renamed from tests/uri-test.c) | 4 | ||||
-rw-r--r-- | p11-kit/uri.c | 6 | ||||
-rw-r--r-- | p11-kit/util.c | 318 | ||||
-rw-r--r-- | p11-kit/util.h | 158 | ||||
-rw-r--r-- | tests/ptr-array-test.c | 257 |
70 files changed, 1746 insertions, 1651 deletions
@@ -20,7 +20,6 @@ .project .cproject .settings -m4/*.m4 ABOUT-NLS aclocal.m4 @@ -46,6 +45,17 @@ missing stamp-h1 temp.txt +/*/tests/test-* +!/*/tests/test-*.c +/*/tests/frob-* +!/*/tests/frob-*.c + +/build/m4/*.m4 +/build/coverage +/build/coverage.info + +/common/tests/hash-test + /doc/html /doc/p11-kit-decl-list.txt /doc/p11-kit-decl.txt @@ -71,19 +81,13 @@ temp.txt /po/remove-potcdate.sed /po/Makevars.template -/tests/coverage -/tests/coverage.info -/tests/hash-test -/tests/p11-test -/tests/pin-test -/tests/print-messages -/tests/print-progname -/tests/progname-test -/tests/ptr-array-test -/tests/conf-test -/tests/uri-test -/tests/test-* -!/tests/test-*.c +/p11-kit/tests/p11-test +/p11-kit/tests/pin-test +/p11-kit/tests/print-messages +/p11-kit/tests/print-progname +/p11-kit/tests/progname-test +/p11-kit/tests/conf-test +/p11-kit/tests/uri-test /tools/p11-kit diff --git a/Makefile.am b/Makefile.am index ac00ffd..2e28212 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,12 +1,13 @@ SUBDIRS = \ + build \ + common \ p11-kit \ tools \ - tests \ doc \ po -ACLOCAL_AMFLAGS = -I m4 +ACLOCAL_AMFLAGS = -I build/m4 DISTCHECK_CONFIGURE_FLAGS = \ --enable-gtk-doc \ @@ -28,12 +29,12 @@ dist-hook: if WITH_COVERAGE coverage: - mkdir -p tests/coverage + mkdir -p build/coverage $(LCOV) --directory . --zerocounters $(MAKE) check - $(LCOV) --directory . --capture --output-file tests/coverage.info - $(GENHTML) --output-directory tests/coverage tests/coverage.info - @echo "file://$(abs_top_builddir)/tests/coverage/index.html" + $(LCOV) --directory . --capture --output-file build/coverage.info + $(GENHTML) --output-directory build/coverage build/coverage.info + @echo "file://$(abs_top_builddir)/build/coverage/index.html" endif if ENABLE_GTK_DOC diff --git a/build/Makefile.am b/build/Makefile.am new file mode 100644 index 0000000..f8841ec --- /dev/null +++ b/build/Makefile.am @@ -0,0 +1,11 @@ + +EXTRA_DIST = \ + cutest \ + Makefile.tests + +noinst_LTLIBRARIES = \ + libcutest.la + +libcutest_la_SOURCES = \ + cutest/CuTest.c \ + cutest/CuTest.h diff --git a/build/Makefile.tests b/build/Makefile.tests new file mode 100644 index 0000000..9f41045 --- /dev/null +++ b/build/Makefile.tests @@ -0,0 +1,8 @@ + +CUTEST_CFLAGS = \ + -I$(top_srcdir)/build/cutest \ + -DSRCDIR=\"$(abs_srcdir)\" \ + -DBUILDDIR=\"$(abs_builddir)\" \ + -DP11_KIT_FUTURE_UNSTABLE_API + +CUTEST_LIBS = $(top_builddir)/build/libcutest.la diff --git a/tests/cutest/CuTest.c b/build/cutest/CuTest.c index b033483..b033483 100644 --- a/tests/cutest/CuTest.c +++ b/build/cutest/CuTest.c diff --git a/tests/cutest/CuTest.h b/build/cutest/CuTest.h index b82d05b..b82d05b 100644 --- a/tests/cutest/CuTest.h +++ b/build/cutest/CuTest.h diff --git a/tests/cutest/README.txt b/build/cutest/README.txt index 96e8853..96e8853 100644 --- a/tests/cutest/README.txt +++ b/build/cutest/README.txt diff --git a/tests/cutest/license.txt b/build/cutest/license.txt index 3d94167..3d94167 100644 --- a/tests/cutest/license.txt +++ b/build/cutest/license.txt diff --git a/m4/.empty b/build/m4/.empty index be533a1..be533a1 100644 --- a/m4/.empty +++ b/build/m4/.empty diff --git a/common/Makefile.am b/common/Makefile.am new file mode 100644 index 0000000..f37a501 --- /dev/null +++ b/common/Makefile.am @@ -0,0 +1,11 @@ +NULL = + +SUBDIRS = . tests + +EXTRA_DIST = \ + array.c array.h \ + compat.c compat.h \ + debug.c debug.h \ + dict.c dict.h \ + library.c library.h \ + $(NULL) diff --git a/p11-kit/ptr-array.c b/common/array.c index f393641..f645fd1 100644 --- a/p11-kit/ptr-array.c +++ b/common/array.c @@ -32,20 +32,13 @@ #include "config.h" -#include "ptr-array.h" +#include "array.h" #include <stdlib.h> #include <string.h> -struct ptr_array { - void **memory; - unsigned int length; - unsigned int allocated; - ptr_array_destroy_func destroy; -}; - static int -maybe_expand_array (ptr_array_t *array, +maybe_expand_array (p11_array *array, unsigned int length) { unsigned int new_allocated; @@ -58,97 +51,69 @@ maybe_expand_array (ptr_array_t *array, if (new_allocated < length) new_allocated = length; - new_memory = realloc (array->memory, new_allocated * sizeof (void*)); + new_memory = realloc (array->elem, new_allocated * sizeof (void*)); if (new_memory == NULL) return 0; - array->memory = new_memory; + array->elem = new_memory; array->allocated = new_allocated; return 1; } -ptr_array_t * -_p11_ptr_array_create (ptr_array_destroy_func destroy_func) +p11_array * +p11_array_new (p11_destroyer destroyer) { - ptr_array_t *array; + p11_array *array; - array = calloc (1, sizeof (ptr_array_t)); + array = calloc (1, sizeof (p11_array)); if (array == NULL) return NULL; if (!maybe_expand_array (array, 2)) { - _p11_ptr_array_free (array); + p11_array_free (array); return NULL; } - array->destroy = destroy_func; + array->destroyer = destroyer; return array; } void -_p11_ptr_array_free (ptr_array_t *array) +p11_array_free (p11_array *array) { unsigned int i; if (array == NULL) return; - if (array->destroy) { - for (i = 0; i < array->length; i++) - (array->destroy) (array->memory[i]); + if (array->destroyer) { + for (i = 0; i < array->num; i++) + (array->destroyer) (array->elem[i]); } - free (array->memory); + free (array->elem); free (array); } -unsigned int -_p11_ptr_array_count (ptr_array_t *array) -{ - return array->length; -} - int -_p11_ptr_array_add (ptr_array_t *array, - void *value) +p11_array_push (p11_array *array, + void *value) { - if (!maybe_expand_array (array, array->length + 1)) + if (!maybe_expand_array (array, array->num + 1)) return 0; - array->memory[array->length] = value; - array->length++; + array->elem[array->num] = value; + array->num++; return 1; } void -_p11_ptr_array_remove (ptr_array_t *array, - unsigned int index) -{ - if (array->destroy) - (array->destroy) (array->memory[index]); - memmove (array->memory + index, array->memory + index + 1, - (array->length - (index + 1)) * sizeof (void*)); - array->length--; -} - -void * -_p11_ptr_array_at (ptr_array_t *array, - unsigned int index) +p11_array_remove (p11_array *array, + unsigned int index) { - return array->memory[index]; -} - -void ** -_p11_ptr_array_snapshot (ptr_array_t *array) -{ - void **snapshot; - size_t bytes; - - bytes = array->length * sizeof (void*); - snapshot = malloc (bytes); - if (!snapshot) - return NULL; - - memcpy (snapshot, array->memory, bytes); - return snapshot; + if (array->destroyer) + (array->destroyer) (array->elem[index]); + memmove (array->elem + index, array->elem + index + 1, + (array->num - (index + 1)) * sizeof (void*)); + array->num--; } diff --git a/p11-kit/ptr-array.h b/common/array.h index ba7ec87..6ed7866 100644 --- a/p11-kit/ptr-array.h +++ b/common/array.h @@ -32,30 +32,35 @@ * Author: Stef Waler <stefw@collabora.co.uk> */ -#ifndef __PTR_ARRAY_H__ -#define __PTR_ARRAY_H__ +#ifndef __P11_ARRAY_H__ +#define __P11_ARRAY_H__ #include <sys/types.h> -typedef struct ptr_array ptr_array_t; +#ifndef P11_DESTROYER_DEFINED +#define P11_DESTROYER_DEFINED -typedef void (*ptr_array_destroy_func) (void *data); +typedef void (*p11_destroyer) (void *data); -ptr_array_t* _p11_ptr_array_create (ptr_array_destroy_func destroy_func); +#endif -void _p11_ptr_array_free (ptr_array_t *array); +typedef struct _p11_array { + void **elem; + unsigned int num; -unsigned int _p11_ptr_array_count (ptr_array_t *array); + /* private */ + unsigned int allocated; + p11_destroyer destroyer; +} p11_array; -int _p11_ptr_array_add (ptr_array_t *array, - void *value); +p11_array * p11_array_new (p11_destroyer destroyer); -void _p11_ptr_array_remove (ptr_array_t *array, - unsigned int index); +void p11_array_free (p11_array *array); -void* _p11_ptr_array_at (ptr_array_t *array, - unsigned int index); +int p11_array_push (p11_array *array, + void *value); -void** _p11_ptr_array_snapshot (ptr_array_t *array); +void p11_array_remove (p11_array *array, + unsigned int index); -#endif /* __PTR_ARRAY_H__ */ +#endif /* __P11_ARRAY_H__ */ diff --git a/common/compat.c b/common/compat.c index 0f7cb78..fe16e07 100644 --- a/common/compat.c +++ b/common/compat.c @@ -36,6 +36,7 @@ #include "compat.h" +#include <assert.h> #include <stdlib.h> #include <string.h> @@ -110,6 +111,80 @@ getprogname (void) #endif /* HAVE_GETPROGNAME */ +#ifdef OS_UNIX + +void +p11_mutex_init (p11_mutex_t *mutex) +{ + pthread_mutexattr_t attr; + int ret; + + pthread_mutexattr_init (&attr); + pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE); + ret = pthread_mutex_init (mutex, &attr); + assert (ret == 0); + pthread_mutexattr_destroy (&attr); +} + +#endif /* OS_UNIX */ + +#ifdef OS_WIN32 + +const char * +p11_module_error (void) +{ + DWORD code = GetLastError(); + p11_local *local; + LPVOID msg_buf; + + local = p11_library_get_thread_local (); + + FormatMessageA (FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, code, + MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPSTR)&msg_buf, 0, NULL); + + if (local->last_error) + LocalFree (local->last_error); + local->last_error = msg_buf; + + return msg_buf; +} + +int +p11_thread_create (p11_thread_t *thread, + p11_thread_routine routine, + void *arg) +{ + assert (thread); + + *thread = CreateThread (NULL, 0, + (LPTHREAD_START_ROUTINE)routine, + arg, 0, NULL); + + if (*thread == NULL) + return GetLastError (); + + return 0; +} + +int +p11_thread_join (p11_thread_t thread) +{ + DWORD res; + + res = WaitForSingleObject (thread, INFINITE); + if (res == WAIT_FAILED) + return GetLastError (); + + CloseHandle (thread); + return 0; +} + +#endif /* OS_WIN32 */ + #ifndef HAVE_ERR_H #include <stdlib.h> @@ -273,3 +348,23 @@ vwarnx (const char *fmt, } #endif /* HAVE_ERR_H */ + +#ifndef HAVE_MEMDUP + +void * +memdup (void *data, + size_t length) +{ + void *dup; + + if (!data) + return NULL; + + dup = malloc (length); + if (dup != NULL) + memcpy (dup, data, length); + + return dup; +} + +#endif /* HAVE_MEMDUP */ diff --git a/common/compat.h b/common/compat.h index 8b3ac21..5061b9f 100644 --- a/common/compat.h +++ b/common/compat.h @@ -37,6 +37,8 @@ #include "config.h" +#include <sys/types.h> + #if !defined(__cplusplus) && (__GNUC__ > 2) #define GNUC_PRINTF(x, y) __attribute__((__format__(__printf__, x, y))) #else @@ -53,6 +55,120 @@ const char * getprogname (void); #endif +/* ----------------------------------------------------------------------------- + * WIN32 + */ + +#ifdef OS_WIN32 + +#ifndef _WIN32_WINNT +#define _WIN32_WINNT 0x500 +#endif + +#ifndef _WIN32_IE +#define _WIN32_IE 0x500 +#endif + +#define WIN32_LEAN_AND_MEAN 1 +#include <windows.h> + +/* Oh ... my ... god */ +#undef CreateMutex + +typedef CRITICAL_SECTION p11_mutex_t; + +typedef HANDLE p11_thread_t; + +typedef DWORD p11_thread_id_t; + +#define p11_mutex_init(m) \ + (InitializeCriticalSection (m)) +#define p11_mutex_lock(m) \ + (EnterCriticalSection (m)) +#define p11_mutex_unlock(m) \ + (LeaveCriticalSection (m)) +#define p11_mutex_uninit(m) \ + (DeleteCriticalSection (m)) + +typedef void * (*p11_thread_routine) (void *arg); + +int p11_thread_create (thread_t *thread, thread_routine, void *arg); + +int p11_thread_join (thread_t thread); + +/* Returns a thread_id_t */ +#define p11_thread_id_self() \ + (GetCurrentThreadId ()) + +typedef HMODULE dl_module_t; + +#define p11_module_open(f) \ + (LoadLibrary (f)) +#define p11_module_close(d) \ + (FreeLibrary (d)) +#define p11_module_symbol(d, s) \ + ((void *)GetProcAddress ((d), (s))) + +const char * p11_module_error (void); + +#define p11_sleep_ms(ms) \ + (Sleep (ms)) + +#endif /* OS_WIN32 */ + +/* ---------------------------------------------------------------------------- + * UNIX + */ + +#ifdef OS_UNIX + +#include <pthread.h> +#include <dlfcn.h> +#include <time.h> + +typedef pthread_mutex_t p11_mutex_t; + +void p11_mutex_init (p11_mutex_t *mutex); + +#define p11_mutex_lock(m) \ + (pthread_mutex_lock (m)) +#define p11_mutex_unlock(m) \ + (pthread_mutex_unlock (m)) +#define p11_mutex_uninit(m) \ + (pthread_mutex_destroy(m)) + +typedef pthread_t p11_thread_t; + +typedef pthread_t p11_thread_id_t; + +typedef void * (*p11_thread_routine) (void *arg); + +#define p11_thread_create(t, r, a) \ + (pthread_create ((t), NULL, (r), (a))) +#define p11_thread_join(t) \ + (pthread_join ((t), NULL)) +#define p11_thread_id_self(m) \ + (pthread_self ()) + +typedef void * dl_module_t; + +#define p11_module_open(f) \ + (dlopen ((f), RTLD_LOCAL | RTLD_NOW)) +#define p11_module_close(d) \ + (dlclose(d)) +#define p11_module_error() \ + (dlerror ()) +#define p11_module_symbol(d, s) \ + (dlsym ((d), (s))) + +#define p11_sleep_ms(ms) \ + do { int _ms = (ms); \ + struct timespec _ts = { _ms / 1000, (_ms % 1000) * 1000 * 1000 }; \ + nanosleep (&_ts, NULL); \ + } while(0) + +#endif /* OS_UNIX */ + #ifdef HAVE_ERR_H #include <err.h> @@ -80,4 +196,11 @@ void vwarnx (const char *fmt, va_list ap); #include <errno.h> #endif /* HAVE_ERRNO_H */ +#ifndef HAVE_MEMDUP + +void * memdup (void *data, + size_t length); + +#endif /* HAVE_MEMDUP */ + #endif /* __COMPAT_H__ */ diff --git a/p11-kit/debug.c b/common/debug.c index a665d0f..cca9aaf 100644 --- a/p11-kit/debug.c +++ b/common/debug.c @@ -51,10 +51,10 @@ struct DebugKey { }; static struct DebugKey debug_keys[] = { - { "lib", DEBUG_LIB }, - { "conf", DEBUG_CONF }, - { "uri", DEBUG_URI }, - { "proxy", DEBUG_PROXY }, + { "lib", P11_DEBUG_LIB }, + { "conf", P11_DEBUG_CONF }, + { "uri", P11_DEBUG_URI }, + { "proxy", P11_DEBUG_PROXY }, { 0, } }; @@ -62,7 +62,7 @@ static int debug_inited = 0; static int debug_strict = 0; /* global variable exported in debug.h */ -int _p11_debug_current_flags = ~0; +int p11_debug_current_flags = ~0; static int parse_environ_flags (void) @@ -114,20 +114,20 @@ parse_environ_flags (void) } void -_p11_debug_init (void) +p11_debug_init (void) { - _p11_debug_current_flags = parse_environ_flags (); + p11_debug_current_flags = parse_environ_flags (); debug_inited = 1; } void -_p11_debug_message (int flag, +p11_debug_message (int flag, const char *format, ...) { char buffer[512]; va_list args; - if (flag & _p11_debug_current_flags) { + if (flag & p11_debug_current_flags) { va_start (args, format); vsnprintf (buffer, sizeof (buffer), format, args); buffer[sizeof (buffer) -1] = 0; @@ -137,7 +137,7 @@ _p11_debug_message (int flag, } void -_p11_debug_precond (const char *format, +p11_debug_precond (const char *format, ...) { va_list va; diff --git a/p11-kit/debug.h b/common/debug.h index c6e26f5..e4759d4 100644 --- a/p11-kit/debug.h +++ b/common/debug.h @@ -32,54 +32,54 @@ * Author: Stef Walter <stefw@collabora.co.uk> */ -#ifndef DEBUG_H -#define DEBUG_H +#ifndef P11_DEBUG_H +#define P11_DEBUG_H #include "compat.h" /* Please keep this enum in sync with keys in debug.c */ -typedef enum { - DEBUG_LIB = 1 << 1, - DEBUG_CONF = 1 << 2, - DEBUG_URI = 1 << 3, - DEBUG_PROXY = 1 << 4, -} DebugFlags; +enum { + P11_DEBUG_LIB = 1 << 1, + P11_DEBUG_CONF = 1 << 2, + P11_DEBUG_URI = 1 << 3, + P11_DEBUG_PROXY = 1 << 4, +}; -extern int _p11_debug_current_flags; +extern int p11_debug_current_flags; -void _p11_debug_init (void); +void p11_debug_init (void); -void _p11_debug_message (int flag, - const char *format, - ...) GNUC_PRINTF (2, 3); +void p11_debug_message (int flag, + const char *format, + ...) GNUC_PRINTF (2, 3); -void _p11_debug_precond (const char *format, - ...) GNUC_PRINTF (1, 2); +void p11_debug_precond (const char *format, + ...) GNUC_PRINTF (1, 2); #define assert_not_reached() \ (assert (0 && "this code should not be reached")) #define return_val_if_fail(x, v) \ do { if (!(x)) { \ - _p11_debug_precond ("p11-kit: '%s' not true at %s\n", #x, __func__); \ + p11_debug_precond ("p11-kit: '%s' not true at %s\n", #x, __func__); \ return v; \ } } while (0) #define return_if_fail(x) \ do { if (!(x)) { \ - _p11_debug_precond ("p11-kit: '%s' not true at %s\n", #x, __func__); \ + p11_debug_precond ("p11-kit: '%s' not true at %s\n", #x, __func__); \ return; \ } } while (0) #define return_if_reached() \ do { \ - _p11_debug_precond ("p11-kit: shouldn't be reached at %s\n", __func__); \ + p11_debug_precond ("p11-kit: shouldn't be reached at %s\n", __func__); \ return; \ } while (0) #define return_val_if_reached(v) \ do { \ - _p11_debug_precond ("p11-kit: shouldn't be reached at %s\n", __func__); \ + p11_debug_precond ("p11-kit: shouldn't be reached at %s\n", __func__); \ return v; \ } while (0) @@ -89,41 +89,41 @@ void _p11_debug_precond (const char *format, * Below this point is outside the DEBUG_H guard - so it can take effect * more than once. So you can do: * - * #define DEBUG_FLAG DEBUG_ONE_THING + * #define P11_DEBUG_FLAG P11_DEBUG_ONE_THING * #include "debug.h" * ... - * DEBUG ("if we're debugging one thing"); + * p11_debug ("if we're debugging one thing"); * ... - * #undef DEBUG_FLAG - * #define DEBUG_FLAG DEBUG_OTHER_THING + * #undef P11_DEBUG_FLAG + * #define P11_DEBUG_FLAG DEBUG_OTHER_THING * #include "debug.h" * ... - * DEBUG ("if we're debugging the other thing"); + * p11_debug ("if we're debugging the other thing"); * ... */ -#ifdef DEBUG_FLAG +#ifdef P11_DEBUG_FLAG #ifdef WITH_DEBUG -#undef _p11_debug -#define _p11_debug(format, ...) do { \ - if (DEBUG_FLAG & _p11_debug_current_flags) \ - _p11_debug_message (DEBUG_FLAG, "%s: " format, __PRETTY_FUNCTION__, ##__VA_ARGS__); \ +#undef p11_debug +#define p11_debug(format, ...) do { \ + if (P11_DEBUG_FLAG & p11_debug_current_flags) \ + p11_debug_message (P11_DEBUG_FLAG, "%s: " format, __PRETTY_FUNCTION__, ##__VA_ARGS__); \ } while (0) -#undef _p11_debugging -#define _p11_debugging \ - (DEBUG_FLAG & _p11_debug_current_flags) +#undef p11_debugging +#define p11_debugging \ + (P11_DEBUG_FLAG & p11_debug_current_flags) #else /* !defined (WITH_DEBUG) */ -#undef _p11_debug -#define _p11_debug(format, ...) \ +#undef p11_debug +#define p11_debug(format, ...) \ do {} while (0) -#undef _p11_debugging -#define _p11_debugging 0 +#undef p11_debugging +#define p11_debugging 0 #endif /* !defined (WITH_DEBUG) */ -#endif /* defined (DEBUG_FLAG) */ +#endif /* defined (P11_DEBUG_FLAG) */ diff --git a/p11-kit/hashmap.c b/common/dict.c index d420221..2f976c1 100644 --- a/p11-kit/hashmap.c +++ b/common/dict.c @@ -33,7 +33,7 @@ #include "config.h" -#include "hashmap.h" +#include "dict.h" #include <sys/types.h> @@ -41,32 +41,32 @@ #include <stdlib.h> #include <string.h> -struct _hashmap { - hash_hash_func hash_func; - hash_equal_func equal_func; - hash_destroy_func key_destroy_func; - hash_destroy_func value_destroy_func; +struct _p11_dict { + p11_dict_hasher hash_func; + p11_dict_equals equal_func; + p11_destroyer key_destroy_func; + p11_destroyer value_destroy_func; - struct _hashbucket **buckets; + struct _p11_dictbucket **buckets; unsigned int num_items; unsigned int num_buckets; }; -typedef struct _hashbucket { +typedef struct _p11_dictbucket { void *key; unsigned int hashed; void *value; - struct _hashbucket *next; -} hashbucket; + struct _p11_dictbucket *next; +} dictbucket; -static hashbucket * -next_entry (hashiter *iter) +static dictbucket * +next_entry (p11_dictiter *iter) { - hashbucket *bucket = iter->next; + dictbucket *bucket = iter->next; while (!bucket) { - if (iter->index >= iter->map->num_buckets) + if (iter->index >= iter->dict->num_buckets) return NULL; - bucket = iter->map->buckets[iter->index++]; + bucket = iter->dict->buckets[iter->index++]; } iter->next = bucket->next; return bucket; @@ -74,11 +74,11 @@ next_entry (hashiter *iter) int -_p11_hash_next (hashiter *iter, - void **key, - void **value) +p11_dict_next (p11_dictiter *iter, + void **key, + void **value) { - hashbucket *bucket = next_entry (iter); + dictbucket *bucket = next_entry (iter); if (bucket == NULL) return 0; if (key) @@ -89,29 +89,29 @@ _p11_hash_next (hashiter *iter, } void -_p11_hash_iterate (hashmap *map, - hashiter *iter) +p11_dict_iterate (p11_dict *dict, + p11_dictiter *iter) { - iter->map = map; + iter->dict = dict; iter->index = 0; iter->next = NULL; } -static hashbucket ** -lookup_or_create_bucket (hashmap *map, +static dictbucket ** +lookup_or_create_bucket (p11_dict *dict, const void *key, int create) { - hashbucket **bucketp; + dictbucket **bucketp; unsigned int hash; /* Perform the hashing */ - hash = map->hash_func (key); + hash = dict->hash_func (key); /* scan linked list */ - for (bucketp = &map->buckets[hash % map->num_buckets]; + for (bucketp = &dict->buckets[hash % dict->num_buckets]; *bucketp != NULL; bucketp = &(*bucketp)->next) { - if((*bucketp)->hashed == hash && map->equal_func ((*bucketp)->key, key)) + if((*bucketp)->hashed == hash && dict->equal_func ((*bucketp)->key, key)) break; } @@ -119,24 +119,24 @@ lookup_or_create_bucket (hashmap *map, return bucketp; /* add a new entry for non-NULL val */ - (*bucketp) = calloc (sizeof (hashbucket), 1); + (*bucketp) = calloc (sizeof (dictbucket), 1); if (*bucketp != NULL) { (*bucketp)->key = (void*)key; (*bucketp)->hashed = hash; - map->num_items++; + dict->num_items++; } return bucketp; } void * -_p11_hash_get (hashmap *map, - const void *key) +p11_dict_get (p11_dict *dict, + const void *key) { - hashbucket **bucketp; + dictbucket **bucketp; - bucketp = lookup_or_create_bucket (map, key, 0); + bucketp = lookup_or_create_bucket (dict, key, 0); if (bucketp && *bucketp) return (void*)((*bucketp)->value); else @@ -144,48 +144,48 @@ _p11_hash_get (hashmap *map, } int -_p11_hash_set (hashmap *map, - void *key, - void *val) +p11_dict_set (p11_dict *dict, + void *key, + void *val) { - hashbucket **bucketp; - hashiter iter; - hashbucket *bucket; - hashbucket **new_buckets; + dictbucket **bucketp; + p11_dictiter iter; + dictbucket *bucket; + dictbucket **new_buckets; unsigned int num_buckets; - bucketp = lookup_or_create_bucket (map, key, 1); + bucketp = lookup_or_create_bucket (dict, key, 1); if(bucketp && *bucketp) { /* Destroy the previous key */ - if ((*bucketp)->key && (*bucketp)->key != key && map->key_destroy_func) - map->key_destroy_func ((*bucketp)->key); + if ((*bucketp)->key && (*bucketp)->key != key && dict->key_destroy_func) + dict->key_destroy_func ((*bucketp)->key); /* Destroy the previous value */ - if ((*bucketp)->value && (*bucketp)->value != val && map->value_destroy_func) - map->value_destroy_func ((*bucketp)->value); + if ((*bucketp)->value && (*bucketp)->value != val && dict->value_destroy_func) + dict->value_destroy_func ((*bucketp)->value); /* replace entry */ (*bucketp)->key = key; (*bucketp)->value = val; /* check that the collision rate isn't too high */ - if (map->num_items > map->num_buckets) { - num_buckets = map->num_buckets * 2 + 1; - new_buckets = (hashbucket **)calloc (sizeof (hashbucket *), num_buckets); + if (dict->num_items > dict->num_buckets) { + num_buckets = dict->num_buckets * 2 + 1; + new_buckets = (dictbucket **)calloc (sizeof (dictbucket *), num_buckets); /* Ignore failures, maybe we can expand later */ if(new_buckets) { - _p11_hash_iterate (map, &iter); + p11_dict_iterate (dict, &iter); while ((bucket = next_entry (&iter)) != NULL) { unsigned int i = bucket->hashed % num_buckets; bucket->next = new_buckets[i]; new_buckets[i] = bucket; } - free (map->buckets); - map->buckets = new_buckets; - map->num_buckets = num_buckets; + free (dict->buckets); + dict->buckets = new_buckets; + dict->num_buckets = num_buckets; } } @@ -196,18 +196,18 @@ _p11_hash_set (hashmap *map, } int -_p11_hash_steal (hashmap *map, - const void *key, - void **stolen_key, - void **stolen_value) +p11_dict_steal (p11_dict *dict, + const void *key, + void **stolen_key, + void **stolen_value) { - hashbucket **bucketp; + dictbucket **bucketp; - bucketp = lookup_or_create_bucket (map, key, 0); + bucketp = lookup_or_create_bucket (dict, key, 0); if (bucketp && *bucketp) { - hashbucket *old = *bucketp; + dictbucket *old = *bucketp; *bucketp = (*bucketp)->next; - --map->num_items; + --dict->num_items; if (stolen_key) *stolen_key = old->key; if (stolen_value) @@ -221,109 +221,109 @@ _p11_hash_steal (hashmap *map, } int -_p11_hash_remove (hashmap *map, - const void *key) +p11_dict_remove (p11_dict *dict, + const void *key) { void *old_key; void *old_value; - if (!_p11_hash_steal (map, key, &old_key, &old_value)) + if (!p11_dict_steal (dict, key, &old_key, &old_value)) return 0; - if (map->key_destroy_func) - map->key_destroy_func (old_key); - if (map->value_destroy_func) - map->value_destroy_func (old_value); + if (dict->key_destroy_func) + dict->key_destroy_func (old_key); + if (dict->value_destroy_func) + dict->value_destroy_func (old_value); return 1; } void -_p11_hash_clear (hashmap *map) +p11_dict_clear (p11_dict *dict) { - hashbucket *bucket, *next; + dictbucket *bucket, *next; int i; /* Free all entries in the array */ - for (i = 0; i < map->num_buckets; ++i) { - bucket = map->buckets[i]; + for (i = 0; i < dict->num_buckets; ++i) { + bucket = dict->buckets[i]; while (bucket != NULL) { next = bucket->next; - if (map->key_destroy_func) - map->key_destroy_func (bucket->key); - if (map->value_destroy_func) - map->value_destroy_func (bucket->value); + if (dict->key_destroy_func) + dict->key_destroy_func (bucket->key); + if (dict->value_destroy_func) + dict->value_destroy_func (bucket->value); free (bucket); bucket = next; } } - memset (map->buckets, 0, map->num_buckets * sizeof (hashbucket *)); - map->num_items = 0; + memset (dict->buckets, 0, dict->num_buckets * sizeof (dictbucket *)); + dict->num_items = 0; } -hashmap * -_p11_hash_create (hash_hash_func hash_func, - hash_equal_func equal_func, - hash_destroy_func key_destroy_func, - hash_destroy_func value_destroy_func) +p11_dict * +p11_dict_new (p11_dict_hasher hash_func, + p11_dict_equals equal_func, + p11_destroyer key_destroy_func, + p11_destroyer value_destroy_func) { - hashmap *map; + p11_dict *dict; assert (hash_func); assert (equal_func); - map = malloc (sizeof (hashmap)); - if (map) { - map->hash_func = hash_func; - map->equal_func = equal_func; - map->key_destroy_func = key_destroy_func; - map->value_destroy_func = value_destroy_func; - - map->num_buckets = 9; - map->buckets = (hashbucket **)calloc (sizeof (hashbucket *), map->num_buckets); - if (!map->buckets) { - free (map); + dict = malloc (sizeof (p11_dict)); + if (dict) { + dict->hash_func = hash_func; + dict->equal_func = equal_func; + dict->key_destroy_func = key_destroy_func; + dict->value_destroy_func = value_destroy_func; + + dict->num_buckets = 9; + dict->buckets = (dictbucket **)calloc (sizeof (dictbucket *), dict->num_buckets); + if (!dict->buckets) { + free (dict); return NULL; } - map->num_items = 0; + dict->num_items = 0; } - return map; + return dict; } void -_p11_hash_free (hashmap *map) +p11_dict_free (p11_dict *dict) { - hashbucket *bucket; - hashiter iter; + dictbucket *bucket; + p11_dictiter iter; - if (!map) + if (!dict) return; - _p11_hash_iterate (map, &iter); + p11_dict_iterate (dict, &iter); while ((bucket = next_entry (&iter)) != NULL) { - if (map->key_destroy_func) - map->key_destroy_func (bucket->key); - if (map->value_destroy_func) - map->value_destroy_func (bucket->value); + if (dict->key_destroy_func) + dict->key_destroy_func (bucket->key); + if (dict->value_destroy_func) + dict->value_destroy_func (bucket->value); free (bucket); } - if (map->buckets) - free (map->buckets); + if (dict->buckets) + free (dict->buckets); - free (map); + free (dict); } unsigned int -_p11_hash_size (hashmap *map) +p11_dict_size (p11_dict *dict) { - return map->num_items; + return dict->num_items; } unsigned int -_p11_hash_string_hash (const void *string) +p11_dict_str_hash (const void *string) { const char *p = string; unsigned int hash = *p; @@ -336,8 +336,8 @@ _p11_hash_string_hash (const void *string) } int -_p11_hash_string_equal (const void *string_one, - const void *string_two) +p11_dict_str_equal (const void *string_one, + const void *string_two) { assert (string_one); assert (string_two); @@ -346,15 +346,15 @@ _p11_hash_string_equal (const void *string_one, } unsigned int -_p11_hash_ulongptr_hash (const void *to_ulong) +p11_dict_ulongptr_hash (const void *to_ulong) { assert (to_ulong); return (unsigned int)*((unsigned long*)to_ulong); } int -_p11_hash_ulongptr_equal (const void *ulong_one, - const void *ulong_two) +p11_dict_ulongptr_equal (const void *ulong_one, + const void *ulong_two) { assert (ulong_one); assert (ulong_two); @@ -362,14 +362,14 @@ _p11_hash_ulongptr_equal (const void *ulong_one, } unsigned int -_p11_hash_intptr_hash (const void *to_int) +p11_dict_intptr_hash (const void *to_int) { assert (to_int); return (unsigned int)*((int*)to_int); } int -_p11_hash_intptr_equal (const void *int_one, +p11_dict_intptr_equal (const void *int_one, const void *int_two) { assert (int_one); @@ -378,14 +378,14 @@ _p11_hash_intptr_equal (const void *int_one, } unsigned int -_p11_hash_direct_hash (const void *ptr) +p11_dict_direct_hash (const void *ptr) { return (unsigned int)(size_t)ptr; } int -_p11_hash_direct_equal (const void *ptr_one, - const void *ptr_two) +p11_dict_direct_equal (const void *ptr_one, + const void *ptr_two) { return ptr_one == ptr_two; } diff --git a/p11-kit/hashmap.h b/common/dict.h index 3b35487..1ba7185 100644 --- a/p11-kit/hashmap.h +++ b/common/dict.h @@ -33,18 +33,18 @@ * Author: Stef Waler <stefw@collabora.co.uk> */ -#ifndef HASHMAP_H_ -#define HASHMAP_H_ +#ifndef P11_DICT_H_ +#define P11_DICT_H_ #include <sys/types.h> /* * ARGUMENT DOCUMENTATION * - * map: The hashmap + * dict: The dict * key: Pointer to the key value * val: Pointer to the value - * iter: A hashmap iterator + * iter: A dict iterator */ @@ -52,122 +52,127 @@ * TYPES */ -/* Abstract type for hash maps. */ -typedef struct _hashmap hashmap; +/* Abstract type for dicts. */ +typedef struct _p11_dict p11_dict; /* Type for scanning hash tables. */ -typedef struct _hashiter { - hashmap *map; - struct _hashbucket *next; +typedef struct _p11_dictiter { + p11_dict *dict; + struct _p11_dictbucket *next; unsigned int index; -} hashiter; +} p11_dictiter; -typedef unsigned int (*hash_hash_func) (const void *data); +typedef unsigned int (*p11_dict_hasher) (const void *data); -typedef int (*hash_equal_func) (const void *one, - const void *two); +typedef int (*p11_dict_equals) (const void *one, + const void *two); -typedef void (*hash_destroy_func) (void *data); +#ifndef P11_DESTROYER_DEFINED +#define P11_DESTROYER_DEFINED + +typedef void (*p11_destroyer) (void *data); + +#endif /* ----------------------------------------------------------------------------- * MAIN */ /* - * _p11_hash_create : Create a hash table + * p11_dict_create : Create a hash table * - returns an allocated hashtable */ -hashmap* _p11_hash_create (hash_hash_func hash_func, - hash_equal_func equal_func, - hash_destroy_func key_destroy_func, - hash_destroy_func value_destroy_func); +p11_dict * p11_dict_new (p11_dict_hasher hasher, + p11_dict_equals equals, + p11_destroyer key_destroyer, + p11_destroyer value_destroyer); /* - * _p11_hash_free : Free a hash table + * p11_dict_free : Free a hash table */ -void _p11_hash_free (hashmap *map); +void p11_dict_free (p11_dict *dict); /* - * _p11_hash_size: Number of values in hash table + * p11_dict_size: Number of values in hash table * - returns the number of entries in hash table */ -unsigned int _p11_hash_size (hashmap *map); +unsigned int p11_dict_size (p11_dict *dict); /* - * _p11_hash_get: Retrieves a value from the hash table + * p11_dict_get: Retrieves a value from the hash table * - returns the value of the entry */ -void* _p11_hash_get (hashmap *map, +void* p11_dict_get (p11_dict *dict, const void *key); /* - * _p11_hash_set: Set a value in the hash table + * p11_dict_set: Set a value in the hash table * - returns 1 if the entry was added properly */ -int _p11_hash_set (hashmap *map, +int p11_dict_set (p11_dict *dict, void *key, void *value); /* - * _p11_hash_remove: Remove a value from the hash table + * p11_dict_remove: Remove a value from the hash table * - returns 1 if the entry was found */ -int _p11_hash_remove (hashmap *map, +int p11_dict_remove (p11_dict *dict, const void *key); /* - * _p11_hash_steal: Remove a value from the hash table without calling + * p11_dict_steal: Remove a value from the hash table without calling * destroy funcs * - returns 1 if the entry was found */ -int _p11_hash_steal (hashmap *map, +int p11_dict_steal (p11_dict *dict, const void *key, void **stolen_key, void **stolen_value); /* - * _p11_hash_iterate: Start enumerating through the hash table + * p11_dict_iterate: Start enumerating through the hash table * - returns a hash iterator */ -void _p11_hash_iterate (hashmap *map, - hashiter *iter); +void p11_dict_iterate (p11_dict *dict, + p11_dictiter *iter); /* - * _p11_hash_next: Enumerate through hash table + * p11_dict_next: Enumerate through hash table * - sets key and value to key and/or value * - returns whether there was another entry */ -int _p11_hash_next (hashiter *iter, +int p11_dict_next (p11_dictiter *iter, void **key, void **value); /* - * _p11_hash_clear: Clear all values from has htable. + * p11_dict_clear: Clear all values from has htable. */ -void _p11_hash_clear (hashmap *map); +void p11_dict_clear (p11_dict *dict); /* ----------------------------------------------------------------------------- - * HASH FUNCTIONS + * KEY FUNCTIONS */ -unsigned int _p11_hash_string_hash (const void *string); +unsigned int p11_dict_str_hash (const void *string); -int _p11_hash_string_equal (const void *string_one, +int p11_dict_str_equal (const void *string_one, const void *string_two); -unsigned int _p11_hash_ulongptr_hash (const void *to_ulong); +unsigned int p11_dict_ulongptr_hash (const void *to_ulong); -int _p11_hash_ulongptr_equal (const void *ulong_one, +int p11_dict_ulongptr_equal (const void *ulong_one, const void *ulong_two); -unsigned int _p11_hash_intptr_hash (const void *to_int); +unsigned int p11_dict_intptr_hash (const void *to_int); -int _p11_hash_intptr_equal (const void *int_one, +int p11_dict_intptr_equal (const void *int_one, const void *int_two); -unsigned int _p11_hash_direct_hash (const void *ptr); +unsigned int p11_dict_direct_hash (const void *ptr); -int _p11_hash_direct_equal (const void *ptr_one, +int p11_dict_direct_equal (const void *ptr_one, const void *ptr_two); -#endif /* __HASHMAP_H__ */ +#endif /* __P11_DICT_H__ */ diff --git a/common/library.c b/common/library.c new file mode 100644 index 0000000..e8547b3 --- /dev/null +++ b/common/library.c @@ -0,0 +1,286 @@ +/* + * Copyright (c) 2011 Collabora Ltd + * Copyright (c) 2012 Stef 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. + * + * + * CONTRIBUTORS + * Stef Walter <stef@thewalter.net> + */ + +#include "config.h" + +#include "compat.h" +#define P11_DEBUG_FLAG P11_DEBUG_LIB +#include "debug.h" +#include "library.h" + +#include <assert.h> +#include <stdarg.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +#define P11_MAX_MESSAGE 512 + +typedef struct { + char message[P11_MAX_MESSAGE]; +#ifdef OS_WIN32 + void *last_error; +#endif +} p11_local; + +static p11_local * _p11_library_get_thread_local (void); + +p11_mutex_t p11_library_mutex; + +#ifdef OS_UNIX +pthread_once_t p11_library_once; +#endif + +static int print_messages = 1; + +void +p11_message_store (const char* msg, + size_t length) +{ + p11_local *local; + + if (length > P11_MAX_MESSAGE - 1) + length = P11_MAX_MESSAGE - 1; + + local = _p11_library_get_thread_local (); + if (local != NULL) { + memcpy (local->message, msg, length); + local->message[length] = 0; + } +} + +void +p11_message (const char* msg, + ...) +{ + char buffer[P11_MAX_MESSAGE]; + va_list va; + size_t length; + + va_start (va, msg); + length = vsnprintf (buffer, P11_MAX_MESSAGE - 1, msg, va); + va_end (va); + + /* Was it truncated? */ + if (length > P11_MAX_MESSAGE - 1) + length = P11_MAX_MESSAGE - 1; + buffer[length] = 0; + + /* If printing is not disabled, just print out */ + if (print_messages) + fprintf (stderr, "p11-kit: %s\n", buffer); + + p11_debug_message (P11_DEBUG_LIB, "message: %s", buffer); + p11_message_store (buffer, length); +} + +void +p11_message_quiet (void) +{ + p11_lock (); + print_messages = 0; + p11_unlock (); +} + +const char* +p11_message_last (void) +{ + p11_local *local; + local = _p11_library_get_thread_local (); + return local && local->message[0] ? local->message : NULL; +} + +void +p11_message_clear (void) +{ + p11_local *local; + local = _p11_library_get_thread_local (); + if (local != NULL) + local->message[0] = 0; +} + +static void +uninit_common (void) +{ + p11_debug ("uninitializing library"); +} + +#ifdef OS_UNIX + +static pthread_key_t thread_local = 0; + +static p11_local * +_p11_library_get_thread_local (void) +{ + p11_local *local; + + p11_library_init_once (); + + local = pthread_getspecific (thread_local); + if (local == NULL) { + local = calloc (1, sizeof (p11_local)); + pthread_setspecific (thread_local, local); + } + + return local; +} + +#ifdef __GNUC__ +__attribute__((constructor)) +#endif +void +p11_library_init (void) +{ + p11_debug_init (); + p11_debug ("initializing library"); + p11_mutex_init (&p11_library_mutex); + pthread_key_create (&thread_local, free); +} + +#ifdef __GNUC__ +__attribute__((destructor)) +#endif +void +p11_library_uninit (void) +{ + uninit_common (); + + /* Some cleanup to pacify valgrind */ + free (pthread_getspecific (thread_local)); + pthread_setspecific (thread_local, NULL); + + pthread_key_delete (thread_local); + p11_mutex_uninit (&p11_library_mutex); +} + +#endif /* OS_UNIX */ + +#ifdef OS_WIN32 + +static DWORD thread_local = TLS_OUT_OF_INDEXES; + +BOOL WINAPI DllMain (HINSTANCE, DWORD, LPVOID); + +static p11_local * +_p11_library_get_thread_local (void) +{ + LPVOID data; + + if (thread_local == TLS_OUT_OF_INDEXES) + return NULL; + + data = TlsGetValue (thread_local); + if (data == NULL) { + data = LocalAlloc (LPTR, sizeof (p11_local)); + TlsSetValue (thread_local, data); + } + + return (p11_local *)data; +} + +void +p11_library_init (void) +{ + p11_debug_init (); + p11_debug ("initializing library"); + p11_mutex_init (&p11_library_mutex); + thread_local = TlsAlloc (); +} + +static void +free_tls_value (LPVOID data) +{ + p11_local *local = data; + if (local == NULL) + return; + if (local->last_error) + LocalFree (local->last_error); + LocalFree (data); +} + +void +p11_library_uninit (void) +{ + LPVOID data; + + uninit_common (); + + if (thread_local != TLS_OUT_OF_INDEXES) { + data = TlsGetValue (thread_local); + free_tls_value (data); + TlsFree (thread_local); + } + _p11_mutex_uninit (&p11_library_mutex); +} + + +BOOL WINAPI +DllMain (HINSTANCE instance, + DWORD reason, + LPVOID reserved) +{ + LPVOID data; + + switch (reason) { + case DLL_PROCESS_ATTACH: + p11_library_init (); + if (thread_local == TLS_OUT_OF_INDEXES) { + p11_debug ("couldn't setup tls"); + return FALSE; + } + break; + + case DLL_THREAD_DETACH: + if (thread_local != TLS_OUT_OF_INDEXES) { + p11_debug ("thread stopped, freeing tls"); + data = TlsGetValue (thread_local); + free_tls_value (data); + } + break; + + case DLL_PROCESS_DETACH: + p11_library_uninit (); + break; + + default: + break; + } + + return TRUE; +} + +#endif /* OS_WIN32 */ diff --git a/common/library.h b/common/library.h new file mode 100644 index 0000000..00e9c2c --- /dev/null +++ b/common/library.h @@ -0,0 +1,80 @@ +/* + * 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. + * + * + * CONTRIBUTORS + * Stef Walter <stef@memberwebs.com> + */ + +#ifndef P11_LIBRARY_H_ +#define P11_LIBRARY_H_ + +#include "config.h" +#include "compat.h" + +#include <sys/types.h> + +extern p11_mutex_t p11_library_mutex; + +#define p11_lock() p11_mutex_lock (&p11_library_mutex); + +#define p11_unlock() p11_mutex_unlock (&p11_library_mutex); + +void p11_message (const char* msg, + ...) GNUC_PRINTF (1, 2); + +void p11_message_store (const char* msg, + size_t length); + +const char * p11_message_last (void); + +void p11_message_clear (void); + +void p11_message_quiet (void); + +#ifdef OS_WIN32 + +/* No implementation, because done by DllMain */ +#define p11_library_init_once() + +#else /* !OS_WIN32 */ +extern pthread_once_t p11_library_once; + +#define p11_library_init_once() \ + pthread_once (&p11_library_once, p11_library_init); + +#endif /* !OS_WIN32 */ + +void p11_library_init (void); + +void p11_library_uninit (void); + +#endif /* P11_LIBRARY_H_ */ diff --git a/common/tests/Makefile.am b/common/tests/Makefile.am new file mode 100644 index 0000000..11f2369 --- /dev/null +++ b/common/tests/Makefile.am @@ -0,0 +1,32 @@ + +include $(top_srcdir)/build/Makefile.tests + +NULL = + +COMMON = $(top_srcdir)/common + +INCLUDES = \ + -I$(top_srcdir) \ + -I$(srcdir)/.. \ + -I$(COMMON) \ + $(CUTEST_CFLAGS) + +LDADD = $(CUTEST_LIBS) + +CHECK_PROGS = \ + test-dict \ + test-array \ + $(NULL) + +test_dict_SOURCES = \ + test-dict.c \ + $(COMMON)/dict.c + +test_array_SOURCES = \ + test-array.c \ + $(COMMON)/array.c + +noinst_PROGRAMS = \ + $(CHECK_PROGS) + +TESTS = $(CHECK_PROGS:=$(EXEEXT)) diff --git a/common/tests/test-array.c b/common/tests/test-array.c new file mode 100644 index 0000000..a52f3b5 --- /dev/null +++ b/common/tests/test-array.c @@ -0,0 +1,194 @@ +/* + * 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 "CuTest.h" + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +#include "array.h" + +static void +test_p11_array_create (CuTest *tc) +{ + p11_array *array; + + array = p11_array_new (NULL); + CuAssertPtrNotNull (tc, array); + p11_array_free (array); +} + +static void +test_p11_array_free_null (CuTest *tc) +{ + p11_array_free (NULL); +} + +static void +destroy_value (void *data) +{ + int *value = data; + *value = 2; +} + +static void +test_p11_array_free_destroys (CuTest *tc) +{ + p11_array *array; + int value = 0; + + array = p11_array_new (destroy_value); + CuAssertPtrNotNull (tc, array); + if (!p11_array_push (array, &value)) + CuFail (tc, "should not be reached"); + p11_array_free (array); + + CuAssertIntEquals (tc, 2, value); +} + +static void +test_p11_array_add (CuTest *tc) +{ + char *value = "VALUE"; + p11_array *array; + + array = p11_array_new (NULL); + if (!p11_array_push (array, value)) + CuFail (tc, "should not be reached"); + + CuAssertIntEquals (tc, 1, array->num); + CuAssertPtrEquals (tc, array->elem[0], value); + + p11_array_free (array); +} + +static void +test_p11_array_add_remove (CuTest *tc) +{ + char *value = "VALUE"; + p11_array *array; + + array = p11_array_new (NULL); + if (!p11_array_push (array, value)) + CuFail (tc, "should not be reached"); + + CuAssertIntEquals (tc, 1, array->num); + + CuAssertPtrEquals (tc, array->elem[0], value); + + p11_array_remove (array, 0); + + CuAssertIntEquals (tc, 0, array->num); + + p11_array_free (array); +} + +static void +test_p11_array_remove_destroys (CuTest *tc) +{ + p11_array *array; + int value = 0; + + array = p11_array_new (destroy_value); + if (!p11_array_push (array, &value)) + CuFail (tc, "should not be reached"); + + p11_array_remove (array, 0); + + CuAssertIntEquals (tc, 2, value); + + /* should not be destroyed again */ + value = 0; + + p11_array_free (array); + + CuAssertIntEquals (tc, 0, value); +} + +static void +test_p11_array_remove_and_count (CuTest *tc) +{ + p11_array *array; + int *value; + int i; + + array = p11_array_new (free); + + CuAssertIntEquals (tc, 0, array->num); + + for (i = 0; i < 20000; ++i) { + value = malloc (sizeof (int)); + *value = i; + if (!p11_array_push (array, value)) + CuFail (tc, "should not be reached"); + CuAssertIntEquals (tc, i + 1, array->num); + } + + for (i = 10; i < 20000; ++i) { + p11_array_remove (array, 10); + CuAssertIntEquals (tc, 20010 - (i + 1), array->num); + } + + CuAssertIntEquals (tc, 10, array->num); + + p11_array_free (array); +} + +int +main (void) +{ + CuString *output = CuStringNew (); + CuSuite* suite = CuSuiteNew (); + int ret; + + SUITE_ADD_TEST (suite, test_p11_array_create); + SUITE_ADD_TEST (suite, test_p11_array_add); + SUITE_ADD_TEST (suite, test_p11_array_add_remove); + SUITE_ADD_TEST (suite, test_p11_array_remove_destroys); + SUITE_ADD_TEST (suite, test_p11_array_remove_and_count); + SUITE_ADD_TEST (suite, test_p11_array_free_null); + SUITE_ADD_TEST (suite, test_p11_array_free_destroys); + + CuSuiteRun (suite); + CuSuiteSummary (suite, output); + CuSuiteDetails (suite, output); + printf ("%s\n", output->buffer); + ret = suite->failCount; + CuSuiteDelete (suite); + CuStringDelete (output); + + return ret; +} diff --git a/tests/hash-test.c b/common/tests/test-dict.c index cf77094..3af3daa 100644 --- a/tests/hash-test.c +++ b/common/tests/test-dict.c @@ -40,22 +40,22 @@ #include <stdio.h> #include <string.h> -#include "hashmap.h" +#include "dict.h" static void test_create (CuTest *tc) { - hashmap *map; + p11_dict *map; - map = _p11_hash_create (_p11_hash_direct_hash, _p11_hash_direct_equal, NULL, NULL); + map = p11_dict_new (p11_dict_direct_hash, p11_dict_direct_equal, NULL, NULL); CuAssertPtrNotNull (tc, map); - _p11_hash_free (map); + p11_dict_free (map); } static void test_free_null (CuTest *tc) { - _p11_hash_free (NULL); + p11_dict_free (NULL); } typedef struct { @@ -68,7 +68,7 @@ key_hash (const void *ptr) { const Key *k = ptr; assert (!k->freed); - return _p11_hash_intptr_hash (&k->value); + return p11_dict_intptr_hash (&k->value); } static int @@ -79,7 +79,7 @@ key_equal (const void *one, const Key *k2 = two; assert (!k1->freed); assert (!k2->freed); - return _p11_hash_intptr_equal (&k1->value, &k2->value); + return p11_dict_intptr_equal (&k1->value, &k2->value); } static void @@ -100,15 +100,15 @@ value_destroy (void *data) static void test_free_destroys (CuTest *tc) { - hashmap *map; + p11_dict *map; Key key = { 8, 0 }; int value = 0; - map = _p11_hash_create (key_hash, key_equal, key_destroy, value_destroy); + map = p11_dict_new (key_hash, key_equal, key_destroy, value_destroy); CuAssertPtrNotNull (tc, map); - if (!_p11_hash_set (map, &key, &value)) + if (!p11_dict_set (map, &key, &value)) CuFail (tc, "should not be reached"); - _p11_hash_free (map); + p11_dict_free (map); CuAssertIntEquals (tc, 1, key.freed); CuAssertIntEquals (tc, 2, value); @@ -117,30 +117,30 @@ test_free_destroys (CuTest *tc) static void test_iterate (CuTest *tc) { - hashmap *map; - hashiter iter; + p11_dict *map; + p11_dictiter iter; int key = 1; int value = 2; void *pkey; void *pvalue; int ret; - map = _p11_hash_create (_p11_hash_direct_hash, _p11_hash_direct_equal, NULL, NULL); + map = p11_dict_new (p11_dict_direct_hash, p11_dict_direct_equal, NULL, NULL); CuAssertPtrNotNull (tc, map); - if (!_p11_hash_set (map, &key, &value)) + if (!p11_dict_set (map, &key, &value)) CuFail (tc, "should not be reached"); - _p11_hash_iterate (map, &iter); + p11_dict_iterate (map, &iter); - ret = _p11_hash_next (&iter, &pkey, &pvalue); + ret = p11_dict_next (&iter, &pkey, &pvalue); CuAssertIntEquals (tc, 1, ret); CuAssertPtrEquals (tc, pkey, &key); CuAssertPtrEquals (tc, pvalue, &value); - ret = _p11_hash_next (&iter, &pkey, &pvalue); + ret = p11_dict_next (&iter, &pkey, &pvalue); CuAssertIntEquals (tc, 0, ret); - _p11_hash_free (map); + p11_dict_free (map); } static void @@ -149,14 +149,14 @@ test_set_get (CuTest *tc) char *key = "KEY"; char *value = "VALUE"; char *check; - hashmap *map; + p11_dict *map; - map = _p11_hash_create (_p11_hash_string_hash, _p11_hash_string_equal, NULL, NULL); - _p11_hash_set (map, key, value); - check = _p11_hash_get (map, key); + map = p11_dict_new (p11_dict_str_hash, p11_dict_str_equal, NULL, NULL); + p11_dict_set (map, key, value); + check = p11_dict_get (map, key); CuAssertPtrEquals (tc, check, value); - _p11_hash_free (map); + p11_dict_free (map); } static void @@ -165,26 +165,26 @@ test_set_get_remove (CuTest *tc) char *key = "KEY"; char *value = "VALUE"; char *check; - hashmap *map; + p11_dict *map; int ret; - map = _p11_hash_create (_p11_hash_string_hash, _p11_hash_string_equal, NULL, NULL); + map = p11_dict_new (p11_dict_str_hash, p11_dict_str_equal, NULL, NULL); - if (!_p11_hash_set (map, key, value)) + if (!p11_dict_set (map, key, value)) CuFail (tc, "should not be reached"); - check = _p11_hash_get (map, key); + check = p11_dict_get (map, key); CuAssertPtrEquals (tc, check, value); - ret = _p11_hash_remove (map, key); + ret = p11_dict_remove (map, key); CuAssertIntEquals (tc, ret, 1); - ret = _p11_hash_remove (map, key); + ret = p11_dict_remove (map, key); CuAssertIntEquals (tc, ret, 0); - check = _p11_hash_get (map, key); + check = p11_dict_get (map, key); CuAssert (tc, "should be null", check == NULL); - _p11_hash_free (map); + p11_dict_free (map); } static void @@ -193,35 +193,35 @@ test_set_clear (CuTest *tc) char *key = "KEY"; char *value = "VALUE"; char *check; - hashmap *map; + p11_dict *map; - map = _p11_hash_create (_p11_hash_direct_hash, _p11_hash_direct_equal, NULL, NULL); + map = p11_dict_new (p11_dict_direct_hash, p11_dict_direct_equal, NULL, NULL); - if (!_p11_hash_set (map, key, value)) + if (!p11_dict_set (map, key, value)) CuFail (tc, "should not be reached"); - _p11_hash_clear (map); + p11_dict_clear (map); - check = _p11_hash_get (map, key); + check = p11_dict_get (map, key); CuAssert (tc, "should be null", check == NULL); - _p11_hash_free (map); + p11_dict_free (map); } static void test_remove_destroys (CuTest *tc) { - hashmap *map; + p11_dict *map; Key key = { 8, 0 }; int value = 0; int ret; - map = _p11_hash_create (key_hash, key_equal, key_destroy, value_destroy); + map = p11_dict_new (key_hash, key_equal, key_destroy, value_destroy); CuAssertPtrNotNull (tc, map); - if (!_p11_hash_set (map, &key, &value)) + if (!p11_dict_set (map, &key, &value)) CuFail (tc, "should not be reached"); - ret = _p11_hash_remove (map, &key); + ret = p11_dict_remove (map, &key); CuAssertIntEquals (tc, ret, 1); CuAssertIntEquals (tc, 1, key.freed); CuAssertIntEquals (tc, 2, value); @@ -230,7 +230,7 @@ test_remove_destroys (CuTest *tc) key.freed = 0; value = 0; - ret = _p11_hash_remove (map, &key); + ret = p11_dict_remove (map, &key); CuAssertIntEquals (tc, ret, 0); CuAssertIntEquals (tc, 0, key.freed); CuAssertIntEquals (tc, 0, value); @@ -239,7 +239,7 @@ test_remove_destroys (CuTest *tc) key.freed = 0; value = 0; - _p11_hash_free (map); + p11_dict_free (map); CuAssertIntEquals (tc, 0, key.freed); CuAssertIntEquals (tc, 0, value); @@ -248,21 +248,21 @@ test_remove_destroys (CuTest *tc) static void test_set_destroys (CuTest *tc) { - hashmap *map; + p11_dict *map; Key key = { 8, 0 }; Key key2 = { 8, 0 }; int value, value2; int ret; - map = _p11_hash_create (key_hash, key_equal, key_destroy, value_destroy); + map = p11_dict_new (key_hash, key_equal, key_destroy, value_destroy); CuAssertPtrNotNull (tc, map); - if (!_p11_hash_set (map, &key, &value)) + if (!p11_dict_set (map, &key, &value)) CuFail (tc, "should not be reached"); key.freed = key2.freed = value = value2 = 0; /* Setting same key and value, should not be destroyed */ - ret = _p11_hash_set (map, &key, &value); + ret = p11_dict_set (map, &key, &value); CuAssertIntEquals (tc, ret, 1); CuAssertIntEquals (tc, 0, key.freed); CuAssertIntEquals (tc, 0, key2.freed); @@ -272,7 +272,7 @@ test_set_destroys (CuTest *tc) key.freed = key2.freed = value = value2 = 0; /* Setting a new key same value, key should be destroyed */ - ret = _p11_hash_set (map, &key2, &value); + ret = p11_dict_set (map, &key2, &value); CuAssertIntEquals (tc, ret, 1); CuAssertIntEquals (tc, 1, key.freed); CuAssertIntEquals (tc, 0, key2.freed); @@ -282,7 +282,7 @@ test_set_destroys (CuTest *tc) key.freed = key2.freed = value = value2 = 0; /* Setting same key, new value, value should be destroyed */ - ret = _p11_hash_set (map, &key2, &value2); + ret = p11_dict_set (map, &key2, &value2); CuAssertIntEquals (tc, ret, 1); CuAssertIntEquals (tc, 0, key.freed); CuAssertIntEquals (tc, 0, key2.freed); @@ -292,7 +292,7 @@ test_set_destroys (CuTest *tc) key.freed = key2.freed = value = value2 = 0; /* Setting new key new value, both should be destroyed */ - ret = _p11_hash_set (map, &key, &value); + ret = p11_dict_set (map, &key, &value); CuAssertIntEquals (tc, ret, 1); CuAssertIntEquals (tc, 0, key.freed); CuAssertIntEquals (tc, 1, key2.freed); @@ -301,7 +301,7 @@ test_set_destroys (CuTest *tc) key.freed = key2.freed = value = value2 = 0; - _p11_hash_free (map); + p11_dict_free (map); CuAssertIntEquals (tc, 1, key.freed); CuAssertIntEquals (tc, 2, value); CuAssertIntEquals (tc, 0, key2.freed); @@ -312,16 +312,16 @@ test_set_destroys (CuTest *tc) static void test_clear_destroys (CuTest *tc) { - hashmap *map; + p11_dict *map; Key key = { 18, 0 }; int value = 0; - map = _p11_hash_create (key_hash, key_equal, key_destroy, value_destroy); + map = p11_dict_new (key_hash, key_equal, key_destroy, value_destroy); CuAssertPtrNotNull (tc, map); - if (!_p11_hash_set (map, &key, &value)) + if (!p11_dict_set (map, &key, &value)) CuFail (tc, "should not be reached"); - _p11_hash_clear (map); + p11_dict_clear (map); CuAssertIntEquals (tc, 1, key.freed); CuAssertIntEquals (tc, 2, value); @@ -329,7 +329,7 @@ test_clear_destroys (CuTest *tc) key.freed = 0; value = 0; - _p11_hash_clear (map); + p11_dict_clear (map); CuAssertIntEquals (tc, 0, key.freed); CuAssertIntEquals (tc, 0, value); @@ -337,7 +337,7 @@ test_clear_destroys (CuTest *tc) key.freed = 0; value = 0; - _p11_hash_free (map); + p11_dict_free (map); CuAssertIntEquals (tc, 0, key.freed); CuAssertIntEquals (tc, 0, value); @@ -353,83 +353,83 @@ test_hash_intptr_with_collisions (const void *data) static void test_hash_add_check_lots_and_collisions (CuTest *tc) { - hashmap *map; + p11_dict *map; int *value; int i; - map = _p11_hash_create (test_hash_intptr_with_collisions, - _p11_hash_intptr_equal, NULL, free); + map = p11_dict_new (test_hash_intptr_with_collisions, + p11_dict_intptr_equal, NULL, free); for (i = 0; i < 20000; ++i) { value = malloc (sizeof (int)); *value = i; - if (!_p11_hash_set (map, value, value)) + if (!p11_dict_set (map, value, value)) CuFail (tc, "should not be reached"); } for (i = 0; i < 20000; ++i) { - value = _p11_hash_get (map, &i); + value = p11_dict_get (map, &i); CuAssertPtrNotNull (tc, value); CuAssertIntEquals (tc, i, *value); } - _p11_hash_free (map); + p11_dict_free (map); } static void test_hash_count (CuTest *tc) { - hashmap *map; + p11_dict *map; int *value; int i, ret; - map = _p11_hash_create (_p11_hash_intptr_hash, _p11_hash_intptr_equal, NULL, free); + map = p11_dict_new (p11_dict_intptr_hash, p11_dict_intptr_equal, NULL, free); - CuAssertIntEquals (tc, 0, _p11_hash_size (map)); + CuAssertIntEquals (tc, 0, p11_dict_size (map)); for (i = 0; i < 20000; ++i) { value = malloc (sizeof (int)); *value = i; - if (!_p11_hash_set (map, value, value)) + if (!p11_dict_set (map, value, value)) CuFail (tc, "should not be reached"); - CuAssertIntEquals (tc, i + 1, _p11_hash_size (map)); + CuAssertIntEquals (tc, i + 1, p11_dict_size (map)); } for (i = 0; i < 20000; ++i) { - ret = _p11_hash_remove (map, &i); + ret = p11_dict_remove (map, &i); CuAssertIntEquals (tc, 1, ret); - CuAssertIntEquals (tc, 20000 - (i + 1), _p11_hash_size (map)); + CuAssertIntEquals (tc, 20000 - (i + 1), p11_dict_size (map)); } - _p11_hash_clear (map); - CuAssertIntEquals (tc, 0, _p11_hash_size (map)); + p11_dict_clear (map); + CuAssertIntEquals (tc, 0, p11_dict_size (map)); - _p11_hash_free (map); + p11_dict_free (map); } static void test_hash_ulongptr (CuTest *tc) { - hashmap *map; + p11_dict *map; unsigned long *value; unsigned long i; - map = _p11_hash_create (_p11_hash_ulongptr_hash, _p11_hash_ulongptr_equal, NULL, free); + map = p11_dict_new (p11_dict_ulongptr_hash, p11_dict_ulongptr_equal, NULL, free); for (i = 0; i < 20000; ++i) { value = malloc (sizeof (unsigned long)); *value = i; - if (!_p11_hash_set (map, value, value)) + if (!p11_dict_set (map, value, value)) CuFail (tc, "should not be reached"); } for (i = 0; i < 20000; ++i) { - value = _p11_hash_get (map, &i); + value = p11_dict_get (map, &i); CuAssertPtrNotNull (tc, value); CuAssertIntEquals (tc, i, *value); } - _p11_hash_free (map); + p11_dict_free (map); } int diff --git a/configure.ac b/configure.ac index b0bf648..bb72391 100644 --- a/configure.ac +++ b/configure.ac @@ -22,7 +22,7 @@ P11KIT_AGE=0 # ------------------------------------------------------------------------------ AC_CONFIG_HEADERS([config.h]) -AC_CONFIG_MACRO_DIR([m4]) +AC_CONFIG_MACRO_DIR([build/m4]) AM_INIT_AUTOMAKE([1.10]) AM_SANITY_CHECK AM_MAINTAINER_MODE([enable]) @@ -282,13 +282,16 @@ AC_DEFINE_UNQUOTED(SHLEXT, ["$SHLEXT"], [File extension for shared libraries]) AC_SUBST(SHLEXT) AC_CONFIG_FILES([Makefile + build/Makefile + common/Makefile + common/tests/Makefile doc/Makefile doc/version.xml po/Makefile.in p11-kit/Makefile + p11-kit/tests/Makefile p11-kit/p11-kit-1.pc p11-kit/pkcs11.conf.example - tests/Makefile tools/Makefile ]) AC_OUTPUT diff --git a/doc/Makefile.am b/doc/Makefile.am index 33690d5..76e202f 100644 --- a/doc/Makefile.am +++ b/doc/Makefile.am @@ -42,7 +42,16 @@ CFILE_GLOB=$(top_srcdir)/p11-kit/*.c # Header files to ignore when scanning. # e.g. IGNORE_HFILES=gtkdebug.h gtkintl.h -IGNORE_HFILES= private.h pkcs11.h conf.h debug.h hashmap.h util.h ptr-array.h compat.h +IGNORE_HFILES= \ + private.h \ + pkcs11.h \ + conf.h \ + debug.h \ + dict.h \ + util.h \ + array.h \ + compat.h \ + mock-module.h # Images to copy into HTML directory. # e.g. HTML_IMAGES=$(top_srcdir)/gtk/stock-icons/stock_about_24.png diff --git a/p11-kit/Makefile.am b/p11-kit/Makefile.am index 2985d85..11d5fae 100644 --- a/p11-kit/Makefile.am +++ b/p11-kit/Makefile.am @@ -1,8 +1,12 @@ NULL = +SUBDIRS = . tests + +COMMON = $(top_srcdir)/common + INCLUDES = \ -I$(top_srcdir) \ - -I$(top_srcdir)/common \ + -I$(COMMON) \ -DP11_KIT_FUTURE_UNSTABLE_API \ $(NULL) @@ -15,19 +19,24 @@ inc_HEADERS = \ pkcs11.h MODULE_SRCS = \ - util.c util.h \ + util.c \ conf.c conf.h \ - debug.c debug.h \ - hashmap.c hashmap.h \ modules.c \ pin.c \ proxy.c \ private.h \ - ptr-array.c ptr-array.h \ messages.c \ uri.c \ - $(top_srcdir)/common/compat.c \ - $(top_srcdir)/common/compat.h \ + $(COMMON)/array.c \ + $(COMMON)/array.h \ + $(COMMON)/compat.c \ + $(COMMON)/compat.h \ + $(COMMON)/debug.c \ + $(COMMON)/debug.h \ + $(COMMON)/dict.c \ + $(COMMON)/dict.h \ + $(COMMON)/library.c \ + $(COMMON)/library.h \ $(inc_HEADERS) lib_LTLIBRARIES = \ @@ -62,31 +71,34 @@ libp11_kit_testable_la_SOURCES = $(MODULE_SRCS) if OS_WIN32 libp11_kit_testable_la_CFLAGS = \ - -DP11_SYSTEM_CONFIG_FILE=\""$(abs_top_srcdir)/tests/files/system-pkcs11.conf"\" \ - -DP11_SYSTEM_CONFIG_MODULES=\""$(abs_top_srcdir)/tests/files/system-modules/win32"\" \ - -DP11_USER_CONFIG_FILE=\""$(abs_top_srcdir)/tests/files/user-pkcs11.conf"\" \ - -DP11_USER_CONFIG_MODULES=\""$(abs_top_srcdir)/tests/files/user-modules/win32"\" \ - -DP11_MODULE_PATH=\""$(abs_top_builddir)/tests/.libs"\" \ + -DP11_SYSTEM_CONFIG_FILE=\""$(abs_top_srcdir)/p11-kit/tests/files/system-pkcs11.conf"\" \ + -DP11_SYSTEM_CONFIG_MODULES=\""$(abs_top_srcdir)/p11-kit/tests/files/system-modules/win32"\" \ + -DP11_USER_CONFIG_FILE=\""$(abs_top_srcdir)/p11-kit/tests/files/user-pkcs11.conf"\" \ + -DP11_USER_CONFIG_MODULES=\""$(abs_top_srcdir)/p11-kit/tests/files/user-modules/win32"\" \ + -DP11_MODULE_PATH=\""$(abs_top_builddir)/p11-kit/tests/.libs"\" \ $(NULL) else libp11_kit_testable_la_CFLAGS = \ - -DP11_SYSTEM_CONFIG_FILE=\""$(abs_top_srcdir)/tests/files/system-pkcs11.conf"\" \ - -DP11_SYSTEM_CONFIG_MODULES=\""$(abs_top_srcdir)/tests/files/system-modules"\" \ - -DP11_USER_CONFIG_FILE=\""$(abs_top_srcdir)/tests/files/user-pkcs11.conf"\" \ - -DP11_USER_CONFIG_MODULES=\""$(abs_top_srcdir)/tests/files/user-modules"\" \ - -DP11_MODULE_PATH=\""$(abs_top_builddir)/tests/.libs"\" \ + -DP11_SYSTEM_CONFIG_FILE=\""$(abs_top_srcdir)/p11-kit/tests/files/system-pkcs11.conf"\" \ + -DP11_SYSTEM_CONFIG_MODULES=\""$(abs_top_srcdir)/p11-kit/tests/files/system-modules"\" \ + -DP11_USER_CONFIG_FILE=\""$(abs_top_srcdir)/p11-kit/tests/files/user-pkcs11.conf"\" \ + -DP11_USER_CONFIG_MODULES=\""$(abs_top_srcdir)/p11-kit/tests/files/user-modules"\" \ + -DP11_MODULE_PATH=\""$(abs_top_builddir)/p11-kit/tests/.libs"\" \ $(NULL) endif libp11_kit_compat_la_SOURCES = \ - debug.c debug.h \ messages.c \ - util.c util.h \ - $(top_srcdir)/common/compat.c \ - $(top_srcdir)/common/compat.h + $(COMMON)/compat.c \ + $(COMMON)/compat.h \ + $(COMMON)/debug.c \ + $(COMMON)/debug.h \ + $(COMMON)/library.c \ + $(COMMON)/library.h \ + $(NULL) pkgconfigdir = $(libdir)/pkgconfig pkgconfig_DATA = p11-kit-1.pc diff --git a/p11-kit/conf.c b/p11-kit/conf.c index 8dda39f..1d92942 100644 --- a/p11-kit/conf.c +++ b/p11-kit/conf.c @@ -38,10 +38,10 @@ #include "config.h" #include "conf.h" -#define DEBUG_FLAG DEBUG_CONF +#define P11_DEBUG_FLAG P11_DEBUG_CONF #include "debug.h" +#include "library.h" #include "private.h" -#include "util.h" #include <sys/param.h> #include <sys/stat.h> @@ -167,20 +167,20 @@ read_config_file (const char* filename, int flags) error = errno; if ((flags & CONF_IGNORE_MISSING) && (error == ENOENT || error == ENOTDIR)) { - _p11_debug ("config file does not exist"); + p11_debug ("config file does not exist"); config = strdup ("\n"); return_val_if_fail (config != NULL, NULL); return config; } else if ((flags & CONF_IGNORE_ACCESS_DENIED) && (error == EPERM || error == EACCES)) { - _p11_debug ("config file is inaccessible"); + p11_debug ("config file is inaccessible"); config = strdup ("\n"); return_val_if_fail (config != NULL, NULL); return config; } - _p11_message ("couldn't open config file: %s: %s", filename, - strerror (error)); + p11_message ("couldn't open config file: %s: %s", filename, + strerror (error)); errno = error; return NULL; } @@ -190,14 +190,14 @@ read_config_file (const char* filename, int flags) (len = ftell (f)) == -1 || fseek (f, 0, SEEK_SET) == -1) { error = errno; - _p11_message ("couldn't seek config file: %s", filename); + p11_message ("couldn't seek config file: %s", filename); errno = error; return NULL; } config = malloc (len + 2); if (config == NULL) { - _p11_message ("config file is too large to read into memory: %lu", len); + p11_message ("config file is too large to read into memory: %lu", len); errno = ENOMEM; return NULL; } @@ -205,7 +205,7 @@ read_config_file (const char* filename, int flags) /* And read in one block */ if (fread (config, 1, len, f) != len) { error = errno; - _p11_message ("couldn't read config file: %s", filename); + p11_message ("couldn't read config file: %s", filename); errno = error; return NULL; } @@ -223,34 +223,35 @@ read_config_file (const char* filename, int flags) } int -_p11_conf_merge_defaults (hashmap *map, hashmap *defaults) +_p11_conf_merge_defaults (p11_dict *map, + p11_dict *defaults) { - hashiter iter; + p11_dictiter iter; void *key; void *value; - _p11_hash_iterate (defaults, &iter); - while (_p11_hash_next (&iter, &key, &value)) { + p11_dict_iterate (defaults, &iter); + while (p11_dict_next (&iter, &key, &value)) { /* Only override if not set */ - if (_p11_hash_get (map, key)) + if (p11_dict_get (map, key)) continue; key = strdup (key); return_val_if_fail (key != NULL, -1); value = strdup (value); return_val_if_fail (key != NULL, -1); - if (!_p11_hash_set (map, key, value)) + if (!p11_dict_set (map, key, value)) return_val_if_reached (-1); } return 0; } -hashmap * +p11_dict * _p11_conf_parse_file (const char* filename, int flags) { char *name; char *value; - hashmap *map = NULL; + p11_dict *map = NULL; char *data; char *next; char *end; @@ -258,14 +259,14 @@ _p11_conf_parse_file (const char* filename, int flags) assert (filename); - _p11_debug ("reading config file: %s", filename); + p11_debug ("reading config file: %s", filename); /* Adds an extra newline to end of file */ data = read_config_file (filename, flags); if (!data) return NULL; - map = _p11_hash_create (_p11_hash_string_hash, _p11_hash_string_equal, free, free); + map = p11_dict_new (p11_dict_str_hash, p11_dict_str_equal, free, free); return_val_if_fail (map != NULL, NULL); next = data; @@ -283,7 +284,7 @@ _p11_conf_parse_file (const char* filename, int flags) /* Look for the break between name: value on the same line */ value = name + strcspn (name, ":"); if (!*value) { - _p11_message ("%s: invalid config line: %s", filename, name); + p11_message ("%s: invalid config line: %s", filename, name); error = EINVAL; break; } @@ -301,16 +302,16 @@ _p11_conf_parse_file (const char* filename, int flags) value = strdup (value); return_val_if_fail (value != NULL, NULL); - _p11_debug ("config value: %s: %s", name, value); + p11_debug ("config value: %s: %s", name, value); - if (!_p11_hash_set (map, name, value)) + if (!p11_dict_set (map, name, value)) return_val_if_reached (NULL); } free (data); if (error != 0) { - _p11_hash_free (map); + p11_dict_free (map); map = NULL; errno = error; } @@ -339,8 +340,8 @@ expand_user_path (const char *path) pwd = getpwuid (getuid ()); if (!pwd) { error = errno; - _p11_message ("couldn't lookup home directory for user %d: %s", - getuid (), strerror (errno)); + p11_message ("couldn't lookup home directory for user %d: %s", + getuid (), strerror (errno)); errno = error; return NULL; } @@ -362,12 +363,13 @@ expand_user_path (const char *path) } static int -user_config_mode (hashmap *config, int defmode) +user_config_mode (p11_dict *config, + int defmode) { const char *mode; /* Whether we should use or override from user directory */ - mode = _p11_hash_get (config, "user-config"); + mode = p11_dict_get (config, "user-config"); if (mode == NULL) { return defmode; } else if (strequal (mode, "none")) { @@ -379,18 +381,18 @@ user_config_mode (hashmap *config, int defmode) } else if (strequal (mode, "override")) { return CONF_USER_ONLY; } else { - _p11_message ("invalid mode for 'user-config': %s", mode); + p11_message ("invalid mode for 'user-config': %s", mode); return CONF_USER_INVALID; } } -hashmap * +p11_dict * _p11_conf_load_globals (const char *system_conf, const char *user_conf, int *user_mode) { - hashmap *config = NULL; - hashmap *uconfig = NULL; - hashmap *result = NULL; + p11_dict *config = NULL; + p11_dict *uconfig = NULL; + p11_dict *result = NULL; char *path = NULL; int error = 0; int flags; @@ -447,7 +449,7 @@ _p11_conf_load_globals (const char *system_conf, const char *user_conf, /* If user config valid at all, then replace system with what we have */ if (mode != CONF_USER_NONE) { - _p11_hash_free (config); + p11_dict_free (config); config = uconfig; uconfig = NULL; } @@ -461,8 +463,8 @@ _p11_conf_load_globals (const char *system_conf, const char *user_conf, finished: free (path); - _p11_hash_free (config); - _p11_hash_free (uconfig); + p11_dict_free (config); + p11_dict_free (uconfig); errno = error; return result; } @@ -510,11 +512,11 @@ calc_name_from_filename (const char *fname) static int load_config_from_file (const char *configfile, const char *name, - hashmap *configs, + p11_dict *configs, int flags) { - hashmap *config; - hashmap *prev; + p11_dict *config; + p11_dict *prev; char *key; int error = 0; @@ -522,7 +524,7 @@ load_config_from_file (const char *configfile, key = calc_name_from_filename (name); if (key == NULL) { - _p11_message ("invalid config filename, will be ignored in the future: %s", configfile); + p11_message ("invalid config filename, will be ignored in the future: %s", configfile); key = strdup (name); return_val_if_fail (key != NULL, -1); } @@ -533,9 +535,9 @@ load_config_from_file (const char *configfile, return -1; } - prev = _p11_hash_get (configs, key); + prev = p11_dict_get (configs, key); if (prev == NULL) { - if (!_p11_hash_set (configs, key, config)) + if (!p11_dict_set (configs, key, config)) return_val_if_reached (-1); config = NULL; } else { @@ -545,7 +547,7 @@ load_config_from_file (const char *configfile, } /* If still set */ - _p11_hash_free (config); + p11_dict_free (config); if (error) { errno = error; @@ -557,7 +559,7 @@ load_config_from_file (const char *configfile, static int load_configs_from_directory (const char *directory, - hashmap *configs, + p11_dict *configs, int flags) { struct dirent *dp; @@ -568,7 +570,7 @@ load_configs_from_directory (const char *directory, char *path; int count = 0; - _p11_debug ("loading module configs in: %s", directory); + p11_debug ("loading module configs in: %s", directory); /* First we load all the modules */ dir = opendir (directory); @@ -576,15 +578,15 @@ load_configs_from_directory (const char *directory, error = errno; if ((flags & CONF_IGNORE_MISSING) && (errno == ENOENT || errno == ENOTDIR)) { - _p11_debug ("module configs do not exist"); + p11_debug ("module configs do not exist"); return 0; } else if ((flags & CONF_IGNORE_ACCESS_DENIED) && (errno == EPERM || errno == EACCES)) { - _p11_debug ("couldn't list inacessible module configs"); + p11_debug ("couldn't list inacessible module configs"); return 0; } - _p11_message ("couldn't list directory: %s: %s", directory, - strerror (error)); + p11_message ("couldn't list directory: %s: %s", directory, + strerror (error)); errno = error; return -1; } @@ -603,7 +605,7 @@ load_configs_from_directory (const char *directory, { if (stat (path, &st) < 0) { error = errno; - _p11_message ("couldn't stat path: %s", path); + p11_message ("couldn't stat path: %s", path); free (path); break; } @@ -630,17 +632,17 @@ load_configs_from_directory (const char *directory, return count; } -hashmap * +p11_dict * _p11_conf_load_modules (int mode, const char *system_dir, const char *user_dir) { - hashmap *configs; + p11_dict *configs; char *path; int error = 0; int flags; /* A hash table of name -> config */ - configs = _p11_hash_create (_p11_hash_string_hash, _p11_hash_string_equal, - free, (hash_destroy_func)_p11_hash_free); + configs = p11_dict_new (p11_dict_str_hash, p11_dict_str_equal, + free, (p11_destroyer)p11_dict_free); /* Load each user config first, if user config is allowed */ if (mode != CONF_USER_NONE) { @@ -652,7 +654,7 @@ _p11_conf_load_modules (int mode, const char *system_dir, const char *user_dir) error = errno; free (path); if (error != 0) { - _p11_hash_free (configs); + p11_dict_free (configs); errno = error; return NULL; } @@ -667,7 +669,7 @@ _p11_conf_load_modules (int mode, const char *system_dir, const char *user_dir) flags = CONF_IGNORE_MISSING; if (load_configs_from_directory (system_dir, configs, flags) < 0) { error = errno; - _p11_hash_free (configs); + p11_dict_free (configs); errno = error; return NULL; } @@ -688,8 +690,8 @@ _p11_conf_parse_boolean (const char *string, } else if (strcmp (string, "no") == 0) { return 0; } else { - _p11_message ("invalid setting '%s' defaulting to '%s'", - string, default_value ? "yes" : "no"); + p11_message ("invalid setting '%s' defaulting to '%s'", + string, default_value ? "yes" : "no"); return default_value; } } diff --git a/p11-kit/conf.h b/p11-kit/conf.h index 0627f14..b70666d 100644 --- a/p11-kit/conf.h +++ b/p11-kit/conf.h @@ -36,7 +36,7 @@ #ifndef __CONF_H__ #define __CONF_H__ -#include "hashmap.h" +#include "dict.h" enum { CONF_IGNORE_MISSING = 0x01, @@ -50,19 +50,21 @@ enum { CONF_USER_ONLY }; -int _p11_conf_merge_defaults (hashmap *config, - hashmap *defaults); +int _p11_conf_merge_defaults (p11_dict *config, + p11_dict *defaults); /* Returns a hash of char *key -> char *value */ -hashmap * _p11_conf_parse_file (const char *filename, +p11_dict * _p11_conf_parse_file (const char *filename, int flags); /* Returns a hash of char *key -> char *value */ -hashmap * _p11_conf_load_globals (const char *system_conf, const char *user_conf, +p11_dict * _p11_conf_load_globals (const char *system_conf, + const char *user_conf, int *user_mode); /* Returns a hash of char* name -> hash_t *config */ -hashmap * _p11_conf_load_modules (int user_mode, const char *system_dir, +p11_dict * _p11_conf_load_modules (int user_mode, + const char *system_dir, const char *user_dir); int _p11_conf_parse_boolean (const char *string, diff --git a/p11-kit/modules.c b/p11-kit/modules.c index c097c4b..e4b0f66 100644 --- a/p11-kit/modules.c +++ b/p11-kit/modules.c @@ -36,9 +36,10 @@ #include "config.h" #include "conf.h" -#define DEBUG_FLAG DEBUG_LIB +#define P11_DEBUG_FLAG P11_DEBUG_LIB #include "debug.h" -#include "hashmap.h" +#include "dict.h" +#include "library.h" #include "pkcs11.h" #include "p11-kit.h" #include "private.h" @@ -101,15 +102,15 @@ typedef struct _Module { /* Registered modules */ char *name; - hashmap *config; + p11_dict *config; /* Loaded modules */ dl_module_t dl_module; /* Initialization, mutex must be held */ - mutex_t initialize_mutex; + p11_mutex_t initialize_mutex; int initialize_called; - thread_id_t initialize_thread; + p11_thread_id_t initialize_thread; } Module; /* @@ -117,8 +118,8 @@ typedef struct _Module { * we can audit thread safety easier. */ static struct _Shared { - hashmap *modules; - hashmap *config; + p11_dict *modules; + p11_dict *config; } gl = { NULL, NULL }; /* ----------------------------------------------------------------------------- @@ -128,14 +129,14 @@ static struct _Shared { static CK_RV create_mutex (CK_VOID_PTR_PTR mut) { - mutex_t *pmutex; + p11_mutex_t *pmutex; return_val_if_fail (mut != NULL, CKR_ARGUMENTS_BAD); - pmutex = malloc (sizeof (mutex_t)); + pmutex = malloc (sizeof (p11_mutex_t)); return_val_if_fail (pmutex != NULL, CKR_HOST_MEMORY); - _p11_mutex_init (pmutex); + p11_mutex_init (pmutex); *mut = pmutex; return CKR_OK; } @@ -143,11 +144,11 @@ create_mutex (CK_VOID_PTR_PTR mut) static CK_RV destroy_mutex (CK_VOID_PTR mut) { - mutex_t *pmutex = mut; + p11_mutex_t *pmutex = mut; return_val_if_fail (mut != NULL, CKR_MUTEX_BAD); - _p11_mutex_uninit (pmutex); + p11_mutex_uninit (pmutex); free (pmutex); return CKR_OK; } @@ -155,22 +156,22 @@ destroy_mutex (CK_VOID_PTR mut) static CK_RV lock_mutex (CK_VOID_PTR mut) { - mutex_t *pmutex = mut; + p11_mutex_t *pmutex = mut; return_val_if_fail (mut != NULL, CKR_MUTEX_BAD); - _p11_mutex_lock (pmutex); + p11_mutex_lock (pmutex); return CKR_OK; } static CK_RV unlock_mutex (CK_VOID_PTR mut) { - mutex_t *pmutex = mut; + p11_mutex_t *pmutex = mut; return_val_if_fail (mut != NULL, CKR_MUTEX_BAD); - _p11_mutex_unlock (pmutex); + p11_mutex_unlock (pmutex); return CKR_OK; } @@ -189,10 +190,10 @@ free_module_unlocked (void *data) assert (mod->ref_count == 0); if (mod->dl_module) - _p11_module_close (mod->dl_module); + p11_module_close (mod->dl_module); - _p11_mutex_uninit (&mod->initialize_mutex); - _p11_hash_free (mod->config); + p11_mutex_uninit (&mod->initialize_mutex); + p11_dict_free (mod->config); free (mod->name); free (mod); } @@ -210,7 +211,7 @@ alloc_module_unlocked (void) mod->init_args.LockMutex = lock_mutex; mod->init_args.UnlockMutex = unlock_mutex; mod->init_args.flags = CKF_OS_LOCKING_OK; - _p11_mutex_init (&mod->initialize_mutex); + p11_mutex_init (&mod->initialize_mutex); return mod; } @@ -257,27 +258,27 @@ dlopen_and_get_function_list (Module *mod, const char *path) assert (mod); assert (path); - mod->dl_module = _p11_module_open (path); + mod->dl_module = p11_module_open (path); if (mod->dl_module == NULL) { - _p11_message ("couldn't load module: %s: %s", path, _p11_module_error ()); + p11_message ("couldn't load module: %s: %s", path, p11_module_error ()); return CKR_GENERAL_ERROR; } - gfl = _p11_module_symbol (mod->dl_module, "C_GetFunctionList"); + gfl = p11_module_symbol (mod->dl_module, "C_GetFunctionList"); if (!gfl) { - _p11_message ("couldn't find C_GetFunctionList entry point in module: %s: %s", - path, _p11_module_error ()); + p11_message ("couldn't find C_GetFunctionList entry point in module: %s: %s", + path, p11_module_error ()); return CKR_GENERAL_ERROR; } rv = gfl (&mod->funcs); if (rv != CKR_OK) { - _p11_message ("call to C_GetFunctiontList failed in module: %s: %s", - path, p11_kit_strerror (rv)); + p11_message ("call to C_GetFunctiontList failed in module: %s: %s", + path, p11_kit_strerror (rv)); return rv; } - _p11_debug ("opened module: %s", path); + p11_debug ("opened module: %s", path); return CKR_OK; } @@ -298,14 +299,14 @@ load_module_from_file_unlocked (const char *path, Module **result) } /* Do we have a previous one like this, if so ignore load */ - prev = _p11_hash_get (gl.modules, mod->funcs); + prev = p11_dict_get (gl.modules, mod->funcs); if (prev != NULL) { - _p11_debug ("duplicate module %s, using previous", path); + p11_debug ("duplicate module %s, using previous", path); free_module_unlocked (mod); mod = prev; - } else if (!_p11_hash_set (gl.modules, mod->funcs, mod)) { + } else if (!p11_dict_set (gl.modules, mod->funcs, mod)) { return_val_if_reached (CKR_HOST_MEMORY); } @@ -320,7 +321,7 @@ expand_module_path (const char *filename) char *path; if (is_relative_path (filename)) { - _p11_debug ("module path is relative, loading from: %s", P11_MODULE_PATH); + p11_debug ("module path is relative, loading from: %s", P11_MODULE_PATH); path = build_path (P11_MODULE_PATH, filename); } else { path = strdup (filename); @@ -355,15 +356,15 @@ is_string_in_list (const char *list, static int is_module_enabled_unlocked (const char *name, - hashmap *config) + p11_dict *config) { const char *progname; const char *enable_in; const char *disable_in; int enable = 0; - enable_in = _p11_hash_get (config, "enable-in"); - disable_in = _p11_hash_get (config, "disable-in"); + enable_in = p11_dict_get (config, "enable-in"); + disable_in = p11_dict_get (config, "disable-in"); /* Defaults to enabled if neither of these are set */ if (!enable_in && !disable_in) @@ -371,13 +372,13 @@ is_module_enabled_unlocked (const char *name, progname = _p11_get_progname_unlocked (); if (enable_in && disable_in) - _p11_message ("module '%s' has both enable-in and disable-in options", name); + p11_message ("module '%s' has both enable-in and disable-in options", name); if (enable_in) enable = (progname != NULL && is_string_in_list (enable_in, progname)); else if (disable_in) enable = (progname == NULL || !is_string_in_list (disable_in, progname)); - _p11_debug ("%s module '%s' running in '%s'", + p11_debug ("%s module '%s' running in '%s'", enable ? "enabled" : "disabled", name, progname ? progname : "(null)"); @@ -385,7 +386,8 @@ is_module_enabled_unlocked (const char *name, } static CK_RV -take_config_and_load_module_unlocked (char **name, hashmap **config) +take_config_and_load_module_unlocked (char **name, + p11_dict **config) { Module *mod, *prev; const char *module_filename; @@ -401,9 +403,9 @@ take_config_and_load_module_unlocked (char **name, hashmap **config) if (!is_module_enabled_unlocked (*name, *config)) return CKR_OK; - module_filename = _p11_hash_get (*config, "module"); + module_filename = p11_dict_get (*config, "module"); if (module_filename == NULL) { - _p11_debug ("no module path for module, skipping: %s", *name); + p11_debug ("no module path for module, skipping: %s", *name); return CKR_OK; } @@ -414,7 +416,7 @@ take_config_and_load_module_unlocked (char **name, hashmap **config) return_val_if_fail (key != NULL, CKR_HOST_MEMORY); /* The hash map will take ownership of the variable */ - if (!_p11_hash_set (*config, key, path)) + if (!p11_dict_set (*config, key, path)) return_val_if_reached (CKR_HOST_MEMORY); mod = alloc_module_unlocked (); @@ -437,9 +439,9 @@ take_config_and_load_module_unlocked (char **name, hashmap **config) * 'x-init-reserved' setting in the config. This only works with specific * PKCS#11 modules, and is non-standard use of that field. */ - mod->init_args.pReserved = _p11_hash_get (mod->config, "x-init-reserved"); + mod->init_args.pReserved = p11_dict_get (mod->config, "x-init-reserved"); - prev = _p11_hash_get (gl.modules, mod->funcs); + prev = p11_dict_get (gl.modules, mod->funcs); /* If same module was loaded previously, just take over config */ if (prev && !prev->name && !prev->config) { @@ -451,12 +453,12 @@ take_config_and_load_module_unlocked (char **name, hashmap **config) /* Ignore duplicate module */ } else if (prev) { - _p11_message ("duplicate configured module: %s: %s", mod->name, path); + p11_message ("duplicate configured module: %s: %s", mod->name, path); free_module_unlocked (mod); /* Add this new module to our hash table */ } else { - if (!_p11_hash_set (gl.modules, mod->funcs, mod)) + if (!p11_dict_set (gl.modules, mod->funcs, mod)) return_val_if_reached (CKR_HOST_MEMORY); } @@ -466,11 +468,11 @@ take_config_and_load_module_unlocked (char **name, hashmap **config) static CK_RV load_registered_modules_unlocked (void) { - hashiter iter; - hashmap *configs; + p11_dictiter iter; + p11_dict *configs; void *key; char *name; - hashmap *config; + p11_dict *config; int mode; CK_RV rv; int critical; @@ -489,7 +491,7 @@ load_registered_modules_unlocked (void) P11_USER_CONFIG_MODULES); if (configs == NULL) { rv = CKR_GENERAL_ERROR; - _p11_hash_free (config); + p11_dict_free (config); return rv; } @@ -500,13 +502,13 @@ load_registered_modules_unlocked (void) * Now go through each config and turn it into a module. As we iterate * we steal the values of the config. */ - _p11_hash_iterate (configs, &iter); - while (_p11_hash_next (&iter, &key, NULL)) { - if (!_p11_hash_steal (configs, key, (void**)&name, (void**)&config)) + p11_dict_iterate (configs, &iter); + while (p11_dict_next (&iter, &key, NULL)) { + if (!p11_dict_steal (configs, key, (void**)&name, (void**)&config)) assert_not_reached (); /* Is this a critical module, should abort loading of others? */ - critical = _p11_conf_parse_boolean (_p11_hash_get (config, "critical"), 0); + critical = _p11_conf_parse_boolean (p11_dict_get (config, "critical"), 0); rv = take_config_and_load_module_unlocked (&name, &config); @@ -514,12 +516,12 @@ load_registered_modules_unlocked (void) * These variables will be cleared if ownership is transeferred * by the above function call. */ - _p11_hash_free (config); + p11_dict_free (config); if (critical && rv != CKR_OK) { - _p11_message ("aborting initialization because module '%s' was marked as critical", - name); - _p11_hash_free (configs); + p11_message ("aborting initialization because module '%s' was marked as critical", + name); + p11_dict_free (configs); free (name); return rv; } @@ -527,7 +529,7 @@ load_registered_modules_unlocked (void) free (name); } - _p11_hash_free (configs); + p11_dict_free (configs); return CKR_OK; } @@ -535,13 +537,13 @@ static CK_RV initialize_module_unlocked_reentrant (Module *mod) { CK_RV rv = CKR_OK; - thread_id_t self; + p11_thread_id_t self; assert (mod); - self = _p11_thread_id_self (); + self = p11_thread_id_self (); if (mod->initialize_thread == self) { - _p11_message ("p11-kit initialization called recursively"); + p11_message ("p11-kit initialization called recursively"); return CKR_FUNCTION_FAILED; } @@ -553,22 +555,22 @@ initialize_module_unlocked_reentrant (Module *mod) mod->initialize_thread = self; /* Change over to the module specific mutex */ - _p11_mutex_lock (&mod->initialize_mutex); - _p11_unlock (); + p11_mutex_lock (&mod->initialize_mutex); + p11_unlock (); if (!mod->initialize_called) { assert (mod->funcs); if (mod->funcs == &_p11_proxy_function_list) { - _p11_message ("refusing to load the p11-kit-proxy.so module as a registered module"); + p11_message ("refusing to load the p11-kit-proxy.so module as a registered module"); rv = CKR_FUNCTION_FAILED; } else { - _p11_debug ("C_Initialize: calling"); + p11_debug ("C_Initialize: calling"); rv = mod->funcs->C_Initialize (&mod->init_args); - _p11_debug ("C_Initialize: result: %lu", rv); + p11_debug ("C_Initialize: result: %lu", rv); } /* Module was initialized and C_Finalize should be called */ @@ -580,8 +582,8 @@ initialize_module_unlocked_reentrant (Module *mod) rv = CKR_OK; } - _p11_mutex_unlock (&mod->initialize_mutex); - _p11_lock (); + p11_mutex_unlock (&mod->initialize_mutex); + p11_lock (); /* Don't claim reference if failed */ if (rv != CKR_OK) @@ -596,20 +598,20 @@ initialize_module_unlocked_reentrant (Module *mod) static void reinitialize_after_fork (void) { - hashiter iter; + p11_dictiter iter; Module *mod; - _p11_debug ("forked"); + p11_debug ("forked"); - _p11_lock (); + p11_lock (); if (gl.modules) { - _p11_hash_iterate (gl.modules, &iter); - while (_p11_hash_next (&iter, NULL, (void **)&mod)) + p11_dict_iterate (gl.modules, &iter); + while (p11_dict_next (&iter, NULL, (void **)&mod)) mod->initialize_called = 0; } - _p11_unlock (); + p11_unlock (); _p11_kit_proxy_after_fork (); } @@ -622,8 +624,8 @@ init_globals_unlocked (void) static int once = 0; if (!gl.modules) { - gl.modules = _p11_hash_create (_p11_hash_direct_hash, _p11_hash_direct_equal, - NULL, free_module_unlocked); + gl.modules = p11_dict_new (p11_dict_direct_hash, p11_dict_direct_equal, + NULL, free_module_unlocked); return_val_if_fail (gl.modules != NULL, CKR_HOST_MEMORY); } @@ -642,18 +644,18 @@ static void free_modules_when_no_refs_unlocked (void) { Module *mod; - hashiter iter; + p11_dictiter iter; /* Check if any modules have a ref count */ - _p11_hash_iterate (gl.modules, &iter); - while (_p11_hash_next (&iter, NULL, (void **)&mod)) { + p11_dict_iterate (gl.modules, &iter); + while (p11_dict_next (&iter, NULL, (void **)&mod)) { if (mod->ref_count) return; } - _p11_hash_free (gl.modules); + p11_dict_free (gl.modules); gl.modules = NULL; - _p11_hash_free (gl.config); + p11_dict_free (gl.config); gl.config = NULL; } @@ -679,8 +681,8 @@ finalize_module_unlocked_reentrant (Module *mod) */ ++mod->ref_count; - _p11_mutex_lock (&mod->initialize_mutex); - _p11_unlock (); + p11_mutex_lock (&mod->initialize_mutex); + p11_unlock (); if (mod->initialize_called) { @@ -690,8 +692,8 @@ finalize_module_unlocked_reentrant (Module *mod) mod->initialize_called = 0; } - _p11_mutex_unlock (&mod->initialize_mutex); - _p11_lock (); + p11_mutex_unlock (&mod->initialize_mutex); + p11_lock (); /* Match the increment above */ --mod->ref_count; @@ -704,12 +706,12 @@ static Module* find_module_for_name_unlocked (const char *name) { Module *mod; - hashiter iter; + p11_dictiter iter; assert (name); - _p11_hash_iterate (gl.modules, &iter); - while (_p11_hash_next (&iter, NULL, (void **)&mod)) + p11_dict_iterate (gl.modules, &iter); + while (p11_dict_next (&iter, NULL, (void **)&mod)) if (mod->ref_count && mod->name && strcmp (name, mod->name) == 0) return mod; return NULL; @@ -719,7 +721,7 @@ CK_RV _p11_kit_initialize_registered_unlocked_reentrant (void) { Module *mod; - hashiter iter; + p11_dictiter iter; int critical; CK_RV rv; @@ -729,8 +731,8 @@ _p11_kit_initialize_registered_unlocked_reentrant (void) rv = load_registered_modules_unlocked (); if (rv == CKR_OK) { - _p11_hash_iterate (gl.modules, &iter); - while (_p11_hash_next (&iter, NULL, (void **)&mod)) { + p11_dict_iterate (gl.modules, &iter); + while (p11_dict_next (&iter, NULL, (void **)&mod)) { /* Skip all modules that aren't registered */ if (mod->name == NULL || !is_module_enabled_unlocked (mod->name, mod->config)) @@ -743,12 +745,12 @@ _p11_kit_initialize_registered_unlocked_reentrant (void) * then this, should abort loading of others. */ if (rv != CKR_OK) { - _p11_message ("failed to initialize module: %s: %s", - mod->name, p11_kit_strerror (rv)); + p11_message ("failed to initialize module: %s: %s", + mod->name, p11_kit_strerror (rv)); - critical = _p11_conf_parse_boolean (_p11_hash_get (mod->config, "critical"), 0); + critical = _p11_conf_parse_boolean (p11_dict_get (mod->config, "critical"), 0); if (!critical) { - _p11_debug ("ignoring failure, non-critical module: %s", mod->name); + p11_debug ("ignoring failure, non-critical module: %s", mod->name); rv = CKR_OK; } } @@ -780,27 +782,27 @@ p11_kit_initialize_registered (void) { CK_RV rv; - _p11_library_init_once (); + p11_library_init_once (); /* WARNING: This function must be reentrant */ - _p11_debug ("in"); + p11_debug ("in"); - _p11_lock (); + p11_lock (); - _p11_kit_clear_message (); + p11_message_clear (); /* WARNING: Reentrancy can occur here */ rv = _p11_kit_initialize_registered_unlocked_reentrant (); _p11_kit_default_message (rv); - _p11_unlock (); + p11_unlock (); /* Cleanup any partial initialization */ if (rv != CKR_OK) p11_kit_finalize_registered (); - _p11_debug ("out: %lu", rv); + p11_debug ("out: %lu", rv); return rv; } @@ -808,7 +810,7 @@ CK_RV _p11_kit_finalize_registered_unlocked_reentrant (void) { Module *mod; - hashiter iter; + p11_dictiter iter; Module **to_finalize; int i, count; @@ -817,20 +819,20 @@ _p11_kit_finalize_registered_unlocked_reentrant (void) /* WARNING: This function must be reentrant */ - to_finalize = calloc (_p11_hash_size (gl.modules), sizeof (Module *)); + to_finalize = calloc (p11_dict_size (gl.modules), sizeof (Module *)); if (!to_finalize) return CKR_HOST_MEMORY; count = 0; - _p11_hash_iterate (gl.modules, &iter); - while (_p11_hash_next (&iter, NULL, (void **)&mod)) { + p11_dict_iterate (gl.modules, &iter); + while (p11_dict_next (&iter, NULL, (void **)&mod)) { /* Skip all modules that aren't registered */ if (mod->name) to_finalize[count++] = mod; } - _p11_debug ("finalizing %d modules", count); + p11_debug ("finalizing %d modules", count); for (i = 0; i < count; ++i) { /* WARNING: Reentrant calls can occur here */ @@ -867,23 +869,23 @@ p11_kit_finalize_registered (void) { CK_RV rv; - _p11_library_init_once (); + p11_library_init_once (); /* WARNING: This function must be reentrant */ - _p11_debug ("in"); + p11_debug ("in"); - _p11_lock (); + p11_lock (); - _p11_kit_clear_message (); + p11_message_clear (); /* WARNING: Reentrant calls can occur here */ rv = _p11_kit_finalize_registered_unlocked_reentrant (); _p11_kit_default_message (rv); - _p11_unlock (); + p11_unlock (); - _p11_debug ("out: %lu", rv); + p11_debug ("out: %lu", rv); return rv; } @@ -892,15 +894,15 @@ _p11_kit_registered_modules_unlocked (void) { CK_FUNCTION_LIST_PTR_PTR result = NULL; Module *mod; - hashiter iter; + p11_dictiter iter; int i = 0; if (gl.modules) { - result = calloc (_p11_hash_size (gl.modules) + 1, sizeof (CK_FUNCTION_LIST_PTR)); + result = calloc (p11_dict_size (gl.modules) + 1, sizeof (CK_FUNCTION_LIST_PTR)); return_val_if_fail (result != NULL, NULL); - _p11_hash_iterate (gl.modules, &iter); - while (_p11_hash_next (&iter, NULL, (void **)&mod)) { + p11_dict_iterate (gl.modules, &iter); + while (p11_dict_next (&iter, NULL, (void **)&mod)) { /* * We don't include unreferenced modules. We don't include @@ -940,15 +942,15 @@ p11_kit_registered_modules (void) { CK_FUNCTION_LIST_PTR_PTR result; - _p11_library_init_once (); + p11_library_init_once (); - _p11_lock (); + p11_lock (); - _p11_kit_clear_message (); + p11_message_clear (); result = _p11_kit_registered_modules_unlocked (); - _p11_unlock (); + p11_unlock (); return result; } @@ -974,17 +976,17 @@ p11_kit_registered_module_to_name (CK_FUNCTION_LIST_PTR module) return_val_if_fail (module != NULL, NULL); - _p11_library_init_once (); + p11_library_init_once (); - _p11_lock (); + p11_lock (); - _p11_kit_clear_message (); + p11_message_clear (); - mod = module && gl.modules ? _p11_hash_get (gl.modules, module) : NULL; + mod = module && gl.modules ? p11_dict_get (gl.modules, module) : NULL; if (mod && mod->name) name = strdup (mod->name); - _p11_unlock (); + p11_unlock (); return name; } @@ -1007,9 +1009,9 @@ p11_kit_registered_name_to_module (const char *name) return_val_if_fail (name != NULL, NULL); - _p11_lock (); + p11_lock (); - _p11_kit_clear_message (); + p11_message_clear (); if (gl.modules) { mod = find_module_for_name_unlocked (name); @@ -1017,7 +1019,7 @@ p11_kit_registered_name_to_module (const char *name) module = mod->funcs; } - _p11_unlock (); + p11_unlock (); return module; } @@ -1040,32 +1042,32 @@ p11_kit_registered_option (CK_FUNCTION_LIST_PTR module, const char *field) { Module *mod = NULL; char *option = NULL; - hashmap *config = NULL; + p11_dict *config = NULL; return_val_if_fail (field != NULL, NULL); - _p11_library_init_once (); + p11_library_init_once (); - _p11_lock (); + p11_lock (); - _p11_kit_clear_message (); + p11_message_clear (); if (module == NULL) { config = gl.config; } else { - mod = gl.modules ? _p11_hash_get (gl.modules, module) : NULL; + mod = gl.modules ? p11_dict_get (gl.modules, module) : NULL; if (mod) config = mod->config; } if (config && field) { - option = _p11_hash_get (config, field); + option = p11_dict_get (config, field); if (option) option = strdup (option); } - _p11_unlock (); + p11_unlock (); return option; } @@ -1109,21 +1111,21 @@ p11_kit_initialize_module (CK_FUNCTION_LIST_PTR module) return_val_if_fail (module != NULL, CKR_ARGUMENTS_BAD); - _p11_library_init_once (); + p11_library_init_once (); /* WARNING: This function must be reentrant for the same arguments */ - _p11_debug ("in"); + p11_debug ("in"); - _p11_lock (); + p11_lock (); - _p11_kit_clear_message (); + p11_message_clear (); rv = init_globals_unlocked (); if (rv == CKR_OK) { - mod = _p11_hash_get (gl.modules, module); + mod = p11_dict_get (gl.modules, module); if (mod == NULL) { - _p11_debug ("allocating new module"); + p11_debug ("allocating new module"); allocated = mod = alloc_module_unlocked (); if (mod == NULL) rv = CKR_HOST_MEMORY; @@ -1133,7 +1135,7 @@ p11_kit_initialize_module (CK_FUNCTION_LIST_PTR module) /* If this was newly allocated, add it to the list */ if (rv == CKR_OK && allocated) { - if (_p11_hash_set (gl.modules, allocated->funcs, allocated)) + if (p11_dict_set (gl.modules, allocated->funcs, allocated)) allocated = NULL; else rv = CKR_HOST_MEMORY; @@ -1158,9 +1160,9 @@ p11_kit_initialize_module (CK_FUNCTION_LIST_PTR module) _p11_kit_default_message (rv); - _p11_unlock (); + p11_unlock (); - _p11_debug ("out: %lu", rv); + p11_debug ("out: %lu", rv); return rv; } @@ -1196,18 +1198,18 @@ p11_kit_finalize_module (CK_FUNCTION_LIST_PTR module) return_val_if_fail (module != NULL, CKR_ARGUMENTS_BAD); - _p11_library_init_once (); + p11_library_init_once (); /* WARNING: This function must be reentrant for the same arguments */ - _p11_debug ("in"); + p11_debug ("in"); - _p11_lock (); + p11_lock (); - _p11_kit_clear_message (); + p11_message_clear (); - mod = gl.modules ? _p11_hash_get (gl.modules, module) : NULL; + mod = gl.modules ? p11_dict_get (gl.modules, module) : NULL; if (mod == NULL) { - _p11_debug ("module not found"); + p11_debug ("module not found"); rv = CKR_ARGUMENTS_BAD; } else { /* WARNING: Rentrancy can occur here */ @@ -1216,9 +1218,9 @@ p11_kit_finalize_module (CK_FUNCTION_LIST_PTR module) _p11_kit_default_message (rv); - _p11_unlock (); + p11_unlock (); - _p11_debug ("out: %lu", rv); + p11_debug ("out: %lu", rv); return rv; } @@ -1265,14 +1267,14 @@ p11_kit_load_initialize_module (const char *module_path, return_val_if_fail (module_path != NULL, CKR_ARGUMENTS_BAD); return_val_if_fail (module != NULL, CKR_ARGUMENTS_BAD); - _p11_library_init_once (); + p11_library_init_once (); /* WARNING: This function must be reentrant for the same arguments */ - _p11_debug ("in: %s", module_path); + p11_debug ("in: %s", module_path); - _p11_lock (); + p11_lock (); - _p11_kit_clear_message (); + p11_message_clear (); rv = init_globals_unlocked (); if (rv == CKR_OK) { @@ -1298,8 +1300,8 @@ p11_kit_load_initialize_module (const char *module_path, _p11_kit_default_message (rv); - _p11_unlock (); + p11_unlock (); - _p11_debug ("out: %lu", rv); + p11_debug ("out: %lu", rv); return rv; } diff --git a/p11-kit/pin.c b/p11-kit/pin.c index 96511e6..a8be64b 100644 --- a/p11-kit/pin.c +++ b/p11-kit/pin.c @@ -34,15 +34,15 @@ #include "config.h" -#define DEBUG_FLAG DEBUG_PIN +#define P11_DEBUG_FLAG P11_DEBUG_PIN #include "debug.h" -#include "hashmap.h" +#include "dict.h" +#include "library.h" #include "pkcs11.h" #include "p11-kit.h" #include "pin.h" #include "private.h" -#include "ptr-array.h" -#include "util.h" +#include "array.h" #include <assert.h> #include <errno.h> @@ -145,7 +145,7 @@ typedef struct _PinCallback { * we can audit thread safety easier. */ static struct _Shared { - hashmap *pin_sources; + p11_dict *pin_sources; } gl = { NULL }; static void* @@ -174,29 +174,29 @@ static int register_callback_unlocked (const char *pin_source, PinCallback *cb) { - ptr_array_t *callbacks = NULL; + p11_array *callbacks = NULL; char *name; name = strdup (pin_source); return_val_if_fail (name != NULL, -1); if (gl.pin_sources == NULL) { - gl.pin_sources = _p11_hash_create (_p11_hash_string_hash, _p11_hash_string_equal, - free, (hash_destroy_func)_p11_ptr_array_free); + gl.pin_sources = p11_dict_new (p11_dict_str_hash, p11_dict_str_equal, + free, (p11_destroyer)p11_array_free); return_val_if_fail (gl.pin_sources != NULL, -1); } if (gl.pin_sources != NULL) - callbacks = _p11_hash_get (gl.pin_sources, name); + callbacks = p11_dict_get (gl.pin_sources, name); if (callbacks == NULL) { - callbacks = _p11_ptr_array_create (unref_pin_callback); + callbacks = p11_array_new (unref_pin_callback); return_val_if_fail (callbacks != NULL, -1); - if (!_p11_hash_set (gl.pin_sources, name, callbacks)) + if (!p11_dict_set (gl.pin_sources, name, callbacks)) return_val_if_reached (-1); } - if (_p11_ptr_array_add (callbacks, cb) < 0) + if (p11_array_push (callbacks, cb) < 0) return_val_if_reached (-1); return 0; @@ -240,11 +240,11 @@ p11_kit_pin_register_callback (const char *pin_source, cb->user_data = callback_data; cb->destroy = callback_destroy; - _p11_lock (); + p11_lock (); ret = register_callback_unlocked (pin_source, cb); - _p11_unlock (); + p11_unlock (); return ret; } @@ -266,37 +266,37 @@ p11_kit_pin_unregister_callback (const char *pin_source, void *callback_data) { PinCallback *cb; - ptr_array_t *callbacks; + p11_array *callbacks; unsigned int i; return_if_fail (pin_source != NULL); return_if_fail (callback != NULL); - _p11_lock (); + p11_lock (); if (gl.pin_sources) { - callbacks = _p11_hash_get (gl.pin_sources, pin_source); + callbacks = p11_dict_get (gl.pin_sources, pin_source); if (callbacks) { - for (i = 0; i < _p11_ptr_array_count (callbacks); i++) { - cb = _p11_ptr_array_at (callbacks, i); + for (i = 0; i < callbacks->num; i++) { + cb = callbacks->elem[i]; if (cb->func == callback && cb->user_data == callback_data) { - _p11_ptr_array_remove (callbacks, i); + p11_array_remove (callbacks, i); break; } } - if (_p11_ptr_array_count (callbacks) == 0) - _p11_hash_remove (gl.pin_sources, pin_source); + if (callbacks->num == 0) + p11_dict_remove (gl.pin_sources, pin_source); } /* When there are no more pin sources, get rid of the hash table */ - if (_p11_hash_size (gl.pin_sources) == 0) { - _p11_hash_free (gl.pin_sources); + if (p11_dict_size (gl.pin_sources) == 0) { + p11_dict_free (gl.pin_sources); gl.pin_sources = NULL; } } - _p11_unlock (); + p11_unlock (); } /** @@ -338,31 +338,31 @@ p11_kit_pin_request (const char *pin_source, { PinCallback **snapshot = NULL; unsigned int snapshot_count = 0; - ptr_array_t *callbacks; + p11_array *callbacks; P11KitPin *pin; unsigned int i; return_val_if_fail (pin_source != NULL, NULL); - _p11_lock (); + p11_lock (); /* Find and ref the pin source data */ if (gl.pin_sources) { - callbacks = _p11_hash_get (gl.pin_sources, pin_source); + callbacks = p11_dict_get (gl.pin_sources, pin_source); /* If we didn't find any snapshots try the global ones */ if (callbacks == NULL) - callbacks = _p11_hash_get (gl.pin_sources, P11_KIT_PIN_FALLBACK); + callbacks = p11_dict_get (gl.pin_sources, P11_KIT_PIN_FALLBACK); if (callbacks != NULL) { - snapshot = (PinCallback**)_p11_ptr_array_snapshot (callbacks); - snapshot_count = _p11_ptr_array_count (callbacks); - for (i = 0; i < snapshot_count; i++) + snapshot = memdup (callbacks->elem, sizeof (void *) * callbacks->num); + snapshot_count = callbacks->num; + for (i = 0; snapshot && i < snapshot_count; i++) ref_pin_callback (snapshot[i]); } } - _p11_unlock (); + p11_unlock (); if (snapshot == NULL) return NULL; @@ -372,11 +372,11 @@ p11_kit_pin_request (const char *pin_source, snapshot[i - 1]->user_data); } - _p11_lock (); + p11_lock (); for (i = 0; i < snapshot_count; i++) unref_pin_callback (snapshot[i]); free (snapshot); - _p11_unlock (); + p11_unlock (); return pin; } @@ -666,11 +666,11 @@ p11_kit_pin_get_length (P11KitPin *pin) P11KitPin * p11_kit_pin_ref (P11KitPin *pin) { - _p11_lock (); + p11_lock (); pin->ref_count++; - _p11_unlock (); + p11_unlock (); return pin; } @@ -687,12 +687,12 @@ p11_kit_pin_unref (P11KitPin *pin) { int last = 0; - _p11_lock (); + p11_lock (); last = (pin->ref_count == 1); pin->ref_count--; - _p11_unlock (); + p11_unlock (); if (last) { if (pin->destroy) diff --git a/p11-kit/private.h b/p11-kit/private.h index 61cd142..7348e43 100644 --- a/p11-kit/private.h +++ b/p11-kit/private.h @@ -37,48 +37,9 @@ #include "compat.h" #include "pkcs11.h" -#include "util.h" - -extern mutex_t _p11_mutex; - -#define P11_MAX_MESSAGE 512 - -typedef struct { - char message[P11_MAX_MESSAGE]; -#ifdef OS_WIN32 - void *last_error; -#endif -} p11_local; - -#define _p11_lock() _p11_mutex_lock (&_p11_mutex); - -#define _p11_unlock() _p11_mutex_unlock (&_p11_mutex); - -void _p11_message (const char* msg, - ...) GNUC_PRINTF (1, 2); - -p11_local * _p11_library_get_thread_local (void); - -#ifdef OS_WIN32 - -/* No implementation, because done by DllMain */ -#define _p11_library_init_once() - -#else /* !OS_WIN32 */ - -extern pthread_once_t _p11_once; - -#define _p11_library_init_once() \ - pthread_once (&_p11_once, _p11_library_init); - -#endif /* !OS_WIN32 */ extern CK_FUNCTION_LIST _p11_proxy_function_list; -void _p11_library_init (void); - -void _p11_library_uninit (void); - CK_FUNCTION_LIST_PTR_PTR _p11_kit_registered_modules_unlocked (void); CK_RV _p11_kit_initialize_registered_unlocked_reentrant (void); @@ -91,8 +52,6 @@ CK_RV _p11_load_config_files_unlocked (const char *sys const char *user_conf, int *user_mode); -void _p11_kit_clear_message (void); - void _p11_kit_default_message (CK_RV rv); const char * _p11_get_progname_unlocked (void); diff --git a/p11-kit/proxy.c b/p11-kit/proxy.c index c8ba593..35f0c82 100644 --- a/p11-kit/proxy.c +++ b/p11-kit/proxy.c @@ -35,14 +35,14 @@ #include "config.h" -#define DEBUG_FLAG DEBUG_PROXY +#define P11_DEBUG_FLAG P11_DEBUG_PROXY #include "debug.h" -#include "hashmap.h" +#include "dict.h" +#include "library.h" #define CRYPTOKI_EXPORTS #include "pkcs11.h" #include "p11-kit.h" #include "private.h" -#include "util.h" #include <sys/types.h> #include <assert.h> @@ -77,7 +77,7 @@ static struct _Shared { Mapping *mappings; unsigned int n_mappings; int mappings_refs; - hashmap *sessions; + p11_dict *sessions; CK_ULONG last_handle; } gl = { NULL, 0, 0, NULL, FIRST_HANDLE }; @@ -115,7 +115,7 @@ map_slot_to_real (CK_SLOT_ID_PTR slot, Mapping *mapping) assert (mapping); - _p11_lock (); + p11_lock (); if (!gl.mappings) rv = CKR_CRYPTOKI_NOT_INITIALIZED; @@ -124,7 +124,7 @@ map_slot_to_real (CK_SLOT_ID_PTR slot, Mapping *mapping) if (rv == CKR_OK) *slot = mapping->real_slot; - _p11_unlock (); + p11_unlock (); return rv; } @@ -138,13 +138,13 @@ map_session_to_real (CK_SESSION_HANDLE_PTR handle, Mapping *mapping, Session *se assert (handle); assert (mapping); - _p11_lock (); + p11_lock (); if (!gl.sessions) { rv = CKR_CRYPTOKI_NOT_INITIALIZED; } else { assert (gl.sessions); - sess = _p11_hash_get (gl.sessions, handle); + sess = p11_dict_get (gl.sessions, handle); if (sess != NULL) { *handle = sess->real_session; rv = map_slot_unlocked (sess->wrap_slot, mapping); @@ -155,7 +155,7 @@ map_session_to_real (CK_SESSION_HANDLE_PTR handle, Mapping *mapping, Session *se } } - _p11_unlock (); + p11_unlock (); return rv; } @@ -174,7 +174,7 @@ finalize_mappings_unlocked (void) gl.n_mappings = 0; /* no more sessions */ - _p11_hash_free (gl.sessions); + p11_dict_free (gl.sessions); gl.sessions = NULL; } @@ -187,13 +187,13 @@ _p11_kit_proxy_after_fork (void) * up any mappings and all */ - _p11_lock (); + p11_lock (); gl.mappings_refs = 1; finalize_mappings_unlocked (); assert (!gl.mappings); - _p11_unlock (); + p11_unlock (); } static CK_RV @@ -201,7 +201,7 @@ proxy_C_Finalize (CK_VOID_PTR reserved) { CK_RV rv; - _p11_debug ("in"); + p11_debug ("in"); /* WARNING: This function must be reentrant */ @@ -209,7 +209,7 @@ proxy_C_Finalize (CK_VOID_PTR reserved) rv = CKR_ARGUMENTS_BAD; } else { - _p11_lock (); + p11_lock (); /* WARNING: Reentrancy can occur here */ rv = _p11_kit_finalize_registered_unlocked_reentrant (); @@ -221,10 +221,10 @@ proxy_C_Finalize (CK_VOID_PTR reserved) if (gl.mappings_refs) finalize_mappings_unlocked (); - _p11_unlock (); + p11_unlock (); } - _p11_debug ("out: %lu", rv); + p11_debug ("out: %lu", rv); return rv; } @@ -248,7 +248,7 @@ initialize_mappings_unlocked_reentrant (void) assert (funcs); slots = NULL; - _p11_unlock (); + p11_unlock (); /* Ask module for its slots */ rv = (funcs->C_GetSlotList) (FALSE, NULL, &count); @@ -257,7 +257,7 @@ initialize_mappings_unlocked_reentrant (void) rv = (funcs->C_GetSlotList) (FALSE, slots, &count); } - _p11_lock (); + p11_lock (); if (rv != CKR_OK) { free (slots); @@ -291,7 +291,7 @@ initialize_mappings_unlocked_reentrant (void) assert (!gl.sessions); gl.mappings = mappings; gl.n_mappings = n_mappings; - gl.sessions = _p11_hash_create (_p11_hash_ulongptr_hash, _p11_hash_ulongptr_equal, NULL, free); + gl.sessions = p11_dict_new (p11_dict_ulongptr_hash, p11_dict_ulongptr_equal, NULL, free); ++gl.mappings_refs; /* Any cleanup necessary for failure will happen at caller */ @@ -303,13 +303,13 @@ proxy_C_Initialize (CK_VOID_PTR init_args) { CK_RV rv; - _p11_library_init_once (); + p11_library_init_once (); /* WARNING: This function must be reentrant */ - _p11_debug ("in"); + p11_debug ("in"); - _p11_lock (); + p11_lock (); /* WARNING: Reentrancy can occur here */ rv = _p11_kit_initialize_registered_unlocked_reentrant (); @@ -318,14 +318,14 @@ proxy_C_Initialize (CK_VOID_PTR init_args) if (rv == CKR_OK && gl.mappings_refs == 0) rv = initialize_mappings_unlocked_reentrant (); - _p11_unlock (); + p11_unlock (); - _p11_debug ("here"); + p11_debug ("here"); if (rv != CKR_OK) proxy_C_Finalize (NULL); - _p11_debug ("out: %lu", rv); + p11_debug ("out: %lu", rv); return rv; } @@ -334,16 +334,16 @@ proxy_C_GetInfo (CK_INFO_PTR info) { CK_RV rv = CKR_OK; - _p11_library_init_once (); + p11_library_init_once (); return_val_if_fail (info != NULL, CKR_ARGUMENTS_BAD); - _p11_lock (); + p11_lock (); if (!gl.mappings) rv = CKR_CRYPTOKI_NOT_INITIALIZED; - _p11_unlock (); + p11_unlock (); if (rv != CKR_OK) return rv; @@ -380,7 +380,7 @@ proxy_C_GetSlotList (CK_BBOOL token_present, CK_SLOT_ID_PTR slot_list, return_val_if_fail (count != NULL, CKR_ARGUMENTS_BAD); - _p11_lock (); + p11_lock (); if (!gl.mappings) { rv = CKR_CRYPTOKI_NOT_INITIALIZED; @@ -413,7 +413,7 @@ proxy_C_GetSlotList (CK_BBOOL token_present, CK_SLOT_ID_PTR slot_list, *count = index; } - _p11_unlock (); + p11_unlock (); return rv; } @@ -503,7 +503,7 @@ proxy_C_OpenSession (CK_SLOT_ID id, CK_FLAGS flags, CK_VOID_PTR user_data, rv = (map.funcs->C_OpenSession) (id, flags, user_data, callback, handle); if (rv == CKR_OK) { - _p11_lock (); + p11_lock (); if (!gl.sessions) { /* @@ -519,11 +519,11 @@ proxy_C_OpenSession (CK_SLOT_ID id, CK_FLAGS flags, CK_VOID_PTR user_data, sess->wrap_slot = map.wrap_slot; sess->real_session = *handle; sess->wrap_session = ++gl.last_handle; /* TODO: Handle wrapping, and then collisions */ - _p11_hash_set (gl.sessions, &sess->wrap_session, sess); + p11_dict_set (gl.sessions, &sess->wrap_session, sess); *handle = sess->wrap_session; } - _p11_unlock (); + p11_unlock (); } return rv; @@ -543,12 +543,12 @@ proxy_C_CloseSession (CK_SESSION_HANDLE handle) rv = (map.funcs->C_CloseSession) (handle); if (rv == CKR_OK) { - _p11_lock (); + p11_lock (); if (gl.sessions) - _p11_hash_remove (gl.sessions, &key); + p11_dict_remove (gl.sessions, &key); - _p11_unlock (); + p11_unlock (); } return rv; @@ -561,27 +561,27 @@ proxy_C_CloseAllSessions (CK_SLOT_ID id) CK_RV rv = CKR_OK; Session *sess; CK_ULONG i, count = 0; - hashiter iter; + p11_dictiter iter; - _p11_lock (); + p11_lock (); if (!gl.sessions) { rv = CKR_CRYPTOKI_NOT_INITIALIZED; } else { - to_close = calloc (sizeof (CK_SESSION_HANDLE), _p11_hash_size (gl.sessions)); + to_close = calloc (sizeof (CK_SESSION_HANDLE), p11_dict_size (gl.sessions)); if (!to_close) { rv = CKR_HOST_MEMORY; } else { - _p11_hash_iterate (gl.sessions, &iter); + p11_dict_iterate (gl.sessions, &iter); count = 0; - while (_p11_hash_next (&iter, NULL, (void**)&sess)) { + while (p11_dict_next (&iter, NULL, (void**)&sess)) { if (sess->wrap_slot == id && to_close) to_close[count++] = sess->wrap_session; } } } - _p11_unlock (); + p11_unlock (); if (rv != CKR_OK) return rv; @@ -1390,6 +1390,6 @@ __declspec(dllexport) CK_RV C_GetFunctionList (CK_FUNCTION_LIST_PTR_PTR list) { - _p11_library_init_once (); + p11_library_init_once (); return proxy_C_GetFunctionList (list); } diff --git a/tests/Makefile.am b/p11-kit/tests/Makefile.am index c25ed57..bd08426 100644 --- a/tests/Makefile.am +++ b/p11-kit/tests/Makefile.am @@ -1,20 +1,18 @@ +include $(top_srcdir)/build/Makefile.tests + INCLUDES = \ -I$(top_srcdir) \ + -I$(srcdir)/.. \ -I$(top_srcdir)/common \ - -I$(top_srcdir)/p11-kit \ - -I$(srcdir)/cutest \ - -DSRCDIR=\"$(abs_srcdir)\" \ - -DBUILDDIR=\"$(abs_builddir)\" \ - -DP11_KIT_FUTURE_UNSTABLE_API + $(CUTEST_CFLAGS) LDADD = \ $(top_builddir)/p11-kit/libp11-kit-testable.la \ + $(CUTEST_LIBS) \ $(LTLIBINTL) CHECK_PROGS = \ - hash-test \ - ptr-array-test \ progname-test \ conf-test \ uri-test \ @@ -26,19 +24,9 @@ noinst_PROGRAMS = \ print-messages \ $(CHECK_PROGS) -cutestfiles = cutest/CuTest.c cutest/CuTest.h - -conf_test_SOURCES = conf-test.c $(cutestfiles) -hash_test_SOURCES = hash-test.c $(cutestfiles) -pin_test_SOURCES = pin-test.c $(cutestfiles) -ptr_array_test_SOURCES = ptr-array-test.c $(cutestfiles) -progname_test_SOURCES = progname-test.c $(cutestfiles) -test_init_SOURCES = test-init.c $(cutestfiles) \ +test_init_SOURCES = test-init.c \ mock-module.c mock-module.h test_init_CFLAGS = $(AM_CFLAGS) -test_modules_SOURCES = test-modules.c $(cutestfiles) - -uri_test_SOURCES = uri-test.c $(cutestfiles) TESTS = $(CHECK_PROGS:=$(EXEEXT)) @@ -72,5 +60,4 @@ mock_four_la_LDFLAGS = $(mock_one_la_LDFLAGS) mock_four_la_LIBADD = $(mock_one_la_LIBADD) EXTRA_DIST = \ - cutest \ files diff --git a/tests/conf-test.c b/p11-kit/tests/conf-test.c index 98841a2..4ea1313 100644 --- a/tests/conf-test.c +++ b/p11-kit/tests/conf-test.c @@ -41,50 +41,51 @@ #include <string.h> #include "conf.h" +#include "library.h" #include "p11-kit.h" #include "private.h" static void test_parse_conf_1 (CuTest *tc) { - hashmap *map; + p11_dict *map; const char *value; map = _p11_conf_parse_file (SRCDIR "/files/test-1.conf", 0); CuAssertPtrNotNull (tc, map); - value = _p11_hash_get (map, "key1"); + value = p11_dict_get (map, "key1"); CuAssertStrEquals (tc, "value1", value); - value = _p11_hash_get (map, "with-colon"); + value = p11_dict_get (map, "with-colon"); CuAssertStrEquals (tc, "value-of-colon", value); - value = _p11_hash_get (map, "with-whitespace"); + value = p11_dict_get (map, "with-whitespace"); CuAssertStrEquals (tc, "value-with-whitespace", value); - value = _p11_hash_get (map, "embedded-comment"); + value = p11_dict_get (map, "embedded-comment"); CuAssertStrEquals (tc, "this is # not a comment", value); - _p11_hash_free (map); + p11_dict_free (map); } static void test_parse_ignore_missing (CuTest *tc) { - hashmap *map; + p11_dict *map; map = _p11_conf_parse_file (SRCDIR "/files/non-existant.conf", CONF_IGNORE_MISSING); CuAssertPtrNotNull (tc, map); - CuAssertIntEquals (tc, 0, _p11_hash_size (map)); + CuAssertIntEquals (tc, 0, p11_dict_size (map)); CuAssertPtrEquals (tc, NULL, (void*)p11_kit_message ()); - _p11_hash_free (map); + p11_dict_free (map); } static void test_parse_fail_missing (CuTest *tc) { - hashmap *map; + p11_dict *map; map = _p11_conf_parse_file (SRCDIR "/files/non-existant.conf", 0); CuAssertPtrEquals (tc, map, NULL); @@ -94,37 +95,37 @@ test_parse_fail_missing (CuTest *tc) static void test_merge_defaults (CuTest *tc) { - hashmap *values; - hashmap *defaults; + p11_dict *values; + p11_dict *defaults; - values = _p11_hash_create (_p11_hash_string_hash, _p11_hash_string_equal, free, free); - defaults = _p11_hash_create (_p11_hash_string_hash, _p11_hash_string_equal, free, free); + 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_hash_set (values, strdup ("one"), strdup ("real1")); - _p11_hash_set (values, strdup ("two"), strdup ("real2")); + p11_dict_set (values, strdup ("one"), strdup ("real1")); + p11_dict_set (values, strdup ("two"), strdup ("real2")); - _p11_hash_set (defaults, strdup ("two"), strdup ("default2")); - _p11_hash_set (defaults, strdup ("three"), strdup ("default3")); + p11_dict_set (defaults, strdup ("two"), strdup ("default2")); + p11_dict_set (defaults, strdup ("three"), strdup ("default3")); if (_p11_conf_merge_defaults (values, defaults) < 0) CuFail (tc, "should not be reached"); - _p11_hash_free (defaults); + p11_dict_free (defaults); - CuAssertStrEquals (tc, _p11_hash_get (values, "one"), "real1"); - CuAssertStrEquals (tc, _p11_hash_get (values, "two"), "real2"); - CuAssertStrEquals (tc, _p11_hash_get (values, "three"), "default3"); + CuAssertStrEquals (tc, p11_dict_get (values, "one"), "real1"); + CuAssertStrEquals (tc, p11_dict_get (values, "two"), "real2"); + CuAssertStrEquals (tc, p11_dict_get (values, "three"), "default3"); - _p11_hash_free (values); + p11_dict_free (values); } static void test_load_globals_merge (CuTest *tc) { int user_mode = -1; - hashmap *config; + p11_dict *config; - _p11_kit_clear_message (); + p11_message_clear (); config = _p11_conf_load_globals (SRCDIR "/files/test-system-merge.conf", SRCDIR "/files/test-user.conf", @@ -133,20 +134,20 @@ test_load_globals_merge (CuTest *tc) CuAssertStrEquals (tc, NULL, p11_kit_message ()); CuAssertIntEquals (tc, CONF_USER_MERGE, user_mode); - CuAssertStrEquals (tc, _p11_hash_get (config, "key1"), "system1"); - CuAssertStrEquals (tc, _p11_hash_get (config, "key2"), "user2"); - CuAssertStrEquals (tc, _p11_hash_get (config, "key3"), "user3"); + CuAssertStrEquals (tc, p11_dict_get (config, "key1"), "system1"); + CuAssertStrEquals (tc, p11_dict_get (config, "key2"), "user2"); + CuAssertStrEquals (tc, p11_dict_get (config, "key3"), "user3"); - _p11_hash_free (config); + p11_dict_free (config); } static void test_load_globals_no_user (CuTest *tc) { int user_mode = -1; - hashmap *config; + p11_dict *config; - _p11_kit_clear_message (); + p11_message_clear (); config = _p11_conf_load_globals (SRCDIR "/files/test-system-none.conf", SRCDIR "/files/test-user.conf", @@ -155,20 +156,20 @@ test_load_globals_no_user (CuTest *tc) CuAssertStrEquals (tc, NULL, p11_kit_message ()); CuAssertIntEquals (tc, CONF_USER_NONE, user_mode); - CuAssertStrEquals (tc, _p11_hash_get (config, "key1"), "system1"); - CuAssertStrEquals (tc, _p11_hash_get (config, "key2"), "system2"); - CuAssertStrEquals (tc, _p11_hash_get (config, "key3"), "system3"); + CuAssertStrEquals (tc, p11_dict_get (config, "key1"), "system1"); + CuAssertStrEquals (tc, p11_dict_get (config, "key2"), "system2"); + CuAssertStrEquals (tc, p11_dict_get (config, "key3"), "system3"); - _p11_hash_free (config); + p11_dict_free (config); } static void test_load_globals_user_sets_only (CuTest *tc) { int user_mode = -1; - hashmap *config; + p11_dict *config; - _p11_kit_clear_message (); + p11_message_clear (); config = _p11_conf_load_globals (SRCDIR "/files/test-system-merge.conf", SRCDIR "/files/test-user-only.conf", @@ -177,20 +178,20 @@ test_load_globals_user_sets_only (CuTest *tc) CuAssertStrEquals (tc, NULL, p11_kit_message ()); CuAssertIntEquals (tc, CONF_USER_ONLY, user_mode); - CuAssertStrEquals (tc, _p11_hash_get (config, "key1"), NULL); - CuAssertStrEquals (tc, _p11_hash_get (config, "key2"), "user2"); - CuAssertStrEquals (tc, _p11_hash_get (config, "key3"), "user3"); + CuAssertStrEquals (tc, p11_dict_get (config, "key1"), NULL); + CuAssertStrEquals (tc, p11_dict_get (config, "key2"), "user2"); + CuAssertStrEquals (tc, p11_dict_get (config, "key3"), "user3"); - _p11_hash_free (config); + p11_dict_free (config); } static void test_load_globals_system_sets_only (CuTest *tc) { int user_mode = -1; - hashmap *config; + p11_dict *config; - _p11_kit_clear_message (); + p11_message_clear (); config = _p11_conf_load_globals (SRCDIR "/files/test-system-only.conf", SRCDIR "/files/test-user.conf", @@ -199,21 +200,21 @@ test_load_globals_system_sets_only (CuTest *tc) CuAssertStrEquals (tc, NULL, p11_kit_message ()); CuAssertIntEquals (tc, CONF_USER_ONLY, user_mode); - CuAssertStrEquals (tc, _p11_hash_get (config, "key1"), NULL); - CuAssertStrEquals (tc, _p11_hash_get (config, "key2"), "user2"); - CuAssertStrEquals (tc, _p11_hash_get (config, "key3"), "user3"); + CuAssertStrEquals (tc, p11_dict_get (config, "key1"), NULL); + CuAssertStrEquals (tc, p11_dict_get (config, "key2"), "user2"); + CuAssertStrEquals (tc, p11_dict_get (config, "key3"), "user3"); - _p11_hash_free (config); + p11_dict_free (config); } static void test_load_globals_system_sets_invalid (CuTest *tc) { int user_mode = -1; - hashmap *config; + p11_dict *config; int error; - _p11_kit_clear_message (); + p11_message_clear (); config = _p11_conf_load_globals (SRCDIR "/files/test-system-invalid.conf", SRCDIR "/files/non-existant.conf", @@ -223,17 +224,17 @@ test_load_globals_system_sets_invalid (CuTest *tc) CuAssertIntEquals (tc, EINVAL, error); CuAssertPtrNotNull (tc, p11_kit_message ()); - _p11_hash_free (config); + p11_dict_free (config); } static void test_load_globals_user_sets_invalid (CuTest *tc) { int user_mode = -1; - hashmap *config; + p11_dict *config; int error; - _p11_kit_clear_message (); + p11_message_clear (); config = _p11_conf_load_globals (SRCDIR "/files/test-system-merge.conf", SRCDIR "/files/test-user-invalid.conf", @@ -243,7 +244,7 @@ test_load_globals_user_sets_invalid (CuTest *tc) CuAssertIntEquals (tc, EINVAL, error); CuAssertPtrNotNull (tc, p11_kit_message ()); - _p11_hash_free (config); + p11_dict_free (config); } static int @@ -258,10 +259,10 @@ assert_msg_contains (const char *msg, static void test_load_modules_merge (CuTest *tc) { - hashmap *configs; - hashmap *config; + p11_dict *configs; + p11_dict *config; - _p11_kit_clear_message (); + p11_message_clear (); configs = _p11_conf_load_modules (CONF_USER_MERGE, SRCDIR "/files/system-modules", @@ -269,31 +270,31 @@ test_load_modules_merge (CuTest *tc) CuAssertPtrNotNull (tc, configs); CuAssertTrue (tc, assert_msg_contains (p11_kit_message (), "invalid config filename")); - config = _p11_hash_get (configs, "one"); + config = p11_dict_get (configs, "one"); CuAssertPtrNotNull (tc, config); - CuAssertStrEquals (tc, "mock-one.so", _p11_hash_get (config, "module")); - CuAssertStrEquals (tc, _p11_hash_get (config, "setting"), "user1"); + CuAssertStrEquals (tc, "mock-one.so", p11_dict_get (config, "module")); + CuAssertStrEquals (tc, p11_dict_get (config, "setting"), "user1"); - config = _p11_hash_get (configs, "two.badname"); + config = p11_dict_get (configs, "two.badname"); CuAssertPtrNotNull (tc, config); - CuAssertStrEquals (tc, "mock-two.so", _p11_hash_get (config, "module")); - CuAssertStrEquals (tc, _p11_hash_get (config, "setting"), "system2"); + CuAssertStrEquals (tc, "mock-two.so", p11_dict_get (config, "module")); + CuAssertStrEquals (tc, p11_dict_get (config, "setting"), "system2"); - config = _p11_hash_get (configs, "three"); + config = p11_dict_get (configs, "three"); CuAssertPtrNotNull (tc, config); - CuAssertStrEquals (tc, "mock-three.so", _p11_hash_get (config, "module")); - CuAssertStrEquals (tc, _p11_hash_get (config, "setting"), "user3"); + CuAssertStrEquals (tc, "mock-three.so", p11_dict_get (config, "module")); + CuAssertStrEquals (tc, p11_dict_get (config, "setting"), "user3"); - _p11_hash_free (configs); + p11_dict_free (configs); } static void test_load_modules_user_none (CuTest *tc) { - hashmap *configs; - hashmap *config; + p11_dict *configs; + p11_dict *config; - _p11_kit_clear_message (); + p11_message_clear (); configs = _p11_conf_load_modules (CONF_USER_NONE, SRCDIR "/files/system-modules", @@ -301,29 +302,29 @@ test_load_modules_user_none (CuTest *tc) CuAssertPtrNotNull (tc, configs); CuAssertTrue (tc, assert_msg_contains (p11_kit_message (), "invalid config filename")); - config = _p11_hash_get (configs, "one"); + config = p11_dict_get (configs, "one"); CuAssertPtrNotNull (tc, config); - CuAssertStrEquals (tc, "mock-one.so", _p11_hash_get (config, "module")); - CuAssertStrEquals (tc, _p11_hash_get (config, "setting"), "system1"); + CuAssertStrEquals (tc, "mock-one.so", p11_dict_get (config, "module")); + CuAssertStrEquals (tc, p11_dict_get (config, "setting"), "system1"); - config = _p11_hash_get (configs, "two.badname"); + config = p11_dict_get (configs, "two.badname"); CuAssertPtrNotNull (tc, config); - CuAssertStrEquals (tc, "mock-two.so", _p11_hash_get (config, "module")); - CuAssertStrEquals (tc, _p11_hash_get (config, "setting"), "system2"); + CuAssertStrEquals (tc, "mock-two.so", p11_dict_get (config, "module")); + CuAssertStrEquals (tc, p11_dict_get (config, "setting"), "system2"); - config = _p11_hash_get (configs, "three"); + config = p11_dict_get (configs, "three"); CuAssertPtrEquals (tc, NULL, config); - _p11_hash_free (configs); + p11_dict_free (configs); } static void test_load_modules_user_only (CuTest *tc) { - hashmap *configs; - hashmap *config; + p11_dict *configs; + p11_dict *config; - _p11_kit_clear_message (); + p11_message_clear (); configs = _p11_conf_load_modules (CONF_USER_ONLY, SRCDIR "/files/system-modules", @@ -331,29 +332,29 @@ test_load_modules_user_only (CuTest *tc) CuAssertPtrNotNull (tc, configs); CuAssertPtrEquals (tc, NULL, (void *)p11_kit_message ()); - config = _p11_hash_get (configs, "one"); + config = p11_dict_get (configs, "one"); CuAssertPtrNotNull (tc, config); - CuAssertStrEquals (tc, _p11_hash_get (config, "module"), NULL); - CuAssertStrEquals (tc, _p11_hash_get (config, "setting"), "user1"); + CuAssertStrEquals (tc, p11_dict_get (config, "module"), NULL); + CuAssertStrEquals (tc, p11_dict_get (config, "setting"), "user1"); - config = _p11_hash_get (configs, "two.badname"); + config = p11_dict_get (configs, "two.badname"); CuAssertPtrEquals (tc, NULL, config); - config = _p11_hash_get (configs, "three"); + config = p11_dict_get (configs, "three"); CuAssertPtrNotNull (tc, config); - CuAssertStrEquals (tc, "mock-three.so", _p11_hash_get (config, "module")); - CuAssertStrEquals (tc, _p11_hash_get (config, "setting"), "user3"); + CuAssertStrEquals (tc, "mock-three.so", p11_dict_get (config, "module")); + CuAssertStrEquals (tc, p11_dict_get (config, "setting"), "user3"); - _p11_hash_free (configs); + p11_dict_free (configs); } static void test_load_modules_no_user (CuTest *tc) { - hashmap *configs; - hashmap *config; + p11_dict *configs; + p11_dict *config; - _p11_kit_clear_message (); + p11_message_clear (); configs = _p11_conf_load_modules (CONF_USER_MERGE, SRCDIR "/files/system-modules", @@ -361,20 +362,20 @@ test_load_modules_no_user (CuTest *tc) CuAssertPtrNotNull (tc, configs); CuAssertTrue (tc, assert_msg_contains (p11_kit_message (), "invalid config filename")); - config = _p11_hash_get (configs, "one"); + config = p11_dict_get (configs, "one"); CuAssertPtrNotNull (tc, config); - CuAssertStrEquals (tc, "mock-one.so", _p11_hash_get (config, "module")); - CuAssertStrEquals (tc, _p11_hash_get (config, "setting"), "system1"); + CuAssertStrEquals (tc, "mock-one.so", p11_dict_get (config, "module")); + CuAssertStrEquals (tc, p11_dict_get (config, "setting"), "system1"); - config = _p11_hash_get (configs, "two.badname"); + config = p11_dict_get (configs, "two.badname"); CuAssertPtrNotNull (tc, config); - CuAssertStrEquals (tc, "mock-two.so", _p11_hash_get (config, "module")); - CuAssertStrEquals (tc, _p11_hash_get (config, "setting"), "system2"); + CuAssertStrEquals (tc, "mock-two.so", p11_dict_get (config, "module")); + CuAssertStrEquals (tc, p11_dict_get (config, "setting"), "system2"); - config = _p11_hash_get (configs, "three"); + config = p11_dict_get (configs, "three"); CuAssertPtrEquals (tc, NULL, config); - _p11_hash_free (configs); + p11_dict_free (configs); } int @@ -384,7 +385,7 @@ main (void) CuSuite* suite = CuSuiteNew (); int ret; - _p11_library_init (); + p11_library_init (); SUITE_ADD_TEST (suite, test_parse_conf_1); SUITE_ADD_TEST (suite, test_parse_ignore_missing); diff --git a/tests/files/system-modules/four.module b/p11-kit/tests/files/system-modules/four.module index 6eace3c..6eace3c 100644 --- a/tests/files/system-modules/four.module +++ b/p11-kit/tests/files/system-modules/four.module diff --git a/tests/files/system-modules/one.module b/p11-kit/tests/files/system-modules/one.module index 3620869..3620869 100644 --- a/tests/files/system-modules/one.module +++ b/p11-kit/tests/files/system-modules/one.module diff --git a/tests/files/system-modules/two-duplicate.module b/p11-kit/tests/files/system-modules/two-duplicate.module index 907aa75..907aa75 100644 --- a/tests/files/system-modules/two-duplicate.module +++ b/p11-kit/tests/files/system-modules/two-duplicate.module diff --git a/tests/files/system-modules/two.badname b/p11-kit/tests/files/system-modules/two.badname index 0d41cac..0d41cac 100644 --- a/tests/files/system-modules/two.badname +++ b/p11-kit/tests/files/system-modules/two.badname diff --git a/tests/files/system-modules/win32/four.module b/p11-kit/tests/files/system-modules/win32/four.module index 7fd1540..7fd1540 100644 --- a/tests/files/system-modules/win32/four.module +++ b/p11-kit/tests/files/system-modules/win32/four.module diff --git a/tests/files/system-modules/win32/one.module b/p11-kit/tests/files/system-modules/win32/one.module index 5f80304..5f80304 100644 --- a/tests/files/system-modules/win32/one.module +++ b/p11-kit/tests/files/system-modules/win32/one.module diff --git a/tests/files/system-modules/win32/two-duplicate.module b/p11-kit/tests/files/system-modules/win32/two-duplicate.module index e80c9e8..e80c9e8 100644 --- a/tests/files/system-modules/win32/two-duplicate.module +++ b/p11-kit/tests/files/system-modules/win32/two-duplicate.module diff --git a/tests/files/system-modules/win32/two.badname b/p11-kit/tests/files/system-modules/win32/two.badname index ae44b83..ae44b83 100644 --- a/tests/files/system-modules/win32/two.badname +++ b/p11-kit/tests/files/system-modules/win32/two.badname diff --git a/tests/files/system-pkcs11.conf b/p11-kit/tests/files/system-pkcs11.conf index 20741e7..20741e7 100644 --- a/tests/files/system-pkcs11.conf +++ b/p11-kit/tests/files/system-pkcs11.conf diff --git a/tests/files/test-1.conf b/p11-kit/tests/files/test-1.conf index d4ae0a1..d4ae0a1 100644 --- a/tests/files/test-1.conf +++ b/p11-kit/tests/files/test-1.conf diff --git a/tests/files/test-pinfile b/p11-kit/tests/files/test-pinfile index f646f3d..f646f3d 100644 --- a/tests/files/test-pinfile +++ b/p11-kit/tests/files/test-pinfile diff --git a/tests/files/test-pinfile-large b/p11-kit/tests/files/test-pinfile-large index 506668d..506668d 100644 --- a/tests/files/test-pinfile-large +++ b/p11-kit/tests/files/test-pinfile-large diff --git a/tests/files/test-system-invalid.conf b/p11-kit/tests/files/test-system-invalid.conf index 344ee96..344ee96 100644 --- a/tests/files/test-system-invalid.conf +++ b/p11-kit/tests/files/test-system-invalid.conf diff --git a/tests/files/test-system-merge.conf b/p11-kit/tests/files/test-system-merge.conf index 978427d..978427d 100644 --- a/tests/files/test-system-merge.conf +++ b/p11-kit/tests/files/test-system-merge.conf diff --git a/tests/files/test-system-none.conf b/p11-kit/tests/files/test-system-none.conf index 95351e6..95351e6 100644 --- a/tests/files/test-system-none.conf +++ b/p11-kit/tests/files/test-system-none.conf diff --git a/tests/files/test-system-only.conf b/p11-kit/tests/files/test-system-only.conf index 589f1c7..589f1c7 100644 --- a/tests/files/test-system-only.conf +++ b/p11-kit/tests/files/test-system-only.conf diff --git a/tests/files/test-user-invalid.conf b/p11-kit/tests/files/test-user-invalid.conf index 344ee96..344ee96 100644 --- a/tests/files/test-user-invalid.conf +++ b/p11-kit/tests/files/test-user-invalid.conf diff --git a/tests/files/test-user-only.conf b/p11-kit/tests/files/test-user-only.conf index 3224c01..3224c01 100644 --- a/tests/files/test-user-only.conf +++ b/p11-kit/tests/files/test-user-only.conf diff --git a/tests/files/test-user.conf b/p11-kit/tests/files/test-user.conf index 369544a..369544a 100644 --- a/tests/files/test-user.conf +++ b/p11-kit/tests/files/test-user.conf diff --git a/tests/files/user-modules/one.module b/p11-kit/tests/files/user-modules/one.module index c371e4a..c371e4a 100644 --- a/tests/files/user-modules/one.module +++ b/p11-kit/tests/files/user-modules/one.module diff --git a/tests/files/user-modules/three.module b/p11-kit/tests/files/user-modules/three.module index 00caab5..00caab5 100644 --- a/tests/files/user-modules/three.module +++ b/p11-kit/tests/files/user-modules/three.module diff --git a/tests/files/user-modules/win32/one.module b/p11-kit/tests/files/user-modules/win32/one.module index c371e4a..c371e4a 100644 --- a/tests/files/user-modules/win32/one.module +++ b/p11-kit/tests/files/user-modules/win32/one.module diff --git a/tests/files/user-modules/win32/three.module b/p11-kit/tests/files/user-modules/win32/three.module index 58f883d..58f883d 100644 --- a/tests/files/user-modules/win32/three.module +++ b/p11-kit/tests/files/user-modules/win32/three.module diff --git a/tests/mock-module-ep.c b/p11-kit/tests/mock-module-ep.c index 7440a74..7440a74 100644 --- a/tests/mock-module-ep.c +++ b/p11-kit/tests/mock-module-ep.c diff --git a/tests/mock-module.c b/p11-kit/tests/mock-module.c index 99cde50..1a74806 100644 --- a/tests/mock-module.c +++ b/p11-kit/tests/mock-module.c @@ -34,11 +34,11 @@ #include "config.h" +#include "library.h" #define CRYPTOKI_EXPORTS #include "pkcs11.h" -#include "mock-module.h" -#include "p11-kit/util.h" +#include "mock-module.h" #include <stdarg.h> #include <stdio.h> @@ -50,7 +50,7 @@ */ /* Various mutexes */ -static mutex_t init_mutex; +static p11_mutex_t init_mutex; /* Whether we've been initialized, and on what process id it happened */ static int pkcs11_initialized = 0; @@ -97,7 +97,7 @@ mock_C_Initialize (CK_VOID_PTR init_args) debug (("C_Initialize: enter")); - _p11_mutex_lock (&init_mutex); + p11_mutex_lock (&init_mutex); if (init_args != NULL) { int supplied_ok; @@ -148,7 +148,7 @@ done: pkcs11_initialized_pid = 0; } - _p11_mutex_unlock (&init_mutex); + p11_mutex_unlock (&init_mutex); debug (("C_Initialize: %d", ret)); return ret; @@ -161,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); - _p11_mutex_lock (&init_mutex); + p11_mutex_lock (&init_mutex); /* This should stop all other calls in */ pkcs11_initialized = 0; pkcs11_initialized_pid = 0; - _p11_mutex_unlock (&init_mutex); + p11_mutex_unlock (&init_mutex); debug (("C_Finalize: %d", CKR_OK)); return CKR_OK; @@ -892,7 +892,7 @@ mock_module_init (void) { static int initialized = 0; if (!initialized) { - _p11_mutex_init (&init_mutex); + p11_mutex_init (&init_mutex); initialized = 1; } } diff --git a/tests/mock-module.h b/p11-kit/tests/mock-module.h index 9557f70..9557f70 100644 --- a/tests/mock-module.h +++ b/p11-kit/tests/mock-module.h diff --git a/tests/pin-test.c b/p11-kit/tests/pin-test.c index 7e7dda8..5427f8b 100644 --- a/tests/pin-test.c +++ b/p11-kit/tests/pin-test.c @@ -35,6 +35,8 @@ #include "config.h" #include "CuTest.h" +#include "library.h" + #include <assert.h> #include <errno.h> #include <stdlib.h> @@ -300,7 +302,7 @@ main (void) CuSuite* suite = CuSuiteNew (); int ret; - _p11_library_init (); + p11_library_init (); SUITE_ADD_TEST (suite, test_pin_register_unregister); SUITE_ADD_TEST (suite, test_pin_read); diff --git a/tests/print-messages.c b/p11-kit/tests/print-messages.c index 5870ad1..5870ad1 100644 --- a/tests/print-messages.c +++ b/p11-kit/tests/print-messages.c diff --git a/tests/progname-test.c b/p11-kit/tests/progname-test.c index 1e42056..bd37400 100644 --- a/tests/progname-test.c +++ b/p11-kit/tests/progname-test.c @@ -35,6 +35,8 @@ #include "config.h" #include "CuTest.h" +#include "library.h" + #include <assert.h> #include <string.h> #include <stdio.h> @@ -70,19 +72,7 @@ test_progname_set (CuTest *tc) } /* Defined in util.c */ -extern char *_p11_my_progname; - -static void -test_progname_uninit_clears (CuTest *tc) -{ - _p11_set_progname_unlocked ("love-generation"); - CuAssertStrEquals (tc, "love-generation", _p11_my_progname); - - /* Inititialize should clear above variable */ - _p11_library_uninit (); - - CuAssertPtrEquals (tc, NULL, _p11_my_progname); -} +extern char p11_my_progname[]; int main (void) @@ -91,14 +81,11 @@ main (void) CuSuite* suite = CuSuiteNew (); int ret; - _p11_library_init (); + p11_library_init (); SUITE_ADD_TEST (suite, test_progname_default); SUITE_ADD_TEST (suite, test_progname_set); - /* This test should be last, as it uninitializes the library */ - SUITE_ADD_TEST (suite, test_progname_uninit_clears); - CuSuiteRun (suite); CuSuiteSummary (suite, output); CuSuiteDetails (suite, output); diff --git a/tests/test-init.c b/p11-kit/tests/test-init.c index d872afa..5861bbf 100644 --- a/tests/test-init.c +++ b/p11-kit/tests/test-init.c @@ -37,6 +37,8 @@ #include <sys/types.h> +#include "library.h" + #include <assert.h> #include <stdio.h> #include <stdlib.h> @@ -45,7 +47,6 @@ #include <unistd.h> #include "p11-kit/p11-kit.h" -#include "p11-kit/util.h" #include "mock-module.h" @@ -125,7 +126,7 @@ test_recursive_initialization (CuTest *tc) CuAssertTrue (tc, rv == CKR_FUNCTION_FAILED); } -static mutex_t race_mutex; +static p11_mutex_t race_mutex; static int initialization_count = 0; static int finalization_count = 0; @@ -135,11 +136,11 @@ static CK_RV mock_C_Initialize__threaded_race (CK_VOID_PTR init_args) { /* Atomically increment value */ - _p11_mutex_lock (&race_mutex); + p11_mutex_lock (&race_mutex); initialization_count += 1; - _p11_mutex_unlock (&race_mutex); + p11_mutex_unlock (&race_mutex); - _p11_sleep_ms (100); + p11_sleep_ms (100); return CKR_OK; } @@ -147,11 +148,11 @@ static CK_RV mock_C_Finalize__threaded_race (CK_VOID_PTR reserved) { /* Atomically increment value */ - _p11_mutex_lock (&race_mutex); + p11_mutex_lock (&race_mutex); finalization_count += 1; - _p11_mutex_unlock (&race_mutex); + p11_mutex_unlock (&race_mutex); - _p11_sleep_ms (100); + p11_sleep_ms (100); return CKR_OK; } @@ -183,7 +184,7 @@ static void test_threaded_initialization (CuTest *tc) { static const int num_threads = 2; - thread_t threads[num_threads]; + p11_thread_t threads[num_threads]; int ret; int i; @@ -196,25 +197,25 @@ test_threaded_initialization (CuTest *tc) finalization_count = 0; for (i = 0; i < num_threads; i++) { - ret = _p11_thread_create (&threads[i], initialization_thread, tc); + ret = p11_thread_create (&threads[i], initialization_thread, tc); CuAssertIntEquals (tc, 0, ret); CuAssertTrue (tc, threads[i] != 0); } for (i = 0; i < num_threads; i++) { - ret = _p11_thread_join (threads[i]); + ret = p11_thread_join (threads[i]); CuAssertIntEquals (tc, 0, ret); threads[i] = 0; } for (i = 0; i < num_threads; i++) { - ret = _p11_thread_create (&threads[i], finalization_thread, tc); + ret = p11_thread_create (&threads[i], finalization_thread, tc); CuAssertIntEquals (tc, 0, ret); CuAssertTrue (tc, threads[i] != 0); } for (i = 0; i < num_threads; i++) { - ret = _p11_thread_join (threads[i]); + ret = p11_thread_join (threads[i]); CuAssertIntEquals (tc, 0, ret); threads[i] = 0; } @@ -294,9 +295,9 @@ main (void) CuSuite* suite = CuSuiteNew (); int ret; - _p11_mutex_init (&race_mutex); + p11_mutex_init (&race_mutex); mock_module_init (); - _p11_library_init (); + p11_library_init (); #ifdef OS_UNIX SUITE_ADD_TEST (suite, test_fork_initialization); diff --git a/tests/test-modules.c b/p11-kit/tests/test-modules.c index 74560d9..159e04e 100644 --- a/tests/test-modules.c +++ b/p11-kit/tests/test-modules.c @@ -40,9 +40,10 @@ #include <stdio.h> #include <string.h> +#include "library.h" #include "p11-kit.h" #include "private.h" -#include "hashmap.h" +#include "dict.h" static CK_FUNCTION_LIST_PTR_PTR initialize_and_get_modules (CuTest *tc) @@ -73,32 +74,32 @@ static void test_no_duplicates (CuTest *tc) { CK_FUNCTION_LIST_PTR_PTR modules; - hashmap *paths; - hashmap *funcs; + p11_dict *paths; + p11_dict *funcs; char *path; int i; modules = initialize_and_get_modules (tc); - paths = _p11_hash_create (_p11_hash_string_hash, _p11_hash_string_equal, NULL, NULL); - funcs = _p11_hash_create (_p11_hash_direct_hash, _p11_hash_direct_equal, NULL, NULL); + 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_hash_get (funcs, modules[i])) + if (p11_dict_get (funcs, modules[i])) CuAssert (tc, "found duplicate function list pointer", 0); - if (_p11_hash_get (paths, path)) + if (p11_dict_get (paths, path)) CuAssert (tc, "found duplicate path name", 0); - if (!_p11_hash_set (funcs, modules[i], "")) + if (!p11_dict_set (funcs, modules[i], "")) CuAssert (tc, "shouldn't be reached", 0); - if (!_p11_hash_set (paths, path, "")) + if (!p11_dict_set (paths, path, "")) CuAssert (tc, "shouldn't be reached", 0); } - _p11_hash_free (paths); - _p11_hash_free (funcs); + p11_dict_free (paths); + p11_dict_free (funcs); finalize_and_free_modules (tc, modules); } @@ -225,7 +226,7 @@ main (void) CuSuite* suite = CuSuiteNew (); int ret; - _p11_library_init (); + p11_library_init (); SUITE_ADD_TEST (suite, test_no_duplicates); SUITE_ADD_TEST (suite, test_disable); diff --git a/tests/uri-test.c b/p11-kit/tests/uri-test.c index 4780e4e..0e3e722 100644 --- a/tests/uri-test.c +++ b/p11-kit/tests/uri-test.c @@ -35,6 +35,8 @@ #include "config.h" #include "CuTest.h" +#include "library.h" + #include <assert.h> #include <string.h> #include <stdio.h> @@ -1168,7 +1170,7 @@ main (void) CuSuite* suite = CuSuiteNew (); int ret; - _p11_library_init (); + p11_library_init (); SUITE_ADD_TEST (suite, test_uri_parse); SUITE_ADD_TEST (suite, test_uri_parse_bad_scheme); diff --git a/p11-kit/uri.c b/p11-kit/uri.c index 752d677..1306fed 100644 --- a/p11-kit/uri.c +++ b/p11-kit/uri.c @@ -34,12 +34,12 @@ #include "config.h" -#define DEBUG_FLAG DEBUG_URI +#define P11_DEBUG_FLAG P11_DEBUG_URI #include "debug.h" +#include "library.h" #include "pkcs11.h" #include "p11-kit.h" #include "uri.h" -#include "util.h" #include <assert.h> #include <ctype.h> @@ -1419,7 +1419,7 @@ p11_kit_uri_message (int code) case P11_KIT_URI_NOT_FOUND: return "The URI component was not found"; default: - _p11_debug ("unknown error code: %d", code); + p11_debug ("unknown error code: %d", code); return "Unknown error"; } } diff --git a/p11-kit/util.c b/p11-kit/util.c index ccc954c..9c5d99b 100644 --- a/p11-kit/util.c +++ b/p11-kit/util.c @@ -38,11 +38,11 @@ #include "config.h" #include "compat.h" -#define DEBUG_FLAG DEBUG_LIB +#define P11_DEBUG_FLAG P11_DEBUG_LIB #include "debug.h" +#include "library.h" #include "p11-kit.h" #include "private.h" -#include "util.h" #include <assert.h> #include <stdarg.h> @@ -50,18 +50,6 @@ #include <stdio.h> #include <string.h> -/* - * This is the mutex that protects the global data of this library - * and the pkcs11 proxy module. Note that we *never* call into our - * underlying pkcs11 modules while holding this mutex. Therefore it - * doesn't have to be recursive and we can keep things simple. - */ -mutex_t _p11_mutex; - -#ifdef OS_UNIX -pthread_once_t _p11_once; -#endif - /** * SECTION:p11-kit-future * @title: Future @@ -72,8 +60,6 @@ pthread_once_t _p11_once; * MACRO. See the p11-kit.h header for more details. */ -static int print_messages = 1; - /** * p11_kit_space_strlen: * @string: Pointer to string block @@ -146,45 +132,6 @@ p11_kit_space_strdup (const unsigned char *string, size_t max_length) return result; } -static void -store_message_buffer (const char* msg, size_t length) -{ - p11_local *local; - - if (length > P11_MAX_MESSAGE - 1) - length = P11_MAX_MESSAGE - 1; - - local = _p11_library_get_thread_local (); - if (local != NULL) { - memcpy (local->message, msg, length); - local->message[length] = 0; - } -} - -void -_p11_message (const char* msg, ...) -{ - char buffer[P11_MAX_MESSAGE]; - va_list va; - size_t length; - - va_start (va, msg); - length = vsnprintf (buffer, P11_MAX_MESSAGE - 1, msg, va); - va_end (va); - - /* Was it truncated? */ - if (length > P11_MAX_MESSAGE - 1) - length = P11_MAX_MESSAGE - 1; - buffer[length] = 0; - - /* If printing is not disabled, just print out */ - if (print_messages) - fprintf (stderr, "p11-kit: %s\n", buffer); - - _p11_debug_message (DEBUG_LIB, "message: %s", buffer); - store_message_buffer (buffer, length); -} - /** * p11_kit_be_quiet: * @@ -194,9 +141,7 @@ _p11_message (const char* msg, ...) void p11_kit_be_quiet (void) { - _p11_lock (); - print_messages = 0; - _p11_unlock (); + p11_message_quiet (); } /** @@ -215,18 +160,7 @@ p11_kit_be_quiet (void) const char* p11_kit_message (void) { - p11_local *local; - local = _p11_library_get_thread_local (); - return local && local->message[0] ? local->message : NULL; -} - -void -_p11_kit_clear_message (void) -{ - p11_local *local; - local = _p11_library_get_thread_local (); - if (local != NULL) - local->message[0] = 0; + return p11_message_last (); } void @@ -236,231 +170,12 @@ _p11_kit_default_message (CK_RV rv) if (rv != CKR_OK) { msg = p11_kit_strerror (rv); - store_message_buffer (msg, strlen (msg)); + p11_message_store (msg, strlen (msg)); } } -static void -uninit_common (void) -{ - _p11_debug ("uninitializing library"); - - _p11_set_progname_unlocked (NULL); -} - -#ifdef OS_UNIX - -static pthread_key_t thread_local = 0; - -p11_local * -_p11_library_get_thread_local (void) -{ - p11_local *local; - - _p11_library_init_once (); - - local = pthread_getspecific (thread_local); - if (local == NULL) { - local = calloc (1, sizeof (p11_local)); - pthread_setspecific (thread_local, local); - } - - return local; -} - -#ifdef __GNUC__ -__attribute__((constructor)) -#endif -void -_p11_library_init (void) -{ - _p11_debug_init (); - _p11_debug ("initializing library"); - _p11_mutex_init (&_p11_mutex); - pthread_key_create (&thread_local, free); -} - -#ifdef __GNUC__ -__attribute__((destructor)) -#endif -void -_p11_library_uninit (void) -{ - uninit_common (); - - /* Some cleanup to pacify valgrind */ - free (pthread_getspecific (thread_local)); - pthread_setspecific (thread_local, NULL); - - pthread_key_delete (thread_local); - _p11_mutex_uninit (&_p11_mutex); -} - -void -_p11_mutex_init (mutex_t *mutex) -{ - pthread_mutexattr_t attr; - int ret; - - pthread_mutexattr_init (&attr); - pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE); - ret = pthread_mutex_init (mutex, &attr); - assert (ret == 0); - pthread_mutexattr_destroy (&attr); -} - -#endif /* OS_UNIX */ - -#ifdef OS_WIN32 - -static DWORD thread_local = TLS_OUT_OF_INDEXES; - -BOOL WINAPI DllMain (HINSTANCE, DWORD, LPVOID); - -p11_local * -_p11_library_get_thread_local (void) -{ - LPVOID data; - - if (thread_local == TLS_OUT_OF_INDEXES) - return NULL; - - data = TlsGetValue (thread_local); - if (data == NULL) { - data = LocalAlloc (LPTR, sizeof (p11_local)); - TlsSetValue (thread_local, data); - } - - return (p11_local *)data; -} - -void -_p11_library_init (void) -{ - _p11_debug_init (); - _p11_debug ("initializing library"); - _p11_mutex_init (&_p11_mutex); - thread_local = TlsAlloc (); -} - -static void -free_tls_value (LPVOID data) -{ - p11_local *local = data; - if (local == NULL) - return; - if (local->last_error) - LocalFree (local->last_error); - LocalFree (data); -} - -void -_p11_library_uninit (void) -{ - LPVOID data; - - uninit_common (); - - if (thread_local != TLS_OUT_OF_INDEXES) { - data = TlsGetValue (thread_local); - free_tls_value (data); - TlsFree (thread_local); - } - _p11_mutex_uninit (&_p11_mutex); -} - -const char * -_p11_module_error (void) -{ - DWORD code = GetLastError(); - p11_local *local; - LPVOID msg_buf; - - local = _p11_library_get_thread_local (); - - FormatMessageA (FORMAT_MESSAGE_ALLOCATE_BUFFER | - FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, code, - MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), - (LPSTR)&msg_buf, 0, NULL); - - if (local->last_error) - LocalFree (local->last_error); - local->last_error = msg_buf; - - return msg_buf; -} - -int -_p11_thread_create (thread_t *thread, - thread_routine routine, - void *arg) -{ - assert (thread); - - *thread = CreateThread (NULL, 0, - (LPTHREAD_START_ROUTINE)routine, - arg, 0, NULL); - - if (*thread == NULL) - return GetLastError (); - - return 0; -} - -int -_p11_thread_join (thread_t thread) -{ - DWORD res; - - res = WaitForSingleObject (thread, INFINITE); - if (res == WAIT_FAILED) - return GetLastError (); - - CloseHandle (thread); - return 0; -} - -BOOL WINAPI -DllMain (HINSTANCE instance, - DWORD reason, - LPVOID reserved) -{ - LPVOID data; - - switch (reason) { - case DLL_PROCESS_ATTACH: - _p11_library_init (); - if (thread_local == TLS_OUT_OF_INDEXES) { - _p11_debug ("couldn't setup tls"); - return FALSE; - } - break; - - case DLL_THREAD_DETACH: - if (thread_local != TLS_OUT_OF_INDEXES) { - _p11_debug ("thread stopped, freeing tls"); - data = TlsGetValue (thread_local); - free_tls_value (data); - } - break; - - case DLL_PROCESS_DETACH: - _p11_library_uninit (); - break; - - default: - break; - } - - return TRUE; -} - -#endif /* OS_WIN32 */ - /* This is the progname that we think of this process as. */ -char *_p11_my_progname = NULL; +char p11_my_progname[256] = { 0, }; /** * p11_kit_set_progname: @@ -475,25 +190,30 @@ char *_p11_my_progname = NULL; void p11_kit_set_progname (const char *progname) { - _p11_library_init_once (); + p11_library_init_once (); - _p11_lock (); + p11_lock (); _p11_set_progname_unlocked (progname); - _p11_unlock (); + p11_unlock (); } void _p11_set_progname_unlocked (const char *progname) { - /* We can be called with NULL, to cleanup memory usage */ - free (_p11_my_progname); - _p11_my_progname = progname ? strdup (progname) : NULL; + /* We can be called with NULL */ + if (progname == NULL) + progname = ""; + + strncpy (p11_my_progname, progname, sizeof (p11_my_progname)); + p11_my_progname[sizeof (p11_my_progname) - 1] = 0; } const char * _p11_get_progname_unlocked (void) { - if (_p11_my_progname == NULL) + if (p11_my_progname[0] == '\0') _p11_set_progname_unlocked (getprogname ()); - return _p11_my_progname; + if (p11_my_progname[0] == '\0') + return NULL; + return p11_my_progname; } diff --git a/p11-kit/util.h b/p11-kit/util.h deleted file mode 100644 index b8d4a15..0000000 --- a/p11-kit/util.h +++ /dev/null @@ -1,158 +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. - * - * - * CONTRIBUTORS - * Stef Walter <stef@memberwebs.com> - */ - -#ifndef __UTIL_H__ -#define __UTIL_H__ - -#include "config.h" - -#include <sys/types.h> - -/* ----------------------------------------------------------------------------- - * WIN32 - */ - -#ifdef OS_WIN32 - -#ifndef _WIN32_WINNT -#define _WIN32_WINNT 0x500 -#endif - -#ifndef _WIN32_IE -#define _WIN32_IE 0x500 -#endif - -#define WIN32_LEAN_AND_MEAN 1 -#include <windows.h> - -/* Oh ... my ... god */ -#undef CreateMutex - -typedef CRITICAL_SECTION mutex_t; - -typedef HANDLE thread_t; - -typedef DWORD thread_id_t; - -#define _p11_mutex_init(m) \ - (InitializeCriticalSection (m)) -#define _p11_mutex_lock(m) \ - (EnterCriticalSection (m)) -#define _p11_mutex_unlock(m) \ - (LeaveCriticalSection (m)) -#define _p11_mutex_uninit(m) \ - (DeleteCriticalSection (m)) - -typedef void * (*thread_routine) (void *arg); - -int _p11_thread_create (thread_t *thread, thread_routine, void *arg); - -int _p11_thread_join (thread_t thread); - -/* Returns a thread_id_t */ -#define _p11_thread_id_self() \ - (GetCurrentThreadId ()) - -typedef HMODULE dl_module_t; - -#define _p11_module_open(f) \ - (LoadLibrary (f)) -#define _p11_module_close(d) \ - (FreeLibrary (d)) -#define _p11_module_symbol(d, s) \ - ((void *)GetProcAddress ((d), (s))) - -const char * _p11_module_error (void); - -#define _p11_sleep_ms(ms) \ - (Sleep (ms)) - -#endif /* OS_WIN32 */ - -/* ---------------------------------------------------------------------------- - * UNIX - */ - -#ifdef OS_UNIX - -#include <pthread.h> -#include <dlfcn.h> -#include <time.h> - -typedef pthread_mutex_t mutex_t; - -void _p11_mutex_init (mutex_t *mutex); - -#define _p11_mutex_lock(m) \ - (pthread_mutex_lock (m)) -#define _p11_mutex_unlock(m) \ - (pthread_mutex_unlock (m)) -#define _p11_mutex_uninit(m) \ - (pthread_mutex_destroy(m)) - -typedef pthread_t thread_t; - -typedef pthread_t thread_id_t; - -typedef void * (*thread_routine) (void *arg); - -#define _p11_thread_create(t, r, a) \ - (pthread_create ((t), NULL, (r), (a))) -#define _p11_thread_join(t) \ - (pthread_join ((t), NULL)) -#define _p11_thread_id_self(m) \ - (pthread_self ()) - -typedef void * dl_module_t; - -#define _p11_module_open(f) \ - (dlopen ((f), RTLD_LOCAL | RTLD_NOW)) -#define _p11_module_close(d) \ - (dlclose(d)) -#define _p11_module_error() \ - (dlerror ()) -#define _p11_module_symbol(d, s) \ - (dlsym ((d), (s))) - -#define _p11_sleep_ms(ms) \ - do { int _ms = (ms); \ - struct timespec _ts = { _ms / 1000, (_ms % 1000) * 1000 * 1000 }; \ - nanosleep (&_ts, NULL); \ - } while(0) - -#endif /* OS_UNIX */ - -#endif /* __UTIL_H__ */ diff --git a/tests/ptr-array-test.c b/tests/ptr-array-test.c deleted file mode 100644 index b75614f..0000000 --- a/tests/ptr-array-test.c +++ /dev/null @@ -1,257 +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 "CuTest.h" - -#include <stdlib.h> -#include <stdio.h> -#include <string.h> - -#include "ptr-array.h" - -static void -test__p11_ptr_array_create (CuTest *tc) -{ - ptr_array_t *array; - - array = _p11_ptr_array_create (NULL); - CuAssertPtrNotNull (tc, array); - _p11_ptr_array_free (array); -} - -static void -test__p11_ptr_array_free_null (CuTest *tc) -{ - _p11_ptr_array_free (NULL); -} - -static void -destroy_value (void *data) -{ - int *value = data; - *value = 2; -} - -static void -test__p11_ptr_array_free_destroys (CuTest *tc) -{ - ptr_array_t *array; - int value = 0; - - array = _p11_ptr_array_create (destroy_value); - CuAssertPtrNotNull (tc, array); - if (!_p11_ptr_array_add (array, &value)) - CuFail (tc, "should not be reached"); - _p11_ptr_array_free (array); - - CuAssertIntEquals (tc, 2, value); -} - -#if 0 -static void -test__p11_hash_iterate (CuTest *tc) -{ - hash_t *ht; - hash_iter_t hi; - int key = 1; - int value = 2; - void *pkey; - void *pvalue; - int ret; - - ht = _p11_hash_create (_p11_hash_direct_hash, _p11_hash_direct_equal, NULL, NULL); - CuAssertPtrNotNull (tc, ht); - if (!_p11_hash_set (ht, &key, &value)) - CuFail (tc, "should not be reached"); - - _p11_hash_iterate (ht, &hi); - - ret = _p11_hash_next (&hi, &pkey, &pvalue); - CuAssertIntEquals (tc, 1, ret); - CuAssertPtrEquals (tc, pkey, &key); - CuAssertPtrEquals (tc, pvalue, &value); - - ret = _p11_hash_next (&hi, &pkey, &pvalue); - CuAssertIntEquals (tc, 0, ret); - - _p11_hash_free (ht); -} - -#endif - -static void -test__p11_ptr_array_add (CuTest *tc) -{ - char *value = "VALUE"; - char *check; - ptr_array_t *array; - - array = _p11_ptr_array_create (NULL); - if (!_p11_ptr_array_add (array, value)) - CuFail (tc, "should not be reached"); - - CuAssertIntEquals (tc, 1, _p11_ptr_array_count (array)); - - check = _p11_ptr_array_at (array, 0); - CuAssertPtrEquals (tc, check, value); - - _p11_ptr_array_free (array); -} - -static void -test__p11_ptr_array_add_remove (CuTest *tc) -{ - char *value = "VALUE"; - char *check; - ptr_array_t *array; - - array = _p11_ptr_array_create (NULL); - if (!_p11_ptr_array_add (array, value)) - CuFail (tc, "should not be reached"); - - CuAssertIntEquals (tc, 1, _p11_ptr_array_count (array)); - - check = _p11_ptr_array_at (array, 0); - CuAssertPtrEquals (tc, check, value); - - _p11_ptr_array_remove (array, 0); - - CuAssertIntEquals (tc, 0, _p11_ptr_array_count (array)); - - _p11_ptr_array_free (array); -} - -static void -test__p11_ptr_array_remove_destroys (CuTest *tc) -{ - ptr_array_t *array; - int value = 0; - - array = _p11_ptr_array_create (destroy_value); - if (!_p11_ptr_array_add (array, &value)) - CuFail (tc, "should not be reached"); - - _p11_ptr_array_remove (array, 0); - - CuAssertIntEquals (tc, 2, value); - - /* should not be destroyed again */ - value = 0; - - _p11_ptr_array_free (array); - - CuAssertIntEquals (tc, 0, value); -} - -static void -test__p11_ptr_array_remove_and_count (CuTest *tc) -{ - ptr_array_t *array; - int *value; - int i; - - array = _p11_ptr_array_create (free); - - CuAssertIntEquals (tc, 0, _p11_ptr_array_count (array)); - - for (i = 0; i < 20000; ++i) { - value = malloc (sizeof (int)); - *value = i; - if (!_p11_ptr_array_add (array, value)) - CuFail (tc, "should not be reached"); - CuAssertIntEquals (tc, i + 1, _p11_ptr_array_count (array)); - } - - for (i = 10; i < 20000; ++i) { - _p11_ptr_array_remove (array, 10); - CuAssertIntEquals (tc, 20010 - (i + 1), _p11_ptr_array_count (array)); - } - - CuAssertIntEquals (tc, 10, _p11_ptr_array_count (array)); - - _p11_ptr_array_free (array); -} - -static void -test__p11_ptr_array_snapshot (CuTest *tc) -{ - ptr_array_t *array; - void **snapshot; - - array = _p11_ptr_array_create (NULL); - - _p11_ptr_array_add (array, "1"); - _p11_ptr_array_add (array, "2"); - _p11_ptr_array_add (array, "3"); - _p11_ptr_array_add (array, "4"); - CuAssertIntEquals (tc, 4, _p11_ptr_array_count (array)); - - snapshot = _p11_ptr_array_snapshot (array); - - CuAssertStrEquals (tc, "1", snapshot[0]); - CuAssertStrEquals (tc, "2", snapshot[1]); - CuAssertStrEquals (tc, "3", snapshot[2]); - CuAssertStrEquals (tc, "4", snapshot[3]); - - free (snapshot); - _p11_ptr_array_free (array); -} - -int -main (void) -{ - CuString *output = CuStringNew (); - CuSuite* suite = CuSuiteNew (); - int ret; - - SUITE_ADD_TEST (suite, test__p11_ptr_array_create); - SUITE_ADD_TEST (suite, test__p11_ptr_array_add); - SUITE_ADD_TEST (suite, test__p11_ptr_array_add_remove); - SUITE_ADD_TEST (suite, test__p11_ptr_array_remove_destroys); - SUITE_ADD_TEST (suite, test__p11_ptr_array_remove_and_count); - SUITE_ADD_TEST (suite, test__p11_ptr_array_free_null); - SUITE_ADD_TEST (suite, test__p11_ptr_array_free_destroys); - SUITE_ADD_TEST (suite, test__p11_ptr_array_snapshot); - - CuSuiteRun (suite); - CuSuiteSummary (suite, output); - CuSuiteDetails (suite, output); - printf ("%s\n", output->buffer); - ret = suite->failCount; - CuSuiteDelete (suite); - CuStringDelete (output); - - return ret; -} |