~noskcaj/ubuntu/vivid/gnome-keyring/3.15.90

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Jordi Mallach
  • Date: 2012-05-14 22:13:02 UTC
  • mfrom: (1.3.1)
  • mto: (80.2.8 experimental) (1.1.77)
  • mto: This revision was merged to the branch mainline in revision 148.
  • Revision ID: package-import@ubuntu.com-20120514221302-0l3gjmqpe6xopond
ImportĀ upstreamĀ versionĀ 3.4.1

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 "mock-secret-module.h"
 
27
 
 
28
#include "secret-store/gkm-secret-collection.h"
 
29
#include "secret-store/gkm-secret-fields.h"
 
30
#include "secret-store/gkm-secret-item.h"
 
31
 
 
32
#include "gkm/gkm-credential.h"
 
33
#include "gkm/gkm-session.h"
 
34
#include "gkm/gkm-transaction.h"
 
35
 
 
36
#include "pkcs11/pkcs11i.h"
 
37
 
 
38
#include <glib.h>
 
39
 
 
40
#include <stdlib.h>
 
41
#include <stdio.h>
 
42
#include <string.h>
 
43
 
 
44
typedef struct {
 
45
        GkmModule *module;
 
46
        GkmSession *session;
 
47
        GkmSecretCollection *collection;
 
48
} Test;
 
49
 
 
50
static void
 
51
setup (Test *test, gconstpointer unused)
 
52
{
 
53
        test->module = test_secret_module_initialize_and_enter ();
 
54
        test->session = test_secret_module_open_session (TRUE);
 
55
 
 
56
        test->collection = g_object_new (GKM_TYPE_SECRET_COLLECTION,
 
57
                                   "module", test->module,
 
58
                                   "identifier", "test",
 
59
                                   NULL);
 
60
}
 
61
 
 
62
static void
 
63
teardown (Test *test, gconstpointer unused)
 
64
{
 
65
        if (test->collection)
 
66
                g_object_unref (test->collection);
 
67
        test_secret_module_leave_and_finalize ();
 
68
}
 
69
 
 
70
static void
 
71
unlock_collection (Test *test)
 
72
{
 
73
        GkmCredential *cred;
 
74
        GkmObject *object;
 
75
        CK_RV rv;
 
76
 
 
77
        /* Create credential, which unlocks test->collection */
 
78
        object = GKM_OBJECT (test->collection);
 
79
        rv = gkm_credential_create (gkm_object_get_module (object),
 
80
                                    gkm_session_get_manager (test->session),
 
81
                                    object, NULL, 0, &cred);
 
82
        g_assert (rv == CKR_OK);
 
83
 
 
84
        gkm_session_add_session_object (test->session, NULL, GKM_OBJECT (cred));
 
85
        g_object_unref (cred);
 
86
}
 
87
 
 
88
static void
 
89
test_new (Test *test, gconstpointer unused)
 
90
{
 
91
        GkmSecretItem *item;
 
92
 
 
93
        item = gkm_secret_collection_new_item (test->collection, "the-identifier");
 
94
        g_assert (GKM_IS_SECRET_ITEM (item));
 
95
        g_assert_cmpstr (gkm_secret_object_get_identifier (GKM_SECRET_OBJECT (item)), ==, "the-identifier");
 
96
}
 
97
 
 
98
static void
 
99
test_create (Test *test, gconstpointer unused)
 
100
{
 
101
        GkmTransaction *transaction;
 
102
        GkmSecretItem *item;
 
103
 
 
104
        transaction = gkm_transaction_new ();
 
105
        item = gkm_secret_collection_create_item (test->collection, transaction);
 
106
        g_assert (GKM_IS_SECRET_ITEM (item));
 
107
        g_object_ref (item);
 
108
        g_assert (gkm_secret_collection_has_item (test->collection, item));
 
109
 
 
110
        gkm_transaction_complete (transaction);
 
111
        g_object_unref (transaction);
 
112
 
 
113
        /* Should still be there */
 
114
        g_assert (gkm_secret_collection_has_item (test->collection, item));
 
115
        g_object_unref (item);
 
116
}
 
117
 
 
118
static void
 
119
test_create_failed (Test *test, gconstpointer unused)
 
