1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* test-gck-modules.c - the GObject PKCS#11 wrapper library
3
4 Copyright (C) 2011 Collabora Ltd.
5
6 The Gnome Keyring Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
10
11 The Gnome Keyring Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public
17 License along with the Gnome Library; see the file COPYING.LIB. If not,
18 see <http://www.gnu.org/licenses/>.
19
20 Author: Stef Walter <stefw@collabora.co.uk>
21 */
22
23 #include "config.h"
24
25 #include "gck/gck.h"
26 #include "gck/gck-mock.h"
27 #include "gck/gck-private.h"
28 #include "gck/gck-test.h"
29
30 #include "egg/egg-testing.h"
31
32 #include <glib.h>
33
34 #include <errno.h>
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <string.h>
38
39 typedef struct {
40 GList *modules;
41 } Test;
42
43 static void
setup(Test * test,gconstpointer unused)44 setup (Test *test, gconstpointer unused)
45 {
46 GckModule *module;
47 GError *err = NULL;
48
49 /* Successful load */
50 module = gck_module_initialize (_GCK_TEST_MODULE_PATH, NULL, &err);
51 g_assert_no_error (err);
52 g_assert_true (GCK_IS_MODULE (module));
53
54 test->modules = g_list_append (NULL, module);
55 }
56
57 static void
teardown(Test * test,gconstpointer unused)58 teardown (Test *test, gconstpointer unused)
59 {
60 gck_list_unref_free (test->modules);
61 test->modules = NULL;
62 }
63
64 static void
test_enumerate_objects(Test * test,gconstpointer unused)65 test_enumerate_objects (Test *test, gconstpointer unused)
66 {
67 GckBuilder builder = GCK_BUILDER_INIT;
68 GError *error = NULL;
69 GckEnumerator *en;
70 GList *objects;
71
72 gck_builder_add_string (&builder, CKA_LABEL, "Private Capitalize Key");
73 en = gck_modules_enumerate_objects (test->modules, gck_builder_end (&builder), 0);
74 g_assert_true (GCK_IS_ENUMERATOR (en));
75
76 objects = gck_enumerator_next_n (en, -1, NULL, &error);
77 g_assert_no_error (error);
78 g_assert_cmpint (g_list_length (objects), ==, 1);
79 g_assert_true (GCK_IS_OBJECT (objects->data));
80
81 gck_list_unref_free (objects);
82 g_object_unref (en);
83 }
84
85
86 static void
test_token_for_uri(Test * test,gconstpointer unused)87 test_token_for_uri (Test *test, gconstpointer unused)
88 {
89 GckSlot *slot;
90 GError *error = NULL;
91
92 slot = gck_modules_token_for_uri (test->modules, "pkcs11:token=TEST%20LABEL", &error);
93 g_assert_true (GCK_IS_SLOT (slot));
94
95 g_object_unref (slot);
96 }
97
98 static void
test_token_for_uri_not_found(Test * test,gconstpointer unused)99 test_token_for_uri_not_found (Test *test, gconstpointer unused)
100 {
101 GckSlot *slot;
102 GError *error = NULL;
103
104 slot = gck_modules_token_for_uri (test->modules, "pkcs11:token=UNKNOWN", &error);
105 g_assert_null (slot);
106 g_assert_no_error (error);
107 }
108
109 static void
test_token_for_uri_error(Test * test,gconstpointer unused)110 test_token_for_uri_error (Test *test, gconstpointer unused)
111 {
112 GckSlot *slot;
113 GError *error = NULL;
114
115 slot = gck_modules_token_for_uri (test->modules, "http://invalid.uri", &error);
116 g_assert_null (slot);
117 g_assert_error (error, GCK_URI_ERROR, GCK_URI_BAD_PREFIX);
118 g_error_free (error);
119 }
120
121 static void
test_object_for_uri(Test * test,gconstpointer unused)122 test_object_for_uri (Test *test, gconstpointer unused)
123 {
124 GckObject *object;
125 GError *error = NULL;
126
127 object = gck_modules_object_for_uri (test->modules, "pkcs11:object=Public%20Capitalize%20Key;objecttype=public", 0, &error);
128 g_assert_true (GCK_IS_OBJECT (object));
129 g_object_unref (object);
130 }
131
132 static void
test_object_for_uri_not_found(Test * test,gconstpointer unused)133 test_object_for_uri_not_found (Test *test, gconstpointer unused)
134 {
135 GckObject *object;
136 GError *error = NULL;
137
138 object = gck_modules_object_for_uri (test->modules, "pkcs11:object=Unknown%20Label", 0, &error);
139 g_assert_null (object);
140 g_assert_null (error);
141 }
142
143 static void
test_object_for_uri_error(Test * test,gconstpointer unused)144 test_object_for_uri_error (Test *test, gconstpointer unused)
145 {
146 GckObject *object;
147 GError *error = NULL;
148
149 object = gck_modules_object_for_uri (test->modules, "http://invalid.uri", 0, &error);
150 g_assert_null (object);
151 g_assert_error (error, GCK_URI_ERROR, GCK_URI_BAD_PREFIX);
152 g_error_free (error);
153 }
154
155 static void
test_objects_for_uri(Test * test,gconstpointer unused)156 test_objects_for_uri (Test *test, gconstpointer unused)
157 {
158 GList *objects;
159 GError *error = NULL;
160
161 objects = gck_modules_objects_for_uri (test->modules, "pkcs11:token=TEST%20LABEL", 0, &error);
162 g_assert_nonnull (objects);
163 g_assert_no_error (error);
164 g_assert_cmpint (g_list_length (objects), ==, 5);
165
166 gck_list_unref_free (objects);
167 }
168
169 static void
test_enumerate_uri(Test * test,gconstpointer unused)170 test_enumerate_uri (Test *test, gconstpointer unused)
171 {
172 GckEnumerator *en;
173 GList *objects;
174 GError *error = NULL;
175
176 en = gck_modules_enumerate_uri (test->modules, "pkcs11:token=TEST%20LABEL", 0, &error);
177 g_assert_true (GCK_IS_ENUMERATOR (en));
178 g_assert_no_error (error);
179
180 objects = gck_enumerator_next_n (en, -1, NULL, &error);
181 g_assert_cmpint (g_list_length (objects), ==, 5);
182 g_assert_no_error (error);
183
184 g_object_unref (en);
185 gck_list_unref_free (objects);
186 }
187
188 int
main(int argc,char ** argv)189 main (int argc, char **argv)
190 {
191 g_test_init (&argc, &argv, NULL);
192
193 g_test_add ("/gck/modules/enumerate_objects", Test, NULL, setup, test_enumerate_objects, teardown);
194 g_test_add ("/gck/modules/token_for_uri", Test, NULL, setup, test_token_for_uri, teardown);
195 g_test_add ("/gck/modules/token_for_uri_not_found", Test, NULL, setup, test_token_for_uri_not_found, teardown);
196 g_test_add ("/gck/modules/token_for_uri_error", Test, NULL, setup, test_token_for_uri_error, teardown);
197 g_test_add ("/gck/modules/object_for_uri", Test, NULL, setup, test_object_for_uri, teardown);
198 g_test_add ("/gck/modules/object_for_uri_not_found", Test, NULL, setup, test_object_for_uri_not_found, teardown);
199 g_test_add ("/gck/modules/object_for_uri_error", Test, NULL, setup, test_object_for_uri_error, teardown);
200 g_test_add ("/gck/modules/objects_for_uri", Test, NULL, setup, test_objects_for_uri, teardown);
201 g_test_add ("/gck/modules/enumerate_uri", Test, NULL, setup, test_enumerate_uri, teardown);
202
203 return egg_tests_run_with_loop ();
204 }
205