]> granicus.if.org Git - p11-kit/commitdiff
iter: Add p11_kit_iter_set_uri() function
authorStef Walter <stef@thewalter.net>
Tue, 27 Aug 2013 19:14:35 +0000 (21:14 +0200)
committerStef Walter <stef@thewalter.net>
Wed, 28 Aug 2013 11:52:34 +0000 (13:52 +0200)
This is so we can set a filtering uri on the iterator after construction

doc/manual/p11-kit-sections.txt
p11-kit/iter.c
p11-kit/iter.h
p11-kit/tests/test-iter.c

index efd0cc6797907abaad57d2446b8e0310d0cd552f..e066fe1c9a8461a05acb09073f3d069f8ae62eb0 100644 (file)
@@ -99,9 +99,10 @@ p11_kit_be_loud
 p11_kit_destroyer
 P11KitIter
 p11_kit_iter_new
+p11_kit_iter_set_uri
 p11_kit_iter_add_callback
-p11_kit_iter_callback
 p11_kit_iter_add_filter
+p11_kit_iter_callback
 p11_kit_iter_begin
 p11_kit_iter_begin_with
 p11_kit_iter_next
index 61d0ee300ab239b8467ee4ffaf13f218aa38df49..e8e466c0af55aa8884a3518a4381064fb93d955c 100644 (file)
@@ -127,10 +127,6 @@ p11_kit_iter_new (P11KitUri *uri,
                   P11KitIterBehavior behavior)
 {
        P11KitIter *iter;
-       CK_ATTRIBUTE *attrs;
-       CK_TOKEN_INFO *tinfo;
-       CK_INFO *minfo;
-       CK_ULONG count;
 
        iter = calloc (1, sizeof (P11KitIter));
        return_val_if_fail (iter != NULL, NULL);
@@ -139,6 +135,39 @@ p11_kit_iter_new (P11KitUri *uri,
        return_val_if_fail (iter->modules != NULL, NULL);
 
        iter->want_writable = !!(behavior & P11_KIT_ITER_WANT_WRITABLE);
+       iter->preload_results = !(behavior & P11_KIT_ITER_BUSY_SESSIONS);
+
+       p11_kit_iter_set_uri (iter, uri);
+       return iter;
+}
+
+/**
+ * p11_kit_iter_set_uri:
+ * @iter: the iterator
+ * @uri: (allow-none): a PKCS\#11 URI to filter on, or %NULL
+ *
+ * Set the PKCS\#11 uri for iterator. Only
+ * objects that match the @uri will be returned by the iterator.
+ * Relevant information in @uri is copied, and you need not keep
+ * @uri around.
+ *
+ * If no @uri is specified then the iterator will iterate over all
+ * objects, unless otherwise filtered.
+ *
+ * This function should be called at most once, and should be
+ * called before iterating begins.
+ *
+ */
+void
+p11_kit_iter_set_uri (P11KitIter *iter,
+                      P11KitUri *uri)
+{
+       CK_ATTRIBUTE *attrs;
+       CK_TOKEN_INFO *tinfo;
+       CK_INFO *minfo;
+       CK_ULONG count;
+
+       return_if_fail (iter != NULL);
 
        if (uri != NULL) {
 
@@ -159,12 +188,10 @@ p11_kit_iter_new (P11KitUri *uri,
                }
        } else {
                /* Match any module version number*/
+               memset (&iter->match_module, 0, sizeof (iter->match_module));
                iter->match_module.libraryVersion.major = (CK_BYTE)-1;
                iter->match_module.libraryVersion.minor = (CK_BYTE)-1;
        }
-       iter->preload_results = !(behavior & P11_KIT_ITER_BUSY_SESSIONS);
-
-       return iter;
 }
 
 /**
index ecf7db0865a79f83efc8d7cad06dfd46d48fc987..2ded9fe372c40ca0097c129bb8ef2fdc0c4bf4b9 100644 (file)
@@ -70,6 +70,9 @@ void                  p11_kit_iter_add_filter               (P11KitIter *iter,
                                                              CK_ATTRIBUTE *matching,
                                                              CK_ULONG count);
 
+void                  p11_kit_iter_set_uri                  (P11KitIter *iter,
+                                                             P11KitUri *uri);
+
 void                  p11_kit_iter_begin                    (P11KitIter *iter,
                                                              CK_FUNCTION_LIST_PTR *modules);
 
index a1d1c9c0baaf86537f4c68338c08b2bb24e931e8..46b825e61c8bd029712067149b17f0b19a9f5e89 100644 (file)
@@ -518,6 +518,33 @@ test_uri_with_type (void)
        finalize_and_free_modules (modules);
 }
 
+static void
+test_set_uri (void)
+{
+       CK_FUNCTION_LIST_PTR *modules;
+       P11KitIter *iter;
+       P11KitUri *uri;
+       CK_RV rv;
+
+       modules = initialize_and_get_modules ();
+
+       uri = p11_kit_uri_new ();
+       p11_kit_uri_set_unrecognized (uri, 1);
+       iter = p11_kit_iter_new (NULL, 0);
+       p11_kit_iter_set_uri (iter, uri);
+       p11_kit_uri_free (uri);
+
+       p11_kit_iter_begin (iter, modules);
+
+       /* Nothing should have matched */
+       rv = p11_kit_iter_next (iter);
+       assert_num_eq (rv, CKR_CANCEL);
+
+       p11_kit_iter_free (iter);
+
+       finalize_and_free_modules (modules);
+}
+
 static void
 test_filter (void)
 {
@@ -1165,6 +1192,7 @@ main (int argc,
        p11_test (test_all, "/iter/test_all");
        p11_test (test_unrecognized, "/iter/test_unrecognized");
        p11_test (test_uri_with_type, "/iter/test_uri_with_type");
+       p11_test (test_set_uri, "/iter/set-uri");
        p11_test (test_session_flags, "/iter/test_session_flags");
        p11_test (test_callback, "/iter/test_callback");
        p11_test (test_callback_fails, "/iter/test_callback_fails");