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

« back to all changes in this revision

Viewing changes to pkcs11/secret-store/tests/unit-test-secret-item.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
/* unit-test-secret-item.c: Test secret item
 
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
 
 
26
#include "run-auto-test.h"
 
27
#include "test-secret-module.h"
 
28
 
 
29
#include "gck-secret-collection.h"
 
30
#include "gck-secret-fields.h"
 
31
#include "gck-secret-item.h"
 
32
 
 
33
#include "gck/gck-credential.h"
 
34
#include "gck/gck-session.h"
 
35
#include "gck/gck-transaction.h"
 
36
 
 
37
#include "pkcs11/pkcs11i.h"
 
38
 
 
39
#include <glib.h>
 
40
 
 
41
#include <stdlib.h>
 
42
#include <stdio.h>
 
43
#include <string.h>
 
44
 
 
45
static GckModule *module = NULL;
 
46
static GckSession *session = NULL;
 
47
static GckSecretCollection *collection = NULL;
 
48
 
 
49
DEFINE_SETUP(secret_item)
 
50
{
 
51
        module = test_secret_module_initialize_and_enter ();
 
52
        session = test_secret_module_open_session (TRUE);
 
53
 
 
54
        collection = g_object_new (GCK_TYPE_SECRET_COLLECTION,
 
55
                                   "module", module,
 
56
                                   "identifier", "test",
 
57
                                   NULL);
 
58
}
 
59
 
 
60
DEFINE_TEARDOWN(secret_item)
 
61
{
 
62
        if (collection)
 
63
                g_object_unref (collection);
 
64
        collection = NULL;
 
65
 
 
66
        test_secret_module_leave_and_finalize ();
 
67
        module = NULL;
 
68
        session = NULL;
 
69
 
 
70
}
 
71
 
 
72
static void
 
73
unlock_collection(void)
 
74
{
 
75
        GckCredential *cred;
 
76
        GckObject *object;
 
77
        CK_RV rv;
 
78
 
 
79
        /* Create credential, which unlocks collection */
 
80
        object = GCK_OBJECT (collection);
 
81
        rv = gck_credential_create (gck_object_get_module (object),
 
82
                                    gck_session_get_manager (session),
 
83
                                    object, NULL, 0, &cred);
 
84
        g_assert (rv == CKR_OK);
 
85
 
 
86
        gck_session_add_session_object (session, NULL, GCK_OBJECT (cred));
 
87
        g_object_unref (cred);
 
88
}
 
89
 
 
90
DEFINE_TEST(secret_item_new)
 
91
{
 
92
        GckSecretItem *item;
 
93
 
 
94
        item = gck_secret_collection_new_item (collection, "the-identifier");
 
95
        g_assert (GCK_IS_SECRET_ITEM (item));
 
96
        g_assert_cmpstr (gck_secret_object_get_identifier (GCK_SECRET_OBJECT (item)), ==, "the-identifier");
 
97
}
 
98
 
 
99
DEFINE_TEST(secret_item_create)
 
100
{
 
101
        GckTransaction *transaction;
 
102
        GckSecretItem *item;
 
103
 
 
104
        transaction = gck_transaction_new ();
 
105
        item = gck_secret_collection_create_item (collection, transaction);
 
106
        g_assert (GCK_IS_SECRET_ITEM (item));
 
107
        g_object_ref (item);
 
108
        g_assert (gck_secret_collection_has_item (collection, item));
 
109
 
 
110
        gck_transaction_complete (transaction);
 
111
        g_object_unref (transaction);
 
112
 
 
113
        /* Should still be there */
 
114
        g_assert (gck_secret_collection_has_item (collection, item));
 
115
        g_object_unref (item);
 
116
}
 
117
 
 
118
DEFINE_TEST(secret_item_create_failed)
 
119
{
 
120
        GckTransaction *transaction;
 
121
        GckSecretItem *item;
 
122
 
 
123
        transaction = gck_transaction_new ();
 
124
        item = gck_secret_collection_create_item (collection, transaction);
 
125
        g_assert (GCK_IS_SECRET_ITEM (item));
 
126
        g_object_ref (item);
 
127
        g_assert (gck_secret_collection_has_item (collection, item));
 
128
 
 
129
        gck_transaction_fail (transaction, CKR_GENERAL_ERROR);
 
130
        gck_transaction_complete (transaction);
 
131
        g_object_unref (transaction);
 
132
 
 
133
        /* Should no longer be there */
 
134
        g_assert (!gck_secret_collection_has_item (collection, item));
 
135
        g_object_unref (item);
 
136
}
 
137
 
 
138
DEFINE_TEST(secret_item_destroy)
 
