From dcabaf1d56d410ba7ddb3dfbab9011bbbea5e6bc Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Fri, 5 Apr 2013 23:52:39 +0200 Subject: Our own unit testing framework * Support the TAP protocol * Much cleaner without having to carry around state * First class support for setup/teardown * Port the common tests * Wait on porting other tests until we've merged outstanding code --- common/tests/Makefile.am | 3 +- common/tests/test-array.c | 101 ++++----- common/tests/test-asn1.c | 53 ++--- common/tests/test-attrs.c | 461 ++++++++++++++++++++---------------------- common/tests/test-base64.c | 67 +++--- common/tests/test-buffer.c | 113 +++++------ common/tests/test-compat.c | 28 +-- common/tests/test-constants.c | 45 ++--- common/tests/test-dict.c | 250 +++++++++++------------ common/tests/test-hash.c | 74 +++---- common/tests/test-lexer.c | 126 ++++++------ common/tests/test-oid.c | 45 ++--- common/tests/test-path.c | 68 +++---- common/tests/test-pem.c | 76 +++---- common/tests/test-url.c | 93 ++++----- common/tests/test-utf8.c | 60 +++--- common/tests/test-x509.c | 106 ++++------ 17 files changed, 765 insertions(+), 1004 deletions(-) (limited to 'common/tests') diff --git a/common/tests/Makefile.am b/common/tests/Makefile.am index 6959c4f..945ed70 100644 --- a/common/tests/Makefile.am +++ b/common/tests/Makefile.am @@ -7,7 +7,7 @@ INCLUDES = \ -I$(top_srcdir) \ -I$(srcdir)/.. \ -I$(COMMON) \ - $(CUTEST_CFLAGS) + $(TEST_CFLAGS) LDADD = \ $(NULL) @@ -61,5 +61,6 @@ endif # WITH_ASN1 TESTS = $(CHECK_PROGS) LDADD += \ + $(top_builddir)/common/libp11-test.la \ $(top_builddir)/common/libp11-common.la \ $(CUTEST_LIBS) diff --git a/common/tests/test-array.c b/common/tests/test-array.c index a796365..8e8f996 100644 --- a/common/tests/test-array.c +++ b/common/tests/test-array.c @@ -33,26 +33,26 @@ */ #include "config.h" -#include "CuTest.h" #include #include #include #include "array.h" +#include "test.h" static void -test_p11_array_create (CuTest *tc) +test_create (void) { p11_array *array; array = p11_array_new (NULL); - CuAssertPtrNotNull (tc, array); + assert_ptr_not_null (array); p11_array_free (array); } static void -test_p11_array_free_null (CuTest *tc) +test_free_null (void) { p11_array_free (NULL); } @@ -65,81 +65,81 @@ destroy_value (void *data) } static void -test_p11_array_free_destroys (CuTest *tc) +test_free_destroys (void) { p11_array *array; int value = 0; array = p11_array_new (destroy_value); - CuAssertPtrNotNull (tc, array); + assert_ptr_not_null (array); if (!p11_array_push (array, &value)) - CuFail (tc, "should not be reached"); + assert_not_reached (); p11_array_free (array); - CuAssertIntEquals (tc, 2, value); + assert_num_eq (2, value); } static void -test_p11_array_add (CuTest *tc) +test_add (void) { char *value = "VALUE"; p11_array *array; array = p11_array_new (NULL); if (!p11_array_push (array, value)) - CuFail (tc, "should not be reached"); + assert_not_reached (); - CuAssertIntEquals (tc, 1, array->num); - CuAssertPtrEquals (tc, array->elem[0], value); + assert_num_eq (1, array->num); + assert_ptr_eq (array->elem[0], value); p11_array_free (array); } static void -test_p11_array_add_remove (CuTest *tc) +test_add_remove (void) { char *value = "VALUE"; p11_array *array; array = p11_array_new (NULL); if (!p11_array_push (array, value)) - CuFail (tc, "should not be reached"); + assert_not_reached (); - CuAssertIntEquals (tc, 1, array->num); + assert_num_eq (1, array->num); - CuAssertPtrEquals (tc, array->elem[0], value); + assert_ptr_eq (array->elem[0], value); p11_array_remove (array, 0); - CuAssertIntEquals (tc, 0, array->num); + assert_num_eq (0, array->num); p11_array_free (array); } static void -test_p11_array_remove_destroys (CuTest *tc) +test_remove_destroys (void) { p11_array *array; int value = 0; array = p11_array_new (destroy_value); if (!p11_array_push (array, &value)) - CuFail (tc, "should not be reached"); + assert_not_reached (); p11_array_remove (array, 0); - CuAssertIntEquals (tc, 2, value); + assert_num_eq (2, value); /* should not be destroyed again */ value = 0; p11_array_free (array); - CuAssertIntEquals (tc, 0, value); + assert_num_eq (0, value); } static void -test_p11_array_remove_and_count (CuTest *tc) +test_remove_and_count (void) { p11_array *array; int *value; @@ -147,75 +147,62 @@ test_p11_array_remove_and_count (CuTest *tc) array = p11_array_new (free); - CuAssertIntEquals (tc, 0, array->num); + assert_num_eq (0, array->num); for (i = 0; i < 20000; ++i) { value = malloc (sizeof (int)); *value = i; if (!p11_array_push (array, value)) - CuFail (tc, "should not be reached"); - CuAssertIntEquals (tc, i + 1, array->num); + assert_not_reached (); + assert_num_eq (i + 1, array->num); } for (i = 10; i < 20000; ++i) { p11_array_remove (array, 10); - CuAssertIntEquals (tc, 20010 - (i + 1), array->num); + assert_num_eq (20010 - (i + 1), array->num); } - CuAssertIntEquals (tc, 10, array->num); + assert_num_eq (10, array->num); p11_array_free (array); } static void -test_p11_array_clear_destroys (CuTest *tc) +test_clear_destroys (void) { p11_array *array; int value = 0; array = p11_array_new (destroy_value); if (!p11_array_push (array, &value)) - CuFail (tc, "should not be reached"); + assert_not_reached (); - CuAssertIntEquals (tc, 1, array->num); + assert_num_eq (1, array->num); p11_array_clear (array); - CuAssertIntEquals (tc, 2, value); - CuAssertIntEquals (tc, 0, array->num); + assert_num_eq (2, value); + assert_num_eq (0, array->num); /* should not be destroyed again */ value = 0; p11_array_free (array); - CuAssertIntEquals (tc, 0, value); + assert_num_eq (0, value); } - int -main (void) +main (int argc, + char *argv[]) { - CuString *output = CuStringNew (); - CuSuite* suite = CuSuiteNew (); - int ret; - - SUITE_ADD_TEST (suite, test_p11_array_create); - SUITE_ADD_TEST (suite, test_p11_array_add); - SUITE_ADD_TEST (suite, test_p11_array_add_remove); - SUITE_ADD_TEST (suite, test_p11_array_remove_destroys); - SUITE_ADD_TEST (suite, test_p11_array_remove_and_count); - SUITE_ADD_TEST (suite, test_p11_array_free_null); - SUITE_ADD_TEST (suite, test_p11_array_free_destroys); - SUITE_ADD_TEST (suite, test_p11_array_clear_destroys); - - CuSuiteRun (suite); - CuSuiteSummary (suite, output); - CuSuiteDetails (suite, output); - printf ("%s\n", output->buffer); - ret = suite->failCount; - CuSuiteDelete (suite); - CuStringDelete (output); - - return ret; + 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-asn1.c b/common/tests/test-asn1.c index 0335fa6..710928c 100644 --- a/common/tests/test-asn1.c +++ b/common/tests/test-asn1.c @@ -33,7 +33,7 @@ */ #include "config.h" -#include "CuTest.h" +#include "test.h" #include "asn1.h" #include "debug.h" @@ -49,21 +49,21 @@ struct { } test; static void -setup (CuTest *cu) +setup (void *unused) { test.asn1_defs = p11_asn1_defs_load (); - CuAssertPtrNotNull (cu, test.asn1_defs); + assert_ptr_not_null (test.asn1_defs); } static void -teardown (CuTest *cu) +teardown (void *unused) { p11_dict_free (test.asn1_defs); memset (&test, 0, sizeof (test)); } static void -test_tlv_length (CuTest *cu) +test_tlv_length (void) { struct { const char *der; @@ -79,14 +79,10 @@ test_tlv_length (CuTest *cu) int length; int i; - setup (cu); - for (i = 0; tlv_lengths[i].der != NULL; i++) { length = p11_asn1_tlv_length ((const unsigned char *)tlv_lengths[i].der, tlv_lengths[i].der_len); - CuAssertIntEquals (cu, tlv_lengths[i].expected, length); + assert_num_eq (tlv_lengths[i].expected, length); } - - teardown (cu); } static const unsigned char test_eku_server_and_client[] = { @@ -95,7 +91,7 @@ static const unsigned char test_eku_server_and_client[] = { }; static void -test_asn1_cache (CuTest *cu) +test_asn1_cache (void) { p11_asn1_cache *cache; p11_dict *defs; @@ -103,15 +99,15 @@ test_asn1_cache (CuTest *cu) node_asn *check; cache = p11_asn1_cache_new (); - CuAssertPtrNotNull (cu, cache); + assert_ptr_not_null (cache); defs = p11_asn1_cache_defs (cache); - CuAssertPtrNotNull (cu, defs); + assert_ptr_not_null (defs); asn = p11_asn1_decode (defs, "PKIX1.ExtKeyUsageSyntax", test_eku_server_and_client, sizeof (test_eku_server_and_client), NULL); - CuAssertPtrNotNull (cu, defs); + assert_ptr_not_null (defs); /* Place the parsed data in the cache */ p11_asn1_cache_take (cache, asn, "PKIX1.ExtKeyUsageSyntax", @@ -122,38 +118,27 @@ test_asn1_cache (CuTest *cu) check = p11_asn1_cache_get (cache, "PKIX1.ExtKeyUsageSyntax", test_eku_server_and_client, sizeof (test_eku_server_and_client)); - CuAssertPtrEquals (cu, asn, check); + assert_ptr_eq (asn, check); /* Flush should remove it */ p11_asn1_cache_flush (cache); check = p11_asn1_cache_get (cache, "PKIX1.ExtKeyUsageSyntax", test_eku_server_and_client, sizeof (test_eku_server_and_client)); - CuAssertPtrEquals (cu, NULL, check); + assert_ptr_eq (NULL, check); p11_asn1_cache_free (cache); } int -main (void) +main (int argc, + char *argv[]) { - CuString *output = CuStringNew (); - CuSuite* suite = CuSuiteNew (); - int ret; - - putenv ("P11_KIT_STRICT=1"); - p11_debug_init (); - - SUITE_ADD_TEST (suite, test_tlv_length); - SUITE_ADD_TEST (suite, test_asn1_cache); + p11_fixture (setup, teardown); + p11_test (test_tlv_length, "/asn1/tlv_length"); - CuSuiteRun (suite); - CuSuiteSummary (suite, output); - CuSuiteDetails (suite, output); - printf ("%s\n", output->buffer); - ret = suite->failCount; - CuSuiteDelete (suite); - CuStringDelete (output); + p11_fixture (NULL, NULL); + p11_test (test_asn1_cache, "/asn1/asn1_cache"); - return ret; + return p11_test_run (argc, argv); } diff --git a/common/tests/test-attrs.c b/common/tests/test-attrs.c index 324ed90..6087191 100644 --- a/common/tests/test-attrs.c +++ b/common/tests/test-attrs.c @@ -33,7 +33,7 @@ */ #include "config.h" -#include "CuTest.h" +#include "test.h" #include #include @@ -43,7 +43,7 @@ #include "debug.h" static void -test_terminator (CuTest *tc) +test_terminator (void) { CK_ATTRIBUTE attrs[] = { { CKA_LABEL, "label", 5 }, @@ -51,14 +51,14 @@ test_terminator (CuTest *tc) { CKA_INVALID }, }; - CuAssertIntEquals (tc, true, p11_attrs_terminator (attrs + 2)); - CuAssertIntEquals (tc, true, p11_attrs_terminator (NULL)); - CuAssertIntEquals (tc, false, p11_attrs_terminator (attrs)); - CuAssertIntEquals (tc, false, p11_attrs_terminator (attrs + 1)); + 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 (CuTest *tc) +test_count (void) { CK_BBOOL vtrue = CK_TRUE; @@ -72,13 +72,13 @@ test_count (CuTest *tc) { CKA_INVALID }, }; - CuAssertIntEquals (tc, 2, p11_attrs_count (attrs)); - CuAssertIntEquals (tc, 0, p11_attrs_count (NULL)); - CuAssertIntEquals (tc, 0, p11_attrs_count (empty)); + 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 (CuTest *tc) +test_build_one (void) { CK_ATTRIBUTE *attrs; CK_ATTRIBUTE add = { CKA_LABEL, "yay", 3 }; @@ -86,18 +86,18 @@ test_build_one (CuTest *tc) attrs = p11_attrs_build (NULL, &add, NULL); /* Test the first attribute */ - CuAssertPtrNotNull (tc, attrs); - CuAssertTrue (tc, attrs->type == CKA_LABEL); - CuAssertIntEquals (tc, 3, attrs->ulValueLen); - CuAssertTrue (tc, memcmp (attrs->pValue, "yay", 3) == 0); + assert_ptr_not_null (attrs); + assert (attrs->type == CKA_LABEL); + assert_num_eq (3, attrs->ulValueLen); + assert (memcmp (attrs->pValue, "yay", 3) == 0); - CuAssertTrue (tc, attrs[1].type == CKA_INVALID); + assert (attrs[1].type == CKA_INVALID); p11_attrs_free (attrs); } static void -test_build_two (CuTest *tc) +test_build_two (void) { CK_ATTRIBUTE *attrs; CK_ATTRIBUTE one = { CKA_LABEL, "yay", 3 }; @@ -105,23 +105,23 @@ test_build_two (CuTest *tc) attrs = p11_attrs_build (NULL, &one, &two, NULL); - CuAssertPtrNotNull (tc, attrs); - CuAssertTrue (tc, attrs[0].type == CKA_LABEL); - CuAssertIntEquals (tc, 3, attrs[0].ulValueLen); - CuAssertTrue (tc, memcmp (attrs[0].pValue, "yay", 3) == 0); + 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); - CuAssertPtrNotNull (tc, attrs); - CuAssertTrue (tc, attrs[1].type == CKA_VALUE); - CuAssertIntEquals (tc, 5, attrs[1].ulValueLen); - CuAssertTrue (tc, memcmp (attrs[1].pValue, "eight", 5) == 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); - CuAssertTrue (tc, attrs[2].type == CKA_INVALID); + assert (attrs[2].type == CKA_INVALID); p11_attrs_free (attrs); } static void -test_build_invalid (CuTest *tc) +test_build_invalid (void) { CK_ATTRIBUTE *attrs; CK_ATTRIBUTE one = { CKA_LABEL, "yay", 3 }; @@ -130,23 +130,23 @@ test_build_invalid (CuTest *tc) attrs = p11_attrs_build (NULL, &one, &invalid, &two, NULL); - CuAssertPtrNotNull (tc, attrs); - CuAssertTrue (tc, attrs[0].type == CKA_LABEL); - CuAssertIntEquals (tc, 3, attrs[0].ulValueLen); - CuAssertTrue (tc, memcmp (attrs[0].pValue, "yay", 3) == 0); + 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); - CuAssertPtrNotNull (tc, attrs); - CuAssertTrue (tc, attrs[1].type == CKA_VALUE); - CuAssertIntEquals (tc, 5, attrs[1].ulValueLen); - CuAssertTrue (tc, memcmp (attrs[1].pValue, "eight", 5) == 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); - CuAssertTrue (tc, attrs[2].type == CKA_INVALID); + assert (attrs[2].type == CKA_INVALID); p11_attrs_free (attrs); } static void -test_buildn_two (CuTest *tc) +test_buildn_two (void) { CK_ATTRIBUTE *attrs; CK_ATTRIBUTE add[] = { @@ -157,23 +157,23 @@ test_buildn_two (CuTest *tc) attrs = p11_attrs_buildn (NULL, add, 2); /* Test the first attribute */ - CuAssertPtrNotNull (tc, attrs); - CuAssertTrue (tc, attrs->type == CKA_LABEL); - CuAssertIntEquals (tc, 3, attrs->ulValueLen); - CuAssertTrue (tc, memcmp (attrs->pValue, "yay", 3) == 0); + assert_ptr_not_null (attrs); + assert (attrs->type == CKA_LABEL); + assert_num_eq (3, attrs->ulValueLen); + assert (memcmp (attrs->pValue, "yay", 3) == 0); - CuAssertPtrNotNull (tc, attrs); - CuAssertTrue (tc, attrs[1].type == CKA_VALUE); - CuAssertIntEquals (tc, 5, attrs[1].ulValueLen); - CuAssertTrue (tc, memcmp (attrs[1].pValue, "eight", 5) == 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); - CuAssertTrue (tc, attrs[2].type == CKA_INVALID); + assert (attrs[2].type == CKA_INVALID); p11_attrs_free (attrs); } static void -test_buildn_one (CuTest *tc) +test_buildn_one (void) { CK_ATTRIBUTE *attrs; CK_ATTRIBUTE add = { CKA_LABEL, "yay", 3 }; @@ -181,18 +181,18 @@ test_buildn_one (CuTest *tc) attrs = p11_attrs_buildn (NULL, &add, 1); /* Test the first attribute */ - CuAssertPtrNotNull (tc, attrs); - CuAssertTrue (tc, attrs->type == CKA_LABEL); - CuAssertIntEquals (tc, 3, attrs->ulValueLen); - CuAssertTrue (tc, memcmp (attrs->pValue, "yay", 3) == 0); + assert_ptr_not_null (attrs); + assert (attrs->type == CKA_LABEL); + assert_num_eq (3, attrs->ulValueLen); + assert (memcmp (attrs->pValue, "yay", 3) == 0); - CuAssertTrue (tc, attrs[1].type == CKA_INVALID); + assert (attrs[1].type == CKA_INVALID); p11_attrs_free (attrs); } static void -test_build_add (CuTest *tc) +test_build_add (void) { CK_ATTRIBUTE initial[] = { { CKA_LABEL, "label", 5 }, @@ -206,28 +206,28 @@ test_build_add (CuTest *tc) attrs = p11_attrs_buildn (NULL, initial, 2); attrs = p11_attrs_build (attrs, &one, &two, NULL); - CuAssertPtrNotNull (tc, attrs); - CuAssertTrue (tc, attrs[0].type == CKA_LABEL); - CuAssertIntEquals (tc, 3, attrs[0].ulValueLen); - CuAssertTrue (tc, memcmp (attrs[0].pValue, "yay", 3) == 0); + 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); - CuAssertPtrNotNull (tc, attrs); - CuAssertTrue (tc, attrs[1].type == CKA_VALUE); - CuAssertIntEquals (tc, 4, attrs[1].ulValueLen); - CuAssertTrue (tc, memcmp (attrs[1].pValue, "nine", 4) == 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); - CuAssertPtrNotNull (tc, attrs); - CuAssertTrue (tc, attrs[2].type == CKA_TOKEN); - CuAssertIntEquals (tc, 1, attrs[2].ulValueLen); - CuAssertTrue (tc, memcmp (attrs[2].pValue, "\x01", 1) == 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); - CuAssertTrue (tc, attrs[3].type == CKA_INVALID); + assert (attrs[3].type == CKA_INVALID); p11_attrs_free (attrs); } static void -test_build_null (CuTest *tc) +test_build_null (void) { CK_ATTRIBUTE *attrs; CK_ATTRIBUTE add = { CKA_LABEL, NULL, (CK_ULONG)-1 }; @@ -235,16 +235,16 @@ test_build_null (CuTest *tc) attrs = p11_attrs_build (NULL, &add, NULL); /* Test the first attribute */ - CuAssertPtrNotNull (tc, attrs); - CuAssertTrue (tc, attrs->type == CKA_LABEL); - CuAssertTrue (tc, attrs->ulValueLen == (CK_ULONG)-1); - CuAssertPtrEquals (tc, NULL, attrs->pValue); + 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 (CuTest *tc) +test_dup (void) { CK_ATTRIBUTE *attrs; CK_ATTRIBUTE original[] = { @@ -256,23 +256,23 @@ test_dup (CuTest *tc) attrs = p11_attrs_dup (original); /* Test the first attribute */ - CuAssertPtrNotNull (tc, attrs); - CuAssertTrue (tc, attrs->type == CKA_LABEL); - CuAssertIntEquals (tc, 3, attrs->ulValueLen); - CuAssertTrue (tc, memcmp (attrs->pValue, "yay", 3) == 0); + assert_ptr_not_null (attrs); + assert (attrs->type == CKA_LABEL); + assert_num_eq (3, attrs->ulValueLen); + assert (memcmp (attrs->pValue, "yay", 3) == 0); - CuAssertPtrNotNull (tc, attrs); - CuAssertTrue (tc, attrs[1].type == CKA_VALUE); - CuAssertIntEquals (tc, 5, attrs[1].ulValueLen); - CuAssertTrue (tc, memcmp (attrs[1].pValue, "eight", 5) == 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); - CuAssertTrue (tc, attrs[2].type == CKA_INVALID); + assert (attrs[2].type == CKA_INVALID); p11_attrs_free (attrs); } static void -test_take (CuTest *tc) +test_take (void) { CK_ATTRIBUTE initial[] = { { CKA_LABEL, "label", 5 }, @@ -284,30 +284,30 @@ test_take (CuTest *tc) 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); - CuAssertPtrNotNull (tc, attrs); + assert_ptr_not_null (attrs); - CuAssertTrue (tc, attrs[0].type == CKA_LABEL); - CuAssertIntEquals (tc, 7, attrs[0].ulValueLen); - CuAssertTrue (tc, memcmp (attrs[0].pValue, "boooyah", 7) == 0); + assert (attrs[0].type == CKA_LABEL); + assert_num_eq (7, attrs[0].ulValueLen); + assert (memcmp (attrs[0].pValue, "boooyah", 7) == 0); - CuAssertPtrNotNull (tc, attrs); - CuAssertTrue (tc, attrs[1].type == CKA_VALUE); - CuAssertIntEquals (tc, 4, attrs[1].ulValueLen); - CuAssertTrue (tc, memcmp (attrs[1].pValue, "nine", 4) == 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); - CuAssertPtrNotNull (tc, attrs); - CuAssertTrue (tc, attrs[2].type == CKA_TOKEN); - CuAssertIntEquals (tc, 1, attrs[2].ulValueLen); - CuAssertTrue (tc, memcmp (attrs[2].pValue, "\x01", 1) == 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); - CuAssertTrue (tc, attrs[3].type == CKA_INVALID); + assert (attrs[3].type == CKA_INVALID); p11_attrs_free (attrs); } static void -test_merge_replace (CuTest *tc) +test_merge_replace (void) { CK_ATTRIBUTE initial[] = { { CKA_LABEL, "label", 5 }, @@ -325,29 +325,29 @@ test_merge_replace (CuTest *tc) attrs = p11_attrs_buildn (NULL, initial, 2); merge = p11_attrs_buildn (NULL, extra, 2); attrs = p11_attrs_merge (attrs, merge, true); - CuAssertPtrNotNull (tc, attrs); + assert_ptr_not_null (attrs); - CuAssertTrue (tc, attrs[0].type == CKA_LABEL); - CuAssertIntEquals (tc, 7, attrs[0].ulValueLen); - CuAssertTrue (tc, memcmp (attrs[0].pValue, "boooyah", 7) == 0); + assert (attrs[0].type == CKA_LABEL); + assert_num_eq (7, attrs[0].ulValueLen); + assert (memcmp (attrs[0].pValue, "boooyah", 7) == 0); - CuAssertPtrNotNull (tc, attrs); - CuAssertTrue (tc, attrs[1].type == CKA_VALUE); - CuAssertIntEquals (tc, 4, attrs[1].ulValueLen); - CuAssertTrue (tc, memcmp (attrs[1].pValue, "nine", 4) == 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); - CuAssertPtrNotNull (tc, attrs); - CuAssertTrue (tc, attrs[2].type == CKA_APPLICATION); - CuAssertIntEquals (tc, 5, attrs[2].ulValueLen); - CuAssertTrue (tc, memcmp (attrs[2].pValue, "disco", 5) == 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); - CuAssertTrue (tc, attrs[3].type == CKA_INVALID); + assert (attrs[3].type == CKA_INVALID); p11_attrs_free (attrs); } static void -test_merge_empty (CuTest *tc) +test_merge_empty (void) { CK_ATTRIBUTE extra[] = { { CKA_LABEL, "boooyah", 7 }, @@ -359,14 +359,14 @@ test_merge_empty (CuTest *tc) merge = p11_attrs_buildn (NULL, extra, 2); attrs = p11_attrs_merge (attrs, merge, true); - CuAssertPtrNotNull (tc, attrs); - CuAssertPtrEquals (tc, merge, attrs); + assert_ptr_not_null (attrs); + assert_ptr_eq (merge, attrs); p11_attrs_free (attrs); } static void -test_merge_augment (CuTest *tc) +test_merge_augment (void) { CK_ATTRIBUTE initial[] = { { CKA_LABEL, "label", 5 }, @@ -384,35 +384,35 @@ test_merge_augment (CuTest *tc) attrs = p11_attrs_buildn (NULL, initial, 2); merge = p11_attrs_buildn (NULL, extra, 2); attrs = p11_attrs_merge (attrs, merge, false); - CuAssertPtrNotNull (tc, attrs); + assert_ptr_not_null (attrs); - CuAssertTrue (tc, attrs[0].type == CKA_LABEL); - CuAssertIntEquals (tc, 5, attrs[0].ulValueLen); - CuAssertTrue (tc, memcmp (attrs[0].pValue, "label", 5) == 0); + assert (attrs[0].type == CKA_LABEL); + assert_num_eq (5, attrs[0].ulValueLen); + assert (memcmp (attrs[0].pValue, "label", 5) == 0); - CuAssertPtrNotNull (tc, attrs); - CuAssertTrue (tc, attrs[1].type == CKA_VALUE); - CuAssertIntEquals (tc, 4, attrs[1].ulValueLen); - CuAssertTrue (tc, memcmp (attrs[1].pValue, "nine", 4) == 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); - CuAssertPtrNotNull (tc, attrs); - CuAssertTrue (tc, attrs[2].type == CKA_APPLICATION); - CuAssertIntEquals (tc, 5, attrs[2].ulValueLen); - CuAssertTrue (tc, memcmp (attrs[2].pValue, "disco", 5) == 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); - CuAssertTrue (tc, attrs[3].type == CKA_INVALID); + assert (attrs[3].type == CKA_INVALID); p11_attrs_free (attrs); } static void -test_free_null (CuTest *tc) +test_free_null (void) { p11_attrs_free (NULL); } static void -test_equal (CuTest *tc) +test_equal (void) { char *data = "extra attribute"; CK_ATTRIBUTE one = { CKA_LABEL, "yay", 3 }; @@ -422,19 +422,19 @@ test_equal (CuTest *tc) CK_ATTRIBUTE overflow = { CKA_VALUE, data, 5 }; CK_ATTRIBUTE content = { CKA_VALUE, "conte", 5 }; - CuAssertTrue (tc, p11_attr_equal (&one, &one)); - CuAssertTrue (tc, !p11_attr_equal (&one, NULL)); - CuAssertTrue (tc, !p11_attr_equal (NULL, &one)); - CuAssertTrue (tc, !p11_attr_equal (&one, &two)); - CuAssertTrue (tc, !p11_attr_equal (&two, &other)); - CuAssertTrue (tc, p11_attr_equal (&other, &overflow)); - CuAssertTrue (tc, !p11_attr_equal (&one, &null)); - CuAssertTrue (tc, !p11_attr_equal (&one, &null)); - CuAssertTrue (tc, !p11_attr_equal (&other, &content)); + 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 (CuTest *tc) +test_hash (void) { char *data = "extra attribute"; CK_ATTRIBUTE one = { CKA_LABEL, "yay", 3 }; @@ -446,18 +446,18 @@ test_hash (CuTest *tc) unsigned int hash; hash = p11_attr_hash (&one); - CuAssertTrue (tc, hash != 0); - - CuAssertTrue (tc, p11_attr_hash (&one) == hash); - CuAssertTrue (tc, p11_attr_hash (&two) != hash); - CuAssertTrue (tc, p11_attr_hash (&other) != hash); - CuAssertTrue (tc, p11_attr_hash (&overflow) != hash); - CuAssertTrue (tc, p11_attr_hash (&null) != hash); - CuAssertTrue (tc, p11_attr_hash (&content) != hash); + 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); } static void -test_to_string (CuTest *tc) +test_to_string (void) { char *data = "extra attribute"; CK_ATTRIBUTE one = { CKA_LABEL, "yay", 3 }; @@ -471,20 +471,20 @@ test_to_string (CuTest *tc) string = p11_attr_to_string (&one, CKA_INVALID); - CuAssertStrEquals (tc, "{ CKA_LABEL = (3) \"yay\" }", string); + assert_str_eq ("{ CKA_LABEL = (3) \"yay\" }", string); free (string); string = p11_attrs_to_string (attrs, -1); - CuAssertStrEquals (tc, "(2) [ { CKA_LABEL = (3) \"yay\" }, { CKA_VALUE = (5) NOT-PRINTED } ]", string); + assert_str_eq ("(2) [ { CKA_LABEL = (3) \"yay\" }, { CKA_VALUE = (5) NOT-PRINTED } ]", string); free (string); string = p11_attrs_to_string (attrs, 1); - CuAssertStrEquals (tc, "(1) [ { CKA_LABEL = (3) \"yay\" } ]", string); + assert_str_eq ("(1) [ { CKA_LABEL = (3) \"yay\" } ]", string); free (string); } static void -test_find (CuTest *tc) +test_find (void) { CK_BBOOL vtrue = CK_TRUE; CK_ATTRIBUTE *attr; @@ -496,17 +496,17 @@ test_find (CuTest *tc) }; attr = p11_attrs_find (attrs, CKA_LABEL); - CuAssertPtrEquals (tc, attrs + 0, attr); + assert_ptr_eq (attrs + 0, attr); attr = p11_attrs_find (attrs, CKA_TOKEN); - CuAssertPtrEquals (tc, attrs + 1, attr); + assert_ptr_eq (attrs + 1, attr); attr = p11_attrs_find (attrs, CKA_VALUE); - CuAssertPtrEquals (tc, NULL, attr); + assert_ptr_eq (NULL, attr); } static void -test_findn (CuTest *tc) +test_findn (void) { CK_BBOOL vtrue = CK_TRUE; CK_ATTRIBUTE *attr; @@ -517,20 +517,20 @@ test_findn (CuTest *tc) }; attr = p11_attrs_findn (attrs, 2, CKA_LABEL); - CuAssertPtrEquals (tc, attrs + 0, attr); + assert_ptr_eq (attrs + 0, attr); attr = p11_attrs_findn (attrs, 2, CKA_TOKEN); - CuAssertPtrEquals (tc, attrs + 1, attr); + assert_ptr_eq (attrs + 1, attr); attr = p11_attrs_findn (attrs, 2, CKA_VALUE); - CuAssertPtrEquals (tc, NULL, attr); + assert_ptr_eq (NULL, attr); attr = p11_attrs_findn (attrs, 1, CKA_TOKEN); - CuAssertPtrEquals (tc, NULL, attr); + assert_ptr_eq (NULL, attr); } static void -test_remove (CuTest *tc) +test_remove (void) { CK_BBOOL vtrue = CK_TRUE; CK_ATTRIBUTE *attr; @@ -543,25 +543,25 @@ test_remove (CuTest *tc) }; attrs = p11_attrs_buildn (NULL, initial, 2); - CuAssertPtrNotNull (tc, attrs); + assert_ptr_not_null (attrs); attr = p11_attrs_find (attrs, CKA_LABEL); - CuAssertPtrEquals (tc, attrs + 0, attr); + assert_ptr_eq (attrs + 0, attr); ret = p11_attrs_remove (attrs, CKA_LABEL); - CuAssertIntEquals (tc, CK_TRUE, ret); + assert_num_eq (CK_TRUE, ret); attr = p11_attrs_find (attrs, CKA_LABEL); - CuAssertPtrEquals (tc, NULL, attr); + assert_ptr_eq (NULL, attr); ret = p11_attrs_remove (attrs, CKA_LABEL); - CuAssertIntEquals (tc, CK_FALSE, ret); + assert_num_eq (CK_FALSE, ret); p11_attrs_free (attrs); } static void -test_match (CuTest *tc) +test_match (void) { CK_BBOOL vtrue = CK_TRUE; @@ -588,14 +588,14 @@ test_match (CuTest *tc) { CKA_INVALID }, }; - CuAssertTrue (tc, p11_attrs_match (attrs, attrs)); - CuAssertTrue (tc, p11_attrs_match (attrs, subset)); - CuAssertTrue (tc, !p11_attrs_match (attrs, different)); - CuAssertTrue (tc, !p11_attrs_match (attrs, extra)); + 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 (CuTest *tc) +test_matchn (void) { CK_BBOOL vtrue = CK_TRUE; @@ -620,13 +620,13 @@ test_matchn (CuTest *tc) { CKA_TOKEN, &vtrue, sizeof (vtrue) }, }; - CuAssertTrue (tc, p11_attrs_matchn (attrs, subset, 1)); - CuAssertTrue (tc, !p11_attrs_matchn (attrs, different, 2)); - CuAssertTrue (tc, !p11_attrs_matchn (attrs, extra, 3)); + 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 (CuTest *tc) +test_find_bool (void) { CK_BBOOL vtrue = CK_TRUE; CK_BBOOL vfalse = CK_FALSE; @@ -640,13 +640,13 @@ test_find_bool (CuTest *tc) { CKA_INVALID }, }; - CuAssertTrue (tc, p11_attrs_find_bool (attrs, CKA_TOKEN, &value) && value == CK_TRUE); - CuAssertTrue (tc, !p11_attrs_find_bool (attrs, CKA_LABEL, &value)); - CuAssertTrue (tc, !p11_attrs_find_bool (attrs, CKA_VALUE, &value)); + 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 (CuTest *tc) +test_find_ulong (void) { CK_ULONG v33 = 33UL; CK_ULONG v45 = 45UL; @@ -660,13 +660,13 @@ test_find_ulong (CuTest *tc) { CKA_INVALID }, }; - CuAssertTrue (tc, p11_attrs_find_ulong (attrs, CKA_BITS_PER_PIXEL, &value) && value == v33); - CuAssertTrue (tc, !p11_attrs_find_ulong (attrs, CKA_LABEL, &value)); - CuAssertTrue (tc, !p11_attrs_find_ulong (attrs, CKA_VALUE, &value)); + 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 (CuTest *tc) +test_find_value (void) { void *value; size_t length; @@ -681,21 +681,21 @@ test_find_value (CuTest *tc) }; value = p11_attrs_find_value (attrs, CKA_LABEL, &length); - CuAssertPtrEquals (tc, attrs[3].pValue, value); - CuAssertIntEquals (tc, 4, length); + assert_ptr_eq (attrs[3].pValue, value); + assert_num_eq (4, length); value = p11_attrs_find_value (attrs, CKA_LABEL, NULL); - CuAssertPtrEquals (tc, attrs[3].pValue, value); + assert_ptr_eq (attrs[3].pValue, value); value = p11_attrs_find_value (attrs, CKA_VALUE, &length); - CuAssertPtrEquals (tc, NULL, value); + assert_ptr_eq (NULL, value); value = p11_attrs_find_value (attrs, CKA_TOKEN, &length); - CuAssertPtrEquals (tc, NULL, value); + assert_ptr_eq (NULL, value); } static void -test_find_valid (CuTest *tc) +test_find_valid (void) { CK_ATTRIBUTE *attr; @@ -709,61 +709,46 @@ test_find_valid (CuTest *tc) }; attr = p11_attrs_find_valid (attrs, CKA_LABEL); - CuAssertPtrEquals (tc, attrs + 3, attr); + assert_ptr_eq (attrs + 3, attr); attr = p11_attrs_find_valid (attrs, CKA_VALUE); - CuAssertPtrEquals (tc, attrs + 4, attr); + assert_ptr_eq (attrs + 4, attr); attr = p11_attrs_find_valid (attrs, CKA_TOKEN); - CuAssertPtrEquals (tc, NULL, attr); + assert_ptr_eq (NULL, attr); } int -main (void) +main (int argc, + char *argv[]) { - CuString *output = CuStringNew (); - CuSuite* suite = CuSuiteNew (); - int ret; - - putenv ("P11_KIT_STRICT=1"); - p11_debug_init (); - - SUITE_ADD_TEST (suite, test_equal); - SUITE_ADD_TEST (suite, test_hash); - SUITE_ADD_TEST (suite, test_to_string); - - SUITE_ADD_TEST (suite, test_terminator); - SUITE_ADD_TEST (suite, test_count); - SUITE_ADD_TEST (suite, test_build_one); - SUITE_ADD_TEST (suite, test_build_two); - SUITE_ADD_TEST (suite, test_build_invalid); - SUITE_ADD_TEST (suite, test_buildn_one); - SUITE_ADD_TEST (suite, test_buildn_two); - SUITE_ADD_TEST (suite, test_build_add); - SUITE_ADD_TEST (suite, test_build_null); - SUITE_ADD_TEST (suite, test_dup); - SUITE_ADD_TEST (suite, test_take); - SUITE_ADD_TEST (suite, test_merge_replace); - SUITE_ADD_TEST (suite, test_merge_augment); - SUITE_ADD_TEST (suite, test_merge_empty); - SUITE_ADD_TEST (suite, test_free_null); - SUITE_ADD_TEST (suite, test_match); - SUITE_ADD_TEST (suite, test_matchn); - SUITE_ADD_TEST (suite, test_find); - SUITE_ADD_TEST (suite, test_findn); - SUITE_ADD_TEST (suite, test_find_bool); - SUITE_ADD_TEST (suite, test_find_ulong); - SUITE_ADD_TEST (suite, test_find_value); - SUITE_ADD_TEST (suite, test_find_valid); - SUITE_ADD_TEST (suite, test_remove); - - CuSuiteRun (suite); - CuSuiteSummary (suite, output); - CuSuiteDetails (suite, output); - printf ("%s\n", output->buffer); - ret = suite->failCount; - CuSuiteDelete (suite); - CuStringDelete (output); - - return ret; + 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-base64.c b/common/tests/test-base64.c index 90c1f49..ce303e8 100644 --- a/common/tests/test-base64.c +++ b/common/tests/test-base64.c @@ -33,7 +33,7 @@ */ #include "config.h" -#include "CuTest.h" +#include "test.h" #include "base64.h" #include "debug.h" @@ -45,9 +45,9 @@ #include static void -check_decode_msg (CuTest *tc, - const char *file, +check_decode_msg (const char *file, int line, + const char *function, const char *input, ssize_t input_len, const unsigned char *expected, @@ -63,33 +63,38 @@ check_decode_msg (CuTest *tc, length = p11_b64_pton (input, input_len, decoded, sizeof (decoded)); if (expected == NULL) { - CuAssert_Line (tc, file, line, "decoding should have failed", length < 0); + if (length >= 0) + p11_test_fail (file, line, function, "decoding should have failed"); } else { - CuAssert_Line (tc, file, line, "decoding failed", length >= 0); - CuAssertIntEquals_LineMsg (tc, file, line, "wrong length", expected_len, length); - CuAssert_Line (tc, file, line, "decoded wrong", memcmp (decoded, expected, length) == 0); + if (length < 0) + 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, "decoded wrong"); } } -#define check_decode_success(tc, input, input_len, expected, expected_len) \ - check_decode_msg (tc, __FILE__, __LINE__, input, input_len, expected, expected_len) +#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(tc, input, input_len) \ - check_decode_msg (tc, __FILE__, __LINE__, input, input_len, NULL, 0) +#define check_decode_failure(input, input_len) \ + check_decode_msg (__FILE__, __LINE__, __FUNCTION__, input, input_len, NULL, 0) static void -test_decode_simple (CuTest *tc) +test_decode_simple (void) { - check_decode_success (tc, "", 0, (unsigned char *)"", 0); - check_decode_success (tc, "MQ==", 0, (unsigned char *)"1", 0); - check_decode_success (tc, "YmxhaAo=", -1, (unsigned char *)"blah\n", -1); - check_decode_success (tc, "bGVlbGEK", -1, (unsigned char *)"leela\n", -1); - check_decode_success (tc, "bGVlbG9vCg==", -1, (unsigned char *)"leeloo\n", -1); + check_decode_success ("", 0, (unsigned char *)"", 0); + check_decode_success ("MQ==", 0, (unsigned char *)"1", 0); + check_decode_success ("YmxhaAo=", -1, (unsigned char *)"blah\n", -1); + check_decode_success ("bGVlbGEK", -1, (unsigned char *)"leela\n", -1); + check_decode_success ("bGVlbG9vCg==", -1, (unsigned char *)"leeloo\n", -1); } static void -test_decode_thawte (CuTest *tc) +test_decode_thawte (void) { const char *input = "MIIEKjCCAxKgAwIBAgIQYAGXt0an6rS0mtZLL/eQ+zANBgkqhkiG9w0BAQsFADCB" @@ -186,28 +191,14 @@ test_decode_thawte (CuTest *tc) 0x31, 0xd4, 0x40, 0x1a, 0x62, 0x34, 0x36, 0x3f, 0x35, 0x01, 0xae, 0xac, 0x63, 0xa0, }; - check_decode_success (tc, input, -1, output, sizeof (output)); + check_decode_success (input, -1, output, sizeof (output)); } int -main (void) +main (int argc, + char *argv[]) { - CuString *output = CuStringNew (); - CuSuite* suite = CuSuiteNew (); - int ret; - - putenv ("P11_KIT_STRICT=1"); - p11_debug_init (); - - SUITE_ADD_TEST (suite, test_decode_simple); - SUITE_ADD_TEST (suite, test_decode_thawte); - - CuSuiteRun (suite); - CuSuiteSummary (suite, output); - CuSuiteDetails (suite, output); - printf ("%s\n", output->buffer); - ret = suite->failCount; - CuSuiteDelete (suite); - CuStringDelete (output); - return ret; + p11_test (test_decode_simple, "/base64/decode-simple"); + p11_test (test_decode_thawte, "/base64/decode-thawte"); + return p11_test_run (argc, argv); } diff --git a/common/tests/test-buffer.c b/common/tests/test-buffer.c index baf7b73..4fd060d 100644 --- a/common/tests/test-buffer.c +++ b/common/tests/test-buffer.c @@ -33,7 +33,7 @@ */ #include "config.h" -#include "CuTest.h" +#include "test.h" #include #include @@ -43,41 +43,41 @@ #include "buffer.h" static void -test_init_uninit (CuTest *tc) +test_init_uninit (void) { p11_buffer buffer; p11_buffer_init (&buffer, 10); - CuAssertPtrNotNull (tc, buffer.data); - CuAssertIntEquals (tc, 0, buffer.len); - CuAssertIntEquals (tc, 0, buffer.flags); - CuAssertTrue (tc, buffer.size >= 10); - CuAssertPtrNotNull (tc, buffer.ffree); - CuAssertPtrNotNull (tc, buffer.frealloc); + 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 (CuTest *tc) +test_append (void) { p11_buffer buffer; p11_buffer_init (&buffer, 10); buffer.len = 5; p11_buffer_append (&buffer, 35); - CuAssertIntEquals (tc, 5 + 35, buffer.len); - CuAssertTrue (tc, buffer.size >= 35 + 5); + assert_num_eq (5 + 35, buffer.len); + assert (buffer.size >= 35 + 5); p11_buffer_append (&buffer, 15); - CuAssertIntEquals (tc, 5 + 35 + 15, buffer.len); - CuAssertTrue (tc, buffer.size >= 5 + 35 + 15); + assert_num_eq (5 + 35 + 15, buffer.len); + assert (buffer.size >= 5 + 35 + 15); p11_buffer_uninit (&buffer); } static void -test_null (CuTest *tc) +test_null (void) { p11_buffer buffer; @@ -85,7 +85,7 @@ test_null (CuTest *tc) p11_buffer_add (&buffer, "Blah", -1); p11_buffer_add (&buffer, " blah", -1); - CuAssertStrEquals (tc, "Blah blah", buffer.data); + assert_str_eq ("Blah blah", buffer.data); p11_buffer_uninit (&buffer); } @@ -109,7 +109,7 @@ mock_free (void *data) } static void -test_init_for_data (CuTest *tc) +test_init_for_data (void) { p11_buffer buffer; unsigned char *ret; @@ -121,29 +121,29 @@ test_init_for_data (CuTest *tc) p11_buffer_init_full (&buffer, (unsigned char *)strdup ("blah"), 4, 0, mock_realloc, mock_free); - CuAssertPtrNotNull (tc, buffer.data); - CuAssertStrEquals (tc, "blah", (char *)buffer.data); - CuAssertIntEquals (tc, 4, buffer.len); - CuAssertIntEquals (tc, 0, buffer.flags); - CuAssertIntEquals (tc, 4, buffer.size); - CuAssertPtrEquals (tc, mock_free, buffer.ffree); - CuAssertPtrEquals (tc, mock_realloc, buffer.frealloc); + 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); - CuAssertIntEquals (tc, 0, mock_realloced); - CuAssertIntEquals (tc, 0, mock_freed); + assert_num_eq (0, mock_realloced); + assert_num_eq (0, mock_freed); len = buffer.len; ret = p11_buffer_append (&buffer, 1024); - CuAssertPtrEquals (tc, (char *)buffer.data + len, ret); - CuAssertIntEquals (tc, 1, mock_realloced); + assert_ptr_eq ((char *)buffer.data + len, ret); + assert_num_eq (1, mock_realloced); p11_buffer_uninit (&buffer); - CuAssertIntEquals (tc, 1, mock_realloced); - CuAssertIntEquals (tc, 1, mock_freed); + assert_num_eq (1, mock_realloced); + assert_num_eq (1, mock_freed); } static void -test_steal (CuTest *tc) +test_steal (void) { p11_buffer buffer; char *string; @@ -154,61 +154,46 @@ test_steal (CuTest *tc) p11_buffer_init_full (&buffer, (unsigned char *)strdup ("blah"), 4, P11_BUFFER_NULL, mock_realloc, mock_free); - CuAssertPtrNotNull (tc, buffer.data); - CuAssertStrEquals (tc, "blah", buffer.data); + assert_ptr_not_null (buffer.data); + assert_str_eq ("blah", buffer.data); p11_buffer_add (&buffer, " yada", -1); - CuAssertStrEquals (tc, "blah yada", buffer.data); + assert_str_eq ("blah yada", buffer.data); string = p11_buffer_steal (&buffer, &length); p11_buffer_uninit (&buffer); - CuAssertStrEquals (tc, "blah yada", string); - CuAssertIntEquals (tc, 9, length); - CuAssertIntEquals (tc, 0, mock_freed); + assert_str_eq ("blah yada", string); + assert_num_eq (9, length); + assert_num_eq (0, mock_freed); free (string); } static void -test_add (CuTest *tc) +test_add (void) { p11_buffer buffer; p11_buffer_init (&buffer, 10); p11_buffer_add (&buffer, (unsigned char *)"Planet Express", 15); - CuAssertIntEquals (tc, 15, buffer.len); - CuAssertStrEquals (tc, "Planet Express", (char *)buffer.data); - CuAssertTrue (tc, p11_buffer_ok (&buffer)); + 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 (void) +main (int argc, + char *argv[]) { - CuString *output = CuStringNew (); - CuSuite* suite = CuSuiteNew (); - int ret; - - putenv ("P11_KIT_STRICT=1"); - p11_debug_init (); - - SUITE_ADD_TEST (suite, test_init_uninit); - SUITE_ADD_TEST (suite, test_init_for_data); - SUITE_ADD_TEST (suite, test_append); - SUITE_ADD_TEST (suite, test_null); - SUITE_ADD_TEST (suite, test_add); - SUITE_ADD_TEST (suite, test_steal); - - CuSuiteRun (suite); - CuSuiteSummary (suite, output); - CuSuiteDetails (suite, output); - printf ("%s\n", output->buffer); - ret = suite->failCount; - CuSuiteDelete (suite); - CuStringDelete (output); - - return ret; + 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 index 066e723..f1960ce 100644 --- a/common/tests/test-compat.c +++ b/common/tests/test-compat.c @@ -33,7 +33,7 @@ */ #include "config.h" -#include "CuTest.h" +#include "test.h" #include #include @@ -42,36 +42,24 @@ #include "compat.h" static void -test_strndup (CuTest *tc) +test_strndup (void) { char unterminated[] = { 't', 'e', 's', 't', 'e', 'r', 'o', 'n', 'i', 'o' }; char *res; res = strndup (unterminated, 6); - CuAssertStrEquals (tc, res, "tester"); + assert_str_eq (res, "tester"); free (res); res = strndup ("test", 6); - CuAssertStrEquals (tc, res, "test"); + assert_str_eq (res, "test"); free (res); } int -main (void) +main (int argc, + char *argv[]) { - CuString *output = CuStringNew (); - CuSuite* suite = CuSuiteNew (); - int ret; - - SUITE_ADD_TEST (suite, test_strndup); - - CuSuiteRun (suite); - CuSuiteSummary (suite, output); - CuSuiteDetails (suite, output); - printf ("%s\n", output->buffer); - ret = suite->failCount; - CuSuiteDelete (suite); - CuStringDelete (output); - - return ret; + p11_test (test_strndup, "/test/strndup"); + return p11_test_run (argc, argv); } diff --git a/common/tests/test-constants.c b/common/tests/test-constants.c index 4c2f3eb..76c44d2 100644 --- a/common/tests/test-constants.c +++ b/common/tests/test-constants.c @@ -33,7 +33,7 @@ */ #include "config.h" -#include "CuTest.h" +#include "test.h" #include #include @@ -44,7 +44,7 @@ #include "debug.h" static void -test_constants (CuTest *tc) +test_constants (void) { const p11_constant *constant; p11_dict *nicks, *names; @@ -72,29 +72,27 @@ test_constants (CuTest *tc) for (j = 0; constants[j] != NULL; j++) { constant = constants[j]; for (i = 1; constant[i].value != CKA_INVALID; i++) { - if (constant[i].value < constant[i - 1].value) { - CuFail_Line (tc, __FILE__, __LINE__, - "attr constant out of order", constant[i].name); - } + 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++) { - CuAssertPtrNotNull (tc, constant[i].name); + assert_ptr_not_null (constant[i].name); if (constant[i].nick) { - CuAssertStrEquals (tc, constant[i].nick, - p11_constant_nick (constant, constant[i].value)); + assert_str_eq (constant[i].nick, + p11_constant_nick (constant, constant[i].value)); } - CuAssertStrEquals (tc, constant[i].name, - p11_constant_name (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); - CuAssertIntEquals (tc, constant[i].value, check); + assert_num_eq (constant[i].value, check); } check = p11_constant_resolve (names, constant[i].name); - CuAssertIntEquals (tc, constant[i].value, check); + assert_num_eq (constant[i].value, check); } } @@ -103,23 +101,10 @@ test_constants (CuTest *tc) } int -main (void) +main (int argc, + char *argv[]) { - CuString *output = CuStringNew (); - CuSuite* suite = CuSuiteNew (); - int ret; - - putenv ("P11_KIT_STRICT=1"); - p11_debug_init (); - SUITE_ADD_TEST (suite, test_constants); - - CuSuiteRun (suite); - CuSuiteSummary (suite, output); - CuSuiteDetails (suite, output); - printf ("%s\n", output->buffer); - ret = suite->failCount; - CuSuiteDelete (suite); - CuStringDelete (output); + p11_test (test_constants, "/constants/all"); - return ret; + return p11_test_run (argc, argv); } diff --git a/common/tests/test-dict.c b/common/tests/test-dict.c index fc40b07..7c6f851 100644 --- a/common/tests/test-dict.c +++ b/common/tests/test-dict.c @@ -33,7 +33,7 @@ */ #include "config.h" -#include "CuTest.h" +#include "test.h" #include #include @@ -43,17 +43,17 @@ #include "dict.h" static void -test_create (CuTest *tc) +test_create (void) { p11_dict *map; map = p11_dict_new (p11_dict_direct_hash, p11_dict_direct_equal, NULL, NULL); - CuAssertPtrNotNull (tc, map); + assert_ptr_not_null (map); p11_dict_free (map); } static void -test_free_null (CuTest *tc) +test_free_null (void) { p11_dict_free (NULL); } @@ -98,24 +98,24 @@ value_destroy (void *data) } static void -test_free_destroys (CuTest *tc) +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); - CuAssertPtrNotNull (tc, map); + assert_ptr_not_null (map); if (!p11_dict_set (map, &key, &value)) - CuFail (tc, "should not be reached"); + assert_not_reached (); p11_dict_free (map); - CuAssertIntEquals (tc, true, key.freed); - CuAssertIntEquals (tc, 2, value); + assert_num_eq (true, key.freed); + assert_num_eq (2, value); } static void -test_iterate (CuTest *tc) +test_iterate (void) { p11_dict *map; p11_dictiter iter; @@ -126,19 +126,19 @@ test_iterate (CuTest *tc) int ret; map = p11_dict_new (p11_dict_direct_hash, p11_dict_direct_equal, NULL, NULL); - CuAssertPtrNotNull (tc, map); + assert_ptr_not_null (map); if (!p11_dict_set (map, &key, &value)) - CuFail (tc, "should not be reached"); + assert_not_reached (); p11_dict_iterate (map, &iter); ret = p11_dict_next (&iter, &pkey, &pvalue); - CuAssertIntEquals (tc, 1, ret); - CuAssertPtrEquals (tc, pkey, &key); - CuAssertPtrEquals (tc, pvalue, &value); + assert_num_eq (1, ret); + assert_ptr_eq (pkey, &key); + assert_ptr_eq (pvalue, &value); ret = p11_dict_next (&iter, &pkey, &pvalue); - CuAssertIntEquals (tc, 0, ret); + assert_num_eq (0, ret); p11_dict_free (map); } @@ -153,7 +153,7 @@ compar_strings (const void *one, } static void -test_iterate_remove (CuTest *tc) +test_iterate_remove (void) { p11_dict *map; p11_dictiter iter; @@ -165,45 +165,45 @@ test_iterate_remove (CuTest *tc) int i; map = p11_dict_new (p11_dict_str_hash, p11_dict_str_equal, NULL, NULL); - CuAssertPtrNotNull (tc, map); + assert_ptr_not_null (map); for (i = 0; i < 3; i++) { if (!p11_dict_set (map, keys[i], values[i])) - CuFail (tc, "should not be reached"); + assert_not_reached (); } p11_dict_iterate (map, &iter); ret = p11_dict_next (&iter, &okeys[0], &ovalues[0]); - CuAssertIntEquals (tc, true, ret); + assert_num_eq (true, ret); ret = p11_dict_next (&iter, &okeys[1], &ovalues[1]); - CuAssertIntEquals (tc, true, ret); + assert_num_eq (true, ret); if (!p11_dict_remove (map, okeys[1])) - CuFail (tc, "should not be reached"); + assert_not_reached (); ret = p11_dict_next (&iter, &okeys[2], &ovalues[2]); - CuAssertIntEquals (tc, true, ret); + assert_num_eq (true, ret); ret = p11_dict_next (&iter, NULL, NULL); - CuAssertIntEquals (tc, false, ret); + assert_num_eq (false, ret); - CuAssertIntEquals (tc, 2, p11_dict_size (map)); + 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++) { - CuAssertStrEquals (tc, keys[i], okeys[i]); - CuAssertPtrEquals (tc, keys[i], okeys[i]); - CuAssertStrEquals (tc, values[i], ovalues[i]); - CuAssertPtrEquals (tc, values[i], ovalues[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 (CuTest *tc) +test_set_get (void) { char *key = "KEY"; char *value = "VALUE"; @@ -213,13 +213,13 @@ test_set_get (CuTest *tc) map = p11_dict_new (p11_dict_str_hash, p11_dict_str_equal, NULL, NULL); p11_dict_set (map, key, value); check = p11_dict_get (map, key); - CuAssertPtrEquals (tc, check, value); + assert_ptr_eq (check, value); p11_dict_free (map); } static void -test_set_get_remove (CuTest *tc) +test_set_get_remove (void) { char *key = "KEY"; char *value = "VALUE"; @@ -230,24 +230,24 @@ test_set_get_remove (CuTest *tc) map = p11_dict_new (p11_dict_str_hash, p11_dict_str_equal, NULL, NULL); if (!p11_dict_set (map, key, value)) - CuFail (tc, "should not be reached"); + assert_not_reached (); check = p11_dict_get (map, key); - CuAssertPtrEquals (tc, check, value); + assert_ptr_eq (check, value); ret = p11_dict_remove (map, key); - CuAssertIntEquals (tc, true, ret); + assert_num_eq (true, ret); ret = p11_dict_remove (map, key); - CuAssertIntEquals (tc, false, ret); + assert_num_eq (false, ret); check = p11_dict_get (map, key); - CuAssert (tc, "should be null", check == NULL); + assert (check == NULL); p11_dict_free (map); } static void -test_set_clear (CuTest *tc) +test_set_clear (void) { char *key = "KEY"; char *value = "VALUE"; @@ -257,18 +257,18 @@ test_set_clear (CuTest *tc) map = p11_dict_new (p11_dict_direct_hash, p11_dict_direct_equal, NULL, NULL); if (!p11_dict_set (map, key, value)) - CuFail (tc, "should not be reached"); + assert_not_reached (); p11_dict_clear (map); check = p11_dict_get (map, key); - CuAssert (tc, "should be null", check == NULL); + assert (check == NULL); p11_dict_free (map); } static void -test_remove_destroys (CuTest *tc) +test_remove_destroys (void) { p11_dict *map; Key key = { 8, 0 }; @@ -276,23 +276,23 @@ test_remove_destroys (CuTest *tc) bool ret; map = p11_dict_new (key_hash, key_equal, key_destroy, value_destroy); - CuAssertPtrNotNull (tc, map); + assert_ptr_not_null (map); if (!p11_dict_set (map, &key, &value)) - CuFail (tc, "should not be reached"); + assert_not_reached (); ret = p11_dict_remove (map, &key); - CuAssertIntEquals (tc, true, ret); - CuAssertIntEquals (tc, true, key.freed); - CuAssertIntEquals (tc, 2, value); + 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); - CuAssertIntEquals (tc, false, ret); - CuAssertIntEquals (tc, false, key.freed); - CuAssertIntEquals (tc, 0, value); + assert_num_eq (false, ret); + assert_num_eq (false, key.freed); + assert_num_eq (0, value); /* should not be destroyed again */ key.freed = false; @@ -300,12 +300,12 @@ test_remove_destroys (CuTest *tc) p11_dict_free (map); - CuAssertIntEquals (tc, false, key.freed); - CuAssertIntEquals (tc, 0, value); + assert_num_eq (false, key.freed); + assert_num_eq (0, value); } static void -test_set_destroys (CuTest *tc) +test_set_destroys (void) { p11_dict *map; Key key = { 8, 0 }; @@ -314,88 +314,88 @@ test_set_destroys (CuTest *tc) bool ret; map = p11_dict_new (key_hash, key_equal, key_destroy, value_destroy); - CuAssertPtrNotNull (tc, map); + assert_ptr_not_null (map); if (!p11_dict_set (map, &key, &value)) - CuFail (tc, "should not be reached"); + 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); - CuAssertIntEquals (tc, true, ret); - CuAssertIntEquals (tc, false, key.freed); - CuAssertIntEquals (tc, false, key2.freed); - CuAssertIntEquals (tc, 0, value); - CuAssertIntEquals (tc, 0, value2); + 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); - CuAssertIntEquals (tc, true, ret); - CuAssertIntEquals (tc, true, key.freed); - CuAssertIntEquals (tc, false, key2.freed); - CuAssertIntEquals (tc, 0, value); - CuAssertIntEquals (tc, 0, value2); + 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); - CuAssertIntEquals (tc, true, ret); - CuAssertIntEquals (tc, false, key.freed); - CuAssertIntEquals (tc, false, key2.freed); - CuAssertIntEquals (tc, 2, value); - CuAssertIntEquals (tc, 0, 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); - CuAssertIntEquals (tc, true, ret); - CuAssertIntEquals (tc, false, key.freed); - CuAssertIntEquals (tc, true, key2.freed); - CuAssertIntEquals (tc, 0, value); - CuAssertIntEquals (tc, 2, value2); + 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); - CuAssertIntEquals (tc, true, key.freed); - CuAssertIntEquals (tc, 2, value); - CuAssertIntEquals (tc, false, key2.freed); - CuAssertIntEquals (tc, 0, value2); + 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 (CuTest *tc) +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); - CuAssertPtrNotNull (tc, map); + assert_ptr_not_null (map); if (!p11_dict_set (map, &key, &value)) - CuFail (tc, "should not be reached"); + assert_not_reached (); p11_dict_clear (map); - CuAssertIntEquals (tc, true, key.freed); - CuAssertIntEquals (tc, 2, value); + 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); - CuAssertIntEquals (tc, false, key.freed); - CuAssertIntEquals (tc, 0, value); + assert_num_eq (false, key.freed); + assert_num_eq (0, value); /* should not be destroyed again */ key.freed = false; @@ -403,8 +403,8 @@ test_clear_destroys (CuTest *tc) p11_dict_free (map); - CuAssertIntEquals (tc, false, key.freed); - CuAssertIntEquals (tc, 0, value); + assert_num_eq (false, key.freed); + assert_num_eq (0, value); } static unsigned int @@ -415,7 +415,7 @@ test_hash_intptr_with_collisions (const void *data) } static void -test_hash_add_check_lots_and_collisions (CuTest *tc) +test_hash_add_check_lots_and_collisions (void) { p11_dict *map; int *value; @@ -428,20 +428,20 @@ test_hash_add_check_lots_and_collisions (CuTest *tc) value = malloc (sizeof (int)); *value = i; if (!p11_dict_set (map, value, value)) - CuFail (tc, "should not be reached"); + assert_not_reached (); } for (i = 0; i < 20000; ++i) { value = p11_dict_get (map, &i); - CuAssertPtrNotNull (tc, value); - CuAssertIntEquals (tc, i, *value); + assert_ptr_not_null (value); + assert_num_eq (i, *value); } p11_dict_free (map); } static void -test_hash_count (CuTest *tc) +test_hash_count (void) { p11_dict *map; int *value; @@ -450,30 +450,30 @@ test_hash_count (CuTest *tc) map = p11_dict_new (p11_dict_intptr_hash, p11_dict_intptr_equal, NULL, free); - CuAssertIntEquals (tc, 0, p11_dict_size (map)); + assert_num_eq (0, p11_dict_size (map)); for (i = 0; i < 20000; ++i) { value = malloc (sizeof (int)); *value = i; if (!p11_dict_set (map, value, value)) - CuFail (tc, "should not be reached"); - CuAssertIntEquals (tc, i + 1, p11_dict_size (map)); + assert_not_reached (); + assert_num_eq (i + 1, p11_dict_size (map)); } for (i = 0; i < 20000; ++i) { ret = p11_dict_remove (map, &i); - CuAssertIntEquals (tc, true, ret); - CuAssertIntEquals (tc, 20000 - (i + 1), p11_dict_size (map)); + assert_num_eq (true, ret); + assert_num_eq (20000 - (i + 1), p11_dict_size (map)); } p11_dict_clear (map); - CuAssertIntEquals (tc, 0, p11_dict_size (map)); + assert_num_eq (0, p11_dict_size (map)); p11_dict_free (map); } static void -test_hash_ulongptr (CuTest *tc) +test_hash_ulongptr (void) { p11_dict *map; unsigned long *value; @@ -485,47 +485,35 @@ test_hash_ulongptr (CuTest *tc) value = malloc (sizeof (unsigned long)); *value = i; if (!p11_dict_set (map, value, value)) - CuFail (tc, "should not be reached"); + assert_not_reached (); } for (i = 0; i < 20000; ++i) { value = p11_dict_get (map, &i); - CuAssertPtrNotNull (tc, value); - CuAssertIntEquals (tc, i, *value); + assert_ptr_not_null (value); + assert_num_eq (i, *value); } p11_dict_free (map); } int -main (void) +main (int argc, + char *argv[]) { - CuString *output = CuStringNew (); - CuSuite* suite = CuSuiteNew (); - int ret; - - SUITE_ADD_TEST (suite, test_create); - SUITE_ADD_TEST (suite, test_set_get); - SUITE_ADD_TEST (suite, test_set_get_remove); - SUITE_ADD_TEST (suite, test_remove_destroys); - SUITE_ADD_TEST (suite, test_set_clear); - SUITE_ADD_TEST (suite, test_set_destroys); - SUITE_ADD_TEST (suite, test_clear_destroys); - SUITE_ADD_TEST (suite, test_free_null); - SUITE_ADD_TEST (suite, test_free_destroys); - SUITE_ADD_TEST (suite, test_iterate); - SUITE_ADD_TEST (suite, test_iterate_remove); - SUITE_ADD_TEST (suite, test_hash_add_check_lots_and_collisions); - SUITE_ADD_TEST (suite, test_hash_count); - SUITE_ADD_TEST (suite, test_hash_ulongptr); - - CuSuiteRun (suite); - CuSuiteSummary (suite, output); - CuSuiteDetails (suite, output); - printf ("%s\n", output->buffer); - ret = suite->failCount; - CuSuiteDelete (suite); - CuStringDelete (output); - - return ret; + 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 index eecf09b..c679cad 100644 --- a/common/tests/test-hash.c +++ b/common/tests/test-hash.c @@ -33,7 +33,7 @@ */ #include "config.h" -#include "CuTest.h" +#include "test.h" #include #include @@ -56,7 +56,7 @@ const char *sha1_checksum[] = { }; static void -test_sha1 (CuTest *cu) +test_sha1 (void) { unsigned char checksum[P11_HASH_SHA1_LEN]; size_t len; @@ -67,28 +67,28 @@ test_sha1 (CuTest *cu) len = strlen (sha1_input[i]); p11_hash_sha1 (checksum, sha1_input[i], len, NULL); - CuAssertTrue (cu, memcmp (sha1_checksum[i], checksum, P11_HASH_SHA1_LEN) == 0); + assert (memcmp (sha1_checksum[i], checksum, P11_HASH_SHA1_LEN) == 0); if (len > 6) { p11_hash_sha1 (checksum, sha1_input[i], 6, sha1_input[i] + 6, len - 6, NULL); - CuAssertTrue (cu, memcmp (sha1_checksum[i], checksum, P11_HASH_SHA1_LEN) == 0); + assert (memcmp (sha1_checksum[i], checksum, P11_HASH_SHA1_LEN) == 0); } } } static void -test_sha1_long (CuTest *cu) +test_sha1_long (void) { unsigned char checksum[P11_HASH_SHA1_LEN]; char *expected = "\x34\xAA\x97\x3C\xD4\xC4\xDA\xA4\xF6\x1E\xEB\x2B\xDB\xAD\x27\x31\x65\x34\x01\x6F"; char *input; input = malloc (1000000); - CuAssertTrue (cu, input != NULL); + assert (input != NULL); memset (input, 'a', 1000000); p11_hash_sha1 (checksum, input, 1000000, NULL); - CuAssertTrue (cu, memcmp (expected, checksum, P11_HASH_SHA1_LEN) == 0); + assert (memcmp (expected, checksum, P11_HASH_SHA1_LEN) == 0); free (input); } @@ -112,7 +112,7 @@ const char *md5_checksum[] = { }; static void -test_md5 (CuTest *cu) +test_md5 (void) { unsigned char checksum[P11_HASH_MD5_LEN]; size_t len; @@ -123,17 +123,17 @@ test_md5 (CuTest *cu) len = strlen (md5_input[i]); p11_hash_md5 (checksum, md5_input[i], len, NULL); - CuAssertTrue (cu, memcmp (md5_checksum[i], checksum, P11_HASH_MD5_LEN) == 0); + assert (memcmp (md5_checksum[i], checksum, P11_HASH_MD5_LEN) == 0); if (len > 5) { p11_hash_md5 (checksum, md5_input[i], 5, md5_input[i] + 5, len - 5, NULL); - CuAssertTrue (cu, memcmp (md5_checksum[i], checksum, P11_HASH_MD5_LEN) == 0); + assert (memcmp (md5_checksum[i], checksum, P11_HASH_MD5_LEN) == 0); } } } static void -test_murmur2 (CuTest *cu) +test_murmur3 (void) { uint32_t one, two, four, seven, eleven, split; @@ -146,23 +146,23 @@ test_murmur2 (CuTest *cu) p11_hash_murmur3 ((unsigned char *)&eleven, "eleven", 6, NULL); p11_hash_murmur3 ((unsigned char *)&split, "ele", 3, "ven", 3, NULL); - CuAssertTrue (cu, one != two); - CuAssertTrue (cu, one != four); - CuAssertTrue (cu, one != seven); - CuAssertTrue (cu, one != eleven); + assert (one != two); + assert (one != four); + assert (one != seven); + assert (one != eleven); - CuAssertTrue (cu, two != four); - CuAssertTrue (cu, two != seven); - CuAssertTrue (cu, two != eleven); + assert (two != four); + assert (two != seven); + assert (two != eleven); - CuAssertTrue (cu, four != seven); - CuAssertTrue (cu, four != eleven); + assert (four != seven); + assert (four != eleven); - CuAssertTrue (cu, split == eleven); + assert (split == eleven); } static void -test_murmur2_incr (CuTest *cu) +test_murmur3_incr (void) { uint32_t first, second; @@ -182,29 +182,17 @@ test_murmur2_incr (CuTest *cu) "!", (size_t)1, NULL); - CuAssertIntEquals (cu, first, second); + assert_num_eq (first, second); } int -main (void) +main (int argc, + char *argv[]) { - CuString *output = CuStringNew (); - CuSuite* suite = CuSuiteNew (); - int ret; - - SUITE_ADD_TEST (suite, test_sha1); - SUITE_ADD_TEST (suite, test_sha1_long); - SUITE_ADD_TEST (suite, test_md5); - SUITE_ADD_TEST (suite, test_murmur2); - SUITE_ADD_TEST (suite, test_murmur2_incr); - - CuSuiteRun (suite); - CuSuiteSummary (suite, output); - CuSuiteDetails (suite, output); - printf ("%s\n", output->buffer); - ret = suite->failCount; - CuSuiteDelete (suite); - CuStringDelete (output); - - return ret; + p11_test (test_sha1, "/hash/sha1"); + p11_test (test_sha1_long, "/hash/sha1-long"); + p11_test (test_md5, "/hash/md5"); + 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 index 58d5d65..ff18a89 100644 --- a/common/tests/test-lexer.c +++ b/common/tests/test-lexer.c @@ -33,7 +33,7 @@ */ #include "config.h" -#include "CuTest.h" +#include "test.h" #include #include @@ -62,9 +62,9 @@ on_pem_get_type (const char *type, } static void -check_lex_msg (CuTest *tc, - const char *file, +check_lex_msg (const char *file, int line, + const char *function, const expected_tok *expected, const char *input, bool failure) @@ -77,60 +77,63 @@ check_lex_msg (CuTest *tc, p11_lexer_init (&lexer, "test", input, strlen (input)); for (i = 0; p11_lexer_next (&lexer, &failed); i++) { - CuAssertIntEquals_LineMsg (tc, file, line, - "lexer token type does not match", - expected[i].tok_type, lexer.tok_type); + 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: - CuAssertStrEquals_LineMsg (tc, file, line, - "field name doesn't match", - expected[i].name, lexer.tok.field.name); - CuAssertStrEquals_LineMsg (tc, file, line, - "field value doesn't match", - expected[i].value, lexer.tok.field.value); + 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: - CuAssertStrEquals_LineMsg (tc, file, line, - "section name doesn't match", - expected[i].name, lexer.tok.field.name); + 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: type = NULL; count = p11_pem_parse (lexer.tok.pem.begin, lexer.tok.pem.length, on_pem_get_type, &type); - CuAssertIntEquals_LineMsg (tc, file, line, - "wrong number of PEM blocks", - 1, count); - CuAssertStrEquals_LineMsg (tc, file, line, - "wrong type of PEM block", - expected[i].name, type); + if (count != 1) + p11_test_fail (file, line, function, "more than one PEM block: %d", count); + if (strcmp (expected[i].name, type) != 0) + p11_test_fail (file, line, function, + "wrong type of PEM block: (%s != %s)", + expected[i].name, type); free (type); break; case TOK_EOF: - CuFail_Line (tc, file, line, NULL, "eof should not be recieved"); + p11_test_fail (file, line, function, "eof should not be recieved"); break; } } - if (failure) - CuAssert_Line (tc, file, line, "lexing didn't fail", failed); - else - CuAssert_Line (tc, file, line, "lexing failed", !failed); - CuAssertIntEquals_LineMsg (tc, file, line, - "premature end of lexing", - TOK_EOF, expected[i].tok_type); + 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(tc, expected, input) \ - check_lex_msg (tc, __FILE__, __LINE__, expected, input, false) +#define check_lex_success(expected, input) \ + check_lex_msg (__FILE__, __LINE__, __FUNCTION__, expected, input, false) -#define check_lex_failure(tc, expected, input) \ - check_lex_msg (tc, __FILE__, __LINE__, expected, input, true) +#define check_lex_failure(expected, input) \ + check_lex_msg (__FILE__, __LINE__, __FUNCTION__, expected, input, true) static void -test_basic (CuTest *tc) +test_basic (void) { const char *input = "[the header]\n" "field: value\n" @@ -145,11 +148,11 @@ test_basic (CuTest *tc) { TOK_EOF } }; - check_lex_success (tc, expected, input); + check_lex_success (expected, input); } static void -test_corners (CuTest *tc) +test_corners (void) { const char *input = "\r\n" /* blankline */ " [the header]\r\n" /* bad line endings */ @@ -175,11 +178,11 @@ test_corners (CuTest *tc) { TOK_EOF } }; - check_lex_success (tc, expected, input); + check_lex_success (expected, input); } static void -test_following (CuTest *tc) +test_following (void) { const char *input = "-----BEGIN BLOCK1-----\n" "aYNNXqshlVxCdo8QfKeXh3GUzd/yn4LYIVgQrx4a\n" @@ -192,11 +195,11 @@ test_following (CuTest *tc) { TOK_EOF } }; - check_lex_success (tc, expected, input); + check_lex_success (expected, input); } static void -test_bad_pem (CuTest *tc) +test_bad_pem (void) { const char *input = "field: value\n" "-----BEGIN BLOCK1-----\n" @@ -209,13 +212,13 @@ test_bad_pem (CuTest *tc) p11_message_quiet (); - check_lex_failure (tc, expected, input); + check_lex_failure (expected, input); p11_message_loud (); } static void -test_bad_section (CuTest *tc) +test_bad_section (void) { const char *input = "field: value\n" "[section\n" @@ -228,13 +231,13 @@ test_bad_section (CuTest *tc) p11_message_quiet (); - check_lex_failure (tc, expected, input); + check_lex_failure (expected, input); p11_message_loud (); } static void -test_bad_value (CuTest *tc) +test_bad_value (void) { const char *input = "field_value\n" "[section\n" @@ -246,35 +249,20 @@ test_bad_value (CuTest *tc) p11_message_quiet (); - check_lex_failure (tc, expected, input); + check_lex_failure (expected, input); p11_message_loud (); } int -main (void) +main (int argc, + char *argv[]) { - CuString *output = CuStringNew (); - CuSuite* suite = CuSuiteNew (); - int ret; - - putenv ("P11_KIT_STRICT=1"); - p11_debug_init (); - - SUITE_ADD_TEST (suite, test_basic); - SUITE_ADD_TEST (suite, test_corners); - SUITE_ADD_TEST (suite, test_following); - SUITE_ADD_TEST (suite, test_bad_pem); - SUITE_ADD_TEST (suite, test_bad_section); - SUITE_ADD_TEST (suite, test_bad_value); - - CuSuiteRun (suite); - CuSuiteSummary (suite, output); - CuSuiteDetails (suite, output); - printf ("%s\n", output->buffer); - ret = suite->failCount; - CuSuiteDelete (suite); - CuStringDelete (output); - - return ret; + 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-oid.c b/common/tests/test-oid.c index 71b8278..05945d9 100644 --- a/common/tests/test-oid.c +++ b/common/tests/test-oid.c @@ -33,7 +33,7 @@ */ #include "config.h" -#include "CuTest.h" +#include "test.h" #include #include @@ -47,7 +47,7 @@ #include "pkix.asn.h" static void -test_known_oids (CuTest *cu) +test_known_oids (void) { char buffer[128]; node_asn *definitions = NULL; @@ -79,29 +79,29 @@ test_known_oids (CuTest *cu) }; ret = asn1_array2tree (pkix_asn1_tab, &definitions, NULL); - CuAssertTrue (cu, ret == ASN1_SUCCESS); + assert (ret == ASN1_SUCCESS); for (i = 0; known_oids[i].oid != NULL; i++) { - CuAssertTrue (cu, p11_oid_simple (known_oids[i].oid, known_oids[i].length)); - CuAssertIntEquals (cu, known_oids[i].length, p11_oid_length (known_oids[i].oid)); - CuAssertTrue (cu, p11_oid_equal (known_oids[i].oid, known_oids[i].oid)); + assert (p11_oid_simple (known_oids[i].oid, known_oids[i].length)); + assert_num_eq (known_oids[i].length, p11_oid_length (known_oids[i].oid)); + assert (p11_oid_equal (known_oids[i].oid, known_oids[i].oid)); if (i > 0) - CuAssertTrue (cu, !p11_oid_equal (known_oids[i].oid, known_oids[i - 1].oid)); + assert (!p11_oid_equal (known_oids[i].oid, known_oids[i - 1].oid)); /* AttributeType is a OBJECT IDENTIFIER */ ret = asn1_create_element (definitions, "PKIX1.AttributeType", &node); - CuAssertTrue (cu, ret == ASN1_SUCCESS); + assert (ret == ASN1_SUCCESS); ret = asn1_der_decoding (&node, known_oids[i].oid, known_oids[i].length, NULL); - CuAssertTrue (cu, ret == ASN1_SUCCESS); + assert (ret == ASN1_SUCCESS); len = sizeof (buffer); ret = asn1_read_value (node, "", buffer, &len); - CuAssertTrue (cu, ret == ASN1_SUCCESS); + assert (ret == ASN1_SUCCESS); - CuAssertStrEquals (cu, known_oids[i].string, buffer); + assert_str_eq (known_oids[i].string, buffer); asn1_delete_structure (&node); } @@ -110,24 +110,9 @@ test_known_oids (CuTest *cu) } int -main (void) +main (int argc, + char *argv[]) { - CuString *output = CuStringNew (); - CuSuite* suite = CuSuiteNew (); - int ret; - - putenv ("P11_KIT_STRICT=1"); - p11_debug_init (); - - SUITE_ADD_TEST (suite, test_known_oids); - - CuSuiteRun (suite); - CuSuiteSummary (suite, output); - CuSuiteDetails (suite, output); - printf ("%s\n", output->buffer); - ret = suite->failCount; - CuSuiteDelete (suite); - CuStringDelete (output); - - return ret; + p11_test (test_known_oids, "/oids/known"); + return p11_test_run (argc, argv); } diff --git a/common/tests/test-path.c b/common/tests/test-path.c index def4199..54d6f29 100644 --- a/common/tests/test-path.c +++ b/common/tests/test-path.c @@ -33,7 +33,7 @@ */ #include "config.h" -#include "CuTest.h" +#include "test.h" #include #include @@ -43,7 +43,7 @@ #include "path.h" static void -test_base (CuTest *tc) +test_base (void) { struct { const char *in; @@ -70,27 +70,16 @@ test_base (CuTest *tc) for (i = 0; fixtures[i].in != NULL; i++) { out = p11_path_base (fixtures[i].in); - CuAssertStrEquals (tc, fixtures[i].out, out); + assert_str_eq (fixtures[i].out, out); free (out); } } -static void -check_equals_and_free_msg (CuTest *tc, - const char *file, - int line, - const char *ex, - char *ac) -{ - CuAssertStrEquals_LineMsg (tc, file, line, NULL, ex, ac); - free (ac); -} - #define check_equals_and_free(tc, ex, ac) \ - check_equals_and_free_msg ((tc), __FILE__, __LINE__, (ex), (ac)) + do { assert_str_eq (ex, ac); free (ac); } while (0) static void -test_build (CuTest *tc) +test_build (void) { #ifdef OS_UNIX check_equals_and_free (tc, "/root/second", @@ -118,7 +107,7 @@ test_build (CuTest *tc) } static void -test_expand (CuTest *tc) +test_expand (void) { char *path; @@ -151,52 +140,41 @@ test_expand (CuTest *tc) putenv("HOME="); path = p11_path_expand ("$HOME/this/is/my/path"); - CuAssertTrue (tc, strstr (path, "this/is/my/path") != NULL); + assert (strstr (path, "this/is/my/path") != NULL); free (path); putenv("HOME="); path = p11_path_expand ("~/this/is/my/path"); - CuAssertTrue (tc, strstr (path, "this/is/my/path") != NULL); + assert (strstr (path, "this/is/my/path") != NULL); free (path); putenv("TEMP="); path = p11_path_expand ("$TEMP/this/is/my/path"); - CuAssertTrue (tc, strstr (path, "this/is/my/path") != NULL); + assert (strstr (path, "this/is/my/path") != NULL); free (path); } static void -test_absolute (CuTest *tc) +test_absolute (void) { #ifdef OS_UNIX - CuAssertTrue (tc, p11_path_absolute ("/home")); - CuAssertTrue (tc, !p11_path_absolute ("home")); + assert (p11_path_absolute ("/home")); + assert (!p11_path_absolute ("home")); #else /* OS_WIN32 */ - CuAssertTrue (tc, p11_path_absolute ("C:\\home")); - CuAssertTrue (tc, !p11_path_absolute ("home")); - CuAssertTrue (tc, p11_path_absolute ("/home")); + assert (p11_path_absolute ("C:\\home")); + assert (!p11_path_absolute ("home")); + assert (p11_path_absolute ("/home")); #endif } int -main (void) +main (int argc, + char *argv[]) { - CuString *output = CuStringNew (); - CuSuite* suite = CuSuiteNew (); - int ret; - - SUITE_ADD_TEST (suite, test_base); - SUITE_ADD_TEST (suite, test_build); - SUITE_ADD_TEST (suite, test_expand); - SUITE_ADD_TEST (suite, test_absolute); - - CuSuiteRun (suite); - CuSuiteSummary (suite, output); - CuSuiteDetails (suite, output); - printf ("%s\n", output->buffer); - ret = suite->failCount; - CuSuiteDelete (suite); - CuStringDelete (output); - - return ret; + p11_test (test_base, "/path/base"); + p11_test (test_build, "/path/build"); + p11_test (test_expand, "/path/expand"); + p11_test (test_absolute, "/path/absolute"); + + return p11_test_run (argc, argv); } diff --git a/common/tests/test-pem.c b/common/tests/test-pem.c index 54a59d6..7dd7fb2 100644 --- a/common/tests/test-pem.c +++ b/common/tests/test-pem.c @@ -33,7 +33,7 @@ */ #include "config.h" -#include "CuTest.h" +#include "test.h" #include #include @@ -125,7 +125,6 @@ struct { }; typedef struct { - CuTest *cu; int input_index; int output_index; int parsed; @@ -139,8 +138,8 @@ on_parse_pem_success (const char *type, { Closure *cl = user_data; - CuAssertIntEquals (cl->cu, success_fixtures[cl->input_index].output[cl->output_index].length, length); - CuAssertTrue (cl->cu, memcmp (success_fixtures[cl->input_index].output[cl->output_index].data, contents, + assert_num_eq (success_fixtures[cl->input_index].output[cl->output_index].length, length); + assert (memcmp (success_fixtures[cl->input_index].output[cl->output_index].data, contents, success_fixtures[cl->input_index].output[cl->output_index].length) == 0); cl->output_index++; @@ -148,7 +147,7 @@ on_parse_pem_success (const char *type, } static void -test_pem_success (CuTest *cu) +test_pem_success (void) { Closure cl; int ret; @@ -156,7 +155,6 @@ test_pem_success (CuTest *cu) int j; for (i = 0; success_fixtures[i].input != NULL; i++) { - cl.cu = cu; cl.input_index = i; cl.output_index = 0; cl.parsed = 0; @@ -164,12 +162,12 @@ test_pem_success (CuTest *cu) ret = p11_pem_parse (success_fixtures[i].input, strlen (success_fixtures[i].input), on_parse_pem_success, &cl); - CuAssertTrue (cu, success_fixtures[i].output[cl.output_index].type == NULL); + assert (success_fixtures[i].output[cl.output_index].type == NULL); /* Count number of outputs, return from p11_pem_parse() should match */ for (j = 0; success_fixtures[i].output[j].type != NULL; j++); - CuAssertIntEquals (cu, j, ret); - CuAssertIntEquals (cu, ret, cl.parsed); + assert_num_eq (j, ret); + assert_num_eq (ret, cl.parsed); } } @@ -215,20 +213,19 @@ on_parse_pem_failure (const char *type, size_t length, void *user_data) { - CuTest *cu = user_data; - CuAssertTrue (cu, false && "not reached"); + assert (false && "not reached"); } static void -test_pem_failure (CuTest *cu) +test_pem_failure (void) { int ret; int i; for (i = 0; failure_fixtures[i] != NULL; i++) { ret = p11_pem_parse (failure_fixtures[i], strlen (failure_fixtures[i]), - on_parse_pem_failure, cu); - CuAssertIntEquals (cu, 0, ret); + on_parse_pem_failure, NULL); + assert_num_eq (0, ret); } } @@ -239,11 +236,6 @@ typedef struct { const char *output; } WriteFixture; -typedef struct { - CuTest *cu; - WriteFixture *fixture; -} WriteClosure; - static WriteFixture write_fixtures[] = { { "\x69\x83\x4d\x5e\xab\x21\x95\x5c\x42\x76\x8f\x10\x7c\xa7\x97\x87" @@ -303,18 +295,17 @@ on_parse_written (const char *type, size_t length, void *user_data) { - WriteClosure *cl = user_data; + WriteFixture *fixture = user_data; - CuAssertStrEquals (cl->cu, cl->fixture->type, type); - CuAssertIntEquals (cl->cu, cl->fixture->length, length); - CuAssertTrue (cl->cu, memcmp (contents, cl->fixture->input, length) == 0); + assert_str_eq (fixture->type, type); + assert_num_eq (fixture->length, length); + assert (memcmp (contents, fixture->input, length) == 0); } static void -test_pem_write (CuTest *cu) +test_pem_write (void) { WriteFixture *fixture; - WriteClosure cl; size_t length; char *output; unsigned int count; @@ -326,37 +317,22 @@ test_pem_write (CuTest *cu) output = p11_pem_write ((unsigned char *)fixture->input, fixture->length, fixture->type, &length); - CuAssertStrEquals (cu, fixture->output, output); - CuAssertIntEquals (cu, strlen (fixture->output), length); - - cl.fixture = fixture; - cl.cu = cu; + assert_str_eq (fixture->output, output); + assert_num_eq (strlen (fixture->output), length); - count = p11_pem_parse (output, length, on_parse_written, &cl); - CuAssertIntEquals (cu, 1, count); + count = p11_pem_parse (output, length, on_parse_written, fixture); + assert_num_eq (1, count); free (output); } } int -main (void) +main (int argc, + char *argv[]) { - CuString *output = CuStringNew (); - CuSuite* suite = CuSuiteNew (); - int ret; - - SUITE_ADD_TEST (suite, test_pem_success); - SUITE_ADD_TEST (suite, test_pem_failure); - SUITE_ADD_TEST (suite, test_pem_write); - - CuSuiteRun (suite); - CuSuiteSummary (suite, output); - CuSuiteDetails (suite, output); - printf ("%s\n", output->buffer); - ret = suite->failCount; - CuSuiteDelete (suite); - CuStringDelete (output); - - return ret; + p11_test (test_pem_success, "/pem/success"); + p11_test (test_pem_failure, "/pem/failure"); + p11_test (test_pem_write, "/pem/write"); + return p11_test_run (argc, argv); } diff --git a/common/tests/test-url.c b/common/tests/test-url.c index ed84f0c..4c62594 100644 --- a/common/tests/test-url.c +++ b/common/tests/test-url.c @@ -33,7 +33,7 @@ */ #include "config.h" -#include "CuTest.h" +#include "test.h" #include "debug.h" #include "message.h" @@ -46,9 +46,9 @@ #include "url.h" static void -check_decode_msg (CuTest *tc, - const char *file, +check_decode_msg (const char *file, int line, + const char *function, const char *input, ssize_t input_len, const char *expected, @@ -62,106 +62,97 @@ check_decode_msg (CuTest *tc, decoded = p11_url_decode (input, input + input_len, "", &length); if (expected == NULL) { - CuAssert_Line (tc, file, line, "decoding should have failed", decoded == NULL); + if (decoded != NULL) + p11_test_fail (file, line, function, "decoding should have failed"); } else { - CuAssert_Line (tc, file, line, "decoding failed", decoded != NULL); - CuAssertIntEquals_LineMsg (tc, file, line, "wrong length", expected_len, length); - CuAssert_Line (tc, file, line, "decoded wrong", memcmp (decoded, expected, length) == 0); + 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(tc, input, input_len, expected, expected_len) \ - check_decode_msg (tc, __FILE__, __LINE__, input, input_len, expected, expected_len) +#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(tc, input, input_len) \ - check_decode_msg (tc, __FILE__, __LINE__, input, input_len, NULL, 0) +#define check_decode_failure(input, input_len) \ + check_decode_msg (__FILE__, __LINE__, __FUNCTION__, input, input_len, NULL, 0) static void -test_decode_success (CuTest *tc) +test_decode_success (void) { - check_decode_success (tc, "%54%45%53%54%00", -1, "TEST", 5); - check_decode_success (tc, "%54%45%53%54%00", 6, "TE", 2); - check_decode_success (tc, "%54est%00", -1, "Test", 5); + 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 (CuTest *tc) +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); - CuAssertStrEquals (tc, "TEST", (char *)decoded); - CuAssertIntEquals (tc, 5, length); + assert_str_eq ("TEST", (char *)decoded); + assert_num_eq (5, length); free (decoded); } static void -test_decode_failure (CuTest *tc) +test_decode_failure (void) { /* Early termination */ - check_decode_failure (tc, "%54%45%53%5", -1); - check_decode_failure (tc, "%54%45%53%", -1); + check_decode_failure ("%54%45%53%5", -1); + check_decode_failure ("%54%45%53%", -1); /* Not hex characters */ - check_decode_failure (tc, "%54%XX%53%54%00", -1); + check_decode_failure ("%54%XX%53%54%00", -1); } static void -test_encode (CuTest *tc) +test_encode (void) { const unsigned char *input = (unsigned char *)"TEST"; char *encoded; size_t length; encoded = p11_url_encode (input, input + 5, "", &length); - CuAssertStrEquals (tc, "%54%45%53%54%00", (char *)encoded); - CuAssertIntEquals (tc, 15, length); + assert_str_eq ("%54%45%53%54%00", (char *)encoded); + assert_num_eq (15, length); free (encoded); } static void -test_encode_verbatim (CuTest *tc) +test_encode_verbatim (void) { const unsigned char *input = (unsigned char *)"TEST"; char *encoded; size_t length; encoded = p11_url_encode (input, input + 5, "ES", &length); - CuAssertStrEquals (tc, "%54ES%54%00", (char *)encoded); - CuAssertIntEquals (tc, 11, length); + assert_str_eq ("%54ES%54%00", (char *)encoded); + assert_num_eq (11, length); free (encoded); } int -main (void) +main (int argc, + char *argv[]) { - CuString *output = CuStringNew (); - CuSuite* suite = CuSuiteNew (); - int ret; - - putenv ("P11_KIT_STRICT=1"); - p11_debug_init (); - - SUITE_ADD_TEST (suite, test_decode_success); - SUITE_ADD_TEST (suite, test_decode_skip); - SUITE_ADD_TEST (suite, test_decode_failure); - - SUITE_ADD_TEST (suite, test_encode); - SUITE_ADD_TEST (suite, test_encode_verbatim); - - CuSuiteRun (suite); - CuSuiteSummary (suite, output); - CuSuiteDetails (suite, output); - printf ("%s\n", output->buffer); - ret = suite->failCount; - CuSuiteDelete (suite); - CuStringDelete (output); - return ret; + 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/test-utf8.c b/common/tests/test-utf8.c index ed13fa2..9b2c3d5 100644 --- a/common/tests/test-utf8.c +++ b/common/tests/test-utf8.c @@ -33,7 +33,7 @@ */ #include "config.h" -#include "CuTest.h" +#include "test.h" #include "utf8.h" @@ -43,7 +43,7 @@ #define ELEMS(x) (sizeof (x) / sizeof (x[0])) static void -test_ucs2be (CuTest *cu) +test_ucs2be (void) { char *output; size_t length; @@ -73,14 +73,14 @@ test_ucs2be (CuTest *cu) fixtures[i].input_len, &length); - CuAssertIntEquals (cu, fixtures[i].output_len, length); - CuAssertStrEquals (cu, fixtures[i].output, output); + assert_num_eq (fixtures[i].output_len, length); + assert_str_eq (fixtures[i].output, output); free (output); } } static void -test_ucs2be_fail (CuTest *cu) +test_ucs2be_fail (void) { char *output; size_t length; @@ -97,12 +97,12 @@ test_ucs2be_fail (CuTest *cu) output = p11_utf8_for_ucs2be (fixtures[i].input, fixtures[i].input_len, &length); - CuAssertPtrEquals (cu, NULL, output); + assert_ptr_eq (NULL, output); } } static void -test_ucs4be (CuTest *cu) +test_ucs4be (void) { char *output; size_t length; @@ -146,15 +146,15 @@ test_ucs4be (CuTest *cu) fixtures[i].input_len, &length); - CuAssertIntEquals (cu, fixtures[i].output_len, length); - CuAssertStrEquals (cu, fixtures[i].output, output); + assert_num_eq (fixtures[i].output_len, length); + assert_str_eq (fixtures[i].output, output); free (output); } } static void -test_ucs4be_fail (CuTest *cu) +test_ucs4be_fail (void) { char *output; size_t length; @@ -179,12 +179,12 @@ test_ucs4be_fail (CuTest *cu) output = p11_utf8_for_ucs4be (fixtures[i].input, fixtures[i].input_len, &length); - CuAssertPtrEquals (cu, NULL, output); + assert_ptr_eq (NULL, output); } } static void -test_utf8 (CuTest *cu) +test_utf8 (void) { bool ret; int i; @@ -203,12 +203,12 @@ test_utf8 (CuTest *cu) for (i = 0; i < ELEMS (fixtures); i++) { ret = p11_utf8_validate (fixtures[i].input, fixtures[i].input_len); - CuAssertIntEquals (cu, true, ret); + assert_num_eq (true, ret); } } static void -test_utf8_fail (CuTest *cu) +test_utf8_fail (void) { bool ret; int i; @@ -226,31 +226,19 @@ test_utf8_fail (CuTest *cu) for (i = 0; i < ELEMS (fixtures); i++) { ret = p11_utf8_validate (fixtures[i].input, fixtures[i].input_len); - CuAssertIntEquals (cu, false, ret); + assert_num_eq (false, ret); } } int -main (void) +main (int argc, + char *argv[]) { - CuString *output = CuStringNew (); - CuSuite* suite = CuSuiteNew (); - int ret; - - SUITE_ADD_TEST (suite, test_ucs2be); - SUITE_ADD_TEST (suite, test_ucs2be_fail); - SUITE_ADD_TEST (suite, test_ucs4be); - SUITE_ADD_TEST (suite, test_ucs4be_fail); - SUITE_ADD_TEST (suite, test_utf8); - SUITE_ADD_TEST (suite, test_utf8_fail); - - CuSuiteRun (suite); - CuSuiteSummary (suite, output); - CuSuiteDetails (suite, output); - printf ("%s\n", output->buffer); - ret = suite->failCount; - CuSuiteDelete (suite); - CuStringDelete (output); - - return ret; + p11_test (test_ucs2be, "/utf8/ucs2be"); + p11_test (test_ucs2be_fail, "/utf8/ucs2be_fail"); + p11_test (test_ucs4be, "/utf8/ucs4be"); + p11_test (test_ucs4be_fail, "/utf8/ucs4be_fail"); + p11_test (test_utf8, "/utf8/utf8"); + p11_test (test_utf8_fail, "/utf8/utf8_fail"); + return p11_test_run (argc, argv); } diff --git a/common/tests/test-x509.c b/common/tests/test-x509.c index 2596c9c..9f7d258 100644 --- a/common/tests/test-x509.c +++ b/common/tests/test-x509.c @@ -33,7 +33,7 @@ */ #include "config.h" -#include "CuTest.h" +#include "test.h" #include "asn1.h" #include "debug.h" @@ -51,14 +51,14 @@ struct { } test; static void -setup (CuTest *cu) +setup (void *unused) { test.asn1_defs = p11_asn1_defs_load (); - CuAssertPtrNotNull (cu, test.asn1_defs); + assert_ptr_not_null (test.asn1_defs); } static void -teardown (CuTest *cu) +teardown (void *unused) { p11_dict_free (test.asn1_defs); memset (&test, 0, sizeof (test)); @@ -226,29 +226,25 @@ struct { }; static void -test_parse_extended_key_usage (CuTest *cu) +test_parse_extended_key_usage (void) { p11_array *ekus; int i, j, count; - setup (cu); - for (i = 0; extended_key_usage_fixtures[i].eku != NULL; i++) { ekus = p11_x509_parse_extended_key_usage (test.asn1_defs, (const unsigned char *)extended_key_usage_fixtures[i].eku, extended_key_usage_fixtures[i].length); - CuAssertPtrNotNull (cu, ekus); + assert_ptr_not_null (ekus); for (count = 0; extended_key_usage_fixtures[i].expected[count] != NULL; count++); - CuAssertIntEquals (cu, count, ekus->num); + assert_num_eq (count, ekus->num); for (j = 0; j < count; j++) - CuAssertStrEquals (cu, ekus->elem[j], extended_key_usage_fixtures[i].expected[j]); + assert_str_eq (ekus->elem[j], extended_key_usage_fixtures[i].expected[j]); p11_array_free (ekus); } - - teardown (cu); } struct { @@ -263,82 +259,70 @@ struct { }; static void -test_parse_key_usage (CuTest *cu) +test_parse_key_usage (void) { unsigned int ku; int i; bool ret; - setup (cu); - for (i = 0; key_usage_fixtures[i].ku != NULL; i++) { ku = 0; ret = p11_x509_parse_key_usage (test.asn1_defs, (const unsigned char *)key_usage_fixtures[i].ku, key_usage_fixtures[i].length, &ku); - CuAssertIntEquals (cu, true, ret); + assert_num_eq (true, ret); - CuAssertIntEquals (cu, key_usage_fixtures[i].expected, ku); + assert_num_eq (key_usage_fixtures[i].expected, ku); } - - teardown (cu); } static void -test_parse_extension (CuTest *cu) +test_parse_extension (void) { node_asn *cert; unsigned char *ext; size_t length; bool is_ca; - setup (cu); - cert = p11_asn1_decode (test.asn1_defs, "PKIX1.Certificate", test_cacert3_ca_der, sizeof (test_cacert3_ca_der), NULL); - CuAssertPtrNotNull (cu, cert); + assert_ptr_not_null (cert); ext = p11_x509_find_extension (cert, P11_OID_BASIC_CONSTRAINTS, test_cacert3_ca_der, sizeof (test_cacert3_ca_der), &length); - CuAssertPtrNotNull (cu, ext); - CuAssertTrue (cu, length > 0); + assert_ptr_not_null (ext); + assert (length > 0); asn1_delete_structure (&cert); if (!p11_x509_parse_basic_constraints (test.asn1_defs, ext, length, &is_ca)) - CuFail (cu, "failed to parse message"); + assert_fail ("failed to parse message", "basic constraints"); free (ext); - - teardown (cu); } static void -test_parse_extension_not_found (CuTest *cu) +test_parse_extension_not_found (void) { node_asn *cert; unsigned char *ext; size_t length; - setup (cu); - cert = p11_asn1_decode (test.asn1_defs, "PKIX1.Certificate", test_cacert3_ca_der, sizeof (test_cacert3_ca_der), NULL); - CuAssertPtrNotNull (cu, cert); + assert_ptr_not_null (cert); ext = p11_x509_find_extension (cert, P11_OID_OPENSSL_REJECT, test_cacert3_ca_der, sizeof (test_cacert3_ca_der), &length); - CuAssertPtrEquals (cu, NULL, ext); + assert_ptr_eq (NULL, ext); asn1_delete_structure (&cert); - - teardown (cu); } static void -test_directory_string (CuTest *tc) +test_directory_string (void) { struct { unsigned char input[100]; @@ -392,17 +376,17 @@ test_directory_string (CuTest *tc) string = p11_x509_parse_directory_string (fixtures[i].input, fixtures[i].input_len, &unknown, &length); - CuAssertPtrNotNull (tc, string); - CuAssertIntEquals (tc, false, unknown); + assert_ptr_not_null (string); + assert_num_eq (false, unknown); - CuAssertIntEquals (tc, fixtures[i].output_len, length); - CuAssertStrEquals (tc, fixtures[i].output, string); + assert_num_eq (fixtures[i].output_len, length); + assert_str_eq (fixtures[i].output, string); free (string); } } static void -test_directory_string_unknown (CuTest *tc) +test_directory_string_unknown (void) { /* Not a valid choice in DirectoryString */ unsigned char input[] = { 0x05, 0x07, 'A', ' ', ' ', 'n', 'i', 'c', 'e' }; @@ -411,34 +395,22 @@ test_directory_string_unknown (CuTest *tc) size_t length; string = p11_x509_parse_directory_string (input, sizeof (input), &unknown, &length); - CuAssertPtrEquals (tc, NULL, string); - CuAssertIntEquals (tc, true, unknown); + assert_ptr_eq (NULL, string); + assert_num_eq (true, unknown); } int -main (void) +main (int argc, + char *argv[]) { - CuString *output = CuStringNew (); - CuSuite* suite = CuSuiteNew (); - int ret; - - putenv ("P11_KIT_STRICT=1"); - p11_debug_init (); - - SUITE_ADD_TEST (suite, test_parse_extended_key_usage); - SUITE_ADD_TEST (suite, test_parse_key_usage); - SUITE_ADD_TEST (suite, test_parse_extension); - SUITE_ADD_TEST (suite, test_parse_extension_not_found); - SUITE_ADD_TEST (suite, test_directory_string); - SUITE_ADD_TEST (suite, test_directory_string_unknown); - - CuSuiteRun (suite); - CuSuiteSummary (suite, output); - CuSuiteDetails (suite, output); - printf ("%s\n", output->buffer); - ret = suite->failCount; - CuSuiteDelete (suite); - CuStringDelete (output); - - return ret; + p11_fixture (setup, teardown); + p11_test (test_parse_extended_key_usage, "/x509/parse-extended-key-usage"); + p11_test (test_parse_key_usage, "/x509/parse-key-usage"); + p11_test (test_parse_extension, "/x509/parse-extension"); + p11_test (test_parse_extension_not_found, "/x509/parse-extension-not-found"); + + p11_fixture (NULL, NULL); + p11_test (test_directory_string, "/x509/directory-string"); + p11_test (test_directory_string_unknown, "/x509/directory-string-unknown"); + return p11_test_run (argc, argv); } -- cgit v1.1