120
{
 
121
        GkmTransaction *transaction;
 
122
        GkmSecretItem *item;
 
123
 
 
124
        transaction = gkm_transaction_new ();
 
125
        item = gkm_secret_collection_create_item (test->collection, transaction);
 
126
        g_assert (GKM_IS_SECRET_ITEM (item));
 
127
        g_object_ref (item);
 
128
        g_assert (gkm_secret_collection_has_item (test->collection, item));
 
129
 
 
130
        gkm_transaction_fail (transaction, CKR_GENERAL_ERROR);
 
131
        gkm_transaction_complete (transaction);
 
132
        g_object_unref (transaction);
 
133
 
 
134
        /* Should no longer be there */
 
135
        g_assert (!gkm_secret_collection_has_item (test->collection, item));
 
136
        g_object_unref (item);
 
137
}
 
138
 
 
139
static void
 
140
test_destroy (Test *test, gconstpointer unused)
 
141
{
 
142
        GkmTransaction *transaction;
 
143
        GkmSecretItem *item;
 
144
 
 
145
        item = gkm_secret_collection_new_item (test->collection, "the-identifier");
 
146
        g_assert (gkm_secret_collection_has_item (test->collection, item));
 
147
        g_object_ref (item);
 
148
 
 
149
        transaction = gkm_transaction_new ();
 
150
        gkm_secret_collection_destroy_item (test->collection, transaction, item);
 
151
        g_assert (!gkm_secret_collection_has_item (test->collection, item));
 
152
 
 
153
        gkm_transaction_complete (transaction);
 
154
        g_object_unref (transaction);
 
155
 
 
156
        /* Should not be there */
 
157
        g_assert (!gkm_secret_collection_has_item (test->collection, item));
 
158
        g_object_unref (item);
 
159
}
 
160
 
 
161
static void
 
162
test_destroy_failed (Test *test, gconstpointer unused)
 
163
{
 
164
        GkmTransaction *transaction;
 
165
        GkmSecretItem *item;
 
166
 
 
167
        item = gkm_secret_collection_new_item (test->collection, "the-identifier");
 
168
        g_assert (gkm_secret_collection_has_item (test->collection, item));
 
169
        g_object_ref (item);
 
170
 
 
171
        transaction = gkm_transaction_new ();
 
172
        gkm_secret_collection_destroy_item (test->collection, transaction, item);
 
173
        g_assert (!gkm_secret_collection_has_item (test->collection, item));
 
174
 
 
175
        gkm_transaction_fail (transaction, CKR_GENERAL_ERROR);
 
176
        gkm_transaction_complete (transaction);
 
177
        g_object_unref (transaction);
 
178
 
 
179
        /* Should be there */
 
180
        g_assert (gkm_secret_collection_has_item (test->collection, item));
 
181
        g_object_unref (item);
 
182
}
 
183
 
 
184
static void
 
185
test_collection_get (Test *test, gconstpointer unused)
 
186
{
 
187
        GkmSecretItem *item, *check;
 
188
 
 
189
        item = gkm_secret_collection_new_item (test->collection, "the-identifier");
 
190
        g_assert (GKM_IS_SECRET_ITEM (item));
 
191
 
 
192
        check = gkm_secret_collection_get_item (test->collection, "the-identifier");
 
193
        g_assert (item == check);
 
194
}
 
195
 
 
196
static void
 
197
test_collection_items (Test *test, gconstpointer unused)
 
198
{
 
199
        GList *l, *items;
 
200
        const gchar *identifier;
 
201
 
 
202
        gkm_secret_collection_new_item (test->collection, "one-identifier");
 
203
        gkm_secret_collection_new_item (test->collection, "two-identifier");
 
204
        gkm_secret_collection_new_item (test->collection, "three-identifier");
 
205
 
 
206
        items = gkm_secret_collection_get_items (test->collection);
 
207
        g_assert_cmpuint (g_list_length (items), ==, 3);
 
208
        for (l = items; l; l = g_list_next (l)) {
 
209
                identifier = gkm_secret_object_get_identifier (l->data);
 
210
                if (!g_str_equal (identifier, "one-identifier") &&
 
211
                    !g_str_equal (identifier, "two-identifier") &&
 
212
                    !g_str_equal (identifier, "three-identifier"))
 
213
                        g_assert_not_reached ();
 
214
        }
 
215
 
 
216
        g_list_free (items);
 
217
}
 
218
 
 
219
static void
 
220
test_collection_remove (Test *test, gconstpointer unused)
 
221
{
 
222
        GkmSecretItem *item;
 
223
 
 
224
        item = gkm_secret_collection_new_item (test->collection, "the-identifier");
 
225
        g_assert (gkm_secret_collection_get_item (test->collection, "the-identifier") == item);
 
226
 
 
227
        gkm_secret_collection_remove_item (test->collection, item);
 
228
        g_assert (gkm_secret_collection_get_item (test->collection, "the-identifier") == NULL);
 
229
}
 
230
 
 
231
static void
 