139
{
 
140
        GckTransaction *transaction;
 
141
        GckSecretItem *item;
 
142
 
 
143
        item = gck_secret_collection_new_item (collection, "the-identifier");
 
144
        g_assert (gck_secret_collection_has_item (collection, item));
 
145
        g_object_ref (item);
 
146
 
 
147
        transaction = gck_transaction_new ();
 
148
        gck_secret_collection_destroy_item (collection, transaction, item);
 
149
        g_assert (!gck_secret_collection_has_item (collection, item));
 
150
 
 
151
        gck_transaction_complete (transaction);
 
152
        g_object_unref (transaction);
 
153
 
 
154
        /* Should not be there */
 
155
        g_assert (!gck_secret_collection_has_item (collection, item));
 
156
        g_object_unref (item);
 
157
}
 
158
 
 
159
DEFINE_TEST(secret_item_destroy_failed)
 
160
{
 
161
        GckTransaction *transaction;
 
162
        GckSecretItem *item;
 
163
 
 
164
        item = gck_secret_collection_new_item (collection, "the-identifier");
 
165
        g_assert (gck_secret_collection_has_item (collection, item));
 
166
        g_object_ref (item);
 
167
 
 
168
        transaction = gck_transaction_new ();
 
169
        gck_secret_collection_destroy_item (collection, transaction, item);
 
170
        g_assert (!gck_secret_collection_has_item (collection, item));
 
171
 
 
172
        gck_transaction_fail (transaction, CKR_GENERAL_ERROR);
 
173
        gck_transaction_complete (transaction);
 
174
        g_object_unref (transaction);
 
175
 
 
176
        /* Should be there */
 
177
        g_assert (gck_secret_collection_has_item (collection, item));
 
178
        g_object_unref (item);
 
179
}
 
180
 
 
181
DEFINE_TEST(secret_item_collection_get)
 
182
{
 
183
        GckSecretItem *item, *check;
 
184
 
 
185
        item = gck_secret_collection_new_item (collection, "the-identifier");
 
186
        g_assert (GCK_IS_SECRET_ITEM (item));
 
187
 
 
188
        check = gck_secret_collection_get_item (collection, "the-identifier");
 
189
        g_assert (item == check);
 
190
}
 
191
 
 
192
DEFINE_TEST(secret_item_collection_items)
 
193
{
 
194
        GList *l, *items;
 
195
        const gchar *identifier;
 
196
 
 
197
        gck_secret_collection_new_item (collection, "one-identifier");
 
198
        gck_secret_collection_new_item (collection, "two-identifier");
 
199
        gck_secret_collection_new_item (collection, "three-identifier");
 
200
 
 
201
        items = gck_secret_collection_get_items (collection);
 
202
        g_assert_cmpuint (g_list_length (items), ==, 3);
 
203
        for (l = items; l; l = g_list_next (l)) {
 
204
                identifier = gck_secret_object_get_identifier (l->data);
 
205
                if (!g_str_equal (identifier, "one-identifier") &&
 
206
                    !g_str_equal (identifier, "two-identifier") &&
 
207
                    !g_str_equal (identifier, "three-identifier"))
 
208
                        g_assert_not_reached ();
 
209
        }
 
210
 
 
211
        g_list_free (items);
 
212
}
 
213
 
 
214
DEFINE_TEST(secret_item_collection_remove)
 
215
{
 
216
        GckSecretItem *item;
 
217
 
 
218
        item = gck_secret_collection_new_item (collection, "the-identifier");
 
219
        g_assert (gck_secret_collection_get_item (collection, "the-identifier") == item);
 
220
 
 
221
        gck_secret_collection_remove_item (collection, item);
 
222
        g_assert (gck_secret_collection_get_item (collection, "the-identifier") == NULL);
 
223
}
 
224
 
 
225
DEFINE_TEST(secret_item_is_locked)
 
226
{
 
227
        GckSecretItem *item;
 
228
 
 
229
        item = gck_secret_collection_new_item (collection, "the-identifier");
 
230
        g_assert (gck_secret_object_is_locked (GCK_SECRET_OBJECT (item), session) == 
 
231
                  gck_secret_object_is_locked (GCK_SECRET_OBJECT (collection), session));
 
232
 
 
233
        unlock_collection();
 
234
 
 
235
        g_assert (gck_secret_object_is_locked (GCK_SECRET_OBJECT (item), session) == FALSE);
 
236
}
 
237
 
 
238
DEFINE_TEST(secret_item_get_collection)
 
239
{
 
240
        GckSecretItem *item;
 
241
        item = gck_secret_collection_new_item (collection, "the-identifier");
 
242
        g_assert (gck_secret_item_get_collection (item) == collection);
 
243
}
 
244
 
 
245
DEFINE_TEST(secret_item_tracks_collection)
 
