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
4
Copyright (C) 2009 Stefan Walter
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.
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.
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
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19
Boston, MA 02111-1307, USA.
21
Author: Stef Walter <stef@memberwebs.com>
25
#include "test-secret-module.h"
26
#include "run-auto-test.h"
28
#include "gck/gck-secret.h"
29
#include "gck/gck-module.h"
31
#include "gck-secret-collection.h"
32
#include "gck-secret-data.h"
33
#include "gck-secret-fields.h"
34
#include "gck-secret-item.h"
35
#include "gck-secret-object.h"
36
#include "gck-secret-store.h"
40
static GMutex *mutex = NULL;
42
GckModule* _gck_secret_store_get_module_for_testing (void);
43
GMutex* _gck_module_get_scary_mutex_that_you_should_not_touch (GckModule *module);
46
copy_scratch_file (const gchar *basename)
52
filename = test_data_filename (basename);
53
if (!g_file_get_contents (filename, &data, &n_data, NULL)) {
54
g_warning ("couldn't read: %s", filename);
55
g_return_if_reached ();
59
filename = test_scratch_filename (basename);
60
if (!g_file_set_contents (filename, data, n_data, NULL))
61
g_return_if_reached ();
67
test_secret_module_initialize_and_enter (void)
69
CK_FUNCTION_LIST_PTR funcs;
70
CK_C_INITIALIZE_ARGS args;
75
/* Setup test directory to work in */
76
memset (&args, 0, sizeof (args));
77
string = g_strdup_printf ("directory='%s'", test_scratch_directory ());
78
args.pReserved = string;
79
args.flags = CKF_OS_LOCKING_OK;
81
/* Copy files from test-data to scratch */
82
copy_scratch_file ("encrypted.keyring");
83
copy_scratch_file ("plain.keyring");
85
funcs = gck_secret_store_get_functions ();
86
rv = (funcs->C_Initialize) (&args);
87
g_return_val_if_fail (rv == CKR_OK, NULL);
89
module = _gck_secret_store_get_module_for_testing ();
90
g_return_val_if_fail (module, NULL);
92
mutex = _gck_module_get_scary_mutex_that_you_should_not_touch (module);
93
test_secret_module_enter ();
101
test_secret_module_leave_and_finalize (void)
103
CK_FUNCTION_LIST_PTR funcs;
106
test_secret_module_leave ();
108
funcs = gck_secret_store_get_functions ();
109
rv = (funcs->C_Finalize) (NULL);
110
g_return_if_fail (rv == CKR_OK);
114
test_secret_module_leave (void)
117
g_mutex_unlock (mutex);
121
test_secret_module_enter (void)
124
g_mutex_lock (mutex);
128
test_secret_module_open_session (gboolean writable)
130
CK_ULONG flags = CKF_SERIAL_SESSION;
131
CK_SESSION_HANDLE handle;
136
module = _gck_secret_store_get_module_for_testing ();
137
g_return_val_if_fail (module, NULL);
140
flags |= CKF_RW_SESSION;
142
rv = gck_module_C_OpenSession (module, 1, flags, NULL, NULL, &handle);
143
g_assert (rv == CKR_OK);
145
rv = gck_module_C_Login (module, handle, CKU_USER, NULL, 0);
146
g_assert (rv == CKR_OK);
148
session = gck_module_lookup_session (module, handle);
154
/* Validates plain.keyring and encrypted.keyring in test-data */
156
test_secret_collection_validate (GckSecretCollection *collection, GckSecretData *sdata)
159
GckSecretObject *obj;
167
obj = GCK_SECRET_OBJECT (collection);
169
/* The keyring itself */
170
/* "Missing keyring name" */
171
value = gck_secret_object_get_label (obj);
172
g_assert (value != NULL);
173
/* "Invalid keyring name" */
174
g_assert_cmpstr (value, ==, "unit-test-keyring");
176
/* "Bad lock settings" */
177
g_assert (!keyring->lock_on_idle && keyring->lock_timeout == 0);
179
/* "Bad Creation Time" */
180
when = gck_secret_object_get_created (obj);
181
g_assert (when == 1198027852);
182
/* "Bad Modification Time" */
183
when = gck_secret_object_get_modified (obj);
184
g_assert (when == 1198027852);
185
/* "Wrong number of items" */
186
items = gck_secret_collection_get_items (collection);
187
g_assert_cmpint (g_list_length (items), ==, 2);
191
item = gck_secret_collection_get_item (collection, "2");
192
obj = GCK_SECRET_OBJECT (item);
193
/* "Couldn't find item" */
194
g_assert (item != NULL);
196
/* "Invalid item type" */
197
g_assert_cmpint (item->type, ==, GNOME_KEYRING_ITEM_GENERIC_SECRET);
199
/* "Missing secret" */
200
secret = gck_secret_data_get_secret (sdata, "2");
201
g_assert (secret != NULL);
203
g_assert (gck_secret_equals (secret, (guchar*)"item-secret", -1));
204
/* "Bad Creation Time" */
205
when = gck_secret_object_get_created (obj);
206
g_assert_cmpint (when, ==, 1198027852);
211
g_assert_cmpint (g_list_length (item->acl), ==, 1);
212
ac = (GnomeKeyringAccessControl*)item->acl->data;
214
g_assert (ac && ac->application);
215
/* "Invalid ACL Path" */
216
g_assert (ac->application->pathname && strstr (ac->application->pathname, "run-auto-test"));
217
/* "Invalid ACL Display Name" */
218
g_assert (ac->application->display_name);
219
g_assert_cmpstr (ac->application->display_name, ==, "run-auto-test");
220
/* "Invalid ACL Access Type" */
221
g_assert_cmpint (ac->types_allowed, ==, (GNOME_KEYRING_ACCESS_READ | GNOME_KEYRING_ACCESS_WRITE | GNOME_KEYRING_ACCESS_REMOVE));
225
item = gck_secret_collection_get_item (collection, "3");
226
obj = GCK_SECRET_OBJECT (item);
227
/* "Couldn't find item #3" */
228
g_assert (item != NULL);
229
fields = gck_secret_item_get_fields (item);
230
g_assert (fields != NULL);
231
/* Make fields are the same */
232
value = gck_secret_fields_get (fields, "dog");
233
g_assert_cmpstr (value, ==, "woof");
234
value = gck_secret_fields_get (fields, "bird");
235
g_assert_cmpstr (value, ==, "cheep");
236
value = gck_secret_fields_get (fields, "iguana");
237
g_assert_cmpstr (value, ==, "");
238
g_assert (gck_secret_fields_get_compat_uint32 (fields, "num", &num));
239
g_assert_cmpuint (num, ==, 3);
241
/* "Invalid item type" */
242
g_assert_cmpint (item->type, ==, GNOME_KEYRING_ITEM_GENERIC_SECRET);
244
/* "Missing secret" */
245
secret = gck_secret_data_get_secret (sdata, "3");
246
g_assert (secret != NULL);
248
g_assert (gck_secret_equals (secret, (guchar*)"item-secret", -1));
251
/* Fills a collection with some junk data */
253
test_secret_collection_populate (GckSecretCollection *collection, GckSecretData *sdata)
259
item = gck_secret_collection_new_item (collection, "4");
260
gck_secret_object_set_label (GCK_SECRET_OBJECT (item), "Noises");
261
secret = gck_secret_new_from_password ("4's secret");
262
gck_secret_data_set_secret (sdata, "4", secret);
263
g_object_unref (secret);
264
fields = gck_secret_item_get_fields (item);
265
gck_secret_fields_add (fields, "doggy", "fart");
266
gck_secret_fields_add (fields, "pig", "grunt");
267
gck_secret_fields_add_compat_uint32 (fields, "how-many", 292929);
269
item = gck_secret_collection_new_item (collection, "5");
270
gck_secret_object_set_label (GCK_SECRET_OBJECT (item), "Colors");
271
secret = gck_secret_new_from_password ("5's secret");
272
gck_secret_data_set_secret (sdata, "5", secret);
273
g_object_unref (secret);
274
fields = gck_secret_item_get_fields (item);
275
gck_secret_fields_add (fields, "barney", "purple");
276
gck_secret_fields_add (fields, "piglet", "pink");
277
gck_secret_fields_add_compat_uint32 (fields, "number", 8);
279
item = gck_secret_collection_new_item (collection, "6");
280
gck_secret_object_set_label (GCK_SECRET_OBJECT (item), "Binary Secret");
281
secret = gck_secret_new ((guchar*)"binary\0secret", 13);
282
gck_secret_data_set_secret (sdata, "6", secret);
283
g_object_unref (secret);
284
fields = gck_secret_item_get_fields (item);
285
gck_secret_fields_add (fields, "train", "zoom");
286
gck_secret_fields_add (fields, "hummer", NULL);
287
gck_secret_fields_add_compat_uint32 (fields, "number", 2);