diff options
-rw-r--r-- | p11-kit/p11-kit-uri.c | 75 | ||||
-rw-r--r-- | p11-kit/p11-kit-uri.h | 12 | ||||
-rw-r--r-- | tests/uri-test.c | 41 |
3 files changed, 100 insertions, 28 deletions
diff --git a/p11-kit/p11-kit-uri.c b/p11-kit/p11-kit-uri.c index abd8b22..f2ac3ad 100644 --- a/p11-kit/p11-kit-uri.c +++ b/p11-kit/p11-kit-uri.c @@ -368,27 +368,6 @@ p11_kit_uri_match_token_info (P11KitUri *uri, CK_TOKEN_INFO_PTR token_info) } /** - * p11_kit_uri_get_attribute_types: - * @uri: The URI - * @n_attrs: A location to store the number of attributes returned. - * - * Get the attributes present in this URI. The attributes and values are - * owned by the URI. If the URI is modified, then the attributes that were - * returned from this function will not remain consistent. - * - * Returns: The attributes for this URI. These are owned by the URI. - */ -CK_ATTRIBUTE_PTR -p11_kit_uri_get_attributes (P11KitUri *uri, CK_ULONG_PTR n_attrs) -{ - assert (uri); - assert (n_attrs); - - *n_attrs = uri->n_attributes; - return uri->attributes; -} - -/** * p11_kit_uri_get_attribute: * @uri: The URI * @attr_type: The attribute type @@ -538,6 +517,60 @@ p11_kit_uri_clear_attribute (P11KitUri *uri, CK_ATTRIBUTE_TYPE attr_type) return P11_KIT_URI_OK; } +/** + * p11_kit_uri_get_attribute_types: + * @uri: The URI + * @n_attrs: A location to store the number of attributes returned. + * + * Get the attributes present in this URI. The attributes and values are + * owned by the URI. If the URI is modified, then the attributes that were + * returned from this function will not remain consistent. + * + * Returns: The attributes for this URI. These are owned by the URI. + */ +CK_ATTRIBUTE_PTR +p11_kit_uri_get_attributes (P11KitUri *uri, CK_ULONG_PTR n_attrs) +{ + assert (uri); + assert (n_attrs); + + *n_attrs = uri->n_attributes; + return uri->attributes; +} + +int +p11_kit_uri_set_attributes (P11KitUri *uri, CK_ATTRIBUTE_PTR attrs, + CK_ULONG n_attrs) +{ + CK_ULONG i; + int ret; + + assert (uri); + + p11_kit_uri_clear_attributes (uri); + + for (i = 0; i < n_attrs; i++) { + ret = p11_kit_uri_set_attribute (uri, &attrs[i]); + if (ret != P11_KIT_URI_OK && ret != P11_KIT_URI_NOT_FOUND) + return ret; + } + + return P11_KIT_URI_OK; +} + +void +p11_kit_uri_clear_attributes (P11KitUri *uri) +{ + CK_ULONG i; + + assert (uri); + + for (i = 0; i < uri->n_attributes; i++) + free (uri->attributes[i].pValue); + uri->n_attributes = 0; +} + + static int match_attributes (CK_ATTRIBUTE_PTR one, CK_ATTRIBUTE_PTR two) { diff --git a/p11-kit/p11-kit-uri.h b/p11-kit/p11-kit-uri.h index 698efec..a54f7a4 100644 --- a/p11-kit/p11-kit-uri.h +++ b/p11-kit/p11-kit-uri.h @@ -82,9 +82,6 @@ CK_TOKEN_INFO_PTR p11_kit_uri_get_token_info (P11KitUri *uri); int p11_kit_uri_match_token_info (P11KitUri *uri, CK_TOKEN_INFO_PTR token_info); -CK_ATTRIBUTE_PTR p11_kit_uri_get_attributes (P11KitUri *uri, - CK_ULONG *n_attrs); - CK_ATTRIBUTE_PTR p11_kit_uri_get_attribute (P11KitUri *uri, CK_ATTRIBUTE_TYPE attr_type); @@ -94,6 +91,15 @@ int p11_kit_uri_set_attribute (P11KitUri *uri, int p11_kit_uri_clear_attribute (P11KitUri *uri, CK_ATTRIBUTE_TYPE attr_type); +CK_ATTRIBUTE_PTR p11_kit_uri_get_attributes (P11KitUri *uri, + CK_ULONG *n_attrs); + +int p11_kit_uri_set_attributes (P11KitUri *uri, + CK_ATTRIBUTE_PTR attrs, + CK_ULONG n_attrs); + +void p11_kit_uri_clear_attributes (P11KitUri *uri); + int p11_kit_uri_match_attributes (P11KitUri *uri, CK_ATTRIBUTE_PTR attrs, CK_ULONG n_attrs); diff --git a/tests/uri-test.c b/tests/uri-test.c index c5a65e3..c3fd7f8 100644 --- a/tests/uri-test.c +++ b/tests/uri-test.c @@ -942,11 +942,8 @@ test_uri_match_attributes (CuTest *tc) static void test_uri_get_set_attribute (CuTest *tc) { - CK_ATTRIBUTE_PTR attrs; - CK_OBJECT_CLASS klass; CK_ATTRIBUTE attr; CK_ATTRIBUTE_PTR ptr; - CK_ULONG n_attrs; P11KitUri *uri; int ret; @@ -989,6 +986,22 @@ test_uri_get_set_attribute (CuTest *tc) ptr = p11_kit_uri_get_attribute (uri, CKA_LABEL); CuAssertPtrEquals (tc, NULL, ptr); + p11_kit_uri_free (uri); +} + +static void +test_uri_get_set_attributes (CuTest *tc) +{ + CK_ATTRIBUTE_PTR attrs; + CK_OBJECT_CLASS klass; + CK_ATTRIBUTE attr; + CK_ULONG n_attrs; + P11KitUri *uri; + int ret; + + uri = p11_kit_uri_new (); + CuAssertPtrNotNull (tc, uri); + attrs = p11_kit_uri_get_attributes (uri, &n_attrs); CuAssertPtrNotNull (tc, attrs); CuAssertIntEquals (tc, 0, n_attrs); @@ -1049,9 +1062,28 @@ test_uri_get_set_attribute (CuTest *tc) CuAssertTrue (tc, attrs[0].ulValueLen == sizeof (klass)); CuAssertTrue (tc, memcmp (attrs[0].pValue, &klass, sizeof (klass)) == 0); + attr.type = CKA_LABEL; + attr.pValue = "Three"; + attr.ulValueLen = 5; + + ret = p11_kit_uri_set_attributes (uri, &attr, 1); + CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + + attrs = p11_kit_uri_get_attributes (uri, &n_attrs); + CuAssertPtrNotNull (tc, attrs); + CuAssertIntEquals (tc, 1, n_attrs); + CuAssertTrue (tc, attrs[0].type == CKA_LABEL); + CuAssertTrue (tc, attrs[0].ulValueLen == 5); + CuAssertTrue (tc, memcmp (attrs[0].pValue, "Three", 5) == 0); + + p11_kit_uri_clear_attributes (uri); + + attrs = p11_kit_uri_get_attributes (uri, &n_attrs); + CuAssertPtrNotNull (tc, attrs); + CuAssertIntEquals (tc, 0, n_attrs); + p11_kit_uri_free (uri); } - static void test_uri_pinfile (CuTest *tc) { @@ -1137,6 +1169,7 @@ main (void) SUITE_ADD_TEST (suite, test_uri_match_module); SUITE_ADD_TEST (suite, test_uri_match_attributes); SUITE_ADD_TEST (suite, test_uri_get_set_attribute); + SUITE_ADD_TEST (suite, test_uri_get_set_attributes); SUITE_ADD_TEST (suite, test_uri_pinfile); SUITE_ADD_TEST (suite, test_uri_free_null); SUITE_ADD_TEST (suite, test_uri_message); |