246
{
 
247
        GckSecretItem *item;
 
248
        item = gck_secret_collection_new_item (collection, "the-identifier");
 
249
        g_object_ref (item);
 
250
 
 
251
        unlock_collection();
 
252
 
 
253
        /* At this point the item should be 'unlocked' */
 
254
        g_assert (gck_secret_object_is_locked (GCK_SECRET_OBJECT (item), session) == FALSE);
 
255
 
 
256
        g_object_unref (collection);
 
257
        collection = NULL;
 
258
 
 
259
        /* Collection went away */
 
260
        g_assert (gck_secret_item_get_collection (item) == NULL);
 
261
        g_assert (gck_secret_object_is_locked (GCK_SECRET_OBJECT (item), session) == TRUE);
 
262
 
 
263
        g_object_unref (item);
 
264
}
 
265
 
 
266
DEFINE_TEST(secret_item_get_set_fields)
 
267
{
 
268
        GHashTable *fields = gck_secret_fields_new ();
 
269
        GHashTable *check;
 
270
        GckSecretItem *item;
 
271
 
 
272
        item = gck_secret_collection_new_item (collection, "the-identifier");
 
273
        gck_secret_item_set_fields (item, fields);
 
274
        gck_secret_item_set_fields (item, fields);
 
275
 
 
276
        check = gck_secret_item_get_fields (item);
 
277
        g_assert (check == fields);
 
278
 
 
279
        g_hash_table_unref (fields);
 
280
}
 
281
 
 
282
DEFINE_TEST(secret_item_collection_attr)
 
283
{
 
284
        gchar buffer[32];
 
285
        CK_ATTRIBUTE check = { CKA_G_COLLECTION, buffer, 32 };
 
286
        GckSecretItem *item;
 
287
        CK_RV rv;
 
288
 
 
289
        item = gck_secret_collection_new_item (collection, "the-identifier");
 
290
        rv = gck_object_get_attribute (GCK_OBJECT (item), session, &check);
 
291
        g_assert (rv == CKR_OK);
 
292
        g_assert (check.ulValueLen == 4);
 
293
        g_assert (memcmp (buffer, "test", 4) == 0);
 
294
}
 
295
 
 
296
DEFINE_TEST(secret_item_secret_attr)
 
297
{
 
298
        GckTransaction *transaction = gck_transaction_new ();
 
299
        CK_ATTRIBUTE attr = { CKA_VALUE, "hello", 5 };
 
300
        gchar buffer[32];
 
301
        CK_ATTRIBUTE check = { CKA_VALUE, buffer, 32 };
 
302
        GckSecretItem *item;
 
303
        CK_RV rv;
 
304
 
 
305
        unlock_collection ();
 
306
 
 
307
        item = gck_secret_collection_new_item (collection, "the-identifier");
 
308
        gck_object_set_attribute (GCK_OBJECT (item), session, transaction, &attr);
 
309
        g_assert (gck_transaction_get_failed (transaction) == FALSE);
 
310
        gck_transaction_complete (transaction);
 
311
        g_assert (gck_transaction_get_result (transaction) == CKR_OK);
 
312
 
 
313
        g_object_unref (transaction);
 
314
 
 
315
        rv = gck_object_get_attribute (GCK_OBJECT (item), session, &check);
 
316
        g_assert (rv == CKR_OK);
 
317
        g_assert (check.ulValueLen == 5);
 
318
        g_assert (memcmp (buffer, "hello", 5) == 0);
 
319
}
 
320
 
 
321
DEFINE_TEST(secret_item_secret_attr_locked)
 
322
{
 
323
        GckTransaction *transaction = gck_transaction_new ();
 
324
        CK_ATTRIBUTE attr = { CKA_VALUE, "hello", 5 };
 
325
        gchar buffer[32];
 
326
        CK_ATTRIBUTE check = { CKA_VALUE, buffer, 32 };
 
327
        GckSecretItem *item;
 
328
        CK_RV rv;
 
329
 
 
330
        item = gck_secret_collection_new_item (collection, "the-identifier");
 
331
        gck_object_set_attribute (GCK_OBJECT (item), session, transaction, &attr);
 
332
        g_assert (gck_transaction_get_failed (transaction) == TRUE);
 
333
        gck_transaction_complete (transaction);
 
334
        g_assert (gck_transaction_get_result (transaction) == CKR_USER_NOT_LOGGED_IN);
 
335
 
 
336
        g_object_unref (transaction);
 
337
 
 
338
        rv = gck_object_get_attribute (GCK_OBJECT (item), session, &check);
 
339
        g_assert (rv == CKR_USER_NOT_LOGGED_IN);
 
340
}
 
341
 
 
342
DEFINE_TEST(secret_item_fields_attr)
 
