From 677dee1a04058aefe8c7689f88da52afe3b4b4bb Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Fri, 15 Aug 2014 08:41:43 +0200 Subject: Move to non-recursive Makefile for building bins and libs Still use recursive for documentation and translation. --- common/Makefile.am | 113 +++++-- common/frob-getauxval.c | 61 ++++ common/test-array.c | 209 ++++++++++++ common/test-attrs.c | 757 ++++++++++++++++++++++++++++++++++++++++++ common/test-buffer.c | 199 +++++++++++ common/test-compat.c | 117 +++++++ common/test-constants.c | 102 ++++++ common/test-dict.c | 522 +++++++++++++++++++++++++++++ common/test-hash.c | 106 ++++++ common/test-lexer.c | 253 ++++++++++++++ common/test-message.c | 65 ++++ common/test-path.c | 216 ++++++++++++ common/test-tests.c | 95 ++++++ common/test-url.c | 164 +++++++++ common/tests/Makefile.am | 39 --- common/tests/frob-getauxval.c | 61 ---- common/tests/test-array.c | 209 ------------ common/tests/test-attrs.c | 757 ------------------------------------------ common/tests/test-buffer.c | 199 ----------- common/tests/test-compat.c | 117 ------- common/tests/test-constants.c | 102 ------ common/tests/test-dict.c | 522 ----------------------------- common/tests/test-hash.c | 106 ------ common/tests/test-lexer.c | 253 -------------- common/tests/test-message.c | 65 ---- common/tests/test-path.c | 216 ------------ common/tests/test-tests.c | 95 ------ common/tests/test-url.c | 164 --------- 28 files changed, 2951 insertions(+), 2933 deletions(-) create mode 100644 common/frob-getauxval.c create mode 100644 common/test-array.c create mode 100644 common/test-attrs.c create mode 100644 common/test-buffer.c create mode 100644 common/test-compat.c create mode 100644 common/test-constants.c create mode 100644 common/test-dict.c create mode 100644 common/test-hash.c create mode 100644 common/test-lexer.c create mode 100644 common/test-message.c create mode 100644 common/test-path.c create mode 100644 common/test-tests.c create mode 100644 common/test-url.c delete mode 100644 common/tests/Makefile.am delete mode 100644 common/tests/frob-getauxval.c delete mode 100644 common/tests/test-array.c delete mode 100644 common/tests/test-attrs.c delete mode 100644 common/tests/test-buffer.c delete mode 100644 common/tests/test-compat.c delete mode 100644 common/tests/test-constants.c delete mode 100644 common/tests/test-dict.c delete mode 100644 common/tests/test-hash.c delete mode 100644 common/tests/test-lexer.c delete mode 100644 common/tests/test-message.c delete mode 100644 common/tests/test-path.c delete mode 100644 common/tests/test-tests.c delete mode 100644 common/tests/test-url.c (limited to 'common') diff --git a/common/Makefile.am b/common/Makefile.am index 2df3915..5f185b8 100644 --- a/common/Makefile.am +++ b/common/Makefile.am @@ -1,15 +1,9 @@ -include $(top_srcdir)/build/Makefile.decl - -SUBDIRS = . tests - -incdir = $(includedir)/p11-kit-1/p11-kit - -inc_HEADERS = \ - pkcs11.h \ +inc_HEADERS += \ + common/pkcs11.h \ $(NULL) -noinst_LTLIBRARIES = \ +noinst_LTLIBRARIES += \ libp11-common.la \ libp11-library.la \ libp11-test.la \ @@ -17,31 +11,94 @@ noinst_LTLIBRARIES = \ $(NULL) libp11_common_la_SOURCES = \ - argv.c argv.h \ - attrs.c attrs.h \ - array.c array.h \ - buffer.c buffer.h \ - compat.c compat.h \ - constants.c constants.h \ - debug.c debug.h \ - dict.c dict.h \ - hash.c hash.h \ - lexer.c lexer.h \ - message.c message.h \ - path.c path.h \ - pkcs11.h pkcs11x.h \ - url.c url.h \ + common/argv.c common/argv.h \ + common/attrs.c common/attrs.h \ + common/array.c common/array.h \ + common/buffer.c common/buffer.h \ + common/compat.c common/compat.h \ + common/constants.c common/constants.h \ + common/debug.c common/debug.h \ + common/dict.c common/dict.h \ + common/hash.c common/hash.h \ + common/lexer.c common/lexer.h \ + common/message.c common/message.h \ + common/path.c common/path.h \ + common/pkcs11.h common/pkcs11x.h \ + common/url.c common/url.h \ $(NULL) libp11_library_la_SOURCES = \ - library.c library.h \ + common/library.c common/library.h \ $(NULL) libp11_test_la_SOURCES = \ - mock.c mock.h \ - test.c test.h \ + common/mock.c common/mock.h \ + common/test.c common/test.h \ $(NULL) libp11_tool_la_SOURCES = \ - tool.c tool.h \ - $(NULL) \ No newline at end of file + common/tool.c common/tool.h \ + $(NULL) + +# Tests ---------------------------------------------------------------- + +common_LIBS = \ + libp11-test.la \ + libp11-common.la \ + $(NULL) + +CHECK_PROGS += \ + test-tests \ + test-compat \ + test-hash \ + test-dict \ + test-array \ + test-constants \ + test-attrs \ + test-buffer \ + test-url \ + test-path \ + test-lexer \ + test-message \ + $(NULL) + +test_array_SOURCES = common/test-array.c +test_array_LDADD = $(common_LIBS) + +test_attrs_SOURCES = common/test-attrs.c +test_attrs_LDADD = $(common_LIBS) + +test_buffer_SOURCES = common/test-buffer.c +test_buffer_LDADD = $(common_LIBS) + +test_compat_SOURCES = common/test-compat.c +test_compat_LDADD = $(common_LIBS) + +test_constants_SOURCES = common/test-constants.c +test_constants_LDADD = $(common_LIBS) + +test_dict_SOURCES = common/test-dict.c +test_dict_LDADD = $(common_LIBS) + +test_hash_SOURCES = common/test-hash.c +test_hash_LDADD = $(common_LIBS) + +test_lexer_SOURCES = common/test-lexer.c +test_lexer_LDADD = $(common_LIBS) + +test_message_SOURCES = common/test-message.c +test_message_LDADD = $(common_LIBS) + +test_path_SOURCES = common/test-path.c +test_path_LDADD = $(common_LIBS) + +test_tests_SOURCES = common/test-tests.c +test_tests_LDADD = $(common_LIBS) + +test_url_SOURCES = common/test-url.c +test_url_LDADD = $(common_LIBS) + +noinst_PROGRAMS += frob-getauxval + +frob_getauxval_SOURCES = common/frob-getauxval.c +frob_getauxval_LDADD = $(common_LIBS) diff --git a/common/frob-getauxval.c b/common/frob-getauxval.c new file mode 100644 index 0000000..02745be --- /dev/null +++ b/common/frob-getauxval.c @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2013 Red Hat Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * * The names of contributors to this software may not be + * used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * Author: Stef Walter + */ + +#include "config.h" +#include "compat.h" + +#include +#include +#include +#include + +int +main (int argc, + char *argv[]) +{ + unsigned long type = 0; + unsigned long ret; + + if (argc == 2) + type = atoi (argv[1]); + + if (type == 0) { + fprintf (stderr, "usage: frob-getauxval 23"); + abort (); + } + + ret = getauxval (type); + printf ("getauxval(%lu) == %lu\n", type, ret); + return (int)ret; +} diff --git a/common/test-array.c b/common/test-array.c new file mode 100644 index 0000000..695917a --- /dev/null +++ b/common/test-array.c @@ -0,0 +1,209 @@ +/* + * 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 "array.h" +#include "test.h" + +static void +test_create (void) +{ + p11_array *array; + + array = p11_array_new (NULL); + assert_ptr_not_null (array); + p11_array_free (array); +} + +static void +test_free_null (void) +{ + p11_array_free (NULL); +} + +static void +destroy_value (void *data) +{ + int *value = data; + *value = 2; +} + +static void +test_free_destroys (void) +{ + p11_array *array; + int value = 0; + + array = p11_array_new (destroy_value); + assert_ptr_not_null (array); + if (!p11_array_push (array, &value)) + assert_not_reached (); + p11_array_free (array); + + assert_num_eq (2, value); +} + +static void +test_add (void) +{ + char *value = "VALUE"; + p11_array *array; + + array = p11_array_new (NULL); + if (!p11_array_push (array, value)) + assert_not_reached (); + + assert_num_eq (1, array->num); + assert_ptr_eq (array->elem[0], value); + + p11_array_free (array); +} + +static void +test_add_remove (void) +{ + char *value = "VALUE"; + p11_array *array; + + array = p11_array_new (NULL); + if (!p11_array_push (array, value)) + assert_not_reached (); + + assert_num_eq (1, array->num); + + assert_ptr_eq (array->elem[0], value); + + p11_array_remove (array, 0); + + assert_num_eq (0, array->num); + + p11_array_free (array); +} + +static void +test_remove_destroys (void) +{ + p11_array *array; + int value = 0; + + array = p11_array_new (destroy_value); + if (!p11_array_push (array, &value)) + assert_not_reached (); + + p11_array_remove (array, 0); + + assert_num_eq (2, value); + + /* should not be destroyed again */ + value = 0; + + p11_array_free (array); + + assert_num_eq (0, value); +} + +static void +test_remove_and_count (void) +{ + p11_array *array; + int *value; + int i; + + array = p11_array_new (free); + + assert_num_eq (0, array->num); + + for (i = 0; i < 20000; ++i) { + value = malloc (sizeof (int)); + assert (value != NULL); + *value = i; + if (!p11_array_push (array, value)) + assert_not_reached (); + assert_num_eq (i + 1, array->num); + } + + for (i = 10; i < 20000; ++i) { + p11_array_remove (array, 10); + assert_num_eq (20010 - (i + 1), array->num); + } + + assert_num_eq (10, array->num); + + p11_array_free (array); +} + +static void +test_clear_destroys (void) +{ + p11_array *array; + int value = 0; + + array = p11_array_new (destroy_value); + if (!p11_array_push (array, &value)) + assert_not_reached (); + + assert_num_eq (1, array->num); + + p11_array_clear (array); + + assert_num_eq (2, value); + assert_num_eq (0, array->num); + + /* should not be destroyed again */ + value = 0; + + p11_array_free (array); + + assert_num_eq (0, value); +} + +int +main (int argc, + char *argv[]) +{ + p11_test (test_create, "/array/create"); + p11_test (test_add, "/array/add"); + p11_test (test_add_remove, "/array/add-remove"); + p11_test (test_remove_destroys, "/array/remove-destroys"); + p11_test (test_remove_and_count, "/array/remove-and-count"); + p11_test (test_free_null, "/array/free-null"); + p11_test (test_free_destroys, "/array/free-destroys"); + p11_test (test_clear_destroys, "/array/clear-destroys"); + return p11_test_run (argc, argv); +} diff --git a/common/test-attrs.c b/common/test-attrs.c new file mode 100644 index 0000000..79895e2 --- /dev/null +++ b/common/test-attrs.c @@ -0,0 +1,757 @@ +/* + * 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 "test.h" + +#include +#include +#include + +#include "attrs.h" +#include "debug.h" + +static void +test_terminator (void) +{ + CK_ATTRIBUTE attrs[] = { + { CKA_LABEL, "label", 5 }, + { CKA_LABEL, NULL, 0 }, + { CKA_INVALID }, + }; + + assert_num_eq (true, p11_attrs_terminator (attrs + 2)); + assert_num_eq (true, p11_attrs_terminator (NULL)); + assert_num_eq (false, p11_attrs_terminator (attrs)); + assert_num_eq (false, p11_attrs_terminator (attrs + 1)); +} + +static void +test_count (void) +{ + CK_BBOOL vtrue = CK_TRUE; + + CK_ATTRIBUTE attrs[] = { + { CKA_LABEL, "label", 5 }, + { CKA_TOKEN, &vtrue, sizeof (vtrue) }, + { CKA_INVALID }, + }; + + CK_ATTRIBUTE empty[] = { + { CKA_INVALID }, + }; + + assert_num_eq (2, p11_attrs_count (attrs)); + assert_num_eq (0, p11_attrs_count (NULL)); + assert_num_eq (0, p11_attrs_count (empty)); +} + +static void +test_build_one (void) +{ + CK_ATTRIBUTE *attrs; + CK_ATTRIBUTE add = { CKA_LABEL, "yay", 3 }; + + attrs = p11_attrs_build (NULL, &add, NULL); + + /* Test the first attribute */ + assert_ptr_not_null (attrs); + assert (attrs->type == CKA_LABEL); + assert_num_eq (3, attrs->ulValueLen); + assert (memcmp (attrs->pValue, "yay", 3) == 0); + + assert (attrs[1].type == CKA_INVALID); + + p11_attrs_free (attrs); +} + +static void +test_build_two (void) +{ + CK_ATTRIBUTE *attrs; + CK_ATTRIBUTE one = { CKA_LABEL, "yay", 3 }; + CK_ATTRIBUTE two = { CKA_VALUE, "eight", 5 }; + + attrs = p11_attrs_build (NULL, &one, &two, NULL); + + assert_ptr_not_null (attrs); + assert (attrs[0].type == CKA_LABEL); + assert_num_eq (3, attrs[0].ulValueLen); + assert (memcmp (attrs[0].pValue, "yay", 3) == 0); + + assert_ptr_not_null (attrs); + assert (attrs[1].type == CKA_VALUE); + assert_num_eq (5, attrs[1].ulValueLen); + assert (memcmp (attrs[1].pValue, "eight", 5) == 0); + + assert (attrs[2].type == CKA_INVALID); + + p11_attrs_free (attrs); +} + +static void +test_build_invalid (void) +{ + CK_ATTRIBUTE *attrs; + CK_ATTRIBUTE one = { CKA_LABEL, "yay", 3 }; + CK_ATTRIBUTE invalid = { CKA_INVALID }; + CK_ATTRIBUTE two = { CKA_VALUE, "eight", 5 }; + + attrs = p11_attrs_build (NULL, &one, &invalid, &two, NULL); + + assert_ptr_not_null (attrs); + assert (attrs[0].type == CKA_LABEL); + assert_num_eq (3, attrs[0].ulValueLen); + assert (memcmp (attrs[0].pValue, "yay", 3) == 0); + + assert_ptr_not_null (attrs); + assert (attrs[1].type == CKA_VALUE); + assert_num_eq (5, attrs[1].ulValueLen); + assert (memcmp (attrs[1].pValue, "eight", 5) == 0); + + assert (attrs[2].type == CKA_INVALID); + + p11_attrs_free (attrs); +} + +static void +test_buildn_two (void) +{ + CK_ATTRIBUTE *attrs; + CK_ATTRIBUTE add[] = { + { CKA_LABEL, "yay", 3 }, + { CKA_VALUE, "eight", 5 } + }; + + attrs = p11_attrs_buildn (NULL, add, 2); + + /* Test the first attribute */ + assert_ptr_not_null (attrs); + assert (attrs->type == CKA_LABEL); + assert_num_eq (3, attrs->ulValueLen); + assert (memcmp (attrs->pValue, "yay", 3) == 0); + + assert_ptr_not_null (attrs); + assert (attrs[1].type == CKA_VALUE); + assert_num_eq (5, attrs[1].ulValueLen); + assert (memcmp (attrs[1].pValue, "eight", 5) == 0); + + assert (attrs[2].type == CKA_INVALID); + + p11_attrs_free (attrs); +} + +static void +test_buildn_one (void) +{ + CK_ATTRIBUTE *attrs; + CK_ATTRIBUTE add = { CKA_LABEL, "yay", 3 }; + + attrs = p11_attrs_buildn (NULL, &add, 1); + + /* Test the first attribute */ + assert_ptr_not_null (attrs); + assert (attrs->type == CKA_LABEL); + assert_num_eq (3, attrs->ulValueLen); + assert (memcmp (attrs->pValue, "yay", 3) == 0); + + assert (attrs[1].type == CKA_INVALID); + + p11_attrs_free (attrs); +} + +static void +test_build_add (void) +{ + CK_ATTRIBUTE initial[] = { + { CKA_LABEL, "label", 5 }, + { CKA_VALUE, "nine", 4 }, + }; + + CK_ATTRIBUTE *attrs; + CK_ATTRIBUTE one = { CKA_LABEL, "yay", 3 }; + CK_ATTRIBUTE two = { CKA_TOKEN, "\x01", 1 }; + + attrs = p11_attrs_buildn (NULL, initial, 2); + attrs = p11_attrs_build (attrs, &one, &two, NULL); + + assert_ptr_not_null (attrs); + assert (attrs[0].type == CKA_LABEL); + assert_num_eq (3, attrs[0].ulValueLen); + assert (memcmp (attrs[0].pValue, "yay", 3) == 0); + + assert_ptr_not_null (attrs); + assert (attrs[1].type == CKA_VALUE); + assert_num_eq (4, attrs[1].ulValueLen); + assert (memcmp (attrs[1].pValue, "nine", 4) == 0); + + assert_ptr_not_null (attrs); + assert (attrs[2].type == CKA_TOKEN); + assert_num_eq (1, attrs[2].ulValueLen); + assert (memcmp (attrs[2].pValue, "\x01", 1) == 0); + + assert (attrs[3].type == CKA_INVALID); + + p11_attrs_free (attrs); +} + +static void +test_build_null (void) +{ + CK_ATTRIBUTE *attrs; + CK_ATTRIBUTE add = { CKA_LABEL, NULL, (CK_ULONG)-1 }; + + attrs = p11_attrs_build (NULL, &add, NULL); + + /* Test the first attribute */ + assert_ptr_not_null (attrs); + assert (attrs->type == CKA_LABEL); + assert (attrs->ulValueLen == (CK_ULONG)-1); + assert_ptr_eq (NULL, attrs->pValue); + + p11_attrs_free (attrs); +} + +static void +test_dup (void) +{ + CK_ATTRIBUTE *attrs; + CK_ATTRIBUTE original[] = { + { CKA_LABEL, "yay", 3 }, + { CKA_VALUE, "eight", 5 }, + { CKA_INVALID } + }; + + attrs = p11_attrs_dup (original); + + /* Test the first attribute */ + assert_ptr_not_null (attrs); + assert (attrs->type == CKA_LABEL); + assert_num_eq (3, attrs->ulValueLen); + assert (memcmp (attrs->pValue, "yay", 3) == 0); + + assert_ptr_not_null (attrs); + assert (attrs[1].type == CKA_VALUE); + assert_num_eq (5, attrs[1].ulValueLen); + assert (memcmp (attrs[1].pValue, "eight", 5) == 0); + + assert (attrs[2].type == CKA_INVALID); + + p11_attrs_free (attrs); +} + +static void +test_take (void) +{ + CK_ATTRIBUTE initial[] = { + { CKA_LABEL, "label", 5 }, + { CKA_VALUE, "nine", 4 }, + }; + + CK_ATTRIBUTE *attrs; + + attrs = p11_attrs_buildn (NULL, initial, 2); + attrs = p11_attrs_take (attrs, CKA_LABEL, strdup ("boooyah"), 7); + attrs = p11_attrs_take (attrs, CKA_TOKEN, strdup ("\x01"), 1); + assert_ptr_not_null (attrs); + + assert (attrs[0].type == CKA_LABEL); + assert_num_eq (7, attrs[0].ulValueLen); + assert (memcmp (attrs[0].pValue, "boooyah", 7) == 0); + + assert_ptr_not_null (attrs); + assert (attrs[1].type == CKA_VALUE); + assert_num_eq (4, attrs[1].ulValueLen); + assert (memcmp (attrs[1].pValue, "nine", 4) == 0); + + assert_ptr_not_null (attrs); + assert (attrs[2].type == CKA_TOKEN); + assert_num_eq (1, attrs[2].ulValueLen); + assert (memcmp (attrs[2].pValue, "\x01", 1) == 0); + + assert (attrs[3].type == CKA_INVALID); + + p11_attrs_free (attrs); +} + + +static void +test_merge_replace (void) +{ + CK_ATTRIBUTE initial[] = { + { CKA_LABEL, "label", 5 }, + { CKA_VALUE, "nine", 4 }, + }; + + CK_ATTRIBUTE extra[] = { + { CKA_LABEL, "boooyah", 7 }, + { CKA_APPLICATION, "disco", 5 }, + }; + + CK_ATTRIBUTE *attrs; + CK_ATTRIBUTE *merge; + + attrs = p11_attrs_buildn (NULL, initial, 2); + merge = p11_attrs_buildn (NULL, extra, 2); + attrs = p11_attrs_merge (attrs, merge, true); + assert_ptr_not_null (attrs); + + assert (attrs[0].type == CKA_LABEL); + assert_num_eq (7, attrs[0].ulValueLen); + assert (memcmp (attrs[0].pValue, "boooyah", 7) == 0); + + assert_ptr_not_null (attrs); + assert (attrs[1].type == CKA_VALUE); + assert_num_eq (4, attrs[1].ulValueLen); + assert (memcmp (attrs[1].pValue, "nine", 4) == 0); + + assert_ptr_not_null (attrs); + assert (attrs[2].type == CKA_APPLICATION); + assert_num_eq (5, attrs[2].ulValueLen); + assert (memcmp (attrs[2].pValue, "disco", 5) == 0); + + assert (attrs[3].type == CKA_INVALID); + + p11_attrs_free (attrs); +} + +static void +test_merge_empty (void) +{ + CK_ATTRIBUTE extra[] = { + { CKA_LABEL, "boooyah", 7 }, + { CKA_APPLICATION, "disco", 5 }, + }; + + CK_ATTRIBUTE *attrs = NULL; + CK_ATTRIBUTE *merge; + + merge = p11_attrs_buildn (NULL, extra, 2); + attrs = p11_attrs_merge (attrs, merge, true); + assert_ptr_not_null (attrs); + assert_ptr_eq (merge, attrs); + + p11_attrs_free (attrs); +} + +static void +test_merge_augment (void) +{ + CK_ATTRIBUTE initial[] = { + { CKA_LABEL, "label", 5 }, + { CKA_VALUE, "nine", 4 }, + }; + + CK_ATTRIBUTE extra[] = { + { CKA_LABEL, "boooyah", 7 }, + { CKA_APPLICATION, "disco", 5 }, + }; + + CK_ATTRIBUTE *attrs; + CK_ATTRIBUTE *merge; + + attrs = p11_attrs_buildn (NULL, initial, 2); + merge = p11_attrs_buildn (NULL, extra, 2); + attrs = p11_attrs_merge (attrs, merge, false); + assert_ptr_not_null (attrs); + + assert (attrs[0].type == CKA_LABEL); + assert_num_eq (5, attrs[0].ulValueLen); + assert (memcmp (attrs[0].pValue, "label", 5) == 0); + + assert_ptr_not_null (attrs); + assert (attrs[1].type == CKA_VALUE); + assert_num_eq (4, attrs[1].ulValueLen); + assert (memcmp (attrs[1].pValue, "nine", 4) == 0); + + assert_ptr_not_null (attrs); + assert (attrs[2].type == CKA_APPLICATION); + assert_num_eq (5, attrs[2].ulValueLen); + assert (memcmp (attrs[2].pValue, "disco", 5) == 0); + + assert (attrs[3].type == CKA_INVALID); + + p11_attrs_free (attrs); +} + +static void +test_free_null (void) +{ + p11_attrs_free (NULL); +} + +static void +test_equal (void) +{ + char *data = "extra attribute"; + CK_ATTRIBUTE one = { CKA_LABEL, "yay", 3 }; + CK_ATTRIBUTE null = { CKA_LABEL, NULL, 3 }; + CK_ATTRIBUTE two = { CKA_VALUE, "yay", 3 }; + CK_ATTRIBUTE other = { CKA_VALUE, data, 5 }; + CK_ATTRIBUTE overflow = { CKA_VALUE, data, 5 }; + CK_ATTRIBUTE content = { CKA_VALUE, "conte", 5 }; + + assert (p11_attr_equal (&one, &one)); + assert (!p11_attr_equal (&one, NULL)); + assert (!p11_attr_equal (NULL, &one)); + assert (!p11_attr_equal (&one, &two)); + assert (!p11_attr_equal (&two, &other)); + assert (p11_attr_equal (&other, &overflow)); + assert (!p11_attr_equal (&one, &null)); + assert (!p11_attr_equal (&one, &null)); + assert (!p11_attr_equal (&other, &content)); +} + +static void +test_hash (void) +{ + char *data = "extra attribute"; + CK_ATTRIBUTE one = { CKA_LABEL, "yay", 3 }; + CK_ATTRIBUTE null = { CKA_LABEL, NULL, 3 }; + CK_ATTRIBUTE two = { CKA_VALUE, "yay", 3 }; + CK_ATTRIBUTE other = { CKA_VALUE, data, 5 }; + CK_ATTRIBUTE overflow = { CKA_VALUE, data, 5 }; + CK_ATTRIBUTE content = { CKA_VALUE, "conte", 5 }; + unsigned int hash; + + hash = p11_attr_hash (&one); + assert (hash != 0); + + assert (p11_attr_hash (&one) == hash); + assert (p11_attr_hash (&two) != hash); + assert (p11_attr_hash (&other) != hash); + assert (p11_attr_hash (&overflow) != hash); + assert (p11_attr_hash (&null) != hash); + assert (p11_attr_hash (&content) != hash); + + hash = p11_attr_hash (NULL); + assert (hash == 0); +} + +static void +test_to_string (void) +{ + char *data = "extra attribute"; + CK_ATTRIBUTE one = { CKA_LABEL, "yay", 3 }; + CK_ATTRIBUTE attrs[] = { + { CKA_LABEL, "yay", 3 }, + { CKA_VALUE, data, 5 }, + { CKA_INVALID }, + }; + + char *string; + + + string = p11_attr_to_string (&one, CKA_INVALID); + assert_str_eq ("{ CKA_LABEL = (3) \"yay\" }", string); + free (string); + + string = p11_attrs_to_string (attrs, -1); + assert_str_eq ("(2) [ { CKA_LABEL = (3) \"yay\" }, { CKA_VALUE = (5) NOT-PRINTED } ]", string); + free (string); + + string = p11_attrs_to_string (attrs, 1); + assert_str_eq ("(1) [ { CKA_LABEL = (3) \"yay\" } ]", string); + free (string); +} + +static void +test_find (void) +{ + CK_BBOOL vtrue = CK_TRUE; + CK_ATTRIBUTE *attr; + + CK_ATTRIBUTE attrs[] = { + { CKA_LABEL, "label", 5 }, + { CKA_TOKEN, &vtrue, sizeof (vtrue) }, + { CKA_INVALID }, + }; + + attr = p11_attrs_find (attrs, CKA_LABEL); + assert_ptr_eq (attrs + 0, attr); + + attr = p11_attrs_find (attrs, CKA_TOKEN); + assert_ptr_eq (attrs + 1, attr); + + attr = p11_attrs_find (attrs, CKA_VALUE); + assert_ptr_eq (NULL, attr); +} + +static void +test_findn (void) +{ + CK_BBOOL vtrue = CK_TRUE; + CK_ATTRIBUTE *attr; + + CK_ATTRIBUTE attrs[] = { + { CKA_LABEL, "label", 5 }, + { CKA_TOKEN, &vtrue, sizeof (vtrue) }, + }; + + attr = p11_attrs_findn (attrs, 2, CKA_LABEL); + assert_ptr_eq (attrs + 0, attr); + + attr = p11_attrs_findn (attrs, 2, CKA_TOKEN); + assert_ptr_eq (attrs + 1, attr); + + attr = p11_attrs_findn (attrs, 2, CKA_VALUE); + assert_ptr_eq (NULL, attr); + + attr = p11_attrs_findn (attrs, 1, CKA_TOKEN); + assert_ptr_eq (NULL, attr); +} + +static void +test_remove (void) +{ + CK_BBOOL vtrue = CK_TRUE; + CK_ATTRIBUTE *attr; + CK_ATTRIBUTE *attrs; + CK_BBOOL ret; + + CK_ATTRIBUTE initial[] = { + { CKA_LABEL, "label", 5 }, + { CKA_TOKEN, &vtrue, sizeof (vtrue) }, + }; + + attrs = p11_attrs_buildn (NULL, initial, 2); + assert_ptr_not_null (attrs); + + attr = p11_attrs_find (attrs, CKA_LABEL); + assert_ptr_eq (attrs + 0, attr); + + ret = p11_attrs_remove (attrs, CKA_LABEL); + assert_num_eq (CK_TRUE, ret); + + attr = p11_attrs_find (attrs, CKA_LABEL); + assert_ptr_eq (NULL, attr); + + ret = p11_attrs_remove (attrs, CKA_LABEL); + assert_num_eq (CK_FALSE, ret); + + p11_attrs_free (attrs); +} + +static void +test_match (void) +{ + CK_BBOOL vtrue = CK_TRUE; + + CK_ATTRIBUTE attrs[] = { + { CKA_LABEL, "label", 5 }, + { CKA_TOKEN, &vtrue, sizeof (vtrue) }, + { CKA_INVALID }, + }; + + CK_ATTRIBUTE subset[] = { + { CKA_LABEL, "label", 5 }, + { CKA_INVALID }, + }; + + CK_ATTRIBUTE different[] = { + { CKA_LABEL, "other", 5 }, + { CKA_INVALID }, + }; + + CK_ATTRIBUTE extra[] = { + { CKA_VALUE, "the value", 9 }, + { CKA_LABEL, "other", 5 }, + { CKA_TOKEN, &vtrue, sizeof (vtrue) }, + { CKA_INVALID }, + }; + + assert (p11_attrs_match (attrs, attrs)); + assert (p11_attrs_match (attrs, subset)); + assert (!p11_attrs_match (attrs, different)); + assert (!p11_attrs_match (attrs, extra)); +} + +static void +test_matchn (void) +{ + CK_BBOOL vtrue = CK_TRUE; + + CK_ATTRIBUTE attrs[] = { + { CKA_LABEL, "label", 5 }, + { CKA_TOKEN, &vtrue, sizeof (vtrue) }, + { CKA_INVALID }, + }; + + CK_ATTRIBUTE subset[] = { + { CKA_LABEL, "label", 5 }, + }; + + CK_ATTRIBUTE different[] = { + { CKA_TOKEN, &vtrue, sizeof (vtrue) }, + { CKA_LABEL, "other", 5 }, + }; + + CK_ATTRIBUTE extra[] = { + { CKA_VALUE, "the value", 9 }, + { CKA_LABEL, "other", 5 }, + { CKA_TOKEN, &vtrue, sizeof (vtrue) }, + }; + + assert (p11_attrs_matchn (attrs, subset, 1)); + assert (!p11_attrs_matchn (attrs, different, 2)); + assert (!p11_attrs_matchn (attrs, extra, 3)); +} + +static void +test_find_bool (void) +{ + CK_BBOOL vtrue = CK_TRUE; + CK_BBOOL vfalse = CK_FALSE; + CK_BBOOL value; + + CK_ATTRIBUTE attrs[] = { + { CKA_LABEL, "\x01yy", 3 }, + { CKA_VALUE, &vtrue, (CK_ULONG)-1 }, + { CKA_TOKEN, &vtrue, sizeof (CK_BBOOL) }, + { CKA_TOKEN, &vfalse, sizeof (CK_BBOOL) }, + { CKA_INVALID }, + }; + + assert (p11_attrs_find_bool (attrs, CKA_TOKEN, &value) && value == CK_TRUE); + assert (!p11_attrs_find_bool (attrs, CKA_LABEL, &value)); + assert (!p11_attrs_find_bool (attrs, CKA_VALUE, &value)); +} + +static void +test_find_ulong (void) +{ + CK_ULONG v33 = 33UL; + CK_ULONG v45 = 45UL; + CK_ULONG value; + + CK_ATTRIBUTE attrs[] = { + { CKA_LABEL, &v33, 2 }, + { CKA_VALUE, &v45, (CK_ULONG)-1 }, + { CKA_BITS_PER_PIXEL, &v33, sizeof (CK_ULONG) }, + { CKA_BITS_PER_PIXEL, &v45, sizeof (CK_ULONG) }, + { CKA_INVALID }, + }; + + assert (p11_attrs_find_ulong (attrs, CKA_BITS_PER_PIXEL, &value) && value == v33); + assert (!p11_attrs_find_ulong (attrs, CKA_LABEL, &value)); + assert (!p11_attrs_find_ulong (attrs, CKA_VALUE, &value)); +} + +static void +test_find_value (void) +{ + void *value; + size_t length; + + CK_ATTRIBUTE attrs[] = { + { CKA_LABEL, "", (CK_ULONG)-1 }, + { CKA_LABEL, NULL, 5 }, + { CKA_LABEL, "", 0 }, + { CKA_LABEL, "test", 4 }, + { CKA_VALUE, NULL, 0 }, + { CKA_INVALID }, + }; + + value = p11_attrs_find_value (attrs, CKA_LABEL, &length); + assert_ptr_eq (attrs[3].pValue, value); + assert_num_eq (4, length); + + value = p11_attrs_find_value (attrs, CKA_LABEL, NULL); + assert_ptr_eq (attrs[3].pValue, value); + + value = p11_attrs_find_value (attrs, CKA_VALUE, &length); + assert_ptr_eq (NULL, value); + + value = p11_attrs_find_value (attrs, CKA_TOKEN, &length); + assert_ptr_eq (NULL, value); +} + +static void +test_find_valid (void) +{ + CK_ATTRIBUTE *attr; + + CK_ATTRIBUTE attrs[] = { + { CKA_LABEL, "", (CK_ULONG)-1 }, + { CKA_LABEL, NULL, 5 }, + { CKA_LABEL, "", 0 }, + { CKA_LABEL, "test", 4 }, + { CKA_VALUE, "value", 5 }, + { CKA_INVALID }, + }; + + attr = p11_attrs_find_valid (attrs, CKA_LABEL); + assert_ptr_eq (attrs + 3, attr); + + attr = p11_attrs_find_valid (attrs, CKA_VALUE); + assert_ptr_eq (attrs + 4, attr); + + attr = p11_attrs_find_valid (attrs, CKA_TOKEN); + assert_ptr_eq (NULL, attr); +} + +int +main (int argc, + char *argv[]) +{ + p11_test (test_equal, "/attrs/equal"); + p11_test (test_hash, "/attrs/hash"); + p11_test (test_to_string, "/attrs/to-string"); + + p11_test (test_terminator, "/attrs/terminator"); + p11_test (test_count, "/attrs/count"); + p11_test (test_build_one, "/attrs/build-one"); + p11_test (test_build_two, "/attrs/build-two"); + p11_test (test_build_invalid, "/attrs/build-invalid"); + p11_test (test_buildn_one, "/attrs/buildn-one"); + p11_test (test_buildn_two, "/attrs/buildn-two"); + p11_test (test_build_add, "/attrs/build-add"); + p11_test (test_build_null, "/attrs/build-null"); + p11_test (test_dup, "/attrs/dup"); + p11_test (test_take, "/attrs/take"); + p11_test (test_merge_replace, "/attrs/merge-replace"); + p11_test (test_merge_augment, "/attrs/merge-augment"); + p11_test (test_merge_empty, "/attrs/merge-empty"); + p11_test (test_free_null, "/attrs/free-null"); + p11_test (test_match, "/attrs/match"); + p11_test (test_matchn, "/attrs/matchn"); + p11_test (test_find, "/attrs/find"); + p11_test (test_findn, "/attrs/findn"); + p11_test (test_find_bool, "/attrs/find-bool"); + p11_test (test_find_ulong, "/attrs/find-ulong"); + p11_test (test_find_value, "/attrs/find-value"); + p11_test (test_find_valid, "/attrs/find-valid"); + p11_test (test_remove, "/attrs/remove"); + return p11_test_run (argc, argv); +} diff --git a/common/test-buffer.c b/common/test-buffer.c new file mode 100644 index 0000000..4fd060d --- /dev/null +++ b/common/test-buffer.c @@ -0,0 +1,199 @@ +/* + * 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 "test.h" + +#include +#include +#include + +#include "debug.h" +#include "buffer.h" + +static void +test_init_uninit (void) +{ + p11_buffer buffer; + + p11_buffer_init (&buffer, 10); + assert_ptr_not_null (buffer.data); + assert_num_eq (0, buffer.len); + assert_num_eq (0, buffer.flags); + assert (buffer.size >= 10); + assert_ptr_not_null (buffer.ffree); + assert_ptr_not_null (buffer.frealloc); + + p11_buffer_uninit (&buffer); +} + +static void +test_append (void) +{ + p11_buffer buffer; + + p11_buffer_init (&buffer, 10); + buffer.len = 5; + p11_buffer_append (&buffer, 35); + assert_num_eq (5 + 35, buffer.len); + assert (buffer.size >= 35 + 5); + + p11_buffer_append (&buffer, 15); + assert_num_eq (5 + 35 + 15, buffer.len); + assert (buffer.size >= 5 + 35 + 15); + + p11_buffer_uninit (&buffer); +} + +static void +test_null (void) +{ + p11_buffer buffer; + + p11_buffer_init_null (&buffer, 10); + p11_buffer_add (&buffer, "Blah", -1); + p11_buffer_add (&buffer, " blah", -1); + + assert_str_eq ("Blah blah", buffer.data); + + p11_buffer_uninit (&buffer); +} + +static int mock_realloced = 0; +static int mock_freed = 0; + +static void * +mock_realloc (void *data, + size_t size) +{ + mock_realloced++; + return realloc (data, size); +} + +static void +mock_free (void *data) +{ + mock_freed++; + free (data); +} + +static void +test_init_for_data (void) +{ + p11_buffer buffer; + unsigned char *ret; + size_t len; + + mock_realloced = 0; + mock_freed = 0; + + p11_buffer_init_full (&buffer, (unsigned char *)strdup ("blah"), 4, 0, + mock_realloc, mock_free); + + assert_ptr_not_null (buffer.data); + assert_str_eq ("blah", (char *)buffer.data); + assert_num_eq (4, buffer.len); + assert_num_eq (0, buffer.flags); + assert_num_eq (4, buffer.size); + assert_ptr_eq (mock_free, buffer.ffree); + assert_ptr_eq (mock_realloc, buffer.frealloc); + + assert_num_eq (0, mock_realloced); + assert_num_eq (0, mock_freed); + + len = buffer.len; + ret = p11_buffer_append (&buffer, 1024); + assert_ptr_eq ((char *)buffer.data + len, ret); + assert_num_eq (1, mock_realloced); + + p11_buffer_uninit (&buffer); + assert_num_eq (1, mock_realloced); + assert_num_eq (1, mock_freed); +} + +static void +test_steal (void) +{ + p11_buffer buffer; + char *string; + size_t length; + + mock_freed = 0; + + p11_buffer_init_full (&buffer, (unsigned char *)strdup ("blah"), 4, + P11_BUFFER_NULL, mock_realloc, mock_free); + + assert_ptr_not_null (buffer.data); + assert_str_eq ("blah", buffer.data); + + p11_buffer_add (&buffer, " yada", -1); + assert_str_eq ("blah yada", buffer.data); + + string = p11_buffer_steal (&buffer, &length); + p11_buffer_uninit (&buffer); + + assert_str_eq ("blah yada", string); + assert_num_eq (9, length); + assert_num_eq (0, mock_freed); + + free (string); +} + +static void +test_add (void) +{ + p11_buffer buffer; + + p11_buffer_init (&buffer, 10); + + p11_buffer_add (&buffer, (unsigned char *)"Planet Express", 15); + assert_num_eq (15, buffer.len); + assert_str_eq ("Planet Express", (char *)buffer.data); + assert (p11_buffer_ok (&buffer)); + + p11_buffer_uninit (&buffer); +} + +int +main (int argc, + char *argv[]) +{ + p11_test (test_init_uninit, "/buffer/init-uninit"); + p11_test (test_init_for_data, "/buffer/init-for-data"); + p11_test (test_append, "/buffer/append"); + p11_test (test_null, "/buffer/null"); + p11_test (test_add, "/buffer/add"); + p11_test (test_steal, "/buffer/steal"); + return p11_test_run (argc, argv); +} diff --git a/common/test-compat.c b/common/test-compat.c new file mode 100644 index 0000000..42471ae --- /dev/null +++ b/common/test-compat.c @@ -0,0 +1,117 @@ +/* + * Copyright (c) 2013 Red Hat Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * * The names of contributors to this software may not be + * used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * Author: Stef Walter + */ + +#include "config.h" +#include "test.h" + +#include +#include +#include +#include + +#include "compat.h" + +static void +test_strndup (void) +{ + char unterminated[] = { 't', 'e', 's', 't', 'e', 'r', 'o', 'n', 'i', 'o' }; + char *res; + + res = strndup (unterminated, 6); + assert_str_eq (res, "tester"); + free (res); + + res = strndup ("test", 6); + assert_str_eq (res, "test"); + free (res); +} + +#ifdef OS_UNIX + +static void +test_getauxval (void) +{ + /* 23 is AT_SECURE */ + const char *args[] = { BUILDDIR "/frob-getauxval", "23", NULL }; + char *path; + int ret; + + ret = p11_test_run_child (args, true); + assert_num_eq (ret, 0); + + path = p11_test_copy_setgid (args[0]); + if (path == NULL) + return; + + args[0] = path; + ret = p11_test_run_child (args, true); + assert_num_cmp (ret, !=, 0); + + if (unlink (path) < 0) + assert_fail ("unlink failed", strerror (errno)); + free (path); +} + +static void +test_mmap (void) +{ + p11_mmap *map; + void *data; + size_t size; + char file[] = "emptyfileXXXXXX"; + int fd = mkstemp (file); + close (fd); + /* mmap on empty file should work */ + map = p11_mmap_open (file, NULL, &data, &size); + unlink (file); + assert_ptr_not_null (map); + p11_mmap_close (map); +} + +#endif /* OS_UNIX */ + +int +main (int argc, + char *argv[]) +{ + p11_test (test_strndup, "/compat/strndup"); +#ifdef OS_UNIX + /* Don't run this test when under fakeroot */ + if (!getenv ("FAKED_MODE")) { + p11_test (test_getauxval, "/compat/getauxval"); + } + p11_test (test_mmap, "/compat/mmap"); +#endif + return p11_test_run (argc, argv); +} diff --git a/common/test-constants.c b/common/test-constants.c new file mode 100644 index 0000000..9adc81a --- /dev/null +++ b/common/test-constants.c @@ -0,0 +1,102 @@ +/* + * 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 "test.h" + +#include +#include +#include + +#include "attrs.h" +#include "constants.h" +#include "debug.h" + +static void +test_constants (void *arg) +{ + const p11_constant *constant = arg; + p11_dict *nicks, *names; + CK_ULONG check; + int i; + + nicks = p11_constant_reverse (true); + names = p11_constant_reverse (false); + + for (i = 1; constant[i].value != CKA_INVALID; i++) { + if (constant[i].value < constant[i - 1].value) + assert_fail ("attr constant out of order", constant[i].name); + } + for (i = 0; constant[i].value != CKA_INVALID; i++) { + assert_ptr_not_null (constant[i].name); + + if (constant[i].nick) { + assert_str_eq (constant[i].nick, + p11_constant_nick (constant, constant[i].value)); + } + + assert_str_eq (constant[i].name, + p11_constant_name (constant, constant[i].value)); + + if (constant[i].nick) { + check = p11_constant_resolve (nicks, constant[i].nick); + assert_num_eq (constant[i].value, check); + } + + check = p11_constant_resolve (names, constant[i].name); + assert_num_eq (constant[i].value, check); + } + + p11_dict_free (names); + p11_dict_free (nicks); +} + +int +main (int argc, + char *argv[]) +{ + p11_testx (test_constants, (void *)p11_constant_types, "/constants/types"); + p11_testx (test_constants, (void *)p11_constant_classes, "/constants/classes"); + p11_testx (test_constants, (void *)p11_constant_trusts, "/constants/trusts"); + p11_testx (test_constants, (void *)p11_constant_certs, "/constants/certs"); + p11_testx (test_constants, (void *)p11_constant_keys, "/constants/keys"); + p11_testx (test_constants, (void *)p11_constant_asserts, "/constants/asserts"); + p11_testx (test_constants, (void *)p11_constant_categories, "/constants/categories"); + p11_testx (test_constants, (void *)p11_constant_mechanisms, "/constants/mechanisms"); + p11_testx (test_constants, (void *)p11_constant_users, "/constants/users"); + p11_testx (test_constants, (void *)p11_constant_states, "/constants/states"); + p11_testx (test_constants, (void *)p11_constant_returns, "/constants/returns"); + + return p11_test_run (argc, argv); +} diff --git a/common/test-dict.c b/common/test-dict.c new file mode 100644 index 0000000..f12a34e --- /dev/null +++ b/common/test-dict.c @@ -0,0 +1,522 @@ +/* + * 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 "test.h" + +#include +#include +#include +#include + +#include "dict.h" + +static void +test_create (void) +{ + p11_dict *map; + + map = p11_dict_new (p11_dict_direct_hash, p11_dict_direct_equal, NULL, NULL); + assert_ptr_not_null (map); + p11_dict_free (map); +} + +static void +test_free_null (void) +{ + p11_dict_free (NULL); +} + +typedef struct { + int value; + bool 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 bool +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 = true; +} + +static void +value_destroy (void *data) +{ + int *value = data; + *value = 2; +} + +static void +test_free_destroys (void) +{ + p11_dict *map; + Key key = { 8, 0 }; + int value = 0; + + map = p11_dict_new (key_hash, key_equal, key_destroy, value_destroy); + assert_ptr_not_null (map); + if (!p11_dict_set (map, &key, &value)) + assert_not_reached (); + p11_dict_free (map); + + assert_num_eq (true, key.freed); + assert_num_eq (2, value); +} + +static void +test_iterate (void) +{ + 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); + assert_ptr_not_null (map); + if (!p11_dict_set (map, &key, &value)) + assert_not_reached (); + + p11_dict_iterate (map, &iter); + + ret = p11_dict_next (&iter, &pkey, &pvalue); + assert_num_eq (1, ret); + assert_ptr_eq (pkey, &key); + assert_ptr_eq (pvalue, &value); + + ret = p11_dict_next (&iter, &pkey, &pvalue); + assert_num_eq (0, ret); + + p11_dict_free (map); +} + +static int +compar_strings (const void *one, + const void *two) +{ + char **p1 = (char **)one; + char **p2 = (char **)two; + return strcmp (*p1, *p2); +} + +static void +test_iterate_remove (void) +{ + p11_dict *map; + p11_dictiter iter; + char *keys[] = { "111", "222", "333" }; + char *values[] = { "444", "555", "666" }; + void *okeys[3]; + void *ovalues[3]; + bool ret; + int i; + + map = p11_dict_new (p11_dict_str_hash, p11_dict_str_equal, NULL, NULL); + assert_ptr_not_null (map); + + for (i = 0; i < 3; i++) { + if (!p11_dict_set (map, keys[i], values[i])) + assert_not_reached (); + } + + p11_dict_iterate (map, &iter); + + ret = p11_dict_next (&iter, &okeys[0], &ovalues[0]); + assert_num_eq (true, ret); + + ret = p11_dict_next (&iter, &okeys[1], &ovalues[1]); + assert_num_eq (true, ret); + if (!p11_dict_remove (map, okeys[1])) + assert_not_reached (); + + ret = p11_dict_next (&iter, &okeys[2], &ovalues[2]); + assert_num_eq (true, ret); + + ret = p11_dict_next (&iter, NULL, NULL); + assert_num_eq (false, ret); + + assert_num_eq (2, p11_dict_size (map)); + p11_dict_free (map); + + qsort (okeys, 3, sizeof (void *), compar_strings); + qsort (ovalues, 3, sizeof (void *), compar_strings); + + for (i = 0; i < 3; i++) { + assert_str_eq (keys[i], okeys[i]); + assert_ptr_eq (keys[i], okeys[i]); + assert_str_eq (values[i], ovalues[i]); + assert_ptr_eq (values[i], ovalues[i]); + } +} + +static void +test_set_get (void) +{ + 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); + assert_ptr_eq (check, value); + + p11_dict_free (map); +} + +static void +test_set_get_remove (void) +{ + char *key = "KEY"; + char *value = "VALUE"; + char *check; + p11_dict *map; + bool ret; + + map = p11_dict_new (p11_dict_str_hash, p11_dict_str_equal, NULL, NULL); + + if (!p11_dict_set (map, key, value)) + assert_not_reached (); + + check = p11_dict_get (map, key); + assert_ptr_eq (check, value); + + ret = p11_dict_remove (map, key); + assert_num_eq (true, ret); + ret = p11_dict_remove (map, key); + assert_num_eq (false, ret); + + check = p11_dict_get (map, key); + assert (check == NULL); + + p11_dict_free (map); +} + +static void +test_set_clear (void) +{ + 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)) + assert_not_reached (); + + p11_dict_clear (map); + + check = p11_dict_get (map, key); + assert (check == NULL); + + p11_dict_free (map); +} + +static void +test_remove_destroys (void) +{ + p11_dict *map; + Key key = { 8, 0 }; + int value = 0; + bool ret; + + map = p11_dict_new (key_hash, key_equal, key_destroy, value_destroy); + assert_ptr_not_null (map); + if (!p11_dict_set (map, &key, &value)) + assert_not_reached (); + + ret = p11_dict_remove (map, &key); + assert_num_eq (true, ret); + assert_num_eq (true, key.freed); + assert_num_eq (2, value); + + /* should not be destroyed again */ + key.freed = false; + value = 0; + + ret = p11_dict_remove (map, &key); + assert_num_eq (false, ret); + assert_num_eq (false, key.freed); + assert_num_eq (0, value); + + /* should not be destroyed again */ + key.freed = false; + value = 0; + + p11_dict_free (map); + + assert_num_eq (false, key.freed); + assert_num_eq (0, value); +} + +static void +test_set_destroys (void) +{ + p11_dict *map; + Key key = { 8, 0 }; + Key key2 = { 8, 0 }; + int value, value2; + bool ret; + + map = p11_dict_new (key_hash, key_equal, key_destroy, value_destroy); + assert_ptr_not_null (map); + if (!p11_dict_set (map, &key, &value)) + assert_not_reached (); + + key.freed = key2.freed = false; + value = value2 = 0; + + /* Setting same key and value, should not be destroyed */ + ret = p11_dict_set (map, &key, &value); + assert_num_eq (true, ret); + assert_num_eq (false, key.freed); + assert_num_eq (false, key2.freed); + assert_num_eq (0, value); + assert_num_eq (0, value2); + + key.freed = key2.freed = false; + value = value2 = 0; + + /* Setting a new key same value, key should be destroyed */ + ret = p11_dict_set (map, &key2, &value); + assert_num_eq (true, ret); + assert_num_eq (true, key.freed); + assert_num_eq (false, key2.freed); + assert_num_eq (0, value); + assert_num_eq (0, value2); + + key.freed = key2.freed = false; + value = value2 = 0; + + /* Setting same key, new value, value should be destroyed */ + ret = p11_dict_set (map, &key2, &value2); + assert_num_eq (true, ret); + assert_num_eq (false, key.freed); + assert_num_eq (false, key2.freed); + assert_num_eq (2, value); + assert_num_eq (0, value2); + + key.freed = key2.freed = false; + value = value2 = 0; + + /* Setting new key new value, both should be destroyed */ + ret = p11_dict_set (map, &key, &value); + assert_num_eq (true, ret); + assert_num_eq (false, key.freed); + assert_num_eq (true, key2.freed); + assert_num_eq (0, value); + assert_num_eq (2, value2); + + key.freed = key2.freed = false; + value = value2 = 0; + + p11_dict_free (map); + assert_num_eq (true, key.freed); + assert_num_eq (2, value); + assert_num_eq (false, key2.freed); + assert_num_eq (0, value2); +} + + +static void +test_clear_destroys (void) +{ + p11_dict *map; + Key key = { 18, 0 }; + int value = 0; + + map = p11_dict_new (key_hash, key_equal, key_destroy, value_destroy); + assert_ptr_not_null (map); + if (!p11_dict_set (map, &key, &value)) + assert_not_reached (); + + p11_dict_clear (map); + assert_num_eq (true, key.freed); + assert_num_eq (2, value); + + /* should not be destroyed again */ + key.freed = false; + value = 0; + + p11_dict_clear (map); + assert_num_eq (false, key.freed); + assert_num_eq (0, value); + + /* should not be destroyed again */ + key.freed = false; + value = 0; + + p11_dict_free (map); + + assert_num_eq (false, key.freed); + assert_num_eq (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 (void) +{ + 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)); + assert (value != NULL); + *value = i; + if (!p11_dict_set (map, value, value)) + assert_not_reached (); + } + + for (i = 0; i < 20000; ++i) { + value = p11_dict_get (map, &i); + assert_ptr_not_null (value); + assert_num_eq (i, *value); + } + + p11_dict_free (map); +} + +static void +test_hash_count (void) +{ + p11_dict *map; + int *value; + int i; + bool ret; + + map = p11_dict_new (p11_dict_intptr_hash, p11_dict_intptr_equal, NULL, free); + + assert_num_eq (0, p11_dict_size (map)); + + for (i = 0; i < 20000; ++i) { + value = malloc (sizeof (int)); + assert (value != NULL); + *value = i; + if (!p11_dict_set (map, value, value)) + assert_not_reached (); + assert_num_eq (i + 1, p11_dict_size (map)); + } + + for (i = 0; i < 20000; ++i) { + ret = p11_dict_remove (map, &i); + assert_num_eq (true, ret); + assert_num_eq (20000 - (i + 1), p11_dict_size (map)); + } + + p11_dict_clear (map); + assert_num_eq (0, p11_dict_size (map)); + + p11_dict_free (map); +} + +static void +test_hash_ulongptr (void) +{ + 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)); + assert (value != NULL); + *value = i; + if (!p11_dict_set (map, value, value)) + assert_not_reached (); + } + + for (i = 0; i < 20000; ++i) { + value = p11_dict_get (map, &i); + assert_ptr_not_null (value); + assert_num_eq (i, *value); + } + + p11_dict_free (map); +} + +int +main (int argc, + char *argv[]) +{ + p11_test (test_create, "/dict/create"); + p11_test (test_set_get, "/dict/set-get"); + p11_test (test_set_get_remove, "/dict/set-get-remove"); + p11_test (test_remove_destroys, "/dict/remove-destroys"); + p11_test (test_set_clear, "/dict/set-clear"); + p11_test (test_set_destroys, "/dict/set-destroys"); + p11_test (test_clear_destroys, "/dict/clear-destroys"); + p11_test (test_free_null, "/dict/free-null"); + p11_test (test_free_destroys, "/dict/free-destroys"); + p11_test (test_iterate, "/dict/iterate"); + p11_test (test_iterate_remove, "/dict/iterate-remove"); + p11_test (test_hash_add_check_lots_and_collisions, "/dict/add-check-lots-and-collisions"); + p11_test (test_hash_count, "/dict/count"); + p11_test (test_hash_ulongptr, "/dict/ulongptr"); + return p11_test_run (argc, argv); +} diff --git a/common/test-hash.c b/common/test-hash.c new file mode 100644 index 0000000..a12d5a4 --- /dev/null +++ b/common/test-hash.c @@ -0,0 +1,106 @@ +/* + * 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 "test.h" + +#include +#include +#include +#include +#include + +#include "hash.h" + +static void +test_murmur3 (void) +{ + uint32_t one, two, four, seven, eleven, split; + + assert (sizeof (one) == P11_HASH_MURMUR3_LEN); + + p11_hash_murmur3 ((unsigned char *)&one, "one", 3, NULL); + p11_hash_murmur3 ((unsigned char *)&two, "two", 3, NULL); + p11_hash_murmur3 ((unsigned char *)&four, "four", 4, NULL); + p11_hash_murmur3 ((unsigned char *)&seven, "seven", 5, NULL); + p11_hash_murmur3 ((unsigned char *)&eleven, "eleven", 6, NULL); + p11_hash_murmur3 ((unsigned char *)&split, "ele", 3, "ven", 3, NULL); + + assert (one != two); + assert (one != four); + assert (one != seven); + assert (one != eleven); + + assert (two != four); + assert (two != seven); + assert (two != eleven); + + assert (four != seven); + assert (four != eleven); + + assert (split == eleven); +} + +static void +test_murmur3_incr (void) +{ + uint32_t first, second; + + p11_hash_murmur3 ((unsigned char *)&first, + "this is the long input!", (size_t)23, + NULL); + + p11_hash_murmur3 ((unsigned char *)&second, + "this", (size_t)4, + " ", (size_t)1, + "is ", (size_t)3, + "the long ", (size_t)9, + "in", (size_t)2, + "p", (size_t)1, + "u", (size_t)1, + "t", (size_t)1, + "!", (size_t)1, + NULL); + + assert_num_eq (first, second); +} + +int +main (int argc, + char *argv[]) +{ + p11_test (test_murmur3, "/hash/murmur3"); + p11_test (test_murmur3_incr, "/hash/murmur3-incr"); + return p11_test_run (argc, argv); +} diff --git a/common/test-lexer.c b/common/test-lexer.c new file mode 100644 index 0000000..7d18e87 --- /dev/null +++ b/common/test-lexer.c @@ -0,0 +1,253 @@ +/* + * Copyright (c) 2013 Red Hat Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * * The names of contributors to this software may not be + * used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * Author: Stef Walter + */ + +#include "config.h" +#include "test.h" + +#include +#include +#include + +#include "compat.h" +#include "debug.h" +#include "lexer.h" +#include "message.h" + +typedef struct { + int tok_type; + const char *name; + const char *value; +} expected_tok; + +static void +check_lex_msg (const char *file, + int line, + const char *function, + const expected_tok *expected, + const char *input, + bool failure) +{ + p11_lexer lexer; + size_t len; + bool failed; + int i; + + p11_lexer_init (&lexer, "test", input, strlen (input)); + for (i = 0; p11_lexer_next (&lexer, &failed); i++) { + if (expected[i].tok_type != lexer.tok_type) + p11_test_fail (file, line, function, + "lexer token type does not match: (%d != %d)", + expected[i].tok_type, lexer.tok_type); + switch (lexer.tok_type) { + case TOK_FIELD: + if (strcmp (expected[i].name, lexer.tok.field.name) != 0) + p11_test_fail (file, line, function, + "field name doesn't match: (%s != %s)", + expected[i].name, lexer.tok.field.name); + if (strcmp (expected[i].value, lexer.tok.field.value) != 0) + p11_test_fail (file, line, function, + "field value doesn't match: (%s != %s)", + expected[i].value, lexer.tok.field.value); + break; + case TOK_SECTION: + if (strcmp (expected[i].name, lexer.tok.field.name) != 0) + p11_test_fail (file, line, function, + "section name doesn't match: (%s != %s)", + expected[i].name, lexer.tok.field.name); + break; + case TOK_PEM: + len = strlen (expected[i].name); + if (lexer.tok.pem.length < len || + strncmp (lexer.tok.pem.begin, expected[i].name, len) != 0) { + p11_test_fail (file, line, function, + "wrong type of PEM block: %s", + expected[i].name); + } + break; + case TOK_EOF: + p11_test_fail (file, line, function, "eof should not be recieved"); + break; + } + } + + if (failure && !failed) + p11_test_fail (file, line, function, "lexing didn't fail"); + else if (!failure && failed) + p11_test_fail (file, line, function, "lexing failed"); + if (TOK_EOF != expected[i].tok_type) + p11_test_fail (file, line, function, "premature end of lexing"); + + p11_lexer_done (&lexer); +} + +#define check_lex_success(expected, input) \ + check_lex_msg (__FILE__, __LINE__, __FUNCTION__, expected, input, false) + +#define check_lex_failure(expected, input) \ + check_lex_msg (__FILE__, __LINE__, __FUNCTION__, expected, input, true) + +static void +test_basic (void) +{ + const char *input = "[the header]\n" + "field: value\n" + "-----BEGIN BLOCK1-----\n" + "aYNNXqshlVxCdo8QfKeXh3GUzd/yn4LYIVgQrx4a\n" + "-----END BLOCK1-----\n"; + + const expected_tok expected[] = { + { TOK_SECTION, "the header" }, + { TOK_FIELD, "field", "value" }, + { TOK_PEM, "-----BEGIN BLOCK1-----\n", }, + { TOK_EOF } + }; + + check_lex_success (expected, input); +} + +static void +test_corners (void) +{ + const char *input = "\r\n" /* blankline */ + " [the header]\r\n" /* bad line endings */ + " field: value \r\n" /* whitespace */ + "number: 2\n" /* extra space*/ + "number :3\n" /* extra space*/ + "number : 4\n" /* extra space*/ + "\n" + " # A comment \n" + "not-a-comment: # value\n" + "-----BEGIN BLOCK1-----\r\n" + "aYNNXqshlVxCdo8QfKeXh3GUzd/yn4LYIVgQrx4a\r\n" + "-----END BLOCK1-----"; /* no new line */ + + const expected_tok expected[] = { + { TOK_SECTION, "the header" }, + { TOK_FIELD, "field", "value" }, + { TOK_FIELD, "number", "2" }, + { TOK_FIELD, "number", "3" }, + { TOK_FIELD, "number", "4" }, + { TOK_FIELD, "not-a-comment", "# value" }, + { TOK_PEM, "-----BEGIN BLOCK1-----\r\n", }, + { TOK_EOF } + }; + + check_lex_success (expected, input); +} + +static void +test_following (void) +{ + const char *input = "-----BEGIN BLOCK1-----\n" + "aYNNXqshlVxCdo8QfKeXh3GUzd/yn4LYIVgQrx4a\n" + "-----END BLOCK1-----\n" + "field: value"; + + const expected_tok expected[] = { + { TOK_PEM, "-----BEGIN BLOCK1-----\n", }, + { TOK_FIELD, "field", "value" }, + { TOK_EOF } + }; + + check_lex_success (expected, input); +} + +static void +test_bad_pem (void) +{ + const char *input = "field: value\n" + "-----BEGIN BLOCK1-----\n" + "aYNNXqshlVxCdo8QfKeXh3GUzd/yn4LYIVgQrx4a\n"; + + const expected_tok expected[] = { + { TOK_FIELD, "field", "value" }, + { TOK_EOF } + }; + + p11_message_quiet (); + + check_lex_failure (expected, input); + + p11_message_loud (); +} + +static void +test_bad_section (void) +{ + const char *input = "field: value\n" + "[section\n" + "bad]\n"; + + const expected_tok expected[] = { + { TOK_FIELD, "field", "value" }, + { TOK_EOF } + }; + + p11_message_quiet (); + + check_lex_failure (expected, input); + + p11_message_loud (); +} + +static void +test_bad_value (void) +{ + const char *input = "field_value\n" + "[section\n" + "bad]\n"; + + const expected_tok expected[] = { + { TOK_EOF } + }; + + p11_message_quiet (); + + check_lex_failure (expected, input); + + p11_message_loud (); +} + +int +main (int argc, + char *argv[]) +{ + p11_test (test_basic, "/lexer/basic"); + p11_test (test_corners, "/lexer/corners"); + p11_test (test_following, "/lexer/following"); + p11_test (test_bad_pem, "/lexer/bad-pem"); + p11_test (test_bad_section, "/lexer/bad-section"); + p11_test (test_bad_value, "/lexer/bad-value"); + return p11_test_run (argc, argv); +} diff --git a/common/test-message.c b/common/test-message.c new file mode 100644 index 0000000..63ecf31 --- /dev/null +++ b/common/test-message.c @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2013 Red Hat Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * * The names of contributors to this software may not be + * used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * Author: Stef Walter + */ + +#include "config.h" +#include "test.h" + +#include "message.h" + +#include +#include + +static void +test_with_err (void) +{ + const char *last; + char *expected; + + errno = E2BIG; + p11_message_err (ENOENT, "Details: %s", "value"); + last = p11_message_last (); + + if (asprintf (&expected, "Details: value: %s", strerror (ENOENT)) < 0) + assert_not_reached (); + assert_str_eq (expected, last); + free (expected); +} + +int +main (int argc, + char *argv[]) +{ + p11_test (test_with_err, "/message/with-err"); + return p11_test_run (argc, argv); +} diff --git a/common/test-path.c b/common/test-path.c new file mode 100644 index 0000000..57619c8 --- /dev/null +++ b/common/test-path.c @@ -0,0 +1,216 @@ +/* + * Copyright (c) 2013 Red Hat Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * * The names of contributors to this software may not be + * used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * Author: Stef Walter + */ + +#include "config.h" +#include "test.h" + +#include +#include +#include + +#include "compat.h" +#include "path.h" + +static void +test_base (void) +{ + struct { + const char *in; + const char *out; + } fixtures[] = { + { "/this/is/a/path", "path" }, + { "/this/is/a/folder/", "folder" }, + { "folder/", "folder" }, + { "/", "" }, + { "this", "this" }, +#ifdef OS_WIN32 + { "\\this\\is\\a\\path", "path" }, + { "\\this\\is\\a\\folder\\", "folder" }, + { "C:\\this\\is\\a\\path", "path" }, + { "D:\\this\\is\\a\\folder\\", "folder" }, + { "folder\\", "folder" }, + { "\\", "" }, +#endif + { NULL }, + }; + + char *out; + int i; + + for (i = 0; fixtures[i].in != NULL; i++) { + out = p11_path_base (fixtures[i].in); + assert_str_eq (fixtures[i].out, out); + free (out); + } +} + +#define assert_str_eq_free(ex, ac) \ + do { const char *__s1 = (ex); \ + char *__s2 = (ac); \ + if (__s1 && __s2 && strcmp (__s1, __s2) == 0) ; else \ + p11_test_fail (__FILE__, __LINE__, __FUNCTION__, "assertion failed (%s == %s): (%s == %s)", \ + #ex, #ac, __s1 ? __s1 : "(null)", __s2 ? __s2 : "(null)"); \ + free (__s2); \ + } while (0) + +static void +test_build (void) +{ +#ifdef OS_UNIX + assert_str_eq_free ("/root/second", + p11_path_build ("/root", "second", NULL)); + assert_str_eq_free ("/root/second", + p11_path_build ("/root", "/second", NULL)); + assert_str_eq_free ("/root/second", + p11_path_build ("/root/", "second", NULL)); + assert_str_eq_free ("/root/second/third", + p11_path_build ("/root", "second", "third", NULL)); + assert_str_eq_free ("/root/second/third", + p11_path_build ("/root", "/second/third", NULL)); +#else /* OS_WIN32 */ + assert_str_eq_free ("C:\\root\\second", + p11_path_build ("C:\\root", "second", NULL)); + assert_str_eq_free ("C:\\root\\second", + p11_path_build ("C:\\root", "\\second", NULL)); + assert_str_eq_free ("C:\\root\\second", + p11_path_build ("C:\\root\\", "second", NULL)); + assert_str_eq_free ("C:\\root\\second\\third", + p11_path_build ("C:\\root", "second", "third", NULL)); + assert_str_eq_free ("C:\\root\\second/third", + p11_path_build ("C:\\root", "second/third", NULL)); +#endif +} + +static void +test_expand (void) +{ + char *path; + +#ifdef OS_UNIX + putenv ("HOME=/home/blah"); + assert_str_eq_free ("/home/blah/my/path", + p11_path_expand ("~/my/path")); + assert_str_eq_free ("/home/blah", + p11_path_expand ("~")); + putenv ("XDG_CONFIG_HOME=/my"); + assert_str_eq_free ("/my/path", + p11_path_expand ("~/.config/path")); + putenv ("XDG_CONFIG_HOME="); + assert_str_eq_free ("/home/blah/.config/path", + p11_path_expand ("~/.config/path")); +#else /* OS_WIN32 */ + putenv ("HOME=C:\\Users\\blah"); + assert_str_eq_free ("C:\\Users\\blah\\path", + p11_path_expand ("~/my/path")); + assert_str_eq_free ("C:\\Users\\blah\\path", + p11_path_expand ("~\\path")); +#endif + + putenv("HOME="); + path = p11_path_expand ("~/this/is/my/path"); + assert (strstr (path, "this/is/my/path") != NULL); + free (path); +} + +static void +test_absolute (void) +{ +#ifdef OS_UNIX + assert (p11_path_absolute ("/home")); + assert (!p11_path_absolute ("home")); +#else /* OS_WIN32 */ + assert (p11_path_absolute ("C:\\home")); + assert (!p11_path_absolute ("home")); + assert (p11_path_absolute ("/home")); +#endif +} + +static void +test_parent (void) +{ + assert_str_eq_free ("/", p11_path_parent ("/root")); + assert_str_eq_free ("/", p11_path_parent ("/root/")); + assert_str_eq_free ("/", p11_path_parent ("/root//")); + assert_str_eq_free ("/root", p11_path_parent ("/root/second")); + assert_str_eq_free ("/root", p11_path_parent ("/root//second")); + assert_str_eq_free ("/root", p11_path_parent ("/root//second//")); + assert_str_eq_free ("/root", p11_path_parent ("/root///second")); + assert_str_eq_free ("/root/second", p11_path_parent ("/root/second/test.file")); + assert_ptr_eq (NULL, p11_path_parent ("/")); + assert_ptr_eq (NULL, p11_path_parent ("//")); + assert_ptr_eq (NULL, p11_path_parent ("")); +} + +static void +test_prefix (void) +{ + assert (p11_path_prefix ("/test/second", "/test")); + assert (!p11_path_prefix ("/test", "/test")); + assert (!p11_path_prefix ("/different/prefix", "/test")); + assert (!p11_path_prefix ("/te", "/test")); + assert (!p11_path_prefix ("/test", "/test/blah")); + assert (p11_path_prefix ("/test/other/second", "/test")); + assert (p11_path_prefix ("/test//other//second", "/test")); +} + +static void +test_canon (void) +{ + char *test; + + test = strdup ("2309haonutb;AOE@#$O "); + p11_path_canon (test); + assert_str_eq (test, "2309haonutb_AOE___O_"); + free (test); + + test = strdup ("22@# %ATI@#$onot"); + p11_path_canon (test); + assert_str_eq (test, "22____ATI___onot"); + free (test); +} + +int +main (int argc, + char *argv[]) +{ + p11_test (test_base, "/path/base"); + p11_test (test_build, "/path/build"); + p11_test (test_expand, "/path/expand"); + p11_test (test_absolute, "/path/absolute"); + p11_test (test_parent, "/path/parent"); + p11_test (test_prefix, "/path/prefix"); + p11_test (test_canon, "/path/canon"); + + return p11_test_run (argc, argv); +} diff --git a/common/test-tests.c b/common/test-tests.c new file mode 100644 index 0000000..ba31d83 --- /dev/null +++ b/common/test-tests.c @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2013 Red Hat Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * * The names of contributors to this software may not be + * used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * Author: Stef Walter + */ + +#include "config.h" +#include "test.h" + +#include + +static void +test_success (void) +{ + /* Yup, nothing */ +} + + +static void +test_failure (void) +{ + if (getenv ("TEST_FAIL")) { + p11_test_fail (__FILE__, __LINE__, __FUNCTION__, + "Unconditional test failure due to TEST_FAIL environment variable"); + } +} + +static void +test_memory (void) +{ + char *mem; + + if (getenv ("TEST_FAIL")) { + mem = malloc (1); + assert (mem != NULL); + free (mem); + *mem = 1; + } +} + + +static void +test_leak (void) +{ + char *mem; + + if (getenv ("TEST_FAIL")) { + mem = malloc (1); + assert (mem != NULL); + *mem = 1; + } +} + +int +main (int argc, + char *argv[]) +{ + p11_test (test_success, "/test/success"); + + if (getenv ("TEST_FAIL")) { + p11_test (test_failure, "/test/failure"); + p11_test (test_memory, "/test/memory"); + p11_test (test_leak, "/test/leak"); + } + + return p11_test_run (argc, argv); +} diff --git a/common/test-url.c b/common/test-url.c new file mode 100644 index 0000000..892bf3c --- /dev/null +++ b/common/test-url.c @@ -0,0 +1,164 @@ +/* + * Copyright (c) 2013 Red Hat Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * * The names of contributors to this software may not be + * used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * Author: Stef Walter + */ + +#include "config.h" +#include "test.h" + +#include "debug.h" +#include "message.h" + +#include +#include +#include +#include + +#include "url.h" + +static void +check_decode_msg (const char *file, + int line, + const char *function, + const char *input, + ssize_t input_len, + const char *expected, + size_t expected_len) +{ + unsigned char *decoded; + size_t length; + + if (input_len < 0) + input_len = strlen (input); + decoded = p11_url_decode (input, input + input_len, "", &length); + + if (expected == NULL) { + if (decoded != NULL) + p11_test_fail (file, line, function, "decoding should have failed"); + + } else { + if (decoded == NULL) + p11_test_fail (file, line, function, "decoding failed"); + if (expected_len != length) + p11_test_fail (file, line, function, "wrong length: (%lu != %lu)", + (unsigned long)expected_len, (unsigned long)length); + if (memcmp (decoded, expected, length) != 0) + p11_test_fail (file, line, function, "decoding wrong"); + free (decoded); + } +} + +#define check_decode_success(input, input_len, expected, expected_len) \ + check_decode_msg (__FILE__, __LINE__, __FUNCTION__, input, input_len, expected, expected_len) + +#define check_decode_failure(input, input_len) \ + check_decode_msg (__FILE__, __LINE__, __FUNCTION__, input, input_len, NULL, 0) + +static void +test_decode_success (void) +{ + check_decode_success ("%54%45%53%54%00", -1, "TEST", 5); + check_decode_success ("%54%45%53%54%00", 6, "TE", 2); + check_decode_success ("%54est%00", -1, "Test", 5); +} + +static void +test_decode_skip (void) +{ + const char *input = "%54 %45 %53 %54 %00"; + unsigned char *decoded; + size_t length; + + decoded = p11_url_decode (input, input + strlen (input), P11_URL_WHITESPACE, &length); + assert_str_eq ("TEST", (char *)decoded); + assert_num_eq (5, length); + + free (decoded); +} + +static void +test_decode_failure (void) +{ + /* Early termination */ + check_decode_failure ("%54%45%53%5", -1); + check_decode_failure ("%54%45%53%", -1); + + /* Not hex characters */ + check_decode_failure ("%54%XX%53%54%00", -1); +} + +static void +test_encode (void) +{ + const unsigned char *input = (unsigned char *)"TEST"; + p11_buffer buf; + + if (!p11_buffer_init_null (&buf, 5)) + assert_not_reached (); + + p11_url_encode (input, input + 5, "", &buf); + assert (p11_buffer_ok (&buf)); + assert_str_eq ("%54%45%53%54%00", (char *)buf.data); + assert_num_eq (15, buf.len); + + p11_buffer_uninit (&buf); +} + +static void +test_encode_verbatim (void) +{ + const unsigned char *input = (unsigned char *)"TEST"; + p11_buffer buf; + + if (!p11_buffer_init_null (&buf, 5)) + assert_not_reached (); + + p11_url_encode (input, input + 5, "ES", &buf); + assert (p11_buffer_ok (&buf)); + assert_str_eq ("%54ES%54%00", (char *)buf.data); + assert_num_eq (11, buf.len); + + p11_buffer_uninit (&buf); +} + +int +main (int argc, + char *argv[]) +{ + p11_test (test_decode_success, "/url/decode-success"); + p11_test (test_decode_skip, "/url/decode-skip"); + p11_test (test_decode_failure, "/url/decode-failure"); + + p11_test (test_encode, "/url/encode"); + p11_test (test_encode_verbatim, "/url/encode-verbatim"); + return p11_test_run (argc, argv); +} diff --git a/common/tests/Makefile.am b/common/tests/Makefile.am deleted file mode 100644 index 7bbc4ce..0000000 --- a/common/tests/Makefile.am +++ /dev/null @@ -1,39 +0,0 @@ - -include $(top_srcdir)/build/Makefile.tests - -COMMON = $(top_srcdir)/common - -AM_CPPFLAGS = \ - -I$(top_srcdir) \ - -I$(srcdir)/.. \ - -I$(COMMON) \ - -DBUILDDIR=\"$(abs_builddir)\" \ - $(TEST_CFLAGS) - -LDADD = \ - $(NULL) - -CHECK_PROGS = \ - test-tests \ - test-compat \ - test-hash \ - test-dict \ - test-array \ - test-constants \ - test-attrs \ - test-buffer \ - test-url \ - test-path \ - test-lexer \ - test-message \ - $(NULL) - -noinst_PROGRAMS = \ - frob-getauxval \ - $(CHECK_PROGS) - -TESTS = $(CHECK_PROGS) - -LDADD += \ - $(top_builddir)/common/libp11-test.la \ - $(top_builddir)/common/libp11-common.la diff --git a/common/tests/frob-getauxval.c b/common/tests/frob-getauxval.c deleted file mode 100644 index 02745be..0000000 --- a/common/tests/frob-getauxval.c +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright (c) 2013 Red Hat Inc. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * * Redistributions of source code must retain the above - * copyright notice, this list of conditions and the - * following disclaimer. - * * Redistributions in binary form must reproduce the - * above copyright notice, this list of conditions and - * the following disclaimer in the documentation and/or - * other materials provided with the distribution. - * * The names of contributors to this software may not be - * used to endorse or promote products derived from this - * software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF - * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH - * DAMAGE. - * - * Author: Stef Walter - */ - -#include "config.h" -#include "compat.h" - -#include -#include -#include -#include - -int -main (int argc, - char *argv[]) -{ - unsigned long type = 0; - unsigned long ret; - - if (argc == 2) - type = atoi (argv[1]); - - if (type == 0) { - fprintf (stderr, "usage: frob-getauxval 23"); - abort (); - } - - ret = getauxval (type); - printf ("getauxval(%lu) == %lu\n", type, ret); - return (int)ret; -} diff --git a/common/tests/test-array.c b/common/tests/test-array.c deleted file mode 100644 index 695917a..0000000 --- a/common/tests/test-array.c +++ /dev/null @@ -1,209 +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 "array.h" -#include "test.h" - -static void -test_create (void) -{ - p11_array *array; - - array = p11_array_new (NULL); - assert_ptr_not_null (array); - p11_array_free (array); -} - -static void -test_free_null (void) -{ - p11_array_free (NULL); -} - -static void -destroy_value (void *data) -{ - int *value = data; - *value = 2; -} - -static void -test_free_destroys (void) -{ - p11_array *array; - int value = 0; - - array = p11_array_new (destroy_value); - assert_ptr_not_null (array); - if (!p11_array_push (array, &value)) - assert_not_reached (); - p11_array_free (array); - - assert_num_eq (2, value); -} - -static void -test_add (void) -{ - char *value = "VALUE"; - p11_array *array; - - array = p11_array_new (NULL); - if (!p11_array_push (array, value)) - assert_not_reached (); - - assert_num_eq (1, array->num); - assert_ptr_eq (array->elem[0], value); - - p11_array_free (array); -} - -static void -test_add_remove (void) -{ - char *value = "VALUE"; - p11_array *array; - - array = p11_array_new (NULL); - if (!p11_array_push (array, value)) - assert_not_reached (); - - assert_num_eq (1, array->num); - - assert_ptr_eq (array->elem[0], value); - - p11_array_remove (array, 0); - - assert_num_eq (0, array->num); - - p11_array_free (array); -} - -static void -test_remove_destroys (void) -{ - p11_array *array; - int value = 0; - - array = p11_array_new (destroy_value); - if (!p11_array_push (array, &value)) - assert_not_reached (); - - p11_array_remove (array, 0); - - assert_num_eq (2, value); - - /* should not be destroyed again */ - value = 0; - - p11_array_free (array); - - assert_num_eq (0, value); -} - -static void -test_remove_and_count (void) -{ - p11_array *array; - int *value; - int i; - - array = p11_array_new (free); - - assert_num_eq (0, array->num); - - for (i = 0; i < 20000; ++i) { - value = malloc (sizeof (int)); - assert (value != NULL); - *value = i; - if (!p11_array_push (array, value)) - assert_not_reached (); - assert_num_eq (i + 1, array->num); - } - - for (i = 10; i < 20000; ++i) { - p11_array_remove (array, 10); - assert_num_eq (20010 - (i + 1), array->num); - } - - assert_num_eq (10, array->num); - - p11_array_free (array); -} - -static void -test_clear_destroys (void) -{ - p11_array *array; - int value = 0; - - array = p11_array_new (destroy_value); - if (!p11_array_push (array, &value)) - assert_not_reached (); - - assert_num_eq (1, array->num); - - p11_array_clear (array); - - assert_num_eq (2, value); - assert_num_eq (0, array->num); - - /* should not be destroyed again */ - value = 0; - - p11_array_free (array); - - assert_num_eq (0, value); -} - -int -main (int argc, - char *argv[]) -{ - p11_test (test_create, "/array/create"); - p11_test (test_add, "/array/add"); - p11_test (test_add_remove, "/array/add-remove"); - p11_test (test_remove_destroys, "/array/remove-destroys"); - p11_test (test_remove_and_count, "/array/remove-and-count"); - p11_test (test_free_null, "/array/free-null"); - p11_test (test_free_destroys, "/array/free-destroys"); - p11_test (test_clear_destroys, "/array/clear-destroys"); - return p11_test_run (argc, argv); -} diff --git a/common/tests/test-attrs.c b/common/tests/test-attrs.c deleted file mode 100644 index 79895e2..0000000 --- a/common/tests/test-attrs.c +++ /dev/null @@ -1,757 +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 "test.h" - -#include -#include -#include - -#include "attrs.h" -#include "debug.h" - -static void -test_terminator (void) -{ - CK_ATTRIBUTE attrs[] = { - { CKA_LABEL, "label", 5 }, - { CKA_LABEL, NULL, 0 }, - { CKA_INVALID }, - }; - - assert_num_eq (true, p11_attrs_terminator (attrs + 2)); - assert_num_eq (true, p11_attrs_terminator (NULL)); - assert_num_eq (false, p11_attrs_terminator (attrs)); - assert_num_eq (false, p11_attrs_terminator (attrs + 1)); -} - -static void -test_count (void) -{ - CK_BBOOL vtrue = CK_TRUE; - - CK_ATTRIBUTE attrs[] = { - { CKA_LABEL, "label", 5 }, - { CKA_TOKEN, &vtrue, sizeof (vtrue) }, - { CKA_INVALID }, - }; - - CK_ATTRIBUTE empty[] = { - { CKA_INVALID }, - }; - - assert_num_eq (2, p11_attrs_count (attrs)); - assert_num_eq (0, p11_attrs_count (NULL)); - assert_num_eq (0, p11_attrs_count (empty)); -} - -static void -test_build_one (void) -{ - CK_ATTRIBUTE *attrs; - CK_ATTRIBUTE add = { CKA_LABEL, "yay", 3 }; - - attrs = p11_attrs_build (NULL, &add, NULL); - - /* Test the first attribute */ - assert_ptr_not_null (attrs); - assert (attrs->type == CKA_LABEL); - assert_num_eq (3, attrs->ulValueLen); - assert (memcmp (attrs->pValue, "yay", 3) == 0); - - assert (attrs[1].type == CKA_INVALID); - - p11_attrs_free (attrs); -} - -static void -test_build_two (void) -{ - CK_ATTRIBUTE *attrs; - CK_ATTRIBUTE one = { CKA_LABEL, "yay", 3 }; - CK_ATTRIBUTE two = { CKA_VALUE, "eight", 5 }; - - attrs = p11_attrs_build (NULL, &one, &two, NULL); - - assert_ptr_not_null (attrs); - assert (attrs[0].type == CKA_LABEL); - assert_num_eq (3, attrs[0].ulValueLen); - assert (memcmp (attrs[0].pValue, "yay", 3) == 0); - - assert_ptr_not_null (attrs); - assert (attrs[1].type == CKA_VALUE); - assert_num_eq (5, attrs[1].ulValueLen); - assert (memcmp (attrs[1].pValue, "eight", 5) == 0); - - assert (attrs[2].type == CKA_INVALID); - - p11_attrs_free (attrs); -} - -static void -test_build_invalid (void) -{ - CK_ATTRIBUTE *attrs; - CK_ATTRIBUTE one = { CKA_LABEL, "yay", 3 }; - CK_ATTRIBUTE invalid = { CKA_INVALID }; - CK_ATTRIBUTE two = { CKA_VALUE, "eight", 5 }; - - attrs = p11_attrs_build (NULL, &one, &invalid, &two, NULL); - - assert_ptr_not_null (attrs); - assert (attrs[0].type == CKA_LABEL); - assert_num_eq (3, attrs[0].ulValueLen); - assert (memcmp (attrs[0].pValue, "yay", 3) == 0); - - assert_ptr_not_null (attrs); - assert (attrs[1].type == CKA_VALUE); - assert_num_eq (5, attrs[1].ulValueLen); - assert (memcmp (attrs[1].pValue, "eight", 5) == 0); - - assert (attrs[2].type == CKA_INVALID); - - p11_attrs_free (attrs); -} - -static void -test_buildn_two (void) -{ - CK_ATTRIBUTE *attrs; - CK_ATTRIBUTE add[] = { - { CKA_LABEL, "yay", 3 }, - { CKA_VALUE, "eight", 5 } - }; - - attrs = p11_attrs_buildn (NULL, add, 2); - - /* Test the first attribute */ - assert_ptr_not_null (attrs); - assert (attrs->type == CKA_LABEL); - assert_num_eq (3, attrs->ulValueLen); - assert (memcmp (attrs->pValue, "yay", 3) == 0); - - assert_ptr_not_null (attrs); - assert (attrs[1].type == CKA_VALUE); - assert_num_eq (5, attrs[1].ulValueLen); - assert (memcmp (attrs[1].pValue, "eight", 5) == 0); - - assert (attrs[2].type == CKA_INVALID); - - p11_attrs_free (attrs); -} - -static void -test_buildn_one (void) -{ - CK_ATTRIBUTE *attrs; - CK_ATTRIBUTE add = { CKA_LABEL, "yay", 3 }; - - attrs = p11_attrs_buildn (NULL, &add, 1); - - /* Test the first attribute */ - assert_ptr_not_null (attrs); - assert (attrs->type == CKA_LABEL); - assert_num_eq (3, attrs->ulValueLen); - assert (memcmp (attrs->pValue, "yay", 3) == 0); - - assert (attrs[1].type == CKA_INVALID); - - p11_attrs_free (attrs); -} - -static void -test_build_add (void) -{ - CK_ATTRIBUTE initial[] = { - { CKA_LABEL, "label", 5 }, - { CKA_VALUE, "nine", 4 }, - }; - - CK_ATTRIBUTE *attrs; - CK_ATTRIBUTE one = { CKA_LABEL, "yay", 3 }; - CK_ATTRIBUTE two = { CKA_TOKEN, "\x01", 1 }; - - attrs = p11_attrs_buildn (NULL, initial, 2); - attrs = p11_attrs_build (attrs, &one, &two, NULL); - - assert_ptr_not_null (attrs); - assert (attrs[0].type == CKA_LABEL); - assert_num_eq (3, attrs[0].ulValueLen); - assert (memcmp (attrs[0].pValue, "yay", 3) == 0); - - assert_ptr_not_null (attrs); - assert (attrs[1].type == CKA_VALUE); - assert_num_eq (4, attrs[1].ulValueLen); - assert (memcmp (attrs[1].pValue, "nine", 4) == 0); - - assert_ptr_not_null (attrs); - assert (attrs[2].type == CKA_TOKEN); - assert_num_eq (1, attrs[2].ulValueLen); - assert (memcmp (attrs[2].pValue, "\x01", 1) == 0); - - assert (attrs[3].type == CKA_INVALID); - - p11_attrs_free (attrs); -} - -static void -test_build_null (void) -{ - CK_ATTRIBUTE *attrs; - CK_ATTRIBUTE add = { CKA_LABEL, NULL, (CK_ULONG)-1 }; - - attrs = p11_attrs_build (NULL, &add, NULL); - - /* Test the first attribute */ - assert_ptr_not_null (attrs); - assert (attrs->type == CKA_LABEL); - assert (attrs->ulValueLen == (CK_ULONG)-1); - assert_ptr_eq (NULL, attrs->pValue); - - p11_attrs_free (attrs); -} - -static void -test_dup (void) -{ - CK_ATTRIBUTE *attrs; - CK_ATTRIBUTE original[] = { - { CKA_LABEL, "yay", 3 }, - { CKA_VALUE, "eight", 5 }, - { CKA_INVALID } - }; - - attrs = p11_attrs_dup (original); - - /* Test the first attribute */ - assert_ptr_not_null (attrs); - assert (attrs->type == CKA_LABEL); - assert_num_eq (3, attrs->ulValueLen); - assert (memcmp (attrs->pValue, "yay", 3) == 0); - - assert_ptr_not_null (attrs); - assert (attrs[1].type == CKA_VALUE); - assert_num_eq (5, attrs[1].ulValueLen); - assert (memcmp (attrs[1].pValue, "eight", 5) == 0); - - assert (attrs[2].type == CKA_INVALID); - - p11_attrs_free (attrs); -} - -static void -test_take (void) -{ - CK_ATTRIBUTE initial[] = { - { CKA_LABEL, "label", 5 }, - { CKA_VALUE, "nine", 4 }, - }; - - CK_ATTRIBUTE *attrs; - - attrs = p11_attrs_buildn (NULL, initial, 2); - attrs = p11_attrs_take (attrs, CKA_LABEL, strdup ("boooyah"), 7); - attrs = p11_attrs_take (attrs, CKA_TOKEN, strdup ("\x01"), 1); - assert_ptr_not_null (attrs); - - assert (attrs[0].type == CKA_LABEL); - assert_num_eq (7, attrs[0].ulValueLen); - assert (memcmp (attrs[0].pValue, "boooyah", 7) == 0); - - assert_ptr_not_null (attrs); - assert (attrs[1].type == CKA_VALUE); - assert_num_eq (4, attrs[1].ulValueLen); - assert (memcmp (attrs[1].pValue, "nine", 4) == 0); - - assert_ptr_not_null (attrs); - assert (attrs[2].type == CKA_TOKEN); - assert_num_eq (1, attrs[2].ulValueLen); - assert (memcmp (attrs[2].pValue, "\x01", 1) == 0); - - assert (attrs[3].type == CKA_INVALID); - - p11_attrs_free (attrs); -} - - -static void -test_merge_replace (void) -{ - CK_ATTRIBUTE initial[] = { - { CKA_LABEL, "label", 5 }, - { CKA_VALUE, "nine", 4 }, - }; - - CK_ATTRIBUTE extra[] = { - { CKA_LABEL, "boooyah", 7 }, - { CKA_APPLICATION, "disco", 5 }, - }; - - CK_ATTRIBUTE *attrs; - CK_ATTRIBUTE *merge; - - attrs = p11_attrs_buildn (NULL, initial, 2); - merge = p11_attrs_buildn (NULL, extra, 2); - attrs = p11_attrs_merge (attrs, merge, true); - assert_ptr_not_null (attrs); - - assert (attrs[0].type == CKA_LABEL); - assert_num_eq (7, attrs[0].ulValueLen); - assert (memcmp (attrs[0].pValue, "boooyah", 7) == 0); - - assert_ptr_not_null (attrs); - assert (attrs[1].type == CKA_VALUE); - assert_num_eq (4, attrs[1].ulValueLen); - assert (memcmp (attrs[1].pValue, "nine", 4) == 0); - - assert_ptr_not_null (attrs); - assert (attrs[2].type == CKA_APPLICATION); - assert_num_eq (5, attrs[2].ulValueLen); - assert (memcmp (attrs[2].pValue, "disco", 5) == 0); - - assert (attrs[3].type == CKA_INVALID); - - p11_attrs_free (attrs); -} - -static void -test_merge_empty (void) -{ - CK_ATTRIBUTE extra[] = { - { CKA_LABEL, "boooyah", 7 }, - { CKA_APPLICATION, "disco", 5 }, - }; - - CK_ATTRIBUTE *attrs = NULL; - CK_ATTRIBUTE *merge; - - merge = p11_attrs_buildn (NULL, extra, 2); - attrs = p11_attrs_merge (attrs, merge, true); - assert_ptr_not_null (attrs); - assert_ptr_eq (merge, attrs); - - p11_attrs_free (attrs); -} - -static void -test_merge_augment (void) -{ - CK_ATTRIBUTE initial[] = { - { CKA_LABEL, "label", 5 }, - { CKA_VALUE, "nine", 4 }, - }; - - CK_ATTRIBUTE extra[] = { - { CKA_LABEL, "boooyah", 7 }, - { CKA_APPLICATION, "disco", 5 }, - }; - - CK_ATTRIBUTE *attrs; - CK_ATTRIBUTE *merge; - - attrs = p11_attrs_buildn (NULL, initial, 2); - merge = p11_attrs_buildn (NULL, extra, 2); - attrs = p11_attrs_merge (attrs, merge, false); - assert_ptr_not_null (attrs); - - assert (attrs[0].type == CKA_LABEL); - assert_num_eq (5, attrs[0].ulValueLen); - assert (memcmp (attrs[0].pValue, "label", 5) == 0); - - assert_ptr_not_null (attrs); - assert (attrs[1].type == CKA_VALUE); - assert_num_eq (4, attrs[1].ulValueLen); - assert (memcmp (attrs[1].pValue, "nine", 4) == 0); - - assert_ptr_not_null (attrs); - assert (attrs[2].type == CKA_APPLICATION); - assert_num_eq (5, attrs[2].ulValueLen); - assert (memcmp (attrs[2].pValue, "disco", 5) == 0); - - assert (attrs[3].type == CKA_INVALID); - - p11_attrs_free (attrs); -} - -static void -test_free_null (void) -{ - p11_attrs_free (NULL); -} - -static void -test_equal (void) -{ - char *data = "extra attribute"; - CK_ATTRIBUTE one = { CKA_LABEL, "yay", 3 }; - CK_ATTRIBUTE null = { CKA_LABEL, NULL, 3 }; - CK_ATTRIBUTE two = { CKA_VALUE, "yay", 3 }; - CK_ATTRIBUTE other = { CKA_VALUE, data, 5 }; - CK_ATTRIBUTE overflow = { CKA_VALUE, data, 5 }; - CK_ATTRIBUTE content = { CKA_VALUE, "conte", 5 }; - - assert (p11_attr_equal (&one, &one)); - assert (!p11_attr_equal (&one, NULL)); - assert (!p11_attr_equal (NULL, &one)); - assert (!p11_attr_equal (&one, &two)); - assert (!p11_attr_equal (&two, &other)); - assert (p11_attr_equal (&other, &overflow)); - assert (!p11_attr_equal (&one, &null)); - assert (!p11_attr_equal (&one, &null)); - assert (!p11_attr_equal (&other, &content)); -} - -static void -test_hash (void) -{ - char *data = "extra attribute"; - CK_ATTRIBUTE one = { CKA_LABEL, "yay", 3 }; - CK_ATTRIBUTE null = { CKA_LABEL, NULL, 3 }; - CK_ATTRIBUTE two = { CKA_VALUE, "yay", 3 }; - CK_ATTRIBUTE other = { CKA_VALUE, data, 5 }; - CK_ATTRIBUTE overflow = { CKA_VALUE, data, 5 }; - CK_ATTRIBUTE content = { CKA_VALUE, "conte", 5 }; - unsigned int hash; - - hash = p11_attr_hash (&one); - assert (hash != 0); - - assert (p11_attr_hash (&one) == hash); - assert (p11_attr_hash (&two) != hash); - assert (p11_attr_hash (&other) != hash); - assert (p11_attr_hash (&overflow) != hash); - assert (p11_attr_hash (&null) != hash); - assert (p11_attr_hash (&content) != hash); - - hash = p11_attr_hash (NULL); - assert (hash == 0); -} - -static void -test_to_string (void) -{ - char *data = "extra attribute"; - CK_ATTRIBUTE one = { CKA_LABEL, "yay", 3 }; - CK_ATTRIBUTE attrs[] = { - { CKA_LABEL, "yay", 3 }, - { CKA_VALUE, data, 5 }, - { CKA_INVALID }, - }; - - char *string; - - - string = p11_attr_to_string (&one, CKA_INVALID); - assert_str_eq ("{ CKA_LABEL = (3) \"yay\" }", string); - free (string); - - string = p11_attrs_to_string (attrs, -1); - assert_str_eq ("(2) [ { CKA_LABEL = (3) \"yay\" }, { CKA_VALUE = (5) NOT-PRINTED } ]", string); - free (string); - - string = p11_attrs_to_string (attrs, 1); - assert_str_eq ("(1) [ { CKA_LABEL = (3) \"yay\" } ]", string); - free (string); -} - -static void -test_find (void) -{ - CK_BBOOL vtrue = CK_TRUE; - CK_ATTRIBUTE *attr; - - CK_ATTRIBUTE attrs[] = { - { CKA_LABEL, "label", 5 }, - { CKA_TOKEN, &vtrue, sizeof (vtrue) }, - { CKA_INVALID }, - }; - - attr = p11_attrs_find (attrs, CKA_LABEL); - assert_ptr_eq (attrs + 0, attr); - - attr = p11_attrs_find (attrs, CKA_TOKEN); - assert_ptr_eq (attrs + 1, attr); - - attr = p11_attrs_find (attrs, CKA_VALUE); - assert_ptr_eq (NULL, attr); -} - -static void -test_findn (void) -{ - CK_BBOOL vtrue = CK_TRUE; - CK_ATTRIBUTE *attr; - - CK_ATTRIBUTE attrs[] = { - { CKA_LABEL, "label", 5 }, - { CKA_TOKEN, &vtrue, sizeof (vtrue) }, - }; - - attr = p11_attrs_findn (attrs, 2, CKA_LABEL); - assert_ptr_eq (attrs + 0, attr); - - attr = p11_attrs_findn (attrs, 2, CKA_TOKEN); - assert_ptr_eq (attrs + 1, attr); - - attr = p11_attrs_findn (attrs, 2, CKA_VALUE); - assert_ptr_eq (NULL, attr); - - attr = p11_attrs_findn (attrs, 1, CKA_TOKEN); - assert_ptr_eq (NULL, attr); -} - -static void -test_remove (void) -{ - CK_BBOOL vtrue = CK_TRUE; - CK_ATTRIBUTE *attr; - CK_ATTRIBUTE *attrs; - CK_BBOOL ret; - - CK_ATTRIBUTE initial[] = { - { CKA_LABEL, "label", 5 }, - { CKA_TOKEN, &vtrue, sizeof (vtrue) }, - }; - - attrs = p11_attrs_buildn (NULL, initial, 2); - assert_ptr_not_null (attrs); - - attr = p11_attrs_find (attrs, CKA_LABEL); - assert_ptr_eq (attrs + 0, attr); - - ret = p11_attrs_remove (attrs, CKA_LABEL); - assert_num_eq (CK_TRUE, ret); - - attr = p11_attrs_find (attrs, CKA_LABEL); - assert_ptr_eq (NULL, attr); - - ret = p11_attrs_remove (attrs, CKA_LABEL); - assert_num_eq (CK_FALSE, ret); - - p11_attrs_free (attrs); -} - -static void -test_match (void) -{ - CK_BBOOL vtrue = CK_TRUE; - - CK_ATTRIBUTE attrs[] = { - { CKA_LABEL, "label", 5 }, - { CKA_TOKEN, &vtrue, sizeof (vtrue) }, - { CKA_INVALID }, - }; - - CK_ATTRIBUTE subset[] = { - { CKA_LABEL, "label", 5 }, - { CKA_INVALID }, - }; - - CK_ATTRIBUTE different[] = { - { CKA_LABEL, "other", 5 }, - { CKA_INVALID }, - }; - - CK_ATTRIBUTE extra[] = { - { CKA_VALUE, "the value", 9 }, - { CKA_LABEL, "other", 5 }, - { CKA_TOKEN, &vtrue, sizeof (vtrue) }, - { CKA_INVALID }, - }; - - assert (p11_attrs_match (attrs, attrs)); - assert (p11_attrs_match (attrs, subset)); - assert (!p11_attrs_match (attrs, different)); - assert (!p11_attrs_match (attrs, extra)); -} - -static void -test_matchn (void) -{ - CK_BBOOL vtrue = CK_TRUE; - - CK_ATTRIBUTE attrs[] = { - { CKA_LABEL, "label", 5 }, - { CKA_TOKEN, &vtrue, sizeof (vtrue) }, - { CKA_INVALID }, - }; - - CK_ATTRIBUTE subset[] = { - { CKA_LABEL, "label", 5 }, - }; - - CK_ATTRIBUTE different[] = { - { CKA_TOKEN, &vtrue, sizeof (vtrue) }, - { CKA_LABEL, "other", 5 }, - }; - - CK_ATTRIBUTE extra[] = { - { CKA_VALUE, "the value", 9 }, - { CKA_LABEL, "other", 5 }, - { CKA_TOKEN, &vtrue, sizeof (vtrue) }, - }; - - assert (p11_attrs_matchn (attrs, subset, 1)); - assert (!p11_attrs_matchn (attrs, different, 2)); - assert (!p11_attrs_matchn (attrs, extra, 3)); -} - -static void -test_find_bool (void) -{ - CK_BBOOL vtrue = CK_TRUE; - CK_BBOOL vfalse = CK_FALSE; - CK_BBOOL value; - - CK_ATTRIBUTE attrs[] = { - { CKA_LABEL, "\x01yy", 3 }, - { CKA_VALUE, &vtrue, (CK_ULONG)-1 }, - { CKA_TOKEN, &vtrue, sizeof (CK_BBOOL) }, - { CKA_TOKEN, &vfalse, sizeof (CK_BBOOL) }, - { CKA_INVALID }, - }; - - assert (p11_attrs_find_bool (attrs, CKA_TOKEN, &value) && value == CK_TRUE); - assert (!p11_attrs_find_bool (attrs, CKA_LABEL, &value)); - assert (!p11_attrs_find_bool (attrs, CKA_VALUE, &value)); -} - -static void -test_find_ulong (void) -{ - CK_ULONG v33 = 33UL; - CK_ULONG v45 = 45UL; - CK_ULONG value; - - CK_ATTRIBUTE attrs[] = { - { CKA_LABEL, &v33, 2 }, - { CKA_VALUE, &v45, (CK_ULONG)-1 }, - { CKA_BITS_PER_PIXEL, &v33, sizeof (CK_ULONG) }, - { CKA_BITS_PER_PIXEL, &v45, sizeof (CK_ULONG) }, - { CKA_INVALID }, - }; - - assert (p11_attrs_find_ulong (attrs, CKA_BITS_PER_PIXEL, &value) && value == v33); - assert (!p11_attrs_find_ulong (attrs, CKA_LABEL, &value)); - assert (!p11_attrs_find_ulong (attrs, CKA_VALUE, &value)); -} - -static void -test_find_value (void) -{ - void *value; - size_t length; - - CK_ATTRIBUTE attrs[] = { - { CKA_LABEL, "", (CK_ULONG)-1 }, - { CKA_LABEL, NULL, 5 }, - { CKA_LABEL, "", 0 }, - { CKA_LABEL, "test", 4 }, - { CKA_VALUE, NULL, 0 }, - { CKA_INVALID }, - }; - - value = p11_attrs_find_value (attrs, CKA_LABEL, &length); - assert_ptr_eq (attrs[3].pValue, value); - assert_num_eq (4, length); - - value = p11_attrs_find_value (attrs, CKA_LABEL, NULL); - assert_ptr_eq (attrs[3].pValue, value); - - value = p11_attrs_find_value (attrs, CKA_VALUE, &length); - assert_ptr_eq (NULL, value); - - value = p11_attrs_find_value (attrs, CKA_TOKEN, &length); - assert_ptr_eq (NULL, value); -} - -static void -test_find_valid (void) -{ - CK_ATTRIBUTE *attr; - - CK_ATTRIBUTE attrs[] = { - { CKA_LABEL, "", (CK_ULONG)-1 }, - { CKA_LABEL, NULL, 5 }, - { CKA_LABEL, "", 0 }, - { CKA_LABEL, "test", 4 }, - { CKA_VALUE, "value", 5 }, - { CKA_INVALID }, - }; - - attr = p11_attrs_find_valid (attrs, CKA_LABEL); - assert_ptr_eq (attrs + 3, attr); - - attr = p11_attrs_find_valid (attrs, CKA_VALUE); - assert_ptr_eq (attrs + 4, attr); - - attr = p11_attrs_find_valid (attrs, CKA_TOKEN); - assert_ptr_eq (NULL, attr); -} - -int -main (int argc, - char *argv[]) -{ - p11_test (test_equal, "/attrs/equal"); - p11_test (test_hash, "/attrs/hash"); - p11_test (test_to_string, "/attrs/to-string"); - - p11_test (test_terminator, "/attrs/terminator"); - p11_test (test_count, "/attrs/count"); - p11_test (test_build_one, "/attrs/build-one"); - p11_test (test_build_two, "/attrs/build-two"); - p11_test (test_build_invalid, "/attrs/build-invalid"); - p11_test (test_buildn_one, "/attrs/buildn-one"); - p11_test (test_buildn_two, "/attrs/buildn-two"); - p11_test (test_build_add, "/attrs/build-add"); - p11_test (test_build_null, "/attrs/build-null"); - p11_test (test_dup, "/attrs/dup"); - p11_test (test_take, "/attrs/take"); - p11_test (test_merge_replace, "/attrs/merge-replace"); - p11_test (test_merge_augment, "/attrs/merge-augment"); - p11_test (test_merge_empty, "/attrs/merge-empty"); - p11_test (test_free_null, "/attrs/free-null"); - p11_test (test_match, "/attrs/match"); - p11_test (test_matchn, "/attrs/matchn"); - p11_test (test_find, "/attrs/find"); - p11_test (test_findn, "/attrs/findn"); - p11_test (test_find_bool, "/attrs/find-bool"); - p11_test (test_find_ulong, "/attrs/find-ulong"); - p11_test (test_find_value, "/attrs/find-value"); - p11_test (test_find_valid, "/attrs/find-valid"); - p11_test (test_remove, "/attrs/remove"); - return p11_test_run (argc, argv); -} diff --git a/common/tests/test-buffer.c b/common/tests/test-buffer.c deleted file mode 100644 index 4fd060d..0000000 --- a/common/tests/test-buffer.c +++ /dev/null @@ -1,199 +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 "test.h" - -#include -#include -#include - -#include "debug.h" -#include "buffer.h" - -static void -test_init_uninit (void) -{ - p11_buffer buffer; - - p11_buffer_init (&buffer, 10); - assert_ptr_not_null (buffer.data); - assert_num_eq (0, buffer.len); - assert_num_eq (0, buffer.flags); - assert (buffer.size >= 10); - assert_ptr_not_null (buffer.ffree); - assert_ptr_not_null (buffer.frealloc); - - p11_buffer_uninit (&buffer); -} - -static void -test_append (void) -{ - p11_buffer buffer; - - p11_buffer_init (&buffer, 10); - buffer.len = 5; - p11_buffer_append (&buffer, 35); - assert_num_eq (5 + 35, buffer.len); - assert (buffer.size >= 35 + 5); - - p11_buffer_append (&buffer, 15); - assert_num_eq (5 + 35 + 15, buffer.len); - assert (buffer.size >= 5 + 35 + 15); - - p11_buffer_uninit (&buffer); -} - -static void -test_null (void) -{ - p11_buffer buffer; - - p11_buffer_init_null (&buffer, 10); - p11_buffer_add (&buffer, "Blah", -1); - p11_buffer_add (&buffer, " blah", -1); - - assert_str_eq ("Blah blah", buffer.data); - - p11_buffer_uninit (&buffer); -} - -static int mock_realloced = 0; -static int mock_freed = 0; - -static void * -mock_realloc (void *data, - size_t size) -{ - mock_realloced++; - return realloc (data, size); -} - -static void -mock_free (void *data) -{ - mock_freed++; - free (data); -} - -static void -test_init_for_data (void) -{ - p11_buffer buffer; - unsigned char *ret; - size_t len; - - mock_realloced = 0; - mock_freed = 0; - - p11_buffer_init_full (&buffer, (unsigned char *)strdup ("blah"), 4, 0, - mock_realloc, mock_free); - - assert_ptr_not_null (buffer.data); - assert_str_eq ("blah", (char *)buffer.data); - assert_num_eq (4, buffer.len); - assert_num_eq (0, buffer.flags); - assert_num_eq (4, buffer.size); - assert_ptr_eq (mock_free, buffer.ffree); - assert_ptr_eq (mock_realloc, buffer.frealloc); - - assert_num_eq (0, mock_realloced); - assert_num_eq (0, mock_freed); - - len = buffer.len; - ret = p11_buffer_append (&buffer, 1024); - assert_ptr_eq ((char *)buffer.data + len, ret); - assert_num_eq (1, mock_realloced); - - p11_buffer_uninit (&buffer); - assert_num_eq (1, mock_realloced); - assert_num_eq (1, mock_freed); -} - -static void -test_steal (void) -{ - p11_buffer buffer; - char *string; - size_t length; - - mock_freed = 0; - - p11_buffer_init_full (&buffer, (unsigned char *)strdup ("blah"), 4, - P11_BUFFER_NULL, mock_realloc, mock_free); - - assert_ptr_not_null (buffer.data); - assert_str_eq ("blah", buffer.data); - - p11_buffer_add (&buffer, " yada", -1); - assert_str_eq ("blah yada", buffer.data); - - string = p11_buffer_steal (&buffer, &length); - p11_buffer_uninit (&buffer); - - assert_str_eq ("blah yada", string); - assert_num_eq (9, length); - assert_num_eq (0, mock_freed); - - free (string); -} - -static void -test_add (void) -{ - p11_buffer buffer; - - p11_buffer_init (&buffer, 10); - - p11_buffer_add (&buffer, (unsigned char *)"Planet Express", 15); - assert_num_eq (15, buffer.len); - assert_str_eq ("Planet Express", (char *)buffer.data); - assert (p11_buffer_ok (&buffer)); - - p11_buffer_uninit (&buffer); -} - -int -main (int argc, - char *argv[]) -{ - p11_test (test_init_uninit, "/buffer/init-uninit"); - p11_test (test_init_for_data, "/buffer/init-for-data"); - p11_test (test_append, "/buffer/append"); - p11_test (test_null, "/buffer/null"); - p11_test (test_add, "/buffer/add"); - p11_test (test_steal, "/buffer/steal"); - return p11_test_run (argc, argv); -} diff --git a/common/tests/test-compat.c b/common/tests/test-compat.c deleted file mode 100644 index 42471ae..0000000 --- a/common/tests/test-compat.c +++ /dev/null @@ -1,117 +0,0 @@ -/* - * Copyright (c) 2013 Red Hat Inc. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * * Redistributions of source code must retain the above - * copyright notice, this list of conditions and the - * following disclaimer. - * * Redistributions in binary form must reproduce the - * above copyright notice, this list of conditions and - * the following disclaimer in the documentation and/or - * other materials provided with the distribution. - * * The names of contributors to this software may not be - * used to endorse or promote products derived from this - * software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF - * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH - * DAMAGE. - * - * Author: Stef Walter - */ - -#include "config.h" -#include "test.h" - -#include -#include -#include -#include - -#include "compat.h" - -static void -test_strndup (void) -{ - char unterminated[] = { 't', 'e', 's', 't', 'e', 'r', 'o', 'n', 'i', 'o' }; - char *res; - - res = strndup (unterminated, 6); - assert_str_eq (res, "tester"); - free (res); - - res = strndup ("test", 6); - assert_str_eq (res, "test"); - free (res); -} - -#ifdef OS_UNIX - -static void -test_getauxval (void) -{ - /* 23 is AT_SECURE */ - const char *args[] = { BUILDDIR "/frob-getauxval", "23", NULL }; - char *path; - int ret; - - ret = p11_test_run_child (args, true); - assert_num_eq (ret, 0); - - path = p11_test_copy_setgid (args[0]); - if (path == NULL) - return; - - args[0] = path; - ret = p11_test_run_child (args, true); - assert_num_cmp (ret, !=, 0); - - if (unlink (path) < 0) - assert_fail ("unlink failed", strerror (errno)); - free (path); -} - -static void -test_mmap (void) -{ - p11_mmap *map; - void *data; - size_t size; - char file[] = "emptyfileXXXXXX"; - int fd = mkstemp (file); - close (fd); - /* mmap on empty file should work */ - map = p11_mmap_open (file, NULL, &data, &size); - unlink (file); - assert_ptr_not_null (map); - p11_mmap_close (map); -} - -#endif /* OS_UNIX */ - -int -main (int argc, - char *argv[]) -{ - p11_test (test_strndup, "/compat/strndup"); -#ifdef OS_UNIX - /* Don't run this test when under fakeroot */ - if (!getenv ("FAKED_MODE")) { - p11_test (test_getauxval, "/compat/getauxval"); - } - p11_test (test_mmap, "/compat/mmap"); -#endif - return p11_test_run (argc, argv); -} diff --git a/common/tests/test-constants.c b/common/tests/test-constants.c deleted file mode 100644 index 9adc81a..0000000 --- a/common/tests/test-constants.c +++ /dev/null @@ -1,102 +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 "test.h" - -#include -#include -#include - -#include "attrs.h" -#include "constants.h" -#include "debug.h" - -static void -test_constants (void *arg) -{ - const p11_constant *constant = arg; - p11_dict *nicks, *names; - CK_ULONG check; - int i; - - nicks = p11_constant_reverse (true); - names = p11_constant_reverse (false); - - for (i = 1; constant[i].value != CKA_INVALID; i++) { - if (constant[i].value < constant[i - 1].value) - assert_fail ("attr constant out of order", constant[i].name); - } - for (i = 0; constant[i].value != CKA_INVALID; i++) { - assert_ptr_not_null (constant[i].name); - - if (constant[i].nick) { - assert_str_eq (constant[i].nick, - p11_constant_nick (constant, constant[i].value)); - } - - assert_str_eq (constant[i].name, - p11_constant_name (constant, constant[i].value)); - - if (constant[i].nick) { - check = p11_constant_resolve (nicks, constant[i].nick); - assert_num_eq (constant[i].value, check); - } - - check = p11_constant_resolve (names, constant[i].name); - assert_num_eq (constant[i].value, check); - } - - p11_dict_free (names); - p11_dict_free (nicks); -} - -int -main (int argc, - char *argv[]) -{ - p11_testx (test_constants, (void *)p11_constant_types, "/constants/types"); - p11_testx (test_constants, (void *)p11_constant_classes, "/constants/classes"); - p11_testx (test_constants, (void *)p11_constant_trusts, "/constants/trusts"); - p11_testx (test_constants, (void *)p11_constant_certs, "/constants/certs"); - p11_testx (test_constants, (void *)p11_constant_keys, "/constants/keys"); - p11_testx (test_constants, (void *)p11_constant_asserts, "/constants/asserts"); - p11_testx (test_constants, (void *)p11_constant_categories, "/constants/categories"); - p11_testx (test_constants, (void *)p11_constant_mechanisms, "/constants/mechanisms"); - p11_testx (test_constants, (void *)p11_constant_users, "/constants/users"); - p11_testx (test_constants, (void *)p11_constant_states, "/constants/states"); - p11_testx (test_constants, (void *)p11_constant_returns, "/constants/returns"); - - return p11_test_run (argc, argv); -} diff --git a/common/tests/test-dict.c b/common/tests/test-dict.c deleted file mode 100644 index f12a34e..0000000 --- a/common/tests/test-dict.c +++ /dev/null @@ -1,522 +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 "test.h" - -#include -#include -#include -#include - -#include "dict.h" - -static void -test_create (void) -{ - p11_dict *map; - - map = p11_dict_new (p11_dict_direct_hash, p11_dict_direct_equal, NULL, NULL); - assert_ptr_not_null (map); - p11_dict_free (map); -} - -static void -test_free_null (void) -{ - p11_dict_free (NULL); -} - -typedef struct { - int value; - bool 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 bool -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 = true; -} - -static void -value_destroy (void *data) -{ - int *value = data; - *value = 2; -} - -static void -test_free_destroys (void) -{ - p11_dict *map; - Key key = { 8, 0 }; - int value = 0; - - map = p11_dict_new (key_hash, key_equal, key_destroy, value_destroy); - assert_ptr_not_null (map); - if (!p11_dict_set (map, &key, &value)) - assert_not_reached (); - p11_dict_free (map); - - assert_num_eq (true, key.freed); - assert_num_eq (2, value); -} - -static void -test_iterate (void) -{ - 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); - assert_ptr_not_null (map); - if (!p11_dict_set (map, &key, &value)) - assert_not_reached (); - - p11_dict_iterate (map, &iter); - - ret = p11_dict_next (&iter, &pkey, &pvalue); - assert_num_eq (1, ret); - assert_ptr_eq (pkey, &key); - assert_ptr_eq (pvalue, &value); - - ret = p11_dict_next (&iter, &pkey, &pvalue); - assert_num_eq (0, ret); - - p11_dict_free (map); -} - -static int -compar_strings (const void *one, - const void *two) -{ - char **p1 = (char **)one; - char **p2 = (char **)two; - return strcmp (*p1, *p2); -} - -static void -test_iterate_remove (void) -{ - p11_dict *map; - p11_dictiter iter; - char *keys[] = { "111", "222", "333" }; - char *values[] = { "444", "555", "666" }; - void *okeys[3]; - void *ovalues[3]; - bool ret; - int i; - - map = p11_dict_new (p11_dict_str_hash, p11_dict_str_equal, NULL, NULL); - assert_ptr_not_null (map); - - for (i = 0; i < 3; i++) { - if (!p11_dict_set (map, keys[i], values[i])) - assert_not_reached (); - } - - p11_dict_iterate (map, &iter); - - ret = p11_dict_next (&iter, &okeys[0], &ovalues[0]); - assert_num_eq (true, ret); - - ret = p11_dict_next (&iter, &okeys[1], &ovalues[1]); - assert_num_eq (true, ret); - if (!p11_dict_remove (map, okeys[1])) - assert_not_reached (); - - ret = p11_dict_next (&iter, &okeys[2], &ovalues[2]); - assert_num_eq (true, ret); - - ret = p11_dict_next (&iter, NULL, NULL); - assert_num_eq (false, ret); - - assert_num_eq (2, p11_dict_size (map)); - p11_dict_free (map); - - qsort (okeys, 3, sizeof (void *), compar_strings); - qsort (ovalues, 3, sizeof (void *), compar_strings); - - for (i = 0; i < 3; i++) { - assert_str_eq (keys[i], okeys[i]); - assert_ptr_eq (keys[i], okeys[i]); - assert_str_eq (values[i], ovalues[i]); - assert_ptr_eq (values[i], ovalues[i]); - } -} - -static void -test_set_get (void) -{ - 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); - assert_ptr_eq (check, value); - - p11_dict_free (map); -} - -static void -test_set_get_remove (void) -{ - char *key = "KEY"; - char *value = "VALUE"; - char *check; - p11_dict *map; - bool ret; - - map = p11_dict_new (p11_dict_str_hash, p11_dict_str_equal, NULL, NULL); - - if (!p11_dict_set (map, key, value)) - assert_not_reached (); - - check = p11_dict_get (map, key); - assert_ptr_eq (check, value); - - ret = p11_dict_remove (map, key); - assert_num_eq (true, ret); - ret = p11_dict_remove (map, key); - assert_num_eq (false, ret); - - check = p11_dict_get (map, key); - assert (check == NULL); - - p11_dict_free (map); -} - -static void -test_set_clear (void) -{ - 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)) - assert_not_reached (); - - p11_dict_clear (map); - - check = p11_dict_get (map, key); - assert (check == NULL); - - p11_dict_free (map); -} - -static void -test_remove_destroys (void) -{ - p11_dict *map; - Key key = { 8, 0 }; - int value = 0; - bool ret; - - map = p11_dict_new (key_hash, key_equal, key_destroy, value_destroy); - assert_ptr_not_null (map); - if (!p11_dict_set (map, &key, &value)) - assert_not_reached (); - - ret = p11_dict_remove (map, &key); - assert_num_eq (true, ret); - assert_num_eq (true, key.freed); - assert_num_eq (2, value); - - /* should not be destroyed again */ - key.freed = false; - value = 0; - - ret = p11_dict_remove (map, &key); - assert_num_eq (false, ret); - assert_num_eq (false, key.freed); - assert_num_eq (0, value); - - /* should not be destroyed again */ - key.freed = false; - value = 0; - - p11_dict_free (map); - - assert_num_eq (false, key.freed); - assert_num_eq (0, value); -} - -static void -test_set_destroys (void) -{ - p11_dict *map; - Key key = { 8, 0 }; - Key key2 = { 8, 0 }; - int value, value2; - bool ret; - - map = p11_dict_new (key_hash, key_equal, key_destroy, value_destroy); - assert_ptr_not_null (map); - if (!p11_dict_set (map, &key, &value)) - assert_not_reached (); - - key.freed = key2.freed = false; - value = value2 = 0; - - /* Setting same key and value, should not be destroyed */ - ret = p11_dict_set (map, &key, &value); - assert_num_eq (true, ret); - assert_num_eq (false, key.freed); - assert_num_eq (false, key2.freed); - assert_num_eq (0, value); - assert_num_eq (0, value2); - - key.freed = key2.freed = false; - value = value2 = 0; - - /* Setting a new key same value, key should be destroyed */ - ret = p11_dict_set (map, &key2, &value); - assert_num_eq (true, ret); - assert_num_eq (true, key.freed); - assert_num_eq (false, key2.freed); - assert_num_eq (0, value); - assert_num_eq (0, value2); - - key.freed = key2.freed = false; - value = value2 = 0; - - /* Setting same key, new value, value should be destroyed */ - ret = p11_dict_set (map, &key2, &value2); - assert_num_eq (true, ret); - assert_num_eq (false, key.freed); - assert_num_eq (false, key2.freed); - assert_num_eq (2, value); - assert_num_eq (0, value2); - - key.freed = key2.freed = false; - value = value2 = 0; - - /* Setting new key new value, both should be destroyed */ - ret = p11_dict_set (map, &key, &value); - assert_num_eq (true, ret); - assert_num_eq (false, key.freed); - assert_num_eq (true, key2.freed); - assert_num_eq (0, value); - assert_num_eq (2, value2); - - key.freed = key2.freed = false; - value = value2 = 0; - - p11_dict_free (map); - assert_num_eq (true, key.freed); - assert_num_eq (2, value); - assert_num_eq (false, key2.freed); - assert_num_eq (0, value2); -} - - -static void -test_clear_destroys (void) -{ - p11_dict *map; - Key key = { 18, 0 }; - int value = 0; - - map = p11_dict_new (key_hash, key_equal, key_destroy, value_destroy); - assert_ptr_not_null (map); - if (!p11_dict_set (map, &key, &value)) - assert_not_reached (); - - p11_dict_clear (map); - assert_num_eq (true, key.freed); - assert_num_eq (2, value); - - /* should not be destroyed again */ - key.freed = false; - value = 0; - - p11_dict_clear (map); - assert_num_eq (false, key.freed); - assert_num_eq (0, value); - - /* should not be destroyed again */ - key.freed = false; - value = 0; - - p11_dict_free (map); - - assert_num_eq (false, key.freed); - assert_num_eq (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 (void) -{ - 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)); - assert (value != NULL); - *value = i; - if (!p11_dict_set (map, value, value)) - assert_not_reached (); - } - - for (i = 0; i < 20000; ++i) { - value = p11_dict_get (map, &i); - assert_ptr_not_null (value); - assert_num_eq (i, *value); - } - - p11_dict_free (map); -} - -static void -test_hash_count (void) -{ - p11_dict *map; - int *value; - int i; - bool ret; - - map = p11_dict_new (p11_dict_intptr_hash, p11_dict_intptr_equal, NULL, free); - - assert_num_eq (0, p11_dict_size (map)); - - for (i = 0; i < 20000; ++i) { - value = malloc (sizeof (int)); - assert (value != NULL); - *value = i; - if (!p11_dict_set (map, value, value)) - assert_not_reached (); - assert_num_eq (i + 1, p11_dict_size (map)); - } - - for (i = 0; i < 20000; ++i) { - ret = p11_dict_remove (map, &i); - assert_num_eq (true, ret); - assert_num_eq (20000 - (i + 1), p11_dict_size (map)); - } - - p11_dict_clear (map); - assert_num_eq (0, p11_dict_size (map)); - - p11_dict_free (map); -} - -static void -test_hash_ulongptr (void) -{ - 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)); - assert (value != NULL); - *value = i; - if (!p11_dict_set (map, value, value)) - assert_not_reached (); - } - - for (i = 0; i < 20000; ++i) { - value = p11_dict_get (map, &i); - assert_ptr_not_null (value); - assert_num_eq (i, *value); - } - - p11_dict_free (map); -} - -int -main (int argc, - char *argv[]) -{ - p11_test (test_create, "/dict/create"); - p11_test (test_set_get, "/dict/set-get"); - p11_test (test_set_get_remove, "/dict/set-get-remove"); - p11_test (test_remove_destroys, "/dict/remove-destroys"); - p11_test (test_set_clear, "/dict/set-clear"); - p11_test (test_set_destroys, "/dict/set-destroys"); - p11_test (test_clear_destroys, "/dict/clear-destroys"); - p11_test (test_free_null, "/dict/free-null"); - p11_test (test_free_destroys, "/dict/free-destroys"); - p11_test (test_iterate, "/dict/iterate"); - p11_test (test_iterate_remove, "/dict/iterate-remove"); - p11_test (test_hash_add_check_lots_and_collisions, "/dict/add-check-lots-and-collisions"); - p11_test (test_hash_count, "/dict/count"); - p11_test (test_hash_ulongptr, "/dict/ulongptr"); - return p11_test_run (argc, argv); -} diff --git a/common/tests/test-hash.c b/common/tests/test-hash.c deleted file mode 100644 index a12d5a4..0000000 --- a/common/tests/test-hash.c +++ /dev/null @@ -1,106 +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 "test.h" - -#include -#include -#include -#include -#include - -#include "hash.h" - -static void -test_murmur3 (void) -{ - uint32_t one, two, four, seven, eleven, split; - - assert (sizeof (one) == P11_HASH_MURMUR3_LEN); - - p11_hash_murmur3 ((unsigned char *)&one, "one", 3, NULL); - p11_hash_murmur3 ((unsigned char *)&two, "two", 3, NULL); - p11_hash_murmur3 ((unsigned char *)&four, "four", 4, NULL); - p11_hash_murmur3 ((unsigned char *)&seven, "seven", 5, NULL); - p11_hash_murmur3 ((unsigned char *)&eleven, "eleven", 6, NULL); - p11_hash_murmur3 ((unsigned char *)&split, "ele", 3, "ven", 3, NULL); - - assert (one != two); - assert (one != four); - assert (one != seven); - assert (one != eleven); - - assert (two != four); - assert (two != seven); - assert (two != eleven); - - assert (four != seven); - assert (four != eleven); - - assert (split == eleven); -} - -static void -test_murmur3_incr (void) -{ - uint32_t first, second; - - p11_hash_murmur3 ((unsigned char *)&first, - "this is the long input!", (size_t)23, - NULL); - - p11_hash_murmur3 ((unsigned char *)&second, - "this", (size_t)4, - " ", (size_t)1, - "is ", (size_t)3, - "the long ", (size_t)9, - "in", (size_t)2, - "p", (size_t)1, - "u", (size_t)1, - "t", (size_t)1, - "!", (size_t)1, - NULL); - - assert_num_eq (first, second); -} - -int -main (int argc, - char *argv[]) -{ - p11_test (test_murmur3, "/hash/murmur3"); - p11_test (test_murmur3_incr, "/hash/murmur3-incr"); - return p11_test_run (argc, argv); -} diff --git a/common/tests/test-lexer.c b/common/tests/test-lexer.c deleted file mode 100644 index 7d18e87..0000000 --- a/common/tests/test-lexer.c +++ /dev/null @@ -1,253 +0,0 @@ -/* - * Copyright (c) 2013 Red Hat Inc. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * * Redistributions of source code must retain the above - * copyright notice, this list of conditions and the - * following disclaimer. - * * Redistributions in binary form must reproduce the - * above copyright notice, this list of conditions and - * the following disclaimer in the documentation and/or - * other materials provided with the distribution. - * * The names of contributors to this software may not be - * used to endorse or promote products derived from this - * software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF - * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH - * DAMAGE. - * - * Author: Stef Walter - */ - -#include "config.h" -#include "test.h" - -#include -#include -#include - -#include "compat.h" -#include "debug.h" -#include "lexer.h" -#include "message.h" - -typedef struct { - int tok_type; - const char *name; - const char *value; -} expected_tok; - -static void -check_lex_msg (const char *file, - int line, - const char *function, - const expected_tok *expected, - const char *input, - bool failure) -{ - p11_lexer lexer; - size_t len; - bool failed; - int i; - - p11_lexer_init (&lexer, "test", input, strlen (input)); - for (i = 0; p11_lexer_next (&lexer, &failed); i++) { - if (expected[i].tok_type != lexer.tok_type) - p11_test_fail (file, line, function, - "lexer token type does not match: (%d != %d)", - expected[i].tok_type, lexer.tok_type); - switch (lexer.tok_type) { - case TOK_FIELD: - if (strcmp (expected[i].name, lexer.tok.field.name) != 0) - p11_test_fail (file, line, function, - "field name doesn't match: (%s != %s)", - expected[i].name, lexer.tok.field.name); - if (strcmp (expected[i].value, lexer.tok.field.value) != 0) - p11_test_fail (file, line, function, - "field value doesn't match: (%s != %s)", - expected[i].value, lexer.tok.field.value); - break; - case TOK_SECTION: - if (strcmp (expected[i].name, lexer.tok.field.name) != 0) - p11_test_fail (file, line, function, - "section name doesn't match: (%s != %s)", - expected[i].name, lexer.tok.field.name); - break; - case TOK_PEM: - len = strlen (expected[i].name); - if (lexer.tok.pem.length < len || - strncmp (lexer.tok.pem.begin, expected[i].name, len) != 0) { - p11_test_fail (file, line, function, - "wrong type of PEM block: %s", - expected[i].name); - } - break; - case TOK_EOF: - p11_test_fail (file, line, function, "eof should not be recieved"); - break; - } - } - - if (failure && !failed) - p11_test_fail (file, line, function, "lexing didn't fail"); - else if (!failure && failed) - p11_test_fail (file, line, function, "lexing failed"); - if (TOK_EOF != expected[i].tok_type) - p11_test_fail (file, line, function, "premature end of lexing"); - - p11_lexer_done (&lexer); -} - -#define check_lex_success(expected, input) \ - check_lex_msg (__FILE__, __LINE__, __FUNCTION__, expected, input, false) - -#define check_lex_failure(expected, input) \ - check_lex_msg (__FILE__, __LINE__, __FUNCTION__, expected, input, true) - -static void -test_basic (void) -{ - const char *input = "[the header]\n" - "field: value\n" - "-----BEGIN BLOCK1-----\n" - "aYNNXqshlVxCdo8QfKeXh3GUzd/yn4LYIVgQrx4a\n" - "-----END BLOCK1-----\n"; - - const expected_tok expected[] = { - { TOK_SECTION, "the header" }, - { TOK_FIELD, "field", "value" }, - { TOK_PEM, "-----BEGIN BLOCK1-----\n", }, - { TOK_EOF } - }; - - check_lex_success (expected, input); -} - -static void -test_corners (void) -{ - const char *input = "\r\n" /* blankline */ - " [the header]\r\n" /* bad line endings */ - " field: value \r\n" /* whitespace */ - "number: 2\n" /* extra space*/ - "number :3\n" /* extra space*/ - "number : 4\n" /* extra space*/ - "\n" - " # A comment \n" - "not-a-comment: # value\n" - "-----BEGIN BLOCK1-----\r\n" - "aYNNXqshlVxCdo8QfKeXh3GUzd/yn4LYIVgQrx4a\r\n" - "-----END BLOCK1-----"; /* no new line */ - - const expected_tok expected[] = { - { TOK_SECTION, "the header" }, - { TOK_FIELD, "field", "value" }, - { TOK_FIELD, "number", "2" }, - { TOK_FIELD, "number", "3" }, - { TOK_FIELD, "number", "4" }, - { TOK_FIELD, "not-a-comment", "# value" }, - { TOK_PEM, "-----BEGIN BLOCK1-----\r\n", }, - { TOK_EOF } - }; - - check_lex_success (expected, input); -} - -static void -test_following (void) -{ - const char *input = "-----BEGIN BLOCK1-----\n" - "aYNNXqshlVxCdo8QfKeXh3GUzd/yn4LYIVgQrx4a\n" - "-----END BLOCK1-----\n" - "field: value"; - - const expected_tok expected[] = { - { TOK_PEM, "-----BEGIN BLOCK1-----\n", }, - { TOK_FIELD, "field", "value" }, - { TOK_EOF } - }; - - check_lex_success (expected, input); -} - -static void -test_bad_pem (void) -{ - const char *input = "field: value\n" - "-----BEGIN BLOCK1-----\n" - "aYNNXqshlVxCdo8QfKeXh3GUzd/yn4LYIVgQrx4a\n"; - - const expected_tok expected[] = { - { TOK_FIELD, "field", "value" }, - { TOK_EOF } - }; - - p11_message_quiet (); - - check_lex_failure (expected, input); - - p11_message_loud (); -} - -static void -test_bad_section (void) -{ - const char *input = "field: value\n" - "[section\n" - "bad]\n"; - - const expected_tok expected[] = { - { TOK_FIELD, "field", "value" }, - { TOK_EOF } - }; - - p11_message_quiet (); - - check_lex_failure (expected, input); - - p11_message_loud (); -} - -static void -test_bad_value (void) -{ - const char *input = "field_value\n" - "[section\n" - "bad]\n"; - - const expected_tok expected[] = { - { TOK_EOF } - }; - - p11_message_quiet (); - - check_lex_failure (expected, input); - - p11_message_loud (); -} - -int -main (int argc, - char *argv[]) -{ - p11_test (test_basic, "/lexer/basic"); - p11_test (test_corners, "/lexer/corners"); - p11_test (test_following, "/lexer/following"); - p11_test (test_bad_pem, "/lexer/bad-pem"); - p11_test (test_bad_section, "/lexer/bad-section"); - p11_test (test_bad_value, "/lexer/bad-value"); - return p11_test_run (argc, argv); -} diff --git a/common/tests/test-message.c b/common/tests/test-message.c deleted file mode 100644 index 63ecf31..0000000 --- a/common/tests/test-message.c +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright (c) 2013 Red Hat Inc. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * * Redistributions of source code must retain the above - * copyright notice, this list of conditions and the - * following disclaimer. - * * Redistributions in binary form must reproduce the - * above copyright notice, this list of conditions and - * the following disclaimer in the documentation and/or - * other materials provided with the distribution. - * * The names of contributors to this software may not be - * used to endorse or promote products derived from this - * software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF - * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH - * DAMAGE. - * - * Author: Stef Walter - */ - -#include "config.h" -#include "test.h" - -#include "message.h" - -#include -#include - -static void -test_with_err (void) -{ - const char *last; - char *expected; - - errno = E2BIG; - p11_message_err (ENOENT, "Details: %s", "value"); - last = p11_message_last (); - - if (asprintf (&expected, "Details: value: %s", strerror (ENOENT)) < 0) - assert_not_reached (); - assert_str_eq (expected, last); - free (expected); -} - -int -main (int argc, - char *argv[]) -{ - p11_test (test_with_err, "/message/with-err"); - return p11_test_run (argc, argv); -} diff --git a/common/tests/test-path.c b/common/tests/test-path.c deleted file mode 100644 index 57619c8..0000000 --- a/common/tests/test-path.c +++ /dev/null @@ -1,216 +0,0 @@ -/* - * Copyright (c) 2013 Red Hat Inc. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * * Redistributions of source code must retain the above - * copyright notice, this list of conditions and the - * following disclaimer. - * * Redistributions in binary form must reproduce the - * above copyright notice, this list of conditions and - * the following disclaimer in the documentation and/or - * other materials provided with the distribution. - * * The names of contributors to this software may not be - * used to endorse or promote products derived from this - * software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF - * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH - * DAMAGE. - * - * Author: Stef Walter - */ - -#include "config.h" -#include "test.h" - -#include -#include -#include - -#include "compat.h" -#include "path.h" - -static void -test_base (void) -{ - struct { - const char *in; - const char *out; - } fixtures[] = { - { "/this/is/a/path", "path" }, - { "/this/is/a/folder/", "folder" }, - { "folder/", "folder" }, - { "/", "" }, - { "this", "this" }, -#ifdef OS_WIN32 - { "\\this\\is\\a\\path", "path" }, - { "\\this\\is\\a\\folder\\", "folder" }, - { "C:\\this\\is\\a\\path", "path" }, - { "D:\\this\\is\\a\\folder\\", "folder" }, - { "folder\\", "folder" }, - { "\\", "" }, -#endif - { NULL }, - }; - - char *out; - int i; - - for (i = 0; fixtures[i].in != NULL; i++) { - out = p11_path_base (fixtures[i].in); - assert_str_eq (fixtures[i].out, out); - free (out); - } -} - -#define assert_str_eq_free(ex, ac) \ - do { const char *__s1 = (ex); \ - char *__s2 = (ac); \ - if (__s1 && __s2 && strcmp (__s1, __s2) == 0) ; else \ - p11_test_fail (__FILE__, __LINE__, __FUNCTION__, "assertion failed (%s == %s): (%s == %s)", \ - #ex, #ac, __s1 ? __s1 : "(null)", __s2 ? __s2 : "(null)"); \ - free (__s2); \ - } while (0) - -static void -test_build (void) -{ -#ifdef OS_UNIX - assert_str_eq_free ("/root/second", - p11_path_build ("/root", "second", NULL)); - assert_str_eq_free ("/root/second", - p11_path_build ("/root", "/second", NULL)); - assert_str_eq_free ("/root/second", - p11_path_build ("/root/", "second", NULL)); - assert_str_eq_free ("/root/second/third", - p11_path_build ("/root", "second", "third", NULL)); - assert_str_eq_free ("/root/second/third", - p11_path_build ("/root", "/second/third", NULL)); -#else /* OS_WIN32 */ - assert_str_eq_free ("C:\\root\\second", - p11_path_build ("C:\\root", "second", NULL)); - assert_str_eq_free ("C:\\root\\second", - p11_path_build ("C:\\root", "\\second", NULL)); - assert_str_eq_free ("C:\\root\\second", - p11_path_build ("C:\\root\\", "second", NULL)); - assert_str_eq_free ("C:\\root\\second\\third", - p11_path_build ("C:\\root", "second", "third", NULL)); - assert_str_eq_free ("C:\\root\\second/third", - p11_path_build ("C:\\root", "second/third", NULL)); -#endif -} - -static void -test_expand (void) -{ - char *path; - -#ifdef OS_UNIX - putenv ("HOME=/home/blah"); - assert_str_eq_free ("/home/blah/my/path", - p11_path_expand ("~/my/path")); - assert_str_eq_free ("/home/blah", - p11_path_expand ("~")); - putenv ("XDG_CONFIG_HOME=/my"); - assert_str_eq_free ("/my/path", - p11_path_expand ("~/.config/path")); - putenv ("XDG_CONFIG_HOME="); - assert_str_eq_free ("/home/blah/.config/path", - p11_path_expand ("~/.config/path")); -#else /* OS_WIN32 */ - putenv ("HOME=C:\\Users\\blah"); - assert_str_eq_free ("C:\\Users\\blah\\path", - p11_path_expand ("~/my/path")); - assert_str_eq_free ("C:\\Users\\blah\\path", - p11_path_expand ("~\\path")); -#endif - - putenv("HOME="); - path = p11_path_expand ("~/this/is/my/path"); - assert (strstr (path, "this/is/my/path") != NULL); - free (path); -} - -static void -test_absolute (void) -{ -#ifdef OS_UNIX - assert (p11_path_absolute ("/home")); - assert (!p11_path_absolute ("home")); -#else /* OS_WIN32 */ - assert (p11_path_absolute ("C:\\home")); - assert (!p11_path_absolute ("home")); - assert (p11_path_absolute ("/home")); -#endif -} - -static void -test_parent (void) -{ - assert_str_eq_free ("/", p11_path_parent ("/root")); - assert_str_eq_free ("/", p11_path_parent ("/root/")); - assert_str_eq_free ("/", p11_path_parent ("/root//")); - assert_str_eq_free ("/root", p11_path_parent ("/root/second")); - assert_str_eq_free ("/root", p11_path_parent ("/root//second")); - assert_str_eq_free ("/root", p11_path_parent ("/root//second//")); - assert_str_eq_free ("/root", p11_path_parent ("/root///second")); - assert_str_eq_free ("/root/second", p11_path_parent ("/root/second/test.file")); - assert_ptr_eq (NULL, p11_path_parent ("/")); - assert_ptr_eq (NULL, p11_path_parent ("//")); - assert_ptr_eq (NULL, p11_path_parent ("")); -} - -static void -test_prefix (void) -{ - assert (p11_path_prefix ("/test/second", "/test")); - assert (!p11_path_prefix ("/test", "/test")); - assert (!p11_path_prefix ("/different/prefix", "/test")); - assert (!p11_path_prefix ("/te", "/test")); - assert (!p11_path_prefix ("/test", "/test/blah")); - assert (p11_path_prefix ("/test/other/second", "/test")); - assert (p11_path_prefix ("/test//other//second", "/test")); -} - -static void -test_canon (void) -{ - char *test; - - test = strdup ("2309haonutb;AOE@#$O "); - p11_path_canon (test); - assert_str_eq (test, "2309haonutb_AOE___O_"); - free (test); - - test = strdup ("22@# %ATI@#$onot"); - p11_path_canon (test); - assert_str_eq (test, "22____ATI___onot"); - free (test); -} - -int -main (int argc, - char *argv[]) -{ - p11_test (test_base, "/path/base"); - p11_test (test_build, "/path/build"); - p11_test (test_expand, "/path/expand"); - p11_test (test_absolute, "/path/absolute"); - p11_test (test_parent, "/path/parent"); - p11_test (test_prefix, "/path/prefix"); - p11_test (test_canon, "/path/canon"); - - return p11_test_run (argc, argv); -} diff --git a/common/tests/test-tests.c b/common/tests/test-tests.c deleted file mode 100644 index ba31d83..0000000 --- a/common/tests/test-tests.c +++ /dev/null @@ -1,95 +0,0 @@ -/* - * Copyright (c) 2013 Red Hat Inc. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * * Redistributions of source code must retain the above - * copyright notice, this list of conditions and the - * following disclaimer. - * * Redistributions in binary form must reproduce the - * above copyright notice, this list of conditions and - * the following disclaimer in the documentation and/or - * other materials provided with the distribution. - * * The names of contributors to this software may not be - * used to endorse or promote products derived from this - * software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF - * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH - * DAMAGE. - * - * Author: Stef Walter - */ - -#include "config.h" -#include "test.h" - -#include - -static void -test_success (void) -{ - /* Yup, nothing */ -} - - -static void -test_failure (void) -{ - if (getenv ("TEST_FAIL")) { - p11_test_fail (__FILE__, __LINE__, __FUNCTION__, - "Unconditional test failure due to TEST_FAIL environment variable"); - } -} - -static void -test_memory (void) -{ - char *mem; - - if (getenv ("TEST_FAIL")) { - mem = malloc (1); - assert (mem != NULL); - free (mem); - *mem = 1; - } -} - - -static void -test_leak (void) -{ - char *mem; - - if (getenv ("TEST_FAIL")) { - mem = malloc (1); - assert (mem != NULL); - *mem = 1; - } -} - -int -main (int argc, - char *argv[]) -{ - p11_test (test_success, "/test/success"); - - if (getenv ("TEST_FAIL")) { - p11_test (test_failure, "/test/failure"); - p11_test (test_memory, "/test/memory"); - p11_test (test_leak, "/test/leak"); - } - - return p11_test_run (argc, argv); -} diff --git a/common/tests/test-url.c b/common/tests/test-url.c deleted file mode 100644 index 892bf3c..0000000 --- a/common/tests/test-url.c +++ /dev/null @@ -1,164 +0,0 @@ -/* - * Copyright (c) 2013 Red Hat Inc. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * * Redistributions of source code must retain the above - * copyright notice, this list of conditions and the - * following disclaimer. - * * Redistributions in binary form must reproduce the - * above copyright notice, this list of conditions and - * the following disclaimer in the documentation and/or - * other materials provided with the distribution. - * * The names of contributors to this software may not be - * used to endorse or promote products derived from this - * software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF - * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH - * DAMAGE. - * - * Author: Stef Walter - */ - -#include "config.h" -#include "test.h" - -#include "debug.h" -#include "message.h" - -#include -#include -#include -#include - -#include "url.h" - -static void -check_decode_msg (const char *file, - int line, - const char *function, - const char *input, - ssize_t input_len, - const char *expected, - size_t expected_len) -{ - unsigned char *decoded; - size_t length; - - if (input_len < 0) - input_len = strlen (input); - decoded = p11_url_decode (input, input + input_len, "", &length); - - if (expected == NULL) { - if (decoded != NULL) - p11_test_fail (file, line, function, "decoding should have failed"); - - } else { - if (decoded == NULL) - p11_test_fail (file, line, function, "decoding failed"); - if (expected_len != length) - p11_test_fail (file, line, function, "wrong length: (%lu != %lu)", - (unsigned long)expected_len, (unsigned long)length); - if (memcmp (decoded, expected, length) != 0) - p11_test_fail (file, line, function, "decoding wrong"); - free (decoded); - } -} - -#define check_decode_success(input, input_len, expected, expected_len) \ - check_decode_msg (__FILE__, __LINE__, __FUNCTION__, input, input_len, expected, expected_len) - -#define check_decode_failure(input, input_len) \ - check_decode_msg (__FILE__, __LINE__, __FUNCTION__, input, input_len, NULL, 0) - -static void -test_decode_success (void) -{ - check_decode_success ("%54%45%53%54%00", -1, "TEST", 5); - check_decode_success ("%54%45%53%54%00", 6, "TE", 2); - check_decode_success ("%54est%00", -1, "Test", 5); -} - -static void -test_decode_skip (void) -{ - const char *input = "%54 %45 %53 %54 %00"; - unsigned char *decoded; - size_t length; - - decoded = p11_url_decode (input, input + strlen (input), P11_URL_WHITESPACE, &length); - assert_str_eq ("TEST", (char *)decoded); - assert_num_eq (5, length); - - free (decoded); -} - -static void -test_decode_failure (void) -{ - /* Early termination */ - check_decode_failure ("%54%45%53%5", -1); - check_decode_failure ("%54%45%53%", -1); - - /* Not hex characters */ - check_decode_failure ("%54%XX%53%54%00", -1); -} - -static void -test_encode (void) -{ - const unsigned char *input = (unsigned char *)"TEST"; - p11_buffer buf; - - if (!p11_buffer_init_null (&buf, 5)) - assert_not_reached (); - - p11_url_encode (input, input + 5, "", &buf); - assert (p11_buffer_ok (&buf)); - assert_str_eq ("%54%45%53%54%00", (char *)buf.data); - assert_num_eq (15, buf.len); - - p11_buffer_uninit (&buf); -} - -static void -test_encode_verbatim (void) -{ - const unsigned char *input = (unsigned char *)"TEST"; - p11_buffer buf; - - if (!p11_buffer_init_null (&buf, 5)) - assert_not_reached (); - - p11_url_encode (input, input + 5, "ES", &buf); - assert (p11_buffer_ok (&buf)); - assert_str_eq ("%54ES%54%00", (char *)buf.data); - assert_num_eq (11, buf.len); - - p11_buffer_uninit (&buf); -} - -int -main (int argc, - char *argv[]) -{ - p11_test (test_decode_success, "/url/decode-success"); - p11_test (test_decode_skip, "/url/decode-skip"); - p11_test (test_decode_failure, "/url/decode-failure"); - - p11_test (test_encode, "/url/encode"); - p11_test (test_encode_verbatim, "/url/encode-verbatim"); - return p11_test_run (argc, argv); -} -- cgit v1.1