]> granicus.if.org Git - p11-kit/commitdiff
Add functions for clearing and setting multiple attributes on URI.
authorStef Walter <stefw@collabora.co.uk>
Thu, 26 May 2011 14:13:49 +0000 (16:13 +0200)
committerStef Walter <stefw@collabora.co.uk>
Thu, 26 May 2011 14:13:49 +0000 (16:13 +0200)
p11-kit/p11-kit-uri.c
p11-kit/p11-kit-uri.h
tests/uri-test.c

index abd8b22a561061eda22ab5edcf81b53826cadc70..f2ac3ad3b88adec81a89c05f4cd95612ff24a1fe 100644 (file)
@@ -367,27 +367,6 @@ p11_kit_uri_match_token_info (P11KitUri *uri, CK_TOKEN_INFO_PTR token_info)
                                     sizeof (token_info->serialNumber)));
 }
 
-/**
- * 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
@@ -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)
 {
index 698efec692b14ce90353f19dcadba75a85613d74..a54f7a45a16e57a05f1ade4ce00c9208c7dcaa50 100644 (file)
@@ -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);
index c5a65e3f7c99183d65095bd3aa3cc287c6b45bfc..c3fd7f8249b2ab0d7e27f33883d36a1c17958271 100644 (file)
@@ -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);