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

« back to all changes in this revision

Viewing changes to pkcs11/secret-store/tests/unit-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 "test-suite.h"
27
 
#include "test-secret-module.h"
28
 
 
29
 
#include "gkm-secret-collection.h"
30
 
#include "gkm-secret-fields.h"
31
 
#include "gkm-secret-item.h"
32
 
 
33
 
#include "gkm/gkm-credential.h"
34
 
#include "gkm/gkm-session.h"
35
 
#include "gkm/gkm-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 GkmModule *module = NULL;
46
 
static GkmSession *session = NULL;
47
 
static GkmSecretCollection *collection = NULL;
48
 
 
49
 
TESTING_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 (GKM_TYPE_SECRET_COLLECTION,
55
 
                                   "module", module,
56
 
                                   "identifier", "test",
57
 
                                   NULL);
58
 
}
59
 
 
60
 
TESTING_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
 
        GkmCredential *cred;
76
 
        GkmObject *object;
77
 
        CK_RV rv;
78
 
 
79
 
        /* Create credential, which unlocks collection */
80
 
        object = GKM_OBJECT (collection);
81
 
        rv = gkm_credential_create (gkm_object_get_module (object),
82
 
                                    gkm_session_get_manager (session),
83
 
                                    object, NULL, 0, &cred);
84
 
        g_assert (rv == CKR_OK);
85
 
 
86
 
        gkm_session_add_session_object (session, NULL, GKM_OBJECT (cred));
87
 
        g_object_unref (cred);
88
 
}
89
 
 
90
 
TESTING_TEST(secret_item_new)
91
 
{
92
 
        GkmSecretItem *item;
93
 
 
94
 
        item = gkm_secret_collection_new_item (collection, "the-identifier");
95
 
        g_assert (GKM_IS_SECRET_ITEM (item));
96
 
        g_assert_cmpstr (gkm_secret_object_get_identifier (GKM_SECRET_OBJECT (item)), ==, "the-identifier");
97
 
}
98
 
 
99
 
TESTING_TEST(secret_item_create)
100
 