232
test_is_locked (Test *test, gconstpointer unused)
 
233
{
 
234
        GkmSecretItem *item;
 
235
 
 
236
        item = gkm_secret_collection_new_item (test->collection, "the-identifier");
 
237
        g_assert (gkm_secret_object_is_locked (GKM_SECRET_OBJECT (item), test->session) ==
 
238
                  gkm_secret_object_is_locked (GKM_SECRET_OBJECT (test->collection), test->session));
 
239
 
 
240
        unlock_collection (test);
 
241
 
 
242
        g_assert (gkm_secret_object_is_locked (GKM_SECRET_OBJECT (item), test->session) == FALSE);
 
243
}
 
244
 
 
245
static void
 
246
test_get_collection (Test *test, gconstpointer unused)
 
247
{
 
248
        GkmSecretItem *item;
 
249
        item = gkm_secret_collection_new_item (test->collection, "the-identifier");
 
250
        g_assert (gkm_secret_item_get_collection (item) == test->collection);
 
251
}
 
252
 
 
253
static void
 
254
test_tracks_collection (Test *test, gconstpointer unused)
 
255
{
 
256
        GkmSecretItem *item;
 
257
        item = gkm_secret_collection_new_item (test->collection, "the-identifier");
 
258
        g_object_ref (item);
 
259
 
 
260
        unlock_collection (test);
 
261
 
 
262
        /* At this point the item should be 'unlocked' */
 
263
        g_assert (gkm_secret_object_is_locked (GKM_SECRET_OBJECT (item), test->session) == FALSE);
 
264
 
 
265
        g_object_unref (test->collection);
 
266
        test->collection = NULL;
 
267
 
 
268
        /* Collection went away */
 
269
        g_assert (gkm_secret_item_get_collection (item) == NULL);
 
270
        g_assert (gkm_secret_object_is_locked (GKM_SECRET_OBJECT (item), test->session) == TRUE);
 
271
 
 
272
        g_object_unref (item);
 
273
}
 
274
 
 
275
static void
 
276
test_get_set_fields (Test *test, gconstpointer unused)
 
277
{
 
278
        GHashTable *fields = gkm_secret_fields_new ();
 
279
        GHashTable *check;
 
280
        GkmSecretItem *item;
 
281
 
 
282
        item = gkm_secret_collection_new_item (test->collection, "the-identifier");
 
283
        gkm_secret_item_set_fields (item, fields);
 
284
        gkm_secret_item_set_fields (item, fields);
 
285
 
 
286
        check = gkm_secret_item_get_fields (item);
 
287
        g_assert (check == fields);
 
288
 
 
289
        g_hash_table_unref (fields);
 
290
}
 
291
 
 
292
static void
 
293
test_collection_attr (Test *test, gconstpointer unused)
 
294
{
 
295
        gchar buffer[32];
 
296
        CK_ATTRIBUTE check = { CKA_G_COLLECTION, buffer, 32 };
 
297
        GkmSecretItem *item;
 
298
        CK_RV rv;
 
299
 
 
300
        item = gkm_secret_collection_new_item (test->collection, "the-identifier");
 
301
        rv = gkm_object_get_attribute (GKM_OBJECT (item), test->session, &check);
 
302
        g_assert (rv == CKR_OK);
 
303
        g_assert (check.ulValueLen == 4);
 
304
        g_assert (memcmp (buffer, "test", 4) == 0);
 
305
}
 
306
 
 
307
static void
 
308
test_secret_attr (Test *test, gconstpointer unused)
 
309
{
 
310
        GkmTransaction *transaction = gkm_transaction_new ();
 
311
        CK_ATTRIBUTE attr = { CKA_VALUE, "hello", 5 };
 
312
        gchar buffer[32];
 
313
        CK_ATTRIBUTE check = { CKA_VALUE, buffer, 32 };
 
314
        GkmSecretItem *item;
 
315
        CK_RV rv;
 
316
 
 
317
        unlock_collection (test);
 
318
 
 
319
        item = gkm_secret_collection_new_item (test->collection, "the-identifier");
 
320
        gkm_object_set_attribute (GKM_OBJECT (item), test->session, transaction, &attr);
 
321
        g_assert (gkm_transaction_get_failed (transaction) == FALSE);
 
322
        gkm_transaction_complete (transaction);
 
323
        g_assert (gkm_transaction_get_result (transaction) == CKR_OK);
 
324
 
 
325
        g_object_unref (transaction);
 
326
 
 
327
        rv = gkm_object_get_attribute (GKM_OBJECT (item), test->session, &check);
 
328
        g_assert (rv == CKR_OK);
 
329
        g_assert (check.ulValueLen == 5);
 
330
        g_assert (memcmp (buffer, "hello", 5) == 0);
 
331
}
 
