diff options
-rw-r--r-- | trust/tests/test-data.c | 154 | ||||
-rw-r--r-- | trust/tests/test-data.h | 32 | ||||
-rw-r--r-- | trust/tests/test-module.c | 13 | ||||
-rw-r--r-- | trust/tests/test-parser.c | 11 |
4 files changed, 124 insertions, 86 deletions
diff --git a/trust/tests/test-data.c b/trust/tests/test-data.c index 1decf2e..f95b89a 100644 --- a/trust/tests/test-data.c +++ b/trust/tests/test-data.c @@ -35,94 +35,100 @@ #include "config.h" #include "CuTest.h" +#include "attrs.h" +#include "test-data.h" + +#include <assert.h> #include <stdlib.h> #include <stdio.h> #include <string.h> -#include "attrs.h" -#include "test-data.h" - void -test_check_object (CuTest *cu, - CK_ATTRIBUTE *attrs, - CK_OBJECT_CLASS klass, - const char *label) +test_check_object_msg (CuTest *cu, + const char *file, + int line, + CK_ATTRIBUTE *attrs, + CK_OBJECT_CLASS klass, + const char *label) { - CK_BBOOL val; - CK_ULONG ulong; - CK_ATTRIBUTE *attr; - - if (!p11_attrs_find_bool (attrs, CKA_TOKEN, &val)) - CuFail (cu, "missing CKA_TOKEN"); - CuAssertIntEquals (cu, CK_TRUE, val); - - if (!p11_attrs_find_bool (attrs, CKA_PRIVATE, &val)) - CuFail (cu, "missing CKA_PRIVATE"); - CuAssertIntEquals (cu, CK_FALSE, val); - - if (!p11_attrs_find_bool (attrs, CKA_MODIFIABLE, &val)) - CuFail (cu, "missing CKA_MODIFIABLE"); - CuAssertIntEquals (cu, CK_FALSE, val); - - if (!p11_attrs_find_ulong (attrs, CKA_CLASS, &ulong)) - CuFail (cu, "missing CKA_CLASS"); - CuAssertIntEquals (cu, klass, ulong); + CK_BBOOL vtrue = CK_TRUE; + CK_BBOOL vfalse = CK_FALSE; + + CK_ATTRIBUTE expected[] = { + { CKA_TOKEN, &vtrue, sizeof (vtrue) }, + { CKA_PRIVATE, &vfalse, sizeof (vfalse) }, + { CKA_MODIFIABLE, &vfalse, sizeof (vfalse) }, + { CKA_CLASS, &klass, sizeof (klass) }, + { label ? CKA_LABEL : CKA_INVALID, (void *)label, label ? strlen (label) : 0 }, + { CKA_INVALID }, + }; + + test_check_attrs_msg (cu, file, line, expected, attrs); +} - if (label) { - attr = p11_attrs_find_valid (attrs, CKA_LABEL); - CuAssertPtrNotNull (cu, attr); - CuAssertTrue (cu, p11_attr_match_value (attr, label, -1)); - } +void +test_check_cacert3_ca_msg (CuTest *cu, + const char *file, + int line, + CK_ATTRIBUTE_PTR attrs, + const char *label) +{ + CK_CERTIFICATE_TYPE x509 = CKC_X_509; + CK_ULONG category = 0; /* TODO: Implement */ + + CK_ATTRIBUTE expected[] = { + { CKA_CERTIFICATE_TYPE, &x509, sizeof (x509) }, + { CKA_CERTIFICATE_CATEGORY, &category, sizeof (category) }, + { CKA_VALUE, (void *)test_cacert3_ca_der, sizeof (test_cacert3_ca_der) }, + { CKA_CHECK_VALUE, "\xad\x7c\x3f", 3 }, + { CKA_START_DATE, "20110523", 8 }, + { CKA_END_DATE, "20210520", 8, }, + { CKA_SUBJECT, (void *)test_cacert3_ca_subject, sizeof (test_cacert3_ca_subject) }, + { CKA_ISSUER, (void *)test_cacert3_ca_issuer, sizeof (test_cacert3_ca_issuer) }, + { CKA_SERIAL_NUMBER, (void *)test_cacert3_ca_serial, sizeof (test_cacert3_ca_serial) }, + { CKA_INVALID }, + }; + + test_check_object_msg (cu, file, line, attrs, CKO_CERTIFICATE, label); + test_check_attrs_msg (cu, file, line, expected, attrs); } void -test_check_cacert3_ca (CuTest *cu, - CK_ATTRIBUTE *attrs, - const char *label) +test_check_attrs_msg (CuTest *cu, + const char *file, + int line, + CK_ATTRIBUTE *expected, + CK_ATTRIBUTE *attrs) { CK_ATTRIBUTE *attr; - CK_ULONG ulong; - - test_check_object (cu, attrs, CKO_CERTIFICATE, label); - - if (!p11_attrs_find_ulong (attrs, CKA_CERTIFICATE_TYPE, &ulong)) - CuFail (cu, "missing CKA_CERTIFICATE_TYPE"); - CuAssertIntEquals (cu, CKC_X_509, ulong); - - /* TODO: Implement */ - if (!p11_attrs_find_ulong (attrs, CKA_CERTIFICATE_CATEGORY, &ulong)) - CuFail (cu, "missing CKA_CERTIFICATE_CATEGORY"); - CuAssertIntEquals (cu, 0, ulong); - - attr = p11_attrs_find (attrs, CKA_VALUE); - CuAssertPtrNotNull (cu, attr); - CuAssertTrue (cu, p11_attr_match_value (attr, test_cacert3_ca_der, - sizeof (test_cacert3_ca_der))); - - attr = p11_attrs_find_valid (attrs, CKA_CHECK_VALUE); - CuAssertPtrNotNull (cu, attr); - CuAssertTrue (cu, p11_attr_match_value (attr, "\xad\x7c\x3f", 3)); - - attr = p11_attrs_find (attrs, CKA_START_DATE); - CuAssertPtrNotNull (cu, attr); - CuAssertTrue (cu, p11_attr_match_value (attr, "20110523", -1)); - attr = p11_attrs_find_valid (attrs, CKA_END_DATE); - CuAssertPtrNotNull (cu, attr); - CuAssertTrue (cu, p11_attr_match_value (attr, "20210520", -1)); + while (!p11_attrs_is_empty (expected)) { + attr = p11_attrs_find (attrs, expected->type); + test_check_attr_msg (cu, file, line, expected, attr); + expected++; + } +} - attr = p11_attrs_find (attrs, CKA_SUBJECT); - CuAssertPtrNotNull (cu, attr); - CuAssertTrue (cu, p11_attr_match_value (attr, test_cacert3_ca_subject, - sizeof (test_cacert3_ca_subject))); +void +test_check_attr_msg (CuTest *cu, + const char *file, + int line, + CK_ATTRIBUTE *expected, + CK_ATTRIBUTE *attr) +{ + char *message; + assert (expected != NULL); - attr = p11_attrs_find (attrs, CKA_ISSUER); - CuAssertPtrNotNull (cu, attr); - CuAssertTrue (cu, p11_attr_match_value (attr, test_cacert3_ca_issuer, - sizeof (test_cacert3_ca_issuer))); + if (attr == NULL) { + asprintf (&message, "expected %s but found NULL", + p11_attr_to_string (expected)); + CuFail_Line (cu, file, line, "attribute does not match", message); + } - attr = p11_attrs_find (attrs, CKA_SERIAL_NUMBER); - CuAssertPtrNotNull (cu, attr); - CuAssertTrue (cu, p11_attr_match_value (attr, test_cacert3_ca_serial, - sizeof (test_cacert3_ca_serial))); + if (!p11_attr_equal (attr, expected)) { + asprintf (&message, "expected %s but found %s", + p11_attr_to_string (expected), + p11_attr_to_string (attr)); + CuFail_Line (cu, file, line, "attribute does not match", message); + } } diff --git a/trust/tests/test-data.h b/trust/tests/test-data.h index 9789493..e4ff938 100644 --- a/trust/tests/test-data.h +++ b/trust/tests/test-data.h @@ -37,15 +37,43 @@ #ifndef TEST_DATA_H_ #define TEST_DATA_H_ -void test_check_object (CuTest *cu, +#define test_check_object(cu, attrs, klass, label) \ + test_check_object_msg (cu, __FILE__, __LINE__, attrs, klass, label) + +void test_check_object_msg (CuTest *cu, + const char *file, + int line, CK_ATTRIBUTE *attrs, CK_OBJECT_CLASS klass, const char *label); -void test_check_cacert3_ca (CuTest *cu, +#define test_check_cacert3_ca(cu, attrs, label) \ + test_check_cacert3_ca_msg (cu, __FILE__, __LINE__, attrs, label) + +void test_check_cacert3_ca_msg (CuTest *cu, + const char *file, + int line, CK_ATTRIBUTE *attrs, const char *label); +#define test_check_attrs(cu, expected, attrs) \ + test_check_attrs_msg (cu, __FILE__, __LINE__, expected, attrs) + +void test_check_attrs_msg (CuTest *cu, + const char *file, + int line, + CK_ATTRIBUTE *expected, + CK_ATTRIBUTE *attrs); + +#define test_check_attr(cu, expected, attr) \ + test_check_attr_msg (cu, __FILE__, __LINE__, expected, attr) + +void test_check_attr_msg (CuTest *cu, + const char *file, + int line, + CK_ATTRIBUTE *expected, + CK_ATTRIBUTE *attr); + static const unsigned char test_cacert3_ca_der[] = { 0x30, 0x82, 0x07, 0x59, 0x30, 0x82, 0x05, 0x41, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x03, 0x0a, 0x41, 0x8a, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, diff --git a/trust/tests/test-module.c b/trust/tests/test-module.c index 64857a7..0278fd3 100644 --- a/trust/tests/test-module.c +++ b/trust/tests/test-module.c @@ -153,7 +153,7 @@ check_trust_object_equiv (CuTest *cu, rv = test.module->C_GetAttributeValue (test.session, trust, equiv, 6); CuAssertTrue (cu, rv == CKR_OK); - CuAssertTrue (cu, p11_attrs_match (cert, equiv)); + test_check_attrs (cu, equiv, cert); } static void @@ -220,7 +220,6 @@ check_certificate (CuTest *cu, unsigned char serial[128]; unsigned char id[128]; CK_CERTIFICATE_TYPE type; - CK_BBOOL val; CK_BYTE check[3]; CK_DATE start; CK_DATE end; @@ -256,12 +255,18 @@ check_certificate (CuTest *cu, /* If this is the cacert3 certificate, check its values */ if (memcmp (value, test_cacert3_ca_der, sizeof (test_cacert3_ca_der)) == 0) { CK_BBOOL trusted; + CK_BBOOL vtrue = CK_TRUE; CK_ATTRIBUTE anchor[] = { { CKA_TRUSTED, &trusted, sizeof (trusted) }, { CKA_INVALID, }, }; + CK_ATTRIBUTE check[] = { + { CKA_TRUSTED, &vtrue, sizeof (vtrue) }, + { CKA_INVALID, }, + }; + test_check_cacert3_ca (cu, attrs, NULL); /* Get anchor specific attributes */ @@ -269,9 +274,7 @@ check_certificate (CuTest *cu, CuAssertTrue (cu, rv == CKR_OK); /* It lives in the trusted directory */ - if (!p11_attrs_find_bool (anchor, CKA_TRUSTED, &val)) - CuFail (cu, "missing CKA_TRUSTED"); - CuAssertIntEquals (cu, CK_TRUE, val); + test_check_attrs (cu, check, anchor); /* Other certificates, we can't check the values */ } else { diff --git a/trust/tests/test-parser.c b/trust/tests/test-parser.c index 132d551..3ef979b 100644 --- a/trust/tests/test-parser.c +++ b/trust/tests/test-parser.c @@ -177,7 +177,7 @@ test_parse_openssl_trusted (CuTest *cu) CuAssertPtrEquals (cu, NULL, attr); attrs = test.objects->elem[1]; - CuAssertTrue (cu, p11_attrs_match (attrs, expected)); + test_check_attrs (cu, expected, attrs); teardown (cu); } @@ -200,7 +200,9 @@ static void test_parse_anchor (CuTest *cu) { CK_ATTRIBUTE *attrs; - CK_BBOOL val; + CK_ATTRIBUTE *attr; + CK_BBOOL vtrue = CK_TRUE; + CK_ATTRIBUTE trusted = { CKA_TRUSTED, &vtrue, sizeof (vtrue) }; int ret; setup (cu); @@ -215,9 +217,8 @@ test_parse_anchor (CuTest *cu) attrs = test.objects->elem[0]; test_check_cacert3_ca (cu, attrs, NULL); - if (!p11_attrs_find_bool (attrs, CKA_TRUSTED, &val)) - CuFail (cu, "missing CKA_TRUSTED"); - CuAssertIntEquals (cu, CK_TRUE, val); + attr = p11_attrs_find (attrs, CKA_TRUSTED); + test_check_attr (cu, &trusted, attr); teardown (cu); } |