{
101
 
        GkmTransaction *transaction;
102
 
        GkmSecretItem *item;
103
 
 
104
 
        transaction = gkm_transaction_new ();
105
 
        item = gkm_secret_collection_create_item (collection, transaction);
106
 
        g_assert (GKM_IS_SECRET_ITEM (item));
107
 
        g_object_ref (item);
108
 
        g_assert (gkm_secret_collection_has_item (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 (collection, item));
115
 
        g_object_unref (item);
116
 
}
117
 
 
118
 
TESTING_TEST(secret_item_create_failed)
119
 
{
120
 
        GkmTransaction *transaction;
121
 
        GkmSecretItem *item;
122
 
 
123
 
        transaction = gkm_transaction_new ();
124
 
        item = gkm_secret_collection_create_item (collection, transaction);
125
 
        g_assert (GKM_IS_SECRET_ITEM (item));
126
 
        g_object_ref (item);
127
 
        g_assert (gkm_secret_collection_has_item (collection, item));
128
 
 
129
 
        gkm_transaction_fail (transaction, CKR_GENERAL_ERROR);
130
 
        gkm_transaction_complete (transaction);
131
 
        g_object_unref (transaction);
132
 
 
133
 
        /* Should no longer be there */
134
 
        g_assert (!gkm_secret_collection_has_item (collection, item));
135
 
        g_object_unref (item);
136
 
}
137
 
 
138
 
TESTING_TEST(secret_item_destroy)
139
 
{
140
 
        GkmTransaction *transaction;
141
 
        GkmSecretItem *item;
142
 
 
143
 
        item = gkm_secret_collection_new_item (collection, "the-identifier");
144
 
        g_assert (gkm_secret_collection_has_item (collection, item));
145
 
        g_object_ref (item);
146
 
 
147
 
        transaction = gkm_transaction_new ();
148
 
        gkm_secret_collection_destroy_item (collection, transaction, item);
149
 
        g_assert (!gkm_secret_collection_has_item (collection, item));
150
 
 
151
 
        gkm_transaction_complete (transaction);
152
 
        g_object_unref (transaction);
153
 
 
154
 
        /* Should not be there */
155
 
        g_assert (!gkm_secret_collection_has_item (collection, item));
156
 
        g_object_unref (item);
157
 
}
158
 
 
159
 
TESTING_TEST(secret_item_destroy_failed)
160
 
{
161
 
        GkmTransaction *transaction;
162
 
        GkmSecretItem *item;
163
 
 
164
 
        item = gkm_secret_collection_new_item (collection, "the-identifier");
165
 
        g_assert (gkm_secret_collection_has_item (collection, item));
166
 
        g_object_ref (item);
167
 
 
168
 
        transaction = gkm_transaction_new ();
169
 
        gkm_secret_collection_destroy_item (collection, transaction, item);
170
 
        g_assert (!gkm_secret_collection_has_item (collection, item));
171
 
 
172
 
        gkm_transaction_fail (transaction, CKR_GENERAL_ERROR);
173
 
        gkm_transaction_complete (transaction);
174
 
        g_object_unref (transaction);
175
 
 
176
 
        /* Should be there */
177
 
        g_assert (gkm_secret_collection_has_item (collection, item));
178
 
        g_object_unref (item);
179
 
}
180
 
 
181
 
TESTING_TEST(secret_item_collection_get)
182
 
{
183
 
        GkmSecretItem *item, *check;
184
 
 
185
 
        item = gkm_secret_collection_new_item (collection, "the-identifier");
186
 
        g_assert (GKM_IS_SECRET_ITEM (item));
187
 
 
188
 
        check = gkm_secret_collection_get_item (collection, "the-identifier");
189
 
        g_assert (item == check);
190
 
}
191
 
 
192
 
TESTING_TEST(secret_item_collection_items)
193
 
{
194
 
        GList *l, *items;
195
 
        const gchar *identifier;
196
 
 
197
 
        gkm_secret_collection_new_item (collection, "one-identifier");
198
 
        gkm_secret_collection_new_item (collection, "two-identifier");
199
 
        gkm_secret_collection_new_item (collection, "three-identifier");
200
 
 
201
 
        items = gkm_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 = gkm_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
 
TESTING_TEST(secret_item_collection_remove)
215
 
{
216
 
        GkmSecretItem *item;
217
 
 
218
 
        item = gkm_secret_collection_new_item (collection, "the-identifier");
219
 
        g_assert (gkm_secret_collection_get_item (collection, "the-identifier") == item);
220
 
 
221
 
        gkm_secret_collection_remove_item (collection, item);
222
 
        g_assert (gkm_secret_collection_get_item (collection, "the-identifier") == NULL);
223
 
}
224
 
 
225
 
TESTING_TEST(secret_item_is_locked)
226
 
{
227
 
        GkmSecretItem *item;
228
 
 
229
 
        item = gkm_secret_collection_new_item (collection, "the-identifier");
230
 
        g_assert (gkm_secret_object_is_locked (GKM_SECRET_OBJECT (item), session) ==
231
 
                  gkm_secret_object_is_locked (GKM_SECRET_OBJECT (collection), session));
232
 
 
233
 
        unlock_collection();
234
 
 
235
 
        g_assert (gkm_secret_object_is_locked (GKM_SECRET_OBJECT (item), session) == FALSE);
236
 
}
237
 
 
238
 
TESTING_TEST(secret_item_get_collection)
239
 
{
240
 
        GkmSecretItem *item;
241
 
        item = gkm_secret_collection_new_item (collection, "the-identifier");
242
 
        g_assert (gkm_secret_item_get_collection (item) == collection);
243
 
}
244
 
 
245
 
TESTING_TEST(secret_item_tracks_collection)
246
 
{
247
 
        GkmSecretItem *item;
248
 
        item = gkm_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 (gkm_secret_object_is_locked (GKM_SECRET_OBJECT (item), session) == FALSE);
255
 
 
256
 
        g_object_unref (collection);
257
 
        collection = NULL;
258
 
 
259
 
        /* Collection went away */
260
 
        g_assert (gkm_secret_item_get_collection (item) == NULL);
261
 
        g_assert (gkm_secret_object_is_locked (GKM_SECRET_OBJECT (item), session) == TRUE);
262
 
 
263
 
        g_object_unref (item);
264
 
}
265
 
 
266
 
TESTING_TEST(secret_item_get_set_fields)
267
 
{
268
 
        GHashTable *fields = gkm_secret_fields_new ();
269
 
        GHashTable *check;
270
 
        GkmSecretItem *item;
271
 
 
272
 
        item = gkm_secret_collection_new_item (collection, "the-identifier");
273
 
        gkm_secret_item_set_fields (item, fields);
274
 
        gkm_secret_item_set_fields (item, fields);
275
 
 
276
 
        check = gkm_secret_item_get_fields (item);
277
 
        g_assert (check == fields);
278
 
 
279
 
        g_hash_table_unref (fields);
280
 
}
281
 
 
282
 
TESTING_TEST(secret_item_collection_attr)
283
 
{
284
 
        gchar buffer[32];
285
 
        CK_ATTRIBUTE check = { CKA_G_COLLECTION, buffer, 32 };
286
 
        GkmSecretItem *item;
287
 
        CK_RV rv;
288
 
 
289
 
        item = gkm_secret_collection_new_item (collection, "the-identifier");
290
 
        rv = gkm_object_get_attribute (GKM_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
 
TESTING_TEST(secret_item_secret_attr)
297
 
{
298
 
        GkmTransaction *transaction = gkm_transaction_new ();
299
 
        CK_ATTRIBUTE attr = { CKA_VALUE, "hello", 5 };
300
 
        gchar buffer[32];
301
 
        CK_ATTRIBUTE check = { CKA_VALUE, buffer, 32 };
302
 
        GkmSecretItem *item;
303
 
        CK_RV rv;
304
 
 
305
 
        unlock_collection ();
306
 
 
307
 
        item = gkm_secret_collection_new_item (collection, "the-identifier");
308
 
        gkm_object_set_attribute (GKM_OBJECT (item), session, transaction, &attr);
309
 
        g_assert (gkm_transaction_get_failed (transaction) == FALSE);
310
 
        gkm_transaction_complete (transaction);
311
 
        g_assert (gkm_transaction_get_result (transaction) == CKR_OK);
312
 
 
313
 
        g_object_unref (transaction);
314
 
 
315
 
        rv = gkm_object_get_attribute (GKM_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
 
TESTING_TEST(secret_item_secret_attr_locked)
322
 
{
323
 
        GkmTransaction *transaction = gkm_transaction_new ();
324
 
        CK_ATTRIBUTE attr = { CKA_VALUE, "hello", 5 };
325
 
        gchar buffer[32];
326
 
        CK_ATTRIBUTE check = { CKA_VALUE, buffer, 32 };
327
 
        GkmSecretItem *item;
328
 
        CK_RV rv;
329
 
 
330
 
        item = gkm_secret_collection_new_item (collection, "the-identifier");
331
 
        gkm_object_set_attribute (GKM_OBJECT (item), session, transaction, &attr);
332
 
        g_assert (gkm_transaction_get_failed (transaction) == TRUE);
333
 
        gkm_transaction_complete (transaction);
334
 
        g_assert (gkm_transaction_get_result (transaction) == CKR_USER_NOT_LOGGED_IN);
335
 
 
336
 
        g_object_unref (transaction);
337
 
 
338
 
        rv = gkm_object_get_attribute (GKM_OBJECT (item), session, &check);
339
 
        g_assert (rv == CKR_USER_NOT_LOGGED_IN);
340
 
}
341
 
 
342
 
TESTING_TEST(secret_item_fields_attr)
343
 
{
344
 
        GkmTransaction *transaction = gkm_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
 
        GkmSecretItem *item;
349
 
        GHashTable *fields;
350
 
        const gchar *value;
351
 
        CK_RV rv;
352
 
 
353
 
        unlock_collection ();
354
 
 
355
 
        item = gkm_secret_collection_new_item (collection, "the-identifier");
356
 
        gkm_object_set_attribute (GKM_OBJECT (item), session, transaction, &attr);
357
 
        g_assert (gkm_transaction_get_failed (transaction) == FALSE);
358
 
        gkm_transaction_complete (transaction);
359
 
        g_assert (gkm_transaction_get_result (transaction) == CKR_OK);
360
 
 
361
 
        g_object_unref (transaction);
362
 
 
363
 
        rv = gkm_object_get_attribute (GKM_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 = gkm_secret_item_get_fields (item);
369
 
        g_assert (fields);
370
 
        value = gkm_secret_fields_get (fields, "name1");
371
 
        g_assert_cmpstr (value, ==, "value1");
372
 
        value = gkm_secret_fields_get (fields, "name2");
373
 
        g_assert_cmpstr (value, ==, "value2");
374
 
}
375
 
 
376
 
TESTING_TEST(secret_item_fields_attr_locked)
377
 
{
378
 
        GkmTransaction *transaction = gkm_transaction_new ();
379
 
        CK_ATTRIBUTE attr = { CKA_G_FIELDS, "name1\0value1\0name2\0value2", 26 };
380
 
        GkmSecretItem *item;
381
 
 
382
 
        item = gkm_secret_collection_new_item (collection, "the-identifier");
383
 
        gkm_object_set_attribute (GKM_OBJECT (item), session, transaction, &attr);
384
 
        g_assert (gkm_transaction_get_failed (transaction) == TRUE);
385
 
        gkm_transaction_complete (transaction);
386
 
        g_assert (gkm_transaction_get_result (transaction) == CKR_USER_NOT_LOGGED_IN);
387
 
 
388
 
        g_object_unref (transaction);
389
 
}
390
 
 
391
 
TESTING_TEST(secret_item_fields_attr_reverts)
392
 
{
393
 
        GkmTransaction *transaction = gkm_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
 
        GkmSecretItem *item;
398
 
        GHashTable *fields;
399
 
        CK_RV rv;
400
 
 
401
 
        unlock_collection ();
402
 
 
403
 
        item = gkm_secret_collection_new_item (collection, "the-identifier");
404
 
 
405
 
        /* Set the old value like so */
406
 
        fields = gkm_secret_fields_new ();
407
 
        gkm_secret_fields_add (fields, "old", "value");
408
 
        gkm_secret_item_set_fields (item, fields);
409
 
        g_hash_table_unref (fields);
410
 
 
411
 
        /* Should show old value */
412
 
        rv = gkm_object_get_attribute (GKM_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
 
        gkm_object_set_attribute (GKM_OBJECT (item), session, transaction, &attr);
419
 
        g_assert (gkm_transaction_get_failed (transaction) == FALSE);
420
 
 
421
 
        /* Should have the new value */
422
 
        rv = gkm_object_get_attribute (GKM_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
 
        gkm_transaction_fail (transaction, CKR_CANCEL);
429
 
        gkm_transaction_complete (transaction);
430
 
 
431
 
        /* Should show the old value */
432
 
        rv = gkm_object_get_attribute (GKM_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
 
}