343
{
 
344
        GckTransaction *transaction = gck_transaction_new ();
 
345
        CK_ATTRIBUTE attr = { CKA_G_FIELDS, "name1\0value1\0name2\0value2", 26 };
 
346
        gchar buffer[32];
 
347
        CK_ATTRIBUTE check = { CKA_G_FIELDS, buffer, 32 };
 
348
        GckSecretItem *item;
 
349
        GHashTable *fields;
 
350
        const gchar *value;
 
351
        CK_RV rv;
 
352
 
 
353
        unlock_collection ();
 
354
 
 
355
        item = gck_secret_collection_new_item (collection, "the-identifier");
 
356
        gck_object_set_attribute (GCK_OBJECT (item), session, transaction, &attr);
 
357
        g_assert (gck_transaction_get_failed (transaction) == FALSE);
 
358
        gck_transaction_complete (transaction);
 
359
        g_assert (gck_transaction_get_result (transaction) == CKR_OK);
 
360
 
 
361
        g_object_unref (transaction);
 
362
 
 
363
        rv = gck_object_get_attribute (GCK_OBJECT (item), session, &check);
 
364
        g_assert (rv == CKR_OK);
 
365
        g_assert (check.ulValueLen == 26);
 
366
        g_assert (memcmp (buffer, "name1\0value1\0name2\0value2", 26) == 0);
 
367
 
 
368
        fields = gck_secret_item_get_fields (item);
 
369
        g_assert (fields);
 
370
        value = gck_secret_fields_get (fields, "name1");
 
371
        g_assert_cmpstr (value, ==, "value1");
 
372
        value = gck_secret_fields_get (fields, "name2");
 
373
        g_assert_cmpstr (value, ==, "value2");
 
374
}
 
375
 
 
376
DEFINE_TEST(secret_item_fields_attr_locked)
 
377
{
 
378
        GckTransaction *transaction = gck_transaction_new ();
 
379
        CK_ATTRIBUTE attr = { CKA_G_FIELDS, "name1\0value1\0name2\0value2", 26 };
 
380
        GckSecretItem *item;
 
381
 
 
382
        item = gck_secret_collection_new_item (collection, "the-identifier");
 
383
        gck_object_set_attribute (GCK_OBJECT (item), session, transaction, &attr);
 
384
        g_assert (gck_transaction_get_failed (transaction) == TRUE);
 
385
        gck_transaction_complete (transaction);
 
386
        g_assert (gck_transaction_get_result (transaction) == CKR_USER_NOT_LOGGED_IN);
 
387
 
 
388
        g_object_unref (transaction);
 
389
}
 
390
 
 
391
DEFINE_TEST(secret_item_fields_attr_reverts)
 
392
{
 
393
        GckTransaction *transaction = gck_transaction_new ();
 
394
        CK_ATTRIBUTE attr = { CKA_G_FIELDS, "new\0value\0", 10 };
 
395
        gchar buffer[32];
 
396
        CK_ATTRIBUTE check = { CKA_G_FIELDS, buffer, 32 };
 
397
        GckSecretItem *item;
 
398
        GHashTable *fields;
 
399
        CK_RV rv;
 
400
 
 
401
        unlock_collection ();
 
402
 
 
403
        item = gck_secret_collection_new_item (collection, "the-identifier");
 
404
 
 
405
        /* Set the old value like so */
 
406
        fields = gck_secret_fields_new ();
 
407
        gck_secret_fields_add (fields, "old", "value");
 
408
        gck_secret_item_set_fields (item, fields);
 
409
        g_hash_table_unref (fields);
 
410
 
 
411
        /* Should show old value */
 
412
        rv = gck_object_get_attribute (GCK_OBJECT (item), session, &check);
 
413
        g_assert (rv == CKR_OK);
 
414
        g_assert (check.ulValueLen == 10);
 
415
        g_assert (memcmp (buffer, "old\0value\0", 10) == 0);
 
416
 
 
417
        /* Set the new values */
 
418
        gck_object_set_attribute (GCK_OBJECT (item), session, transaction, &attr);
 
419
        g_assert (gck_transaction_get_failed (transaction) == FALSE);
 
420
 
 
421
        /* Should have the new value */
 
422
        rv = gck_object_get_attribute (GCK_OBJECT (item), session, &check);
 
423
        g_assert (rv == CKR_OK);
 
424
        g_assert (check.ulValueLen == 10);
 
425
        g_assert (memcmp (buffer, "new\0value\0", 10) == 0);
 
426
 
 
427
        /* Fail the transaction */
 
428
        gck_transaction_fail (transaction, CKR_CANCEL);
 
429
        gck_transaction_complete (transaction);
 
430
 
 
431
        /* Should show the old value */
 
432
        rv = gck_object_get_attribute (GCK_OBJECT (item), session, &check);
 
433
        g_assert (rv == CKR_OK);
 
434
        g_assert (check.ulValueLen == 10);
 
435
        g_assert (memcmp (buffer, "old\0value\0", 10) == 0);
 
436
 
 
437
        g_object_unref (transaction);
 
438
}