~ubuntu-branches/ubuntu/natty/gnome-keyring/natty

« back to all changes in this revision

Viewing changes to pkcs11/secret-store/tests/test-secret-module.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2010-02-16 19:00:06 UTC
  • mfrom: (1.1.58 upstream)
  • Revision ID: james.westby@ubuntu.com-20100216190006-cqpnic4zxlkmmi0o
Tags: 2.29.90git20100218-0ubuntu1
Updated to a git snapshot version

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
19
   Boston, MA 02111-1307, USA.
 
20
 
 
21
   Author: Stef Walter <stef@memberwebs.com>
 
22
*/
 
23
 
 
24
#include "config.h"
 
25
#include "test-secret-module.h"
 
26
#include "run-auto-test.h"
 
27
 
 
28
#include "gck/gck-secret.h"
 
29
#include "gck/gck-module.h"
 
30
 
 
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"
 
37
 
 
38
#include <string.h>
 
39
 
 
40
static GMutex *mutex = NULL;
 
41
 
 
42
GckModule*  _gck_secret_store_get_module_for_testing (void);
 
43
GMutex* _gck_module_get_scary_mutex_that_you_should_not_touch (GckModule *module);
 
44
 
 
45
static void
 
46
copy_scratch_file (const gchar *basename)
 
47
{
 
48
        gchar *filename;
 
49
        gchar *data;
 
50
        gsize n_data;
 
51
 
 
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 ();
 
56
        }
 
57
        g_free (filename);
 
58
 
 
59
        filename = test_scratch_filename (basename);
 
60
        if (!g_file_set_contents (filename, data, n_data, NULL))
 
61
                g_return_if_reached ();
 
62
        g_free (filename);
 
63
        g_free (data);
 
64
}
 
65
 
 
66
GckModule*
 
67
test_secret_module_initialize_and_enter (void)
 
68
{
 
69
        CK_FUNCTION_LIST_PTR funcs;
 
70
        CK_C_INITIALIZE_ARGS args;
 
71
        GckModule *module;
 
72
        gchar *string;
 
73
        CK_RV rv;
 
74
 
 
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;
 
80
 
 
81
        /* Copy files from test-data to scratch */
 
82
        copy_scratch_file ("encrypted.keyring");
 
83
        copy_scratch_file ("plain.keyring");
 
84
 
 
85
        funcs = gck_secret_store_get_functions ();
 
86
        rv = (funcs->C_Initialize) (&args);
 
87
        g_return_val_if_fail (rv == CKR_OK, NULL);
 
88
 
 
89
        module = _gck_secret_store_get_module_for_testing ();
 
90
        g_return_val_if_fail (module, NULL);
 
91
 
 
92
        mutex = _gck_module_get_scary_mutex_that_you_should_not_touch (module);
 
93
        test_secret_module_enter ();
 
94
 
 
95
        g_free (string);
 
96
 
 
97
        return module;
 
98
}
 
99
 
 
100
void
 
101
test_secret_module_leave_and_finalize (void)
 
102
{
 
103
        CK_FUNCTION_LIST_PTR funcs;
 
104
        CK_RV rv;
 
105
 
 
106
        test_secret_module_leave ();
 
107
 
 
108
        funcs = gck_secret_store_get_functions ();
 
109
        rv = (funcs->C_Finalize) (NULL);
 
110
        g_return_if_fail (rv == CKR_OK);
 
111
}
 
112
 
 
113
void
 
114
test_secret_module_leave (void)
 
115
{
 
116
        g_assert (mutex);
 
117
        g_mutex_unlock (mutex);
 
118
}
 
119
 
 
120
void
 
121
test_secret_module_enter (void)
 
122
{
 
123
        g_assert (mutex);
 
124
        g_mutex_lock (mutex);
 
125
}
 
126
 
 
127
GckSession*
 
128
test_secret_module_open_session (gboolean writable)
 
129
{
 
130
        CK_ULONG flags = CKF_SERIAL_SESSION;
 
131
        CK_SESSION_HANDLE handle;
 
132
        GckModule *module;
 
133
        GckSession *session;
 
134
        CK_RV rv;
 
135
 
 
136
        module = _gck_secret_store_get_module_for_testing ();
 
137
        g_return_val_if_fail (module, NULL);
 
138
 
 
139
        if (writable)
 
140
                flags |= CKF_RW_SESSION;
 
141
 
 
142
        rv = gck_module_C_OpenSession (module, 1, flags, NULL, NULL, &handle);
 
143
        g_assert (rv == CKR_OK);
 
144
 
 
145
        rv = gck_module_C_Login (module, handle, CKU_USER, NULL, 0);
 
146
        g_assert (rv == CKR_OK);
 
147
 
 
148
        session = gck_module_lookup_session (module, handle);
 
149
        g_assert (session);
 
150
 
 
151
        return session;
 
152
}
 
153
 
 
154
/* Validates plain.keyring and encrypted.keyring in test-data */
 
155
void
 
156
test_secret_collection_validate (GckSecretCollection *collection, GckSecretData *sdata)
 
157
{
 
158
        GckSecretItem* item;
 
159
        GckSecretObject *obj;
 
160
        GHashTable *fields;
 
161
        const gchar *value;
 
162
        GckSecret *secret;
 
163
        GList *items;
 
164
        glong when;
 
165
        guint32 num;
 
166
 
 
167
        obj = GCK_SECRET_OBJECT (collection);
 
168
 
 
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");
 
175
#if 0
 
176
        /* "Bad lock settings" */
 
177
        g_assert (!keyring->lock_on_idle && keyring->lock_timeout == 0);
 
178
#endif
 
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);
 
188
        g_list_free (items);
 
189
 
 
190
        /* Item #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);
 
195
#if 0
 
196
        /* "Invalid item type" */
 
197
        g_assert_cmpint (item->type, ==, GNOME_KEYRING_ITEM_GENERIC_SECRET);
 
198
#endif
 
199
        /* "Missing secret" */
 
200
        secret = gck_secret_data_get_secret (sdata, "2");
 
201
        g_assert (secret != NULL);
 
202
        /* "Wrong secret" */
 
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);
 
207
 
 
208
#if 0
 
209
        /* Item #2 ACL */
 
210
        /* "Bad ACLs" */
 
211
        g_assert_cmpint (g_list_length (item->acl), ==, 1);
 
212
        ac = (GnomeKeyringAccessControl*)item->acl->data;
 
213
        /* "Invalid ACL" */
 
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));
 
222
#endif
 
223
 
 
224
        /* Item #3 */
 
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);
 
240
#if 0
 
241
        /* "Invalid item type" */
 
242
        g_assert_cmpint (item->type, ==, GNOME_KEYRING_ITEM_GENERIC_SECRET);
 
243
#endif
 
244
        /* "Missing secret" */
 
245
        secret = gck_secret_data_get_secret (sdata, "3");
 
246
        g_assert (secret != NULL);
 
247
        /* "Wrong secret" */
 
248
        g_assert (gck_secret_equals (secret, (guchar*)"item-secret", -1));
 
249
}
 
250
 
 
251
/* Fills a collection with some junk data */
 
252
void
 
253
test_secret_collection_populate (GckSecretCollection *collection, GckSecretData *sdata)
 
254
{
 
255
        GckSecretItem *item;
 
256
        GHashTable *fields;
 
257
        GckSecret *secret;
 
258
 
 
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);
 
268
 
 
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);
 
278
 
 
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);
 
288
}