332
 
 
333
static void
 
334
test_secret_attr_locked (Test *test, gconstpointer unused)
 
335
{
 
336
        GkmTransaction *transaction = gkm_transaction_new ();
 
337
        CK_ATTRIBUTE attr = { CKA_VALUE, "hello", 5 };
 
338
        gchar buffer[32];
 
339
        CK_ATTRIBUTE check = { CKA_VALUE, buffer, 32 };
 
340
        GkmSecretItem *item;
 
341
        CK_RV rv;
 
342
 
 
343
        item = gkm_secret_collection_new_item (test->collection, "the-identifier");
 
344
        gkm_object_set_attribute (GKM_OBJECT (item), test->session, transaction, &attr);
 
345
        g_assert (gkm_transaction_get_failed (transaction) == TRUE);
 
346
        gkm_transaction_complete (transaction);
 
347
        g_assert (gkm_transaction_get_result (transaction) == CKR_USER_NOT_LOGGED_IN);
 
348
 
 
349
        g_object_unref (transaction);
 
350
 
 
351
        rv = gkm_object_get_attribute (GKM_OBJECT (item), test->session, &check);
 
352
        g_assert (rv == CKR_USER_NOT_LOGGED_IN);
 
353
}
 
354
 
 
355
static void
 
356
test_fields_attr (Test *test, gconstpointer unused)
 
357
{
 
358
        GkmTransaction *transaction = gkm_transaction_new ();
 
359
        CK_ATTRIBUTE attr = { CKA_G_FIELDS, "name1\0value1\0name2\0value2", 26 };
 
360
        gchar buffer[32];
 
361
        CK_ATTRIBUTE check = { CKA_G_FIELDS, buffer, 32 };
 
362
        GkmSecretItem *item;
 
363
        GHashTable *fields;
 
364
        const gchar *value;
 
365
        CK_RV rv;
 
366
 
 
367
        unlock_collection (test);
 
368
 
 
369
        item = gkm_secret_collection_new_item (test->collection, "the-identifier");
 
370
        gkm_object_set_attribute (GKM_OBJECT (item), test->session, transaction, &attr);
 
371
        g_assert (gkm_transaction_get_failed (transaction) == FALSE);
 
372
        gkm_transaction_complete (transaction);
 
373
        g_assert (gkm_transaction_get_result (transaction) == CKR_OK);
 
374
 
 
375
        g_object_unref (transaction);
 
376
 
 
377
        rv = gkm_object_get_attribute (GKM_OBJECT (item), test->session, &check);
 
378
        g_assert (rv == CKR_OK);
 
379
        g_assert (check.ulValueLen == 26);
 
380
        g_assert (memcmp (buffer, "name1\0value1\0name2\0value2", 26) == 0);
 
381
 
 
382
        fields = gkm_secret_item_get_fields (item);
 
383
        g_assert (fields);
 
384
        value = gkm_secret_fields_get (fields, "name1");
 
385
        g_assert_cmpstr (value, ==, "value1");
 
386
        value = gkm_secret_fields_get (fields, "name2");
 
387
        g_assert_cmpstr (value, ==, "value2");
 
388
}
 
389
 
 
390
static void
 
391
test_fields_attr_locked (Test *test, gconstpointer unused)
 
392
{
 
393
        GkmTransaction *transaction = gkm_transaction_new ();
 
394
        CK_ATTRIBUTE attr = { CKA_G_FIELDS, "name1\0value1\0name2\0value2", 26 };
 
395
        GkmSecretItem *item;
 
396
 
 
397
        item = gkm_secret_collection_new_item (test->collection, "the-identifier");
 
398
        gkm_object_set_attribute (GKM_OBJECT (item), test->session, transaction, &attr);
 
399
        g_assert (gkm_transaction_get_failed (transaction) == TRUE);
 
400
        gkm_transaction_complete (transaction);
 
401
        g_assert (gkm_transaction_get_result (transaction) == CKR_USER_NOT_LOGGED_IN);
 
402
 
 
403
        g_object_unref (transaction);
 
404
}
 
405
 
 
406
static void
 
407
test_fields_attr_reverts (Test *test, gconstpointer unused)
 
