From 3d503948450d69293a3fdfec096e398fedf714f2 Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Thu, 6 Dec 2012 22:42:02 +0100 Subject: Move debug and library code into the common/ subdirectory Start using p11_ as our internal prefix rather than _p11_. We explicitly export p11_kit_ so this is fine as far as visibility. Move the threading, mutex, and module compat, dict, and array code into the common directory too. Take this opportunity to clean up a bit of internal API as well, since so many lines are being touched internally. --- .gitignore | 32 +- Makefile.am | 13 +- build/Makefile.am | 11 + build/Makefile.tests | 8 + build/cutest/CuTest.c | 329 ++++++ build/cutest/CuTest.h | 111 ++ build/cutest/README.txt | 211 ++++ build/cutest/license.txt | 38 + build/m4/.empty | 1 + common/Makefile.am | 11 + common/array.c | 119 ++ common/array.h | 66 ++ common/compat.c | 95 ++ common/compat.h | 123 ++ common/debug.c | 151 +++ common/debug.h | 129 +++ common/dict.c | 391 +++++++ common/dict.h | 178 +++ common/library.c | 286 +++++ common/library.h | 80 ++ common/tests/Makefile.am | 32 + common/tests/test-array.c | 194 ++++ common/tests/test-dict.c | 465 ++++++++ configure.ac | 7 +- doc/Makefile.am | 11 +- m4/.empty | 1 - p11-kit/Makefile.am | 54 +- p11-kit/conf.c | 116 +- p11-kit/conf.h | 14 +- p11-kit/debug.c | 151 --- p11-kit/debug.h | 129 --- p11-kit/hashmap.c | 391 ------- p11-kit/hashmap.h | 173 --- p11-kit/modules.c | 318 ++--- p11-kit/pin.c | 78 +- p11-kit/private.h | 41 - p11-kit/proxy.c | 86 +- p11-kit/ptr-array.c | 154 --- p11-kit/ptr-array.h | 61 - p11-kit/tests/Makefile.am | 63 + p11-kit/tests/conf-test.c | 415 +++++++ p11-kit/tests/files/system-modules/four.module | 3 + p11-kit/tests/files/system-modules/one.module | 3 + .../files/system-modules/two-duplicate.module | 3 + p11-kit/tests/files/system-modules/two.badname | 5 + .../tests/files/system-modules/win32/four.module | 3 + .../tests/files/system-modules/win32/one.module | 3 + .../system-modules/win32/two-duplicate.module | 3 + .../tests/files/system-modules/win32/two.badname | 5 + p11-kit/tests/files/system-pkcs11.conf | 3 + p11-kit/tests/files/test-1.conf | 6 + p11-kit/tests/files/test-pinfile | 1 + p11-kit/tests/files/test-pinfile-large | 53 + p11-kit/tests/files/test-system-invalid.conf | 3 + p11-kit/tests/files/test-system-merge.conf | 7 + p11-kit/tests/files/test-system-none.conf | 8 + p11-kit/tests/files/test-system-only.conf | 8 + p11-kit/tests/files/test-user-invalid.conf | 3 + p11-kit/tests/files/test-user-only.conf | 4 + p11-kit/tests/files/test-user.conf | 3 + p11-kit/tests/files/user-modules/one.module | 2 + p11-kit/tests/files/user-modules/three.module | 5 + p11-kit/tests/files/user-modules/win32/one.module | 2 + .../tests/files/user-modules/win32/three.module | 5 + p11-kit/tests/mock-module-ep.c | 52 + p11-kit/tests/mock-module.c | 898 ++++++++++++++ p11-kit/tests/mock-module.h | 337 ++++++ p11-kit/tests/pin-test.c | 325 ++++++ p11-kit/tests/print-messages.c | 137 +++ p11-kit/tests/progname-test.c | 97 ++ p11-kit/tests/test-init.c | 320 +++++ p11-kit/tests/test-modules.c | 246 ++++ p11-kit/tests/uri-test.c | 1223 ++++++++++++++++++++ p11-kit/uri.c | 6 +- p11-kit/util.c | 318 +---- p11-kit/util.h | 158 --- tests/Makefile.am | 76 -- tests/conf-test.c | 414 ------- tests/cutest/CuTest.c | 329 ------ tests/cutest/CuTest.h | 111 -- tests/cutest/README.txt | 211 ---- tests/cutest/license.txt | 38 - tests/files/system-modules/four.module | 3 - tests/files/system-modules/one.module | 3 - tests/files/system-modules/two-duplicate.module | 3 - tests/files/system-modules/two.badname | 5 - tests/files/system-modules/win32/four.module | 3 - tests/files/system-modules/win32/one.module | 3 - .../system-modules/win32/two-duplicate.module | 3 - tests/files/system-modules/win32/two.badname | 5 - tests/files/system-pkcs11.conf | 3 - tests/files/test-1.conf | 6 - tests/files/test-pinfile | 1 - tests/files/test-pinfile-large | 53 - tests/files/test-system-invalid.conf | 3 - tests/files/test-system-merge.conf | 7 - tests/files/test-system-none.conf | 8 - tests/files/test-system-only.conf | 8 - tests/files/test-user-invalid.conf | 3 - tests/files/test-user-only.conf | 4 - tests/files/test-user.conf | 3 - tests/files/user-modules/one.module | 2 - tests/files/user-modules/three.module | 5 - tests/files/user-modules/win32/one.module | 2 - tests/files/user-modules/win32/three.module | 5 - tests/hash-test.c | 465 -------- tests/mock-module-ep.c | 52 - tests/mock-module.c | 898 -------------- tests/mock-module.h | 337 ------ tests/pin-test.c | 323 ------ tests/print-messages.c | 137 --- tests/progname-test.c | 110 -- tests/ptr-array-test.c | 257 ---- tests/test-init.c | 319 ----- tests/test-modules.c | 245 ---- tests/uri-test.c | 1221 ------------------- 116 files changed, 7687 insertions(+), 7592 deletions(-) create mode 100644 build/Makefile.am create mode 100644 build/Makefile.tests create mode 100644 build/cutest/CuTest.c create mode 100644 build/cutest/CuTest.h create mode 100644 build/cutest/README.txt create mode 100644 build/cutest/license.txt create mode 100644 build/m4/.empty create mode 100644 common/Makefile.am create mode 100644 common/array.c create mode 100644 common/array.h create mode 100644 common/debug.c create mode 100644 common/debug.h create mode 100644 common/dict.c create mode 100644 common/dict.h create mode 100644 common/library.c create mode 100644 common/library.h create mode 100644 common/tests/Makefile.am create mode 100644 common/tests/test-array.c create mode 100644 common/tests/test-dict.c delete mode 100644 m4/.empty delete mode 100644 p11-kit/debug.c delete mode 100644 p11-kit/debug.h delete mode 100644 p11-kit/hashmap.c delete mode 100644 p11-kit/hashmap.h delete mode 100644 p11-kit/ptr-array.c delete mode 100644 p11-kit/ptr-array.h create mode 100644 p11-kit/tests/Makefile.am create mode 100644 p11-kit/tests/conf-test.c create mode 100644 p11-kit/tests/files/system-modules/four.module create mode 100644 p11-kit/tests/files/system-modules/one.module create mode 100644 p11-kit/tests/files/system-modules/two-duplicate.module create mode 100644 p11-kit/tests/files/system-modules/two.badname create mode 100644 p11-kit/tests/files/system-modules/win32/four.module create mode 100644 p11-kit/tests/files/system-modules/win32/one.module create mode 100644 p11-kit/tests/files/system-modules/win32/two-duplicate.module create mode 100644 p11-kit/tests/files/system-modules/win32/two.badname create mode 100644 p11-kit/tests/files/system-pkcs11.conf create mode 100644 p11-kit/tests/files/test-1.conf create mode 100644 p11-kit/tests/files/test-pinfile create mode 100644 p11-kit/tests/files/test-pinfile-large create mode 100644 p11-kit/tests/files/test-system-invalid.conf create mode 100644 p11-kit/tests/files/test-system-merge.conf create mode 100644 p11-kit/tests/files/test-system-none.conf create mode 100644 p11-kit/tests/files/test-system-only.conf create mode 100644 p11-kit/tests/files/test-user-invalid.conf create mode 100644 p11-kit/tests/files/test-user-only.conf create mode 100644 p11-kit/tests/files/test-user.conf create mode 100644 p11-kit/tests/files/user-modules/one.module create mode 100644 p11-kit/tests/files/user-modules/three.module create mode 100644 p11-kit/tests/files/user-modules/win32/one.module create mode 100644 p11-kit/tests/files/user-modules/win32/three.module create mode 100644 p11-kit/tests/mock-module-ep.c create mode 100644 p11-kit/tests/mock-module.c create mode 100644 p11-kit/tests/mock-module.h create mode 100644 p11-kit/tests/pin-test.c create mode 100644 p11-kit/tests/print-messages.c create mode 100644 p11-kit/tests/progname-test.c create mode 100644 p11-kit/tests/test-init.c create mode 100644 p11-kit/tests/test-modules.c create mode 100644 p11-kit/tests/uri-test.c delete mode 100644 p11-kit/util.h delete mode 100644 tests/Makefile.am delete mode 100644 tests/conf-test.c delete mode 100644 tests/cutest/CuTest.c delete mode 100644 tests/cutest/CuTest.h delete mode 100644 tests/cutest/README.txt delete mode 100644 tests/cutest/license.txt delete mode 100644 tests/files/system-modules/four.module delete mode 100644 tests/files/system-modules/one.module delete mode 100644 tests/files/system-modules/two-duplicate.module delete mode 100644 tests/files/system-modules/two.badname delete mode 100644 tests/files/system-modules/win32/four.module delete mode 100644 tests/files/system-modules/win32/one.module delete mode 100644 tests/files/system-modules/win32/two-duplicate.module delete mode 100644 tests/files/system-modules/win32/two.badname delete mode 100644 tests/files/system-pkcs11.conf delete mode 100644 tests/files/test-1.conf delete mode 100644 tests/files/test-pinfile delete mode 100644 tests/files/test-pinfile-large delete mode 100644 tests/files/test-system-invalid.conf delete mode 100644 tests/files/test-system-merge.conf delete mode 100644 tests/files/test-system-none.conf delete mode 100644 tests/files/test-system-only.conf delete mode 100644 tests/files/test-user-invalid.conf delete mode 100644 tests/files/test-user-only.conf delete mode 100644 tests/files/test-user.conf delete mode 100644 tests/files/user-modules/one.module delete mode 100644 tests/files/user-modules/three.module delete mode 100644 tests/files/user-modules/win32/one.module delete mode 100644 tests/files/user-modules/win32/three.module delete mode 100644 tests/hash-test.c delete mode 100644 tests/mock-module-ep.c delete mode 100644 tests/mock-module.c delete mode 100644 tests/mock-module.h delete mode 100644 tests/pin-test.c delete mode 100644 tests/print-messages.c delete mode 100644 tests/progname-test.c delete mode 100644 tests/ptr-array-test.c delete mode 100644 tests/test-init.c delete mode 100644 tests/test-modules.c delete mode 100644 tests/uri-test.c diff --git a/.gitignore b/.gitignore index 77f2789..79ed605 100644 --- a/.gitignore +++ b/.gitignore @@ -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/build/cutest/CuTest.c b/build/cutest/CuTest.c new file mode 100644 index 0000000..b033483 --- /dev/null +++ b/build/cutest/CuTest.c @@ -0,0 +1,329 @@ +#include +#include +#include +#include +#include +#include + +#include "CuTest.h" + +/*-------------------------------------------------------------------------* + * CuStr + *-------------------------------------------------------------------------*/ + +char* CuStrAlloc(int size) +{ + char* newStr = (char*) malloc( sizeof(char) * (size) ); + return newStr; +} + +char* CuStrCopy(const char* old) +{ + int len = strlen(old); + char* newStr = CuStrAlloc(len + 1); + strcpy(newStr, old); + return newStr; +} + +/*-------------------------------------------------------------------------* + * CuString + *-------------------------------------------------------------------------*/ + +void CuStringInit(CuString* str) +{ + str->length = 0; + str->size = STRING_MAX; + str->buffer = (char*) malloc(sizeof(char) * str->size); + str->buffer[0] = '\0'; +} + +CuString* CuStringNew(void) +{ + CuString* str = (CuString*) malloc(sizeof(CuString)); + str->length = 0; + str->size = STRING_MAX; + str->buffer = (char*) malloc(sizeof(char) * str->size); + str->buffer[0] = '\0'; + return str; +} + +void CuStringDelete(CuString *str) +{ + if (!str) return; + free(str->buffer); + free(str); +} + +void CuStringResize(CuString* str, int newSize) +{ + str->buffer = (char*) realloc(str->buffer, sizeof(char) * newSize); + str->size = newSize; +} + +void CuStringAppend(CuString* str, const char* text) +{ + int length; + + if (text == NULL) { + text = "NULL"; + } + + length = strlen(text); + if (str->length + length + 1 >= str->size) + CuStringResize(str, str->length + length + 1 + STRING_INC); + str->length += length; + strcat(str->buffer, text); +} + +void CuStringAppendChar(CuString* str, char ch) +{ + char text[2]; + text[0] = ch; + text[1] = '\0'; + CuStringAppend(str, text); +} + +void CuStringAppendFormat(CuString* str, const char* format, ...) +{ + va_list argp; + char buf[HUGE_STRING_LEN]; + va_start(argp, format); + vsprintf(buf, format, argp); + va_end(argp); + CuStringAppend(str, buf); +} + +void CuStringInsert(CuString* str, const char* text, int pos) +{ + int length = strlen(text); + if (pos > str->length) + pos = str->length; + if (str->length + length + 1 >= str->size) + CuStringResize(str, str->length + length + 1 + STRING_INC); + memmove(str->buffer + pos + length, str->buffer + pos, (str->length - pos) + 1); + str->length += length; + memcpy(str->buffer + pos, text, length); +} + +/*-------------------------------------------------------------------------* + * CuTest + *-------------------------------------------------------------------------*/ + +void CuTestInit(CuTest* t, const char* name, TestFunction function) +{ + t->name = CuStrCopy(name); + t->failed = 0; + t->ran = 0; + t->message = NULL; + t->function = function; + t->jumpBuf = NULL; +} + +CuTest* CuTestNew(const char* name, TestFunction function) +{ + CuTest* tc = CU_ALLOC(CuTest); + CuTestInit(tc, name, function); + return tc; +} + +void CuTestDelete(CuTest *t) +{ + if (!t) return; + free(t->name); + free(t); +} + +void CuTestRun(CuTest* tc) +{ + jmp_buf buf; + tc->jumpBuf = &buf; + if (setjmp(buf) == 0) + { + tc->ran = 1; + (tc->function)(tc); + } + tc->jumpBuf = 0; +} + +static void CuFailInternal(CuTest* tc, const char* file, int line, CuString* string) +{ + char buf[HUGE_STRING_LEN]; + + sprintf(buf, "%s:%d: ", file, line); + CuStringInsert(string, buf, 0); + + tc->failed = 1; + tc->message = string->buffer; + if (tc->jumpBuf != 0) longjmp(*(tc->jumpBuf), 0); +} + +void CuFail_Line(CuTest* tc, const char* file, int line, const char* message2, const char* message) +{ + CuString string; + + CuStringInit(&string); + if (message2 != NULL) + { + CuStringAppend(&string, message2); + CuStringAppend(&string, ": "); + } + CuStringAppend(&string, message); + CuFailInternal(tc, file, line, &string); +} + +void CuAssert_Line(CuTest* tc, const char* file, int line, const char* message, int condition) +{ + if (condition) return; + CuFail_Line(tc, file, line, NULL, message); +} + +void CuAssertStrEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message, + const char* expected, const char* actual) +{ + CuString string; + if ((expected == NULL && actual == NULL) || + (expected != NULL && actual != NULL && + strcmp(expected, actual) == 0)) + { + return; + } + + CuStringInit(&string); + if (message != NULL) + { + CuStringAppend(&string, message); + CuStringAppend(&string, ": "); + } + CuStringAppend(&string, "expected <"); + CuStringAppend(&string, expected); + CuStringAppend(&string, "> but was <"); + CuStringAppend(&string, actual); + CuStringAppend(&string, ">"); + CuFailInternal(tc, file, line, &string); +} + +void CuAssertIntEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message, + int expected, int actual) +{ + char buf[STRING_MAX]; + if (expected == actual) return; + sprintf(buf, "expected <%d> but was <%d>", expected, actual); + CuFail_Line(tc, file, line, message, buf); +} + +void CuAssertPtrEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message, + void* expected, void* actual) +{ + char buf[STRING_MAX]; + if (expected == actual) return; + sprintf(buf, "expected pointer <0x%p> but was <0x%p>", expected, actual); + CuFail_Line(tc, file, line, message, buf); +} + + +/*-------------------------------------------------------------------------* + * CuSuite + *-------------------------------------------------------------------------*/ + +void CuSuiteInit(CuSuite* testSuite) +{ + testSuite->count = 0; + testSuite->failCount = 0; + memset(testSuite->list, 0, sizeof(testSuite->list)); +} + +CuSuite* CuSuiteNew(void) +{ + CuSuite* testSuite = CU_ALLOC(CuSuite); + CuSuiteInit(testSuite); + return testSuite; +} + +void CuSuiteDelete(CuSuite *testSuite) +{ + unsigned int n; + for (n=0; n < MAX_TEST_CASES; n++) + { + if (testSuite->list[n]) + { + CuTestDelete(testSuite->list[n]); + } + } + free(testSuite); + +} + +void CuSuiteAdd(CuSuite* testSuite, CuTest *testCase) +{ + assert(testSuite->count < MAX_TEST_CASES); + testSuite->list[testSuite->count] = testCase; + testSuite->count++; +} + +void CuSuiteAddSuite(CuSuite* testSuite, CuSuite* testSuite2) +{ + int i; + for (i = 0 ; i < testSuite2->count ; ++i) + { + CuTest* testCase = testSuite2->list[i]; + CuSuiteAdd(testSuite, testCase); + } +} + +void CuSuiteRun(CuSuite* testSuite) +{ + int i; + for (i = 0 ; i < testSuite->count ; ++i) + { + CuTest* testCase = testSuite->list[i]; + CuTestRun(testCase); + if (testCase->failed) { testSuite->failCount += 1; } + } +} + +void CuSuiteSummary(CuSuite* testSuite, CuString* summary) +{ + int i; + for (i = 0 ; i < testSuite->count ; ++i) + { + CuTest* testCase = testSuite->list[i]; + CuStringAppend(summary, testCase->failed ? "F" : "."); + } + CuStringAppend(summary, "\n\n"); +} + +void CuSuiteDetails(CuSuite* testSuite, CuString* details) +{ + int i; + int failCount = 0; + + if (testSuite->failCount == 0) + { + int passCount = testSuite->count - testSuite->failCount; + const char* testWord = passCount == 1 ? "test" : "tests"; + CuStringAppendFormat(details, "OK (%d %s)\n", passCount, testWord); + } + else + { + if (testSuite->failCount == 1) + CuStringAppend(details, "There was 1 failure:\n"); + else + CuStringAppendFormat(details, "There were %d failures:\n", testSuite->failCount); + + for (i = 0 ; i < testSuite->count ; ++i) + { + CuTest* testCase = testSuite->list[i]; + if (testCase->failed) + { + failCount++; + CuStringAppendFormat(details, "%d) %s: %s\n", + failCount, testCase->name, testCase->message); + } + } + CuStringAppend(details, "\n!!!FAILURES!!!\n"); + + CuStringAppendFormat(details, "Runs: %d ", testSuite->count); + CuStringAppendFormat(details, "Passes: %d ", testSuite->count - testSuite->failCount); + CuStringAppendFormat(details, "Fails: %d\n", testSuite->failCount); + } +} diff --git a/build/cutest/CuTest.h b/build/cutest/CuTest.h new file mode 100644 index 0000000..b82d05b --- /dev/null +++ b/build/cutest/CuTest.h @@ -0,0 +1,111 @@ +#ifndef CU_TEST_H +#define CU_TEST_H + +#include +#include + +#define CUTEST_VERSION "CuTest 1.5" + +/* CuString */ + +char* CuStrAlloc(int size); +char* CuStrCopy(const char* old); + +#define CU_ALLOC(TYPE) ((TYPE*) malloc(sizeof(TYPE))) + +#define HUGE_STRING_LEN 8192 +#define STRING_MAX 256 +#define STRING_INC 256 + +typedef struct +{ + int length; + int size; + char* buffer; +} CuString; + +void CuStringInit(CuString* str); +CuString* CuStringNew(void); +void CuStringRead(CuString* str, const char* path); +void CuStringAppend(CuString* str, const char* text); +void CuStringAppendChar(CuString* str, char ch); +void CuStringAppendFormat(CuString* str, const char* format, ...); +void CuStringInsert(CuString* str, const char* text, int pos); +void CuStringResize(CuString* str, int newSize); +void CuStringDelete(CuString* str); + +/* CuTest */ + +typedef struct CuTest CuTest; + +typedef void (*TestFunction)(CuTest *); + +struct CuTest +{ + char* name; + TestFunction function; + int failed; + int ran; + const char* message; + jmp_buf *jumpBuf; +}; + +void CuTestInit(CuTest* t, const char* name, TestFunction function); +CuTest* CuTestNew(const char* name, TestFunction function); +void CuTestRun(CuTest* tc); +void CuTestDelete(CuTest *t); + +/* Internal versions of assert functions -- use the public versions */ +void CuFail_Line(CuTest* tc, const char* file, int line, const char* message2, const char* message); +void CuAssert_Line(CuTest* tc, const char* file, int line, const char* message, int condition); +void CuAssertStrEquals_LineMsg(CuTest* tc, + const char* file, int line, const char* message, + const char* expected, const char* actual); +void CuAssertIntEquals_LineMsg(CuTest* tc, + const char* file, int line, const char* message, + int expected, int actual); +void CuAssertPtrEquals_LineMsg(CuTest* tc, + const char* file, int line, const char* message, + void* expected, void* actual); + +/* public assert functions */ + +#define CuFail(tc, ms) CuFail_Line( (tc), __FILE__, __LINE__, NULL, (ms)) +#define CuAssert(tc, ms, cond) CuAssert_Line((tc), __FILE__, __LINE__, (ms), (cond)) +#define CuAssertTrue(tc, cond) CuAssert_Line((tc), __FILE__, __LINE__, "assert failed", (cond)) + +#define CuAssertStrEquals(tc,ex,ac) CuAssertStrEquals_LineMsg((tc),__FILE__,__LINE__,NULL,(ex),(ac)) +#define CuAssertStrEquals_Msg(tc,ms,ex,ac) CuAssertStrEquals_LineMsg((tc),__FILE__,__LINE__,(ms),(ex),(ac)) +#define CuAssertIntEquals(tc,ex,ac) CuAssertIntEquals_LineMsg((tc),__FILE__,__LINE__,NULL,(ex),(ac)) +#define CuAssertIntEquals_Msg(tc,ms,ex,ac) CuAssertIntEquals_LineMsg((tc),__FILE__,__LINE__,(ms),(ex),(ac)) +#define CuAssertPtrEquals(tc,ex,ac) CuAssertPtrEquals_LineMsg((tc),__FILE__,__LINE__,NULL,(ex),(ac)) +#define CuAssertPtrEquals_Msg(tc,ms,ex,ac) CuAssertPtrEquals_LineMsg((tc),__FILE__,__LINE__,(ms),(ex),(ac)) + +#define CuAssertPtrNotNull(tc,p) CuAssert_Line((tc),__FILE__,__LINE__,"null pointer unexpected",(p != NULL)) +#define CuAssertPtrNotNullMsg(tc,msg,p) CuAssert_Line((tc),__FILE__,__LINE__,(msg),(p != NULL)) + +/* CuSuite */ + +#define MAX_TEST_CASES 1024 + +#define SUITE_ADD_TEST(SUITE,TEST) CuSuiteAdd(SUITE, CuTestNew(#TEST, TEST)) + +typedef struct +{ + int count; + CuTest* list[MAX_TEST_CASES]; + int failCount; + +} CuSuite; + + +void CuSuiteInit(CuSuite* testSuite); +CuSuite* CuSuiteNew(void); +void CuSuiteDelete(CuSuite *testSuite); +void CuSuiteAdd(CuSuite* testSuite, CuTest *testCase); +void CuSuiteAddSuite(CuSuite* testSuite, CuSuite* testSuite2); +void CuSuiteRun(CuSuite* testSuite); +void CuSuiteSummary(CuSuite* testSuite, CuString* summary); +void CuSuiteDetails(CuSuite* testSuite, CuString* details); + +#endif /* CU_TEST_H */ diff --git a/build/cutest/README.txt b/build/cutest/README.txt new file mode 100644 index 0000000..96e8853 --- /dev/null +++ b/build/cutest/README.txt @@ -0,0 +1,211 @@ +HOW TO USE + +You can use CuTest to create unit tests to drive your development +in the style of Extreme Programming. You can also add unit tests to +existing code to ensure that it works as you suspect. + +Your unit tests are an investment. They let you to change your +code and add new features confidently without worrying about +accidentally breaking earlier features. + + +LICENSING + +For details on licensing see license.txt. + + +GETTING STARTED + +To add unit testing to your C code the only files you need are +CuTest.c and CuTest.h. + +CuTestTest.c and AllTests.c have been included to provide an +example of how to write unit tests and then how to aggregate them +into suites and into a single AllTests.c file. Suites allow you +to put group tests into logical sets. AllTests.c combines all the +suites and runs them. + +You should not have to look inside CuTest.c. Looking in +CuTestTest.c and AllTests.c (for example usage) should be +sufficient. + +After downloading the sources, run your compiler to create an +executable called AllTests.exe. For example, if you are using +Windows with the cl.exe compiler you would type: + + cl.exe AllTests.c CuTest.c CuTestTest.c + AllTests.exe + +This will run all the unit tests associated with CuTest and print +the output on the console. You can replace cl.exe with gcc or +your favorite compiler in the command above. + + +DETAILED EXAMPLE + +Here is a more detailed example. We will work through a simple +test first exercise. The goal is to create a library of string +utilities. First, lets write a function that converts a +null-terminated string to all upper case. + +Ensure that CuTest.c and CuTest.h are accessible from your C +project. Next, create a file called StrUtil.c with these +contents: + + #include "CuTest.h" + + char* StrToUpper(char* str) { + return str; + } + + void TestStrToUpper(CuTest *tc) { + char* input = strdup("hello world"); + char* actual = StrToUpper(input); + char* expected = "HELLO WORLD"; + CuAssertStrEquals(tc, expected, actual); + } + + CuSuite* StrUtilGetSuite() { + CuSuite* suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, TestStrToUpper); + return suite; + } + +Create another file called AllTests.c with these contents: + + #include "CuTest.h" + + CuSuite* StrUtilGetSuite(); + + void RunAllTests(void) { + CuString *output = CuStringNew(); + CuSuite* suite = CuSuiteNew(); + + CuSuiteAddSuite(suite, StrUtilGetSuite()); + + CuSuiteRun(suite); + CuSuiteSummary(suite, output); + CuSuiteDetails(suite, output); + printf("%s\n", output->buffer); + } + + int main(void) { + RunAllTests(); + } + +Then type this on the command line: + + gcc AllTests.c CuTest.c StrUtil.c + +to compile. You can replace gcc with your favorite compiler. +CuTest should be portable enough to handle all Windows and Unix +compilers. Then to run the tests type: + + a.out + +This will print an error because we haven't implemented the +StrToUpper function correctly. We are just returning the string +without changing it to upper case. + + char* StrToUpper(char* str) { + return str; + } + +Rewrite this as follows: + + char* StrToUpper(char* str) { + char* p; + for (p = str ; *p ; ++p) *p = toupper(*p); + return str; + } + +Recompile and run the tests again. The test should pass this +time. + + +WHAT TO DO NEXT + +At this point you might want to write more tests for the +StrToUpper function. Here are some ideas: + +TestStrToUpper_EmptyString : pass in "" +TestStrToUpper_UpperCase : pass in "HELLO WORLD" +TestStrToUpper_MixedCase : pass in "HELLO world" +TestStrToUpper_Numbers : pass in "1234 hello" + +As you write each one of these tests add it to StrUtilGetSuite +function. If you don't the tests won't be run. Later as you write +other functions and write tests for them be sure to include those +in StrUtilGetSuite also. The StrUtilGetSuite function should +include all the tests in StrUtil.c + +Over time you will create another file called FunkyStuff.c +containing other functions unrelated to StrUtil. Follow the same +pattern. Create a FunkyStuffGetSuite function in FunkyStuff.c. +And add FunkyStuffGetSuite to AllTests.c. + +The framework is designed in the way it is so that it is easy to +organize a lot of tests. + +THE BIG PICTURE + +Each individual test corresponds to a CuTest. These are grouped +to form a CuSuite. CuSuites can hold CuTests or other CuSuites. +AllTests.c collects all the CuSuites in the program into a single +CuSuite which it then runs as a single CuSuite. + +The project is open source so feel free to take a peek under the +hood at the CuTest.c file to see how it works. CuTestTest.c +contains tests for CuTest.c. So CuTest tests itself. + +Since AllTests.c has a main() you will need to exclude this when +you are building your product. Here is a nicer way to do this if +you want to avoid messing with multiple builds. Remove the main() +in AllTests.c. Note that it just calls RunAllTests(). Instead +we'll call this directly from the main program. + +Now in the main() of the actual program check to see if the +command line option "--test" was passed. If it was then I call +RunAllTests() from AllTests.c. Otherwise run the real program. + +Shipping the tests with the code can be useful. If you customers +complain about a problem you can ask them to run the unit tests +and send you the output. This can help you to quickly isolate the +piece of your system that is malfunctioning in the customer's +environment. + +CuTest offers a rich set of CuAssert functions. Here is a list: + +void CuAssert(CuTest* tc, char* message, int condition); +void CuAssertTrue(CuTest* tc, int condition); +void CuAssertStrEquals(CuTest* tc, char* expected, char* actual); +void CuAssertIntEquals(CuTest* tc, int expected, int actual); +void CuAssertPtrEquals(CuTest* tc, void* expected, void* actual); +void CuAssertPtrNotNull(CuTest* tc, void* pointer); + +The project is open source and so you can add other more powerful +asserts to make your tests easier to write and more concise. +Please feel free to send me changes you make so that I can +incorporate them into future releases. + +If you see any errors in this document please contact me at +asimjalis@peakprogramming.com. + + +AUTOMATING TEST SUITE GENERATION + +make-tests.sh will grep through all the .c files in the current +directory and generate the code to run all the tests contained in +them. Using this script you don't have to worry about writing +AllTests.c or dealing with any of the other suite code. + + +CREDITS + +These people have contributed useful code changes to the CuTest project. +Thanks! + +- [02.23.2003] Dave Glowacki +- [04.17.2009] Tobias Lippert +- [11.13.2009] Eli Bendersky +- [12.14.2009] Andrew Brown diff --git a/build/cutest/license.txt b/build/cutest/license.txt new file mode 100644 index 0000000..3d94167 --- /dev/null +++ b/build/cutest/license.txt @@ -0,0 +1,38 @@ +NOTE + +The license is based on the zlib/libpng license. For more details see +http://www.opensource.org/licenses/zlib-license.html. The intent of the +license is to: + +- keep the license as simple as possible +- encourage the use of CuTest in both free and commercial applications + and libraries +- keep the source code together +- give credit to the CuTest contributors for their work + +If you ship CuTest in source form with your source distribution, the +following license document must be included with it in unaltered form. +If you find CuTest useful we would like to hear about it. + +LICENSE + +Copyright (c) 2003 Asim Jalis + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any damages +arising from the use of this software. + +Permission is granted to anyone to use this software for any purpose, +including commercial applications, and to alter it and redistribute it +freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must not +claim that you wrote the original software. If you use this software in +a product, an acknowledgment in the product documentation would be +appreciated but is not required. + +2. Altered source versions must be plainly marked as such, and must not +be misrepresented as being the original software. + +3. This notice may not be removed or altered from any source +distribution. diff --git a/build/m4/.empty b/build/m4/.empty new file mode 100644 index 0000000..be533a1 --- /dev/null +++ b/build/m4/.empty @@ -0,0 +1 @@ +Stub file to track in git 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/common/array.c b/common/array.c new file mode 100644 index 0000000..f645fd1 --- /dev/null +++ b/common/array.c @@ -0,0 +1,119 @@ +/* + * 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. + */ + +#include "config.h" + +#include "array.h" + +#include +#include + +static int +maybe_expand_array (p11_array *array, + unsigned int length) +{ + unsigned int new_allocated; + void **new_memory; + + if (length <= array->allocated) + return 1; + + new_allocated = array->allocated + 16; + if (new_allocated < length) + new_allocated = length; + + new_memory = realloc (array->elem, new_allocated * sizeof (void*)); + if (new_memory == NULL) + return 0; + + array->elem = new_memory; + array->allocated = new_allocated; + return 1; +} + +p11_array * +p11_array_new (p11_destroyer destroyer) +{ + p11_array *array; + + array = calloc (1, sizeof (p11_array)); + if (array == NULL) + return NULL; + + if (!maybe_expand_array (array, 2)) { + p11_array_free (array); + return NULL; + } + + array->destroyer = destroyer; + return array; +} + +void +p11_array_free (p11_array *array) +{ + unsigned int i; + + if (array == NULL) + return; + + if (array->destroyer) { + for (i = 0; i < array->num; i++) + (array->destroyer) (array->elem[i]); + } + + free (array->elem); + free (array); +} + +int +p11_array_push (p11_array *array, + void *value) +{ + if (!maybe_expand_array (array, array->num + 1)) + return 0; + + array->elem[array->num] = value; + array->num++; + return 1; +} + +void +p11_array_remove (p11_array *array, + unsigned int index) +{ + 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/common/array.h b/common/array.h new file mode 100644 index 0000000..6ed7866 --- /dev/null +++ b/common/array.h @@ -0,0 +1,66 @@ +/* + * 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 Waler + */ + +#ifndef __P11_ARRAY_H__ +#define __P11_ARRAY_H__ + +#include + +#ifndef P11_DESTROYER_DEFINED +#define P11_DESTROYER_DEFINED + +typedef void (*p11_destroyer) (void *data); + +#endif + +typedef struct _p11_array { + void **elem; + unsigned int num; + + /* private */ + unsigned int allocated; + p11_destroyer destroyer; +} p11_array; + +p11_array * p11_array_new (p11_destroyer destroyer); + +void p11_array_free (p11_array *array); + +int p11_array_push (p11_array *array, + void *value); + +void p11_array_remove (p11_array *array, + unsigned int index); + +#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 #include #include @@ -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 @@ -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 + #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 + +/* 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 +#include +#include + +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 @@ -80,4 +196,11 @@ void vwarnx (const char *fmt, va_list ap); #include #endif /* HAVE_ERRNO_H */ +#ifndef HAVE_MEMDUP + +void * memdup (void *data, + size_t length); + +#endif /* HAVE_MEMDUP */ + #endif /* __COMPAT_H__ */ diff --git a/common/debug.c b/common/debug.c new file mode 100644 index 0000000..cca9aaf --- /dev/null +++ b/common/debug.c @@ -0,0 +1,151 @@ +/* + * 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 + */ + +#include "config.h" + +#include "debug.h" + +#include +#include +#include +#include +#include +#include + +struct DebugKey { + const char *name; + int value; +}; + +static struct DebugKey debug_keys[] = { + { "lib", P11_DEBUG_LIB }, + { "conf", P11_DEBUG_CONF }, + { "uri", P11_DEBUG_URI }, + { "proxy", P11_DEBUG_PROXY }, + { 0, } +}; + +static int debug_inited = 0; +static int debug_strict = 0; + +/* global variable exported in debug.h */ +int p11_debug_current_flags = ~0; + +static int +parse_environ_flags (void) +{ + const char *env; + int result = 0; + const char *p; + const char *q; + int i; + + env = getenv ("P11_KIT_STRICT"); + if (env && env[0] != '\0') + debug_strict = 1; + + env = getenv ("P11_KIT_DEBUG"); + if (!env) + return 0; + + if (strcmp (env, "all") == 0) { + for (i = 0; debug_keys[i].name; i++) + result |= debug_keys[i].value; + + } else if (strcmp (env, "help") == 0) { + fprintf (stderr, "Supported debug values:"); + for (i = 0; debug_keys[i].name; i++) + fprintf (stderr, " %s", debug_keys[i].name); + fprintf (stderr, "\n"); + + } else { + p = env; + while (*p) { + q = strpbrk (p, ":;, \t"); + if (!q) + q = p + strlen (p); + + for (i = 0; debug_keys[i].name; i++) { + if (q - p == strlen (debug_keys[i].name) && + strncmp (debug_keys[i].name, p, q - p) == 0) + result |= debug_keys[i].value; + } + + p = q; + if (*p) + p++; + } + } + + return result; +} + +void +p11_debug_init (void) +{ + p11_debug_current_flags = parse_environ_flags (); + debug_inited = 1; +} + +void +p11_debug_message (int flag, + const char *format, ...) +{ + char buffer[512]; + va_list args; + + if (flag & p11_debug_current_flags) { + va_start (args, format); + vsnprintf (buffer, sizeof (buffer), format, args); + buffer[sizeof (buffer) -1] = 0; + va_end (args); + fprintf (stderr, "(p11-kit:%d) %s\n", getpid(), buffer); + } +} + +void +p11_debug_precond (const char *format, + ...) +{ + va_list va; + + va_start (va, format); + vfprintf (stderr, format, va); + va_end (va); + + if (debug_strict) + abort (); +} diff --git a/common/debug.h b/common/debug.h new file mode 100644 index 0000000..e4759d4 --- /dev/null +++ b/common/debug.h @@ -0,0 +1,129 @@ +/* + * 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 + */ + +#ifndef P11_DEBUG_H +#define P11_DEBUG_H + +#include "compat.h" + +/* Please keep this enum in sync with keys in debug.c */ +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; + +void p11_debug_init (void); + +void p11_debug_message (int flag, + const char *format, + ...) GNUC_PRINTF (2, 3); + +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__); \ + 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__); \ + return; \ + } } while (0) + +#define return_if_reached() \ + do { \ + 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__); \ + return v; \ + } while (0) + +#endif /* DEBUG_H */ + +/* ----------------------------------------------------------------------------- + * Below this point is outside the DEBUG_H guard - so it can take effect + * more than once. So you can do: + * + * #define P11_DEBUG_FLAG P11_DEBUG_ONE_THING + * #include "debug.h" + * ... + * p11_debug ("if we're debugging one thing"); + * ... + * #undef P11_DEBUG_FLAG + * #define P11_DEBUG_FLAG DEBUG_OTHER_THING + * #include "debug.h" + * ... + * p11_debug ("if we're debugging the other thing"); + * ... + */ + +#ifdef P11_DEBUG_FLAG +#ifdef WITH_DEBUG + +#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 \ + (P11_DEBUG_FLAG & p11_debug_current_flags) + +#else /* !defined (WITH_DEBUG) */ + +#undef p11_debug +#define p11_debug(format, ...) \ + do {} while (0) + +#undef p11_debugging +#define p11_debugging 0 + +#endif /* !defined (WITH_DEBUG) */ + +#endif /* defined (P11_DEBUG_FLAG) */ diff --git a/common/dict.c b/common/dict.c new file mode 100644 index 0000000..2f976c1 --- /dev/null +++ b/common/dict.c @@ -0,0 +1,391 @@ +/* + * Copyright (c) 2004 Stefan Walter + * 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. + */ + +#include "config.h" + +#include "dict.h" + +#include + +#include +#include +#include + +struct _p11_dict { + p11_dict_hasher hash_func; + p11_dict_equals equal_func; + p11_destroyer key_destroy_func; + p11_destroyer value_destroy_func; + + struct _p11_dictbucket **buckets; + unsigned int num_items; + unsigned int num_buckets; +}; + +typedef struct _p11_dictbucket { + void *key; + unsigned int hashed; + void *value; + struct _p11_dictbucket *next; +} dictbucket; + +static dictbucket * +next_entry (p11_dictiter *iter) +{ + dictbucket *bucket = iter->next; + while (!bucket) { + if (iter->index >= iter->dict->num_buckets) + return NULL; + bucket = iter->dict->buckets[iter->index++]; + } + iter->next = bucket->next; + return bucket; +} + + +int +p11_dict_next (p11_dictiter *iter, + void **key, + void **value) +{ + dictbucket *bucket = next_entry (iter); + if (bucket == NULL) + return 0; + if (key) + *key = bucket->key; + if (value) + *value = bucket->value; + return 1; +} + +void +p11_dict_iterate (p11_dict *dict, + p11_dictiter *iter) +{ + iter->dict = dict; + iter->index = 0; + iter->next = NULL; +} + +static dictbucket ** +lookup_or_create_bucket (p11_dict *dict, + const void *key, + int create) +{ + dictbucket **bucketp; + unsigned int hash; + + /* Perform the hashing */ + hash = dict->hash_func (key); + + /* scan linked list */ + for (bucketp = &dict->buckets[hash % dict->num_buckets]; + *bucketp != NULL; bucketp = &(*bucketp)->next) { + if((*bucketp)->hashed == hash && dict->equal_func ((*bucketp)->key, key)) + break; + } + + if ((*bucketp) != NULL || !create) + return bucketp; + + /* add a new entry for non-NULL val */ + (*bucketp) = calloc (sizeof (dictbucket), 1); + + if (*bucketp != NULL) { + (*bucketp)->key = (void*)key; + (*bucketp)->hashed = hash; + dict->num_items++; + } + + return bucketp; +} + +void * +p11_dict_get (p11_dict *dict, + const void *key) +{ + dictbucket **bucketp; + + bucketp = lookup_or_create_bucket (dict, key, 0); + if (bucketp && *bucketp) + return (void*)((*bucketp)->value); + else + return NULL; +} + +int +p11_dict_set (p11_dict *dict, + void *key, + void *val) +{ + dictbucket **bucketp; + p11_dictiter iter; + dictbucket *bucket; + dictbucket **new_buckets; + unsigned int num_buckets; + + bucketp = lookup_or_create_bucket (dict, key, 1); + if(bucketp && *bucketp) { + + /* Destroy the previous 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 && 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 (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_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 (dict->buckets); + dict->buckets = new_buckets; + dict->num_buckets = num_buckets; + } + } + + return 1; + } + + return 0; +} + +int +p11_dict_steal (p11_dict *dict, + const void *key, + void **stolen_key, + void **stolen_value) +{ + dictbucket **bucketp; + + bucketp = lookup_or_create_bucket (dict, key, 0); + if (bucketp && *bucketp) { + dictbucket *old = *bucketp; + *bucketp = (*bucketp)->next; + --dict->num_items; + if (stolen_key) + *stolen_key = old->key; + if (stolen_value) + *stolen_value = old->value; + free (old); + return 1; + } + + return 0; + +} + +int +p11_dict_remove (p11_dict *dict, + const void *key) +{ + void *old_key; + void *old_value; + + if (!p11_dict_steal (dict, key, &old_key, &old_value)) + return 0; + + 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_dict_clear (p11_dict *dict) +{ + dictbucket *bucket, *next; + int i; + + /* Free all entries in the array */ + for (i = 0; i < dict->num_buckets; ++i) { + bucket = dict->buckets[i]; + while (bucket != NULL) { + next = bucket->next; + 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 (dict->buckets, 0, dict->num_buckets * sizeof (dictbucket *)); + dict->num_items = 0; +} + +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) +{ + p11_dict *dict; + + assert (hash_func); + assert (equal_func); + + 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; + } + + dict->num_items = 0; + } + + return dict; +} + +void +p11_dict_free (p11_dict *dict) +{ + dictbucket *bucket; + p11_dictiter iter; + + if (!dict) + return; + + p11_dict_iterate (dict, &iter); + while ((bucket = next_entry (&iter)) != NULL) { + 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 (dict->buckets) + free (dict->buckets); + + free (dict); +} + +unsigned int +p11_dict_size (p11_dict *dict) +{ + return dict->num_items; +} + +unsigned int +p11_dict_str_hash (const void *string) +{ + const char *p = string; + unsigned int hash = *p; + + if (hash) + for (p += 1; *p != '\0'; p++) + hash = (hash << 5) - hash + *p; + + return hash; +} + +int +p11_dict_str_equal (const void *string_one, + const void *string_two) +{ + assert (string_one); + assert (string_two); + + return strcmp (string_one, string_two) == 0; +} + +unsigned int +p11_dict_ulongptr_hash (const void *to_ulong) +{ + assert (to_ulong); + return (unsigned int)*((unsigned long*)to_ulong); +} + +int +p11_dict_ulongptr_equal (const void *ulong_one, + const void *ulong_two) +{ + assert (ulong_one); + assert (ulong_two); + return *((unsigned long*)ulong_one) == *((unsigned long*)ulong_two); +} + +unsigned int +p11_dict_intptr_hash (const void *to_int) +{ + assert (to_int); + return (unsigned int)*((int*)to_int); +} + +int +p11_dict_intptr_equal (const void *int_one, + const void *int_two) +{ + assert (int_one); + assert (int_two); + return *((int*)int_one) == *((int*)int_two); +} + +unsigned int +p11_dict_direct_hash (const void *ptr) +{ + return (unsigned int)(size_t)ptr; +} + +int +p11_dict_direct_equal (const void *ptr_one, + const void *ptr_two) +{ + return ptr_one == ptr_two; +} diff --git a/common/dict.h b/common/dict.h new file mode 100644 index 0000000..1ba7185 --- /dev/null +++ b/common/dict.h @@ -0,0 +1,178 @@ +/* + * Copyright (c) 2004 Stefan Walter + * 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 Waler + */ + +#ifndef P11_DICT_H_ +#define P11_DICT_H_ + +#include + +/* + * ARGUMENT DOCUMENTATION + * + * dict: The dict + * key: Pointer to the key value + * val: Pointer to the value + * iter: A dict iterator + */ + + +/* ---------------------------------------------------------------------------------- + * TYPES + */ + +/* Abstract type for dicts. */ +typedef struct _p11_dict p11_dict; + +/* Type for scanning hash tables. */ +typedef struct _p11_dictiter { + p11_dict *dict; + struct _p11_dictbucket *next; + unsigned int index; +} p11_dictiter; + +typedef unsigned int (*p11_dict_hasher) (const void *data); + +typedef int (*p11_dict_equals) (const void *one, + const void *two); + +#ifndef P11_DESTROYER_DEFINED +#define P11_DESTROYER_DEFINED + +typedef void (*p11_destroyer) (void *data); + +#endif + +/* ----------------------------------------------------------------------------- + * MAIN + */ + +/* + * p11_dict_create : Create a hash table + * - returns an allocated hashtable + */ +p11_dict * p11_dict_new (p11_dict_hasher hasher, + p11_dict_equals equals, + p11_destroyer key_destroyer, + p11_destroyer value_destroyer); + +/* + * p11_dict_free : Free a hash table + */ +void p11_dict_free (p11_dict *dict); + +/* + * p11_dict_size: Number of values in hash table + * - returns the number of entries in hash table + */ +unsigned int p11_dict_size (p11_dict *dict); + +/* + * p11_dict_get: Retrieves a value from the hash table + * - returns the value of the entry + */ +void* p11_dict_get (p11_dict *dict, + const void *key); + +/* + * p11_dict_set: Set a value in the hash table + * - returns 1 if the entry was added properly + */ +int p11_dict_set (p11_dict *dict, + void *key, + void *value); + +/* + * p11_dict_remove: Remove a value from the hash table + * - returns 1 if the entry was found + */ +int p11_dict_remove (p11_dict *dict, + const void *key); + +/* + * p11_dict_steal: Remove a value from the hash table without calling + * destroy funcs + * - returns 1 if the entry was found + */ +int p11_dict_steal (p11_dict *dict, + const void *key, + void **stolen_key, + void **stolen_value); + +/* + * p11_dict_iterate: Start enumerating through the hash table + * - returns a hash iterator + */ +void p11_dict_iterate (p11_dict *dict, + p11_dictiter *iter); + +/* + * p11_dict_next: Enumerate through hash table + * - sets key and value to key and/or value + * - returns whether there was another entry + */ +int p11_dict_next (p11_dictiter *iter, + void **key, + void **value); + +/* + * p11_dict_clear: Clear all values from has htable. + */ +void p11_dict_clear (p11_dict *dict); + +/* ----------------------------------------------------------------------------- + * KEY FUNCTIONS + */ + +unsigned int p11_dict_str_hash (const void *string); + +int p11_dict_str_equal (const void *string_one, + const void *string_two); + +unsigned int p11_dict_ulongptr_hash (const void *to_ulong); + +int p11_dict_ulongptr_equal (const void *ulong_one, + const void *ulong_two); + +unsigned int p11_dict_intptr_hash (const void *to_int); + +int p11_dict_intptr_equal (const void *int_one, + const void *int_two); + +unsigned int p11_dict_direct_hash (const void *ptr); + +int p11_dict_direct_equal (const void *ptr_one, + const void *ptr_two); + +#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 + */ + +#include "config.h" + +#include "compat.h" +#define P11_DEBUG_FLAG P11_DEBUG_LIB +#include "debug.h" +#include "library.h" + +#include +#include +#include +#include +#include + +#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 + */ + +#ifndef P11_LIBRARY_H_ +#define P11_LIBRARY_H_ + +#include "config.h" +#include "compat.h" + +#include + +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 + */ + +#include "config.h" +#include "CuTest.h" + +#include +#include +#include + +#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/common/tests/test-dict.c b/common/tests/test-dict.c new file mode 100644 index 0000000..3af3daa --- /dev/null +++ b/common/tests/test-dict.c @@ -0,0 +1,465 @@ +/* + * 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 + */ + +#include "config.h" +#include "CuTest.h" + +#include +#include +#include +#include + +#include "dict.h" + +static void +test_create (CuTest *tc) +{ + p11_dict *map; + + map = p11_dict_new (p11_dict_direct_hash, p11_dict_direct_equal, NULL, NULL); + CuAssertPtrNotNull (tc, map); + p11_dict_free (map); +} + +static void +test_free_null (CuTest *tc) +{ + p11_dict_free (NULL); +} + +typedef struct { + int value; + int freed; +} Key; + +static unsigned int +key_hash (const void *ptr) +{ + const Key *k = ptr; + assert (!k->freed); + return p11_dict_intptr_hash (&k->value); +} + +static int +key_equal (const void *one, + const void *two) +{ + const Key *k1 = one; + const Key *k2 = two; + assert (!k1->freed); + assert (!k2->freed); + return p11_dict_intptr_equal (&k1->value, &k2->value); +} + +static void +key_destroy (void *data) +{ + Key *k = data; + assert (!k->freed); + k->freed = 1; +} + +static void +value_destroy (void *data) +{ + int *value = data; + *value = 2; +} + +static void +test_free_destroys (CuTest *tc) +{ + p11_dict *map; + Key key = { 8, 0 }; + int value = 0; + + map = p11_dict_new (key_hash, key_equal, key_destroy, value_destroy); + CuAssertPtrNotNull (tc, map); + if (!p11_dict_set (map, &key, &value)) + CuFail (tc, "should not be reached"); + p11_dict_free (map); + + CuAssertIntEquals (tc, 1, key.freed); + CuAssertIntEquals (tc, 2, value); +} + +static void +test_iterate (CuTest *tc) +{ + p11_dict *map; + p11_dictiter iter; + int key = 1; + int value = 2; + void *pkey; + void *pvalue; + int ret; + + map = p11_dict_new (p11_dict_direct_hash, p11_dict_direct_equal, NULL, NULL); + CuAssertPtrNotNull (tc, map); + if (!p11_dict_set (map, &key, &value)) + CuFail (tc, "should not be reached"); + + p11_dict_iterate (map, &iter); + + ret = p11_dict_next (&iter, &pkey, &pvalue); + CuAssertIntEquals (tc, 1, ret); + CuAssertPtrEquals (tc, pkey, &key); + CuAssertPtrEquals (tc, pvalue, &value); + + ret = p11_dict_next (&iter, &pkey, &pvalue); + CuAssertIntEquals (tc, 0, ret); + + p11_dict_free (map); +} + +static void +test_set_get (CuTest *tc) +{ + char *key = "KEY"; + char *value = "VALUE"; + char *check; + p11_dict *map; + + 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_dict_free (map); +} + +static void +test_set_get_remove (CuTest *tc) +{ + char *key = "KEY"; + char *value = "VALUE"; + char *check; + p11_dict *map; + int ret; + + map = p11_dict_new (p11_dict_str_hash, p11_dict_str_equal, NULL, NULL); + + if (!p11_dict_set (map, key, value)) + CuFail (tc, "should not be reached"); + + check = p11_dict_get (map, key); + CuAssertPtrEquals (tc, check, value); + + ret = p11_dict_remove (map, key); + CuAssertIntEquals (tc, ret, 1); + ret = p11_dict_remove (map, key); + CuAssertIntEquals (tc, ret, 0); + + check = p11_dict_get (map, key); + CuAssert (tc, "should be null", check == NULL); + + p11_dict_free (map); +} + +static void +test_set_clear (CuTest *tc) +{ + char *key = "KEY"; + char *value = "VALUE"; + char *check; + p11_dict *map; + + map = p11_dict_new (p11_dict_direct_hash, p11_dict_direct_equal, NULL, NULL); + + if (!p11_dict_set (map, key, value)) + CuFail (tc, "should not be reached"); + + p11_dict_clear (map); + + check = p11_dict_get (map, key); + CuAssert (tc, "should be null", check == NULL); + + p11_dict_free (map); +} + +static void +test_remove_destroys (CuTest *tc) +{ + p11_dict *map; + Key key = { 8, 0 }; + int value = 0; + int ret; + + map = p11_dict_new (key_hash, key_equal, key_destroy, value_destroy); + CuAssertPtrNotNull (tc, map); + if (!p11_dict_set (map, &key, &value)) + CuFail (tc, "should not be reached"); + + ret = p11_dict_remove (map, &key); + CuAssertIntEquals (tc, ret, 1); + CuAssertIntEquals (tc, 1, key.freed); + CuAssertIntEquals (tc, 2, value); + + /* should not be destroyed again */ + key.freed = 0; + value = 0; + + ret = p11_dict_remove (map, &key); + CuAssertIntEquals (tc, ret, 0); + CuAssertIntEquals (tc, 0, key.freed); + CuAssertIntEquals (tc, 0, value); + + /* should not be destroyed again */ + key.freed = 0; + value = 0; + + p11_dict_free (map); + + CuAssertIntEquals (tc, 0, key.freed); + CuAssertIntEquals (tc, 0, value); +} + +static void +test_set_destroys (CuTest *tc) +{ + p11_dict *map; + Key key = { 8, 0 }; + Key key2 = { 8, 0 }; + int value, value2; + int ret; + + map = p11_dict_new (key_hash, key_equal, key_destroy, value_destroy); + CuAssertPtrNotNull (tc, map); + 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_dict_set (map, &key, &value); + CuAssertIntEquals (tc, ret, 1); + CuAssertIntEquals (tc, 0, key.freed); + CuAssertIntEquals (tc, 0, key2.freed); + CuAssertIntEquals (tc, 0, value); + CuAssertIntEquals (tc, 0, value2); + + key.freed = key2.freed = value = value2 = 0; + + /* Setting a new key same value, key should be destroyed */ + ret = p11_dict_set (map, &key2, &value); + CuAssertIntEquals (tc, ret, 1); + CuAssertIntEquals (tc, 1, key.freed); + CuAssertIntEquals (tc, 0, key2.freed); + CuAssertIntEquals (tc, 0, value); + CuAssertIntEquals (tc, 0, value2); + + key.freed = key2.freed = value = value2 = 0; + + /* Setting same key, new value, value should be destroyed */ + ret = p11_dict_set (map, &key2, &value2); + CuAssertIntEquals (tc, ret, 1); + CuAssertIntEquals (tc, 0, key.freed); + CuAssertIntEquals (tc, 0, key2.freed); + CuAssertIntEquals (tc, 2, value); + CuAssertIntEquals (tc, 0, value2); + + key.freed = key2.freed = value = value2 = 0; + + /* Setting new key new value, both should be destroyed */ + ret = p11_dict_set (map, &key, &value); + CuAssertIntEquals (tc, ret, 1); + CuAssertIntEquals (tc, 0, key.freed); + CuAssertIntEquals (tc, 1, key2.freed); + CuAssertIntEquals (tc, 0, value); + CuAssertIntEquals (tc, 2, value2); + + key.freed = key2.freed = value = value2 = 0; + + p11_dict_free (map); + CuAssertIntEquals (tc, 1, key.freed); + CuAssertIntEquals (tc, 2, value); + CuAssertIntEquals (tc, 0, key2.freed); + CuAssertIntEquals (tc, 0, value2); +} + + +static void +test_clear_destroys (CuTest *tc) +{ + p11_dict *map; + Key key = { 18, 0 }; + int value = 0; + + map = p11_dict_new (key_hash, key_equal, key_destroy, value_destroy); + CuAssertPtrNotNull (tc, map); + if (!p11_dict_set (map, &key, &value)) + CuFail (tc, "should not be reached"); + + p11_dict_clear (map); + CuAssertIntEquals (tc, 1, key.freed); + CuAssertIntEquals (tc, 2, value); + + /* should not be destroyed again */ + key.freed = 0; + value = 0; + + p11_dict_clear (map); + CuAssertIntEquals (tc, 0, key.freed); + CuAssertIntEquals (tc, 0, value); + + /* should not be destroyed again */ + key.freed = 0; + value = 0; + + p11_dict_free (map); + + CuAssertIntEquals (tc, 0, key.freed); + CuAssertIntEquals (tc, 0, value); +} + +static unsigned int +test_hash_intptr_with_collisions (const void *data) +{ + /* lots and lots of collisions, only returns 100 values */ + return (unsigned int)(*((int*)data) % 100); +} + +static void +test_hash_add_check_lots_and_collisions (CuTest *tc) +{ + p11_dict *map; + int *value; + int i; + + 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_dict_set (map, value, value)) + CuFail (tc, "should not be reached"); + } + + for (i = 0; i < 20000; ++i) { + value = p11_dict_get (map, &i); + CuAssertPtrNotNull (tc, value); + CuAssertIntEquals (tc, i, *value); + } + + p11_dict_free (map); +} + +static void +test_hash_count (CuTest *tc) +{ + p11_dict *map; + int *value; + int i, ret; + + map = p11_dict_new (p11_dict_intptr_hash, p11_dict_intptr_equal, NULL, free); + + CuAssertIntEquals (tc, 0, p11_dict_size (map)); + + for (i = 0; i < 20000; ++i) { + value = malloc (sizeof (int)); + *value = i; + if (!p11_dict_set (map, value, value)) + CuFail (tc, "should not be reached"); + CuAssertIntEquals (tc, i + 1, p11_dict_size (map)); + } + + for (i = 0; i < 20000; ++i) { + ret = p11_dict_remove (map, &i); + CuAssertIntEquals (tc, 1, ret); + CuAssertIntEquals (tc, 20000 - (i + 1), p11_dict_size (map)); + } + + p11_dict_clear (map); + CuAssertIntEquals (tc, 0, p11_dict_size (map)); + + p11_dict_free (map); +} + +static void +test_hash_ulongptr (CuTest *tc) +{ + p11_dict *map; + unsigned long *value; + unsigned long i; + + 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_dict_set (map, value, value)) + CuFail (tc, "should not be reached"); + } + + for (i = 0; i < 20000; ++i) { + value = p11_dict_get (map, &i); + CuAssertPtrNotNull (tc, value); + CuAssertIntEquals (tc, i, *value); + } + + p11_dict_free (map); +} + +int +main (void) +{ + CuString *output = CuStringNew (); + CuSuite* suite = CuSuiteNew (); + int ret; + + SUITE_ADD_TEST (suite, test_create); + SUITE_ADD_TEST (suite, test_set_get); + SUITE_ADD_TEST (suite, test_set_get_remove); + SUITE_ADD_TEST (suite, test_remove_destroys); + SUITE_ADD_TEST (suite, test_set_clear); + SUITE_ADD_TEST (suite, test_set_destroys); + SUITE_ADD_TEST (suite, test_clear_destroys); + SUITE_ADD_TEST (suite, test_free_null); + SUITE_ADD_TEST (suite, test_free_destroys); + SUITE_ADD_TEST (suite, test_iterate); + SUITE_ADD_TEST (suite, test_hash_add_check_lots_and_collisions); + SUITE_ADD_TEST (suite, test_hash_count); + SUITE_ADD_TEST (suite, test_hash_ulongptr); + + 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/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/m4/.empty b/m4/.empty deleted file mode 100644 index be533a1..0000000 --- a/m4/.empty +++ /dev/null @@ -1 +0,0 @@ -Stub file to track in git 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 #include @@ -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/debug.c b/p11-kit/debug.c deleted file mode 100644 index a665d0f..0000000 --- a/p11-kit/debug.c +++ /dev/null @@ -1,151 +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 - */ - -#include "config.h" - -#include "debug.h" - -#include -#include -#include -#include -#include -#include - -struct DebugKey { - const char *name; - int value; -}; - -static struct DebugKey debug_keys[] = { - { "lib", DEBUG_LIB }, - { "conf", DEBUG_CONF }, - { "uri", DEBUG_URI }, - { "proxy", DEBUG_PROXY }, - { 0, } -}; - -static int debug_inited = 0; -static int debug_strict = 0; - -/* global variable exported in debug.h */ -int _p11_debug_current_flags = ~0; - -static int -parse_environ_flags (void) -{ - const char *env; - int result = 0; - const char *p; - const char *q; - int i; - - env = getenv ("P11_KIT_STRICT"); - if (env && env[0] != '\0') - debug_strict = 1; - - env = getenv ("P11_KIT_DEBUG"); - if (!env) - return 0; - - if (strcmp (env, "all") == 0) { - for (i = 0; debug_keys[i].name; i++) - result |= debug_keys[i].value; - - } else if (strcmp (env, "help") == 0) { - fprintf (stderr, "Supported debug values:"); - for (i = 0; debug_keys[i].name; i++) - fprintf (stderr, " %s", debug_keys[i].name); - fprintf (stderr, "\n"); - - } else { - p = env; - while (*p) { - q = strpbrk (p, ":;, \t"); - if (!q) - q = p + strlen (p); - - for (i = 0; debug_keys[i].name; i++) { - if (q - p == strlen (debug_keys[i].name) && - strncmp (debug_keys[i].name, p, q - p) == 0) - result |= debug_keys[i].value; - } - - p = q; - if (*p) - p++; - } - } - - return result; -} - -void -_p11_debug_init (void) -{ - _p11_debug_current_flags = parse_environ_flags (); - debug_inited = 1; -} - -void -_p11_debug_message (int flag, - const char *format, ...) -{ - char buffer[512]; - va_list args; - - if (flag & _p11_debug_current_flags) { - va_start (args, format); - vsnprintf (buffer, sizeof (buffer), format, args); - buffer[sizeof (buffer) -1] = 0; - va_end (args); - fprintf (stderr, "(p11-kit:%d) %s\n", getpid(), buffer); - } -} - -void -_p11_debug_precond (const char *format, - ...) -{ - va_list va; - - va_start (va, format); - vfprintf (stderr, format, va); - va_end (va); - - if (debug_strict) - abort (); -} diff --git a/p11-kit/debug.h b/p11-kit/debug.h deleted file mode 100644 index c6e26f5..0000000 --- a/p11-kit/debug.h +++ /dev/null @@ -1,129 +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 - */ - -#ifndef DEBUG_H -#define 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; - -extern int _p11_debug_current_flags; - -void _p11_debug_init (void); - -void _p11_debug_message (int flag, - const char *format, - ...) GNUC_PRINTF (2, 3); - -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__); \ - 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__); \ - return; \ - } } while (0) - -#define return_if_reached() \ - do { \ - _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__); \ - return v; \ - } while (0) - -#endif /* DEBUG_H */ - -/* ----------------------------------------------------------------------------- - * 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 - * #include "debug.h" - * ... - * DEBUG ("if we're debugging one thing"); - * ... - * #undef DEBUG_FLAG - * #define DEBUG_FLAG DEBUG_OTHER_THING - * #include "debug.h" - * ... - * DEBUG ("if we're debugging the other thing"); - * ... - */ - -#ifdef 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__); \ - } while (0) - -#undef _p11_debugging -#define _p11_debugging \ - (DEBUG_FLAG & _p11_debug_current_flags) - -#else /* !defined (WITH_DEBUG) */ - -#undef _p11_debug -#define _p11_debug(format, ...) \ - do {} while (0) - -#undef _p11_debugging -#define _p11_debugging 0 - -#endif /* !defined (WITH_DEBUG) */ - -#endif /* defined (DEBUG_FLAG) */ diff --git a/p11-kit/hashmap.c b/p11-kit/hashmap.c deleted file mode 100644 index d420221..0000000 --- a/p11-kit/hashmap.c +++ /dev/null @@ -1,391 +0,0 @@ -/* - * Copyright (c) 2004 Stefan Walter - * 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. - */ - -#include "config.h" - -#include "hashmap.h" - -#include - -#include -#include -#include - -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 _hashbucket **buckets; - unsigned int num_items; - unsigned int num_buckets; -}; - -typedef struct _hashbucket { - void *key; - unsigned int hashed; - void *value; - struct _hashbucket *next; -} hashbucket; - -static hashbucket * -next_entry (hashiter *iter) -{ - hashbucket *bucket = iter->next; - while (!bucket) { - if (iter->index >= iter->map->num_buckets) - return NULL; - bucket = iter->map->buckets[iter->index++]; - } - iter->next = bucket->next; - return bucket; -} - - -int -_p11_hash_next (hashiter *iter, - void **key, - void **value) -{ - hashbucket *bucket = next_entry (iter); - if (bucket == NULL) - return 0; - if (key) - *key = bucket->key; - if (value) - *value = bucket->value; - return 1; -} - -void -_p11_hash_iterate (hashmap *map, - hashiter *iter) -{ - iter->map = map; - iter->index = 0; - iter->next = NULL; -} - -static hashbucket ** -lookup_or_create_bucket (hashmap *map, - const void *key, - int create) -{ - hashbucket **bucketp; - unsigned int hash; - - /* Perform the hashing */ - hash = map->hash_func (key); - - /* scan linked list */ - for (bucketp = &map->buckets[hash % map->num_buckets]; - *bucketp != NULL; bucketp = &(*bucketp)->next) { - if((*bucketp)->hashed == hash && map->equal_func ((*bucketp)->key, key)) - break; - } - - if ((*bucketp) != NULL || !create) - return bucketp; - - /* add a new entry for non-NULL val */ - (*bucketp) = calloc (sizeof (hashbucket), 1); - - if (*bucketp != NULL) { - (*bucketp)->key = (void*)key; - (*bucketp)->hashed = hash; - map->num_items++; - } - - return bucketp; -} - -void * -_p11_hash_get (hashmap *map, - const void *key) -{ - hashbucket **bucketp; - - bucketp = lookup_or_create_bucket (map, key, 0); - if (bucketp && *bucketp) - return (void*)((*bucketp)->value); - else - return NULL; -} - -int -_p11_hash_set (hashmap *map, - void *key, - void *val) -{ - hashbucket **bucketp; - hashiter iter; - hashbucket *bucket; - hashbucket **new_buckets; - unsigned int num_buckets; - - bucketp = lookup_or_create_bucket (map, 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); - - /* Destroy the previous value */ - if ((*bucketp)->value && (*bucketp)->value != val && map->value_destroy_func) - map->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); - - /* Ignore failures, maybe we can expand later */ - if(new_buckets) { - _p11_hash_iterate (map, &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; - } - } - - return 1; - } - - return 0; -} - -int -_p11_hash_steal (hashmap *map, - const void *key, - void **stolen_key, - void **stolen_value) -{ - hashbucket **bucketp; - - bucketp = lookup_or_create_bucket (map, key, 0); - if (bucketp && *bucketp) { - hashbucket *old = *bucketp; - *bucketp = (*bucketp)->next; - --map->num_items; - if (stolen_key) - *stolen_key = old->key; - if (stolen_value) - *stolen_value = old->value; - free (old); - return 1; - } - - return 0; - -} - -int -_p11_hash_remove (hashmap *map, - const void *key) -{ - void *old_key; - void *old_value; - - if (!_p11_hash_steal (map, 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); - return 1; -} - -void -_p11_hash_clear (hashmap *map) -{ - hashbucket *bucket, *next; - int i; - - /* Free all entries in the array */ - for (i = 0; i < map->num_buckets; ++i) { - bucket = map->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); - free (bucket); - bucket = next; - } - } - - memset (map->buckets, 0, map->num_buckets * sizeof (hashbucket *)); - map->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) -{ - hashmap *map; - - 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); - return NULL; - } - - map->num_items = 0; - } - - return map; -} - -void -_p11_hash_free (hashmap *map) -{ - hashbucket *bucket; - hashiter iter; - - if (!map) - return; - - _p11_hash_iterate (map, &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); - free (bucket); - } - - if (map->buckets) - free (map->buckets); - - free (map); -} - -unsigned int -_p11_hash_size (hashmap *map) -{ - return map->num_items; -} - -unsigned int -_p11_hash_string_hash (const void *string) -{ - const char *p = string; - unsigned int hash = *p; - - if (hash) - for (p += 1; *p != '\0'; p++) - hash = (hash << 5) - hash + *p; - - return hash; -} - -int -_p11_hash_string_equal (const void *string_one, - const void *string_two) -{ - assert (string_one); - assert (string_two); - - return strcmp (string_one, string_two) == 0; -} - -unsigned int -_p11_hash_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) -{ - assert (ulong_one); - assert (ulong_two); - return *((unsigned long*)ulong_one) == *((unsigned long*)ulong_two); -} - -unsigned int -_p11_hash_intptr_hash (const void *to_int) -{ - assert (to_int); - return (unsigned int)*((int*)to_int); -} - -int -_p11_hash_intptr_equal (const void *int_one, - const void *int_two) -{ - assert (int_one); - assert (int_two); - return *((int*)int_one) == *((int*)int_two); -} - -unsigned int -_p11_hash_direct_hash (const void *ptr) -{ - return (unsigned int)(size_t)ptr; -} - -int -_p11_hash_direct_equal (const void *ptr_one, - const void *ptr_two) -{ - return ptr_one == ptr_two; -} diff --git a/p11-kit/hashmap.h b/p11-kit/hashmap.h deleted file mode 100644 index 3b35487..0000000 --- a/p11-kit/hashmap.h +++ /dev/null @@ -1,173 +0,0 @@ -/* - * Copyright (c) 2004 Stefan Walter - * 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 Waler - */ - -#ifndef HASHMAP_H_ -#define HASHMAP_H_ - -#include - -/* - * ARGUMENT DOCUMENTATION - * - * map: The hashmap - * key: Pointer to the key value - * val: Pointer to the value - * iter: A hashmap iterator - */ - - -/* ---------------------------------------------------------------------------------- - * TYPES - */ - -/* Abstract type for hash maps. */ -typedef struct _hashmap hashmap; - -/* Type for scanning hash tables. */ -typedef struct _hashiter { - hashmap *map; - struct _hashbucket *next; - unsigned int index; -} hashiter; - -typedef unsigned int (*hash_hash_func) (const void *data); - -typedef int (*hash_equal_func) (const void *one, - const void *two); - -typedef void (*hash_destroy_func) (void *data); - -/* ----------------------------------------------------------------------------- - * MAIN - */ - -/* - * _p11_hash_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_hash_free : Free a hash table - */ -void _p11_hash_free (hashmap *map); - -/* - * _p11_hash_size: Number of values in hash table - * - returns the number of entries in hash table - */ -unsigned int _p11_hash_size (hashmap *map); - -/* - * _p11_hash_get: Retrieves a value from the hash table - * - returns the value of the entry - */ -void* _p11_hash_get (hashmap *map, - const void *key); - -/* - * _p11_hash_set: Set a value in the hash table - * - returns 1 if the entry was added properly - */ -int _p11_hash_set (hashmap *map, - void *key, - void *value); - -/* - * _p11_hash_remove: Remove a value from the hash table - * - returns 1 if the entry was found - */ -int _p11_hash_remove (hashmap *map, - const void *key); - -/* - * _p11_hash_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, - const void *key, - void **stolen_key, - void **stolen_value); - -/* - * _p11_hash_iterate: Start enumerating through the hash table - * - returns a hash iterator - */ -void _p11_hash_iterate (hashmap *map, - hashiter *iter); - -/* - * _p11_hash_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, - void **key, - void **value); - -/* - * _p11_hash_clear: Clear all values from has htable. - */ -void _p11_hash_clear (hashmap *map); - -/* ----------------------------------------------------------------------------- - * HASH FUNCTIONS - */ - -unsigned int _p11_hash_string_hash (const void *string); - -int _p11_hash_string_equal (const void *string_one, - const void *string_two); - -unsigned int _p11_hash_ulongptr_hash (const void *to_ulong); - -int _p11_hash_ulongptr_equal (const void *ulong_one, - const void *ulong_two); - -unsigned int _p11_hash_intptr_hash (const void *to_int); - -int _p11_hash_intptr_equal (const void *int_one, - const void *int_two); - -unsigned int _p11_hash_direct_hash (const void *ptr); - -int _p11_hash_direct_equal (const void *ptr_one, - const void *ptr_two); - -#endif /* __HASHMAP_H__ */ 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 #include @@ -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 #include @@ -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/p11-kit/ptr-array.c b/p11-kit/ptr-array.c deleted file mode 100644 index f393641..0000000 --- a/p11-kit/ptr-array.c +++ /dev/null @@ -1,154 +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. - */ - -#include "config.h" - -#include "ptr-array.h" - -#include -#include - -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, - unsigned int length) -{ - unsigned int new_allocated; - void **new_memory; - - if (length <= array->allocated) - return 1; - - new_allocated = array->allocated + 16; - if (new_allocated < length) - new_allocated = length; - - new_memory = realloc (array->memory, new_allocated * sizeof (void*)); - if (new_memory == NULL) - return 0; - - array->memory = new_memory; - array->allocated = new_allocated; - return 1; -} - -ptr_array_t * -_p11_ptr_array_create (ptr_array_destroy_func destroy_func) -{ - ptr_array_t *array; - - array = calloc (1, sizeof (ptr_array_t)); - if (array == NULL) - return NULL; - - if (!maybe_expand_array (array, 2)) { - _p11_ptr_array_free (array); - return NULL; - } - - array->destroy = destroy_func; - return array; -} - -void -_p11_ptr_array_free (ptr_array_t *array) -{ - unsigned int i; - - if (array == NULL) - return; - - if (array->destroy) { - for (i = 0; i < array->length; i++) - (array->destroy) (array->memory[i]); - } - - free (array->memory); - 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) -{ - if (!maybe_expand_array (array, array->length + 1)) - return 0; - - array->memory[array->length] = value; - array->length++; - 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) -{ - 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; -} diff --git a/p11-kit/ptr-array.h b/p11-kit/ptr-array.h deleted file mode 100644 index ba7ec87..0000000 --- a/p11-kit/ptr-array.h +++ /dev/null @@ -1,61 +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 Waler - */ - -#ifndef __PTR_ARRAY_H__ -#define __PTR_ARRAY_H__ - -#include - -typedef struct ptr_array ptr_array_t; - -typedef void (*ptr_array_destroy_func) (void *data); - -ptr_array_t* _p11_ptr_array_create (ptr_array_destroy_func destroy_func); - -void _p11_ptr_array_free (ptr_array_t *array); - -unsigned int _p11_ptr_array_count (ptr_array_t *array); - -int _p11_ptr_array_add (ptr_array_t *array, - void *value); - -void _p11_ptr_array_remove (ptr_array_t *array, - unsigned int index); - -void* _p11_ptr_array_at (ptr_array_t *array, - unsigned int index); - -void** _p11_ptr_array_snapshot (ptr_array_t *array); - -#endif /* __PTR_ARRAY_H__ */ diff --git a/p11-kit/tests/Makefile.am b/p11-kit/tests/Makefile.am new file mode 100644 index 0000000..bd08426 --- /dev/null +++ b/p11-kit/tests/Makefile.am @@ -0,0 +1,63 @@ + +include $(top_srcdir)/build/Makefile.tests + +INCLUDES = \ + -I$(top_srcdir) \ + -I$(srcdir)/.. \ + -I$(top_srcdir)/common \ + $(CUTEST_CFLAGS) + +LDADD = \ + $(top_builddir)/p11-kit/libp11-kit-testable.la \ + $(CUTEST_LIBS) \ + $(LTLIBINTL) + +CHECK_PROGS = \ + progname-test \ + conf-test \ + uri-test \ + pin-test \ + test-init \ + test-modules + +noinst_PROGRAMS = \ + print-messages \ + $(CHECK_PROGS) + +test_init_SOURCES = test-init.c \ + mock-module.c mock-module.h +test_init_CFLAGS = $(AM_CFLAGS) + +TESTS = $(CHECK_PROGS:=$(EXEEXT)) + +noinst_LTLIBRARIES = \ + mock-one.la \ + mock-two.la \ + mock-three.la \ + mock-four.la + +mock_one_la_SOURCES = \ + mock-module.c mock-module.h \ + mock-module-ep.c + +mock_one_la_LIBADD = \ + $(top_builddir)/p11-kit/libp11-kit-compat.la + +mock_one_la_LDFLAGS = \ + -module -avoid-version -rpath /nowhere \ + -no-undefined -export-symbols-regex 'C_GetFunctionList' + +mock_two_la_SOURCES = $(mock_one_la_SOURCES) +mock_two_la_LDFLAGS = $(mock_one_la_LDFLAGS) +mock_two_la_LIBADD = $(mock_one_la_LIBADD) + +mock_three_la_SOURCES = $(mock_one_la_SOURCES) +mock_three_la_LDFLAGS = $(mock_one_la_LDFLAGS) +mock_three_la_LIBADD = $(mock_one_la_LIBADD) + +mock_four_la_SOURCES = $(mock_one_la_SOURCES) +mock_four_la_LDFLAGS = $(mock_one_la_LDFLAGS) +mock_four_la_LIBADD = $(mock_one_la_LIBADD) + +EXTRA_DIST = \ + files diff --git a/p11-kit/tests/conf-test.c b/p11-kit/tests/conf-test.c new file mode 100644 index 0000000..4ea1313 --- /dev/null +++ b/p11-kit/tests/conf-test.c @@ -0,0 +1,415 @@ +/* + * 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 + */ + +#include "config.h" +#include "CuTest.h" + +#include +#include +#include +#include + +#include "conf.h" +#include "library.h" +#include "p11-kit.h" +#include "private.h" + +static void +test_parse_conf_1 (CuTest *tc) +{ + p11_dict *map; + const char *value; + + map = _p11_conf_parse_file (SRCDIR "/files/test-1.conf", 0); + CuAssertPtrNotNull (tc, map); + + value = p11_dict_get (map, "key1"); + CuAssertStrEquals (tc, "value1", value); + + value = p11_dict_get (map, "with-colon"); + CuAssertStrEquals (tc, "value-of-colon", value); + + value = p11_dict_get (map, "with-whitespace"); + CuAssertStrEquals (tc, "value-with-whitespace", value); + + value = p11_dict_get (map, "embedded-comment"); + CuAssertStrEquals (tc, "this is # not a comment", value); + + p11_dict_free (map); +} + +static void +test_parse_ignore_missing (CuTest *tc) +{ + p11_dict *map; + + map = _p11_conf_parse_file (SRCDIR "/files/non-existant.conf", CONF_IGNORE_MISSING); + CuAssertPtrNotNull (tc, map); + + CuAssertIntEquals (tc, 0, p11_dict_size (map)); + CuAssertPtrEquals (tc, NULL, (void*)p11_kit_message ()); + p11_dict_free (map); +} + +static void +test_parse_fail_missing (CuTest *tc) +{ + p11_dict *map; + + map = _p11_conf_parse_file (SRCDIR "/files/non-existant.conf", 0); + CuAssertPtrEquals (tc, map, NULL); + CuAssertPtrNotNull (tc, p11_kit_message ()); +} + +static void +test_merge_defaults (CuTest *tc) +{ + p11_dict *values; + p11_dict *defaults; + + values = p11_dict_new (p11_dict_str_hash, p11_dict_str_equal, free, free); + defaults = p11_dict_new (p11_dict_str_hash, p11_dict_str_equal, free, free); + + p11_dict_set (values, strdup ("one"), strdup ("real1")); + p11_dict_set (values, strdup ("two"), strdup ("real2")); + + p11_dict_set (defaults, strdup ("two"), strdup ("default2")); + p11_dict_set (defaults, strdup ("three"), strdup ("default3")); + + if (_p11_conf_merge_defaults (values, defaults) < 0) + CuFail (tc, "should not be reached"); + + p11_dict_free (defaults); + + CuAssertStrEquals (tc, p11_dict_get (values, "one"), "real1"); + CuAssertStrEquals (tc, p11_dict_get (values, "two"), "real2"); + CuAssertStrEquals (tc, p11_dict_get (values, "three"), "default3"); + + p11_dict_free (values); +} + +static void +test_load_globals_merge (CuTest *tc) +{ + int user_mode = -1; + p11_dict *config; + + p11_message_clear (); + + config = _p11_conf_load_globals (SRCDIR "/files/test-system-merge.conf", + SRCDIR "/files/test-user.conf", + &user_mode); + CuAssertPtrNotNull (tc, config); + CuAssertStrEquals (tc, NULL, p11_kit_message ()); + CuAssertIntEquals (tc, CONF_USER_MERGE, user_mode); + + CuAssertStrEquals (tc, p11_dict_get (config, "key1"), "system1"); + CuAssertStrEquals (tc, p11_dict_get (config, "key2"), "user2"); + CuAssertStrEquals (tc, p11_dict_get (config, "key3"), "user3"); + + p11_dict_free (config); +} + +static void +test_load_globals_no_user (CuTest *tc) +{ + int user_mode = -1; + p11_dict *config; + + p11_message_clear (); + + config = _p11_conf_load_globals (SRCDIR "/files/test-system-none.conf", + SRCDIR "/files/test-user.conf", + &user_mode); + CuAssertPtrNotNull (tc, config); + CuAssertStrEquals (tc, NULL, p11_kit_message ()); + CuAssertIntEquals (tc, CONF_USER_NONE, user_mode); + + CuAssertStrEquals (tc, p11_dict_get (config, "key1"), "system1"); + CuAssertStrEquals (tc, p11_dict_get (config, "key2"), "system2"); + CuAssertStrEquals (tc, p11_dict_get (config, "key3"), "system3"); + + p11_dict_free (config); +} + +static void +test_load_globals_user_sets_only (CuTest *tc) +{ + int user_mode = -1; + p11_dict *config; + + p11_message_clear (); + + config = _p11_conf_load_globals (SRCDIR "/files/test-system-merge.conf", + SRCDIR "/files/test-user-only.conf", + &user_mode); + CuAssertPtrNotNull (tc, config); + CuAssertStrEquals (tc, NULL, p11_kit_message ()); + CuAssertIntEquals (tc, CONF_USER_ONLY, user_mode); + + CuAssertStrEquals (tc, p11_dict_get (config, "key1"), NULL); + CuAssertStrEquals (tc, p11_dict_get (config, "key2"), "user2"); + CuAssertStrEquals (tc, p11_dict_get (config, "key3"), "user3"); + + p11_dict_free (config); +} + +static void +test_load_globals_system_sets_only (CuTest *tc) +{ + int user_mode = -1; + p11_dict *config; + + p11_message_clear (); + + config = _p11_conf_load_globals (SRCDIR "/files/test-system-only.conf", + SRCDIR "/files/test-user.conf", + &user_mode); + CuAssertPtrNotNull (tc, config); + CuAssertStrEquals (tc, NULL, p11_kit_message ()); + CuAssertIntEquals (tc, CONF_USER_ONLY, user_mode); + + CuAssertStrEquals (tc, p11_dict_get (config, "key1"), NULL); + CuAssertStrEquals (tc, p11_dict_get (config, "key2"), "user2"); + CuAssertStrEquals (tc, p11_dict_get (config, "key3"), "user3"); + + p11_dict_free (config); +} + +static void +test_load_globals_system_sets_invalid (CuTest *tc) +{ + int user_mode = -1; + p11_dict *config; + int error; + + p11_message_clear (); + + config = _p11_conf_load_globals (SRCDIR "/files/test-system-invalid.conf", + SRCDIR "/files/non-existant.conf", + &user_mode); + error = errno; + CuAssertPtrEquals (tc, NULL, config); + CuAssertIntEquals (tc, EINVAL, error); + CuAssertPtrNotNull (tc, p11_kit_message ()); + + p11_dict_free (config); +} + +static void +test_load_globals_user_sets_invalid (CuTest *tc) +{ + int user_mode = -1; + p11_dict *config; + int error; + + p11_message_clear (); + + config = _p11_conf_load_globals (SRCDIR "/files/test-system-merge.conf", + SRCDIR "/files/test-user-invalid.conf", + &user_mode); + error = errno; + CuAssertPtrEquals (tc, NULL, config); + CuAssertIntEquals (tc, EINVAL, error); + CuAssertPtrNotNull (tc, p11_kit_message ()); + + p11_dict_free (config); +} + +static int +assert_msg_contains (const char *msg, + const char *text) +{ + if (msg == NULL) + return 0; + return strstr (msg, text) ? 1 : 0; +} + +static void +test_load_modules_merge (CuTest *tc) +{ + p11_dict *configs; + p11_dict *config; + + p11_message_clear (); + + configs = _p11_conf_load_modules (CONF_USER_MERGE, + SRCDIR "/files/system-modules", + SRCDIR "/files/user-modules"); + CuAssertPtrNotNull (tc, configs); + CuAssertTrue (tc, assert_msg_contains (p11_kit_message (), "invalid config filename")); + + config = p11_dict_get (configs, "one"); + CuAssertPtrNotNull (tc, config); + CuAssertStrEquals (tc, "mock-one.so", p11_dict_get (config, "module")); + CuAssertStrEquals (tc, p11_dict_get (config, "setting"), "user1"); + + config = p11_dict_get (configs, "two.badname"); + CuAssertPtrNotNull (tc, config); + CuAssertStrEquals (tc, "mock-two.so", p11_dict_get (config, "module")); + CuAssertStrEquals (tc, p11_dict_get (config, "setting"), "system2"); + + config = p11_dict_get (configs, "three"); + CuAssertPtrNotNull (tc, config); + CuAssertStrEquals (tc, "mock-three.so", p11_dict_get (config, "module")); + CuAssertStrEquals (tc, p11_dict_get (config, "setting"), "user3"); + + p11_dict_free (configs); +} + +static void +test_load_modules_user_none (CuTest *tc) +{ + p11_dict *configs; + p11_dict *config; + + p11_message_clear (); + + configs = _p11_conf_load_modules (CONF_USER_NONE, + SRCDIR "/files/system-modules", + SRCDIR "/files/user-modules"); + CuAssertPtrNotNull (tc, configs); + CuAssertTrue (tc, assert_msg_contains (p11_kit_message (), "invalid config filename")); + + config = p11_dict_get (configs, "one"); + CuAssertPtrNotNull (tc, config); + CuAssertStrEquals (tc, "mock-one.so", p11_dict_get (config, "module")); + CuAssertStrEquals (tc, p11_dict_get (config, "setting"), "system1"); + + config = p11_dict_get (configs, "two.badname"); + CuAssertPtrNotNull (tc, config); + CuAssertStrEquals (tc, "mock-two.so", p11_dict_get (config, "module")); + CuAssertStrEquals (tc, p11_dict_get (config, "setting"), "system2"); + + config = p11_dict_get (configs, "three"); + CuAssertPtrEquals (tc, NULL, config); + + p11_dict_free (configs); +} + +static void +test_load_modules_user_only (CuTest *tc) +{ + p11_dict *configs; + p11_dict *config; + + p11_message_clear (); + + configs = _p11_conf_load_modules (CONF_USER_ONLY, + SRCDIR "/files/system-modules", + SRCDIR "/files/user-modules"); + CuAssertPtrNotNull (tc, configs); + CuAssertPtrEquals (tc, NULL, (void *)p11_kit_message ()); + + config = p11_dict_get (configs, "one"); + CuAssertPtrNotNull (tc, config); + CuAssertStrEquals (tc, p11_dict_get (config, "module"), NULL); + CuAssertStrEquals (tc, p11_dict_get (config, "setting"), "user1"); + + config = p11_dict_get (configs, "two.badname"); + CuAssertPtrEquals (tc, NULL, config); + + config = p11_dict_get (configs, "three"); + CuAssertPtrNotNull (tc, config); + CuAssertStrEquals (tc, "mock-three.so", p11_dict_get (config, "module")); + CuAssertStrEquals (tc, p11_dict_get (config, "setting"), "user3"); + + p11_dict_free (configs); +} + +static void +test_load_modules_no_user (CuTest *tc) +{ + p11_dict *configs; + p11_dict *config; + + p11_message_clear (); + + configs = _p11_conf_load_modules (CONF_USER_MERGE, + SRCDIR "/files/system-modules", + SRCDIR "/files/non-existant"); + CuAssertPtrNotNull (tc, configs); + CuAssertTrue (tc, assert_msg_contains (p11_kit_message (), "invalid config filename")); + + config = p11_dict_get (configs, "one"); + CuAssertPtrNotNull (tc, config); + CuAssertStrEquals (tc, "mock-one.so", p11_dict_get (config, "module")); + CuAssertStrEquals (tc, p11_dict_get (config, "setting"), "system1"); + + config = p11_dict_get (configs, "two.badname"); + CuAssertPtrNotNull (tc, config); + CuAssertStrEquals (tc, "mock-two.so", p11_dict_get (config, "module")); + CuAssertStrEquals (tc, p11_dict_get (config, "setting"), "system2"); + + config = p11_dict_get (configs, "three"); + CuAssertPtrEquals (tc, NULL, config); + + p11_dict_free (configs); +} + +int +main (void) +{ + CuString *output = CuStringNew (); + CuSuite* suite = CuSuiteNew (); + int ret; + + p11_library_init (); + + SUITE_ADD_TEST (suite, test_parse_conf_1); + SUITE_ADD_TEST (suite, test_parse_ignore_missing); + SUITE_ADD_TEST (suite, test_parse_fail_missing); + SUITE_ADD_TEST (suite, test_merge_defaults); + SUITE_ADD_TEST (suite, test_load_globals_merge); + SUITE_ADD_TEST (suite, test_load_globals_no_user); + SUITE_ADD_TEST (suite, test_load_globals_system_sets_only); + SUITE_ADD_TEST (suite, test_load_globals_user_sets_only); + SUITE_ADD_TEST (suite, test_load_globals_system_sets_invalid); + SUITE_ADD_TEST (suite, test_load_globals_user_sets_invalid); + SUITE_ADD_TEST (suite, test_load_modules_merge); + SUITE_ADD_TEST (suite, test_load_modules_no_user); + SUITE_ADD_TEST (suite, test_load_modules_user_only); + SUITE_ADD_TEST (suite, test_load_modules_user_none); + + p11_kit_be_quiet (); + + CuSuiteRun (suite); + CuSuiteSummary (suite, output); + CuSuiteDetails (suite, output); + printf ("%s\n", output->buffer); + ret = suite->failCount; + CuSuiteDelete (suite); + CuStringDelete (output); + return ret; +} diff --git a/p11-kit/tests/files/system-modules/four.module b/p11-kit/tests/files/system-modules/four.module new file mode 100644 index 0000000..6eace3c --- /dev/null +++ b/p11-kit/tests/files/system-modules/four.module @@ -0,0 +1,3 @@ + +module: mock-four.so +disable-in: test-disable, test-other diff --git a/p11-kit/tests/files/system-modules/one.module b/p11-kit/tests/files/system-modules/one.module new file mode 100644 index 0000000..3620869 --- /dev/null +++ b/p11-kit/tests/files/system-modules/one.module @@ -0,0 +1,3 @@ + +module: mock-one.so +setting: system1 \ No newline at end of file diff --git a/p11-kit/tests/files/system-modules/two-duplicate.module b/p11-kit/tests/files/system-modules/two-duplicate.module new file mode 100644 index 0000000..907aa75 --- /dev/null +++ b/p11-kit/tests/files/system-modules/two-duplicate.module @@ -0,0 +1,3 @@ + +# This is a duplicate of the 'two' module +module: mock-two.so diff --git a/p11-kit/tests/files/system-modules/two.badname b/p11-kit/tests/files/system-modules/two.badname new file mode 100644 index 0000000..0d41cac --- /dev/null +++ b/p11-kit/tests/files/system-modules/two.badname @@ -0,0 +1,5 @@ +# This module doesn't have a .module extension, but p11-kit doesn't yet +# enforce the naming, just warns, so it should still be loaded + +module: mock-two.so +setting: system2 diff --git a/p11-kit/tests/files/system-modules/win32/four.module b/p11-kit/tests/files/system-modules/win32/four.module new file mode 100644 index 0000000..7fd1540 --- /dev/null +++ b/p11-kit/tests/files/system-modules/win32/four.module @@ -0,0 +1,3 @@ + +module: mock-four.dll +disable-in: test-disable, test-other diff --git a/p11-kit/tests/files/system-modules/win32/one.module b/p11-kit/tests/files/system-modules/win32/one.module new file mode 100644 index 0000000..5f80304 --- /dev/null +++ b/p11-kit/tests/files/system-modules/win32/one.module @@ -0,0 +1,3 @@ + +module: mock-one.dll +setting: system1 \ No newline at end of file diff --git a/p11-kit/tests/files/system-modules/win32/two-duplicate.module b/p11-kit/tests/files/system-modules/win32/two-duplicate.module new file mode 100644 index 0000000..e80c9e8 --- /dev/null +++ b/p11-kit/tests/files/system-modules/win32/two-duplicate.module @@ -0,0 +1,3 @@ + +# This is a duplicate of the 'two' module +module: mock-two.dll diff --git a/p11-kit/tests/files/system-modules/win32/two.badname b/p11-kit/tests/files/system-modules/win32/two.badname new file mode 100644 index 0000000..ae44b83 --- /dev/null +++ b/p11-kit/tests/files/system-modules/win32/two.badname @@ -0,0 +1,5 @@ +# This module doesn't have a .module extension, but p11-kit doesn't yet +# enforce the naming, just warns, so it should still be loaded + +module: mock-two.dll +setting: system2 diff --git a/p11-kit/tests/files/system-pkcs11.conf b/p11-kit/tests/files/system-pkcs11.conf new file mode 100644 index 0000000..20741e7 --- /dev/null +++ b/p11-kit/tests/files/system-pkcs11.conf @@ -0,0 +1,3 @@ + +# Merge in user config +user-config: merge \ No newline at end of file diff --git a/p11-kit/tests/files/test-1.conf b/p11-kit/tests/files/test-1.conf new file mode 100644 index 0000000..d4ae0a1 --- /dev/null +++ b/p11-kit/tests/files/test-1.conf @@ -0,0 +1,6 @@ +key1:value1 +with-whitespace : value-with-whitespace +with-colon: value-of-colon + +# A comment +embedded-comment: this is # not a comment diff --git a/p11-kit/tests/files/test-pinfile b/p11-kit/tests/files/test-pinfile new file mode 100644 index 0000000..f646f3d --- /dev/null +++ b/p11-kit/tests/files/test-pinfile @@ -0,0 +1 @@ +yogabbagabba \ No newline at end of file diff --git a/p11-kit/tests/files/test-pinfile-large b/p11-kit/tests/files/test-pinfile-large new file mode 100644 index 0000000..506668d --- /dev/null +++ b/p11-kit/tests/files/test-pinfile-large @@ -0,0 +1,53 @@ +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba +yogabbagabba yogabbagabba yogabbagabba yo \ No newline at end of file diff --git a/p11-kit/tests/files/test-system-invalid.conf b/p11-kit/tests/files/test-system-invalid.conf new file mode 100644 index 0000000..344ee96 --- /dev/null +++ b/p11-kit/tests/files/test-system-invalid.conf @@ -0,0 +1,3 @@ + +# Invalid user-config setting +user-config: bad diff --git a/p11-kit/tests/files/test-system-merge.conf b/p11-kit/tests/files/test-system-merge.conf new file mode 100644 index 0000000..978427d --- /dev/null +++ b/p11-kit/tests/files/test-system-merge.conf @@ -0,0 +1,7 @@ + +# Merge in user config +user-config: merge + +key1: system1 +key2: system2 +key3: system3 \ No newline at end of file diff --git a/p11-kit/tests/files/test-system-none.conf b/p11-kit/tests/files/test-system-none.conf new file mode 100644 index 0000000..95351e6 --- /dev/null +++ b/p11-kit/tests/files/test-system-none.conf @@ -0,0 +1,8 @@ + +# Only user config +user-config: none + +# These values will not be overriden +key1: system1 +key2: system2 +key3: system3 \ No newline at end of file diff --git a/p11-kit/tests/files/test-system-only.conf b/p11-kit/tests/files/test-system-only.conf new file mode 100644 index 0000000..589f1c7 --- /dev/null +++ b/p11-kit/tests/files/test-system-only.conf @@ -0,0 +1,8 @@ + +# Only user config +user-config: only + +# This stuff will be ignored +key1: system1 +key2: system2 +key3: system3 \ No newline at end of file diff --git a/p11-kit/tests/files/test-user-invalid.conf b/p11-kit/tests/files/test-user-invalid.conf new file mode 100644 index 0000000..344ee96 --- /dev/null +++ b/p11-kit/tests/files/test-user-invalid.conf @@ -0,0 +1,3 @@ + +# Invalid user-config setting +user-config: bad diff --git a/p11-kit/tests/files/test-user-only.conf b/p11-kit/tests/files/test-user-only.conf new file mode 100644 index 0000000..3224c01 --- /dev/null +++ b/p11-kit/tests/files/test-user-only.conf @@ -0,0 +1,4 @@ + +user-config: only +key2: user2 +key3: user3 \ No newline at end of file diff --git a/p11-kit/tests/files/test-user.conf b/p11-kit/tests/files/test-user.conf new file mode 100644 index 0000000..369544a --- /dev/null +++ b/p11-kit/tests/files/test-user.conf @@ -0,0 +1,3 @@ + +key2: user2 +key3: user3 \ No newline at end of file diff --git a/p11-kit/tests/files/user-modules/one.module b/p11-kit/tests/files/user-modules/one.module new file mode 100644 index 0000000..c371e4a --- /dev/null +++ b/p11-kit/tests/files/user-modules/one.module @@ -0,0 +1,2 @@ + +setting: user1 \ No newline at end of file diff --git a/p11-kit/tests/files/user-modules/three.module b/p11-kit/tests/files/user-modules/three.module new file mode 100644 index 0000000..00caab5 --- /dev/null +++ b/p11-kit/tests/files/user-modules/three.module @@ -0,0 +1,5 @@ + +module: mock-three.so +setting: user3 + +enable-in: test-enable \ No newline at end of file diff --git a/p11-kit/tests/files/user-modules/win32/one.module b/p11-kit/tests/files/user-modules/win32/one.module new file mode 100644 index 0000000..c371e4a --- /dev/null +++ b/p11-kit/tests/files/user-modules/win32/one.module @@ -0,0 +1,2 @@ + +setting: user1 \ No newline at end of file diff --git a/p11-kit/tests/files/user-modules/win32/three.module b/p11-kit/tests/files/user-modules/win32/three.module new file mode 100644 index 0000000..58f883d --- /dev/null +++ b/p11-kit/tests/files/user-modules/win32/three.module @@ -0,0 +1,5 @@ + +module: mock-three.dll +setting: user3 + +enable-in: test-enable \ No newline at end of file diff --git a/p11-kit/tests/mock-module-ep.c b/p11-kit/tests/mock-module-ep.c new file mode 100644 index 0000000..7440a74 --- /dev/null +++ b/p11-kit/tests/mock-module-ep.c @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2012 Stefan Walter + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * * The names of contributors to this software may not be + * used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * Author: Stef Walter + */ + +#include "config.h" + +#define CRYPTOKI_EXPORTS 1 +#include "pkcs11.h" +#include "mock-module.h" + +#ifdef OS_WIN32 +__declspec(dllexport) +#endif +CK_RV +C_GetFunctionList (CK_FUNCTION_LIST_PTR_PTR list) +{ + mock_module_init (); + if (list == NULL) + return CKR_ARGUMENTS_BAD; + *list = &mock_module_no_slots; + return CKR_OK; +} diff --git a/p11-kit/tests/mock-module.c b/p11-kit/tests/mock-module.c new file mode 100644 index 0000000..1a74806 --- /dev/null +++ b/p11-kit/tests/mock-module.c @@ -0,0 +1,898 @@ +/* + * 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 + */ + +#include "config.h" + +#include "library.h" +#define CRYPTOKI_EXPORTS +#include "pkcs11.h" + +#include "mock-module.h" + +#include +#include +#include +#include + +/* ------------------------------------------------------------------- + * GLOBALS / DEFINES + */ + +/* Various mutexes */ +static p11_mutex_t init_mutex; + +/* Whether we've been initialized, and on what process id it happened */ +static int pkcs11_initialized = 0; +static pid_t pkcs11_initialized_pid = 0; + +/* ----------------------------------------------------------------------------- + * LOGGING and DEBUGGING + */ + +#define DEBUG_OUTPUT 0 + +#if DEBUG_OUTPUT +#define debug(x) mock_log x +#else +#define debug(x) +#endif + +#define warning(x) mock_log x + +#define return_val_if_fail(x, v) \ + if (!(x)) { mock_log ("'%s' not true at %s", #x, __func__); return v; } + +static void +mock_log (const char *format, ...) +{ + va_list va; + va_start (va, format); + fprintf (stderr, "mock-module: "); + vfprintf (stderr, format, va); + fprintf (stderr, "\n"); + va_end (va); +} + +/* ------------------------------------------------------------------- + * INITIALIZATION and 'GLOBAL' CALLS + */ + +CK_RV +mock_C_Initialize (CK_VOID_PTR init_args) +{ + CK_C_INITIALIZE_ARGS_PTR args = NULL; + CK_RV ret = CKR_OK; + pid_t pid; + + debug (("C_Initialize: enter")); + + p11_mutex_lock (&init_mutex); + + if (init_args != NULL) { + int supplied_ok; + + /* pReserved must be NULL */ + args = init_args; + + /* ALL supplied function pointers need to have the value either NULL or non-NULL. */ + supplied_ok = (args->CreateMutex == NULL && args->DestroyMutex == NULL && + args->LockMutex == NULL && args->UnlockMutex == NULL) || + (args->CreateMutex != NULL && args->DestroyMutex != NULL && + args->LockMutex != NULL && args->UnlockMutex != NULL); + if (!supplied_ok) { + warning (("invalid set of mutex calls supplied")); + ret = CKR_ARGUMENTS_BAD; + goto done; + } + + /* + * When the CKF_OS_LOCKING_OK flag isn't set return an error. + * We must be able to use our pthread functionality. + */ + if (!(args->flags & CKF_OS_LOCKING_OK)) { + warning (("can't do without os locking")); + ret = CKR_CANT_LOCK; + goto done; + } + } + + pid = getpid (); + if (pkcs11_initialized) { + + /* This process has called C_Initialize already */ + if (pid == pkcs11_initialized_pid) { + warning (("C_Initialize called twice for same process")); + ret = CKR_CRYPTOKI_ALREADY_INITIALIZED; + goto done; + } + } + +done: + /* Mark us as officially initialized */ + if (ret == CKR_OK) { + pkcs11_initialized = 1; + pkcs11_initialized_pid = pid; + } else if (ret != CKR_CRYPTOKI_ALREADY_INITIALIZED) { + pkcs11_initialized = 0; + pkcs11_initialized_pid = 0; + } + + p11_mutex_unlock (&init_mutex); + + debug (("C_Initialize: %d", ret)); + return ret; +} + +CK_RV +mock_C_Finalize (CK_VOID_PTR reserved) +{ + debug (("C_Finalize: enter")); + 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); + + /* This should stop all other calls in */ + pkcs11_initialized = 0; + pkcs11_initialized_pid = 0; + + p11_mutex_unlock (&init_mutex); + + debug (("C_Finalize: %d", CKR_OK)); + return CKR_OK; +} + +static const CK_INFO MOCK_INFO = { + { CRYPTOKI_VERSION_MAJOR, CRYPTOKI_VERSION_MINOR }, + "MOCK MANUFACTURER ", + 0, + "MOCK LIBRARY ", + { 45, 145 } +}; + + +CK_RV +mock_C_GetInfo (CK_INFO_PTR info) +{ + return_val_if_fail (info, CKR_ARGUMENTS_BAD); + + memcpy (info, &MOCK_INFO, sizeof (*info)); + return CKR_OK; +} + +CK_RV +mock_C_GetFunctionList (CK_FUNCTION_LIST_PTR_PTR list) +{ + /* This would be a strange call to receive */ + return C_GetFunctionList (list); +} + +CK_RV +mock_C_GetSlotList__no_tokens (CK_BBOOL token_present, + CK_SLOT_ID_PTR slot_list, + CK_ULONG_PTR count) +{ + return_val_if_fail (count, CKR_ARGUMENTS_BAD); + + /* No tokens */ + *count = 0; + return CKR_OK; +} + +CK_RV +mock_C_GetSlotInfo__invalid_slotid (CK_SLOT_ID id, + CK_SLOT_INFO_PTR info) +{ + return_val_if_fail (info, CKR_ARGUMENTS_BAD); + + return CKR_SLOT_ID_INVALID; +} + +CK_RV +mock_C_GetTokenInfo__invalid_slotid (CK_SLOT_ID id, + CK_TOKEN_INFO_PTR info) +{ + return_val_if_fail (info, CKR_ARGUMENTS_BAD); + + return CKR_SLOT_ID_INVALID; +} + +CK_RV +mock_C_GetMechanismList__invalid_slotid (CK_SLOT_ID id, + CK_MECHANISM_TYPE_PTR mechanism_list, + CK_ULONG_PTR count) +{ + return_val_if_fail (count, CKR_ARGUMENTS_BAD); + + return CKR_SLOT_ID_INVALID; +} + +CK_RV +mock_C_GetMechanismInfo__invalid_slotid (CK_SLOT_ID id, + CK_MECHANISM_TYPE type, + CK_MECHANISM_INFO_PTR info) +{ + return_val_if_fail (info, CKR_ARGUMENTS_BAD); + + return CKR_SLOT_ID_INVALID; +} + +CK_RV +mock_C_InitToken__invalid_slotid (CK_SLOT_ID id, + CK_UTF8CHAR_PTR pin, + CK_ULONG pin_len, + CK_UTF8CHAR_PTR label) +{ + return CKR_SLOT_ID_INVALID; +} + +CK_RV +mock_C_WaitForSlotEvent__no_event (CK_FLAGS flags, + CK_SLOT_ID_PTR slot, + CK_VOID_PTR reserved) +{ + return_val_if_fail (slot, CKR_ARGUMENTS_BAD); + + return CKR_NO_EVENT; +} + +CK_RV +mock_C_OpenSession__invalid_slotid (CK_SLOT_ID id, + CK_FLAGS flags, + CK_VOID_PTR user_data, + CK_NOTIFY callback, + CK_SESSION_HANDLE_PTR session) +{ + return_val_if_fail (session, CKR_ARGUMENTS_BAD); + + return CKR_SLOT_ID_INVALID; +} + +CK_RV +mock_C_CloseSession__invalid_handle (CK_SESSION_HANDLE session) +{ + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_CloseAllSessions__invalid_slotid (CK_SLOT_ID id) +{ + return CKR_SLOT_ID_INVALID; +} + +CK_RV +mock_C_GetFunctionStatus__not_parallel (CK_SESSION_HANDLE session) +{ + return CKR_FUNCTION_NOT_PARALLEL; +} + +CK_RV +mock_C_CancelFunction__not_parallel (CK_SESSION_HANDLE session) +{ + return CKR_FUNCTION_NOT_PARALLEL; +} + +CK_RV +mock_C_GetSessionInfo__invalid_handle (CK_SESSION_HANDLE session, + CK_SESSION_INFO_PTR info) +{ + return_val_if_fail (info, CKR_ARGUMENTS_BAD); + + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_InitPIN__invalid_handle (CK_SESSION_HANDLE session, + CK_UTF8CHAR_PTR pin, + CK_ULONG pin_len) +{ + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_SetPIN__invalid_handle (CK_SESSION_HANDLE session, + CK_UTF8CHAR_PTR old_pin, + CK_ULONG old_pin_len, + CK_UTF8CHAR_PTR new_pin, + CK_ULONG new_pin_len) +{ + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_GetOperationState__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR operation_state, + CK_ULONG_PTR operation_state_len) +{ + return_val_if_fail (operation_state_len, CKR_ARGUMENTS_BAD); + + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_SetOperationState__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR operation_state, + CK_ULONG operation_state_len, + CK_OBJECT_HANDLE encryption_key, + CK_OBJECT_HANDLE authentication_key) +{ + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_Login__invalid_handle (CK_SESSION_HANDLE session, + CK_USER_TYPE user_type, + CK_UTF8CHAR_PTR pin, + CK_ULONG pin_len) +{ + return CKR_SESSION_HANDLE_INVALID; + +} + +CK_RV +mock_C_Logout__invalid_handle (CK_SESSION_HANDLE session) +{ + return CKR_SESSION_HANDLE_INVALID; + +} + +CK_RV +mock_C_CreateObject__invalid_handle (CK_SESSION_HANDLE session, + CK_ATTRIBUTE_PTR template, + CK_ULONG count, + CK_OBJECT_HANDLE_PTR new_object) +{ + return_val_if_fail (new_object, CKR_ARGUMENTS_BAD); + + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_CopyObject__invalid_handle (CK_SESSION_HANDLE session, + CK_OBJECT_HANDLE object, + CK_ATTRIBUTE_PTR template, + CK_ULONG count, + CK_OBJECT_HANDLE_PTR new_object) +{ + return_val_if_fail (new_object, CKR_ARGUMENTS_BAD); + + return CKR_SESSION_HANDLE_INVALID; +} + + +CK_RV +mock_C_DestroyObject__invalid_handle (CK_SESSION_HANDLE session, + CK_OBJECT_HANDLE object) +{ + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_GetObjectSize__invalid_handle (CK_SESSION_HANDLE session, + CK_OBJECT_HANDLE object, + CK_ULONG_PTR size) +{ + return_val_if_fail (size, CKR_ARGUMENTS_BAD); + + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_GetAttributeValue__invalid_handle (CK_SESSION_HANDLE session, + CK_OBJECT_HANDLE object, + CK_ATTRIBUTE_PTR template, + CK_ULONG count) +{ + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_SetAttributeValue__invalid_handle (CK_SESSION_HANDLE session, + CK_OBJECT_HANDLE object, + CK_ATTRIBUTE_PTR template, + CK_ULONG count) +{ + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_FindObjectsInit__invalid_handle (CK_SESSION_HANDLE session, + CK_ATTRIBUTE_PTR template, + CK_ULONG count) +{ + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_FindObjects__invalid_handle (CK_SESSION_HANDLE session, + CK_OBJECT_HANDLE_PTR objects, + CK_ULONG max_count, + CK_ULONG_PTR count) +{ + return_val_if_fail (count, CKR_ARGUMENTS_BAD); + + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_FindObjectsFinal__invalid_handle (CK_SESSION_HANDLE session) +{ + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_EncryptInit__invalid_handle (CK_SESSION_HANDLE session, + CK_MECHANISM_PTR mechanism, + CK_OBJECT_HANDLE key) +{ + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_Encrypt__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR data, CK_ULONG data_len, + CK_BYTE_PTR encrypted_data, + CK_ULONG_PTR encrypted_data_len) +{ + return_val_if_fail (encrypted_data_len, CKR_ARGUMENTS_BAD); + + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_EncryptUpdate__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR part, + CK_ULONG part_len, + CK_BYTE_PTR encrypted_part, + CK_ULONG_PTR encrypted_part_len) +{ + return_val_if_fail (encrypted_part_len, CKR_ARGUMENTS_BAD); + + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_EncryptFinal__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR last_part, + CK_ULONG_PTR last_part_len) +{ + return_val_if_fail (last_part_len, CKR_ARGUMENTS_BAD); + + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_DecryptInit__invalid_handle (CK_SESSION_HANDLE session, + CK_MECHANISM_PTR mechanism, + CK_OBJECT_HANDLE key) +{ + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_Decrypt__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR enc_data, + CK_ULONG enc_data_len, + CK_BYTE_PTR data, + CK_ULONG_PTR data_len) +{ + return_val_if_fail (data_len, CKR_ARGUMENTS_BAD); + + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_DecryptUpdate__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR enc_part, + CK_ULONG enc_part_len, + CK_BYTE_PTR part, + CK_ULONG_PTR part_len) +{ + return_val_if_fail (part_len, CKR_ARGUMENTS_BAD); + + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_DecryptFinal__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR last_part, + CK_ULONG_PTR last_part_len) +{ + return_val_if_fail (last_part_len, CKR_ARGUMENTS_BAD); + + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_DigestInit__invalid_handle (CK_SESSION_HANDLE session, + CK_MECHANISM_PTR mechanism) +{ + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_Digest__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR data, + CK_ULONG data_len, + CK_BYTE_PTR digest, + CK_ULONG_PTR digest_len) +{ + return_val_if_fail (digest_len, CKR_ARGUMENTS_BAD); + + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_DigestUpdate__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR part, + CK_ULONG part_len) +{ + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_DigestKey__invalid_handle (CK_SESSION_HANDLE session, + CK_OBJECT_HANDLE key) +{ + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_DigestFinal__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR digest, + CK_ULONG_PTR digest_len) +{ + return_val_if_fail (digest_len, CKR_ARGUMENTS_BAD); + + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_SignInit__invalid_handle (CK_SESSION_HANDLE session, + CK_MECHANISM_PTR mechanism, + CK_OBJECT_HANDLE key) +{ + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_Sign__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR data, + CK_ULONG data_len, + CK_BYTE_PTR signature, + CK_ULONG_PTR signature_len) +{ + return_val_if_fail (signature_len, CKR_ARGUMENTS_BAD); + + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_SignUpdate__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR part, + CK_ULONG part_len) +{ + return_val_if_fail (part_len, CKR_ARGUMENTS_BAD); + + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_SignFinal__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR signature, + CK_ULONG_PTR signature_len) +{ + return_val_if_fail (signature_len, CKR_ARGUMENTS_BAD); + + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_SignRecoverInit__invalid_handle (CK_SESSION_HANDLE session, + CK_MECHANISM_PTR mechanism, + CK_OBJECT_HANDLE key) +{ + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_SignRecover__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR data, + CK_ULONG data_len, + CK_BYTE_PTR signature, + CK_ULONG_PTR signature_len) +{ + return_val_if_fail (signature_len, CKR_ARGUMENTS_BAD); + + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_VerifyInit__invalid_handle (CK_SESSION_HANDLE session, + CK_MECHANISM_PTR mechanism, + CK_OBJECT_HANDLE key) +{ + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_Verify__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR data, + CK_ULONG data_len, + CK_BYTE_PTR signature, + CK_ULONG signature_len) +{ + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_VerifyUpdate__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR part, + CK_ULONG part_len) +{ + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_VerifyFinal__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR signature, + CK_ULONG signature_len) +{ + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_VerifyRecoverInit__invalid_handle (CK_SESSION_HANDLE session, + CK_MECHANISM_PTR mechanism, + CK_OBJECT_HANDLE key) +{ + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_VerifyRecover__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR signature, + CK_ULONG signature_len, + CK_BYTE_PTR data, + CK_ULONG_PTR data_len) +{ + return_val_if_fail (data_len, CKR_ARGUMENTS_BAD); + + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_DigestEncryptUpdate__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR part, + CK_ULONG part_len, CK_BYTE_PTR enc_part, + CK_ULONG_PTR enc_part_len) +{ + return_val_if_fail (enc_part_len, CKR_ARGUMENTS_BAD); + + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_DecryptDigestUpdate__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR enc_part, + CK_ULONG enc_part_len, + CK_BYTE_PTR part, + CK_ULONG_PTR part_len) +{ + return_val_if_fail (part_len, CKR_ARGUMENTS_BAD); + + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_SignEncryptUpdate__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR part, + CK_ULONG part_len, + CK_BYTE_PTR enc_part, + CK_ULONG_PTR enc_part_len) +{ + return_val_if_fail (enc_part_len, CKR_ARGUMENTS_BAD); + + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_DecryptVerifyUpdate__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR enc_part, + CK_ULONG enc_part_len, + CK_BYTE_PTR part, + CK_ULONG_PTR part_len) +{ + return_val_if_fail (part_len, CKR_ARGUMENTS_BAD); + + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_GenerateKey__invalid_handle (CK_SESSION_HANDLE session, + CK_MECHANISM_PTR mechanism, + CK_ATTRIBUTE_PTR template, + CK_ULONG count, + CK_OBJECT_HANDLE_PTR key) +{ + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_GenerateKeyPair__invalid_handle (CK_SESSION_HANDLE session, + CK_MECHANISM_PTR mechanism, + CK_ATTRIBUTE_PTR pub_template, + CK_ULONG pub_count, + CK_ATTRIBUTE_PTR priv_template, + CK_ULONG priv_count, + CK_OBJECT_HANDLE_PTR pub_key, + CK_OBJECT_HANDLE_PTR priv_key) +{ + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_WrapKey__invalid_handle (CK_SESSION_HANDLE session, + CK_MECHANISM_PTR mechanism, + CK_OBJECT_HANDLE wrapping_key, + CK_OBJECT_HANDLE key, + CK_BYTE_PTR wrapped_key, + CK_ULONG_PTR wrapped_key_len) +{ + return_val_if_fail (wrapped_key_len, CKR_ARGUMENTS_BAD); + + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_UnwrapKey__invalid_handle (CK_SESSION_HANDLE session, + CK_MECHANISM_PTR mechanism, + CK_OBJECT_HANDLE unwrapping_key, + CK_BYTE_PTR wrapped_key, + CK_ULONG wrapped_key_len, + CK_ATTRIBUTE_PTR template, + CK_ULONG count, + CK_OBJECT_HANDLE_PTR key) +{ + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_DeriveKey__invalid_handle (CK_SESSION_HANDLE session, + CK_MECHANISM_PTR mechanism, + CK_OBJECT_HANDLE base_key, + CK_ATTRIBUTE_PTR template, + CK_ULONG count, + CK_OBJECT_HANDLE_PTR key) +{ + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_SeedRandom__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR seed, + CK_ULONG seed_len) +{ + return CKR_SESSION_HANDLE_INVALID; +} + +CK_RV +mock_C_GenerateRandom__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR random_data, + CK_ULONG random_len) +{ + return CKR_SESSION_HANDLE_INVALID; +} + +CK_FUNCTION_LIST mock_module_no_slots = { + { CRYPTOKI_VERSION_MAJOR, CRYPTOKI_VERSION_MINOR }, /* version */ + mock_C_Initialize, + mock_C_Finalize, + mock_C_GetInfo, + mock_C_GetFunctionList, + mock_C_GetSlotList__no_tokens, + mock_C_GetSlotInfo__invalid_slotid, + mock_C_GetTokenInfo__invalid_slotid, + mock_C_GetMechanismList__invalid_slotid, + mock_C_GetMechanismInfo__invalid_slotid, + mock_C_InitToken__invalid_slotid, + mock_C_InitPIN__invalid_handle, + mock_C_SetPIN__invalid_handle, + mock_C_OpenSession__invalid_slotid, + mock_C_CloseSession__invalid_handle, + mock_C_CloseAllSessions__invalid_slotid, + mock_C_GetSessionInfo__invalid_handle, + mock_C_GetOperationState__invalid_handle, + mock_C_SetOperationState__invalid_handle, + mock_C_Login__invalid_handle, + mock_C_Logout__invalid_handle, + mock_C_CreateObject__invalid_handle, + mock_C_CopyObject__invalid_handle, + mock_C_DestroyObject__invalid_handle, + mock_C_GetObjectSize__invalid_handle, + mock_C_GetAttributeValue__invalid_handle, + mock_C_SetAttributeValue__invalid_handle, + mock_C_FindObjectsInit__invalid_handle, + mock_C_FindObjects__invalid_handle, + mock_C_FindObjectsFinal__invalid_handle, + mock_C_EncryptInit__invalid_handle, + mock_C_Encrypt__invalid_handle, + mock_C_EncryptUpdate__invalid_handle, + mock_C_EncryptFinal__invalid_handle, + mock_C_DecryptInit__invalid_handle, + mock_C_Decrypt__invalid_handle, + mock_C_DecryptUpdate__invalid_handle, + mock_C_DecryptFinal__invalid_handle, + mock_C_DigestInit__invalid_handle, + mock_C_Digest__invalid_handle, + mock_C_DigestUpdate__invalid_handle, + mock_C_DigestKey__invalid_handle, + mock_C_DigestFinal__invalid_handle, + mock_C_SignInit__invalid_handle, + mock_C_Sign__invalid_handle, + mock_C_SignUpdate__invalid_handle, + mock_C_SignFinal__invalid_handle, + mock_C_SignRecoverInit__invalid_handle, + mock_C_SignRecover__invalid_handle, + mock_C_VerifyInit__invalid_handle, + mock_C_Verify__invalid_handle, + mock_C_VerifyUpdate__invalid_handle, + mock_C_VerifyFinal__invalid_handle, + mock_C_VerifyRecoverInit__invalid_handle, + mock_C_VerifyRecover__invalid_handle, + mock_C_DigestEncryptUpdate__invalid_handle, + mock_C_DecryptDigestUpdate__invalid_handle, + mock_C_SignEncryptUpdate__invalid_handle, + mock_C_DecryptVerifyUpdate__invalid_handle, + mock_C_GenerateKey__invalid_handle, + mock_C_GenerateKeyPair__invalid_handle, + mock_C_WrapKey__invalid_handle, + mock_C_UnwrapKey__invalid_handle, + mock_C_DeriveKey__invalid_handle, + mock_C_SeedRandom__invalid_handle, + mock_C_GenerateRandom__invalid_handle, + mock_C_GetFunctionStatus__not_parallel, + mock_C_CancelFunction__not_parallel, + mock_C_WaitForSlotEvent__no_event, +}; + +void +mock_module_init (void) +{ + static int initialized = 0; + if (!initialized) { + p11_mutex_init (&init_mutex); + initialized = 1; + } +} diff --git a/p11-kit/tests/mock-module.h b/p11-kit/tests/mock-module.h new file mode 100644 index 0000000..9557f70 --- /dev/null +++ b/p11-kit/tests/mock-module.h @@ -0,0 +1,337 @@ +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ +/* mock-module.c - a mock PKCS#11 module + + Copyright (C) 2011 Collabora Ltd. + + The Gnome Keyring Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The Gnome Keyring Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the Gnome Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. + + Author: Stef Walter +*/ + +#ifndef __MOCK_MODULE_H__ +#define __MOCK_MODULE_H__ + +#include "pkcs11.h" + +extern CK_FUNCTION_LIST mock_module_no_slots; + +void mock_module_init (void); + +CK_RV mock_C_Initialize (CK_VOID_PTR init_args); + +CK_RV mock_C_Finalize (CK_VOID_PTR reserved); + +CK_RV mock_C_GetInfo (CK_INFO_PTR info); + +CK_RV mock_C_GetFunctionList (CK_FUNCTION_LIST_PTR_PTR list); + +CK_RV mock_C_GetSlotList__no_tokens (CK_BBOOL token_present, + CK_SLOT_ID_PTR slot_list, + CK_ULONG_PTR count); + +CK_RV mock_C_GetSlotInfo__invalid_slotid (CK_SLOT_ID id, + CK_SLOT_INFO_PTR info); + +CK_RV mock_C_GetTokenInfo__invalid_slotid (CK_SLOT_ID id, + CK_TOKEN_INFO_PTR info); + +CK_RV mock_C_GetMechanismList__invalid_slotid (CK_SLOT_ID id, + CK_MECHANISM_TYPE_PTR mechanism_list, + CK_ULONG_PTR count); + +CK_RV mock_C_GetMechanismInfo__invalid_slotid (CK_SLOT_ID id, + CK_MECHANISM_TYPE type, + CK_MECHANISM_INFO_PTR info); + +CK_RV mock_C_InitToken__invalid_slotid (CK_SLOT_ID id, + CK_UTF8CHAR_PTR pin, + CK_ULONG pin_len, + CK_UTF8CHAR_PTR label); + +CK_RV mock_C_WaitForSlotEvent__no_event (CK_FLAGS flags, + CK_SLOT_ID_PTR slot, + CK_VOID_PTR reserved); + +CK_RV mock_C_OpenSession__invalid_slotid (CK_SLOT_ID id, + CK_FLAGS flags, + CK_VOID_PTR user_data, + CK_NOTIFY callback, + CK_SESSION_HANDLE_PTR session); + +CK_RV mock_C_CloseSession__invalid_handle (CK_SESSION_HANDLE session); + +CK_RV mock_C_CloseAllSessions__invalid_slotid (CK_SLOT_ID id); + +CK_RV mock_C_GetFunctionStatus__not_parallel (CK_SESSION_HANDLE session); + +CK_RV mock_C_CancelFunction__not_parallel (CK_SESSION_HANDLE session); + +CK_RV mock_C_GetSessionInfo__invalid_handle (CK_SESSION_HANDLE session, + CK_SESSION_INFO_PTR info); + +CK_RV mock_C_InitPIN__invalid_handle (CK_SESSION_HANDLE session, + CK_UTF8CHAR_PTR pin, + CK_ULONG pin_len); + +CK_RV mock_C_SetPIN__invalid_handle (CK_SESSION_HANDLE session, + CK_UTF8CHAR_PTR old_pin, + CK_ULONG old_pin_len, + CK_UTF8CHAR_PTR new_pin, + CK_ULONG new_pin_len); + +CK_RV mock_C_GetOperationState__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR operation_state, + CK_ULONG_PTR operation_state_len); + +CK_RV mock_C_SetOperationState__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR operation_state, + CK_ULONG operation_state_len, + CK_OBJECT_HANDLE encryption_key, + CK_OBJECT_HANDLE authentication_key); + +CK_RV mock_C_Login__invalid_handle (CK_SESSION_HANDLE session, + CK_USER_TYPE user_type, + CK_UTF8CHAR_PTR pin, + CK_ULONG pin_len); + +CK_RV mock_C_Logout__invalid_handle (CK_SESSION_HANDLE session); + +CK_RV mock_C_CreateObject__invalid_handle (CK_SESSION_HANDLE session, + CK_ATTRIBUTE_PTR template, + CK_ULONG count, + CK_OBJECT_HANDLE_PTR new_object); + +CK_RV mock_C_CopyObject__invalid_handle (CK_SESSION_HANDLE session, + CK_OBJECT_HANDLE object, + CK_ATTRIBUTE_PTR template, + CK_ULONG count, + CK_OBJECT_HANDLE_PTR new_object); + +CK_RV mock_C_DestroyObject__invalid_handle (CK_SESSION_HANDLE session, + CK_OBJECT_HANDLE object); + +CK_RV mock_C_GetObjectSize__invalid_handle (CK_SESSION_HANDLE session, + CK_OBJECT_HANDLE object, + CK_ULONG_PTR size); + +CK_RV mock_C_GetAttributeValue__invalid_handle (CK_SESSION_HANDLE session, + CK_OBJECT_HANDLE object, + CK_ATTRIBUTE_PTR template, + CK_ULONG count); + +CK_RV mock_C_SetAttributeValue__invalid_handle (CK_SESSION_HANDLE session, + CK_OBJECT_HANDLE object, + CK_ATTRIBUTE_PTR template, + CK_ULONG count); + +CK_RV mock_C_FindObjectsInit__invalid_handle (CK_SESSION_HANDLE session, + CK_ATTRIBUTE_PTR template, + CK_ULONG count); + +CK_RV mock_C_FindObjects__invalid_handle (CK_SESSION_HANDLE session, + CK_OBJECT_HANDLE_PTR objects, + CK_ULONG max_count, + CK_ULONG_PTR count); + +CK_RV mock_C_FindObjectsFinal__invalid_handle (CK_SESSION_HANDLE session); + +CK_RV mock_C_EncryptInit__invalid_handle (CK_SESSION_HANDLE session, + CK_MECHANISM_PTR mechanism, + CK_OBJECT_HANDLE key); + +CK_RV mock_C_Encrypt__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR data, + CK_ULONG data_len, + CK_BYTE_PTR encrypted_data, + CK_ULONG_PTR encrypted_data_len); + +CK_RV mock_C_EncryptUpdate__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR part, + CK_ULONG part_len, + CK_BYTE_PTR encrypted_part, + CK_ULONG_PTR encrypted_part_len); + +CK_RV mock_C_EncryptFinal__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR last_part, + CK_ULONG_PTR last_part_len); + +CK_RV mock_C_DecryptInit__invalid_handle (CK_SESSION_HANDLE session, + CK_MECHANISM_PTR mechanism, + CK_OBJECT_HANDLE key); + +CK_RV mock_C_Decrypt__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR enc_data, + CK_ULONG enc_data_len, + CK_BYTE_PTR data, + CK_ULONG_PTR data_len); + +CK_RV mock_C_DecryptUpdate__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR enc_part, + CK_ULONG enc_part_len, + CK_BYTE_PTR part, + CK_ULONG_PTR part_len); + +CK_RV mock_C_DecryptFinal__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR last_part, + CK_ULONG_PTR last_part_len); + +CK_RV mock_C_DigestInit__invalid_handle (CK_SESSION_HANDLE session, + CK_MECHANISM_PTR mechanism); + +CK_RV mock_C_Digest__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR data, + CK_ULONG data_len, + CK_BYTE_PTR digest, + CK_ULONG_PTR digest_len); + +CK_RV mock_C_DigestUpdate__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR part, + CK_ULONG part_len); + +CK_RV mock_C_DigestKey__invalid_handle (CK_SESSION_HANDLE session, + CK_OBJECT_HANDLE key); + +CK_RV mock_C_DigestFinal__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR digest, + CK_ULONG_PTR digest_len); + +CK_RV mock_C_SignInit__invalid_handle (CK_SESSION_HANDLE session, + CK_MECHANISM_PTR mechanism, + CK_OBJECT_HANDLE key); + +CK_RV mock_C_Sign__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR data, + CK_ULONG data_len, + CK_BYTE_PTR signature, + CK_ULONG_PTR signature_len); + +CK_RV mock_C_SignUpdate__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR part, + CK_ULONG part_len); + +CK_RV mock_C_SignFinal__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR signature, + CK_ULONG_PTR signature_len); + +CK_RV mock_C_SignRecoverInit__invalid_handle (CK_SESSION_HANDLE session, + CK_MECHANISM_PTR mechanism, + CK_OBJECT_HANDLE key); + +CK_RV mock_C_SignRecover__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR data, + CK_ULONG data_len, + CK_BYTE_PTR signature, + CK_ULONG_PTR signature_len); + +CK_RV mock_C_VerifyInit__invalid_handle (CK_SESSION_HANDLE session, + CK_MECHANISM_PTR mechanism, + CK_OBJECT_HANDLE key); + +CK_RV mock_C_Verify__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR data, + CK_ULONG data_len, + CK_BYTE_PTR signature, + CK_ULONG signature_len); + +CK_RV mock_C_VerifyUpdate__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR part, + CK_ULONG part_len); + +CK_RV mock_C_VerifyFinal__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR signature, + CK_ULONG signature_len); + +CK_RV mock_C_VerifyRecoverInit__invalid_handle (CK_SESSION_HANDLE session, + CK_MECHANISM_PTR mechanism, + CK_OBJECT_HANDLE key); + +CK_RV mock_C_VerifyRecover__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR signature, + CK_ULONG signature_len, + CK_BYTE_PTR data, + CK_ULONG_PTR data_len); + +CK_RV mock_C_DigestEncryptUpdate__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR part, + CK_ULONG part_len, + CK_BYTE_PTR enc_part, + CK_ULONG_PTR enc_part_len); + +CK_RV mock_C_DecryptDigestUpdate__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR enc_part, + CK_ULONG enc_part_len, + CK_BYTE_PTR part, + CK_ULONG_PTR part_len); + +CK_RV mock_C_SignEncryptUpdate__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR part, + CK_ULONG part_len, + CK_BYTE_PTR enc_part, + CK_ULONG_PTR enc_part_len); + +CK_RV mock_C_DecryptVerifyUpdate__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR enc_part, + CK_ULONG enc_part_len, + CK_BYTE_PTR part, + CK_ULONG_PTR part_len); + +CK_RV mock_C_GenerateKey__invalid_handle (CK_SESSION_HANDLE session, + CK_MECHANISM_PTR mechanism, + CK_ATTRIBUTE_PTR template, + CK_ULONG count, + CK_OBJECT_HANDLE_PTR key); + +CK_RV mock_C_GenerateKeyPair__invalid_handle (CK_SESSION_HANDLE session, + CK_MECHANISM_PTR mechanism, + CK_ATTRIBUTE_PTR pub_template, + CK_ULONG pub_count, + CK_ATTRIBUTE_PTR priv_template, + CK_ULONG priv_count, + CK_OBJECT_HANDLE_PTR pub_key, + CK_OBJECT_HANDLE_PTR priv_key); + +CK_RV mock_C_WrapKey__invalid_handle (CK_SESSION_HANDLE session, + CK_MECHANISM_PTR mechanism, + CK_OBJECT_HANDLE wrapping_key, + CK_OBJECT_HANDLE key, + CK_BYTE_PTR wrapped_key, + CK_ULONG_PTR wrapped_key_len); + +CK_RV mock_C_UnwrapKey__invalid_handle (CK_SESSION_HANDLE session, + CK_MECHANISM_PTR mechanism, + CK_OBJECT_HANDLE unwrapping_key, + CK_BYTE_PTR wrapped_key, + CK_ULONG wrapped_key_len, + CK_ATTRIBUTE_PTR template, + CK_ULONG count, + CK_OBJECT_HANDLE_PTR key); + +CK_RV mock_C_DeriveKey__invalid_handle (CK_SESSION_HANDLE session, + CK_MECHANISM_PTR mechanism, + CK_OBJECT_HANDLE base_key, + CK_ATTRIBUTE_PTR template, + CK_ULONG count, + CK_OBJECT_HANDLE_PTR key); + +CK_RV mock_C_SeedRandom__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR seed, + CK_ULONG seed_len); + +CK_RV mock_C_GenerateRandom__invalid_handle (CK_SESSION_HANDLE session, + CK_BYTE_PTR random_data, + CK_ULONG random_len); + +#endif /* __MOCK_MODULE_H__ */ diff --git a/p11-kit/tests/pin-test.c b/p11-kit/tests/pin-test.c new file mode 100644 index 0000000..5427f8b --- /dev/null +++ b/p11-kit/tests/pin-test.c @@ -0,0 +1,325 @@ +/* + * 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 + */ + +#include "config.h" +#include "CuTest.h" + +#include "library.h" + +#include +#include +#include +#include +#include + +#include "p11-kit/pin.h" +#include "p11-kit/private.h" + +static P11KitPin * +callback_one (const char *pin_source, P11KitUri *pin_uri, const char *pin_description, + P11KitPinFlags pin_flags, void *callback_data) +{ + int *data = callback_data; + assert (*data == 33); + return p11_kit_pin_new_for_buffer ((unsigned char*)strdup ("one"), 3, free); +} + +static P11KitPin* +callback_other (const char *pin_source, P11KitUri *pin_uri, const char *pin_description, + P11KitPinFlags pin_flags, void *callback_data) +{ + char *data = callback_data; + return p11_kit_pin_new_for_string (data); +} + +static void +destroy_data (void *callback_data) +{ + int *data = callback_data; + (*data)++; +} + +static void +test_pin_register_unregister (CuTest *tc) +{ + int data = 33; + + p11_kit_pin_register_callback ("/the/pin_source", callback_one, + &data, destroy_data); + + p11_kit_pin_unregister_callback ("/the/pin_source", callback_one, + &data); + + CuAssertIntEquals (tc, 34, data); +} + +static void +test_pin_read (CuTest *tc) +{ + P11KitUri *uri; + P11KitPin *pin; + int data = 33; + size_t length; + const unsigned char *ptr; + + p11_kit_pin_register_callback ("/the/pin_source", callback_one, + &data, destroy_data); + + uri = p11_kit_uri_new (); + pin = p11_kit_pin_request ("/the/pin_source", uri, "The token", + P11_KIT_PIN_FLAGS_USER_LOGIN); + p11_kit_uri_free (uri); + + CuAssertPtrNotNull (tc, pin); + ptr = p11_kit_pin_get_value (pin, &length); + CuAssertIntEquals (tc, 3, length); + CuAssertTrue (tc, memcmp (ptr, "one", 3) == 0); + + p11_kit_pin_unregister_callback ("/the/pin_source", callback_one, + &data); + + p11_kit_pin_ref (pin); + p11_kit_pin_unref (pin); +} + +static void +test_pin_read_no_match (CuTest *tc) +{ + P11KitUri *uri; + P11KitPin *pin; + + uri = p11_kit_uri_new (); + pin = p11_kit_pin_request ("/the/pin_source", uri, "The token", + P11_KIT_PIN_FLAGS_USER_LOGIN); + p11_kit_uri_free (uri); + + CuAssertPtrEquals (tc, NULL, pin); +} + +static void +test_pin_register_duplicate (CuTest *tc) +{ + P11KitUri *uri; + P11KitPin *pin; + char *value = "secret"; + int data = 33; + size_t length; + const unsigned char *ptr; + + uri = p11_kit_uri_new (); + + p11_kit_pin_register_callback ("/the/pin_source", callback_one, + &data, destroy_data); + + p11_kit_pin_register_callback ("/the/pin_source", callback_other, + value, NULL); + + pin = p11_kit_pin_request ("/the/pin_source", uri, "The token", + P11_KIT_PIN_FLAGS_USER_LOGIN); + + CuAssertPtrNotNull (tc, pin); + ptr = p11_kit_pin_get_value (pin, &length); + CuAssertIntEquals (tc, 6, length); + CuAssertTrue (tc, memcmp (ptr, "secret", length) == 0); + p11_kit_pin_unref (pin); + + p11_kit_pin_unregister_callback ("/the/pin_source", callback_other, + value); + + pin = p11_kit_pin_request ("/the/pin_source", uri, "The token", + P11_KIT_PIN_FLAGS_USER_LOGIN); + + CuAssertPtrNotNull (tc, pin); + ptr = p11_kit_pin_get_value (pin, &length); + CuAssertIntEquals (tc, 3, length); + CuAssertTrue (tc, memcmp (ptr, "one", length) == 0); + p11_kit_pin_unref (pin); + + p11_kit_pin_unregister_callback ("/the/pin_source", callback_one, + &data); + + pin = p11_kit_pin_request ("/the/pin_source", uri, "The token", + P11_KIT_PIN_FLAGS_USER_LOGIN); + + CuAssertPtrEquals (tc, NULL, pin); + + p11_kit_uri_free (uri); +} + +static void +test_pin_register_fallback (CuTest *tc) +{ + char *value = "secret"; + P11KitUri *uri; + P11KitPin *pin; + int data = 33; + size_t length; + const unsigned char *ptr; + + uri = p11_kit_uri_new (); + + p11_kit_pin_register_callback (P11_KIT_PIN_FALLBACK, callback_one, + &data, destroy_data); + + pin = p11_kit_pin_request ("/the/pin_source", uri, "The token", + P11_KIT_PIN_FLAGS_USER_LOGIN); + + CuAssertPtrNotNull (tc, pin); + ptr = p11_kit_pin_get_value (pin, &length); + CuAssertIntEquals (tc, 3, length); + CuAssertTrue (tc, memcmp (ptr, "one", length) == 0); + p11_kit_pin_unref (pin); + + p11_kit_pin_register_callback ("/the/pin_source", callback_other, + value, NULL); + + pin = p11_kit_pin_request ("/the/pin_source", uri, "The token", + P11_KIT_PIN_FLAGS_USER_LOGIN); + + CuAssertPtrNotNull (tc, pin); + ptr = p11_kit_pin_get_value (pin, &length); + CuAssertIntEquals (tc, 6, length); + CuAssertTrue (tc, memcmp (ptr, "secret", length) == 0); + p11_kit_pin_unref (pin); + + p11_kit_pin_unregister_callback ("/the/pin_source", callback_other, + value); + + p11_kit_pin_unregister_callback (P11_KIT_PIN_FALLBACK, callback_one, + &data); + + p11_kit_uri_free (uri); +} + +static void +test_pin_file (CuTest *tc) +{ + P11KitUri *uri; + P11KitPin *pin; + size_t length; + const unsigned char *ptr; + + uri = p11_kit_uri_new (); + + p11_kit_pin_register_callback (P11_KIT_PIN_FALLBACK, p11_kit_pin_file_callback, + NULL, NULL); + + pin = p11_kit_pin_request (SRCDIR "/files/test-pinfile", uri, "The token", + P11_KIT_PIN_FLAGS_USER_LOGIN); + + CuAssertPtrNotNull (tc, pin); + ptr = p11_kit_pin_get_value (pin, &length); + CuAssertIntEquals (tc, 12, length); + CuAssertTrue (tc, memcmp (ptr, "yogabbagabba", length) == 0); + p11_kit_pin_unref (pin); + + pin = p11_kit_pin_request (SRCDIR "/files/nonexistant", uri, "The token", + P11_KIT_PIN_FLAGS_USER_LOGIN); + + CuAssertPtrEquals (tc, NULL, pin); + + p11_kit_pin_unregister_callback (P11_KIT_PIN_FALLBACK, p11_kit_pin_file_callback, + NULL); + + p11_kit_uri_free (uri); +} + +static void +test_pin_file_large (CuTest *tc) +{ + P11KitUri *uri; + P11KitPin *pin; + int error; + + uri = p11_kit_uri_new (); + + p11_kit_pin_register_callback (P11_KIT_PIN_FALLBACK, p11_kit_pin_file_callback, + NULL, NULL); + + pin = p11_kit_pin_request (SRCDIR "/files/test-pinfile-large", uri, "The token", + P11_KIT_PIN_FLAGS_USER_LOGIN); + + error = errno; + CuAssertPtrEquals (tc, NULL, pin); + CuAssertIntEquals (tc, EFBIG, error); + + p11_kit_pin_unregister_callback (P11_KIT_PIN_FALLBACK, p11_kit_pin_file_callback, + NULL); + + p11_kit_uri_free (uri); +} + +static void +test_pin_ref_unref (CuTest *tc) +{ + P11KitPin *pin; + P11KitPin *check; + + pin = p11_kit_pin_new_for_string ("crack of lies"); + + check = p11_kit_pin_ref (pin); + CuAssertPtrEquals (tc, pin, check); + + p11_kit_pin_unref (pin); + p11_kit_pin_unref (check); +} + +int +main (void) +{ + CuString *output = CuStringNew (); + CuSuite* suite = CuSuiteNew (); + int ret; + + p11_library_init (); + + SUITE_ADD_TEST (suite, test_pin_register_unregister); + SUITE_ADD_TEST (suite, test_pin_read); + SUITE_ADD_TEST (suite, test_pin_read_no_match); + SUITE_ADD_TEST (suite, test_pin_register_duplicate); + SUITE_ADD_TEST (suite, test_pin_register_fallback); + SUITE_ADD_TEST (suite, test_pin_file); + SUITE_ADD_TEST (suite, test_pin_file_large); + SUITE_ADD_TEST (suite, test_pin_ref_unref); + + CuSuiteRun (suite); + CuSuiteSummary (suite, output); + CuSuiteDetails (suite, output); + printf ("%s\n", output->buffer); + ret = suite->failCount; + CuSuiteDelete (suite); + CuStringDelete (output); + + return ret; +} diff --git a/p11-kit/tests/print-messages.c b/p11-kit/tests/print-messages.c new file mode 100644 index 0000000..5870ad1 --- /dev/null +++ b/p11-kit/tests/print-messages.c @@ -0,0 +1,137 @@ +/* + * 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 + */ + +#include "config.h" + +#include +#include +#include +#include + +#include "p11-kit.h" + +int +main (int argc, char *argv[]) +{ + if (argc != 1) { + fprintf (stderr, "usage: print-messages\n"); + exit (2); + } + + #define X(x) printf ("%s: %s\n", #x, p11_kit_strerror (x)) + X(CKR_CANCEL); + X(CKR_FUNCTION_CANCELED); + X(CKR_HOST_MEMORY); + X(CKR_SLOT_ID_INVALID); + X(CKR_GENERAL_ERROR); + X(CKR_FUNCTION_FAILED); + X(CKR_ARGUMENTS_BAD); + X(CKR_NEED_TO_CREATE_THREADS); + X(CKR_CANT_LOCK); + X(CKR_ATTRIBUTE_READ_ONLY); + X(CKR_ATTRIBUTE_SENSITIVE); + X(CKR_ATTRIBUTE_TYPE_INVALID); + X(CKR_ATTRIBUTE_VALUE_INVALID); + X(CKR_DATA_INVALID); + X(CKR_DATA_LEN_RANGE); + X(CKR_DEVICE_ERROR); + X(CKR_DEVICE_MEMORY); + X(CKR_DEVICE_REMOVED); + X(CKR_ENCRYPTED_DATA_INVALID); + X(CKR_ENCRYPTED_DATA_LEN_RANGE); + X(CKR_FUNCTION_NOT_SUPPORTED); + X(CKR_KEY_HANDLE_INVALID); + X(CKR_KEY_SIZE_RANGE); + X(CKR_KEY_TYPE_INCONSISTENT); + X(CKR_KEY_NOT_NEEDED); + X(CKR_KEY_CHANGED); + X(CKR_KEY_NEEDED); + X(CKR_KEY_INDIGESTIBLE); + X(CKR_KEY_FUNCTION_NOT_PERMITTED); + X(CKR_KEY_NOT_WRAPPABLE); + X(CKR_KEY_UNEXTRACTABLE); + X(CKR_MECHANISM_INVALID); + X(CKR_MECHANISM_PARAM_INVALID); + X(CKR_OBJECT_HANDLE_INVALID); + X(CKR_OPERATION_ACTIVE); + X(CKR_OPERATION_NOT_INITIALIZED); + X(CKR_PIN_INCORRECT); + X(CKR_PIN_INVALID); + X(CKR_PIN_LEN_RANGE); + X(CKR_PIN_EXPIRED); + X(CKR_PIN_LOCKED); + X(CKR_SESSION_CLOSED); + X(CKR_SESSION_COUNT); + X(CKR_SESSION_HANDLE_INVALID); + X(CKR_SESSION_READ_ONLY); + X(CKR_SESSION_EXISTS); + X(CKR_SESSION_READ_ONLY_EXISTS); + X(CKR_SESSION_READ_WRITE_SO_EXISTS); + X(CKR_SIGNATURE_INVALID); + X(CKR_SIGNATURE_LEN_RANGE); + X(CKR_TEMPLATE_INCOMPLETE); + X(CKR_TEMPLATE_INCONSISTENT); + X(CKR_TOKEN_NOT_PRESENT); + X(CKR_TOKEN_NOT_RECOGNIZED); + X(CKR_TOKEN_WRITE_PROTECTED); + X(CKR_UNWRAPPING_KEY_HANDLE_INVALID); + X(CKR_UNWRAPPING_KEY_SIZE_RANGE); + X(CKR_UNWRAPPING_KEY_TYPE_INCONSISTENT); + X(CKR_USER_ALREADY_LOGGED_IN); + X(CKR_USER_NOT_LOGGED_IN); + X(CKR_USER_PIN_NOT_INITIALIZED); + X(CKR_USER_TYPE_INVALID); + X(CKR_USER_ANOTHER_ALREADY_LOGGED_IN); + X(CKR_USER_TOO_MANY_TYPES); + X(CKR_WRAPPED_KEY_INVALID); + X(CKR_WRAPPED_KEY_LEN_RANGE); + X(CKR_WRAPPING_KEY_HANDLE_INVALID); + X(CKR_WRAPPING_KEY_SIZE_RANGE); + X(CKR_WRAPPING_KEY_TYPE_INCONSISTENT); + X(CKR_RANDOM_SEED_NOT_SUPPORTED); + X(CKR_RANDOM_NO_RNG); + X(CKR_DOMAIN_PARAMS_INVALID); + X(CKR_BUFFER_TOO_SMALL); + X(CKR_SAVED_STATE_INVALID); + X(CKR_INFORMATION_SENSITIVE); + X(CKR_STATE_UNSAVEABLE); + X(CKR_CRYPTOKI_NOT_INITIALIZED); + X(CKR_CRYPTOKI_ALREADY_INITIALIZED); + X(CKR_MUTEX_BAD); + X(CKR_MUTEX_NOT_LOCKED); + X(CKR_FUNCTION_REJECTED); + #undef X + + return 0; +} diff --git a/p11-kit/tests/progname-test.c b/p11-kit/tests/progname-test.c new file mode 100644 index 0000000..bd37400 --- /dev/null +++ b/p11-kit/tests/progname-test.c @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2012 Stefan Walter + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * * The names of contributors to this software may not be + * used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * Author: Stef Walter + */ + +#include "config.h" +#include "CuTest.h" + +#include "library.h" + +#include +#include +#include +#include + +#include "p11-kit/uri.h" +#include "p11-kit/p11-kit.h" +#include "p11-kit/private.h" + +static void +test_progname_default (CuTest *tc) +{ + const char *progname; + + progname = _p11_get_progname_unlocked (); + CuAssertStrEquals (tc, "progname-test", progname); +} + +static void +test_progname_set (CuTest *tc) +{ + const char *progname; + + p11_kit_set_progname ("love-generation"); + + progname = _p11_get_progname_unlocked (); + CuAssertStrEquals (tc, "love-generation", progname); + + _p11_set_progname_unlocked (NULL); + + progname = _p11_get_progname_unlocked (); + CuAssertStrEquals (tc, "progname-test", progname); +} + +/* Defined in util.c */ +extern char p11_my_progname[]; + +int +main (void) +{ + CuString *output = CuStringNew (); + CuSuite* suite = CuSuiteNew (); + int ret; + + p11_library_init (); + + SUITE_ADD_TEST (suite, test_progname_default); + SUITE_ADD_TEST (suite, test_progname_set); + + CuSuiteRun (suite); + CuSuiteSummary (suite, output); + CuSuiteDetails (suite, output); + printf ("%s\n", output->buffer); + ret = suite->failCount; + CuSuiteDelete (suite); + CuStringDelete (output); + return ret; +} diff --git a/p11-kit/tests/test-init.c b/p11-kit/tests/test-init.c new file mode 100644 index 0000000..5861bbf --- /dev/null +++ b/p11-kit/tests/test-init.c @@ -0,0 +1,320 @@ +/* + * 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 + */ + +#include "config.h" +#include "CuTest.h" + +#include + +#include "library.h" + +#include +#include +#include +#include +#include +#include + +#include "p11-kit/p11-kit.h" + +#include "mock-module.h" + +CK_FUNCTION_LIST module; + +#ifdef OS_UNIX + +#include + +static CK_RV +mock_C_Initialize__with_fork (CK_VOID_PTR init_args) +{ + struct timespec ts = { 0, 100 * 1000 * 1000 }; + CK_RV rv; + pid_t child; + pid_t ret; + int status; + + rv = mock_C_Initialize (init_args); + assert (rv == CKR_OK); + + /* Fork during the initialization */ + child = fork (); + if (child == 0) { + nanosleep (&ts, NULL); + exit (66); + } + + ret = waitpid (child, &status, 0); + assert (ret == child); + assert (WIFEXITED (status)); + assert (WEXITSTATUS (status) == 66); + + return CKR_OK; +} + +static void +test_fork_initialization (CuTest *tc) +{ + CK_RV rv; + + /* Build up our own function list */ + memcpy (&module, &mock_module_no_slots, sizeof (CK_FUNCTION_LIST)); + module.C_Initialize = mock_C_Initialize__with_fork; + + rv = p11_kit_initialize_module (&module); + CuAssertTrue (tc, rv == CKR_OK); + + rv = p11_kit_finalize_module (&module); + CuAssertTrue (tc, rv == CKR_OK); +} + +#endif /* OS_UNIX */ + +static CK_RV +mock_C_Initialize__with_recursive (CK_VOID_PTR init_args) +{ + CK_RV rv; + + rv = mock_C_Initialize (init_args); + assert (rv == CKR_OK); + + /* Recursively initialize, this is broken */ + return p11_kit_initialize_module (&module); +} + +static void +test_recursive_initialization (CuTest *tc) +{ + CK_RV rv; + + /* Build up our own function list */ + memcpy (&module, &mock_module_no_slots, sizeof (CK_FUNCTION_LIST)); + module.C_Initialize = mock_C_Initialize__with_recursive; + + rv = p11_kit_initialize_module (&module); + CuAssertTrue (tc, rv == CKR_FUNCTION_FAILED); +} + +static p11_mutex_t race_mutex; +static int initialization_count = 0; +static int finalization_count = 0; + +#include "private.h" + +static CK_RV +mock_C_Initialize__threaded_race (CK_VOID_PTR init_args) +{ + /* Atomically increment value */ + p11_mutex_lock (&race_mutex); + initialization_count += 1; + p11_mutex_unlock (&race_mutex); + + p11_sleep_ms (100); + return CKR_OK; +} + +static CK_RV +mock_C_Finalize__threaded_race (CK_VOID_PTR reserved) +{ + /* Atomically increment value */ + p11_mutex_lock (&race_mutex); + finalization_count += 1; + p11_mutex_unlock (&race_mutex); + + p11_sleep_ms (100); + return CKR_OK; +} + +static void * +initialization_thread (void *data) +{ + CuTest *tc = data; + CK_RV rv; + + rv = p11_kit_initialize_module (&module); + CuAssertTrue (tc, rv == CKR_OK); + + return tc; +} + +static void * +finalization_thread (void *data) +{ + CuTest *tc = data; + CK_RV rv; + + rv = p11_kit_finalize_module (&module); + CuAssertTrue (tc, rv == CKR_OK); + + return tc; +} + +static void +test_threaded_initialization (CuTest *tc) +{ + static const int num_threads = 2; + p11_thread_t threads[num_threads]; + int ret; + int i; + + /* Build up our own function list */ + memcpy (&module, &mock_module_no_slots, sizeof (CK_FUNCTION_LIST)); + module.C_Initialize = mock_C_Initialize__threaded_race; + module.C_Finalize = mock_C_Finalize__threaded_race; + + initialization_count = 0; + finalization_count = 0; + + for (i = 0; i < num_threads; i++) { + ret = p11_thread_create (&threads[i], initialization_thread, tc); + CuAssertIntEquals (tc, 0, ret); + CuAssertTrue (tc, threads[i] != 0); + } + + for (i = 0; i < num_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); + CuAssertIntEquals (tc, 0, ret); + CuAssertTrue (tc, threads[i] != 0); + } + + for (i = 0; i < num_threads; i++) { + ret = p11_thread_join (threads[i]); + CuAssertIntEquals (tc, 0, ret); + threads[i] = 0; + } + + /* C_Initialize should have been called exactly once */ + CuAssertIntEquals (tc, 1, initialization_count); + CuAssertIntEquals (tc, 1, finalization_count); +} + +static CK_RV +mock_C_Initialize__test_mutexes (CK_VOID_PTR args) +{ + CK_C_INITIALIZE_ARGS_PTR init_args; + void *mutex = NULL; + CK_RV rv; + + assert (args != NULL); + init_args = args; + + rv = (init_args->CreateMutex) (&mutex); + assert (rv == CKR_OK); + + rv = (init_args->LockMutex) (mutex); + assert (rv == CKR_OK); + + rv = (init_args->UnlockMutex) (mutex); + assert (rv == CKR_OK); + + rv = (init_args->DestroyMutex) (mutex); + assert (rv == CKR_OK); + + return CKR_OK; +} + +static void +test_mutexes (CuTest *tc) +{ + CK_RV rv; + + /* Build up our own function list */ + memcpy (&module, &mock_module_no_slots, sizeof (CK_FUNCTION_LIST)); + module.C_Initialize = mock_C_Initialize__test_mutexes; + + rv = p11_kit_initialize_module (&module); + CuAssertTrue (tc, rv == CKR_OK); + + rv = p11_kit_finalize_module (&module); + CuAssertTrue (tc, rv == CKR_OK); +} + +static void +test_load_and_initialize (CuTest *tc) +{ + CK_FUNCTION_LIST_PTR module; + CK_INFO info; + CK_RV rv; + int ret; + + rv = p11_kit_load_initialize_module (BUILDDIR "/.libs/mock-one" SHLEXT, &module); + CuAssertTrue (tc, rv == CKR_OK); + CuAssertTrue (tc, module != NULL); + + rv = (module->C_GetInfo) (&info); + CuAssertTrue (tc, rv == CKR_OK); + + ret = memcmp (info.manufacturerID, "MOCK MANUFACTURER ", 32); + CuAssertTrue (tc, ret == 0); + + rv = p11_kit_finalize_module (module); + CuAssertTrue (tc, ret == CKR_OK); +} + +int +main (void) +{ + CuString *output = CuStringNew (); + CuSuite* suite = CuSuiteNew (); + int ret; + + p11_mutex_init (&race_mutex); + mock_module_init (); + p11_library_init (); + +#ifdef OS_UNIX + SUITE_ADD_TEST (suite, test_fork_initialization); +#endif + + SUITE_ADD_TEST (suite, test_recursive_initialization); + SUITE_ADD_TEST (suite, test_threaded_initialization); + SUITE_ADD_TEST (suite, test_mutexes); + SUITE_ADD_TEST (suite, test_load_and_initialize); + + CuSuiteRun (suite); + CuSuiteSummary (suite, output); + CuSuiteDetails (suite, output); + printf ("%s\n", output->buffer); + ret = suite->failCount; + CuSuiteDelete (suite); + CuStringDelete (output); + + return ret; +} diff --git a/p11-kit/tests/test-modules.c b/p11-kit/tests/test-modules.c new file mode 100644 index 0000000..159e04e --- /dev/null +++ b/p11-kit/tests/test-modules.c @@ -0,0 +1,246 @@ +/* + * Copyright (c) 2012 Red Hat Inc + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * * The names of contributors to this software may not be + * used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * Author: Stef Walter + */ + +#include "config.h" +#include "CuTest.h" + +#include +#include +#include +#include + +#include "library.h" +#include "p11-kit.h" +#include "private.h" +#include "dict.h" + +static CK_FUNCTION_LIST_PTR_PTR +initialize_and_get_modules (CuTest *tc) +{ + CK_FUNCTION_LIST_PTR_PTR modules; + CK_RV rv; + + rv = p11_kit_initialize_registered (); + CuAssertIntEquals (tc, CKR_OK, rv); + modules = p11_kit_registered_modules (); + CuAssertTrue (tc, modules != NULL && modules[0] != NULL); + + return modules; +} + +static void +finalize_and_free_modules (CuTest *tc, + CK_FUNCTION_LIST_PTR_PTR modules) +{ + CK_RV rv; + + free (modules); + rv = p11_kit_finalize_registered (); + CuAssertIntEquals (tc, CKR_OK, rv); +} + +static void +test_no_duplicates (CuTest *tc) +{ + CK_FUNCTION_LIST_PTR_PTR modules; + p11_dict *paths; + p11_dict *funcs; + char *path; + int i; + + modules = initialize_and_get_modules (tc); + paths = p11_dict_new (p11_dict_str_hash, p11_dict_str_equal, NULL, NULL); + funcs = p11_dict_new (p11_dict_direct_hash, p11_dict_direct_equal, NULL, NULL); + + /* The loaded modules should not contain duplicates */ + for (i = 0; modules[i] != NULL; i++) { + path = p11_kit_registered_option (modules[i], "module"); + + if (p11_dict_get (funcs, modules[i])) + CuAssert (tc, "found duplicate function list pointer", 0); + if (p11_dict_get (paths, path)) + CuAssert (tc, "found duplicate path name", 0); + + if (!p11_dict_set (funcs, modules[i], "")) + CuAssert (tc, "shouldn't be reached", 0); + if (!p11_dict_set (paths, path, "")) + CuAssert (tc, "shouldn't be reached", 0); + } + + p11_dict_free (paths); + p11_dict_free (funcs); + finalize_and_free_modules (tc, modules); +} + +static CK_FUNCTION_LIST_PTR +lookup_module_with_name (CuTest *tc, + CK_FUNCTION_LIST_PTR_PTR modules, + const char *name) +{ + CK_FUNCTION_LIST_PTR match = NULL; + CK_FUNCTION_LIST_PTR module; + char *module_name; + int i; + + for (i = 0; match == NULL && modules[i] != NULL; i++) { + module_name = p11_kit_registered_module_to_name (modules[i]); + CuAssertPtrNotNull (tc, module_name); + if (strcmp (module_name, name) == 0) + match = modules[i]; + free (module_name); + } + + /* + * As a side effect, we should check that the results of this function + * matches the above search. + */ + module = p11_kit_registered_name_to_module (name); + CuAssert(tc, "different result from p11_kit_registered_name_to_module()", + module == match); + + return match; +} + +static void +test_disable (CuTest *tc) +{ + CK_FUNCTION_LIST_PTR_PTR modules; + + /* + * The module four should be present, as we don't match any prognames + * that it has disabled. + */ + + modules = initialize_and_get_modules (tc); + CuAssertTrue (tc, lookup_module_with_name (tc, modules, "four") != NULL); + finalize_and_free_modules (tc, modules); + + /* + * The module two shouldn't have been loaded, because in its config + * file we have: + * + * disable-in: test-disable + */ + + p11_kit_set_progname ("test-disable"); + + modules = initialize_and_get_modules (tc); + CuAssertTrue (tc, lookup_module_with_name (tc, modules, "four") == NULL); + finalize_and_free_modules (tc, modules); + + p11_kit_set_progname (NULL); +} + +static void +test_disable_later (CuTest *tc) +{ + CK_FUNCTION_LIST_PTR_PTR modules; + CK_RV rv; + + /* + * The module two shouldn't be matched, because in its config + * file we have: + * + * disable-in: test-disable + */ + + rv = p11_kit_initialize_registered (); + CuAssertIntEquals (tc, CKR_OK, rv); + + p11_kit_set_progname ("test-disable"); + + modules = p11_kit_registered_modules (); + CuAssertTrue (tc, modules != NULL && modules[0] != NULL); + + CuAssertTrue (tc, lookup_module_with_name (tc, modules, "two") == NULL); + finalize_and_free_modules (tc, modules); + + p11_kit_set_progname (NULL); +} + +static void +test_enable (CuTest *tc) +{ + CK_FUNCTION_LIST_PTR_PTR modules; + + /* + * The module three should not be present, as we don't match the current + * program. + */ + + modules = initialize_and_get_modules (tc); + CuAssertTrue (tc, lookup_module_with_name (tc, modules, "three") == NULL); + finalize_and_free_modules (tc, modules); + + /* + * The module three should be loaded here , because in its config + * file we have: + * + * enable-in: test-enable + */ + + p11_kit_set_progname ("test-enable"); + + modules = initialize_and_get_modules (tc); + CuAssertTrue (tc, lookup_module_with_name (tc, modules, "three") != NULL); + finalize_and_free_modules (tc, modules); + + p11_kit_set_progname (NULL); +} + +int +main (void) +{ + CuString *output = CuStringNew (); + CuSuite* suite = CuSuiteNew (); + int ret; + + p11_library_init (); + + SUITE_ADD_TEST (suite, test_no_duplicates); + SUITE_ADD_TEST (suite, test_disable); + SUITE_ADD_TEST (suite, test_disable_later); + SUITE_ADD_TEST (suite, test_enable); + + p11_kit_be_quiet (); + + CuSuiteRun (suite); + CuSuiteSummary (suite, output); + CuSuiteDetails (suite, output); + printf ("%s\n", output->buffer); + ret = suite->failCount; + CuSuiteDelete (suite); + CuStringDelete (output); + return ret; +} diff --git a/p11-kit/tests/uri-test.c b/p11-kit/tests/uri-test.c new file mode 100644 index 0000000..0e3e722 --- /dev/null +++ b/p11-kit/tests/uri-test.c @@ -0,0 +1,1223 @@ +/* + * 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 + */ + +#include "config.h" +#include "CuTest.h" + +#include "library.h" + +#include +#include +#include +#include + +#include "p11-kit/uri.h" +#include "p11-kit/private.h" + +static int +is_module_empty (P11KitUri *uri) +{ + CK_INFO_PTR info = p11_kit_uri_get_module_info (uri); + return (info->libraryDescription[0] == 0 && + info->manufacturerID[0] == 0 && + info->libraryVersion.major == (CK_BYTE)-1 && + info->libraryVersion.minor == (CK_BYTE)-1); +} + +static int +is_token_empty (P11KitUri *uri) +{ + CK_TOKEN_INFO_PTR token = p11_kit_uri_get_token_info (uri); + return (token->serialNumber[0] == 0 && + token->manufacturerID[0] == 0 && + token->label[0] == 0 && + token->model[0] == 0); +} + +static int +are_attributes_empty (P11KitUri *uri) +{ + return (p11_kit_uri_get_attribute (uri, CKA_LABEL) == NULL && + p11_kit_uri_get_attribute (uri, CKA_ID) == NULL && + p11_kit_uri_get_attribute (uri, CKA_CLASS) == NULL); +} + +static void +test_uri_parse (CuTest *tc) +{ + P11KitUri *uri; + int ret; + + uri = p11_kit_uri_new (); + CuAssertPtrNotNull (tc, uri); + + ret = p11_kit_uri_parse ("pkcs11:", P11_KIT_URI_FOR_MODULE, uri); + CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + + CuAssertTrue (tc, is_module_empty (uri)); + CuAssertTrue (tc, is_token_empty (uri)); + CuAssertTrue (tc, are_attributes_empty (uri)); + + p11_kit_uri_free (uri); +} + +static void +test_uri_parse_bad_scheme (CuTest *tc) +{ + P11KitUri *uri; + int ret; + + uri = p11_kit_uri_new (); + CuAssertPtrNotNull (tc, uri); + + ret = p11_kit_uri_parse ("http:\\example.com\test", P11_KIT_URI_FOR_ANY, uri); + CuAssertIntEquals (tc, P11_KIT_URI_BAD_SCHEME, ret); + + p11_kit_uri_free (uri); +} + +static void +test_uri_parse_with_label (CuTest *tc) +{ + CK_ATTRIBUTE_PTR attr; + P11KitUri *uri; + int ret; + + uri = p11_kit_uri_new (); + CuAssertPtrNotNull (tc, uri); + + ret = p11_kit_uri_parse ("pkcs11:object=Test%20Label", P11_KIT_URI_FOR_ANY, uri); + CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + + CuAssertTrue (tc, is_module_empty (uri)); + CuAssertTrue (tc, is_token_empty (uri)); + + attr = p11_kit_uri_get_attribute (uri, CKA_LABEL); + CuAssertPtrNotNull (tc, attr); + CuAssertTrue (tc, attr->ulValueLen == strlen ("Test Label")); + CuAssertTrue (tc, memcmp (attr->pValue, "Test Label", attr->ulValueLen) == 0); + + p11_kit_uri_free (uri); +} + +static void +test_uri_parse_with_label_and_klass (CuTest *tc) +{ + CK_ATTRIBUTE_PTR attr; + P11KitUri *uri; + int ret; + + uri = p11_kit_uri_new (); + CuAssertPtrNotNull (tc, uri); + + ret = p11_kit_uri_parse ("pkcs11:object=Test%20Label;object-type=cert", P11_KIT_URI_FOR_ANY, uri); + CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + + attr = p11_kit_uri_get_attribute (uri, CKA_LABEL); + CuAssertPtrNotNull (tc, attr); + CuAssertTrue (tc, attr->ulValueLen == strlen ("Test Label")); + CuAssertTrue (tc, memcmp (attr->pValue, "Test Label", attr->ulValueLen) == 0); + + attr = p11_kit_uri_get_attribute (uri, CKA_CLASS); + CuAssertPtrNotNull (tc, attr); + CuAssertTrue (tc, attr->ulValueLen == sizeof (CK_OBJECT_CLASS)); + CuAssertTrue (tc, *((CK_OBJECT_CLASS_PTR)attr->pValue) == CKO_CERTIFICATE); + + p11_kit_uri_free (uri); +} + +static void +test_uri_parse_with_id (CuTest *tc) +{ + CK_ATTRIBUTE_PTR attr; + P11KitUri *uri; + int ret; + + uri = p11_kit_uri_new (); + CuAssertPtrNotNull (tc, uri); + + ret = p11_kit_uri_parse ("pkcs11:id=%54%45%53%54%00", P11_KIT_URI_FOR_OBJECT, uri); + CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + + /* Note that there's a NULL in the attribute (end) */ + attr = p11_kit_uri_get_attribute (uri, CKA_ID); + CuAssertPtrNotNull (tc, attr); + CuAssertTrue (tc, attr->ulValueLen == 5); + CuAssertTrue (tc, memcmp (attr->pValue, "TEST", 5) == 0); + + + p11_kit_uri_free (uri); +} + +static void +test_uri_parse_with_bad_string_encoding (CuTest *tc) +{ + P11KitUri *uri; + int ret; + + uri = p11_kit_uri_new (); + CuAssertPtrNotNull (tc, uri); + + ret = p11_kit_uri_parse ("pkcs11:object=Test%", P11_KIT_URI_FOR_OBJECT, uri); + CuAssertIntEquals (tc, P11_KIT_URI_BAD_ENCODING, ret); + + p11_kit_uri_free (uri); +} + +static void +test_uri_parse_with_bad_hex_encoding (CuTest *tc) +{ + P11KitUri *uri; + int ret; + + uri = p11_kit_uri_new (); + CuAssertPtrNotNull (tc, uri); + + ret = p11_kit_uri_parse ("pkcs11:object=T%xxest", P11_KIT_URI_FOR_OBJECT, uri); + CuAssertIntEquals (tc, P11_KIT_URI_BAD_ENCODING, ret); + + p11_kit_uri_free (uri); +} + +static int +is_space_string (CK_UTF8CHAR_PTR string, CK_ULONG size, const char *check) +{ + size_t i, len = strlen (check); + if (len > size) + return 0; + if (memcmp (string, check, len) != 0) + return 0; + for (i = len; i < size; ++i) + if (string[i] != ' ') + return 0; + return 1; +} + +static void +test_uri_parse_with_token (CuTest *tc) +{ + P11KitUri *uri = NULL; + CK_TOKEN_INFO_PTR token; + int ret; + + uri = p11_kit_uri_new (); + CuAssertPtrNotNull (tc, uri); + + ret = p11_kit_uri_parse ("pkcs11:token=Token%20Label;serial=3333;model=Deluxe;manufacturer=Me", + P11_KIT_URI_FOR_TOKEN, uri); + CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + + token = p11_kit_uri_get_token_info (uri); + CuAssertTrue (tc, is_space_string (token->label, sizeof (token->label), "Token Label")); + CuAssertTrue (tc, is_space_string (token->serialNumber, sizeof (token->serialNumber), "3333")); + CuAssertTrue (tc, is_space_string (token->model, sizeof (token->model), "Deluxe")); + CuAssertTrue (tc, is_space_string (token->manufacturerID, sizeof (token->manufacturerID), "Me")); + + p11_kit_uri_free (uri); +} + +static void +test_uri_parse_with_token_bad_encoding (CuTest *tc) +{ + P11KitUri *uri; + int ret; + + uri = p11_kit_uri_new (); + CuAssertPtrNotNull (tc, uri); + + ret = p11_kit_uri_parse ("pkcs11:token=Token%", P11_KIT_URI_FOR_TOKEN, uri); + CuAssertIntEquals (tc, P11_KIT_URI_BAD_ENCODING, ret); + + p11_kit_uri_free (uri); +} + +static void +test_uri_parse_with_bad_syntax (CuTest *tc) +{ + P11KitUri *uri; + int ret; + + uri = p11_kit_uri_new (); + CuAssertPtrNotNull (tc, uri); + + ret = p11_kit_uri_parse ("pkcs11:token", P11_KIT_URI_FOR_ANY, uri); + CuAssertIntEquals (tc, P11_KIT_URI_BAD_SYNTAX, ret); + + p11_kit_uri_free (uri); +} + +static void +test_uri_parse_with_spaces (CuTest *tc) +{ + P11KitUri *uri = NULL; + CK_INFO_PTR info; + int ret; + + uri = p11_kit_uri_new (); + CuAssertPtrNotNull (tc, uri); + + ret = p11_kit_uri_parse ("pkc\ns11: lib rary-desc\rrip \n tion =The%20Library;\n\n\nlibrary-manufacturer=\rMe", + P11_KIT_URI_FOR_MODULE, uri); + CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + + info = p11_kit_uri_get_module_info (uri); + + CuAssertTrue (tc, is_space_string (info->manufacturerID, sizeof (info->manufacturerID), "Me")); + CuAssertTrue (tc, is_space_string (info->libraryDescription, sizeof (info->libraryDescription), "The Library")); + + p11_kit_uri_free (uri); +} + + +static void +test_uri_parse_with_library (CuTest *tc) +{ + P11KitUri *uri = NULL; + CK_INFO_PTR info; + int ret; + + uri = p11_kit_uri_new (); + CuAssertPtrNotNull (tc, uri); + + ret = p11_kit_uri_parse ("pkcs11:library-description=The%20Library;library-manufacturer=Me", + P11_KIT_URI_FOR_MODULE, uri); + CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + + info = p11_kit_uri_get_module_info (uri); + + CuAssertTrue (tc, is_space_string (info->manufacturerID, sizeof (info->manufacturerID), "Me")); + CuAssertTrue (tc, is_space_string (info->libraryDescription, sizeof (info->libraryDescription), "The Library")); + + p11_kit_uri_free (uri); +} + +static void +test_uri_parse_with_library_bad_encoding (CuTest *tc) +{ + P11KitUri *uri; + int ret; + + uri = p11_kit_uri_new (); + CuAssertPtrNotNull (tc, uri); + + ret = p11_kit_uri_parse ("pkcs11:library-description=Library%", P11_KIT_URI_FOR_MODULE, uri); + CuAssertIntEquals (tc, P11_KIT_URI_BAD_ENCODING, ret); + + p11_kit_uri_free (uri); +} + +static void +test_uri_build_empty (CuTest *tc) +{ + P11KitUri *uri; + char *string; + int ret; + + uri = p11_kit_uri_new (); + CuAssertPtrNotNull (tc, uri); + + ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string); + CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + CuAssertStrEquals (tc, "pkcs11:", string); + free (string); + + p11_kit_uri_free (uri); +} + +static void +set_space_string (CK_BYTE_PTR buffer, CK_ULONG length, const char *string) +{ + size_t len = strlen (string); + assert (len <= length); + memset (buffer, ' ', length); + memcpy (buffer, string, len); +} + +static void +test_uri_build_with_token_info (CuTest *tc) +{ + char *string = NULL; + P11KitUri *uri; + P11KitUri *check; + CK_TOKEN_INFO_PTR token; + int ret; + + uri = p11_kit_uri_new (); + CuAssertPtrNotNull (tc, uri); + + token = p11_kit_uri_get_token_info (uri); + set_space_string (token->label, sizeof (token->label), "The Label"); + set_space_string (token->serialNumber, sizeof (token->serialNumber), "44444"); + set_space_string (token->manufacturerID, sizeof (token->manufacturerID), "Me"); + set_space_string (token->model, sizeof (token->model), "Deluxe"); + + ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string); + CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + CuAssertPtrNotNull (tc, string); + + check = p11_kit_uri_new (); + CuAssertPtrNotNull (tc, check); + + ret = p11_kit_uri_parse (string, P11_KIT_URI_FOR_TOKEN, check); + CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + + p11_kit_uri_match_token_info (check, p11_kit_uri_get_token_info (uri)); + + p11_kit_uri_free (uri); + p11_kit_uri_free (check); + + CuAssertTrue (tc, strstr (string, "token=The%20Label") != NULL); + CuAssertTrue (tc, strstr (string, "serial=44444") != NULL); + CuAssertTrue (tc, strstr (string, "manufacturer=Me") != NULL); + CuAssertTrue (tc, strstr (string, "model=Deluxe") != NULL); + + free (string); +} + +static void +test_uri_build_with_token_null_info (CuTest *tc) +{ + char *string = NULL; + P11KitUri *uri; + CK_TOKEN_INFO_PTR token; + int ret; + + uri = p11_kit_uri_new (); + CuAssertPtrNotNull (tc, uri); + + token = p11_kit_uri_get_token_info (uri); + set_space_string (token->label, sizeof (token->label), "The Label"); + + ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string); + CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + + CuAssertTrue (tc, strstr (string, "token=The%20Label") != NULL); + CuAssertTrue (tc, strstr (string, "serial=") == NULL); + + free (string); + p11_kit_uri_free (uri); +} + +static void +test_uri_build_with_token_empty_info (CuTest *tc) +{ + char *string = NULL; + P11KitUri *uri; + CK_TOKEN_INFO_PTR token; + int ret; + + uri = p11_kit_uri_new (); + CuAssertPtrNotNull (tc, uri); + + token = p11_kit_uri_get_token_info (uri); + set_space_string (token->label, sizeof (token->label), ""); + set_space_string (token->serialNumber, sizeof (token->serialNumber), ""); + + ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string); + CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + + CuAssertTrue (tc, strstr (string, "token=") != NULL); + CuAssertTrue (tc, strstr (string, "serial=") != NULL); + + free (string); + p11_kit_uri_free (uri); +} + +static void +test_uri_build_with_attributes (CuTest *tc) +{ + char *string = NULL; + P11KitUri *uri; + P11KitUri *check; + CK_OBJECT_CLASS klass; + CK_ATTRIBUTE_PTR attr; + CK_ATTRIBUTE at; + int ret; + + uri = p11_kit_uri_new (); + CuAssertPtrNotNull (tc, uri); + + at.type = CKA_LABEL; + at.pValue = "The Label"; + at.ulValueLen = 9; + ret = p11_kit_uri_set_attribute (uri, &at); + + at.type = CKA_ID; + at.pValue = "HELLO"; + at.ulValueLen = 5; + ret = p11_kit_uri_set_attribute (uri, &at); + + klass = CKO_DATA; + at.type = CKA_CLASS; + at.pValue = &klass; + at.ulValueLen = sizeof (klass); + ret = p11_kit_uri_set_attribute (uri, &at); + + ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string); + CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + + check = p11_kit_uri_new (); + CuAssertPtrNotNull (tc, check); + + ret = p11_kit_uri_parse (string, P11_KIT_URI_FOR_ANY, check); + CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + + attr = p11_kit_uri_get_attribute (check, CKA_LABEL); + CuAssertPtrNotNull (tc, attr); + CuAssertTrue (tc, attr->ulValueLen == 9); + CuAssertTrue (tc, memcmp (attr->pValue, "The Label", attr->ulValueLen) == 0); + + attr = p11_kit_uri_get_attribute (check, CKA_CLASS); + CuAssertPtrNotNull (tc, attr); + CuAssertTrue (tc, attr->ulValueLen == sizeof (klass)); + CuAssertTrue (tc, *((CK_OBJECT_CLASS_PTR)attr->pValue) == klass); + + attr = p11_kit_uri_get_attribute (check, CKA_ID); + CuAssertPtrNotNull (tc, attr); + CuAssertTrue (tc, attr->ulValueLen == 5); + CuAssertTrue (tc, memcmp (attr->pValue, "HELLO", attr->ulValueLen) == 0); + + p11_kit_uri_free (check); + + CuAssertTrue (tc, strstr (string, "object=The%20Label") != NULL); + CuAssertTrue (tc, strstr (string, "object-type=data") != NULL); + CuAssertTrue (tc, strstr (string, "id=%48%45%4c%4c%4f") != NULL); + + free (string); + p11_kit_uri_free (uri); +} + +static void +test_uri_parse_private_key (CuTest *tc) +{ + P11KitUri *uri; + CK_ATTRIBUTE_PTR attr; + int ret; + + uri = p11_kit_uri_new (); + CuAssertPtrNotNull (tc, uri); + + ret = p11_kit_uri_parse ("pkcs11:object-type=private", P11_KIT_URI_FOR_OBJECT, uri); + CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + + attr = p11_kit_uri_get_attribute (uri, CKA_CLASS); + CuAssertPtrNotNull (tc, attr); + CuAssertTrue (tc, attr->ulValueLen == sizeof (CK_OBJECT_CLASS)); + CuAssertTrue (tc, *((CK_OBJECT_CLASS_PTR)attr->pValue) == CKO_PRIVATE_KEY); + + p11_kit_uri_free (uri); +} + +static void +test_uri_parse_secret_key (CuTest *tc) +{ + P11KitUri *uri; + CK_ATTRIBUTE_PTR attr; + int ret; + + uri = p11_kit_uri_new (); + CuAssertPtrNotNull (tc, uri); + + ret = p11_kit_uri_parse ("pkcs11:object-type=secret-key", P11_KIT_URI_FOR_OBJECT, uri); + CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + + attr = p11_kit_uri_get_attribute (uri, CKA_CLASS); + CuAssertPtrNotNull (tc, attr); + CuAssertTrue (tc, attr->ulValueLen == sizeof (CK_OBJECT_CLASS)); + CuAssertTrue (tc, *((CK_OBJECT_CLASS_PTR)attr->pValue) == CKO_SECRET_KEY); + + p11_kit_uri_free (uri); +} + +static void +test_uri_parse_library_version (CuTest *tc) +{ + P11KitUri *uri; + CK_INFO_PTR info; + int ret; + + uri = p11_kit_uri_new (); + CuAssertPtrNotNull (tc, uri); + + ret = p11_kit_uri_parse ("pkcs11:library-version=2.101", P11_KIT_URI_FOR_MODULE_WITH_VERSION, uri); + CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + + info = p11_kit_uri_get_module_info (uri); + CuAssertIntEquals (tc, 2, info->libraryVersion.major); + CuAssertIntEquals (tc, 101, info->libraryVersion.minor); + + ret = p11_kit_uri_parse ("pkcs11:library-version=23", P11_KIT_URI_FOR_MODULE_WITH_VERSION, uri); + CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + + info = p11_kit_uri_get_module_info (uri); + CuAssertIntEquals (tc, 23, info->libraryVersion.major); + CuAssertIntEquals (tc, 0, info->libraryVersion.minor); + + ret = p11_kit_uri_parse ("pkcs11:library-version=23.", P11_KIT_URI_FOR_MODULE_WITH_VERSION, uri); + CuAssertIntEquals (tc, P11_KIT_URI_BAD_VERSION, ret); + + ret = p11_kit_uri_parse ("pkcs11:library-version=a.a", P11_KIT_URI_FOR_MODULE_WITH_VERSION, uri); + CuAssertIntEquals (tc, P11_KIT_URI_BAD_VERSION, ret); + + ret = p11_kit_uri_parse ("pkcs11:library-version=.23", P11_KIT_URI_FOR_MODULE_WITH_VERSION, uri); + CuAssertIntEquals (tc, P11_KIT_URI_BAD_VERSION, ret); + + ret = p11_kit_uri_parse ("pkcs11:library-version=1000", P11_KIT_URI_FOR_MODULE_WITH_VERSION, uri); + CuAssertIntEquals (tc, P11_KIT_URI_BAD_VERSION, ret); + + ret = p11_kit_uri_parse ("pkcs11:library-version=2.1000", P11_KIT_URI_FOR_MODULE_WITH_VERSION, uri); + CuAssertIntEquals (tc, P11_KIT_URI_BAD_VERSION, ret); + + p11_kit_uri_free (uri); +} + +static void +test_uri_parse_parse_unknown_object_type (CuTest *tc) +{ + P11KitUri *uri; + CK_ATTRIBUTE_PTR attr; + int ret; + + uri = p11_kit_uri_new (); + CuAssertPtrNotNull (tc, uri); + + ret = p11_kit_uri_parse ("pkcs11:object-type=unknown", P11_KIT_URI_FOR_OBJECT, uri); + CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + + attr = p11_kit_uri_get_attribute (uri, CKA_CLASS); + CuAssertPtrEquals (tc, NULL, attr); + + p11_kit_uri_free (uri); +} + +static void +test_uri_parse_unrecognized (CuTest *tc) +{ + P11KitUri *uri; + int ret; + + uri = p11_kit_uri_new (); + CuAssertPtrNotNull (tc, uri); + + ret = p11_kit_uri_parse ("pkcs11:x-blah=some-value", P11_KIT_URI_FOR_ANY, uri); + CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + + ret = p11_kit_uri_any_unrecognized (uri); + CuAssertIntEquals (tc, 1, ret); + + p11_kit_uri_free (uri); +} + +static void +test_uri_parse_too_long_is_unrecognized (CuTest *tc) +{ + P11KitUri *uri; + int ret; + + uri = p11_kit_uri_new (); + CuAssertPtrNotNull (tc, uri); + + ret = p11_kit_uri_parse ("pkcs11:model=a-value-that-is-too-long-for-the-field-that-it-goes-with", + P11_KIT_URI_FOR_ANY, uri); + CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + + ret = p11_kit_uri_any_unrecognized (uri); + CuAssertIntEquals (tc, 1, ret); + + p11_kit_uri_free (uri); +} + + + +static void +test_uri_build_object_type_cert (CuTest *tc) +{ + CK_ATTRIBUTE attr; + CK_OBJECT_CLASS klass; + P11KitUri *uri; + char *string; + int ret; + + uri = p11_kit_uri_new (); + CuAssertPtrNotNull (tc, uri); + + klass = CKO_CERTIFICATE; + attr.type = CKA_CLASS; + attr.pValue = &klass; + attr.ulValueLen = sizeof (klass); + p11_kit_uri_set_attribute (uri, &attr); + + ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string); + CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + CuAssertTrue (tc, strstr (string, "object-type=cert") != NULL); + + p11_kit_uri_free (uri); + free (string); +} + +static void +test_uri_build_object_type_private (CuTest *tc) +{ + CK_ATTRIBUTE attr; + CK_OBJECT_CLASS klass; + P11KitUri *uri; + char *string; + int ret; + + uri = p11_kit_uri_new (); + CuAssertPtrNotNull (tc, uri); + + klass = CKO_PRIVATE_KEY; + attr.type = CKA_CLASS; + attr.pValue = &klass; + attr.ulValueLen = sizeof (klass); + p11_kit_uri_set_attribute (uri, &attr); + + ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string); + CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + CuAssertTrue (tc, strstr (string, "object-type=private") != NULL); + + p11_kit_uri_free (uri); + free (string); +} + +static void +test_uri_build_object_type_public (CuTest *tc) +{ + CK_ATTRIBUTE attr; + CK_OBJECT_CLASS klass; + P11KitUri *uri; + char *string; + int ret; + + uri = p11_kit_uri_new (); + CuAssertPtrNotNull (tc, uri); + + klass = CKO_PUBLIC_KEY; + attr.type = CKA_CLASS; + attr.pValue = &klass; + attr.ulValueLen = sizeof (klass); + p11_kit_uri_set_attribute (uri, &attr); + + ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string); + CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + CuAssertTrue (tc, strstr (string, "object-type=public") != NULL); + + p11_kit_uri_free (uri); + free (string); +} + +static void +test_uri_build_object_type_secret (CuTest *tc) +{ + CK_ATTRIBUTE attr; + CK_OBJECT_CLASS klass; + P11KitUri *uri; + char *string; + int ret; + + uri = p11_kit_uri_new (); + CuAssertPtrNotNull (tc, uri); + + klass = CKO_SECRET_KEY; + attr.type = CKA_CLASS; + attr.pValue = &klass; + attr.ulValueLen = sizeof (klass); + p11_kit_uri_set_attribute (uri, &attr); + + ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string); + CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + CuAssertTrue (tc, strstr (string, "object-type=secret-key") != NULL); + + p11_kit_uri_free (uri); + free (string); +} + +static void +test_uri_build_with_library (CuTest *tc) +{ + CK_INFO_PTR info; + P11KitUri *uri; + char *string; + int ret; + + uri = p11_kit_uri_new (); + CuAssertPtrNotNull (tc, uri); + + info = p11_kit_uri_get_module_info (uri); + set_space_string (info->libraryDescription, sizeof (info->libraryDescription), "The Description"); + + ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string); + CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + CuAssertTrue (tc, strstr (string, "library-description=The%20Description") != NULL); + + p11_kit_uri_free (uri); + free (string); +} + +static void +test_uri_build_library_version (CuTest *tc) +{ + CK_INFO_PTR info; + P11KitUri *uri; + char *string; + int ret; + + uri = p11_kit_uri_new (); + CuAssertPtrNotNull (tc, uri); + + info = p11_kit_uri_get_module_info (uri); + info->libraryVersion.major = 2; + info->libraryVersion.minor = 10; + + ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string); + CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + CuAssertTrue (tc, strstr (string, "library-version=2.10") != NULL); + + p11_kit_uri_free (uri); + free (string); +} + +static void +test_uri_get_set_unrecognized (CuTest *tc) +{ + P11KitUri *uri; + int ret; + + uri = p11_kit_uri_new (); + CuAssertPtrNotNull (tc, uri); + + ret = p11_kit_uri_any_unrecognized (uri); + CuAssertIntEquals (tc, 0, ret); + + p11_kit_uri_set_unrecognized (uri, 1); + + ret = p11_kit_uri_any_unrecognized (uri); + CuAssertIntEquals (tc, 1, ret); + + p11_kit_uri_set_unrecognized (uri, 0); + + ret = p11_kit_uri_any_unrecognized (uri); + CuAssertIntEquals (tc, 0, ret); + + p11_kit_uri_free (uri); +} + +static void +test_uri_match_token (CuTest *tc) +{ + CK_TOKEN_INFO token; + P11KitUri *uri; + int ret; + + uri = p11_kit_uri_new (); + CuAssertPtrNotNull (tc, uri); + + ret = p11_kit_uri_parse ("pkcs11:model=Giselle", P11_KIT_URI_FOR_ANY, uri); + CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + + set_space_string (token.label, sizeof (token.label), "A label"); + set_space_string (token.model, sizeof (token.model), "Giselle"); + + ret = p11_kit_uri_match_token_info (uri, &token); + CuAssertIntEquals (tc, 1, ret); + + set_space_string (token.label, sizeof (token.label), "Another label"); + + ret = p11_kit_uri_match_token_info (uri, &token); + CuAssertIntEquals (tc, 1, ret); + + set_space_string (token.model, sizeof (token.model), "Zoolander"); + + ret = p11_kit_uri_match_token_info (uri, &token); + CuAssertIntEquals (tc, 0, ret); + + p11_kit_uri_set_unrecognized (uri, 1); + + ret = p11_kit_uri_match_token_info (uri, &token); + CuAssertIntEquals (tc, 0, ret); + + p11_kit_uri_free (uri); +} + +static void +test_uri_match_module (CuTest *tc) +{ + CK_INFO info; + P11KitUri *uri; + int ret; + + uri = p11_kit_uri_new (); + CuAssertPtrNotNull (tc, uri); + + ret = p11_kit_uri_parse ("pkcs11:library-description=Quiet", P11_KIT_URI_FOR_ANY, uri); + CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + + set_space_string (info.libraryDescription, sizeof (info.libraryDescription), "Quiet"); + set_space_string (info.manufacturerID, sizeof (info.manufacturerID), "Someone"); + + ret = p11_kit_uri_match_module_info (uri, &info); + CuAssertIntEquals (tc, 1, ret); + + set_space_string (info.manufacturerID, sizeof (info.manufacturerID), "Someone else"); + + ret = p11_kit_uri_match_module_info (uri, &info); + CuAssertIntEquals (tc, 1, ret); + + set_space_string (info.libraryDescription, sizeof (info.libraryDescription), "Leise"); + + ret = p11_kit_uri_match_module_info (uri, &info); + CuAssertIntEquals (tc, 0, ret); + + p11_kit_uri_set_unrecognized (uri, 1); + + ret = p11_kit_uri_match_module_info (uri, &info); + CuAssertIntEquals (tc, 0, ret); + + p11_kit_uri_free (uri); +} + +static void +test_uri_match_attributes (CuTest *tc) +{ + CK_ATTRIBUTE attrs[4]; + CK_OBJECT_CLASS klass; + P11KitUri *uri; + int ret; + + attrs[0].type = CKA_ID; + attrs[0].pValue = "Blah"; + attrs[0].ulValueLen = 4; + + attrs[1].type = CKA_LABEL; + attrs[1].pValue = "Junk"; + attrs[1].ulValueLen = 4; + + attrs[2].type = CKA_COLOR; + attrs[2].pValue = "blue"; + attrs[2].ulValueLen = 4; + + klass = CKO_DATA; + attrs[3].type = CKA_CLASS; + attrs[3].pValue = &klass; + attrs[3].ulValueLen = sizeof (klass); + + uri = p11_kit_uri_new (); + CuAssertPtrNotNull (tc, uri); + + ret = p11_kit_uri_parse ("pkcs11:object=Fancy;id=Blah;object-type=data", P11_KIT_URI_FOR_ANY, uri); + CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + + ret = p11_kit_uri_match_attributes (uri, attrs, 4); + CuAssertIntEquals (tc, 0, ret); + + attrs[1].pValue = "Fancy"; + attrs[1].ulValueLen = 5; + + ret = p11_kit_uri_match_attributes (uri, attrs, 4); + CuAssertIntEquals (tc, 1, ret); + + p11_kit_uri_clear_attribute (uri, CKA_CLASS); + + ret = p11_kit_uri_match_attributes (uri, attrs, 4); + CuAssertIntEquals (tc, 1, ret); + + attrs[2].pValue = "pink"; + + ret = p11_kit_uri_match_attributes (uri, attrs, 4); + CuAssertIntEquals (tc, 1, ret); + + p11_kit_uri_set_unrecognized (uri, 1); + + ret = p11_kit_uri_match_attributes (uri, attrs, 4); + CuAssertIntEquals (tc, 0, ret); + + p11_kit_uri_free (uri); +} + +static void +test_uri_get_set_attribute (CuTest *tc) +{ + CK_ATTRIBUTE attr; + CK_ATTRIBUTE_PTR ptr; + P11KitUri *uri; + int ret; + + uri = p11_kit_uri_new (); + CuAssertPtrNotNull (tc, uri); + + ptr = p11_kit_uri_get_attribute (uri, CKA_LABEL); + CuAssertPtrEquals (tc, NULL, ptr); + + ret = p11_kit_uri_clear_attribute (uri, CKA_LABEL); + CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + + ret = p11_kit_uri_clear_attribute (uri, CKA_COLOR); + CuAssertIntEquals (tc, P11_KIT_URI_NOT_FOUND, ret); + + attr.type = CKA_LABEL; + attr.pValue = "Test"; + attr.ulValueLen = 4; + + ret = p11_kit_uri_set_attribute (uri, &attr); + CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + + attr.type = CKA_COLOR; + ret = p11_kit_uri_set_attribute (uri, &attr); + CuAssertIntEquals (tc, P11_KIT_URI_NOT_FOUND, ret); + + ptr = p11_kit_uri_get_attribute (uri, CKA_COLOR); + CuAssertPtrEquals (tc, NULL, ptr); + + ptr = p11_kit_uri_get_attribute (uri, CKA_LABEL); + CuAssertPtrNotNull (tc, ptr); + + CuAssertTrue (tc, ptr->type == CKA_LABEL); + CuAssertTrue (tc, ptr->ulValueLen == 4); + CuAssertTrue (tc, memcmp (ptr->pValue, "Test", 4) == 0); + + ret = p11_kit_uri_clear_attribute (uri, CKA_LABEL); + CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + + ptr = p11_kit_uri_get_attribute (uri, CKA_LABEL); + CuAssertPtrEquals (tc, NULL, ptr); + + p11_kit_uri_free (uri); +} + +static void +test_uri_get_set_attributes (CuTest *tc) +{ + CK_ATTRIBUTE_PTR attrs; + CK_OBJECT_CLASS klass; + CK_ATTRIBUTE attr; + CK_ULONG n_attrs; + P11KitUri *uri; + int ret; + + uri = p11_kit_uri_new (); + CuAssertPtrNotNull (tc, uri); + + attrs = p11_kit_uri_get_attributes (uri, &n_attrs); + CuAssertPtrNotNull (tc, attrs); + CuAssertIntEquals (tc, 0, n_attrs); + + attr.type = CKA_LABEL; + attr.pValue = "Test"; + attr.ulValueLen = 4; + + ret = p11_kit_uri_set_attribute (uri, &attr); + CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + + attrs = p11_kit_uri_get_attributes (uri, &n_attrs); + CuAssertPtrNotNull (tc, attrs); + CuAssertIntEquals (tc, 1, n_attrs); + CuAssertTrue (tc, attrs[0].type == CKA_LABEL); + CuAssertTrue (tc, attrs[0].ulValueLen == 4); + CuAssertTrue (tc, memcmp (attrs[0].pValue, "Test", 4) == 0); + + attr.type = CKA_LABEL; + attr.pValue = "Kablooey"; + attr.ulValueLen = 8; + + ret = p11_kit_uri_set_attribute (uri, &attr); + CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + + attrs = p11_kit_uri_get_attributes (uri, &n_attrs); + CuAssertPtrNotNull (tc, attrs); + CuAssertIntEquals (tc, 1, n_attrs); + CuAssertTrue (tc, attrs[0].type == CKA_LABEL); + CuAssertTrue (tc, attrs[0].ulValueLen == 8); + CuAssertTrue (tc, memcmp (attrs[0].pValue, "Kablooey", 8) == 0); + + klass = CKO_DATA; + attr.type = CKA_CLASS; + attr.pValue = &klass; + attr.ulValueLen = sizeof (klass); + + ret = p11_kit_uri_set_attribute (uri, &attr); + CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + + attrs = p11_kit_uri_get_attributes (uri, &n_attrs); + CuAssertPtrNotNull (tc, attrs); + CuAssertIntEquals (tc, 2, n_attrs); + CuAssertTrue (tc, attrs[0].type == CKA_LABEL); + CuAssertTrue (tc, attrs[0].ulValueLen == 8); + CuAssertTrue (tc, memcmp (attrs[0].pValue, "Kablooey", 8) == 0); + CuAssertTrue (tc, attrs[1].type == CKA_CLASS); + CuAssertTrue (tc, attrs[1].ulValueLen == sizeof (klass)); + CuAssertTrue (tc, memcmp (attrs[1].pValue, &klass, sizeof (klass)) == 0); + + ret = p11_kit_uri_clear_attribute (uri, CKA_LABEL); + CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + + attrs = p11_kit_uri_get_attributes (uri, &n_attrs); + CuAssertPtrNotNull (tc, attrs); + CuAssertIntEquals (tc, 1, n_attrs); + CuAssertTrue (tc, attrs[0].type == CKA_CLASS); + CuAssertTrue (tc, attrs[0].ulValueLen == sizeof (klass)); + CuAssertTrue (tc, memcmp (attrs[0].pValue, &klass, sizeof (klass)) == 0); + + attr.type = CKA_LABEL; + attr.pValue = "Three"; + attr.ulValueLen = 5; + + ret = p11_kit_uri_set_attributes (uri, &attr, 1); + CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + + attrs = p11_kit_uri_get_attributes (uri, &n_attrs); + CuAssertPtrNotNull (tc, attrs); + CuAssertIntEquals (tc, 1, n_attrs); + CuAssertTrue (tc, attrs[0].type == CKA_LABEL); + CuAssertTrue (tc, attrs[0].ulValueLen == 5); + CuAssertTrue (tc, memcmp (attrs[0].pValue, "Three", 5) == 0); + + p11_kit_uri_clear_attributes (uri); + + attrs = p11_kit_uri_get_attributes (uri, &n_attrs); + CuAssertPtrNotNull (tc, attrs); + CuAssertIntEquals (tc, 0, n_attrs); + + p11_kit_uri_free (uri); +} +static void +test_uri_pin_source (CuTest *tc) +{ + P11KitUri *uri; + const char *pin_source; + char *string; + int ret; + + uri = p11_kit_uri_new (); + CuAssertPtrNotNull (tc, uri); + + p11_kit_uri_set_pin_source (uri, "|my-pin-source"); + + pin_source = p11_kit_uri_get_pin_source (uri); + CuAssertStrEquals (tc, "|my-pin-source", pin_source); + + pin_source = p11_kit_uri_get_pinfile (uri); + CuAssertStrEquals (tc, "|my-pin-source", pin_source); + + p11_kit_uri_set_pinfile (uri, "|my-pin-file"); + + pin_source = p11_kit_uri_get_pin_source (uri); + CuAssertStrEquals (tc, "|my-pin-file", pin_source); + + ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string); + CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + CuAssertTrue (tc, strstr (string, "pin-source=%7cmy-pin-file") != NULL); + free (string); + + ret = p11_kit_uri_parse ("pkcs11:pin-source=blah%2Fblah", P11_KIT_URI_FOR_ANY, uri); + CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + + pin_source = p11_kit_uri_get_pin_source (uri); + CuAssertStrEquals (tc, "blah/blah", pin_source); + + p11_kit_uri_free (uri); +} + +static void +test_uri_free_null (CuTest *tc) +{ + p11_kit_uri_free (NULL); +} + +static void +test_uri_message (CuTest *tc) +{ + CuAssertTrue (tc, p11_kit_uri_message (P11_KIT_URI_OK) == NULL); + CuAssertPtrNotNull (tc, p11_kit_uri_message (P11_KIT_URI_UNEXPECTED)); + CuAssertPtrNotNull (tc, p11_kit_uri_message (-555555)); +} + +int +main (void) +{ + CuString *output = CuStringNew (); + CuSuite* suite = CuSuiteNew (); + int ret; + + p11_library_init (); + + SUITE_ADD_TEST (suite, test_uri_parse); + SUITE_ADD_TEST (suite, test_uri_parse_bad_scheme); + SUITE_ADD_TEST (suite, test_uri_parse_with_label); + SUITE_ADD_TEST (suite, test_uri_parse_with_label_and_klass); + SUITE_ADD_TEST (suite, test_uri_parse_with_id); + SUITE_ADD_TEST (suite, test_uri_parse_with_bad_string_encoding); + SUITE_ADD_TEST (suite, test_uri_parse_with_bad_hex_encoding); + SUITE_ADD_TEST (suite, test_uri_parse_with_token); + SUITE_ADD_TEST (suite, test_uri_parse_with_token_bad_encoding); + SUITE_ADD_TEST (suite, test_uri_parse_with_bad_syntax); + SUITE_ADD_TEST (suite, test_uri_parse_with_spaces); + SUITE_ADD_TEST (suite, test_uri_parse_with_library); + SUITE_ADD_TEST (suite, test_uri_parse_with_library_bad_encoding); + SUITE_ADD_TEST (suite, test_uri_build_empty); + SUITE_ADD_TEST (suite, test_uri_build_with_token_info); + SUITE_ADD_TEST (suite, test_uri_build_with_token_null_info); + SUITE_ADD_TEST (suite, test_uri_build_with_token_empty_info); + SUITE_ADD_TEST (suite, test_uri_build_with_attributes); + SUITE_ADD_TEST (suite, test_uri_parse_private_key); + SUITE_ADD_TEST (suite, test_uri_parse_secret_key); + SUITE_ADD_TEST (suite, test_uri_parse_library_version); + SUITE_ADD_TEST (suite, test_uri_parse_parse_unknown_object_type); + SUITE_ADD_TEST (suite, test_uri_parse_unrecognized); + SUITE_ADD_TEST (suite, test_uri_parse_too_long_is_unrecognized); + SUITE_ADD_TEST (suite, test_uri_build_object_type_cert); + SUITE_ADD_TEST (suite, test_uri_build_object_type_private); + SUITE_ADD_TEST (suite, test_uri_build_object_type_public); + SUITE_ADD_TEST (suite, test_uri_build_object_type_secret); + SUITE_ADD_TEST (suite, test_uri_build_with_library); + SUITE_ADD_TEST (suite, test_uri_build_library_version); + SUITE_ADD_TEST (suite, test_uri_get_set_unrecognized); + SUITE_ADD_TEST (suite, test_uri_match_token); + SUITE_ADD_TEST (suite, test_uri_match_module); + SUITE_ADD_TEST (suite, test_uri_match_attributes); + SUITE_ADD_TEST (suite, test_uri_get_set_attribute); + SUITE_ADD_TEST (suite, test_uri_get_set_attributes); + SUITE_ADD_TEST (suite, test_uri_pin_source); + SUITE_ADD_TEST (suite, test_uri_free_null); + SUITE_ADD_TEST (suite, test_uri_message); + + CuSuiteRun (suite); + CuSuiteSummary (suite, output); + CuSuiteDetails (suite, output); + printf ("%s\n", output->buffer); + ret = suite->failCount; + CuSuiteDelete (suite); + CuStringDelete (output); + return ret; +} 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 #include @@ -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 #include @@ -50,18 +50,6 @@ #include #include -/* - * 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 - */ - -#ifndef __UTIL_H__ -#define __UTIL_H__ - -#include "config.h" - -#include - -/* ----------------------------------------------------------------------------- - * 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 - -/* 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 -#include -#include - -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/Makefile.am b/tests/Makefile.am deleted file mode 100644 index c25ed57..0000000 --- a/tests/Makefile.am +++ /dev/null @@ -1,76 +0,0 @@ - -INCLUDES = \ - -I$(top_srcdir) \ - -I$(top_srcdir)/common \ - -I$(top_srcdir)/p11-kit \ - -I$(srcdir)/cutest \ - -DSRCDIR=\"$(abs_srcdir)\" \ - -DBUILDDIR=\"$(abs_builddir)\" \ - -DP11_KIT_FUTURE_UNSTABLE_API - -LDADD = \ - $(top_builddir)/p11-kit/libp11-kit-testable.la \ - $(LTLIBINTL) - -CHECK_PROGS = \ - hash-test \ - ptr-array-test \ - progname-test \ - conf-test \ - uri-test \ - pin-test \ - test-init \ - test-modules - -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) \ - 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)) - -noinst_LTLIBRARIES = \ - mock-one.la \ - mock-two.la \ - mock-three.la \ - mock-four.la - -mock_one_la_SOURCES = \ - mock-module.c mock-module.h \ - mock-module-ep.c - -mock_one_la_LIBADD = \ - $(top_builddir)/p11-kit/libp11-kit-compat.la - -mock_one_la_LDFLAGS = \ - -module -avoid-version -rpath /nowhere \ - -no-undefined -export-symbols-regex 'C_GetFunctionList' - -mock_two_la_SOURCES = $(mock_one_la_SOURCES) -mock_two_la_LDFLAGS = $(mock_one_la_LDFLAGS) -mock_two_la_LIBADD = $(mock_one_la_LIBADD) - -mock_three_la_SOURCES = $(mock_one_la_SOURCES) -mock_three_la_LDFLAGS = $(mock_one_la_LDFLAGS) -mock_three_la_LIBADD = $(mock_one_la_LIBADD) - -mock_four_la_SOURCES = $(mock_one_la_SOURCES) -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/tests/conf-test.c deleted file mode 100644 index 98841a2..0000000 --- a/tests/conf-test.c +++ /dev/null @@ -1,414 +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 - */ - -#include "config.h" -#include "CuTest.h" - -#include -#include -#include -#include - -#include "conf.h" -#include "p11-kit.h" -#include "private.h" - -static void -test_parse_conf_1 (CuTest *tc) -{ - hashmap *map; - const char *value; - - map = _p11_conf_parse_file (SRCDIR "/files/test-1.conf", 0); - CuAssertPtrNotNull (tc, map); - - value = _p11_hash_get (map, "key1"); - CuAssertStrEquals (tc, "value1", value); - - value = _p11_hash_get (map, "with-colon"); - CuAssertStrEquals (tc, "value-of-colon", value); - - value = _p11_hash_get (map, "with-whitespace"); - CuAssertStrEquals (tc, "value-with-whitespace", value); - - value = _p11_hash_get (map, "embedded-comment"); - CuAssertStrEquals (tc, "this is # not a comment", value); - - _p11_hash_free (map); -} - -static void -test_parse_ignore_missing (CuTest *tc) -{ - hashmap *map; - - map = _p11_conf_parse_file (SRCDIR "/files/non-existant.conf", CONF_IGNORE_MISSING); - CuAssertPtrNotNull (tc, map); - - CuAssertIntEquals (tc, 0, _p11_hash_size (map)); - CuAssertPtrEquals (tc, NULL, (void*)p11_kit_message ()); - _p11_hash_free (map); -} - -static void -test_parse_fail_missing (CuTest *tc) -{ - hashmap *map; - - map = _p11_conf_parse_file (SRCDIR "/files/non-existant.conf", 0); - CuAssertPtrEquals (tc, map, NULL); - CuAssertPtrNotNull (tc, p11_kit_message ()); -} - -static void -test_merge_defaults (CuTest *tc) -{ - hashmap *values; - hashmap *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); - - _p11_hash_set (values, strdup ("one"), strdup ("real1")); - _p11_hash_set (values, strdup ("two"), strdup ("real2")); - - _p11_hash_set (defaults, strdup ("two"), strdup ("default2")); - _p11_hash_set (defaults, strdup ("three"), strdup ("default3")); - - if (_p11_conf_merge_defaults (values, defaults) < 0) - CuFail (tc, "should not be reached"); - - _p11_hash_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"); - - _p11_hash_free (values); -} - -static void -test_load_globals_merge (CuTest *tc) -{ - int user_mode = -1; - hashmap *config; - - _p11_kit_clear_message (); - - config = _p11_conf_load_globals (SRCDIR "/files/test-system-merge.conf", - SRCDIR "/files/test-user.conf", - &user_mode); - CuAssertPtrNotNull (tc, config); - CuAssertStrEquals (tc, NULL, p11_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"); - - _p11_hash_free (config); -} - -static void -test_load_globals_no_user (CuTest *tc) -{ - int user_mode = -1; - hashmap *config; - - _p11_kit_clear_message (); - - config = _p11_conf_load_globals (SRCDIR "/files/test-system-none.conf", - SRCDIR "/files/test-user.conf", - &user_mode); - CuAssertPtrNotNull (tc, config); - CuAssertStrEquals (tc, NULL, p11_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"); - - _p11_hash_free (config); -} - -static void -test_load_globals_user_sets_only (CuTest *tc) -{ - int user_mode = -1; - hashmap *config; - - _p11_kit_clear_message (); - - config = _p11_conf_load_globals (SRCDIR "/files/test-system-merge.conf", - SRCDIR "/files/test-user-only.conf", - &user_mode); - CuAssertPtrNotNull (tc, config); - CuAssertStrEquals (tc, NULL, p11_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"); - - _p11_hash_free (config); -} - -static void -test_load_globals_system_sets_only (CuTest *tc) -{ - int user_mode = -1; - hashmap *config; - - _p11_kit_clear_message (); - - config = _p11_conf_load_globals (SRCDIR "/files/test-system-only.conf", - SRCDIR "/files/test-user.conf", - &user_mode); - CuAssertPtrNotNull (tc, config); - CuAssertStrEquals (tc, NULL, p11_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"); - - _p11_hash_free (config); -} - -static void -test_load_globals_system_sets_invalid (CuTest *tc) -{ - int user_mode = -1; - hashmap *config; - int error; - - _p11_kit_clear_message (); - - config = _p11_conf_load_globals (SRCDIR "/files/test-system-invalid.conf", - SRCDIR "/files/non-existant.conf", - &user_mode); - error = errno; - CuAssertPtrEquals (tc, NULL, config); - CuAssertIntEquals (tc, EINVAL, error); - CuAssertPtrNotNull (tc, p11_kit_message ()); - - _p11_hash_free (config); -} - -static void -test_load_globals_user_sets_invalid (CuTest *tc) -{ - int user_mode = -1; - hashmap *config; - int error; - - _p11_kit_clear_message (); - - config = _p11_conf_load_globals (SRCDIR "/files/test-system-merge.conf", - SRCDIR "/files/test-user-invalid.conf", - &user_mode); - error = errno; - CuAssertPtrEquals (tc, NULL, config); - CuAssertIntEquals (tc, EINVAL, error); - CuAssertPtrNotNull (tc, p11_kit_message ()); - - _p11_hash_free (config); -} - -static int -assert_msg_contains (const char *msg, - const char *text) -{ - if (msg == NULL) - return 0; - return strstr (msg, text) ? 1 : 0; -} - -static void -test_load_modules_merge (CuTest *tc) -{ - hashmap *configs; - hashmap *config; - - _p11_kit_clear_message (); - - configs = _p11_conf_load_modules (CONF_USER_MERGE, - SRCDIR "/files/system-modules", - SRCDIR "/files/user-modules"); - CuAssertPtrNotNull (tc, configs); - CuAssertTrue (tc, assert_msg_contains (p11_kit_message (), "invalid config filename")); - - config = _p11_hash_get (configs, "one"); - CuAssertPtrNotNull (tc, config); - CuAssertStrEquals (tc, "mock-one.so", _p11_hash_get (config, "module")); - CuAssertStrEquals (tc, _p11_hash_get (config, "setting"), "user1"); - - config = _p11_hash_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"); - - config = _p11_hash_get (configs, "three"); - CuAssertPtrNotNull (tc, config); - CuAssertStrEquals (tc, "mock-three.so", _p11_hash_get (config, "module")); - CuAssertStrEquals (tc, _p11_hash_get (config, "setting"), "user3"); - - _p11_hash_free (configs); -} - -static void -test_load_modules_user_none (CuTest *tc) -{ - hashmap *configs; - hashmap *config; - - _p11_kit_clear_message (); - - configs = _p11_conf_load_modules (CONF_USER_NONE, - SRCDIR "/files/system-modules", - SRCDIR "/files/user-modules"); - CuAssertPtrNotNull (tc, configs); - CuAssertTrue (tc, assert_msg_contains (p11_kit_message (), "invalid config filename")); - - config = _p11_hash_get (configs, "one"); - CuAssertPtrNotNull (tc, config); - CuAssertStrEquals (tc, "mock-one.so", _p11_hash_get (config, "module")); - CuAssertStrEquals (tc, _p11_hash_get (config, "setting"), "system1"); - - config = _p11_hash_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"); - - config = _p11_hash_get (configs, "three"); - CuAssertPtrEquals (tc, NULL, config); - - _p11_hash_free (configs); -} - -static void -test_load_modules_user_only (CuTest *tc) -{ - hashmap *configs; - hashmap *config; - - _p11_kit_clear_message (); - - configs = _p11_conf_load_modules (CONF_USER_ONLY, - SRCDIR "/files/system-modules", - SRCDIR "/files/user-modules"); - CuAssertPtrNotNull (tc, configs); - CuAssertPtrEquals (tc, NULL, (void *)p11_kit_message ()); - - config = _p11_hash_get (configs, "one"); - CuAssertPtrNotNull (tc, config); - CuAssertStrEquals (tc, _p11_hash_get (config, "module"), NULL); - CuAssertStrEquals (tc, _p11_hash_get (config, "setting"), "user1"); - - config = _p11_hash_get (configs, "two.badname"); - CuAssertPtrEquals (tc, NULL, config); - - config = _p11_hash_get (configs, "three"); - CuAssertPtrNotNull (tc, config); - CuAssertStrEquals (tc, "mock-three.so", _p11_hash_get (config, "module")); - CuAssertStrEquals (tc, _p11_hash_get (config, "setting"), "user3"); - - _p11_hash_free (configs); -} - -static void -test_load_modules_no_user (CuTest *tc) -{ - hashmap *configs; - hashmap *config; - - _p11_kit_clear_message (); - - configs = _p11_conf_load_modules (CONF_USER_MERGE, - SRCDIR "/files/system-modules", - SRCDIR "/files/non-existant"); - CuAssertPtrNotNull (tc, configs); - CuAssertTrue (tc, assert_msg_contains (p11_kit_message (), "invalid config filename")); - - config = _p11_hash_get (configs, "one"); - CuAssertPtrNotNull (tc, config); - CuAssertStrEquals (tc, "mock-one.so", _p11_hash_get (config, "module")); - CuAssertStrEquals (tc, _p11_hash_get (config, "setting"), "system1"); - - config = _p11_hash_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"); - - config = _p11_hash_get (configs, "three"); - CuAssertPtrEquals (tc, NULL, config); - - _p11_hash_free (configs); -} - -int -main (void) -{ - CuString *output = CuStringNew (); - CuSuite* suite = CuSuiteNew (); - int ret; - - _p11_library_init (); - - SUITE_ADD_TEST (suite, test_parse_conf_1); - SUITE_ADD_TEST (suite, test_parse_ignore_missing); - SUITE_ADD_TEST (suite, test_parse_fail_missing); - SUITE_ADD_TEST (suite, test_merge_defaults); - SUITE_ADD_TEST (suite, test_load_globals_merge); - SUITE_ADD_TEST (suite, test_load_globals_no_user); - SUITE_ADD_TEST (suite, test_load_globals_system_sets_only); - SUITE_ADD_TEST (suite, test_load_globals_user_sets_only); - SUITE_ADD_TEST (suite, test_load_globals_system_sets_invalid); - SUITE_ADD_TEST (suite, test_load_globals_user_sets_invalid); - SUITE_ADD_TEST (suite, test_load_modules_merge); - SUITE_ADD_TEST (suite, test_load_modules_no_user); - SUITE_ADD_TEST (suite, test_load_modules_user_only); - SUITE_ADD_TEST (suite, test_load_modules_user_none); - - p11_kit_be_quiet (); - - CuSuiteRun (suite); - CuSuiteSummary (suite, output); - CuSuiteDetails (suite, output); - printf ("%s\n", output->buffer); - ret = suite->failCount; - CuSuiteDelete (suite); - CuStringDelete (output); - return ret; -} diff --git a/tests/cutest/CuTest.c b/tests/cutest/CuTest.c deleted file mode 100644 index b033483..0000000 --- a/tests/cutest/CuTest.c +++ /dev/null @@ -1,329 +0,0 @@ -#include -#include -#include -#include -#include -#include - -#include "CuTest.h" - -/*-------------------------------------------------------------------------* - * CuStr - *-------------------------------------------------------------------------*/ - -char* CuStrAlloc(int size) -{ - char* newStr = (char*) malloc( sizeof(char) * (size) ); - return newStr; -} - -char* CuStrCopy(const char* old) -{ - int len = strlen(old); - char* newStr = CuStrAlloc(len + 1); - strcpy(newStr, old); - return newStr; -} - -/*-------------------------------------------------------------------------* - * CuString - *-------------------------------------------------------------------------*/ - -void CuStringInit(CuString* str) -{ - str->length = 0; - str->size = STRING_MAX; - str->buffer = (char*) malloc(sizeof(char) * str->size); - str->buffer[0] = '\0'; -} - -CuString* CuStringNew(void) -{ - CuString* str = (CuString*) malloc(sizeof(CuString)); - str->length = 0; - str->size = STRING_MAX; - str->buffer = (char*) malloc(sizeof(char) * str->size); - str->buffer[0] = '\0'; - return str; -} - -void CuStringDelete(CuString *str) -{ - if (!str) return; - free(str->buffer); - free(str); -} - -void CuStringResize(CuString* str, int newSize) -{ - str->buffer = (char*) realloc(str->buffer, sizeof(char) * newSize); - str->size = newSize; -} - -void CuStringAppend(CuString* str, const char* text) -{ - int length; - - if (text == NULL) { - text = "NULL"; - } - - length = strlen(text); - if (str->length + length + 1 >= str->size) - CuStringResize(str, str->length + length + 1 + STRING_INC); - str->length += length; - strcat(str->buffer, text); -} - -void CuStringAppendChar(CuString* str, char ch) -{ - char text[2]; - text[0] = ch; - text[1] = '\0'; - CuStringAppend(str, text); -} - -void CuStringAppendFormat(CuString* str, const char* format, ...) -{ - va_list argp; - char buf[HUGE_STRING_LEN]; - va_start(argp, format); - vsprintf(buf, format, argp); - va_end(argp); - CuStringAppend(str, buf); -} - -void CuStringInsert(CuString* str, const char* text, int pos) -{ - int length = strlen(text); - if (pos > str->length) - pos = str->length; - if (str->length + length + 1 >= str->size) - CuStringResize(str, str->length + length + 1 + STRING_INC); - memmove(str->buffer + pos + length, str->buffer + pos, (str->length - pos) + 1); - str->length += length; - memcpy(str->buffer + pos, text, length); -} - -/*-------------------------------------------------------------------------* - * CuTest - *-------------------------------------------------------------------------*/ - -void CuTestInit(CuTest* t, const char* name, TestFunction function) -{ - t->name = CuStrCopy(name); - t->failed = 0; - t->ran = 0; - t->message = NULL; - t->function = function; - t->jumpBuf = NULL; -} - -CuTest* CuTestNew(const char* name, TestFunction function) -{ - CuTest* tc = CU_ALLOC(CuTest); - CuTestInit(tc, name, function); - return tc; -} - -void CuTestDelete(CuTest *t) -{ - if (!t) return; - free(t->name); - free(t); -} - -void CuTestRun(CuTest* tc) -{ - jmp_buf buf; - tc->jumpBuf = &buf; - if (setjmp(buf) == 0) - { - tc->ran = 1; - (tc->function)(tc); - } - tc->jumpBuf = 0; -} - -static void CuFailInternal(CuTest* tc, const char* file, int line, CuString* string) -{ - char buf[HUGE_STRING_LEN]; - - sprintf(buf, "%s:%d: ", file, line); - CuStringInsert(string, buf, 0); - - tc->failed = 1; - tc->message = string->buffer; - if (tc->jumpBuf != 0) longjmp(*(tc->jumpBuf), 0); -} - -void CuFail_Line(CuTest* tc, const char* file, int line, const char* message2, const char* message) -{ - CuString string; - - CuStringInit(&string); - if (message2 != NULL) - { - CuStringAppend(&string, message2); - CuStringAppend(&string, ": "); - } - CuStringAppend(&string, message); - CuFailInternal(tc, file, line, &string); -} - -void CuAssert_Line(CuTest* tc, const char* file, int line, const char* message, int condition) -{ - if (condition) return; - CuFail_Line(tc, file, line, NULL, message); -} - -void CuAssertStrEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message, - const char* expected, const char* actual) -{ - CuString string; - if ((expected == NULL && actual == NULL) || - (expected != NULL && actual != NULL && - strcmp(expected, actual) == 0)) - { - return; - } - - CuStringInit(&string); - if (message != NULL) - { - CuStringAppend(&string, message); - CuStringAppend(&string, ": "); - } - CuStringAppend(&string, "expected <"); - CuStringAppend(&string, expected); - CuStringAppend(&string, "> but was <"); - CuStringAppend(&string, actual); - CuStringAppend(&string, ">"); - CuFailInternal(tc, file, line, &string); -} - -void CuAssertIntEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message, - int expected, int actual) -{ - char buf[STRING_MAX]; - if (expected == actual) return; - sprintf(buf, "expected <%d> but was <%d>", expected, actual); - CuFail_Line(tc, file, line, message, buf); -} - -void CuAssertPtrEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message, - void* expected, void* actual) -{ - char buf[STRING_MAX]; - if (expected == actual) return; - sprintf(buf, "expected pointer <0x%p> but was <0x%p>", expected, actual); - CuFail_Line(tc, file, line, message, buf); -} - - -/*-------------------------------------------------------------------------* - * CuSuite - *-------------------------------------------------------------------------*/ - -void CuSuiteInit(CuSuite* testSuite) -{ - testSuite->count = 0; - testSuite->failCount = 0; - memset(testSuite->list, 0, sizeof(testSuite->list)); -} - -CuSuite* CuSuiteNew(void) -{ - CuSuite* testSuite = CU_ALLOC(CuSuite); - CuSuiteInit(testSuite); - return testSuite; -} - -void CuSuiteDelete(CuSuite *testSuite) -{ - unsigned int n; - for (n=0; n < MAX_TEST_CASES; n++) - { - if (testSuite->list[n]) - { - CuTestDelete(testSuite->list[n]); - } - } - free(testSuite); - -} - -void CuSuiteAdd(CuSuite* testSuite, CuTest *testCase) -{ - assert(testSuite->count < MAX_TEST_CASES); - testSuite->list[testSuite->count] = testCase; - testSuite->count++; -} - -void CuSuiteAddSuite(CuSuite* testSuite, CuSuite* testSuite2) -{ - int i; - for (i = 0 ; i < testSuite2->count ; ++i) - { - CuTest* testCase = testSuite2->list[i]; - CuSuiteAdd(testSuite, testCase); - } -} - -void CuSuiteRun(CuSuite* testSuite) -{ - int i; - for (i = 0 ; i < testSuite->count ; ++i) - { - CuTest* testCase = testSuite->list[i]; - CuTestRun(testCase); - if (testCase->failed) { testSuite->failCount += 1; } - } -} - -void CuSuiteSummary(CuSuite* testSuite, CuString* summary) -{ - int i; - for (i = 0 ; i < testSuite->count ; ++i) - { - CuTest* testCase = testSuite->list[i]; - CuStringAppend(summary, testCase->failed ? "F" : "."); - } - CuStringAppend(summary, "\n\n"); -} - -void CuSuiteDetails(CuSuite* testSuite, CuString* details) -{ - int i; - int failCount = 0; - - if (testSuite->failCount == 0) - { - int passCount = testSuite->count - testSuite->failCount; - const char* testWord = passCount == 1 ? "test" : "tests"; - CuStringAppendFormat(details, "OK (%d %s)\n", passCount, testWord); - } - else - { - if (testSuite->failCount == 1) - CuStringAppend(details, "There was 1 failure:\n"); - else - CuStringAppendFormat(details, "There were %d failures:\n", testSuite->failCount); - - for (i = 0 ; i < testSuite->count ; ++i) - { - CuTest* testCase = testSuite->list[i]; - if (testCase->failed) - { - failCount++; - CuStringAppendFormat(details, "%d) %s: %s\n", - failCount, testCase->name, testCase->message); - } - } - CuStringAppend(details, "\n!!!FAILURES!!!\n"); - - CuStringAppendFormat(details, "Runs: %d ", testSuite->count); - CuStringAppendFormat(details, "Passes: %d ", testSuite->count - testSuite->failCount); - CuStringAppendFormat(details, "Fails: %d\n", testSuite->failCount); - } -} diff --git a/tests/cutest/CuTest.h b/tests/cutest/CuTest.h deleted file mode 100644 index b82d05b..0000000 --- a/tests/cutest/CuTest.h +++ /dev/null @@ -1,111 +0,0 @@ -#ifndef CU_TEST_H -#define CU_TEST_H - -#include -#include - -#define CUTEST_VERSION "CuTest 1.5" - -/* CuString */ - -char* CuStrAlloc(int size); -char* CuStrCopy(const char* old); - -#define CU_ALLOC(TYPE) ((TYPE*) malloc(sizeof(TYPE))) - -#define HUGE_STRING_LEN 8192 -#define STRING_MAX 256 -#define STRING_INC 256 - -typedef struct -{ - int length; - int size; - char* buffer; -} CuString; - -void CuStringInit(CuString* str); -CuString* CuStringNew(void); -void CuStringRead(CuString* str, const char* path); -void CuStringAppend(CuString* str, const char* text); -void CuStringAppendChar(CuString* str, char ch); -void CuStringAppendFormat(CuString* str, const char* format, ...); -void CuStringInsert(CuString* str, const char* text, int pos); -void CuStringResize(CuString* str, int newSize); -void CuStringDelete(CuString* str); - -/* CuTest */ - -typedef struct CuTest CuTest; - -typedef void (*TestFunction)(CuTest *); - -struct CuTest -{ - char* name; - TestFunction function; - int failed; - int ran; - const char* message; - jmp_buf *jumpBuf; -}; - -void CuTestInit(CuTest* t, const char* name, TestFunction function); -CuTest* CuTestNew(const char* name, TestFunction function); -void CuTestRun(CuTest* tc); -void CuTestDelete(CuTest *t); - -/* Internal versions of assert functions -- use the public versions */ -void CuFail_Line(CuTest* tc, const char* file, int line, const char* message2, const char* message); -void CuAssert_Line(CuTest* tc, const char* file, int line, const char* message, int condition); -void CuAssertStrEquals_LineMsg(CuTest* tc, - const char* file, int line, const char* message, - const char* expected, const char* actual); -void CuAssertIntEquals_LineMsg(CuTest* tc, - const char* file, int line, const char* message, - int expected, int actual); -void CuAssertPtrEquals_LineMsg(CuTest* tc, - const char* file, int line, const char* message, - void* expected, void* actual); - -/* public assert functions */ - -#define CuFail(tc, ms) CuFail_Line( (tc), __FILE__, __LINE__, NULL, (ms)) -#define CuAssert(tc, ms, cond) CuAssert_Line((tc), __FILE__, __LINE__, (ms), (cond)) -#define CuAssertTrue(tc, cond) CuAssert_Line((tc), __FILE__, __LINE__, "assert failed", (cond)) - -#define CuAssertStrEquals(tc,ex,ac) CuAssertStrEquals_LineMsg((tc),__FILE__,__LINE__,NULL,(ex),(ac)) -#define CuAssertStrEquals_Msg(tc,ms,ex,ac) CuAssertStrEquals_LineMsg((tc),__FILE__,__LINE__,(ms),(ex),(ac)) -#define CuAssertIntEquals(tc,ex,ac) CuAssertIntEquals_LineMsg((tc),__FILE__,__LINE__,NULL,(ex),(ac)) -#define CuAssertIntEquals_Msg(tc,ms,ex,ac) CuAssertIntEquals_LineMsg((tc),__FILE__,__LINE__,(ms),(ex),(ac)) -#define CuAssertPtrEquals(tc,ex,ac) CuAssertPtrEquals_LineMsg((tc),__FILE__,__LINE__,NULL,(ex),(ac)) -#define CuAssertPtrEquals_Msg(tc,ms,ex,ac) CuAssertPtrEquals_LineMsg((tc),__FILE__,__LINE__,(ms),(ex),(ac)) - -#define CuAssertPtrNotNull(tc,p) CuAssert_Line((tc),__FILE__,__LINE__,"null pointer unexpected",(p != NULL)) -#define CuAssertPtrNotNullMsg(tc,msg,p) CuAssert_Line((tc),__FILE__,__LINE__,(msg),(p != NULL)) - -/* CuSuite */ - -#define MAX_TEST_CASES 1024 - -#define SUITE_ADD_TEST(SUITE,TEST) CuSuiteAdd(SUITE, CuTestNew(#TEST, TEST)) - -typedef struct -{ - int count; - CuTest* list[MAX_TEST_CASES]; - int failCount; - -} CuSuite; - - -void CuSuiteInit(CuSuite* testSuite); -CuSuite* CuSuiteNew(void); -void CuSuiteDelete(CuSuite *testSuite); -void CuSuiteAdd(CuSuite* testSuite, CuTest *testCase); -void CuSuiteAddSuite(CuSuite* testSuite, CuSuite* testSuite2); -void CuSuiteRun(CuSuite* testSuite); -void CuSuiteSummary(CuSuite* testSuite, CuString* summary); -void CuSuiteDetails(CuSuite* testSuite, CuString* details); - -#endif /* CU_TEST_H */ diff --git a/tests/cutest/README.txt b/tests/cutest/README.txt deleted file mode 100644 index 96e8853..0000000 --- a/tests/cutest/README.txt +++ /dev/null @@ -1,211 +0,0 @@ -HOW TO USE - -You can use CuTest to create unit tests to drive your development -in the style of Extreme Programming. You can also add unit tests to -existing code to ensure that it works as you suspect. - -Your unit tests are an investment. They let you to change your -code and add new features confidently without worrying about -accidentally breaking earlier features. - - -LICENSING - -For details on licensing see license.txt. - - -GETTING STARTED - -To add unit testing to your C code the only files you need are -CuTest.c and CuTest.h. - -CuTestTest.c and AllTests.c have been included to provide an -example of how to write unit tests and then how to aggregate them -into suites and into a single AllTests.c file. Suites allow you -to put group tests into logical sets. AllTests.c combines all the -suites and runs them. - -You should not have to look inside CuTest.c. Looking in -CuTestTest.c and AllTests.c (for example usage) should be -sufficient. - -After downloading the sources, run your compiler to create an -executable called AllTests.exe. For example, if you are using -Windows with the cl.exe compiler you would type: - - cl.exe AllTests.c CuTest.c CuTestTest.c - AllTests.exe - -This will run all the unit tests associated with CuTest and print -the output on the console. You can replace cl.exe with gcc or -your favorite compiler in the command above. - - -DETAILED EXAMPLE - -Here is a more detailed example. We will work through a simple -test first exercise. The goal is to create a library of string -utilities. First, lets write a function that converts a -null-terminated string to all upper case. - -Ensure that CuTest.c and CuTest.h are accessible from your C -project. Next, create a file called StrUtil.c with these -contents: - - #include "CuTest.h" - - char* StrToUpper(char* str) { - return str; - } - - void TestStrToUpper(CuTest *tc) { - char* input = strdup("hello world"); - char* actual = StrToUpper(input); - char* expected = "HELLO WORLD"; - CuAssertStrEquals(tc, expected, actual); - } - - CuSuite* StrUtilGetSuite() { - CuSuite* suite = CuSuiteNew(); - SUITE_ADD_TEST(suite, TestStrToUpper); - return suite; - } - -Create another file called AllTests.c with these contents: - - #include "CuTest.h" - - CuSuite* StrUtilGetSuite(); - - void RunAllTests(void) { - CuString *output = CuStringNew(); - CuSuite* suite = CuSuiteNew(); - - CuSuiteAddSuite(suite, StrUtilGetSuite()); - - CuSuiteRun(suite); - CuSuiteSummary(suite, output); - CuSuiteDetails(suite, output); - printf("%s\n", output->buffer); - } - - int main(void) { - RunAllTests(); - } - -Then type this on the command line: - - gcc AllTests.c CuTest.c StrUtil.c - -to compile. You can replace gcc with your favorite compiler. -CuTest should be portable enough to handle all Windows and Unix -compilers. Then to run the tests type: - - a.out - -This will print an error because we haven't implemented the -StrToUpper function correctly. We are just returning the string -without changing it to upper case. - - char* StrToUpper(char* str) { - return str; - } - -Rewrite this as follows: - - char* StrToUpper(char* str) { - char* p; - for (p = str ; *p ; ++p) *p = toupper(*p); - return str; - } - -Recompile and run the tests again. The test should pass this -time. - - -WHAT TO DO NEXT - -At this point you might want to write more tests for the -StrToUpper function. Here are some ideas: - -TestStrToUpper_EmptyString : pass in "" -TestStrToUpper_UpperCase : pass in "HELLO WORLD" -TestStrToUpper_MixedCase : pass in "HELLO world" -TestStrToUpper_Numbers : pass in "1234 hello" - -As you write each one of these tests add it to StrUtilGetSuite -function. If you don't the tests won't be run. Later as you write -other functions and write tests for them be sure to include those -in StrUtilGetSuite also. The StrUtilGetSuite function should -include all the tests in StrUtil.c - -Over time you will create another file called FunkyStuff.c -containing other functions unrelated to StrUtil. Follow the same -pattern. Create a FunkyStuffGetSuite function in FunkyStuff.c. -And add FunkyStuffGetSuite to AllTests.c. - -The framework is designed in the way it is so that it is easy to -organize a lot of tests. - -THE BIG PICTURE - -Each individual test corresponds to a CuTest. These are grouped -to form a CuSuite. CuSuites can hold CuTests or other CuSuites. -AllTests.c collects all the CuSuites in the program into a single -CuSuite which it then runs as a single CuSuite. - -The project is open source so feel free to take a peek under the -hood at the CuTest.c file to see how it works. CuTestTest.c -contains tests for CuTest.c. So CuTest tests itself. - -Since AllTests.c has a main() you will need to exclude this when -you are building your product. Here is a nicer way to do this if -you want to avoid messing with multiple builds. Remove the main() -in AllTests.c. Note that it just calls RunAllTests(). Instead -we'll call this directly from the main program. - -Now in the main() of the actual program check to see if the -command line option "--test" was passed. If it was then I call -RunAllTests() from AllTests.c. Otherwise run the real program. - -Shipping the tests with the code can be useful. If you customers -complain about a problem you can ask them to run the unit tests -and send you the output. This can help you to quickly isolate the -piece of your system that is malfunctioning in the customer's -environment. - -CuTest offers a rich set of CuAssert functions. Here is a list: - -void CuAssert(CuTest* tc, char* message, int condition); -void CuAssertTrue(CuTest* tc, int condition); -void CuAssertStrEquals(CuTest* tc, char* expected, char* actual); -void CuAssertIntEquals(CuTest* tc, int expected, int actual); -void CuAssertPtrEquals(CuTest* tc, void* expected, void* actual); -void CuAssertPtrNotNull(CuTest* tc, void* pointer); - -The project is open source and so you can add other more powerful -asserts to make your tests easier to write and more concise. -Please feel free to send me changes you make so that I can -incorporate them into future releases. - -If you see any errors in this document please contact me at -asimjalis@peakprogramming.com. - - -AUTOMATING TEST SUITE GENERATION - -make-tests.sh will grep through all the .c files in the current -directory and generate the code to run all the tests contained in -them. Using this script you don't have to worry about writing -AllTests.c or dealing with any of the other suite code. - - -CREDITS - -These people have contributed useful code changes to the CuTest project. -Thanks! - -- [02.23.2003] Dave Glowacki -- [04.17.2009] Tobias Lippert -- [11.13.2009] Eli Bendersky -- [12.14.2009] Andrew Brown diff --git a/tests/cutest/license.txt b/tests/cutest/license.txt deleted file mode 100644 index 3d94167..0000000 --- a/tests/cutest/license.txt +++ /dev/null @@ -1,38 +0,0 @@ -NOTE - -The license is based on the zlib/libpng license. For more details see -http://www.opensource.org/licenses/zlib-license.html. The intent of the -license is to: - -- keep the license as simple as possible -- encourage the use of CuTest in both free and commercial applications - and libraries -- keep the source code together -- give credit to the CuTest contributors for their work - -If you ship CuTest in source form with your source distribution, the -following license document must be included with it in unaltered form. -If you find CuTest useful we would like to hear about it. - -LICENSE - -Copyright (c) 2003 Asim Jalis - -This software is provided 'as-is', without any express or implied -warranty. In no event will the authors be held liable for any damages -arising from the use of this software. - -Permission is granted to anyone to use this software for any purpose, -including commercial applications, and to alter it and redistribute it -freely, subject to the following restrictions: - -1. The origin of this software must not be misrepresented; you must not -claim that you wrote the original software. If you use this software in -a product, an acknowledgment in the product documentation would be -appreciated but is not required. - -2. Altered source versions must be plainly marked as such, and must not -be misrepresented as being the original software. - -3. This notice may not be removed or altered from any source -distribution. diff --git a/tests/files/system-modules/four.module b/tests/files/system-modules/four.module deleted file mode 100644 index 6eace3c..0000000 --- a/tests/files/system-modules/four.module +++ /dev/null @@ -1,3 +0,0 @@ - -module: mock-four.so -disable-in: test-disable, test-other diff --git a/tests/files/system-modules/one.module b/tests/files/system-modules/one.module deleted file mode 100644 index 3620869..0000000 --- a/tests/files/system-modules/one.module +++ /dev/null @@ -1,3 +0,0 @@ - -module: mock-one.so -setting: system1 \ No newline at end of file diff --git a/tests/files/system-modules/two-duplicate.module b/tests/files/system-modules/two-duplicate.module deleted file mode 100644 index 907aa75..0000000 --- a/tests/files/system-modules/two-duplicate.module +++ /dev/null @@ -1,3 +0,0 @@ - -# This is a duplicate of the 'two' module -module: mock-two.so diff --git a/tests/files/system-modules/two.badname b/tests/files/system-modules/two.badname deleted file mode 100644 index 0d41cac..0000000 --- a/tests/files/system-modules/two.badname +++ /dev/null @@ -1,5 +0,0 @@ -# This module doesn't have a .module extension, but p11-kit doesn't yet -# enforce the naming, just warns, so it should still be loaded - -module: mock-two.so -setting: system2 diff --git a/tests/files/system-modules/win32/four.module b/tests/files/system-modules/win32/four.module deleted file mode 100644 index 7fd1540..0000000 --- a/tests/files/system-modules/win32/four.module +++ /dev/null @@ -1,3 +0,0 @@ - -module: mock-four.dll -disable-in: test-disable, test-other diff --git a/tests/files/system-modules/win32/one.module b/tests/files/system-modules/win32/one.module deleted file mode 100644 index 5f80304..0000000 --- a/tests/files/system-modules/win32/one.module +++ /dev/null @@ -1,3 +0,0 @@ - -module: mock-one.dll -setting: system1 \ No newline at end of file diff --git a/tests/files/system-modules/win32/two-duplicate.module b/tests/files/system-modules/win32/two-duplicate.module deleted file mode 100644 index e80c9e8..0000000 --- a/tests/files/system-modules/win32/two-duplicate.module +++ /dev/null @@ -1,3 +0,0 @@ - -# This is a duplicate of the 'two' module -module: mock-two.dll diff --git a/tests/files/system-modules/win32/two.badname b/tests/files/system-modules/win32/two.badname deleted file mode 100644 index ae44b83..0000000 --- a/tests/files/system-modules/win32/two.badname +++ /dev/null @@ -1,5 +0,0 @@ -# This module doesn't have a .module extension, but p11-kit doesn't yet -# enforce the naming, just warns, so it should still be loaded - -module: mock-two.dll -setting: system2 diff --git a/tests/files/system-pkcs11.conf b/tests/files/system-pkcs11.conf deleted file mode 100644 index 20741e7..0000000 --- a/tests/files/system-pkcs11.conf +++ /dev/null @@ -1,3 +0,0 @@ - -# Merge in user config -user-config: merge \ No newline at end of file diff --git a/tests/files/test-1.conf b/tests/files/test-1.conf deleted file mode 100644 index d4ae0a1..0000000 --- a/tests/files/test-1.conf +++ /dev/null @@ -1,6 +0,0 @@ -key1:value1 -with-whitespace : value-with-whitespace -with-colon: value-of-colon - -# A comment -embedded-comment: this is # not a comment diff --git a/tests/files/test-pinfile b/tests/files/test-pinfile deleted file mode 100644 index f646f3d..0000000 --- a/tests/files/test-pinfile +++ /dev/null @@ -1 +0,0 @@ -yogabbagabba \ No newline at end of file diff --git a/tests/files/test-pinfile-large b/tests/files/test-pinfile-large deleted file mode 100644 index 506668d..0000000 --- a/tests/files/test-pinfile-large +++ /dev/null @@ -1,53 +0,0 @@ -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba yogabbagabba -yogabbagabba yogabbagabba yogabbagabba yo \ No newline at end of file diff --git a/tests/files/test-system-invalid.conf b/tests/files/test-system-invalid.conf deleted file mode 100644 index 344ee96..0000000 --- a/tests/files/test-system-invalid.conf +++ /dev/null @@ -1,3 +0,0 @@ - -# Invalid user-config setting -user-config: bad diff --git a/tests/files/test-system-merge.conf b/tests/files/test-system-merge.conf deleted file mode 100644 index 978427d..0000000 --- a/tests/files/test-system-merge.conf +++ /dev/null @@ -1,7 +0,0 @@ - -# Merge in user config -user-config: merge - -key1: system1 -key2: system2 -key3: system3 \ No newline at end of file diff --git a/tests/files/test-system-none.conf b/tests/files/test-system-none.conf deleted file mode 100644 index 95351e6..0000000 --- a/tests/files/test-system-none.conf +++ /dev/null @@ -1,8 +0,0 @@ - -# Only user config -user-config: none - -# These values will not be overriden -key1: system1 -key2: system2 -key3: system3 \ No newline at end of file diff --git a/tests/files/test-system-only.conf b/tests/files/test-system-only.conf deleted file mode 100644 index 589f1c7..0000000 --- a/tests/files/test-system-only.conf +++ /dev/null @@ -1,8 +0,0 @@ - -# Only user config -user-config: only - -# This stuff will be ignored -key1: system1 -key2: system2 -key3: system3 \ No newline at end of file diff --git a/tests/files/test-user-invalid.conf b/tests/files/test-user-invalid.conf deleted file mode 100644 index 344ee96..0000000 --- a/tests/files/test-user-invalid.conf +++ /dev/null @@ -1,3 +0,0 @@ - -# Invalid user-config setting -user-config: bad diff --git a/tests/files/test-user-only.conf b/tests/files/test-user-only.conf deleted file mode 100644 index 3224c01..0000000 --- a/tests/files/test-user-only.conf +++ /dev/null @@ -1,4 +0,0 @@ - -user-config: only -key2: user2 -key3: user3 \ No newline at end of file diff --git a/tests/files/test-user.conf b/tests/files/test-user.conf deleted file mode 100644 index 369544a..0000000 --- a/tests/files/test-user.conf +++ /dev/null @@ -1,3 +0,0 @@ - -key2: user2 -key3: user3 \ No newline at end of file diff --git a/tests/files/user-modules/one.module b/tests/files/user-modules/one.module deleted file mode 100644 index c371e4a..0000000 --- a/tests/files/user-modules/one.module +++ /dev/null @@ -1,2 +0,0 @@ - -setting: user1 \ No newline at end of file diff --git a/tests/files/user-modules/three.module b/tests/files/user-modules/three.module deleted file mode 100644 index 00caab5..0000000 --- a/tests/files/user-modules/three.module +++ /dev/null @@ -1,5 +0,0 @@ - -module: mock-three.so -setting: user3 - -enable-in: test-enable \ No newline at end of file diff --git a/tests/files/user-modules/win32/one.module b/tests/files/user-modules/win32/one.module deleted file mode 100644 index c371e4a..0000000 --- a/tests/files/user-modules/win32/one.module +++ /dev/null @@ -1,2 +0,0 @@ - -setting: user1 \ No newline at end of file diff --git a/tests/files/user-modules/win32/three.module b/tests/files/user-modules/win32/three.module deleted file mode 100644 index 58f883d..0000000 --- a/tests/files/user-modules/win32/three.module +++ /dev/null @@ -1,5 +0,0 @@ - -module: mock-three.dll -setting: user3 - -enable-in: test-enable \ No newline at end of file diff --git a/tests/hash-test.c b/tests/hash-test.c deleted file mode 100644 index cf77094..0000000 --- a/tests/hash-test.c +++ /dev/null @@ -1,465 +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 - */ - -#include "config.h" -#include "CuTest.h" - -#include -#include -#include -#include - -#include "hashmap.h" - -static void -test_create (CuTest *tc) -{ - hashmap *map; - - map = _p11_hash_create (_p11_hash_direct_hash, _p11_hash_direct_equal, NULL, NULL); - CuAssertPtrNotNull (tc, map); - _p11_hash_free (map); -} - -static void -test_free_null (CuTest *tc) -{ - _p11_hash_free (NULL); -} - -typedef struct { - int value; - int freed; -} Key; - -static unsigned int -key_hash (const void *ptr) -{ - const Key *k = ptr; - assert (!k->freed); - return _p11_hash_intptr_hash (&k->value); -} - -static int -key_equal (const void *one, - const void *two) -{ - const Key *k1 = one; - const Key *k2 = two; - assert (!k1->freed); - assert (!k2->freed); - return _p11_hash_intptr_equal (&k1->value, &k2->value); -} - -static void -key_destroy (void *data) -{ - Key *k = data; - assert (!k->freed); - k->freed = 1; -} - -static void -value_destroy (void *data) -{ - int *value = data; - *value = 2; -} - -static void -test_free_destroys (CuTest *tc) -{ - hashmap *map; - Key key = { 8, 0 }; - int value = 0; - - map = _p11_hash_create (key_hash, key_equal, key_destroy, value_destroy); - CuAssertPtrNotNull (tc, map); - if (!_p11_hash_set (map, &key, &value)) - CuFail (tc, "should not be reached"); - _p11_hash_free (map); - - CuAssertIntEquals (tc, 1, key.freed); - CuAssertIntEquals (tc, 2, value); -} - -static void -test_iterate (CuTest *tc) -{ - hashmap *map; - hashiter 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); - CuAssertPtrNotNull (tc, map); - if (!_p11_hash_set (map, &key, &value)) - CuFail (tc, "should not be reached"); - - _p11_hash_iterate (map, &iter); - - ret = _p11_hash_next (&iter, &pkey, &pvalue); - CuAssertIntEquals (tc, 1, ret); - CuAssertPtrEquals (tc, pkey, &key); - CuAssertPtrEquals (tc, pvalue, &value); - - ret = _p11_hash_next (&iter, &pkey, &pvalue); - CuAssertIntEquals (tc, 0, ret); - - _p11_hash_free (map); -} - -static void -test_set_get (CuTest *tc) -{ - char *key = "KEY"; - char *value = "VALUE"; - char *check; - hashmap *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); - CuAssertPtrEquals (tc, check, value); - - _p11_hash_free (map); -} - -static void -test_set_get_remove (CuTest *tc) -{ - char *key = "KEY"; - char *value = "VALUE"; - char *check; - hashmap *map; - int ret; - - map = _p11_hash_create (_p11_hash_string_hash, _p11_hash_string_equal, NULL, NULL); - - if (!_p11_hash_set (map, key, value)) - CuFail (tc, "should not be reached"); - - check = _p11_hash_get (map, key); - CuAssertPtrEquals (tc, check, value); - - ret = _p11_hash_remove (map, key); - CuAssertIntEquals (tc, ret, 1); - ret = _p11_hash_remove (map, key); - CuAssertIntEquals (tc, ret, 0); - - check = _p11_hash_get (map, key); - CuAssert (tc, "should be null", check == NULL); - - _p11_hash_free (map); -} - -static void -test_set_clear (CuTest *tc) -{ - char *key = "KEY"; - char *value = "VALUE"; - char *check; - hashmap *map; - - map = _p11_hash_create (_p11_hash_direct_hash, _p11_hash_direct_equal, NULL, NULL); - - if (!_p11_hash_set (map, key, value)) - CuFail (tc, "should not be reached"); - - _p11_hash_clear (map); - - check = _p11_hash_get (map, key); - CuAssert (tc, "should be null", check == NULL); - - _p11_hash_free (map); -} - -static void -test_remove_destroys (CuTest *tc) -{ - hashmap *map; - Key key = { 8, 0 }; - int value = 0; - int ret; - - map = _p11_hash_create (key_hash, key_equal, key_destroy, value_destroy); - CuAssertPtrNotNull (tc, map); - if (!_p11_hash_set (map, &key, &value)) - CuFail (tc, "should not be reached"); - - ret = _p11_hash_remove (map, &key); - CuAssertIntEquals (tc, ret, 1); - CuAssertIntEquals (tc, 1, key.freed); - CuAssertIntEquals (tc, 2, value); - - /* should not be destroyed again */ - key.freed = 0; - value = 0; - - ret = _p11_hash_remove (map, &key); - CuAssertIntEquals (tc, ret, 0); - CuAssertIntEquals (tc, 0, key.freed); - CuAssertIntEquals (tc, 0, value); - - /* should not be destroyed again */ - key.freed = 0; - value = 0; - - _p11_hash_free (map); - - CuAssertIntEquals (tc, 0, key.freed); - CuAssertIntEquals (tc, 0, value); -} - -static void -test_set_destroys (CuTest *tc) -{ - hashmap *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); - CuAssertPtrNotNull (tc, map); - if (!_p11_hash_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); - CuAssertIntEquals (tc, ret, 1); - CuAssertIntEquals (tc, 0, key.freed); - CuAssertIntEquals (tc, 0, key2.freed); - CuAssertIntEquals (tc, 0, value); - CuAssertIntEquals (tc, 0, value2); - - key.freed = key2.freed = value = value2 = 0; - - /* Setting a new key same value, key should be destroyed */ - ret = _p11_hash_set (map, &key2, &value); - CuAssertIntEquals (tc, ret, 1); - CuAssertIntEquals (tc, 1, key.freed); - CuAssertIntEquals (tc, 0, key2.freed); - CuAssertIntEquals (tc, 0, value); - CuAssertIntEquals (tc, 0, value2); - - key.freed = key2.freed = value = value2 = 0; - - /* Setting same key, new value, value should be destroyed */ - ret = _p11_hash_set (map, &key2, &value2); - CuAssertIntEquals (tc, ret, 1); - CuAssertIntEquals (tc, 0, key.freed); - CuAssertIntEquals (tc, 0, key2.freed); - CuAssertIntEquals (tc, 2, value); - CuAssertIntEquals (tc, 0, value2); - - key.freed = key2.freed = value = value2 = 0; - - /* Setting new key new value, both should be destroyed */ - ret = _p11_hash_set (map, &key, &value); - CuAssertIntEquals (tc, ret, 1); - CuAssertIntEquals (tc, 0, key.freed); - CuAssertIntEquals (tc, 1, key2.freed); - CuAssertIntEquals (tc, 0, value); - CuAssertIntEquals (tc, 2, value2); - - key.freed = key2.freed = value = value2 = 0; - - _p11_hash_free (map); - CuAssertIntEquals (tc, 1, key.freed); - CuAssertIntEquals (tc, 2, value); - CuAssertIntEquals (tc, 0, key2.freed); - CuAssertIntEquals (tc, 0, value2); -} - - -static void -test_clear_destroys (CuTest *tc) -{ - hashmap *map; - Key key = { 18, 0 }; - int value = 0; - - map = _p11_hash_create (key_hash, key_equal, key_destroy, value_destroy); - CuAssertPtrNotNull (tc, map); - if (!_p11_hash_set (map, &key, &value)) - CuFail (tc, "should not be reached"); - - _p11_hash_clear (map); - CuAssertIntEquals (tc, 1, key.freed); - CuAssertIntEquals (tc, 2, value); - - /* should not be destroyed again */ - key.freed = 0; - value = 0; - - _p11_hash_clear (map); - CuAssertIntEquals (tc, 0, key.freed); - CuAssertIntEquals (tc, 0, value); - - /* should not be destroyed again */ - key.freed = 0; - value = 0; - - _p11_hash_free (map); - - CuAssertIntEquals (tc, 0, key.freed); - CuAssertIntEquals (tc, 0, value); -} - -static unsigned int -test_hash_intptr_with_collisions (const void *data) -{ - /* lots and lots of collisions, only returns 100 values */ - return (unsigned int)(*((int*)data) % 100); -} - -static void -test_hash_add_check_lots_and_collisions (CuTest *tc) -{ - hashmap *map; - int *value; - int i; - - map = _p11_hash_create (test_hash_intptr_with_collisions, - _p11_hash_intptr_equal, NULL, free); - - for (i = 0; i < 20000; ++i) { - value = malloc (sizeof (int)); - *value = i; - if (!_p11_hash_set (map, value, value)) - CuFail (tc, "should not be reached"); - } - - for (i = 0; i < 20000; ++i) { - value = _p11_hash_get (map, &i); - CuAssertPtrNotNull (tc, value); - CuAssertIntEquals (tc, i, *value); - } - - _p11_hash_free (map); -} - -static void -test_hash_count (CuTest *tc) -{ - hashmap *map; - int *value; - int i, ret; - - map = _p11_hash_create (_p11_hash_intptr_hash, _p11_hash_intptr_equal, NULL, free); - - CuAssertIntEquals (tc, 0, _p11_hash_size (map)); - - for (i = 0; i < 20000; ++i) { - value = malloc (sizeof (int)); - *value = i; - if (!_p11_hash_set (map, value, value)) - CuFail (tc, "should not be reached"); - CuAssertIntEquals (tc, i + 1, _p11_hash_size (map)); - } - - for (i = 0; i < 20000; ++i) { - ret = _p11_hash_remove (map, &i); - CuAssertIntEquals (tc, 1, ret); - CuAssertIntEquals (tc, 20000 - (i + 1), _p11_hash_size (map)); - } - - _p11_hash_clear (map); - CuAssertIntEquals (tc, 0, _p11_hash_size (map)); - - _p11_hash_free (map); -} - -static void -test_hash_ulongptr (CuTest *tc) -{ - hashmap *map; - unsigned long *value; - unsigned long i; - - map = _p11_hash_create (_p11_hash_ulongptr_hash, _p11_hash_ulongptr_equal, NULL, free); - - for (i = 0; i < 20000; ++i) { - value = malloc (sizeof (unsigned long)); - *value = i; - if (!_p11_hash_set (map, value, value)) - CuFail (tc, "should not be reached"); - } - - for (i = 0; i < 20000; ++i) { - value = _p11_hash_get (map, &i); - CuAssertPtrNotNull (tc, value); - CuAssertIntEquals (tc, i, *value); - } - - _p11_hash_free (map); -} - -int -main (void) -{ - CuString *output = CuStringNew (); - CuSuite* suite = CuSuiteNew (); - int ret; - - SUITE_ADD_TEST (suite, test_create); - SUITE_ADD_TEST (suite, test_set_get); - SUITE_ADD_TEST (suite, test_set_get_remove); - SUITE_ADD_TEST (suite, test_remove_destroys); - SUITE_ADD_TEST (suite, test_set_clear); - SUITE_ADD_TEST (suite, test_set_destroys); - SUITE_ADD_TEST (suite, test_clear_destroys); - SUITE_ADD_TEST (suite, test_free_null); - SUITE_ADD_TEST (suite, test_free_destroys); - SUITE_ADD_TEST (suite, test_iterate); - SUITE_ADD_TEST (suite, test_hash_add_check_lots_and_collisions); - SUITE_ADD_TEST (suite, test_hash_count); - SUITE_ADD_TEST (suite, test_hash_ulongptr); - - 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/mock-module-ep.c b/tests/mock-module-ep.c deleted file mode 100644 index 7440a74..0000000 --- a/tests/mock-module-ep.c +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright (c) 2012 Stefan Walter - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * * Redistributions of source code must retain the above - * copyright notice, this list of conditions and the - * following disclaimer. - * * Redistributions in binary form must reproduce the - * above copyright notice, this list of conditions and - * the following disclaimer in the documentation and/or - * other materials provided with the distribution. - * * The names of contributors to this software may not be - * used to endorse or promote products derived from this - * software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF - * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH - * DAMAGE. - * - * Author: Stef Walter - */ - -#include "config.h" - -#define CRYPTOKI_EXPORTS 1 -#include "pkcs11.h" -#include "mock-module.h" - -#ifdef OS_WIN32 -__declspec(dllexport) -#endif -CK_RV -C_GetFunctionList (CK_FUNCTION_LIST_PTR_PTR list) -{ - mock_module_init (); - if (list == NULL) - return CKR_ARGUMENTS_BAD; - *list = &mock_module_no_slots; - return CKR_OK; -} diff --git a/tests/mock-module.c b/tests/mock-module.c deleted file mode 100644 index 99cde50..0000000 --- a/tests/mock-module.c +++ /dev/null @@ -1,898 +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 - */ - -#include "config.h" - -#define CRYPTOKI_EXPORTS -#include "pkcs11.h" -#include "mock-module.h" - -#include "p11-kit/util.h" - -#include -#include -#include -#include - -/* ------------------------------------------------------------------- - * GLOBALS / DEFINES - */ - -/* Various mutexes */ -static mutex_t init_mutex; - -/* Whether we've been initialized, and on what process id it happened */ -static int pkcs11_initialized = 0; -static pid_t pkcs11_initialized_pid = 0; - -/* ----------------------------------------------------------------------------- - * LOGGING and DEBUGGING - */ - -#define DEBUG_OUTPUT 0 - -#if DEBUG_OUTPUT -#define debug(x) mock_log x -#else -#define debug(x) -#endif - -#define warning(x) mock_log x - -#define return_val_if_fail(x, v) \ - if (!(x)) { mock_log ("'%s' not true at %s", #x, __func__); return v; } - -static void -mock_log (const char *format, ...) -{ - va_list va; - va_start (va, format); - fprintf (stderr, "mock-module: "); - vfprintf (stderr, format, va); - fprintf (stderr, "\n"); - va_end (va); -} - -/* ------------------------------------------------------------------- - * INITIALIZATION and 'GLOBAL' CALLS - */ - -CK_RV -mock_C_Initialize (CK_VOID_PTR init_args) -{ - CK_C_INITIALIZE_ARGS_PTR args = NULL; - CK_RV ret = CKR_OK; - pid_t pid; - - debug (("C_Initialize: enter")); - - _p11_mutex_lock (&init_mutex); - - if (init_args != NULL) { - int supplied_ok; - - /* pReserved must be NULL */ - args = init_args; - - /* ALL supplied function pointers need to have the value either NULL or non-NULL. */ - supplied_ok = (args->CreateMutex == NULL && args->DestroyMutex == NULL && - args->LockMutex == NULL && args->UnlockMutex == NULL) || - (args->CreateMutex != NULL && args->DestroyMutex != NULL && - args->LockMutex != NULL && args->UnlockMutex != NULL); - if (!supplied_ok) { - warning (("invalid set of mutex calls supplied")); - ret = CKR_ARGUMENTS_BAD; - goto done; - } - - /* - * When the CKF_OS_LOCKING_OK flag isn't set return an error. - * We must be able to use our pthread functionality. - */ - if (!(args->flags & CKF_OS_LOCKING_OK)) { - warning (("can't do without os locking")); - ret = CKR_CANT_LOCK; - goto done; - } - } - - pid = getpid (); - if (pkcs11_initialized) { - - /* This process has called C_Initialize already */ - if (pid == pkcs11_initialized_pid) { - warning (("C_Initialize called twice for same process")); - ret = CKR_CRYPTOKI_ALREADY_INITIALIZED; - goto done; - } - } - -done: - /* Mark us as officially initialized */ - if (ret == CKR_OK) { - pkcs11_initialized = 1; - pkcs11_initialized_pid = pid; - } else if (ret != CKR_CRYPTOKI_ALREADY_INITIALIZED) { - pkcs11_initialized = 0; - pkcs11_initialized_pid = 0; - } - - _p11_mutex_unlock (&init_mutex); - - debug (("C_Initialize: %d", ret)); - return ret; -} - -CK_RV -mock_C_Finalize (CK_VOID_PTR reserved) -{ - debug (("C_Finalize: enter")); - 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); - - /* This should stop all other calls in */ - pkcs11_initialized = 0; - pkcs11_initialized_pid = 0; - - _p11_mutex_unlock (&init_mutex); - - debug (("C_Finalize: %d", CKR_OK)); - return CKR_OK; -} - -static const CK_INFO MOCK_INFO = { - { CRYPTOKI_VERSION_MAJOR, CRYPTOKI_VERSION_MINOR }, - "MOCK MANUFACTURER ", - 0, - "MOCK LIBRARY ", - { 45, 145 } -}; - - -CK_RV -mock_C_GetInfo (CK_INFO_PTR info) -{ - return_val_if_fail (info, CKR_ARGUMENTS_BAD); - - memcpy (info, &MOCK_INFO, sizeof (*info)); - return CKR_OK; -} - -CK_RV -mock_C_GetFunctionList (CK_FUNCTION_LIST_PTR_PTR list) -{ - /* This would be a strange call to receive */ - return C_GetFunctionList (list); -} - -CK_RV -mock_C_GetSlotList__no_tokens (CK_BBOOL token_present, - CK_SLOT_ID_PTR slot_list, - CK_ULONG_PTR count) -{ - return_val_if_fail (count, CKR_ARGUMENTS_BAD); - - /* No tokens */ - *count = 0; - return CKR_OK; -} - -CK_RV -mock_C_GetSlotInfo__invalid_slotid (CK_SLOT_ID id, - CK_SLOT_INFO_PTR info) -{ - return_val_if_fail (info, CKR_ARGUMENTS_BAD); - - return CKR_SLOT_ID_INVALID; -} - -CK_RV -mock_C_GetTokenInfo__invalid_slotid (CK_SLOT_ID id, - CK_TOKEN_INFO_PTR info) -{ - return_val_if_fail (info, CKR_ARGUMENTS_BAD); - - return CKR_SLOT_ID_INVALID; -} - -CK_RV -mock_C_GetMechanismList__invalid_slotid (CK_SLOT_ID id, - CK_MECHANISM_TYPE_PTR mechanism_list, - CK_ULONG_PTR count) -{ - return_val_if_fail (count, CKR_ARGUMENTS_BAD); - - return CKR_SLOT_ID_INVALID; -} - -CK_RV -mock_C_GetMechanismInfo__invalid_slotid (CK_SLOT_ID id, - CK_MECHANISM_TYPE type, - CK_MECHANISM_INFO_PTR info) -{ - return_val_if_fail (info, CKR_ARGUMENTS_BAD); - - return CKR_SLOT_ID_INVALID; -} - -CK_RV -mock_C_InitToken__invalid_slotid (CK_SLOT_ID id, - CK_UTF8CHAR_PTR pin, - CK_ULONG pin_len, - CK_UTF8CHAR_PTR label) -{ - return CKR_SLOT_ID_INVALID; -} - -CK_RV -mock_C_WaitForSlotEvent__no_event (CK_FLAGS flags, - CK_SLOT_ID_PTR slot, - CK_VOID_PTR reserved) -{ - return_val_if_fail (slot, CKR_ARGUMENTS_BAD); - - return CKR_NO_EVENT; -} - -CK_RV -mock_C_OpenSession__invalid_slotid (CK_SLOT_ID id, - CK_FLAGS flags, - CK_VOID_PTR user_data, - CK_NOTIFY callback, - CK_SESSION_HANDLE_PTR session) -{ - return_val_if_fail (session, CKR_ARGUMENTS_BAD); - - return CKR_SLOT_ID_INVALID; -} - -CK_RV -mock_C_CloseSession__invalid_handle (CK_SESSION_HANDLE session) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_CloseAllSessions__invalid_slotid (CK_SLOT_ID id) -{ - return CKR_SLOT_ID_INVALID; -} - -CK_RV -mock_C_GetFunctionStatus__not_parallel (CK_SESSION_HANDLE session) -{ - return CKR_FUNCTION_NOT_PARALLEL; -} - -CK_RV -mock_C_CancelFunction__not_parallel (CK_SESSION_HANDLE session) -{ - return CKR_FUNCTION_NOT_PARALLEL; -} - -CK_RV -mock_C_GetSessionInfo__invalid_handle (CK_SESSION_HANDLE session, - CK_SESSION_INFO_PTR info) -{ - return_val_if_fail (info, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_InitPIN__invalid_handle (CK_SESSION_HANDLE session, - CK_UTF8CHAR_PTR pin, - CK_ULONG pin_len) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_SetPIN__invalid_handle (CK_SESSION_HANDLE session, - CK_UTF8CHAR_PTR old_pin, - CK_ULONG old_pin_len, - CK_UTF8CHAR_PTR new_pin, - CK_ULONG new_pin_len) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_GetOperationState__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR operation_state, - CK_ULONG_PTR operation_state_len) -{ - return_val_if_fail (operation_state_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_SetOperationState__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR operation_state, - CK_ULONG operation_state_len, - CK_OBJECT_HANDLE encryption_key, - CK_OBJECT_HANDLE authentication_key) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_Login__invalid_handle (CK_SESSION_HANDLE session, - CK_USER_TYPE user_type, - CK_UTF8CHAR_PTR pin, - CK_ULONG pin_len) -{ - return CKR_SESSION_HANDLE_INVALID; - -} - -CK_RV -mock_C_Logout__invalid_handle (CK_SESSION_HANDLE session) -{ - return CKR_SESSION_HANDLE_INVALID; - -} - -CK_RV -mock_C_CreateObject__invalid_handle (CK_SESSION_HANDLE session, - CK_ATTRIBUTE_PTR template, - CK_ULONG count, - CK_OBJECT_HANDLE_PTR new_object) -{ - return_val_if_fail (new_object, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_CopyObject__invalid_handle (CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE object, - CK_ATTRIBUTE_PTR template, - CK_ULONG count, - CK_OBJECT_HANDLE_PTR new_object) -{ - return_val_if_fail (new_object, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - - -CK_RV -mock_C_DestroyObject__invalid_handle (CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE object) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_GetObjectSize__invalid_handle (CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE object, - CK_ULONG_PTR size) -{ - return_val_if_fail (size, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_GetAttributeValue__invalid_handle (CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE object, - CK_ATTRIBUTE_PTR template, - CK_ULONG count) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_SetAttributeValue__invalid_handle (CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE object, - CK_ATTRIBUTE_PTR template, - CK_ULONG count) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_FindObjectsInit__invalid_handle (CK_SESSION_HANDLE session, - CK_ATTRIBUTE_PTR template, - CK_ULONG count) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_FindObjects__invalid_handle (CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE_PTR objects, - CK_ULONG max_count, - CK_ULONG_PTR count) -{ - return_val_if_fail (count, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_FindObjectsFinal__invalid_handle (CK_SESSION_HANDLE session) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_EncryptInit__invalid_handle (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_Encrypt__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR data, CK_ULONG data_len, - CK_BYTE_PTR encrypted_data, - CK_ULONG_PTR encrypted_data_len) -{ - return_val_if_fail (encrypted_data_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_EncryptUpdate__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR part, - CK_ULONG part_len, - CK_BYTE_PTR encrypted_part, - CK_ULONG_PTR encrypted_part_len) -{ - return_val_if_fail (encrypted_part_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_EncryptFinal__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR last_part, - CK_ULONG_PTR last_part_len) -{ - return_val_if_fail (last_part_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_DecryptInit__invalid_handle (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_Decrypt__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR enc_data, - CK_ULONG enc_data_len, - CK_BYTE_PTR data, - CK_ULONG_PTR data_len) -{ - return_val_if_fail (data_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_DecryptUpdate__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR enc_part, - CK_ULONG enc_part_len, - CK_BYTE_PTR part, - CK_ULONG_PTR part_len) -{ - return_val_if_fail (part_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_DecryptFinal__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR last_part, - CK_ULONG_PTR last_part_len) -{ - return_val_if_fail (last_part_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_DigestInit__invalid_handle (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_Digest__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR data, - CK_ULONG data_len, - CK_BYTE_PTR digest, - CK_ULONG_PTR digest_len) -{ - return_val_if_fail (digest_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_DigestUpdate__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR part, - CK_ULONG part_len) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_DigestKey__invalid_handle (CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE key) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_DigestFinal__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR digest, - CK_ULONG_PTR digest_len) -{ - return_val_if_fail (digest_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_SignInit__invalid_handle (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_Sign__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR data, - CK_ULONG data_len, - CK_BYTE_PTR signature, - CK_ULONG_PTR signature_len) -{ - return_val_if_fail (signature_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_SignUpdate__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR part, - CK_ULONG part_len) -{ - return_val_if_fail (part_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_SignFinal__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR signature, - CK_ULONG_PTR signature_len) -{ - return_val_if_fail (signature_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_SignRecoverInit__invalid_handle (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_SignRecover__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR data, - CK_ULONG data_len, - CK_BYTE_PTR signature, - CK_ULONG_PTR signature_len) -{ - return_val_if_fail (signature_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_VerifyInit__invalid_handle (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_Verify__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR data, - CK_ULONG data_len, - CK_BYTE_PTR signature, - CK_ULONG signature_len) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_VerifyUpdate__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR part, - CK_ULONG part_len) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_VerifyFinal__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR signature, - CK_ULONG signature_len) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_VerifyRecoverInit__invalid_handle (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_VerifyRecover__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR signature, - CK_ULONG signature_len, - CK_BYTE_PTR data, - CK_ULONG_PTR data_len) -{ - return_val_if_fail (data_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_DigestEncryptUpdate__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR part, - CK_ULONG part_len, CK_BYTE_PTR enc_part, - CK_ULONG_PTR enc_part_len) -{ - return_val_if_fail (enc_part_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_DecryptDigestUpdate__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR enc_part, - CK_ULONG enc_part_len, - CK_BYTE_PTR part, - CK_ULONG_PTR part_len) -{ - return_val_if_fail (part_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_SignEncryptUpdate__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR part, - CK_ULONG part_len, - CK_BYTE_PTR enc_part, - CK_ULONG_PTR enc_part_len) -{ - return_val_if_fail (enc_part_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_DecryptVerifyUpdate__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR enc_part, - CK_ULONG enc_part_len, - CK_BYTE_PTR part, - CK_ULONG_PTR part_len) -{ - return_val_if_fail (part_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_GenerateKey__invalid_handle (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_ATTRIBUTE_PTR template, - CK_ULONG count, - CK_OBJECT_HANDLE_PTR key) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_GenerateKeyPair__invalid_handle (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_ATTRIBUTE_PTR pub_template, - CK_ULONG pub_count, - CK_ATTRIBUTE_PTR priv_template, - CK_ULONG priv_count, - CK_OBJECT_HANDLE_PTR pub_key, - CK_OBJECT_HANDLE_PTR priv_key) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_WrapKey__invalid_handle (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE wrapping_key, - CK_OBJECT_HANDLE key, - CK_BYTE_PTR wrapped_key, - CK_ULONG_PTR wrapped_key_len) -{ - return_val_if_fail (wrapped_key_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_UnwrapKey__invalid_handle (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE unwrapping_key, - CK_BYTE_PTR wrapped_key, - CK_ULONG wrapped_key_len, - CK_ATTRIBUTE_PTR template, - CK_ULONG count, - CK_OBJECT_HANDLE_PTR key) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_DeriveKey__invalid_handle (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE base_key, - CK_ATTRIBUTE_PTR template, - CK_ULONG count, - CK_OBJECT_HANDLE_PTR key) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_SeedRandom__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR seed, - CK_ULONG seed_len) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_GenerateRandom__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR random_data, - CK_ULONG random_len) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_FUNCTION_LIST mock_module_no_slots = { - { CRYPTOKI_VERSION_MAJOR, CRYPTOKI_VERSION_MINOR }, /* version */ - mock_C_Initialize, - mock_C_Finalize, - mock_C_GetInfo, - mock_C_GetFunctionList, - mock_C_GetSlotList__no_tokens, - mock_C_GetSlotInfo__invalid_slotid, - mock_C_GetTokenInfo__invalid_slotid, - mock_C_GetMechanismList__invalid_slotid, - mock_C_GetMechanismInfo__invalid_slotid, - mock_C_InitToken__invalid_slotid, - mock_C_InitPIN__invalid_handle, - mock_C_SetPIN__invalid_handle, - mock_C_OpenSession__invalid_slotid, - mock_C_CloseSession__invalid_handle, - mock_C_CloseAllSessions__invalid_slotid, - mock_C_GetSessionInfo__invalid_handle, - mock_C_GetOperationState__invalid_handle, - mock_C_SetOperationState__invalid_handle, - mock_C_Login__invalid_handle, - mock_C_Logout__invalid_handle, - mock_C_CreateObject__invalid_handle, - mock_C_CopyObject__invalid_handle, - mock_C_DestroyObject__invalid_handle, - mock_C_GetObjectSize__invalid_handle, - mock_C_GetAttributeValue__invalid_handle, - mock_C_SetAttributeValue__invalid_handle, - mock_C_FindObjectsInit__invalid_handle, - mock_C_FindObjects__invalid_handle, - mock_C_FindObjectsFinal__invalid_handle, - mock_C_EncryptInit__invalid_handle, - mock_C_Encrypt__invalid_handle, - mock_C_EncryptUpdate__invalid_handle, - mock_C_EncryptFinal__invalid_handle, - mock_C_DecryptInit__invalid_handle, - mock_C_Decrypt__invalid_handle, - mock_C_DecryptUpdate__invalid_handle, - mock_C_DecryptFinal__invalid_handle, - mock_C_DigestInit__invalid_handle, - mock_C_Digest__invalid_handle, - mock_C_DigestUpdate__invalid_handle, - mock_C_DigestKey__invalid_handle, - mock_C_DigestFinal__invalid_handle, - mock_C_SignInit__invalid_handle, - mock_C_Sign__invalid_handle, - mock_C_SignUpdate__invalid_handle, - mock_C_SignFinal__invalid_handle, - mock_C_SignRecoverInit__invalid_handle, - mock_C_SignRecover__invalid_handle, - mock_C_VerifyInit__invalid_handle, - mock_C_Verify__invalid_handle, - mock_C_VerifyUpdate__invalid_handle, - mock_C_VerifyFinal__invalid_handle, - mock_C_VerifyRecoverInit__invalid_handle, - mock_C_VerifyRecover__invalid_handle, - mock_C_DigestEncryptUpdate__invalid_handle, - mock_C_DecryptDigestUpdate__invalid_handle, - mock_C_SignEncryptUpdate__invalid_handle, - mock_C_DecryptVerifyUpdate__invalid_handle, - mock_C_GenerateKey__invalid_handle, - mock_C_GenerateKeyPair__invalid_handle, - mock_C_WrapKey__invalid_handle, - mock_C_UnwrapKey__invalid_handle, - mock_C_DeriveKey__invalid_handle, - mock_C_SeedRandom__invalid_handle, - mock_C_GenerateRandom__invalid_handle, - mock_C_GetFunctionStatus__not_parallel, - mock_C_CancelFunction__not_parallel, - mock_C_WaitForSlotEvent__no_event, -}; - -void -mock_module_init (void) -{ - static int initialized = 0; - if (!initialized) { - _p11_mutex_init (&init_mutex); - initialized = 1; - } -} diff --git a/tests/mock-module.h b/tests/mock-module.h deleted file mode 100644 index 9557f70..0000000 --- a/tests/mock-module.h +++ /dev/null @@ -1,337 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* mock-module.c - a mock PKCS#11 module - - Copyright (C) 2011 Collabora Ltd. - - The Gnome Keyring Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The Gnome Keyring Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with the Gnome Library; see the file COPYING.LIB. If not, - write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. - - Author: Stef Walter -*/ - -#ifndef __MOCK_MODULE_H__ -#define __MOCK_MODULE_H__ - -#include "pkcs11.h" - -extern CK_FUNCTION_LIST mock_module_no_slots; - -void mock_module_init (void); - -CK_RV mock_C_Initialize (CK_VOID_PTR init_args); - -CK_RV mock_C_Finalize (CK_VOID_PTR reserved); - -CK_RV mock_C_GetInfo (CK_INFO_PTR info); - -CK_RV mock_C_GetFunctionList (CK_FUNCTION_LIST_PTR_PTR list); - -CK_RV mock_C_GetSlotList__no_tokens (CK_BBOOL token_present, - CK_SLOT_ID_PTR slot_list, - CK_ULONG_PTR count); - -CK_RV mock_C_GetSlotInfo__invalid_slotid (CK_SLOT_ID id, - CK_SLOT_INFO_PTR info); - -CK_RV mock_C_GetTokenInfo__invalid_slotid (CK_SLOT_ID id, - CK_TOKEN_INFO_PTR info); - -CK_RV mock_C_GetMechanismList__invalid_slotid (CK_SLOT_ID id, - CK_MECHANISM_TYPE_PTR mechanism_list, - CK_ULONG_PTR count); - -CK_RV mock_C_GetMechanismInfo__invalid_slotid (CK_SLOT_ID id, - CK_MECHANISM_TYPE type, - CK_MECHANISM_INFO_PTR info); - -CK_RV mock_C_InitToken__invalid_slotid (CK_SLOT_ID id, - CK_UTF8CHAR_PTR pin, - CK_ULONG pin_len, - CK_UTF8CHAR_PTR label); - -CK_RV mock_C_WaitForSlotEvent__no_event (CK_FLAGS flags, - CK_SLOT_ID_PTR slot, - CK_VOID_PTR reserved); - -CK_RV mock_C_OpenSession__invalid_slotid (CK_SLOT_ID id, - CK_FLAGS flags, - CK_VOID_PTR user_data, - CK_NOTIFY callback, - CK_SESSION_HANDLE_PTR session); - -CK_RV mock_C_CloseSession__invalid_handle (CK_SESSION_HANDLE session); - -CK_RV mock_C_CloseAllSessions__invalid_slotid (CK_SLOT_ID id); - -CK_RV mock_C_GetFunctionStatus__not_parallel (CK_SESSION_HANDLE session); - -CK_RV mock_C_CancelFunction__not_parallel (CK_SESSION_HANDLE session); - -CK_RV mock_C_GetSessionInfo__invalid_handle (CK_SESSION_HANDLE session, - CK_SESSION_INFO_PTR info); - -CK_RV mock_C_InitPIN__invalid_handle (CK_SESSION_HANDLE session, - CK_UTF8CHAR_PTR pin, - CK_ULONG pin_len); - -CK_RV mock_C_SetPIN__invalid_handle (CK_SESSION_HANDLE session, - CK_UTF8CHAR_PTR old_pin, - CK_ULONG old_pin_len, - CK_UTF8CHAR_PTR new_pin, - CK_ULONG new_pin_len); - -CK_RV mock_C_GetOperationState__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR operation_state, - CK_ULONG_PTR operation_state_len); - -CK_RV mock_C_SetOperationState__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR operation_state, - CK_ULONG operation_state_len, - CK_OBJECT_HANDLE encryption_key, - CK_OBJECT_HANDLE authentication_key); - -CK_RV mock_C_Login__invalid_handle (CK_SESSION_HANDLE session, - CK_USER_TYPE user_type, - CK_UTF8CHAR_PTR pin, - CK_ULONG pin_len); - -CK_RV mock_C_Logout__invalid_handle (CK_SESSION_HANDLE session); - -CK_RV mock_C_CreateObject__invalid_handle (CK_SESSION_HANDLE session, - CK_ATTRIBUTE_PTR template, - CK_ULONG count, - CK_OBJECT_HANDLE_PTR new_object); - -CK_RV mock_C_CopyObject__invalid_handle (CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE object, - CK_ATTRIBUTE_PTR template, - CK_ULONG count, - CK_OBJECT_HANDLE_PTR new_object); - -CK_RV mock_C_DestroyObject__invalid_handle (CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE object); - -CK_RV mock_C_GetObjectSize__invalid_handle (CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE object, - CK_ULONG_PTR size); - -CK_RV mock_C_GetAttributeValue__invalid_handle (CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE object, - CK_ATTRIBUTE_PTR template, - CK_ULONG count); - -CK_RV mock_C_SetAttributeValue__invalid_handle (CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE object, - CK_ATTRIBUTE_PTR template, - CK_ULONG count); - -CK_RV mock_C_FindObjectsInit__invalid_handle (CK_SESSION_HANDLE session, - CK_ATTRIBUTE_PTR template, - CK_ULONG count); - -CK_RV mock_C_FindObjects__invalid_handle (CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE_PTR objects, - CK_ULONG max_count, - CK_ULONG_PTR count); - -CK_RV mock_C_FindObjectsFinal__invalid_handle (CK_SESSION_HANDLE session); - -CK_RV mock_C_EncryptInit__invalid_handle (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key); - -CK_RV mock_C_Encrypt__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR data, - CK_ULONG data_len, - CK_BYTE_PTR encrypted_data, - CK_ULONG_PTR encrypted_data_len); - -CK_RV mock_C_EncryptUpdate__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR part, - CK_ULONG part_len, - CK_BYTE_PTR encrypted_part, - CK_ULONG_PTR encrypted_part_len); - -CK_RV mock_C_EncryptFinal__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR last_part, - CK_ULONG_PTR last_part_len); - -CK_RV mock_C_DecryptInit__invalid_handle (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key); - -CK_RV mock_C_Decrypt__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR enc_data, - CK_ULONG enc_data_len, - CK_BYTE_PTR data, - CK_ULONG_PTR data_len); - -CK_RV mock_C_DecryptUpdate__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR enc_part, - CK_ULONG enc_part_len, - CK_BYTE_PTR part, - CK_ULONG_PTR part_len); - -CK_RV mock_C_DecryptFinal__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR last_part, - CK_ULONG_PTR last_part_len); - -CK_RV mock_C_DigestInit__invalid_handle (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism); - -CK_RV mock_C_Digest__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR data, - CK_ULONG data_len, - CK_BYTE_PTR digest, - CK_ULONG_PTR digest_len); - -CK_RV mock_C_DigestUpdate__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR part, - CK_ULONG part_len); - -CK_RV mock_C_DigestKey__invalid_handle (CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE key); - -CK_RV mock_C_DigestFinal__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR digest, - CK_ULONG_PTR digest_len); - -CK_RV mock_C_SignInit__invalid_handle (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key); - -CK_RV mock_C_Sign__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR data, - CK_ULONG data_len, - CK_BYTE_PTR signature, - CK_ULONG_PTR signature_len); - -CK_RV mock_C_SignUpdate__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR part, - CK_ULONG part_len); - -CK_RV mock_C_SignFinal__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR signature, - CK_ULONG_PTR signature_len); - -CK_RV mock_C_SignRecoverInit__invalid_handle (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key); - -CK_RV mock_C_SignRecover__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR data, - CK_ULONG data_len, - CK_BYTE_PTR signature, - CK_ULONG_PTR signature_len); - -CK_RV mock_C_VerifyInit__invalid_handle (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key); - -CK_RV mock_C_Verify__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR data, - CK_ULONG data_len, - CK_BYTE_PTR signature, - CK_ULONG signature_len); - -CK_RV mock_C_VerifyUpdate__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR part, - CK_ULONG part_len); - -CK_RV mock_C_VerifyFinal__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR signature, - CK_ULONG signature_len); - -CK_RV mock_C_VerifyRecoverInit__invalid_handle (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key); - -CK_RV mock_C_VerifyRecover__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR signature, - CK_ULONG signature_len, - CK_BYTE_PTR data, - CK_ULONG_PTR data_len); - -CK_RV mock_C_DigestEncryptUpdate__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR part, - CK_ULONG part_len, - CK_BYTE_PTR enc_part, - CK_ULONG_PTR enc_part_len); - -CK_RV mock_C_DecryptDigestUpdate__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR enc_part, - CK_ULONG enc_part_len, - CK_BYTE_PTR part, - CK_ULONG_PTR part_len); - -CK_RV mock_C_SignEncryptUpdate__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR part, - CK_ULONG part_len, - CK_BYTE_PTR enc_part, - CK_ULONG_PTR enc_part_len); - -CK_RV mock_C_DecryptVerifyUpdate__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR enc_part, - CK_ULONG enc_part_len, - CK_BYTE_PTR part, - CK_ULONG_PTR part_len); - -CK_RV mock_C_GenerateKey__invalid_handle (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_ATTRIBUTE_PTR template, - CK_ULONG count, - CK_OBJECT_HANDLE_PTR key); - -CK_RV mock_C_GenerateKeyPair__invalid_handle (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_ATTRIBUTE_PTR pub_template, - CK_ULONG pub_count, - CK_ATTRIBUTE_PTR priv_template, - CK_ULONG priv_count, - CK_OBJECT_HANDLE_PTR pub_key, - CK_OBJECT_HANDLE_PTR priv_key); - -CK_RV mock_C_WrapKey__invalid_handle (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE wrapping_key, - CK_OBJECT_HANDLE key, - CK_BYTE_PTR wrapped_key, - CK_ULONG_PTR wrapped_key_len); - -CK_RV mock_C_UnwrapKey__invalid_handle (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE unwrapping_key, - CK_BYTE_PTR wrapped_key, - CK_ULONG wrapped_key_len, - CK_ATTRIBUTE_PTR template, - CK_ULONG count, - CK_OBJECT_HANDLE_PTR key); - -CK_RV mock_C_DeriveKey__invalid_handle (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE base_key, - CK_ATTRIBUTE_PTR template, - CK_ULONG count, - CK_OBJECT_HANDLE_PTR key); - -CK_RV mock_C_SeedRandom__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR seed, - CK_ULONG seed_len); - -CK_RV mock_C_GenerateRandom__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR random_data, - CK_ULONG random_len); - -#endif /* __MOCK_MODULE_H__ */ diff --git a/tests/pin-test.c b/tests/pin-test.c deleted file mode 100644 index 7e7dda8..0000000 --- a/tests/pin-test.c +++ /dev/null @@ -1,323 +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 - */ - -#include "config.h" -#include "CuTest.h" - -#include -#include -#include -#include -#include - -#include "p11-kit/pin.h" -#include "p11-kit/private.h" - -static P11KitPin * -callback_one (const char *pin_source, P11KitUri *pin_uri, const char *pin_description, - P11KitPinFlags pin_flags, void *callback_data) -{ - int *data = callback_data; - assert (*data == 33); - return p11_kit_pin_new_for_buffer ((unsigned char*)strdup ("one"), 3, free); -} - -static P11KitPin* -callback_other (const char *pin_source, P11KitUri *pin_uri, const char *pin_description, - P11KitPinFlags pin_flags, void *callback_data) -{ - char *data = callback_data; - return p11_kit_pin_new_for_string (data); -} - -static void -destroy_data (void *callback_data) -{ - int *data = callback_data; - (*data)++; -} - -static void -test_pin_register_unregister (CuTest *tc) -{ - int data = 33; - - p11_kit_pin_register_callback ("/the/pin_source", callback_one, - &data, destroy_data); - - p11_kit_pin_unregister_callback ("/the/pin_source", callback_one, - &data); - - CuAssertIntEquals (tc, 34, data); -} - -static void -test_pin_read (CuTest *tc) -{ - P11KitUri *uri; - P11KitPin *pin; - int data = 33; - size_t length; - const unsigned char *ptr; - - p11_kit_pin_register_callback ("/the/pin_source", callback_one, - &data, destroy_data); - - uri = p11_kit_uri_new (); - pin = p11_kit_pin_request ("/the/pin_source", uri, "The token", - P11_KIT_PIN_FLAGS_USER_LOGIN); - p11_kit_uri_free (uri); - - CuAssertPtrNotNull (tc, pin); - ptr = p11_kit_pin_get_value (pin, &length); - CuAssertIntEquals (tc, 3, length); - CuAssertTrue (tc, memcmp (ptr, "one", 3) == 0); - - p11_kit_pin_unregister_callback ("/the/pin_source", callback_one, - &data); - - p11_kit_pin_ref (pin); - p11_kit_pin_unref (pin); -} - -static void -test_pin_read_no_match (CuTest *tc) -{ - P11KitUri *uri; - P11KitPin *pin; - - uri = p11_kit_uri_new (); - pin = p11_kit_pin_request ("/the/pin_source", uri, "The token", - P11_KIT_PIN_FLAGS_USER_LOGIN); - p11_kit_uri_free (uri); - - CuAssertPtrEquals (tc, NULL, pin); -} - -static void -test_pin_register_duplicate (CuTest *tc) -{ - P11KitUri *uri; - P11KitPin *pin; - char *value = "secret"; - int data = 33; - size_t length; - const unsigned char *ptr; - - uri = p11_kit_uri_new (); - - p11_kit_pin_register_callback ("/the/pin_source", callback_one, - &data, destroy_data); - - p11_kit_pin_register_callback ("/the/pin_source", callback_other, - value, NULL); - - pin = p11_kit_pin_request ("/the/pin_source", uri, "The token", - P11_KIT_PIN_FLAGS_USER_LOGIN); - - CuAssertPtrNotNull (tc, pin); - ptr = p11_kit_pin_get_value (pin, &length); - CuAssertIntEquals (tc, 6, length); - CuAssertTrue (tc, memcmp (ptr, "secret", length) == 0); - p11_kit_pin_unref (pin); - - p11_kit_pin_unregister_callback ("/the/pin_source", callback_other, - value); - - pin = p11_kit_pin_request ("/the/pin_source", uri, "The token", - P11_KIT_PIN_FLAGS_USER_LOGIN); - - CuAssertPtrNotNull (tc, pin); - ptr = p11_kit_pin_get_value (pin, &length); - CuAssertIntEquals (tc, 3, length); - CuAssertTrue (tc, memcmp (ptr, "one", length) == 0); - p11_kit_pin_unref (pin); - - p11_kit_pin_unregister_callback ("/the/pin_source", callback_one, - &data); - - pin = p11_kit_pin_request ("/the/pin_source", uri, "The token", - P11_KIT_PIN_FLAGS_USER_LOGIN); - - CuAssertPtrEquals (tc, NULL, pin); - - p11_kit_uri_free (uri); -} - -static void -test_pin_register_fallback (CuTest *tc) -{ - char *value = "secret"; - P11KitUri *uri; - P11KitPin *pin; - int data = 33; - size_t length; - const unsigned char *ptr; - - uri = p11_kit_uri_new (); - - p11_kit_pin_register_callback (P11_KIT_PIN_FALLBACK, callback_one, - &data, destroy_data); - - pin = p11_kit_pin_request ("/the/pin_source", uri, "The token", - P11_KIT_PIN_FLAGS_USER_LOGIN); - - CuAssertPtrNotNull (tc, pin); - ptr = p11_kit_pin_get_value (pin, &length); - CuAssertIntEquals (tc, 3, length); - CuAssertTrue (tc, memcmp (ptr, "one", length) == 0); - p11_kit_pin_unref (pin); - - p11_kit_pin_register_callback ("/the/pin_source", callback_other, - value, NULL); - - pin = p11_kit_pin_request ("/the/pin_source", uri, "The token", - P11_KIT_PIN_FLAGS_USER_LOGIN); - - CuAssertPtrNotNull (tc, pin); - ptr = p11_kit_pin_get_value (pin, &length); - CuAssertIntEquals (tc, 6, length); - CuAssertTrue (tc, memcmp (ptr, "secret", length) == 0); - p11_kit_pin_unref (pin); - - p11_kit_pin_unregister_callback ("/the/pin_source", callback_other, - value); - - p11_kit_pin_unregister_callback (P11_KIT_PIN_FALLBACK, callback_one, - &data); - - p11_kit_uri_free (uri); -} - -static void -test_pin_file (CuTest *tc) -{ - P11KitUri *uri; - P11KitPin *pin; - size_t length; - const unsigned char *ptr; - - uri = p11_kit_uri_new (); - - p11_kit_pin_register_callback (P11_KIT_PIN_FALLBACK, p11_kit_pin_file_callback, - NULL, NULL); - - pin = p11_kit_pin_request (SRCDIR "/files/test-pinfile", uri, "The token", - P11_KIT_PIN_FLAGS_USER_LOGIN); - - CuAssertPtrNotNull (tc, pin); - ptr = p11_kit_pin_get_value (pin, &length); - CuAssertIntEquals (tc, 12, length); - CuAssertTrue (tc, memcmp (ptr, "yogabbagabba", length) == 0); - p11_kit_pin_unref (pin); - - pin = p11_kit_pin_request (SRCDIR "/files/nonexistant", uri, "The token", - P11_KIT_PIN_FLAGS_USER_LOGIN); - - CuAssertPtrEquals (tc, NULL, pin); - - p11_kit_pin_unregister_callback (P11_KIT_PIN_FALLBACK, p11_kit_pin_file_callback, - NULL); - - p11_kit_uri_free (uri); -} - -static void -test_pin_file_large (CuTest *tc) -{ - P11KitUri *uri; - P11KitPin *pin; - int error; - - uri = p11_kit_uri_new (); - - p11_kit_pin_register_callback (P11_KIT_PIN_FALLBACK, p11_kit_pin_file_callback, - NULL, NULL); - - pin = p11_kit_pin_request (SRCDIR "/files/test-pinfile-large", uri, "The token", - P11_KIT_PIN_FLAGS_USER_LOGIN); - - error = errno; - CuAssertPtrEquals (tc, NULL, pin); - CuAssertIntEquals (tc, EFBIG, error); - - p11_kit_pin_unregister_callback (P11_KIT_PIN_FALLBACK, p11_kit_pin_file_callback, - NULL); - - p11_kit_uri_free (uri); -} - -static void -test_pin_ref_unref (CuTest *tc) -{ - P11KitPin *pin; - P11KitPin *check; - - pin = p11_kit_pin_new_for_string ("crack of lies"); - - check = p11_kit_pin_ref (pin); - CuAssertPtrEquals (tc, pin, check); - - p11_kit_pin_unref (pin); - p11_kit_pin_unref (check); -} - -int -main (void) -{ - CuString *output = CuStringNew (); - CuSuite* suite = CuSuiteNew (); - int ret; - - _p11_library_init (); - - SUITE_ADD_TEST (suite, test_pin_register_unregister); - SUITE_ADD_TEST (suite, test_pin_read); - SUITE_ADD_TEST (suite, test_pin_read_no_match); - SUITE_ADD_TEST (suite, test_pin_register_duplicate); - SUITE_ADD_TEST (suite, test_pin_register_fallback); - SUITE_ADD_TEST (suite, test_pin_file); - SUITE_ADD_TEST (suite, test_pin_file_large); - SUITE_ADD_TEST (suite, test_pin_ref_unref); - - CuSuiteRun (suite); - CuSuiteSummary (suite, output); - CuSuiteDetails (suite, output); - printf ("%s\n", output->buffer); - ret = suite->failCount; - CuSuiteDelete (suite); - CuStringDelete (output); - - return ret; -} diff --git a/tests/print-messages.c b/tests/print-messages.c deleted file mode 100644 index 5870ad1..0000000 --- a/tests/print-messages.c +++ /dev/null @@ -1,137 +0,0 @@ -/* - * Copyright (c) 2011, Collabora Ltd. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met); - * - * * Redistributions of source code must retain the above - * copyright notice, this list of conditions and the - * following disclaimer. - * * Redistributions in binary form must reproduce the - * above copyright notice, this list of conditions and - * the following disclaimer in the documentation and/or - * other materials provided with the distribution. - * * The names of contributors to this software may not be - * used to endorse or promote products derived from this - * software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF - * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH - * DAMAGE. - * - * Author: Stef Walter - */ - -#include "config.h" - -#include -#include -#include -#include - -#include "p11-kit.h" - -int -main (int argc, char *argv[]) -{ - if (argc != 1) { - fprintf (stderr, "usage: print-messages\n"); - exit (2); - } - - #define X(x) printf ("%s: %s\n", #x, p11_kit_strerror (x)) - X(CKR_CANCEL); - X(CKR_FUNCTION_CANCELED); - X(CKR_HOST_MEMORY); - X(CKR_SLOT_ID_INVALID); - X(CKR_GENERAL_ERROR); - X(CKR_FUNCTION_FAILED); - X(CKR_ARGUMENTS_BAD); - X(CKR_NEED_TO_CREATE_THREADS); - X(CKR_CANT_LOCK); - X(CKR_ATTRIBUTE_READ_ONLY); - X(CKR_ATTRIBUTE_SENSITIVE); - X(CKR_ATTRIBUTE_TYPE_INVALID); - X(CKR_ATTRIBUTE_VALUE_INVALID); - X(CKR_DATA_INVALID); - X(CKR_DATA_LEN_RANGE); - X(CKR_DEVICE_ERROR); - X(CKR_DEVICE_MEMORY); - X(CKR_DEVICE_REMOVED); - X(CKR_ENCRYPTED_DATA_INVALID); - X(CKR_ENCRYPTED_DATA_LEN_RANGE); - X(CKR_FUNCTION_NOT_SUPPORTED); - X(CKR_KEY_HANDLE_INVALID); - X(CKR_KEY_SIZE_RANGE); - X(CKR_KEY_TYPE_INCONSISTENT); - X(CKR_KEY_NOT_NEEDED); - X(CKR_KEY_CHANGED); - X(CKR_KEY_NEEDED); - X(CKR_KEY_INDIGESTIBLE); - X(CKR_KEY_FUNCTION_NOT_PERMITTED); - X(CKR_KEY_NOT_WRAPPABLE); - X(CKR_KEY_UNEXTRACTABLE); - X(CKR_MECHANISM_INVALID); - X(CKR_MECHANISM_PARAM_INVALID); - X(CKR_OBJECT_HANDLE_INVALID); - X(CKR_OPERATION_ACTIVE); - X(CKR_OPERATION_NOT_INITIALIZED); - X(CKR_PIN_INCORRECT); - X(CKR_PIN_INVALID); - X(CKR_PIN_LEN_RANGE); - X(CKR_PIN_EXPIRED); - X(CKR_PIN_LOCKED); - X(CKR_SESSION_CLOSED); - X(CKR_SESSION_COUNT); - X(CKR_SESSION_HANDLE_INVALID); - X(CKR_SESSION_READ_ONLY); - X(CKR_SESSION_EXISTS); - X(CKR_SESSION_READ_ONLY_EXISTS); - X(CKR_SESSION_READ_WRITE_SO_EXISTS); - X(CKR_SIGNATURE_INVALID); - X(CKR_SIGNATURE_LEN_RANGE); - X(CKR_TEMPLATE_INCOMPLETE); - X(CKR_TEMPLATE_INCONSISTENT); - X(CKR_TOKEN_NOT_PRESENT); - X(CKR_TOKEN_NOT_RECOGNIZED); - X(CKR_TOKEN_WRITE_PROTECTED); - X(CKR_UNWRAPPING_KEY_HANDLE_INVALID); - X(CKR_UNWRAPPING_KEY_SIZE_RANGE); - X(CKR_UNWRAPPING_KEY_TYPE_INCONSISTENT); - X(CKR_USER_ALREADY_LOGGED_IN); - X(CKR_USER_NOT_LOGGED_IN); - X(CKR_USER_PIN_NOT_INITIALIZED); - X(CKR_USER_TYPE_INVALID); - X(CKR_USER_ANOTHER_ALREADY_LOGGED_IN); - X(CKR_USER_TOO_MANY_TYPES); - X(CKR_WRAPPED_KEY_INVALID); - X(CKR_WRAPPED_KEY_LEN_RANGE); - X(CKR_WRAPPING_KEY_HANDLE_INVALID); - X(CKR_WRAPPING_KEY_SIZE_RANGE); - X(CKR_WRAPPING_KEY_TYPE_INCONSISTENT); - X(CKR_RANDOM_SEED_NOT_SUPPORTED); - X(CKR_RANDOM_NO_RNG); - X(CKR_DOMAIN_PARAMS_INVALID); - X(CKR_BUFFER_TOO_SMALL); - X(CKR_SAVED_STATE_INVALID); - X(CKR_INFORMATION_SENSITIVE); - X(CKR_STATE_UNSAVEABLE); - X(CKR_CRYPTOKI_NOT_INITIALIZED); - X(CKR_CRYPTOKI_ALREADY_INITIALIZED); - X(CKR_MUTEX_BAD); - X(CKR_MUTEX_NOT_LOCKED); - X(CKR_FUNCTION_REJECTED); - #undef X - - return 0; -} diff --git a/tests/progname-test.c b/tests/progname-test.c deleted file mode 100644 index 1e42056..0000000 --- a/tests/progname-test.c +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Copyright (c) 2012 Stefan Walter - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * * Redistributions of source code must retain the above - * copyright notice, this list of conditions and the - * following disclaimer. - * * Redistributions in binary form must reproduce the - * above copyright notice, this list of conditions and - * the following disclaimer in the documentation and/or - * other materials provided with the distribution. - * * The names of contributors to this software may not be - * used to endorse or promote products derived from this - * software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF - * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH - * DAMAGE. - * - * Author: Stef Walter - */ - -#include "config.h" -#include "CuTest.h" - -#include -#include -#include -#include - -#include "p11-kit/uri.h" -#include "p11-kit/p11-kit.h" -#include "p11-kit/private.h" - -static void -test_progname_default (CuTest *tc) -{ - const char *progname; - - progname = _p11_get_progname_unlocked (); - CuAssertStrEquals (tc, "progname-test", progname); -} - -static void -test_progname_set (CuTest *tc) -{ - const char *progname; - - p11_kit_set_progname ("love-generation"); - - progname = _p11_get_progname_unlocked (); - CuAssertStrEquals (tc, "love-generation", progname); - - _p11_set_progname_unlocked (NULL); - - progname = _p11_get_progname_unlocked (); - CuAssertStrEquals (tc, "progname-test", progname); -} - -/* 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); -} - -int -main (void) -{ - CuString *output = CuStringNew (); - CuSuite* suite = CuSuiteNew (); - int ret; - - _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); - printf ("%s\n", output->buffer); - ret = suite->failCount; - CuSuiteDelete (suite); - CuStringDelete (output); - return ret; -} 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 - */ - -#include "config.h" -#include "CuTest.h" - -#include -#include -#include - -#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; -} diff --git a/tests/test-init.c b/tests/test-init.c deleted file mode 100644 index d872afa..0000000 --- a/tests/test-init.c +++ /dev/null @@ -1,319 +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 - */ - -#include "config.h" -#include "CuTest.h" - -#include - -#include -#include -#include -#include -#include -#include - -#include "p11-kit/p11-kit.h" -#include "p11-kit/util.h" - -#include "mock-module.h" - -CK_FUNCTION_LIST module; - -#ifdef OS_UNIX - -#include - -static CK_RV -mock_C_Initialize__with_fork (CK_VOID_PTR init_args) -{ - struct timespec ts = { 0, 100 * 1000 * 1000 }; - CK_RV rv; - pid_t child; - pid_t ret; - int status; - - rv = mock_C_Initialize (init_args); - assert (rv == CKR_OK); - - /* Fork during the initialization */ - child = fork (); - if (child == 0) { - nanosleep (&ts, NULL); - exit (66); - } - - ret = waitpid (child, &status, 0); - assert (ret == child); - assert (WIFEXITED (status)); - assert (WEXITSTATUS (status) == 66); - - return CKR_OK; -} - -static void -test_fork_initialization (CuTest *tc) -{ - CK_RV rv; - - /* Build up our own function list */ - memcpy (&module, &mock_module_no_slots, sizeof (CK_FUNCTION_LIST)); - module.C_Initialize = mock_C_Initialize__with_fork; - - rv = p11_kit_initialize_module (&module); - CuAssertTrue (tc, rv == CKR_OK); - - rv = p11_kit_finalize_module (&module); - CuAssertTrue (tc, rv == CKR_OK); -} - -#endif /* OS_UNIX */ - -static CK_RV -mock_C_Initialize__with_recursive (CK_VOID_PTR init_args) -{ - CK_RV rv; - - rv = mock_C_Initialize (init_args); - assert (rv == CKR_OK); - - /* Recursively initialize, this is broken */ - return p11_kit_initialize_module (&module); -} - -static void -test_recursive_initialization (CuTest *tc) -{ - CK_RV rv; - - /* Build up our own function list */ - memcpy (&module, &mock_module_no_slots, sizeof (CK_FUNCTION_LIST)); - module.C_Initialize = mock_C_Initialize__with_recursive; - - rv = p11_kit_initialize_module (&module); - CuAssertTrue (tc, rv == CKR_FUNCTION_FAILED); -} - -static mutex_t race_mutex; -static int initialization_count = 0; -static int finalization_count = 0; - -#include "private.h" - -static CK_RV -mock_C_Initialize__threaded_race (CK_VOID_PTR init_args) -{ - /* Atomically increment value */ - _p11_mutex_lock (&race_mutex); - initialization_count += 1; - _p11_mutex_unlock (&race_mutex); - - _p11_sleep_ms (100); - return CKR_OK; -} - -static CK_RV -mock_C_Finalize__threaded_race (CK_VOID_PTR reserved) -{ - /* Atomically increment value */ - _p11_mutex_lock (&race_mutex); - finalization_count += 1; - _p11_mutex_unlock (&race_mutex); - - _p11_sleep_ms (100); - return CKR_OK; -} - -static void * -initialization_thread (void *data) -{ - CuTest *tc = data; - CK_RV rv; - - rv = p11_kit_initialize_module (&module); - CuAssertTrue (tc, rv == CKR_OK); - - return tc; -} - -static void * -finalization_thread (void *data) -{ - CuTest *tc = data; - CK_RV rv; - - rv = p11_kit_finalize_module (&module); - CuAssertTrue (tc, rv == CKR_OK); - - return tc; -} - -static void -test_threaded_initialization (CuTest *tc) -{ - static const int num_threads = 2; - thread_t threads[num_threads]; - int ret; - int i; - - /* Build up our own function list */ - memcpy (&module, &mock_module_no_slots, sizeof (CK_FUNCTION_LIST)); - module.C_Initialize = mock_C_Initialize__threaded_race; - module.C_Finalize = mock_C_Finalize__threaded_race; - - initialization_count = 0; - finalization_count = 0; - - for (i = 0; i < num_threads; i++) { - ret = _p11_thread_create (&threads[i], initialization_thread, tc); - CuAssertIntEquals (tc, 0, ret); - CuAssertTrue (tc, threads[i] != 0); - } - - for (i = 0; i < num_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); - CuAssertIntEquals (tc, 0, ret); - CuAssertTrue (tc, threads[i] != 0); - } - - for (i = 0; i < num_threads; i++) { - ret = _p11_thread_join (threads[i]); - CuAssertIntEquals (tc, 0, ret); - threads[i] = 0; - } - - /* C_Initialize should have been called exactly once */ - CuAssertIntEquals (tc, 1, initialization_count); - CuAssertIntEquals (tc, 1, finalization_count); -} - -static CK_RV -mock_C_Initialize__test_mutexes (CK_VOID_PTR args) -{ - CK_C_INITIALIZE_ARGS_PTR init_args; - void *mutex = NULL; - CK_RV rv; - - assert (args != NULL); - init_args = args; - - rv = (init_args->CreateMutex) (&mutex); - assert (rv == CKR_OK); - - rv = (init_args->LockMutex) (mutex); - assert (rv == CKR_OK); - - rv = (init_args->UnlockMutex) (mutex); - assert (rv == CKR_OK); - - rv = (init_args->DestroyMutex) (mutex); - assert (rv == CKR_OK); - - return CKR_OK; -} - -static void -test_mutexes (CuTest *tc) -{ - CK_RV rv; - - /* Build up our own function list */ - memcpy (&module, &mock_module_no_slots, sizeof (CK_FUNCTION_LIST)); - module.C_Initialize = mock_C_Initialize__test_mutexes; - - rv = p11_kit_initialize_module (&module); - CuAssertTrue (tc, rv == CKR_OK); - - rv = p11_kit_finalize_module (&module); - CuAssertTrue (tc, rv == CKR_OK); -} - -static void -test_load_and_initialize (CuTest *tc) -{ - CK_FUNCTION_LIST_PTR module; - CK_INFO info; - CK_RV rv; - int ret; - - rv = p11_kit_load_initialize_module (BUILDDIR "/.libs/mock-one" SHLEXT, &module); - CuAssertTrue (tc, rv == CKR_OK); - CuAssertTrue (tc, module != NULL); - - rv = (module->C_GetInfo) (&info); - CuAssertTrue (tc, rv == CKR_OK); - - ret = memcmp (info.manufacturerID, "MOCK MANUFACTURER ", 32); - CuAssertTrue (tc, ret == 0); - - rv = p11_kit_finalize_module (module); - CuAssertTrue (tc, ret == CKR_OK); -} - -int -main (void) -{ - CuString *output = CuStringNew (); - CuSuite* suite = CuSuiteNew (); - int ret; - - _p11_mutex_init (&race_mutex); - mock_module_init (); - _p11_library_init (); - -#ifdef OS_UNIX - SUITE_ADD_TEST (suite, test_fork_initialization); -#endif - - SUITE_ADD_TEST (suite, test_recursive_initialization); - SUITE_ADD_TEST (suite, test_threaded_initialization); - SUITE_ADD_TEST (suite, test_mutexes); - SUITE_ADD_TEST (suite, test_load_and_initialize); - - CuSuiteRun (suite); - CuSuiteSummary (suite, output); - CuSuiteDetails (suite, output); - printf ("%s\n", output->buffer); - ret = suite->failCount; - CuSuiteDelete (suite); - CuStringDelete (output); - - return ret; -} diff --git a/tests/test-modules.c b/tests/test-modules.c deleted file mode 100644 index 74560d9..0000000 --- a/tests/test-modules.c +++ /dev/null @@ -1,245 +0,0 @@ -/* - * Copyright (c) 2012 Red Hat Inc - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * * Redistributions of source code must retain the above - * copyright notice, this list of conditions and the - * following disclaimer. - * * Redistributions in binary form must reproduce the - * above copyright notice, this list of conditions and - * the following disclaimer in the documentation and/or - * other materials provided with the distribution. - * * The names of contributors to this software may not be - * used to endorse or promote products derived from this - * software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF - * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH - * DAMAGE. - * - * Author: Stef Walter - */ - -#include "config.h" -#include "CuTest.h" - -#include -#include -#include -#include - -#include "p11-kit.h" -#include "private.h" -#include "hashmap.h" - -static CK_FUNCTION_LIST_PTR_PTR -initialize_and_get_modules (CuTest *tc) -{ - CK_FUNCTION_LIST_PTR_PTR modules; - CK_RV rv; - - rv = p11_kit_initialize_registered (); - CuAssertIntEquals (tc, CKR_OK, rv); - modules = p11_kit_registered_modules (); - CuAssertTrue (tc, modules != NULL && modules[0] != NULL); - - return modules; -} - -static void -finalize_and_free_modules (CuTest *tc, - CK_FUNCTION_LIST_PTR_PTR modules) -{ - CK_RV rv; - - free (modules); - rv = p11_kit_finalize_registered (); - CuAssertIntEquals (tc, CKR_OK, rv); -} - -static void -test_no_duplicates (CuTest *tc) -{ - CK_FUNCTION_LIST_PTR_PTR modules; - hashmap *paths; - hashmap *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); - - /* 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])) - CuAssert (tc, "found duplicate function list pointer", 0); - if (_p11_hash_get (paths, path)) - CuAssert (tc, "found duplicate path name", 0); - - if (!_p11_hash_set (funcs, modules[i], "")) - CuAssert (tc, "shouldn't be reached", 0); - if (!_p11_hash_set (paths, path, "")) - CuAssert (tc, "shouldn't be reached", 0); - } - - _p11_hash_free (paths); - _p11_hash_free (funcs); - finalize_and_free_modules (tc, modules); -} - -static CK_FUNCTION_LIST_PTR -lookup_module_with_name (CuTest *tc, - CK_FUNCTION_LIST_PTR_PTR modules, - const char *name) -{ - CK_FUNCTION_LIST_PTR match = NULL; - CK_FUNCTION_LIST_PTR module; - char *module_name; - int i; - - for (i = 0; match == NULL && modules[i] != NULL; i++) { - module_name = p11_kit_registered_module_to_name (modules[i]); - CuAssertPtrNotNull (tc, module_name); - if (strcmp (module_name, name) == 0) - match = modules[i]; - free (module_name); - } - - /* - * As a side effect, we should check that the results of this function - * matches the above search. - */ - module = p11_kit_registered_name_to_module (name); - CuAssert(tc, "different result from p11_kit_registered_name_to_module()", - module == match); - - return match; -} - -static void -test_disable (CuTest *tc) -{ - CK_FUNCTION_LIST_PTR_PTR modules; - - /* - * The module four should be present, as we don't match any prognames - * that it has disabled. - */ - - modules = initialize_and_get_modules (tc); - CuAssertTrue (tc, lookup_module_with_name (tc, modules, "four") != NULL); - finalize_and_free_modules (tc, modules); - - /* - * The module two shouldn't have been loaded, because in its config - * file we have: - * - * disable-in: test-disable - */ - - p11_kit_set_progname ("test-disable"); - - modules = initialize_and_get_modules (tc); - CuAssertTrue (tc, lookup_module_with_name (tc, modules, "four") == NULL); - finalize_and_free_modules (tc, modules); - - p11_kit_set_progname (NULL); -} - -static void -test_disable_later (CuTest *tc) -{ - CK_FUNCTION_LIST_PTR_PTR modules; - CK_RV rv; - - /* - * The module two shouldn't be matched, because in its config - * file we have: - * - * disable-in: test-disable - */ - - rv = p11_kit_initialize_registered (); - CuAssertIntEquals (tc, CKR_OK, rv); - - p11_kit_set_progname ("test-disable"); - - modules = p11_kit_registered_modules (); - CuAssertTrue (tc, modules != NULL && modules[0] != NULL); - - CuAssertTrue (tc, lookup_module_with_name (tc, modules, "two") == NULL); - finalize_and_free_modules (tc, modules); - - p11_kit_set_progname (NULL); -} - -static void -test_enable (CuTest *tc) -{ - CK_FUNCTION_LIST_PTR_PTR modules; - - /* - * The module three should not be present, as we don't match the current - * program. - */ - - modules = initialize_and_get_modules (tc); - CuAssertTrue (tc, lookup_module_with_name (tc, modules, "three") == NULL); - finalize_and_free_modules (tc, modules); - - /* - * The module three should be loaded here , because in its config - * file we have: - * - * enable-in: test-enable - */ - - p11_kit_set_progname ("test-enable"); - - modules = initialize_and_get_modules (tc); - CuAssertTrue (tc, lookup_module_with_name (tc, modules, "three") != NULL); - finalize_and_free_modules (tc, modules); - - p11_kit_set_progname (NULL); -} - -int -main (void) -{ - CuString *output = CuStringNew (); - CuSuite* suite = CuSuiteNew (); - int ret; - - _p11_library_init (); - - SUITE_ADD_TEST (suite, test_no_duplicates); - SUITE_ADD_TEST (suite, test_disable); - SUITE_ADD_TEST (suite, test_disable_later); - SUITE_ADD_TEST (suite, test_enable); - - p11_kit_be_quiet (); - - CuSuiteRun (suite); - CuSuiteSummary (suite, output); - CuSuiteDetails (suite, output); - printf ("%s\n", output->buffer); - ret = suite->failCount; - CuSuiteDelete (suite); - CuStringDelete (output); - return ret; -} diff --git a/tests/uri-test.c b/tests/uri-test.c deleted file mode 100644 index 4780e4e..0000000 --- a/tests/uri-test.c +++ /dev/null @@ -1,1221 +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 - */ - -#include "config.h" -#include "CuTest.h" - -#include -#include -#include -#include - -#include "p11-kit/uri.h" -#include "p11-kit/private.h" - -static int -is_module_empty (P11KitUri *uri) -{ - CK_INFO_PTR info = p11_kit_uri_get_module_info (uri); - return (info->libraryDescription[0] == 0 && - info->manufacturerID[0] == 0 && - info->libraryVersion.major == (CK_BYTE)-1 && - info->libraryVersion.minor == (CK_BYTE)-1); -} - -static int -is_token_empty (P11KitUri *uri) -{ - CK_TOKEN_INFO_PTR token = p11_kit_uri_get_token_info (uri); - return (token->serialNumber[0] == 0 && - token->manufacturerID[0] == 0 && - token->label[0] == 0 && - token->model[0] == 0); -} - -static int -are_attributes_empty (P11KitUri *uri) -{ - return (p11_kit_uri_get_attribute (uri, CKA_LABEL) == NULL && - p11_kit_uri_get_attribute (uri, CKA_ID) == NULL && - p11_kit_uri_get_attribute (uri, CKA_CLASS) == NULL); -} - -static void -test_uri_parse (CuTest *tc) -{ - P11KitUri *uri; - int ret; - - uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); - - ret = p11_kit_uri_parse ("pkcs11:", P11_KIT_URI_FOR_MODULE, uri); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - - CuAssertTrue (tc, is_module_empty (uri)); - CuAssertTrue (tc, is_token_empty (uri)); - CuAssertTrue (tc, are_attributes_empty (uri)); - - p11_kit_uri_free (uri); -} - -static void -test_uri_parse_bad_scheme (CuTest *tc) -{ - P11KitUri *uri; - int ret; - - uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); - - ret = p11_kit_uri_parse ("http:\\example.com\test", P11_KIT_URI_FOR_ANY, uri); - CuAssertIntEquals (tc, P11_KIT_URI_BAD_SCHEME, ret); - - p11_kit_uri_free (uri); -} - -static void -test_uri_parse_with_label (CuTest *tc) -{ - CK_ATTRIBUTE_PTR attr; - P11KitUri *uri; - int ret; - - uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); - - ret = p11_kit_uri_parse ("pkcs11:object=Test%20Label", P11_KIT_URI_FOR_ANY, uri); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - - CuAssertTrue (tc, is_module_empty (uri)); - CuAssertTrue (tc, is_token_empty (uri)); - - attr = p11_kit_uri_get_attribute (uri, CKA_LABEL); - CuAssertPtrNotNull (tc, attr); - CuAssertTrue (tc, attr->ulValueLen == strlen ("Test Label")); - CuAssertTrue (tc, memcmp (attr->pValue, "Test Label", attr->ulValueLen) == 0); - - p11_kit_uri_free (uri); -} - -static void -test_uri_parse_with_label_and_klass (CuTest *tc) -{ - CK_ATTRIBUTE_PTR attr; - P11KitUri *uri; - int ret; - - uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); - - ret = p11_kit_uri_parse ("pkcs11:object=Test%20Label;object-type=cert", P11_KIT_URI_FOR_ANY, uri); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - - attr = p11_kit_uri_get_attribute (uri, CKA_LABEL); - CuAssertPtrNotNull (tc, attr); - CuAssertTrue (tc, attr->ulValueLen == strlen ("Test Label")); - CuAssertTrue (tc, memcmp (attr->pValue, "Test Label", attr->ulValueLen) == 0); - - attr = p11_kit_uri_get_attribute (uri, CKA_CLASS); - CuAssertPtrNotNull (tc, attr); - CuAssertTrue (tc, attr->ulValueLen == sizeof (CK_OBJECT_CLASS)); - CuAssertTrue (tc, *((CK_OBJECT_CLASS_PTR)attr->pValue) == CKO_CERTIFICATE); - - p11_kit_uri_free (uri); -} - -static void -test_uri_parse_with_id (CuTest *tc) -{ - CK_ATTRIBUTE_PTR attr; - P11KitUri *uri; - int ret; - - uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); - - ret = p11_kit_uri_parse ("pkcs11:id=%54%45%53%54%00", P11_KIT_URI_FOR_OBJECT, uri); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - - /* Note that there's a NULL in the attribute (end) */ - attr = p11_kit_uri_get_attribute (uri, CKA_ID); - CuAssertPtrNotNull (tc, attr); - CuAssertTrue (tc, attr->ulValueLen == 5); - CuAssertTrue (tc, memcmp (attr->pValue, "TEST", 5) == 0); - - - p11_kit_uri_free (uri); -} - -static void -test_uri_parse_with_bad_string_encoding (CuTest *tc) -{ - P11KitUri *uri; - int ret; - - uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); - - ret = p11_kit_uri_parse ("pkcs11:object=Test%", P11_KIT_URI_FOR_OBJECT, uri); - CuAssertIntEquals (tc, P11_KIT_URI_BAD_ENCODING, ret); - - p11_kit_uri_free (uri); -} - -static void -test_uri_parse_with_bad_hex_encoding (CuTest *tc) -{ - P11KitUri *uri; - int ret; - - uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); - - ret = p11_kit_uri_parse ("pkcs11:object=T%xxest", P11_KIT_URI_FOR_OBJECT, uri); - CuAssertIntEquals (tc, P11_KIT_URI_BAD_ENCODING, ret); - - p11_kit_uri_free (uri); -} - -static int -is_space_string (CK_UTF8CHAR_PTR string, CK_ULONG size, const char *check) -{ - size_t i, len = strlen (check); - if (len > size) - return 0; - if (memcmp (string, check, len) != 0) - return 0; - for (i = len; i < size; ++i) - if (string[i] != ' ') - return 0; - return 1; -} - -static void -test_uri_parse_with_token (CuTest *tc) -{ - P11KitUri *uri = NULL; - CK_TOKEN_INFO_PTR token; - int ret; - - uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); - - ret = p11_kit_uri_parse ("pkcs11:token=Token%20Label;serial=3333;model=Deluxe;manufacturer=Me", - P11_KIT_URI_FOR_TOKEN, uri); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - - token = p11_kit_uri_get_token_info (uri); - CuAssertTrue (tc, is_space_string (token->label, sizeof (token->label), "Token Label")); - CuAssertTrue (tc, is_space_string (token->serialNumber, sizeof (token->serialNumber), "3333")); - CuAssertTrue (tc, is_space_string (token->model, sizeof (token->model), "Deluxe")); - CuAssertTrue (tc, is_space_string (token->manufacturerID, sizeof (token->manufacturerID), "Me")); - - p11_kit_uri_free (uri); -} - -static void -test_uri_parse_with_token_bad_encoding (CuTest *tc) -{ - P11KitUri *uri; - int ret; - - uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); - - ret = p11_kit_uri_parse ("pkcs11:token=Token%", P11_KIT_URI_FOR_TOKEN, uri); - CuAssertIntEquals (tc, P11_KIT_URI_BAD_ENCODING, ret); - - p11_kit_uri_free (uri); -} - -static void -test_uri_parse_with_bad_syntax (CuTest *tc) -{ - P11KitUri *uri; - int ret; - - uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); - - ret = p11_kit_uri_parse ("pkcs11:token", P11_KIT_URI_FOR_ANY, uri); - CuAssertIntEquals (tc, P11_KIT_URI_BAD_SYNTAX, ret); - - p11_kit_uri_free (uri); -} - -static void -test_uri_parse_with_spaces (CuTest *tc) -{ - P11KitUri *uri = NULL; - CK_INFO_PTR info; - int ret; - - uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); - - ret = p11_kit_uri_parse ("pkc\ns11: lib rary-desc\rrip \n tion =The%20Library;\n\n\nlibrary-manufacturer=\rMe", - P11_KIT_URI_FOR_MODULE, uri); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - - info = p11_kit_uri_get_module_info (uri); - - CuAssertTrue (tc, is_space_string (info->manufacturerID, sizeof (info->manufacturerID), "Me")); - CuAssertTrue (tc, is_space_string (info->libraryDescription, sizeof (info->libraryDescription), "The Library")); - - p11_kit_uri_free (uri); -} - - -static void -test_uri_parse_with_library (CuTest *tc) -{ - P11KitUri *uri = NULL; - CK_INFO_PTR info; - int ret; - - uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); - - ret = p11_kit_uri_parse ("pkcs11:library-description=The%20Library;library-manufacturer=Me", - P11_KIT_URI_FOR_MODULE, uri); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - - info = p11_kit_uri_get_module_info (uri); - - CuAssertTrue (tc, is_space_string (info->manufacturerID, sizeof (info->manufacturerID), "Me")); - CuAssertTrue (tc, is_space_string (info->libraryDescription, sizeof (info->libraryDescription), "The Library")); - - p11_kit_uri_free (uri); -} - -static void -test_uri_parse_with_library_bad_encoding (CuTest *tc) -{ - P11KitUri *uri; - int ret; - - uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); - - ret = p11_kit_uri_parse ("pkcs11:library-description=Library%", P11_KIT_URI_FOR_MODULE, uri); - CuAssertIntEquals (tc, P11_KIT_URI_BAD_ENCODING, ret); - - p11_kit_uri_free (uri); -} - -static void -test_uri_build_empty (CuTest *tc) -{ - P11KitUri *uri; - char *string; - int ret; - - uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); - - ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - CuAssertStrEquals (tc, "pkcs11:", string); - free (string); - - p11_kit_uri_free (uri); -} - -static void -set_space_string (CK_BYTE_PTR buffer, CK_ULONG length, const char *string) -{ - size_t len = strlen (string); - assert (len <= length); - memset (buffer, ' ', length); - memcpy (buffer, string, len); -} - -static void -test_uri_build_with_token_info (CuTest *tc) -{ - char *string = NULL; - P11KitUri *uri; - P11KitUri *check; - CK_TOKEN_INFO_PTR token; - int ret; - - uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); - - token = p11_kit_uri_get_token_info (uri); - set_space_string (token->label, sizeof (token->label), "The Label"); - set_space_string (token->serialNumber, sizeof (token->serialNumber), "44444"); - set_space_string (token->manufacturerID, sizeof (token->manufacturerID), "Me"); - set_space_string (token->model, sizeof (token->model), "Deluxe"); - - ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - CuAssertPtrNotNull (tc, string); - - check = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, check); - - ret = p11_kit_uri_parse (string, P11_KIT_URI_FOR_TOKEN, check); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - - p11_kit_uri_match_token_info (check, p11_kit_uri_get_token_info (uri)); - - p11_kit_uri_free (uri); - p11_kit_uri_free (check); - - CuAssertTrue (tc, strstr (string, "token=The%20Label") != NULL); - CuAssertTrue (tc, strstr (string, "serial=44444") != NULL); - CuAssertTrue (tc, strstr (string, "manufacturer=Me") != NULL); - CuAssertTrue (tc, strstr (string, "model=Deluxe") != NULL); - - free (string); -} - -static void -test_uri_build_with_token_null_info (CuTest *tc) -{ - char *string = NULL; - P11KitUri *uri; - CK_TOKEN_INFO_PTR token; - int ret; - - uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); - - token = p11_kit_uri_get_token_info (uri); - set_space_string (token->label, sizeof (token->label), "The Label"); - - ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - - CuAssertTrue (tc, strstr (string, "token=The%20Label") != NULL); - CuAssertTrue (tc, strstr (string, "serial=") == NULL); - - free (string); - p11_kit_uri_free (uri); -} - -static void -test_uri_build_with_token_empty_info (CuTest *tc) -{ - char *string = NULL; - P11KitUri *uri; - CK_TOKEN_INFO_PTR token; - int ret; - - uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); - - token = p11_kit_uri_get_token_info (uri); - set_space_string (token->label, sizeof (token->label), ""); - set_space_string (token->serialNumber, sizeof (token->serialNumber), ""); - - ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - - CuAssertTrue (tc, strstr (string, "token=") != NULL); - CuAssertTrue (tc, strstr (string, "serial=") != NULL); - - free (string); - p11_kit_uri_free (uri); -} - -static void -test_uri_build_with_attributes (CuTest *tc) -{ - char *string = NULL; - P11KitUri *uri; - P11KitUri *check; - CK_OBJECT_CLASS klass; - CK_ATTRIBUTE_PTR attr; - CK_ATTRIBUTE at; - int ret; - - uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); - - at.type = CKA_LABEL; - at.pValue = "The Label"; - at.ulValueLen = 9; - ret = p11_kit_uri_set_attribute (uri, &at); - - at.type = CKA_ID; - at.pValue = "HELLO"; - at.ulValueLen = 5; - ret = p11_kit_uri_set_attribute (uri, &at); - - klass = CKO_DATA; - at.type = CKA_CLASS; - at.pValue = &klass; - at.ulValueLen = sizeof (klass); - ret = p11_kit_uri_set_attribute (uri, &at); - - ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - - check = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, check); - - ret = p11_kit_uri_parse (string, P11_KIT_URI_FOR_ANY, check); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - - attr = p11_kit_uri_get_attribute (check, CKA_LABEL); - CuAssertPtrNotNull (tc, attr); - CuAssertTrue (tc, attr->ulValueLen == 9); - CuAssertTrue (tc, memcmp (attr->pValue, "The Label", attr->ulValueLen) == 0); - - attr = p11_kit_uri_get_attribute (check, CKA_CLASS); - CuAssertPtrNotNull (tc, attr); - CuAssertTrue (tc, attr->ulValueLen == sizeof (klass)); - CuAssertTrue (tc, *((CK_OBJECT_CLASS_PTR)attr->pValue) == klass); - - attr = p11_kit_uri_get_attribute (check, CKA_ID); - CuAssertPtrNotNull (tc, attr); - CuAssertTrue (tc, attr->ulValueLen == 5); - CuAssertTrue (tc, memcmp (attr->pValue, "HELLO", attr->ulValueLen) == 0); - - p11_kit_uri_free (check); - - CuAssertTrue (tc, strstr (string, "object=The%20Label") != NULL); - CuAssertTrue (tc, strstr (string, "object-type=data") != NULL); - CuAssertTrue (tc, strstr (string, "id=%48%45%4c%4c%4f") != NULL); - - free (string); - p11_kit_uri_free (uri); -} - -static void -test_uri_parse_private_key (CuTest *tc) -{ - P11KitUri *uri; - CK_ATTRIBUTE_PTR attr; - int ret; - - uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); - - ret = p11_kit_uri_parse ("pkcs11:object-type=private", P11_KIT_URI_FOR_OBJECT, uri); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - - attr = p11_kit_uri_get_attribute (uri, CKA_CLASS); - CuAssertPtrNotNull (tc, attr); - CuAssertTrue (tc, attr->ulValueLen == sizeof (CK_OBJECT_CLASS)); - CuAssertTrue (tc, *((CK_OBJECT_CLASS_PTR)attr->pValue) == CKO_PRIVATE_KEY); - - p11_kit_uri_free (uri); -} - -static void -test_uri_parse_secret_key (CuTest *tc) -{ - P11KitUri *uri; - CK_ATTRIBUTE_PTR attr; - int ret; - - uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); - - ret = p11_kit_uri_parse ("pkcs11:object-type=secret-key", P11_KIT_URI_FOR_OBJECT, uri); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - - attr = p11_kit_uri_get_attribute (uri, CKA_CLASS); - CuAssertPtrNotNull (tc, attr); - CuAssertTrue (tc, attr->ulValueLen == sizeof (CK_OBJECT_CLASS)); - CuAssertTrue (tc, *((CK_OBJECT_CLASS_PTR)attr->pValue) == CKO_SECRET_KEY); - - p11_kit_uri_free (uri); -} - -static void -test_uri_parse_library_version (CuTest *tc) -{ - P11KitUri *uri; - CK_INFO_PTR info; - int ret; - - uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); - - ret = p11_kit_uri_parse ("pkcs11:library-version=2.101", P11_KIT_URI_FOR_MODULE_WITH_VERSION, uri); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - - info = p11_kit_uri_get_module_info (uri); - CuAssertIntEquals (tc, 2, info->libraryVersion.major); - CuAssertIntEquals (tc, 101, info->libraryVersion.minor); - - ret = p11_kit_uri_parse ("pkcs11:library-version=23", P11_KIT_URI_FOR_MODULE_WITH_VERSION, uri); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - - info = p11_kit_uri_get_module_info (uri); - CuAssertIntEquals (tc, 23, info->libraryVersion.major); - CuAssertIntEquals (tc, 0, info->libraryVersion.minor); - - ret = p11_kit_uri_parse ("pkcs11:library-version=23.", P11_KIT_URI_FOR_MODULE_WITH_VERSION, uri); - CuAssertIntEquals (tc, P11_KIT_URI_BAD_VERSION, ret); - - ret = p11_kit_uri_parse ("pkcs11:library-version=a.a", P11_KIT_URI_FOR_MODULE_WITH_VERSION, uri); - CuAssertIntEquals (tc, P11_KIT_URI_BAD_VERSION, ret); - - ret = p11_kit_uri_parse ("pkcs11:library-version=.23", P11_KIT_URI_FOR_MODULE_WITH_VERSION, uri); - CuAssertIntEquals (tc, P11_KIT_URI_BAD_VERSION, ret); - - ret = p11_kit_uri_parse ("pkcs11:library-version=1000", P11_KIT_URI_FOR_MODULE_WITH_VERSION, uri); - CuAssertIntEquals (tc, P11_KIT_URI_BAD_VERSION, ret); - - ret = p11_kit_uri_parse ("pkcs11:library-version=2.1000", P11_KIT_URI_FOR_MODULE_WITH_VERSION, uri); - CuAssertIntEquals (tc, P11_KIT_URI_BAD_VERSION, ret); - - p11_kit_uri_free (uri); -} - -static void -test_uri_parse_parse_unknown_object_type (CuTest *tc) -{ - P11KitUri *uri; - CK_ATTRIBUTE_PTR attr; - int ret; - - uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); - - ret = p11_kit_uri_parse ("pkcs11:object-type=unknown", P11_KIT_URI_FOR_OBJECT, uri); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - - attr = p11_kit_uri_get_attribute (uri, CKA_CLASS); - CuAssertPtrEquals (tc, NULL, attr); - - p11_kit_uri_free (uri); -} - -static void -test_uri_parse_unrecognized (CuTest *tc) -{ - P11KitUri *uri; - int ret; - - uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); - - ret = p11_kit_uri_parse ("pkcs11:x-blah=some-value", P11_KIT_URI_FOR_ANY, uri); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - - ret = p11_kit_uri_any_unrecognized (uri); - CuAssertIntEquals (tc, 1, ret); - - p11_kit_uri_free (uri); -} - -static void -test_uri_parse_too_long_is_unrecognized (CuTest *tc) -{ - P11KitUri *uri; - int ret; - - uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); - - ret = p11_kit_uri_parse ("pkcs11:model=a-value-that-is-too-long-for-the-field-that-it-goes-with", - P11_KIT_URI_FOR_ANY, uri); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - - ret = p11_kit_uri_any_unrecognized (uri); - CuAssertIntEquals (tc, 1, ret); - - p11_kit_uri_free (uri); -} - - - -static void -test_uri_build_object_type_cert (CuTest *tc) -{ - CK_ATTRIBUTE attr; - CK_OBJECT_CLASS klass; - P11KitUri *uri; - char *string; - int ret; - - uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); - - klass = CKO_CERTIFICATE; - attr.type = CKA_CLASS; - attr.pValue = &klass; - attr.ulValueLen = sizeof (klass); - p11_kit_uri_set_attribute (uri, &attr); - - ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - CuAssertTrue (tc, strstr (string, "object-type=cert") != NULL); - - p11_kit_uri_free (uri); - free (string); -} - -static void -test_uri_build_object_type_private (CuTest *tc) -{ - CK_ATTRIBUTE attr; - CK_OBJECT_CLASS klass; - P11KitUri *uri; - char *string; - int ret; - - uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); - - klass = CKO_PRIVATE_KEY; - attr.type = CKA_CLASS; - attr.pValue = &klass; - attr.ulValueLen = sizeof (klass); - p11_kit_uri_set_attribute (uri, &attr); - - ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - CuAssertTrue (tc, strstr (string, "object-type=private") != NULL); - - p11_kit_uri_free (uri); - free (string); -} - -static void -test_uri_build_object_type_public (CuTest *tc) -{ - CK_ATTRIBUTE attr; - CK_OBJECT_CLASS klass; - P11KitUri *uri; - char *string; - int ret; - - uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); - - klass = CKO_PUBLIC_KEY; - attr.type = CKA_CLASS; - attr.pValue = &klass; - attr.ulValueLen = sizeof (klass); - p11_kit_uri_set_attribute (uri, &attr); - - ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - CuAssertTrue (tc, strstr (string, "object-type=public") != NULL); - - p11_kit_uri_free (uri); - free (string); -} - -static void -test_uri_build_object_type_secret (CuTest *tc) -{ - CK_ATTRIBUTE attr; - CK_OBJECT_CLASS klass; - P11KitUri *uri; - char *string; - int ret; - - uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); - - klass = CKO_SECRET_KEY; - attr.type = CKA_CLASS; - attr.pValue = &klass; - attr.ulValueLen = sizeof (klass); - p11_kit_uri_set_attribute (uri, &attr); - - ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - CuAssertTrue (tc, strstr (string, "object-type=secret-key") != NULL); - - p11_kit_uri_free (uri); - free (string); -} - -static void -test_uri_build_with_library (CuTest *tc) -{ - CK_INFO_PTR info; - P11KitUri *uri; - char *string; - int ret; - - uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); - - info = p11_kit_uri_get_module_info (uri); - set_space_string (info->libraryDescription, sizeof (info->libraryDescription), "The Description"); - - ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - CuAssertTrue (tc, strstr (string, "library-description=The%20Description") != NULL); - - p11_kit_uri_free (uri); - free (string); -} - -static void -test_uri_build_library_version (CuTest *tc) -{ - CK_INFO_PTR info; - P11KitUri *uri; - char *string; - int ret; - - uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); - - info = p11_kit_uri_get_module_info (uri); - info->libraryVersion.major = 2; - info->libraryVersion.minor = 10; - - ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - CuAssertTrue (tc, strstr (string, "library-version=2.10") != NULL); - - p11_kit_uri_free (uri); - free (string); -} - -static void -test_uri_get_set_unrecognized (CuTest *tc) -{ - P11KitUri *uri; - int ret; - - uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); - - ret = p11_kit_uri_any_unrecognized (uri); - CuAssertIntEquals (tc, 0, ret); - - p11_kit_uri_set_unrecognized (uri, 1); - - ret = p11_kit_uri_any_unrecognized (uri); - CuAssertIntEquals (tc, 1, ret); - - p11_kit_uri_set_unrecognized (uri, 0); - - ret = p11_kit_uri_any_unrecognized (uri); - CuAssertIntEquals (tc, 0, ret); - - p11_kit_uri_free (uri); -} - -static void -test_uri_match_token (CuTest *tc) -{ - CK_TOKEN_INFO token; - P11KitUri *uri; - int ret; - - uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); - - ret = p11_kit_uri_parse ("pkcs11:model=Giselle", P11_KIT_URI_FOR_ANY, uri); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - - set_space_string (token.label, sizeof (token.label), "A label"); - set_space_string (token.model, sizeof (token.model), "Giselle"); - - ret = p11_kit_uri_match_token_info (uri, &token); - CuAssertIntEquals (tc, 1, ret); - - set_space_string (token.label, sizeof (token.label), "Another label"); - - ret = p11_kit_uri_match_token_info (uri, &token); - CuAssertIntEquals (tc, 1, ret); - - set_space_string (token.model, sizeof (token.model), "Zoolander"); - - ret = p11_kit_uri_match_token_info (uri, &token); - CuAssertIntEquals (tc, 0, ret); - - p11_kit_uri_set_unrecognized (uri, 1); - - ret = p11_kit_uri_match_token_info (uri, &token); - CuAssertIntEquals (tc, 0, ret); - - p11_kit_uri_free (uri); -} - -static void -test_uri_match_module (CuTest *tc) -{ - CK_INFO info; - P11KitUri *uri; - int ret; - - uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); - - ret = p11_kit_uri_parse ("pkcs11:library-description=Quiet", P11_KIT_URI_FOR_ANY, uri); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - - set_space_string (info.libraryDescription, sizeof (info.libraryDescription), "Quiet"); - set_space_string (info.manufacturerID, sizeof (info.manufacturerID), "Someone"); - - ret = p11_kit_uri_match_module_info (uri, &info); - CuAssertIntEquals (tc, 1, ret); - - set_space_string (info.manufacturerID, sizeof (info.manufacturerID), "Someone else"); - - ret = p11_kit_uri_match_module_info (uri, &info); - CuAssertIntEquals (tc, 1, ret); - - set_space_string (info.libraryDescription, sizeof (info.libraryDescription), "Leise"); - - ret = p11_kit_uri_match_module_info (uri, &info); - CuAssertIntEquals (tc, 0, ret); - - p11_kit_uri_set_unrecognized (uri, 1); - - ret = p11_kit_uri_match_module_info (uri, &info); - CuAssertIntEquals (tc, 0, ret); - - p11_kit_uri_free (uri); -} - -static void -test_uri_match_attributes (CuTest *tc) -{ - CK_ATTRIBUTE attrs[4]; - CK_OBJECT_CLASS klass; - P11KitUri *uri; - int ret; - - attrs[0].type = CKA_ID; - attrs[0].pValue = "Blah"; - attrs[0].ulValueLen = 4; - - attrs[1].type = CKA_LABEL; - attrs[1].pValue = "Junk"; - attrs[1].ulValueLen = 4; - - attrs[2].type = CKA_COLOR; - attrs[2].pValue = "blue"; - attrs[2].ulValueLen = 4; - - klass = CKO_DATA; - attrs[3].type = CKA_CLASS; - attrs[3].pValue = &klass; - attrs[3].ulValueLen = sizeof (klass); - - uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); - - ret = p11_kit_uri_parse ("pkcs11:object=Fancy;id=Blah;object-type=data", P11_KIT_URI_FOR_ANY, uri); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - - ret = p11_kit_uri_match_attributes (uri, attrs, 4); - CuAssertIntEquals (tc, 0, ret); - - attrs[1].pValue = "Fancy"; - attrs[1].ulValueLen = 5; - - ret = p11_kit_uri_match_attributes (uri, attrs, 4); - CuAssertIntEquals (tc, 1, ret); - - p11_kit_uri_clear_attribute (uri, CKA_CLASS); - - ret = p11_kit_uri_match_attributes (uri, attrs, 4); - CuAssertIntEquals (tc, 1, ret); - - attrs[2].pValue = "pink"; - - ret = p11_kit_uri_match_attributes (uri, attrs, 4); - CuAssertIntEquals (tc, 1, ret); - - p11_kit_uri_set_unrecognized (uri, 1); - - ret = p11_kit_uri_match_attributes (uri, attrs, 4); - CuAssertIntEquals (tc, 0, ret); - - p11_kit_uri_free (uri); -} - -static void -test_uri_get_set_attribute (CuTest *tc) -{ - CK_ATTRIBUTE attr; - CK_ATTRIBUTE_PTR ptr; - P11KitUri *uri; - int ret; - - uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); - - ptr = p11_kit_uri_get_attribute (uri, CKA_LABEL); - CuAssertPtrEquals (tc, NULL, ptr); - - ret = p11_kit_uri_clear_attribute (uri, CKA_LABEL); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - - ret = p11_kit_uri_clear_attribute (uri, CKA_COLOR); - CuAssertIntEquals (tc, P11_KIT_URI_NOT_FOUND, ret); - - attr.type = CKA_LABEL; - attr.pValue = "Test"; - attr.ulValueLen = 4; - - ret = p11_kit_uri_set_attribute (uri, &attr); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - - attr.type = CKA_COLOR; - ret = p11_kit_uri_set_attribute (uri, &attr); - CuAssertIntEquals (tc, P11_KIT_URI_NOT_FOUND, ret); - - ptr = p11_kit_uri_get_attribute (uri, CKA_COLOR); - CuAssertPtrEquals (tc, NULL, ptr); - - ptr = p11_kit_uri_get_attribute (uri, CKA_LABEL); - CuAssertPtrNotNull (tc, ptr); - - CuAssertTrue (tc, ptr->type == CKA_LABEL); - CuAssertTrue (tc, ptr->ulValueLen == 4); - CuAssertTrue (tc, memcmp (ptr->pValue, "Test", 4) == 0); - - ret = p11_kit_uri_clear_attribute (uri, CKA_LABEL); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - - ptr = p11_kit_uri_get_attribute (uri, CKA_LABEL); - CuAssertPtrEquals (tc, NULL, ptr); - - p11_kit_uri_free (uri); -} - -static void -test_uri_get_set_attributes (CuTest *tc) -{ - CK_ATTRIBUTE_PTR attrs; - CK_OBJECT_CLASS klass; - CK_ATTRIBUTE attr; - CK_ULONG n_attrs; - P11KitUri *uri; - int ret; - - uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); - - attrs = p11_kit_uri_get_attributes (uri, &n_attrs); - CuAssertPtrNotNull (tc, attrs); - CuAssertIntEquals (tc, 0, n_attrs); - - attr.type = CKA_LABEL; - attr.pValue = "Test"; - attr.ulValueLen = 4; - - ret = p11_kit_uri_set_attribute (uri, &attr); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - - attrs = p11_kit_uri_get_attributes (uri, &n_attrs); - CuAssertPtrNotNull (tc, attrs); - CuAssertIntEquals (tc, 1, n_attrs); - CuAssertTrue (tc, attrs[0].type == CKA_LABEL); - CuAssertTrue (tc, attrs[0].ulValueLen == 4); - CuAssertTrue (tc, memcmp (attrs[0].pValue, "Test", 4) == 0); - - attr.type = CKA_LABEL; - attr.pValue = "Kablooey"; - attr.ulValueLen = 8; - - ret = p11_kit_uri_set_attribute (uri, &attr); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - - attrs = p11_kit_uri_get_attributes (uri, &n_attrs); - CuAssertPtrNotNull (tc, attrs); - CuAssertIntEquals (tc, 1, n_attrs); - CuAssertTrue (tc, attrs[0].type == CKA_LABEL); - CuAssertTrue (tc, attrs[0].ulValueLen == 8); - CuAssertTrue (tc, memcmp (attrs[0].pValue, "Kablooey", 8) == 0); - - klass = CKO_DATA; - attr.type = CKA_CLASS; - attr.pValue = &klass; - attr.ulValueLen = sizeof (klass); - - ret = p11_kit_uri_set_attribute (uri, &attr); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - - attrs = p11_kit_uri_get_attributes (uri, &n_attrs); - CuAssertPtrNotNull (tc, attrs); - CuAssertIntEquals (tc, 2, n_attrs); - CuAssertTrue (tc, attrs[0].type == CKA_LABEL); - CuAssertTrue (tc, attrs[0].ulValueLen == 8); - CuAssertTrue (tc, memcmp (attrs[0].pValue, "Kablooey", 8) == 0); - CuAssertTrue (tc, attrs[1].type == CKA_CLASS); - CuAssertTrue (tc, attrs[1].ulValueLen == sizeof (klass)); - CuAssertTrue (tc, memcmp (attrs[1].pValue, &klass, sizeof (klass)) == 0); - - ret = p11_kit_uri_clear_attribute (uri, CKA_LABEL); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - - attrs = p11_kit_uri_get_attributes (uri, &n_attrs); - CuAssertPtrNotNull (tc, attrs); - CuAssertIntEquals (tc, 1, n_attrs); - CuAssertTrue (tc, attrs[0].type == CKA_CLASS); - CuAssertTrue (tc, attrs[0].ulValueLen == sizeof (klass)); - CuAssertTrue (tc, memcmp (attrs[0].pValue, &klass, sizeof (klass)) == 0); - - attr.type = CKA_LABEL; - attr.pValue = "Three"; - attr.ulValueLen = 5; - - ret = p11_kit_uri_set_attributes (uri, &attr, 1); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - - attrs = p11_kit_uri_get_attributes (uri, &n_attrs); - CuAssertPtrNotNull (tc, attrs); - CuAssertIntEquals (tc, 1, n_attrs); - CuAssertTrue (tc, attrs[0].type == CKA_LABEL); - CuAssertTrue (tc, attrs[0].ulValueLen == 5); - CuAssertTrue (tc, memcmp (attrs[0].pValue, "Three", 5) == 0); - - p11_kit_uri_clear_attributes (uri); - - attrs = p11_kit_uri_get_attributes (uri, &n_attrs); - CuAssertPtrNotNull (tc, attrs); - CuAssertIntEquals (tc, 0, n_attrs); - - p11_kit_uri_free (uri); -} -static void -test_uri_pin_source (CuTest *tc) -{ - P11KitUri *uri; - const char *pin_source; - char *string; - int ret; - - uri = p11_kit_uri_new (); - CuAssertPtrNotNull (tc, uri); - - p11_kit_uri_set_pin_source (uri, "|my-pin-source"); - - pin_source = p11_kit_uri_get_pin_source (uri); - CuAssertStrEquals (tc, "|my-pin-source", pin_source); - - pin_source = p11_kit_uri_get_pinfile (uri); - CuAssertStrEquals (tc, "|my-pin-source", pin_source); - - p11_kit_uri_set_pinfile (uri, "|my-pin-file"); - - pin_source = p11_kit_uri_get_pin_source (uri); - CuAssertStrEquals (tc, "|my-pin-file", pin_source); - - ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - CuAssertTrue (tc, strstr (string, "pin-source=%7cmy-pin-file") != NULL); - free (string); - - ret = p11_kit_uri_parse ("pkcs11:pin-source=blah%2Fblah", P11_KIT_URI_FOR_ANY, uri); - CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); - - pin_source = p11_kit_uri_get_pin_source (uri); - CuAssertStrEquals (tc, "blah/blah", pin_source); - - p11_kit_uri_free (uri); -} - -static void -test_uri_free_null (CuTest *tc) -{ - p11_kit_uri_free (NULL); -} - -static void -test_uri_message (CuTest *tc) -{ - CuAssertTrue (tc, p11_kit_uri_message (P11_KIT_URI_OK) == NULL); - CuAssertPtrNotNull (tc, p11_kit_uri_message (P11_KIT_URI_UNEXPECTED)); - CuAssertPtrNotNull (tc, p11_kit_uri_message (-555555)); -} - -int -main (void) -{ - CuString *output = CuStringNew (); - CuSuite* suite = CuSuiteNew (); - int ret; - - _p11_library_init (); - - SUITE_ADD_TEST (suite, test_uri_parse); - SUITE_ADD_TEST (suite, test_uri_parse_bad_scheme); - SUITE_ADD_TEST (suite, test_uri_parse_with_label); - SUITE_ADD_TEST (suite, test_uri_parse_with_label_and_klass); - SUITE_ADD_TEST (suite, test_uri_parse_with_id); - SUITE_ADD_TEST (suite, test_uri_parse_with_bad_string_encoding); - SUITE_ADD_TEST (suite, test_uri_parse_with_bad_hex_encoding); - SUITE_ADD_TEST (suite, test_uri_parse_with_token); - SUITE_ADD_TEST (suite, test_uri_parse_with_token_bad_encoding); - SUITE_ADD_TEST (suite, test_uri_parse_with_bad_syntax); - SUITE_ADD_TEST (suite, test_uri_parse_with_spaces); - SUITE_ADD_TEST (suite, test_uri_parse_with_library); - SUITE_ADD_TEST (suite, test_uri_parse_with_library_bad_encoding); - SUITE_ADD_TEST (suite, test_uri_build_empty); - SUITE_ADD_TEST (suite, test_uri_build_with_token_info); - SUITE_ADD_TEST (suite, test_uri_build_with_token_null_info); - SUITE_ADD_TEST (suite, test_uri_build_with_token_empty_info); - SUITE_ADD_TEST (suite, test_uri_build_with_attributes); - SUITE_ADD_TEST (suite, test_uri_parse_private_key); - SUITE_ADD_TEST (suite, test_uri_parse_secret_key); - SUITE_ADD_TEST (suite, test_uri_parse_library_version); - SUITE_ADD_TEST (suite, test_uri_parse_parse_unknown_object_type); - SUITE_ADD_TEST (suite, test_uri_parse_unrecognized); - SUITE_ADD_TEST (suite, test_uri_parse_too_long_is_unrecognized); - SUITE_ADD_TEST (suite, test_uri_build_object_type_cert); - SUITE_ADD_TEST (suite, test_uri_build_object_type_private); - SUITE_ADD_TEST (suite, test_uri_build_object_type_public); - SUITE_ADD_TEST (suite, test_uri_build_object_type_secret); - SUITE_ADD_TEST (suite, test_uri_build_with_library); - SUITE_ADD_TEST (suite, test_uri_build_library_version); - SUITE_ADD_TEST (suite, test_uri_get_set_unrecognized); - SUITE_ADD_TEST (suite, test_uri_match_token); - SUITE_ADD_TEST (suite, test_uri_match_module); - SUITE_ADD_TEST (suite, test_uri_match_attributes); - SUITE_ADD_TEST (suite, test_uri_get_set_attribute); - SUITE_ADD_TEST (suite, test_uri_get_set_attributes); - SUITE_ADD_TEST (suite, test_uri_pin_source); - SUITE_ADD_TEST (suite, test_uri_free_null); - SUITE_ADD_TEST (suite, test_uri_message); - - CuSuiteRun (suite); - CuSuiteSummary (suite, output); - CuSuiteDetails (suite, output); - printf ("%s\n", output->buffer); - ret = suite->failCount; - CuSuiteDelete (suite); - CuStringDelete (output); - return ret; -} -- cgit v1.1