Debian Codegraph @

pkcs11/secret-store/mock-secret-module.c

raw
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* test-secret-module.c: A test PKCS#11 module implementation
3
4 Copyright (C) 2009 Stefan Walter
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 <http://www.gnu.org/licenses/>.
19
20 Author: Stef Walter <[email protected]>
21 */
22
23 #include "config.h"
24
25 #include "mock-secret-module.h"
26
27 #include "gkm/gkm-secret.h"
28 #include "gkm/gkm-module.h"
29
30 #include "secret-store/gkm-secret-collection.h"
31 #include "secret-store/gkm-secret-data.h"
32 #include "secret-store/gkm-secret-fields.h"
33 #include "secret-store/gkm-secret-item.h"
34 #include "secret-store/gkm-secret-object.h"
35 #include "secret-store/gkm-secret-store.h"
36
37 #include "egg/egg-secure-memory.h"
38 #include "egg/egg-testing.h"
39
40 #include <glib.h>
41
42 #include <string.h>
43
44 EGG_SECURE_DEFINE_GLIB_GLOBALS ();
45
46 static GMutex *mutex = NULL;
47 static gchar *directory = NULL;
48
49 GkmModule* _gkm_secret_store_get_module_for_testing (void);
50 GMutex* _gkm_module_get_scary_mutex_that_you_should_not_touch (GkmModule *module);
51
52 GkmModule*
53 test_secret_module_initialize_and_enter (void)
54 {
55 CK_FUNCTION_LIST_PTR funcs;
56 CK_C_INITIALIZE_ARGS args;
57 GkmModule *module;
58 gchar *string;
59 CK_RV rv;
60
61 directory = egg_tests_create_scratch_directory (
62 SRCDIR "/pkcs11/secret-store/fixtures/encrypted.keyring",
63 SRCDIR "/pkcs11/secret-store/fixtures/plain.keyring",
64 NULL);
65
66 /* Setup test directory to work in */
67 memset (&args, 0, sizeof (args));
68 string = g_strdup_printf ("directory='%s'", directory);
69 args.pReserved = string;
70 args.flags = CKF_OS_LOCKING_OK;
71
72 funcs = gkm_secret_store_get_functions ();
73 rv = (funcs->C_Initialize) (&args);
74 g_return_val_if_fail (rv == CKR_OK, NULL);
75
76 module = _gkm_secret_store_get_module_for_testing ();
77 g_return_val_if_fail (module, NULL);
78
79 mutex = _gkm_module_get_scary_mutex_that_you_should_not_touch (module);
80 test_secret_module_enter ();
81
82 g_free (string);
83
84 return module;
85 }
86
87 void
88 test_secret_module_leave_and_finalize (void)
89 {
90 CK_FUNCTION_LIST_PTR funcs;
91 CK_RV rv;
92
93 test_secret_module_leave ();
94
95 funcs = gkm_secret_store_get_functions ();
96 rv = (funcs->C_Finalize) (NULL);
97 g_return_if_fail (rv == CKR_OK);
98
99 egg_tests_remove_scratch_directory (directory);
100 g_free (directory);
101 directory = NULL;
102 }
103
104 void
105 test_secret_module_leave (void)
106 {
107 g_assert (mutex);
108 g_mutex_unlock (mutex);
109 }
110
111 void
112 test_secret_module_enter (void)
113 {
114 g_assert (mutex);
115 g_mutex_lock (mutex);
116 }
117
118 GkmSession*
119 test_secret_module_open_session (gboolean writable)
120 {
121 CK_ULONG flags = CKF_SERIAL_SESSION;
122 CK_SESSION_HANDLE handle;
123 GkmModule *module;
124 GkmSession *session;
125 CK_RV rv;
126
127 module = _gkm_secret_store_get_module_for_testing ();
128 g_return_val_if_fail (module, NULL);
129
130 if (writable)
131 flags |= CKF_RW_SESSION;
132
133 rv = gkm_module_C_OpenSession (module, 1, flags, NULL, NULL, &handle);
134 g_assert (rv == CKR_OK);
135
136 rv = gkm_module_C_Login (module, handle, CKU_USER, NULL, 0);
137 g_assert (rv == CKR_OK);
138
139 session = gkm_module_lookup_session (module, handle);
140 g_assert (session);
141
142 return session;
143 }
144
145 /* Validates plain.keyring and encrypted.keyring in test-data */
146 void
147 test_secret_collection_validate (GkmSecretCollection *collection, GkmSecretData *sdata)
148 {
149 GkmSecretItem* item;
150 GkmSecretObject *obj;
151 GHashTable *fields;
152 const gchar *value;
153 GkmSecret *secret;
154 GList *items;
155 glong when;
156 guint32 num;
157
158 obj = GKM_SECRET_OBJECT (collection);
159
160 /* The keyring itself */
161 /* "Missing keyring name" */
162 value = gkm_secret_object_get_label (obj);
163 g_assert (value != NULL);
164 /* "Invalid keyring name" */
165 g_assert_cmpstr (value, ==, "unit-test-keyring");
166 #if 0
167 /* "Bad lock settings" */
168 g_assert (!keyring->lock_on_idle && keyring->lock_timeout == 0);
169 #endif
170 /* "Bad Creation Time" */
171 when = gkm_secret_object_get_created (obj);
172 g_assert (when == 1198027852);
173 /* "Bad Modification Time" */
174 when = gkm_secret_object_get_modified (obj);
175 g_assert (when == 1198027852);
176 /* "Wrong number of items" */
177 items = gkm_secret_collection_get_items (collection);
178 g_assert_cmpint (g_list_length (items), ==, 2);
179 g_list_free (items);
180
181 /* Item #2 */
182 item = gkm_secret_collection_get_item (collection, "2");
183 obj = GKM_SECRET_OBJECT (item);
184 /* "Couldn't find item" */
185 g_assert (item != NULL);
186 #if 0
187 /* "Invalid item type" */
188 g_assert_cmpint (item->type, ==, GNOME_KEYRING_ITEM_GENERIC_SECRET);
189 #endif
190 /* "Missing secret" */
191 secret = gkm_secret_data_get_secret (sdata, "2");
192 g_assert (secret != NULL);
193 /* "Wrong secret" */
194 g_assert (gkm_secret_equals (secret, (guchar*)"item-secret", -1));
195 /* "Bad Creation Time" */
196 when = gkm_secret_object_get_created (obj);
197 g_assert_cmpint (when, ==, 1198027852);
198
199 #if 0
200 /* Item #2 ACL */
201 /* "Bad ACLs" */
202 g_assert_cmpint (g_list_length (item->acl), ==, 1);
203 ac = (GnomeKeyringAccessControl*)item->acl->data;
204 /* "Invalid ACL" */
205 g_assert (ac && ac->application);
206 /* "Invalid ACL Path" */
207 g_assert (ac->application->pathname && strstr (ac->application->pathname, "test-suite"));
208 /* "Invalid ACL Display Name" */
209 g_assert (ac->application->display_name);
210 g_assert_cmpstr (ac->application->display_name, ==, "test-suite");
211 /* "Invalid ACL Access Type" */
212 g_assert_cmpint (ac->types_allowed, ==, (GNOME_KEYRING_ACCESS_READ | GNOME_KEYRING_ACCESS_WRITE | GNOME_KEYRING_ACCESS_REMOVE));
213 #endif
214
215 /* Item #3 */
216 item = gkm_secret_collection_get_item (collection, "3");
217 obj = GKM_SECRET_OBJECT (item);
218 /* "Couldn't find item #3" */
219 g_assert (item != NULL);
220 fields = gkm_secret_item_get_fields (item);
221 g_assert (fields != NULL);
222 /* Make fields are the same */
223 value = gkm_secret_fields_get (fields, "dog");
224 g_assert_cmpstr (value, ==, "woof");
225 value = gkm_secret_fields_get (fields, "bird");
226 g_assert_cmpstr (value, ==, "cheep");
227 value = gkm_secret_fields_get (fields, "iguana");
228 g_assert_cmpstr (value, ==, "");
229 g_assert (gkm_secret_fields_get_compat_uint32 (fields, "num", &num));
230 g_assert_cmpuint (num, ==, 3);
231 #if 0
232 /* "Invalid item type" */
233 g_assert_cmpint (item->type, ==, GNOME_KEYRING_ITEM_GENERIC_SECRET);
234 #endif
235 /* "Missing secret" */
236 secret = gkm_secret_data_get_secret (sdata, "3");
237 g_assert (secret != NULL);
238 /* "Wrong secret" */
239 g_assert (gkm_secret_equals (secret, (guchar*)"item-secret", -1));
240 }
241
242 /* Fills a collection with some junk data */
243 void
244 test_secret_collection_populate (GkmSecretCollection *collection, GkmSecretData *sdata)
245 {
246 GkmSecretItem *item;
247 GHashTable *fields;
248 GkmSecret *secret;
249
250 item = gkm_secret_collection_new_item (collection, "4");
251 gkm_secret_object_set_label (GKM_SECRET_OBJECT (item), "Noises");
252 secret = gkm_secret_new_from_password ("4's secret");
253 gkm_secret_data_set_secret (sdata, "4", secret);
254 g_object_unref (secret);
255 fields = gkm_secret_item_get_fields (item);
256 gkm_secret_fields_add (fields, "doggy", "fart");
257 gkm_secret_fields_add (fields, "pig", "grunt");
258 gkm_secret_fields_add_compat_uint32 (fields, "how-many", 292929);
259
260 item = gkm_secret_collection_new_item (collection, "5");
261 gkm_secret_object_set_label (GKM_SECRET_OBJECT (item), "Colors");
262 secret = gkm_secret_new_from_password ("5's secret");
263 gkm_secret_data_set_secret (sdata, "5", secret);
264 g_object_unref (secret);
265 fields = gkm_secret_item_get_fields (item);
266 gkm_secret_fields_add (fields, "barney", "purple");
267 gkm_secret_fields_add (fields, "piglet", "pink");
268 gkm_secret_fields_add_compat_uint32 (fields, "number", 8);
269
270 item = gkm_secret_collection_new_item (collection, "6");
271 gkm_secret_object_set_label (GKM_SECRET_OBJECT (item), "Binary Secret");
272 secret = gkm_secret_new ((guchar*)"binary\0secret", 13);
273 gkm_secret_data_set_secret (sdata, "6", secret);
274 g_object_unref (secret);
275 fields = gkm_secret_item_get_fields (item);
276 gkm_secret_fields_add (fields, "train", "zoom");
277 gkm_secret_fields_add (fields, "hummer", NULL);
278 gkm_secret_fields_add_compat_uint32 (fields, "number", 2);
279 }