408
{
 
409
        GkmTransaction *transaction = gkm_transaction_new ();
 
410
        CK_ATTRIBUTE attr = { CKA_G_FIELDS, "new\0value\0", 10 };
 
411
        gchar buffer[32];
 
412
        CK_ATTRIBUTE check = { CKA_G_FIELDS, buffer, 32 };
 
413
        GkmSecretItem *item;
 
414
        GHashTable *fields;
 
415
        CK_RV rv;
 
416
 
 
417
        unlock_collection (test);
 
418
 
 
419
        item = gkm_secret_collection_new_item (test->collection, "the-identifier");
 
420
 
 
421
        /* Set the old value like so */
 
422
        fields = gkm_secret_fields_new ();
 
423
        gkm_secret_fields_add (fields, "old", "value");
 
424
        gkm_secret_item_set_fields (item, fields);
 
425
        g_hash_table_unref (fields);
 
426
 
 
427
        /* Should show old value */
 
428
        rv = gkm_object_get_attribute (GKM_OBJECT (item), test->session, &check);
 
429
        g_assert (rv == CKR_OK);
 
430
        g_assert (check.ulValueLen == 10);
 
431
        g_assert (memcmp (buffer, "old\0value\0", 10) == 0);
 
432
 
 
433
        /* Set the new values */
 
434
        gkm_object_set_attribute (GKM_OBJECT (item), test->session, transaction, &attr);
 
435
        g_assert (gkm_transaction_get_failed (transaction) == FALSE);
 
436
 
 
437
        /* Should have the new value */
 
438
        rv = gkm_object_get_attribute (GKM_OBJECT (item), test->session, &check);
 
439
        g_assert (rv == CKR_OK);
 
440
        g_assert (check.ulValueLen == 10);
 
441
        g_assert (memcmp (buffer, "new\0value\0", 10) == 0);
 
442
 
 
443
        /* Fail the transaction */
 
444
        gkm_transaction_fail (transaction, CKR_CANCEL);
 
445
        gkm_transaction_complete (transaction);
 
446
 
 
447
        /* Should show the old value */
 
448
        rv = gkm_object_get_attribute (GKM_OBJECT (item), test->session, &check);
 
449
        g_assert (rv == CKR_OK);
 
450
        g_assert (check.ulValueLen == 10);
 
451
        g_assert (memcmp (buffer, "old\0value\0", 10) == 0);
 
452
 
 
453
        g_object_unref (transaction);
 
454
}
 
455
 
 
456
int
 
457
main (int argc, char **argv)
 
458
{
 
459
        g_type_init ();
 
460
        g_test_init (&argc, &argv, NULL);
 
461
 
 
462
        g_test_add ("/secret-store/item/new", Test, NULL, setup, test_new, teardown);
 
463
        g_test_add ("/secret-store/item/create", Test, NULL, setup, test_create, teardown);
 
464
        g_test_add ("/secret-store/item/create_failed", Test, NULL, setup, test_create_failed, teardown);
 
465
        g_test_add ("/secret-store/item/destroy", Test, NULL, setup, test_destroy, teardown);
 
466
        g_test_add ("/secret-store/item/destroy_failed", Test, NULL, setup, test_destroy_failed, teardown);
 
467
        g_test_add ("/secret-store/item/collection_get", Test, NULL, setup, test_collection_get, teardown);
 
468
        g_test_add ("/secret-store/item/collection_items", Test, NULL, setup, test_collection_items, teardown);
 
469
        g_test_add ("/secret-store/item/collection_remove", Test, NULL, setup, test_collection_remove, teardown);
 
470
        g_test_add ("/secret-store/item/is_locked", Test, NULL, setup, test_is_locked, teardown);
 
471
        g_test_add ("/secret-store/item/get_collection", Test, NULL, setup, test_get_collection, teardown);
 
472
        g_test_add ("/secret-store/item/tracks_collection", Test, NULL, setup, test_tracks_collection, teardown);
 
473
        g_test_add ("/secret-store/item/get_set_fields", Test, NULL, setup, test_get_set_fields, teardown);
 
474
        g_test_add ("/secret-store/item/collection_attr", Test, NULL, setup, test_collection_attr, teardown);
 
475
        g_test_add ("/secret-store/item/secret_attr", Test, NULL, setup, test_secret_attr, teardown);
 
476
        g_test_add ("/secret-store/item/secret_attr_locked", Test, NULL, setup, test_secret_attr_locked, teardown);
 
477
        g_test_add ("/secret-store/item/fields_attr", Test, NULL, setup, test_fields_attr, teardown);
 
478
        g_test_add ("/secret-store/item/fields_attr_locked", Test, NULL, setup, test_fields_attr_locked, teardown);
 
479
        g_test_add ("/secret-store/item/fields_attr_reverts", Test, NULL, setup, test_fields_attr_reverts, teardown);
 
480
 
 
481
        return g_test_run ();
 
482
}